如何调整CNN的图像区域大小?

时间:2019-07-18 15:13:08

标签: python tensorflow conv-neural-network

我正在使用AlexNet进行对象识别。我已经使用大小为(277,277)的图像训练了模型。然后使用选择性搜索算法从图像中提取区域,并将这些区域提供给网络进行测试/预测。 无论如何,当我调整图像区域的大小(来自SelectiveSearch)时,都会出现错误。

用于调整训练图像大小的代码:

try:
 img_array = cv2.imread(os.path.join(path,img))
 new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
 gray_img = cv2.cvtColor(new_array, cv2.COLOR_BGR2GRAY)
 training_data.append([gray_img, class_num])

except Exception as e:
 pass

用于调整所选图像区域大小的代码:

img_lbl, regions = selectivesearch.selective_search(img, scale=500, sigma=0.4, min_size=10)
for r in regions:
 x, y, w, h = r['rect']
 segment = img[y:y + h, x:x + w]
 gray_img = cv2.resize(segment, (277, 277))
 gray_img = cv2.cvtColor(gray_img, cv2.COLOR_BGR2GRAY)
 gray_img = np.array(gray_img).reshape(-1, 277, 277, 1)
 gray_img = gray_img / 255.0
 prediction = model.predict(gray_img)

它在最后一行给出错误,即:

  

预测= model.predict(gray_img)

,错误是:

  

错误:检查输入时出错:预期conv2d_1_input具有形状(227、227、1),但形状为(277、277、1)的数组

当两个形状相同时,为什么会出现此错误。

1 个答案:

答案 0 :(得分:0)

您的模型期望张量作为输入,但是您尝试对numpy数组求值。而是使用给定形状的占位符,然后在会话中将数组输入该占位符。

# define a placeholder for input 
image = tf.placeholder(dtype=tf.float32, name="image", shape=[277,277,1])
prediction = model.predict(image)

# evaluate each of your resized images in a session
with tf.Session() as sess:
    for r in regions:
        x, y, w, h = r['rect']
        # rest of your code from the loop here 
        gray_img = gray_img /255.
        p = sess.run(prediction, feed_dict={image: gray_img})
        print(p) # to print the prediction of your model for this image

也许您应该看一下这个问题:What's the difference between tf.placeholder and tf.Variable?