def top_layer(topNode=64): """ Return SqueezeNet model with specified output layer Call `show_layers()` too see list, and change topNode to desired output layer """ model = squeezenet.SqueezeNet() def show_layers(): [print(i, m.name) for i, m in enumerate(model.layers)] n = 67 - topNode for i in range(1, n): model.layers.pop() model.outputs = [model.layers[-i].output] model.layers[-i].outbound_nodes = [] print(model.layers[-1].name) return model
def train(train_dir, mean_image_path, batchsize, num_epochs, lr, weight_decay_l2, img_height, img_width, weights): # Make a './model' directory to store trained model and model parameters if not os.path.exists('./model'): os.makedirs('./model') # Data augmentation datagen = ImageDataGenerator(featurewise_center=True, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, zca_epsilon=1e-06, rotation_range=20, width_shift_range=0.1, height_shift_range=0.1, brightness_range=None, shear_range=0.01, zoom_range=0.1, channel_shift_range=0.0, fill_mode='nearest', cval=0.0, horizontal_flip=True, vertical_flip=False, rescale=None, preprocessing_function=None, data_format="channels_last", validation_split=0.25, dtype=None) # Dataset image mean to center the data img_mean_array = img_to_array( load_img(mean_image_path, target_size=(img_height, img_width))) datagen.mean = img_mean_array # Train generator train_generator = datagen.flow_from_directory( train_dir, target_size=(img_height, img_width), color_mode='rgb', batch_size=batchsize, class_mode='categorical', subset='training', # set as training data interpolation='bilinear') # Validation generator validation_generator = datagen.flow_from_directory( train_dir, # same directory as training data target_size=(img_height, img_width), color_mode='rgb', batch_size=batchsize, class_mode='categorical', subset='validation', # set as validation data interpolation='bilinear') classes = list(train_generator.class_indices.keys()) num_classes = len(classes) # SGD Optimizer opt = SGD(lr=lr, momentum=0.9, decay=0.0, nesterov=False) # Generate and compile the model model = squeezenet.SqueezeNet(num_classes, weight_decay_l2, inputs=(img_height, img_width, 3)) model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) # Steps per epoch step_size_train = train_generator.n // train_generator.batch_size step_size_valid = validation_generator.n // validation_generator.batch_size step_size_train = 5 step_size_valid = 2 # Linear LR decay after each batch update linearDecayLR = utils.LinearDecayLR(min_lr=1e-5, max_lr=lr, steps_per_epoch=step_size_train, epochs=num_epochs, verbose=1) # Add ImageNet weights if needed if weights == 'imagenet': model.load_weights(WEIGHTS_PATH, by_name=True) # Train the model print("Start training the model") training = model.fit_generator(train_generator, steps_per_epoch=step_size_train, validation_data=validation_generator, validation_steps=step_size_valid, epochs=num_epochs, verbose=1, callbacks=[linearDecayLR]) print("Model training finished") # Make model parameters to be used for prediction model_parms = { 'num_classes': num_classes, 'classes': classes, 'height': img_height, 'width': img_width } # Create the training history train_history = training.history return model, model_parms, train_history
image_size = 256 style_size = 256 content_layer = 3 style_layers = [1, 4, 6, 7] style_weights, content_weight = proc.get_weight(style_img_name) tv_weight = 5e-1 init_random = False initial_lr = 3.0 decayed_lr = 0.1 decay_lr_at = 180 max_iter = 200 # if not path.exists(save_path): # raise ValueError('Where is SqueezeNet ???') sess = proc.get_session() model = net.SqueezeNet(save_path, sess) """ Load data for testing """ content_img = proc.load_image(content_img_path, size=image_size) style_img = proc.load_image(style_img_path, size=image_size) content_img = proc.pre_process(content_img)[None] # turn shape into (1, 192, 256, 3) style_img = proc.pre_process(style_img)[None] answers = np.load('F:\\spring1617_assignment3_v3\\assignment3\\style-transfer-checks-tf.npz') """ Extract features""" # feats = model.extract_features(model.image) # content_target = sess.run(feats[content_layer], {model.image: content_img}) # style_feats = [feats[idx] for idx in style_layers] # style_targets = [] # for style_feat in style_feats: # feat = sess.run(style_feat, {model.image: style_img})
def get_model(): model = squeezenet.SqueezeNet() layer_outputs = [layer.output for layer in model.layers] return Model(input=model.input, output=layer_outputs)
import os import numpy as np import squeezenet as sq from keras.applications.imagenet_utils import preprocess_input, decode_predictions from keras.preprocessing import image model = sq.SqueezeNet() img = image.load_img('squeeze_test.jpg', target_size=(227, 227)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) print('Predicted:', decode_predictions(preds))
# -*- coding: utf-8 -*- import numpy as np from keras_squeezenet import SqueezeNet from tensorflow.keras.applications.imagenet_utils import preprocess_input, decode_predictions from keras.preprocessing import image model = SqueezeNet() import squeezenet model = squeezenet.SqueezeNet() model.summary()
# Input/Ouptut Parameters width = 224 >> 2 height = 224 >> 2 channel = 3 n_outputs = 10 model_name = "models/mobilenet/digists" data_path = "../data_img/MNIST/train/" # Step 0: Global Parameters epochs = 2 lr_rate = 0.0001 batch_size = 32 # Step 1: Create Model # model = squeezenetv2.SqueezeNetv2((None, height, width, channel), classes = n_outputs) model = squeezenet.SqueezeNet((None, height, width, channel), classes = n_outputs, , filters = 16) # Step 2: Define Metrics print(model.summary()) if sys.argv[1] == "train": # Step 3: Load data X_train, Y_train, X_valid, Y_valid = loader.load_light(data_path,width,height,True,0.8,True) # Step 4: Training #model.load_weights(model_name) # Define The Optimizer optimizer= tf.keras.optimizers.Adam(learning_rate=lr_rate) # Define The Loss