Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<train_on_grid>
train = pd.read_csv(".. /input/bike-sharing-demand/train.csv")
 test = pd.read_csv(".. /input/bike-sharing-demand/test.csv")
 train.head()
class EffnetTest(nn.Module):
def __init__(self, version):
super(EffnetTest, self ).__init__()
self.model = EfficientNet.from_name(f"efficientnet-{version}", override_params={"num_classes":1})
 self.model.fc = nn.Linear(512, 1)
 self.model._norm_layer = nn.GroupNorm(num_groups=32, num_channels=3)
 def forward(self, x):
x = self.model(x)
 return torch.sigmoid(x )<load_pretrained>
for df in [train, test]:
df["datetime"] = pd.DatetimeIndex(df["datetime"])
 df["hour"] = [x.hour for x in df["datetime"]]
df["weekday"] = [x.dayofweek for x in df["datetime"]]
df["month"] = [x.month for x in df["datetime"]]
df["year"] = [x.year for x in df["datetime"]]
df['year_season'] = df['year'].astype(str)+ "_" + df['season'].astype(str)
 df["year"] = df["year"].map({2011:1, 2012:0})
 df.drop('datetime',axis=1,inplace=True )
class SimpleCNNInference:
def __init__(self, model_path, version='b6', img_size=200):
self.img_size = img_size
self.transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.Resize(size=(self.img_size, self.img_size)) ,
transforms.ToTensor() ,
transforms.Normalize(( 0.485, 0.456, 0.406),(0.229, 0.224, 0.225)) ])
 self.device = torch.device("cuda")
 self.model = EffnetTest(version=version)
 self.model.load_state_dict(torch.load(model_path, map_location="cuda:0"))
self.model.to(self.device)
 self.model.eval()
def predict_single_from_array(self, img_frame):
img_frame = cv2.cvtColor(img_frame, cv2.COLOR_BGR2RGB)
 img_frame = self.transform(img_frame ).to(self.device ).unsqueeze(0)
 return float(self.model(img_frame))<choose_model_class>
df_list = {"train":None, "test" : None}
for name, df in zip(df_list.keys() ,[train, test]):
df['windspeed'] = np.log(df['windspeed']+1)
 df["weekday_working"] = df["weekday"]*df["workingday"]
df["weekday_holiday"] = df["weekday"]*df["holiday"]
df['casual_workhour'] = df[['hour', 'workingday']].apply(lambda x: int(x['workingday'] == 0 and 10 <= x['hour'] <= 19), axis=1)
 df['casual_holi_hour'] = df[['hour', 'holiday']].apply(lambda x: int(x['holiday'] == 1 and 9 <= x['hour'] <= 22), axis=1)
 df['register_workhour'] = df[['hour', 'workingday']].apply(
 lambda x:int(( x['workingday'] == 1 and(6 <= x['hour'] <= 8 or 17 <= x['hour'] <= 20))
or(x['workingday'] == 0 and 10 <= x['hour'] <= 15)) , axis=1)
 df['register_holi_hour'] = df[['hour', 'holiday']].apply(
 lambda x:int(x['holiday'] == 0 and(7 <= x['hour'] <= 8 or 17 <= x['hour'] <= 18)) , axis=1)
 df.drop('atemp',axis=1,inplace=True)
 by_season = train.groupby('year_season')[['count']].median()
by_season.columns = ['count_season']
train1 = train.join(by_season, on='year_season' ).drop('year_season',axis=1)
 test1 = test.join(by_season, on='year_season' ).drop('year_season',axis=1 )
test_filenames = [string.split('/')[-1] for string in test_img_list]
detector = RetinaFaceDetector(weights="/kaggle/input/resnetretinaface/Resnet50_Final.pth")
 inference = SimpleCNNInference(model_path="/kaggle/input/effnetb6-200-0741/EffnetB6_pytorch_group_imgface6_200_0.0741.pth",
version='b6', img_size=200)
 inference2 = SimpleCNNInference(model_path="/kaggle/input/testefficientnet/testEfficientNet.pth",
version='b5', img_size=224)
 inference3 = SimpleCNNInference(model_path="/kaggle/input/effnetb5-imgface6-0074/EffnetB5_imgface6_0.074.pth",
version='b5', img_size=224)
 inference4 = SimpleCNNInference(model_path="/kaggle/input/effnetb6-0071-finetuned/EffnetB6_pytorch_group_imgface6_0.071_finetuned.pth",
version='b6', img_size=224)
 inference5 = SimpleCNNInference(model_path="/kaggle/input/effnetb4-0691/EffnetB4_pytorch_group_imgface6_0.0691.pth",
version='b4', img_size=224)
 frame_skip = 16
res = 2
final_predictions = dict()
for i, video in enumerate(tqdm(test_img_list)) :
single_predictions = []
cap = cv2.VideoCapture(video)
 for j in range(300):
_ = cap.grab()
if j % frame_skip == 0:
pass
else:
continue
try:
_, frame = cap.retrieve()
result = detector.detect_faces(frame[::res, ::res])
 if result == []:
continue
bounding_box = result[0]
x1, y1 = bounding_box[0]*res, bounding_box[1]*res
x2, y2 = bounding_box[2]*res, bounding_box[3]*res
frame_face = frame[y1:y2, x1:x2]
pred = inference.predict_single_from_array(frame_face)
 single_predictions.append(1-pred)
 pred2 = inference2.predict_single_from_array(frame_face)
 single_predictions.append(1-pred2)
 pred3 = inference3.predict_single_from_array(frame_face)
 single_predictions.append(1-pred3)
 pred4 = inference4.predict_single_from_array(frame_face)
 single_predictions.append(1-pred4)
 pred5 = inference5.predict_single_from_array(frame_face)
 single_predictions.append(1-pred5)
 except:
print("E")
 final_predictions[test_filenames[i]] = np.mean(single_predictions)
 cv2.destroyAllWindows()<prepare_output>
y_list = ["casual","registered","count"]
train_x = train1[[col for col in train1.columns if col not in ['casual','registered', 'count']]]
train_y = np.log(train1[y_list]+1 )
sub = pd.read_csv(SAMP_PATH)
 sub['label'] = sub['filename'].map(final_predictions ).clip(0.01, 0.99 ).fillna(0.5 )<save_to_csv>
folds = KFold(n_splits = 5, shuffle = True, random_state = 123)
 rms1,rms2 = [],[]
models1,models2 = [], []
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(train1)) :
x_train, y_train = train_x.ix[trn_idx], train_y.ix[trn_idx]
x_val, y_val = train_x.ix[val_idx], train_y.ix[val_idx]
lgb_param = {'boosting_type':'gbdt',
'num_leaves': 45,
'max_depth': 30,
'learning_rate': 0.01,
'bagging_fraction' : 0.9,
'bagging_freq': 20,
'colsample_bytree': 0.9,
'metric': 'rmse',
'min_child_weight': 1,
'min_child_samples': 10,
'zero_as_missing': True,
'objective': 'regression',
}
train_set1 = lgb.Dataset(x_train, y_train["registered"], silent=False)
 valid_set1 = lgb.Dataset(x_val, y_val["registered"], silent=False)
 lgb_model1 = lgb.train(params = lgb_param, train_set = train_set1 , num_boost_round=5000, early_stopping_rounds=100,verbose_eval=500, valid_sets=valid_set1)
 train_set2 = lgb.Dataset(x_train, y_train["casual"], silent=False)
 valid_set2 = lgb.Dataset(x_val, y_val["casual"], silent=False)
 lgb_model2 = lgb.train(params = lgb_param, train_set = train_set2 , num_boost_round=5000, early_stopping_rounds=100,verbose_eval=500, valid_sets=valid_set2)
 models1.append(lgb_model1)
 models2.append(lgb_model2 )
sub.to_csv('submission.csv', index=False )<install_modules>
preds = []
for model in models1:
regi_pred = model.predict(test1)
 preds.append(regi_pred)
 fin_casual = np.mean(preds, axis=0)
 preds = []
for model in models2:
casual_pred = model.predict(test1)
 preds.append(casual_pred)
 fin_regi = np.mean(preds, axis=0)
 count_pred1 = np.exp(fin_casual)+ np.exp(fin_regi)- 2
%%time
%%capture 
<define_variables>
def lgb_cv(num_leaves, learning_rate, n_estimators, reg_alpha, reg_lambda, min_split_gain, min_child_weight,min_child_samples, colsample_bytree, x_data=None, y_data=None, n_splits=5, output='score'):
score = 0
kf = KFold(n_splits=n_splits)
 models = []
for train_index, valid_index in kf.split(x_data):
x_train, y_train = x_data.iloc[train_index], y_data[train_index]
x_valid, y_valid = x_data.iloc[valid_index], y_data[valid_index]
model = lgb.LGBMRegressor(
 num_leaves = int(num_leaves),
learning_rate = learning_rate,
n_estimators = int(n_estimators),
reg_alpha = reg_alpha,
reg_lambda = reg_lambda,
min_split_gain= min_split_gain,
min_child_weight = min_child_weight,
min_child_samples = int(min_child_samples),
colsample_bytree = np.clip(colsample_bytree, 0, 1), 
)
 model.fit(x_train, y_train)
 models.append(model)
 pred = model.predict(x_valid)
 true = y_valid
score -= mean_squared_error(true, pred)/n_splits
if output == 'score':
return score
if output == 'model':
return models
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 len(test_videos )<import_modules>
func_fixed1 = partial(lgb_cv, x_data=train_x, y_data=train_y["casual"], n_splits=5, output='score')
 func_fixed2 = partial(lgb_cv, x_data=train_x, y_data=train_y["registered"], n_splits=5, output='score')
 lgbBO = BayesianOptimization(
 func_fixed1,
{
'num_leaves':(30, 100),
'learning_rate':(0.001, 0.015),
'n_estimators':(1000, 3000),
'reg_alpha':(0.0001, 1),
'reg_lambda':(0.0001, 1),
'min_split_gain' :(0.001, 0.1),
'min_child_weight' :(0.001, 0.1),
'min_child_samples' :(10,25),
'colsample_bytree':(0.85, 1.0),
},
random_state=4321 
)
 lgbBO.maximize(init_points=5, n_iter=20)
 lgbB1 = BayesianOptimization(
 func_fixed2,
{
'num_leaves':(30, 100),
'learning_rate':(0.001, 0.015),
'n_estimators':(1000, 3000),
'reg_alpha':(0.0001, 1),
'reg_lambda':(0.0001, 1),
'min_split_gain' :(0.001, 0.1),
'min_child_weight' :(0.001, 0.1),
'min_child_samples' :(10,25),
'colsample_bytree':(0.85, 1.0),
},
random_state=4321 
)
 lgbB1.maximize(init_points=5, n_iter=20 )
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )<set_options>
params1 = lgbBO.max['params']
params2 = lgbB1.max['params']
lgb_models1 = lgb_cv(
 params1['num_leaves'],
params1['learning_rate'],
params1['n_estimators'],
params1['reg_alpha'],
params1['reg_lambda'],
params1['min_split_gain'],
params1['min_child_weight'],
params1['min_child_samples'],
params1['colsample_bytree'],
x_data=train_x, y_data=train_y["casual"], n_splits=5, output='model')
 lgb_models2 = lgb_cv(
 params2['num_leaves'],
params2['learning_rate'],
params2['n_estimators'],
params2['reg_alpha'],
params2['reg_lambda'],
params2['min_split_gain'],
params2['min_child_weight'],
params2['min_child_samples'],
params2['colsample_bytree'],
x_data=train_x, y_data=train_y["registered"], n_splits=5, output='model')
 preds = []
for model in lgb_models1:
pred = model.predict(test1)
 preds.append(pred)
 casual_pred = np.mean(preds, axis=0)
 preds = []
for model in lgb_models2:
pred = model.predict(test1)
 preds.append(pred)
 registered_pred = np.mean(preds, axis=0)
 count_pred2 = np.exp(casual_pred)+ np.exp(registered_pred)- 2
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
<load_pretrained>
preds = {}
regs = {"gbdt": GradientBoostingRegressor(random_state=0),
"rf": RandomForestRegressor(random_state=0, n_jobs=-1)}
for name, reg in regs.items() :
if name == 'gbdt':
reg.set_params(n_estimators=1500, min_samples_leaf=6)
 elif name == 'rf':
reg.set_params(n_estimators=1500, min_samples_leaf=2)
 reg.fit(train_x, train_y['casual'])
 pred_casual = reg.predict(test1)
 pred_casual = np.exp(pred_casual)- 1
pred_casual[pred_casual < 0] = 0
if name == 'gbdt':
reg.set_params(n_estimators=1500, min_samples_leaf=6)
 elif name == 'rf':
reg.set_params(n_estimators=1500, min_samples_leaf=2)
 reg.fit(train_x, train_y['registered'])
 pred_registered = reg.predict(test1)
 pred_registered = np.exp(pred_registered)- 1
pred_registered[pred_registered < 0] = 0
preds[name] = pred_casual + pred_registered
<define_variables><EOS> 
pred_mean =(count_pred1 + count_pred2 + preds['gbdt'] + preds['rf'])/4
sample = pd.read_csv(".. /input/bike-sharing-demand/sampleSubmission.csv")
 sample["count"] = pred_mean
sample.to_csv("sample.csv",index=False )