python-3.x - ValueError:无法为形状为“(40、224、224、3)”的张量“Placeholder_4:0”提供形状(40、244、244)的值
问题描述
我是 TensorFlow 和机器学习的新手。我正在尝试提取图像的特征。我成功提取了 1 张图像的特征,但无法提取图像[] 这是脚本:
#read image
def readfile(pathdir):
paths = glob.glob(pathdir + "*.png")
img_data_list=[]
imgs = [cv2.imread(file,cv2.IMREAD_COLOR) for file in paths]
for img in imgs:
img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_resize=cv2.resize(img,(244,244),3)
img_data_list.append(img_resize)
img_data = np.array(img_data_list)
np.expand_dims(img_data, axis=0)
#img_data = array(img_data).reshape(1, 244,244,3)
names = [os.path.basename(path) for path in paths]
with open(pathdir + "value.txt") as file:
nvl= file.readlines()
nvl = {line.split()[0]:float(re.sub(r'\n', "", line.split()[1])) for line
in nvl}
value =[]
for i in names:
value.append(nvl[i])
return img_data, np.array(value), names
# extract features
def feature(train_imgs):
with tf.Session(config=tf.ConfigProto(gpu_options=
(tf.GPUOptions(per_process_gpu_memory_fraction=0.7)))) as sess:
#with tf.device('/cpu:0'):
#with tf.Session() as sess:
images = tf.placeholder("float", [np.shape(train_imgs)[0], 224,
224, 3])
[bunch of code]
return features
我收到以下错误:
ValueError Traceback (most recent call
last)
<ipython-input-23-b21a5d063db6> in feature(train_imgs)
47 vgg.build(images)
48 print("Extracting feature...")
---> 49 features = sess.run(vgg.fc6, feed_dict=feed_dict)
50 print('FC6 feature: ', features)
51 print('Number of input: ', len(features))
/usr/local/lib/python3.6/dist-
packages/tensorflow/python/client/session.py in run(self, fetches,
feed_dict, options, run_metadata)
927 try:
928 result = self._run(None, fetches, feed_dict, options_ptr,
--> 929 run_metadata_ptr)
930 if run_metadata:
931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python3.6/dist-
packages/tensorflow/python/client/session.py in _run(self, handle,
fetches, feed_dict, options, run_metadata)
1126 'which has shape %r' %
1127 (np_val.shape, subfeed_t.name,
-> 1128 str(subfeed_t.get_shape())))
1129 if not self.graph.is_feedable(subfeed_t):
1130 raise ValueError('Tensor %s may not be fed.' %
subfeed_t)
ValueError: Cannot feed value of shape (40, 244, 244) for Tensor
Placeholder_4:0', which has shape '(40, 224, 224, 3)'
出了什么问题以及如何解决此问题。预先感谢!!
解决方案
当您应用 cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 时,您将图像更改为灰度,仅获得一个颜色通道。您的图像的形状现在是 (?,244,244),并且您正在尝试输入形状 (?,244,244,3) 的占位符。如果要使用灰度图像,则应将图像集重新整形为 (?,244,244,1) 并使用相同形状的占位符。
推荐阅读
- react-native - 更改状态后组件未更新
- python - 如何用 python 克服这个 sql 连接错误?
- spring-boot - 为什么我在使用 Spring boot+ Angular 7 时收到 403 错误“CSRF 令牌丢失”?
- java - 通过创建会话登录后从数据库中检索到的jsp页面上未列出的数据
- python - 烧瓶:400 错误请求:KeyError:'密码'
- javascript - 我可以从嵌套数组中删除冗余数据结构包装器到嵌套 json 脚本吗?
- jenkins - Jenkins 没有为共享库使用给定的 SCM 凭据
- python - 如何修复 urllib2.URLError:
错误? - sas - 计算给定小时内接受治疗的患者人数
- jquery - jQuery多步骤表单,根据选择跳过步骤