def buildDenseNet201Model(img_height, img_width, img_channl, num_classes,
                          num_GPU):
    inputs = Input(shape=(img_height, img_width, img_channl))
    # --------------------------------------------
    StopNum = 0
    AppModel = DenseNet201(include_top=False,
                           pooling='avg',
                           weights='imagenet')
    for idx, layer in enumerate(AppModel.layers):
        layer.trainable = False
        if (idx == StopNum):
            break
    x = AppModel.layers[-1].output
    x = Flatten()(x)
    x = BatchNormalization()(x)
    x = Dropout(0.5)(x)
    output_layer = Dense(num_classes, activation='sigmoid', name='sigmoid')(x)
    model = Model(inputs=AppModel.input, outputs=output_layer)
    # --------------------------------------------
    model.summary()
    if (num_GPU > 1):
        model = multi_gpu_model(model, gpus=num_GPU)
    model.compile(
        loss='binary_crossentropy',
        optimizer=Adam(lr=1e-4),  #0.001
        metrics=['categorical_accuracy', 'binary_accuracy', f1_m])
    return model
Пример #2
0
def loadPretrainedWeights():
    pretrained_weights={}

    pretrained_weights['vgg16']=VGG16(weights='imagenet', include_top=False,pooling='avg')
    pretrained_weights['vgg19']=VGG19(weights='imagenet', include_top=False,pooling='avg')

    pretrained_weights['resnet50']=ResNet50(weights='imagenet', include_top=False,pooling='avg')

    pretrained_weights['inceptionv3']=InceptionV3(weights='imagenet', include_top=False,pooling='avg')
    pretrained_weights['inception-resentv2']=InceptionResNetV2(weights='imagenet', include_top=False,pooling='avg')


    pretrained_weights['xception']=Xception(weights='imagenet', include_top=False,pooling='avg')

    pretrained_weights['densenet121']=DenseNet121(weights='imagenet', include_top=False,pooling='avg')
    pretrained_weights['densenet169']=DenseNet169(weights='imagenet', include_top=False,pooling='avg')
    pretrained_weights['densenet201']=DenseNet201(weights='imagenet', include_top=False,pooling='avg')
    pretrained_weights['mobilenet']=MobileNet(weights='imagenet', include_top=False,pooling='avg')


  #N retrained_weights['nasnetlarge']=NASNetLarge(weights='imagenet', include_top=False,pooling='avg',input_shape = (224, 224, 3))
  #N pretrained_weights['nasnetmobile']=NASNetMobile(weights='imagenet', include_top=False,pooling='avg')



    
  #N  pretrained_weights['mobilenetV2']=MobileNetV2(weights='imagenet', include_top=False,pooling='avg')
    
    return pretrained_weights
Пример #3
0
def densenet201():
    base_model = DenseNet201(include_top=False)
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1, activation='sigmoid')(x)
    model = Model(inputs=base_model.input, outputs=x)
    model.compile(optimizer='adam', loss='binary_crossentropy')
    return model
Пример #4
0
def build_model():
    base_model = DenseNet201(weights='imagenet',
                             include_top=False,
                             input_shape=(224, 224, 3))
    x = base_model.output
    x = GlobalMaxPool2D()(x)
    x = Dropout(0.7)(x)
    predictions = Dense(len(class_names), activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=predictions)
    return model
Пример #5
0
def get_model(architecture, iteracion, models_info, pipeline):

    print("="*len(architecture))
    print(architecture)
    print("="*len(architecture))

    if iteracion > 0:
        base_model = models_info[architecture]['model_memory']

    if architecture == 'InceptionV3':
        from tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input
        if iteracion == 0:
            base_model = InceptionV3(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'InceptionV4':
        from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input
        if iteracion == 0:
            base_model = InceptionResNetV2(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'ResNet50':
        from tensorflow.keras.applications.resnet import ResNet50, preprocess_input
        if iteracion == 0:
            base_model = ResNet50(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'ResNet101':
        from tensorflow.keras.applications.resnet import ResNet101, preprocess_input
        if iteracion == 0:
            base_model = ResNet101(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'ResNet152':
        from tensorflow.keras.applications.resnet import ResNet152, preprocess_input
        if iteracion == 0:
            base_model = ResNet152(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'DenseNet121':
        from tensorflow.keras.applications.densenet import DenseNet121, preprocess_input
        if iteracion == 0:
            base_model = DenseNet121(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'DenseNet169':
        from tensorflow.keras.applications.densenet import DenseNet169, preprocess_input
        if iteracion == 0:
            base_model = DenseNet169(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'DenseNet201': 
        from tensorflow.keras.applications.densenet import DenseNet201, preprocess_input
        if iteracion == 0:
            base_model = DenseNet201(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'NASNetLarge': 
        from tensorflow.keras.applications.nasnet import NASNetLarge, preprocess_input
        if iteracion == 0:
            base_model = NASNetLarge(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3))
    if architecture == 'Xception':
        from tensorflow.keras.applications.xception import Xception, preprocess_input
        if iteracion == 0:
            base_model = Xception(weights=pipeline['weights'], include_top=False, input_shape=(pipeline['img_height'], pipeline['img_width'], 3))

    return base_model, preprocess_input
Пример #6
0
def init_model(model_name):
    if (model_name == "VGG19"):
        return VGG19(include_top=True, weights='imagenet')
    if (model_name == "VGG16"):
        return tf.keras.applications.VGG16(include_top=True,
                                           weights='imagenet')
    if (model_name == "ResNet50"):
        return ResNet50(include_top=True, weights="imagenet")
    if (model_name == "DenseNet201"):
        return DenseNet201(include_top=True, weights="imagenet")
    if (model_name == "DenseNet121"):
        return DenseNet121(include_top=True, weights="imagenet")
    if (model_name == "InceptionResNetV2"):
        return InceptionResNetV2(include_top=True, weights="imagenet")
Пример #7
0
def get_model_densenet(num_class):
    base_model = DenseNet201(weights='imagenet', include_top=False)
    x = base_model.output

    # custom top
    predictions = get_custom_top(x, num_class)

    model = Model(inputs=base_model.input, outputs=predictions)
    for layer in base_model.layers:
        layer.trainable = False
    model.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    return model
def create_cnn_model():
    model = keras.models.Sequential()
    pre_trained_model = DenseNet201(input_shape=(*IMG_SIZE, 3),
                                    weights='imagenet',
                                    include_top=False)

    model.add(pre_trained_model)
    model.add(layers.Flatten())
    model.add(layers.Dense(512, activation='relu'))
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(5, activation='softmax'))

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

    print(model.summary())
    return model
def get_basemodel(model_name):
    if model_name == "MobileNetV2":
        from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
        return MobileNetV2(include_top=True,weights='imagenet',classes=1000)

    elif model_name == "ResNet50":
        from tensorflow.keras.applications.resnet50 import ResNet50
        return ResNet50(include_top=True,weights='imagenet',classes=1000)

    elif model_name=="InceptionV2":
        from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2
        return InceptionResNetV2(include_top=True,weights='imagenet',classes=1000)

    elif model_name=="DenseNet201":
        from tensorflow.keras.applications.densenet import DenseNet201
        return DenseNet201(include_top=True,weights='imagenet',classes=1000)

    elif model_name=="NASNetLarge":
        from tensorflow.keras.applications.nasnet import NASNetLarge
        return NASNetLarge(include_top=True,weights='imagenet',classes=1000)
def densenet():
    base_model = DenseNet201(include_top=False,
                             weights='imagenet',
                             input_tensor=None,
                             input_shape=(224, 224, 3))

    last_layer = base_model.output
    X = Flatten()(last_layer)
    X = Dense(128, activation="relu",
              kernel_regularizer=regularizers.l2(0.02))(X)
    X = Dropout(0.5)(X)
    X = Dense(7,
              activation="softmax",
              kernel_regularizer=regularizers.l2(0.02))(X)

    for layer in base_model.layers:
        layer.trainable = False

    model = Model(base_model.input, X)
    return model
Пример #11
0
    def dense201(self):
        '''
        DenseNet201(初期値Imagenet)
        '''

        base_model = DenseNet201(include_top=False,
                                 weights='imagenet',
                                 input_shape=(self.h, self.w, self.ch))
        x = GlobalAveragePooling2D()(base_model.output)
        x = Dense(512, kernel_initializer='he_normal')(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        if self.classes == 1:
            outputs = Dense(self.classes,
                            kernel_initializer='he_normal',
                            activation='relu')(x)
        else:
            outputs = Dense(self.classes,
                            kernel_initializer='he_normal',
                            activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=outputs)

        return model
Пример #12
0
model = models.Sequential()

if '121' in args.model:
    base_model = DenseNet121(weights=None,
                             include_top=False,
                             input_shape=(32, 32, 3),
                             pooling='avg')
elif '169' in args.model:
    base_model = DenseNet169(weights=None,
                             include_top=False,
                             input_shape=(32, 32, 3),
                             pooling='avg')
elif '201' in args.model:
    base_model = DenseNet201(weights=None,
                             include_top=False,
                             input_shape=(32, 32, 3),
                             pooling='avg')

#base_model.summary()

#pdb.set_trace()

#model.add(layers.UpSampling2D((2,2)))
#model.add(layers.UpSampling2D((2,2)))
#model.add(layers.UpSampling2D((2,2)))
model.add(base_model)
#model.add(layers.Flatten())
#model.add(layers.BatchNormalization())
#model.add(layers.Dense(128, activation='relu'))
#model.add(layers.Dropout(0.5))
#model.add(layers.BatchNormalization())
Пример #13
0
    添加最后的层
    输入
    base_model和分类数量
    输出
    新的keras的model
    """
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    predictions = Dense(nb_classes,
                        activation='softmax')(x)  # new softmax layer
    model = Model(inputs=base_model.input, outputs=predictions)
    return model


# 搭建模型
model = DenseNet201(include_top=False)
model = add_new_last_layer(model, nb_classes)
# model.load_weights('../model/checkpoint-02e-val_acc_0.82.hdf5')  第二次训练可以接着第一次训练得到的模型接着训练
model.compile(optimizer=SGD(lr=0.001,
                            momentum=0.9,
                            decay=0.0001,
                            nesterov=True),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# 更好地保存模型 Save the model after every epoch.
output_model_file = 'C:\\Users\\StarDust\\PycharmProjects\\untitled2\\memory\\checkpoint-{epoch:02d}e-accuracy_{accuracy:.2f}.hdf5'
checkpoint = ModelCheckpoint(output_model_file,
                             monitor='val-acc',
                             verbose=1,
                             save_best_only=True)
Пример #14
0
 def download_for_url(self, path: str, **kwargs):
     """
     Download the file at the given URL
     :param path:  the path to download
     :param kwargs:  various kwargs for customizing the underlying behavior of
     the model download and setup
     :return: the absolute path to the model
     """
     path_split = path.split('/')
     type = path_split[0]
     weights_file = path_split[1]
     include_top = 'no_top' in weights_file
     if type == 'vgg19':
         ret = VGG19(include_top=include_top, **kwargs)
     elif type == 'vgg16':
         ret = VGG16(include_top=include_top, **kwargs)
     elif type == 'resnet50':
         ret = ResNet50(include_top=include_top, **kwargs)
     elif type == 'resnet101':
         ret = ResNet101(include_top=include_top, **kwargs)
     elif type == 'resnet152':
         ret = ResNet152(include_top=include_top, **kwargs)
     elif type == 'resnet50v2':
         ret = ResNet50V2(include_top=include_top, **kwargs)
     elif type == 'resnet101v2':
         ret = ResNet101V2(include_top=include_top, **kwargs)
     elif type == 'resnet152v2':
         ret = ResNet152V2(include_top=include_top, **kwargs)
     elif type == 'densenet121':
         ret = DenseNet121(include_top=include_top)
     elif type == 'densenet169':
         ret = DenseNet169(include_top=include_top, **kwargs)
     elif type == 'densenet201':
         ret = DenseNet201(include_top=include_top, **kwargs)
     elif type == 'inceptionresnetv2':
         ret = InceptionResNetV2(include_top=include_top, **kwargs)
     elif type == 'efficientnetb0':
         ret = EfficientNetB0(include_top=include_top, **kwargs)
     elif type == 'efficientnetb1':
         ret = EfficientNetB1(include_top=include_top, **kwargs)
     elif type == 'efficientnetb2':
         ret = EfficientNetB2(include_top=include_top, **kwargs)
     elif type == 'efficientnetb3':
         ret = EfficientNetB3(include_top=include_top, **kwargs)
     elif type == 'efficientnetb4':
         ret = EfficientNetB4(include_top=include_top, **kwargs)
     elif type == 'efficientnetb5':
         ret = EfficientNetB5(include_top=include_top, **kwargs)
     elif type == 'efficientnetb6':
         ret = EfficientNetB6(include_top=include_top, **kwargs)
     elif type == 'efficientnetb7':
         efficient_net = EfficientNetB7(include_top=include_top, **kwargs)
     elif type == 'mobilenet':
         ret = MobileNet(include_top=include_top, **kwargs)
     elif type == 'mobilenetv2':
         ret = MobileNetV2(include_top=include_top)
     #  MobileNetV3() missing 2 required positional arguments: 'stack_fn' and 'last_point_ch'
     #elif type == 'mobilenetv3':
     #    mobile_net = MobileNetV3(include_top=include_top, **kwargs)
     elif type == 'inceptionv3':
         ret = InceptionV3(include_top=include_top, **kwargs)
     elif type == 'nasnet':
         ret = NASNetLarge(include_top=include_top, **kwargs)
     elif type == 'nasnet_mobile':
         ret = NASNetMobile(include_top=include_top, **kwargs)
     elif type == 'xception':
         ret = Xception(include_top=include_top, **kwargs)
     model_path = os.path.join(keras_path, weights_file)
     ret.save(model_path)
     return model_path
Пример #15
0
    def get_base_model(self, name='vgg16'):
        print('using model : ', name)
        if name == 'mobilenet_v2':
            from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
            base_model = MobileNetV2(input_shape=(self.IMAGE_SIZE,
                                                  self.IMAGE_SIZE, 3),
                                     include_top=False,
                                     weights='imagenet')
        elif name == 'mobilenet':
            from tensorflow.keras.applications.mobilenet import MobileNet
            base_model = MobileNet(input_shape=(self.IMAGE_SIZE,
                                                self.IMAGE_SIZE, 3),
                                   include_top=False,
                                   weights='imagenet')
        elif name == 'densenet121':
            from tensorflow.keras.applications.densenet import DenseNet121
            base_model = DenseNet121(input_shape=(self.IMAGE_SIZE,
                                                  self.IMAGE_SIZE, 3),
                                     include_top=False,
                                     weights='imagenet')
        elif name == 'densenet169':
            from tensorflow.keras.applications.densenet import DenseNet169
            base_model = DenseNet169(input_shape=(self.IMAGE_SIZE,
                                                  self.IMAGE_SIZE, 3),
                                     include_top=False,
                                     weights='imagenet')
        elif name == 'densenet201':
            from tensorflow.keras.applications.densenet import DenseNet201
            base_model = DenseNet201(input_shape=(self.IMAGE_SIZE,
                                                  self.IMAGE_SIZE, 3),
                                     include_top=False,
                                     weights='imagenet')
        elif name == 'inception_resnet_v2':
            from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2
            base_model = InceptionResNetV2(input_shape=(self.IMAGE_SIZE,
                                                        self.IMAGE_SIZE, 3),
                                           include_top=False,
                                           weights='imagenet')
        elif name == 'inception_v3':
            from tensorflow.keras.applications.inception_v3 import InceptionV3
            base_model = InceptionV3(input_shape=(self.IMAGE_SIZE,
                                                  self.IMAGE_SIZE, 3),
                                     include_top=False,
                                     weights='imagenet')
        elif name == 'nasnet_large':
            from tensorflow.keras.applications.nasnet import NASNetLarge
            base_model = NASNetLarge(input_shape=(self.IMAGE_SIZE,
                                                  self.IMAGE_SIZE, 3),
                                     include_top=False,
                                     weights='imagenet')
        elif name == 'nasnet_mobile':
            from tensorflow.keras.applications.nasnet import NASNetMobile
            base_model = NASNetMobile(input_shape=(self.IMAGE_SIZE,
                                                   self.IMAGE_SIZE, 3),
                                      include_top=False,
                                      weights='imagenet')
        elif name == 'resnet50':
            from tensorflow.keras.applications.resnet50 import ResNet50
            base_model = ResNet50(input_shape=(self.IMAGE_SIZE,
                                               self.IMAGE_SIZE, 3),
                                  include_top=False,
                                  weights='imagenet')
        elif name == 'xception':
            from tensorflow.keras.applications.xception import Xception
            base_model = Xception(input_shape=(self.IMAGE_SIZE,
                                               self.IMAGE_SIZE, 3),
                                  include_top=False,
                                  weights='imagenet')
        elif name == 'vgg19':
            from tensorflow.keras.applications.vgg19 import VGG19
            base_model = VGG19(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE,
                                            3),
                               include_top=False,
                               weights='imagenet')
        else:
            from tensorflow.keras.applications.vgg16 import VGG16
            # 采用VGG16为基本模型,include_top为False,表示FC层是可自定义的,抛弃模型中的FC层;该模型会在~/.keras/models下载基本模型
            base_model = VGG16(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE,
                                            3),
                               include_top=False,
                               weights='imagenet')

        # self.preprocess_input = preprocess_input
        return base_model
Пример #16
0
def get_model(architecture, iteracion, models_info, pipeline):

    print("=" * len(architecture))
    print(architecture)
    print("=" * len(architecture))

    if iteracion > 0 and not pipeline["restart_weights"]:
        print("USING MODELS FROM MEMORY")
        base_model = models_info[architecture]['model_memory']
        print("OK - USING MODELS FROM MEMORY")

    if architecture == 'InceptionV3':
        from tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input

        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = InceptionV3(weights=pipeline['weights'],
                                     include_top=False,
                                     input_shape=(pipeline['img_height'],
                                                  pipeline['img_width'], 3))
            print(f"OK - RESTARTING WEIGHTS FROM IMAGENET FOR {architecture}")

    if architecture == 'InceptionV4':
        from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input

        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = InceptionResNetV2(weights=pipeline['weights'],
                                           include_top=False,
                                           input_shape=(pipeline['img_height'],
                                                        pipeline['img_width'],
                                                        3))
            print(f"OK - RESTARTING WEIGHTS FROM IMAGENET FOR {architecture}")

    if architecture == 'ResNet50':
        from tensorflow.keras.applications.resnet import ResNet50, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = ResNet50(weights=pipeline['weights'],
                                  include_top=False,
                                  input_shape=(pipeline['img_height'],
                                               pipeline['img_width'], 3))
    if architecture == 'ResNet101':
        from tensorflow.keras.applications.resnet import ResNet101, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = ResNet101(weights=pipeline['weights'],
                                   include_top=False,
                                   input_shape=(pipeline['img_height'],
                                                pipeline['img_width'], 3))

    if architecture == 'ResNet152':
        from tensorflow.keras.applications.resnet import ResNet152, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = ResNet152(weights=pipeline['weights'],
                                   include_top=False,
                                   input_shape=(pipeline['img_height'],
                                                pipeline['img_width'], 3))
            print(f"OK - RESTARTING WEIGHTS FROM IMAGENET FOR {architecture}")

    if architecture == 'DenseNet121':
        from tensorflow.keras.applications.densenet import DenseNet121, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = DenseNet121(weights=pipeline['weights'],
                                     include_top=False,
                                     input_shape=(pipeline['img_height'],
                                                  pipeline['img_width'], 3))
    if architecture == 'DenseNet169':
        from tensorflow.keras.applications.densenet import DenseNet169, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = DenseNet169(weights=pipeline['weights'],
                                     include_top=False,
                                     input_shape=(pipeline['img_height'],
                                                  pipeline['img_width'], 3))
    if architecture == 'DenseNet201':
        from tensorflow.keras.applications.densenet import DenseNet201, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = DenseNet201(weights=pipeline['weights'],
                                     include_top=False,
                                     input_shape=(pipeline['img_height'],
                                                  pipeline['img_width'], 3))
    if architecture == 'NASNetLarge':
        from tensorflow.keras.applications.nasnet import NASNetLarge, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = NASNetLarge(weights=pipeline['weights'],
                                     include_top=False,
                                     input_shape=(pipeline['img_height'],
                                                  pipeline['img_width'], 3))
    if architecture == 'Xception':
        from tensorflow.keras.applications.xception import Xception, preprocess_input
        if iteracion == 0 or pipeline["restart_weights"]:
            base_model = Xception(weights=pipeline['weights'],
                                  include_top=False,
                                  input_shape=(pipeline['img_height'],
                                               pipeline['img_width'], 3))

    return base_model, preprocess_input
Пример #17
0
    def __init__(self, train_file_name, validation_file_name, test_file_name, embedding_file_name, max_train,
                 image_encoding_algo, use_keyword=False, build_vocab_dev=True):
        
        bucket_name = 'vqg-data'
        self.image_encoding_algo = image_encoding_algo
        self.build_vocab_dev = build_vocab_dev

        if not os.path.exists(train_file_name):
            logger.info("Download training data to %s" % train_file_name)
            exit(0)

        if not os.path.exists(validation_file_name):
            logger.info("Download validation data to %s" % validation_file_name)
            exit(0)

        if not os.path.exists(test_file_name):
            logger.info("Download testing data to %s" % test_file_name)
            exit(0)

        if 'glove' in embedding_file_name and not os.path.exists(embedding_file_name):
            logger.info("Download embedding file to %s" % embedding_file_name)
            exit(0)

        self.train_file = train_file_name
        self.validation_file = validation_file_name
        self.test_file = test_file_name
        self.embedding_file = embedding_file_name

        self.max_samples = max_train
        if self.image_encoding_algo == 'VGG19':
            self.image_encoding_model = VGG19(weights='imagenet', include_top=False)
        elif self.image_encoding_algo == 'MobileNet':
            self.image_encoding_model = MobileNetV2(weights='imagenet', include_top=False)
        elif image_encoding_algo == 'ResNet':
            self.image_encoding_model = ResNet50(weights='imagenet', include_top=False)
        # elif self.image_encoding_algo=='Inception':
        #     self.image_encoding_model = InceptionV3(weights='imagenet', include_top=False)
        elif self.image_encoding_algo == 'DenseNet':
            self.image_encoding_model = DenseNet201(weights='imagenet', include_top=False)

        logger.info('%s model loaded' % image_encoding_algo)

        self.vocabulary = dict()
        self.idx_to_word = dict()
        self.word_to_idx = dict()
        self.train_image_id_questions_dict = dict()
        self.train_image_id_imagefeat_dict = dict()
        self.train_image_id_keyword_dict = dict()

        self.dev_image_id_questions_dict = dict()
        self.dev_image_id_imagefeat_dict = dict()
        self.dev_image_id_keyword_dict = dict()

        self.test_image_id_questions_dict = dict()
        self.test_image_id_imagefeat_dict = dict()
        self.test_image_id_url_dict = dict()
        self.test_image_id_keyword_dict = dict()

        self.unique_train_questions = set()
        self.unique_generated_questions = set()
        self.generated_questions = []

        self.max_question_len = -1
        self.max_keyword_len = 10
        self.no_of_samples = 0
        if use_keyword == 'YES':
            self.use_keyword = True
        else:
            self.use_keyword = False

        # self.load_data(self.train_file)

        self.build_vocabulary()
Пример #18
0
if not args.load_model and not args.mode == 'finetune':
  # create the base pre-trained model
  if args.pretrained_model: 
    base_model = keras.models.load_model(args.pretrained_model)
  elif args.net == 'inception_v3':
    base_model = InceptionV3(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
  elif args.net == 'xception':
    base_model = Xception(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
  elif args.net == 'resnet_50':
    base_model = ResNet50(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
  elif args.net == 'densenet_121':
    base_model = DenseNet121(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
  elif args.net == 'densenet_169':
    base_model = DenseNet169(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
  elif args.net == 'densenet_201':
    base_model = DenseNet201(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
  elif args.net == 'mobilenet_v2':
    base_model = MobileNetV2(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
  elif args.net == 'nasnetlarge':
    base_model = NASNetLarge(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
  elif args.net == 'nasnetmobile':
    base_model = NASNetMobile(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
  elif args.net == 'inceptionresnet_v2':
    base_model = InceptionResNetV2(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
  else:
    print('Not supported network type')
    sys.exit()

  x = base_model.output
  x = GlobalAveragePooling2D()(x)
  x = Dropout(rate=args.dropout0)(x)
x_train = np.concatenate((x_train, x_train2), axis=3)
x_test = np.concatenate((x_test, x_test2), axis=3)

# Reshape the inputs for feeding into densenet201
x_train = x_train.reshape(x_train.shape[0], 64, 64, 1)
x_test = x_test.reshape(x_test.shape[0], 64, 64, 1)
print(x_train.shape)
print(x_test.shape)

######################################## Classification #########################################

# Classification via Densenet201 pretraioned model
model = Sequential()
model.add(Conv2D(3, (3, 3), activation='relu', input_shape=(64, 64, 1)))
model.add(MaxPooling2D((2, 2)))
dense = DenseNet201(weights='imagenet', include_top=False)
for layer in dense.layers:
    layer.Trainable = False
model.add(dense)
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.summary()
# Compiling the densenet model
model.compile(loss='categorical_crossentropy',
              optimizer=SGD(lr=1e-3, momentum=0.9),
              metrics=['acc'])

######################################### Run 3 times ###########################################
Пример #20
0
fc1_dim = 512
# endregion

# region 定义输入变量
x_inputs = tf.placeholder(shape=(None, 224, 224, 3),
                          dtype=tf.float32,
                          name='x_inputs')
y_inputs = tf.placeholder(shape=(None, n_classes),
                          dtype=tf.float32,
                          name='y_inputs')
# lr          = tf.placeholder(dtype=tf.float32, name='lr')
inputs = preprocess_input(x_inputs, )
# endregion

# region 定义网络结构
densenet201 = DenseNet201(include_top=False, weights=None, pooling='avg')
features = densenet201(inputs)
fc1 = tf.layers.Dense(fc1_dim, activation='relu')(features)
fc2 = tf.layers.Dense(n_classes, )(fc1)
logits = tf.nn.softmax(fc2)
pred_y = tf.argmax(logits, axis=-1, name='pred_y')

loss = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=y_inputs, logits=fc2))

# keras 的BatchNormalization当在tensorflow的session中使用时,必须将其手动加入到更新操作集合中
ops = tf.get_default_graph().get_operations()
update_ops = [
    op for op in ops
    if ("AssignMovingAvg" in op.name and op.type == "AssignSubVariableOp")
]
x_valid, y_valid = load_dataset_et_process(args['validation'])

# Converts it into binary format
y_train = lb.fit_transform(y_train)
y_valid = lb.fit_transform(y_valid)

print('x_train shape : ', x_train.shape)
print('y_train shape : ', y_train.shape)
print('x_validation shape : ', x_valid.shape)
print('y_validation shape : ', y_valid.shape)

# Starting to construct the model
print('Starting to construct model')
# Loading the DenseNet model pre_trained on the imageNet, simultaneously cut off the original FC_Layer
base_model = DenseNet201(weights='imagenet',
                         include_top=False,
                         input_tensor=Input(shape=(224, 224, 3)))

# It is compulsory for the base_model to freeze each layer,
# which result will never train again in the network.
for layer in base_model.layers:
    layer.trainable = False

# To construct the our own ful_connection layer will be placed on top of the base model
head_model = base_model.output
head_model = MaxPooling2D(pool_size=(7, 7))(head_model)
head_model = Flatten(name='flatten')(head_model)
head_model = Dense(64, activation='relu')(head_model)
head_model = Dense(128, activation='relu')(head_model)
head_model = Dropout(0.5)(head_model)
head_model = Dense(3, activation='softmax')(head_model)
from tensorflow.keras.applications.densenet import DenseNet201
from tensorflow.keras.layers import Dense, Dropout, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam

#7, 4 e 2 for the experiments
CLASSES = 4

base_model = DenseNet201(weights='imagenet',
                         include_top=False,
                         input_shape=(200, 200, 3))

x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.5)(x)
predictions = Dense(CLASSES, activation='softmax')(x)

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

#training all the layers
for layer in base_model.layers:
    layer.trainable = True

#model compiling
model.compile(optimizer=optimizers.Adam(lr=0.0001),
              loss='categorical_crossentropy',
              metrics=['acc'])
Пример #23
0
    def _get_ImageNet_trained_model(self, model):
        """Initialize an ImageNet pre-trained model

        After the chosen convolution neural network (see `model` for options)
        is loaded with the ImageNet pre-trained weights from
        `tensorflow.keras.applications`, the last fully connected layer(s) is
        removed to turn the classification model into a feature extractor.

        Parameters
        ----------
        model : str
            Name of the pre-trained deep learning model to be used for feature
            extraction. Can be one of "ResNet50", "DenseNet121", "DenseNet201",
            "VGG16", "VGG19", "MobileNet", "MobileNetV2", "Xception",
            "InceptionV3".

        Returns
        -------
        feature_extractor
            A Keras model object.
        image_size : tuple
            Image size as (height, width).
        preprocess_input
            A preprocessing function - the `preprocess_input` function from
            `tensorflow.keras.applications` that corresponds to the chosen `model`.
        """
        if model == "ResNet50":
            from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input

            image_size = (224, 224)
            res = ResNet50(input_shape=image_size + (3, ),
                           weights="imagenet",
                           include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=res.input, outputs=res.get_layer("avg_pool").output)
        elif model == "DenseNet121":
            from tensorflow.keras.applications.densenet import DenseNet121, preprocess_input

            image_size = (224, 224)
            dense121 = DenseNet121(input_shape=image_size + (3, ),
                                   weights="imagenet",
                                   include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=dense121.input,
                outputs=dense121.get_layer("avg_pool").output)
        elif model == "DenseNet201":
            from tensorflow.keras.applications.densenet import DenseNet201, preprocess_input

            image_size = (224, 224)
            dense201 = DenseNet201(input_shape=image_size + (3, ),
                                   weights="imagenet",
                                   include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=dense201.input,
                outputs=dense201.get_layer("avg_pool").output)
        elif model == "VGG16":
            from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input

            image_size = (224, 224)
            vgg16 = VGG16(input_shape=image_size + (3, ),
                          weights="imagenet",
                          include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=vgg16.input, outputs=vgg16.get_layer("fc2").output)
        elif model == "VGG19":
            from tensorflow.keras.applications.vgg19 import VGG19, preprocess_input

            image_size = (224, 224)
            vgg19 = VGG19(input_shape=image_size + (3, ),
                          weights="imagenet",
                          include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=vgg19.input, outputs=vgg19.get_layer("fc2").output)
        elif model == "MobileNet":
            from tensorflow.keras.applications.mobilenet import MobileNet, preprocess_input

            image_size = (224, 224)
            mobile = MobileNet(input_shape=image_size + (3, ),
                               weights="imagenet",
                               include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=mobile.input, outputs=mobile.get_layer(index=-6).output)
            # the output layer in the above is 'global_average_pooling2d', but we use the equivalent 'index' due to a bug in tensorflow
        elif model == "MobileNetV2":
            from tensorflow.keras.applications.mobilenet_v2 import (
                MobileNetV2,
                preprocess_input,
            )

            image_size = (224, 224)
            mobilev2 = MobileNetV2(input_shape=image_size + (3, ),
                                   weights="imagenet",
                                   include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=mobilev2.input,
                outputs=mobilev2.get_layer(index=-2).output)
            # output layer in the above is 'global_average_pooling2d', but we use the equivalent 'index' due to a bug in tensorflow
        elif model == "Xception":
            from tensorflow.keras.applications.xception import Xception, preprocess_input

            image_size = (299, 299)
            xception = Xception(input_shape=image_size + (3, ),
                                weights="imagenet",
                                include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=xception.input,
                outputs=xception.get_layer("avg_pool").output)
        elif model == "InceptionV3":
            from tensorflow.keras.applications.inception_v3 import (
                InceptionV3,
                preprocess_input,
            )

            image_size = (299, 299)
            inception = InceptionV3(input_shape=image_size + (3, ),
                                    weights="imagenet",
                                    include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=inception.input,
                outputs=inception.get_layer("avg_pool").output)
        else:
            raise NotImplementedError("please specify a model!")

        return feature_extractor, image_size, preprocess_input
Пример #24
0
    def model_Initializer(self):

        from tensorflow.keras.layers import Dense, Flatten
        from tensorflow.keras.models import Model
        import tensorflow as tf

        #Resources
        print("Num GPUs Available: ",
              len(tf.config.experimental.list_physical_devices('GPU')))
        print("Using Tensorflow : ", tf.__version__)

        # initializing the network model and excluding the last layer of network
        if self.MODEL == 'VGG16':
            from tensorflow.keras.applications.vgg16 import VGG16
            self.model = VGG16(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        if self.MODEL == 'VGG19':
            from tensorflow.keras.applications.vgg19 import VGG19
            self.model = VGG19(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        if self.MODEL == 'Xception':
            from tensorflow.keras.applications.xception import Xception
            self.model = Xception(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        if self.MODEL == 'ResNet50V2':
            from tensorflow.keras.applications.resnet_v2 import ResNet50V2
            self.model = ResNet50V2(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        if self.MODEL == 'ResNet101V2':
            from tensorflow.keras.applications.resnet_v2 import ResNet101V2
            self.model = ResNet101V2(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        if self.MODEL == 'ResNet152V2':
            from tensorflow.keras.applications.resnet_v2 import ResNet152V2
            self.model = ResNet152V2(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        if self.MODEL == 'InceptionV3':
            from tensorflow.keras.applications.inception_v3 import InceptionV3
            self.model = InceptionV3(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        if self.MODEL == 'InceptionResNetV2':
            from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2
            self.model = InceptionResNetV2(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        if self.MODEL == 'MobileNetV2':
            from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
            self.model = MobileNetV2(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        if self.MODEL == 'DenseNet121':
            from tensorflow.keras.applications.densenet import DenseNet121
            self.model = DenseNet121(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        if self.MODEL == 'DenseNet169':
            from tensorflow.keras.applications.densenet import DenseNet169
            self.model = DenseNet169(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        if self.MODEL == 'DenseNet201':
            from tensorflow.keras.applications.densenet import DenseNet201
            self.model = DenseNet201(
                input_shape=self.IMAGE_SIZE + [3],
                weights='imagenet',  #using pretrained imagenet weights
                include_top=False)  #excluding the last layer of network

        # Freezing the layes of the network
        for layer in self.model.layers:
            layer.trainable = False

        #flatterning the last layer
        self.x = Flatten()(self.model.output)

        #Created a dense layer for output
        self.outlayers = Dense(self.count_output_classes,
                               activation='softmax')(self.x)

        #Binding pretrained layers with custom output layer
        self.model = Model(inputs=self.model.input, outputs=self.outlayers)

        #Compile the Model
        self.model.compile(loss='categorical_crossentropy',
                           optimizer='adam',
                           metrics=['accuracy'])
Пример #25
0
def get_densenet(classes=9,
                 depth=121,
                 input_shape=(224, 224, 3),
                 base_layer_trainable=False):
    from tensorflow.keras.applications.densenet import DenseNet121, DenseNet169, DenseNet201
    assert depth in [121, 169, 201]
    if depth == 121:
        base_model = DenseNet121(include_top=False, input_shape=input_shape)
        for layer in base_model.layers:
            layer.trainable = base_layer_trainable
        head_model = KL.GlobalMaxPool2D()(base_model.output)
        head_model = KL.Dense(1024,
                              activation='relu',
                              name='0000',
                              kernel_initializer='he_uniform')(head_model)
        head_model = KL.Dropout(0.5)(head_model)
        head_model = KL.Dense(1024,
                              activation='relu',
                              name='1111',
                              kernel_initializer='he_uniform')(head_model)
        head_model = KL.Dropout(0.5)(head_model)
        head_model = KL.Dense(classes, activation='softmax',
                              name='3333')(head_model)
        model = KM.Model(inputs=base_model.input, outputs=head_model)
        return model

    elif depth == 169:
        base_model = DenseNet169(include_top=False, input_shape=input_shape)
        for layer in base_model.layers:
            layer.trainable = base_layer_trainable
        head_model = KL.GlobalMaxPool2D()(base_model.output)
        head_model = KL.Dense(1024,
                              activation='relu',
                              name='00',
                              kernel_initializer='he_uniform')(head_model)
        head_model = KL.Dropout(0.5)(head_model)
        head_model = KL.Dense(1024,
                              activation='relu',
                              name='01',
                              kernel_initializer='he_uniform')(head_model)
        head_model = KL.Dropout(0.5)(head_model)
        head_model = KL.Dense(classes, activation='softmax',
                              name='11')(head_model)
        model = KM.Model(inputs=base_model.input, outputs=head_model)
        return model

    else:
        base_model = DenseNet201(include_top=False, input_shape=input_shape)
        for layer in base_model.layers:
            layer.trainable = base_layer_trainable
        head_model = KL.GlobalMaxPool2D()(base_model.output)
        head_model = KL.Dense(1024,
                              activation='relu',
                              name='0000',
                              kernel_initializer='he_uniform')(head_model)
        head_model = KL.Dropout(0.5)(head_model)
        head_model = KL.Dense(1024,
                              activation='relu',
                              name='1111',
                              kernel_initializer='he_uniform')(head_model)
        head_model = KL.Dropout(0.5)(head_model)
        head_model = KL.Dense(classes, activation='softmax',
                              name='3333')(head_model)
        model = KM.Model(inputs=base_model.input, outputs=head_model)
        return model