|
板凳
楼主 |
发表于 2019-3-28 16:08:24
|
只看该作者
报错信息是:Aborted (core dumped),日志是空的模型结构:
- import tensorflow as tf
- def weight_variable(shape):
- tf.set_random_seed(1)
- return tf.Variable(tf.truncated_normal(shape, stddev=0.1))
- def bias_variable(shape):
- return tf.Variable(tf.constant(0.0, shape=shape))
- def conv2d(x, W):
- return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
- def max_pool_2x2(z):
- return tf.nn.max_pool(z, ksize=[1,1,2,1], strides=[1,1,2,1], padding='SAME')
- X = tf.placeholder(tf.float32, [None, 1, 64, 1], name="input_x")
- y = tf.placeholder(tf.float32, [None, 10], name="input_y")
- kp = tf.placeholder_with_default(1.0, shape=(), name="keep_prob")
- lam = tf.placeholder(tf.float32, name="lamda")
- #conv1
- W_conv1 = weight_variable([1,5,1,32])
- b_conv1 = bias_variable([32])
- z1 = tf.nn.relu(conv2d(X, W_conv1) + b_conv1)
- maxpool1 = max_pool_2x2(z1) #max_pool1完后maxpool1维度为[?,32,32,32]
- #conv2
- W_conv2 = weight_variable([1,5,32,64])
- b_conv2 = bias_variable([64])
- z2 = tf.nn.relu(conv2d(maxpool1, W_conv2) + b_conv2)
- maxpool2 = max_pool_2x2(z2) #max_pool2,shape [?,16,16,64]
- print("maxpool2.shape",maxpool2.get_shape())
- #conv3 效果比较好的一次模型是没有这一层,只有两次卷积层,隐藏单元100,训练20次
- # W_conv3 = weight_variable([5, 5, 64, 128])
- # b_conv3 = bias_variable([128])
- # z3 = tf.nn.relu(conv2d(maxpool2, W_conv3) + b_conv3)
- # maxpool3 = max_pool_2x2(z3) # max_pool3,shape [?,8,8,128]
- #full connection1
- W_fc1 = weight_variable([1*16*64, 200])
- b_fc1 = bias_variable([200])
- maxpool2_flat = tf.reshape(maxpool2, [-1, 2*16*64])
- z_fc1 = tf.nn.relu(tf.matmul(maxpool2_flat, W_fc1) + b_fc1)
- #z_fc1_drop = tf.nn.dropout(z_fc1, keep_prob=kp)
- #softmax layer
- W_fc2 = weight_variable([200, 10])
- b_fc2 = bias_variable([10])
- z_fc2 = tf.add(tf.matmul(z_fc1, W_fc2),b_fc2, name="outlayer")
- prob = tf.nn.softmax(z_fc2, name="probability")
- pred = tf.argmax(prob, 1, output_type="int32", name="predict")
- init = tf.global_variables_initializer()
- with tf.Session() as sess:
- sess.run(init)
- saver = tf.train.Saver({'W_conv1':W_conv1, 'b_conv1':b_conv1, 'W_conv2':W_conv2, 'b_conv2':b_conv2,
- 'W_fc1':W_fc1, 'b_fc1':b_fc1, 'W_fc2':W_fc2, 'b_fc2':b_fc2})
- saver.save(sess, "make_model/cnn_model.ckpt")
- output_graph_def = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, output_node_names=['predict'])
- with tf.gfile.FastGFile('make_model_tl/digital_gesture.pb', mode='wb') as f:
- f.write(output_graph_def.SerializeToString())
转换模型的代码是:- from rknn.api import RKNN
- INPUT_SIZE = 64
- if __name__ == '__main__':
- # 创建RKNN执行对象
- rknn = RKNN()
- #初始化RKNN运行环境,指定host=’rk3399pro’,后续的操作将会运行在NPU上
- print('--> Init runtime environment')
- ret = rknn.init_runtime()
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- print('done')
- # 配置模型输入,用于NPU对数据输入的预处理
- # channel_mean_value='0 0 0 255',那么模型推理时,将会对RGB数据做如下转换
- # (R - 0)/255, (G - 0)/255, (B - 0)/255。推理时,RKNN模型会自动做均值和归一化处理
- # reorder_channel=’0 1 2’用于指定是否调整RBG顺序,设置成0 1 2即按输入的RGB顺序不做调整
- # reorder_channel=’2 1 0’表示交换0和2通道,如果输入是RGB,将会被调整为BGR。如果是BGR将会被
- # 调整为RGB
- #rknn.config(channel_mean_value='0 0 0 255', reorder_channel='0 1 2')
- rknn.config(channel_mean_value='0 0 0 1',reorder_channel='0 1 2')
- # 加载TensorFlow模型
- # tf_pb='../digital_gesture_recognition/model_2500/digital_gesture.pb'指定待转换的TensorFlow模型
- # inputs指定模型中的输入节点
- # outputs指定模型中输出节点
- # input_size_list指定模型输入的大小
- print('--> Loading model')
- rknn.load_tensorflow(tf_pb='make_model_tl/digital_gesture.pb',
- inputs=['input_x'],
- outputs=['outlayer'],
- input_size_list=[[1, 64, 1]])
- print('done')
- # 创建解析pb模型
- # do_quantization=False指定不进行量化
- # 量化会减小模型的体积和提升运算速度,但是会有精度的丢失
- print('--> Building model')
- rknn.build(do_quantization=False)
- print('done')
用pb推理成功的代码:
- import numpy as np
- #read image
- def img_to_mat(path):
- mat = np.arange(64).reshape(1,1,64,1)
- return mat
- #load pb
- def load_model(pb_path):
- with tf.gfile.GFile(pb_path, "rb") as f:
- graph_def = tf.GraphDef()
- graph_def.ParseFromString(f.read())
- with tf.Graph().as_default() as graph:
- tf.import_graph_def(graph_def, name="")
- return graph
- #do inference
- def predict(graph):
- mat = img_to_mat("out.jpg")
- x = graph.get_tensor_by_name("input_x:0")
- outlayer = graph.get_tensor_by_name("outlayer:0")
- with tf.Session(graph=graph) as sess:
- np.set_printoptions(suppress=True)
- out= sess.run([outlayer],feed_dict={x:mat})
- return out
- if __name__=="__main__":
- graph = load_model('make_model_tl/digital_gesture.pb')
- out = predict(graph)
- print(out)
rknn推理失败的代码:
- from rknn.api import RKNN
- from PIL import Image
- import numpy as np
- #read image
- def img_to_mat(path):
- mat = np.arange(64).reshape(1,64)
- return mat
- #load rknn
- def load_model(rknn_path):
- rknn = RKNN(verbose=True, verbose_file='./log.txt')
- # 载入RKNN模型
- print('-->loading model')
- rknn.load_rknn(rknn_path)
- print('loading model done')
- # 初始化RKNN运行环境
- print('--> Init runtime environment')
- ret = rknn.init_runtime()
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- print('done')
- return rknn
- #do inference
- def predict(rknn):
- mat = img_to_mat("out.jpg")
- print("mat.shape",mat.shape)
- #mat = mat.reshape(64,1)
- # print("mat.shape",mat.shape)
- out=rknn.inference(inputs=[mat])
- print('ooo')
- return out
- if __name__=="__main__":
- rknn = load_model('make_model_tl/digital_gesture.rknn')
- out = predict(rknn)
- print(out)
结论:是不支持这种1*n的卷积核吗?
|
|