Skip to content

Instantly share code, notes, and snippets.

@xpngzhng
Created August 21, 2018 01:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xpngzhng/1e20d54a70c8e04912c0b37fa7e7b931 to your computer and use it in GitHub Desktop.
Save xpngzhng/1e20d54a70c8e04912c0b37fa7e7b931 to your computer and use it in GitHub Desktop.
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
from keras.preprocessing.image import ImageDataGenerator
from keras import callbacks
train_dir = 'D:\\Meitu\\Slim\\train'
validation_dir = 'D:\\Meitu\\Slim\\val'
save_dir = './result'
image_size = 224
os.makedirs(save_dir, exist_ok=True)
model_type = 'vgg16'
model_type = 'resnet50'
model_type = 'inceptionv3'
from keras import models
from keras import layers
from keras import optimizers
# Create the model
model = models.Sequential()
assert model_type in ['vgg16', 'resnet50', 'inceptionv3']
if model_type == 'vgg16':
from keras.applications import VGG16
vgg_conv = VGG16(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3))
for layer in vgg_conv.layers[:-4]:
layer.trainable = False
for layer in vgg_conv.layers:
print(layer, layer.trainable)
model.add(vgg_conv)
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(50, activation='softmax'))
elif model_type == 'resnet50':
from keras.applications import ResNet50
resnet50_conv = ResNet50(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3))
for i, layer in enumerate(resnet50_conv.layers):
print(i, layer.name)
for layer in resnet50_conv.layers[:80]:
layer.trainable = False
for layer in resnet50_conv.layers:
if 'bn' in layer.name:
layer.trainable = False
for i, layer in enumerate(resnet50_conv.layers):
print(i, layer.name, layer.trainable)
model.add(resnet50_conv)
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(50, activation='softmax'))
elif model_type == 'inceptionv3':
from keras.applications import InceptionV3
inceptionv3_conv = InceptionV3(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3))
for layer in inceptionv3_conv.layers[:249]:
layer.trainable = False
for layer in inceptionv3_conv.layers[249:]:
layer.trainable = True
for i, layer in enumerate(inceptionv3_conv.layers):
print(i, layer.name, layer.trainable)
model.add(inceptionv3_conv)
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(50, activation='softmax'))
# Show a summary of the model. Check the number of trainable parameters
model.summary()
# train_datagen = ImageDataGenerator(
# rescale=1./255,
# rotation_range=20,
# width_shift_range=0.2,
# height_shift_range=0.2,
# horizontal_flip=True,
# fill_mode='nearest')
train_datagen = ImageDataGenerator(rescale=1./255)
validation_datagen = ImageDataGenerator(rescale=1./255)
# Change the batchsize according to your system RAM
train_batchsize = 100
val_batchsize = 100
# Data Generator for Training data
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(image_size, image_size),
batch_size=train_batchsize,
class_mode='categorical')
print('train_generator\'s class indices:')
print(train_generator.class_indices)
# Data Generator for Validation data
validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(image_size, image_size),
batch_size=val_batchsize,
class_mode='categorical',
shuffle=False)
print('validation_generator\'s class indices:')
print(validation_generator.class_indices)
# exit()
# Select optimizer
sgd = optimizers.SGD(lr=0.001, decay=1e-4, momentum=0.9, nesterov=True)
rms_prop = optimizers.RMSprop(lr=1e-4)
adam = optimizers.adam(lr=0.0001)
# Compile the model
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['acc'])
# Add callbacks
save_model_cb = callbacks.ModelCheckpoint(os.path.join(save_dir, '{epoch:02d}.hdf5'), save_best_only=False, verbose=1)
log_cb = callbacks.CSVLogger(os.path.join(save_dir, 'log.csv'), append=True, separator=';')
reduce_lr_cb = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=4, verbose=1)
cbs = [save_model_cb, log_cb, reduce_lr_cb]
# Train the Model
history = model.fit_generator(
train_generator,
steps_per_epoch=train_generator.samples/train_generator.batch_size ,
epochs=20,
validation_data=validation_generator,
validation_steps=validation_generator.samples/validation_generator.batch_size,
verbose=1,
callbacks=cbs,
workers=2)
@bulentsiyah
Copy link

thank you so much. great work

@Sri-Harish-Kalidass
Copy link

Hi Xuping,

I trained my model with the inceptionV3 in the same way(like from above) - created a sequential model, added inceptionv3 model (with include top = false and imagenet weights) and added the following;
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(50, activation='softmax'))

I have successfully trained my model and I have my weights and model with me. How should i make predictions?

Any assistance would be greatly useful.

Regards
Sri

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment