Kaggle competition: iMet Collection 2019 - FGVC6
<SOS> metric: MeanFScoreBeta Kaggle data source: imet-2019-fgvc6<define_variables>
fastai.__version__
cols_train = ['Store', 'DayOfWeek', 'Date', 'Sales', 'Promo','PromoOpen',
'StateHoliday', 'SchoolHoliday', 'StoreType', 'Assortment',
'CompetitionDistance', 'Promo2', 'Month', 'Year', 'Day','IsPromoMonth',
'Sales_StrType_DayOfWeek','SalesPerCustomer_StrType_Assortment', 'Customers_StrType_Promo2',
'Sales_StrType_Promo', 'Sales_StrType_Promo2', 'Sales_StrType_Month','Customers_StrType_StateHoliday',
'Sales_StrType_Year', 'Sales_StrType_Day', 'Sales_StrType_StateHoliday','SalesPerCustomer_StrType_Promo2',
'Sales_StrType_Assortment', 'Customers_StrType_DayOfWeek', 'Customers_StrType_Promo',
'Customers_StrType_Month', 'Customers_StrType_Year', 'Customers_StrType_Day', 'SalesPerCustomer_StrType_StateHoliday',
'Customers_StrType_Assortment', 'SalesPerCustomer_StrType_DayOfWeek', 'SalesPerCustomer_StrType_Promo',
'SalesPerCustomer_StrType_Month', 'SalesPerCustomer_StrType_Year', 'SalesPerCustomer_StrType_Day',
]<compute_test_metric>
BATCH = 126
SIZE = 250
path = Path('.. /input/imet-2019-fgvc6/' )
params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.02,
"max_depth": 11,
"subsample": 0.9,
"colsample_bytree": 0.7,
"silent": 1,
"seed": 10,
'tree_method': 'gpu_hist',
}
num_boost_round = 800
def rmspe_xg(yhat, y):
y = np.expm1(y.get_label())
 yhat = np.expm1(yhat)
 return "rmspe", rmspe(y,yhat)
<normalization>
Path('models' ).mkdir(exist_ok=True)
 !cp '.. /input/resnet50/resnet50.pth' 'models/'
def load_url(*args, **kwargs):
model_dir = Path('models')
 filename = 'resnet50.pth'
if not(model_dir/filename ).is_file() : raise FileNotFoundError
return torch.load(model_dir/filename)
 model_zoo.load_url = load_url
def process(x, cols=None, all_stores=False):
x.sort_values("Date",inplace=True)
 if cols is None:
cols = x.columns
x = x.fillna(x.median())
 x_train = x[x["Date"]<="2015-06-12"][cols].copy()
x_test = x[x["Date"]>"2015-06-12"][cols].copy()
store_test = x_test['Store'].unique().tolist()
x_train = x_train[(x_train['Store'].isin(store_test)) ]
y_train = np.log(x_train['Sales'])
 if all_stores:
rmv = ['Date', 'Sales']
else:
rmv = ['Date', 'Sales', 'Store']
x_train= x_train.drop(rmv, 1)
 x_train = pd.get_dummies(x_train)
 x_train_arr = x_train.values
x_test_arr = pd.get_dummies(x_test.drop(rmv, 1)).values
return x_train.columns, x_train_arr, y_train, x_test, x_test_arr<find_best_model_class>
train_df = pd.read_csv(path/'train.csv')
 train_df.head()
fig, ax = plt.subplots(5, 2, figsize=(25, 15))
X = df_train[df_train['Store'].isin(top_stores)].copy()
X_train_col, X_train_arr, Y_train, X_test, X_test_arr = process(X, cols_train, True)
 dtrain = xgb.DMatrix(X_train_arr, Y_train)
 estimator = xgb.train(params, dtrain, num_boost_round, feval=rmspe_xg,)
 Y_pred = estimator.predict(xgb.DMatrix(X_test_arr))
X_test["Pred"] = np.exp(Y_pred)
 scores = np.round(mean_squared_error(X_test['Sales'], X_test["Pred"]))
cpt = 0
for i in top_stores[:5]:
x_train = df_train[df_train["Store"]==i]
x_test = X_test[X_test["Store"]==i]
ax[cpt, 0].plot(x_train["Date"], x_train["Sales"])
 ax[cpt, 0].plot(x_test["Date"], x_test["Pred"])
 ax[cpt, 0].set_title(i)
 ax[cpt, 1].scatter(x_test["Date"].values, x_test['Sales'].values - x_test["Pred"].values)
 ax[cpt, 1].plot(x_test["Date"], [0 for _ in range(len(x_test)) ])
 ax[cpt, 1].set_title(np.round(mean_squared_error(X_test['Sales'], X_test["Pred"])))
 cpt+=1
plt.tight_layout()
print(np.mean(scores))<init_hyperparams>
labels_df = pd.read_csv(path/'labels.csv')
 labels_df.head()
num_boost_round = 500
params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.03,
"max_depth": 10,
"subsample": 0.9,
"colsample_bytree": 0.7,
"silent": 1,
"seed": 10,
'tree_method': 'gpu_hist',
}
tmp = prepareDf(all_data, True)
 tmp = tmp.fillna(tmp.dropna().median())
 test = tmp[tmp['part']=='test'].sort_values('Id')
 df_train = tmp[tmp['part']=='train']
result = pd.Series()
X_train = df_train
X_train= X_train[cols_train]
X_test = test
store_test = X_test['Store'].unique().tolist()
X_train = X_train[(X_train['Store'].isin(store_test)) ]
store_ind = X_test["Id"]
Y_train = np.log(X_train["Sales"])
 X_train = pd.get_dummies(X_train.drop(['Date', 'Sales'], 1)).values
X_test = pd.get_dummies(X_test[cols_train] ).drop(['Date', 'Sales'], 1 ).values
dtrain = xgb.DMatrix(X_train, Y_train)
 estimator = xgb.train(params, dtrain, num_boost_round, feval=rmspe_xg,)
 Y_pred = np.exp(estimator.predict(xgb.DMatrix(X_test)))
 result = result.append(pd.Series(Y_pred, index=store_ind))
results = pd.DataFrame({ "Id": result.index, "Sales": result.values} )<merge>
test_df = pd.read_csv(path/'sample_submission.csv')
 test_df.head()
df_test = pd.read_csv('.. /input/test.csv')
 df_test = pd.merge(df_test, stores, 'left', 'Store')
 df_test.head()<save_to_csv>
tfms = get_transforms(do_flip=True, flip_vert=False, max_rotate=0.10, max_zoom=1.5, max_warp=0.2, max_lighting=0.2,
xtra_tfms=[(symmetric_warp(magnitude=(-0,0), p=0)) ,] )
merged_test = pd.merge(df_test, results, 'left', ['Id'])
 merged_test.loc[ merged_test.Open == 0, 'Sales' ] = 0
sub = merged_test[['Id', 'Sales']].copy()
sub['Sales'] = sub['Sales'].fillna(0)
 sub.to_csv('submission.csv', index=False)
 sub.info()<compute_test_metric>
data.show_batch()
def metric(y_true, y_pred):
return np.mean(np.sum(np.abs(y_true - y_pred), axis=0)/np.sum(y_true, axis=0))<load_from_csv>
class FocalLoss(nn.Module):
def __init__(self, gamma=2):
super().__init__()
self.gamma = gamma
def forward(self, logit, target):
target = target.float()
max_val =(-logit ).clamp(min=0)
 loss = logit - logit * target + max_val + \ 
(( -max_val ).exp() +(-logit - max_val ).exp() ).log()
invprobs = F.logsigmoid(-logit *(target * 2.0 - 1.0))
loss =(invprobs * self.gamma ).exp() * loss
if len(loss.size())==2:
loss = loss.sum(dim=1)
 return loss.mean()
fnc_df = cudf.read_csv(".. /input/trends-assessment-prediction/fnc.csv")
 loading_df = cudf.read_csv(".. /input/trends-assessment-prediction/loading.csv")
 fnc_features, loading_features = list(fnc_df.columns[1:]), list(loading_df.columns[1:])
 df = fnc_df.merge(loading_df, on="Id")
 labels_df = cudf.read_csv(".. /input/trends-assessment-prediction/train_scores.csv")
 labels_df["is_train"] = True
df = df.merge(labels_df, on="Id", how="left")
 test_df = df[df["is_train"] != True].copy()
df = df[df["is_train"] == True].copy()
df.shape, test_df.shape<normalization>
learn = cnn_learner(data, base_arch=models.resnet50, loss_func=FocalLoss() , metrics=fbeta )
FNC_SCALE = 1/500
df[fnc_features] *= FNC_SCALE
test_df[fnc_features] *= FNC_SCALE<prepare_x_and_y>
learn.lr_find()
learn.recorder.plot()
%%time
NUM_FOLDS = 7
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=0)
 features = loading_features + fnc_features
overal_score = 0
for target, c, w in [("age", 100, 0.3),("domain1_var1", 10, 0.175),("domain1_var2", 10, 0.175),("domain2_var1", 10, 0.175),("domain2_var2", 10, 0.175)]:
y_oof = np.zeros(df.shape[0])
 y_test = np.zeros(( test_df.shape[0], NUM_FOLDS))
for f,(train_ind, val_ind)in enumerate(kf.split(df, df)) :
train_df, val_df = df.iloc[train_ind], df.iloc[val_ind]
train_df = train_df[train_df[target].notnull() ]
model = SVR(C=c, cache_size=3000.0)
 model.fit(train_df[features], train_df[target])
 y_oof[val_ind] = model.predict(val_df[features])
 y_test[:, f] = model.predict(test_df[features])
 df["pred_{}".format(target)] = y_oof
test_df[target] = y_test.mean(axis=1)
 score = metric(df[df[target].notnull() ][target].values, df[df[target].notnull() ]["pred_{}".format(target)].values)
 overal_score += w*score
print(target, np.round(score, 4))
print()
print("Overal score:", np.round(overal_score, 4))<data_type_conversions>
learn.unfreeze()
learn.fit_one_cycle(22, slice(1e-5,1e-2))
learn.freeze()
learn.save('stage-1', return_path=True )
sub_df = cudf.melt(test_df[["Id", "age", "domain1_var1", "domain1_var2", "domain2_var1", "domain2_var2"]], id_vars=["Id"], value_name="Predicted")
 sub_df["Id"] = sub_df["Id"].astype("str")+ "_" + sub_df["variable"].astype("str")
 sub_df = sub_df.drop("variable", axis=1 ).sort_values("Id")
 assert sub_df.shape[0] == test_df.shape[0]*5
sub_df.head(10 )<save_to_csv>
valid_preds = learn.get_preds(DatasetType.Valid)
 best_thr = find_best_fixed_threshold(*valid_preds )
sub_df.to_csv("submission.csv", index=False )<set_options>
test_preds = learn.TTA(ds_type=DatasetType.Test)
 test_df.attribute_ids = join_preds(test_preds, best_thr)
 test_df.head()
<install_modules><EOS> 
test_df.to_csv('submission.csv', index=False )