Created
April 28, 2017 18:48
Star
You must be signed in to star a gist
Keras VGG16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from keras.preprocessing.image import ImageDataGenerator | |
from keras.models import Sequential | |
from keras.layers import Dropout, Flatten, Dense | |
from keras import applications | |
# dimensions of our images. | |
img_width, img_height = 150, 150 | |
top_model_weights_path = '/home/paperspace/Downloads/bottleneck_fc_model.h5' | |
train_data_dir = '/home/paperspace/Documents/data/train' | |
validation_data_dir = '/home/paperspace/Documents/data/validation' | |
nb_train_samples = 44927 | |
nb_validation_samples = 5525 | |
epochs = 50 | |
batch_size = 16 | |
def save_bottlebeck_features(): | |
datagen = ImageDataGenerator(rescale=1. / 255) | |
# build the VGG16 network | |
model = applications.VGG16(include_top=False, weights='imagenet') | |
generator = datagen.flow_from_directory( | |
train_data_dir, | |
target_size=(img_width, img_height), | |
batch_size=batch_size, | |
class_mode=None, | |
shuffle=False) | |
bottleneck_features_train = model.predict_generator( | |
generator, nb_train_samples // batch_size) | |
np.save(open('bottleneck_features_train.npy', 'w'), | |
bottleneck_features_train) | |
generator = datagen.flow_from_directory( | |
validation_data_dir, | |
target_size=(img_width, img_height), | |
batch_size=batch_size, | |
class_mode=None, | |
shuffle=False) | |
bottleneck_features_validation = model.predict_generator( | |
generator, nb_validation_samples // batch_size) | |
np.save(open('bottleneck_features_validation.npy', 'w'), | |
bottleneck_features_validation) | |
def train_top_model(): | |
train_data = np.load(open('bottleneck_features_train.npy')) | |
train_labels = np.array( | |
[0] * (nb_train_samples / 2) + [1] * (nb_train_samples / 2)) | |
validation_data = np.load(open('bottleneck_features_validation.npy')) | |
validation_labels = np.array( | |
[0] * (nb_validation_samples / 2) + [1] * (nb_validation_samples / 2)) | |
model = Sequential() | |
model.add(Flatten(input_shape=train_data.shape[1:])) | |
model.add(Dense(256, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(14, activation='softmax')) | |
model.compile(optimizer='rmsprop', | |
loss='categorical_crossentropy', metrics=['accuracy']) | |
model.fit(train_data, train_labels, | |
epochs=epochs, | |
batch_size=batch_size, | |
validation_data=(validation_data, validation_labels)) | |
model.save_weights(top_model_weights_path) | |
save_bottlebeck_features() | |
train_top_model() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment