Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<train_model>
print(tf.__version__)
 print(tf.keras.__version__ )
model_rf = RandomForestRegressor(n_estimators=32, oob_score=False, random_state=0, n_jobs = -1, verbose = 2)
 model_rf.fit(pubg_x, pubg_y)
 predict_train_rf = model_rf.predict(pubg_x)
 print('Mean absolute error for the training set using random forest regressor model %.4f' %
mean_absolute_error(pubg_y, np.clip(predict_train_rf, 0, 1)) )<drop_column>
!pip install efficientnet
del(pubg_data)
 del(pubg_data_all)
 del(pubg_x)
 del(pubg_y)
 del(predict_train_rf )<load_from_csv>
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()
pubg_data_test = pd.read_csv('.. /input/test_V2.csv')
 pubg_data_test['winPlacePerc'] = 0<compute_test_metric>
img = plt.imread('.. /input/plant-pathology-2020-fgvc7/images/Train_2.jpg')
 print(img.shape)
 plt.imshow(img )
pubg_x, pubg_y = feature_engineering(pubg_data_test )<predict_on_test>
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
pubg_x = pd.DataFrame(scaler.transform(pubg_x), columns= labels)
 predict_test_rf = model_rf.predict(pubg_x )<define_variables>
nb_classes = 4
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
img_size = 768
EPOCHS = 31
predict_test_rf_clip = np.clip(predict_test_rf, 0, 1 )<save_to_csv>
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)
 if label is None:
return image
else:
return image, label
predict_test_rf_df = pd.DataFrame(data= predict_test_rf_clip, columns=['winPlacePerc'])
 output_df = pd.merge(pubg_data_test["Id"].to_frame() ,predict_test_rf_df['winPlacePerc'].to_frame() , left_index=True, right_index=True)
 output_df.head()
output_df.to_csv("submission2.csv", index = False, index_label=False )<load_from_csv>
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)
 )
train=pd.read_csv(".. /input/train_V2.csv")
 test=pd.read_csv(".. /input/test_V2.csv")
 sample_submission=pd.read_csv(".. /input/sample_submission_V2.csv" )<count_missing_values>
test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )
train.isnull().sum()<count_missing_values>
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]))
train.isnull().sum()<prepare_x_and_y>
def get_model() :
base_model = efn.EfficientNetB4(weights='imagenet', include_top=False, pooling='avg', input_shape=(img_size, img_size, 3))
x = base_model.output
predictions = Dense(nb_classes, activation="softmax" )(x)
 return Model(inputs=base_model.input, outputs=predictions )
y=train['winPlacePerc']
train=train.drop(columns=['winPlacePerc','Id','groupId','matchId'],axis=1)
 test=test.drop(columns=['Id','groupId','matchId'],axis=1 )<categorify>
with strategy.scope() :
model = get_model()
model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'] )
train=pd.get_dummies(train)
 test=pd.get_dummies(test)
 print('train shape:',train.shape,'test shape:',test.shape )<import_modules>
%%time
model.fit(
 train_dataset,
steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
callbacks=[lr_callback],
epochs=EPOCHS
)
from sklearn.model_selection import train_test_split 
<split>
%%time
probs = model.predict(test_dataset )
<import_modules><EOS> 
sub.loc[:, 'healthy':] = probs
sub.to_csv('submission.csv', index=False)
 sub.head()