def Multimodel(cnn_weights_path=None, all_weights_path=None, class_num=61):
    input_layer = Input(shape=(224, 224, 3))

    xception = Xception(include_top=False,
                        weights=None,
                        input_tensor=input_layer,
                        input_shape=(224, 224, 3))
    if cnn_weights_path != None:
        xception.load_weights(cnn_weights_path)

#对dense_121和xception进行全局最大池化
    top1_model = GlobalMaxPooling2D(input_shape=(7, 7, 1024),
                                    data_format='channels_last')(
                                        xception.output)

    #第一个全连接层
    top_model = Dense(units=512, activation="relu")(top1_model)
    top_model = Dropout(rate=0.5)(top_model)
    top_model = Dense(units=class_num, activation="softmax")(top_model)

    model = Model(inputs=input_layer, outputs=top_model)

    #加载全部的参数
    if all_weights_path:
        model.load_weights(all_weights_path)
    return model
Exemplo n.º 2
0
def make_submission_xception(name, name_ext, dropout_p):
    data_info = load_organized_data_info(imgs_dim=HEIGHT, name=name)
    _, _, _, _, _, te_names = create_embeddings(name)
    batch_size = 32

    datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
    datagen = datagen.flow_from_directory(directory=data_info['dir_te'],
                                          target_size=(HEIGHT, WIDTH),
                                          class_mode=None,
                                          batch_size=batch_size,
                                          shuffle=False)

    model_file = join(MODELS_DIR, MODEL_FILE.format(name, name_ext))
    model = Xception(weights='imagenet', include_top=False, pooling='avg')
    top_classifier = _top_classifier(l2_reg=0,
                                     dropout_p=dropout_p,
                                     input_shape=(2048, ))
    model = Model(inputs=model.input, outputs=top_classifier(model.output))
    model.load_weights(model_file)

    probs_pred = model.predict_generator(generator=datagen,
                                         steps=ceil(data_info['num_te'] /
                                                    batch_size))

    submission_file = 'xception_fine_tuned_{:s}.csv'.format(name)
    create_submission_file(image_names=te_names,
                           probs=probs_pred,
                           file_name=join(SUBMISSIONS_DIR, submission_file))
Exemplo n.º 3
0
    def create_model(self, weights_filename=None):
        model = Xception(weights=None, input_shape=[self.height, self.width, self.depth],
                         include_top=True, classes=self.n_classes)

        if weights_filename is not None:
            model.load_weights(weights_filename)

        model.compile(optimizer='adam',
                      loss='categorical_crossentropy',
                      metrics=self.metrics)

        return model
Exemplo n.º 4
0
def xception(weights=None, include_top=False, input_shape=(224, 224, 3)):
    xcpt = Xception(weights=weights, include_top=include_top, input_shape=input_shape)
    if include_top:
        WRN_WEIGHTS_PATH = 'xception_weights_tf_dim_ordering_tf_kernels.h5'
    else:
        WRN_WEIGHTS_PATH = 'xception_weights_tf_dim_ordering_tf_kernels_notop.h5'
    model_dir = MODEL_PATH
    filename = WRN_WEIGHTS_PATH.split('|')[-1]
    fpath = get_file(filename, WRN_WEIGHTS_PATH, cache_subdir=MODEL_PATH)
    xcpt.load_weights(fpath)
    print('Xception weights loaded!')
    return xcpt
Exemplo n.º 5
0
def uResNet34Xceptionregr(input_size=None,
                          unet_weights=None,
                          weights=None,
                          n_classes=4,
                          freeze_unet=False):
    assert any([unet_weights is None,
                weights is None])  # do not load both weights

    # U-Net
    if K.image_data_format() == "channels_last":
        input_shape = input_size + (3, )
    else:
        input_shape = (3, ) + input_size
    model_unet = uResNet34(input_size=input_size,
                           encoder_weights=None,
                           weights=unet_weights,
                           n_classes=n_classes)
    if freeze_unet:
        print("Freeze U-Net")
        for layer in model_unet.layers:
            layer.trainable = False
    img_input = Input(shape=input_shape)
    model_unet(img_input)

    # Regression
    if K.image_data_format() == "channels_last":
        input_shape = input_size + (n_classes, )
    else:
        input_shape = (n_classes, ) + input_size
    model = Xception(input_shape=input_shape,
                     include_top=False,
                     weights=None,
                     pooling="avg")
    x = model(model_unet.get_output_at(0))
    x = Dense(1, activation="linear", name="predictions")(x)

    # Create model.
    model = Model(model_unet.get_input_at(0), x, name="uResNet34Xceptionregr")

    if weights is not None:
        print("Load weights from", weights)
        model.load_weights(weights)

    optimizer = Adam(decay=1e-4)
    # optimizer = SGD(momentum=0.95, decay=0.0005, nesterov=True)

    model.compile(loss="mse", optimizer=optimizer, metrics=["mse"])

    return model
Exemplo n.º 6
0
def model_fn(FLAGS, objective, optimizer, metrics):
    inputs_dim = Input(shape=(FLAGS.input_size, FLAGS.input_size, 3))
    Xception_notop = Xception(include_top=False,
                weights=None,
                input_tensor=None,
                input_shape=(FLAGS.input_size, FLAGS.input_size, 3),
                pooling=max)

    Xception_notop.load_weights('/home/work/user-job-dir/src/xception_weights_tf_dim_ordering_tf_kernels_notop.h5')
    output = Xception_notop.output
    output = GlobalAveragePooling2D()(output)
    output = Dense(FLAGS.num_classes, activation='softmax')(output)
    Xception_model = Model(inputs=Xception_notop.input, outputs=output)
    # Xception_model = multi_gpu_model(Xception_model, 4)
    Xception_model.compile(loss=objective, optimizer=optimizer, metrics=metrics)
    return Xception_model
def Multimodel(cnn_weights_path=None,all_weights_path=None,class_num=61,cnn_no_vary=False):
	'''
	获取densent121,xinception并联的网络
	此处的cnn_weights_path是个列表是densenet和xception的卷积部分的权值
	'''
	input_layer=Input(shape=(229,229,3))
	
	dense=DenseNet121(include_top=False,weights=None,input_tensor=input_layer,
		input_shape=(229,229,3))
	xception=Xception(include_top=False,weights=None,input_tensor=input_layer,
		input_shape=(250,250,3))
	#res=ResNet50(include_top=False,weights=None,input_shape=(229,229,3))
 
	if cnn_no_vary:
		for i,layer in  enumerate(dense.layers):
			dense.layers[i].trainable=True
		for i,layer in enumerate(xception.layers):
			xception.layers[i].trainable=False
		#for i,layer in enumerate(res.layers):
		#	res.layers[i].trainable=False
	if cnn_weights_path!=None:
		dense.load_weights(cnn_weights_path[0])
		xception.load_weights(cnn_weights_path[1])
 
	#print(dense.shape,xception.shape)
	#对dense_121和xception进行全局最大池化
	top1_model=GlobalMaxPooling2D(input_shape=(7,7,1024),data_format='channels_last')(dense.output)
	top2_model=GlobalMaxPooling2D(input_shape=(7,7,1024),data_format='channels_last')(xception.output)
	#top3_model=GlobalMaxPool2D(input_shape=res.output_shape)(res.outputs[0])
	
	#print(top1_model.shape,top2_model.shape)
	#把top1_model和top2_model连接起来
	t=keras.layers.Concatenate(axis=1)([top1_model,top2_model])
	#第一个全连接层
	top_model=Dense(units=1024,activation="relu")(t)
	top_model=Dropout(rate=0.5)(top_model)
	top_model=Dense(units=class_num,activation="softmax")(top_model)
	
	model=Model(inputs=input_layer,outputs=top_model)
 
	#加载全部的参数
	if all_weights_path:
		model.load_weights(all_weights_path)
	return model
Exemplo n.º 8
0
def model_fn(FLAGS, objective, optimizer, metrics):
    inputs_dim = Input(shape=(FLAGS.input_size, FLAGS.input_size, 3))
    Xception_notop = Xception(include_top=False,
                              weights=None,
                              input_tensor=None,
                              input_shape=(FLAGS.input_size, FLAGS.input_size,
                                           3),
                              pooling=max)

    Xception_notop.load_weights(
        '/home/work/user-job-dir/src/xception_weights_tf_dim_ordering_tf_kernels_notop.h5'
    )
    output = Xception_notop.output
    output = GlobalAveragePooling2D()(output)
    output = Dense(FLAGS.num_classes, activation='softmax')(output)
    Xception_model = Model(inputs=Xception_notop.input, outputs=output)
    # Xception_model = multi_gpu_model(Xception_model, 4)
    Xception_model.compile(loss=objective,
                           optimizer=optimizer,
                           metrics=metrics)
    return Xception_model


#######################################################################SE-Xception
# Xception_notop = Xception_notop(inputs_dim)
# squeeze =  GlobalAveragePooling2D()(Xception_notop)
# excitation = Dense(units=2048 // 16)(squeeze)
# excitation = Activation('relu')(excitation)
# excitation = Dense(units=2048)(excitation)
# excitation = Activation('sigmoid')(excitation)
# excitation = Reshape((1, 1, 2048))(excitation)
#
# scale = multiply([Xception_notop, excitation])
# x = GlobalAveragePooling2D()(scale)
# x = Dropout(0.3)(x)
# fc2 = Dense(FLAGS.num_classes)(x)
# fc2 = Activation('sigmoid')(fc2) #此处注意,为sigmoid函数
# model = Model(inputs=inputs_dim, outputs=fc2)
# # model = multi_gpu_model(model, 4)
# model.compile(loss=objective, optimizer=optimizer, metrics=metrics)
# return model
Exemplo n.º 9
0
    def _load_model(self):
        model = None
        try:
            if self._model_name == 'VGG16' or self._model_name == 'vgg16':
                model = VGG16(weights='imagenet', include_top=True)
            elif self._model_name == 'VGG19' or self._model_name == 'vgg19':
                model = VGG19(weights='imagenet', include_top=True)
            elif self._model_name == 'ResNet50' or self._model_name == 'resNet50' \
              or self._model_name == 'Resnet50' or self._model_name == 'resnet50':
                model = ResNet50(weights='imagenet', include_top=True)
            elif self._model_name == 'Xception' or self._model_name == 'xception':
                model = Xception(weights='imagenet', include_top=True)
            elif self._model_name == 'InceptionV3' or self._model_name == 'inceptionV3' \
              or self._model_name == 'inceptionv3' or self._model_name == 'Inceptionv3':
                model = InceptionV3(weights='imagenet', include_top=True)
            else:
                model = load_model(self._model_path)
                model.load_weights(self._model_weights)

        except Exception as e:
            PrintHelper.failure_print("loading models.....", e)

        return model
def load_branch(model_name,
                input_img,
                aug_or_rotate,
                noise_input,
                return_model,
                load_weights=True,
                keras_rename=True):
    """
    return_model - return a keras Model if True, else return the 
        softmax activated output tensor 
    keras_rename - whether to rename layers if loading a keras model, 
        not sure if renaming is a good idea if return_model is True
    """
    def rename_layers(model, prefix):
        for m in model.layers:
            m.name = prefix + m.name

    if not (model_name in SUPPORTED_MODELS):
        raise FileNotFoundError(model_name + ' not found')

    input_resized = input_img if model_name in INPUT_SIZE_299 \
            else ResNet50_resize(input_img, model_name+'_')

    if aug_or_rotate == 'aug':
        input_aug = ImageAugmentation()([input_resized, noise_input])
    elif aug_or_rotate == 'rot':
        input_aug = ImageRotation()([input_resized, noise_input])
    else:
        input_aug = input_resized
    #if aug_layer_func is not None:
    #    input_aug = aug_layer_func(input_resized, noise_input)
    #else:
    #    input_aug = input_resized

    if model_name in INPUT_SIZE_299:
        input_preprocessed = InceptionV3_pre(input_aug, model_name + '_')
    elif model_name in INPUT_SIZE_224:
        input_preprocessed = ResNet50_pre(input_aug, model_name + '_')
    else:
        input_preprocessed = ResNet50_TF_pre(input_aug, model_name + '_')

    if model_name == 'xception':
        with tf.name_scope('xception'):
            model = Xception(weights=None, input_tensor=input_preprocessed)
            if load_weights:
                model.load_weights('xception.h5')
        if keras_rename:
            rename_layers(model, 'xception/')
        if not return_model:
            return model.output
        else:
            return model
    elif model_name == 'inceptionv3':
        with tf.name_scope('inceptionv3'):
            model = InceptionV3(weights=None, input_tensor=input_preprocessed)
            if load_weights:
                model.load_weights('inceptionv3.h5')
        if keras_rename:
            rename_layers(model, 'inceptionv3/')
        if not return_model:
            return model.output
        else:
            return model
    elif model_name == 'resnet50':
        with tf.name_scope('resnet50'):
            model = ResNet50(weights=None, input_tensor=input_preprocessed)
            if load_weights:
                model.load_weights('resnet50.h5')
        if keras_rename:
            rename_layers(model, 'resnet50/')
        if not return_model:
            return model.output
        else:
            return model
    elif model_name == 'inceptionv4':
        # JUST TO BE CLEAR: this is not logits that is the output here
        model_logits = model_wrappers.InceptionV4(\
                load_weights=load_weights, \
                # don't remember why this is needed

                is_prediction=True,\
                is_training=False, \
                create_aux_logits=False)(input_preprocessed)
        if not return_model:
            return model_logits
        else:
            return Model(inputs=input_img,
                         outputs=model_logits,
                         name='inception_v4')
    elif model_name == 'incresv2':
        model_logits = model_wrappers.IncResV2(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False, \
                create_aux_logits=False)(input_preprocessed)
        if not return_model:
            return model_logits
        else:
            return Model(inputs=input_img,
                         outputs=model_logits,
                         name='incres_v2')
    elif model_name == 'incresv2ensadv':
        model_logits = model_wrappers.IncResV2EnsAdv(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False, \
                create_aux_logits=False)(input_preprocessed)
        if not return_model:
            return model_logits
        else:
            return Model(inputs=input_img,
                         outputs=model_logits,
                         name='incres_v2_ens_adv')
    elif model_name == 'inceptionv3adv':
        model_logits = model_wrappers.InceptionV3Adv(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False)(input_preprocessed)
        if not return_model:
            return model_logits
        else:
            return Model(inputs=input_img,
                         outputs=model_logits,
                         name='inception_v3_adv')
    elif model_name == 'inceptionv3ens3adv':
        model_logits = model_wrappers.InceptionV3Ens3Adv(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False)(input_preprocessed)
        if not return_model:
            return model_logits
        else:
            return Model(inputs=input_img,
                         outputs=model_logits,
                         name='inception_v3_ens3_adv')
    elif model_name == 'inceptionv3ens4adv':
        model_logits = model_wrappers.InceptionV3Ens4Adv(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False)(input_preprocessed)
        if not return_model:
            return model_logits
        else:
            return Model(inputs=input_img,
                         outputs=model_logits,
                         name='inception_v3_ens4_adv')
    elif model_name == 'resnet101':
        model_logits = model_wrappers.ResNet101(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False)(input_preprocessed)
        if not return_model:
            return model_logits
        else:
            return Model(inputs=input_img,
                         outputs=model_logits,
                         name='resnet101')
    elif model_name == 'vgg16':
        print('WARNING: DO NOT USE VGG FOR SUBMISSION')
        model = VGG16(input_tensor=input_preprocessed)
        #        model = VGG16(weights=None, input_tensor=input_preprocessed)
        #        model.load_weights('vgg16.h5')
        if not return_model:
            return model.output
        else:
            return model
Exemplo n.º 11
0
get_ipython().system(
    'wget https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels_notop.h5'
)

# In[ ]:

from keras.applications.xception import Xception

xception_weights_path = "xception_weights_tf_dim_ordering_tf_kernels_notop.h5"
xception_conv_base = Xception(include_top=False,
                              weights=None,
                              input_tensor=None,
                              input_shape=(224, 224, 3),
                              pooling=None,
                              classes=None)
xception_conv_base.load_weights(xception_weights_path)

#xception_conv_final_predictor = Xception(include_top=False, weights="imagenet", input_tensor=None, input_shape=(224, 224, 3), pooling=None, classes=None)

xception_conv_base.summary()

#xception_conv_base.load_weights(xception_weights_path)

# In[ ]:

xception_conv_base.weights

# In[ ]:

from keras.models import Sequential, Model
from keras.layers import Flatten, Dense, Lambda, Input, Multiply, Dot, Add, Concatenate, Average
def load_pretrained_model(pretrained_model_name,
                          pooling,
                          model_weights_path=None):
    """ Load pretrained model with given pooling applied
    
    Args:
        pretrained_model: name of pretrained model ["Xception", "VGG16", "ResNet50", "InceptionV3", "InceptionResNetV2", "MobileNetV2"]
        pooling: pooling strategy for final pretrained model layer ["max","avg"]
        :model_weights_path: path to custom model weights if we want to load CNN model we've fine-tuned to produce features (e.g. for LRCNN)
    
    Returns:
        Pretrained model object (excluding dense softmax 1000 ImageNet classes layer)
    """

    # initialize output
    model = None

    pretrained_model_name = pretrained_model_name.lower()

    ###########################
    ### import pretrained model
    ###########################
    if pretrained_model_name == "xception":
        from keras.applications.xception import Xception
        model = Xception(include_top=False,
                         weights='imagenet',
                         pooling=pooling)
    elif pretrained_model_name == "vgg16":
        from keras.applications.vgg16 import VGG16
        model = VGG16(include_top=False, weights='imagenet', pooling=pooling)
    elif pretrained_model_name == "resnet50":
        from keras.applications.resnet50 import ResNet50
        model = ResNet50(include_top=False,
                         weights='imagenet',
                         pooling=pooling)
    elif pretrained_model_name == "inception_v3":
        from keras.applications.inception_v3 import InceptionV3
        model = InceptionV3(include_top=False,
                            weights='imagenet',
                            pooling=pooling)
    elif pretrained_model_name == "inception_resnet_v2":
        from keras.applications.inception_resnet_v2 import InceptionResNetV2
        model = InceptionResNetV2(include_top=False,
                                  weights='imagenet',
                                  pooling=pooling)
    elif pretrained_model_name == "mobilenetv2_1.00_224":
        from keras.applications.mobilenet_v2 import MobileNetV2
        model = MobileNetV2(include_top=False,
                            weights='imagenet',
                            pooling=pooling)
    else:
        raise NameError(
            'Invalid pretrained model name - must be one of ["Xception", "VGG16", "ResNet50", "InceptionV3", "InceptionResNetV2", "MobileNetV2"]'
        )

    if model_weights_path is not None:
        if os.path.exists(model_weights_path):
            model.load_weights(model_weights_path)
        else:
            raise NameError('pretrained model weights not found')

    return model
Exemplo n.º 13
0
def pre_trained_model(model_name,
                      weights='imagenet',
                      input_shape=None,
                      classes=1000,
                      include_top=True,
                      pooling=None,
                      verbose=True):
    '''
    Create Keras pre-trained model.
    :param model_name: Keras pre-trained model name
    :param weights: model weights
    :param input_shape: model input
    :param classes: categories number
    :param include_top:
    :param pooling:
    :param verbose:
    :return: model
    '''

    # load model
    if model_name not in KERAS_MODELS:
        raise Exception("Model  %s is not available!!!!. Use : %s " %
                        (model_name, KERAS_MODELS))
    else:
        input_shape = DEFAULT_SHAPES[
            model_name] if input_shape is None else input_shape
        #  BUILD MODELS
        if model_name == 'VGG16':
            if weights == 'imagenet':
                model = VGG16(weights='imagenet')
            else:
                model = VGG16(
                    include_top=include_top,
                    weights=None,
                    input_tensor=None,
                    input_shape=input_shape,  # default: (224, 224, 3)
                    pooling=pooling,
                    classes=classes)
        if model_name == 'ResNet50':
            if weights == 'imagenet':
                model = ResNet50(weights='imagenet')
            else:
                model = ResNet50(
                    include_top=include_top,
                    weights=None,
                    input_tensor=None,
                    input_shape=input_shape,  # default: (224, 224, 3)
                    pooling=pooling,
                    classes=classes)
        if model_name == 'Xception':
            model = Xception(
                include_top=include_top,
                weights=None,
                input_tensor=None,
                input_shape=input_shape,  # default: (299, 299, 3)
                pooling=pooling,
                classes=classes)

        if model_name == 'InceptionV3':
            input_tensor = Input(
                shape=input_shape
            )  # this assumes K.image_data_format() == 'channels_last'
            model = InceptionV3(
                include_top=include_top,
                weights=None,
                input_tensor=input_tensor,
                input_shape=input_shape,  # default: (299, 299, 3)
                pooling=pooling,
                classes=classes)
        # model loaded
        if verbose:
            print("{} model loaded.     Classes : {}  \
                            Input_shape : {}".format(model_name, classes,
                                                     input_shape))
        # load weights
        if weights != 'imagenet':
            if os.path.isfile(weights):
                model.load_weights(weights)
                if verbose:
                    print("{} model weights loaded from {}".format(
                        model_name, weights))
            else:
                raise Exception("Weights file %s don't exist!!!! " % weights)

        # Freezing layers
        for layer in model.layers:
            layer.trainable = False
            # return model
    return model
exp_name = FLAGS.exp
gpu_id = FLAGS.gpu_id
img_size = FLAGS.img_size
batch_size = FLAGS.batch_size
sex = FLAGS.sex
start_layer = FLAGS.start_layer


model_out_dir = "E{}_S{}_IMG_{}/model".format(exp_name, sex, img_size)
inference_out_dir = "E{}_S{}_IMG_{}/cam".format(exp_name, sex, img_size)

input_shape = (img_size, img_size, 3)
base_model = Xception(input_shape=input_shape, weights="imagenet", include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
base_model.load_weights(filepath=model_out_dir + "/base_model.h5")
predictions = Dense(1, activation=keras.activations.relu)(x)
model = Model(inputs=base_model.input, outputs=predictions)
model.load_weights(model_out_dir+"/model.h5")
optimizer = optimizers.RMSprop(lr=5E-4, decay=0.95)
model.compile(optimizer=optimizer, loss='mean_absolute_error')


print "[x] building models on GPU {}".format(gpu_id)
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "{}".format(gpu_id)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
set_session(tf.Session(config=config))

print "load model and data"
                      input_shape=(height, width, 3),
                      pooling='avg')
base_model2 = Xception(include_top=False,
                       weights=None,
                       input_shape=(height_inception, width_inception, 3),
                       pooling='avg')
base_model3 = InceptionV3(include_top=False,
                          weights=None,
                          input_shape=(height_inception, width_inception, 3),
                          pooling='avg')

base_model.load_weights(
    './keras_weights/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
)  #pls attention the location you run the script!!!!!!
base_model2.load_weights(
    './keras_weights/xception_weights_tf_dim_ordering_tf_kernels_notop.h5'
)  #pls attention the location you run the script!!!!!!
base_model3.load_weights(
    './keras_weights/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'
)  #pls attention the location you run the script!!!!!!
x = base_model.output
x2 = base_model2.output
x3 = base_model3.output

concat_x = concatenate([x, x2, x3])
x = Dropout(0.5)(concat_x)
x = Dense(4096, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(4096, activation='relu')(x)
pred = Dense(1, activation='sigmoid')(x)
model = Model(inputs=[base_model.input, base_model2.input, base_model3.input],
Exemplo n.º 16
0
                             filepath=root_dir + 'weights/' + weight_name,
                             save_best_only=True,
                             save_weights_only=True,
                             mode='min') ,
             TQDMCallback()]

history = model.fit_generator(generator=train_generator(batch_size),
                              steps_per_epoch=1,#int(np.ceil(train_df.shape[0]/batch_size)/300),#344,
                              epochs=60,
                              verbose=2,
                              callbacks=callbacks,
                              validation_data=valid_generator(batch_size),
                              validation_steps=1)#int(np.ceil(valid_df.shape[0]/batch_size))*20)


model.load_weights(root_dir + 'weights/'+ weight_name)

test_paths = glob(os.path.join(root_dir , 'test/audio/*wav'))


def test_generator(test_batch_size):
    while True:
        for start in range(0, len(test_paths), test_batch_size):
            x_batch = []
            end = min(start + test_batch_size, len(test_paths))
            this_paths = test_paths[start:end]
            for x in this_paths:
                x_batch.append(process_wav_file(x,phase='TEST'))
            x_batch = np.array(x_batch)
            yield x_batch
Exemplo n.º 17
0
def main():
    # Parameters
    if len(sys.argv) == 3:
        superclass = sys.argv[1]
        model_weight = sys.argv[2]
    else:
        print('Parameters error')
        exit()

    # The constants
    classNum = {'A': 40, 'F': 40, 'V': 40, 'E': 40, 'H': 24}
    testName = {'A': 'a', 'F': 'a', 'V': 'b', 'E': 'b', 'H': 'b'}
    date = '20180321'

    # Feature extraction model
    base_model = Xception(include_top=True,
                          weights=None,
                          input_tensor=None,
                          input_shape=None,
                          pooling=None,
                          classes=classNum[superclass[0]])
    base_model.load_weights(model_weight)
    base_model.summary()
    model = Model(inputs=base_model.input,
                  outputs=base_model.get_layer('avg_pool').output)

    imgdir_train = '../zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_'+date\
                     +'/zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_images_'+date
    imgdir_test = '../zsl_' + testName[superclass[0]] + '_' + str(
        superclass).lower() + '_test_' + date
    categories = os.listdir(imgdir_train)
    categories.append('test')

    num = 0
    for eachclass in categories:
        if eachclass[0] == '.':
            continue
        if eachclass == 'test':
            classpath = imgdir_test
        else:
            classpath = imgdir_train + '/' + eachclass
        num += len(os.listdir(classpath))

    print('Total image number = ' + str(num))

    features_all = np.ndarray((num, 2048))
    labels_all = list()
    images_all = list()
    idx = 0

    # Feature extraction
    for iter in tqdm(range(len(categories))):
        eachclass = categories[iter]
        if eachclass[0] == '.':
            continue
        if eachclass == 'test':
            classpath = imgdir_test
        else:
            classpath = imgdir_train + '/' + eachclass
        imgs = os.listdir(classpath)

        for eachimg in imgs:
            if eachimg[0] == '.':
                continue

            img_path = classpath + '/' + eachimg
            img = image.load_img(img_path, target_size=(229, 299))
            x = image.img_to_array(img)
            x = np.expand_dims(x, axis=0)
            x = preprocess_input(x)
            feature = model.predict(x)

            features_all[idx, :] = feature
            labels_all.append(eachclass)
            images_all.append(eachimg)
            idx += 1

    features_all = features_all[:idx, :]
    labels_all = labels_all[:idx]
    images_all = images_all[:idx]
    data_all = {
        'features_all': features_all,
        'labels_all': labels_all,
        'images_all': images_all
    }

    # Save features
    savename = 'features_' + superclass + '.pickle'
    fsave = open(savename, 'wb')
    pickle.dump(data_all, fsave)
    fsave.close()
Exemplo n.º 18
0
def main():
    # Parameters
    if len(sys.argv) == 4:
        superclass = sys.argv[1]
        imgmove = sys.argv[2]
        if imgmove == 'False':
            imgmove = False
        else:
            imgmove = True
        lr = float(sys.argv[3])
    else:
        print('Parameters error')
        exit()

    # The constants
    classNum = {'A': 40, 'F': 40, 'V': 40, 'E': 40, 'H': 24}
    testName = {'A': 'a', 'F': 'a', 'V': 'b', 'E': 'b', 'H': 'b'}
    date = '20180321'

    trainpath = 'trainval_' + superclass + '/train'
    valpath = 'trainval_' + superclass + '/val'

    if not os.path.exists('model'):
        os.mkdir('model')

    # Train/validation data preparation
    if imgmove:
        os.mkdir('trainval_' + superclass)
        os.mkdir(trainpath)
        os.mkdir(valpath)
        sourcepath = '../zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_'+date+'_crop'\
                     +'/zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_images_'+date
        categories = os.listdir(sourcepath)
        for eachclass in categories:
            if eachclass[0] == superclass[0]:
                print(eachclass)
                os.mkdir(trainpath + '/' + eachclass)
                os.mkdir(valpath + '/' + eachclass)
                imgs = os.listdir(sourcepath + '/' + eachclass)
                idx = 0
                for im in imgs:
                    if idx % 8 == 0:
                        shutil.copyfile(
                            sourcepath + '/' + eachclass + '/' + im,
                            valpath + '/' + eachclass + '/' + im)
                    else:
                        shutil.copyfile(
                            sourcepath + '/' + eachclass + '/' + im,
                            trainpath + '/' + eachclass + '/' + im)
                    idx += 1

    # Train and validation ImageDataGenerator
    batchsize = 32

    train_datagen = ImageDataGenerator(rescale=1. / 255,
                                       rotation_range=15,
                                       width_shift_range=5,
                                       height_shift_range=5,
                                       horizontal_flip=True)

    test_datagen = ImageDataGenerator(rescale=1. / 255)

    train_generator = train_datagen.flow_from_directory(trainpath,
                                                        target_size=(72, 72),
                                                        batch_size=batchsize)

    valid_generator = test_datagen.flow_from_directory(valpath,
                                                       target_size=(72, 72),
                                                       batch_size=batchsize)

    # Train MobileNet
    model = Xception(include_top=True,
                     weights=None,
                     input_tensor=None,
                     input_shape=(72, 72, 3),
                     pooling=None,
                     classes=classNum[superclass[0]])
    model.summary()
    model.compile(optimizer=SGD(lr=lr, momentum=0.9),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    steps_per_epoch = int(train_generator.n / batchsize)
    validation_steps = int(valid_generator.n / batchsize)

    weightname = 'model/mobile_' + superclass + '_wgt.h5'

    if os.path.exists(weightname):
        model.load_weights(weightname)

    checkpointer = ModelCheckpoint(weightname,
                                   monitor='val_loss',
                                   verbose=0,
                                   save_best_only=True,
                                   save_weights_only=True,
                                   mode='auto',
                                   period=1)
    model.fit_generator(train_generator,
                        steps_per_epoch=steps_per_epoch,
                        epochs=100,
                        validation_data=valid_generator,
                        validation_steps=validation_steps,
                        callbacks=[checkpointer])
Exemplo n.º 19
0
# In[ ]:


with open(historyfilename, 'w') as f:
    json.dump(history.history, f)

history_df = pd.DataFrame(history.history)
history_df.to_csv('history.csv')
history_df[['loss', 'val_loss']].plot()
history_df[['acc', 'val_acc']].plot()



print("Preds after loading weights")
model.load_weights(modelweights)
preds = model.predict(X_test, verbose=2)
from sklearn.metrics import accuracy_score,f1_score,roc_curve,auc
print(accuracy_score(Y_test,np.round(preds)))
print(f1_score(Y_test,np.round(preds)))
fpr, tpr, thresholds = roc_curve(Y_test, preds)
print("AUC: " + str(auc(fpr, tpr)))

from keras.models import Model
layer_name = 'global_average_pooling2d_1'#'global_average_pooling2d_1'
model_extracted= Model(inputs=model.input, outputs=model.get_layer(layer_name).output)
model_extracted.summary()

Train_Features=model_extracted.predict(X_train)
Train_Target=Y_train
layers = ['new_pool_1','new_batch_1','new_dense_1','new_batch_2','new_drop_2','new_dene_2']
model_1.get_layer('new_dene_2').set_weights([np.array(weights[10],dtype=np.float32),np.zeros([3],dtype=np.float32)])
model_1.get_layer('new_dense_1').set_weights([np.array(weights[4],dtype=np.float32),np.zeros([512],dtype=np.float32)])

# the first 249 layers and unfreeze the rest:
for layer in model_1.layers[:249]:
   layer.trainable = False
for layer in model_1.layers[249:]:
   layer.trainable = True

chkp = ModelCheckpoint('skin_2.cancer.models.best.hdf5',verbose=1)
# print(weights[10])
# print(model_1.get_layer('new_dene_2').get_weights()[0])

model_1.load_weights('skin_2.cancer.models.best.hdf5')

model_1.fit(bottle_neck_train_set_1,train_targets,batch_size=64,epochs=300,verbose=1
          ,callbacks=[chkp],shuffle=True,validation_data=(bottle_neck_valid_set_1,valid_targets))

model_1.load_weights('skin_2.cancer.models.best.hdf5')
_pred = np.argmax(model_1.predict(bottle_neck_train_set_1),axis=1)
true_pred = np.argmax(train_targets,axis=1)
true=[]
for index in range(len(_pred)):
      if(true_pred[index]==_pred[index]):
          true.append(1)
      else:
        true.append(0)
print("Test Set Accuracy {}%".format((sum(true)/len(true))*100))
Exemplo n.º 21
0
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from keras.applications.xception import Xception
from keras.optimizers import Adam
from keras import layers
from keras import Model

pre_model = Xception(weights=None,
                     input_shape=(150, 150, 3),
                     include_top=False)

pre_weights = 'C:/Users/hp/Desktop/pneumonia/xception_weights_tf_dim_ordering_tf_kernels_notop.h5'

pre_model.load_weights(pre_weights)

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

last_layer = pre_model.get_layer('block4_pool')
last_output = last_layer.output

x = layers.AveragePooling2D(7, 7)(last_output)
x = layers.Flatten()(x)
x = layers.Dense(1024, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(512, activation='relu')(x)
x = layers.Dropout(0.4)(x)
x = layers.Dense(1, activation='sigmoid')(x)
Exemplo n.º 22
0
def load_branch(model_name, input_img, aug_or_rotate, noise_input, return_model, \
        load_weights=True, keras_rename=True):
    """
    Arguments
        model_name
        input_img       input image tensor
        aug_or_rotate   'aug' for geometric transformation, 'rot' for simple
                        rotation, everything else means no augmentation
        noise_input     tensor containing the scale of noise
        return_model    return a keras Model if True, else return the softmax 
                        activated output tensor. Don't remember why this option is 
                        there. Never used
        keras_rename    whether to rename layers if loading a keras model, not sure 
                        if renaming is a good idea if return_model is True
    """
    def rename_layers(model, prefix):
        for m in model.layers:
            m.name = prefix + m.name

    if not (model_name in SUPPORTED_MODELS):
        raise NotImplementedError(model_name + ' not supported')

    input_resized = input_img if model_name in INPUT_SIZE_299 \
            else ResNet50_resize(input_img, model_name+'_')

    if aug_or_rotate == 'aug':
        input_aug = ImageAugmentation()([input_resized, noise_input])
    elif aug_or_rotate == 'rot':
        input_aug = ImageRotation()([input_resized, noise_input])
    else:
        input_aug = input_resized

    if model_name in INPUT_SIZE_299:
        input_preprocessed = InceptionV3_pre(input_aug, model_name + '_')
    elif model_name in INPUT_SIZE_224:
        input_preprocessed = ResNet50_pre(input_aug, model_name + '_')
    else:
        input_preprocessed = ResNet50_TF_pre(input_aug, model_name + '_')

    if model_name == 'xception':
        with tf.name_scope('xception'):
            model = Xception(weights=None, input_tensor=input_preprocessed)
            if load_weights:
                model.load_weights('xception.h5')
        if keras_rename:
            rename_layers(model, 'xception/')
        if not return_model:
            return model.output
        else:
            return model
    elif model_name == 'inceptionv3':
        with tf.name_scope('inceptionv3'):
            model = InceptionV3(weights=None, input_tensor=input_preprocessed)
            if load_weights:
                model.load_weights('inceptionv3.h5')
        if keras_rename:
            rename_layers(model, 'inceptionv3/')
        if not return_model:
            return model.output
        else:
            return model
    elif model_name == 'resnet50':
        with tf.name_scope('resnet50'):
            model = ResNet50(weights=None, input_tensor=input_preprocessed)
            if load_weights:
                model.load_weights('resnet50.h5')
        if keras_rename:
            rename_layers(model, 'resnet50/')
        if not return_model:
            return model.output
        else:
            return model
    elif model_name == 'inceptionv4':
        # JUST TO BE CLEAR: this is not logits that is the output here
        model_preds = model_wrappers.InceptionV4(\
                load_weights=load_weights, \
                # don't remember why this is needed

                is_prediction=True,\
                is_training=False, \
                create_aux_logits=False)(input_preprocessed)
        if not return_model:
            return model_preds
        else:
            return Model(inputs=input_img,
                         outputs=model_preds,
                         name='inception_v4')
    elif model_name == 'incresv2':
        model_preds = model_wrappers.IncResV2(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False, \
                create_aux_logits=False)(input_preprocessed)
        if not return_model:
            return model_preds
        else:
            return Model(inputs=input_img,
                         outputs=model_preds,
                         name='incres_v2')
    elif model_name == 'incresv2ensadv':
        model_preds = model_wrappers.IncResV2EnsAdv(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False, \
                create_aux_logits=False)(input_preprocessed)
        if not return_model:
            return model_preds
        else:
            return Model(inputs=input_img,
                         outputs=model_preds,
                         name='incres_v2_ens_adv')
    elif model_name == 'inceptionv3adv':
        model_preds = model_wrappers.InceptionV3Adv(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False)(input_preprocessed)
        if not return_model:
            return model_preds
        else:
            return Model(inputs=input_img,
                         outputs=model_preds,
                         name='inception_v3_adv')
    elif model_name == 'inceptionv3ens3adv':
        model_preds = model_wrappers.InceptionV3Ens3Adv(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False)(input_preprocessed)
        if not return_model:
            return model_preds
        else:
            return Model(inputs=input_img,
                         outputs=model_preds,
                         name='inception_v3_ens3_adv')
    elif model_name == 'inceptionv3ens4adv':
        model_preds = model_wrappers.InceptionV3Ens4Adv(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False)(input_preprocessed)
        if not return_model:
            return model_preds
        else:
            return Model(inputs=input_img,
                         outputs=model_preds,
                         name='inception_v3_ens4_adv')
    elif model_name == 'resnet101':
        model_preds = model_wrappers.ResNet101(\
                load_weights=load_weights, \
                is_prediction=True,\
                is_training=False)(input_preprocessed)
        if not return_model:
            return model_preds
        else:
            return Model(inputs=input_img,
                         outputs=model_preds,
                         name='resnet101')