@@ -21,7 +21,7 @@ pip install pytorch2keras
21
21
To use the converter properly, please, make changes in your ` ~/.keras/keras.json ` :
22
22
23
23
24
- ```
24
+ ``` json
25
25
...
26
26
"backend" : " tensorflow" ,
27
27
"image_data_format" : " channels_first" ,
@@ -36,13 +36,13 @@ Here is a short instruction how to get a tensorflow.js model:
36
36
37
37
1 . First of all, you have to convert your model to Keras with this converter:
38
38
39
- ```
39
+ ``` python
40
40
k_model = pytorch_to_keras(model, input_var, [(10 , 32 , 32 ,)], verbose = True , names = ' short' )
41
41
```
42
42
43
43
2 . Now you have Keras model. You can save it as h5 file and then convert it with ` tensorflowjs_converter ` but it doesn't work sometimes. As alternative, you may get Tensorflow Graph and save it as a frozen model:
44
44
45
- ```
45
+ ``` python
46
46
# Function below copied from here:
47
47
# https://stackoverflow.com/questions/45466020/how-to-export-keras-h5-to-tensorflow-pb
48
48
def freeze_session (session , keep_var_names = None , output_names = None , clear_devices = True ):
@@ -88,7 +88,7 @@ print([i for i in k_model.outputs])
88
88
89
89
3 . You will see the output layer name, so, now it's time to convert ` my_model.pb ` to tfjs model:
90
90
91
- ```
91
+ ``` bash
92
92
tensorflowjs_converter \
93
93
--input_format=tf_frozen_model \
94
94
--output_node_names=' TANHTObs/Tanh' \
@@ -98,10 +98,10 @@ tensorflowjs_converter \
98
98
99
99
4 . Thats all!
100
100
101
- ```
101
+ ``` js
102
102
const MODEL_URL = ` model_tfjs/tensorflowjs_model.pb` ;
103
103
const WEIGHTS_URL = ` model_tfjs/weights_manifest.json` ;
104
- cont model = await tf.loadFrozenModel(MODEL_URL, WEIGHTS_URL);
104
+ const model = await tf .loadFrozenModel (MODEL_URL , WEIGHTS_URL );
105
105
```
106
106
107
107
## How to use
@@ -110,7 +110,7 @@ It's the converter of PyTorch graph to a Keras (Tensorflow backend) model.
110
110
111
111
Firstly, we need to load (or create) a valid PyTorch model:
112
112
113
- ```
113
+ ``` python
114
114
class TestConv2d (nn .Module ):
115
115
"""
116
116
Module for Conv2d testing
@@ -132,22 +132,22 @@ model = TestConv2d()
132
132
133
133
The next step - create a dummy variable with correct shape:
134
134
135
- ```
135
+ ``` python
136
136
input_np = np.random.uniform(0 , 1 , (1 , 10 , 32 , 32 ))
137
137
input_var = Variable(torch.FloatTensor(input_np))
138
138
```
139
139
140
140
We use the dummy-variable to trace the model (with jit.trace):
141
141
142
- ```
142
+ ``` python
143
143
from pytorch2keras import pytorch_to_keras
144
144
# we should specify shape of the input tensor
145
145
k_model = pytorch_to_keras(model, input_var, [(10 , 32 , 32 ,)], verbose = True )
146
146
```
147
147
148
148
You can also set H and W dimensions to None to make your model shape-agnostic (e.g. fully convolutional netowrk):
149
149
150
- ```
150
+ ``` python
151
151
from pytorch2keras.converter import pytorch_to_keras
152
152
# we should specify shape of the input tensor
153
153
k_model = pytorch_to_keras(model, input_var, [(10 , None , None ,)], verbose = True )
@@ -160,7 +160,7 @@ That's all! If all the modules have converted properly, the Keras model will be
160
160
161
161
Here is the only method ` pytorch_to_keras ` from ` pytorch2keras ` module.
162
162
163
- ```
163
+ ``` python
164
164
def pytorch_to_keras (
165
165
model , args , input_shapes = None ,
166
166
change_ordering = False , verbose = False , name_policy = None ,
0 commit comments