- import tensorflow as tf
- from tensorflow.keras.applications import VGG16
- from tensorflow.keras.layers import Dense, Flatten
- # Загрузка предварительно обученной модели VGG16 без последних полносвязных слоев
- base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
- # Заморозка всех слоев базовой модели
- for layer in base_model.layers:
- layer.trainable = False
- # Добавление полносвязных слоев сверху базовой модели
- model = tf.keras.Sequential([
- base_model,
- Flatten(),
- Dense(4096, activation='relu'),
- Dense(4096, activation='relu'),
- Dense(1, activation='sigmoid')
- ])
- # Компиляция модели
- model.compile(optimizer='adam',
- loss='binary_crossentropy',
- metrics=['accuracy'])
- # Загрузка и предобработка данных LFW
- # Предполагается, что данные LFW уже разделены на тренировочный и тестовый наборы
- train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
- rescale=1./255,
- shear_range=0.2,
- zoom_range=0.2,
- horizontal_flip=True)
- test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
- train_generator = train_datagen.flow_from_directory(
- '/path/to/train_dataset_directory',
- target_size=(224, 224),
- batch_size=32,
- class_mode='binary')
- test_generator = test_datagen.flow_from_directory(
- '/path/to/test_dataset_directory',
- target_size=(224, 224),
- batch_size=32,
- class_mode='binary')
- # Обучение модели
- model.fit(
- train_generator,
- steps_per_epoch=train_generator.samples // train_generator.batch_size,
- epochs=10,
- validation_data=test_generator,
- validation_steps=test_generator.samples // test_generator.batch_size)
- # Оценка модели
- loss, accuracy = model.evaluate(test_generator)
- print('Test loss:', loss)
- print('Test accuracy:', accuracy)
[text] Test
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: