对于FCN卷积神经网络,我具有以下代码部分
from PIL import Image
import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
IMAGE_SIZE = 101
train_label_path = '/home/mikimakie/down/kaggle/masks/'
train_path = '/home/mikimakie/down/kaggle/images/'
writer = tf.python_io.TFRecordWriter('train.tfrecords') def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
train_mask = os.listdir(train_label_path)
train_image = os.listdir(train_path)
#for img_name in os.listdir(data_path):
#print(img_name)
train_mask.sort()
train_image.sort()
num = 1
for ln, lm in zip(train_image, train_mask):
img_path = train_path + ln
label_path = train_label_path + lm
img = Image.open(img_path)
lab = Image.open(label_path)
try:
#img = img.resize((101, 101))
#lab = lab.resize((101, 101))
img_raw = img.tobytes()
lab_raw = lab.tobytes()
example = tf.train.Example(features=tf.train.Features(feature={"mask": _bytes_feature(lab_raw),
"image": _bytes_feature(img_raw)
}))
writer.write(example.SerializeToString())
except ValueError:
continue
writer.close()
print("finish to write data to tfrecord file!")
def read_and_decode(filename): *
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,
features={
'mask':tf.FixedLenFeature([], tf.string),
'image':tf.FixedLenFeature([], tf.string)
})
image = tf.decode_raw(features['image'], tf.uint8)
image = tf.reshape(image, [101, 101, 3])
mask = tf.decode_raw(features['mask'], tf.uint8)
mask = tf.reshape(mask, [101,101])
images, masks = tf.train.batch([image,mask],
batch_size=1,
num_threads=4,
capacity=1000)
return images, masks
if __name__=='__main__':
#a,b = read_and_decode(save)
save= '/home/mikimakie/down/kaggle/train.tfrecords'
pwd = '/home/mikimakie/down/kaggle/'
filename_queue = tf.train.string_input_producer([save],num_epochs=3)
images, masks = read_and_decode(save)
with tf.Session() as sess:
init = tf.global_variables_initializer()
init1= tf.local_variables_initializer()
sess.run(init)
sess.run(init1)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coord)
for i in range(4):
example1, example2 = sess.run([images, masks])
plt.subplot(4,2,i*2+1)
plt.imshow(example1)
plt.subplot(4, 2, i * 2 + 2)
coord.request_stop()
coord.join(threads)`enter code here`
这是我将图片和遮罩制作为tfrecord
的代码,我想检查它们是否相互对应,因此我将它们输出为图像。
但是...出现错误!
Tensor("batch:1", shape=(1, 101, 101), dtype=uint8)
Tensor("batch:0", shape=(1, 101, 101, 3), dtype=uint8)
2018-08-10 13:56:00.518159: W tensorflow/core/framework/op_kernel.cc:1192] Invalid argument: Input to reshape is a tensor with 40804 values, but the requested shape has 10201
[[Node: Reshape_1 = Reshape[T=DT_UINT8, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](DecodeRaw_1/_9, Reshape_1/shape)]]
Traceback (most recent call last):
File "/home/mikimakie/down/kaggle/record.py", line 127, in <module>
coord.join(threads)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/coordinator.py", line 389, in join
six.reraise(*self._exc_info_to_raise)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/queue_runner_impl.py", line 238, in _run
enqueue_callable()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1231, in _single_operation_run
target_list_as_strings, status, None)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 40804 values, but the requested shape has 10201
[[Node: Reshape_1 = Reshape[T=DT_UINT8, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](DecodeRaw_1/_9, Reshape_1/shape)]]
[[Node: Reshape_1/_15 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_22_Reshape_1", tensor_type=DT_UINT8, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Process finished with exit code 1
我不知道为什么会这样。我是新手。我现在感到无助。希望得到您的帮助。谢谢!
答案 0 :(得分:0)
问题似乎来自mask = tf.reshape(mask, [101,101])
。您的其中一个遮罩具有40804
值(例如形状(101, 101, 4)
或(202, 202)
,...),并且您试图将其重塑为10201
值的张量(参见形状(101, 101)
)。
确保所有图像/遮罩均具有预期的尺寸(例如,取消注释行img.resize((101, 101)
和lab = lab.resize((101, 101))
的注释,和/或使用tf.image.resize_images()
。