发新帖

求大神解决一下恢复两次模型报错的问题qwq

[复制链接]
169 1

快来加入 TensorFlowers 大家庭!

您需要 登录 才可以下载或查看,没有帐号?加入社区

x
本帖最后由 hhz_1998 于 2019-3-24 16:31 编辑

分别训练了两个CNN模型,并将预测程序封装成了两个不同的方法,但是在一个函数中分别调用两个函数的时候会报错,请问这是为什么?如何解决呢?
调用函数 test.py
  1. import PlateNumberRecognition.predict_digits as pd
  2. import PlateNumberRecognition.predict_provinces as pp
  3. import cv2
  4. import tensorflow as tf
  5. pic_1=cv2.imread(r'6.bmp',0)
  6. pic_2=cv2.imread('1.bmp',0)
  7. pp.predict_provinces(pic_2)
  8. pd.digit_predict(pic_1)
复制代码

predict_digits.py:
  1. import tensorflow as tf
  2. import os
  3. import PlateNumberRecognition.config as config
  4. import cv2
  5. import numpy as np

  6. NUM_CLASSES = config.DIGITS_NUM_CLASSES
  7. SAVER_DIR = config.DIGITS_SAVER_DIR
  8. HEIGHT = config.HEIGHT
  9. WIDTH = config.WIDTH
  10. CHANNEL_NUM = config.CHANNEL_NUM
  11. LETTERS_DIGITS=config.LETTERS_DIGITS
  12. def digit_predict(digit_arr):
  13.     saver = tf.train.import_meta_graph(os.path.join(SAVER_DIR, 'model.ckpt.meta'))
  14.     x = tf.placeholder(tf.float32, shape=[None, HEIGHT, WIDTH, CHANNEL_NUM])
  15.     with tf.Session() as sess:
  16.         model_file = tf.train.latest_checkpoint(SAVER_DIR)
  17.         saver.restore(sess, model_file)
  18.         conv1_w = sess.graph.get_tensor_by_name('layer1-conv1/weight:0')
  19.         conv1_b = sess.graph.get_tensor_by_name('layer1-conv1/bias:0')
  20.         conv1 = tf.nn.conv2d(x, conv1_w, strides=[1, 1, 1, 1], padding='SAME')
  21.         relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_b))
  22.         pool1 = tf.nn.max_pool(relu1, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')

  23.         # 第二个卷积层
  24.         conv2_w = sess.graph.get_tensor_by_name('layer3-conv2/weight:0')
  25.         conv2_b = sess.graph.get_tensor_by_name('layer3-conv2/bias:0')
  26.         conv2 = tf.nn.conv2d(pool1, conv2_w, strides=[1, 1, 1, 1], padding='SAME')
  27.         relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_b))
  28.         pool2 = tf.nn.max_pool(relu2, [1, 1, 1, 1], [1, 1, 1, 1], padding='SAME')

  29.         # 全连接层
  30.         fc1_w = sess.graph.get_tensor_by_name('layer5-fc1/weight:0')
  31.         fc1_b = sess.graph.get_tensor_by_name('layer5-fc1/bias:0')
  32.         pool_shape = pool2.get_shape().as_list()
  33.         nodes = pool_shape[1] * pool_shape[2] * pool_shape[3]

  34.         reshaped = tf.reshape(pool2, [-1, nodes])
  35.         fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_w) + fc1_b)

  36.         fc2_w = sess.graph.get_tensor_by_name('layer6-fc2/weight:0')
  37.         fc2_b = sess.graph.get_tensor_by_name('layer6-fc2/bias:0')

  38.         result = tf.nn.softmax(tf.matmul(fc1, fc2_w) + fc2_b)

  39.         _, binary = cv2.threshold(digit_arr, 127, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  40.         # print(binary.shape)
  41.         # cv2.imshow('window1', binary)
  42.         # cv2.waitKey(0)
  43.         for i in range(binary.shape[0]):
  44.             for j in range(binary.shape[1]):
  45.                 if binary[i][j]==0:
  46.                     binary[i][j]=1
  47.                 else:
  48.                     binary[i][j]=0
  49.         img_data = np.reshape(binary,[1, HEIGHT, WIDTH, CHANNEL_NUM])
  50.         result = sess.run(result, feed_dict={x: np.array(img_data)})

  51.         max = 0
  52.         max_index = 0

  53.         for j in range(NUM_CLASSES):
  54.             if result[0][j] > max:
  55.                 max = result[0][j]
  56.                 max_index = j
  57.                 continue
  58.     return config.LETTERS_DIGITS[max_index]
复制代码

predict_provinces.py
  1. import tensorflow as tf
  2. import os
  3. import PlateNumberRecognition.config as config
  4. from PIL import Image
  5. import numpy as np
  6. import PlateNumberRecognition
  7. NUM_CLASSES = config.PROVINCE_NUM_CLASSES
  8. SAVER_DIR = config.PROVINCE_SAVER_DIR
  9. HEIGHT = config.HEIGHT
  10. WIDTH = config.WIDTH
  11. CHANNEL_NUM = config.CHANNEL_NUM

  12. SIZE = config.SIZE
  13. PROVINCES = config.PROVINCES

  14. def predict_provinces(pro_arr):
  15.     x = tf.placeholder(tf.float32, shape=[None, HEIGHT, WIDTH, CHANNEL_NUM])
  16.     saver = tf.train.import_meta_graph(os.path.join(SAVER_DIR, 'model.ckpt.meta'),clear_devices=True)
  17.     with tf.Session() as sess:
  18.         model_file = tf.train.latest_checkpoint(SAVER_DIR)
  19.         saver.restore(sess, model_file)
  20.         conv1_w = sess.graph.get_tensor_by_name('layer1-conv1/weight:0')
  21.         conv1_b = sess.graph.get_tensor_by_name('layer1-conv1/bias:0')

  22.         conv1 = tf.nn.conv2d(x, conv1_w, strides=[1, 1, 1, 1], padding='SAME')
  23.         relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_b))
  24.         pool1 = tf.nn.max_pool(relu1, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')

  25.         # 第二个卷积层
  26.         conv2_w = sess.graph.get_tensor_by_name('layer3-conv2/weight:0')
  27.         conv2_b = sess.graph.get_tensor_by_name('layer3-conv2/bias:0')
  28.         conv2 = tf.nn.conv2d(pool1, conv2_w, strides=[1, 1, 1, 1], padding='SAME')
  29.         relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_b))
  30.         pool2 = tf.nn.max_pool(relu2, [1, 1, 1, 1], [1, 1, 1, 1], padding='SAME')

  31.         # 全连接层
  32.         fc1_w = sess.graph.get_tensor_by_name('layer5-fc1/weight:0')
  33.         fc1_b = sess.graph.get_tensor_by_name('layer5-fc1/bias:0')
  34.         pool_shape = pool2.get_shape().as_list()
  35.         nodes = pool_shape[1] * pool_shape[2] * pool_shape[3]

  36.         reshaped = tf.reshape(pool2, [-1, nodes])
  37.         fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_w) + fc1_b)

  38.         fc2_w = sess.graph.get_tensor_by_name('layer6-fc2/weight:0')
  39.         fc2_b = sess.graph.get_tensor_by_name('layer6-fc2/bias:0')

  40.         result = tf.nn.softmax(tf.matmul(fc1, fc2_w) + fc2_b)

  41.         img_data = np.reshape(pro_arr, [1, HEIGHT, WIDTH, CHANNEL_NUM])
  42.         result = sess.run(result, feed_dict={x: np.array(img_data)})

  43.         max = 0
  44.         max_index = 0

  45.         for j in range(NUM_CLASSES):
  46.             if result[0][j] > max:
  47.                 max = result[0][j]
  48.                 max_index = j
  49.                 continue
  50.         license_number = PROVINCES[max_index]
  51.     return license_number
复制代码

错误信息:[code]G:\Pycharm\test\venv\Scripts\python.exe C:/Users/asus/Desktop/Intelligent-Transportation/PlateNumberRecognition/test.py
2019-03-19 17:21:24.765040: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
  File "G:\Pycharm\test\venv\lib\site-packages\tensorflow\python\client\session.py", line 1334, in _do_call
    return fn(*args)
  File "G:\Pycharm\test\venv\lib\site-packages\tensorflow\python\client\session.py", line 1319, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "G:\Pycharm\test\venv\lib\site-packages\tensorflow\python\client\session.py", line 1407, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [512,6] rhs shape= [512,34]
我知道答案 回答被采纳将会获得10 金币 + 25 金币 已有1人回答
本楼点评(1) 收起
  • shuaikuang楼主你好,我也遇到了同样的问题,用的方法也和你差不多,请问你是如何解决这个问题的?
    2019-4-11 19:58 回复

精彩评论1

您需要登录后才可以回帖 登录 | 加入社区

本版积分规则

主题

帖子

3

积分
快速回复 返回顶部 返回列表