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 73 74 75 76 77 78 79 80 81 82 83 84 85
| def export_input(f,channel,width,height): f.write('InputLayer %d %d %d\n' % (channel,width,height)) def export_layer(f,name): f.write(name + '\n') def export_conv(f,conv_weight,conv_bias,padding_type): print('export conv layer.') print(conv_weight.shape) print(conv_bias.shape) conv_weight = np.transpose(conv_weight,[3,2,0,1]) oc,ic,kw,kh,sw,sh,bias = conv_weight.shape[0],conv_weight.shape[1],conv_weight.shape[2],conv_weight.shape[3],1,1,1 f.write('ConvolutionLayer %d %d %d %d %d %d %d %d ' % (oc,ic,kw,kh,sw,sh,bias,padding_type)) f.write(' '.join(map(str,conv_weight.flatten().tolist())) + ' ') f.write(' '.join(map(str,conv_bias.flatten().tolist())) + ' ') f.write('\n') def export_pool(f,channel): f.write('PoolingLayer 0 1 %d 2 2 2 2\n' % (channel)) def export_fc(f,fc_weight,fc_bias): print(fc_weight.shape) print(fc_bias.shape) f.write('FullconnectLayer 1 %d 1 1 1 ' % fc_bias.shape[0]) fc_weight = np.transpose(fc_weight,[1,0]) f.write(' '.join(map(str,fc_weight.flatten().tolist())) + ' ') f.write(' '.join(map(str,fc_bias.flatten().tolist())) + ' ') f.write('\n') def export_model(tf_model_path,easycnn_model_path): x = tf.placeholder(tf.float32, [None,std_width*std_height]) predict,params = build_cnn(x) y = tf.placeholder(tf.float32, [None,classes])
saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess,tf_model_path) f = open(easycnn_model_path,'w') export_input(f,1,std_width,std_height) conv_weight = params[0].eval() conv_bias = params[1].eval() export_conv(f,conv_weight,conv_bias,1) export_layer(f,'ReluLayer') export_pool(f,conv_weight.shape[3]) conv_weight = params[2].eval() conv_bias = params[3].eval() export_conv(f,conv_weight,conv_bias,1) export_layer(f,'ReluLayer') export_pool(f,conv_weight.shape[3]) conv_weight = params[4].eval() conv_bias = params[5].eval() export_conv(f,conv_weight,conv_bias,1) export_layer(f,'ReluLayer') export_pool(f,conv_weight.shape[3]) conv_weight = params[6].eval() conv_bias = params[7].eval() export_conv(f,conv_weight,conv_bias,1) export_layer(f,'ReluLayer') export_pool(f,conv_weight.shape[3]) fc_weight = params[8].eval() fc_bias = params[9].eval() export_fc(f,fc_weight,fc_bias) export_layer(f,'ReluLayer') fc_weight = params[10].eval() fc_bias = params[11].eval() export_fc(f,fc_weight,fc_bias) export_layer(f,'SoftmaxLayer')
|