Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<normalization>
!pip install --quiet /kaggle/input/kerasapplications
!pip install --quiet /kaggle/input/efficientnet-git
def augment(X, Y):
print("FlipLR")
 X1 = np.append(X, [np.fliplr(x)for x in X], axis = 0)
 Y1 = np.append(Y, [np.fliplr(y)for y in Y], axis = 0)
 print("Roll")
 X = np.append(X1, [np.roll(x, 40, axis = 1)for x in X1], axis = 0)
 Y = np.append(Y1, [np.roll(y, 40, axis = 1)for y in Y1], axis = 0)
 m = X.shape[0]
np.random.seed(2000)
 index = np.arange(0, m)
 np.random.shuffle(index)
 print("Shuffle")
 X = X[index]
Y = Y[index]
return X, Y
print("Training Augmentation")
 train_X, train_Y = augment(train_X, train_Y)
 val_X, val_Y = augment(val_X, val_Y)
 print(train_X.shape, train_Y.shape)
 print(val_X.shape, val_Y.shape)
<data_type_conversions>
def seed_everything(seed=0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
seed = 0
seed_everything(seed)
 warnings.filterwarnings('ignore' )
print(train_Y[70, :, :, 0])
 train_X =(train_X.astype(np.float32)/255.0)
 train_Y =(train_Y/255.0 ).astype(np.bool ).astype(np.uint8)
 val_X =(val_X.astype(np.float32)/255.0)
 val_Y =(val_Y/255.0 ).astype(np.bool ).astype(np.uint8)
 print(train_X.dtype, train_Y.dtype)
 print(train_Y[70, :, :, 0] )<import_modules>
strategy = tf.distribute.get_strategy()
AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}' )
import keras 
<import_modules>
BATCH_SIZE = 16 * REPLICAS
HEIGHT = 512
WIDTH = 512
CHANNELS = 3
N_CLASSES = 5
TTA_STEPS = 8
from keras.models import *
from keras.layers import *
from keras.optimizers import *
from keras.callbacks import *<define_search_model>
def data_augment(image, label):
p_spatial = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_rotate = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_1 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_2 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_3 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_crop = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 image = tf.image.random_flip_left_right(image)
 image = tf.image.random_flip_up_down(image)
 if p_spatial >.75:
image = tf.image.transpose(image)
 if p_rotate >.75:
image = tf.image.rot90(image, k=3)
 elif p_rotate >.5:
image = tf.image.rot90(image, k=2)
 elif p_rotate >.25:
image = tf.image.rot90(image, k=1)
 if p_pixel_1 >=.4:
image = tf.image.random_saturation(image, lower=.7, upper=1.3)
 if p_pixel_2 >=.4:
image = tf.image.random_contrast(image, lower=.8, upper=1.2)
 if p_pixel_3 >=.4:
image = tf.image.random_brightness(image, max_delta=.1)
 if p_crop >.7:
if p_crop >.9:
image = tf.image.central_crop(image, central_fraction=.7)
 elif p_crop >.8:
image = tf.image.central_crop(image, central_fraction=.8)
 else:
image = tf.image.central_crop(image, central_fraction=.9)
 elif p_crop >.4:
crop_size = tf.random.uniform([], int(HEIGHT*.8), HEIGHT, dtype=tf.int32)
 image = tf.image.random_crop(image, size=[crop_size, crop_size, CHANNELS])
 return image, label
def batchActivate(x):
x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 return x
def residualBlock(blockInput, numChannel, matchChannel = False):
x = batchActivate(blockInput)
 x = Conv2D(numChannel,(3, 3), activation= None, padding = "same",
use_bias = False )(x)
 x = batchActivate(x)
 x = Conv2D(numChannel,(3, 3), activation=None, padding = "same",
use_bias = False )(x)
 if matchChannel:
blockInput = Conv2D(numChannel,(1, 1), activation= None, padding = "same",
use_bias = False )(x)
 x = Add()([x, blockInput])
 return x<define_search_model>
def get_name(file_path):

 parts = tf.strings.split(file_path, os.path.sep)
 name = parts[-1]
return name
def decode_image(image_data):

 image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
return image
def center_crop(image):
image = tf.reshape(image, [600, 800, CHANNELS])
 h, w = image.shape[0], image.shape[1]
if h > w:
image = tf.image.crop_to_bounding_box(image,(h - w)// 2, 0, w, w)
 else:
image = tf.image.crop_to_bounding_box(image, 0,(w - h)// 2, h, h)
 image = tf.image.resize(image, [HEIGHT, WIDTH])
 return image
def resize_image(image, label):

 image = tf.image.resize(image, [HEIGHT, WIDTH])
 image = tf.reshape(image, [HEIGHT, WIDTH, CHANNELS])
 return image, label
def process_path(file_path):

 name = get_name(file_path)
 img = tf.io.read_file(file_path)
 img = decode_image(img)
 return img, name
def get_dataset(files_path, shuffled=False, tta=False, extension='jpg'):

 dataset = tf.data.Dataset.list_files(f'{files_path}*{extension}', shuffle=shuffled)
 dataset = dataset.map(process_path, num_parallel_calls=AUTO)
 if tta:
dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.map(resize_image, num_parallel_calls=AUTO)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n )
inputs = Input(shape =(im_height, im_width, 1))
init = Conv2D(16,(7, 7), activation="relu", padding = "same" )(inputs)
 conv1 = residualBlock(init, 16)
 conv1 = residualBlock(conv1, 16)
 c1 = residualBlock(conv1, 16)
 c1 = BatchNormalization()(c1)
 p1 = MaxPooling2D(pool_size=(2, 2))(c1)
 conv2 = residualBlock(p1, 32, True)
 c2 = residualBlock(conv2, 32)
 c2 = BatchNormalization()(c2)
 p2 = MaxPooling2D(pool_size =(2, 2))(c2)
 conv3 = residualBlock(p2, 64, True)
 c3 = residualBlock(conv3, 64)
 c3 = BatchNormalization()(c3)
 p3 = MaxPooling2D(pool_size =(2, 2))(c3)
 conv4 = residualBlock(p3, 128, True)
 c4 = residualBlock(conv4, 128)
 c4 = BatchNormalization()(c4)
 p4 = MaxPooling2D(pool_size=(2, 2))(c4)
 conv5 = residualBlock(p4, 256, True)
 conv5 = residualBlock(conv5, 256)
 conv5 = residualBlock(conv5, 256)
 c5 = residualBlock(conv5, 256)
 c5 = BatchNormalization()(c5)
 u6 = Conv2DTranspose(128,(2, 2), strides=(2, 2), activation = "relu",
padding = "same" )(c5)
 u6 = concatenate([u6, c4])
 conv6 = residualBlock(u6, 128, True)
 conv6 = residualBlock(conv6, 128)
 u7 = Conv2DTranspose(64,(2, 2), strides =(2, 2,), activation = "relu",
padding = "same" )(conv6)
 u7 = concatenate([u7, c3])
 conv7 = residualBlock(u7, 64, True)
 conv7 = residualBlock(conv7, 64)
 u8 = Conv2DTranspose(32,(2, 2), strides =(2, 2), activation = "relu",
padding = "same" )(conv7)
 u8 = concatenate([u8, c2])
 conv8 = residualBlock(u8, 32, True)
 conv8 = residualBlock(conv8, 32)
 u9 = Conv2DTranspose(16,(2, 2), strides =(2, 2), activation = "relu",
padding = "same" )(conv8)
 u9 = concatenate([u9, c1], axis=3)
 conv9 = residualBlock(u9, 16, True)
 conv9 = residualBlock(conv9, 16)
 conv9 = residualBlock(conv9, 16)
 outputs1 = Conv2D(16,(1, 1), activation = "relu", padding = "same" )(conv9)
 outputs = Conv2D(1,(1, 1), activation = "sigmoid" )(outputs1)
 model = Model(inputs = [inputs], outputs = [outputs])
 optimizer = keras.optimizers.Adam()
model.compile(optimizer = optimizer, loss = "binary_crossentropy",
metrics = [my_iou_metric])
 model.summary()<train_model>
model_path_list = glob.glob('/kaggle/input/cassava-leaf-disease-training-with-tpu-v2-pods/*.h5')
 model_path_list.sort()
print('Models to predict:')
 print(*model_path_list, sep='
' )
earlyStopping = EarlyStopping(patience = 20, verbose = 1)
 checkpointer = ModelCheckpoint('model-tgs-salt-1.h5',monitor = 'val_my_iou_metric',
verbose=1, save_best_only=True)
 reducelr=ReduceLROnPlateau(monitor='val_my_iou_metric',patience=5,
min_lr=0.00001, verbose=1,factor=0.5)
 epochs = 50
batch_size = 64
history = model.fit(train_X , train_Y, epochs = epochs,
batch_size = batch_size,
validation_data =(val_X, val_Y),
callbacks=[earlyStopping, checkpointer, reducelr] )<load_pretrained>
def model_fn(input_shape, N_CLASSES):
inputs = L.Input(shape=input_shape, name='input_image')
 base_model = efn.EfficientNetB4(input_tensor=inputs,
include_top=False,
weights=None,
pooling='avg')
 x = L.Dropout (.5 )(base_model.output)
 output = L.Dense(N_CLASSES, activation='softmax', name='output' )(x)
 model = Model(inputs=inputs, outputs=output)
 return model
with strategy.scope() :
model = model_fn(( None, None, CHANNELS), N_CLASSES)
 model.summary()
model1 = load_model("./model-tgs-salt-1.h5",
custom_objects = {'my_iou_metric':my_iou_metric} )<set_options>
files_path = f'{database_base_path}test_images/'
test_size = len(os.listdir(files_path))
test_preds = np.zeros(( test_size, N_CLASSES))
for model_path in model_path_list:
print(model_path)
 K.clear_session()
model.load_weights(model_path)
 if TTA_STEPS > 0:
test_ds = get_dataset(files_path, tta=True ).repeat()
ct_steps = TTA_STEPS *(( test_size/BATCH_SIZE)+ 1)
 preds = model.predict(test_ds, steps=ct_steps, verbose=1)[:(test_size * TTA_STEPS)]
preds = np.mean(preds.reshape(test_size, TTA_STEPS, N_CLASSES, order='F'), axis=1)
 test_preds += preds / len(model_path_list)
 else:
test_ds = get_dataset(files_path, tta=False)
 x_test = test_ds.map(lambda image, image_name: image)
 test_preds += model.predict(x_test)/ len(model_path_list)
 test_preds = np.argmax(test_preds, axis=-1)
 test_names_ds = get_dataset(files_path)
 image_names = [img_name.numpy().decode('utf-8')for img, img_name in iter(test_names_ds.unbatch())]
<define_variables><EOS> 
submission = pd.DataFrame({'image_id': image_names, 'label': test_preds})
 submission.to_csv('submission.csv', index=False)
 display(submission.head() )