def eval(opt): GPU_setting(opt) img1 = Image.open(opt.data_path+'/S1/S1c03_frame_00103_SCTid_00010.jpg') img1 = np.array(img1)[np.newaxis,:] img2 = Image.open(opt.data_path+'/S1/S1c03_frame_00105_SCTid_00010.jpg') img2 = np.array(img2)[np.newaxis,:] if opt.model_name == 'ResNet152': # model_ = keras_applications.resnet.ResNet152(include_top=False, weights='imagenet', pooling='avg') model_ = resnet152_model(opt.image_size, opt.image_size, 3, opt.classes) model_.load_weights('./model.96-0.89.hdf5') model_extract_feature = Model(inputs=model_.input, outputs=model_.get_layer('avg_pool').output) else: print('model name error.') model_classify = Model(inputs=model_.input, outputs=model_.get_layer('fc8').output) model_classify.load_weights('./model.96-0.89.hdf5', by_name=True) class_1 = model_classify.predict(img1) class_2 = model_classify.predict(img2) print('class of img 1:', np.argmax(class_1)) print('class of img 2:', np.argmax(class_2)) pred_1 = model_extract_feature.predict(img1).reshape((2048)) pred_2 = model_extract_feature.predict(img2).reshape((2048)) norm_1 = np.linalg.norm(pred_1) norm_2 = np.linalg.norm(pred_2) pred_1 = pred_1.transpose() feature_dot = np.dot(pred_1, pred_2) cos = feature_dot / (norm_1 * norm_2) print('cos: ',cos)
def load_model(modelFile): model_weights_path = modelFile img_width, img_height = 224, 224 num_channels = 3 num_classes = 196 model = resnet152_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.96-0.89.hdf5' img_width, img_height = 224, 224 num_channels = 3 num_classes = 196 model = resnet152_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.63-0.93.hdf5' # img_width, img_height = 224, 224 num_channels = 3 num_classes = 60 # 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
def load_model(): global det_model global graph model_weights_path = 'models/model.96-0.89.hdf5' img_width, img_height = 224, 224 num_channels = 3 num_classes = 196 det_model = resnet152_model(img_height, img_width, num_channels, num_classes) det_model.load_weights(model_weights_path, by_name=True) graph = tf.get_default_graph()
def load_model(): # model_weights_path = 'models_brand_encar/model.58-0.95.hdf5' model_weights_path = 'models_brand_back/model.57-0.99.hdf5' # img_width, img_height = 224, 224 num_channels = 3 num_classes = 106 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
def load_pretrained_model(model_weights_path='models/model.96-0.89.hdf5'): """ Function: - loads a pretrained ResNet-152 model Argument: - model_weights_path (str): the relative path to model weights Returns: - model" the pretrained ResNet-152 model """ img_width, img_height = 224, 224 num_channels = 3 num_classes = 196 model = resnet152_model(img_height, img_width, num_channels, num_classes) model.load_weights(model_weights_path, by_name=True) return model
def load_model(): """ Loads the pre-trained and fine-tuned ResNet152 image classifier built in `resnet_152.py` Args: None Returns: None """ model_weights_path = 'models/model.96-0.89.hdf5' img_width, img_height = 224, 224 num_channels = 3 num_classes = 196 model = resnet152_model(img_height, img_width, num_channels, num_classes) model.load_weights(model_weights_path, by_name=True) return model
def get_Feature(self): GPU_setting(opt) print('\nloading model ...') if opt.model_name == 'ResNet152': # model_ = keras_applications.resnet.ResNet152(include_top=False, weights='imagenet', pooling='avg') model_ = resnet152_model(opt.image_size, opt.image_size, 3, opt.classes) model_ = Model(inputs=model_.input, outputs=model_.get_layer('avg_pool').output) model_.load_weights(opt.weight_path, by_name=True) # model_ = load_model(opt.weight_path, custom_objects = {'Scale': Scale}) print('model loaded.') else: print('model name error.') for i in range(len(self.img_name)): pred = model_.predict(self.train_img[i][np.newaxis,], batch_size=1, verbose=2) pred = np.reshape(pred,(2048)) # print('pred shape:', pred.shape) # np.save(opt.feature_path_padding + '/%s_res152/%s'%(SET[0][0:2],self.img_name[i]), pred) np.save(opt.feature_path + '/%s_res152_VeRI_color/%s'%(SET[0][0:2], self.img_name[i]), pred)
def train_(opt): GPU_setting(opt) train_img , train_label, valid_img, valid_label = load_data(opt) print('loading model ...') if opt.model_name == 'ResNet152V2': model_ = keras_applications.resnet_v2.ResNet152V2(include_top=True, weights='imagenet') # model_.load_weights('./resnet152_weights_tf.h5', by_name=True) elif opt.model_name == 'ResNet152': # model_ = keras_applications.resnet.ResNet152(include_top=False, weights='imagenet', pooling='avg') model_ = resnet152_model(opt.image_size, opt.image_size, 3, opt.classes) model_.load_weights('./model.96-0.89.hdf5') model_ = Model(inputs=model_.input, outputs=model_.output) elif opt.model_name == 'ResNet101V2': model_ = keras_applications.resnet_v2.ResNet101V2(include_top=True, weight='imagenet') elif opt.model_name == 'ResNet101': model_ = keras_applications.resnet.ResNet101(include_top=True, weights='imagenet') model_.load_weights('./ResNet101weight_epoch_243_val_acc_0.62983.hdf5', by_name=True) elif opt.model_name == 'ResNeXt101': model_ = keras_applications.resnext.ResNeXt101(include_top=True, weights='imagenet') elif opt.model_name == 'NASNetLarge': model_ = keras_applications.nasnet.NASNetLarge(include_top=True, weights='imagenet') elif opt.model_name == 'NASNetMobile': model_ = keras_applications.nasnet.NASNetMobile(include_top=True, weights='imagenet') elif opt.model_name == 'InceptionResNetV2': model_ = keras_applications.inception_resnet_v2.InceptionResNetV2(include_top=True, weights='imagenet') elif opt.model_name == 'vgg19': model_ = applications.vgg19.VGG19(include_top=True, weights='imagenet') else: print('model name error.') # x = model_.output # x = Dropout(0.1)(x) # x = GlobalAveragePooling2D()(x) # x = Dense(1000, activation='relu')(x) # x = Dense(512, activation='relu')(x) # predictions = Dense(opt.classes, activation='softmax')(x) # model = Model(inputs=model_.input, outputs=predictions) #for i, layer in enumerate(model_.layers): # print(i, layer.name) if opt.optimizer == 'adam': adam = Adam(lr=opt.learning_rate) model_.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy']) else: sgd = SGD(lr=opt.learning_rate, decay=1e-6, momentum=0.9, nesterov=True) model_.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) model_.summary() print('loading successful.') print('data_shape:') print('train_img:',train_img.shape) print('train_label:', train_label.shape) print('valid_img:', valid_img.shape) print('valid_label:', valid_label.shape) loss, acc = model_.evaluate(train_img, train_label, batch_size=opt.batch_size) print('loss:',loss,'\nacc:', acc) ''' for epoch in range(opt.epochs): print('\nTrue Epoch:%d'%(epoch+1)) model.fit(x=train_img, y=train_label, batch_size=opt.batch_size, epochs=1, validation_split=0.15, shuffle=True ) if (epoch+1) % opt.savemodel_interval == 0: model.save('model/Xception_model_epoch_{}.h5'.format((epoch+1))) model.save_weights('weight/Xception_weights_epoch_{}.h5'.format((epoch+1))) ''' ''' train_datagen = ImageDataGenerator( rotation_range=20, width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.2, horizontal_flip=True) valid_datagen = ImageDataGenerator() train_datagen.fit(train_img) valid_datagen.fit(valid_img) # callbacks patience = 50 tensor_board = TensorBoard( log_dir='./logs/new', histogram_freq=0, batch_size=opt.batch_size, write_graph=True, write_images=True) csv_logger = CSVLogger(opt.log_file_path, append=False) early_stop = EarlyStopping('val_acc', patience=patience) reduce_lr = ReduceLROnPlateau('val_acc', factor=0.1, patience=int(patience / 4), verbose=1, min_delta=0.0005) model_names = './models/' + opt.model_name + 'model_{epoch:02d}_val_acc_{val_acc:.5f}.hdf5' model_checkpoint = ModelCheckpoint( filepath=model_names, monitor='val_acc', verbose=1, save_best_only=True) weight_names = './weights/' + opt.model_name + 'weight_epoch_{epoch:02d}_val_acc_{val_acc:.5f}.hdf5' weight_checkpoint = ModelCheckpoint( filepath=weight_names, monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=True) callbacks = [tensor_board, model_checkpoint, weight_checkpoint, csv_logger, early_stop, reduce_lr] model.fit_generator( train_datagen.flow(train_img, train_label, batch_size=opt.batch_size), steps_per_epoch=train_img.shape[0] / opt.batch_size, validation_data=valid_datagen.flow(valid_img, valid_label, batch_size=opt.batch_size), validation_steps=valid_img.shape[0]/opt.batch_size, epochs=opt.epochs, callbacks=callbacks) ''' '''
from keras.callbacks import ReduceLROnPlateau img_width, img_height = 227, 227 num_channels = 3 train_data = 'mart/standford-cars-crop/train' valid_data = 'mart/standford-cars-crop/valid' num_classes = 196 num_train_samples = 6549 num_valid_samples = 1595 verbose = 1 batch_size = 16 num_epochs = 100000 patience = 50 # build a classifier model model = resnet152_model(img_height, img_width, num_channels, num_classes) # prepare data augmentation configuration train_data_gen = ImageDataGenerator(rescale=1 / 255., rotation_range=20., width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.2, horizontal_flip=True) valid_data_gen = ImageDataGenerator(rescale=1. / 255) # callbacks tensor_board = keras.callbacks.TensorBoard(log_dir='./logs', histogram_freq=0, write_graph=True, write_images=True)
def getResNet152(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 ResNet152 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, custom_objects={'Scale': customlayers.Scale}) else: model = resnet152_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, RESNET152_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) print(get_time_string() + 'Epoch ' + str(e) + ' complete. Evaluating on validation set..') evaluateYearbookFromModel(model=model, architecture=RESNET152_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
def get_model(self, train_only_top=False): weights_path = os.path.join('checkpoints', 'resnet152_weights_tf.h5') base_model = resnet152_model(weights_path) if train_only_top: for layer in base_model.layers: layer.trainable = False else: for layer in base_model.layers: layer.trainable = True resnet_1 = base_model.get_layer('conv1_relu').output resnet_2 = base_model.get_layer('res2c_relu').output resnet_3 = base_model.get_layer('res3b7_relu').output resnet_4 = base_model.get_layer('res4b35_relu').output resnet_5 = base_model.get_layer('res5c_relu').output c6 = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(resnet_5) u6 = self.upsample(filters=512, kernel_size=(3, 3), strides=(2, 2), padding='same')(c6) u6 = layers.concatenate([u6, resnet_4]) c6 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(u6) c6 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(c6) u7 = self.upsample(filters=256, kernel_size=(3, 3), strides=(2, 2), padding='same')(c6) u7 = layers.concatenate([u7, resnet_3]) c7 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(u7) c7 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(c7) u8 = self.upsample(filters=128, kernel_size=(3, 3), strides=(2, 2), padding='same')(c7) u8 = layers.Conv2D(128, (2, 2), strides=(1, 1), activation='relu', padding='valid')(u8) u8 = layers.concatenate([u8, resnet_2]) c8 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(u8) c8 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(c8) u9 = self.upsample(filters=64, kernel_size=(3, 3), strides=(2, 2), padding='valid', output_padding=(1, 1))(c8) u9 = layers.concatenate([u9, resnet_1], axis=3) c9 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(u9) c9 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(c9) u10 = self.upsample(filters=32, kernel_size=(3, 3), strides=(2, 2), padding='same')(c9) output = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(u10) return models.Model(inputs=base_model.input, outputs=[output])
def __init__(self, height=180, width=180, batch_size=64, max_epochs=6, base_model='inceptionResnetV2', num_classes=5270): self.height = height self.width = width self.batch_size = batch_size self.max_epochs = max_epochs self.base_model = base_model self.num_classes = num_classes self.make_category_tables() # Check if it works: print(self.cat2idx[1000012755], self.idx2cat[4]) if not os.path.exists("train_offsets.csv"): self.read_bson_files() else: self.train_offsets_df = pd.DataFrame.from_csv("train_offsets.csv") if not os.path.exists("train_images.csv") or not os.path.exists( "val_images.csv"): self.train_val_split() self.data_generator() # models = Models(input_shape=(self.height, self.width, 3), classes=self.num_classes) # if self.base_model == 'vgg16': # models.vgg16() # elif self.base_model == 'vgg19': # models.vgg19() # elif self.base_model == 'resnet50': # models.resnet50() # elif self.base_model == 'inceptionV3': # models.inceptionV3() # else: # print('Uknown base model') # raise SystemExit # # models.compile(optimizer=RMSprop(lr=1e-4)) # self.model = models.get_model() if self.base_model == 'resnet101': self.model = resnet101_model(self.height, self.width, color_type=3, num_classes=self.num_classes) elif self.base_model == 'resnet152': self.model = resnet152_model(self.height, self.width, color_type=3, num_classes=self.num_classes) else: models = Models(input_shape=(self.height, self.width, 3), classes=self.num_classes) if self.base_model == 'inceptionV4': models.inceptionV3() elif self.base_model == 'inceptionResnetV2': models.inceptionResnetV2() sgd = SGD(lr=1e-4, decay=1e-6, momentum=0.9, nesterov=True) models.compile(optimizer=sgd) self.model = models.get_model() self.model.summary()
def sliding_window(imageinput): posx = 0 posy = 0 cont = 0 classes = next(os.walk('1_Dataset/train'))[1] train, train_labels = loadData('1_Dataset/train', 224, 224, classes) test, test_labels = loadData('1_Dataset/test', 224, 224, classes) model_features = resnet152_model(224, 224, 3, len(classes)) features = getFeatures(model_features, train, 8) test_features = getFeatures(model_features, test, 8) prediction_model = Sequential() prediction_model.add( Dense(256, input_shape=features.shape[1:], activation='relu')) prediction_model.add(Dense(len(classes), activation='softmax')) sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True) prediction_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) prediction_model.fit(features, train_labels, epochs=10, batch_size=8) predictions = prediction_model.predict(test_features, batch_size=8, verbose=1) acc = getAccuracy(test_labels, predictions) print('############### current model accuracy ################') print(acc) print('#######################################################') if os.path.isfile(imageinput.file_input): image = Image.open(imageinput.file_input) size = image.size width = size[0] height = size[1] csv_path = os.path.splitext(imageinput.file_input)[0] + '.csv' csv_file = open(csv_path, 'w') csv_file.write( 'cont,label,score,divisor,posx,posy,posx,new_width,new_width,new_height,new_height,posy\n' ) for divisor in range(40, 41, 1): posx = 0 posy = 0 new_width = width / divisor new_height = height / divisor overlap_x = new_width * imageinput.overlapx overlap_y = new_height * imageinput.overlapy new_path = 'solution/divisor_' + str(divisor) if not os.path.isdir(new_path): os.makedirs(new_path) while new_height <= height: posx = 0 new_width = width / divisor while new_width <= width: cont += 1 box = (posx, posy, new_width, new_height) region = image.crop(box) new_name = new_path + '/' + str(cont) + 'image_' + str( posx) + '_' + str(posy) + '.jpg' region.save(new_name) img = np.array([ cv2.resize((cv2.imread(new_name)).astype(np.float32), (224, 224)) ]) img_features = getFeatures(model_features, img, 8) predictions = prediction_model.predict(img_features, batch_size=8, verbose=1) csv_file.write( '%i,Z,%f,%i,%i,%i,%i,%i,%i,%i,%i,%i\n' % (cont, predictions[0][0], divisor, posx, posy, posx, new_width, new_width, new_height, new_height, posy)) csv_file.write( '%i,S,%f,%i,%i,%i,%i,%i,%i,%i,%i,%i\n' % (cont, predictions[0][1], divisor, posx, posy, posx, new_width, new_width, new_height, new_height, posy)) posx += overlap_x new_width += overlap_x posy += overlap_y new_height += overlap_y image.close() else: print("image not found") sys.exit(1)
from keras.preprocessing.image import ImageDataGenerator from keras.callbacks import CSVLogger, ModelCheckpoint, EarlyStopping from keras.callbacks import ReduceLROnPlateau from resnet_152 import resnet152_model image_width, image_height = 224, 224 classes, train_samples, valid_samples = 196, 6549, 1595 batch_size = 4 epochs = 30 data_train_dir = 'data/train' data_valid_dir = 'data/valid' if __name__ == '__main__': model = resnet152_model(image_height, image_width, 3, classes) trainData, validData = ImageDataGenerator( rotation_range=20., width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.2, horizontal_flip=True), ImageDataGenerator() trainGen = trainData.flow_from_directory(data_train_dir, (image_width, image_height), batch_size=batch_size, class_mode='categorical') validGen = validData.flow_from_directory(data_valid_dir, (image_width, image_height), batch_size=batch_size,
channel = 3 num_classes = 10 batch_size = 8 nb_epoch = 10 epochs = 10 batchSize = 1024 #trainingScenario = "balanced_20100319_1030010004B92000_056030364010" trainingScenario = 'result' numOfClasses = 10 # Load Cifar10 data. Please implement your own load_data() module for your own dataset X_train, Y_train, X_valid, Y_valid = load_cifar10_data(img_rows, img_cols) # Load our model model = resnet152_model(img_rows, img_cols, channel, num_classes) model.summary() model.get_config() model.layers[0].get_config() model.layers[0].input_shape model.layers[0].output_shape model.layers[0].get_weights() #np.shape(model.layers[0].get_weights()[0]) model.layers[0].trainable # Start Fine-tuning hist = model.fit( X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, shuffle=True,