Пример #1
0
def find_matching_image_with_rapids():

    model = EfficientNetB0(weights='imagenet',
                           include_top=False,
                           pooling='avg',
                           input_shape=None)
    train_gen = DataGenerator(train, batch_size=128)
    image_embeddings = model.predict(train_gen, verbose=1)
    print('image embeddings shape is', image_embeddings.shape)

    # After fitting KNN, we will display some example rows of train and their 8 closest other images in train (based EffNetB0 image embeddings).

    KNN = 50
    model = NearestNeighbors(n_neighbors=KNN)
    model.fit(image_embeddings)
    distances, indices = model.kneighbors(image_embeddings)

    for k in range(180, 190):
        plt.figure(figsize=(20, 3))
        plt.plot(np.arange(50), cupy.asnumpy(distances[k, ]), 'o-')
        plt.title('Image Distance From Train Row %i to Other Train Rows' % k,
                  size=16)
        plt.ylabel('Distance to Train Row %i' % k, size=14)
        plt.xlabel('Index Sorted by Distance to Train Row %i' % k, size=14)
        plt.show()

        cluster = train.loc[cupy.asnumpy(indices[k, :8])]
        displayDF(cluster, random=False, ROWS=2, COLS=4)
Пример #2
0
def create_first_model():
    """Initial model prototype that I used for testing in the notebook"""

    effnet = EfficientNetB0(weights="imagenet",
                            include_top=False,
                            input_shape=(224, 224, 3))
    site1 = Input(shape=(224, 224, 3))
    site2 = Input(shape=(224, 224, 3))
    x = effnet(site1)
    x = GlobalAveragePooling2D()(x)
    x = Model(inputs=site1, outputs=x)
    y = effnet(site2)
    y = GlobalAveragePooling2D()(y)
    y = Model(inputs=site2, outputs=y)
    combined = concatenate([x.output, y.output])
    z = Dropout(0.5)(combined)
    z = Dense(1108, activation="softmax")(z)
    model = Model(inputs=[x.input, y.input], outputs=z)
    model.compile(
        loss="sparse_categorical_crossentropy",
        optimizer=Adam(0.0001),
        metrics=["accuracy"],
    )
    model.summary()

    return model
Пример #3
0
def build_model(nb_classes):
    base_model = EfficientNetB0(weights='imagenet', include_top=False)

    # add a global spatial average pooling layer
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    # let's add a fully-connected layer
    x = Dense(1024, activation='relu')(x)
    # and a logistic layer
    predictions = Dense(nb_classes, activation='softmax')(x)

    # this is the model we will train
    model = Model(inputs=base_model.input, outputs=predictions)

    # first: train only the top layers (which were randomly initialized)
    # i.e. freeze all convolutional InceptionV3 layers
    for layer in base_model.layers:
        layer.trainable = True
    Adadelta0 = optimizers.Adadelta(lr=0.13)
    # compile the model (should be done *after* setting layers to non-trainable)
    print("starting model compile")
    #compile(model)
    model.compile(optimizer=Adadelta0,
                  loss='sparse_categorical_crossentropy',
                  metrics=['sparse_categorical_accuracy'])
    print("model compile done")
    return model
Пример #4
0
def adapt_efficient_net() -> Model:
    """This code uses adapts the most up-to-date version of EfficientNet with NoisyStudent weights to a regression
    problem. Most of this code is adapted from the official keras documentation.

    Returns
    -------
    Model
        The keras model.
    """
    inputs = layers.Input(
        shape=(224, 224, 3)
    )  # input shapes of the images should always be 224x224x3 with EfficientNetB0
    # use the downloaded and converted newest EfficientNet wheights
    model = EfficientNetB0(include_top=False,
                           input_tensor=inputs,
                           weights="efficientnetb0_notop.h5")
    # Freeze the pretrained weights
    model.trainable = False

    # Rebuild top
    x = layers.GlobalAveragePooling2D(name="avg_pool")(model.output)
    x = layers.BatchNormalization()(x)
    top_dropout_rate = 0.4
    x = layers.Dropout(top_dropout_rate, name="top_dropout")(x)
    outputs = layers.Dense(1, name="pred")(x)

    # Compile
    model = keras.Model(inputs, outputs, name="EfficientNet")

    return model
Пример #5
0
def image_embeddings_extraction(test):
    BASE = '../input/shopee-product-matching/test_images/'
    if COMPUTE_CV:
        BASE = '../input/shopee-product-matching/train_images/'

    WGT = '../input/effnetb0/efficientnetb0_notop.h5'
    model = EfficientNetB0(weights=WGT, include_top=False, pooling='avg', input_shape=None)

    embeds = []
    CHUNK = 1024 * 4

    print('Computing image embeddings...')
    CTS = len(test) // CHUNK
    if len(test) % CHUNK != 0:
        CTS += 1
    for i, j in enumerate(range(CTS)):
        a = j * CHUNK
        b = (j + 1) * CHUNK
        b = min(b, len(test))
        print('chunk', a, 'to', b)

        test_gen = DataGenerator(test.iloc[a:b], batch_size=32, path=BASE)
        image_embeddings = model.predict(test_gen, verbose=1, use_multiprocessing=True, workers=4)
        embeds.append(image_embeddings)

        # if i>=1: break

    del model
    _ = gc.collect()
    image_embeddings = np.concatenate(embeds)
    print('image embeddings shape', image_embeddings.shape)
Пример #6
0
def build_model():
  inputs = tf.keras.Input(shape=(RESIZE_TO, RESIZE_TO, 3))
  model = EfficientNetB0(include_top=False, weights="imagenet", classes=NUM_CLASSES, input_tensor=inputs)
  model.trainable = False
  x = tf.keras.layers.GlobalAveragePooling2D()(model.output)
  outputs = tf.keras.layers.Dense(NUM_CLASSES, activation=tf.keras.activations.softmax)(x)
  return tf.keras.Model(inputs=inputs, outputs=outputs)
Пример #7
0
def build_model():
  inputs = tf.keras.Input(shape=(RESIZE_TO, RESIZE_TO, 3))
  x = EfficientNetB0(include_top=False, input_tensor = inputs,pooling ='avg', weights='imagenet')
  x.trainable = False
  x = tf.keras.layers.Flatten()(x.output)
  outputs = tf.keras.layers.Dense(NUM_CLASSES, activation=tf.keras.activations.softmax)(x)
  return tf.keras.Model(inputs=inputs, outputs=outputs)
Пример #8
0
    def load_model(self, name_model=''):
        """Loads the model indicated by name_model.

        Args:
            name_model (str, optional): name of the model we want to load.
            Defaults to ''.
        """
        if name_model != '':
            if name_model in self.name_model:
                if name_model == 'VGG16':
                    self.MODEL = VGG16(weights='imagenet')
                    self.fonction_preprocessing = prepro_vg116
                elif name_model == "mobile_net":
                    self.MODEL = MobileNet()
                    self.fonction_preprocessing = prepro_mobile_net
                elif name_model == "efficient_net":
                    self.MODEL = EfficientNetB0()
                    self.fonction_preprocessing = prepro_efficient_net
                elif name_model == "efficient_netB3":
                    self.MODEL = EfficientNetB3()
                    self.fonction_preprocessing = prepro_efficient_net
            else:
                raise(Exception(f"name_model not found in {self.name_model}"))
        else:
            self.MODEL = VGG16(weights='imagenet')
            self.fonction_preprocessing = prepro_efficient_net
Пример #9
0
def build_model(num_classes):
    inputs = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3))
    x = img_augmentation(inputs)
    model = EfficientNetB0(include_top=False,
                           input_tensor=x,
                           weights="imagenet")

    # Freeze the pretrained weights
    model.trainable = False

    # Rebuild top
    x = layers.GlobalAveragePooling2D(name="avg_pool")(model.output)
    x = layers.BatchNormalization()(x)

    top_dropout_rate = 0.2
    x = layers.Dropout(top_dropout_rate, name="top_dropout")(x)
    outputs = layers.Dense(NUM_CLASSES, activation="softmax", name="pred")(x)

    # Compile
    model = tf.keras.Model(inputs, outputs, name="EfficientNet")
    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-2)
    model.compile(optimizer=optimizer,
                  loss="categorical_crossentropy",
                  metrics=["accuracy"])
    return model
Пример #10
0
def get_pca_plot(test_dataset_dir, model, classes):

    IMG_SIZE = 224
    NUM_CLASSES = 48

    test_dataset = tf.keras.preprocessing.image_dataset_from_directory(
        test_dataset_dir, seed=123, image_size=(IMG_SIZE, IMG_SIZE))

    base_model = EfficientNetB0(include_top=False, weights='imagenet')
    global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
    preprocess_input = tf.keras.applications.efficientnet.preprocess_input
    prediction_layer = tf.keras.layers.Dense(NUM_CLASSES, activation='softmax')

    inputs = tf.keras.Input(shape=(IMG_SIZE, IMG_SIZE, 3))
    #y = rescale(inputs)
    x = preprocess_input(inputs)
    x = base_model(x, training=False)
    outputs = global_average_layer(x)

    model1 = tf.keras.Model(inputs, outputs)
    model1.set_weights(model.get_weights()[:len(model1.get_weights())])

    intermediates = []
    labels = []

    for x, y in test_dataset:
        output = list(model1.predict(x))
        intermediates.extend(output)
        labels.extend(list(y))

    output_pca_data = get_pca(intermediates)
    path = plot_representations(output_pca_data, labels, classes)
    return path
Пример #11
0
    def __get_cnn(self):
        size_sq = 224
        dict_cnn = {
            'resnet':
            ResNet50(
                weights='imagenet',
                include_top=False,  # remove the last layer
                input_shape=(size_sq, size_sq, 3)),
            'vgg':
            VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(size_sq, size_sq, 3)),
            'inceptV3':
            InceptionV3(
                weights="imagenet",
                include_top=False,
                input_shape=(size_sq, size_sq, 3),
            ),
            'effnetB0':
            EfficientNetB0(weights='imagenet',
                           include_top=False,
                           input_shape=(size_sq, size_sq, 3)),
            'effnetB7':
            EfficientNetB7(weights='imagenet',
                           include_top=False,
                           input_shape=(size_sq, size_sq, 3))
        }

        return dict_cnn[self.cnn_name]
Пример #12
0
def build_model():
  inputs = tf.keras.Input(shape=(RESIZE_TO, RESIZE_TO, 3))
  x = tf.keras.layers.experimental.preprocessing.RandomRotation(factor=(0, 0.025), fill_mode='wrap')(inputs)
  model = EfficientNetB0(input_tensor=x,include_top=False,pooling='avg',weights='imagenet')
  model.trainable=False
  x = tf.keras.layers.Flatten()(model.output)
  outputs = tf.keras.layers.Dense(NUM_CLASSES, activation=tf.keras.activations.softmax)(x)
  return tf.keras.Model(inputs=inputs, outputs=outputs)
Пример #13
0
def build_model():
  inputs = tf.keras.Input(shape=(RESIZE_TO, RESIZE_TO, 3))
  x = data_augmentation(inputs)
  x = EfficientNetB0(include_top=False, weights='imagenet', input_tensor = x)
  x.trainable = False
  x = tf.keras.layers.GlobalAveragePooling2D()(x.output)
  outputs = tf.keras.layers.Dense(NUM_CLASSES, activation=tf.keras.activations.softmax)(x)
  return tf.keras.Model(inputs=inputs, outputs=outputs)
Пример #14
0
def efficientnetB0(
        input_shape, nclasses=2, num_dense_blocks=3, growth_rate=12, depth=100, compression_factor=0.5,
        data_augmentation=True, regularization=0.
):

    keras_shape = input_shape
    if input_shape[-1] == 1:
        keras_shape = (32, 32, 3)

    keras_model = EfficientNetB0(
        include_top=False,
        input_shape=keras_shape,
        weights=None
    )

    keras_model.trainable = True

    # adding regularization
    # regularizer = l2(regularization)
    #
    # for layer in keras_model.layers:
    #     for attr in ['kernel_regularizer']:
    #         if hasattr(layer, attr):
    #             setattr(layer, attr, regularizer)
    #
    # tmp_weights_path = os.path.join(tempfile.gettempdir(), 'tmp_weights.h5')
    # keras_model.save_weights(tmp_weights_path)
    #
    # keras_json = keras_model.to_json()
    # keras_model = models.model_from_json(keras_json)
    # keras_model.load_weights(tmp_weights_path, by_name=True)

    outputs = keras_model.output
    inputs = keras_model.input
    if input_shape[-1] == 1:
        inputs = layers.Input(shape=input_shape)
        x = layers.ZeroPadding2D(padding=(2, 2))(inputs)
        output_shape = K.int_shape(x)
        output_shape = output_shape[:-1] + (3,)
        x = layers.Lambda(lambda x: K.tile(x, (1, 1, 1, 3)), output_shape=output_shape)(x)
        outputs = keras_model(x)

    outputs = layers.Flatten()(outputs)
    # outputs = layers.GlobalAveragePooling2D()(outputs)
    # outputs = layers.Dropout(rate=.2)(outputs)
    outputs = layers.Dense(nclasses,
                           kernel_initializer='he_normal',
                           kernel_regularizer=None,
                           activation='softmax')(outputs)

    # instantiate and compile model
    # orig paper uses SGD but RMSprop works better for DenseNet
    model = models.Model(inputs=inputs, outputs=outputs)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizers.SGD(1e-4),
                  metrics=['acc'])

    return model
Пример #15
0
    def __init__(self):
        super(Encoder, self).__init__()
        self.base_model = EfficientNetB0(include_top=False, weights='imagenet')
        print('Base model loaded {}'.format(EfficientNetB0.__name__))

        # Create encoder model that produce final features along with multiple intermediate features
        outputs = [self.base_model.outputs[-1]]
        for name in ['block2a_activation', 'block3a_activation', 'block4a_activation', 'block1a_activation'] : outputs.append( self.base_model.get_layer(name).output )
        self.encoder = Model(inputs=self.base_model.inputs, outputs=outputs)
def efficient_b0_2(size):
    conv_base = EfficientNetB0(include_top=False,
                               weights=None,
                               input_shape=(size, size, 3))
    model = conv_base.output
    model = layers.GlobalAveragePooling2D()(model)
    model = layers.Dense(5, activation="softmax")(model)
    model = models.Model(conv_base.input, model)
    return model
Пример #17
0
def EfficientNet(cfg):
    regularizer = l2(cfg.TRAIN.WD)

    if cfg.MODEL.SIZE == 0:
        backbone = EfficientNetB0(include_top=False,
                                  input_shape=tuple(cfg.DATASET.INPUT_SHAPE))
    elif cfg.MODEL.SIZE == 1:
        backbone = EfficientNetB1(include_top=False,
                                  input_shape=tuple(cfg.DATASET.INPUT_SHAPE))
    elif cfg.MODEL.SIZE == 2:
        backbone = EfficientNetB2(include_top=False,
                                  input_shape=tuple(cfg.DATASET.INPUT_SHAPE))
    elif cfg.MODEL.SIZE == 3:
        backbone = EfficientNetB3(include_top=False,
                                  input_shape=tuple(cfg.DATASET.INPUT_SHAPE))
    elif cfg.MODEL.SIZE == 4:
        backbone = EfficientNetB4(include_top=False,
                                  input_shape=tuple(cfg.DATASET.INPUT_SHAPE))

    backbone = add_regularization(backbone, regularizer)

    d, w, _ = scaling_parameters(cfg.DATASET.INPUT_SHAPE)

    width_coefficient = cfg.MODEL.WIDTH_COEFFICIENT * w
    depth_divisor = cfg.MODEL.DEPTH_DIVISOR
    head_filters = cfg.MODEL.HEAD_CHANNELS
    head_kernel = cfg.MODEL.HEAD_KERNEL
    head_activation = cfg.MODEL.HEAD_ACTIVATION
    keypoints = cfg.DATASET.OUTPUT_SHAPE[-1]
    regularizer = l2(cfg.TRAIN.WD)

    x = backbone.layers[-1].output
    for i in range(cfg.MODEL.HEAD_BLOCKS):
        x = layers.Conv2DTranspose(round_filters(head_filters,
                                                 width_coefficient,
                                                 depth_divisor),
                                   head_kernel,
                                   strides=2,
                                   padding='same',
                                   use_bias=False,
                                   kernel_initializer=CONV_KERNEL_INITIALIZER,
                                   kernel_regularizer=regularizer,
                                   name='head_block{}_conv'.format(i + 1))(x)
        x = layers.BatchNormalization(name='head_block{}_bn'.format(i + 1))(x)
        x = layers.Activation(head_activation,
                              name='head_block{}_activation'.format(i + 1))(x)

    x = layers.Conv2D(keypoints,
                      cfg.MODEL.FINAL_KERNEL,
                      padding='same',
                      use_bias=True,
                      kernel_initializer=DENSE_KERNEL_INITIALIZER,
                      kernel_regularizer=regularizer,
                      name='final_conv')(x)

    return Model(backbone.input, x, name=f'EfficientNetLite_{cfg.MODEL.SIZE}')
Пример #18
0
def build_model():
  inputs = tf.keras.Input(shape=(235, 235, 3))
  x = tf.keras.layers.GaussianNoise(stddev = 0.6)(inputs)
  x = tf.keras.layers.experimental.preprocessing.RandomCrop(224,224)(x)
  x = tf.keras.layers.experimental.preprocessing.RandomRotation(factor = 0.02)(x)
  model = EfficientNetB0(include_top=False, input_tensor = x, pooling = 'avg', weights='imagenet')
  model.trainable = False
  x = tf.keras.layers.Flatten()(model.output)
  outputs = tf.keras.layers.Dense(NUM_CLASSES, activation = tf.keras.activations.softmax)(x)
  return tf.keras.Model(inputs=inputs, outputs=outputs)
Пример #19
0
def build_model(num_classes, model="B7"):
    inputs = layers.Input(shape=(WIDTH, HEIGHT, 3))
    if model == "B7":
        model = EfficientNetB7(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B6":
        model = EfficientNetB6(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B5":
        model = EfficientNetB5(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B4":
        model = EfficientNetB4(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B3":
        model = EfficientNetB3(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B2":
        model = EfficientNetB2(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B1":
        model = EfficientNetB1(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    elif model == "B0":
        model = EfficientNetB0(include_top=False,
                               input_tensor=inputs,
                               weights="imagenet")
    # Freeze the pretrained weights
    model.trainable = False

    # Rebuild top
    x = layers.GlobalAveragePooling2D(name="avg_pool")(model.output)
    x = layers.BatchNormalization()(x)

    top_dropout_rate = 0.2
    x = layers.Dropout(top_dropout_rate, name="top_dropout", seed=SEED)(x)
    outputs = layers.Dense(num_classes,
                           activation="softmax",
                           name="predictions")(x)

    # Compile
    model = tf.keras.Model(inputs, outputs, name="EfficientNet")
    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-2)
    model.compile(
        optimizer=optimizer,
        loss="sparse_categorical_crossentropy",
        metrics=[tf.keras.metrics.SparseTopKCategoricalAccuracy(k=1)])
    return model
Пример #20
0
def get_parent(input_shape):
    if input_shape[0] == 256:
        parent = EfficientNetB0(include_top=False)
    elif input_shape[0] == 384:
        parent = EfficientNetB4(include_top=False)
    elif input_shape[0] == 512:
        parent = EfficientNetB5(include_top=False)
    else:
        print('Not a valid input shape for EfficientNet parent')
        parent = None
    return parent
Пример #21
0
    def __init__(self, model_name="efficientnetb0"):
        """Class to abstract useful methods for extracting representations using a pretrained model.
        Extend this class to use any of the dozen pretrained image analysis models on 
        tensorflow.keras.applications https://www.tensorflow.org/api_docs/python/tf/keras/applications .


        Args:
            model_name (str, optional): [description]. Defaults to "efficientnetb0".
        """
        if (model_name == "efficientnetb0"):
            self.model = EfficientNetB0(include_top=False)
            self.image_size = 224
Пример #22
0
    def newModel(self) -> tf.keras.Model:
        if self.modelstr == "MobileNetV2":

            base_model = MobileNetV2(weights='imagenet', include_top=False,
                                     input_shape=(224, 224, 3))
            base_model.trainable = True
            inputs = tf.keras.Input(shape=(224, 224, 3), name="image_input")
            x = base_model(inputs, training=True, )
            x = GlobalAveragePooling2D()(x)
            x = BatchNormalization()(x)
            x = Dropout(0.3)(x)
            x = Dense(1024, activation='relu')(x)
            x = Dense(1024, activation='relu')(x)
            x = Dense(512, activation='relu')(x)
            x = Dense(128, activation='relu')(x)
            preds = Dense(3, activation='softmax')(x)

            self.model = tf.keras.Model(inputs=inputs, outputs=[preds], name="mobileNetV2")
            self.model.compile(loss=losses.SparseCategoricalCrossentropy(from_logits=False),
                               optimizer=optimizers.Adam(learning_rate=1e-3),
                               metrics=['accuracy'])

        else:  # EfficientNet

            def unfreeze_model(model):
                # We unfreeze the top 20 layers while leaving BatchNorm layers frozen
                for layer in model.layers[-20:]:
                    if not isinstance(layer, layers.BatchNormalization):
                        layer.trainable = True

            inputs = tf.keras.Input(shape=(224, 224, 3), name="image_input")
            conv_base = EfficientNetB0(input_shape=(224, 224, 3),
                                       input_tensor=inputs, drop_connect_rate=0.4,
                                       include_top=False)

            conv_base.trainable = False

            unfreeze_model(conv_base)
            x = GlobalAveragePooling2D()(conv_base.output)
            x = BatchNormalization()(x)
            x = Dropout(0.3)(x)
            x = Dense(512, activation='relu')(x)
            x = Dense(256, activation='relu')(x)
            x = Dense(128, activation='relu')(x)
            preds = Dense(3, activation='softmax')(x)

            self.model = tf.keras.Model(inputs=inputs, outputs=[preds], name="efficientNetB0")
            self.model.compile(loss=losses.SparseCategoricalCrossentropy(from_logits=False),
                               optimizer=optimizers.Adam(learning_rate=1e-3),
                               metrics=['accuracy'])

        return self.model
Пример #23
0
def create_model():
    _model = models.Sequential()

    _model.add(EfficientNetB0(include_top=False, weights='imagenet',
                              input_shape=(TARGET_SIZE, TARGET_SIZE, 3)))

    _model.add(layers.GlobalAveragePooling2D())
    _model.add(layers.Dense(5, activation="softmax"))

    _model.compile(optimizer=Adam(lr=0.001),
                   loss="sparse_categorical_crossentropy",
                   metrics=["accuracy"])
    return _model
def build_model():
    inputs = tf.keras.Input(shape=(RESIZE_TO, RESIZE_TO, 3))
    x = tf.keras.layers.experimental.preprocessing.Resizing(235, 235)(inputs)
    x = tf.keras.layers.experimental.preprocessing.RandomCrop(224, 224)(x)
    model = EfficientNetB0(input_tensor=x,
                           include_top=False,
                           pooling='avg',
                           weights='imagenet')
    model.trainable = False
    model = tf.keras.layers.Flatten()(model.output)
    outputs = tf.keras.layers.Dense(
        NUM_CLASSES, activation=tf.keras.activations.softmax)(model)
    return tf.keras.Model(inputs=inputs, outputs=outputs)
Пример #25
0
def create_model(training=True):
    base_model = EfficientNetB0(weights="imagenet",
                                include_top=False,
                                input_shape=(ROW, COL, 3))

    x = Input(shape=(ROW, COL, 3))
    out_1 = base_model(x, training=training)
    out_1 = GlobalAveragePooling2D(name='encoding')(out_1)
    out_1 = Dropout(0.5)(out_1)
    output = Dense(934, activation="softmax")(out_1)

    final_model = Model(inputs=x, outputs=output)

    return final_model
Пример #26
0
def build_model():
    inputs = tf.keras.Input(shape=(RESIZE_TO, RESIZE_TO, 3))
    model = img_rotate(inputs)
    model = img_contrast(model)
    model = img_gauss(model)
    model = EfficientNetB0(include_top=False,
                           input_tensor=inputs,
                           weights='imagenet')
    model.trainable = False
    model = tf.keras.layers.GlobalAveragePooling2D()(model.output)
    model = tf.image.adjust_brightness(model, 0.3)
    outputs = tf.keras.layers.Dense(
        NUM_CLASSES, activation=tf.keras.activations.softmax)(model)
    return tf.keras.Model(inputs=inputs, outputs=outputs)
Пример #27
0
def build_model(nb_classes):
    base_model = EfficientNetB0(weights='imagenet', include_top=False)
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(nb_classes, activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=predictions)
    for layer in base_model.layers:
        layer.trainable = True
    Adadelta0 = optimizers.Adadelta(lr=0.013)
    model.compile(optimizer=Adadelta0, loss='sparse_categorical_crossentropy', metrics=[
                  'sparse_categorical_accuracy'])
    #model.load_weights("./Weight/efficientNetB0.h5")
    return model
Пример #28
0
def EfficientNetClassifier():
    '''
    Using transfer learning, create an EfficientNetB0 model to be used. We
    will update the output for the model as well as unfreeze some of the final
    15 layers to be trainined and learn more low level features in our images.
    '''
    effnetb0 = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3))
    model = layers.GlobalAveragePooling2D()(effnetb0.output)
    model = layers.Dropout(0.3)(model)
    model = layers.Dense(4, activation='softmax')(model)
    model = models.Model(inputs=effnetb0.input, outputs=model)
    model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
    
    return model
    
Пример #29
0
def EfficientNetB0model(no_classes, shape):
    """
    EfficientNetB0
    https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/
    Uses a fixed input size 224,224
    """
    base_model = EfficientNetB0(include_top=False,
                                weights='imagenet',
                                input_shape=shape)
    base_model.trainable = False
    inputs = Input(shape=shape)
    x = base_model(inputs, training=False)
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(no_classes, activation='softmax',
                        name='predictions')(x)
    model = Model(inputs, outputs=predictions)
    return model
Пример #30
0
def get_efficientB0_model(config):
    # https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/

    size = (config['img_height'], config['img_width'])
    inputs = tf.keras.layers.Input(shape=(config['img_height'],
                                          config['img_width'], 3))
    #x = tf.keras.layers.GaussianNoise(20)(inputs)
    x = inputs
    from tensorflow.keras.applications import EfficientNetB0
    weights = 'imagenet'
    if 'should_init_pretrained' in config and config[
            'should_init_pretrained'] == False:
        weights = None

    encoder = EfficientNetB0(include_top=False,
                             weights=weights,
                             drop_connect_rate=0.2,
                             input_tensor=x)
    encoder.trainable = False
    for l in encoder.layers:
        l.trainable = False
    #encoder.summary()
    encoded_layer_names = [
        'block1a_activation',  # (112,112,32)
        'block3a_expand_activation',  # (56,56,144)
        'block4a_expand_activation',  # (28,28,240)
        'block5a_expand_activation'  #, # (14,14,672)
        #'block6a_activation' # (7,7,1152)
    ]

    outputs = [get_decoded(config, encoder, encoded_layer_names)]
    net = tf.keras.Model(inputs=encoder.inputs,
                         outputs=[outputs],
                         name="EfficientUnet")
    #for l in net.layers:
    #    if 'block' in l.name:
    #        l.trainable=False
    return encoder, net