12
12
import turicreate .toolkits ._tf_utils as _utils
13
13
import tensorflow .compat .v1 as _tf
14
14
15
+ # This toolkit is compatible with TensorFlow V2 behavior.
16
+ # However, until all toolkits are compatible, we must call `disable_v2_behavior()`.
15
17
_tf .disable_v2_behavior ()
16
18
17
19
@@ -20,7 +22,6 @@ def __init__(self, net_params, batch_size, num_classes):
20
22
"""
21
23
Defines the TensorFlow model, loss, optimisation and accuracy. Then
22
24
loads the weights into the model.
23
-
24
25
"""
25
26
self .gpu_policy = _utils .TensorFlowGPUPolicy ()
26
27
self .gpu_policy .start ()
@@ -54,40 +55,34 @@ def init_drawing_classifier_graph(self, net_params):
54
55
55
56
# Weights
56
57
weights = {
57
- "drawing_conv0_weight" : _tf .Variable (
58
- _tf .zeros ([3 , 3 , 1 , 16 ]), name = "drawing_conv0_weight"
59
- ),
60
- "drawing_conv1_weight" : _tf .Variable (
61
- _tf .zeros ([3 , 3 , 16 , 32 ]), name = "drawing_conv1_weight"
62
- ),
63
- "drawing_conv2_weight" : _tf .Variable (
64
- _tf .zeros ([3 , 3 , 32 , 64 ]), name = "drawing_conv2_weight"
65
- ),
66
- "drawing_dense0_weight" : _tf .Variable (
67
- _tf .zeros ([576 , 128 ]), name = "drawing_dense0_weight"
68
- ),
69
- "drawing_dense1_weight" : _tf .Variable (
70
- _tf .zeros ([128 , self .num_classes ]), name = "drawing_dense1_weight"
71
- ),
58
+ name : _tf .Variable (_utils .convert_conv2d_coreml_to_tf (net_params [name ]), name = name )
59
+ for name in ("drawing_conv0_weight" ,
60
+ "drawing_conv1_weight" ,
61
+ "drawing_conv2_weight" )
72
62
}
63
+ weights ["drawing_dense1_weight" ] = _tf .Variable (
64
+ _utils .convert_dense_coreml_to_tf (net_params ["drawing_dense1_weight" ]), name = "drawing_dense1_weight"
65
+ )
66
+ """
67
+ To make output of CoreML pool3 (NCHW) compatible with TF (NHWC).
68
+ Decompose FC weights to NCHW. Transpose to NHWC. Reshape back to FC.
69
+ """
70
+ coreml_128_576 = net_params ["drawing_dense0_weight" ]
71
+ coreml_128_576 = _np .reshape (coreml_128_576 , (128 , 64 , 3 , 3 ))
72
+ coreml_128_576 = _np .transpose (coreml_128_576 , (0 , 2 , 3 , 1 ))
73
+ coreml_128_576 = _np .reshape (coreml_128_576 , (128 , 576 ))
74
+ weights ["drawing_dense0_weight" ] = _tf .Variable (
75
+ _np .transpose (coreml_128_576 , (1 , 0 )), name = "drawing_dense0_weight"
76
+ )
73
77
74
78
# Biases
75
79
biases = {
76
- "drawing_conv0_bias" : _tf .Variable (
77
- _tf .zeros ([16 ]), name = "drawing_conv0_bias"
78
- ),
79
- "drawing_conv1_bias" : _tf .Variable (
80
- _tf .zeros ([32 ]), name = "drawing_conv1_bias"
81
- ),
82
- "drawing_conv2_bias" : _tf .Variable (
83
- _tf .zeros ([64 ]), name = "drawing_conv2_bias"
84
- ),
85
- "drawing_dense0_bias" : _tf .Variable (
86
- _tf .zeros ([128 ]), name = "drawing_dense0_bias"
87
- ),
88
- "drawing_dense1_bias" : _tf .Variable (
89
- _tf .zeros ([self .num_classes ]), name = "drawing_dense1_bias"
90
- ),
80
+ name : _tf .Variable (net_params [name ], name = name )
81
+ for name in ("drawing_conv0_bias" ,
82
+ "drawing_conv1_bias" ,
83
+ "drawing_conv2_bias" ,
84
+ "drawing_dense0_bias" ,
85
+ "drawing_dense1_bias" )
91
86
}
92
87
93
88
conv_1 = _tf .nn .conv2d (
@@ -119,23 +114,19 @@ def init_drawing_classifier_graph(self, net_params):
119
114
120
115
# Flatten the data to a 1-D vector for the fully connected layer
121
116
fc1 = _tf .reshape (pool_3 , (- 1 , 576 ))
122
-
123
117
fc1 = _tf .nn .xw_plus_b (
124
118
fc1 ,
125
119
weights = weights ["drawing_dense0_weight" ],
126
120
biases = biases ["drawing_dense0_bias" ],
127
121
)
128
-
129
122
fc1 = _tf .nn .relu (fc1 )
130
123
131
124
out = _tf .nn .xw_plus_b (
132
125
fc1 ,
133
126
weights = weights ["drawing_dense1_weight" ],
134
127
biases = biases ["drawing_dense1_bias" ],
135
128
)
136
- softmax_out = _tf .nn .softmax (out )
137
-
138
- self .predictions = softmax_out
129
+ self .predictions = _tf .nn .softmax (out )
139
130
140
131
# Loss
141
132
self .cost = _tf .losses .softmax_cross_entropy (
@@ -153,60 +144,6 @@ def init_drawing_classifier_graph(self, net_params):
153
144
self .sess = _tf .Session ()
154
145
self .sess .run (_tf .global_variables_initializer ())
155
146
156
- # Assign the initialised weights from C++ to tensorflow
157
- layers = [
158
- "drawing_conv0_weight" ,
159
- "drawing_conv0_bias" ,
160
- "drawing_conv1_weight" ,
161
- "drawing_conv1_bias" ,
162
- "drawing_conv2_weight" ,
163
- "drawing_conv2_bias" ,
164
- "drawing_dense0_weight" ,
165
- "drawing_dense0_bias" ,
166
- "drawing_dense1_weight" ,
167
- "drawing_dense1_bias" ,
168
- ]
169
-
170
- for key in layers :
171
- if "bias" in key :
172
- self .sess .run (
173
- _tf .assign (
174
- _tf .get_default_graph ().get_tensor_by_name (key + ":0" ),
175
- net_params [key ],
176
- )
177
- )
178
- else :
179
- if "drawing_dense0_weight" in key :
180
- """
181
- To make output of CoreML pool3 (NCHW) compatible with TF (NHWC).
182
- Decompose FC weights to NCHW. Transpose to NHWC. Reshape back to FC.
183
- """
184
- coreml_128_576 = net_params [key ]
185
- coreml_128_576 = _np .reshape (coreml_128_576 , (128 , 64 , 3 , 3 ))
186
- coreml_128_576 = _np .transpose (coreml_128_576 , (0 , 2 , 3 , 1 ))
187
- coreml_128_576 = _np .reshape (coreml_128_576 , (128 , 576 ))
188
- self .sess .run (
189
- _tf .assign (
190
- _tf .get_default_graph ().get_tensor_by_name (key + ":0" ),
191
- _np .transpose (coreml_128_576 , (1 , 0 )),
192
- )
193
- )
194
- elif "dense" in key :
195
- dense_weights = _utils .convert_dense_coreml_to_tf (net_params [key ])
196
- self .sess .run (
197
- _tf .assign (
198
- _tf .get_default_graph ().get_tensor_by_name (key + ":0" ),
199
- dense_weights ,
200
- )
201
- )
202
- else :
203
- self .sess .run (
204
- _tf .assign (
205
- _tf .get_default_graph ().get_tensor_by_name (key + ":0" ),
206
- _utils .convert_conv2d_coreml_to_tf (net_params [key ]),
207
- )
208
- )
209
-
210
147
def __del__ (self ):
211
148
self .sess .close ()
212
149
self .gpu_policy .stop ()
0 commit comments