def create_model(conf):
    """
    """
    if conf["model"] == 'EfficientNetB0':
        from efficientnet.tfkeras import EfficientNetB0 as Network  # 4.7M params
    elif conf["model"] == 'EfficientNetB1':
        from efficientnet.tfkeras import EfficientNetB1 as Network  # 7.2M params
    elif conf["model"] == 'EfficientNetB2':
        from efficientnet.tfkeras import EfficientNetB2 as Network  # 8.5M params
    elif conf["model"] == 'EfficientNetB3':
        from efficientnet.tfkeras import EfficientNetB3 as Network  # 11.5M params
    elif conf["model"] == 'EfficientNetB4':
        from efficientnet.tfkeras import EfficientNetB4 as Network  # 18.6M params
    elif conf["model"] == 'EfficientNetB5':
        from efficientnet.tfkeras import EfficientNetB5 as Network  # 29.5M params
    elif conf["model"] == 'EfficientNetB6':
        from efficientnet.tfkeras import EfficientNetB6 as Network  # 42.2M params
    elif conf["model"] == 'EfficientNetB7':
        from efficientnet.tfkeras import EfficientNetB7 as Network  # 65.4M params

    elif conf["model"] == 'ResNet50':
        from tensorflow.keras.applications import ResNet50 as Network  # 23,6M params

    base_model = Network(
        weights=conf["weights"],  # "imagenet", None or "noisy-student"
        include_top=False,
        input_shape=conf["img_shape"])

    # Unfreeze the layers. I.E we're just using the pre-trained
    # weights as initial weigths and biases and train over them
    base_model.trainable = True

    # Define model
    model = Sequential()
    model.add(base_model)
    model.add(layers.GlobalAveragePooling2D())
    model.add(layers.Dropout(conf["dropout"]))
    model.add(layers.Dense(512, activation='relu'))
    model.add(layers.Dropout(conf["dropout"]))
    model.add(
        layers.Dense(conf["num_classes"], activation=conf["final_activation"]))

    if conf['optimizer'] == 'Adam':
        opt = tf.keras.optimizers.Adam(learning_rate=conf["learning_rate"])
    elif conf['optimizer'] == 'SGD':
        opt = tf.keras.optimizers.SGD(learning_rate=conf["learning_rate"])

    model.compile(optimizer=opt,
                  loss='sparse_categorical_crossentropy',
                  metrics=['sparse_categorical_accuracy'])

    if conf["verbosity"]:
        model.summary()

    return model
示例#2
0
    def build_efficientnet(self):
        """Builds efficient model with dense layers on top

        Returns:
            keras.models.Model

        :return:
        """
        from efficientnet.tfkeras import EfficientNetB0
        strategy = tf.distribute.MirroredStrategy()
        with strategy.scope():
            conv_base = EfficientNetB0(weights='noisy-student',
                                       include_top=False,
                                       input_shape=(224, 224, 3))

            outputconv_base = conv_base.output
            t_flat = Flatten()(outputconv_base)
            t_dense1 = Dense(1024, activation='relu')(t_flat)
            t_dense2 = Dense(256, activation='relu')(t_dense1)
            t_dense3 = Dense(128, activation='relu')(t_dense2)
            t_do = Dropout(0.5)(t_dense3)
            predictions = Dense(2, activation='softmax')(t_do)

            model = Model(inputs=conv_base.input,
                          outputs=predictions,
                          name='model')
            conv_base.trainable = False
            opt = tf.keras.optimizers.Adam(learning_rate=0.0002)

            model.compile(
                loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=0.01),
                optimizer=opt,
                metrics=['accuracy'])

            return model
def SR_model(num_classes,
             dropout,
             mc_dropout,
             input_dim,
             training,
             pooling='avg'):
    inputs = Input(input_dim)
    base_model = EfficientNetB0(include_top=False,
                                weights='imagenet',
                                input_tensor=inputs)
    base_model.trainable = True
    x = base_model.output
    x = Dropout(dropout, name='top_dropout_1')(x, training=training)
    if pooling == 'avg':
        x = GlobalAveragePooling2D(name='avg_pool')(x)
    elif pooling == 'max':
        x = GlobalMaxPooling2D(name='max_pool')(x)
    x = Dropout(dropout, name='top_dropout_2')(x, training=training)
    x = Dense(512, activation='relu', name='dense_512')(x)
    x = BatchNormalization()(x)
    x = Dropout(dropout, name='top_dropout_3')(x, training=training)
    x = Lambda(lambda x: K.dropout(x, level=mc_dropout))(x)

    #classification head (f)
    sr = Dense(num_classes, activation='softmax', name='dense_f')(x)
    return Model(inputs=inputs, outputs=sr)
示例#4
0
    def __init__(self,
                 dropout=0.2,
                 mc_dropout=0.2,
                 num_classes=1,
                 training=True,
                 input_dim=(224, 224, 3),
                 pooling="avg"):
        self.c = 0.75
        self.lamda = 32
        self.alpha = 0.5
        self.dropout = dropout
        self.mc_dropout = mc_dropout
        self.pooling = pooling
        self.input_dim = input_dim
        self.training = training
        self.num_classes = num_classes

        #create model
        inputs = Input(shape=self.input_dim)
        base_model = EfficientNetB0(include_top=False,
                                    weights='imagenet',
                                    input_tensor=inputs)
        base_model.trainable = True
        x = base_model.output
        x = Dropout(self.dropout, name='top_dropout_1')(x,
                                                        training=self.training)
        if pooling == 'avg':
            x = GlobalAveragePooling2D(name='avg_pool')(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D(name='max_pool')(x)
        x = Dropout(self.dropout, name='top_dropout_2')(x,
                                                        training=self.training)
        x = Dense(512, activation='relu', name='dense_512')(x)
        x = BatchNormalization()(x)
        x = Dropout(self.mc_dropout,
                    name='top_dropout_3')(x, training=self.training)
        x = Lambda(lambda x: K.dropout(x, level=self.mc_dropout))(x)

        #classification head (f)
        f = Dense(self.num_classes, activation='softmax', name='f_head')(x)

        #selection head (g)
        g = Dense(512, activation='relu', name='dense_512_g')(x)
        g = BatchNormalization()(g)
        # this normalization is identical to initialization of batchnorm gamma to 1/10
        g = Lambda(lambda a: a / 10)(g)
        g = Dense(1, activation='sigmoid', name='g_head')(g)

        # auxiliary head (h)
        selective_output = Concatenate(axis=1, name="selective_head")([f, g])

        auxillary_output = Dense(self.num_classes,
                                 activation='softmax',
                                 name='auxilary_head')(x)

        self.model = Model(inputs=inputs,
                           outputs=[selective_output, auxillary_output])
示例#5
0
def efficientnet(b,
                 weights='imagenet',
                 include_top=False,
                 input_shape=(None, None, 3)):
    """Loads the appropriate EfficientNet model with weights

    :param b: The size of the EfficientNet model.
    :type b: int
    :param weights: The pretrained weights to load. Defaults to iamgenet.
    :type weights: str
    :param include_top: Include the pretrained softmax layer. Defaults to False
    :type include_top: bool
    :param input_shape: Shape of input images. Defaults to no hight or width, 3 channels.
    :type input_shape: Tuple

    :return: EfficientNet Model.
    :rtype: tf.keras.models.Model
    """

    if b == 0:
        return EfficientNetB0(weights=weights,
                              include_top=include_top,
                              input_shape=input_shape)
    elif b == 1:
        return EfficientNetB1(weights=weights,
                              include_top=include_top,
                              input_shape=input_shape)
    elif b == 2:
        return EfficientNetB2(weights=weights,
                              include_top=include_top,
                              input_shape=input_shape)
    elif b == 3:
        return EfficientNetB3(weights=weights,
                              include_top=include_top,
                              input_shape=input_shape)
    elif b == 4:
        return EfficientNetB4(weights=weights,
                              include_top=include_top,
                              input_shape=input_shape)
    elif b == 5:
        return EfficientNetB5(weights=weights,
                              include_top=include_top,
                              input_shape=input_shape)
    elif b == 6:
        return EfficientNetB6(weights=weights,
                              include_top=include_top,
                              input_shape=input_shape)
    elif b == 7:
        return EfficientNetB7(weights=weights,
                              include_top=include_top,
                              input_shape=input_shape)
    else:
        raise Exception("Invalid size for EfficientNet")
示例#6
0
def load_effnet(shape, efftype = 'B0', weights = 'imagenet', trainable = False):
    
    if efftype == 'B0':
        backbone = EfficientNetB0(weights = weights, include_top = False, input_shape = shape)
        
    if efftype == 'B3':
        backbone = EfficientNetB3(weights = weights, include_top = False, input_shape = shape)

    if efftype == 'B5':
        backbone = EfficientNetB5(weights = weights, include_top = False, input_shape = shape)

    for layer in backbone.layers:
        layer.trainable = trainable
    
    return backbone
示例#7
0
    def __init__(self):
        
        num_cls = 5
        imag_size = 224
        #------------------------------------start building model-----------------------------# 
        model = EfficientNetB0(weights = 'imagenet', input_shape = (imag_size,imag_size,3), include_top = False)

        ENet_out = model.output
        ENet_out = Flatten()(ENet_out)

        Hidden1_in = Dense(1024, activation="relu")(ENet_out)
        Hidden1_in = Dropout(0.5)(Hidden1_in)

        predictions = Dense(units = num_cls, activation="softmax")(Hidden1_in)
        self.model_f = Model(inputs = model.input, outputs = predictions)
        self.model_f.compile(optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08), loss='categorical_crossentropy', metrics=[metrics.mae, metrics.categorical_accuracy])
        self.model_f.load_weights("efficientnetB0_weights.h5")
示例#8
0
    def build_model(self):
        self.base_model = EfficientNetB0(weights='noisy-student',
                                         include_top=False)
        self.base_model.trainable = True

        self.x = self.base_model.output
        self.x = GlobalAveragePooling2D()(self.x)
        self.x = Dense(512, activation='relu')(self.x)
        self.x = Dropout(rate=0.2)(self.x)
        self.x = Dense(128, activation='relu')(self.x)
        self.x = Dropout(rate=0.2)(self.x)
        self.preds = Dense(1, activation='sigmoid')(self.x)

        self.model = Model(inputs=self.base_model.input, outputs=self.preds)

        self.model.compile(optimizer='adam',
                           loss='binary_crossentropy',
                           metrics=['accuracy'])
示例#9
0
def get_base_model(modelname):
    if modelname == 'vgg16':
        print('MODELO CARGADO: VGG16')
        return tf.keras.applications.VGG16(input_shape=(img_size(), img_size(),
                                                        3),
                                           include_top=False,
                                           weights='imagenet')
    elif modelname == 'mobilenet':
        print('MODELO CARGADO: MOBILENET')
        return tf.keras.applications.MobileNetV2(input_shape=(img_size(),
                                                              img_size(), 3),
                                                 include_top=False,
                                                 weights='imagenet')
    elif modelname == 'efficientnet':
        print('MODELO CARGADO: EFFICIENTNET')
        return EfficientNetB0(input_shape=(img_size(), img_size(), 3),
                              include_top=False,
                              weights='imagenet')
示例#10
0
def Eff_net():

    # loading pretrained conv base model
    conv_base = EfficientNetB0(weights="imagenet",
                               include_top=true,
                               input_shape=(200, 200, 3))

    dropout_rate = 0.05
    model = models.Sequential()
    model.add(conv_base)
    model.add(layers.Flatten(name="flatten"))
    # model.add(layers.Dense(32, activation="relu"))
    # model.add(layers.Dropout(dropout_rate))
    # model.add(layers.Dense(16, activation="relu"))
    # model.add(layers.Dropout(dropout_rate))
    # model.add(layers.Dense(256, activation='relu', name="fc1"))
    model.add(layers.Dense(1, activation="sigmoid", name="fc_out"))
    conv_base.trainable = False

    return model
示例#11
0
    def __init__(self, arch, pretrained=None):
        super(EfficientNet, self).__init__()

        self.net = EfficientNetB0(include_top=False,
                                  weights=pretrained,
                                  input_shape=(224, 224, 3),
                                  classes=10)
        # elif arch == 'efficientnet-b2':
        #     self.net = EfficientNetB2(include_top=False,
        #                             weights=pretrained,
        #                             input_shape=(260, 260, 3),
        #                             classes=10)

        self.feature_3 = self.net.get_layer('block4a_expand_activation').output
        self.feature_2 = self.net.get_layer('block6a_expand_activation').output
        self.feature_1 = self.net.get_layer('top_activation').output

        self.feature_extractor = tf.keras.Model(
            inputs=self.net.input,
            outputs=[self.feature_3, self.feature_2, self.feature_1])
示例#12
0
def create_efficientNet():
    if image_model == 4:
        baseModel = EfficientNetB4(weights='imagenet',
                                   include_top=False,
                                   input_shape=input_size)
    elif image_model == 2:
        baseModel = EfficientNetB2(weights='imagenet',
                                   include_top=False,
                                   input_shape=input_size)
    elif image_model == 0:
        baseModel = EfficientNetB0(weights='imagenet',
                                   include_top=False,
                                   input_shape=input_size)
    probs = baseModel.layers.pop()
    top_conv = probs.input
    headModel = layers.Activation(swish, name='top_activation')(top_conv)
    headModel = layers.GlobalAveragePooling2D(name='avg_pool')(headModel)
    headModel = layers.Dropout(0.2, name='top_dropout')(headModel)
    headModel = layers.Dense(num_classes, activation='softmax')(headModel)
    model = Model(inputs=baseModel.input, outputs=headModel)
    return model
示例#13
0
    def __init__(self):

        self.num_cls = 5
        self.imag_size = 224

        model = EfficientNetB0(weights='imagenet',
                               input_shape=(self.imag_size, self.imag_size, 3),
                               include_top=False)

        outputs = model.output
        outputs = Flatten()(outputs)

        Hidden1_inputs = Dense(1024, activation="relu")(outputs)
        Hidden1_inputs = Dropout(0.5)(Hidden1_inputs)

        predictions = Dense(units=self.num_cls,
                            activation="softmax")(Hidden1_inputs)
        self.model_f = Model(inputs=model.input, outputs=predictions)
        self.model_f.compile(
            optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999,
                            epsilon=1e-08),
            loss='categorical_crossentropy',
            metrics=[metrics.mae, metrics.categorical_accuracy])
示例#14
0
def Eff_net_B0_simplest(Net_file_path, Trainable='All', load=True):

    # loading pretrained conv base model
    conv_base = EfficientNetB0(weights="imagenet",
                               include_top=False,
                               input_shape=(200, 200, 3))

    dropout_rate = 0.2
    model = models.Sequential()
    model.add(conv_base)
    model.add(layers.Flatten(name="flatten"))
    model.add(tf.keras.layers.BatchNormalization())
    model.add(layers.Dense(32, activation="relu", name="Dense1_add"))
    # model.add(tf.keras.layers.BatchNormalization())
    model.add(layers.Dropout(dropout_rate))
    model.add(layers.Dense(16, activation="relu", name="Dense2_add"))
    # model.add(tf.keras.layers.BatchNormalization())
    model.add(layers.Dropout(dropout_rate))

    model.add(layers.Dense(1, activation="sigmoid", name="fc_out"))

    if load:
        model.load_weights(Net_file_path)

    if Trainable == 'Classifier':
        conv_base.trainable = False

    elif Trainable == 'Class_FirstConv':
        conv_base.trainable = True

        set_trainable = False
        for layer in conv_base.layers:
            if layer.name == 'multiply_16':
                set_trainable = True
            if set_trainable:
                layer.trainable = True
            else:
                layer.trainable = False

    elif Trainable == 'Class_SecondConv':
        conv_base.trainable = True

        set_trainable = False
        for layer in conv_base.layers:
            if layer.name == 'multiply_14':
                set_trainable = True
            if set_trainable:
                layer.trainable = True
            else:
                layer.trainable = False
    elif Trainable == 'Class_ThirdConv':
        conv_base.trainable = True

        set_trainable = False
        for layer in conv_base.layers:
            if layer.name == 'multiply_12':
                set_trainable = True
            if set_trainable:
                layer.trainable = True
            else:
                layer.trainable = False

    return model
示例#15
0
print(f'Train of Label : {x_test.shape}')
print(f'Test of Label : {y_test.shape}')

x_test = x_test[:5138]
y_test = y_test[:5138]
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)

# model
BATCH_SIZE = 64
EPOCHS = 10

model = tf.compat.v1.keras.models.Sequential([
    EfficientNetB0(include_top=False, input_shape=(224, 224, 3),
                   pooling='avg'),
    tf.compat.v1.keras.layers.Dense(20),
    tf.compat.v1.keras.layers.BatchNormalization(),
    tf.compat.v1.keras.layers.Activation('softmax')
])

# fitting
model.compile(loss='sparse_categorical_crossentropy',
              optimizer=tf.keras.optimizers.Adam(lr=0.01),
              metrics=['accuracy'])
hist = model.fit(x_train,
                 y_train,
                 epochs=EPOCHS,
                 batch_size=BATCH_SIZE,
                 validation_split=0.2)
示例#16
0
def run_inference(tfrecords_folder, output_folder, batch_size=1000):
    Path(output_folder).mkdir(parents=True, exist_ok=True)
    model = EfficientNetB0(weights="imagenet", include_top=False, pooling="avg")
    tfrecords_to_write_embeddings(tfrecords_folder, output_folder, model, batch_size)
示例#17
0
def run_inference_from_files(image_folder, output_folder, num_shards=10, batch_size=1000):
    model = EfficientNetB0(weights="imagenet", include_top=False, pooling="avg")
    list_ds = list_files(image_folder)
    compute_save_embeddings(list_ds, output_folder, num_shards, model, batch_size)
示例#18
0
#from efficientnet.tfkeras import center_crop_and_resize, preprocess_input
from tensorflow.keras import models
from tensorflow.keras import layers
from keras.datasets import cifar100
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import optimizers
from tensorflow.keras.utils import plot_model
from IPython.display import Image
from keras.utils import to_categorical
import matplotlib.pyplot as plt
from skimage.transform import resize
import numpy as np
import cv2
from keras import backend as K

baseModel = EfficientNetB0(weights="imagenet")
#print(baseModel.summary())

dropout_rate = 0.2
batch_size = 64
epochs = 25

baseModel = EfficientNetB0(weights="imagenet", include_top=False)
#print(baseModel.summary())

model = models.Sequential()
model.add(baseModel)
model.add(layers.GlobalMaxPooling2D(name="gmp"))

if dropout_rate > 0:
    model.add(layers.Dropout(dropout_rate, name="dropout_out"))
示例#19
0
        f.write("Model : " + model_name)
        f.close()

    else:
        pass

    train_ds = make_tf_dataset(train_images, train_labels)
    valid_ds = make_tf_dataset(valid_images, valid_labels)

    train_ds = train_ds.repeat().batch(BATCH_SIZE)
    train_ds = train_ds.prefetch(AUTOTUNE)
    valid_ds = valid_ds.repeat().batch(BATCH_SIZE)
    valid_ds = valid_ds.prefetch(AUTOTUNE)

    base_model = EfficientNetB0(input_shape=(IMG_SIZE, IMG_SIZE, 3),
                                weights="imagenet",
                                include_top=False)
    avg = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)
    output = tf.keras.layers.Dense(train_labels_len, activation="sigmoid")(avg)
    model = tf.keras.Model(inputs=base_model.input, outputs=output)

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

    # optimizer = tf.keras.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    # model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"])
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model.summary()
示例#20
0
# train_generator = CutMixImageDataGenerator(
#     generator1=train_generator1,
#     generator2=train_generator2,
#     img_size=(224,224),
#     batch_size=64
# )
test_generator = test_datagen.flow_from_directory(
    '/home/john/Study/pytorch_retinaface/facedetect/data/test',
    target_size=(224, 224),
    batch_size=64,
    class_mode='binary')

# '/home/john/Study/mask/mask_model_0925.h5'
# 2. 모델
model = Sequential()
model.add(EfficientNetB0(include_top=False, pooling='avg'))
model.add(Dropout(0.1, name='hidden1'))
#model.add(GlobalAveragePooling2D(name='hidden2'))
model.add(Dense(1, activation='sigmoid', name='s1'))
model.summary()
# 3. 훈련

# tb_hist = TensorBoard(log_dir='./graph', histogram_freq=0, write_graph=True, write_images=True, update_freq='batch')
# parallel_model = multi_gpu_model(model, gpus=['/xla_gpu:1', '/xla_gpu:2'])
model.compile(optimizer='adam',
              loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=0.9),
              metrics=['acc'])
hist = model.fit_generator(train_generator,
                           steps_per_epoch=100,
                           epochs=100,
                           validation_data=test_generator,
import os
import sys
import numpy as np
from skimage.io import imread
import matplotlib.pyplot as plt
sys.path.append('..')
# if you use tensorflow.keras:
from efficientnet.tfkeras import EfficientNetB0
from efficientnet.tfkeras import center_crop_and_resize, preprocess_input
from tensorflow.keras.applications.imagenet_utils import decode_predictions
# test image
test_b_2 = "../predict/pics/ROI.png"
image = imread(test_b_2)

plt.figure(figsize=(10, 10))
plt.imshow(image)
plt.show()

# loading pretrained model
model = EfficientNetB0(weights='imagenet')

# preprocess input
image_size = model.input_shape[1]
x = center_crop_and_resize(image, image_size=image_size)
x = preprocess_input(x)
print(x.shape)
x = np.expand_dims(x, 0)

# make prediction and decode
y = model.predict(x)
print(decode_predictions(y))
示例#22
0
                                   rotation_range=45,
                                   width_shift_range=0.2,
                                   height_shift_range=0.2)

# Validation data generator
datagen_val = ImageDataGenerator(rescale=1. / 255)

##################################
## MODEL #########################
##################################

input_shape = (IMG_ROWS, IMG_COLS, 3)

#### Efficient ####
effnet = EfficientNetB0(weights='imagenet',
                        include_top=False,
                        input_shape=input_shape)

effnet.trainable = True

x = effnet.output
x = Flatten()(x)
x = Dense(2048, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.5)(x)

predictions = Dense(NUM_CLASSES, activation='sigmoid')(x)
model = Model(inputs=effnet.input, outputs=predictions)
###################
示例#23
0
def build_model(base_model, input_shape, metrics, loss, loss_weights,
                **kwargs):

    if base_model == 'resnet50':
        from tensorflow.keras.applications import ResNet50
        base_model = ResNet50(include_top=False,
                              weights='imagenet',
                              input_shape=input_shape)
    if base_model == 'densenet121':
        from tensorflow.keras.applications import DenseNet121
        base_model = DenseNet121(include_top=False,
                                 weights='imagenet',
                                 input_shape=input_shape)
    if base_model == 'efficientnetb0':
        from efficientnet.tfkeras import EfficientNetB0
        base_model = EfficientNetB0(include_top=False,
                                    weights='imagenet',
                                    input_shape=input_shape)
    if base_model == 'efficientnetb1':
        from efficientnet.tfkeras import EfficientNetB1
        base_model = EfficientNetB1(include_top=False,
                                    weights='imagenet',
                                    input_shape=input_shape)
    if base_model == 'efficientnetb2':
        from efficientnet.tfkeras import EfficientNetB2
        base_model = EfficientNetB2(include_top=False,
                                    weights='imagenet',
                                    input_shape=input_shape)

    if base_model == 'efficientnetb3':
        from efficientnet.tfkeras import EfficientNetB3
        base_model = EfficientNetB3(include_top=False,
                                    weights='imagenet',
                                    input_shape=input_shape)
    if base_model == 'efficientnetb4':
        from efficientnet.tfkeras import EfficientNetB4
        base_model = EfficientNetB4(include_top=False,
                                    weights='imagenet',
                                    input_shape=input_shape)
    if base_model == 'efficientnetb5':
        from efficientnet.tfkeras import EfficientNetB5
        base_model = EfficientNetB5(include_top=False,
                                    weights='imagenet',
                                    input_shape=input_shape)

    x_in = Input(shape=input_shape)
    x = Conv2D(3, (3, 3), padding='same')(x_in)
    x = base_model(x)

    x = GlobalAvgPool2D()(x)
    x = BatchNormalization()(x)
    x = Dropout(0.5)(x)

    out_grapheme = Dense(168, activation='softmax', name='root')(x)
    out_vowel = Dense(11, activation='softmax', name='vowel')(x)
    out_consonant = Dense(7, activation='softmax', name='consonant')(x)

    model = Model(inputs=x_in,
                  outputs=[out_grapheme, out_vowel, out_consonant])
    model.compile(Adam(lr=0.0001),
                  metrics=metrics,
                  loss=loss,
                  loss_weights=loss_weights)

    return model
示例#24
0
from efficientnet.tfkeras import EfficientNetB0
import numpy as np

image = np.random.randint(0, 255, (1, 224, 224, 3)) / 255.
label = np.array([1])

model = EfficientNetB0()

model.compile(loss='sparse_categorical_crossentropy',
              optimizer='adam',
              metrics=['acc'])
model.fit(image, label, epochs=1)

pred = model.predict(image)
print(pred)
示例#25
0
                         "dogs.csv")
dogs_photos_path = os.path.join(embedding_utils.DATASET_PATH, "dog_data_small",
                                "dogs_photos.csv")
dog_images_path = os.path.join(embedding_utils.DATASET_PATH,
                               "resized_dogs_small")
embeddings_path = os.path.join(embedding_utils.DATASET_PATH,
                               "embeddings_small")

# Variable to hold the dogs embeddings
embeddings = None
stacked_embeddings = None
embeddings_id_to_name = None
faiss_index = None
dogs = None
efficientnet_model = EfficientNetB0(weights='imagenet',
                                    include_top=False,
                                    pooling="avg")


def create_faiss_index(stacked_embeddings):
    d = 1280
    index = faiss.IndexFlatIP(d)
    index.add(stacked_embeddings)
    return index


def faiss_search(index, id_to_name, emb, k=5):
    # Peform actual search
    D, I = index.search(np.expand_dims(emb, 0), k)
    return list(zip(D[0], [id_to_name[x] for x in I[0]]))
        headModel = baseModel.output
        headModel = Dense(51, activation='softmax')(headModel)
    elif network == "NASNetMobile":
        print(network)
        train, test, lb2, labelsTest = preprocessing(network)
        baseModel = NASNetMobile(weights=pretraining,
                                 include_top=False,
                                 input_tensor=Input(shape=(224, 224, 3)),
                                 pooling="avg")
        headModel = baseModel.output
        headModel = Dense(51, activation='softmax')(headModel)
    elif network == "EfficientNetB0":
        print(network)
        train, test, lb2, labelsTest = preprocessing_EfficcientNet()
        baseModel = EfficientNetB0(weights=pretraining,
                                   include_top=False,
                                   input_tensor=Input(shape=(224, 224, 3)),
                                   pooling="avg")
        headModel = baseModel.output
        headModel = Dropout(0.2)(headModel)
        headModel = Dense(51, activation='softmax')(headModel)
    else:
        print(network)
        train, test, lb2, labelsTest = preprocessing_EfficcientNet()
        baseModel = EfficientNetB3(weights=pretraining,
                                   include_top=False,
                                   input_tensor=Input(shape=(224, 224, 3)),
                                   pooling="avg")
        headModel = baseModel.output
        headModel = Dropout(0.2)(headModel)
        headModel = Dense(51, activation='softmax')(headModel)
示例#27
0
test_datagen = ImageDataGenerator(rescale=1 /
                                  255  #rescale the tensor values to [0,1]
                                  )

test_generator = test_datagen.flow_from_directory(directory=test_path,
                                                  classes=['real', 'fake'],
                                                  target_size=(input_size,
                                                               input_size),
                                                  color_mode="rgb",
                                                  class_mode=None,
                                                  batch_size=1,
                                                  shuffle=False)

# Train a CNN classifier
efficient_net = EfficientNetB0(weights='imagenet',
                               input_shape=(input_size, input_size, 3),
                               include_top=False,
                               pooling='max')

model = Sequential()
model.add(efficient_net)
model.add(Dense(units=512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=128, activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))
model.summary()

# Compile model
model.compile(optimizer=Adam(lr=0.0001),
              loss='binary_crossentropy',
              metrics=['accuracy'])
示例#28
0
文件: train.py 项目: flowersai/birds
def train_model(CONFIG):
    checkpoint_path = CONFIG['checkpoint_path'].format(
        experiment_i=CONFIG['experiment_i'])
    checkpoint_dir = os.path.dirname(checkpoint_path)

    # Create a callback that saves the model's weights every 1 epoch
    cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
                                                     verbose=1,
                                                     save_weights_only=True,
                                                     save_best_only=True)

    #early_stopping_callback = tf.keras.callbacks.EarlyStopping(
    #    monitor='val_loss', min_delta=0, patience=5, verbose=0, mode='min',
    #    baseline=None, restore_best_weights=True
    #)

    tensorboard_callback = tf.keras.callbacks.TensorBoard(
        log_dir=CONFIG['log_dir'].format(experiment_i=CONFIG['experiment_i']),
        update_freq='epoch')

    callbacks = [tensorboard_callback, cp_callback]

    image_dir = CONFIG['image_dir']

    base_model = EfficientNetB0(weights='noisy-student', include_top=False)
    base_model.trainable = True

    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1000, activation='relu')(x)
    x = Dropout(rate=0.2)(x)
    preds = Dense(200, activation='softmax')(x)

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

    train_datagen = ImageDataGenerator(rotation_range=20,
                                       zoom_range=0.15,
                                       width_shift_range=0.2,
                                       height_shift_range=0.2,
                                       shear_range=0.15,
                                       horizontal_flip=True,
                                       fill_mode="nearest")
    val_datagen = ImageDataGenerator()

    train_generator = train_datagen.flow_from_directory(
        directory=os.path.join(image_dir, 'train'),
        batch_size=32,
        target_size=(224, 224),
        seed=3,
        shuffle=True,
        class_mode="categorical")
    val_generator = val_datagen.flow_from_directory(directory=os.path.join(
        image_dir, 'val'),
                                                    batch_size=32,
                                                    target_size=(224, 224),
                                                    seed=3,
                                                    shuffle=False,
                                                    class_mode="categorical")

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

    step_size_train = train_generator.n // train_generator.batch_size
    step_size_val = val_generator.n // val_generator.batch_size

    model.fit(train_generator,
              validation_data=val_generator,
              validation_steps=step_size_val,
              steps_per_epoch=step_size_train,
              epochs=CONFIG['epochs'],
              callbacks=callbacks)

    path_to_save = CONFIG['saved_model'].format(
        experiment_i=CONFIG['experiment_i'])
    print(f"Saving model to {path_to_save}...")
    model.save(path_to_save)
    print("Done")

    return model