2017-05-14 30 views
-1

我試着寫一個tfrecords並讀取它,而且我已經得到'train.tfrecords'文件,但當我使用這個功能來讀取它InvalidArgumentError(參見上面的回溯):輸入重塑是一個有154587個值的張量,但所要求的形狀有150528

image_size=224 
def read_and_decode(filename,batch_size): 
    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={ 
    "label": tf.FixedLenFeature([],tf.int64), 
    "img_raw":tf.FixedLenFeature([],tf.string), 
    }) 
    img=tf.decode_raw(features['img_raw'],tf.uint8) 
    img=tf.cast(img,tf.float32) 
    img=tf.reshape(img,[image_size,image_size,3]) 
    img = tf.random_crop(img, [image_size, image_size, 3]) 
    img = tf.image.random_flip_left_right(img) 
    img=tf.image.per_image_standardization(img) 
    label=tf.cast(features['label'],tf.int32) 
    img_batch, label_batch = tf.train.shuffle_batch([img, label], 
               batch_size=batch_size, num_threads=10, capacity=16 * batch_size, 
               min_after_dequeue=8*batch_size) 
    label_batch= tf.reshape(label_batch, [batch_size, 1]) 
    indices = tf.reshape(tf.range(0, batch_size, 1), [batch_size, 1]) 
    label_batch = tf.sparse_to_dense(
    tf.concat(values=[indices, label_batch], axis=1), 
    [batch_size, 3], 1.0, 0.0) 
    assert len(img_batch.get_shape()) == 4 
    assert img_batch.get_shape()[0] == batch_size 
    assert img_batch.get_shape()[-1] == 3 
    assert len(label_batch.get_shape()) == 2 
    assert label_batch.get_shape()[0] == batch_size 
    assert label_batch.get_shape()[1] == 3 

    # Display the training images in the visualizer. 
    tf.summary.image('images', img_batch) 
    return img_batch, label_batch 

的錯誤是:

Caused by op 'Reshape', defined at: 
File "C:/Users/Administrator/Desktop/tensorflow/ResNet/main.py", line 176, in <module>tf.app.run() 
File"C:\ProgramFiles\Python35\lib\sitepackages\tensorflow\python\platform\app.py", line 48, in run 
_sys.exit(main(_sys.argv[:1] + flags_passthrough)) 
File "C:/Users/Administrator/Desktop/tensorflow/ResNet/main.py", line 169, in main 
train(hps) 
File "C:/Users/Administrator/Desktop/tensorflow/ResNet/main.py", line 31, in train 
images, labels = read_and_decode('train.tfrecords', hps.batch_size) 
File "C:\Users\Administrator\Desktop\tensorflow\ResNet\input.py", line 15, in read_and_decode 
img=tf.reshape(img,[image_size,image_size,3]) 
File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 2510, in reshape 
name=name) 
    File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op 
op_def=op_def) 
File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2336, in create_op 
original_op=self._default_original_op, op_def=op_def) 
File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1228, in __init__ 
self._traceback = _extract_stack() 
InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 154587 values, but the requested shape has 150528 
[[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](Cast, Reshape/shape)]] 

我在網上搜索了一會兒上,我認爲這個問題發生在這些代碼,但我不知道如何修復它,請幫助我,謝謝

img=tf.decode_raw(features['img_raw'],tf.uint8) 
img=tf.cast(img,tf.float32) 
img=tf.reshape(img,[image_size,image_size,3]) 
+1

確定記錄是224×224×3圖像而不是227×227×3圖像?嘗試改變image_size爲227. –

+0

哦...我沒有注意到,我準備TFRecords再次,它的工作原理,謝謝! –

回答

0

您可以在日誌中清楚地看到這一點,如@Mathias Rav在註釋中所述。

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 154587 values, but the requested shape has 150528 

TFrecords的大小是154587:227x227x3

但是,你提供的張量形狀的尺寸是150528:224x224x3

首先檢查模型所需的輸入大小並相應地調整圖像。 要麼,

  1. 調整大小圖像使用tf.image.resize_images()或
  2. 與大小再次準備TFRecords到224x224x3 224x224x3或
  3. 如果模型輸入大小是227x227x3,則只需改變IMAGE_SIZE至227 。

我希望這有助於。

+0

@Ge這是否解決了您的問題? – hars

+0

是的,我重寫了TFRecords,它很有用,非常感謝! –

+0

很高興知道! – hars

相關問題