Kaggle competition: Humpback Whale Identification
<SOS> metric: MAP@{K} Kaggle data source: humpback-whale-identification<define_variables>
%matplotlib inline
warnings.filterwarnings("ignore",category=DeprecationWarning )
categorical_features = [
"building_id",
"primary_use",
"meter",
"weekday",
"hour"
]
all_features = [col for col in df_train.columns if col not in ["timestamp", "site_id", "meter_reading", "log_meter_reading"]]<split>
train_df = pd.read_csv('.. /input/train.csv')
 TRN_IMGS_DIR = '.. /input/train/'
TST_IMGS_DIR = '.. /input/test/'
cv = 2
models = {}
cv_scores = {"site_id": [], "cv_score": []}
for site_id in tqdm(range(16), desc="site_id"):
print(cv, "fold CV for site_id:", site_id)
 kf = KFold(n_splits=cv, random_state=seed)
 models[site_id] = []
X_train_site = df_train[df_train.site_id==site_id].reset_index(drop=True)
 y_train_site = X_train_site.log_meter_reading
y_pred_train_site = np.zeros(X_train_site.shape[0])
 score = 0
for fold,(train_index, valid_index)in enumerate(kf.split(X_train_site, y_train_site)) :
X_train, X_valid = X_train_site.loc[train_index, all_features], X_train_site.loc[valid_index, all_features]
y_train, y_valid = y_train_site.iloc[train_index], y_train_site.iloc[valid_index]
dtrain = lgb.Dataset(X_train, label=y_train, categorical_feature=categorical_features)
 dvalid = lgb.Dataset(X_valid, label=y_valid, categorical_feature=categorical_features)
 watchlist = [dtrain, dvalid]
params = {"objective": "regression",
"num_leaves": 41,
"learning_rate": 0.049,
"bagging_freq": 5,
"bagging_fraction": 0.51,
"feature_fraction": 0.81,
"metric": "rmse"
}
model_lgb = lgb.train(params, train_set=dtrain, num_boost_round=999, valid_sets=watchlist, verbose_eval=101, early_stopping_rounds=21)
 models[site_id].append(model_lgb)
 y_pred_valid = model_lgb.predict(X_valid, num_iteration=model_lgb.best_iteration)
 y_pred_train_site[valid_index] = y_pred_valid
rmse = np.sqrt(mean_squared_error(y_valid, y_pred_valid))
print("Site Id:", site_id, ", Fold:", fold+1, ", RMSE:", rmse)
 score += rmse / cv
gc.collect()
cv_scores["site_id"].append(site_id)
 cv_scores["cv_score"].append(score)
 print("
Site Id:", site_id, ", CV RMSE:", np.sqrt(mean_squared_error(y_train_site, y_pred_train_site)) , "
" )<create_dataframe>
NCLASSES = len(train_df['Id'].unique())
 print(NCLASSES )
pd.DataFrame.from_dict(cv_scores )<drop_column>
def prepare_labels(y):
values = np.array(y)
 label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)
 onehot_encoder = OneHotEncoder(sparse=False)
 integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
 onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
 y = onehot_encoded
return y, label_encoder
del df_train, X_train_site, y_train_site, X_train, y_train, dtrain, X_valid, y_valid, dvalid, y_pred_train_site, y_pred_valid, rmse, score, cv_scores
gc.collect()<drop_column>
y, label_encoder = prepare_labels(train_df['Id'] )
weather_test = create_lag_features(weather_test, 18)
 weather_test.drop(["air_temperature", "cloud_coverage", "dew_temperature", "precip_depth_1_hr"], axis=1, inplace=True )<merge>
trn_trnsfms = transforms.Compose([
transforms.Resize(( 224, 224)) ,
transforms.RandomHorizontalFlip() ,
transforms.RandomVerticalFlip() ,
transforms.RandomAffine(degrees=30),
transforms.ToTensor() ,
transforms.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225] 
)
 ])
 tst_trnsfms = transforms.Compose([
transforms.Resize(( 224, 224)) ,
transforms.ToTensor() ,
transforms.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225] 
)
 ] )
df_test_sites = []
for site_id in tqdm(range(16), desc="site_id"):
print("Preparing test data for site_id", site_id)
 X_test_site = df_test[df_test.site_id==site_id]
weather_test_site = weather_test[weather_test.site_id==site_id]
X_test_site = X_test_site.merge(weather_test_site, on=["site_id", "timestamp"], how="left")
 row_ids_site = X_test_site.row_id
X_test_site = X_test_site[all_features]
y_pred_test_site = np.zeros(X_test_site.shape[0])
 print("Scoring for site_id", site_id)
 for fold in range(cv):
model_lgb = models[site_id][fold]
y_pred_test_site += model_lgb.predict(X_test_site, num_iteration=model_lgb.best_iteration)/ cv
gc.collect()
df_test_site = pd.DataFrame({"row_id": row_ids_site, "meter_reading": y_pred_test_site})
 df_test_sites.append(df_test_site)
 print("Scoring for site_id", site_id, "completed
")
 gc.collect()<save_to_csv>
class WhaleDataLoader(Dataset):
def __init__(self, image_folder, process='train', df=None, transform=None, y=None):
self.image_folder = image_folder
self.imgs_list = [img for img in os.listdir(image_folder)]
self.process = process
self.transform = transform
self.y = y
if self.process == 'train':
self.df = df.values
def __len__(self):
return len(self.imgs_list)
 def __getitem__(self, idx):
if self.process == 'train':
img_name = os.path.join(self.image_folder, self.df[idx][0])
 label = self.y[idx]
elif self.process == 'test':
img_name = os.path.join(self.image_folder, self.imgs_list[idx])
 label = np.zeros(( NCLASSES,))
img = Image.open(img_name ).convert('RGB')
 img = self.transform(img)
 if self.process == 'train':
return img, label
elif self.process == 'test':
return img, label, self.imgs_list[idx]
submit = pd.concat(df_test_sites)
 submit.meter_reading = np.clip(np.expm1(submit.meter_reading), 0, a_max=None)
 submit.to_csv("submission_noleak.csv", index=False )<load_from_csv>
train_dataloader = WhaleDataLoader(image_folder = TRN_IMGS_DIR, process='train', df=train_df, transform=trn_trnsfms, y=y)
 test_dataloader = WhaleDataLoader(image_folder = TST_IMGS_DIR, process='test', transform=tst_trnsfms )
leak0 = pd.read_csv("/kaggle/input/ashrae-leak-data/site0.csv")
 leak1 = pd.read_csv("/kaggle/input/ashrae-leak-data/site1.csv")
 leak2 = pd.read_csv("/kaggle/input/ashrae-leak-data/site2.csv")
 leak4 = pd.read_csv("/kaggle/input/ashrae-leak-data/site4.csv")
 leak15 = pd.read_csv("/kaggle/input/ashrae-leak-data/site15.csv")
 leak = pd.concat([leak0, leak1, leak2, leak4, leak15])
 del leak0, leak1, leak2, leak4, leak15, df_test_sites
gc.collect()
test = pd.read_csv(path_test)
 test = test[test.building_id.isin(leak.building_id.unique())]
leak = leak.merge(test, on=["building_id", "meter", "timestamp"])
 del test
gc.collect()
submit = submit.merge(leak[["row_id", "meter_reading_scraped"]], on=["row_id"], how="left")
 submit.loc[submit.meter_reading_scraped.notnull() , "meter_reading"] = submit.loc[submit.meter_reading_scraped.notnull() , "meter_reading_scraped"]
submit.drop(["meter_reading_scraped"], axis=1, inplace=True)
 submit.to_csv("submission.csv", index=False )<set_options>
batch_size = 10
num_workers = 4
train_loader = DataLoader(train_dataloader, batch_size=batch_size, num_workers=num_workers, pin_memory=True, shuffle=True)
 test_loader = DataLoader(test_dataloader, batch_size=batch_size, num_workers=num_workers, pin_memory=True )
%matplotlib inline
warnings.filterwarnings("ignore" )<define_variables>

 pretrained_settings = {
'se_resnext101_32x4d': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnext101_32x4d-3b2fe3d8.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
}
class SEModule(nn.Module):
def __init__(self, channels, reduction):
super(SEModule, self ).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
 self.fc1 = nn.Conv2d(channels, channels // reduction, kernel_size=1,
padding=0)
 self.relu = nn.ReLU(inplace=True)
 self.fc2 = nn.Conv2d(channels // reduction, channels, kernel_size=1,
padding=0)
 self.sigmoid = nn.Sigmoid()
def forward(self, x):
module_input = x
x = self.avg_pool(x)
 x = self.fc1(x)
 x = self.relu(x)
 x = self.fc2(x)
 x = self.sigmoid(x)
 return module_input * x
class Bottleneck(nn.Module):

 def forward(self, x):
residual = x
out = self.conv1(x)
 out = self.bn1(out)
 out = self.relu(out)
 out = self.conv2(out)
 out = self.bn2(out)
 out = self.relu(out)
 out = self.conv3(out)
 out = self.bn3(out)
 if self.downsample is not None:
residual = self.downsample(x)
 out = self.se_module(out)+ residual
out = self.relu(out)
 return out
class SEBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None):
super(SEBottleneck, self ).__init__()
self.conv1 = nn.Conv2d(inplanes, planes * 2, kernel_size=1, bias=False)
 self.bn1 = nn.BatchNorm2d(planes * 2)
 self.conv2 = nn.Conv2d(planes * 2, planes * 4, kernel_size=3,
stride=stride, padding=1, groups=groups,
bias=False)
 self.bn2 = nn.BatchNorm2d(planes * 4)
 self.conv3 = nn.Conv2d(planes * 4, planes * 4, kernel_size=1,
bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SEResNetBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None):
super(SEResNetBottleneck, self ).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False,
stride=stride)
 self.bn1 = nn.BatchNorm2d(planes)
 self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1,
groups=groups, bias=False)
 self.bn2 = nn.BatchNorm2d(planes)
 self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SEResNeXtBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None, base_width=4):
super(SEResNeXtBottleneck, self ).__init__()
width = math.floor(planes *(base_width / 64)) * groups
self.conv1 = nn.Conv2d(inplanes, width, kernel_size=1, bias=False,
stride=1)
 self.bn1 = nn.BatchNorm2d(width)
 self.conv2 = nn.Conv2d(width, width, kernel_size=3, stride=stride,
padding=1, groups=groups, bias=False)
 self.bn2 = nn.BatchNorm2d(width)
 self.conv3 = nn.Conv2d(width, planes * 4, kernel_size=1, bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SENet(nn.Module):
def __init__(self, block, layers, groups, reduction, dropout_p=0.2,
inplanes=128, input_3x3=True, downsample_kernel_size=3,
downsample_padding=1, num_classes=1000):

 super(SENet, self ).__init__()
self.inplanes = inplanes
if input_3x3:
layer0_modules = [ 
('conv1', nn.Conv2d(3, 64, 3, stride=2, padding=1,
bias=False)) , 
('bn1', nn.BatchNorm2d(64)) , 
('relu1', nn.ReLU(inplace=True)) , 
('conv2', nn.Conv2d(64, 64, 3, stride=1, padding=1,
bias=False)) , 
('bn2', nn.BatchNorm2d(64)) , 
('relu2', nn.ReLU(inplace=True)) , 
('conv3', nn.Conv2d(64, inplanes, 3, stride=1, padding=1,
bias=False)) , 
('bn3', nn.BatchNorm2d(inplanes)) , 
('relu3', nn.ReLU(inplace=True)) ,
]
else:
layer0_modules = [ 
('conv1', nn.Conv2d(3, inplanes, kernel_size=7, stride=2,
padding=3, bias=False)) , 
('bn1', nn.BatchNorm2d(inplanes)) , 
('relu1', nn.ReLU(inplace=True)) ,
]
layer0_modules.append(( 'pool', nn.MaxPool2d(3, stride=2,
ceil_mode=True)))
 self.layer0 = nn.Sequential(OrderedDict(layer0_modules))
self.layer1 = self._make_layer(
 block,
planes=64,
blocks=layers[0],
groups=groups,
reduction=reduction,
downsample_kernel_size=1,
downsample_padding=0 
)
 self.layer2 = self._make_layer(
 block,
planes=128,
blocks=layers[1],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.layer3 = self._make_layer(
 block,
planes=256,
blocks=layers[2],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.layer4 = self._make_layer(
 block,
planes=512,
blocks=layers[3],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.avg_pool = nn.AvgPool2d(7, stride=1)
 self.dropout = nn.Dropout(dropout_p)if dropout_p is not None else None
self.last_linear = nn.Linear(512 * block.expansion, num_classes)
 def _make_layer(self, block, planes, blocks, groups, reduction, stride=1,
downsample_kernel_size=1, downsample_padding=0):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
 nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=downsample_kernel_size, stride=stride,
padding=downsample_padding, bias=False),
nn.BatchNorm2d(planes * block.expansion), 
)
 layers = []
layers.append(block(self.inplanes, planes, groups, reduction, stride,
downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes, groups, reduction))
return nn.Sequential(*layers)
 def features(self, x):
x = self.layer0(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 return x
def logits(self, x):
x = self.avg_pool(x)
 if self.dropout is not None:
x = self.dropout(x)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 return x
def forward(self, x):
x = self.features(x)
 x = self.logits(x)
 return x
def initialize_pretrained_model(model, num_classes, settings):
assert num_classes == settings['num_classes'], \
'num_classes should be {}, but is {}'.format(
 settings['num_classes'], num_classes)
 model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
def se_resnext101(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNeXtBottleneck, [3, 4, 23, 3], groups=32, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnext101_32x4d'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 frame_h = 5
frame_l = 5
len(test_videos )<import_modules>
model = se_resnext101()
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )<set_options>
model.last_linear = nn.Linear(model.last_linear.in_features, NCLASSES )
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu<load_pretrained>
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters() , lr=0.0005)
 scheduler = lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1 )
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )<load_pretrained>
n_epochs = 13
for epoch in range(1, n_epochs+1):
train_loss = []
for batch_i,(data, target)in tqdm(enumerate(train_loader), total = len(train_loader)) :
data, target = data.cuda() , target.cuda()
optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target.float())
 train_loss.append(loss.item())
 loss.backward()
optimizer.step()
scheduler.step()
print(f'Epoch {epoch}, train loss: {np.mean(train_loss):.4f}' )
<define_variables><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 model.eval()
for(data, target, name)in tqdm(test_loader):
data = data.cuda()
output = model(data)
 output = output.cpu().detach().numpy()
for i,(e, n)in enumerate(list(zip(output, name))):
sub.loc[sub['Image'] == n, 'Id'] = ' '.join(label_encoder.inverse_transform(e.argsort() [-5:][::-1]))
sub.to_csv('submission.csv', index=False)