Exemplo n.º 1
0
def finetuned_resnet(include_top, weights_dir):
    '''

    :param include_top: True for training, False for generating intermediate results for
                        LSTM cell
    :param weights_dir: path to load finetune_resnet.h5
    :return:
    '''
    base_model = ResNet50(include_top=False,
                          weights='imagenet',
                          input_shape=IMSIZE)
    for layer in base_model.layers:
        layer.trainable = False

    x = base_model.output
    x = Flatten()(x)
    x = Dense(2048, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(1024, activation='relu')(x)
    x = Dropout(0.5)(x)

    if include_top:
        x = Dense(N_CLASSES, activation='softmax')(x)

    model = Model(inputs=base_model.input, outputs=x)
    if os.path.exists(weights_dir):
        model.load_weights(weights_dir, by_name=True)

    return model
 def init_model(self):
     self.model = ResNet50(include_top=True, weights=None,
                           input_shape=(self.input_dim, self.input_dim, 1),
                           backend=keras.backend,
                           layers=keras.layers,
                           models=keras.models,
                           utils=keras.utils,
                           classes=self.num_classes)
     self.model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
Exemplo n.º 3
0
def __main():
    model = resnet50_unet_sigmoid()
    layers = model.layers
    layers = [layer.name for layer in layers]
    model.summary()
    # del model
    model2 = ResNet50(include_top=False, weights=None)
    layers2 = model2.layers
    layers2 = [layer.name for layer in layers2]
    model2.summary()
    print(layers[:5])
    print(layers2[:5])
Exemplo n.º 4
0
def build_model(config):
    """
    Returns: a model with specified weights
    """

    input_size = config['input_size']
    input_shape = (input_size, input_size, 3)

    if ('pretrain' in config) and config['pretrain']:
        assert config['input_size'] == 224
        weights = 'imagenet'
    else:
        weights = None

    assert config['image_model'] in ['densenet', 'resnet', 'linear']
    if config['image_model'] == 'densenet':
        print("Using Densenet.")
        base_model = DenseNet121(input_shape=input_shape,
                                 weights=weights,
                                 include_top=False,
                                 pooling='avg')
        image_input = base_model.input
        layer = base_model.output
    elif config['image_model'] == 'resnet':
        print("Using Resnet.")
        base_model = ResNet50(input_shape=input_shape,
                              weights=weights,
                              include_top=False,
                              pooling='avg')
        image_input = base_model.input
        layer = base_model.output
    elif config['image_model'] == 'linear':
        print("Using linear model.")
        image_input = Input(shape=input_shape)
        layer = Flatten()(image_input)

    if ('freeze' in config) and config['freeze']:
        for layer in base_model.layers:
            try:
                layer.trainable = False
                print("Freezing {}".format(layer))
            except Exception:
                print("Not trainable {}".format(layer))

    predictions = Dense(14, activation='sigmoid')(layer)
    model = Model(inputs=image_input, outputs=predictions)
    return model
def rn50_10():
    base = ResNet50(include_top=False,
                    weights='imagenet',
                    backend=ks.backend,
                    layers=ks.layers,
                    utils=ks.utils,
                    models=ks.models)
    base.layers.pop(0)
    inp = Input(shape=(config.img_size, config.img_size, config.channel))
    x = BatchNormalization()(inp)
    base_out = base(x)
    x = GlobalAveragePooling2D()(base_out)
    x = Dense(1280, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dense(128, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dense(1)(x)
    model = Model(inputs=[inp], outputs=x)

    return model
def get_model(img_size,
              num_channels,
              num_classes,
              model_name='densenet121',
              pretrain=False):
    weights = 'imagenet' if pretrain else None
    if pretrain:
        assert img_size == 224
    input_tensor = Input(shape=(img_size, img_size, num_channels))

    if model_name == 'densenet121':
        model = DenseNet121(include_top=False,
                            weights=weights,
                            input_tensor=input_tensor,
                            pooling='avg')
        preprocessor = pi_densenet121

    elif model_name == 'resnet50':
        model = ResNet50(include_top=False,
                         weights=weights,
                         input_tensor=input_tensor,
                         pooling='avg')
        preprocessor = pi_resnet50

    elif model_name == 'vgg19':
        model = VGG19(include_top=False,
                      weights=weights,
                      input_tensor=input_tensor,
                      pooling='avg')
        preprocessor = pi_vgg19

    else:
        raise Exception("Unrecognized model name: %s" % model_name)

    output_tensor = model(input_tensor)
    output_tensor = Dense(num_classes, activation='sigmoid')(output_tensor)
    complete_model = Model(input_tensor, output_tensor)
    return complete_model, preprocessor
from keras.preprocessing import sequence
# 从config文件中引入一些参数 包括token最大长度 测试文件夹长度 最优的模型参数
from imgapp.ch_image_cap.src.config import max_token_length, test_a_image_folder, best_model
from imgapp.ch_image_cap.forward import build_model
import keras
from keras_applications.resnet50 import ResNet50
from keras_preprocessing.image import (load_img, img_to_array)
from keras.models import load_model
from imgapp.ch_image_cap.src.config import img_rows, img_cols

from imgapp.ch_image_cap.src.config import train_image_folder, valid_image_folder, test_a_image_folder, test_b_image_folder

image_model = ResNet50(include_top=False,
                       weights='imagenet',
                       layers=keras.layers,
                       models=keras.models,
                       utils=keras.utils,
                       pooling='avg',
                       backend=keras.backend)


def encode_images(image_path):
    encoding = {}
    #names储存文件夹中所有的jpg文件名称
    name = image_path.split('/')[-1]
    #输出编码过程
    print('ResNet50提取特征中...')
    #对每个batche进行处理,使用tqdm库显示处理进度
    #使用empty创建一个多维数组
    image_input = np.empty((1, img_rows, img_cols, 3))
    #对于每一张图片
Exemplo n.º 8
0
def build_model(input_shape, num_classes, weights='imagenet', opt=None):
    # create the base pre-trained model

    base_model = ResNet50(weights=weights,
                          include_top=False,
                          input_shape=input_shape,
                          backend=keras.backend,
                          layers=keras.layers,
                          models=keras.models,
                          utils=keras.utils)

    # add a global spatial average pooling layer
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu', name='fc2014_1')(x)
    x = Dropout(0.5)(x)
    x = Dense(1024, activation='relu', name='fc2014_2')(x)
    x = Dropout(0.5)(x)
    x = Dense(num_classes, activation='sigmoid', name='fc28')(x)

    # this is the model we will train
    model = Model(inputs=base_model.input,
                  outputs=x,
                  name='pre_trained_resnet50')

    optimizer = None

    if opt == 0:
        optimizer = SGD(lr=0.01, momentum=0.9, nesterov=True, decay=1e-06)

    elif opt == 1:
        optimizer = RMSprop(decay=1e-06)

    elif opt == 2:
        optimizer = Adagrad(decay=1e-06)

    elif opt == 3:
        optimizer = Adadelta(decay=1e-06)

    elif opt == 4:
        optimizer = Adam(lr=0.001,
                         beta_1=0.9,
                         beta_2=0.999,
                         epsilon=None,
                         decay=1e-06,
                         amsgrad=False)

    elif opt == 5:
        optimizer = Adamax(decay=1e-06)

    elif opt == 6:
        optimizer = Adam(amsgrad=True, decay=1e-06)

    elif opt == 7:
        optimizer = Adam(lr=0.0001, amsgrad=True, decay=1e-06)

    # compile the model (should be done *after* setting layers to non-trainable)
    model.compile(optimizer=optimizer,
                  loss=binary_crossentropy,
                  metrics=['accuracy'])

    return model
Exemplo n.º 9
0
def resnet50_unet_sigmoid(
        input_shape=(IMG_H, IMG_W, IMG_C), weights='imagenet'):
    inp = Input(input_shape)

    x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(inp)
    x = layers.Conv2D(64, (7, 7),
                      strides=(2, 2),
                      padding='valid',
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=BN_AXIS, name='bn_conv1')(x)
    x = layers.Activation('relu')(x)
    c1 = x
    # print("c1")
    # print(c1.shape)
    x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
    c2 = x
    # print("c2")
    # print(c2.shape)

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
    c3 = x
    # print("c3")
    # print(c3.shape)

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
    c4 = x
    # print("c4")
    # print(c4.shape)

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')
    c5 = x
    # print("c5")
    # print(c5.shape)

    u6 = conv_block_custom(UpSampling2D()(c5), 1024)
    # print("u6")
    # print(u6.shape)
    u6 = concatenate([u6, c4], axis=-1)
    u6 = conv_block_custom(u6, 1024)

    u7 = conv_block_custom(UpSampling2D()(u6), 512)
    # print("u7")
    # print(u7.shape)
    u7 = concatenate([u7, c3], axis=-1)
    u7 = conv_block_custom(u7, 512)

    u8 = conv_block_custom(UpSampling2D()(u7), 256)
    # print("u8")
    # print(u8.shape)
    u8 = concatenate([u8, c2], axis=-1)
    u8 = conv_block_custom(u8, 256)

    u9 = conv_block_custom(UpSampling2D()(u8), 64)
    # print("u9")
    # print(u9.shape)
    u9 = concatenate([u9, c1], axis=-1)
    u9 = conv_block_custom(u9, 64)

    u10 = conv_block_custom(UpSampling2D()(u9), 32)
    u10 = conv_block_custom(u10, 32)

    res = Conv2D(2, (1, 1), activation='sigmoid')(u10)

    model = Model(inp, res)

    if weights == "imagenet":
        resnet50 = ResNet50(weights=weights,
                            include_top=False,
                            input_shape=(input_shape[0], input_shape[1], 3))
        # resnet50.summary()
        print("Loading imagenet weitghts ...")
        for i in tqdm(range(3, len(resnet50.layers) - 2)):
            try:
                model.layers[i].set_weights(resnet50.layers[i].get_weights())
                model.layers[i].trainable = False
            except:
                print(resnet50.layers[i].name)
                exit()
        print("imagenet weights have been loaded.")
        del resnet50

    return model
Exemplo n.º 10
0
import keras
import numpy as np

from keras_applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from keras.preprocessing import image

default_setting = {
    "backend": keras.backend,
    "layers": keras.layers,
    "models": keras.models,
    "utils": keras.utils
}

if __name__ == '__main__':

    model = ResNet50(weights='imagenet', **default_setting)

    img_path = "imgs/img2.jpg"
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x, **default_setting)

    preds = model.predict(x)
    results = decode_predictions(preds, top=3, **default_setting)[0]

    print('Predicted:', results)
    print(results)
    print(preds.shape)
#!/usr/bin/env python

from keras_applications.resnet50 import ResNet50
from keras_preprocessing import image
from keras_applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(weights=None)

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
print("{0} {1}".format(x.shape, x.dtype))
x = np.expand_dims(x, axis=0)
print("{0} {1}".format(x.shape, x.dtype))
x = preprocess_input(x)
print("{0} {1}".format(x.shape, x.dtype))

preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print("Predicted: {0}".format(decode_predictions(preds, top=3)[0]))
# Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]