Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<data_type_conversions>
!pip install -q '/kaggle/input/birdcall-identification-submission-custom/Keras_Applications-1.0.8-py3-none-any.whl'
!pip install -q '/kaggle/input/birdcall-identification-submission-custom/efficientnet-1.1.0-py3-none-any.whl'
test_df["Date"] = test_df["Date"].apply(lambda x: datetime.strptime(x,'%Y-%m-%d'))
test_df["Date"] = test_df["Date"].apply(lambda x: x.timestamp())
 test_df["Date"] = test_df["Date"].astype(int)
 test_df = test_df.drop(['Province/State'],axis=1)
 test_df = test_df.dropna()
test_df.head()<prepare_x_and_y>
import numpy as np
import pandas as pd
import tensorflow as tf
import efficientnet.tfkeras as efn
import matplotlib.pyplot as plt
from tqdm.notebook import tqdm
X = train_df[['Lat', 'Long', 'Date']]
Y1 = train_df[['ConfirmedCases']]
X_test = test_df[['Lat', 'Long', 'Date']]
Y2 = train_df[['Fatalities']]<prepare_output>
IMG_HEIGHT = 600
IMG_WIDTH = 800
IMG_SIZE = 600
IMG_TARGET_SIZE = 512
N_CHANNELS = 3
N_LABELS = 5
N_FOLDS = 5
BATCH_SIZE = 16
AUTO = tf.data.experimental.AUTOTUNE
IMAGENET_MEAN = tf.constant([0.485, 0.456, 0.406], dtype=tf.float32)
 IMAGENET_STD = tf.constant([0.229, 0.224, 0.225], dtype=tf.float32 )
rf = RandomForestRegressor(n_estimators=100)
 rf.fit(X,Y1)
 pred1 = rf.predict(X_test)
 pred1 = pd.DataFrame(pred1)
 pred1.columns = ["ConfirmedCases_prediction"] 
<prepare_output>
def get_model(fold):
tf.keras.backend.clear_session()
net = efn.EfficientNetB4(
 include_top=False,
weights=None,
input_shape=(IMG_TARGET_SIZE, IMG_TARGET_SIZE, N_CHANNELS), 
)
 for layer in reversed(net.layers):
if isinstance(layer, tf.keras.layers.BatchNormalization):
layer.trainable = False
else:
layer.trainable = True
model = tf.keras.Sequential([
net,
tf.keras.layers.Dropout(0.45),
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dropout(0.45),
tf.keras.layers.Dense(N_LABELS, activation='softmax', dtype=tf.float32),
])
 model.load_weights(f'/kaggle/input/cassava-leaf-disease-prediction/model_fold_{fold}_weights.h5')
 return model
rf_fatalities_model = RandomForestRegressor(n_estimators=100)
 rf_fatalities_model.fit(X,Y2)
 pred2 = rf_fatalities_model.predict(X_test)
 pred2 = pd.DataFrame(pred2)
 pred2.columns = ["Death_prediction"]<load_from_csv>
@tf.function
def decode_tfrecord_test(file_path):
image = tf.io.read_file(file_path)
 image = tf.io.decode_jpeg(image)
 image = tf.reshape(image, [IMG_HEIGHT, IMG_WIDTH, N_CHANNELS])
 image = tf.cast(image, tf.float32)
 image_id = tf.strings.split(file_path, '/')[-1]
return image, image_id
submission = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/submission.csv")
 submission.columns
sub = submission[["ForecastId"]]<concatenate>
def get_test_dataset() :
ignore_order = tf.data.Options()
ignore_order.experimental_deterministic = False
test_dataset = tf.data.Dataset.list_files('/kaggle/input/cassava-leaf-disease-classification/test_images/*.jpg')
 test_dataset = test_dataset.with_options(ignore_order)
 test_dataset = test_dataset.map(decode_tfrecord_test, num_parallel_calls=AUTO)
 test_dataset = test_dataset.batch(BATCH_SIZE)
 test_dataset = test_dataset.prefetch(AUTO)
 return test_dataset
combined_preds = pd.concat([pred1,pred2,sub],axis=1)
 combined_preds.head()
combined_preds.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
combined_preds = combined_preds[['ForecastId','ConfirmedCases', 'Fatalities']]<data_type_conversions>
def show_first_test_batch() :
imgs, imgs_ids = next(iter(get_test_dataset()))
img = imgs[0].numpy().astype(np.float32)
 print(f'imgs.shape: {imgs.shape}, imgs.dtype: {imgs.dtype}, imgs_ids.shape: {imgs_ids.shape}, imgs_ids.dtype: {imgs_ids.dtype}')
 print('img mean: {:.3f}, img std {:.3f}, img min: {:.3f}, img max: {:.3f}'.format(img.mean() , img.std() , img.min() , img.max()))
print(f'imgs_id: {imgs_ids[0]}')
 img += abs(img.min())
 img /= img.max()
plt.imshow(img)
 plt.show()
show_first_test_batch()
<save_to_csv><EOS> 
submission = pd.DataFrame(columns=['image_id', 'label'])
 preds_dict = dict()
for fold in range(N_FOLDS):
model = get_model(fold)
 for idx,(imgs, image_ids)in tqdm(enumerate(get_test_dataset())) :
for img, image_id in zip(imgs, image_ids.numpy().astype(str)) :
pred = predict_tta(model, img)
 if image_id in preds_dict:
preds_dict[image_id] += pred
else:
preds_dict[image_id] = pred
for idx,(image_id, preds)in enumerate(preds_dict.items()):
if idx is 0:
print(f'image {image_id} predictions:{preds}')
 label = np.argmax(preds)
 submission = submission.append({ 'image_id': image_id, 'label': label }, ignore_index=True)
 submission.to_csv('./submission.csv', index=False )