Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<define_variables>
print(tf.__version__)
 print(tf.keras.__version__ )
SEED = 7
np.random.seed(SEED)
 set_random_seed(SEED)
 dir_path = "/kaggle/input/"
IMG_DIM = 299
BATCH_SIZE = 8
CHANNEL_SIZE = 3
NUM_EPOCHS = 60
TRAIN_DIR = 'train_images'
TEST_DIR = 'test_images'
FREEZE_LAYERS = 2
CLASSS = {0: "No DR", 1: "Mild", 2: "Moderate", 3: "Severe", 4: "Proliferative DR"}
NUM_CLASSS = 5<define_variables>
!pip install efficientnet
ROOT_PATH = '/kaggle/input/aptos2019-blindness-detection'
TRAIN_PATH = '/kaggle/input/aptos2019-blindness-detection/' + TRAIN_DIR
TEST_PATH = '/kaggle/input/aptos2019-blindness-detection/' + TEST_DIR
dir_path = ROOT_PATH + '/'<load_from_csv>
img = plt.imread('.. /input/plant-pathology-2020-fgvc7/images/Train_0.jpg')
 print(img.shape)
 plt.imshow(img )
train_img_names = glob.glob(TRAIN_PATH + '/*.png')
 df_train = pd.read_csv(ROOT_PATH + '/train.csv')
<load_from_csv>
path='.. /input/plant-pathology-2020-fgvc7/'
train = pd.read_csv(path + 'train.csv')
 test = pd.read_csv(path + 'test.csv')
 sub = pd.read_csv(path + 'sample_submission.csv')
 train_paths = train.image_id.apply(lambda x: GCS_DS_PATH + '/images/' + x + '.jpg' ).values
test_paths = test.image_id.apply(lambda x: GCS_DS_PATH + '/images/' + x + '.jpg' ).values
train_labels = train.loc[:, 'healthy':].values
test_img_names = glob.glob(TEST_PATH + '/*.png')
 df_test = pd.read_csv(ROOT_PATH + '/test.csv')
<set_options>
train
gc.collect()<split>
train_labels
x_train, x_test, y_train, y_test = train_test_split(df_train.id_code, df_train.diagnosis, test_size=0.2,
random_state=SEED, stratify=df_train.diagnosis )<choose_model_class>
Path.ls = lambda x: list(x.iterdir() )
input_tensor = Input(shape =(299, 299, 3))
base_model = InceptionV3(weights=None, include_top=False, input_tensor=input_tensor)
 base_model.load_weights('/kaggle/input/inceptionv3/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5')
 x = base_model.output
output = BatchNormalization()(x)
 x = GlobalAveragePooling2D()(x)
 x = Dense(1024, activation='relu' )(x)
 predictions = Dense(NUM_CLASSS, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 for layer in base_model.layers:
layer.trainable = False
for layer in model.layers:
layer.trainable = True
print(model.summary() )<choose_model_class>
path_images = Path('/kaggle/input/plant-pathology-2020-fgvc7')
 path_images.ls()
epochs = 25
lrate = 0.01
decay = lrate/epochs
sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)
 model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
<feature_engineering>
nb_classes = 4
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
img_size = 768
EPOCHS = 25
print("available RAM:", psutil.virtual_memory())
 gc.collect()
print("available RAM:", psutil.virtual_memory())
 df_train.id_code = df_train.id_code.apply(lambda x: x + ".png")
 df_test.id_code = df_test.id_code.apply(lambda x: x + ".png")
 df_train['diagnosis'] = df_train['diagnosis'].astype('str' )<train_model>
x = tf.constant([1.8, 2.2], dtype=tf.float32)
 print(type(x))
tf.dtypes.cast(x, tf.int32)
 print(type(x))
train_datagen = image.ImageDataGenerator(rescale=1./ 255, validation_split=0.15, horizontal_flip=True,
vertical_flip=True, rotation_range=360, zoom_range=0.2, shear_range=0.1 )<create_dataframe>
def decode_image(filename, label=None, image_size=(img_size, img_size)) :
bits = tf.io.read_file(filename)
 image = tf.image.decode_jpeg(bits, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, image_size)
 if label is None:
return image
else:
return image, label
def data_augment(image, label=None, seed=2020):
image = tf.image.random_flip_left_right(image, seed=seed)
 image = tf.image.random_flip_up_down(image, seed=seed)
 image=tf.image.adjust_saturation(image, 2)
 if label is None:
return image
else:
return image, label
train_generator = train_datagen.flow_from_dataframe(dataframe=df_train,
directory= TRAIN_PATH + '/',
x_col='id_code',
y_col='diagnosis',
batch_size=BATCH_SIZE,
class_mode='categorical',
target_size=(IMG_DIM, IMG_DIM),
subset='training',
shaffle=True,
seed=SEED 
)
 valid_generator = train_datagen.flow_from_dataframe(dataframe=df_train,
directory= TRAIN_PATH + '/',
x_col='id_code',
y_col='diagnosis',
batch_size=BATCH_SIZE,
class_mode='categorical',
target_size=(IMG_DIM, IMG_DIM),
subset='validation',
shaffle=True,
seed=SEED 
)
 gc.collect() 
<define_variables>
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths, train_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.repeat() 
.shuffle(512)
.batch(BATCH_SIZE)
.prefetch(AUTO)
 )
NUB_TRAIN_STEPS = train_generator.n // train_generator.batch_size
NUB_VALID_STEPS = valid_generator.n // valid_generator.batch_size
NUB_TRAIN_STEPS, NUB_VALID_STEPS<choose_model_class>
test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )
eraly_stop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=3, verbose=1, mode='auto')
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', min_delta=0.0004, patience=2, factor=0.1, min_lr=1e-6, mode='auto',
verbose=1 )<train_model>
LR_START = 0.00001
LR_MAX = 0.0001 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 15
LR_SUSTAIN_EPOCHS = 3
LR_EXP_DECAY =.8
def lrfn(epoch):
if epoch < LR_RAMPUP_EPOCHS:
lr =(LR_MAX - LR_START)/ LR_RAMPUP_EPOCHS * epoch + LR_START
elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
lr = LR_MAX
else:
lr =(LR_MAX - LR_MIN)* LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS)+ LR_MIN
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)
 rng = [i for i in range(EPOCHS)]
y = [lrfn(x)for x in rng]
plt.plot(rng, y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1]))
history = model.fit_generator(generator=train_generator,
steps_per_epoch=NUB_TRAIN_STEPS,
validation_data=valid_generator,
validation_steps=NUB_VALID_STEPS,
epochs=NUM_EPOCHS,
callbacks=[eraly_stop, reduce_lr],
verbose=1)
 gc.collect()<create_dataframe>
def get_model() :
pretrained_model = efn.EfficientNetB7(weights='imagenet', include_top=False, pooling='avg', input_shape=(img_size, img_size, 3))
pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(4096, activation='relu'),
tf.keras.layers.Dense(4096, activation='relu'),
tf.keras.layers.Dense(4, activation='softmax')
 ])
 return model
test_datagen = image.ImageDataGenerator(rescale=1./ 255, validation_split=0.2, horizontal_flip=True)
 test_generator = test_datagen.flow_from_dataframe(dataframe=df_test,
directory= TEST_PATH + '/',
x_col="id_code",
target_size=(IMG_DIM, IMG_DIM),
batch_size=1,
shuffle=False,
class_mode=None,
seed=SEED)
 print(df_test.shape[0])
 gc.collect()<predict_on_test>
with strategy.scope() :
model = get_model()
model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'] )
tta_steps = 5
preds_tta = []
for i in tqdm(range(tta_steps)) :
test_generator.reset()
preds = model.predict_generator(generator=test_generator, steps=ceil(df_test.shape[0]))
preds_tta.append(preds)
<prepare_output>
%%time
history=model.fit(
 train_dataset,
steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
callbacks=[lr_callback],
epochs=EPOCHS
)
final_pred = np.mean(preds_tta, axis=0)
 predicted_class_indices = np.argmax(final_pred, axis=1)
 len(predicted_class_indices )<save_to_csv>
%%time
probs = model.predict(test_dataset )
<import_modules><EOS> 
sub.loc[:, 'healthy':] = probs
sub.to_csv('submission.csv', index=False)
 sub.head()