-
Notifications
You must be signed in to change notification settings - Fork 17
/
pixelshuffle.py
executable file
·72 lines (53 loc) · 1.91 KB
/
pixelshuffle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""
import torch
from torch import nn
import tensorflow as tf
from tensorflow.lite.python.lite import TFLiteConverter
import keras
import nobuco
from nobuco import ChannelOrder, ChannelOrderingStrategy
from nobuco.layers.weight import WeightLayer
from nobuco.addons.torch.depth_to_space import DepthToSpace
from nobuco.addons.torch.space_to_depth import SpaceToDepth
class MyModule(nn.Module):
def __init__(self):
super().__init__()
self.factor = 4
self.conv = nn.Conv2d(3*self.factor**2, 3*self.factor**2, kernel_size=1)
def forward(self, x):
x = nn.PixelUnshuffle(self.factor)(x)
x = self.conv(x)
x = nn.PixelShuffle(self.factor)(x)
return x
class MyModuleTFOptimized(nn.Module):
def __init__(self):
super().__init__()
self.factor = 4
self.conv = nn.Conv2d(3*self.factor**2, 3*self.factor**2, kernel_size=1)
def forward(self, x):
x = SpaceToDepth(self.factor)(x)
x = self.conv(x)
x = DepthToSpace(self.factor)(x)
return x
input = torch.normal(0, 1, size=(1, 3, 128, 128))
# pytorch_module = MyModule()
pytorch_module = MyModuleTFOptimized()
pytorch_module.eval()
keras_model = nobuco.pytorch_to_keras(
pytorch_module,
args=[input],
inputs_channel_order=ChannelOrder.TENSORFLOW,
trace_shape=False,
)
model_path = 'pixelshuffle'
keras_model.save(model_path + '.h5')
print('Model saved')
custom_objects = {'WeightLayer': WeightLayer}
keras_model_restored = keras.models.load_model(model_path + '.h5', custom_objects=custom_objects)
print('Model loaded')
converter = TFLiteConverter.from_keras_model_file(model_path + '.h5', custom_objects=custom_objects)
converter.target_ops = [tf.lite.OpsSet.SELECT_TF_OPS, tf.lite.OpsSet.TFLITE_BUILTINS]
tflite_model = converter.convert()
with open(model_path + '.tflite', 'wb') as f:
f.write(tflite_model)