Kaggle competition: ALASKA2 Image Steganalysis
<SOS> metric: WeightedAUC Kaggle data source: alaska2-image-steganalysis<split>
!pip install -q efficientnet_pytorch
Compose, HorizontalFlip,
ToFloat, VerticalFlip 
)
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.1)
 params = {'num_leaves': 30,
'min_data_in_leaf': 20,
'objective': 'regression',
'max_depth': 6,
'learning_rate': 0.01,
"boosting": "gbdt",
"feature_fraction": 0.9,
"bagging_freq": 1,
"bagging_fraction": 0.9,
"bagging_seed": 11,
"metric": 'rmse',
"lambda_l1": 0.2,
"verbosity": -1}
model1 = lgb.LGBMRegressor(**params, n_estimators = 20000, nthread = 4, n_jobs = -1)
 model1.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='rmse',
verbose=1000, early_stopping_rounds=200)
 eli5.show_weights(model1, feature_filter=lambda x: x != '<BIAS>' )<feature_engineering>
data_dir = '.. /input/alaska2-image-steganalysis'
folder_names = ['JMiPOD/', 'JUNIWARD/', 'UERD/']
class_names = ['Normal', 'JMiPOD_75', 'JMiPOD_90', 'JMiPOD_95',
'JUNIWARD_75', 'JUNIWARD_90', 'JUNIWARD_95',
'UERD_75', 'UERD_90', 'UERD_95']
class_labels = { name: i for i, name in enumerate(class_names)}
def top_cols_interaction(df):
df['budget_to_year'] = df['budget'] / df['release_date_year']
df['budget_to_mean_year_to_year'] = df['budget_to_mean_year'] / df['release_date_year']
df['popularity_to_mean_year_to_log_budget'] = df['popularity_to_mean_year'] / df['log_budget']
df['year_to_log_budget'] = df['release_date_year'] / df['log_budget']
df['budget_to_runtime_to_year'] = df['budget_to_runtime'] / df['release_date_year']
df['genders_1_cast_to_log_budget'] = df['genders_1_cast'] / df['log_budget']
df['all_genres_to_popularity_to_mean_year'] = df['all_genres'] / df['popularity_to_mean_year']
df['genders_2_crew_to_budget_to_mean_year'] = df['genders_2_crew'] / df['budget_to_mean_year']
df['overview_oof_to_genders_2_crew'] = df['overview_oof'] / df['genders_2_crew']
return df<concatenate>
train_df = pd.read_csv('.. /input/alaska2trainvalsplit/alaska2_train_df.csv')
 train_df = train_df.sample(5000 ).reset_index(drop=True)
 val_df = pd.read_csv('.. /input/alaska2trainvalsplit/alaska2_val_df.csv' )
X = top_cols_interaction(X)
 X_test = top_cols_interaction(X_test )<categorify>
class Alaska2Dataset(Dataset):
def __init__(self, df, augmentations=None, test = False):
self.data = df
self.augment = augmentations
self.test = test
def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
if self.test:
fn = self.data.loc[idx][0]
else:
fn, label = self.data.loc[idx]
im = imread(fn)
 if self.augment:
im = self.augment(image=im)
 if self.test:
item = {'features': im['image']}
else:
item = {'features': im['image'], 'targets':label, 'bool_targets': get_one_hot(label, 10)}
return item
AUGMENTATIONS_TRAIN = Compose([
VerticalFlip(p=0.5),
HorizontalFlip(p=0.5),
ToFloat(max_value=255),
ToTensorV2()
], p=1)
 AUGMENTATIONS_TEST = Compose([
ToFloat(max_value=255),
ToTensorV2()
], p=1 )
X = X.replace([np.inf, -np.inf], 0 ).fillna(0)
 X_test = X_test.replace([np.inf, -np.inf], 0 ).fillna(0 )<load_from_csv>
batch_size = 24
num_workers = 8
train_dataset = Alaska2Dataset(train_df, augmentations=AUGMENTATIONS_TRAIN)
 valid_dataset = Alaska2Dataset(val_df.sample(5000 ).reset_index(drop=True), augmentations=AUGMENTATIONS_TEST)
 train_loader = torch.utils.data.DataLoader(train_dataset,
batch_size=batch_size,
num_workers=num_workers,
shuffle=True)
 valid_loader = torch.utils.data.DataLoader(valid_dataset,
batch_size=batch_size*2,
num_workers=num_workers,
shuffle=False )
trainAdditionalFeatures = pd.read_csv('.. /input/tmdb-competition-additional-features/TrainAdditionalFeatures.csv')
 testAdditionalFeatures = pd.read_csv('.. /input/tmdb-competition-additional-features/TestAdditionalFeatures.csv')
 train = pd.read_csv('.. /input/tmdb-box-office-prediction/train.csv')
 test = pd.read_csv('.. /input/tmdb-box-office-prediction/test.csv')
 X['imdb_id'] = train['imdb_id']
X_test['imdb_id'] = test['imdb_id']
del train, test
X = pd.merge(X, trainAdditionalFeatures, how='left', on=['imdb_id'])
 X_test = pd.merge(X_test, testAdditionalFeatures, how='left', on=['imdb_id'])
 X = X.drop(['imdb_id'], axis=1)
 X_test = X_test.drop(['imdb_id'], axis=1 )<train_model>
class Net(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = EfficientNet.from_name('efficientnet-b0')
 self.dense_output = nn.Linear(1280, num_classes)
 def forward(self, x):
feat = self.model.extract_features(x)
 feat = F.avg_pool2d(feat, feat.size() [2:] ).reshape(-1, 1280)
 return self.dense_output(feat )
params = {'num_leaves': 30,
'min_data_in_leaf': 20,
'objective': 'regression',
'max_depth': 9,
'learning_rate': 0.01,
"boosting": "gbdt",
"feature_fraction": 0.9,
"bagging_freq": 1,
"bagging_fraction": 0.9,
"bagging_seed": 11,
"metric": 'rmse',
"lambda_l1": 0.2,
"verbosity": -1}
oof_lgb, prediction_lgb, _ = train_model(X, X_test, y, params=params, model_type='lgb', plot_feature_importance=True )<train_model>
loaders = {
"train": train_loader,
"valid": valid_loader
}
model = Net(num_classes=len(class_labels))
model.load_state_dict(torch.load('.. /input/alaska2trainvalsplit/epoch_5_val_loss_3.75_auc_0.833.pth'))
optimizer = torch.optim.AdamW(model.parameters() , lr=0.0003)
 criterion = torch.nn.CrossEntropyLoss()
callbacks = [
AccuracyCallback() ,
AUCCallback(input_key='bool_targets', num_classes = 1)
 ]
runner = SupervisedRunner()
xgb_params = {'eta': 0.01,
'objective': 'reg:linear',
'max_depth': 7,
'subsample': 0.8,
'colsample_bytree': 0.8,
'eval_metric': 'rmse',
'seed': 11,
'silent': True}
oof_xgb, prediction_xgb = train_model(X, X_test, y, params=xgb_params, model_type='xgb', plot_feature_importance=False )<define_variables>
runner.train(
 model=model,
criterion=criterion,
optimizer=optimizer,
loaders=loaders,
num_epochs=5,
verbose=True,
callbacks=callbacks,
logdir="logs",
main_metric="auc/class_0",
minimize_metric = False,
)
cat_params = {'learning_rate': 0.002,
'depth': 5,
'l2_leaf_reg': 10,
'colsample_bylevel': 0.8,
'bagging_temperature': 0.2,
'od_type': 'Iter',
'od_wait': 100,
'random_seed': 11,
'allow_writing_files': False}
oof_cat, prediction_cat = train_model(X, X_test, y, params=cat_params, model_type='cat' )<train_model>
test_filenames = sorted(glob(f"{data_dir}/Test/*.jpg"))
test_df = pd.DataFrame({'ImageFileName': list(
 test_filenames)}, columns=['ImageFileName'])
 batch_size = 16
num_workers = 4
test_dataset = Alaska2Dataset(test_df, augmentations=AUGMENTATIONS_TEST, test=True)
 test_loader = torch.utils.data.DataLoader(test_dataset,
batch_size=batch_size,
num_workers=num_workers,
shuffle=False,
drop_last=False )
<train_model><EOS> 
model.load_state_dict(torch.load('logs/checkpoints/best.pth')["model_state_dict"])
 model.cuda()
preds = []
for outputs in tqdm(runner.predict_loader(loader=test_loader, model=model)) :
preds.append(softmax(outputs))
preds = np.array(preds)
 test_df['Id'] = test_df['ImageFileName'].apply(lambda x: x.split(os.sep)[-1])
 test_df['Label'] = 1-preds[:, 0]
test_df = test_df.drop('ImageFileName', axis=1)
 test_df.to_csv('submission.csv', index=False)
 print(test_df.head() )