我正在尝试在此处找到的疟疾细胞图像数据集上训练CNN:https://www.kaggle.com/iarunava/cell-images-for-detecting-malaria
我已经指定了计算图,并且该模型能够编译。它也能够针对我目前正在测试的10个时期中的7-9个进行训练。但是,当训练过程到达纪元7、8或9时,它将失败并显示以下错误:
InvalidArgumentError: Incompatible shapes: [100,1] vs. [34,1]
错误的全文,包括训练进度条,在这里:
Epoch 1/10
20/20 [==============================] - 48s 2s/step - loss: 7.8437 - binary_accuracy: 0.5080
Epoch 2/10
20/20 [==============================] - 45s 2s/step - loss: 8.0509 - binary_accuracy: 0.4950
Epoch 3/10
20/20 [==============================] - 44s 2s/step - loss: 7.5806 - binary_accuracy: 0.5245
Epoch 4/10
20/20 [==============================] - 44s 2s/step - loss: 8.1147 - binary_accuracy: 0.4910
Epoch 5/10
20/20 [==============================] - 44s 2s/step - loss: 8.1306 - binary_accuracy: 0.4900
Epoch 6/10
20/20 [==============================] - 44s 2s/step - loss: 7.9313 - binary_accuracy: 0.5025
Epoch 7/10
20/20 [==============================] - 44s 2s/step - loss: 8.2741 - binary_accuracy: 0.4810
Epoch 8/10
20/20 [==============================] - 44s 2s/step - loss: 7.8676 - binary_accuracy: 0.5065
Epoch 9/10
5/20 [======>.......................] - ETA: 33s - loss: 7.8118 - binary_accuracy: 0.5100
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-16-a5ebd8d74728> in <module>
7 train_it,
8 steps_per_epoch = steps_per_ep,
----> 9 epochs = eps)
~/.local/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/.local/lib/python3.6/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1416 use_multiprocessing=use_multiprocessing,
1417 shuffle=shuffle,
-> 1418 initial_epoch=initial_epoch)
1419
1420 @interfaces.legacy_generator_methods_support
~/.local/lib/python3.6/site-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
215 outs = model.train_on_batch(x, y,
216 sample_weight=sample_weight,
--> 217 class_weight=class_weight)
218
219 outs = to_list(outs)
~/.local/lib/python3.6/site-packages/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight)
1215 ins = x + y + sample_weights
1216 self._make_train_function()
-> 1217 outputs = self.train_function(ins)
1218 return unpack_singleton(outputs)
1219
~/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
2713 return self._legacy_call(inputs)
2714
-> 2715 return self._call(inputs)
2716 else:
2717 if py_any(is_tensor(x) for x in inputs):
~/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
2674 else:
-> 2675 fetched = self._callable_fn(*array_vals)
2676 return fetched[:len(self.outputs)]
2677
~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1437 ret = tf_session.TF_SessionRunCallable(
1438 self._session._session, self._handle, args, status,
-> 1439 run_metadata_ptr)
1440 if run_metadata:
1441 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/.local/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
526 None, None,
527 compat.as_text(c_api.TF_Message(self.status.status)),
--> 528 c_api.TF_GetCode(self.status.status))
529 # Delete the underlying status object from memory otherwise it stays alive
530 # as there is a reference to status from this from the traceback due to
InvalidArgumentError: Incompatible shapes: [100,1] vs. [34,1]
[[{{node training/Adagrad/gradients/loss/dense_4_loss/logistic_loss/mul_grad/BroadcastGradientArgs}} = BroadcastGradientArgs[T=DT_INT32, _class=["loc:@train...ad/Reshape"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](training/Adagrad/gradients/loss/dense_4_loss/logistic_loss/mul_grad/Shape, training/Adagrad/gradients/loss/dense_4_loss/logistic_loss/mul_grad/Shape_1)]]
我试图以多种方式更改第四层完全连接的层,但没有任何运气。我还使用Keras的ImageDataGenerator
类来标准化图像。
这里有一个类似的问题:Keras/TF error: Incompatible shapes
不幸的是,他们提供的解决方案不适用于我的情况。
这是我的模型,其中包含ImageDataGenerator
对象:
# train batch size and image dims
batch_size = 100
inp_height = 128
inp_width = 128
# data generator/ preprocessor
datagen = ImageDataGenerator(
featurewise_center = False,
featurewise_std_normalization = False,
rescale = 1./255,
rotation_range = 180,
horizontal_flip = True,
vertical_flip = True,
data_format = "channels_last",
validation_split = 0.1
)
input_shape = (inp_height, inp_width, 3)
### train, test, validation iterators ###
# uniform key word arguments passed to each iterator
ffd_kwargs = {"target_size" : (inp_height, inp_width),
"class_mode" : "binary",
"batch_size" : batch_size,
"color_mode" : "rgb",
"seed" : 1}
train_it = datagen.flow_from_directory("./cell_images/train", **ffd_kwargs)
test_it = datagen.flow_from_directory("./cell_images/test", **ffd_kwargs)
valid_it = datagen.flow_from_directory("./cell_images/validation", **ffd_kwargs)
malaria_CNN = Sequential()
malaria_CNN.add(Conv2D(
input_shape = input_shape,
batch_size = batch_size,
data_format = "channels_last",
filters = 32,
kernel_size = (3, 3),
strides = (1, 1),
padding = "same",
activation = "relu"
))
malaria_CNN.add(Conv2D(
filters = 32,
kernel_size = (3, 3),
strides = (1, 1),
padding = "same",
activation = "relu"
))
malaria_CNN.add(MaxPooling2D(
pool_size = (3, 3),
strides = None,
padding = "same"))
#################################################
malaria_CNN.add(Conv2D(
filters = 64,
kernel_size = (3, 3),
strides = (1, 1),
padding = "same",
activation = "relu"
))
malaria_CNN.add(Conv2D(
filters = 64,
kernel_size = (3, 3),
strides = (1, 1),
padding = "same",
activation = "relu"
))
malaria_CNN.add(MaxPooling2D(
pool_size = (3, 3),
strides = None,
padding = "same"))
#################################################
malaria_CNN.add(Flatten())
malaria_CNN.add(Dense(
units = 256,
activation = "relu"))
malaria_CNN.add(Dense(
units = 64,
activation = "relu"))
malaria_CNN.add(Dense(
units = 32,
activation = "relu"))
malaria_CNN.add(Dense(
units = 1,
activation = "sigmoid"))
### compile malaria_CNN ###
adagrad_opt = Adagrad(
lr = 0.01,
epsilon = None,
decay = 0.0)
malaria_CNN.compile(
optimizer = "adagrad",
loss = "binary_crossentropy",
metrics = ["binary_accuracy"])
### train network ###
eps = 10
steps_per_ep = 20
history = malaria_CNN.fit_generator(
train_it,
steps_per_epoch = steps_per_ep,
epochs = eps)
过去,我已经训练了几个不同的CNN,而没有出现此错误。我在最后一个完全连接的层中做错了什么吗?我对这个问题不知所措。