Created
May 12, 2018 08:01
-
-
Save Madhivarman/676650f71ec35a5f2802631fcfa0ff73 to your computer and use it in GitHub Desktop.
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 tensorflow as tf | |
import os | |
from tensorflow.python.keras.applications import ResNet50 | |
from tensorflow.python.keras.models import Sequential | |
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D | |
from tensorflow.python.keras.applications.resnet50 import preprocess_input | |
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator | |
#reset default graph | |
tf.reset_default_graph() | |
IMG_SIZE = 224 | |
num_classes = 2 | |
resnet_weight_paths = 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5' | |
SAVE_MODEL = 'catvsdog_trainedmodel.hd5' | |
new_model = Sequential() | |
new_model.add(ResNet50(include_top=False, | |
pooling='avg', | |
weights=resnet_weight_paths)) | |
new_model.add(Dense(num_classes,activation = 'softmax')) | |
#not to train first layer (ResNet) model. It is already trained | |
new_model.layers[0].trainable = False | |
#now compile the model | |
new_model.compile(optimizer='sgd',loss='categorical_crossentropy',metrics=['accuracy']) | |
#Fit model | |
data_generator = ImageDataGenerator() | |
train_generator = data_generator.flow_from_directory( | |
'/media/madhi/347b8fa1-b2fa-499a-a0da-3dc2835341ac/madhi/Documents/python programs/intern/signzy/Assignment_2/training_set', | |
target_size = (IMG_SIZE,IMG_SIZE), | |
batch_size = 12, | |
class_mode = 'categorical' | |
) | |
validation_generator = data_generator.flow_from_directory( | |
'/media/madhi/347b8fa1-b2fa-499a-a0da-3dc2835341ac/madhi/Documents/python programs/intern/signzy/Assignment_2/validation_set', | |
target_size = (IMG_SIZE,IMG_SIZE), | |
class_mode = 'categorical' | |
) | |
#compile the model | |
new_model.fit_generator( | |
train_generator, | |
steps_per_epoch = 3, | |
validation_data = validation_generator, | |
validation_steps = 1 | |
) | |
new_model.save(SAVE_MODEL) | |
print("Model is Saved..!") | |
#score trained model | |
scores = new_model.evaluate(validation_generator) | |
print("Test Loss:{}".format(scores[0])) | |
print("Test Accuracy:{}".format(scores[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment