Package: pytorch2keras
pytorch2kerasPyTorch to Keras model converter.Installationpip install pytorch2kerasImportant noticeTo use the converter properly, please, make changes in your~/.keras/keras.json:..."backend":"tensorflow","image_data_format":"channels_first",...Tensorflow.jsFor the proper conversion to a tensorflow.js format, please use the new flagnames='short'.Here is a short instruction how to get a tensorflow.js model:First of all, you have to convert your model to Keras with this converter:k_model=pytorch_to_keras(model,input_var,[(10,32,32,)],verbose=True,names='short')Now you have Keras model. You can save it as h5 file and then convert it withtensorflowjs_converterbut it doesn't work sometimes. As alternative, you may get Tensorflow Graph and save it as a frozen model:# Function below copied from here:# https://stackoverflow.com/questions/45466020/how-to-export-keras-h5-to-tensorflow-pbdeffreeze_session(session,keep_var_names=None,output_names=None,clear_devices=True):"""Freezes the state of a session into a pruned computation graph.Creates a new computation graph where variable nodes are replaced byconstants taking their current value in the session. The new graph will bepruned so subgraphs that are not necessary to compute the requestedoutputs are removed.@param session The TensorFlow session to be frozen.@param keep_var_names A list of variable names that should not be frozen,or None to freeze all the variables in the graph.@param output_names Names of the relevant graph outputs.@param clear_devices Remove the device directives from the graph for better portability.@return The frozen graph definition."""fromtensorflow.python.framework.graph_utilimportconvert_variables_to_constantsgraph=session.graphwithgraph.as_default():freeze_var_names=\list(set(v.op.nameforvintf.global_variables()).difference(keep_var_namesor[]))output_names=output_namesor[]output_names+=[v.op.nameforvintf.global_variables()]input_graph_def=graph.as_graph_def()ifclear_devices:fornodeininput_graph_def.node:node.device=""frozen_graph=convert_variables_to_constants(session,input_graph_def,output_names,freeze_var_names)returnfrozen_graphfromkerasimportbackendasKimporttensorflowastffrozen_graph=freeze_session(K.get_session(),output_names=[out.op.nameforoutink_model.outputs])tf.train.write_graph(frozen_graph,".","my_model.pb",as_text=False)print([iforiink_model.outputs])You will see the output layer name, so, now it's time to convertmy_model.pbto tfjs model:tensorflowjs_converter\--input_format=tf_frozen_model\--output_node_names='TANHTObs/Tanh'\my_model.pb\model_tfjsThats all!constMODEL_URL=`model_tfjs/tensorflowjs_model.pb`;constWEIGHTS_URL=`model_tfjs/weights_manifest.json`;constmodel=awaittf.loadFrozenModel(MODEL_URL,WEIGHTS_URL);How to useIt's the converter of PyTorch graph to a Keras (Tensorflow backend) model.Firstly, we need to load (or create) a valid PyTorch model:classTestConv2d(nn.Module):"""Module for Conv2d testing"""def__init__(self,inp=10,out=16,kernel_size=3):super(TestConv2d,self).__init__()self.conv2d=nn.Conv2d(inp,out,stride=1,kernel_size=kernel_size,bias=True)defforward(self,x):x=self.conv2d(x)returnxmodel=TestConv2d()# load weights here# model.load_state_dict(torch.load(path_to_weights.pth))The next step - create a dummy variable with correct shape:input_np=np.random.uniform(0,1,(1,10,32,32))input_var=Variable(torch.FloatTensor(input_np))We use the dummy-variable to trace the model (with jit.trace):frompytorch2kerasimportpytorch_to_keras# we should specify shape of the input tensork_model=pytorch_to_keras(model,input_var,[(10,32,32,)],verbose=True)You can also set H and W dimensions to None to make your model shape-agnostic (e.g. fully convolutional netowrk):frompytorch2keras.converterimportpytorch_to_keras# we should specify shape of the input tensork_model=pytorch_to_keras(model,input_var,[(10,None,None,)],verbose=True)That's all! If all the modules have converted properly, the Keras model will be stored in thek_modelvariable.APIHere is the only methodpytorch_to_kerasfrompytorch2kerasmodule.defpytorch_to_keras(model,args,input_shapes=None,change_ordering=False,verbose=False,name_policy=None,):Options:model- a PyTorch model (nn.Module) to convert;args- a list of dummy variables with proper shapes;input_shapes- (experimental) list with overrided shapes for inputs;change_ordering- (experimental) boolean, if enabled, the converter will try to changeBCHWtoBHWCverbose- boolean, detailed log of conversionname_policy- (experimental) choice from [keep,short,random]. The selector set the target layer naming policy.Supported layersActivations:ReLULeakyReLUSELUSigmoidSoftmaxTanhConstantsConvolutions:Conv2dConvTrsnpose2dElement-wise:AddMulSubDivLinearNormalizations:BatchNorm2dInstanceNorm2dPoolings:MaxPool2dAvgPool2dGlobal MaxPool2d (adaptive pooling to shape [1, 1])Models converted with pytorch2kerasResNet*VGG*PreResNet*DenseNet*AlexNetMobilenet v2UsageLook at thetestsdirectory.LicenseThis software is covered by MIT License.