35
35
import pandas as pd
36
36
37
37
import tensorflow as tf
38
+ dtype = np .float32
38
39
#with tf.device('/GPU:0'):
39
40
#with tf.device('/cpu:0'):
40
41
def train_a_font (input_filters_dict ,output_feature_list , nEpochs = 5000 ):
41
42
42
43
ds = ocr_utils .read_data (input_filters_dict = input_filters_dict ,
43
44
output_feature_list = output_feature_list ,
44
45
test_size = .1 ,
45
- engine_type = 'tensorflow' )
46
+ engine_type = 'tensorflow' ,
47
+ dtype = dtype )
46
48
47
49
48
50
"""# ==============================================================================
@@ -86,7 +88,7 @@ def train_a_font(input_filters_dict,output_feature_list, nEpochs=5000):
86
88
nm = 'x_' + nm
87
89
if i > 1 :
88
90
extra_features_width += ds .train .feature_width [i ]
89
- lst .append (tf .placeholder (tf . float32 , shape = [None , ds .train .feature_width [i ]], name = nm ))
91
+ lst .append (tf .placeholder (dtype , shape = [None , ds .train .feature_width [i ]], name = nm ))
90
92
91
93
# ph is a named tuple with key names like 'image', 'm_label', and values that
92
94
# are tensors. The display name on the Chrome graph are 'y_m_label', 'x_image,
@@ -113,13 +115,13 @@ def train_a_font(input_filters_dict,output_feature_list, nEpochs=5000):
113
115
114
116
""" # ==============================================================================
115
117
116
- def weight_variable (shape ):
117
- initial = tf .truncated_normal (shape , stddev = 0.1 )
118
+ def weight_variable (shape , dtype ):
119
+ initial = tf .truncated_normal (shape , stddev = 0.1 , dtype = dtype )
118
120
return tf .Variable (initial )
119
121
120
- def bias_variable (shape ):
121
- initial = tf .constant (0.1 , shape = shape )
122
- return tf .Variable (initial )
122
+ def bias_variable (shape , dtype ):
123
+ initial = tf .constant (0.1 , shape = shape , dtype = dtype )
124
+ return tf .Variable (initial )
123
125
124
126
"""# ==============================================================================
125
127
@@ -142,8 +144,8 @@ def max_pool_2x2(x):
142
144
143
145
""" # ==============================================================================
144
146
with tf .name_scope ("w_conv1" ) as scope :
145
- W_conv1 = weight_variable ([5 , 5 , 1 , nConv1 ])
146
- b_conv1 = bias_variable ([nConv1 ])
147
+ W_conv1 = weight_variable ([5 , 5 , 1 , nConv1 ], dtype )
148
+ b_conv1 = bias_variable ([nConv1 ], dtype )
147
149
148
150
with tf .name_scope ("reshape_x_image" ) as scope :
149
151
x_image = tf .reshape (ph .image , [- 1 ,nCols ,nRows ,1 ])
@@ -173,8 +175,8 @@ def max_pool_2x2(x):
173
175
""" # ==============================================================================
174
176
175
177
with tf .name_scope ("convolve_2" ) as scope :
176
- W_conv2 = weight_variable ([5 , 5 , nConv1 , nConv2 ])
177
- b_conv2 = bias_variable ([64 ])
178
+ W_conv2 = weight_variable ([5 , 5 , nConv1 , nConv2 ], dtype )
179
+ b_conv2 = bias_variable ([64 ], dtype )
178
180
h_conv2 = tf .nn .relu (conv2d (h_pool1 , W_conv2 ) + b_conv2 )
179
181
180
182
with tf .name_scope ("pool_2" ) as scope :
@@ -192,8 +194,8 @@ def max_pool_2x2(x):
192
194
""" # ==============================================================================
193
195
194
196
with tf .name_scope ("W_fc0_b" ) as scope :
195
- W_fc0 = weight_variable ([n_h_pool2_outputsx , nFc0 ])
196
- b_fc0 = bias_variable ([nFc0 ])
197
+ W_fc0 = weight_variable ([n_h_pool2_outputsx , nFc0 ], dtype )
198
+ b_fc0 = bias_variable ([nFc0 ], dtype )
197
199
198
200
h_pool2_flat = tf .reshape (h_pool2 , [- 1 , n_h_pool2_outputs ])
199
201
@@ -213,8 +215,8 @@ def max_pool_2x2(x):
213
215
""" # ==============================================================================
214
216
215
217
with tf .name_scope ("W_fc1_b" ) as scope :
216
- W_fc1 = weight_variable ([nFc0 , nFc1 ])
217
- b_fc1 = bias_variable ([nFc1 ])
218
+ W_fc1 = weight_variable ([nFc0 , nFc1 ], dtype )
219
+ b_fc1 = bias_variable ([nFc1 ], dtype )
218
220
219
221
h_fc1 = tf .nn .relu (tf .matmul (h_fc0 , W_fc1 ) + b_fc1 )
220
222
@@ -230,16 +232,16 @@ def max_pool_2x2(x):
230
232
""" # ==============================================================================
231
233
232
234
with tf .name_scope ("W_fc2_b" ) as scope :
233
- W_fc2 = weight_variable ([nFc1 , nFc2 ])
234
- b_fc2 = bias_variable ([nFc2 ])
235
+ W_fc2 = weight_variable ([nFc1 , nFc2 ], dtype )
236
+ b_fc2 = bias_variable ([nFc2 ], dtype )
235
237
236
238
h_fc2 = tf .nn .relu (tf .matmul (h_fc1 , W_fc2 ) + b_fc2 )
237
239
238
240
"""# ==============================================================================
239
241
Dropout
240
242
241
243
""" # ==============================================================================
242
- keep_prob = tf .placeholder (tf . float32 ,name = 'keep_prob' )
244
+ keep_prob = tf .placeholder (dtype ,name = 'keep_prob' )
243
245
244
246
with tf .name_scope ("drop" ) as scope :
245
247
h_fc2_drop = tf .nn .dropout (h_fc2 , keep_prob )
@@ -250,8 +252,8 @@ def max_pool_2x2(x):
250
252
251
253
""" # ==============================================================================
252
254
with tf .name_scope ("softmax" ) as scope :
253
- W_fc3 = weight_variable ([nFc2 , nTarget ])
254
- b_fc3 = bias_variable ([nTarget ])
255
+ W_fc3 = weight_variable ([nFc2 , nTarget ], dtype )
256
+ b_fc3 = bias_variable ([nTarget ], dtype )
255
257
y_conv = tf .nn .softmax (tf .matmul (h_fc2_drop , W_fc3 ) + b_fc3 )
256
258
257
259
"""# ==============================================================================
@@ -271,7 +273,7 @@ def max_pool_2x2(x):
271
273
with tf .name_scope ("test" ) as scope :
272
274
correct_prediction = tf .equal (tf .argmax (y_conv ,1 ), tf .argmax (ph [0 ],1 ))
273
275
274
- accuracy = tf .reduce_mean (tf .cast (correct_prediction , tf . float32 ))
276
+ accuracy = tf .reduce_mean (tf .cast (correct_prediction ,dtype ))
275
277
accuracy_summary = tf .scalar_summary ("accuracy" , accuracy )
276
278
277
279
merged = tf .merge_all_summaries ()
@@ -399,7 +401,7 @@ def computeSize(s,tens):
399
401
#output_feature_list = ['font_one_hot','image','italic','aspect_ratio','upper_case']
400
402
401
403
# train the digits 0-9 for all fonts
402
- input_filters_dict = {'m_label' : list (range (48 ,58 ))+ list (range (65 ,91 ))+ list (range (97 ,123 ))}
404
+ input_filters_dict = {'m_label' : list (range (48 ,58 ))+ list (range (65 ,91 ))+ list (range (97 ,123 )), 'fontVariant' : 'scanned' }
403
405
#input_filters_dict = {}
404
406
output_feature_list = ['m_label_one_hot' ,'image' ,'italic' ,'aspect_ratio' ,'upper_case' ]
405
407
train_a_font (input_filters_dict , output_feature_list , nEpochs = 20000 )
0 commit comments