Kaggle competition: Plant Pathology 2020 - FGVC7
train.isnull().sum()<filter>
!pip install tensor-dash
train[train['winPlacePerc'].isnull() ]<correct_missing_values>
user_secrets = UserSecretsClient()
secret_value_0 = user_secrets.get_secret("email")
 secret_value_1 = user_secrets.get_secret("pin" )
train = train.dropna()<categorify>
dataset = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/train.csv' )
matchType_categories = pd.get_dummies(train[['matchType']])
 matchType_categories.head()
test_matchType_categories = pd.get_dummies(test[['matchType']])
 test_Id = test['Id']
train_drop = train.drop(['Id','groupId','matchId','matchType'], axis=1)
 train_drop = pd.merge(train_drop, matchType_categories, left_index=True, right_index=True)
 test_drop = test.drop(['Id','groupId','matchId','matchType'], axis=1)
 test_drop = pd.merge(test_drop, test_matchType_categories, left_index=True, right_index=True )<prepare_x_and_y>
dataset.isnull().any()
y_train = train_drop['winPlacePerc']
X_train = train_drop.drop(['winPlacePerc'], axis=1)
<split>
dataset['image_id'] = dataset['image_id']+'.jpg'
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, train_size=0.25, random_state=1 )<choose_model_class>
datagen = keras.preprocessing.image.ImageDataGenerator(
 rescale=1./255,
zca_whitening=False,
rotation_range=180,
zoom_range = 0.15,
width_shift_range=0.15,
height_shift_range=0.15,
horizontal_flip=True,
vertical_flip=True )
<choose_model_class>
X_train, X_valid = train_test_split(dataset, test_size=0.05, shuffle=False )
exported_pipeline = make_pipeline(
 make_union(
 FunctionTransformer(copy),
StackingEstimator(estimator=LassoLarsCV(normalize=False)) 
),
GradientBoostingRegressor(alpha=0.9, learning_rate=0.1, loss="huber", max_depth=8, max_features=0.55, min_samples_leaf=16, min_samples_split=12, n_estimators=100, subsample=1.0)
)
<predict_on_test>
BATCH_SIZE = 8
train_generator = datagen.flow_from_dataframe(dataset,
directory='/kaggle/input/plant-pathology-2020-fgvc7/images/',
x_col='image_id',
y_col=['healthy', 'multiple_diseases', 'rust', 'scab'] ,
target_size=(512, 512),
class_mode='raw',
batch_size=BATCH_SIZE, shuffle=False)
 valid_generator = datagen.flow_from_dataframe(X_valid,
directory='/kaggle/input/plant-pathology-2020-fgvc7/images/',
x_col='image_id',
y_col=['healthy', 'multiple_diseases', 'rust', 'scab'] ,
target_size=(512, 512),
class_mode='raw',
batch_size=BATCH_SIZE, shuffle=False )
exported_pipeline.fit(X_train,y_train)
 test_predict = exported_pipeline.predict(test_drop )<save_to_csv>
xception_model = tf.keras.models.Sequential([
tf.keras.applications.xception.Xception(include_top=False, weights='imagenet', input_shape=(512, 512, 3)) ,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(4,activation='softmax')
 ])
 xception_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 xception_model.summary()
submission = pd.DataFrame({
"Id": test_Id,
"winPlacePerc": test_predict
})
 submission.to_csv('submission2.csv', index=False )<set_options>
densenet_model = tf.keras.models.Sequential([
tf.keras.applications.densenet.DenseNet121(include_top=False, weights='imagenet',input_shape=(512, 512, 3)) ,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(4,activation='softmax')
 ])
 densenet_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 densenet_model.summary()
gc.enable()<load_from_csv>
LR_START = 0.00001
LR_MAX = 0.0001
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 15
LR_SUSTAIN_EPOCHS = 3
LR_EXP_DECAY =.8
EPOCHS = 100
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]))
def preprocessing(is_train=True):
if is_train:
print("Preprocessing train_V2.csv")
 print("Reading the data...",end='')
 df = pd.read_csv('.. /input/train_V2.csv')
 print("Done!")
 print("Kicking out the loner...",end='')
 df = df[df['maxPlace'] > 1]
print("Done!")
 else:
print("Preprocessing test_V2.csv",)
 print("Reading the data...",end='')
 df = pd.read_csv('.. /input/test_V2.csv')
 print('Done!')
 if is_train:
print("Getting the target...",end='')
 target = df['winPlacePerc']
print("Done!")
 print('Remove target from dataframe...',end='')
 df = df.drop(['winPlacePerc'],axis=1)
 print("Done!")
 print("Dataframe Exported.")
 if is_train:
return df,target
return df 
<drop_column>
model_checkpoint = tf.keras.callbacks.ModelCheckpoint('model.h5', monitor='val_accuracy', verbose=2, save_best_only=True)
 histories = Tensordash(
 email = secret_value_0,
password = secret_value_1,
ModelName = "Plant Disease Model" )
def feature_managing(df,is_train=True,lowfeature=True,valuematchtype=True):
err = 0
print("Feature Management Initiated")
 if is_train:
print("Dropping Player's Id")
 try:
df = df.drop(['Id'],axis=1)
 except:
print("Warning: 'Id' not found.")
 err += 1
else:
print('Retrive Player's Id...',end='')
 test_id = df['Id']
df = df.drop(['Id'],axis=1)
 print('Done!')
 if lowfeature:
print("Dropping MatchId")
 try:
df = df.drop(['matchId'],axis=1)
 print('Dropping GroupId')
 df = df.drop(['groupId'],axis=1)
 except:
print('Warning: MatchId and GroupId not found')
 err += 1
if valuematchtype:
mtype = ['solo','solo-fpp','duo','duo-fpp','squad','squad-fpp']
print('Determine Value for MatchType',end='')
 df.loc[df.matchType == 'solo','matchType'] = 1
print('.',end='')
 df.loc[df.matchType == 'normal-solo','matchType'] = 1
print('.',end='')
 df.loc[df.matchType == 'solo-fpp','matchType'] = 0.5
print('.',end='')
 df.loc[df.matchType == 'normal-solo-fpp','matchType'] = 0.5
print('.',end='')
 df.loc[df.matchType == 'duo','matchType'] = 2
print('.',end='')
 df.loc[df.matchType == 'normal-duo','matchType'] = 2
print('.',end='')
 df.loc[df.matchType == 'duo-fpp','matchType'] = 2.5
print('.',end='')
 df.loc[df.matchType == 'normal-duo-fpp','matchType'] = 2.5
print('.',end='')
 df.loc[df.matchType == 'squad','matchType'] = 4
print('.',end='')
 df.loc[df.matchType == 'normal-squad','matchType'] = 4
print('.',end='')
 df.loc[df.matchType == 'squad-fpp','matchType'] = 3.5
print('.',end='')
 df.loc[df.matchType == 'normal-squad-fpp','matchType'] = 4
print('.',end='')
 df.loc[df.matchType == 'crashfpp','matchType'] = 5
print('.',end='')
 df.loc[df.matchType == 'crashtpp','matchType'] = 5.5
print('.',end='')
 df.loc[df.matchType == 'flarefpp','matchType'] = 6
print('.',end='')
 df.loc[df.matchType == 'flaretpp','matchType'] = 6.5
print('Done!')
 else:
df = df.drop['matchType']
print('Generating Feature')
 print('Generate KillKnockRatio')
 df['KillKnockRatio'] = df['kills']/df['DBNOs']
print('Generate HealthItemsUsed')
 df['HealthItemsUsed'] = df['heals']+df['boosts']
print('Generate HeadshotRatio')
 df['HeadshotRatio'] = df['headshotKills']/df['kills']
print('Generate AverageDamagePerkill')
 df['AverageDamagePerKill'] = df['damageDealt']/df['kills']
print('Generate KillstreakRate')
 df['KillstreakRate'] = df['killStreaks']/df['kills']
print('Generate TotalDistance')
 df['TotalDistance'] = df['rideDistance'] + df['swimDistance'] + df['walkDistance']
print('Generate WeaponRetriveOverDistance')
 df['WROD'] = df['TotalDistance']/df['weaponsAcquired']
print('Generate WalkingHeal')
 df['walkHeal'] = df['walkDistance']/df['heals']
print('Generate WalkingKills')
 df['walkkills'] = df['walkDistance']/df['kills']
print('Generate KillPerMeters')
 df['KPM'] = df['kills']/df['walkDistance']
print('Purging NaN from Dataframe...',end='')
 try:
df[df == np.Inf] = np.NaN
df[df == np.NINF] =np.NaN
df.fillna(0,inplace=True)
 print('Done!')
 except:
print('Nothing to Purge')
 err += 1
if is_train:
print('End of Operation')
 return df
else:
print('Retrive maxPlace...',end='')
 test_edge = df['maxPlace']
test_edge = pd.DataFrame(test_edge)
 print('Done!')
 print('End of Operation')
 return df,test_id,test_edge<prepare_x_and_y>
model_history = model.fit_generator(train_generator, epochs=EPOCHS, validation_data=valid_generator, callbacks=[model_checkpoint,lr_callback, histories] )
df_train,target = preprocessing(is_train=True )<create_dataframe>
pd.DataFrame(model_history.history ).to_csv('ModelHistory.csv' )
df_train = feature_managing(df_train,is_train=True,lowfeature=True,valuematchtype=True )<feature_engineering>
test_dataset = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/test.csv')
 submission = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/sample_submission.csv')
 test_dataset
df_train = reduce_mem_usage(df_train )<train_model>
test_dataset['image_id'] = test_dataset['image_id']+'.jpg'
print('scaling the dataset')
 scaler = StandardScaler()
scaler.fit(df_train)
 scaled_train = scaler.transform(df_train)
 print('scaler ready!')
 mlpreg = MLPRegressor(hidden_layer_sizes=(35,20,15),max_iter=500)
 print("begin fitting...",end='')
 mlpreg.fit(scaled_train,target)
 print("Done!" )<save_to_csv>
test_gen = datagen.flow_from_dataframe(test_dataset,
directory='/kaggle/input/plant-pathology-2020-fgvc7/images/',
x_col='image_id',
target_size=(512, 512),
class_mode=None,
shuffle=False,
batch_size=8 )
res,submis = prediction(reducemem=True,returnall=True,smoothedge=True,csvwrite=True )<create_dataframe>
predictions = model.predict_generator(test_gen )
targetframe = pd.DataFrame(target )<create_dataframe>
submission['healthy'] = predictions[:, 0]
submission['multiple_diseases'] = predictions[:, 1]
submission['rust'] = predictions[:, 2]
submission['scab'] = predictions[:, 3]
resframe = pd.DataFrame(res)
 resframe.plot()<load_from_csv>
submission.to_csv('submission.csv', index=False )
<correct_missing_values><EOS> 
submission.to_csv('submission.csv', index=False )