Ejemplo n.º 1
0
def get_model(pooling='avg', input_shape=None):
    base_model = ResNet50(weights='imagenet',
                          include_top=False,
                          pooling=pooling,
                          input_shape=input_shape)

    return base_model, preprocess_input
Ejemplo n.º 2
0
    def __init__(self,
                 input_data_shape,
                 num_classes,
                 model_name='resnet50',
                 trainable_layers_amount=1):
        super(ImageModel, self).__init__()

        self.num_classes = num_classes
        self.model_name = model_name
        self.trainable_layers_amount = trainable_layers_amount
        self.input_data_shape = input_data_shape

        if self.model_name == 'resnet50':
            self.base_model = ResNet50(include_top=False,
                                       weights='imagenet',
                                       input_tensor=None,
                                       input_shape=self.input_data_shape)
            # Avoid training layers in resnet model.
            layers = self.base_model.layers
            print("Layers name")
            for layer in layers:
                print(layer.name)
                layer.trainable = False
            print("Making layers trainable")
            for layer in layers[-trainable_layers_amount:]:
                print(layer.name)
                layer.trainable = True

        x0 = Input(shape=self.input_data_shape)
        x1 = Lambda(preprocess_input, output_shape=self.input_data_shape)(x0)
        x2 = self.base_model(x1)
        x3 = GlobalAveragePooling2D()(x2)
        x4 = Dense(1024, activation='relu')(x3)
        x5 = Dense(num_classes, activation='softmax', name='softmax')(x4)
        self.model = Model(inputs=x0, outputs=x5)
def create_model():
    dropout = 0.5
    reg = l2(0.01)
    opt = Adam(lr=LEARN_RATE)

    conv_base = ResNet50(weights='imagenet',
                         include_top=False,
                         input_shape=INPUT_SHAPE)
    conv_base.trainable = False
    model = models.Sequential()
    model.add(conv_base)
    model.add(AveragePooling2D(pool_size=(2, 2)))
    model.add(layers.Flatten())
    model.add(layers.Dropout(dropout))
    model.add(
        layers.Dense(512,
                     activation='relu',
                     kernel_initializer='uniform',
                     kernel_regularizer=reg))
    model.add(layers.Dropout(dropout))
    model.add(
        layers.Dense(512,
                     activation='relu',
                     kernel_initializer='uniform',
                     kernel_regularizer=reg))
    model.add(layers.Dropout(dropout))
    model.add(layers.Dense(1, activation='sigmoid'))
    model.compile(loss='binary_crossentropy',
                  optimizer=opt,
                  metrics=['accuracy'])
    model.summary()
    # tf.keras.utils.plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
    return model
Ejemplo n.º 4
0
def create_model():
    conv_base = ResNet50(weights='imagenet',
                         include_top=False,
                         input_shape=INPUT_SHAPE)
    conv_base.trainable = False
    model = models.Sequential()
    model.add(conv_base)
    model.add(layers.GlobalAveragePooling2D())
    model.add(
        layers.Dense(512,
                     activation='relu',
                     kernel_initializer='uniform',
                     kernel_regularizer=reg))
    model.add(layers.Dropout(0.5))
    model.add(
        layers.Dense(512,
                     activation='relu',
                     kernel_initializer='uniform',
                     kernel_regularizer=reg))
    model.add(layers.Dropout(0.5))
    model.add(
        layers.Dense(2,
                     activation='softmax',
                     kernel_initializer='uniform',
                     kernel_regularizer=reg))
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizers.SGD(lr=LEARN_RATE, momentum=0.9),
                  metrics=['accuracy'])
    model.summary()
    return model
    def __init__(self,
                 x,
                 t,
                 LR,
                 input_shape,
                 output_shape,
                 model_name='FCN_ResNet50'):
        # optimization setting
        self.LR = LR

        # naming setting
        self.model_name = model_name

        # pretrain model
        self.resnet50 = ResNet50(include_top=False,
                                 weights='imagenet',
                                 input_tensor=None,
                                 input_shape=input_shape[1:])
        self.resnet50.trainable = False
        #for layer in resnet50.layers[:164]:
        #    layer.trainable = False

        # model setting
        self.input_shape = input_shape
        self.output_shape = output_shape
        with tf.variable_scope(self.model_name):
            self.x = x
            self.t = t
            self.features = self.resnet50(inputs=self.x)  # (3, 14, 2048)
            self.y = self._forward_pass(self.features)
Ejemplo n.º 6
0
    def __generate_pretrained_encoder(input_layer: Model):
        resnet = ResNet50(include_top=False,
                          weights='imagenet',
                          input_tensor=input_layer,
                          input_shape=(512, 512, 3),
                          pooling=None)

        return resnet
Ejemplo n.º 7
0
    def __init__(self, input_sizeW, input_sizeH):
        resnet50 = ResNet50(input_shape=(input_sizeW, input_sizeH, 3),
                            include_top=False)
        resnet50.layers.pop()  # remove the average pooling layer
        #resnet50.load_weights(RESNET50_BACKEND_PATH)

        self.feature_extractor = Model(resnet50.layers[0].input,
                                       resnet50.layers[-1].output)
Ejemplo n.º 8
0
def initResNet50():
    print('Loading ResNet50 model...')
    if not os.path.isfile('resnet50.h5'):
        urllib.urlretrieve(RESNET50_URL, filename='resnet50.h5')
        print('ResNet50 model downloaded')
    model = ResNet50(weights='resnet50.h5')
    print('ResNet50 model loaded from disk')

    return model
Ejemplo n.º 9
0
def main():
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    y_train = to_categorical(y_train)
    y_test = to_categorical(y_test)

    model = ResNet50(input_shape=(32, 32, 3),
                     include_top=False,
                     weights='imagenet',
                     classes=10)
    model.summary()
Ejemplo n.º 10
0
def get_model():
    # define ResNet50 model
    model = ResNet50(weights='imagenet')
	
    # get AMP layer weights
    amp_weights = model.layers[-1].get_weights()[0]
	
    # extract wanted output
    model = Model(inputs=model.input, 
        outputs=(model.layers[-4].output, model.layers[-1].output)) 
    return model, amp_weights
Ejemplo n.º 11
0
def get_model(input_shape=(128, 128, 16), classes=4, activation='sigmoid'):
    resnet = ResNet50(weights=None, include_top=False, input_shape=input_shape)
    model = Sequential()
    model.add(resnet)
    model.add(GlobalAveragePooling2D())
    model.add(
        Dense(units=classes,
              activation=activation,
              kernel_initializer="he_normal"))

    return model
def evaluate(encoder, decoder, optimizer, step_counter, image):
    attention_plot = np.zeros((max_length, attention_features_shape))

    hidden = decoder.reset_state(batch_size=1)
    size = [224, 224]

    def load_image(image_path):
        img = tf.read_file(PATH + image_path)
        img = tf.image.decode_jpeg(img, channels=3)
        img = tf.image.resize(img, size)
        img = tf.keras.applications.resnet50.preprocess_input(img)
        return img, image_path

    from tensorflow.python.keras.applications.resnet50 import ResNet50

    image_model = ResNet50(
        weights='resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
        include_top=False)  #创建ResNet网络

    new_input = image_model.input
    hidden_layer = image_model.layers[-2].output
    image_features_extract_model = tf.keras.Model(new_input, hidden_layer)

    temp_input = tf.expand_dims(load_image(image)[0], 0)
    img_tensor_val = image_features_extract_model(temp_input)
    img_tensor_val = tf.reshape(
        img_tensor_val, (img_tensor_val.shape[0], -1, img_tensor_val.shape[3]))

    features = encoder(img_tensor_val)

    #    print(step_counter.numpy())
    dec_input = tf.expand_dims([tokenizer.word_index['<start>']], 0)
    result = []

    for i in range(max_length):
        predictions, hidden, attention_weights = decoder(
            dec_input, features, hidden)

        attention_plot[i] = tf.reshape(attention_weights, (-1, )).numpy()

        print(predictions.get_shape())

        predicted_id = tf.multinomial(predictions, num_samples=1)[0][0].numpy()
        result.append(index_word[predicted_id])

        print(predicted_id)

        if index_word[predicted_id] == '<end>':
            return result, attention_plot

        dec_input = tf.expand_dims([predicted_id], 0)

    attention_plot = attention_plot[:len(result), :]
    return result, attention_plot
def create_model():
    model = ResNet50(include_top=False, weights='imagenet', pooling='max')
    inp = Input(input_shape, name='input_image')
    out = model(inp)
    out = Flatten()(out)
    out = Dense(1024)(out)
    out = Dropout(0.75)(out)
    out = Dense(1)(out)
    model = Model(inputs=[inp], outputs=out)
    model.compile(optimizer=Adam(lr=0.001), loss='binary_crossentropy')
    return model
Ejemplo n.º 14
0
def resnet_model():

    model = ResNet50(weights=None,
                     include_top=True,
                     input_shape=(75, 75, 3),
                     classes=196)

    model.compile(loss='sparse_categorical_crossentropy',
                  optimizer=tf.keras.optimizers.Adam(lr=2e-5),
                  metrics=['acc'])

    return model
Ejemplo n.º 15
0
def buildModel(num_classes):
    model = Sequential()
    model.add(ResNet50(include_top=False, pooling='avg', weights='imagenet'))
    model.add(Dense(num_classes, activation='softmax'))

    # Say not to train first layer (ResNet) model. It is already trained
    model.layers[0].trainable = False

    model.compile(optimizer='sgd',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    return model
def init_model(model_name):
    if (model_name == "VGG19"):  # Initialisierung des 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")
Ejemplo n.º 17
0
    def build(self):
        resnet50_model = ResNet50(include_top=False,
                                  weights='imagenet',
                                  input_shape=(32, 256, 3))
        x = resnet50_model.output
        conv_outputs = Lambda(self.squeeze_wrapper)(x)

        logger.debug("Resnet50输出的feature map shape:%r",
                     conv_outputs.get_shape().as_list())

        inputs = resnet50_model.inputs

        # inputs=[(-1,32,256,3)]
        # conv_outputs=[-1,8,2048]
        return conv_outputs, inputs[0]
Ejemplo n.º 18
0
def make_numpy_feature(numpyPATH, img_filename, PATH, weights=RESNET50_WEIGHTS):
    '''
    将图片转化为特征数据
    :param numpyPATH:存储提取好特征后的存储文件夹
    :param img_filename:图片文件列表
    :param PATH: 图片所在的文件夹
    :param weights: ResNet50模型的权重
    :return:
    '''
    if os.path.exists(numpyPATH):  # 去除已有文件夹
        shutil.rmtree(numpyPATH, ignore_errors=True)

    os.mkdir(numpyPATH)  # 新建文件夹

    size = [224, 224]  # 设置输出尺寸
    batch_size = 10  # 批量大小

    def load_image(image_path):
        '''输入图片的预处理'''
        img = tf.compat.v1.read_file(PATH + image_path)
        img = tf.image.decode_jpeg(img, channels=3)
        img = tf.image.resize(img, size)
        # 使用Reset模型的统一预处理
        img = tf.keras.applications.resnet50.preprocess_input(img)
        return img, image_path

    # 创建ResNet模型
    image_model = ResNet50(weights=weights, include_top=False)
    new_input = image_model.input
    # 获取ResNet导数第二层(池化前的卷积结果)
    hidden_layer = image_model.layers[-2].output
    image_feature_extract_model = tf.keras.Model(new_input, hidden_layer)
    image_feature_extract_model.summary()

    # 对文件目录去重
    encode_train = sorted(set(img_filename))

    # 图片数据集
    image_dataset = tf.data.Dataset.from_tensor_slices(encode_train).map(load_image).batch(batch_size)

    for img, path in image_dataset:
        batch_feature = image_feature_extract_model(img)
        print(batch_feature.shape)
        batch_feature = tf.reshape(batch_feature, (batch_feature.shape[0], -1, batch_feature.shape[3]))
        print(batch_feature.shape)
        for bf, p in zip(batch_feature, path):
            path_of_feature = p.numpy().decode('utf-8')
            np.save(numpyPATH + path_of_feature, bf.numpy())
Ejemplo n.º 19
0
def model_pretraining(model_name, num_labels):
    if model_name.lower() == 'resnet50':
        restnet_base_model = ResNet50(weights='imagenet',
                                      include_top=False,
                                      input_shape=input_shape)
        x = Flatten()(restnet_base_model.get_output_at(-1))
        x = Dense(64, activation='relu')(x)
        output = Dense(num_labels, activation='sigmoid')(x)
        model = Model(restnet_base_model.input, output, name='resnet')
    elif model_name.lower() == 'inceptionv3':
        inception_base_model = InceptionV3(weights='imagenet',
                                           include_top=False,
                                           input_shape=input_shape)
        x = Flatten()(inception_base_model.get_output_at(-1))
        x = Dense(64, activation='relu')(x)
        output = Dense(num_labels, activation='sigmoid')(x)
        model = Model(inception_base_model.input, output, name='inception')
    elif model_name.lower() == 'xception':
        xception_base_model = Xception(weights='imagenet',
                                       include_top=False,
                                       input_shape=input_shape)
        x = Flatten()(xception_base_model.get_output_at(-1))
        x = Dense(64, activation='relu')(x)
        output = Dense(num_labels, activation='sigmoid')(x)
        model = Model(xception_base_model.input, output, name='xception')
    elif model_name.lower() == 'densenet':
        xception_base_model = DenseNet121(weights='imagenet',
                                          include_top=False,
                                          input_shape=input_shape)
        x = Flatten()(xception_base_model.get_output_at(-1))
        x = Dense(64, activation='relu')(x)
        output = Dense(num_labels, activation='sigmoid')(x)
        model = Model(xception_base_model.input, output, name='densenet')
    elif model_name.lower() == 'inceptionresnet':
        xception_base_model = DenseNet121(weights='imagenet',
                                          include_top=False,
                                          input_shape=input_shape)
        x = Flatten()(xception_base_model.get_output_at(-1))
        x = Dense(64, activation='relu')(x)
        output = Dense(num_labels, activation='sigmoid')(x)
        model = Model(xception_base_model.input,
                      output,
                      name='inceptionresnet')
    else:
        raise ValueError('Invalid model name!')
    return model
def run_model(args):
    # Configure the memory optimizer
    #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
    config = tf.ConfigProto()
    config.graph_options.rewrite_options.memory_optimization = rewriter_config_pb2.RewriterConfig.SCHEDULING_HEURISTICS
    #config.gpu_options.allow_growth=True
    config.gpu_options.per_process_gpu_memory_fraction = 0.5
    K.set_session(tf.Session(config=config))

    num_classes = args.num_classes
    batch_size = args.batch_size

    model_name = args.model
    if model_name == 'ResNet50':
        model = ResNet50(weights=None, include_top=True,
                                              input_shape=input_shape,
                                              classes=num_classes)
Ejemplo n.º 21
0
def network(hidden_units):
  
    input = Input(shape=IMAGE_SHAPE, name=INPUT_NAME)
    conv_base = ResNet50(weights='imagenet',
                   include_top=False,
                   pooling = 'avg',
                   input_tensor=input)
    
    for layer in conv_base.layers:
        layer.trainable = False

    a = Flatten()(conv_base.output)
    a = Dense(hidden_units, activation='relu')(a)
    y = Dense(NUM_CLASSES, activation='softmax')(a)
    
    model = Model(inputs=input, outputs=y)
    
    return model
Ejemplo n.º 22
0
def makenumpyfeature(numpyPATH, img_filename, PATH):
    if os.path.exists(numpyPATH):
        shutil.rmtree(numpyPATH, ignore_errors=True)
    os.mkdir(numpyPATH)

    size = [224, 224]
    batchsize = 10

    def load_image(image_path):
        img = tf.read_file(PATH + image_path)
        img = tf.image.decode_jpeg(img, channels=3)
        img = tf.image.resize(img, size)
        img = tf.keras.applications.resnet50.preprocess_input(img)
        return img, image_path

    image_model = ResNet50(
        weights='resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
        include_top=False)  #创建ResNet网络

    new_input = image_model.input
    hidden_layer = image_model.layers[-2].output  #获取ResNet的倒数第二层(池化前的卷积结果)

    image_features_extract_model = tf.keras.Model(new_input, hidden_layer)

    #对文件目录去重
    encode_train = sorted(set(img_filename))

    #图片数据集
    image_dataset = tf.data.Dataset.from_tensor_slices(encode_train).map(
        load_image).batch(batchsize)

    for img, path in image_dataset:
        batch_features = image_features_extract_model(img)
        print(batch_features.shape)
        batch_features = tf.reshape(
            batch_features,
            (batch_features.shape[0], -1, batch_features.shape[3]))
        print(batch_features.shape)

        for bf, p in zip(batch_features, path):
            path_of_feature = p.numpy().decode("utf-8")
            np.save(numpyPATH + path_of_feature, bf.numpy())
def Mildnet_resnet():
    model = ResNet50(include_top=False, weights='imagenet', input_shape=(224, 224, 3), pooling='avg')

    for layer in model.layers[:143]:
        layer.trainable = False

    intermediate_layer_outputs = get_layers_output_by_name(model, ['activation_46', 'activation_43'])
    convnet_output = model.output
    for layer_name, output in intermediate_layer_outputs.items():
        output = GlobalAveragePooling2D()(output)
        convnet_output = concatenate([convnet_output, output])

    convnet_output = Dense(2048, activation='relu')(convnet_output)
    convnet_output = Dropout(0.6)(convnet_output)
    convnet_output = Dense(2048, activation='relu')(convnet_output)
    convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))(convnet_output)

    final_model = tf.keras.models.Model(inputs=model.input, outputs=convnet_output)

    return final_model
Ejemplo n.º 24
0
def create_model(input_shape, n_classes, weights_path, freeze_convnet):
    convnet = ResNet50(
        include_top=False, weights='imagenet', input_shape=input_shape)
    x = convnet.output
    x = Flatten()(x)
    x = Dense(n_classes, activation='softmax', name='out')(x)
    model = Model(inputs=convnet.input, outputs=x)

    # load weights
    if type(weights_path) is str:
        model.load_weights(weights_path, by_name=True)
    elif type(weights_path) is list:
        for p in weights_path:
            model.load_weights(p, by_name=True)

    if freeze_convnet:
        print('Freeze convolutional layers')
        for layer in convnet.layers:
            layer.trainable = False

    return model
Ejemplo n.º 25
0
 def get_model(self, model_name, weights):
     if model_name == 'InceptionV3':
         from tensorflow.python.keras.applications.inception_v3 import InceptionV3
         base_model = InceptionV3(weights=weights, include_top=False)
     elif model_name == 'NASNetLarge':
         from tensorflow.python.keras.applications.nasnet import NASNetLarge
         base_model = NASNetLarge(weights=weights, include_top=False)
     elif model_name == 'DenseNet201':
         from tensorflow.python.keras.applications.densenet import DenseNet201
         base_model = DenseNet201(weights=weights, include_top=False)
     elif model_name == 'Xception':
         from tensorflow.python.keras.applications.xception import Xception
         base_model = Xception(weights=weights, include_top=False)
     elif model_name == 'VGG16':
         from tensorflow.python.keras.applications.vgg16 import VGG16
         base_model = VGG16(weights=weights, include_top=False)
     elif model_name == 'VGG19':
         from tensorflow.python.keras.applications.vgg19 import VGG19
         base_model = VGG19(weights=weights, include_top=False)
     elif model_name == 'NASNetMobile':
         from tensorflow.python.keras.applications.nasnet import NASNetMobile
         base_model = NASNetMobile(weights=weights, include_top=False)
     elif model_name == 'MobileNetV2':
         from tensorflow.python.keras.applications.mobilenet_v2 import MobileNetV2
         base_model = MobileNetV2(weights=weights, include_top=False)
     elif model_name == 'ResNet50':
         from tensorflow.python.keras.applications.resnet50 import ResNet50
         base_model = ResNet50(weights=weights, include_top=False)
     elif model_name == 'InceptionResNetV2':
         from tensorflow.python.keras.applications.inception_resnet_v2 import InceptionResNetV2
         base_model = InceptionResNetV2(weights=weights, include_top=False, )
     else:
         raise KeyError('Unknown network.')
     x = base_model.output
     x = GlobalAveragePooling2D()(x)
     x = Dense(512, activation='relu')(x)
     predictions = Dense(1, activation='sigmoid')(x)
     model = Model(inputs=base_model.input, outputs=predictions)
     return model
Ejemplo n.º 26
0
 def get_model(self, model_name, weights='imagenet'):
     if model_name == 'InceptionV3':
         from tensorflow.python.keras.applications.inception_v3 import InceptionV3
         base_model = InceptionV3(weights=weights, include_top=False)
     elif model_name == 'NASNetLarge':
         from tensorflow.python.keras.applications.nasnet import NASNetLarge
         base_model = NASNetLarge(weights=weights, include_top=False)
     elif model_name == 'DenseNet201':
         from tensorflow.python.keras.applications.densenet import DenseNet201
         base_model = DenseNet201(weights=weights, include_top=False)
     elif model_name == 'Xception':
         from tensorflow.python.keras.applications.xception import Xception
         base_model = Xception(weights=weights, include_top=False)
     elif model_name == 'VGG19':
         from tensorflow.python.keras.applications.vgg19 import VGG19
         base_model = VGG19(weights=weights, include_top=False)
     elif model_name == 'NASNetMobile':
         from tensorflow.python.keras.applications.nasnet import NASNetMobile
         base_model = NASNetMobile(weights=weights, include_top=False)
     elif model_name == 'MobileNetV2':
         from tensorflow.python.keras.applications.mobilenet_v2 import MobileNetV2
         base_model = MobileNetV2(weights=weights, include_top=False)
     elif model_name == 'ResNet50':
         from tensorflow.python.keras.applications.resnet50 import ResNet50
         base_model = ResNet50(weights=weights, include_top=False)
     elif model_name == 'InceptionResNetV2':
         from tensorflow.python.keras.applications.inception_resnet_v2 import InceptionResNetV2
         base_model = InceptionResNetV2(weights=weights, include_top=False)
     else:
         raise KeyError('Unknown network.')
     x = base_model.output
     x = GlobalAveragePooling2D()(x)
     # x = Dense(1024, activation='relu')(x)
     output = Dense(1, activation='sigmoid')(x)
     # output = SiameseLossLayer(self.batch_size, self.num_distort, self.num_level)(x)
     model = Model(inputs=base_model.input, outputs=output)
     self.name = model_name
     return model
Ejemplo n.º 27
0
def get_model(batch_size, train=True):
    resnet = ResNet50(include_top=False,
                      weights=None,
                      input_tensor=None,
                      input_shape=None,
                      pooling=None)
    top_model = TopModel()

    input = Input(shape=[256, 256, 3], batch_size=batch_size)
    output = resnet(input)
    print("resnet output shape:", output.shape)
    out = top_model(output)
    prediction = out[0]
    heatmap = out[1]
    # set proper names for outputs
    prediction = Activation('linear', name="xyz")(prediction)
    heatmap = Activation('linear', name="mask")(heatmap)
    if train:
        model = tf.keras.models.Model(inputs=[input],
                                      outputs=[prediction, heatmap])
    else:
        model = tf.keras.models.Model(inputs=[input], outputs=[prediction])
    return model
def run_model(args):
    # Configure the memory optimizer
    #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
    config = tf.ConfigProto()
    config.graph_options.rewrite_options.memory_optimization = rewriter_config_pb2.RewriterConfig.SCHEDULING_HEURISTICS
    #config.gpu_options.allow_growth=True
    config.gpu_options.per_process_gpu_memory_fraction = 0.5
    K.set_session(tf.Session(config=config))

    num_classes = args.num_classes
    batch_size = args.batch_size

    model_name = args.model
    if model_name == 'ResNet50':
        model = ResNet50(weights=None,
                         include_top=True,
                         input_shape=input_shape,
                         classes=num_classes)
    elif model_name == 'ResNet101':
        model = keras.applications.resnet.ResNet101(weights=None,
                                                    include_top=True,
                                                    input_shape=input_shape,
                                                    classes=num_classes)
    elif model_name == 'ResNet152':
        model = ResNet152(weights=None,
                          include_top=True,
                          input_shape=input_shape,
                          classes=num_classes)
    elif model_name == 'VGG16':
        model = VGG16(weights=None,
                      include_top=True,
                      input_shape=input_shape,
                      classes=num_classes)
    elif model_name == 'VGG19':
        model = VGG19(weights=None,
                      include_top=True,
                      input_shape=input_shape,
                      classes=num_classes)
    elif model_name == 'Xception':
        model = Xception(weights=None,
                         include_top=True,
                         input_shape=input_shape,
                         classes=num_classes)
    elif model_name == 'MobileNet':
        model = MobileNet(weights=None,
                          include_top=True,
                          input_shape=input_shape,
                          classes=num_classes)
    elif model_name == 'MobileNetV2':
        model = MobilenetV2(weights=None,
                            include_top=True,
                            input_shape=input_shape,
                            classes=num_classes)
    elif model_name == 'InceptionV3':
        model = InceptionV3(weights=None,
                            include_top=True,
                            input_shape=input_shape,
                            classes=num_classes)
    else:
        print('Running with ResNet50 -- the default model')
        model = ResNet50(weights=None,
                         include_top=True,
                         input_shape=input_shape,
                         classes=num_classes)
    execute_model(model, input_shape)
    def fit(self,
            learning_rate=1e-4,
            epochs=5,
            activation='relu',
            dropout=0,
            hidden_size=1024,
            nb_layers=1,
            include_class_weight=False,
            batch_size=20,
            save_model=False,
            verbose=True,
            fine_tuning=False,
            NB_IV3_LAYERS_TO_FREEZE=279,
            use_TPU=False,
            transfer_model='Inception',
            min_accuracy=None,
            extract_SavedModel=False):

        if transfer_model in ['Inception', 'Xception', 'Inception_Resnet']:
            target_size = (299, 299)
        else:
            target_size = (224, 224)

        #We expect the classes to be the name of the folders in the training set
        self.categories = os.listdir(TRAIN_DIR)
        """
        helper functions to to build tensors
        inspired by https://www.tensorflow.org/tutorials/load_data/images
        """
        def prepare_image(img_path):
            #reshape the image
            image = Image.open(img_path)
            image = image.resize(target_size,
                                 PIL.Image.BILINEAR).convert("RGB")
            #convert the image into a numpy array, and expend to a size 4 tensor
            image = img_to_array(image)
            #rescale the pixels to a 0-1 range
            image = image.astype(np.float32) / 255
            return image

        def generate_tuples(img_folder):
            #loop through all the images
            # Get all file names of images present in folder
            classes = os.listdir(img_folder)
            classes_paths = [
                os.path.abspath(os.path.join(img_folder, i)) for i in classes
            ]
            x = []
            y = []

            for i, j in enumerate(classes):
                #for all the classes, get the list of pictures
                img_paths = os.listdir(classes_paths[i])
                img_paths = [
                    os.path.abspath(os.path.join(classes_paths[i], x))
                    for x in img_paths
                ]

                for img_path in img_paths:
                    x.append(prepare_image(img_path))
                    y = y + [i]

            return (np.array(x), np.array(y).astype(np.int32))

        #get training data
        (x_train,
         y_train) = generate_tuples(parentdir + '/data/image_dataset/train')
        (x_val, y_val) = generate_tuples(parentdir + '/data/image_dataset/val')

        #train input_function: see https://colab.research.google.com/drive/1F8txK1JLXKtAkcvSRQz2o7NSTNoksuU2#scrollTo=abbwQQfH0td3
        def get_training_dataset(batch_size=batch_size):
            # Convert the inputs to a Dataset.
            dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))

            # Shuffle, repeat, and batch the examples.
            dataset = dataset.shuffle(1000).repeat().batch(batch_size,
                                                           drop_remainder=True)

            return dataset

        def get_validation_dataset(batch_size=batch_size):
            # Convert the inputs to a Dataset.
            dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val))

            # Shuffle, repeat, and batch the examples.
            dataset = dataset.shuffle(1000).repeat().batch(batch_size,
                                                           drop_remainder=True)

            return dataset

        #if we want stop training when no sufficient improvement in accuracy has been achieved
        if min_accuracy is not None:
            callback = EarlyStopping(monitor='acc', baseline=min_accuracy)
            callback = [callback]
        else:
            callback = None

        #load the pretrained model, without the classification (top) layers
        if transfer_model == 'Xception':
            base_model = Xception(weights='imagenet',
                                  include_top=False,
                                  input_shape=(299, 299, 3))
        elif transfer_model == 'Inception_Resnet':
            base_model = InceptionResNetV2(weights='imagenet',
                                           include_top=False,
                                           input_shape=(299, 299, 3))
        elif transfer_model == 'Resnet':
            base_model = ResNet50(weights='imagenet',
                                  include_top=False,
                                  input_shape=(224, 224, 3))
        else:
            base_model = InceptionV3(weights='imagenet',
                                     include_top=False,
                                     input_shape=(299, 299, 3))

        #Add the classification layers using Keras functional API
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
        for _ in range(nb_layers):
            x = Dense(hidden_size, activation=activation)(
                x)  #Hidden layer for classification
            if dropout > 0:
                x = Dropout(rate=dropout)(x)

        predictions = Dense(len(self.categories),
                            activation='softmax')(x)  #Output layer
        model = Model(inputs=base_model.input, outputs=predictions)

        #Set only the top layers as trainable (if we want to do fine-tuning,
        #we can train the base layers as a second step)
        for layer in base_model.layers:
            layer.trainable = False

        #Define the optimizer and the loss, and compile the model
        loss = 'sparse_categorical_crossentropy'
        if use_TPU:
            #if we want to try out the TPU, it looks like we currently need to use
            #tensorflow optimizers...see https://stackoverflow.com/questions/52940552/valueerror-operation-utpu-140462710602256-varisinitializedop-has-been-marked
            #...and https://www.youtube.com/watch?v=jgNwywYcH4w
            optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
            model.compile(optimizer=optimizer,
                          loss=sparse_softmax_cross_entropy,
                          metrics=['acc'])

            TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR']
            model = tf.contrib.tpu.keras_to_tpu_model(
                model,
                strategy=tf.contrib.tpu.TPUDistributionStrategy(
                    tf.contrib.cluster_resolver.TPUClusterResolver(
                        TPU_WORKER)))
            tf.logging.set_verbosity(tf.logging.INFO)

        else:
            optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
            model.compile(optimizer=optimizer, loss=loss, metrics=['acc'])

        #if we want to weight the classes given the imbalanced number of images
        if include_class_weight:
            from sklearn.utils.class_weight import compute_class_weight
            cls_train = self.categories
            class_weight = compute_class_weight(class_weight='balanced',
                                                classes=np.unique(cls_train),
                                                y=cls_train)
        else:
            class_weight = None

        steps_per_epoch = int(
            sum([
                len(files)
                for r, d, files in os.walk(parentdir +
                                           '/data/image_dataset/train')
            ]) / batch_size)
        validation_steps = int(
            sum([
                len(files)
                for r, d, files in os.walk(parentdir +
                                           '/data/image_dataset/val')
            ]) / batch_size)

        #Fit the model
        if use_TPU:
            history = model.fit(get_training_dataset,
                                steps_per_epoch=steps_per_epoch,
                                epochs=epochs,
                                validation_data=get_validation_dataset,
                                validation_steps=validation_steps,
                                verbose=verbose,
                                callbacks=callback,
                                class_weight=class_weight)
        else:
            history = model.fit(get_training_dataset(),
                                steps_per_epoch=steps_per_epoch,
                                epochs=epochs,
                                validation_data=get_validation_dataset(),
                                validation_steps=validation_steps,
                                verbose=verbose,
                                callbacks=callback,
                                class_weight=class_weight)

        #Fine-tune the model, if we wish so
        if fine_tuning and not model.stop_training:
            print('============')
            print('Begin fine-tuning')
            print('============')

            #declare the first layers as trainable
            for layer in model.layers[:NB_IV3_LAYERS_TO_FREEZE]:
                layer.trainable = False
            for layer in model.layers[NB_IV3_LAYERS_TO_FREEZE:]:
                layer.trainable = True

            model.compile(optimizer=tf.train.AdamOptimizer(
                learning_rate=learning_rate * 0.1),
                          loss=loss,
                          metrics=['acc'])

            #Fit the model
            if use_TPU:
                history = model.fit(get_training_dataset,
                                    steps_per_epoch=steps_per_epoch,
                                    epochs=epochs,
                                    validation_data=get_validation_dataset,
                                    validation_steps=validation_steps,
                                    verbose=verbose,
                                    callbacks=callback,
                                    class_weight=class_weight)
            else:
                history = model.fit(get_training_dataset(),
                                    steps_per_epoch=steps_per_epoch,
                                    epochs=epochs,
                                    validation_data=get_validation_dataset(),
                                    validation_steps=validation_steps,
                                    verbose=verbose,
                                    callbacks=callback,
                                    class_weight=class_weight)

        #Evaluate the model, just to be sure
        self.fitness = history.history['val_categorical_accuracy'][-1]

        #Save the model
        if save_model:
            if not os.path.exists(parentdir + '/data/trained_models'):
                os.makedirs(parentdir + '/data/trained_models')
            model.save(parentdir + '/data/trained_models/trained_model.h5')
            print('Model saved!')

        #save model in production format
        if extract_SavedModel:
            export_path = "./image_classifier/1/"

            with K.get_session() as sess:
                tf.saved_model.simple_save(
                    sess,
                    export_path,
                    inputs={'input_image': model.input},
                    outputs={t.name: t
                             for t in model.outputs})

        else:
            self.model = model
            del history
            del model
Ejemplo n.º 30
0
valid_datagen = ImageDataGenerator()
valid_batches = valid_datagen.flow_from_directory(DATASET_PATH + '/test1',
                                                  target_size=IMAGE_SIZE,
                                                  interpolation='bicubic',
                                                  class_mode='categorical',
                                                  shuffle=False,
                                                  batch_size=BATCH_SIZE)

# Output class index
for cls, idx in train_batches.class_indices.items():
    print('Class #{} = {}'.format(idx, cls))

net = ResNet50(include_top=False,
               weights='imagenet',
               input_tensor=None,
               input_shape=INPUT_SHAPE,
               classes=NUM_CLASSES)
x = net.output
x = Flatten()(x)

# add DropOut layer
x = Dropout(0.5)(x)

#Fully connected layer 1
fc1 = tf.keras.layers.Dense(100, activation='relu', name="AddedDense1")(x)

# Use softmax
output_layer = Dense(NUM_CLASSES, activation='softmax', name='softmax')(fc1)

net_final = Model(inputs=net.input, outputs=output_layer)