Я пытался построить модель гендерной классификации (2 класса) с использованием увеличения данных.
Мои данные состоят из 2 папок с именами «мужчины» и «женщины», в которых всего 3339 изображений. datapath = 'data /' внутри папки данных у меня есть две папки классов с именами men и women.
Вот моя модель и сводка модели:
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(5,5), padding='same', kernel_initializer='he_normal', input_shape=(128,128,3)))
model.add(Activation('relu'))
model.add(Conv2D(filters=32, kernel_size=(5,5), padding='same', kernel_initializer='he_normal'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(5,5), padding='same', kernel_initializer='he_normal'))
model.add(Activation('relu'))
model.add(Conv2D(filters=64, kernel_size=(5,5), padding='same', kernel_initializer='he_normal'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(units=256, activation='relu'))
model.add(Dense(units=num_classes, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
hist = model.fit(datagen.flow(x_train, y_train, batch_size=32),
epochs=30,
steps_per_epoch=x_train.shape[0]//32,
validation_data=(x_test,y_test),
validation_steps=x_test.shape[0]//32,
verbose=1,
callbacks=callbacks)
сводка модели:
Model: "sequential_5"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_17 (Conv2D) (None, 128, 128, 32) 2432
_________________________________________________________________
activation_16 (Activation) (None, 128, 128, 32) 0
_________________________________________________________________
conv2d_18 (Conv2D) (None, 128, 128, 32) 25632
_________________________________________________________________
activation_17 (Activation) (None, 128, 128, 32) 0
_________________________________________________________________
max_pooling2d_8 (MaxPooling2 (None, 64, 64, 32) 0
_________________________________________________________________
dropout_8 (Dropout) (None, 64, 64, 32) 0
_________________________________________________________________
conv2d_19 (Conv2D) (None, 64, 64, 64) 51264
_________________________________________________________________
activation_18 (Activation) (None, 64, 64, 64) 0
_________________________________________________________________
conv2d_20 (Conv2D) (None, 64, 64, 64) 102464
_________________________________________________________________
activation_19 (Activation) (None, 64, 64, 64) 0
_________________________________________________________________
max_pooling2d_9 (MaxPooling2 (None, 32, 32, 64) 0
_________________________________________________________________
dropout_9 (Dropout) (None, 32, 32, 64) 0
_________________________________________________________________
flatten_4 (Flatten) (None, 65536) 0
_________________________________________________________________
dense_8 (Dense) (None, 256) 16777472
_________________________________________________________________
dense_9 (Dense) (None, 2) 514
=================================================================
Total params: 16,959,778
Trainable params: 16,959,778
Non-trainable params: 0
Мои формы данных для обучения и тестирования:
x_train.shape = (3005, 224, 224, 3)
x_test.shape = (334, 224, 224, 3)
y_train.shape = (3005, 2)
y_test.shape = (334, 2)
Следующая ошибка:
Epoch 1/30
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-42-f9e107b66258> in <module>()
13 validation_steps=x_test.shape[0]//32,
14 verbose=1,
---> 15 callbacks=callbacks)
8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
InvalidArgumentError: Incompatible shapes: [98,2] vs. [32,2]
[[node gradient_tape/binary_crossentropy/logistic_loss/mul/BroadcastGradientArgs (defined at <ipython-input-42-f9e107b66258>:15) ]] [Op:__inference_train_function_5749]
Function call stack:
train_function