예제 #1
0
def main():

    import os
    os.environ['CUDA_VISIBLE_DEVICES'] = str(3)

    from netgraph.nx.netgraph import NetGraph, HDF5Graph

    import numpy as np
    import pandas as pd
    # import holoviews as hv
    import networkx as nx
    from holoviews import opts
    import matplotlib.pyplot as plt
    from tqdm.autonotebook import tqdm

    from keras import backend as K

    
    

    

    from keras.preprocessing import image
    from keras.applications.resnet50 import preprocess_input, decode_predictions


    from pathlib import Path


    input_name = 'input_1'
    image_size = (64, 64)

    from keras.applications.resnet import ResNet50
    net = ResNet50(input_shape=(*image_size, 3), include_top=False)

    imagenet_dir = Path('/media/disk1/anerinovsky/datasets/fvs/imagenette-320/')

    

    img = image.load_img(imagenet_dir/'val'/'n01440764'/'ILSVRC2012_val_00000293.JPEG', 
        # target_size=(224, 224),
        target_size=image_size
    )
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    sess = K.get_session()

    ng = NetGraph(sess=sess, 
        feed_dict={ sess.graph.get_tensor_by_name(input_name + ':0') : x }, 
        graph=HDF5Graph(
            file='~/data/ng/resnet50.hdf5',
            node_attrs=['activation', 'tensor_name'],
            edge_attrs=['comp_weight', 'mult_weight']
        ))

    from netgraph.nx.nets.resnet import ResNet50
    ResNet50(ng)
예제 #2
0
def extract_feature(X, cnn_arch="resnet50"):
    if cnn_arch == "resnet50":
        from keras.applications.resnet import preprocess_input
        cnn = ResNet50(include_top=False, weights='imagenet')

    elif cnn_arch == "resnet50v2":
        from keras.applications.resnet_v2 import preprocess_input
        cnn = ResNet50V2(include_top=False, weights='imagenet')
    elif cnn_arch == "inceptionv3":
        from keras.applications.inception_v3 import preprocess_input
        cnn = InceptionV3(include_top=False, weights='imagenet')
        X = np.array([resize(X, (299, 299, 3)) for x in X])
        import ipdb
        ipdb.set_trace()
    else:
        raise ValueError(f"Not supported cnn_arch {cnn_arch}")

    input = Input(shape=X.shape[1:], name='image_input')
    x = cnn(input)
    x = Flatten()(x)
    model = Model(inputs=input, outputs=x)

    X = preprocess_input(X)

    return model.predict(X, batch_size=64)
    def resnet(self, epochs=100):

        model = ResNet50(include_top=False,
                         input_shape=(self._height, self._width, 3))
        last_layer = model.output

        x = Flatten(name='flatten')(last_layer)
        out = Dense(self._nb_classes, activation='softmax', name='fc8')(x)
        train_model = Model(model.input, out)
        train_model.compile(loss='categorical_crossentropy',
                            optimizer=Adam(lr=1e-5),
                            metrics=['accuracy'])

        steps_per_epoch = self._train_genrator.n // self._batch_size
        validation_steps = self._vali_generator.n // self._batch_size

        #callbacks =[self._checkpoint, self._monitor]
        callbacks = [self._checkpoint]

        train_model.fit_generator(self._train_genrator,
                                  steps_per_epoch=steps_per_epoch,
                                  epochs=epochs,
                                  validation_data=self._vali_generator,
                                  validation_steps=validation_steps,
                                  callbacks=callbacks)
예제 #4
0
def extract_features(imgs_np,
                     pretrained_model="resnet50",
                     pooling_method='avg'):
    print('Input images shape: ', imgs_np.shape)
    pretrained_model = pretrained_model.lower()
    assert pretrained_model in ['resnet50', 'inception_v3', 'vgg19']
    assert pooling_method in ['avg', 'max']

    model_args = {
        'weights': 'imagenet',
        'include_top': False,
        'input_shape': imgs_np[0].shape
    }

    if pretrained_model == "resnet50":
        base = ResNet50(**model_args)
        from keras.applications.resnet50 import preprocess_input
    elif pretrained_model == "inception_v3":
        base = InceptionV3(**model_args)
        from keras.applications.inception_v3 import preprocess_input
    elif pretrained_model == "vgg19":
        base = VGG19(**model_args)
        from keras.applications.vgg19 import preprocess_input

    feat_extractor = create_feat_extractor(base, pooling_method=pooling_method)

    imgs_np = preprocess_input(imgs_np)
    embeddings_np = feat_extractor.predict(imgs_np)
    print('Features shape: ', embeddings_np.shape)

    return embeddings_np
def create_nn(num_classes):
    print("CREATING MODEL: "+config.model_name)

    model = Sequential(name=config.model_name)
    
    if config.model_name == 'resnet50':
        model.add(ResNet50(pooling='avg', weights='imagenet'))  # input_shape = (224,224,3)
        opt = Adam(lr=0.0001)
    if config.model_name == 'inceptionv3':
        model.add(InceptionV3(pooling='avg', weights='imagenet'))  # input_shape = (299,299,3)
        opt = Adam(lr=0.0001)
    if config.model_name == 'mobilenetv2':
        model.add(MobileNetV2(pooling='avg', weights='imagenet'))  # input_shape = (224,224,3)
        opt = Adam(lr=0.0001)
    if config.model_name == 'nasnetmobile':
        model.add(NASNetMobile(pooling='avg', weights='imagenet'))  # input_shape = (224,224,3)
        opt = Adam(lr=0.0001)

    model.add(Dropout(0.2))
    model.add(Dense(500, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(250, activation='relu'))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())
    model.add(Dense(num_classes, activation='softmax'))

#    model.layers[0].trainable = False
    
    model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])

    model.summary()

    return model
예제 #6
0
파일: _1_0_CNN_OS.py 프로젝트: SpecDI/cs407
def cnn(input_shape, kernel_shape, pool_shape, classes):
    """
    Model definition. Has the following features:
    - Transfer Learning
    """
    embedded_dims = 512
    resnet = ResNet50(input_shape=(FRAME_LENGTH, FRAME_WIDTH, CHANNELS),
                      weights="imagenet",
                      include_top=False,
                      pooling='avg')

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

    cnn = Model(inputs=resnet.input, outputs=resnet.output)
    cnn.summary()

    input_ = Input(shape=input_shape)
    x = TimeDistributed(cnn)(input_)
    x = Lambda(function=lambda x: K.mean(x, axis=1),
               output_shape=lambda shape: (shape[0], ) + shape[2:])(x)
    outputs = Dense(classes, name='output', activation='sigmoid')(x)

    model = Model(inputs=[input_], outputs=outputs)
    model.summary()

    return model
예제 #7
0
    def load_model(self):
        FACTOR = 0.70
        HEIGHT = 137
        WIDTH = 236
        HEIGHT_NEW = int(HEIGHT * FACTOR)
        WIDTH_NEW = int(WIDTH * FACTOR)
        HEIGHT_NEW = 224
        WIDTH_NEW = 224
        
        base_model=ResNet50(include_top=False, weights='imagenet', input_tensor=None, input_shape=(HEIGHT_NEW,WIDTH_NEW,3), pooling=None, classes=1000)
        # base_model.trainable=False
        x = base_model.output
        x = layers.GlobalAveragePooling2D()(x)
        
        grapheme_root = layers.Dense(168, activation = 'softmax', name = 'root')(x)
        vowel_diacritic = layers.Dense(11, activation = 'softmax', name = 'vowel')(x)
        consonant_diacritic = layers.Dense(7, activation = 'softmax', name = 'consonant')(x)

        model = Model(inputs=base_model.input,outputs = [grapheme_root, vowel_diacritic, consonant_diacritic])
        for layer in base_model.layers:
            layer.trainable = True
        model.compile(optimizer='adam', loss = {'root' : 'categorical_crossentropy', 
                    'vowel' : 'categorical_crossentropy', 
                    'consonant': 'categorical_crossentropy'},
                    loss_weights = {'root' : 0.5,
                            'vowel' : 0.25,
                            'consonant': 0.25},
                    metrics={'root' : 'accuracy', 
                    'vowel' : 'accuracy', 
                    'consonant': 'accuracy'})
        # print(model.summary())

        return model
예제 #8
0
    def load_model(self):
        
        base_model=ResNet50(include_top=False, weights='imagenet', input_tensor=None, input_shape=(137,236,3), pooling=None, classes=1000)
        # base_model.trainable=False
        x = base_model.output
        x = layers.GlobalAveragePooling2D()(x)
        x = layers.BatchNormalization()(x)
        x = layers.Dropout(0.3)(x)
        x = layers.Dense(256, activation='relu')(x)
        x = layers.BatchNormalization()(x)
        x = layers.Dropout(0.3)(x)
        grapheme_root = layers.Dense(168, activation = 'softmax', name = 'root')(x)
        vowel_diacritic = layers.Dense(11, activation = 'softmax', name = 'vowel')(x)
        consonant_diacritic = layers.Dense(7, activation = 'softmax', name = 'consonant')(x)

        model = Model(inputs=base_model.input,outputs = [grapheme_root, vowel_diacritic, consonant_diacritic])
        for layer in base_model.layers:
            layer.trainable = True
        model.compile(optimizer='adam', loss = {'root' : 'categorical_crossentropy', 
                    'vowel' : 'categorical_crossentropy', 
                    'consonant': 'categorical_crossentropy'},
                    loss_weights = {'root' : 0.5,
                            'vowel' : 0.25,
                            'consonant': 0.25},
                    metrics={'root' : 'accuracy', 
                    'vowel' : 'accuracy', 
                    'consonant': 'accuracy'})
        # print(model.summary())

        return model
def load_pretrained_ResNet50(lay_of_interest='avg_pool',
                             trainable_after_layer=17):

    # Todo
    model = ResNet50(include_top=True, weights='imagenet')
    transfer_layer = model.get_layer(lay_of_interest)
    resnet50_model = Model(inputs=model.input, outputs=transfer_layer.output)
    for layer in resnet50_model.layers[0:trainable_after_layer]:
        layer.trainable = False

    return resnet50_model
def load_classification_model():
    global classification_model
    global graph
    global session
    session = get_session()
    init = tf.global_variables_initializer()
    session.run(init)
    graph = tf.get_default_graph()
    classification_model = ResNet50(weights="imagenet")
    # https://github.com/keras-team/keras/issues/6124
    classification_model._make_predict_function()
예제 #11
0
def transfer2(*, input_shape = None) -> Model:

    if input_shape is None:
        input_shape = (None, None, 3)

    resnet = ResNet50(
        include_top=False,
        weights='imagenet',
        input_shape=input_shape,

    )   # type: Model
    resnet.trainable = False

    model = Sequential()
    model.add(resnet)

    model.add(Conv2DTranspose(
        filters=512,
        kernel_size=3,
        strides=2,
        padding='same',
    ))
    model.add(BatchNormalization())
    model.add(Activation('relu'))

    model.add(Conv2DTranspose(
        filters=128,
        kernel_size=3,
        strides=2,
        padding='same',
    ))
    model.add(BatchNormalization())
    model.add(Activation('relu'))

    model.add(Conv2DTranspose(
        filters=32,
        kernel_size=3,
        strides=2,
        padding='same',
    ))
    model.add(BatchNormalization())
    model.add(Activation('relu'))

    model.add(Conv2D(
        filters=1,
        kernel_size=3,
        strides=1,
        padding='same',
    ))

    return model
예제 #12
0
def create_model(img_shape=(128, 64, 3), n_att=27, n_ids=1501):
    """
    INPUT
    """
    img_input = Input(shape=img_shape)
    cnn_backbone = ResNet50(include_top=False)(img_input)
    """
    FEATURES
    """
    features = Flatten()(cnn_backbone)
    features = Dense(512)(features)
    features = BatchNormalization()(features)
    features = Dropout(0.5)(features)
    features = Activation('relu', name='img_features')(features)
    """
    ATTRIBUTES
    """
    attributes = Dense(n_att, activation='sigmoid',
                       name='attributes_output')(features)
    """
    REWEIGHTING
    """
    attributes_r = Dense(n_att, activation='sigmoid')(attributes)
    attributes_r = Multiply()([attributes, attributes_r])
    """
    ID PREDICTION
    """
    ids = Concatenate()([features, attributes_r])
    ids = Dense(1024)(attributes)
    ids = BatchNormalization()(ids)
    ids = Dropout(0.5)(ids)
    ids = Activation('relu')(ids)

    ids = Dense(n_ids, activation='softmax', name='ids_output')(ids)
    """
    FULL MODEL
    """
    model = Model(inputs=img_input, outputs=[attributes, ids])
    losses = {
        'attributes_output': 'binary_crossentropy',
        'ids_output': 'categorical_crossentropy'
    }
    losses_weights = {'attributes_output': 0.1, 'ids_output': 0.9}
    optimizer = SGD(learning_rate=0.01)

    model.compile(optimizer=optimizer,
                  loss=losses,
                  loss_weights=losses_weights,
                  metrics=['accuracy'])

    return model
def cnn_model(model_name):
    """
    Model definition using Xception net architecture
    """
    if model_name == "xception":
        baseModel = Xception(weights="imagenet",
                             include_top=False,
                             input_shape=(160, 160, 3))
    elif model_name == "iv3":
        baseModel = InceptionV3(weights="imagenet",
                                include_top=False,
                                input_shape=(160, 160, 3))
    elif model_name == "irv2":
        baseModel = InceptionResNetV2(weights="imagenet",
                                      include_top=False,
                                      input_shape=(160, 160, 3))
    elif model_name == "resnet":
        baseModel = ResNet50(weights="imagenet",
                             include_top=False,
                             input_shape=(160, 160, 3))

    headModel = baseModel.output
    headModel = MaxPooling2D(pool_size=(3, 3))(headModel)
    headModel = Flatten(name="flatten")(headModel)
    headModel = Dense(512, activation="relu",
                      kernel_initializer="he_uniform")(headModel)
    headModel = Dense(512, activation="relu",
                      kernel_initializer="he_uniform")(headModel)
    headModel = Dropout(0.5)(headModel)
    headModel = Dense(512, activation="relu",
                      kernel_initializer="he_uniform")(headModel)
    headModel = Dropout(0.5)(headModel)
    predictions = Dense(2,
                        activation="softmax",
                        kernel_initializer="he_uniform")(headModel)
    model = Model(inputs=baseModel.input, outputs=predictions)

    for layer in baseModel.layers:
        layer.trainable = True

    optimizer = Nadam(lr=0.002,
                      beta_1=0.9,
                      beta_2=0.999,
                      epsilon=1e-08,
                      schedule_decay=0.004)
    model.compile(loss="categorical_crossentropy",
                  optimizer=optimizer,
                  metrics=["accuracy"])
    return model
예제 #14
0
def gen_resnet(img_height, img_width, num_classes):
    base_model = ResNet50(weights=None,
                          include_top=False,
                          input_shape=(img_height, img_width, 1))

    x = base_model.layers[-1].output
    x = Conv2D(filters=64, kernel_size=(3, 3), activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Flatten()(x)
    x = Dense(256, activation='relu')(x)
    x = Dense(256, activation='relu')(x)
    x = Dense(256, activation='relu')(x)
    predictions = Dense(num_classes, activation='softmax')(x)

    return Model(inputs=base_model.input, outputs=predictions)
예제 #15
0
def make_encoder(input, name='resnet50', pretrained=True):
	if name == 'resnet18':
		from classification_models.keras import Classifiers
		ResNet18, _ = Classifiers.get('resnet18')
		model = ResNet18(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	elif name == 'resnet50':
		from keras.applications.resnet import ResNet50
		model = ResNet50(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	elif name == 'resnet101':
		from keras.applications.resnet import ResNet101
		model = ResNet101(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	elif name == 'resnet152':
		from keras.applications.resnet import ResNet152
		model = ResNet152(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	elif name == 'vgg16':
		from keras.applications.vgg16 import VGG16
		model = VGG16(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	elif name == 'vgg19':
		from keras.applications.vgg19 import VGG19
		model = VGG19(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	else:
		raise Exception(f'unknown encoder {name}')

	return model
예제 #16
0
 def get_image_features(img_abs_path):
     '''
     Given the path where the image is stored,
     pass it through a ResNet50 imagenet trained model and
     return the features for the same
     '''
     model = ResNet50()                                                            # Instantiate a ResNet50 model
     model.layers.pop()                                                            # Remove the last layer of pretrained ImageNet data
     Image_Feature_Generator = Model(inputs = model.inputs, outputs = model.layers[-1].output)  # Redefine the model after removing the last layer
     img = load_img(img_abs_path, target_size = (224, 224))                        # Load the image from the path and resize to (224,224) for passing it through the ResNet pretrained model
     img = img_to_array(img)                                                       # Convert the image into a numpy array
     rows, columns, channels = img.shape                                           # Extract the dimensions of the image
     img = img.reshape((1, rows, columns, channels))                               # Redefine the image dimensions in a batch format
     img = preprocess_input(img)                                                   # Preprocess the input in order to bring it in a similar format as the imagenet preprocessing steps in original ResNet architecture
     features = Image_Feature_Generator.predict(img)                               # Generate features from the image
     return features
예제 #17
0
def get_model_body(input_tensor, net='vgg16', trainable=True):
    if net == config.network[0]:
        pre_model = VGG16(input_tensor=input_tensor, include_top=False)
    elif net == config.network[1]:
        pre_model = ResNet50(input_tensor=input_tensor, include_top=False)
    elif net == config.network[2]:
        pre_model = InceptionV3(input_tensor=input_tensor, include_top=False)
    else:
        raise ValueValidException('%s not defined yet, please choose another' %
                                  net)
    # 默认是不可以训练的  如果需要训练可以放开
    if not trainable:
        for layer in pre_model.layers:
            layer.trainable = False
    model = Model(inputs=pre_model.input,
                  outputs=pre_model.get_layer(index=-2).output)
    return model
예제 #18
0
def generate_xBD_baseline_model(dire):
    #weights = dire+'/resnet50_weight.h5'
    inputs = Input(shape=(128, 128, 3))
    base_model = ResNet50(include_top=False,
                          weights=dire + '/resnet50_weight.h5')
    for layer in base_model.layers:
        layer.trainable = False

    x = Conv2D(32, (5, 5),
               strides=(1, 1),
               padding='same',
               activation='relu',
               input_shape=(128, 128, 3))(inputs)
    x = MaxPooling2D(pool_size=(2, 2),
                     strides=None,
                     padding='valid',
                     data_format=None)(x)

    x = Conv2D(64, (3, 3), strides=(1, 1), padding='same',
               activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2),
                     strides=None,
                     padding='valid',
                     data_format=None)(x)

    x = Conv2D(64, (3, 3), strides=(1, 1), padding='same',
               activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2),
                     strides=None,
                     padding='valid',
                     data_format=None)(x)

    x = Flatten()(x)

    base_resnet = base_model(inputs)
    base_resnet = Flatten()(base_resnet)

    concated_layers = concatenate([x, base_resnet])

    concated_layers = Dense(2024, activation='relu')(concated_layers)
    concated_layers = Dense(524, activation='relu')(concated_layers)
    concated_layers = Dense(124, activation='relu')(concated_layers)
    output = Dense(4, activation='softmax')(concated_layers)

    model = Model(inputs=inputs, outputs=output)
    return model
예제 #19
0
def create_model(input_shape=(92, 92, 3),
                 n_radiomics=len(r_train[0]),
                 n_clinicals=len(c_train[0])):
    """
    SCANS
    """
    scans_input = ResNet50(input_shape=input_shape, include_top=False)
    scans_features = Flatten()(scans_input.output)
    scans_features = Dropout(0.5)(scans_features)
    scans_features = Dense(64)(scans_features)
    scans_features = BatchNormalization()(scans_features)
    scans_features = Activation('relu')(scans_features)
    """
    RADIOMICS DATA
    """
    rad_input = Input(shape=(n_radiomics, ))
    rad_features = Dense(n_radiomics)(rad_input)
    rad_features = BatchNormalization()(rad_features)
    rad_features = Dropout(0.5)(rad_features)
    rad_features = Activation('relu')(rad_features)
    """
    CLINICALS DATA
    """
    cl_input = Input(shape=(n_clinicals, ))
    cl_features = Dense(n_clinicals)(cl_input)
    cl_features = BatchNormalization()(cl_features)
    cl_features = Dropout(0.5)(cl_features)
    cl_features = Activation('relu')(cl_features)

    hazard = Concatenate()([scans_features, rad_features, cl_features])
    hazard = Dropout(0.2)(hazard)
    hazard = Dense(64)(hazard)
    hazard = BatchNormalization()(hazard)
    hazard = Activation('relu')(hazard)
    hazard = Dense(10)(hazard)
    model_output = Dense(1)(hazard)
    model_output = Activation('linear')(model_output)

    model = Model(inputs=[scans_input.input, rad_input, cl_input],
                  outputs=model_output)
    model.compile(optimizer='rmsprop',
                  loss=negative_log_likelihood(events_train))

    model.summary()

    return model
예제 #20
0
def build_resnet_model(initializer=None):
    base_model = ResNet50(include_top=False,
                          pooling='max',
                          weights="imagenet",
                          input_shape=(224, 224, 3))
    model = Sequential()
    model.add(base_model)
    model.add(Flatten())
    model.add(Dropout(0.2))
    model.add(Dense(256, activation='relu', kernel_initializer=initializer))
    model.add(Dropout(0.2))
    model.add(Dense(6, activation='softmax', kernel_regularizer=l1(0.01)))
    model.summary()
    opt = SGD(lr=0.001, momentum=0.6)
    model.compile(optimizer=opt,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    return model
예제 #21
0
def prep_model(weight=None):
    """
    Loads a model for tile encoding. This function
    can optionnaly take a different weight path to load.
    Parameters
    ----------
    weight: string, 
        path to weight folder
    Returns
    -------
    A keras model to encode.
    """
    shape = (224, 224, 3)
    model = ResNet50(include_top=False,
                     weights="imagenet",
                     input_shape=shape,
                     pooling='avg')
    if weight != "imagenet":
        print('loading')
        model.load_weights(weight, by_name=True)
    return model
예제 #22
0
def UNetFromResNet(input_shape=(256, 256, 3), n_classes=3):
    #This models a decoder block
    def DecoderBlock(filters, x, skip):

        x = UpSampling2D(size=2)(x)
        # print(skip.output_shape)
        x = Concatenate()([x, skip])

        x = Conv2D(filters, (3, 3), activation='relu', padding='same')(x)
        x = BatchNormalization()(x)

        x = Conv2D(filters, (3, 3), activation='relu', padding='same')(x)
        x = BatchNormalization()(x)

        return x

    backbone = ResNet50(input_shape=input_shape,
                        include_top=False,
                        weights='imagenet',
                        pooling='avg')
    model_input = backbone.input

    #We eliminate the last average pooling
    x = backbone.layers[-2].output

    #Layers were we are going to do skip connections.
    feature_layers = [142, 80, 38, 4, 0]
    filters = [1024, 512, 256, 64, 32]

    for i in range(len(feature_layers)):
        skip = backbone.layers[feature_layers[i]].output
        x = DecoderBlock(filters[i], x, skip)

    #Final Convolution
    x = Conv2D(n_classes, (3, 3), activation='sigmoid', padding='same')(x)

    model_output = x
    model = Model(model_input, model_output)

    return model
예제 #23
0
def ResNet(input_shape = (const.X_Height, const.X_Width, const.X_Channels), classes = const.Y_Classes, Layers = 50, source = 'keras', weights = 'imagenet'):
    """
    create RestNet model

    Arguments:
        input_shape = Height, Width and channels for each input image
        classes = how many classes model will be trainned on
        Layers: how many layers; should be one of [18, 34, 50, 101, 152]
        source: 'keras' (use built-in model) or 'manual' (use my custom model above)
        weights: 'imagenet' (load weights from keras lib) or None (no weights loading) 
            'imagenet' only available if layers in [50,101,152]
    """

    # validate parameters
    if (Layers not in [18, 34, 50, 101, 152]):
        raise ValueError('Invalid layer number: ' + str(Layers) + ' (must be one of [18, 34, 50, 101, 152]).')

    if (source not in ['keras', 'manual']):
        raise ValueError('Invalid model source: ' + str(source) + " (must be 'keras' or 'manual'.")

    if (weights not in [None, 'imagenet']):
        raise ValueError('Invalid weights definition: ' + str(weights) + " (must be None or 'imagenet'.")

    if (Layers in [18, 34]):
        if (source == 'keras'):
            raise ValueError("No keras model available for small ResNets. 'source' parameter must be 'manual' when layers are 18 or 34.")

        if (weights != None):
            raise ValueError("No weights available for small ResNets. 'weights' Parameter must be None when layers are 18 or 34.")

    # build model
    if (source == 'keras'):
        # load base model from keras
        if (Layers == 50):
            from keras.applications.resnet import ResNet50
            baseModel = ResNet50(include_top = False, weights = weights, input_shape = input_shape)
        elif (Layers == 101):
            from keras.applications.resnet import ResNet101
            baseModel = ResNet101(include_top = False, weights = weights, input_shape = input_shape)
        elif (Layers == 152):
            from keras.applications.resnet import ResNet152
            baseModel = ResNet152(include_top = False, weights = weights, input_shape = input_shape)
    elif (source == 'manual'):
        # load model from my implementation
        if (Layers in [18,34]):
            baseModel = ResNetSmall(input_shape=input_shape, classes=classes, Layers=Layers)
        else:
            baseModel = ResNetLarge(input_shape=input_shape, classes=classes, Layers=Layers, weights=weights)

    # add final layers to built-in keras model
    from keras.models import Model
    from keras.layers import Dense, Flatten, AveragePooling2D

    X = baseModel.output
    X = AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)(X)
    X = Flatten()(X)
    Preds = Dense(const.Y_Classes, activation='softmax', name='fc' + str(const.Y_Classes))(X)

    model = Model(inputs=baseModel.input, outputs=Preds)

    # return the model
    return model
예제 #24
0
def load_model(name, classes=2, dropout=0.5, batch_size=6):
    if name == 'resnet50':
        basemodel = ResNet50(include_top=False, weights='imagenet', pooling='avg', classes=classes)
        # construct the head of the model that will be placed on top of the
        # the base model
        headModel = Dense(512, activation="relu")(basemodel.output)
        headModel = Dropout(0.5)(headModel)
        headModel = Dense(classes, activation="softmax")(headModel)
        model = Model(inputs=basemodel.input, outputs=headModel)
    elif name == 'resnet50conv':

        basemodel = ResNet50(include_top=False, weights='imagenet', pooling=None, classes=classes)
        headModel = Conv2D(512, (7,7), activation='relu')(basemodel.output)
        if dropout:
            headModel = Dropout(dropout)(headModel)
        headModel = Conv2D(512, (1,1), activation='relu')(headModel)
        if dropout:
            headModel = Dropout(dropout)(headModel)
        headModel = Conv2D(classes, (1,1), activation='softmax')(headModel)
        model = Model(inputs=basemodel.input, outputs=headModel)

    elif name == 'resnet50convflat':

        basemodel = ResNet50(input_shape=(),    
                             include_top=False, 
                             weights='imagenet',
                             pooling=None, 
                             classes=classes)
        headModel = Conv2D(512, (7,7), activation='relu')(basemodel.output)
        if dropout:
            headModel = Dropout(dropout)(headModel)
        headModel = Conv2D(512, (1,1), activation='relu')(headModel)
        if dropout:
            headModel = Dropout(dropout)(headModel)
        headModel = Conv2D(classes, (1,1), activation='softmax')(headModel)
        # headModel = Flatten()(headModel)
        headModel = Lambda(lambda x: x[:,0,0,:], input_shape=(batch_size, classes))(headModel)
        model = Model(inputs=basemodel.input, outputs=headModel)
        
    elif name == 'resnet50convflatfreeze':

        basemodel = ResNet50(input_shape=(),    
                             include_top=False, 
                             weights='imagenet',
                             pooling=None, 
                             classes=classes)
        headModel = Conv2D(512, (7,7), activation='relu')(basemodel.output)
        if dropout:
            headModel = Dropout(dropout)(headModel)
        headModel = Conv2D(512, (1,1), activation='relu')(headModel)
        if dropout:
            headModel = Dropout(dropout)(headModel)
        headModel = Conv2D(classes, (1,1), activation='softmax')(headModel)
        # headModel = Flatten()(headModel)
        headModel = Lambda(lambda x: x[:,0,0,:], input_shape=(batch_size, classes))(headModel)
        model = Model(inputs=basemodel.input, outputs=headModel)
        for layer in model.layers[4:-4]:
            layer.trainable = False
    elif name == 'custom_simple':
        pass

    return model 
    parser.add_argument(
        '--batch_size',
        type=int,
        default=64,
        help='Training batch size (larger batches are usually more efficient on GPUs)',
    )
    flags = parser.parse_args()
    return flags

flags = parse_args()

num_classes = 7 # number of classes
epochs = 20

model = Sequential()
resNet = ResNet50(include_top=False, pooling='avg', weights='imagenet') # pretrained model

# froze layers in resnet
if flags.layers_fine_tune != 0:
    layers_fine_tune = -flags.layers_fine_tune
    for layer in resNet.layers[:layers_fine_tune]:
        layer.trainable = False
else:
    for layer in resNet.layers:
        layer.trainable = False
model.add(resNet)
model.add(Dense(num_classes, activation='softmax')) # Classification layer

# Compiling the model
adam = Adam(lr=flags.learning_rate)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
	model = imagenet_model(base_model=mod)
	#train
	H = model.fit(
		trainX, trainY,
		validation_data=(testX, testY),
		epochs=EPOCHS,
		verbose=0)
	histories.append(H)

X,Y = next(imagenet_gen)
trainX, testX, trainY, testY = train_test_split(X, Y, test_size=0.25)

from keras.layers import GlobalAveragePooling2D
import tensorflow.keras.backend as K

baseModel = ResNet50(weights="imagenet", include_top=False,
input_tensor=Input(shape=(224, 224, 3)),layers=tf.keras.layers)

for layer in model.layers:
    if hasattr(layer, 'moving_mean') and hasattr(layer, 'moving_variance'):
        layer.trainable = True
        K.eval(K.update(layer.moving_mean, K.zeros_like(layer.moving_mean)))
        K.eval(K.update(layer.moving_variance, K.ones_like(layer.moving_variance)))
    else:
        layer.trainable = False

headModel = baseModel.output
headModel = AveragePooling2D()(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(2, activation="softmax")(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)
opt = Adam(lr=1e-5, decay=INIT_LR / EPOCHS)
예제 #27
0
def cnn_model(model_name, img_size):
    """
    Model definition using Xception net architecture
    """
    input_size = (img_size, img_size, 3)

    if model_name == "xception":
        print("Loading Xception wts...")
        baseModel = Xception(weights="imagenet",
                             include_top=False,
                             input_shape=(img_size, img_size, 3))
    elif model_name == "iv3":
        baseModel = InceptionV3(weights="imagenet",
                                include_top=False,
                                input_shape=(img_size, img_size, 3))
    elif model_name == "irv2":
        baseModel = InceptionResNetV2(weights="imagenet",
                                      include_top=False,
                                      input_shape=(img_size, img_size, 3))
    elif model_name == "resnet":
        baseModel = ResNet50(weights="imagenet",
                             include_top=False,
                             input_shape=(img_size, img_size, 3))
    elif model_name == "nasnet":
        baseModel = NASNetLarge(weights="imagenet",
                                include_top=False,
                                input_shape=(img_size, img_size, 3))
    elif model_name == "ef0":
        baseModel = EfficientNetB0(input_size,
                                   weights="imagenet",
                                   include_top=False)
    elif model_name == "ef5":
        baseModel = EfficientNetB5(input_size,
                                   weights="imagenet",
                                   include_top=False)

    headModel = baseModel.output
    headModel = GlobalAveragePooling2D()(headModel)
    headModel = Dense(512, activation="relu",
                      kernel_initializer="he_uniform")(headModel)
    headModel = Dropout(0.4)(headModel)
    # headModel = Dense(512, activation="relu", kernel_initializer="he_uniform")(
    #     headModel
    # )
    # headModel = Dropout(0.5)(headModel)
    predictions = Dense(5,
                        activation="softmax",
                        kernel_initializer="he_uniform")(headModel)
    model = Model(inputs=baseModel.input, outputs=predictions)

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

    optimizer = Nadam(lr=0.002,
                      beta_1=0.9,
                      beta_2=0.999,
                      epsilon=1e-08,
                      schedule_decay=0.004)
    model.compile(loss="categorical_crossentropy",
                  optimizer=optimizer,
                  metrics=["accuracy"])
    return model
예제 #28
0
from keras.optimizers import Adam
from keras.applications import NASNetLarge

#setting train, validation and test dataset path
train_data_path = 'sanket/train'
validation_data_path = 'sanket/valid'
test_data_path = 'sanket_test/test'

#Setting some parameters for training the model
batch_size = 8
NUM_CLASSES = 1
img_rows, img_cols = 512, 512
img_channels = 3

#loading Resnet50 from keras application with imagenet weights
base_model = ResNet50(include_top=False, weights="imagenet")

#adding few more layers to make the model work according to current problem statement
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = (Dropout(0.5))(x)

# let's add a fully-connected layer
x = Dense(512, activation='relu')(x)

predictions = Dense(1, activation='sigmoid')(x)

#join all the layers and create a fully connected layer
model = Model(inputs=base_model.input, outputs=predictions)

#Setting train, validation and test Image generator
예제 #29
0
print('y_train shape:', y_train.shape)

# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

if args.resume:
    print('resume from checkpoint')
    model = keras.models.load_model(save_file)
else:
    print('train from start')
    model = models.Sequential()

    if '50' in args_model:
        base_model = ResNet50(weights=None,
                              include_top=False,
                              input_shape=(32, 32, 3),
                              pooling=None)
    elif '101' in args_model:
        base_model = ResNet101(weights=None,
                               include_top=False,
                               input_shape=(32, 32, 3),
                               pooling=None)
    elif '152' in args_model:
        base_model = ResNet152(weights=None,
                               include_top=False,
                               input_shape=(32, 32, 3),
                               pooling=None)

    #base_model.summary()

    #pdb.set_trace()
broj_na_klasi = 133 # broj na klasi

"""# Create your model architecture"""

from keras.applications.xception import Xception
from keras.applications.densenet import DenseNet169
from keras.applications.resnet import ResNet50
from keras.models import Model
from keras.layers import Dense, Activation, Conv2D, Flatten, MaxPooling2D, AveragePooling2D, Dropout, GlobalAveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.models import Sequential

# YOUR ARCHITECTURE HERE
model = Sequential()

model = ResNet50(include_top=False, input_shape=(224,224,3), weights='imagenet')

#layer = model.get_layer('conv3_block1_1_conv')

av_pool = GlobalAveragePooling2D()(model.output)
class1 = Dense(490, activation='relu')(av_pool)
normaliz = BatchNormalization()(class1)
class2 = Dense(520, activation='relu')(normaliz)
normaliz1 = BatchNormalization()(class2)
outputs = Dense(133, activation='softmax')(normaliz1)

model = Model(inputs=model.inputs, outputs=outputs)

model.summary()

"""# Print summary of your model"""