tensorflow/tools/graph_transforms/fold_old_batch_norms.cc
// Finds monolithic batch norm ops (as used in early versions of TensorFlow) and
// converts them into premultiplied weight inputs to convolutions.
Finds monolithic batch norm ops (as used in early versions of TensorFlow) and
converts them into premultiplied weight inputs to convolutions.
Finds monolithic batch norm ops ( as used in early versions of TensorFlow ) and converts them into premultiplied weight inputs to convolutions .
Status FoldOldBatchNorms(const GraphDef& input_graph_def,
                         const TransformFuncContext& context,
                         GraphDef* output_graph_def) {
  GraphDef current_graph_def = input_graph_def;
  bool did_graph_change;
  do {
    did_graph_change = false;
    GraphDef replaced_graph_def;
    TF_RETURN_IF_ERROR(ReplaceMatchingOpTypes(
        current_graph_def,  
      {"BatchNormWithGlobalNormalization|FusedBatchNorm",    
        {
          {"Conv2D|DepthwiseConv2dNative",                          
            {
              {"*"},                          
              {"Const"},                      
            }
          },
          {"Const"},                          
          {"Const"},                          
          {"Const"},                          
          {"Const"},                          
        }
      },  
        [&did_graph_change](const NodeMatch& match,
                            const std::set<string>& input_nodes,
                            const std::set<string>& output_nodes,
                            std::vector<NodeDef>* new_nodes) {
          TF_RETURN_IF_ERROR(FuseBatchNormWithConv(match, new_nodes));
          did_graph_change = true;
          return Status::OK();
        },
        {}, &replaced_graph_def));
    current_graph_def = replaced_graph_def;
  } while (did_graph_change);
  do {
    did_graph_change = false;
    GraphDef replaced_graph_def;
    TF_RETURN_IF_ERROR(ReplaceMatchingOpTypes(
        current_graph_def,  
        {"BatchNormWithGlobalNormalization|FusedBatchNorm",    
         {
             {"BatchToSpaceND",                  
              {
                  {"Conv2D|DepthwiseConv2dNative",                     
                   {
                       {"*"},                    
                       {"Const"},                
                   }
                  },
                  {"Const"},                     
                  {"Const"},                     
              }
             },
             {"Const"},                          
             {"Const"},                          
             {"Const"},                          
             {"Const"},                          
         }
        },  
        [&did_graph_change](const NodeMatch& match,
                            const std::set<string>& input_nodes,
                            const std::set<string>& output_nodes,
                            std::vector<NodeDef>* new_nodes) {
          TF_RETURN_IF_ERROR(FuseBatchNormWithBatchToSpace(match, new_nodes));
          did_graph_change = true;
          return Status::OK();
        },
        {}, &replaced_graph_def));
    current_graph_def = replaced_graph_def;
  } while (did_graph_change);
  do {
    did_graph_change = false;
    GraphDef replaced_graph_def;
    TF_RETURN_IF_ERROR(ReplaceMatchingOpTypes(
        current_graph_def,  
      {"BatchNormWithGlobalNormalization|FusedBatchNorm",    
        {
          {"ConcatV2|Concat",                     
            {
              {"Conv2D|DepthwiseConv2dNative",                          
                {
                  {"*"},                          
                  {"Const"},                      
                }
              },
              {"Conv2D|DepthwiseConv2dNative",                          
                {
                  {"*"},                          
                  {"Const"},                      
                }
              },
              {"Const"},                          
            },
          },
          {"Const"},                          
          {"Const"},                          
          {"Const"},                          
          {"Const"},                          
        }
      },  
        [&did_graph_change](const NodeMatch& match,
                            const std::set<string>& input_nodes,
                            const std::set<string>& output_nodes,
                            std::vector<NodeDef>* new_nodes) {
          TF_RETURN_IF_ERROR(FuseBatchNormWithConvConcat(match, new_nodes));
          did_graph_change = true;
          return Status::OK();
        },
        {}, &replaced_graph_def));
    current_graph_def = replaced_graph_def;
  } while (did_graph_change);
  *output_graph_def = current_graph_def;
  return Status::OK();
}
Status FoldOldBatchNorms ( const GraphDef & input_graph_def , const TransformFuncContext & context , GraphDef * output_graph_def ) { GraphDef current_graph_def = input_graph_def ; bool did_graph_change ; do { did_graph_change = false ; GraphDef replaced_graph_def ; TF_RETURN_IF_ERROR ( ReplaceMatchingOpTypes ( current_graph_def , { " " , { { " " , { { " " } , { " " } , } } , { " " } , { " " } , { " " } , { " " } , } } , [ & did_graph_change ] ( const NodeMatch & match , const std :: set < string > & input_nodes , const std :: set < string > & output_nodes , std :: vector < NodeDef > * new_nodes ) { TF_RETURN_IF_ERROR ( FuseBatchNormWithConv ( match , new_nodes ) ) ; did_graph_change = true ; return Status :: OK ( ) ; } , { } , & replaced_graph_def ) ) ; current_graph_def = replaced_graph_def ; } while ( did_graph_change ) ; do { did_graph_change = false ; GraphDef replaced_graph_def ; TF_RETURN_IF_ERROR ( ReplaceMatchingOpTypes ( current_graph_def , { " " , { { " " , { { " " , { { " " } , { " " } , } } , { " " } , { " " } , } } , { " " } , { " " } , { " " } , { " " } , } } , [ & did_graph_change ] ( const NodeMatch & match , const std :: set < string > & input_nodes , const std :: set < string > & output_nodes , std :: vector < NodeDef > * new_nodes ) { TF_RETURN_IF_ERROR ( FuseBatchNormWithBatchToSpace ( match , new_nodes ) ) ; did_graph_change = true ; return Status :: OK ( ) ; } , { } , & replaced_graph_def ) ) ; current_graph_def = replaced_graph_def ; } while ( did_graph_change ) ; do { did_graph_change = false ; GraphDef replaced_graph_def ; TF_RETURN_IF_ERROR ( ReplaceMatchingOpTypes ( current_graph_def , { " " , { { " " , { { " " , { { " " } , { " " } , } } , { " " , { { " " } , { " " } , } } , { " " } , } , } , { " " } , { " " } , { " " } , { " " } , } } , [ & did_graph_change ] ( const NodeMatch & match , const std :: set < string > & input_nodes , const std :: set < string > & output_nodes , std :: vector < NodeDef > * new_nodes ) { TF_RETURN_IF_ERROR ( FuseBatchNormWithConvConcat ( match , new_nodes ) ) ; did_graph_change = true ; return Status :: OK ( ) ; } , { } , & replaced_graph_def ) ) ; current_graph_def = replaced_graph_def ; } while ( did_graph_change ) ; * output_graph_def = current_graph_def ; return Status :: OK ( ) ; }
Finds monolithic batch norm ops (as used in early versions of TensorFlow) and
converts them into premultiplied weight inputs to convolutions.
Finds monolithic batch norm ops ( as used in early versions of TensorFlow ) and converts them into premultiplied weight inputs to convolutions .
// We have to do several passes to catch all the old BN nodes, since many of // them may share inputs and so be excluded from replacement in one pass. // clang-format off // batch_norm_node // conv_node // input_node // weights_node // mean_node // variance_node // beta_node // gamma_node // clang-format on // clang-format off // batch_norm_node // batch_to_space_node // conv_node // input_node // weights_node // block_shape // crops // mean_node // variance_node // beta_node // gamma_node // clang-format on // Replace BatchNorm with concat as input. // clang-format off // batch_norm_node // concat two conv2d. // conv_node // input_node // weights_node // conv_node // input_node // weights_node // axis // mean_node // variance_node // beta_node // gamma_node // clang-format on