Last active
December 7, 2017 21:04
-
-
Save josete89/04e6a687281a217da40fa7800bc807c2 to your computer and use it in GitHub Desktop.
coremltools error
This file contains hidden or 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
from keras.engine import Model | |
from keras.layers import GlobalAveragePooling2D, Dense | |
from keras.optimizers import SGD | |
from keras.preprocessing.image import ImageDataGenerator | |
from keras.applications import InceptionV3 | |
from keras.applications.inception_v3 import preprocess_input | |
from coremltools.converters.keras import convert | |
from keras.models import load_model | |
train_data_dir = "./../data/train" | |
validation_data_dir = "./../data/validation" | |
img_width, img_height = 256, 256 | |
batch_size = 16 | |
NB_IV3_LAYERS_TO_FREEZE = 2 | |
train_datagen = ImageDataGenerator( | |
preprocessing_function=preprocess_input, | |
rotation_range=30, | |
width_shift_range=0.2, | |
height_shift_range=0.2, | |
shear_range=0.2, | |
zoom_range=0.2, | |
horizontal_flip=True | |
) | |
test_datagen = ImageDataGenerator( | |
preprocessing_function=preprocess_input, | |
rotation_range=30, | |
width_shift_range=0.2, | |
height_shift_range=0.2, | |
shear_range=0.2, | |
zoom_range=0.2, | |
horizontal_flip=True | |
) | |
train_generator = train_datagen.flow_from_directory( | |
train_data_dir, | |
target_size=(img_width, img_height), | |
batch_size=batch_size, | |
) | |
validation_generator = test_datagen.flow_from_directory( | |
validation_data_dir, | |
target_size=(img_width, img_height), | |
batch_size=batch_size, | |
) | |
def add_new_last_layer(base_model, nb_classes): | |
"""Add last layer to the convnet | |
Args: | |
base_model: keras model excluding top | |
nb_classes: # of classes | |
Returns: | |
new keras model with last layer | |
""" | |
x = base_model.output | |
x = GlobalAveragePooling2D()(x) | |
x = Dense(1024, activation='relu')(x) | |
predictions = Dense(nb_classes, activation='softmax')(x) | |
model = Model(input=base_model.input, output=predictions) | |
return model | |
def setup_to_transfer_learn(model, base_model): | |
"""Freeze all layers and compile the model""" | |
for layer in base_model.layers: | |
layer.trainable = False | |
model.compile(optimizer='rmsprop', | |
loss='categorical_crossentropy', | |
metrics=['accuracy']) | |
def setup_to_finetune(model): | |
"""Freeze the bottom NB_IV3_LAYERS and retrain the remaining top | |
layers. | |
note: NB_IV3_LAYERS corresponds to the top 2 inception blocks in | |
the inceptionv3 architecture | |
Args: | |
model: keras model | |
""" | |
for layer in model.layers[:NB_IV3_LAYERS_TO_FREEZE]: | |
layer.trainable = False | |
for layer in model.layers[NB_IV3_LAYERS_TO_FREEZE:]: | |
layer.trainable = True | |
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), | |
loss='binary_crossentropy') | |
def saveToCoreml(model): | |
coreml_model = convert(model,image_input_names="input_1") | |
coreml_model.save('image_classifier.mlmodel') | |
if __name__ == '__main__': | |
base_model = InceptionV3(weights='imagenet', include_top=False) | |
model = add_new_last_layer(base_model,2) | |
setup_to_transfer_learn(model,base_model) | |
print model.summary() | |
nb_epoch = 2 | |
model.fit( | |
train_generator, | |
samples_per_epoch=128, | |
nb_epoch=nb_epoch, | |
validation_data=validation_generator, | |
nb_val_samples=64, | |
class_weight='auto') | |
saveToCoreml(model) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment