Kaggle competition: ALASKA2 Image Steganalysis
<SOS> metric: WeightedAUC Kaggle data source: alaska2-image-steganalysis<data_type_conversions>
!pip install -q efficientnet_pytorch > /dev/null
train['bool_belongs_to_collection'] =(train['belongs_to_collection'].notnull() ).astype(int)
 test['bool_belongs_to_collection'] =(test['belongs_to_collection'].notnull() ).astype(int )<define_variables>
SEED = 42
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed_everything(SEED )
len(train_tagline_keyword_w_cols )<data_type_conversions>
def get_net() :
net = EfficientNet.from_pretrained('efficientnet-b2')
 net._fc = nn.Linear(in_features=1408, out_features=4, bias=True)
 return net
net = get_net().cuda()
train['release_date'] = pd.to_datetime(train['release_date'])
 test['release_date'] = pd.to_datetime(test['release_date'] )<feature_engineering>
checkpoint = torch.load('.. /input/eb2-weights/best-checkpoint-038epoch_c.bin')
 net.load_state_dict(checkpoint['model_state_dict']);
net.eval() ;
train['release_month'] = train['release_date'].dt.month
test['release_month'] = test['release_date'].dt.month 
<feature_engineering>
class DatasetSubmissionRetriever(Dataset):
def __init__(self, image_names, transforms=None):
super().__init__()
self.image_names = image_names
self.transforms = transforms
def __getitem__(self, index: int):
image_name = self.image_names[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/Test/{image_name}', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image_name, image
def __len__(self)-> int:
return self.image_names.shape[0]
train['release_year'] = train['release_date'].dt.year
test['release_year'] = test['release_date'].dt.year 
<feature_engineering>
DATA_ROOT_PATH = '.. /input/alaska2-image-steganalysis'
results = []
for mode in range(0, 4):
dataset = DatasetSubmissionRetriever(
 image_names=np.array([path.split('/')[-1] for path in glob('.. /input/alaska2-image-steganalysis/Test/*.jpg')]),
transforms=get_test_transforms(mode), 
)
 data_loader = DataLoader(
 dataset,
batch_size=8,
shuffle=False,
num_workers=2,
drop_last=False, 
)
 result = {'Id': [], 'Label': []}
for step,(image_names, images)in enumerate(data_loader):
print(step, end='\r')
 y_pred = net(images.cuda())
 y_pred = 1 - nn.functional.softmax(y_pred, dim=1 ).data.cpu().numpy() [:,0]
result['Id'].extend(image_names)
 result['Label'].extend(y_pred)
 results.append(result )
<feature_engineering><EOS> 
y_pred = net(images.cuda())
 y_pred = 1 - nn.functional.softmax(y_pred, dim=1 ).data.cpu().numpy() [:,0]
result['Id'].extend(image_names)
 result['Label'].extend(y_pred)
 submissions = []
for mode in range(0,4):
submission = pd.DataFrame(results[mode])
 submissions.append(submission)
 for mode in range(0,4):
submissions[mode].to_csv(f'submission_{mode}.csv', index=False)
 submissions[0]['Label'] =(submissions[0]['Label']*3 + submissions[1]['Label']*1 + submissions[2]['Label']*1 + submissions[3]['Label']*1)/ 6
submissions[0].to_csv(f'submission_B2_c.csv', index=False )