Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tensorflow.keras.applications import VGG16
- from tensorflow.keras.models import Model
- from tensorflow.keras.layers import Dense, Flatten
- from tensorflow.keras.preprocessing.image import ImageDataGenerator
- # Load the pre-trained VGG16 model
- base_model = VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
- # Freeze the layers except the last 4 layers
- for layer in base_model.layers[:-4]:
- layer.trainable = False
- # Add custom layers on top
- x = base_model.output
- x = Flatten()(x)
- x = Dense(512, activation='relu')(x)
- predictions = Dense(1, activation='sigmoid')(x)
- model = Model(inputs=base_model.input, outputs=predictions)
- # Compile the model
- model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
- # Prepare the augmented data generator
- train_datagen = ImageDataGenerator(rescale=1./255)
- train_generator = train_datagen.flow_from_directory(
- 'path_to_augmented_images',
- target_size=(150, 150),
- batch_size=32,
- class_mode='binary')
- # Train the model
- model.fit(train_generator, epochs=5, steps_per_epoch=20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement