def __init__(self,
                 model_id='efficientnet_b4',
                 input_shape=(256, 256, 3),
                 output_stride=16):
        assert model_id == 'efficientnet_b4'

        super(EfficientNet_B4, self).__init__()
        self.output_stride = output_stride
        self.features = EfficientNetB4(
            input_shape=input_shape,
            include_top=False,
            weights='imagenet',
            classifier_activation=None,
        )
        self.features.trainable = False
        self.conv = Conv2D(1024, 3, padding='same')
        self.dconv1 = Conv2DTranspose(1024, 3, strides=2, padding='same')
        self.dconv2 = Conv2DTranspose(1024, 3, strides=2, padding='same')
        self.heatmap = Conv2D(17, 1, 1, activation='sigmoid', padding='same')
        self.offset = Conv2D(34, 1, 1, padding='same')
        self.displacement_fwd = Conv2D(32, 1, 1, padding='same')
        self.displacement_bwd = Conv2D(32, 1, 1, padding='same')
rl = ReduceLROnPlateau(patience=10, verbose=1)

x = np.load('c:/data/npy/lotte_xs.npy')

y = np.load('c:/data/npy/lotte_ys.npy')

test = np.load('c:/data/npy/lotte_tests.npy')

submission = pd.read_csv('c:/LPD_competition/sample.csv')

# mob = MobileNet(
#     include_top=False,
#     input_shape=(128, 128, 3)
# )

eff = EfficientNetB4(include_top=False, input_shape=(128, 128, 3))

# mob.trainable = True
eff.trainable = True

batch_size = 16
epochs = len(x) // batch_size

x = preprocess_input(x)
test = preprocess_input(test)

count = 0
results = 0
for train_index, val_index in kf.split(x, y):
    print(str(count) + ' 번째 훈련 시작')
示例#3
0
        return X, y


# y = np.argmax(y, axis=1)

x_train, x_valid, y_train, y_valid = train_test_split(x,y, train_size = 0.8, shuffle = True, random_state=SEED)
train_generator  =  MixupGenerator (x_train , y_train , batch_size = 32 , alpha = 0.2 , datagen = idg) ()
valid_generator = idg2.flow(x_valid,y_valid)
test_generator = x_pred

from keras.utils.generic_utils import get_custom_objects
from tensorflow.python.keras.activations import swish
from keras.layers import Activation
get_custom_objects().update({'swish': Activation(swish)})

md = EfficientNetB4(input_shape = IMAGE_SIZE, weights = "imagenet", include_top = False)
for layer in md.layers:
    layer.trainable = True
x = md.output
x = GlobalAvgPool2D(name='global_avg')(md.output)
prediction = Dense(1000, activation='softmax')(x)
model = Model(inputs=md.input, outputs=prediction)

model.summary()

from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau

from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
cp = ModelCheckpoint('../../data/modelcheckpoint/lotte_projcet31.h5',monitor='val_acc',save_best_only=True, verbose=1)
early_stopping = EarlyStopping(monitor='val_acc',patience= 12,verbose=1)
lr = ReduceLROnPlateau(monitor='val_loss',patience= 6, factor=0.3,verbose=1)
示例#4
0
                                                    y,
                                                    train_size=0.8,
                                                    shuffle=True,
                                                    random_state=42)

aaa = 1
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2],
                          aaa)
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], aaa)
print(x_train.shape, y_train.shape)  # (3628, 128, 862, 1) (3628,)
print(x_test.shape, y_test.shape)  # (908, 128, 862, 1) (908,)

model = EfficientNetB4(
    include_top=True,
    input_shape=(128, 862, 1),
    classes=2,
    pooling=None,
    weights=None,
)

model.summary()
# model.trainable = False

model.save('C:/nmb/nmb_data/h5/5s/EfficientNet/efficientnet_nadam_1.h5')

# 컴파일, 훈련
op = Nadam(lr=1e-3)
batch_size = 4

es = EarlyStopping(monitor='val_loss',
                   patience=20,
示例#5
0
# y = np.argmax(y, axis=1)
from sklearn.model_selection import train_test_split
x_train, x_valid, y_train, y_valid = train_test_split(x,y, train_size = 0.9, shuffle = True, random_state=66)

train_generator = idg.flow(x_train,y_train,batch_size=32, seed = 1024)
# seed => random_state
valid_generator = idg2.flow(x_valid,y_valid)
# test_generator = idg2.flow(x_pred)

mc = ModelCheckpoint('../data/modelcheckpoint/lotte_12.h5',save_best_only=True, verbose=1)


from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalAveragePooling2D, Flatten, BatchNormalization, Dense, Activation
efficientnet = EfficientNetB4(include_top=False,weights='imagenet',input_shape=x_train.shape[1:])
efficientnet.trainable = True
a = efficientnet.output
a = Dense(2048, activation= 'swish') (a)
a = Dropout(0.3) (a)
a = GlobalAveragePooling2D() (a)
a = Dense(1000, activation= 'softmax') (a)

model = Model(inputs = efficientnet.input, outputs = a)

from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
early_stopping = EarlyStopping(patience= 15)
lr = ReduceLROnPlateau(patience= 5, factor=0.5, verbose=1)
from tensorflow.keras.optimizers import Adam

model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.0005),
示例#6
0
                                                  shuffle=True)

#control
image_size = (256, 256, 3)
bts = 16
optimizer = Adam(learning_rate=0.001)

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

#2. MODEL
from tensorflow.keras.applications import EfficientNetB4

TF = EfficientNetB4(weights="imagenet",
                    include_top=False,
                    input_shape=image_size)
TF.trainable = True
x = TF.output
x = GlobalAveragePooling2D()(x)
x = Flatten()(x)
x = Dense(4096, activation='relu')(x)
x = Dropout(0.2)(x)
outputs = Dense(1000, activation='softmax')(x)
model = Model(inputs=TF.input, outputs=outputs)
model.summary()

#COMPILE
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau

model.compile(loss='categorical_crossentropy',
示例#7
0
                                                    train_size=0.9,
                                                    shuffle=True,
                                                    random_state=42)

train_generator = idg.flow(x_train, y_train, batch_size=32, seed=42)
# seed => random_state
test_generator = idg2.flow(x_test, y_test)
# pred_generator = x_pred
print(x_train.shape, y_train.shape)

from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalAveragePooling2D, Flatten, BatchNormalization, Dense, Activation
from tensorflow.keras.applications import VGG19, MobileNet, EfficientNetB4, EfficientNetB7

ef = EfficientNetB4(weights="imagenet",
                    include_top=False,
                    input_shape=(250, 250, 3))
top_model = ef.output
top_model = Flatten()(top_model)
top_model = Dense(1000, activation="softmax")(top_model)

model = Model(inputs=ef.input, outputs=top_model)

model.summary()

import time
import datetime
start = time.time()

from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
es = EarlyStopping(patience=10)
示例#8
0
# # y = np.argmax(y, axis=1)
# from sklearn.model_selection import train_test_split
# x_train, x_valid, y_train, y_valid = train_test_split(x,y, train_size = 0.9, shuffle = True, random_state=66)

# train_generator = idg.flow(x_train,y_train,batch_size=16, seed = 1024)
# # seed => random_state
# valid_generator = idg2.flow(x_valid,y_valid, batch_size=16)
# # test_generator = idg2.flow(x_pred)

# mc = ModelCheckpoint('../data/modelcheckpoint/lotte_13.h5',save_best_only=True, verbose=1)


from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalAveragePooling2D, Flatten, BatchNormalization, Dense, Activation
efficientnet = EfficientNetB4(include_top=False,weights='imagenet',input_shape=(160,160,3))
efficientnet.trainable = True
a = efficientnet.output
a = Dense(2048, activation= 'swish') (a)
a = Dropout(0.3) (a)
a = GlobalAveragePooling2D() (a)
a = Dense(1000, activation= 'softmax') (a)

model = Model(inputs = efficientnet.input, outputs = a)

from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
early_stopping = EarlyStopping(patience= 15)
lr = ReduceLROnPlateau(patience= 5, factor=0.5, verbose=1)
from tensorflow.keras.optimizers import Adam

# model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.0005),
示例#9
0
def create_efficientnet(width, height, depth, model_base,
                        first_layers_to_freeze, num_classes, learning_rate,
                        epochs):
    inputShape = (height, width, depth)

    inputs = K.Input(shape=inputShape)

    if model_base == "b0":
        effnet = EfficientNetB0(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b1":
        effnet = EfficientNetB1(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b2":
        effnet = EfficientNetB2(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b3":
        effnet = EfficientNetB3(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b4":
        effnet = EfficientNetB4(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b5":
        effnet = EfficientNetB5(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b6":
        effnet = EfficientNetB6(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    else:
        effnet = EfficientNetB7(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")

    # # Print architecture of effnet
    # for i, layer in enumerate(effnet.layers[:]):
    # 	print(i, layer.name, layer.output_shape)
    # print(f"Effnet len: {len(effnet.layers[:])}")

    # b0: 20; b2: 33; b4: 236; b6: 45; b7: 265
    for i, layer in enumerate(effnet.layers[:first_layers_to_freeze]):
        layer.trainable = False
    for i, layer in enumerate(effnet.layers[first_layers_to_freeze:]):
        if not isinstance(layer, K.layers.BatchNormalization):
            layer.trainable = True

    model = Sequential()
    model.add(effnet)
    model.add(K.layers.Dropout(0.25))
    model.add(K.layers.Dense(effnet.layers[-1].output_shape[3]))
    model.add(K.layers.LeakyReLU())
    model.add(K.layers.GlobalAveragePooling2D())
    model.add(K.layers.BatchNormalization())
    model.add(K.layers.Dropout(0.5))
    model.add(K.layers.Dense(num_classes, activation='softmax'))

    # Freeze the batchnorm layer of our model
    for i, layer in enumerate(model.layers[:]):
        if isinstance(layer, K.layers.BatchNormalization):
            layer.trainable = False

    opt = Adam(lr=learning_rate, decay=learning_rate / epochs)
    model.compile(loss="categorical_crossentropy",
                  optimizer=opt,
                  metrics=["accuracy"])

    model.summary()

    return model
示例#10
0
# Train-Val = [:21000], [21000:], test_files
# Train-Val-Test (for tuning model) = [:18000], [18000:24000], [24000:]

#----------------EfficientNetB4------------------------------------------------------------------------------

# base_model = ResNet50(include_top=False,
#                       weights=None,
#                       input_shape=(image_size, image_size, 3))
# base_model.load_weights("../input/keras-pretrained-models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5", by_name=True)

# for layer in base_model.layers[:-6]:
#     layer.trainable = False

base_model = EfficientNetB4(include_top=False,
                            weights="imagenet",
                            input_shape=(image_size, image_size, 3))
# base_model.load_weights("../input/tfkeras-22-pretrained-and-vanilla-efficientnet/TF2.2_EfficientNetB4_NoTop_ImageNet.h5", by_name=True)
# base_model.trainable = False

for layer in base_model.layers[:-6]:
    if not isinstance(layer, BatchNormalization):
        layer.trainable = False

#----------------IMAGE DATA GENERATOR------------------------------------------------------------------
label_cols = df_train.columns.tolist()
label_cols.remove("StudyInstanceUID")
label_cols.remove("PatientID")
datagen = ImageDataGenerator(rescale=1. / 255.)
test_datagen = ImageDataGenerator(rescale=1. / 255.)
示例#11
0
mc = ModelCheckpoint('C:/LPD_competition/lotte_0318_1.h5',
                     save_best_only=True,
                     verbose=1)

train_generator = idg.flow(x_train, y_train, batch_size=24)
# seed => random_state
valid_generator = idg2.flow(x_valid, y_valid)
test_generator = x_pred

from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalAveragePooling2D, Flatten, BatchNormalization, Dense, Activation
from tensorflow.keras.applications import VGG19, MobileNet, ResNet50
from keras.layers import concatenate, Concatenate

EfficientNetB4 = EfficientNetB4(weights="imagenet",
                                include_top=False,
                                input_shape=(128, 128, 3))
a = EfficientNetB4.output
a = GlobalAveragePooling2D()(a)
a = Flatten()(a)
a = Dense(2024, activation='relu')(a)

mobile_net = MobileNet(weights="imagenet",
                       include_top=False,
                       input_shape=(128, 128, 3))
b = mobile_net.output
b = GlobalAveragePooling2D()(b)
b = Flatten()(b)
b = Dense(2024, activation='relu')(b)

# resnet50 = ResNet50(weights="imagenet", include_top=False, input_shape=(128, 128, 3))
示例#12
0
                                             target_size=(128, 128),
                                             batch_size=16,
                                             class_mode='sparse',
                                             subset='training')
xy_val = train_datagen.flow_from_directory('../data/lotte/train',
                                           target_size=(128, 128),
                                           batch_size=16,
                                           class_mode='sparse',
                                           subset='validation')
# Found 39000 images belonging to 1000 classes.
# Found 9000 images belonging to 1000 classes.
print(xy_train[0][0].shape)  # (16, 128, 128, 3)
print(xy_train[0][1].shape)  # (16,)

effi = EfficientNetB4(weights='imagenet',
                      include_top=False,
                      input_shape=(128, 128, 3))
effi.trainable = False

# model = Sequential()
# model.add(effi)
# model.add(Flatten())
# model.add(Dense(4096, activation='relu'))
# model.add(Dropout(0.3))
# model.add(Dense(1000, activation='softmax'))

# model.summary()

# from tensorflow.keras.optimizers import Adam
# optimizer = Adam(lr=0.001)
# model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['sparse_categorical_accuracy'])
示例#13
0
                                             batch_size=batch,
                                             class_mode='sparse',
                                             subset='training',
                                             seed=seed)
xy_val = train_datagen.flow_from_directory('../data/lotte/train_new',
                                           target_size=(img_size, img_size),
                                           batch_size=batch,
                                           class_mode='sparse',
                                           subset='validation',
                                           seed=seed)

from tensorflow.keras.applications import EfficientNetB4
from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalAveragePooling2D, Flatten, BatchNormalization, Dense, Activation, GaussianDropout
efficientnet = EfficientNetB4(include_top=False,
                              weights='imagenet',
                              input_shape=(img_size, img_size, 3))
efficientnet.trainable = True
a = efficientnet.output
a = Dense(2048, activation='swish')(a)
a = GaussianDropout(0.3)(a)
a = GlobalAveragePooling2D()(a)
a = Dense(1000, activation='softmax')(a)

model = Model(inputs=efficientnet.input, outputs=a)

from tensorflow.keras.optimizers import Adam
optimizer = Adam(lr=0.0005)
model.compile(loss='sparse_categorical_crossentropy',
              optimizer=optimizer,
              metrics=['sparse_categorical_accuracy'])
示例#14
0
ds_train = ds_train.map(input_preprocess,
                        num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds_train = ds_train.batch(batch_size=batch_size, drop_remainder=True)
ds_train = ds_train.prefetch(tf.data.experimental.AUTOTUNE)

ds_test = ds_test.map(input_preprocess)
ds_test = ds_test.batch(batch_size=batch_size, drop_remainder=True)

from tensorflow.keras.applications import EfficientNetB4

with strategy.scope():
    inputs = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3))
    x = img_augmentation(inputs)
    outputs = EfficientNetB4(include_top=True,
                             weights=None,
                             classes=NUM_CLASSES)(x)

    model = tf.keras.Model(inputs, outputs)
    model.compile(optimizer="adam",
                  loss="categorical_crossentropy",
                  metrics=["accuracy"])

model.summary()

epochs = 10  # @param {type: "slider", min:10, max:100}
hist = model.fit(ds_train, epochs=epochs, validation_data=ds_test, verbose=2)

import matplotlib.pyplot as plt

示例#15
0
def create_efficientnet(width, height, depth, model_base,
                        first_layers_to_freeze):
    inputShape = (height, width, depth)

    inputs = K.Input(shape=inputShape)

    if model_base == "b0":
        effnet = EfficientNetB0(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b1":
        effnet = EfficientNetB1(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b2":
        effnet = EfficientNetB2(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b3":
        effnet = EfficientNetB3(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b4":
        effnet = EfficientNetB4(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b5":
        effnet = EfficientNetB5(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    elif model_base == "b6":
        effnet = EfficientNetB6(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")
    else:
        effnet = EfficientNetB7(include_top=False,
                                input_tensor=inputs,
                                weights="imagenet")

    # # Print architecture of effnet
    # for i, layer in enumerate(effnet.layers[:]):
    # 	print(i, layer.name, layer.output_shape)

    # b0: 20; b2: 33; b4: 147; b6: 45; b7: 265

    for i, layer in enumerate(effnet.layers[:first_layers_to_freeze]):
        layer.trainable = False
    for i, layer in enumerate(effnet.layers[first_layers_to_freeze:]):
        layer.trainable = True

    effnet.summary()

    model = Sequential()
    model.add(effnet)
    model.add(K.layers.Dropout(0.25))
    model.add(K.layers.Dense(effnet.layers[-1].output_shape[3]))
    model.add(K.layers.LeakyReLU())
    model.add(K.layers.GlobalAveragePooling2D())
    model.add(K.layers.Dropout(0.5))
    model.add(K.layers.Dense(1, activation='linear'))

    return model