def get_model(input_shape):
    model = resnet50.ResNet50(include_top=False, input_shape=input_shape)

    mul2 = mul_layer(model.get_layer('activation_40'), model.output)
    mul3 = mul_layer(model.get_layer('activation_22'), mul2)
    mul4 = mul_layer(model.get_layer('activation_10'), mul3)
    mul5 = mul_layer(model.get_layer('activation_1'), mul4)
    mul6 = mul_layer(model.get_layer('input_1'), mul5)

    output6 = layers.Conv2D(1, (1, 1), activation='sigmoid')(mul6)

    return models.Model(inputs=model.inputs, outputs=[output6])
Esempio n. 2
0
def get_model(input_shape):
    model = resnet50.ResNet50(include_top=False, input_shape=input_shape)
    output = layers.Conv2D(1, (1, 1), activation='sigmoid')(model.output)

    mul2, output2 = mul_layer(model.get_layer('activation_40'), model.output)
    mul3, output3 = mul_layer(model.get_layer('activation_22'), mul2)
    mul4, output4 = mul_layer(model.get_layer('activation_10'), mul3)
    mul5, output5 = mul_layer(model.get_layer('activation_1'), mul4)

    return models.Model(
        inputs=model.inputs,
        outputs=[output5, output4, output3, output2, output])
    def create_resNet50_model(self):
        """
            Method that builds (but doesn't train) a ResNet50 image model
        """
        
        base_model = resnet50.ResNet50(include_top = False,
                          weights = 'imagenet',
                          input_shape = (self.input_dim, self.input_dim, self.image_depth))

        reshape_output = Reshape((self.n_image_regions, self.n_image_embed))(base_model.get_layer("activation_48").output)
        self.model = Model(inputs=base_model.input, outputs=reshape_output)
        self.model.summary()
Esempio n. 4
0
    def __init__(self):
        self.root_dir = '/home/stas/dev/demo_projects/dog_breed_classifier'

        with open(self._get_dir('resources/dog_names.txt'), 'r') as f:
            lines = f.readlines()
        self.dog_names = [line.strip() for line in lines]

        self.model = self.create_transferred_model()
        self.face_cascade = cv2.CascadeClassifier(
            self._get_dir('resources/haarcascade_frontalface_alt.xml'))
        self.ResNet50_model = r50.ResNet50(weights='imagenet')
        self.xcModel = xc.Xception(weights='imagenet', include_top=False)
    def build_base_model(self, inputs, **kwarg):
        # create the vgg backbone
        if self.backbone_name == 'resnet50':
            inputs = keras.layers.Lambda(
                lambda x: keras_resnet50.preprocess_input(x))(inputs)
            resnet = keras_resnet50.ResNet50(input_tensor=inputs,
                                             include_top=False,
                                             weights=None)
        else:
            raise ValueError("Backbone '{}' not recognized.".format(
                self.backbone_name))

        return resnet
def load_model(model_name):
    if model_name == 'vgg':
        #Load the VGG model
        model = vgg16.VGG16(weights='imagenet')
        #Load the Inception_V3 model
        #inception_model = inception_v3.InceptionV3(weights='imagenet')
        #Load the ResNet50 model
    if model_name == 'resnet50':
        model = resnet50.ResNet50(weights='imagenet')
        #Load the MobileNet model
    if model_name == 'mobilenet':
        mobilenet_model = mobilenet.MobileNet(weights='imagenet')
    return model
Esempio n. 7
0
 def resnet50_classificator(self, image_path):
     resnet_model = resnet50.ResNet50(weights='imagenet')
     filename = image_path
     original = load_img(filename, target_size=(WIDTH, HEIGHT))
     plt.imshow(original)
     numpy_image = img_to_array(original)
     plt.imshow(np.uint8(numpy_image))
     image_batch = np.expand_dims(numpy_image, axis=0)
     plt.imshow(np.uint8(image_batch[0]))
     processed_image = resnet50.preprocess_input(image_batch.copy())
     predictions = resnet_model.predict(processed_image)
     label = decode_predictions(predictions)
     return sorted(label[0], key=lambda x: x[2], reverse=True)
Esempio n. 8
0
def get_model(input_shape, num_classes):
    model = resnet50.ResNet50(include_top=False, input_shape=input_shape)

    mul2 = mul_layer(model.get_layer('activation_40'), model.output)
    mul3 = mul_layer(model.get_layer('activation_22'), mul2)
    mul4 = mul_layer(model.get_layer('activation_10'), mul3)
    mul5 = mul_layer(model.get_layer('activation_1'), mul4)
    mul6 = mul_layer(model.get_layer('input_1'), mul5)

    output7 = layers.Conv2D(1, (1, 1), activation='linear')(mul6)
    output7 = layers.BatchNormalization()(output7)

    return models.Model(inputs=model.inputs, outputs=output7)
Esempio n. 9
0
def build(inputs, output_dims, model_name):
    '''
    A factory function to create models.

    Args:
        inputs: input variable.
        output_dims(int): number of classes.
        model_name(str): model name.

    Return:
        An instance of the model.
    '''
    # return vgg16.VGG16(inputs, output_dims)
    return resnet50.ResNet50(inputs, output_dims)
Esempio n. 10
0
def resnet_classifier(in_shape=(256, 256, 3)):
    model = resnet50.ResNet50(weights='imagenet',
                              include_top=False,
                              input_shape=in_shape)
    flatten = Flatten()
    new_layer2 = Dense(2, activation='softmax', name='my_dense_2')
    inp2 = model.input
    out2 = new_layer2(flatten(model.output))
    model = Model(inp2, out2)
    model.summary(line_length=150)
    model.compile(loss='categorical_crossentropy',
                  optimizer=Adam(lr=0.0002, beta_1=0.5),
                  metrics=['accuracy'])
    return model
Esempio n. 11
0
def keras_pretrained_model(netname):
    #import pdb; pdb.set_trace()
    if netname == 'VGG16':
        model = vgg16.VGG16(weights='imagenet')
    elif netname == 'VGG19':
        model = vgg19.VGG19(weights='imagenet')
    elif netname == 'ResNet50':
        model = resnet50.ResNet50(weights='imagenet')
    elif netname == 'DenseNet':
        model = densenet.DenseNet121(weights='imagenet')
    elif netname == 'Inception_v3':
        model = inception_v3.InceptionV3(weights='imagenet')

    return _maybe_save(netname, model)
Esempio n. 12
0
def load_resnet50_model():
    """Instantiate the keras Resnet50 convolutional neural network

    Returns
    -------
    keras.applications.resnet50.Resnet50
        Resnet50 CNN with weights trained on imagenet
    """

    clear_session()
    resnet50_model = resnet50.ResNet50(weights="imagenet")
    global resnet50_graph
    resnet50_graph = tf.get_default_graph()
    return resnet50_model
Esempio n. 13
0
def main(args):
    if args.model == "resnet50":
        model = resnet50.ResNet50(include_top=False, input_shape=(224, 224, 3))
        image_size = 224

    dataset = utils.get_data(args.data_path)
    image_paths, labels = utils.get_image_paths_and_labels(dataset)
    images = utils.load_images(image_paths, image_size)

    last_output = model.predict(images)
    with open(args.model + "_" + args.set + ".pkl", "wb") as outfile:
        pickle.dump((last_output, labels), outfile)

    return
Esempio n. 14
0
def model_selector(model_name, weights=True):
    model = ModelMock()
    if model_name == 'vgg_like' or model_name == 'hinton':
        if model_name == 'vgg_like':
            model_class = cifar10.Vgg_like()
            logger.debug("Model: vgg_like")
        else:
            model_class = cifar10.Hinton()
            logger.debug("Model: hinton")
        model = model_class.build((32, 32, 3))
        if weights:
            model.load_weights('data/' + model_class.name + '.h5')
            logger.debug("Load weights: success.")
    else:
        if model_name == 'vgg16':
            logger.debug("Model: vgg16")
            if weights:
                model = vgg16.VGG16(weights='data/vgg16_retraining.h5')
                logger.debug("Load weights: success.")
            else:
                model = vgg16.VGG16(weights=None)
        elif model_name == 'resnet50':
            logger.debug("Model: resnet50")
            if weights:
                model = resnet50.ResNet50(
                    weights='data/resnet50_retraining.h5')
                logger.debug("Load weights: success")
            else:
                model = resnet50.ResNet50(weights=None)
        elif model_name == 'mnist':
            model_class = mnist.Mnist()
            logger.debug("Model: MNIST")
            model = model_class.build()
            if weights:
                model.load_weights('data/' + model_class.name + '.h5')
                logger.debug("Load weights: success.")
    return model
def build_ripped_resnet50(output_classes_count, trainable_layers, name,
                          input_shape):
    m = resnet50.ResNet50(include_top=False, input_shape=input_shape)

    x = GlobalAveragePooling2D(name="tail_avg_pool")(m.output)

    x = Dense(output_classes_count, name="tail_fc_final",
              activation='softmax')(x)
    result = Model(m.input, x, name=name)

    for layer in result.layers:
        if not is_trainable_layer(trainable_layers, layer):
            layer.trainable = False

    return result
Esempio n. 16
0
def _resnet(num_classes, pretrained=True, freezed=True):
    weights = 'imagenet' if pretrained else None
    base_model = resnet50.ResNet50(input_shape=(224, 224, 3),
                                   weights=weights,
                                   include_top=False)
    x = base_model.output
    x = layers.Flatten()(x)
    predictions = layers.Dense(num_classes,
                               activation=activations.softmax,
                               name='predictions')(x)
    if freezed:
        for layer in base_model.layers:
            layer.trainable = False
    model = models.Model(inputs=base_model.input, outputs=predictions)
    return model
Esempio n. 17
0
    def _setup_model(self):
        """setup ResNet50
        Returns: TODO

        """
        model_input = keras.layers.Input(self._input_shape)
        base_model = resnet50.ResNet50(input_tensor=model_input,
                                       include_top=False)
        x = base_model.output
        x = keras.layers.Flatten()(x)
        x = keras.layers.Dense(self._num_classes,
                               activation='softmax',
                               name='global_cls')(x)
        model = keras.models.Model(inputs=base_model.input, outputs=x)
        return model
Esempio n. 18
0
def e2e_network():
    inception_1 = resnet50.ResNet50(weights="imagenet", include_top=True)
    inception_2 = resnet50.ResNet50(weights="imagenet", include_top=True)

    for layer in inception_1.layers:
        layer.trainable = False
        layer.name = layer.name + "_1"
    for layer in inception_2.layers:
        layer.trainable = False
        layer.name = layer.name + "_2"

    vector_1 = inception_1.get_layer("avg_pool_1").output
    vector_2 = inception_2.get_layer("avg_pool_2").output

    sim_head = models.load_model(os.path.join(DATA_DIR, "models", "resnet-dot-best.h5"))
    for layer in sim_head.layers:
        print(layer.name, layer.input_shape, layer.output_shape)

    prediction = sim_head([vector_1, vector_2])

    model = models.Model(inputs=[inception_1.input, inception_2.input], outputs=prediction)
    model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])

    return model
def get_model(input_shape, num_classes):
    model = resnet50.ResNet50(include_top=False, input_shape=input_shape)

    mul2 = mul_layer(model.get_layer('activation_40'), model.output)
    mul3 = mul_layer(model.get_layer('activation_22'), mul2)
    mul4 = mul_layer(model.get_layer('activation_10'), mul3)
    mul5 = mul_layer(model.get_layer('activation_1'), mul4)
    mul6 = mul_layer(model.get_layer('input_1'), mul5)

    output7 = [
        layers.Conv2D(1, (1, 1), activation='sigmoid')(mul6)
        for i in range(0, num_classes)
    ]

    return models.Model(inputs=model.inputs, outputs=output7)
Esempio n. 20
0
def create_keras_model(output_path):
    base = resnet50.ResNet50(include_top=False,
                             weights="imagenet",
                             input_shape=(225, 225, 3))

    X = base.output
    X = GlobalAveragePooling2D()(X)
    X = Dropout(0.5)(X)
    X = Dense(2, activation='softmax')(X)
    model = Model(inputs=base.input, outputs=X)

    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizers.Adam(lr=0.001, clipnorm=1.),
                  metrics=['accuracy'])
    model.save(output_path)
Esempio n. 21
0
def build_resnet50_model():
    # get the model without the denses
    base_model = resnet50.ResNet50(weights='imagenet', include_top='false')
    new_dense = base_model.output
    # add the new denses to classify the hate images
    new_dense = Dense(1024, activation='relu')(new_dense)
    predictions = Dense(2, activation='softmax')(new_dense)
    model = Model(inputs=base_model.input, outputs=predictions)
    # we will only train the new denses for the baseline
    for layer in base_model.layers:
        layer.trainable = False
    model.compile(optimizer='adam',
                  loss='binary_crossentropy',
                  metrics=["accuracy"])
    return model
Esempio n. 22
0
def classification_model(number_classes):
    #Load the MobileNet model
    image_size = 224
    resnet = resnet50.ResNet50(weights='imagenet',
                               include_top=False,
                               input_shape=(image_size, image_size, 3),
                               pooling='avg')

    #build model
    inp = layers.Input(shape=(image_size, image_size, 3))
    x = resnet(inp)
    z = layers.Dense(number_classes, activation='softmax')(x)
    model = models.Model(inp, z)

    return model
Esempio n. 23
0
def regression_model():
    #Load the MobileNet model
    image_size = 224
    resnet = resnet50.ResNet50(weights='imagenet',
                               include_top=False,
                               input_shape=(image_size, image_size, 3),
                               pooling='avg')

    #build model
    inp = layers.Input(shape=(image_size, image_size, 3))
    x = resnet(inp)
    z = layers.Dense(2, activation='sigmoid')(x)
    model = models.Model(inp, z)

    return model
Esempio n. 24
0
def getModel(net_settings, num_classes=1):
    '''
		Should be modified with model type as input and returns the desired model
    '''
    if net_settings['model_type'] == 'resnet':
        base_model = resnet50.ResNet50(include_top=True, weights='imagenet')
        finetuning = Dense(1, activation='sigmoid',
                           name='predictions')(base_model.layers[-2].output)
        model = Model(input=base_model.input, output=finetuning)

        ## Adjust learning rate based on number of GPUs
        hv_lr = net_settings['lr'] * hvd.size()
        opt = optimizers.SGD(lr=hv_lr, momentum=0.9, decay=1e-6, nesterov=True)
        ## Adding Horovod DistributedOptimizer
        opt = hvd.DistributedOptimizer(opt)

        model.compile(loss=net_settings['loss'],
                      optimizer=opt,
                      metrics=['accuracy'])
        callbacks = [
            hvd.callbacks.BroadcastGlobalVariablesCallback(0),
        ]
        if hvd.rank() == 0:
            callbacks.append(
                keras.callbacks.ModelCheckpoint('./checkpoint-{epoch}.h5'))
        return model
    elif net_settings['model_type'] == 'resnet101':
        model = resnet101_model(224, 224, 3, 1)
        ## Adjust learning rate based on number of GPUs
        hv_lr = net_settings['lr'] * hvd.size()
        opt = optimizers.SGD(lr=hv_lr, momentum=0.9, decay=1e-6, nesterov=True)
        ## Adding Horovod DistributedOptimizer
        opt = hvd.DistributedOptimizer(opt)

        model.compile(loss=net_settings['loss'],
                      optimizer=opt,
                      metrics=['accuracy'])
        callbacks = [
            hvd.callbacks.BroadcastGlobalVariablesCallback(0),
        ]
        if hvd.rank() == 0:
            callbacks.append(
                keras.callbacks.ModelCheckpoint('./checkpoint-{epoch}.h5'))
            return model
    else:
        print '[models] Ugggh. Not ready for this yet.'
        exit(0)
        return None
Esempio n. 25
0
def load_resnet(finetune):
    '''Load in the pre-trained Resnet50 model'''
    resnet_model = resnet50.ResNet50(weights='imagenet', include_top=False,\
                                     input_shape=(268, 182, 3))
    resnet_model.summary()
    if not finetune:
        return resnet_model
    #Add in last layer.
    link = resnet_model.output
    link = GlobalAveragePooling2D()(link)
    predictions = Dense(26, activation='sigmoid')(link)
    model = Model(inputs=resnet_model.input, outputs=predictions)
    #Freeze all but the last specified layers.
    for layer in model.layers[:-10]:
        layer.trainable = False
    return model
Esempio n. 26
0
def create_model(img_size, model_type, base_name):

    if model_type == 0:
        print("Creating MobileNet model")
        base = mobilenet.MobileNet(input_shape=img_size,
                                   include_top=False,
                                   weights='imagenet')

    elif model_type == 1:
        print("Creating InceptionV3 model")
        base = inception_v3.InceptionV3(input_shape=img_size,
                                        include_top=False,
                                        weights='imagenet')

    elif model_type == 2:
        print("Creating Resnet50 model")
        base = resnet50.ResNet50(input_shape=img_size,
                                 include_top=False,
                                 weights='imagenet')

    elif model_type == 3:
        print("Creating InceptionResNet-V2 model")
        base = inception_resnet_v2.InceptionResNetV2(input_shape=img_size,
                                                     include_top=False,
                                                     weights='imagenet')

    top = base.output
    top = GlobalAveragePooling2D()(top)

    top = Dense(units=2048,
                activation='relu',
                kernel_regularizer=None,
                name='fc_1')(top)
    predictions = Dense(units=n_classes,
                        activation='softmax',
                        kernel_regularizer=l2(l=wd),
                        name='softmax')(top)

    model_combined = Model(inputs=base.input,
                           outputs=predictions,
                           name=base_name)

    path_to_weights = 'weights/' + weights_filename
    model_combined.load_weights(filepath=path_to_weights, by_name=True)
    print('Loading weights from ' + path_to_weights)

    return model_combined
Esempio n. 27
0
    def resnet(self):
        """Build the structure of a convolutional neural network from input
        image data to the last hidden layer on a similar manner than ResNet

        See: He, Zhang, Ren, Sun. Deep Residual Learning for Image
        Recognition. ArXiv technical report, 2015.

        Returns
        -------
        tensor
            (batch_size, nb_labels)-shaped output predictions, that have to be
        compared with ground-truth values
        """
        resnet_model = resnet50.ResNet50(include_top=False,
                                         input_tensor=self.X)
        y = self.flatten(resnet_model.output)
        return self.output_layer(y, depth=self.nb_labels)
Esempio n. 28
0
    def model_construction(self, learning_rate):
        resnet = resnet50.ResNet50(include_top=False,
                                   weights='imagenet',
                                   input_shape=(224, 224, 3),
                                   pooling='max')
        for layer in resnet.layers[:-5]:
            layer.trainable = False

        self.model.add(resnet)

        self.model.add(Dense(10, activation="softmax"))

        sgd = SGD(lr=learning_rate, decay=1e-6, momentum=0.9, nesterov=True)
        self.model.compile(loss=keras.losses.categorical_crossentropy,
                           optimizer=sgd,
                           metrics=['accuracy'])
        self.model.summary()
Esempio n. 29
0
def reid_net(base_model='vgg16', include_top=True, input_shape=None):
    if base_model is 'vgg16':
        vgg = vgg16.VGG16(include_top=True,
                          weights='imagenet',
                          input_shape=input_shape)
        #x = vgg.output
        #x = Flatten(name='flatten')(x)
        #x = Dense(1024, activation = 'relu', name='fc1')(x)
        #x = Dense(1024, activation = 'relu', name='fc2')(x)
        base_model = Model(vgg.input,
                           vgg.get_layer('fc2').output,
                           name='base_model')
    elif base_model is 'inception':
        inception = inception_v3.InceptionV3(include_top=True,
                                             weights='imagenet')
        base_model = Model(
            inputs=inception.input,
            outputs=inception.get_layer('avg_pool').output,
        )
    elif base_model is 'resnet50':
        resnet = resnet50.ResNet50(include_top=True, weights='imagenet')
        base_model = Model(inputs=resnet.input,
                           outputs=resnet.layers[-2].output,
                           name='base_model')
    elif base_model is 'xception':
        model = xception.Xception(weights='imagenet')
        base_model = Model(inputs=model.input,
                           outputs=model.layers[-1].input,
                           name='base_model')

    drop_f = Dropout(0.1, name='drop_f')
    cls_out = Dense(751, activation='softmax', name='y_clss')
    input1 = Input(shape=input_shape, name='input1')
    input2 = Input(shape=input_shape, name='input2')
    fea1, fea2 = base_model(input1), base_model(input2)
    if not include_top:
        model = Model(input1, fea1)
        return model
    cls1, cls2 = cls_out(fea1), cls_out(fea2)
    distance = Lambda(euclidean_distance,
                      output_shape=eucl_dist_output_shape,
                      name='distance')([fea1, fea2])
    #drop_d = Dropout(0.1,name='drop_d')(distance)
    diff_out = Dense(2, activation='softmax', name='y_diff')(distance)
    model = Model(inputs=[input1, input2], outputs=[diff_out, cls1, cls2])
    return model
Esempio n. 30
0
def get_model(exp_id):
    basedir = os.path.abspath(os.path.dirname(__file__))

    exp_model_dict = {
        'lenet1': 'model/LeNet-1.h5',
        'lenet4': 'model/LeNet-4.h5',
        'lenet5': 'model/LeNet-5.h5',
        'mutant1': 'model/mutant1.h5',
        'mutant2': 'model/mutant2.h5',
        'mutant3': 'model/mutant3.h5',
        'cifar10': 'model/model_cifar10.h5',
        'cifar100': 'model/model_cifar100.h5',
        'svhn': 'model/model_svhn.hdf5',
        'fashion': 'model/model_fashion.hdf5',
        'traffic_sign': "model/model_squeezeNet_TSR.hdf5",
        'adv_mnist': 'model/LeNet-5.h5',
        'adv_cifar10': 'model/model_cifar10.h5',
        'adv_fashion': 'model/model_fashion.hdf5',
        'adv_svhn': 'model/model_svhn.hdf5',
        'combined_cifar10': 'model/model_cifar10.h5',
        'combined_fashion': 'model/model_fashion.hdf5',
        'combined_svhn': 'model/model_svhn.hdf5',
        'vgg16': 'model/cifar10-vgg16_model_alllayers.h5',
        'catvsdog': 'model/cats_and_dogs_small_1.h5'
    }

    if exp_id == 'vgg19':
        my_model = vgg19.VGG19(weights='imagenet')
        adam = keras.optimizers.Adagrad(lr=0.01, epsilon=None, decay=0.0)
        my_model.compile(loss='categorical_crossentropy',
                         optimizer=adam,
                         metrics=['accuracy'])
    elif exp_id == 'resnet50':
        my_model = resnet50.ResNet50(weights='imagenet')
        adam = keras.optimizers.Adagrad(lr=0.01, epsilon=None, decay=0.0)
        my_model.compile(loss='categorical_crossentropy',
                         optimizer=adam,
                         metrics=['accuracy'])
    elif exp_id in exp_model_dict.keys():
        my_model = keras.models.load_model(
            os.path.join(basedir, exp_model_dict[exp_id]))
    else:
        raise Exception("no such dataset found: {}".format(exp_id))

    return my_model