Error : Input to reshape is a tensor with 327680 values, but the requested shape requires a multiple of 25088

Error : Input to reshape is a tensor with 327680 values, but the requested shape requires a multiple of 25088

我在 运行 我的模型时遇到错误。

Input to reshape is a tensor with 327680 values, but the requested shape requires a multiple of 25088

我正在使用 (256 * 256) 张图片。我正在 google colab 中读取来自驱动器的图像。谁能告诉我如何摆脱这个错误?

我的 colab code:

#defining the path to our data using the pathlib library
data_root = Path('/content/drive/MyDrive/Plant Disease detection/plantdisease_dataset')
print('data_root:', data_root) 

Dim = 256 #dimension of the image 
batch_size = 10 #batch size specified to be trained
Num_class = 38 #number of classes of the dataset

train_steps_per_epoch = 52424 // batch_size 
val_steps_per_epoch = 13097 // batch_size
test_steps_per_epoch = 154 // batch_size


datagen = ImageDataGenerator(rescale = 1./255, validation_split = 0.2)

train_gen = datagen.flow_from_directory(data_root/'/content/drive/MyDrive/Plant Disease detection/plantdisease_dataset', target_size = (Dim, Dim),batch_size = 10,subset = 'training') 
val_gen = datagen.flow_from_directory(data_root/'/content/drive/MyDrive/Plant Disease detection/plantdisease_dataset', target_size = (Dim, Dim), batch_size = 10,subset = 'validation')

#Found 52424 images belonging to 39 classes.
#Found 13097 images belonging to 39 classes. 

history = model.fit(
    train_gen,steps_per_epoch = train_steps_per_epoch,
    epochs = 100,
    validation_data = val_gen,
    validation_steps = val_steps_per_epoch,                      
)

错误信息:

Epoch 1/100
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-49-bb01bdcc02ea> in <module>()
      3     epochs = 100,
      4     validation_data = val_gen,
----> 5     validation_steps = val_steps_per_epoch,
      6     # callbacks  = [cb_checkpointer,cb_early_stopper,reducelr, tensorboard_callback]
      7 )

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     57     ctx.ensure_initialized()
     58     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 59                                         inputs, attrs, num_outputs)
     60   except core._NotOkStatusException as e:
     61     if name is not None:

InvalidArgumentError:  Input to reshape is a tensor with 327680 values, but the requested shape requires a multiple of 25088
     [[node sequential_1/flatten/Reshape
 (defined at /usr/local/lib/python3.7/dist-packages/keras/layers/core/flatten.py:96)
]] [Op:__inference_train_function_4466]

Errors may have originated from an input operation.
Input Source operations connected to node sequential_1/flatten/Reshape:
In[0] sequential_1/block5_pool/MaxPool (defined at /usr/local/lib/python3.7/dist-packages/keras/layers/pooling.py:362)  
In[1] sequential_1/flatten/Const (defined at /usr/local/lib/python3.7/dist-packages/keras/layers/core/flatten.py:91)

Operation defined at: (most recent call last)
>>>   File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
>>>     "__main__", mod_spec)
>>> 
>>>   File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
>>>     exec(code, run_globals)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
>>>     app.launch_new_instance()
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
>>>     app.start()
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 499, in start
>>>     self.io_loop.start()
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start
>>>     self.asyncio_loop.run_forever()
>>> 
>>>   File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
>>>     self._run_once()
>>> 
>>>   File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once
>>>     handle._run()
>>> 
>>>   File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
>>>     self._context.run(self._callback, *self._args)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 122, in _handle_events
>>>     handler_func(fileobj, events)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
>>>     return fn(*args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 452, in _handle_events
>>>     self._handle_recv()
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 481, in _handle_recv
>>>     self._run_callback(callback, msg)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 431, in _run_callback
>>>     callback(*args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
>>>     return fn(*args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
>>>     return self.dispatch_shell(stream, msg)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
>>>     handler(stream, idents, msg)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
>>>     user_expressions, allow_stdin)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute
>>>     res = shell.run_cell(code, store_history=store_history, silent=silent)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell
>>>     return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
>>>     interactivity=interactivity, compiler=compiler, result=result)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
>>>     if self.run_code(code, result):
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
>>>     exec(code_obj, self.user_global_ns, self.user_ns)
>>> 
>>>   File "<ipython-input-49-bb01bdcc02ea>", line 5, in <module>
>>>     validation_steps = val_steps_per_epoch,
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
>>>     return fn(*args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1216, in fit
>>>     tmp_logs = self.train_function(iterator)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function
>>>     return step_function(self, iterator)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, 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 860, in run_step
>>>     outputs = model.train_step(data)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 808, in train_step
>>>     y_pred = self(x, training=True)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
>>>     return fn(*args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py", line 1083, in __call__
>>>     outputs = call_fn(inputs, *args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 92, in error_handler
>>>     return fn(*args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/sequential.py", line 373, in call
>>>     return super(Sequential, self).call(inputs, training=training, mask=mask)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py", line 452, in call
>>>     inputs, training=training, mask=mask)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py", line 589, in _run_internal_graph
>>>     outputs = node.layer(*args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
>>>     return fn(*args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py", line 1083, in __call__
>>>     outputs = call_fn(inputs, *args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 92, in error_handler
>>>     return fn(*args, **kwargs)
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/layers/core/flatten.py", line 96, in call
>>>     return tf.reshape(inputs, flattened_shape)
>>> 

问题是 VGG19 模型使用的默认形状。您可以尝试替换输入形状并在输出层之前应用 Flatten 层。这是一个工作示例:

import tensorflow as tf
import pathlib

flowers = tf.keras.utils.get_file(
    'flower_photos',
    'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
    untar=True)

batch_size = 32
img_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, rotation_range=20)
ds = img_gen.flow_from_directory(flowers, batch_size=batch_size, target_size = (256, 256), shuffle=True)
train_steps_per_epoch = len(ds) // batch_size 

vgg19_model=tf.keras.applications.vgg19.VGG19(include_top=False, input_tensor=tf.keras.layers.Input(shape=(256, 256, 3)))
model = tf.keras.Sequential()
for layer in vgg19_model.layers[:-1]:
  model.add(layer)
for layer in model.layers:
  layer.trainable=False

model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(5, activation='softmax'))
model.summary()
model.compile(tf.keras.optimizers.Adam(learning_rate=0.0001), loss='categorical_crossentropy',metrics=['accuracy'])

history = model.fit(
    ds,
    steps_per_epoch = train_steps_per_epoch,
    epochs = 100                   
)

您还可以像这样将图像调整为 VGG19 模型的默认输入形状 (224, 224),它也可以在没有 Flatten 层的情况下工作:

flowers = tf.keras.utils.get_file(
    'flower_photos',
    'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
    untar=True)

batch_size = 32
img_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, rotation_range=20)
ds = img_gen.flow_from_directory(flowers, batch_size=batch_size, target_size = (224, 224), shuffle=True)
train_steps_per_epoch = len(ds) // batch_size 

vgg19_model=tf.keras.applications.vgg19.VGG19()
model = tf.keras.Sequential()
for layer in vgg19_model.layers[:-1]:
  model.add(layer)
for layer in model.layers:
  layer.trainable=False

model.add(tf.keras.layers.Dense(5, activation='softmax'))
model.summary()
model.compile(tf.keras.optimizers.Adam(learning_rate=0.0001), loss='categorical_crossentropy',metrics=['accuracy'])

history = model.fit(
    ds,
    steps_per_epoch = train_steps_per_epoch,
    epochs = 100                   
)