def load_model(): model_weights_path = 'models/model.89-0.90.hdf5' img_width, img_height = 224, 224 num_channels = 3 num_classes = 196 model = resnet50_model(img_height, img_width, num_channels, num_classes) model.load_weights(model_weights_path, by_name=True) return model
def load_model(): model_weights_path = 'G:/fyp/resnet_50/model/epoch_100_aug/model.48-0.89.hdf5' img_width, img_height = 224, 224 num_channels = 3 num_classes = 196 model = resnet50_model(img_height, img_width, num_channels, num_classes) model.load_weights(model_weights_path, by_name=True) return model
def load_model(): model_weights_path = 'models/model.09-0.98.hdf5' # img_width, img_height = 224, 224 num_channels = 3 num_classes = 3 # num_classes = 196 # model = resnet152_model(IMG_WIDTH, IMG_HEIGHT, num_channels, num_classes) model = resnet50_model(IMG_WIDTH, IMG_HEIGHT, num_channels, num_classes) model.load_weights(model_weights_path, by_name=True) return model
import keras from resnet_50 import resnet50_model from keras.preprocessing.image import ImageDataGenerator from keras.callbacks import CSVLogger, ModelCheckpoint, EarlyStopping from keras.callbacks import ReduceLROnPlateau from keras.models import Model img_width, img_height = 224, 224 num_channels = 3 num_classes = 196 resnet = resnet50_model(img_height, img_width, num_channels, num_classes) for layer in resnet.layers: layer.trainable = False model = Model(inputs=resnet.input, outputs=resnet.output) model.summary()
def getResNet50(self, train_images, train_labels, load_saved_model, model_save_path, use_pretraining, pretrained_weights_path, train_dir, val_dir, fine_tuning_method, batch_size, num_epochs, optimizer, loss, initial_epoch, sample, lr=None): """ :param load_saved_model: boolean (whether to just load the model from weights path) :param model_save_path: (final model weights path, if load_pretrained is true) :param pretrained_weights_path: if load_trained is false and if use_pretraining is true, the path of weights to load for pre-training :param train_dir: training data directory :param val_dir: validation data directory :param use_pretraining: boolean, whether to use pre-training or train from scratch :param fine_tuning_method: whether to use end-to-end pre-training or phase-by-phase pre-training :param initial_epoch: starting epoch to start training :return: Returns the AlexNet model according to the parameters provided """ print(get_time_string() + 'Creating ResNet50 model..') img_rows, img_cols = 224, 224 # Resolution of inputs channels = 3 if load_saved_model: if model_save_path is None: raise Exception('Unable to load trained model as model_save_path is None!') print(get_time_string() + 'Loading saved model from ' + model_save_path + '..') model = load_model(model_save_path) else: model = resnet50_model(img_rows, img_cols, channels, NUM_CLASSES_YEARBOOK, use_pretraining, pretrained_weights_path, fine_tuning_method, optimizer, loss) if initial_epoch >= num_epochs: print(get_time_string() + 'Not fitting the model since initial_epoch is >= num_epochs. Returning model..') return model # Start Fine-tuning print(get_time_string() + 'Fitting the model..') for e in range(initial_epoch, num_epochs): print_line() print('Starting epoch ' + str(e)) print_line() completed = 0 for x_chunk, y_chunk in chunks(train_images, train_labels, batch_size, RESNET50_ARCHITECTURE): print(get_time_string() + 'Fitting model for chunk of size ' + str(len(x_chunk)) + '...') model.fit(x_chunk, y_chunk, batch_size=batch_size, nb_epoch=1, verbose=1 ) completed += len(x_chunk) print(get_time_string() + str(completed) + ' of ' + str(len(train_images)) + ' complete. ') file_name = self.getCheckpointFileName(base_model_save_path=model_save_path, epoch=e) print(get_time_string() + 'Saving model to ' + file_name) model.save(file_name) if (e % 5 == 0): print(get_time_string() + 'Epoch ' + str(e) + ' complete. Evaluating on validation set..') evaluateYearbookFromModel(model=model, architecture=RESNET50_ARCHITECTURE, sample=sample) print_line() print(get_time_string() + 'Fitting complete. Returning model..') if model_save_path is not None: print(get_time_string() + 'Saving final model to ' + model_save_path + '..') model.save(model_save_path) return model