Skip to content

Commit 8ab35b5

Browse files
authored
Merge pull request #90 from kittinan/master
Update README.md to highlight code blocks and fix missing typo
2 parents b31ccda + 8bf5048 commit 8ab35b5

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pip install pytorch2keras
2121
To use the converter properly, please, make changes in your `~/.keras/keras.json`:
2222

2323

24-
```
24+
```json
2525
...
2626
"backend": "tensorflow",
2727
"image_data_format": "channels_first",
@@ -36,13 +36,13 @@ Here is a short instruction how to get a tensorflow.js model:
3636

3737
1. First of all, you have to convert your model to Keras with this converter:
3838

39-
```
39+
```python
4040
k_model = pytorch_to_keras(model, input_var, [(10, 32, 32,)], verbose=True, names='short')
4141
```
4242

4343
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:
4444

45-
```
45+
```python
4646
# Function below copied from here:
4747
# https://stackoverflow.com/questions/45466020/how-to-export-keras-h5-to-tensorflow-pb
4848
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])
8888

8989
3. You will see the output layer name, so, now it's time to convert `my_model.pb` to tfjs model:
9090

91-
```
91+
```bash
9292
tensorflowjs_converter \
9393
--input_format=tf_frozen_model \
9494
--output_node_names='TANHTObs/Tanh' \
@@ -98,10 +98,10 @@ tensorflowjs_converter \
9898

9999
4. Thats all!
100100

101-
```
101+
```js
102102
const MODEL_URL = `model_tfjs/tensorflowjs_model.pb`;
103103
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);
105105
```
106106

107107
## How to use
@@ -110,7 +110,7 @@ It's the converter of PyTorch graph to a Keras (Tensorflow backend) model.
110110

111111
Firstly, we need to load (or create) a valid PyTorch model:
112112

113-
```
113+
```python
114114
class TestConv2d(nn.Module):
115115
"""
116116
Module for Conv2d testing
@@ -132,22 +132,22 @@ model = TestConv2d()
132132

133133
The next step - create a dummy variable with correct shape:
134134

135-
```
135+
```python
136136
input_np = np.random.uniform(0, 1, (1, 10, 32, 32))
137137
input_var = Variable(torch.FloatTensor(input_np))
138138
```
139139

140140
We use the dummy-variable to trace the model (with jit.trace):
141141

142-
```
142+
```python
143143
from pytorch2keras import pytorch_to_keras
144144
# we should specify shape of the input tensor
145145
k_model = pytorch_to_keras(model, input_var, [(10, 32, 32,)], verbose=True)
146146
```
147147

148148
You can also set H and W dimensions to None to make your model shape-agnostic (e.g. fully convolutional netowrk):
149149

150-
```
150+
```python
151151
from pytorch2keras.converter import pytorch_to_keras
152152
# we should specify shape of the input tensor
153153
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
160160

161161
Here is the only method `pytorch_to_keras` from `pytorch2keras` module.
162162

163-
```
163+
```python
164164
def pytorch_to_keras(
165165
model, args, input_shapes=None,
166166
change_ordering=False, verbose=False, name_policy=None,

0 commit comments

Comments
 (0)