Esempio n. 1
0
def load_model():
    global model
    print('Loading model')
    model = NASNetMobile(weights=None)
    model.load_weights('nasnet.h5')
    print('Loaded model')
    global graph
    graph = tf.get_default_graph()
def nasNetMoble(weights=None, include_top=False, input_shape=(224, 224, 3)):
    nasNet = NASNetMobile(weights=weights,
                          include_top=include_top,
                          input_shape=input_shape)
    if include_top:
        WRN_WEIGHTS_PATH = "https://www.flyai.com/m/v0.8|NASNet-mobile.h5"
    else:
        WRN_WEIGHTS_PATH = "https://www.flyai.com/m/v0.8|NASNet-mobile-no-top.h5"
    filename = WRN_WEIGHTS_PATH.split('|')[-1]
    fpath = get_file(filename, WRN_WEIGHTS_PATH, cache_subdir=MODEL_PATH)
    nasNet.load_weights(fpath)

    print('NASNetMobile weights loaded!')
    return nasNet
Esempio n. 3
0
def unet_model_3d(first_input_shape, second_input_shape, nb_classes,
                  feature_size):

    channel_first_first_input = Input(first_input_shape)
    first_input = Permute([2, 3, 4, 1])(channel_first_first_input)

    first_conv_permute = Permute([4, 2, 3, 1])(first_input)
    first_gpooling_0 = GlobalAveragePooling3D()(first_conv_permute)
    first_gpooling_dense_0 = Dense(units=32,
                                   activation='linear')(first_gpooling_0)
    first_gpooling_dense_1_1 = Dense(
        units=29, activation='sigmoid')(first_gpooling_dense_0)
    first_gpooling_fused_2 = Lambda(fuse)(
        [first_conv_permute, first_gpooling_dense_1_1])

    first_conv_layer0 = Conv3D(8, (5, 5, 5),
                               padding='same',
                               activation='linear')(first_input)

    first_conv_permute = Permute([4, 2, 3, 1])(first_conv_layer0)
    first_gpooling_0 = GlobalAveragePooling3D()(first_conv_permute)
    first_gpooling_dense_0 = Dense(units=32,
                                   activation='linear')(first_gpooling_0)
    first_gpooling_dense_1_0 = Dense(
        units=29, activation='sigmoid')(first_gpooling_dense_0)
    first_gpooling_fused_0 = Lambda(fuse)(
        [first_conv_permute, first_gpooling_dense_1_0])

    first_conv_layer1 = Conv3D(8, (3, 3, 3),
                               padding='same',
                               activation='linear')(first_conv_layer0)

    first_conv_permute = Permute([4, 2, 3, 1])(first_conv_layer1)
    first_gpooling_0 = GlobalAveragePooling3D()(first_conv_permute)
    first_gpooling_dense_0 = Dense(units=32,
                                   activation='linear')(first_gpooling_0)
    first_gpooling_dense_1_1 = Dense(
        units=29, activation='sigmoid')(first_gpooling_dense_0)
    first_gpooling_fused_1 = Lambda(fuse)(
        [first_conv_permute, first_gpooling_dense_1_1])

    first_gpooling_add_0 = Add()([
        first_gpooling_fused_0, first_gpooling_fused_1, first_gpooling_fused_2
    ])

    first_conv_layer2 = Conv2D(16, (3, 3), padding='same',
                               activation='linear')(first_gpooling_add_0)
    first_pooling_layer1 = MaxPooling2D(pool_size=(2, 2))(first_conv_layer2)

    first_conv_layer3 = Conv2D(16, (3, 3), padding='same',
                               activation='linear')(first_pooling_layer1)
    first_pooling_layer2 = MaxPooling2D(pool_size=(2, 2))(first_conv_layer3)

    first_conv_layer4 = Conv2D(16, (3, 3), padding='same',
                               activation='linear')(first_pooling_layer2)
    first_pooling_layer3 = MaxPooling2D(pool_size=(2, 2),
                                        padding='same')(first_conv_layer4)

    first_flatten_layer1 = Flatten()(first_pooling_layer3)
    first_dense_layer1 = Dense(units=feature_size,
                               activation='relu')(first_flatten_layer1)
    first_dense_layer2 = Dense(units=feature_size,
                               activation='relu')(first_dense_layer1)

    base_model = NASNetMobile(weights=None,
                              include_top=False,
                              input_shape=[128, 128, 3])
    base_model.load_weights('/home/mdd/.keras/models/nasnet_mobile_no_top.h5',
                            by_name=True)

    second_input = base_model.input
    activation_191_output = base_model.output
    gpooling = GlobalAveragePooling2D()(activation_191_output)

    concat_layer = concatenate([first_dense_layer2, gpooling], axis=1)

    input_target = Input(shape=(1, ))
    centers = Embedding(nb_classes, 2080)(input_target)
    l2_loss = Lambda(center_loss, name='l2_loss')([concat_layer, centers])

    concat_result = Dense(units=nb_classes,
                          activation='softmax',
                          name='softmax')(concat_layer)
    concat_model = Model(
        inputs=[channel_first_first_input, second_input, input_target],
        outputs=[concat_result, l2_loss])
    concat_test_model = Model(inputs=[channel_first_first_input, second_input],
                              outputs=concat_result)

    # return model_train, model_test, second_train_model
    return concat_model, concat_test_model
validateSamplesNumber = countFiles(validateFolder)
testSamplesNumber = countFiles(testFolder)

#model = resnet.ResnetBuilder.build_resnet_50((channels,img_width,img_height), numOfClasses) #_18 _34 _50 _101
#model = resnet_activity_reg.ResnetBuilder.build_resnet_50((channels,img_width,img_height), numOfClasses) #_18 _34 _50 _101
model = NASNetMobile(
    include_top=True,
    weights=None,
    #  '             input_tensor=Input(shape=input_shape))
    input_tensor=None,
    input_shape=input_shape,
    pooling='avg',
    classes=2)

if modelContinueFlag:
    model.load_weights(modelContinueWeigthsFile, by_name=False)

model.summary()
model.compile(
    loss='categorical_crossentropy',
    #model.compile(loss='categorical_hinge',
    #optimizer=Adam(lr=startingLeraningRate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0),
    #optimizer=RMSprop(lr=startingLeraningRate, rho=0.9, epsilon=None, decay=0.0),
    optimizer=SGD(lr=startingLeraningRate,
                  decay=1e-6,
                  momentum=0.9,
                  nesterov=True),
    metrics=['accuracy', 'categorical_accuracy']
)  # default lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0 or 0.00005

tensorboard = TensorBoard(
Esempio n. 5
0
  def Build_model(self):

    ############# Feature Extraction Network ##################

    if self.Backbone_model == "VGG16":

      Backbone = VGG16(input_shape=(352,352,3), include_top=False, weights='imagenet')

      Stages=[Backbone.get_layer("block5_conv3").output, Backbone.get_layer("block4_conv3").output,
              Backbone.get_layer("block3_conv3").output]

              # # # # # # # # # # # # # # # # # # # # # # # # # # # #
                              
    elif self.Backbone_model == "ResNet50":

      Backbone = ResNet50(input_shape=(352,352,3), include_top=False, weights='imagenet')

      Stages=[Backbone.output, Backbone.get_layer("conv4_block6_out").output,
              Backbone.get_layer("conv3_block4_out").output, Backbone.get_layer("conv2_block3_out").output]

              # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 
    elif self.Backbone_model == "NASNetMobile":

      Backbone = NASNetMobile(input_shape=(352,352,3), include_top=False, weights=None)
      if os.path.exists('./NASNet-mobile-no-top.h5'):
          pass
      else:
          urlretrieve('https://github.com/titu1994/Keras-NASNet/releases/download/v1.2/NASNet-mobile-no-top.h5',
                      './NASNet-mobile-no-top.h5')
          
      Backbone.load_weights('./NASNet-mobile-no-top.h5')

      Stages=[Backbone.output, Backbone.get_layer("activation_131").output,
              Backbone.get_layer("activation_72").output, Backbone.get_layer("activation_13").output]

              # # # # # # # # # # # # # # # # # # # # # # # # # # # #
          
    elif self.Backbone_model == "NASNetLarge":

      Backbone = NASNetLarge(input_shape=(352,352,3), include_top=False, weights=None)
      if os.path.exists('./NASNet-large-no-top.h5'):
          pass
      else:
          urlretrieve('https://github.com/titu1994/Keras-NASNet/releases/download/v1.2/NASNet-large-no-top.h5',
                      './NASNet-large-no-top.h5')
          
      Backbone.load_weights('./NASNet-large-no-top.h5')

      Stages=[Backbone.output, Backbone.get_layer("activation_319").output,
              Backbone.get_layer("activation_260").output, Backbone.get_layer("activation_201").output]

              # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    else:
      raise ValueError("The name of the Backbone_model must be one of the following options: VGG16, ResNet50, NASNetMobile, NASNetLarge")

              # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    ExtractedFeatures = []
    Number_of_Filters = [192,128,96,64]

    for Stage,Num in zip(Stages,Number_of_Filters):
      MAG_output=MAG_Module(Stage, Num)
      extr=Conv2D(Num, (1, 1), padding='same')(MAG_output) 
      ExtractedFeatures.append(extr)    #Extracted features from the Feature Extraction Network


    ############### Feature Integration Network ##################

    z = BilinearUpsampling(output_size=(ExtractedFeatures[0]._keras_shape[1]*2,
                                        ExtractedFeatures[0]._keras_shape[2]*2))(ExtractedFeatures[0])

    for i in range(len(ExtractedFeatures)-1):
      z = AMI_Module(z, ExtractedFeatures[i+1], ExtractedFeatures[i+1]._keras_shape[-1])
      z = BilinearUpsampling(output_size=(z._keras_shape[1]*2,z._keras_shape[2]*2))(z)

    z = Conv2D(64, (3, 3), padding='same', strides=(1,1))(z)
    z = BatchNormalization(axis=-1)(z)
    z = Activation('relu')(z)
    z = BilinearUpsampling(output_size=(352,352))(z)
    z = Conv2D(2, (1, 1), padding='same')(z)
    z = Activation('softmax')(z)


    self.model = Model(Backbone.input,z)
    self.model.compile(optimizer=SGD(lr=self.learning_rate, momentum=0.9), loss=Sharpenning_Loss(self.Lambda), metrics=["accuracy"])

    if self.show_ModelSummary == True:
      self.model.summary()