Я создаю относительно простые CNN для многоклассовой классификации изображений в оттенках серого 128x128. Ниже приведен упрощенный фрагмент кода. Почему ошибка значения (на model.fit)? Должен ли я сначала преобразовать изображения в массивы numpy? Любое понимание было бы очень полезно.
Я использую: tenorflow.keras API (tf версия> 2.0) в pycharm (Ananconda 3) <12k изображений в оттенках серого (подкаталоги для каждого класса в папке поезда и без метки в тестовая папка) </p>
train_path = os.path.abspath("D:\plankton_cultures\Train")
test_path = os.path.abspath("D:\plankton_cultures")
save_dir = os.path.abspath("D:\plankton_cultures")
#Pre-defined parameters
IMAGE_LENGTH = 4
CLASS_SIZE = 19
input_img = (128,128,1)
######################### retrieving dataset #################################
train_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(
directory=train_path,
target_size=input_img,
color_mode="grayscale",
batch_size=BATCH_SIZE,
class_mode="categorical")
test_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(
directory=test_path,
target_size=input_img,
color_mode="grayscale",
batch_size=BATCH_SIZE,
classes=['Test'],
class_mode="categorical",
shuffle=False)
def model_function():
model = keras.Sequential()
model.add(Conv2D(64, (5, 5), padding='same', activation='relu', input_shape=input_img,
kernel_regularizer=regularizers.l2(0.0001)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), padding='same', activation='relu',
kernel_regularizer=regularizers.l2(0.0001)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(CLASS_SIZE, activation= 'softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['accuracy'])
return model
model = model_function()
model.summary()
history = model.fit_generator(train_generator, epochs=EPOCH, verbose= 1)
model.save("rps.h5")
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(EPOCH, acc, 'r', label='Training accuracy')
plt.plot(EPOCH, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
plt.show()
Epoch 1/50
Traceback (most recent call last):
File "D:/Keras_model (2).py", line 130, in <module>
history = model.fit_generator(train_generator, epochs=EPOCH, verbose= 1)
File "C:\Users\Dal\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line
1433, in fit_generator steps_name='steps_per_epoch')
File "C:\Users\Dal\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_generator.py",
line 220, in model_iteration batch_data = _get_next_batch(generator, mode)
File "C:\Users\Dal\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_generator.py",
line 362, in _get_next_batch
generator_output = next(generator)
File "C:\Users\Dal\anaconda3\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line
785, in get six.reraise(*sys.exc_info())
File "C:\Users\Dal\anaconda3\lib\site-packages\six.py", line 703, in raise value
File "C:\Users\Dal\anaconda3\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line
779, in get inputs = self.queue.get(block=True).get()
File "C:\Users\Dal\anaconda3\lib\multiprocessing\pool.py", line 657, in get raise self._value
File "C:\Users\Dal\anaconda3\lib\multiprocessing\pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "C:\Users\Dal\anaconda3\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line
571, in get_index return _SHARED_SEQUENCES[uid][i]
File "C:\Users\Dal\anaconda3\lib\site-packages\keras_preprocessing\image\iterator.py", line 65, in __
getitem__ return self._get_batches_of_transformed_samples(index_array)
File "C:\Users\Dal\anaconda3\lib\site-packages\keras_preprocessing\image\iterator.py", line 240, in
_get_batches_of_transformed_samples batch_x[i] = x
**ValueError: could not broadcast input array from shape (128,128,1) into shape (128,128,3,1)**