Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<data_type_conversions>
!pip install -q efficientnet
sales_data['d'] = sales_data['d'].map(lambda s: int(s[2:]))
sales_data['d'] = sales_data['d'].astype(np.int16)
 sales_data['sales'] = sales_data['sales'].astype(np.int16)
 sales_data['lag_7'] = sales_data['lag_7'].astype(np.int16)
 sales_data['lag_28'] = sales_data['lag_28'].astype(np.int16)
 sales_data['lag_28_rmean_7'] = sales_data['lag_28_rmean_7'].astype(np.float32)
 sales_data['lag_28_rmean_28'] = sales_data['lag_28_rmean_28'].astype(np.float32 )<data_type_conversions>
SEED = 2020
convert_categorical_columns(calendar, ['event_name_1', 'event_type_1', 'event_name_2', 'event_type_2'] )<feature_engineering>
AUTO = tf.data.experimental.AUTOTUNE
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('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()
print("REPLICAS: ", strategy.num_replicas_in_sync)
 GCS_DS_PATH = KaggleDatasets().get_gcs_path()
EPOCHS = 40
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
FOLDS = 5
calendar['day'] = calendar['date'].map(lambda s: s[-2:])
 calendar['is_weekend'] = calendar['weekday'].isin(['Saturday', 'Sunday'] )<drop_column>
def format_path(st):
return GCS_DS_PATH + '/images/' + st + '.jpg'
calendar.drop(['date', 'weekday'], axis=1, inplace=True )<data_type_conversions>
train = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/train.csv')
 test = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/test.csv')
 sub = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/sample_submission.csv')
 train_paths = train.image_id.apply(format_path ).values
test_paths = test.image_id.apply(format_path ).values
train_labels = train.loc[:, 'healthy':].values
image_size=512
calendar['d'] = calendar['d'].map(lambda s: int(s[2:]))
calendar['wm_yr_wk'] = calendar['wm_yr_wk'].astype(np.int16)
 calendar['wday'] = calendar['wday'].astype(np.int8)
 calendar['month'] = calendar['month'].astype(np.int8)
 calendar['year'] = calendar['year'].astype(np.int16)
 calendar['d'] = calendar['d'].astype(np.int16)
 calendar['snap_CA'] = calendar['snap_CA'].astype(np.int8)
 calendar['snap_TX'] = calendar['snap_TX'].astype(np.int8)
 calendar['snap_WI'] = calendar['snap_WI'].astype(np.int8)
 calendar['day'] = calendar['day'].astype(np.int8 )<merge>
def decode_image(filename, label=None, image_size=(512, 512)) :
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.random_contrast(image,lower=0,upper=0.1,seed=seed)
 if label is None:
return image
else:
return image, label
sales_data = sales_data.merge(calendar, on='d', how='left' )<data_type_conversions>
def get_training_dataset(train_paths,train_labels):
dataset = tf.data.Dataset.from_tensor_slices(( train_paths, train_labels))
dataset = dataset.map(decode_image, num_parallel_calls=AUTO)
 dataset = dataset.cache()
dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(512)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset(valid_paths,valid_labels):
dataset = tf.data.Dataset.from_tensor_slices(( valid_paths, valid_labels))
dataset = dataset.map(decode_image, num_parallel_calls=AUTO)
 dataset = dataset.cache()
dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(test_paths):
dataset = tf.data.Dataset.from_tensor_slices(test_paths)
 dataset = dataset.map(decode_image, num_parallel_calls=AUTO)
 dataset = dataset.batch(BATCH_SIZE)
 return dataset
sell_prices['sell_price'] = sell_prices['sell_price'].astype(np.float32 )<merge>
LR_START = 0.00001
LR_MAX = 0.0001 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 10
LR_SUSTAIN_EPOCHS = 5
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]))
sales_data = sales_data.merge(sell_prices, how='left', on=['store_id', 'item_id', 'wm_yr_wk'] )<correct_missing_values>
def get_model() :
with strategy.scope() :
model = tf.keras.Sequential([
efn.EfficientNetB5(
 input_shape=(image_size, image_size, 3),
weights='imagenet',
include_top=False 
),
L.GlobalAveragePooling2D() ,
L.Dense(train_labels.shape[1], activation='softmax')
 ])
 model.compile(
 optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics=['categorical_accuracy'] 
)
 return model
sales_data.dropna(inplace=True )<data_type_conversions>
STEPS_PER_EPOCH = train_labels.shape[0] // BATCH_SIZE
histories = []
probs =[]
auc_score=[]
kfold = KFold(FOLDS , shuffle = True, random_state = SEED)
 i=0
for trn_ind, val_ind in kfold.split(train_paths,train_labels):
print() ; print('
print('
print('
trn_paths = train_paths[trn_ind]
val_paths=train_paths[val_ind]
trn_labels = train_labels[trn_ind]
val_labels=train_labels[val_ind]
model = get_model()
history = model.fit(
 get_training_dataset(trn_paths,trn_labels),
steps_per_epoch = STEPS_PER_EPOCH,
epochs = EPOCHS,
callbacks = [lr_callback],
validation_data = get_validation_dataset(val_paths,val_labels),
verbose=2 
)
 i+=1
prob = model.predict(get_test_dataset(test_paths), verbose=1)
 probs.append(prob)
 histories.append(history )
<prepare_output><EOS> 
prob_sum = 0
for prob in probs:
prob_sum += prob
prob_avg = prob_sum/FOLDS
sub.loc[:, 'healthy':] = prob_avg
sub.to_csv('submission.csv', index=False)
 sub.head()