Exemple #1
0
def pretrained_model(model):
    if model == 'densenet':
        base_model = densenet.DenseNet121(include_top=False,
                                          weights='imagenet',
                                          input_shape=(IMG_SIZE, IMG_SIZE, 3))
    elif model == 'inception':
        base_model = inception_v3.InceptionV3(include_top=False,
                                              weights='imagenet',
                                              input_shape=(IMG_SIZE, IMG_SIZE,
                                                           3))
    elif model == 'mobilenet':
        base_model = mobilenet.MobileNet(include_top=False,
                                         weights='imagenet',
                                         input_shape=(IMG_SIZE, IMG_SIZE, 3))
    elif model == 'vgg':
        base_model = vgg19.VGG19(include_top=False,
                                 weights='imagenet',
                                 input_shape=(IMG_SIZE, IMG_SIZE, 3))
    elif model == 'resnet':
        base_model = resnet50.ResNet50(include_top=False,
                                       weights='imagenet',
                                       input_shape=(IMG_SIZE, IMG_SIZE, 3))
    elif model == 'xception':
        base_model = xception.Xception(include_top=False,
                                       weights='imagenet',
                                       input_shape=(IMG_SIZE, IMG_SIZE, 3))
    for layer in base_model.layers:
        layer.trainable = True
    x = base_model.output
    x = Flatten()(x)
    x = Dense(150, activation='relu')(x)
    x = Dropout(0.2)(x)
    predictions = Dense(1, activation='sigmoid')(x)

    return models.Model(base_model.input, predictions)
    def get_mobilenet(self):
        """ Criação da Rede - MobileNet """
        print("MobileNet")
        input_shape = (wsize, hsize, channels)
        model = mobilenet.MobileNet(
            input_shape=input_shape,
            alpha=1.0,
            depth_multiplier=1,
            dropout=1e-3,
            include_top=False,
            weights=None,  #'imagenet', 
            input_tensor=None,
            pooling=None)
        x = model.input
        y = model.output
        y = Flatten()(y)
        y = Dense(1, activation='sigmoid', name="last")(y)  #5

        model = Model(input=x, output=y)

        model.compile(loss='binary_crossentropy',
                      optimizer=Adam(lr=0.001),
                      metrics=['binary_accuracy'])

        print(model.summary())

        self.model_xray = model
Exemple #3
0
def create_model():
    """Initializes a MobileNet with a custom top.

    The top layers is made of fc layers fine-tuned for the bi-classification
    tasks while the conv layers are left frozen, keeping the ImageNet weights.

    # Returns:
        model: A MobileNet with custom top.
    """
    model = mobilenet.MobileNet(weights="imagenet",
                                include_top=False,
                                input_shape=(224, 224,
                                             3))  # Will auto dl the model

    for layer in model.layers:
        layer.trainable = False  # Freeze conv layers

    output = Sequential()
    output.add(Flatten(input_shape=model.output_shape[1:]))
    output.add(Dense(256, activation='relu'))
    output.add(Dropout(0.5))
    output.add(Dense(1, activation='sigmoid'))

    model = Model(inputs=model.input, outputs=output(model.output))

    model.compile(optimizer=optimizers.Adam(lr=1e-5),
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    return model
Exemple #4
0
def mobilenet_retinanet(num_classes, backbone='mobilenet224_1.0', inputs=None, modifier=None, **kwargs):
    """ Constructs a retinanet model using a mobilenet backbone.

    Args
        num_classes: Number of classes to predict.
        backbone: Which backbone to use (one of ('mobilenet128', 'mobilenet160', 'mobilenet192', 'mobilenet224')).
        inputs: The inputs to the network (defaults to a Tensor of shape (None, None, 3)).
        modifier: A function handler which can modify the backbone before using it in retinanet (this can be used to freeze backbone layers for example).

    Returns
        RetinaNet model with a MobileNet backbone.
    """
    alpha = float(backbone.split('_')[1])

    # choose default input
    if inputs is None:
        inputs = keras.layers.Input((None, None, 3))

    backbone = mobilenet.MobileNet(input_tensor=inputs, alpha=alpha, include_top=False, pooling=None, weights=None)

    # create the full model
    layer_names = ['conv_pw_5_relu', 'conv_pw_11_relu', 'conv_pw_13_relu']
    layer_outputs = [backbone.get_layer(name).output for name in layer_names]
    backbone = keras.models.Model(inputs=inputs, outputs=layer_outputs, name=backbone.name)

    # invoke modifier if given
    if modifier:
        backbone = modifier(backbone)

    return retinanet.retinanet(inputs=inputs, num_classes=num_classes, backbone_layers=backbone.outputs, **kwargs)
Exemple #5
0
 def __init__(self):
     print('Loading mobilenet model...')
     self.mnet = mobilenet.MobileNet(
     )  # define the mobilenet model (not thread safe!!!)
     self.flat_categories = [y for x in categories for y in x]
     self.imagenet_idx = {}
     self.getimagenetclasses()
     print('Done')
def pre_trainedmodel():
    model=mobilenet.MobileNet(include_top=False, weights='imagenet', input_shape=(224,224,3))
    X=AveragePooling2D(pool_size=(7, 7), strides=None, padding='valid', data_format=None)(model.output)
    X=Dense(209,activation='softmax',input_shape=(1,1,1024),kernel_regularizer=regularizers.l2(0.01))(X)
    for layer in model2.layers[:-1]:
      layer.trainable=False
    model=Model(model.input,X)
    return model
 def loadMobileNet(self, input_shape):
     '''MobileNet is a general architecture and can be used for 
     multiple use cases.
     Requires TensorFlow backend.'''
     mobilenet_model = mobilenet.MobileNet(weights=None,
                                           input_shape=input_shape,
                                           include_top=False,
                                           pooling='max')
     self.add(mobilenet_model)
Exemple #8
0
def ft_mobile_net():
    """
    Use pretrained mobile net as bottom

    :return:
    """
    # input = layers.Input((224,224,3), name='RGB')
    base_model = mobilenet.MobileNet(include_top=False,
                                     weights='imagenet',
                                     input_shape=(224, 224, 3))
    base_out = base_model.output
    x = layers.Flatten()(base_out)
    x = layers.Dense(1024,
                     activation='relu',
                     kernel_initializer='random_uniform',
                     bias_initializer='zeros',
                     name='Dense1024')(x)
    x = layers.Dense(256,
                     activation='relu',
                     kernel_initializer='random_uniform',
                     bias_initializer='zeros',
                     name='Dense256')(x)
    x = layers.Dense(64,
                     activation='relu',
                     kernel_initializer='random_uniform',
                     bias_initializer='zeros',
                     name='Dense64')(x)
    x = layers.Dense(16,
                     activation='relu',
                     kernel_initializer='random_uniform',
                     bias_initializer='zeros',
                     name='Dense16')(x)
    predictions = layers.Dense(7,
                               activation='softmax',
                               kernel_initializer='random_uniform',
                               bias_initializer='zeros')(x)

    model = Model(inputs=base_model.input, outputs=predictions)
    for layer in base_model.layers:
        layer.trainable = False
    sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    with tf.Session() as sess:
        adam = tf.train.AdamOptimizer()
        print(adam.variables())
        sess.run(tf.variables_initializer(adam.variables()))
        model.compile(optimizer=adam,
                      loss='categorical_crossentropy',
                      metrics=[metrics.categorical_accuracy])
        # model.compile(optimizer=sgd,
        #               loss='categorical_crossentropy',
        #               metrics=[tf.keras.metrics.categorical_accuracy])
        K.set_session(tf.Session(graph=model.output.graph))
        init = K.tf.global_variables_initializer()
        K.get_session().run(init)

        # tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value training/TFOptimizer/beta1_power
        return model
Exemple #9
0
    def build_model(self):

        base_model =mobilenet.MobileNet(input_shape=(224,224,3),alpha=1.0 ,include_top=False, weights='imagenet', input_tensor=None, pooling=None, classes=self.dataset.num_classes)

        # self.model = Sequential()
        # self.model.add(Convolution2D(filters=96,kernel_size=(7,7),strides=(4, 4),padding='valid',
        #                              data_format='channels_last',input_shape=self.dataset.X_train.shape[1:]))
        # # self.model.add(BatchNormalization())#标准化
        # self.model.add(Activation('relu'))#激活层
        # self.model.add(MaxPooling2D(pool_size=(3, 3),strides=(2, 2),padding='valid'))
        #
        # self.model.add(Convolution2D(filters=256, kernel_size=(5, 5), strides=(1, 1), padding='same'))
        # self.model.add(Activation('relu'))
        # self.model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))
        #
        # self.model.add(Convolution2D(filters=384, kernel_size=(3, 3), strides=(1, 1), padding='same'))
        # self.model.add(Activation('relu'))
        # self.model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))
        #
        # self.model.add(Convolution2D(filters=256, kernel_size=(3, 3), strides=(1, 1), padding='same'))
        # self.model.add(Activation('relu'))
        # self.model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))



        top_model= Sequential()
        top_model.add(Convolution2D(filters=512, kernel_size=(3, 3), strides=(1, 1), padding='valid',input_shape=base_model.output_shape[1:]))
        # self.model.add(BatchNormalization())#标准化
        top_model.add(Activation('relu'))  # 激活层
        top_model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='valid'))


        top_model.add(Flatten())
        top_model.add(Dense(4096))
        top_model.add(Activation('relu'))
        top_model.add(Dropout(0.3))
        top_model.add(Dense(4096))
        top_model.add(Activation('relu'))
        top_model.add(Dropout(0.3))
        top_model.add(Dense(self.dataset.num_classes))
        top_model.add(Activation('softmax'))
        self.model = models.Model(inputs=base_model.input, outputs=top_model(base_model.output))



        # self.model.add(Flatten())
        # self.model.add(Dense(4096))
        # self.model.add(Activation('relu'))
        # self.model.add(Dropout(0.3))
        # self.model.add(Dense(4096))
        # self.model.add(Activation('relu'))
        # self.model.add(Dropout(0.3))
        # self.model.add(Dense(self.dataset.num_classes))
        # self.model.add(Activation('softmax'))脸识别场景下的金融反欺诈模型脸识别场景下的金融反欺诈模型脸识别场景下的金融反欺诈模型


        self.model.summary()
def LoadPretrainedModel(model, toplayers):
    if model == 'VGG16':
        print('pretrained VGG16 model is successfully loaded ')
        VggModel = VGG16(include_top=toplayers, weights='imagenet') 
        return VggModel
    else:
        print('pretrained mobilenet model is successfully loaded ')
        mobilemodel = mobilenet.MobileNet(include_top=toplayers, weights='imagenet')
        return mobilemodel
def make_prediction(image):
    mobilenet_model = mobilenet.MobileNet()
    prediction = mobilenet_model.predict(image)
    results = imagenet_utils.decode_predictions(prediction)
    print('Decoded predictions: ', results)
    return {
        'prediction': results[0][0][1],
        'confidence': float(results[0][0][2]),
    }
    def _load_mobilenet(self, input_tensor):
        mobilenet_model = mobilenet.MobileNet(
            input_tensor=input_tensor, weights='imagenet')  #, alpha=0.75)
        cropped_model = Model(
            inputs=mobilenet_model.input,
            outputs=mobilenet_model.get_layer(index=20).output)
        cropped_model.layers[0].inbound_nodes = []
        cropped_model.layers[-1].outbound_nodes = []

        return cropped_model
Exemple #13
0
def classify_image_with_mobilenet():

    model = mobilenet.MobileNet(weights='imagenet')
    print(model.summary())

    image = load_image()
    image = mobilenet.preprocess_input(image)

    predicts = model.predict(image)  
    print(imagenet_utils.decode_predictions(predicts))
Exemple #14
0
    def __init__(self, model, input_size):

        input_shape = (input_size, input_size, 3)

        if model == 'xception':
            base_model = xception.Xception(weights='imagenet',
                                           include_top=False,
                                           pooling='max',
                                           input_shape=input_shape)
        elif model == 'vgg16':
            base_model = vgg16.VGG16(weights='imagenet',
                                     include_top=False,
                                     pooling='max',
                                     input_shape=input_shape)
        elif model == 'vgg19':
            base_model = vgg19.VGG19(weights='imagenet',
                                     include_top=False,
                                     pooling='max',
                                     input_shape=input_shape)
        elif model == 'inception_v3':
            base_model = inception_v3.InceptionV3(weights='imagenet',
                                                  include_top=False,
                                                  pooling='max',
                                                  input_shape=input_shape)
        elif model == 'mobilenet':
            base_model = mobilenet.MobileNet(weights='imagenet',
                                             include_top=False,
                                             pooling='max',
                                             input_shape=input_shape)
        elif model == 'inception_resnet_v2':
            base_model = inception_resnet_v2.InceptionResNetV2(
                weights='imagenet',
                include_top=False,
                pooling='max',
                input_shape=input_shape)
        elif model == 'resnet50':
            base_model = resnet50.ResNet50(weights='imagenet',
                                           include_top=False,
                                           pooling='max',
                                           input_shape=input_shape)
        elif model == 'nasnetlarge':
            base_model = nasnet.NASNetLarge(weights='imagenet',
                                            include_top=False,
                                            pooling='max',
                                            input_shape=input_shape)
        else:
            base_model = nasnet.NASNetMobile(weights='imagenet',
                                             include_top=False,
                                             pooling='max',
                                             input_shape=input_shape)

        self.input_size = input_size
        self.model = base_model
        self.graph = tf.get_default_graph()
        base_model.summary()
def mobilenet_model():
    base_model = mobilenet.MobileNet(input_shape=(WIDTH, HEIGHT, NB_CHANNELS), include_top=False)
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    predictions = Dense(NUM_CLASSES, activation='softmax')(x)
    model = Model(base_model.input, predictions)
    for layer in base_model.layers:
        layer.trainable = False

    model.compile(Adam(lr=1e-3), loss='categorical_crossentropy', metrics=['accuracy'])
    return model
def mobile(input_image, **kwargs):
    from keras.applications import mobilenet
    model = mobilenet.MobileNet(input_tensor=input_image,
                                include_top=False,
                                **kwargs)
    return [
        model.get_layer(name=n).output for n in [
            'conv_pw_1_relu', 'conv_pw_3_relu', 'conv_pw_5_relu',
            'conv_pw_11_relu', 'conv_pw_13_relu'
        ]
    ]
Exemple #17
0
    def get_architecture(self, params):
        from keras import models
        from keras.applications import mobilenet
        from keras.layers import Dense, Dropout, MaxPooling2D, BatchNormalization, Flatten, Conv2D, Conv3D, MaxPooling3D, AveragePooling3D, Softmax, AveragePooling2D

        # input_shape = params['input_shape'] #TODO: check_dims

        initial_model = mobilenet.MobileNet()  # TODO: tal vez incluir image_data_format='channels_last'

        for i in range(len(initial_model.layers) - 1 - params['last_layers']):
            initial_model.layers[i].trainable = False

        model = initial_model.layers[
            len(initial_model.layers) - 1 - params['last_layers']].output  # assuming you want the 3rd layer from the last

        if not params['use_sklearn']:

            dimensions = params['custom_dimension_list']
            layers = params['custom_layer_list']

            for i in range(len(layers)):
                if (layers[i].lower() == 'conv3'):
                    model = Conv3D(params['batch'], kernel_size=dimensions[i],
                                   strides=(1, 1,1),
                                   activation=params['activation'])(model)
                elif (layers[i].lower() == 'maxpool3'):
                    model = MaxPooling3D(pool_size=dimensions[i])(model)
                elif (layers[i].lower() == 'avgpool3'):
                    model = AveragePooling3D(pool_size=dimensions[i])(model)
                elif (layers[i].lower() == 'conv2'):
                    model = Conv2D(params['batch'], kernel_size=dimensions[i],
                                   strides=(1, 1),
                                   activation=params['activation'])(model)
                elif (layers[i].lower() == 'maxpool2'):
                    model = MaxPooling2D(pool_size=dimensions[i])(model)
                elif (layers[i].lower() == 'avgpool2'):
                    model = AveragePooling2D(pool_size=dimensions[i])(model)
                elif (layers[i].lower() == 'drop'):
                    model = Dropout(dimensions[i])(model)
                elif (layers[i].lower() == 'flatten'):
                    model = Flatten()(model)
                elif (layers[i].lower() == 'norm'):
                    model = BatchNormalization(axis=-1)(model)
                elif (layers[i].lower() == 'dense'):
                    model = Dense(dimensions[i], activation=params['activation'])(model)
                elif (layers[i].lower() == 'softmax'):
                    model = Softmax()(model)
        else:
            model = Flatten()(model)

        model = models.Model(initial_model.input, model)

        return model
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
Exemple #19
0
    def _get_model():
        '''
        Lazy loading the model to save time
        In addition, hack to surpress write to stderr during keras initialization
        '''

        _stderr = sys.stderr
        sys.stderr = open(os.devnull, 'wb')
        from keras.applications import mobilenet
        sys.stderr.close()
        sys.stderr = _stderr

        return mobilenet.MobileNet(weights='imagenet')
Exemple #20
0
 def mobilenet_classificator(self, image_path):
     mobilenet_model = mobilenet.MobileNet(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 = mobilenet.preprocess_input(image_batch.copy())
     predictions = mobilenet_model.predict(processed_image)
     label = decode_predictions(predictions)
     return sorted(label[0], key=lambda x: x[2], reverse=True)
def upload_file1():
   if request.method == 'POST':
      f = request.files['file']
      mobilenet_model = mobilenet.MobileNet(weights='mobilenet_1_0_224_tf.h5')
      original = load_img(f, target_size=(224, 224))
      numpy_image = img_to_array(original)
      image_batch = np.expand_dims(numpy_image, axis=0)
      processed_image = mobilenet.preprocess_input(image_batch)

      predictions = mobilenet_model.predict(processed_image)
      
      label = decode_predictions(predictions)
      
      return str(label)
Exemple #22
0
    def getBboxRegressor(self):
        in_shape = (self.im_width, self.im_height, 3)
        base_model = mobilenet.MobileNet(include_top=False,
                                         input_shape=in_shape,
                                         alpha=0.25)

        # without the final pooling layer
        #x = base_model.layers[-2].output
        x = base_model.output
        x = Flatten()(x)
        x = Dense(units=4, activation='linear')(x)
        model = Model(inputs=base_model.input, outputs=x)
        model.compile(loss=self.scaledSquaredDistanceBboxLoss,
                      optimizer='adam')
        return model
def classify_image(test_img_path):
	# define the mobilenet model
	mobilenet_model = mobilenet.MobileNet()

	# process the test image
	pImg = process_image(test_img_path)

	# make predictions on test image using mobilenet
	prediction = mobilenet_model.predict(pImg)

	# obtain the top-5 predictions
	results = imagenet_utils.decode_predictions(prediction)
	print('classification results',[(clz, score) for _, clz, score in results[0]])

	return [clz for _, clz, score in results[0][0:3] if score > THRESHOLD]
Exemple #24
0
def build_mobilenet_model():
    # get the model without the denses
    base_model = mobilenet.MobileNet(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
Exemple #25
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
Exemple #26
0
    def MobileNet(self):
        model = mobilenet.MobileNet(include_top=False, input_shape=(self.image_size, self.image_size, 3))

        for layer in model.layers:
            #freeze the layers
            layer.trainable = False

        x = model.output
        x = Flatten()(x)
        x = Dense(64, activation='relu')(x)
        x = Dropout(0.5)(x)
        x = Dense(32, activation='relu')(x)
        pred = Dense(self.num_classes, activation='softmax')(x)

        model = Model(inputs=model.input, outputs=pred)
        model.compile(optimizer='adam', loss='categorical_crossentropy',
                metrics=['accuracy', metric.precision, metric.recall, metric.f1])

        model.name = 'MobileNet'
        return model
def pretrained_model(model):
    if model == 'densenet':
        base_model = densenet.DenseNet121(include_top=False,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3))
    elif model == 'inception':
        base_model = inception_v3.InceptionV3(include_top=False,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3))
    elif model == 'mobilenet':
        base_model = mobilenet.MobileNet(include_top=False,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3))
    elif model == 'vgg':
        base_model = vgg19.VGG19(include_top=True,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3))
    elif model == 'resnet':
        base_model = resnet50.ResNet50(include_top=False,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3))
    elif model == 'xception':
        base_model = xception.Xception(include_top=False,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3))
    for layer in base_model.layers:
        layer.trainable = False
    base_model = Model(inputs = base_model.input, outputs = base_model.get_layer('fc2').output)
    #x = Dense(2048,activation='relu')(x)
    #x = Dropout(0.2)(x)
    predictions = Dense(1108,activation='softmax')(base_model.output)
    return models.Model(base_model.input,predictions)
Exemple #28
0
def create_base_model(input_shape, base_id):

    # Inception-v3
    if base_id is 0:
        base = inception_v3.InceptionV3(input_shape=input_shape,
                                        weights='imagenet',
                                        include_top=False)
        base_name = 'Inception-V3'

    # MobileNet
    elif base_id is 1:
        base = mobilenet.MobileNet(input_shape=input_shape,
                                   weights='imagenet',
                                   include_top=False)
        base_name = 'MobileNet'

    #Inception-ResNet-v2
    elif base_id is 2:
        base = inception_resnet_v2.InceptionResNetV2(input_shape=input_shape,
                                                     weights='imagenet',
                                                     include_top=False)
        base_name = 'InceptionResNet-v2'

    #ResNet50
    elif base_id is 3:
        base = resnet50.ResNet50(input_shape=input_shape,
                                 weights='imagenet',
                                 include_top=False)
        base_name = 'ResNet50'

    print("\nBase Network: %s" % base_name)

    top = GlobalAveragePooling2D()(base.output)

    # freeze all layers in the base network
    for layers in base.layers:
        layers.trainable = False

    model = Model(inputs=base.input, outputs=top, name='base_model')

    return model
Exemple #29
0
def process(filename,modelname):
    img = cv2.imread(filename)
    img = cv2.resize(img, (224,224))[...,::-1]
    img = np.expand_dims(img, axis=0).astype(np.float32)
    x_batch = preprocess_input(img)
    if modelname == 'ResNet50':
        model = resnet50.ResNet50(include_top=True, weights='imagenet')
    elif modelname == 'MobileNet':
        model = mobilenet.MobileNet(include_top=True, weights='imagenet')
    else:
        model = vgg19.VGG19(include_top=True, weights='imagenet')
    pred = model.predict(x_batch)
    decode_result = decode_predictions(pred)[0]
    result = []
    for r in decode_result:
        result.append({'name':r[1], 'prob':r[2]*100})

    return render_template('predict.html',
                           model = modelname,
                           filename=filename,
                           predictions=result)
def _mobilenet(num_classes,
               input_shape=(32, 32, 3),
               pretrained=True,
               freezed=True):
    input_tensor = Input(shape=input_shape)

    weights = 'imagenet' if pretrained else None
    base_model = mobilenet.MobileNet(input_tensor=input_tensor,
                                     weights=weights,
                                     include_top=False)
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Reshape(shape, name='reshape_1')(x)
    x = Dropout(dropout, name='dropout')(x)
    x = Conv2D(classes, (1, 1), padding='same', name='conv_preds')(x)
    x = Activation('softmax', name='act_softmax')(x)
    predictions = Reshape((num_classes, ), name='reshape_2')(x)
    if freezed:
        for layer in base_model.layers:
            layer.trainable = False
    model = Model(inputs=base_model.input, outputs=predictions)
    return model