Beispiel #1
0
def model_DenseNet121_multich(size, upd_ch):
    from keras.models import Model, load_model
    from keras.layers import Dense, Input
    from keras.applications import DenseNet121

    ref_ch = 3
    required_layer_name = 'conv1/conv'

    weights_cache_path = CACHE_PATH + 'model_DenseNet121_{}ch_imagenet_{}.h5'.format(
        upd_ch, size)
    if not os.path.isfile(weights_cache_path):
        model_ref = DenseNet121(
            include_top=False,
            weights='imagenet',
            input_shape=(size, size, ref_ch),
            pooling='avg',
        )
        model_upd = DenseNet121(
            include_top=False,
            weights=None,
            input_shape=(size, size, upd_ch),
            pooling='avg',
        )

        for i, layer in enumerate(model_ref.layers):
            print('Update weights layer [{}]: {}'.format(i, layer.name))
            if layer.name == required_layer_name:
                print('Recalc weights!')
                config = layer.get_config()
                use_bias = config['use_bias']
                if use_bias:
                    w, b = layer.get_weights()
                else:
                    w = layer.get_weights()[0]
                print('Use bias?: {}'.format(use_bias))
                print('Shape ref: {}'.format(w.shape))
                shape_upd = (w.shape[0], w.shape[1], upd_ch, w.shape[3])
                print('Shape upd: {}'.format(shape_upd))

                w_new = np.zeros(shape_upd, dtype=np.float32)
                for j in range(upd_ch):
                    w_new[:, :,
                          j, :] = ref_ch * w[:, :, j % ref_ch, :] / upd_ch
                if use_bias:
                    model_upd.layers[i].set_weights((w_new, b))
                else:
                    model_upd.layers[i].set_weights((w_new, ))
                continue
            else:
                model_upd.layers[i].set_weights(layer.get_weights())
        model_upd.save(weights_cache_path)
    else:
        model_upd = load_model(weights_cache_path)

    x = model_upd.layers[-1].output
    x = Dense(NUM_CLASSES, activation='softmax', name='prediction')(x)
    model = Model(inputs=model_upd.inputs, outputs=x)
    # print(model.summary())
    return model
Beispiel #2
0
    def __init__(self, input_size, weights):
        input_image = Input(shape=(input_size[0], input_size[1], 3))

        if weights == 'imagenet':
            densenet = DenseNet121(input_tensor=input_image, include_top=False, weights='imagenet', pooling=None)
            print('Successfully loaded imagenet backend weights')
        else:
            densenet = DenseNet121(input_tensor=input_image, include_top=False, weights=None, pooling=None)
            if weights:
                densenet.load_weights(weights)
                print('Loaded backend weigths: ' + weights)

        self.feature_extractor = densenet
Beispiel #3
0
def get_model(summary=False,
              img_width=150,
              fc_layers=[4096, 4096],
              fc_dropout_layers=[0.5, 0.5]):
    # Get back the convolutional part of a VGG network trained on ImageNet
    base_model = DenseNet121(input_tensor=Input(shape=(img_width, img_width,
                                                       3)),
                             include_top=False)
    x = GlobalAveragePooling2D(name='avg_pool')(base_model.output)
    x = Dropout(0.5)(x)
    x = Dense(10,
              activation='softmax',
              kernel_regularizer=regularizers.l2(0.01))(x)
    my_model = Model(input=base_model.input, output=x)
    layers_to_freeze = 313
    for i in range(layers_to_freeze):
        my_model.layers[i].trainable = False
    if summary:
        print("---------------------------------------------------------")
        for i, layer in enumerate(my_model.layers):
            print(i, layer.name)
        print("---------------------------------------------------------")
        print("---------------------------------------------------------")
        print("---------------------------------------------------------")
        my_model.summary()
    return my_model, layers_to_freeze, 3
Beispiel #4
0
def load_densenet121(width, height, classes_num):
    with tf.device('/cpu:0'):
        model = DenseNet121(weights=None,
                            input_shape=(width, height, 3),
                            classes=classes_num)

    return model
    def __init__(self,
                 model='resnet34',
                 batchSize=64,
                 input_shape=(224, 224, 3),
                 sampleSize=None):
        '''
        model: inception_v3, vgg13, vgg16, vgg19, resnet18, resnet34,
               resnet50, resnet101, or resnet152
        '''

        self.model = model
        self.batch_size = batchSize
        self.input_shape = input_shape
        self.sampleSize = sampleSize

        if "resnet50" in self.model:
            self.resnet50 = ResNet50(include_top=False,
                                     weights="imagenet",
                                     input_shape=input_shape)
            x = self.resnet50.output
            x = GlobalAveragePooling2D()(x)
            self.resnet50 = Model(inputs=self.resnet50.input, outputs=x)
        elif "densenet121" in self.model:
            self.densenet_model = DenseNet121(include_top=False,
                                              weights="imagenet",
                                              input_shape=input_shape)
            x = self.densenet_model.output
            x = GlobalAveragePooling2D()(x)
            self.densenet_model = Model(inputs=self.densenet_model.input,
                                        outputs=x)
Beispiel #6
0
def UDenseNet121(input_shape=(None, None, 3),
                 classes=1,
                 decoder_filters=16,
                 decoder_block_type='upsampling',
                 encoder_weights=None,
                 input_tensor=None,
                 activation='sigmoid',
                 **kwargs):

    backbone = DenseNet121(input_shape=input_shape,
                           input_tensor=input_tensor,
                           weights=encoder_weights,
                           include_top=False)

    skip_connections = list(reversed([4, 51, 139, 311]))
    model = build_unet(backbone,
                       classes,
                       decoder_filters,
                       skip_connections,
                       block_type=decoder_block_type,
                       activation=activation,
                       **kwargs)
    model.name = 'u-densenet121'

    return model
Beispiel #7
0
def shadowModel(class_num,
                activation="softmax",
                loss='binary_crossentropy',
                learning_rate=0.00005):
    """
    Set the default configuration for a DenseNet Model.
    :param class_num: The number of classes
    :param activation: Set the activation function and the default function is softmax
    :param loss: Set the loss function and the default function is binary_crossentropy
    :param learning_rate: Set the learning rate and the default rate is 0.00005
    :return: A DenseNet Model
    """
    densenet = DenseNet121(
        weights='densenet-keras/DenseNet-BC-121-32-no-top.h5',
        include_top=False,
        input_shape=(224, 224, 3))

    model = Sequential()
    model.add(densenet)
    model.add(layers.GlobalAveragePooling2D())
    model.add(layers.Dense(class_num))
    model.add(layers.Activation(activation))

    model.summary()

    model.compile(loss=loss,
                  optimizer=Adam(lr=learning_rate),
                  metrics=['accuracy', precision, recall, f1])

    return model
def func4(shape):
	from keras.applications import DenseNet121
	BS = 32
	conv_base = DenseNet121(weights = 'imagenet',
                 	include_top = False,
                 	input_shape = (shape[0],shape[1],3))
	return BS,conv_base
Beispiel #9
0
def change_model(model0):  # 选择模型
    if model0 == "ResNet50":
        tr_model = ResNet50(include_top=False,
                            weights='imagenet',
                            input_shape=(220, 220, 3),
                            pooling='avg')
    elif model0 == "VGG19":
        tr_model = VGG19(include_top=False,
                         weights='imagenet',
                         input_shape=(220, 220, 3),
                         pooling='avg')
    elif model0 == "InceptionV3":
        tr_model = InceptionV3(include_top=False,
                               weights='imagenet',
                               input_shape=(220, 220, 3),
                               pooling='avg')
    # 不能用input_shape=(220, 220, 3)
    #elif model0 == "MobileNet":
    #   tr_model = MobileNet(include_top=False, weights='imagenet', input_shape=(220, 220, 3), pooling='avg')
    #只能在weights=None时使用
    #elif model0 == "NASNetMobile":
    #    tr_model = NASNetMobile(include_top=False, weights='imagenet', input_shape=(220, 220, 3), pooling='avg')
    elif model0 == "Xception":
        tr_model = Xception(include_top=False,
                            weights='imagenet',
                            input_shape=(220, 220, 3),
                            pooling='avg')
    elif model0 == "DenseNet121":
        tr_model = DenseNet121(include_top=False,
                               weights='imagenet',
                               input_shape=(220, 220, 3),
                               pooling='avg')
    return tr_model
Beispiel #10
0
def build_model(base='DenseNet121',
                n_class=21,
                weights_conv='imagenet',
                input_shape=(224, 224, 3)):

    now = datetime.datetime.now().strftime("%Y-%m-%d_%Hh%Mm%Ss")
    model_name = f'{base}_{now}'

    model = Sequential(name=model_name)

    if base == 'DenseNet121':

        conv = DenseNet121(input_shape=input_shape,
                           include_top=False,
                           weights=weights_conv)

        model.add(conv)
        model.add(GlobalAveragePooling2D())
        model.add(Dense(n_class, activation='softmax'))

    else:
        print('Only DenseNet121 is supported for CNN')
        return

    return model
Beispiel #11
0
def build_densenet():
    densenet = DenseNet121(weights='imagenet', include_top=False)

    input = Input(shape=(SIZE, SIZE, N_ch))
    x = Conv2D(3, (3, 3), padding='same')(input)

    x = densenet(x)

    x = GlobalAveragePooling2D()(x)
    x = BatchNormalization()(x)
    x = Dropout(0.5)(x)
    x = Dense(256, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dropout(0.5)(x)

    # multi output
    output = Dense(15, activation='softmax', name='root')(x)

    # model
    model = Model(input, output)

    optimizer = Adam(lr=0.002,
                     beta_1=0.9,
                     beta_2=0.999,
                     epsilon=0.1,
                     decay=0.0)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])
    model.summary()

    return model
Beispiel #12
0
def get_densenet121_unet_softmax(input_shape, weights='imagenet'):
    blocks = [6, 12, 24, 16]
    img_input = Input(input_shape + (4, ))

    x = ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input)
    x = Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)
    x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x)
    x = Activation('relu', name='conv1/relu')(x)
    conv1 = x
    x = ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
    x = MaxPooling2D(3, strides=2, name='pool1')(x)
    x = dense_block(x, blocks[0], name='conv2')
    conv2 = x
    x = transition_block(x, 0.5, name='pool2')
    x = dense_block(x, blocks[1], name='conv3')
    conv3 = x
    x = transition_block(x, 0.5, name='pool3')
    x = dense_block(x, blocks[2], name='conv4')
    conv4 = x
    x = transition_block(x, 0.5, name='pool4')
    x = dense_block(x, blocks[3], name='conv5')
    x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x)
    conv5 = x

    conv6 = conv_block(UpSampling2D()(conv5), 320)
    conv6 = concatenate([conv6, conv4], axis=-1)
    conv6 = conv_block(conv6, 320)

    conv7 = conv_block(UpSampling2D()(conv6), 256)
    conv7 = concatenate([conv7, conv3], axis=-1)
    conv7 = conv_block(conv7, 256)

    conv8 = conv_block(UpSampling2D()(conv7), 128)
    conv8 = concatenate([conv8, conv2], axis=-1)
    conv8 = conv_block(conv8, 128)

    conv9 = conv_block(UpSampling2D()(conv8), 96)
    conv9 = concatenate([conv9, conv1], axis=-1)
    conv9 = conv_block(conv9, 96)

    conv10 = conv_block(UpSampling2D()(conv9), 64)
    conv10 = conv_block(conv10, 64)
    res = Conv2D(3, (1, 1), activation='softmax')(conv10)
    model = Model(img_input, res)

    if weights == 'imagenet':
        densenet = DenseNet121(input_shape=input_shape + (3, ),
                               weights=weights,
                               include_top=False)
        w0 = densenet.layers[2].get_weights()
        w = model.layers[2].get_weights()
        w[0][:, :, [0, 1, 2], :] = 0.9 * w0[0][:, :, :3, :]
        w[0][:, :, 3, :] = 0.1 * w0[0][:, :, 1, :]
        model.layers[2].set_weights(w)
        for i in range(3, len(densenet.layers)):
            model.layers[i].set_weights(densenet.layers[i].get_weights())
            model.layers[i].trainable = False

    return model
Beispiel #13
0
    def build(self) -> Model:
        model = DenseNet121(include_top=True,
                            weights=None,
                            input_shape=(self.width, self.height,
                                         self.channels),
                            classes=2)

        return model
Beispiel #14
0
 def test_validate_keras_densenet(self):
     input_tensor = Input(shape=(224, 224, 3))
     model = DenseNet121(weights="imagenet", input_tensor=input_tensor)
     file_name = "keras" + model.name + ".pmml"
     pmml_obj = KerasToPmml(model,
                            dataSet="image",
                            predictedClasses=[str(i) for i in range(1000)])
     pmml_obj.export(open(file_name, 'w'), 0)
     self.assertEqual(self.schema.is_valid(file_name), True)
Beispiel #15
0
def build_model(lr, l2, activation='sigmoid'):
    ##############
    # BRANCH MODEL
    ##############
    regul = regularizers.l2(l2)
    optim = Adam(lr=lr)
    kwargs = {'padding': 'same', 'kernel_regularizer': regul}
    
    
    img_input=Input(shape=img_shape)
    img_conc = Concatenate()([img_input, img_input, img_input])
    model2 = DenseNet121(input_tensor=img_conc, weights=None, include_top=False)
    
    
    
    
    x = GlobalAveragePooling2D(name='global_average_pooling_x')(model2.output)
    branch_model = Model(inputs=[model2.input], outputs=x)

    
    
    ############
    # HEAD MODEL
    ############
    mid = 32
    xa_inp = Input(shape=branch_model.output_shape[1:])
    xb_inp = Input(shape=branch_model.output_shape[1:])
    x1 = Lambda(lambda x: x[0] * x[1])([xa_inp, xb_inp])
    x2 = Lambda(lambda x: x[0] + x[1])([xa_inp, xb_inp])
    x3 = Lambda(lambda x: K.abs(x[0] - x[1]))([xa_inp, xb_inp])
    x4 = Lambda(lambda x: K.square(x))(x3)
    x = Concatenate()([x1, x2, x3, x4])
    x = Reshape((4, branch_model.output_shape[1], 1), name='reshape1')(x)

    # Per feature NN with shared weight is implemented using CONV2D with appropriate stride.
    x = Conv2D(mid, (4, 1), activation='relu', padding='valid')(x)
    x = Reshape((branch_model.output_shape[1], mid, 1))(x)
    x = Conv2D(1, (1, mid), activation='linear', padding='valid')(x)
    x = Flatten(name='flatten')(x)

    # Weighted sum implemented as a Dense layer.
    x = Dense(1, use_bias=True, activation='sigmoid', name='weighted-average')(x)
    head_model = Model([xa_inp, xb_inp], x, name='head')

    ########################
    # SIAMESE NEURAL NETWORK
    ########################
    # Complete model is constructed by calling the branch model on each input image,
    # and then the head model on the resulting 512-vectors.
    img_a = Input(shape=img_shape)
    img_b = Input(shape=img_shape)
    xa = branch_model(img_a)
    xb = branch_model(img_b)
    x = head_model([xa, xb])
    model = Model([img_a, img_b], x)
    return model, branch_model, head_model
Beispiel #16
0
    def __init__(self, num_classes=10):
        """Declare all needed layers."""
        self.num_classes = num_classes
        try:
            # weights_path =None
            # weights_path = remote_helper.get_remote_date("https://www.flyai.com/m/v0.2|resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5")
            weights_path = remote_helper.get_remote_data(
                'https://www.flyai.com/m/v0.8|densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5'
            )
            # weights_path = remote_helper.get_remote_date('https://www.flyai.com/m/v0.8|densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5')
        except OSError:
            weights_path = 'imagenet'

        # base_model = ResNet50(weights=None, input_shape=input_shape=(img_size[0], img_size[1], 3), include_top=False)
        base_model = DenseNet121(weights=weights_path,
                                 include_top=False,
                                 input_shape=(img_size[0], img_size[1], 3))

        Inp = Input(shape=(img_size[0], img_size[1], 3))

        # x = Conv2D(256,3,
        #                   activation='relu',
        #                   padding='same',
        #                   name='wangyi_conv1')(Inp)
        # x = Conv2D(256,5,
        #                   activation='relu',
        #                   padding='same',
        #                   name='wangyi_conv2')(x)
        # x = MaxPooling2D((2, 2), strides=(1, 1), name='wangyi_pool')(x)
        # x =Flatten()(x)
        # x = Conv2D(3,7,
        #                   activation='relu',
        #                   padding='same',
        #                   name='wangyi_conv3')(x)
        # 增加定制层
        x = base_model(Inp)
        # x = base_model.output
        # x = GlobalAveragePooling2D()(x)
        # x = Flatten(name='flatten_1')(x)

        # 冻结不打算训练的层。
        # print('base_model.layers', len(base_model.layers))
        # for i, layer in enumerate(base_model.layers):
        #     print(i, layer.name)
        #
        # for layer in base_model.layers[:]:
        #     layer.trainable = False
        # print(layer)

        x = GlobalAveragePooling2D()(x)
        # x = Flatten(name='flatten_1')(x)
        # x = Dense(2048, activation='relu' )(x)
        predictions = Dense(num_classes, activation="softmax")(x)
        # 创建最终模型

        self.model_cnn = keras_model(inputs=Inp, outputs=predictions)
Beispiel #17
0
    def buildDenseNet121Base(self):

        print("building `DenseNet121` base model...")

        # default INPUT_SIZE = 224
        base_model = DenseNet121(input_shape=self.INPUT_SHAPE,
                                 weights='imagenet',
                                 include_top=False)

        return base_model
Beispiel #18
0
def build_model(mode, model_name=None, model_path=None):

    clear_session()

    if mode == 'train':
        img = Input(shape=(224, 224, 3))

        if model_name == 'DenseNet121':

            model = DenseNet121(include_top=False,
                                weights='imagenet',
                                input_tensor=img,
                                input_shape=None,
                                pooling='avg')

        elif model_name == 'MobileNet':

            model = MobileNet(include_top=False,
                              weights='imagenet',
                              input_tensor=img,
                              input_shape=None,
                              pooling='avg')

        elif model_name == 'Xception':

            model = Xception(include_top=False,
                             weights='imagenet',
                             input_tensor=img,
                             input_shape=None,
                             pooling='avg')

        elif model_name == 'ResNet50':

            model = ResNet50(include_top=False,
                             weights='imagenet',
                             input_tensor=img,
                             input_shape=None,
                             pooling='avg')

        final_layer = model.layers[-1].output

        dense_layer_1 = Dense(128, activation='relu')(
            final_layer)  # funcion activacion
        output_layer = Dense(10, activation='sigmoid')(
            dense_layer_1)  # funcion activacion

        model = Model(input=img, output=output_layer)
        model.compile(optimizer='adam',
                      loss='binary_crossentropy',
                      metrics=['accuracy'])

    elif mode == 'inference':
        model = load_model(model_path)

    return model
    def make_densenet_121(self, weights):
        model = DenseNet121(
            include_top=False,
            weights=weights,
            input_tensor=None,
            input_shape=self.input_dim,
            pooling=None,
            classes=1000,
        )

        return model
Beispiel #20
0
def get_model():
    conv_base = DenseNet121(weights=None,
                            include_top=False,
                            input_shape=(224, 224, 3))

    x = layers.GlobalAveragePooling2D()(conv_base.output)
    x = layers.Dense(9, activation='softmax', name='fc_out')(x)

    model = models.Model(inputs=conv_base.input, outputs=x)

    return model
Beispiel #21
0
    def build(self, output_layer=False, learning_rate=False, loadfile=False):
        '''Build multi-task DenseNet121 model.'''

        strategy = tf.distribute.MirroredStrategy()                                         # Set distributed strategy
        print('Number of devices: {}'.format(strategy.num_replicas_in_sync))
        (numC, numA, numT, numH) = self.nums                                                # Get number of classes
        with strategy.scope():
            base_model = DenseNet121(weights='imagenet', include_top=True,                      # Import DenseNet121
                                     input_tensor=Input(shape=(self.scaleDim, self.scaleDim, 3)))

            base_model.layers.pop()                                                             # Remove the last layer
            print('Base model: {}\nNumber of Layers: {}'.format(base_model.name,
                                                                    len(base_model.layers)))
            self.name = base_model.name

            if output_layer:
                self.output_layer = output_layer        # Set width of PFP layer
            else:
                self.output_layer = 512

            # Amend DenseNet backbone for multi-task learning

            x = base_model.output
            PFP = Dense(self.output_layer, input_shape=(2048,), activation='relu', name='PFP')(x)   # Protein fingerprint layer
            BN = BatchNormalization(name='BatchNorm')(PFP)
            Drop = Dropout(0.25)(BN)
            C_labels = Dense(numC, input_shape=(self.output_layer,), activation='softmax', name='C_labels')(Drop)
            A_labels = Dense(numA, input_shape=(self.output_layer,), activation='softmax', name='A_labels')(Drop)
            T_labels = Dense(numT, input_shape=(self.output_layer,), activation='softmax', name='T_labels')(Drop)
            H_labels = Dense(numH, input_shape=(self.output_layer,), activation='softmax', name='H_labels')(Drop)

            # Compile

            self.model = Model(inputs=base_model.input, outputs=[C_labels, A_labels, T_labels, H_labels])

            if not learning_rate:
                lr = 0.001
            else:
                lr = learning_rate

            if loadfile:
            # Load weights from pre-trained model
                print('Loading model weights from ', loadfile)
                self.model.load_weights(loadfile)
                print('Loaded')

            lossWeights = {"C_labels": 1.0, "A_labels": 1.0, "T_labels": 1.0, "H_labels": 5.0}
            opt = Adam(learning_rate=lr)
            self.model.compile(optimizer=opt,
                               loss={"C_labels": 'categorical_crossentropy', "A_labels": 'categorical_crossentropy',
                                     "T_labels": 'categorical_crossentropy', "H_labels": 'categorical_crossentropy'},
                               loss_weights=lossWeights,
                               metrics=['accuracy'])
Beispiel #22
0
def load_model():
    global model
    img_input = Input(shape=input_shape)
    base_model = DenseNet121(include_top=False,
                             input_tensor=img_input,
                             input_shape=input_shape,
                             pooling='avg')
    prediction_layer = Dense(len(CLASS_NAMES),
                             activation='sigmoid',
                             name='predictions')(base_model.output)
    model = Model(inputs=img_input, outputs=prediction_layer)
    model.load_weights('praeder_weights.h5')
Beispiel #23
0
def DenseNetBinClass(weights='imagenet', target_size=(224, 224)):
    base = DenseNet121(weights=weights,
                       include_top=False,
                       input_shape=target_size + (3, ))
    x = GlobalAveragePooling2D()(base.output)
    pred_layer = Dense(1, activation='sigmoid', name='pred_layer')(x)

    model = Model(inputs=base.input, outputs=pred_layer)

    opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)
    model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['acc'])

    return model
def get_model(model_name):

    if model_name == 'VGG16':

        from keras.applications import VGG16
        model = VGG16(weights="imagenet", include_top=False, pooling='avg')
        size = 512  # if pooling is 'avg', else  512 * 7 * 7 if pooling is 'None'

    elif model_name == 'ResNet50':

        from keras.applications import ResNet50
        model = ResNet50(weights="imagenet", include_top=False, pooling='avg')
        size = 2048,  # if pooling is 'avg',  2048 * 7 * 7 if pooling is 'None'

    elif model_name == 'ResNet152':

        from keras.applications import ResNet152
        model = ResNet152(weights="imagenet", include_top=False, pooling='avg')
        size = 2048  # if pooling is 'avg', 2048 * 7 * 7 # if pooling is 'None'

    elif model_name == 'ResNet152V2':

        from keras.applications import ResNet152V2
        model = ResNet152V2(weights="imagenet",
                            include_top=False,
                            pooling='avg')
        size = 2048  # if pooling is 'avg', 2048 * 7 * 7 # if pooling is 'None'

    elif model_name == 'DenseNet121':

        from keras.applications import DenseNet121
        model = DenseNet121(weights="imagenet",
                            include_top=False,
                            pooling='avg'),
        size = 1024  # if pooling is 'avg'

    elif model_name == 'Custom':

        ## CUSTOM MODEL

        from keras.models import load_model
        model = load_model(config.FINE_TUNED_MODEL)
        size = 2048  # our trained models are based on ResNet152

    else:

        raise ValueError(
            "Model needs to be defined. Examples: VGG16 or ResNet50.")

    return model, size
Beispiel #25
0
 def get_model(self):
     base = DenseNet121(
     include_top=False,
     input_shape = (136, 136, 3),
     weights=None
     )
     x = base.output
     x = Flatten()(x)
     x = Dense(512, activation="relu")(x)
     x = BatchNormalization()(x)
     x = Dropout(0.3)(x)
     x = Dense(256, activation="relu")(x)
     x = BatchNormalization()(x)
     x = Dropout(0.3)(x)
     x = Dense(7, activation="softmax")(x)
     return Model(inputs=[base.input], outputs = [x])
Beispiel #26
0
def build_model():

    densenet = DenseNet121(weights='DenseNet-BC-121-32-no-top.h5',
                           include_top=False,
                           input_shape=(224, 224, 3))

    model = Sequential()
    model.add(densenet)
    model.add(layers.GlobalMaxPooling2D())
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(5, activation='sigmoid'))

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

    return model
Beispiel #27
0
def DenseNetMultiClass(weights='imagenet', target_size=(224, 224), model=None):
    if model is not None:
        return model  # return model loaded from checkpoint

    base = DenseNet121(weights=weights,
                       include_top=False,
                       pooling='avg',
                       input_shape=target_size + (3, ))
    pred_layer = Dense(7, activation='sigmoid', name='pred_layer')(base.output)

    model = Model(inputs=base.input, outputs=pred_layer, name='dn')

    # opt = SGD(lr=0.01, momentum=0.9, decay=1e-4)
    opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)
    model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['acc'])

    return model
Beispiel #28
0
def talos_model(sub_train_images, y_train, sub_val_images, y_val, params):
    print(f"parameters: {params}")
    print(f"y_train.shape: {y_train.shape}")
    #input_tensor = Input(shape=(224, 224, 3))  # this assumes K.image_data_format() == 'channels_last'
    base_model = DenseNet121(weights='imagenet', include_top=False)
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(num_classes, activation='softmax')(x)

    model = Model(inputs=base_model.input, outputs=predictions)

    es = EarlyStopping(monitor='val_loss',
                       min_delta=0,
                       patience=5,
                       verbose=0,
                       mode='auto',
                       baseline=None,
                       restore_best_weights=True)
    mc = ModelCheckpoint('best_model.h5',
                         monitor='val_loss',
                         mode='max',
                         verbose=1,
                         save_best_only=True)

    for layer in base_model.layers:
        layer.trainable = True
    model.compile(optimizer=params['optimizer'](
        lr=lr_normalizer(params['lr'], params['optimizer'])),
                  loss=loss_fx,
                  metrics=metrics,
                  class_weight=class_weight)

    out = model.fit_generator(
        imageLoader(sub_train_images, y_train, params['batch_size']),
        steps_per_epoch=sub_train_images.shape[0] // params['batch_size'],
        epochs=20,
        validation_data=imageLoader(sub_val_images, y_val,
                                    params['batch_size']),
        validation_steps=sub_val_images.shape[0] // params['batch_size'],
        callbacks=[es, mc],
        verbose=2)

    #print(f"out:{out.history.keys()}")
    return out, model
Beispiel #29
0
def build_model(input_shape, summary=True) -> keras.Model:
    """Create keras Model and Compile."""
    inp = layers.Input(shape=input_shape)
    x = DenseNet121(weights=MODEL_PATH,
                    include_top=False,
                    input_shape=input_shape)(inp)
    x = layers.GlobalAveragePooling2D(name='last-pooling')(x)
    x = layers.BatchNormalization(name='last-bn')(x)
    x = layers.Dense(1024, activation='relu', name='fc')(x)
    x = layers.Dropout(0.5, name='last-dropout')(x)
    out = layers.Dense(1, activation=relu_max5, name='out')(x)
    model = models.Model(inp, out, name='aptos-densenet')

    model.compile(loss='mse', optimizer=Adam(lr=5e-5), metrics=[])
    if summary:
        model.summary()

    return model
def DenseNet(height, width, channels, classes):
    base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=(height, width, channels))
    for layer in base_model.layers[-4:]:
        layer.trainable = False
    x = base_model.layers[-1].output  # es la salida del ultimo activation despues del add
    x = layers.GlobalAveragePooling2D()(x)
    #x = layers.GlobalMaxPool2D()(x)
    # output layer
    #---1) NO LINEAL + LINEAL
    # prepredictions = Dense(256, activation='relu')(x)
    # predictions = Dense(classes, activation='sigmoid')(prepredictions)

    #---2) LINEAL
    predictions = Dense(classes, activation='sigmoid')(x)

    model = models.Model(inputs=base_model.input, outputs=predictions)
    model.summary()
    return model