Я загружаю нейронную сеть с помощью tenorflow и colab notbook от Google. Я удалил полностью связанный слой выходного слоя и добавил еще один полностью связанный только с одним нейроном, и я заморозил другой слой. Я использую tf.keras.application.MobileNetV2 и использую mledu-datasets / cats_and_dogs. Я хочу тренировать только этот добавленный выходной слой, но я получаю «ошибку».
Мой код следующий:
_URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file('cats_and_dogs.zip', origin=_URL, extract=True)
PATH = os.path.join(os.path.dirname(path_to_zip), 'cats_and_dogs_filtered')
train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')
train_cats_dir = os.path.join(train_dir, 'cats') # directory with our training cat pictures
train_dogs_dir = os.path.join(train_dir, 'dogs') # directory with our training dog pictures
validation_cats_dir = os.path.join(validation_dir, 'cats') # directory with our validation cat pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs') # directory with our validation dog pictures
num_cats_tr = len(os.listdir(train_cats_dir))
num_dogs_tr = len(os.listdir(train_dogs_dir))
num_cats_val = len(os.listdir(validation_cats_dir))
num_dogs_val = len(os.listdir(validation_dogs_dir))
total_train = num_cats_tr + num_dogs_tr
total_val = num_cats_val + num_dogs_val
print('total training cat images:', num_cats_tr)
print('total training dog images:', num_dogs_tr)
print('total validation cat images:', num_cats_val)
print('total validation dog images:', num_dogs_val)
print("--")
print("Total training images:", total_train)
print("Total validation images:", total_val)
batch_size = 32
epochs = 15
IMG_HEIGHT = 160
IMG_WIDTH = 160
train_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our training data
validation_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our validation data
train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='binary')
val_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size,
directory=validation_dir,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='binary')
sample_training_images, _ = next(train_data_gen)
# This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column.
def plotImages(images_arr):
fig, axes = plt.subplots(1, 5, figsize=(20,20))
axes = axes.flatten()
for img, ax in zip( images_arr, axes):
ax.imshow(img)
ax.axis('off')
plt.tight_layout()
plt.show()
plotImages(sample_training_images[:5])
## Create the model
model = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(IMG_HEIGHT, IMG_WIDTH ,3), alpha=1.0, include_top=False, weights='imagenet', input_tensor=None , pooling='max', classes=2)
model.summary()
penultimate_layer = model.layers[-2] # layer that you want to connect your new FC layer to
new_top_layer = tf.keras.layers.Dense(1)(penultimate_layer.output) # create new FC layer and connect it to the rest of the model
new_new_top_layer = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)(new_top_layer)
new_model = tf.keras.models.Model(inputs=model.input, outputs=new_new_top_layer) # define your new model
new_model.summary()
for layer in new_model.layers[:-2]:
layer.trainable = False
new_model.layers[-1].trainable = True
Для обучения:
new_model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
У меня проблемы с этой частью кода
history = new_model.fit_generator(
train_data_gen,
steps_per_epoch = total_train // batch_size,
epochs = epochs,
validation_data = val_data_gen,
validation_steps = total_val // batch_size
)
Я получаю следующую ошибку.
Epoch 1/15
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-38-55517a65f99f> in <module>()
4 epochs = epochs,
5 validation_data = val_data_gen,
----> 6 validation_steps = total_val // batch_size
7 )
5 frames
/tensorflow-2.0.0/python3.6/tensorflow_core/python/keras/engine/training_utils.py in check_loss_and_target_compatibility(targets, loss_fns, output_shapes)
741 raise ValueError('A target array with shape ' + str(y.shape) +
742 ' was passed for an output of shape ' + str(shape) +
--> 743 ' while using as loss `' + loss_name + '`. '
744 'This loss expects targets to have the same shape '
745 'as the output.')
ValueError: A target array with shape (32, 1) was passed for an output of shape (None, 2, 2, 1) while using as loss `binary_crossentropy`. This loss expects targets to have the same shape as the output.
И мне нужно настроить сеть следующим образом:
batch_size = 32
epochs = 15
IMG_HEIGHT = 160
IMG_WIDTH = 160
спасибо