Python 在自己的图像集上读取训练记录时TensorFlow InvalidArgumeInterror

Python 在自己的图像集上读取训练记录时TensorFlow InvalidArgumeInterror,python,tensorflow,Python,Tensorflow,我正在尝试用TensorFlow训练我自己的图像(682x1024x3=2095104像素)。因此,我组合了几个已发布的脚本,1)使用TFRecord writer创建一个.tfrcords文件2)读取此记录,3)对其进行训练。 要读取图像,我从这里获取函数read_images(): 然后把它改成这些代码 def read_images(path_queue, is_directory=True): images = [] reader = tf.WholeFileReader

我正在尝试用TensorFlow训练我自己的图像(682x1024x3=2095104像素)。因此,我组合了几个已发布的脚本,1)使用TFRecord writer创建一个.tfrcords文件2)读取此记录,3)对其进行训练。 要读取图像,我从这里获取函数read_images(): 然后把它改成这些代码

def read_images(path_queue, is_directory=True):
    images = []
    reader = tf.WholeFileReader()
    jpeg_files = path_queue
    if len(jpeg_files) > 0:
       jpeg_file_queue = tf.train.string_input_producer(jpeg_files)
       jkey, jvalue = reader.read(jpeg_file_queue)
       j_img = tf.image.decode_jpeg(jvalue)
    with tf.Session() as sess:
        # Start populating the filename queue.
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        if len(jpeg_files) > 0:
           for i in range(len(jpeg_files)):
           jpeg = j_img.eval()
           images.append(jpeg)
    coord.request_stop()
    coord.join(threads)
return np.asarray(images)
read_images()的结果与标签列表和文件名一起传递给此处发布的convert_to()函数:

我在脚本中的更改在哪里

rows = images[0].shape[0]
cols = images[0].shape[1]
depth = images[0].shape[2]
至于for循环,我正在从字符串中删除labelId

for index in range(num_examples):
    print ('pics written: %d '%index)
    image_raw = images[index].tostring()
    print ('len: %d'%len(image_raw))
    label = labels[index]
    lbl = int(label[(len(label)-5):len(label)])
    example = tf.train.Example(features=tf.train.Features(feature={
        'height': _int64_feature(rows),
        'width': _int64_feature(cols),
        'depth': _int64_feature(depth),
        'label': _int64_feature(lbl),
        'image_raw': _bytes_feature(image_raw)}))
    writer.write(example.SerializeToString())
我在_int64_feature()和_bytes_feature()中没有做任何更改 调试人员在该点说我的变量值是:

  • 行=682列=1024深=3 len(image_raw)给了我2095104,等于像素
这就是我写唱片的方式。 要读取记录,我正在使用此脚本:

/master/tensorflow/g3doc/tutorials/mnist/mnist.py

还有这个:

/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_reader.py (sry仅允许2个链接)

我将vlaue 1000更改为capacity=10+3*batchsize和minu\u after\u deque=10 input()函数(最后一行):

现在我在创建TFRecord时没有任何问题(请参见),但当我运行整个脚本时,我会遇到以下错误:

W tensorflow/core/common_runtime/executor.cc:1027] 0x7fb89bae8860 Compute status: Invalid argument: Shape mismatch in tuple component 0. Expected [2095104], got [2101248]
 [[Node: input/shuffle_batch/random_shuffle_queue_enqueue = QueueEnqueue[Tcomponents=[DT_FLOAT, DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](input/shuffle_batch/random_shuffle_queue, input/sub, input/Cast_1)]]
也就是说,我期望,[2095104]=682x1024x3,得到了[2101248]=684x1024x3

所以我不知道这两个额外的像素是从哪里来的。有人能帮忙吗

第行的inputs()函数中出现错误(117个已发布代码)


将Alexander的评论答案转化为真实答案:

1.)将图像大小调整为1024x768

2.)再次将其调整为图像高度=192图像宽度=256。(你怎么看它只是除以4。)

OP使用了这个代码片段

im = Image.open(source_folders[i]).resize((1024,768)) 
im = im.resize((256,192),Image.ANTIALIAS)

也许图像处理是将大小四舍五入到4的倍数?您是否尝试过将不同大小的图像放入,以查看大小更改是否有规律?我将图像的大小调整为341x227x3,并创建了一个记录。读取记录时会出现类似错误:元组组件0中的形状不匹配。预期为[232221],获得[233244],目前我没有看到图案,但差异是1023,增加了3个像素-新尺寸是344,也是4的倍数。你能在x和y方向上都裁剪/调整到4的倍数吗?@barny当前没有。我试着用tensorflow'j_img=tf.image.decode_jpeg(jvalue,3,4)'来实现它,但没有成功。我找到了一些PIL的解决方案,其中图像在x和y方向都可以调整大小,但不要尝试它们。这周我会做这些。你应该把你的评论作为一个答案,因为人们通常不会从评论中寻找答案。:)
images, sparse_labels = tf.train.shuffle_batch(
    [image, label], batch_size=batch_size, num_threads=2,
    capacity=10 + 3 * batch_size,
    # Ensures a minimum amount of shuffling of examples.
    min_after_dequeue=10)
im = Image.open(source_folders[i]).resize((1024,768)) 
im = im.resize((256,192),Image.ANTIALIAS)