我是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)'
出了什么问题以及如何解决此问题。 预先感谢!!
答案 0 :(得分:0)
应用cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)时,您将图像更改为灰度,仅获得一个颜色通道。您图像的形状现在是(?,244,244),并且您正在尝试填充形状(?,244,244,3)的占位符。 如果要使用灰度图像,则应将图像集重塑为(?,244,244,1)并使用相同形状的占位符。