【问题标题】:ValueError: Cannot feed value of shape (637, 1162) for Tensor u'Placeholder:0', which has shape '(?, 637, 1162)'ValueError:无法为具有形状“(?,637,1162)”的张量u'Placeholder:0'提供形状(637、1162)的值
【发布时间】:2019-02-21 19:30:23
【问题描述】:

我收到提到的错误。我想加载单个图像作为输入 并在给定的蒙版图像上训练它以进行图像二进制 分类。

import tensorflow as tf

import os

import cv2

import matplotlib.pyplot as plt

import numpy as np



images = []

file_names = [os.path.join('../', f)
                      for f in os.listdir('../')
                      if f.endswith(".jpg")]

for f in file_names:
            images.append(cv2.cvtColor(cv2.imread(f,1), cv2.COLOR_BGR2GRAY))

img_mask = images[0];

retval,mask_img = cv2.threshold(img_mask, 50, 255, cv2.THRESH_BINARY)

mask_img = mask_img/255

maskk = np.concatenate(mask_img)

x = tf.placeholder(dtype = tf.float32, shape = [None, 637, 1162])

y = tf.placeholder(dtype = tf.int32, shape = [None])

展平输入数据

images_flat = tf.contrib.layers.flatten(x)

全连接层

logits = tf.contrib.layers.fully_connected(images_flat, 2, tf.nn.relu)

定义损失函数

loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y, logits = logits))

定义优化器

train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

将 logits 转换为标签索引

correct_pred = tf.argmax(logits, 1)

定义准确度指标

accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)


tf.set_random_seed(1234)
sess = tf.Session()

sess.run(tf.global_variables_initializer())

for i in range(201):

        print('EPOCH', i)
        _, accuracy_val = sess.run([train_op, accuracy], feed_dict={x: 
        images[1], y: maskk})
        if i % 10 == 0:
            print("Loss: ", loss)
        print('DONE WITH EPOCH')

【问题讨论】:

  • 你能告诉我们你是从哪里得到这个错误的吗?

标签: python tensorflow machine-learning deep-learning


【解决方案1】:

听起来您缺少批量大小维度,请尝试np.expand_dims(image, dim=0)

【讨论】:

  • 非常感谢,成功了。但现在我收到标签排名错误。我有想要用作标签的蒙版图像。掩码图像有两种类型的值 1 和 0。我想在给定的掩码图像真相上训练输入图像的像素。现在我收到这个错误。 “ValueError:排名不匹配:标签排名(收到 2)应该等于 logits 排名减去 1(收到 2)。”我的会话运行是。 “sess.run([train_op, accuracy], feed_dict={x: np.expand_dims(images[1], axis=0), y: np.expand_dims(maskk, axis=1)})”。
猜你喜欢
  • 2018-08-03
  • 2020-11-18
  • 2019-02-01
  • 1970-01-01
  • 2019-07-23
  • 2023-03-19
  • 2018-04-29
  • 2018-04-05
相关资源
最近更新 更多