コード例 #1
0
    def load_model(self):
        input_layer = layers.Input(shape=(self.input_shape + (3, )))

        # Loading base model
        mobilenet = MobileNet(weights="imagenet",
                              input_tensor=input_layer,
                              alpha=0.5,
                              include_top=False)

        mobilenet.trainable = False
        x = layers.GlobalAveragePooling2D()(mobilenet.output)
        model = Model(inputs=input_layer, outputs=x, name='pose_model')

        return model
コード例 #2
0
def mobile_net(n_labels, input_shape):
    mobilenet_layer = MobileNet(weights='imagenet',
                                include_top=False,
                                input_shape=input_shape)
    mobilenet_layer.trainable = False

    inputs = Input(shape=input_shape)
    x = mobilenet_layer(inputs)
    x = Flatten()(x)
    x = Dense(256, activation='relu')(x)
    x = Dropout(0.2)(x)
    outputs = Dense(n_labels, activation='softmax')(x)

    return Model(inputs=inputs, outputs=outputs)
コード例 #3
0
def mobilenet_encoder(input_shape=[224, 224, 3]):
    mn = MobileNet(weights='imagenet',
                   include_top=False,
                   input_shape=input_shape)
    mn.trainable = False
    layer_names = [
        'conv_pw_1_relu',
        'conv_pw_3_relu',
        'conv_pw_5_relu',
        'conv_pw_11_relu',
        'conv_pw_13_relu',
    ]
    layers = [mn.get_layer(name).output for name in layer_names]
    down_stack = tf.keras.Model(inputs=mn.input, outputs=layers)
    down_stack.trainable = False
    return down_stack
コード例 #4
0
def ejer3_imagenet_fixed(n_epochs):
    input_image = Input(shape=(32, 32, 3))
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    x_train, y_train = preprocessing(x_train, y_train)
    x_test, y_test = preprocessing(x_test, y_test)

    model_keras = MobileNet(include_top=False,
                            weights="imagenet",
                            input_tensor=input_image)
    model_keras.trainable = False

    model = Sequential()
    model.add(model_keras)
    model.add(Flatten())
    model.add(Dropout(0.1))
    model.add(BatchNormalization())
    model.add(Dense(10, activation='softmax'))

    model.compile(loss=losses.CategoricalCrossentropy(),
                  optimizer=optimizers.Adam(learning_rate=0.00001),
                  metrics=[metrics.CategoricalAccuracy('acc')])

    model.summary()

    history = model.fit(x_train,
                        y_train,
                        validation_data=(x_test, y_test),
                        batch_size=50,
                        epochs=n_epochs,
                        verbose=1)

    acc, val_acc, loss, val_loss = plot_ejercicio(history)

    np.savetxt(
        "ejer3{}epochs{}_mobilenet_fixed.txt".format("_imagenet_", n_epochs),
        np.array([acc, val_acc, loss, val_loss]).T)
コード例 #5
0
from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(x, y, train_size = 0.8, random_state = 66, shuffle = True)

#control
image_size = (128, 128, 3)
bts = 32
optimizer = Adam(learning_rate = 0.001)

train_generator = idg.flow(x_train, y_train, batch_size = bts)
valid_generator = idg2.flow(x_val, y_val)
test_generator = idg2.flow(target)

#2. MODEL
from tensorflow.keras.applications import MobileNet
TF = MobileNet(weights="imagenet", include_top=False, input_shape = image_size)    
TF.trainable = True
x = TF.output
x = GlobalAveragePooling2D()(x)
x = Flatten()(x)
x = Dense(4096, activation='relu')(x)
x = Dropout(0.3)(x)

outputs = Dense(1000, activation='softmax')(x)
model = Model(inputs = TF.input, outputs = outputs)
model.summary()

#COMPILE   
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from tensorflow.train import Checkpoint, latest_checkpoint
model.compile(loss = 'categorical_crossentropy', optimizer = optimizer, metrics = ['acc'])
mc = ModelCheckpoint('C:/data/MC/best_LT_vision2_LT.hdf5', save_best_only=True, mode = 'auto')
コード例 #6
0
#MODEL:
#base=VGG16(include_top=False, weights='imagenet',input_shape=(input_size,input_size,3), pooling='avg')
#base = ResNet50(include_top=False, weights='imagenet', input_shape=(input_size,input_size,3), pooling='avg')

base = MobileNet(include_top=False,
                 weights='imagenet',
                 input_shape=(input_size, input_size, 3),
                 pooling='avg')
model = Sequential()
model.add(base)
model.add(Dropout(drop_rate))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(drop_rate))
model.add(Dense(102, activation='softmax'))

base.trainable = False

#pt = keras.optimizers.RMSprop()
opt2 = keras.optimizers.Adam(lr=0.001)
model.compile(optimizer=opt2,
              loss='categorical_crossentropy',
              metrics=['categorical_accuracy'])
model.summary()

#for i in range (len(base.layers)):
#    print (i,base.layers[i])

checkpointer = tf.keras.callbacks.ModelCheckpoint(
    'chkpnt_best.h5',
    save_best_only=True,
    mode='max',
コード例 #7
0
def create_model(opt,
                 metrics,
                 loss,
                 trainable_pretrained=True,
                 input_shape=(224, 224, 3)):
    old_model = MobileNet(input_shape=input_shape,
                          weights='imagenet',
                          include_top=False)
    old_model.trainable = trainable_pretrained

    original_image = Lambda(
        lambda x: x,
        name='original_image',
        # trainable=True
    )(old_model.input)

    x = old_model.output
    y_names = [
        "conv_pw_11_relu", "conv_pw_5_relu", "conv_pw_3_relu", "conv_pw_1_relu"
    ]
    f_nums = [1024, 64, 64, 64]
    ys = [
        Conv2D(f_num, kernel_size=1, name=f'skip_hair_conv_{i}')(
            old_model.get_layer(name=name).output)
        for i, (name, f_num) in enumerate(zip(y_names, f_nums))
    ] + [None]

    for i in range(5):
        y = ys[i]
        x = UpSampling2D(name=f'upsampling_hair_{i}')(x)
        if y is not None:
            x = Add(name=f'skip_hair_add_{i}')([x, y])
        x = DepthwiseConv2D(
            kernel_size=3,
            padding='same',
            name=f'depth_conv2d_hair_{i}',
            kernel_initializer=GlorotNormal(seed=(i + 1)),
        )(x)
        x = Conv2D(
            64,
            kernel_size=1,
            padding='same',
            name=f'conv2d_hair_{i}',
            kernel_regularizer=L2(2e-5),
            kernel_initializer=GlorotNormal(seed=11 * (i + 1)),
        )(x)
        x = ReLU(name=f'relu_hair_{i}')(x)
    x = Conv2D(
        # 1,
        2,
        kernel_size=1,
        padding='same',
        name='conv2d_hair_final',
        kernel_regularizer=L2(2e-5),
        kernel_initializer=GlorotNormal(seed=0))(x)
    x = Softmax(name='sigmoid_hair_final')(x)
    x = Concatenate()([x, original_image])
    # x = Activation('sigmoid', name='sigmoid_hair_final')(x)

    model = Model(old_model.input, x)
    if opt:
        model.compile(
            optimizer=opt,
            loss=loss,
            metrics=metrics,
        )
    return model
コード例 #8
0
                                                    x_col='file_name',
                                                    y_col='primary_posture',
                                                    batch_size=batch_size,
                                                    shuffle=True,
                                                    target_size=(IMG_HEIGHT,
                                                                 IMG_WIDTH),
                                                    class_mode='categorical',
                                                    subset='validation')

#MobileNet pretrained on imagenet

from tensorflow.keras.applications import EfficientNetB7, MobileNet
base_model = MobileNet(
    weights='imagenet', input_shape=(224, 224, 3), include_top=False
)  #imports the mobilenet model and discards the last 1000 neuron layer.
base_model.trainable = True
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(
    x
)  #we add dense layers so that the model can learn more complex functions and classify for better results.
x = Dense(1024, activation='relu')(x)  #dense layer 2
x = Dense(512, activation='relu')(x)  #dense layer 3
preds = Dense(3, activation='softmax')(x)  #final layer with softmax activation
model = Model(inputs=base_model.input, outputs=preds)
model.compile(optimizer='Adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.summary()

#Inception-V3 pretrained on imagenet
# Parameters settings
lr = 1e-3
momentum = 0.9

# Hyperparameters
epochs = 20


# Using the pre-trained model: 

# Import the pre-trained model MobileNet and use the weights from ImageNet
base_model = MobileNet(
    weights='imagenet',                     # Load pre-trained weights on ImageNet
    input_shape=(img_size,img_size,3),      # Lower down the shape for the input images to (100, 100, 3)
    include_top=False)                      # Do not include the ImageNet classifier at the top.
base_model.trainable = False                # Freeze all layers in the base model

# Create a new model on top of the output of one (or several) layers from the base model

# Adding new FC Layers to the modified model
modified_MobileNet_output = base_model.output
modified_MobileNet_output = GlobalAveragePooling2D()(modified_MobileNet_output)
# modified_MobileNet_output = Dense(512, activation='relu')(modified_MobileNet_output)
# modified_MobileNet_output = Dropout(0.5)(modified_MobileNet_output)
modified_MobileNet_output = Dense(num_classes, activation='softmax')(modified_MobileNet_output)

# Create the new model by using Input and Output layers
new_MobileNet_model = Model(inputs=base_model.input, outputs=modified_MobileNet_output)

new_MobileNet_model.summary()
コード例 #10
0
def load_backbone(backbone_type="resnet50",
                  backbone_outputs=('C3', 'C4', 'C5', 'P6', 'P7'),
                  num_features=256):
    global BACKBONE_LAYERS
    inputs = Input((None, None, 3), name='images')
    if backbone_type.lower() == 'resnet50':
        preprocess = BackBonePreProcess(rgb=False,
                                        mean_shift=True,
                                        normalize=0)(inputs)
        model = ResNet50(input_tensor=preprocess, include_top=False)
    elif backbone_type.lower() == 'resnet50v2':
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=2)(inputs)
        resnet50v2, _ = Classifiers.get('resnet50v2')
        model = resnet50v2(input_tensor=preprocess,
                           include_top=False,
                           weights='imagenet')
    elif backbone_type.lower() == "resnet101v2":
        preprocess = BackBonePreProcess(rgb=True,
                                        mean_shift=False,
                                        normalize=2)(inputs)
        model = ResNet101V2(input_tensor=preprocess,
                            include_top=False,
                            backend=tf.keras.backend,
                            layers=tf.keras.layers,
                            models=tf.keras.models,
                            utils=tf.keras.utils)
    elif backbone_type.lower() == 'resnext50':
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=2)(inputs)
        model = ResNeXt50(input_tensor=preprocess, include_top=False)
    elif backbone_type.lower() == "seresnet50":
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=3)(inputs)
        seresnet50, _ = Classifiers.get('seresnet50')
        model = seresnet50(input_tensor=preprocess,
                           original_input=inputs,
                           include_top=False,
                           weights='imagenet')
    elif backbone_type.lower() == "seresnet34":
        preprocess = BackBonePreProcess(rgb=True,
                                        mean_shift=False,
                                        normalize=0)(inputs)
        seresnet34, _ = Classifiers.get('seresnet34')
        model = seresnet34(input_tensor=preprocess,
                           original_input=inputs,
                           include_top=False,
                           weights='imagenet')
    elif backbone_type.lower() == "seresnext50":
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=3)(inputs)
        seresnext50, _ = Classifiers.get('seresnext50')
        model = seresnext50(input_tensor=preprocess,
                            original_input=inputs,
                            include_top=False,
                            weights='imagenet')
    elif backbone_type.lower() == "vgg16":
        preprocess = BackBonePreProcess(rgb=False,
                                        mean_shift=True,
                                        normalize=0)(inputs)
        model = VGG16(input_tensor=preprocess, include_top=False)
    elif backbone_type.lower() == "mobilenet":
        preprocess = BackBonePreProcess(rgb=False,
                                        mean_shift=False,
                                        normalize=2)(inputs)
        model = MobileNet(input_tensor=preprocess,
                          include_top=False,
                          alpha=1.0)
    elif backbone_type.lower() == 'efficientnetb2':
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=3)(inputs)
        model = efn.EfficientNetB2(input_tensor=preprocess,
                                   include_top=False,
                                   weights='imagenet')
    elif backbone_type.lower() == 'efficientnetb3':
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=3)(inputs)
        model = efn.EfficientNetB3(input_tensor=preprocess,
                                   include_top=False,
                                   weights='imagenet')
    elif backbone_type.lower() == 'efficientnetb4':
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=3)(inputs)
        model = efn.EfficientNetB4(input_tensor=preprocess,
                                   include_top=False,
                                   weights='imagenet')
    else:
        raise NotImplementedError(
            f"backbone_type은 {BACKBONE_LAYERS.keys()} 중에서 하나가 되어야 합니다.")
    model.trainable = False

    # Block Layer 가져오기
    features = []
    for key, layer_name in BACKBONE_LAYERS[backbone_type.lower()].items():
        if key in backbone_outputs:
            layer_tensor = model.get_layer(layer_name).output
            features.append(Identity(name=key)(layer_tensor))

    if backbone_type.lower() == "mobilenet":
        # Extra Layer for Feature Extracting
        Z6 = ZeroPadding2D(((0, 1), (0, 1)),
                           name=f'P6_zeropadding')(features[-1])
        P6 = Conv2D(num_features, (3, 3),
                    strides=(2, 2),
                    padding='valid',
                    activation='relu',
                    name=f'P6_conv')(Z6)
        if 'P6' in backbone_outputs:
            features.append(Identity(name='P6')(P6))
        G6 = GroupNormalization(name=f'P6_norm')(P6)
        Z7 = ZeroPadding2D(((0, 1), (0, 1)), name=f'P7_zeropadding')(G6)
        P7 = Conv2D(num_features, (3, 3),
                    strides=(2, 2),
                    padding='valid',
                    activation='relu',
                    name=f'P7_conv')(Z7)
        if 'P7' in backbone_outputs:
            features.append(Identity(name=f'P7')(P7))
    else:
        P6 = Conv2D(num_features, (3, 3),
                    strides=(2, 2),
                    padding='same',
                    activation='relu',
                    name=f'P6_conv')(features[-1])
        if 'P6' in backbone_outputs:
            features.append(Identity(name=f'P6')(P6))
        G6 = GroupNormalization(name=f'P6_norm')(P6)
        P7 = Conv2D(num_features, (3, 3),
                    strides=(2, 2),
                    padding='same',
                    activation='relu',
                    name=f'P7_conv')(G6)
        if 'P7' in backbone_outputs:
            features.append(Identity(name=f'P7')(P7))

    return Model(inputs, features, name=backbone_type)