Kaggle competition: Histopathologic Cancer Detection
<SOS> metric: AUC Kaggle data source: histopathologic-cancer-detection<save_to_csv>
%matplotlib inline
train_on_gpu = True
results_df = pd.DataFrame({"row_id": row_ids, "meter_reading": np.clip(results, 0, a_max=None)})
 del row_ids,results
gc.collect()
results_df.to_csv("submission.csv", index=False)
<set_options>
!pip install albumentations > /dev/null 2>&1
!pip install pretrainedmodels > /dev/null 2>&1
!pip install kekas > /dev/null 2>&1
!pip install adabound > /dev/null 2>&1
def reduce_mem_usage(df, use_float16=False):

 start_mem = df.memory_usage().sum() / 1024**2
print("Memory usage of dataframe is {:.2f} MB".format(start_mem))
for col in df.columns:
if is_datetime(df[col])or is_categorical_dtype(df[col]):
continue
col_type = df[col].dtype
if col_type != object:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == "int":
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if use_float16 and c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 else:
df[col] = df[col].astype("category")
 end_mem = df.memory_usage().sum() / 1024**2
print("Memory usage after optimization is: {:.2f} MB".format(end_mem))
print("Decreased by {:.1f}%".format(100 *(start_mem - end_mem)/ start_mem))
return df<load_from_csv>
import albumentations
from albumentations import torch as AT
import pretrainedmodels
import adabound
from kekas import Keker, DataOwner, DataKek
from kekas.transformations import Transformer, to_torch, normalize
from kekas.metrics import accuracy
from kekas.modules import Flatten, AdaptiveConcatPool2d
from kekas.callbacks import Callback, Callbacks, DebuggerCallback
from kekas.utils import DotDict
train_df = pd.read_csv('/kaggle/input/ashrae-energy-prediction/train.csv')
 building_df = pd.read_csv('/kaggle/input/ashrae-energy-prediction/building_metadata.csv')
 weather_df = pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_train.csv' )<filter>
test_img = os.listdir('.. /input/test/')
 test_df = pd.DataFrame(test_img, columns=['id'])
 test_df['label'] = -1
test_df['data_type'] = 'test'
test_df['id'] = test_df['id'].apply(lambda x: x.split('.')[0])
 labels['label'] = labels['label'].astype(int)
 labels['data_type'] = 'train'
labels.head()
<load_from_csv>
train_short, _ = train_test_split(labels, stratify=labels.label, test_size=0.95 )
bad_rows = pd.read_csv('/kaggle/input/rows-to-drop/rows_to_drop.csv')
 train_df.drop(bad_rows.loc[:, '0'], inplace = True)
 train_df.reset_index(drop = True, inplace = True )<data_type_conversions>
train, valid = train_test_split(train_short, stratify=train_short.label, test_size=0.1 )
def fill_weather_dataset(weather_df):
time_format = "%Y-%m-%d %H:%M:%S"
start_date = datetime.datetime.strptime(weather_df['timestamp'].min() ,time_format)
 end_date = datetime.datetime.strptime(weather_df['timestamp'].max() ,time_format)
 total_hours = int(((end_date - start_date ).total_seconds() + 3600)/ 3600)
 hours_list = [(end_date - datetime.timedelta(hours=x)).strftime(time_format)for x in range(total_hours)]
missing_hours = []
for site_id in range(16):
site_hours = np.array(weather_df[weather_df['site_id'] == site_id]['timestamp'])
 new_rows = pd.DataFrame(np.setdiff1d(hours_list,site_hours),columns=['timestamp'])
 new_rows['site_id'] = site_id
weather_df = pd.concat([weather_df,new_rows])
 weather_df = weather_df.reset_index(drop=True)
 weather_df["datetime"] = pd.to_datetime(weather_df["timestamp"])
 weather_df["day"] = weather_df["datetime"].dt.day
weather_df["week"] = weather_df["datetime"].dt.week
weather_df["month"] = weather_df["datetime"].dt.month
weather_df = weather_df.set_index(['site_id','day','month'])
 air_temperature_filler = pd.DataFrame(weather_df.groupby(['site_id','day','month'])['air_temperature'].mean() ,columns=["air_temperature"])
 weather_df.update(air_temperature_filler,overwrite=False)
 cloud_coverage_filler = weather_df.groupby(['site_id','day','month'])['cloud_coverage'].mean()
cloud_coverage_filler = pd.DataFrame(cloud_coverage_filler.fillna(method='ffill'),columns=["cloud_coverage"])
 weather_df.update(cloud_coverage_filler,overwrite=False)
 due_temperature_filler = pd.DataFrame(weather_df.groupby(['site_id','day','month'])['dew_temperature'].mean() ,columns=["dew_temperature"])
 weather_df.update(due_temperature_filler,overwrite=False)
 sea_level_filler = weather_df.groupby(['site_id','day','month'])['sea_level_pressure'].mean()
sea_level_filler = pd.DataFrame(sea_level_filler.fillna(method='ffill'),columns=['sea_level_pressure'])
 weather_df.update(sea_level_filler,overwrite=False)
 wind_direction_filler = pd.DataFrame(weather_df.groupby(['site_id','day','month'])['wind_direction'].mean() ,columns=['wind_direction'])
 weather_df.update(wind_direction_filler,overwrite=False)
 wind_speed_filler = pd.DataFrame(weather_df.groupby(['site_id','day','month'])['wind_speed'].mean() ,columns=['wind_speed'])
 weather_df.update(wind_speed_filler,overwrite=False)
 precip_depth_filler = weather_df.groupby(['site_id','day','month'])['precip_depth_1_hr'].mean()
precip_depth_filler = pd.DataFrame(precip_depth_filler.fillna(method='ffill'),columns=['precip_depth_1_hr'])
 weather_df.update(precip_depth_filler,overwrite=False)
 weather_df = weather_df.reset_index()
weather_df = weather_df.drop(['datetime','day','week','month'],axis=1)
 def get_meteorological_features(data):
def calculate_rh(df):
df['relative_humidity'] = 100 *(np.exp(( 17.625 * df['dew_temperature'])/(243.04 + df['dew_temperature'])) / np.exp(( 17.625 * df['air_temperature'])/(243.04 + df['air_temperature'])))
 def calculate_fl(df):
flike_final = []
flike = []
for i in range(len(df)) :
at = df['air_temperature'][i]
rh = df['relative_humidity'][i]
ws = df['wind_speed'][i]
flike.append(feels_like(Temp(at, unit = 'C'), rh, ws))
for i in range(len(flike)) :
flike_final.append(flike[i].f)
 df['feels_like'] = flike_final
del flike_final, flike, at, rh, ws
calculate_rh(data)
 calculate_fl(data)
 return data
weather_df = get_meteorological_features(weather_df)
 return weather_df<feature_engineering>
def reader_fn(i, row):
image = cv2.imread(f".. /input/{row['data_type']}/{row['id']}.tif")[:,:,::-1]
label = torch.Tensor([row["label"]])
 return {"image": image, "label": label}
def features_engineering(df):
df.sort_values("timestamp")
 df.reset_index(drop=True)
 df["timestamp"] = pd.to_datetime(df["timestamp"],format="%Y-%m-%d %H:%M:%S")
 df["hour"] = df["timestamp"].dt.hour
df["dayofweek"] = df["timestamp"].dt.dayofweek
df['month'] = df['timestamp'].dt.month
df['month'].replace(( 1, 2, 3, 4), 1, inplace = True)
 df['month'].replace(( 5, 6, 7, 8), 2, inplace = True)
 df['month'].replace(( 9, 10, 11, 12), 3, inplace = True)
 df['square_feet'] = np.log1p(df['square_feet'])
 drop = ["timestamp"]
df = df.drop(drop, axis=1)
 gc.collect()
le = LabelEncoder()
df["primary_use"] = le.fit_transform(df["primary_use"])
 return df<merge>
def augs(p=0.5):
return albumentations.Compose([
albumentations.HorizontalFlip() ,
albumentations.RandomRotate90() ,
albumentations.Transpose() ,
albumentations.Flip() ,
], p=p )
weather_df = fill_weather_dataset(weather_df)
 train_df = reduce_mem_usage(train_df,use_float16=True)
 building_df = reduce_mem_usage(building_df,use_float16=True)
 weather_df = reduce_mem_usage(weather_df,use_float16=True)
 train_df = train_df.merge(building_df, left_on='building_id',right_on='building_id',how='left')
 train_df = train_df.merge(weather_df,how='left',left_on=['site_id','timestamp'],right_on=['site_id','timestamp'])
 del weather_df
gc.collect()<feature_engineering>
def get_transforms(dataset_key, size, p):
PRE_TFMS = Transformer(dataset_key, lambda x: cv2.resize(x,(size, size)))
 AUGS = Transformer(dataset_key, lambda x: augs()(image=x)["image"])
 NRM_TFMS = transforms.Compose([
Transformer(dataset_key, to_torch()),
Transformer(dataset_key, normalize())
 ])
 train_tfms = transforms.Compose([PRE_TFMS, AUGS, NRM_TFMS])
 val_tfms = transforms.Compose([PRE_TFMS, NRM_TFMS])
 return train_tfms, val_tfms
train_df = features_engineering(train_df)
 train_df['meter_reading'] = np.log1p(train_df["meter_reading"] )<define_variables>
train_tfms, val_tfms = get_transforms("image", 224, 0.5 )
target = 'meter_reading'
categorical = ['building_id', 'site_id', 'primary_use', 'meter','dayofweek']
numeric_cols = [col for col in train_df.columns if col not in categorical + [target, 'timestamp', 'month']]
features = categorical + numeric_cols<create_dataframe>
train_dk = DataKek(df=train, reader_fn=reader_fn, transforms=train_tfms)
 val_dk = DataKek(df=valid, reader_fn=reader_fn, transforms=val_tfms)
 batch_size = 32
workers = 0
train_dl = DataLoader(train_dk, batch_size=batch_size, num_workers=workers, shuffle=True, drop_last=True)
 val_dl = DataLoader(val_dk, batch_size=batch_size, num_workers=workers, shuffle=False )
def run_lgbm(train, cat_features = categorical, num_rounds = 20000, folds = 5):
kf = StratifiedKFold(n_splits=folds, shuffle=False, random_state=2319)
 models = []
feature_importance_df = pd.DataFrame()
param = {'num_leaves': 3160,
'objective': 'regression',
'learning_rate': 0.03,
'boosting': 'gbdt',
'subsample': 0.5,
'feature_fraction': 0.7,
'n_jobs': -1,
'seed': 50,
'metric': 'rmse'
}
oof = np.zeros(len(train))
for tr_idx, val_idx in tqdm(kf.split(train_df,train_df['month']), total = folds):
tr_x, tr_y = train[features].iloc[tr_idx], train[target].iloc[tr_idx]
vl_x, vl_y = train[features].iloc[val_idx], train[target].iloc[val_idx]
tr_data = lgb.Dataset(tr_x, label = tr_y, categorical_feature = categorical)
 vl_data = lgb.Dataset(vl_x, label = vl_y, categorical_feature = categorical)
 clf = lgb.train(param, tr_data, num_rounds, valid_sets = [tr_data, vl_data], verbose_eval = 25,
early_stopping_rounds = 50)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 models.append(clf)
 oof[val_idx] = clf.predict(vl_x)
 gc.collect()
score = np.sqrt(metrics.mean_squared_error(train[target], np.clip(oof, a_min=0, a_max=None)))
 print('Our oof cv is :', score)
 cols =(feature_importance_df[["feature", "importance"]] 
.groupby("feature")
.mean() 
.sort_values(by="importance", ascending=False)[:20].index)
 best_features = feature_importance_df.loc[feature_importance_df.feature.isin(cols)]
plt.figure(figsize=(14,26))
sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance",ascending=False))
plt.title('LightGBM Features(averaged over folds)')
 plt.tight_layout()
plt.savefig('lgbm_importances.png')
 return models
models = run_lgbm(train_df )<load_from_csv>
test_dk = DataKek(df=test_df, reader_fn=reader_fn, transforms=val_tfms)
 test_dl = DataLoader(test_dk, batch_size=batch_size, num_workers=workers, shuffle=False )
test_df = pd.read_csv('/kaggle/input/ashrae-energy-prediction/test.csv')
 row_ids = test_df["row_id"]
test_df.drop("row_id", axis=1, inplace=True)
 test_df = reduce_mem_usage(test_df)
 test_df = test_df.merge(building_df,left_on='building_id',right_on='building_id',how='left')
 del building_df
gc.collect()
weather_df = pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_test.csv')
 weather_df = fill_weather_dataset(weather_df)
 weather_df = reduce_mem_usage(weather_df)
 test_df = test_df.merge(weather_df,how='left',on=['timestamp','site_id'])
 del weather_df
gc.collect()
test_df = features_engineering(test_df )<load_from_csv>
class Net(nn.Module):
def __init__(
 self,
num_classes: int,
p: float = 0.2,
pooling_size: int = 2,
last_conv_size: int = 81536,
arch: str = "densenet169",
pretrained: str = "imagenet")-> None:

 super().__init__()
net = pretrainedmodels.__dict__[arch](pretrained=pretrained)
 modules = list(net.children())[:-1]
modules += [nn.Sequential(
 AdaptiveConcatPool2d(size=pooling_size),
Flatten() ,
nn.BatchNorm1d(13312),
nn.Dropout(p),
nn.Linear(13312, num_classes)
)]
self.net = nn.Sequential(*modules)
 def forward(self, x):
logits = self.net(x)
 return logits
def predictions(models, iterations = 120):
set_size = len(test_df)
 batch_size = set_size // iterations
meter_reading = []
for i in tqdm(range(iterations)) :
pos = i*batch_size
fold_preds = [np.expm1(model.predict(test_df[features].iloc[pos : pos+batch_size])) for model in models]
meter_reading.extend(np.mean(fold_preds, axis=0))
print(len(meter_reading))
assert len(meter_reading)== set_size
submission = pd.read_csv('/kaggle/input/ashrae-energy-prediction/sample_submission.csv')
 submission['meter_reading'] = np.clip(meter_reading, a_min=0, a_max=None)
 submission.to_csv('fe2_lgbm.csv', index=False)
 print('We are done!')
 predictions(models )<define_variables>
dataowner = DataOwner(train_dl, val_dl, None )
folds = 5
debug = False
num_rounds=1000
use_ucf = True
use_sort = True
del_2016 = False
ucf_year = [2017, 2018]<set_options>
model = Net(num_classes=1)
 criterion = nn.BCEWithLogitsLoss()
py.init_notebook_mode(connected=True)
<set_options>
def step_fn(model: torch.nn.Module,
batch: torch.Tensor)-> torch.Tensor:

 inp = batch["image"]
return model(inp )
def reduce_mem_usage(df, use_float16=False):

 start_mem = df.memory_usage().sum() / 1024**2
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
for col in df.columns:
if is_datetime(df[col])or is_categorical_dtype(df[col]):
continue
col_type = df[col].dtype
if col_type != object:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if use_float16 and c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 else:
df[col] = df[col].astype('category')
 end_mem = df.memory_usage().sum() / 1024**2
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 *(start_mem - end_mem)/ start_mem))
return df<load_from_disk>
def bce_accuracy(target: torch.Tensor,
preds: torch.Tensor)-> float:
thresh = 0.5
target = target.cpu().detach().numpy()
preds =(torch.sigmoid(preds ).cpu().detach().numpy() > thresh ).astype(int)
 return accuracy_score(target, preds )
%%time
root = Path('.. /input/ashrae-feather-format-for-fast-loading')
 train_df = pd.read_feather(root/'train.feather')
 weather_train_df = pd.read_feather(root/'weather_train.feather')
 weather_test_df = pd.read_feather(root/'weather_test.feather')
 building_meta_df = pd.read_feather(root/'building_metadata.feather' )<categorify>
keker = Keker(model=model,
dataowner=dataowner,
criterion=criterion,
step_fn=step_fn,
target_key="label",
metrics={"acc": bce_accuracy},
opt=torch.optim.SGD,
opt_params={"momentum": 0.99} )
ucf_root = Path('.. /input/ashrae-ucf-spider-and-eda-full-test-labels')
 leak0_df = pd.read_pickle(ucf_root/'site0.pkl')
 leak0_df['meter_reading'] = leak0_df.meter_reading_scraped
leak0_df.drop(['meter_reading_original','meter_reading_scraped'], axis=1, inplace=True)
 leak0_df.fillna(0, inplace=True)
 leak0_df.loc[leak0_df.meter_reading < 0, 'meter_reading'] = 0
leak0_df = leak0_df[leak0_df.timestamp.dt.year > 2016]
print(len(leak0_df))<load_pretrained>
keker.unfreeze(model_attr="net")
 layer_num = -1
keker.freeze_to(layer_num, model_attr="net" )
ucl_root = Path('.. /usr/lib/ucl_data_leakage_episode_2')
 leak1_df = pd.read_pickle(ucl_root/'site1.pkl')
 leak1_df['meter_reading'] = leak1_df.meter_reading_scraped
leak1_df.drop(['meter_reading_scraped'], axis=1, inplace=True)
 leak1_df.fillna(0, inplace=True)
 leak1_df.loc[leak1_df.meter_reading < 0, 'meter_reading'] = 0
leak1_df = leak1_df[leak1_df.timestamp.dt.year > 2016]
print(len(leak1_df))<load_from_csv>
keker.kek_lr(final_lr=0.001, logdir="logs" )
leak2_df = pd.read_csv('/kaggle/input/asu-buildings-energy-consumption/asu_2016-2018.csv')
 leak2_df['timestamp'] = pd.to_datetime(leak2_df['timestamp'])
 leak2_df.fillna(0, inplace=True)
 leak2_df.loc[leak2_df.meter_reading < 0, 'meter_reading'] = 0
leak2_df = leak2_df[leak2_df.timestamp.dt.year > 2016]
print(len(leak2_df))<load_from_csv>
leak5_df = pd.read_csv('/kaggle/input/ashrae-site15-cornell/site15_leakage.csv')
 leak5_df['timestamp'] = pd.to_datetime(leak5_df['timestamp'])
 leak5_df.fillna(0, inplace=True)
 leak5_df.loc[leak5_df.meter_reading < 0, 'meter_reading'] = 0
leak5_df = leak5_df[leak5_df.timestamp.dt.year > 2016]
print(len(leak5_df))<define_variables>
keker.kek(lr=2e-5, epochs=1, logdir='train_logs0' )
print('New leak rate = {}'.format(( len(leak0_df)+ len(leak1_df)+ len(leak2_df)+ len(leak5_df)) / len(train_df)) )<feature_engineering>
keker.kek(lr=2e-5, epochs=3,
sched=torch.optim.lr_scheduler.StepLR,
sched_params={"step_size":1, "gamma": 0.9},
opt_params={"weight_decay": 1e-5},
early_stop_params={
"patience": 2,
"metric": "acc",
"mode": "min",
"min_delta": 0
}, logdir='train_logs' )
if use_ucf:
if del_2016:
print('delete all buildings site0 in 2016')
 bids = leak_df.building_id.unique()
train_df = train_df[train_df.building_id.isin(bids)== False]
leak0_df = leak0_df[leak0_df.timestamp.dt.year.isin(ucf_year)]
leak1_df = leak1_df[leak1_df.timestamp.dt.year.isin(ucf_year)]
leak2_df = leak2_df[leak2_df.timestamp.dt.year.isin(ucf_year)]
leak5_df = leak5_df[leak5_df.timestamp.dt.year.isin(ucf_year)]
train_df = pd.concat([train_df, leak0_df, leak1_df, leak2_df,leak5_df])
 train_df.reset_index(inplace=True)
 weather_train_df = pd.concat([weather_train_df, weather_test_df])
 weather_train_df.reset_index(inplace=True )<drop_column>
keker.kek_one_cycle(max_lr=1e-4,
cycle_len=3,
momentum_range=(0.95, 0.85),
div_factor=25,
increase_fraction=0.3,
logdir='train_logs1' )
del weather_test_df, leak0_df, leak1_df, leak2_df, leak5_df
gc.collect()<feature_engineering>
train, valid = train_test_split(labels, stratify=labels.label, test_size=0.1)
 train_dk = DataKek(df=train, reader_fn=reader_fn, transforms=train_tfms)
 val_dk = DataKek(df=valid, reader_fn=reader_fn, transforms=val_tfms)
 train_dl = DataLoader(train_dk, batch_size=batch_size, num_workers=workers, shuffle=True, drop_last=True)
 val_dl = DataLoader(val_dk, batch_size=batch_size, num_workers=workers, shuffle=False)
 test_dk = DataKek(df=test_df, reader_fn=reader_fn, transforms=val_tfms)
 test_dl = DataLoader(test_dk, batch_size=batch_size, num_workers=workers, shuffle=False)
 dataowner = DataOwner(train_dl, val_dl, None)
 keker = Keker(model=model,
dataowner=dataowner,
criterion=criterion,
step_fn=step_fn,
target_key="label",
metrics={"acc": bce_accuracy},
opt=adabound.AdaBound,
opt_params={'final_lr': 0.001})
 keker.unfreeze(model_attr="net")
 layer_num = -3
keker.freeze_to(layer_num, model_attr="net" )
train_df['date'] = train_df['timestamp'].dt.date
train_df['meter_reading_log1p'] = np.log1p(train_df['meter_reading'] )<load_from_csv>
keker.kek_one_cycle(max_lr=1e-4,
cycle_len=6,
momentum_range=(0.95, 0.85),
div_factor=25,
increase_fraction=0.3 )
bad_rows = pd.read_csv('/kaggle/input/rows-to-drop/rows_to_drop.csv')
 train_df.drop(bad_rows.loc[:, '0'], inplace = True)
 train_df.reset_index(drop = True, inplace = True )<feature_engineering>
preds = keker.predict_loader(loader=test_dl )
zone_dict={0:4,1:0,2:7,3:4,4:7,5:0,6:4,7:4,8:4,9:5,10:7,11:4,12:0,13:5,14:4,15:4}
def set_localtime(df):
for sid, zone in zone_dict.items() :
sids = df.site_id == sid
df.loc[sids, 'timestamp'] = df[sids].timestamp - pd.offsets.Hour(zone )<feature_engineering>
flip_ = albumentations.Flip(always_apply=True)
 transpose_ = albumentations.Transpose(always_apply=True)
 def insert_aug(aug, dataset_key="image", size=224):
PRE_TFMS = Transformer(dataset_key, lambda x: cv2.resize(x,(size, size)))
 AUGS = Transformer(dataset_key, lambda x: aug(image=x)["image"])
 NRM_TFMS = transforms.Compose([
Transformer(dataset_key, to_torch()),
Transformer(dataset_key, normalize())
 ])
 tfm = transforms.Compose([PRE_TFMS, AUGS, NRM_TFMS])
 return tfm
flip = insert_aug(flip_)
 transpose = insert_aug(transpose_)
 tta_tfms = {"flip": flip, "transpose": transpose}
keker.TTA(loader=test_dl,
tfms=tta_tfms,
savedir="tta_preds1",
prefix="preds")
def preprocess(df):
df["hour"] = df["timestamp"].dt.hour
df["weekend"] = df["timestamp"].dt.weekday
df["month"] = df["timestamp"].dt.month<categorify>
prediction = np.zeros(( test_df.shape[0], 1))
for i in os.listdir('tta_preds1'):
pr = np.load('tta_preds1/' + i)
 prediction += pr
prediction = prediction / len(os.listdir('tta_preds1'))
preprocess(train_df )<sort_values>
test_preds = pd.DataFrame({'imgs': test_df.id.values, 'preds': prediction.reshape(-1,)})
 test_preds.columns = ['id', 'label']
test_preds.to_csv('sub.csv', index=False )