在TensorFlow中通过tfds.load()导入斯坦福大学Dogs数据集时遇到问题

cyvaqqii  于 2022-11-25  发布在  其他
关注(0)|答案(1)|浏览(317)

我正在尝试使用斯坦福大学Dogs数据集来练习使用图像增强和创建图像分类器。
我想我的问题可能与我如何在normalize函数中调整图像大小有关。
代码如下:
第一个
错误:

Epoch 1/50
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)
<ipython-input-12-a35416ceec02> in <module>
     47     train_dataset,
     48     epochs = epochs,
---> 49     steps_per_epoch = 1200
     50 )

1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in tf__train_function(iterator)
     13                 try:
     14                     do_return = True
---> 15                     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16                 except:
     17                     do_return = False

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 889, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

    ValueError: Input 0 of layer "sequential_5" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(None, None, 3)

]
我试着在模型中使用tf.layers.resizing(),但是得到了一个关于输入形状的类似错误。

huwehgph

huwehgph1#

引发此错误的原因是train_dataset和输入形状之间的形状不匹配,这两个形状分别为(None,None,3)和(224,224,3)。必须首先使用normalize函数调整train数据集的大小,然后创建training_batches。

train_dataset=train_dataset.map(normalize)
training_batches = train_dataset.cache().shuffle(num_train_examples//4).batch(batch_size).prefetch(1)

然后,training_batches应该传递给fit()。

history = model.fit(
    training_batches,
    epochs = 10, 
    steps_per_epoch = 120
)

请参考此gist。谢谢!

相关问题