Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<feature_engineering>
submission.loc[submission['id'].str.contains('CA_1'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('CA_2'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('CA_3'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('CA_4'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('TX_1'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('TX_2'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('TX_3'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('WI_1'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('WI_2'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('WI_3'), 'F11'] *= 1
submission['F11'].sum()<categorify>
!pip install -q efficientnet
for i in range(9,20):
if i!=11:
submission.loc[submission['id'].str.contains('CA_1'), 'F'+str(i)] *= 1
submission.loc[submission['id'].str.contains('CA_2'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('CA_3'), 'F'+str(i)] *= 1
submission.loc[submission['id'].str.contains('CA_4'), 'F'+str(i)] *= 1.013
submission.loc[submission['id'].str.contains('TX_1'), 'F'+str(i)] *= 1
submission.loc[submission['id'].str.contains('TX_2'), 'F'+str(i)] *= 1.013
submission.loc[submission['id'].str.contains('TX_3'), 'F'+str(i)] *= 1.013
submission.loc[submission['id'].str.contains('WI_1'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('WI_2'), 'F'+str(i)] *= 1.02
submission.loc[submission['id'].str.contains('WI_3'), 'F'+str(i)] *= 1.015
print(submission['F'+str(i)].sum() )<categorify>
import math, re, os
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from kaggle_datasets import KaggleDatasets
import tensorflow as tf
import tensorflow.keras.layers as L
from sklearn import metrics
from sklearn.model_selection import train_test_split
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2
from tensorflow.keras.applications.densenet import DenseNet121, DenseNet169, DenseNet201
from tensorflow.keras.applications.xception import Xception
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.applications.resnet_v2 import ResNet50V2, ResNet101V2, ResNet152V2
from tensorflow.keras.applications.nasnet import NASNetLarge
from efficientnet.tfkeras import EfficientNetB7, EfficientNetL2
from tensorflow.keras.layers import Flatten,Dense,Dropout,BatchNormalization
from tensorflow.keras.models import Model,Sequential
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from keras.optimizers import Adam,SGD,Adagrad,Adadelta,RMSprop
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
for i in range(20,29):
submission.loc[submission['id'].str.contains('CA_1'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('CA_2'), 'F'+str(i)] *= 1.03
submission.loc[submission['id'].str.contains('CA_3'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('CA_4'), 'F'+str(i)] *= 1.025
submission.loc[submission['id'].str.contains('TX_1'), 'F'+str(i)] *= 1.02
submission.loc[submission['id'].str.contains('TX_2'), 'F'+str(i)] *= 1.025
submission.loc[submission['id'].str.contains('TX_3'), 'F'+str(i)] *= 1.025
submission.loc[submission['id'].str.contains('WI_1'), 'F'+str(i)] *= 1.03
submission.loc[submission['id'].str.contains('WI_2'), 'F'+str(i)] *= 1.04
submission.loc[submission['id'].str.contains('WI_3'), 'F'+str(i)] *= 1.03
print(submission['F'+str(i)].sum() )<save_to_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()
EPOCHS = 40
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
submission.to_csv('submission.csv', index=False )<load_from_csv>
def format_path(st):
return GCS_DS_PATH + '/images/' + st + '.jpg'
sales_train_validation = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_validation.csv')
 print(sales_train_validation.shape)
 calendar = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/calendar.csv')
 print(calendar.shape)
 sell_prices = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sell_prices.csv')
 print(sell_prices.shape )<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
def convert_categorical_columns(df, columns):
for column in columns:
df[column] = df[column].astype('category')
 df[column] = df[column].cat.codes
df[column] = df[column].astype('category' )<define_variables>
img_size = 768
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
for i in range(1914, 1914+28):
sales_train_validation['d_'+str(i)] = 0<data_type_conversions>
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)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )
convert_categorical_columns(sales_train_validation, ['dept_id', 'cat_id', 'state_id'] )<categorify>
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]))
sales_data = sales_train_validation.melt(id_vars=['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'],
var_name='d',
value_name='sales' )<feature_engineering>
def get_model() :
base_model = InceptionResNetV2(weights='imagenet',
include_top=False, pooling='avg',
input_shape=(img_size, img_size, 3))
x = base_model.output
predictions = Dense(train_labels.shape[1], activation="softmax" )(x)
 return Model(inputs=base_model.input, outputs=predictions)
 with strategy.scope() :
model = get_model()
model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['categorical_accuracy'])
 model.summary()
df = sales_train_validation.loc[:, sales_train_validation.columns.str.startswith('d_')]
sales_data['lag_7'] = df.shift(7, axis=1 ).melt() ['value']
sales_data['lag_28'] = df.shift(28, axis=1 ).melt() ['value']
del df
gc.collect()<feature_engineering>
model.fit(
 train_dataset,
steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
callbacks=[lr_callback],
epochs=EPOCHS
)
df = sales_train_validation.loc[:, sales_train_validation.columns.str.startswith('d_')]
sales_data['lag_28_rmean_7'] = df.shift(28, axis=1 ).rolling(7, axis=1 ).mean().melt() ['value']
sales_data['lag_28_rmean_28'] = df.shift(28, axis=1 ).rolling(28, axis=1 ).mean().melt() ['value']
del df
gc.collect()<data_type_conversions>
name_model = 'InceptionResNetV2.h5'
model.save(name_model)
<correct_missing_values><EOS> 
probs = model.predict(test_dataset, verbose=1)
 sub.loc[:, 'healthy':] = probs
sub.to_csv('submission.csv', index=False)
 sub.head()