Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<feature_engineering>
!pip install --quiet /kaggle/input/kerasapplications
!pip install --quiet /kaggle/input/efficientnet-git
train['year'] = train['year'].apply(lambda x: x-100 if x>2020 else x)
 test['year'] = test['year'].apply(lambda x: x-100 if x>2020 else x )<feature_engineering>
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' )
avg_runtime_train = train['runtime'].mean()
train['runtime'] = train['runtime'].apply(lambda x: x if str(x)!= 'nan' else avg_runtime_train)
 avg_runtime_test = test['runtime'].mean()
test['runtime'] = test['runtime'].apply(lambda x: x if str(x)!= 'nan' else avg_runtime_test )<feature_engineering>
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print(f'Running on TPU {tpu.master() }')
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}' )
train['spoken_languages_count'] = train['spoken_languages'].apply(lambda x: len(eval(x)) if str(x)!= 'nan' else 0)
 test['spoken_languages_count'] = test['spoken_languages'].apply(lambda x: len(eval(x)) if str(x)!= 'nan' else 0 )<drop_column>
BATCH_SIZE = 16 * REPLICAS
HEIGHT = 512
WIDTH = 512
CHANNELS = 3
N_CLASSES = 5
TTA_STEPS = 0
USE_REGULAR = False
USE_SCL = True
train = train.drop(['spoken_languages'], axis=1)
 test = test.drop(['spoken_languages'], axis=1 )<define_variables>
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 >.6:
if p_crop >.9:
image = tf.image.central_crop(image, central_fraction=.5)
 elif p_crop >.8:
image = tf.image.central_crop(image, central_fraction=.6)
 elif p_crop >.7:
image = tf.image.central_crop(image, central_fraction=.7)
 else:
image = tf.image.central_crop(image, central_fraction=.8)
 elif p_crop >.3:
crop_size = tf.random.uniform([], int(HEIGHT*.6), HEIGHT, dtype=tf.int32)
 image = tf.image.random_crop(image, size=[crop_size, crop_size, CHANNELS])
 image = tf.image.resize(image, size=[HEIGHT, WIDTH])
 return image, label
list_of_keywords = list(train['Keywords'].apply(lambda x: [i['name'] for i in eval(x)] if str(x)!= 'nan' else [] ).values )<feature_engineering>
database_base_path = '/kaggle/input/cassava-leaf-disease-classification/'
submission = pd.read_csv(f'{database_base_path}sample_submission.csv')
 display(submission.head())
 TEST_FILENAMES = tf.io.gfile.glob(f'{database_base_path}test_tfrecords/ld_test*.tfrec')
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 print(f'GCS: test: {NUM_TEST_IMAGES}' )
train['num_Keywords'] = train['Keywords'].apply(lambda x: len(eval(x)) if str(x)!= 'nan' else 0)
 train['all_Keywords'] = train['Keywords'].apply(lambda x: ' '.join(sorted([i['name'] for i in eval(x)])) if str(x)!= 'nan' else '')
 top_keywords = [m[0] for m in Counter([i for j in list_of_keywords for i in j] ).most_common(30)]
for g in top_keywords:
train['keyword_'+g] = train['all_Keywords'].apply(lambda x: 1 if g in x else 0)
 test['num_Keywords'] = test['Keywords'].apply(lambda x: len(eval(x)) if str(x)!= 'nan' else 0)
 test['all_Keywords'] = test['Keywords'].apply(lambda x: ' '.join(sorted([i['name'] for i in eval(x)])) if str(x)!= 'nan' else '')
 top_keywords = [m[0] for m in Counter([i for j in list_of_keywords for i in j] ).most_common(30)]
for g in top_keywords:
test['keyword_'+g] = test['all_Keywords'].apply(lambda x: 1 if g in x else 0 )<categorify>
model_path_list = glob.glob('/kaggle/input/cassava-leaf-supervised-contrastive-learning/model_reg*.h5')
 model_path_list.sort()
print('Models to predict:')
 print(*model_path_list, sep='
')
 model_path_list_scl = glob.glob('/kaggle/input/cassava-leaf-supervised-contrastive-learning/model_scl*.h5')
 model_path_list_scl.sort()
print('
Models to predict:')
 print(*model_path_list_scl, sep='
' )
keywords_encoder = LabelEncoder()
train['all_Keywords'] = keywords_encoder.fit_transform(train['all_Keywords'])
 test['all_Keywords'] = keywords_encoder.fit_transform(test['all_Keywords'] )<drop_column>
def encoder_fn(input_shape):
inputs = L.Input(shape=input_shape, name='inputs')
 base_model = efn.EfficientNetB3(input_tensor=inputs,
include_top=False,
weights=None,
pooling='avg')
 model = Model(inputs=inputs, outputs=base_model.outputs)
 return model
def classifier_fn(input_shape, N_CLASSES, encoder, trainable=True):
for layer in encoder.layers:
layer.trainable = trainable
inputs = L.Input(shape=input_shape, name='inputs')
 features = encoder(inputs)
 features = L.Dropout (.5 )(features)
 features = L.Dense(1000, activation='relu' )(features)
 features = L.Dropout (.5 )(features)
 outputs = L.Dense(N_CLASSES, activation='softmax', name='outputs', dtype='float32' )(features)
 model = Model(inputs=inputs, outputs=outputs)
 return model
train = train.drop(['Keywords'], axis=1)
 test = test.drop(['Keywords'], axis=1 )<feature_engineering>
files_path = f'{database_base_path}test_images/'
test_size = len(os.listdir(files_path))
test_preds = np.zeros(( test_size, N_CLASSES))
if USE_REGULAR:
print('Inference for regular trainining models')
 with strategy.scope() :
encoder = encoder_fn(( None, None, CHANNELS))
model = classifier_fn(( None, None, CHANNELS), N_CLASSES, encoder)
 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)
 if USE_SCL:
print('
Inference for "Supervised Contrastive Learning" models')
 with strategy.scope() :
encoder = encoder_fn(( None, None, CHANNELS))
model = classifier_fn(( None, None, CHANNELS), N_CLASSES, encoder, trainable=False)
 for model_path in model_path_list_scl:
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_scl)
 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_scl)
 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())]
<feature_engineering><EOS> 
submission = pd.DataFrame({'image_id': image_names, 'label': test_preds})
 submission.to_csv('submission.csv', index=False)
 display(submission.head() )