Kaggle competition: Tabular Playground Series - Jan 2021
EPOCHS = 50
STEP_SIZE_TRAIN = train_set.n // train_set.batch_size
STEP_SIZE_TEST = test_set.n // test_set.batch_size<train_model>
import plotly.express as px
import plotly.graph_objects as go
import catboost as cgb
from catboost import CatBoostRegressor,Pool
import shap
from bayes_opt import BayesianOptimization
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
def model_fit() :
leaf_model = create_model()
loss = tf.keras.losses.CategoricalCrossentropy(
 from_logits = False,
label_smoothing=0.0001,
name='categorical_crossentropy' 
)
 leaf_model.compile(
 optimizer = Adam(learning_rate = 1e-3),
loss = loss,
metrics = ['categorical_accuracy'] 
)
 es = EarlyStopping(
 monitor='val_loss',
mode='min',
patience=5,
restore_best_weights=True,
verbose=1, 
)
 checkpoint_cb = ModelCheckpoint(
 "Cassava_best_model_b5.h5",
save_best_only=True,
monitor='val_loss',
mode='min', 
)
 reduce_lr = ReduceLROnPlateau(
 monitor='val_loss',
factor=0.2,
patience=2,
min_lr=1e-6,
mode='min',
verbose=1, 
)
 history = leaf_model.fit(
 train_set,
validation_data=test_set,
epochs=EPOCHS,
batch_size=BATCH_SIZE,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_steps=STEP_SIZE_TEST,
callbacks=[es, checkpoint_cb, reduce_lr], 
)
 leaf_model.save('Cassava_best_model'+'.h5')
 return history<set_options>
X=pd.read_csv('.. /input/tabular-playground-series-jan-2021/train.csv')
 X.head()
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))
K.set_session(sess )<train_model>
X_test=pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv')
 X_test.head()
try:
final_model = keras.models.load_model('Cassava_best_model.h5')
 except Exception as e:
with tf.device('/GPU:0'):
results = model_fit()
print('Train Categorical Accuracy: ', max(results.history['categorical_accuracy']))
print('Test Categorical Accuracy: ', max(results.history['val_categorical_accuracy']))<define_variables>
corr=X.corr()
corr_t=corr['target'].sort_values(ascending=False)
 corr_t
debug = True
MODEL_DIR = '.. /input/20t-efficientnet-b3-cutmix-tta'<define_variables>
X=X.drop('id',axis=1 )
OUTPUT_DIR = './'
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
 TRAIN_PATH = '.. /input/cassava-leaf-disease-classification/train_images'
TEST_PATH = '.. /input/cassava-leaf-disease-classification/test_images'
assert len(glob.glob(f'{MODEL_DIR}/*.yml')) ==1
config_path = glob.glob(f'{MODEL_DIR}/*.yml')[0]<init_hyperparams>
y=X.target
X=X.drop('target',axis=1)
 X_train,X_val,y_train,y_val=train_test_split(X,y,test_size=0.2,random_state=1 )
with open(config_path)as f:
config = yaml.load(f)
 INFO = config['info']
TAG = config['tag']
CFG = config['cfg']
CFG['train'] = False
CFG['inference'] = True
inference_batch_size = 8 
<compute_train_metric>
def objective(max_depth,random_strength,learning_rate,iterations,bagging_temperature):
params={'eval_metric':'RMSE',
'verbose':False,
'loss_function':'RMSE',
'num_leaves':31,
'bootstrap_type':'Bayesian'
}
params['max_depth']=int(round(max_depth))
params['random_strength']=random_strength
params['learning_rate']=learning_rate
params['iterations']=int(round(iterations))
params['bagging_temperature']=bagging_temperature
cv_dataset=cgb.Pool(data=X,label=y)
 scores=cgb.cv(cv_dataset,params,fold_count=3,)
 return np.min(scores['test-RMSE-mean'])
 pds={
'max_depth':(3,10),
'random_strength':(1,10),
'learning_rate':(0.01,0.3),
'iterations':(100,1000),
'bagging_temperature':(3,10)
 }
optimizer=BayesianOptimization(objective,pds,random_state=2100)
 optimizer.maximize(init_points=3,n_iter=7 )
def get_result(result_df):
preds = result_df['preds'].values
labels = result_df['label'].values
score = get_score(labels, preds)
 LOGGER.info(f'Score: {score:<.5f}')
 return score
def get_aug_name(compose):
aug_list = []
for aug in compose:
aug_list.append(aug.__class__.__name__)
 return aug_list
def get_aug_score(aug_preds, labels, tta_list):
for i in range(aug_preds.shape[1]):
aug_pred = aug_preds[:,i,:]
aug_list = get_aug_name(tta_list[i])
 score = get_score(labels, aug_pred.argmax(1))
print(score, aug_list)
 LOGGER.info(f"========== aug: {aug_list} result ==========")
 LOGGER.info(f'Score: {score:<.5f}')
 def get_aug_csv(aug_preds, oof_df, tta_list):
for i in range(aug_preds.shape[1]):
base_df = oof_df.copy() [['image_id', 'label', 'fold']]
aug_pred = aug_preds[:,i,:]
base_df[[str(c)for c in range(5)]] = aug_pred
base_df['preds'] = aug_pred.argmax(1)
 aug_pred = aug_preds[:,i,:]
aug_list = get_aug_name(tta_list[i])
 csv_name = '-'.join(aug_list)
 base_df.to_csv(f'{OUTPUT_DIR}{csv_name}.csv', index=False )<load_from_csv>
ct=CatBoostRegressor(iterations=786
,learning_rate=0.08262,
max_depth=6,
num_leaves=31,
random_strength=1.427,
use_best_model=True,
eval_metric='RMSE',
verbose=False,
bagging_temperature=4.901)
 ct.fit(X_train,y_train,
verbose_eval=100,
early_stopping_rounds=30,
eval_set=(X_val,y_val),
plot=True);
test = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 test.head()<normalization>
pred=ct.predict(X_val)
 print(np.sqrt(mean_squared_error(y_val,pred)) )
<normalization><EOS> 
prediction_file=ct.predict(X_test.drop('id',axis=1))
submission=pd.DataFrame({
"id":X_test['id'],
"target":prediction_file
})
 submission.to_csv('my_submission.csv',index=False )