def build_model(img_width, img_height):
    # Initializing weights with Imagenet weights
    mobilenet = applications.MobileNetV2(weights="imagenet",
                                         include_top=False,
                                         input_shape=(img_width, img_height,
                                                      3))

    # freezing the layers except last 6 layers
    for layer in mobilenet.layers[:-6]:
        layer.trainable = False

    #debugging
    for layer in mobilenet.layers:
        print(layer.name, layer.trainable)

    # Create the model
    model = models.Sequential()

    # Add the mobilenet convolutional base model
    model.add(mobilenet)

    # Add new layers
    model.add(layers.Flatten())
    model.add(layers.Dense(1024, activation='relu'))
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(6, activation='softmax'))

    return model
def collect_models():
    models = dict()
    models["MobileNetV2"] = tfapp.MobileNetV2(input_shape=IMG_SHAPE,
                                              include_top=False,
                                              weights='imagenet')
    models["NASNetMobile"] = tfapp.NASNetMobile(input_shape=(130, 386, 3),
                                                include_top=False,
                                                weights='imagenet')
    models["DenseNet121"] = tfapp.DenseNet121(input_shape=IMG_SHAPE,
                                              include_top=False,
                                              weights='imagenet')
    models["VGG16"] = tfapp.VGG16(input_shape=IMG_SHAPE,
                                  include_top=False,
                                  weights='imagenet')
    models["Xception"] = tfapp.Xception(input_shape=(134, 390, 3),
                                        include_top=False,
                                        weights='imagenet')
    models["ResNet50V2"] = tfapp.ResNet50V2(input_shape=IMG_SHAPE,
                                            include_top=False,
                                            weights='imagenet')
    models["NASNetLarge"] = tfapp.NASNetLarge(input_shape=(130, 386, 3),
                                              include_top=False,
                                              weights='imagenet')

    # omit non 2^n shape
    # models["InceptionV3"] = tfapp.InceptionV3(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')
    # models["InceptionResNetV2"] = \
    #     tfapp.InceptionResNetV2(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')
    return models
Esempio n. 3
0
File: apps.py Progetto: krtbb/faces
def app_net(archname='mobilenet', z_dim=128, input_shape=(160, 160, 3)):
    if archname == 'mobilenet':
        basemodel = app.MobileNetV2(input_shape=input_shape,
                                    include_top=False,
                                    weights='imagenet')
    elif archname == 'resnet50':
        basemodel = app.ResNet50(input_shape=input_shape,
                                 include_top=False,
                                 weights='imagenet')
    elif archname == 'resnet101':
        basemodel = app.ResNet101(input_shape=input_shape,
                                  include_top=False,
                                  weights='imagenet')
    elif archname == 'resnet152':
        basemodel = app.ResNet152(input_shape=input_shape,
                                  include_top=False,
                                  weights='imagenet')
    else:
        raise ValueError('Invalid archname: {}'.format(archname))

    gal = L.GlobalAveragePooling2D()
    dense = L.Dense(z_dim)

    inputs = tf.keras.Input(shape=input_shape)
    h = basemodel(inputs, training=True)
    h = gal(h)
    h = dense(h)
    outputs = tf.tanh(h)

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

    return model
Esempio n. 4
0
    def init_model(self, base_model, weights, output_dim):

        if base_model == "mobilenetv2":
            base = models.MobileNetV2(
                alpha=1.0,
                include_top=False,
                input_shape=(224, 224, 3),
                weights=weights,
                #classes=output_dim,
                #classifier_activation="softmax"
            )

        if base_model == "resnet50":
            base = models.resnet50(
                include_top=False,
                weights=weights,
                input_shape=(224, 224, 3),
                #classes=output_dim,
                #classifier_activation="softmax"
            )

        inputs = keras.Input(shape=(224, 224, 3))
        x = base(inputs, training=False)
        x = keras.layers.GlobalAveragePooling2D()(x)
        outputs = keras.layers.Dense(output_dim,
                                     activation=keras.activations.softmax)(x)
        #outputs = keras.layers.Softmax()(x)
        self.model = keras.Model(inputs, outputs)
    def build(self):
        """
        Build the model
        input shape: [batch_size, w, h, channels]
        """
        inputs = layers.Input(self.config.model.input_shape)
        x = inputs
        batch_norm = self.config.model.batch_norm

        base_model = applications.MobileNetV2(
            input_shape=self.config.model.input_shape,
            include_top=False,
            alpha=self.config.model.get("alpha", 1.))

        # Use the activations of these layers
        layer_names = [
            'block_1_expand_relu',
            'block_3_expand_relu',
            'block_6_expand_relu',
            'block_13_expand_relu',
            'block_16_project',
        ]
        outputs = [base_model.get_layer(name).output for name in layer_names]
        output_channels = [
            base_model.get_layer(name).output_shape[-1] for name in layer_names
        ]

        # Create the feature extraction model
        down_stack = models.Model(inputs=base_model.input,
                                  outputs=outputs,
                                  name="MobileNetV2")
        down_stack.trainable = False
        skips = down_stack(x)
        x = skips[-1]

        for i, (skip, k) in enumerate(
                zip(reversed(skips[:-1]), reversed(output_channels[:-1]))):
            upsample = _upconvolution(k,
                                      batch_norm=batch_norm,
                                      name=f"up_conv2d_{i}")
            x = upsample(x)
            concat = layers.Concatenate(axis=3)
            x = concat([skip, x])
            block = _upsample_block(k,
                                    batch_norm=batch_norm,
                                    name=f"upsampler_block_{i}",
                                    padding="same")
            x = block(x)

        output_upsample = _upconvolution(output_channels[0],
                                         batch_norm=batch_norm,
                                         name=f"up_conv2d_out")
        x = output_upsample(x)
        output_conv = layers.Conv2D(self.config.model.num_classes, 1)
        x = output_conv(x)

        flatten = layers.Reshape((-1, self.config.model.num_classes))
        outputs = flatten(x)
        self.model = models.Model(inputs=inputs, outputs=outputs)
Esempio n. 6
0
def mobilenet(img_size=(224,224), num_class=2, weights="imagenet", dtype=tf.float32):
    input_layer = layers.Input(shape=(img_size[0],img_size[1],3), dtype=dtype)
    base = models.MobileNetV2(input_tensor=input_layer, include_top=False, weights=weights)
    base.trainable = True
    x = base.output
    x = layers.GlobalAveragePooling2D()(x)
    x = layers.Dense(num_class)(x)
    preds = layers.Activation("softmax", dtype=tf.float32)(x)
    model = tf.keras.models.Model(inputs=input_layer, outputs=preds)
    return model
Esempio n. 7
0
def get_model(weights="imagenet"):
    input_tensor = Input(shape=(224, 224, 3))
    model = applications.MobileNetV2(
        weights=weights,
        input_tensor=input_tensor,
        input_shape=(224, 224, 3),
        include_top=False,
    )

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

    op0 = Flatten()(model.output)
    op1 = Dense(16, activation="relu")(op0)
    op2 = Dropout(0.5)(op1)

    output_tensor = Dense(3, activation="softmax")(op2)

    model = Model(inputs=input_tensor, outputs=output_tensor)
    return model
Esempio n. 8
0
 def load_pretrained_network():
     input_layer = layers.Input(shape=(IMG_HEIGHT,IMG_WIDTH,3,), name="input_layer")
     base = models.MobileNetV2(input_tensor=input_layer, include_top=False, weights='imagenet')
     base.trainable = True
     base_output = base.output
     vis_map = layers.Conv2D(filters=3, kernel_size=(1,1))(base_output)
     vis_up = layers.Conv2D(filters=1024, kernel_size=(1,1))(vis_map)
     x = layers.GlobalAveragePooling2D()(vis_up)
     x = layers.Dropout(0.2)(x)
     type_pred = layers.Dense(max_cat, name="type_pred", activation="softmax")(x)
     dep_pred = layers.Dense(max_cat, name="dep_pred", activation="softmax")(x)
     med_pred = layers.Dense(max_cat, name="med_pred", activation="softmax")(x)
     model = tf.keras.models.Model(inputs=input_layer,
                                   outputs=[type_pred, dep_pred, med_pred])
     opt = tf.keras.optimizers.SGD(learning_rate=0.01, decay=0.001)
     model.load_weights("./best_weights.h5")
     model.compile(loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),
                   optimizer=opt,
                   metrics=["accuracy"])
     @tf.function(experimental_compile=True)
     def forward(image):
         preds = model(tf.reshape(image, [1,IMG_WIDTH,IMG_HEIGHT,3]))
         return preds
     return model, forward
Esempio n. 9
0
    def load_model(self, train_data):
        print(f'norm_labels: {self.norm_labels}')
        if self.norm_labels:
            train_data[self.y_name] = (train_data[self.y_name] -
                                       self.mean_pt) / self.std_pt
        if self.lf_setting == 'weighted_mape':
            self.weight = train_data[self.y_name].max()

        if self.retrain is None:  # Train new model
            # Import Base Model from Bag of Models
            base_model = applications.MobileNetV2(
                input_shape=(self.model_img_width, self.model_img_height,
                             self.model_img_channels),
                weights=None,
                include_top=False)
            x = GlobalAveragePooling2D()(base_model.output)
            x = Dense(1280)(x)  # add a fully-connected layer
            x = Dense(1, name='predictions')(x)
            predictions = LeakyReLU(alpha=0.3)(x)

            # Create model
            model = Model(base_model.input, predictions)
            model.summary()

            sgd = SGD(lr=self.learning_rate,
                      decay=0.01,
                      momentum=0.9,
                      nesterov=True)
            model.compile(loss=self.__loss_function(), optimizer=sgd)
        else:  # Re-train model
            print(self.retrain)
            model = load_model(
                self.retrain,
                custom_objects={'weighted_mape': weighted_mape(250)})

        return model
Esempio n. 10
0
def estimate(X_train, y_train, back_bone):
    IMAGE_WIDTH = 224                               # Image width
    IMAGE_HEIGHT = 224                              # Image height
    input_shape = (IMAGE_WIDTH, IMAGE_HEIGHT, 3)    # (width, height, channel) channel = 3 ---> RGB
    batch_size = 8
    epochs = 1                                     # Number of epochs
    ntrain = 0.8 * len(X_train)                     # split data with 80/20 train/validation
    nval = 0.2 * len(X_train)
    back_bone = str(back_bone)
    print("Using " + back_bone + "...")
    X = []
    X_train = np.reshape(np.array(X_train), [len(X_train), ])

    for img in list(range(0, len(X_train))):

        if X_train[img].ndim >= 3:
            X.append(cv2.resize(
                X_train[img][:, :, :3], (IMAGE_WIDTH, IMAGE_HEIGHT), interpolation=cv2.INTER_CUBIC))
        else:
            smimg = cv2.cvtColor(X_train[img][0], cv2.COLOR_GRAY2RGB)
            X.append(cv2.resize(smimg, (IMAGE_WIDTH, IMAGE_HEIGHT),
                                interpolation=cv2.INTER_CUBIC))

        if y_train[img] == 'COVID':
            y_train[img] = 1
        elif y_train[img] == 'NonCOVID':
            y_train[img] = 0
        else:
            continue

    x = np.array(X)
    X_train, X_val, y_train, y_val = train_test_split(          # 20% validation set
        x, y_train, test_size=0.20, random_state=2)

    # data generator
    if back_bone == 'ResNet50V2' or back_bone =='1':
        train_datagen = ImageDataGenerator(
            preprocessing_function=resnet_preprocess,
            rotation_range=15,
            shear_range=0.1,
            zoom_range=0.2,
            horizontal_flip=True,
            width_shift_range=0.1,
            height_shift_range=0.1
        )

        val_datagen = ImageDataGenerator(preprocessing_function=resnet_preprocess)

    elif back_bone == 'Xception' or back_bone =='2':
        train_datagen = ImageDataGenerator(
            preprocessing_function=xception_preprocess,
            rotation_range=15,
            shear_range=0.1,
            zoom_range=0.2,
            horizontal_flip=True,
            width_shift_range=0.1,
            height_shift_range=0.1
        )

        val_datagen = ImageDataGenerator(preprocessing_function=xception_preprocess)
    elif back_bone == "DenseNet201" or back_bone =='3':
        train_datagen = ImageDataGenerator(
            preprocessing_function=denset_preprocess,
            rotation_range=15,
            shear_range=0.1,
            zoom_range=0.2,
            horizontal_flip=True,
            width_shift_range=0.1,
            height_shift_range=0.1
        )

        val_datagen = ImageDataGenerator(preprocessing_function=denset_preprocess)
    elif back_bone == "MobileNetV2" or back_bone == '4':
        train_datagen = ImageDataGenerator(
            preprocessing_function= mobile_preprocess,
            rotation_range=15,
            shear_range=0.1,
            zoom_range=0.2,
            horizontal_flip=True,
            width_shift_range=0.1,
            height_shift_range=0.1
        )

        val_datagen = ImageDataGenerator(preprocessing_function=mobile_preprocess)
    else:
        raise ValueError('Please select transfer learning model!')
    train_generator = train_datagen.flow(
        X_train, y_train, batch_size=batch_size, shuffle=True)
    val_generator = val_datagen.flow(
        X_val, y_val, batch_size=batch_size, shuffle=True)

    # model
    model = Sequential()
    if back_bone == 'ResNet50V2' or back_bone == '1':
        base_model = applications.resnet_v2.ResNet50V2(
            include_top=False, pooling='avg', weights='imagenet', input_shape=input_shape)
    elif back_bone == 'Xception' or back_bone == '2':
        base_model = applications.Xception(
            include_top=False, pooling='avg', weights='imagenet', input_shape=input_shape)
    elif back_bone == 'DenseNet201' or back_bone =='3':
        base_model = applications.DenseNet201(
            include_top=False, pooling='avg', weights='imagenet', input_shape=input_shape)
    elif back_bone == 'MobileNetV2' or back_bone =='4':
        base_model = applications.MobileNetV2(
            include_top=False, pooling='avg', weights='imagenet', input_shape=input_shape)
    else:
        raise ValueError('Please select transfer learning model!')

    base_model.trainable = False

    model.add(base_model)

    model.add(BatchNormalization())
    model.add(Flatten())

    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.1))

    model.add(Dense(64, activation='relu'))
    model.add(Dropout(0.2))

    model.add(Dense(1, activation='sigmoid'))

    model.compile(loss='binary_crossentropy',
                  optimizer=optimizers.Adam(lr=1e-4),
                  metrics=['acc'])

    # callbacks
    lr_reducer = ReduceLROnPlateau(
        monitor='val_loss',
        factor=0.8,
        patience=5,
        verbose=1,
        mode='auto',
        min_delta=0.0001,
        cooldown=3,
        min_lr=1e-14)

    best_loss_path = "Model.h5"
    ck_loss_model = ModelCheckpoint(
        best_loss_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min')

    callbacks = [ck_loss_model, lr_reducer]

    history = model.fit_generator(train_generator,
                                  steps_per_epoch=ntrain//batch_size,
                                  epochs=epochs,
                                  validation_data=val_generator,
                                  validation_steps=nval // batch_size,
                                  callbacks=callbacks)

    return model
Esempio n. 11
0
# load X, y data
X = np.load(f'./imgs_{IMAGE_SIZE}_3.npy', allow_pickle=True)
y = np.load(f'./labels_{IMAGE_SIZE}_3.npy', allow_pickle=True)
print(X.shape)
print(y.shape)

# transform the labels to binary representation so that we can train on the data
mlb = MultiLabelBinarizer()
y = mlb.fit_transform(y)

print(list(mlb.classes_))

X = applications.mobilenet_v2.preprocess_input(X)

base_model = applications.MobileNetV2(weights='imagenet',
                                      input_shape=(150, 150, 3),
                                      include_top=False)
base_model.trainable = False

inputs = layers.Input(shape=(150, 150, 3))
x = base_model(inputs, training=False)
x = layers.GlobalAveragePooling2D()(x)
x = layers.Flatten()(x)
x = layers.Dense(1024, activation='relu')(x)
outputs = layers.Dense(len(mlb.classes_), activation='sigmoid')(x)
model = models.Model(inputs, outputs)
model.compile(loss='binary_crossentropy',
              optimizer=optimizers.Adam(learning_rate=0.01),
              metrics=['accuracy'])

wandb.init(project="texture-classification",
Esempio n. 12
0
import tensorflow.compat.v1 as tf
import matplotlib.pyplot as plt

tf.disable_v2_behavior()
from tensorflow.keras import datasets, models, applications, layers, losses, optimizers, metrics

(train_images, train_labels), (test_images,
                               test_labels) = datasets.cifar10.load_data()
#image = prepare(arg)

base_model = applications.MobileNetV2(input_shape=train_images.shape[1:],
                                      include_top=False,
                                      weights='imagenet')
base_model.trainable = False
model = models.Sequential([base_model])

model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.summary()

model.compile(optimizer='adam',
              loss=losses.sparse_categorical_crossentropy,
              metrics=['accuracy'])

# graph the learning process
history = model.fit(train_images,
                    train_labels,
                    epochs=30,
                    validation_data=(test_images, test_labels))
Esempio n. 13
0
def create_model(input_shape):
    model = applications.MobileNetV2(
        weights=None, input_shape=input_shape, classes=512)
    return model
Esempio n. 14
0
def get_model(num_classes, model_name=MODEL_NAME, size_of_image=SIZE_OF_IMAGE):
    """
    Returns a CNN model, or None if the parameter model is not recognised.

    Parameters:
        num_classes (int): Total number of different classes of Pokemon.
        model (string): One of the 3 models (custom, MobileNetV2, VGG19). Default: config.MODEL.
        size_of_image (int): Size of image after resizing. Default: config.SIZE_OF_IMAGE.
    
    Returns:
        model (tf.keras.Model): A CNN model.
    """
    if model_name == 'custom':
        """ Custom CNN Model """
        model = models.Sequential()
        model.add(layers.Conv2D(32, (3, 3), activation='relu',
                                input_shape=(size_of_image, size_of_image, CHANNEL)))
        model.add(layers.MaxPooling2D((2, 2)))
        model.add(layers.Conv2D(64, (3, 3), activation='relu'))
        model.add(layers.MaxPooling2D((2, 2)))
        model.add(layers.Conv2D(128, (3, 3), activation='relu'))
        model.add(layers.MaxPooling2D((2, 2)))
        model.add(layers.Conv2D(256, (3, 3), activation='relu'))
        model.add(layers.MaxPooling2D((2, 2)))

        model.add(layers.Flatten())
        model.add(layers.Dense(196, activation='relu'))
        # model.add(layers.Dropout(0.3))
        model.add(layers.Dense(num_classes))
    elif model_name == 'MobileNetV2':
        """ MobileNetV2 """
        base_model = applications.MobileNetV2(input_shape=(size_of_image, size_of_image, CHANNEL),
                                              include_top=False,
                                              weights='imagenet')
        base_model.trainable = False
        global_average_layer = layers.GlobalAveragePooling2D()
        output_layer = layers.Dense(num_classes)

        model = tf.keras.Sequential([
            base_model,
            global_average_layer,
            layers.Flatten(),
            layers.Dense(196, activation='relu'),
            layers.Dropout(0.3),
            output_layer
        ])
    elif model_name == 'VGG19':
        """ VGG19 """
        base_model = applications.VGG19(input_shape=(size_of_image, size_of_image, CHANNEL),
                                        include_top=False,
                                        weights='imagenet')
        base_model.trainable = False
        global_average_layer = layers.GlobalAveragePooling2D()
        output_layer = layers.Dense(num_classes)

        model = tf.keras.Sequential([
            base_model,
            global_average_layer,
            layers.Flatten(),
            layers.Dense(196, activation='relu'),
            layers.Dropout(0.3),
            output_layer
        ])
    else:
        print("ERROR: Cannot recognise model.")
        sys.exit(1)

    return model
Esempio n. 15
0
    def encode(self, input_image):
        """
        :param input_image: (batch, height, width, channel)
        """
        input_shape = input_image.get_shape()
        height = input_shape[1]
        net_name = self.net_name
        weights = "imagenet" if self.pretrained_weight else None

        jsonfile = op.join(opts.PROJECT_ROOT, "model", "build_model",
                           "scaled_layers.json")
        output_layers = self.read_output_layers(jsonfile)
        out_layer_names = output_layers[net_name]

        if net_name == "MobileNetV2":
            from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
            pproc_img = layers.Lambda(lambda x: preprocess_input(x),
                                      name="preprocess_mobilenet")(input_image)
            ptmodel = tfapp.MobileNetV2(input_shape=input_shape,
                                        include_top=False,
                                        weights=weights)

        elif net_name == "NASNetMobile":
            from tensorflow.keras.applications.nasnet import preprocess_input
            assert height == 128

            def preprocess_layer(x):
                x = preprocess_input(x)
                x = tf.image.resize(x,
                                    size=NASNET_SHAPE[:2],
                                    method="bilinear")
                return x

            pproc_img = layers.Lambda(lambda x: preprocess_layer(x),
                                      name="preprocess_nasnet")(input_image)
            ptmodel = tfapp.NASNetMobile(input_shape=NASNET_SHAPE,
                                         include_top=False,
                                         weights=weights)

        elif net_name == "DenseNet121":
            from tensorflow.keras.applications.densenet import preprocess_input
            pproc_img = layers.Lambda(lambda x: preprocess_input(x),
                                      name="preprocess_densenet")(input_image)
            ptmodel = tfapp.DenseNet121(input_shape=input_shape,
                                        include_top=False,
                                        weights=weights)

        elif net_name == "VGG16":
            from tensorflow.keras.applications.vgg16 import preprocess_input
            pproc_img = layers.Lambda(lambda x: preprocess_input(x),
                                      name="preprocess_vgg16")(input_image)
            ptmodel = tfapp.VGG16(input_shape=input_shape,
                                  include_top=False,
                                  weights=weights)

        elif net_name == "Xception":
            from tensorflow.keras.applications.xception import preprocess_input
            assert height == 128

            def preprocess_layer(x):
                x = preprocess_input(x)
                x = tf.image.resize(x,
                                    size=XCEPTION_SHAPE[:2],
                                    method="bilinear")
                return x

            pproc_img = layers.Lambda(lambda x: preprocess_layer(x),
                                      name="preprocess_xception")(input_image)
            ptmodel = tfapp.Xception(input_shape=XCEPTION_SHAPE,
                                     include_top=False,
                                     weights=weights)

        elif net_name == "ResNet50V2":
            from tensorflow.keras.applications.resnet import preprocess_input
            pproc_img = layers.Lambda(lambda x: preprocess_input(x),
                                      name="preprocess_resnet")(input_image)
            ptmodel = tfapp.ResNet50V2(input_shape=input_shape,
                                       include_top=False,
                                       weights=weights)

        elif net_name == "NASNetLarge":
            from tensorflow.keras.applications.nasnet import preprocess_input
            assert height == 128

            def preprocess_layer(x):
                x = preprocess_input(x)
                x = tf.image.resize(x,
                                    size=NASNET_SHAPE[:2],
                                    method="bilinear")
                return x

            pproc_img = layers.Lambda(lambda x: preprocess_layer(x),
                                      name="preprocess_nasnet")(input_image)
            ptmodel = tfapp.NASNetLarge(input_shape=NASNET_SHAPE,
                                        include_top=False,
                                        weights=weights)
        else:
            raise WrongInputException("Wrong pretrained model name: " +
                                      net_name)

        # collect multi scale convolutional features
        layer_outs = []
        for layer_name in out_layer_names:
            layer = ptmodel.get_layer(name=layer_name[1], index=layer_name[0])
            # print("extract feature layers:", layer.name, layer.get_input_shape_at(0), layer.get_output_shape_at(0))
            layer_outs.append(layer.output)

        # create model with multi scale features
        multi_scale_model = tf.keras.Model(ptmodel.input,
                                           layer_outs,
                                           name=net_name + "_base")
        features_ms = multi_scale_model(pproc_img)
        return features_ms
Esempio n. 16
0
                required=True,
                help="path to the testing dataset folder")
args = vars(ap.parse_args())

import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras import applications
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D

classes = [1, 2, 3, 4, 5]

base_model = applications.MobileNetV2(
    weights=args["imagenet"],
    include_top=False,
)

x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(5, activation='softmax')(x)

model = Model(inputs=base_model.input, outputs=predictions)

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

traingen = image.ImageDataGenerator(
    rescale=1 / 255,
    horizontal_flip=True,
def model(input_size=(1024, 1024, 3),
          num_classes=20,
          depthwise=False,
          output_stride=16,
          backbone='xception'):
    if backbone == 'modified_xception':
        # Input Layer
        inputs = keras.Input(input_size, name='xception_input')
        # Do the entry block, also returns the low layer for ASPP and Decoder
        xception, low_layer = entry_block(inputs)
        # Now do the middle block
        for i in range(16):
            blockname = 'middle_block{}'.format(i)
            xception = middle_block(xception, blockname)
        # Activate the last add
        xception = keras.layers.Activation(
            'relu', name='exit_block_relu_add')(xception)
        # Do the exit block
        xception = exit_block(xception)
    elif backbone == 'xception':
        xception = applications.Xception(weights='imagenet',
                                         include_top=False,
                                         input_shape=input_size,
                                         classes=20)
        for layer in xception.layers:
            layer.trainable = False
        inputs = xception.layers[0].output
        low_layer = xception.get_layer('add_1').output
        low_layer.trainable = True
        previous = xception.layers[-1].output
        previous.trainable = True
    elif backbone == 'mobilenetv2':
        mobilenet = applications.MobileNetV2(weights='imagenet',
                                             include_top=False,
                                             input_shape=input_size,
                                             classes=20)
        inputs = mobilenet.layers[0].output
        low_layer = mobilenet.get_layer('block_3_depthwise').output
        previous = mobilenet.layers[-1].output
    # ASPP
    aspp = ASPP(previous, depthwise=depthwise, output_stride=output_stride)
    aspp_up = keras.layers.UpSampling2D(size=(4, 4),
                                        interpolation='bilinear',
                                        name='decoder_ASPP_upsample')(aspp)
    # Decoder Begins Here
    conv1x1 = keras.layers.Conv2D(48,
                                  1,
                                  strides=1,
                                  padding='same',
                                  use_bias=False,
                                  name="decoder_conv1x1")(low_layer)
    norm1x1 = keras.layers.BatchNormalization(
        name='decoder_conv1x1_batch_norm')(conv1x1)
    relu1x1 = keras.layers.Activation('relu',
                                      name='decoder_conv1x1_relu')(norm1x1)

    # Concatenate ASPP and the 1x1 Convolution
    decode_concat = keras.layers.Concatenate(name='decoder_concat')(
        [aspp_up, relu1x1])

    # Do some Convolutions
    if depthwise:
        conv1_decoder = keras.layers.SeparableConv2D(
            256, 3, strides=1, padding='same',
            name='decoder_conv1')(decode_concat)
    else:
        conv1_decoder = keras.layers.Conv2D(
            256, 3, strides=1, padding='same',
            name='decoder_conv1')(decode_concat)
    norm1_decoder = keras.layers.BatchNormalization(
        name='decoder_conv1_batch_norm')(conv1_decoder)
    relu1_decoder = keras.layers.Activation(
        'relu', name='decoder_conv1_relu')(norm1_decoder)

    if depthwise:
        conv2_decoder = keras.layers.SeparableConv2D(
            256, 3, strides=1, padding='same',
            name='decoder_conv2')(relu1_decoder)
    else:
        conv2_decoder = keras.layers.Conv2D(
            256, 3, strides=1, padding='same',
            name='decoder_conv2')(relu1_decoder)
    norm2_decoder = keras.layers.BatchNormalization(
        name='decoder_conv2_batch_norm')(conv2_decoder)
    relu2_decoder = keras.layers.Activation(
        'relu', name='decoder_conv2_relu')(norm2_decoder)

    # Do classification 1x1 layer
    classification = keras.layers.Conv2D(num_classes,
                                         kernel_size=(1, 1),
                                         strides=(1, 1),
                                         activation='softmax',
                                         name='Classification',
                                         dtype=tf.float32)(relu2_decoder)
    classification_up = keras.layers.UpSampling2D(
        size=(8, 8),
        interpolation='bilinear',
        name="Classificaton_Upsample",
        dtype=tf.float32)(classification)

    # Create the model
    model = keras.Model(inputs=inputs, outputs=classification_up)
    # Return the model
    return model
Esempio n. 18
0
(train_images, train_labels), (test_images, test_labels) = datasets.fashion_mnist.load_data()

train_images, test_images = train_images / 255.0, test_images / 255.0

# resize image to 32 x 32 using bicubic interpolation
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1)
train_images = tf.image.resize(train_images,[32,32], method='bicubic')
train_images = tf.image.grayscale_to_rgb(train_images)
plt.imshow(train_images[1])

test_images = test_images.reshape(test_images.shape[0], 28, 28, 1)
test_images = tf.image.resize(test_images,[32,32], method='bicubic')
test_images = tf.image.grayscale_to_rgb(test_images)

# load MobileNetV2
base_model = applications.MobileNetV2(input_shape=(32, 32, 3), include_top=False, weights='imagenet')
base_model.trainable = False

# design of actual NN
model = models.Sequential([base_model])

model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))

# train NN
model.compile(optimizer='adam',
              loss=losses.sparse_categorical_crossentropy,
              metrics=['accuracy'])

# graph the learning process