Exemple #1
0
	def set_up(self,add_dropout=True,feature_extractor=2):
		#self.create_input_structure()

		if feature_extractor == 2:#ResNet50
			weights_path='./transfer_learning/resnet50weights/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
			base_model=ResNet50(include_top=False,input_shape=input_shape,weights=weights_path)



			self.model = Sequential()

			self.model.add(base_model)

			self.create_classifier_layers(add_dropout=add_dropout)



		elif feature_extractor ==3:
			weights_path = './transfer_learning/vgg19/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5'
			base_model = VGG19(include_top=False, input_shape=input_shape,
										  weights=weights_path)

			self.model = Sequential()

			self.model.add(base_model)

			self.create_classifier_layers(add_dropout=add_dropout)

		else:
			raise Exception('Feature extractor not defined')
Exemple #2
0
def resnet_trained_2(n_retrain_layers=0):
    K.set_image_data_format('channels_last')
    base_model = ResNet50(include_top=False, input_shape=(224, 224, 3))
    features = GlobalAveragePooling2D()(base_model.output)
    model = Model(inputs=base_model.input, outputs=features)
    model = _set_n_retrain(model, n_retrain_layers, reinit=True)
    return model
Exemple #3
0
def get_model(use_model) -> Model:
    if use_model == 'MobileNetV2':
        conv_model: Model = MobileNetV2(weights='imagenet', include_top=False, input_shape=IMG_SHAPE)

    else:
        conv_model: Model = ResNet50(weights='imagenet', include_top=False, input_shape=IMG_SHAPE)

    layer: Layer
    for layer in conv_model.layers:
        layer.trainable = False

    image_a = Input(IMG_SHAPE)
    image_b = Input(IMG_SHAPE)

    branch_a = conv_model(image_a)
    branch_b = conv_model(image_b)

    merged_layers = concatenate([branch_a, branch_b])
    merged_layers = GlobalAveragePooling2D()(merged_layers)

    merged_layers = Dense(256, activation='relu')(merged_layers)
    merged_layers = Dropout(0.1)(merged_layers)

    merged_layers = Dense(256, activation='relu')(merged_layers)
    merged_layers = Dropout(0.0)(merged_layers)

    output = Dense(1, kernel_initializer='normal', activation='linear')(merged_layers)
    model = Model(inputs=[image_a, image_b], outputs=output)

    model.compile(optimizer=tf.keras.optimizers.Adam(0.00100),
                  loss='mse',
                  metrics=[loss_in_fact])

    return model
Exemple #4
0
def get_resnet_transfer_model(input, train_mode=True, freeze_reznet=True):
    base = ResNet50(input_tensor=input, include_top=False, weights='imagenet')

    if not train_mode:
        base.trainable = False
    elif freeze_reznet:
        for layer in base.layers:
            # freeze all but the last (5th) and half (inner) of the 4th layers
            if not (layer.name.startswith('bn5')
                    or layer.name.startswith('res5')
                    or layer.name.startswith('bn4d')
                    or layer.name.startswith('res4d')
                    or layer.name.startswith('bn4e')
                    or layer.name.startswith('res4e')
                    or layer.name.startswith('bn4f')
                    or layer.name.startswith('res4f')):
                layer.trainable = False
            else:
                log.info('keep layer %s trainable' % layer.name)

    x = base.output
    x = BatchNormalization(momentum=0.94, trainable=train_mode)(x)
    x = Activation('relu')(x)
    if train_mode:
        x = Dropout(0.3)(x)
    x = Dense(1024,
              activation='relu',
              name='pose_dense_6',
              trainable=train_mode)(x)  # , kernel_regularizer='l2'
    x = Flatten(name='pose_flatten_2')(x)
    if train_mode:
        x = Dropout(0.5)(x)

    return x
Exemple #5
0
def main():
    #step2:
    horizontal_line_conv, vertical_line_conv = design_convolution()

    #step3:
    build_convolution_model(horizontal_line_conv, vertical_line_conv)

    #step4:
    image_paths = pre_process_picture()

    #step5:
    image_size = 224
    img_height = image_size
    img_width = image_size
    #output = read_and_prep_images(image_paths, img_height, img_width)

    #step6:
    my_model = ResNet50(
        weights=
        '/Users/sai-pc/Desktop/另外實作/transfer_learning/Code/utils/dl/ResNet50/resnet50_weights_tf_dim_ordering_tf_kernels.h5'
    )
    image_data = read_and_prep_images(image_paths, img_height, img_width)
    my_preds = my_model.predict(image_data)

    #step7:
    #decode_predictions 把結果 show 出來
    most_likely_labels = decode_predictions(
        my_preds,
        top=2,
        class_list_path=
        '/Users/sai-pc/Desktop/另外實作/transfer_learning/Code/utils/dl/result/imagenet_class_index.json'
    )
    for i, img_path in enumerate(image_paths):
        display(Image(img_path))
        print(most_likely_labels[i])
Exemple #6
0
def resnet_window():
    model = Sequential()
    resnet = ResNet50(include_top=False,
                      pooling='avg',
                      weights='imagenet',
                      input_shape=(256, 256, 3))

    model.add(resnet)
    model.add(Flatten())
    model.add(BatchNormalization())
    model.add(Dense(2048, activation='relu'))
    model.add(BatchNormalization())
    model.add(Dense(1024, activation='relu'))
    model.add(BatchNormalization())
    # model.add(Dense(13, activation='relu'))
    model.add(Dense(2, activation=None))
    model.add(BatchNormalization())
    model.add(Activation('softmax'))

    opt = Adam(lr=1e-4, decay=1e-6)
    model.compile(loss='categorical_crossentropy',
                  optimizer=opt,
                  metrics=['accuracy'])

    return model
def create_model(model_size):
    my_new_model = Sequential()
    if model_size == 'L':
        resnet_weights_path = 'input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
        resnet = ResNet50(include_top=False,
                          pooling='avg',
                          weights=resnet_weights_path)
        #resnet.summary()
        my_new_model.add(resnet)
        my_new_model.layers[0].trainable = False
    else:
        vgg_weights_path = 'input/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
        vgg = VGG16(include_top=False, weights=vgg_weights_path)
        vgg.summary()
        my_new_model.add(vgg)
        my_new_model.add(GlobalAveragePooling2D())
        my_new_model.layers[0].trainable = False
        my_new_model.layers[1].trainable = False

    my_new_model.add(Dense(NUM_CLASSES, activation='softmax'))

    # Say no to train first layer (ResNet) model. It is already trained

    opt = optimizers.adam()
    my_new_model.compile(optimizer=opt,
                         loss='categorical_crossentropy',
                         metrics=['accuracy'])
    return my_new_model
 def load_model(self):
     """This function is used to load pretrained model
     """
     usage = """USAGE: %python run_experiment.py -m [model]
                      Model ResNet50:                  python run_experiment.py -o resnet50
                      Model VGG16:                     python run_experiment.py -o vgg16
                      Model LeNet-5:                   python run_experiment.py -o lenet5
                      Model AlexNet:                   python run_experiment.py -o alexnet
                      Model Xception:                  python run_experiment.py -o xception
                      Model InceptionV3:               python run_experiment.py -o inceptionv3
             """
     try:
         # resnet50
         if self.model_name == "resnet50":
             from tensorflow.python.keras.applications import ResNet50
             from tensorflow.python.keras.applications.resnet50 import preprocess_input
             self.preprocess = preprocess_input
             self.model = ResNet50()
         # vgg16
         elif self.model_name == "vgg16":
             from tensorflow.python.keras.applications import VGG16
             from tensorflow.python.keras.applications.vgg16 import preprocess_input
             self.preprocess = preprocess_input
             self.model = VGG16()
         # # vgg19
         # elif self.model_name == "lenet5":
         #     # compiling and training teacher network
         #     teacher = LeNet5Teacher()
         #     teacher.__init__()
         #     teacher.load(cfg.lenet_config + ".json", cfg.lenet_config + ".h5")
         #     self.model = teacher.getModel()
         # xception
         elif self.model_name == "xception":
             from tensorflow.python.keras.applications import Xception
             from tensorflow.python.keras.applications.xception import preprocess_input
             self.preprocess = preprocess_input
             self.model = Xception()
         # inceptionv3
         elif self.model_name == "inceptionv3":
             from tensorflow.python.keras.applications import InceptionV3
             from tensorflow.python.keras.applications.inception_v3 import preprocess_input
             self.preprocess = preprocess_input
             self.model = InceptionV3()
         # alexnet
         elif self.model_name == "alexnet":
             # TODO get a pre-trained alexnet model
             self.logger.info("[ERROR]: Not yet implemented")
         # custom teacher model
         # elif self.model_name == "custom_teacher":
         #     # compiling and training teacher network
         #     teacher = TeacherCNN()
         #     teacher.__init__()
         #     teacher.load(cfg.custom_teacher_config + ".json", cfg.custom_teacher_config + ".h5")
         #     self.model = teacher.getModel()
         else:
             self.logger.error("Invalid model name")
     except Exception as e:
         self.logger.error("Loading of model failed due to {0}".format(
             str(e)))
Exemple #9
0
def empty_resnet():
    K.set_image_data_format('channels_last')
    base_model = ResNet50(weights=None,
                          include_top=False,
                          input_shape=(224, 224, 3))
    features = GlobalAveragePooling2D()(base_model.output)
    model = Model(inputs=base_model.input, outputs=features)
    return model
def get_prediction(filename):
   global my_model
   if my_model is None:
         my_model = ResNet50(weights='weights/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
   test_data = read_and_prep_images([filename])
   preds = my_model.predict(test_data)
   most_likely_labels = decode_predictions(preds, top=3, class_list_path='weights/imagenet_class_index.json')
   return most_likely_labels[0]
Exemple #11
0
def example():
    image_path = 'D:\\Onepredict_MK\\LG CNS\\cat.jpg'

    img = image.load_img(image_path, target_size=(224, 224))
    img_input = preprocess_input(np.expand_dims(img, 0))

    resnet = ResNet50(input_shape=(224, 224, 3),
                      weights='imagenet',
                      include_top=True)

    probs = resnet.predict(img_input)
    pred = np.argmax(probs[0])

    activation_layer = resnet.layers[-3].name
    inp = resnet.input
    # for idx in range(1000):
    y_c = resnet.output.op.inputs[0][:, pred]
    A_k = resnet.get_layer(activation_layer).output

    grads = K.gradients(y_c, A_k)[0]
    # Model(inputs=[inp], outputs=[A_k, grads, resnet.output])
    get_output = K.function(inputs=[inp], outputs=[A_k, grads, resnet.output])
    [conv_output, grad_val, model_output] = get_output([img_input])

    conv_output = conv_output[0]
    grad_val = grad_val[0]

    weights = np.mean(grad_val, axis=(0, 1))
    grad_cam = np.zeros(dtype=np.float32, shape=conv_output.shape[0:2])
    for k, w in enumerate(weights):
        grad_cam += w * conv_output[:, :, k]
        # RELU
        grad_cam = np.maximum(grad_cam, 0)

    grad_cam = cv2.resize(grad_cam, (224, 224))

    # Guided grad-CAM
    register_gradient()
    guided_model, activation_layer = modify_backprop(resnet, 'GuidedBackProp',
                                                     args.checkpoint_path,
                                                     args.main_name)
    saliency_fn = compile_saliency_function(guided_model, activation_layer)
    saliency = saliency_fn([img_input, 0])
    gradcam = saliency[0] * grad_cam[..., np.newaxis]
    gradcam = deprocess_image(gradcam)

    # grad_cam = ndimage.zoom(grad_cam, (32, 32), order=1)
    plt.subplot(1, 2, 1)
    plt.imshow(img, alpha=0.8)
    plt.imshow(grad_cam, cmap='jet', alpha=0.5)
    plt.axis('off')

    plt.subplot(1, 2, 2)
    plt.imshow(gradcam, cmap='jet', alpha=0.5)
    plt.axis('off')
    plt.show()
Exemple #12
0
def backbone(input_shape=(512, 512, 3)):
    image = Input(shape=input_shape, name="image")
    base_model = ResNet50(input_tensor=image, include_top=False, weights=None)
    x = base_model.get_layer(name="activation_48").output
    x = _deconv_block(x, 1024)
    x = Add()([x, base_model.get_layer(name="activation_39").output])
    x = _deconv_block(x, 512)
    x = Add()([x, base_model.get_layer(name="activation_21").output])
    model = Model(image, x)
    return model, 8
Exemple #13
0
    def __init__(self) -> None:
        """Create ResNet50 classification model.

        tf.keras.applications provides a preprocessing method for its ResNet50
        model, please use it here.
        """
        super().__init__()
        # TODO (DariaShel): implement model
        self.model = Sequential()
        self.model.add(ResNet50())
Exemple #14
0
def train(img_path):
    K.clear_session()
    my_model = ResNet50(
        weights='./resnet50_weights_tf_dim_ordering_tf_kernels.h5')
    image_size = 224
    img = load_img(img_path, target_size=(image_size, image_size))
    img_array = np.array([img_to_array(img)])
    inpu = preprocess_input(img_array)
    preds = my_model.predict(inpu)
    deco = decode_predictions(preds)
    return deco
Exemple #15
0
def cnnModel4():
    from tensorflow.python.keras.applications import ResNet50
    from tensorflow.python.keras import Model
    resnet = ResNet50(weights='imagenet',
                      include_top=False,
                      input_shape=(IMG_SIZE, IMG_SIZE, 3))
    x = Flatten(input_shape=resnet.output.shape)(resnet.output)
    x = Dense(1024, activation='sigmoid')(x)
    predictions = Dense(NUM_CLASSES, activation='softmax', name='pred')(x)
    model = Model(inputs=[resnet.input], outputs=[predictions])
    return model
Exemple #16
0
def build_model_resnet50(lock_base_model: bool):
    base_model = ResNet50(input_shape=INPUT_SHAPE, include_top=False, pooling=None)
    if lock_base_model:
        for layer in base_model.layers:
            layer.trainable = False
    x = AveragePooling2D((5, 5), name='avg_pool5', strides=1)(base_model.layers[-2].output)
    x = GlobalMaxPooling2D()(x)
    res = Dense(NB_CLASSES, activation='sigmoid', name='classes', kernel_initializer='zero',
                kernel_regularizer=l1(1e-5))(x)
    model = Model(inputs=base_model.inputs, outputs=res)
    return model
def load_task_3b_model():
    NUM_CLASSES = 2

    RESNET50_POOLING_AVERAGE = 'avg'
    DENSE_LAYER_ACTIVATION = 'softmax'
    resnet_weights_path = 'models/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'

    model = Sequential()
    model.add(ResNet50(include_top=False, pooling=RESNET50_POOLING_AVERAGE, weights=resnet_weights_path))
    model.add(Dense(NUM_CLASSES, activation=DENSE_LAYER_ACTIVATION))
    model.load_weights("models/trained_models/the_best_model.hdf5")
    return model
def RN50(input_shape=(192, 192, 3), num_classes=8, lr=0.001):
    resnet50 = ResNet50(include_top=False,
                        weights=None,
                        input_shape=input_shape,
                        pooling='avg')
    out = resnet50.output
    out = Dense(8, activation='softmax')(out)
    model = Model(inputs=resnet50.input, outputs=out)
    model.compile(optimizer=Adam(lr=lr),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    return model
Exemple #19
0
def createModel():
    base_model = ResNet50(include_top=False, pooling='max', weights='imagenet')

    model = Sequential()
    model.add(base_model)
    model.add(Dense(NUM_CLASSES, activation='softmax'))

    for layer in base_model.layers:
        layer.trainable = False
    
    model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
    return model
def build_resnet50(num_classes, img_size):
    from tensorflow.python.keras.applications import ResNet50
    from tensorflow.python.keras import Model
    from tensorflow.python.keras.layers import Dense, Flatten
    resnet = ResNet50(weights='imagenet',
                      include_top=False,
                      input_shape=img_size)
    x = Flatten(input_shape=resnet.output.shape)(resnet.output)
    x = Dense(1024, activation='sigmoid')(x)
    predictions = Dense(num_classes, activation='softmax', name='pred')(x)
    model = Model(inputs=[resnet.input], outputs=[predictions])
    return model
def model_flipout():
    model = Sequential()
    model.add(ResNet50(include_top=False, pooling='avg', weights='imagenet'))
    model.add(tfp.layers.DenseFlipout(1024, activation=tf.nn.relu))
    model.add(Dropout(rate=0.2))
    model.add(tfp.layers.DenseFlipout(512, activation=tf.nn.relu))
    model.add(Dropout(rate=0.2))
    model.add(tfp.layers.DenseFlipout(5, activation=tf.nn.relu))
    model.layers[0].trainable = False
    # model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.00001, decay=1e-6, momentum=0.9), loss='mse')
    # model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss='mse')
    model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=min_mse)
    return model
def model_resnet(bayesian=False):
    inputs = Input(shape=(224, 224, 3))
    x = ResNet50(include_top=False, pooling='avg', weights='imagenet')(inputs)
    x = Dense(1024, activation='relu')(x)
    x = Dropout(0.2)(x, training=bayesian)
    x = Dense(512, activation='relu')(x)
    x = Dropout(0.2)(x, training=bayesian)
    x = Dense(5, activation='linear')(x)
    model = Model(inputs, x)
    # model.layers[1].trainable = False
    # model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.00001, decay=1e-6, momentum=0.9), loss='mse')
    model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss='mse')
    return model
Exemple #23
0
    def run(self):
        base_model = ResNet50(include_top=False, weights='imagenet', input_tensor=None, input_shape=(32, 32, 3))

        # Add final layers
        x = base_model.output
        x = Flatten()(x)
        n_classes = 10
        predictions = Dense(n_classes, activation='softmax', name='fc10')(x)

        # This is the model we will train
        model = Model(inputs=base_model.input, outputs=predictions)
        model.summary()
        output_df = model2df(model)
        outputRDD('<#zzjzRddName#>_resnet50', output_df)
Exemple #24
0
 def __init__(self):
     hot_dog_image_dir = './train/hot_dog'
     hot_dog_paths = [join(hot_dog_image_dir, filename) for filename in 
                      ['1000288.jpg',
                       '127117.jpg']]
     
     not_hot_dog_image_dir = './train/not_hot_dog'
     not_hot_dog_paths = [join(not_hot_dog_image_dir, filename) for filename in
                                 ['823536.jpg',
                                  'IMG_20180526_194347.jpg',
                                  '99890.jpg']]
     
     self.image_paths = hot_dog_paths + not_hot_dog_paths
     self.model = \
     ResNet50(weights='./resnet50_weights_tf_dim_ordering_tf_kernels.h5')
Exemple #25
0
def build_model_resnet50_avg(lock_base_model: bool):
    """
    Build the Resnet50 based level 1 model
    :param lock_base_model:
    :return:
    """
    base_model = ResNet50(input_shape=INPUT_SHAPE, include_top=False, pooling=None)
    if lock_base_model:
        for layer in base_model.layers:
            layer.trainable = False
    x = GlobalAveragePooling2D(name='avg_pool_final')(base_model.layers[-2].output)
    res = Dense(NB_CLASSES, activation='sigmoid', name='classes', kernel_initializer='zero',
                kernel_regularizer=l1(1e-5))(x)
    model = Model(inputs=base_model.inputs, outputs=res)
    return model
Exemple #26
0
def get_age_model():

    age_model = ResNet50(include_top=False,
                         weights=None,
                         input_shape=(config.RESNET50_DEFAULT_IMG_WIDTH,
                                      config.RESNET50_DEFAULT_IMG_WIDTH, 3),
                         pooling='avg')

    prediction = Dense(units=101,
                       kernel_initializer='he_normal',
                       use_bias=False,
                       activation='softmax',
                       name='pred_age')(age_model.output)

    age_model = Model(inputs=age_model.input, outputs=prediction)
    return age_model
def RN50CovPool(input_shape=(192, 192, 3), num_classes=8, lr=0.001):
    resnet50 = ResNet50(include_top=False,
                        weights=None,
                        input_shape=input_shape)
    out = resnet50.output
    out = Conv2D(256, (1, 1))(out)
    out = BatchNormalization()(out)
    out = Activation('relu')(out)
    out = GlobalCovPooling2D(num_iter=5)(out)
    out = Flatten()(out)
    out = Dense(8)(out)
    out = Activation('softmax')(out)
    model = Model(inputs=resnet50.input, outputs=out)
    model.compile(optimizer=Adam(lr=lr),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    return model
def resnet_model():
    model = Sequential()
    # 1st layer as the lumpsum weights from resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
    # NOTE that this layer will be set below as NOT TRAINABLE, i.e., use it as is
    model.add(ResNet50(include_top=False, pooling='avg', weights='imagenet'))

    model.add(Dense(1024, activation='relu'))
    model.add(Dropout(rate=0.2))
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(rate=0.2))
    model.add(Dense(5, activation='linear'))
    # Say not to train first layer (ResNet) model as it is already trained
    model.layers[0].trainable = False

    # model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.00001, decay=1e-6, momentum=0.9), loss='mse')
    # model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss='mse')
    model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=min_mse)
    return model
Exemple #29
0
def train():
    my_new_model = Sequential()
    my_new_model.add(
        ResNet50(include_top=False, pooling='avg', weights='imagenet'))
    my_new_model.add(Dense(num_classes, activation='softmax'))
    my_new_model.layers[0].trainable = False
    my_new_model.compile(optimizer='adam',
                         loss='categorical_crossentropy',
                         metrics=['accuracy'])
    my_new_model.fit(X_train, y_train, epochs=2, batch_size=128)
    preds = my_new_model.evaluate(X_test,
                                  y_test,
                                  batch_size=10,
                                  verbose=1,
                                  sample_weight=None)
    my_new_model.summary()
    print("Loss = " + str(preds[0]))
    print("Test Accuracy = " + str(preds[1]))
Exemple #30
0
    def build_model(self):
        """
        builds train model
        resnet + embedding layer + softmax layer
        """
        model = Sequential()
        resnet = ResNet50(include_top=False, pooling='avg', weights='imagenet')
        # resnet.summary()

        model.add(resnet)
        model.add(Dense(EMBEDDING_SIZE, activation='relu', name="embedding"))
        model.add(Dense(NUM_CLASSES, activation='softmax'))
        model.layers[0].trainable = False
        model.summary()

        sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
        model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

        return model