Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<load_from_csv>
print(tf.__version__)
 print(tf.keras.__version__ )
train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv',parse_dates = ['Date'])
 test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv',parse_dates = ['Date'])
 train['Country_State']=train.Country_Region+train.Province_State.fillna('')
 test['Country_State']=test.Country_Region+test.Province_State.fillna('')
 train.head()<drop_column>
!pip install efficientnet
test_df = test[['ForecastId','Country_State']]<count_unique_values>
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()
countriesorstates = train_df["Country_State"].unique()
print(len(countriesorstates))<create_dataframe>
img = plt.imread('.. /input/plant-pathology-2020-fgvc7/images/Train_0.jpg')
 print(img.shape)
 plt.imshow(img )
sub = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 strt = '2020-04-02'
end = '2020-05-14'
for value in countriesorstates:
random.seed(0)
 train_model_cc = Prophet(seasonality_mode = 'multiplicative',changepoint_prior_scale = 30, interval_width=0.95)
 train_model_ft = Prophet(seasonality_mode = 'multiplicative',changepoint_prior_scale = 30, interval_width=0.95)
 train_temp = train_df.loc[train_df["Country_State"] == value]
test_temp = test_df.loc[test_df["Country_State"] == value]
train_temp_cc = train_temp[['Date','ConfirmedCases']]
train_temp_cc = train_temp_cc.sort_values(by = ['Date'])
 train_temp_cc = train_temp_cc.rename(columns = {'Date':'ds','ConfirmedCases':'y'})
 train_temp_ft = train_temp[['Date','Fatalities']]
train_temp_ft = train_temp_ft.sort_values(by = ['Date'])
 train_temp_ft = train_temp_ft.rename(columns = {'Date':'ds','Fatalities':'y'})
 train_model_cc.fit(train_temp_cc)
 train_forecast_cc = train_model_cc.make_future_dataframe(periods=43, freq='D',include_history = True)
 train_forecast_cc = train_model_cc.predict(train_forecast_cc)
 reg_cc = train_forecast_cc[['ds','yhat']]
test_cc = reg_cc[(reg_cc['ds']>=strt)&(reg_cc['ds']<=end)]
test_cc = test_cc.yhat.values
train_model_ft.fit(train_temp_ft)
 train_forecast_ft = train_model_ft.make_future_dataframe(periods=43, freq='D',include_history = True)
 train_forecast_ft = train_model_ft.predict(train_forecast_ft)
 reg_ft = train_forecast_ft[['ds','yhat']]
test_ft = reg_ft[(reg_ft['ds']>=strt)&(reg_ft['ds']<=end)]
test_ft = test_ft.yhat.values
test_cc = test_cc.flatten()
test_ft = test_ft.flatten()
sub_temp = pd.DataFrame({'ForecastId': test_temp["ForecastId"].loc[test_temp["Country_State"] == value],
'ConfirmedCases': test_cc, 'Fatalities': test_ft})
 sub = pd.concat([sub, sub_temp], axis = 0 )<save_to_csv>
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
sub.ForecastId = sub.ForecastId.astype('int')
 for i in range(len(sub)) :
sub["ConfirmedCases"][i] = int(round(sub["ConfirmedCases"][i]))
sub["Fatalities"][i] = int(round(sub["Fatalities"][i]))
sub.to_csv("submission.csv", index = False )<import_modules>
nb_classes = 4
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
img_size = 768
EPOCHS = 40
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import nltk
from sklearn.preprocessing import LabelBinarizer,LabelEncoder,StandardScaler,MinMaxScaler
from sklearn.linear_model import LogisticRegression,SGDClassifier,LinearRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
from sklearn.model_selection import train_test_split
import keras
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from keras.models import Sequential
from keras.layers import Dense,LSTM
import tensorflow as tf<load_from_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
train = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )<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)
 )
train['ConfirmedCases'] = train['ConfirmedCases'].apply(int)
 train['Fatalities'] = train['Fatalities'].apply(int)
 cases = train.ConfirmedCases
fatalities = train.Fatalities
del train['ConfirmedCases']
del train['Fatalities']<categorify>
test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )
train['Province_State'] = train['Province_State'].fillna("")
 test['Province_State'] = test['Province_State'].fillna("")
 train['geography'] = train['Country_Region']+train['Province_State']
test['geography'] = test['Country_Region']+test['Province_State']
le=LabelEncoder()
train['geography'] = le.fit_transform(train['geography'])
 test['geography'] = le.fit_transform(test['geography'] )<drop_column>
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 = train.drop(['Province_State','Country_Region'],axis=1)
 test = test.drop(['Province_State','Country_Region'],axis=1 )<drop_column>
def get_model() :
base_model = efn.EfficientNetB7(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 )
del train['Id']
del test['ForecastId']<feature_engineering>
with strategy.scope() :
model = get_model()
model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'] )
def create_features(df):
df['day'] = df['Date'].dt.day
df['month'] = df['Date'].dt.month
return df<data_type_conversions>
%%time
model.fit(
 train_dataset,
steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
callbacks=[lr_callback],
epochs=EPOCHS
)
train['Date']=pd.to_datetime(train['Date'])
 test['Date']=pd.to_datetime(test['Date'])
 train = create_features(train)
 test = create_features(test)
 train = train.drop("Date",axis =1)
 test = test.drop("Date",axis =1)
<train_model>
%%time
probs = model.predict(test_dataset )
<import_modules><EOS> 
sub.loc[:, 'healthy':] = probs
sub.to_csv('submission.csv', index=False)
 sub.head()