【问题标题】:InvalidArgumentError: Input to reshape is a tensor with 178802 values, but the requested shape has 89401InvalidArgumentError:reshape 的输入是具有 178802 个值的张量,但请求的形状具有 89401
【发布时间】:2017-11-18 03:50:16
【问题描述】:

我遇到了另一个无效参数错误,我不确定这次是什么原因。

我创建了一个 TFRecord,其中包含形状为 [299,299] 的图像(据我所知是混合扩展)。

我正在尝试批量加载图像,但我遇到了这个错误:

'InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 178802 values, but the requested shape has 89401
     [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](DecodeRaw, Reshape/shape)]]

这是我的代码:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os

IMAGE_DIR =r'C:\Users\Moondra\Desktop\TF_FISH_PROJECT\FINAL_FISHES'

data_path = r'E:\TFRECORDS\normal_fish_conversion_2.tfrecords'  

with tf.Session() as sess:
    feature = {'train/image': tf.FixedLenFeature([], tf.string),
               'train/label': tf.FixedLenFeature([], tf.int64),
               'rows':  tf.FixedLenFeature([], tf.int64),
                'columns':  tf.FixedLenFeature([], tf.int64)}

    # Create a list of filenames and pass it to a queue
    filename_queue = tf.train.string_input_producer([data_path], num_epochs=1000)

    # Define a reader and read the next record
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)

    # Decode the record read by the reader
    features = tf.parse_single_example(serialized_example, features=feature)

    # Convert the image data from string back to the numbers
    image = tf.decode_raw(features['train/image'], tf.float32)

    # Cast label data into int32
    label = tf.cast(features['train/label'], tf.int32)

    # Reshape image data into the original shape
    image = tf.reshape(image, [299, 299])
    print(image.shape) #shape is printing out correctly


    # Creates batches by randomly shuffling tensors
    #images, labels = tf.train.shuffle_batch([image, label], batch_size=50, capacity=10000, num_threads=3, min_after_dequeue=2000)
    init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    sess.run(init_op)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    for batch_index in range(5):
            img  = sess.run([image])
            img = img.astype(np.uint8)
            print(img.shape)





    coord.request_stop()
    coord.join(threads)
    sess.close()

我不确定如何调试这个..

第一个打印语句(reshape_image.shape)正在打印出一个 (299,299) 形状,所以不确定是什么问题。

谢谢。

【问题讨论】:

  • 重塑只会改变张量维度的比率。您的原始图像似乎不适合目标矩阵。在重塑之前缩放图像怎么样?
  • 缩放是什么意思?至于原始图像,我在将它们写入 TFrecords 之前明确地将它们调整为 [299,299]。那么我的原始图像不应该有 299x299 像素吗?谢谢。
  • 我只是想确认一下。例如,如果图像是 FullHD,则无法调整为目标大小。所以问题出在其他地方。看着它。编码没问题?像解码raw一样,数据是JPEG在哪里? Inception 教程展示了一个带有 JPEG 图像的示例。
  • 如果不看数据,我很难说出更多信息。可能是其中的图像尺寸不正确。代码看起来不错。也许您可以打印每个 decode_raw 的长度,然后查看 178802 何时出现,令人惊讶的是 2 x 299 x 299。
  • @EricPlaton 我在编码为字符串字节时(在写入 TFRecords 之前)没有遇到任何问题。但是,我认为我的图像使用了不同的扩展名(除了 jpegs)。我想知道这是否会导致问题。我将尝试打印出decode_raw 长度并查看 178802 是否是异常值。非常感谢您的帮助。

标签: python python-3.x tensorflow deep-learning


【解决方案1】:

我需要做的是将图像解码为 JPEG,将其转换为浮点数,扩展其尺寸,然后使用双线性插值调整其大小,如下所示:

image = tf.image.decode_jpeg(features['train/image'], channels=3)
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(image, [299, 299], align_corners=False)

注意:

  • 您的图像应该已经以 JPEG 格式存储(在创建 TFRecords 时)。
  • 如果您的图像是灰度图像,您可以改为将 channels 设置为 1,或者将每个图像的通道数保存在 TFRecords 中并从那里动态获取(每个图像都不同)。

【讨论】:

    猜你喜欢
    • 2022-01-15
    • 2020-02-15
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多