Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<train_model>
!pip install fastprogress==0.2.3
!pip install fastai==1.0.61
ts = time.time()
model = XGBRegressor(
 max_depth=6,
n_estimators=800,
min_child_weight=100,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=42)
 model.fit(
 X_train,
Y_train,
eval_metric="mae",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 10)
 time.time() - ts 
<predict_on_test>
device = 'cuda' if torch.cuda.is_available() else 'cpu'
Y_pred = model.predict(test ).clip(0, 1 )<save_to_csv>
path = Path('/kaggle/input/plant-pathology-2020-fgvc7/' )
submission = pd.DataFrame({
"ID": test.Id,
"winPlacePerc": Y_pred
})
 submission.to_csv('xgb_submission.csv', index=False)
 pickle.dump(Y_pred, open('xgb_train.pickle', 'wb'))<import_modules>
train_df = pd.read_csv(path/'train.csv')
 test_df = pd.read_csv(path/'test.csv')
 sample_df = pd.read_csv(path/'sample_submission.csv' )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from math import sqrt 
<load_from_csv>
test_df['image_id'] = test_df['image_id'] + '.jpg'
train_df['image_id'] = train_df['image_id'] + '.jpg'
pubg_train = pd.read_csv('.. /input/train_V2.csv')
 pubg_test = pd.read_csv('.. /input/test_V2.csv')
<count_values>
def get_label(row):
if row.healthy == 1:
return 'healthy'
elif row.rust == 1:
return 'rust'
elif row.scab == 1:
return 'scab'
else:
return 'multiple_diseases'
pubg_train['matchType'].value_counts()<categorify>
train_df['label'] = train_df.apply(get_label, axis=1 )
le = LabelEncoder()
pubg_train['matchType'] = le.fit_transform(pubg_train['matchType'])
 pubg_test['matchType'] = le.fit_transform(pubg_test['matchType'] )<categorify>
train_df = train_df[['image_id', 'label']]
pubg_train['Id']= pd.factorize(pubg_train.Id)[0]
pubg_train['groupId']= pd.factorize(pubg_train.groupId)[0]
pubg_train['matchId']= pd.factorize(pubg_train.matchId)[0] 
<filter>
c = Counter(train_df.label), len(train_df)
 c
submission = pubg_test.loc[:,['Id']]<categorify>
psuedo_label_submission = pd.read_csv('/kaggle/input/plant-pathology-fgvc7-2020-submissions/971968_966_964.csv' )
pubg_test['Id']= pd.factorize(pubg_test.Id)[0]
pubg_test['groupId']= pd.factorize(pubg_test.groupId)[0]
pubg_test['matchId']= pd.factorize(pubg_test.matchId)[0] 
<define_variables>
def get_pseudo_label(row):
probs = [row.healthy, row.multiple_diseases, row.rust, row.scab]
label = ['healthy', 'multiple_diseases', 'rust', 'scab'][np.argmax(probs)]
return label
dtype_list=[]
for col in pubg_train.columns:
dtype_list.append(pubg_train[col].dtypes)
 dtype_list=list(set(dtype_list))
print("Total Datatypes present: %s "%dtype_list)
<data_type_conversions>
psuedo_label_submission['label'] = psuedo_label_submission.apply(get_pseudo_label, axis=1 )
for dtype in dtype_list:
if 'int' in str(dtype):
print("Analyse %s"%str(dtype))
df_int=pubg_train.select_dtypes(include=[str(dtype)])
 converted_int = df_int.apply(pd.to_numeric,downcast='unsigned')
 print(mem_usage(df_int))
print(mem_usage(converted_int))
elif 'float' in str(dtype):
print("Analyse %s"%str(dtype))
df_float = pubg_train.select_dtypes(include=[str(dtype)])
 converted_float = df_float.apply(pd.to_numeric,downcast='float')
 print(mem_usage(df_float))
print(mem_usage(converted_float)) 
<train_model>
psuedo_label_submission['image_id'] = psuedo_label_submission['image_id'] + '.jpg'
print("Memory Usage of Original dataset: %s"%mem_usage(pubg_train))
pubg_train[converted_int.columns] = converted_int
pubg_train[converted_float.columns] = converted_float
print("Memory Usage of Optimized dataset: %s"%mem_usage(pubg_train))<data_type_conversions>
psuedo_label_submission = psuedo_label_submission[['image_id', 'label']]
for dtype in dtype_list:
if 'int' in str(dtype):
print("Analyse %s"%str(dtype))
df_int=pubg_test.select_dtypes(include=[str(dtype)])
 converted_int = df_int.apply(pd.to_numeric,downcast='unsigned')
 print(mem_usage(df_int))
print(mem_usage(converted_int))
elif 'float' in str(dtype):
print("Analyse %s"%str(dtype))
df_float = pubg_test.select_dtypes(include=[str(dtype)])
 converted_float = df_float.apply(pd.to_numeric,downcast='float')
 print(mem_usage(df_float))
print(mem_usage(converted_float))<train_model>
train_df_with_pl = pd.concat([train_df, psuedo_label_submission] )
print("Memory Usage of Original dataset: %s"%mem_usage(pubg_test))
pubg_test[converted_int.columns] = converted_int
pubg_test[converted_float.columns] = converted_float
print("Memory Usage of Optimized dataset: %s"%mem_usage(pubg_test))<feature_engineering>
train_idx_with_pl = train_idx + list(range(len(train_df), len(train_df)+ len(psuedo_label_submission)) )
pubg_train['headShotsPerKill'] = pubg_train['headshotKills']/pubg_train['kills']
pubg_test['headShotsPerKill'] = pubg_test['headshotKills']/pubg_test['kills']
pubg_train['headShotsPerKill'].fillna(0,inplace=True)
 pubg_test['headShotsPerKill'].fillna(0,inplace=True)
 pubg_train['headShotsPerKill'].replace(np.inf,0,inplace=True)
 pubg_test['headShotsPerKill'].replace(np.inf,0,inplace=True )<feature_engineering>
def get_img_id_from_idx(idx):
return train_df.iloc[idx].image_id
pubg_train['distTravelled'] = pubg_train['rideDistance']+pubg_train['swimDistance']+pubg_train['walkDistance']
pubg_test['distTravelled'] = pubg_test['rideDistance']+pubg_test['swimDistance']+pubg_test['walkDistance'] 
<feature_engineering>
img_size = 1024
train_data = create_databunch_from_img_size(img_size, 32 )
pubg_train['itemsUsed'] = pubg_train['heals']+pubg_train['boosts']
pubg_test['itemsUsed'] = pubg_test['heals']+pubg_test['boosts']<feature_engineering>
train_data.show_batch()
<feature_engineering>
class ROCAUCScore(Callback):
def on_epoch_begin(self, **kwargs):
self.targets, self.preds = [], []
def on_batch_end(self, last_output, last_target, **kwargs):
self.targets.extend(last_target.tolist())
 self.preds.extend(list(map(scipy.special.softmax, last_output.tolist())))
 def on_epoch_end(self, last_metrics, **kwargs):
sc = roc_auc_score(self.targets, self.preds, multi_class='ovr')
 return add_metrics(last_metrics, sc )
<feature_engineering>
class StopTrainingAtEpoch(Callback):
def __init__(self, learn, n):
self.learn = learn
self.n = n
def on_epoch_end(self, epoch, **kwargs):
if epoch == self.n:
self.learn.save('model-' + str(self.n))
return {'stop_training': True}
pubg_train['teamwork']=pubg_train['revives']+pubg_train['assists']
pubg_test['teamwork']=pubg_test['revives']+pubg_test['assists'] 
<feature_engineering>
ce_weight = torch.tensor([1.0, 4.0, 1.0, 1.0] ).to(device)
 ce_weight=None
print(ce_weight)
 learn = cnn_learner(train_data, models.resnet18, metrics=[ROCAUCScore() , accuracy, error_rate, Precision(average='macro'), Recall(average='macro'), FBeta(beta=1.0, average='macro')],
loss_func=CrossEntropyFlat(reduction='mean', weight=ce_weight), ps=[0.5, 0.5, 0.5], wd=0.01,
path='/kaggle/working', callback_fns=[ShowGraph])
pubg_train['killpoints_norm'] =(pubg_train['killPoints']*pubg_train['playerJoined'])/100
pubg_train['damageDealt_norm']=(pubg_train['damageDealt']*pubg_train['playerJoined'])/100
pubg_train['kills_norm'] =(pubg_train['kills']*pubg_train['playerJoined'])/100
pubg_train['rankPoints_norm']=(pubg_train['rankPoints']*pubg_train['playerJoined'])/100
pubg_train['roadKills_norm']=(pubg_train['roadKills']*pubg_train['playerJoined'])/100
pubg_train['teamKills_norm']=(pubg_train['teamKills']*pubg_train['playerJoined'])/100
pubg_train['winPoints_norm']=(pubg_train['winPoints']*pubg_train['playerJoined'])/100
pubg_train['headShotsPerKill_norm']=(pubg_train['headShotsPerKill']*pubg_train['playerJoined'])/100
pubg_train['teamwork_norm']=(pubg_train['teamwork']*pubg_train['playerJoined'])/100<feature_engineering>
learn.load('/kaggle/input/plant-pathology-2020-fgvc7-best-single-model/stage-05');
learn.unfreeze()
pubg_train['roadKills_log'] = np.log(1+pubg_train.roadKills)
 pubg_test['roadKills_log'] = np.log(1+pubg_test.roadKills )<feature_engineering>
img_size = 1024
train_data = create_databunch_from_img_size(img_size, 16)
 learn.data = train_data
pubg_test['killpoints_norm'] =(pubg_test['killPoints']*pubg_test['playerJoined'])/100
pubg_test['damageDealt_norm']=(pubg_test['damageDealt']*pubg_test['playerJoined'])/100
pubg_test['kills_norm'] =(pubg_test['kills']*pubg_test['playerJoined'])/100
pubg_test['rankPoints_norm']=(pubg_test['rankPoints']*pubg_test['playerJoined'])/100
pubg_test['roadKills_norm']=(pubg_test['roadKills']*pubg_test['playerJoined'])/100
pubg_test['teamKills_norm']=(pubg_test['teamKills']*pubg_test['playerJoined'])/100
pubg_test['winPoints_norm']=(pubg_test['winPoints']*pubg_test['playerJoined'])/100
pubg_test['headShotsPerKill_norm']=(pubg_test['headShotsPerKill']*pubg_test['playerJoined'])/100
pubg_test['teamwork_norm']=(pubg_test['teamwork']*pubg_test['playerJoined'])/100<feature_engineering>
learn.save('stage-05' )
pubg_train['vehicleDestroys_log'] = np.log(1+pubg_train.vehicleDestroys)
 pubg_test['vehicleDestroys_log'] = np.log(1+pubg_test.vehicleDestroys)
 pubg_train['swimDistance_log']=np.log(1+pubg_train.swimDistance)
 pubg_test['swimDistance_log']=np.log(1+pubg_test.swimDistance)
 pubg_train['teamKills_log']=np.log(1+pubg_train.teamKills)
 pubg_test['teamKills_log']=np.log(1+pubg_test.teamKills)
<drop_column>
learn.fit_one_cycle(20, slice(1e-7, 1e-2 / 10), start_epoch=0, callbacks=[StopTrainingAtEpoch(learn, 19)] )
pubg_train.drop(columns=['Id','groupId','matchId',],inplace=True)
 pubg_test.drop(columns=['Id','groupId','matchId',],inplace=True )<prepare_x_and_y>
learn.save('stage-06' )
X=pubg_train.drop(['winPlacePerc'],axis=1)
 y=pubg_train['winPlacePerc'] 
<split>
learn.load('stage-06');
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=22,shuffle=True )<import_modules>
learn.validate(learn.data.train_dl )
from sklearn.preprocessing import StandardScaler<normalization>
learn.validate(learn.data.valid_dl )
scaler = StandardScaler(copy=False)
 scaler.fit(X_train)
 Scaled_X_train = scaler.transform(X_train)
 Scaled_X_test = scaler.transform(X_test )<import_modules>
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
from sklearn import model_selection
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.pipeline import Pipeline<split>
valid_tta_preds, y, losses = learn.TTA(scale=1.10, with_loss=True )
sample_X_train = Scaled_X_train[:100000,]
sample_y_train = y_train[:100000] 
<choose_model_class>
accuracy(valid_tta_preds, y )
def randomized_search() :
pipeline1 = Pipeline(( 
('clf', RandomForestRegressor(max_depth=500,max_features=None)) , 
))
pipeline2 = Pipeline(( 
('clf', LogisticRegression(class_weight='balanced',penalty='l2',solver='saga',max_iter=100)) , 
))
pipeline3 = Pipeline(( 
('clf', MLPRegressor()), 
))
pipeline4 = Pipeline(( 
('clf', DecisionTreeRegressor(splitter='random')) , 
))
parameters1 = {
'clf__n_estimators': [ 100],
}
parameters2 = {
'clf__C':[0.1,0.001,1,10]
}
parameters3 = {
'clf__alpha':[0.001,0.00001,0.0001],
'clf__max_iter':[250,400,500],
'clf__batch_size':[100,500,1000]
}
parameters4 = {
'clf__max_depth': [10,100,500,1000],
'clf__max_features': ['auto', 'sqrt', 'log2', None],
'clf__min_weight_fraction_leaf':[0,0.25,0.5]
}
pars = [(parameters1,"RandomForestRegressor"),(parameters2,"LogisticRegression"),(parameters3,"MLPRegressor"),(parameters4,"DecisionTreeRegressor")]
pips = [pipeline1, pipeline2, pipeline3, pipeline4]

 model,name = pars[0]
print('-'*50)
 print("Starting Randomized Search for %s" %name)
 rs = RandomizedSearchCV(pips[0], model, verbose=5, refit=False, n_jobs=3,cv=5,random_state=22,n_iter=20)
 rs = rs.fit(sample_X_train, sample_y_train)
 print("Finished Randomized Search for %s"%name)
 print('Best Score %.5f'%rs.best_score_)
 print('Best Param %s'%rs.best_params_)
 print('-'*50)
<import_modules>
confusion_matrix(y.cpu().numpy() , valid_tta_preds.argmax(1 ).cpu().numpy() )
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score
from math import sqrt 
<compute_test_metric>
learn.export('model.pkl' )
<import_modules>
preds, y = learn.get_preds(DatasetType.Test )
<compute_test_metric><EOS> 
sample_df.iloc[:,1:] = preds.numpy()
sample_df.to_csv('submission.csv', index=False )