Kaggle competition: Humpback Whale Identification
<SOS> metric: MAP@{K} Kaggle data source: humpback-whale-identification<set_options>
train_on_gpu = True
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )<feature_engineering>
model_name = "vgg"
num_classes = 5005
feature_extract = False
sys.path.insert(0, "/kaggle/input/xxxxxx/pytorchretinaface")
 sys.path.insert(0, "/kaggle/input/zzzzzz/mymodel" )<normalization>
print(os.listdir(".. /input"))
!ls ".. /input/humpback-whale-identification"
!ls ".. /input/train-val"
path_train = ".. /input/train-val/train.csv"
path_val = ".. /input/train-val/val.csv"
df_train = pd.read_csv(".. /input/humpback-whale-identification/train.csv")
 df_val = pd.read_csv(".. /input/train-val/val.csv")
 print(len(df_train))
print(len(df_val))
print(df_train.head())
 print(df_val.head() )
arcface_src = np.array([
[122.5, 141.25],
[197.5, 141.25],
[160., 178.75],
[137.5, 225.25],
[182.5, 225.25] ], dtype=np.float32)
 arcface_src = np.expand_dims(arcface_src, axis=0)
 def estimate_norm(lmk, image_size = 112, mode='arcface'):
assert lmk.shape==(5,2)
 tform = trans.SimilarityTransform()
lmk_tran = np.insert(lmk, 2, values=np.ones(5), axis=1)
 min_M = []
min_index = []
min_error = float('inf')
 if mode=='arcface':
src = arcface_src
else:
src = src_map[image_size]
for i in np.arange(src.shape[0]):
tform.estimate(lmk, src[i])
 M = tform.params[0:2,:]
results = np.dot(M, lmk_tran.T)
 results = results.T
error = np.sum(np.sqrt(np.sum(( results - src[i])** 2,axis=1)))
 if error< min_error:
min_error = error
min_M = M
min_index = i
return min_M, min_index
def norm_crop(img, landmark, image_size=112, mode='arcface'):
M, pose_index = estimate_norm(landmark, image_size, mode)
 warped = cv2.warpAffine(img,M,(image_size, image_size), borderValue = 0.0)
 return warped
def check_keys(model, pretrained_state_dict):
ckpt_keys = set(pretrained_state_dict.keys())
 model_keys = set(model.state_dict().keys())
 used_pretrained_keys = model_keys & ckpt_keys
unused_pretrained_keys = ckpt_keys - model_keys
missing_keys = model_keys - ckpt_keys
print('Missing keys:{}'.format(len(missing_keys)))
 print('Unused checkpoint keys:{}'.format(len(unused_pretrained_keys)))
 print('Used keys:{}'.format(len(used_pretrained_keys)))
 assert len(used_pretrained_keys)> 0, 'load NONE from pretrained checkpoint'
return True
def remove_prefix(state_dict, prefix):

 f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix)else x
return {f(key): value for key, value in state_dict.items() }
def load_model(model, pretrained_path, load_to_cpu):
print('Loading pretrained model from {}'.format(pretrained_path))
if load_to_cpu:
pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage)
 else:
device = torch.cuda.current_device()
pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage.cuda(device))
if "state_dict" in pretrained_dict.keys() :
pretrained_dict = remove_prefix(pretrained_dict['state_dict'], 'module.')
 else:
pretrained_dict = remove_prefix(pretrained_dict, 'module.')
 check_keys(model, pretrained_dict)
 model.load_state_dict(pretrained_dict, strict=False)
 return model
torch.set_grad_enabled(False)
 cfg = cfg_re50
cfg['pretrain']=False
!cp /kaggle/input/xxxxxx/pytorchretinaface/weights./ -rf
net = RetinaFace(cfg=cfg, phase = 'test')
 net = load_model(net, './weights/Resnet50_Final.pth', False)
 net.eval()
print('Finished loading model!')
 cudnn.benchmark = True
device = torch.device("cuda")
 net = net.to(device )<save_to_csv>
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
y_train, le_train = prepare_labels(df_train['Id'])
 y_train = y_train[range(20286)]
y_val = y_train[range(5075)]
print(( y_train ).shape)
 print(y_val.shape )
modelx=WSDAN(num_classes=2, M=8, net='xception', pretrained=False ).cuda()
modelx.load_state_dict(torch.load('/kaggle/input/zzzzzz/mymodel/ckpt_x.pth')['state_dict'])
 modely=WSDAN(num_classes=2, M=8, net='efficientnet', pretrained=False ).cuda()
modely.load_state_dict(torch.load('/kaggle/input/zzzzzz/mymodel/ckpt_e.pth')['state_dict'])
 modelx.eval()
modely.eval()
predictions = predict_on_video_set(test_videos,modelx,modely)
 submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )<install_modules>
class Whaledataset(Dataset):
def __init__(self, datafolder, datatype, transform, y, df):
self.datafolder = datafolder
self.datatype = datatype
self.transform = transform
self.y = y
if datatype =="train":
self.df = df.values
if datatype =="val":
self.df = df.values
self.image_list = [s for s in os.listdir(datafolder)]
def __len__(self):
if self.datatype == "train":
return 20286
elif self.datatype == "val":
return 5075
elif self.datatype == "test":
return len(os.listdir(self.datafolder))
def __getitem__(self, idx):
if self.datatype =='train' or self.datatype =="val":
img_name = os.path.join(self.datafolder, self.df[idx][0])
 label = self.y[idx]
elif self.datatype == 'test':
img_name = os.path.join(self.datafolder, self.image_list[idx])
 label = np.zeros(( 5005,))
image = Image.open(img_name ).convert('RGB')
 image = self.transform(image)
 if self.datatype == 'train' or self.datatype == "val":
return image, label
elif self.datatype == 'test':
return image, label, self.image_list[idx]
%%capture
!pip install --no-deps.. /input/maruti/maruti-1.3.1-py3-none-any.whl
!pip install /kaggle/input/facenetmtcnn/facenet_pytorch-2.1.1-py3-none-any.whl<import_modules>
def set_parameter_requires_grad(model, feature_extracting):
if feature_extracting:
for param in model.parameters() :
param.requires_grad = False
def initialize_model(model_name, num_classes, feature_extract, use_pretrained=True):
model_ft = None
input_size = 0
if model_name == "resnet":

 model_ft = models.resnet34(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, num_classes)
 input_size = 224
elif model_name == "alexnet":

 model_ft = models.alexnet(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 num_ftrs = model_ft.classifier[6].in_features
model_ft.classifier[6] = nn.Linear(num_ftrs,num_classes)
 input_size = 224
elif model_name == "vgg":

 model_ft = models.vgg11_bn(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 num_ftrs = model_ft.classifier[6].in_features
model_ft.classifier[6] = nn.Linear(num_ftrs,num_classes)
 input_size = 224
elif model_name == "squeezenet":

 model_ft = models.squeezenet1_0(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 model_ft.classifier[1] = nn.Conv2d(512, num_classes, kernel_size=(1,1), stride=(1,1))
model_ft.num_classes = num_classes
input_size = 224
elif model_name == "densenet":

 model_ft = models.densenet121(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 num_ftrs = model_ft.classifier.in_features
model_ft.classifier = nn.Linear(num_ftrs, num_classes)
 input_size = 224
elif model_name == "inception":

 model_ft = models.inception_v3(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 num_ftrs = model_ft.AuxLogits.fc.in_features
model_ft.AuxLogits.fc = nn.Linear(num_ftrs, num_classes)
 num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs,num_classes)
 input_size = 299
else:
print("Invalid model name, exiting...")
 exit()
return model_ft, input_size
model_ft, input_size = initialize_model(model_name, num_classes, feature_extract, use_pretrained=True)
 print(model_ft)
import os
import torch
from facenet_pytorch import MTCNN
import pandas as pd
import cv2
from tqdm import tqdm
from PIL import Image
import numpy as np
import math
from torchvision import transforms
import torchvision.models as models
from maruti.imports.ml import *
from torch.nn.utils.rnn import pack_sequence
import keras
import tensorflow<define_variables>
train_transforms = transforms.Compose([
transforms.Resize(( 224,224)) ,
transforms.RandomHorizontalFlip(0.5),
transforms.RandomAffine(30),
transforms.ToTensor() ,
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
 ])
 val_transforms = transforms.Compose([
transforms.Resize(( 224,224)) ,
transforms.RandomHorizontalFlip(0.5),
transforms.RandomAffine(30),
transforms.ToTensor() ,
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
 ])
 test_transforms = transforms.Compose([
transforms.Resize(( 224,224)) ,
transforms.ToTensor() ,
transforms.Normalize(mean = [0.485, 0.456, 0.406],
std = [0.229, 0.224, 0.225])
 ] )
test_dir = '.. /input/deepfake-detection-challenge/test_videos/'
test_videos = sorted(os.listdir(test_dir))<load_pretrained>
def accuracy(outputs,labels):
preds = outputs
preds_idx = torch.argmax(preds, dim=1)
 labels_idx = torch.argmax(labels, dim=1)
 return(preds_idx==labels_idx ).float().sum()
train_datafolder = ".. /input/humpback-whale-identification/train"
train_datatype = "train"
train_df = df_train
train_transform = train_transforms
train_y = y_train
batch_size = 32
num_workers = 0
val_datafolder = ".. /input/humpback-whale-identification/train"
val_datatype = "val"
val_df = df_val
val_transform = val_transforms
val_y = y_val
batch_size = 32
num_workers = 0
def train(batch_size = 32, epochs = 10, transform = None):
tfmd_dataset = Whaledataset(datafolder = train_datafolder, datatype = train_datatype, df = train_df ,
transform = train_transform, y = train_y)
 dataloader = torch.utils.data.DataLoader(tfmd_dataset, batch_size = batch_size, shuffle=True,
num_workers=num_workers)
 val_dataset = Whaledataset(datafolder = val_datafolder, datatype = val_datatype, df = val_df ,
transform = val_transform, y = val_y)
 val_dataloader = torch.utils.data.DataLoader(val_dataset, batch_size = batch_size, shuffle=True,
num_workers=num_workers)
 criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model_ft.parameters() , lr=0.0001)
 if torch.cuda.is_available() :
model_ft.cuda()
nn.DataParallel(model_ft)
 print("GPU")
 epoch_loss_data = []
epoch_accuracy_data = []
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device)
 for epoch in range(1,epochs+1):
i = 0.0
running_loss, running_loss_total, epoch_accuracy = 0.0, 0.0, 0.0
val_running_loss, val_running_loss_total, val_epoch_accuracy = 0.0, 0.0, 0.0
for i, data in enumerate(dataloader, 0):
inputs, labels = data
labels = labels.double()
inputs, labels = inputs.to(device), labels.to(device)
 optimizer.zero_grad()
outputs = model_ft(inputs ).double()
loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
epoch_accuracy += accuracy(outputs,labels)
 running_loss += loss.item()


epoch_accuracy = float(epoch_accuracy)/float(32 *(i+1))
print('[%d, %5d] loss: %.3f, epoch_accuracy: %f' %(epoch, i + 1, running_loss, epoch_accuracy))
running_loss = 0.0
epoch_accuracy = 0.0
for i, data in enumerate(val_dataloader,0):
inputs, labels = data
labels = labels.double()
inputs, labels = inputs.to(device), labels.to(device)
 outputs = model_ft(inputs ).double()
loss = criterion(outputs, labels)
 val_epoch_accuracy += accuracy(outputs,labels)
 val_running_epoch_acc = float(val_epoch_accuracy)/float(( labels.size() [0] *(i+1)))
 val_running_loss += loss.item()

 val_epoch_accuracy = float(val_epoch_accuracy)/float(32 *(i+1))
print('[%d, %5d] val loss: %.3f, val_epoch_accuracy: %f' %(epoch, i + 1, val_running_loss, val_epoch_accuracy))
val_running_loss = 0.0
val_epoch_accuracy = 0.0
with tensorflow.device('/gpu:0'):
model = tensorflow.keras.models.load_model('/kaggle/input/pytorch-keras-models/Resnext101.h5' )<set_options>
train(transform=train_transforms,epochs=25,batch_size=32 )
device = torch.device("cuda" )<categorify>
test_datafolder = ".. /input/humpback-whale-identification/test"
test_datatype = "test"
test_transform = test_transforms
batch_size = 32
num_workers = 0
sub = pd.read_csv(".. /input/humpback-whale-identification/sample_submission.csv")
 def test(batch_size, transform, model):
test_set = Whaledataset(datafolder = test_datafolder, datatype = test_datatype, transform = test_transform, y = y_train, df = df_train)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size=32, num_workers=num_workers)
 model.cuda()
model.eval()
for(data, target, name)in test_loader:
data = data.cuda()
output = model(data)
 print(output)
 output = output.cpu().detach().numpy()
for i,(e, n)in enumerate(list(zip(output, name))):
sub.loc[sub['Image'] == n, 'Id'] = ' '.join(le_train.inverse_transform(e.argsort() [-5:][::-1]))
sub.to_csv('basic_model.csv', index=False )
<statistical_test><EOS> 
test(batch_size, test_transform, model_ft )