Kaggle competition: Humpback Whale Identification
<SOS> metric: MAP@{K} Kaggle data source: humpback-whale-identification<import_modules>
!pip install albumentations > /dev/null
class VideoReader:

 def __init__(self, verbose=True):

 self.verbose = verbose
def read_frames_at_indices(self, path, frame_idxs):

 assert len(frame_idxs)> 0
capture = cv2.VideoCapture(path)
 result = self._read_frames_at_indices(path, capture, frame_idxs)
 capture.release()
return result
def _read_frames_at_indices(self, path, capture, frame_idxs):
try:
frames = []
idxs_read = []
for frame_idx in range(frame_idxs[0], frame_idxs[-1] + 1):
ret = capture.grab()
if not ret:
if self.verbose:
print("Error grabbing frame %d from movie %s" %(frame_idx, path))
break
current = len(idxs_read)
 if frame_idx == frame_idxs[current]:
ret, frame = capture.retrieve()
if not ret or frame is None:
if self.verbose:
print("Error retrieving frame %d from movie %s" %(frame_idx, path))
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 frames.append(frame)
 idxs_read.append(frame_idx)
 if len(frames)> 0:
return np.stack(frames), idxs_read
if self.verbose:
print("No frames read from movie %s" % path)
 return None
except:
if self.verbose:
print("Exception while reading movie %s" % path)
 return None
video_reader = VideoReader()<define_variables>
%matplotlib inline
warnings.simplefilter("ignore", category=DeprecationWarning )
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)] )<define_variables>
BBOX_TRAIN = '.. /input/bboxsplit/bounding_boxes_train.csv'
BBOX_TEST = '.. /input/bboxsplit/bounding_boxes_test.csv'
train_df = pd.read_csv(".. /input/humpback-whale-identification/train.csv")
 train_df.head()
weights = ["efnet_6_2020-03-26_14-08-45_epoch_06",
"efnet_6_2020-03-27_21-28-54_epoch_06",
"efnet_6_2020-03-29_21-28-21_epoch_06",
"efnet_6_2020-03-29_21-29-50_epoch_06",
"efnet_6_2020-03-30_00-26-00_epoch_06"
]<choose_model_class>
def efnet(backbone='6', input_shape=(224,224,3), num_classes=1):
DENSE_KERNEL_INITIALIZER = {
'class_name': 'VarianceScaling',
'config': {
'scale': 1./ 3.,
'mode': 'fan_out',
'distribution': 'uniform'
}
}
if backbone == '0':
EFNet = efn.EfficientNetB0
elif backbone == '1':
EFNet = efn.EfficientNetB1
elif backbone == '2':
EFNet = efn.EfficientNetB2
elif backbone == '3':
EFNet = efn.EfficientNetB3
elif backbone == '4':
EFNet = efn.EfficientNetB4
elif backbone == '5':
EFNet = efn.EfficientNetB5
elif backbone == '6':
EFNet = efn.EfficientNetB6
elif backbone == '7':
EFNet = efn.EfficientNetB7
base_model = EFNet(input_shape=input_shape, weights=None, include_top=False, classes=num_classes)
 x = layers.GlobalAveragePooling2D()(base_model.output)
 x = layers.Dropout(0.2 )(x)
 output = layers.Dense(num_classes, activation='sigmoid', kernel_initializer=DENSE_KERNEL_INITIALIZER )(x)
 return Model(inputs=[base_model.input], outputs=[output] )<choose_model_class>
for i in range(4):
newdf = train_df.groupby("Id" ).filter(lambda x: len(x)== i+1)
 train_df = train_df.append([newdf]*(4-i), ignore_index=True )
models = []
for i in range(len(weights)) :
models.append(efnet() )<load_pretrained>
train_df.shape, train_df.Id.nunique()
for model, weight in zip(models, weights):
model.load_weights('/kaggle/input/efnet6weights/' + weight + '.hdf5' )<define_variables>
NUM_CLASSES = train_df.Id.nunique()
faces_batch_size = 55
frames_per_video = 55
face_size = 224
clip = 1e-15
ws = [1/(len(models)) ]*len(models )<save_to_csv>
RESIZE_H = 299
RESIZE_W = 299
data_transforms = albumentations.Compose([
albumentations.Resize(RESIZE_H, RESIZE_W),
albumentations.HorizontalFlip() ,
albumentations.OneOf([
albumentations.RandomContrast() ,
albumentations.RandomBrightness() ,
]),
albumentations.ShiftScaleRotate(rotate_limit=10, scale_limit=0.15),
albumentations.JpegCompression(80),
albumentations.HueSaturationValue() ,
AT.ToTensor()
])
 data_transforms_test = albumentations.Compose([
albumentations.Resize(RESIZE_H, RESIZE_W),
AT.ToTensor()
] )
submission_df = pd.DataFrame({"filename": test_videos, "label": out_probs})
 submission_df.to_csv("submission.csv", index=False )<install_modules>
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
!pip install.. /input/efficientnet/efficientnet-1.0.0-py3-none-any.whl<load_from_csv>
y, lab_encoder = prepare_labels(train_df['Id'] )
sample_submission = pd.read_csv(".. /input/deepfake-detection-challenge/sample_submission.csv")
 detection_graph = tf.Graph()
detection_graph = tf.Graph()
with detection_graph.as_default() :
od_graph_def = tf.compat.v1.GraphDef()
with tf.io.gfile.GFile('.. /input/mobilenet-face/frozen_inference_graph_face.pb', 'rb')as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
 tf.import_graph_def(od_graph_def, name='')
 config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess=tf.compat.v1.Session(graph=detection_graph, config=config)
 image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
 boxes_tensor = detection_graph.get_tensor_by_name('detection_boxes:0')
 scores_tensor = detection_graph.get_tensor_by_name('detection_scores:0')
 num_detections = detection_graph.get_tensor_by_name('num_detections:0')
 def get_img(images):
global boxes,scores,num_detections
im_heights,im_widths=[],[]
imgs=[]
for image in images: 
(im_height,im_width)=image.shape[:-1]
imgs.append(image)
 im_heights.append(im_height)
 im_widths.append(im_widths)
 imgs=np.array(imgs)
(boxes, scores_)= sess.run(
 [boxes_tensor, scores_tensor],
feed_dict={image_tensor: imgs})
 finals=[]
for x in range(boxes.shape[0]):
scores=scores_[x]
max_=np.where(scores==scores.max())[0][0]
box=boxes[x][max_]
ymin, xmin, ymax, xmax = box 
(left, right, top, bottom)=(xmin * im_width, xmax * im_width,
ymin * im_height, ymax * im_height)
 left, right, top, bottom = int(left), int(right), int(top), int(bottom)
 finals.append([left, right, top, bottom])
 return finals
def resize_imgs(bbox,imgs,resize=240,pad=40):
finals=[]
for image,(left, right, top, bottom)in zip(imgs,bbox):
finals.append(cv2.cvtColor(cv2.resize(image[max([0,top-pad]):bottom+(pad*2),max([0,left-pad]):right+(pad*2)],(resize,resize)) ,cv2.COLOR_BGR2RGB))
return np.hstack(finals)
 def detect_video(video,frame_count=10):
capture = cv2.VideoCapture(video)
 v_len = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
frame_idxs = np.linspace(0,v_len,frame_count, endpoint=False, dtype=np.int)
 imgs=[]
i=0
for frame_idx in range(int(v_len)) :
ret = capture.grab()
if not ret:
print("Error grabbing frame %d from movie %s" %(frame_idx, video))
if frame_idx >= frame_idxs[i]:
if frame_idx-frame_idxs[i]>20:
return None
ret, frame = capture.retrieve()
if not ret or frame is None:
print("Error retrieving frame %d from movie %s" %(frame_idx, video))
else:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 imgs.append(frame)
 i += 1
if i >= len(frame_idxs):
break
bbox=get_img(imgs)
 if len(bbox)<10:
return None
return imgs,bbox<define_search_space>
class WhaleDataset(Dataset):
def __init__(self, datafolder, datatype='train', df=None, df_bbox = None, transform=None, y=None):
self.datafolder = datafolder
self.datatype = datatype
self.y = y
if self.datatype == 'train':
self.df = df.values
self.df_bbox = df_bbox
if self.datatype == 'train':
self.image_files_list = list(df.Image)
 else:
self.image_files_list = [s for s in os.listdir(datafolder)]
self.transform = transform
def __len__(self):
return len(self.image_files_list)
 def __getitem__(self, idx):
if self.datatype == 'train':
img_name = os.path.join(self.datafolder, self.df[idx][0])
 bbox = self.df_bbox.loc[self.df[idx][0]]
x0, y0, x1, y1 = int(bbox['x0']), int(bbox['y0']), int(bbox['x1']), int(bbox['y1'])
 label = self.y[idx]
elif self.datatype == 'test':
img_name = os.path.join(self.datafolder, self.image_files_list[idx])
 bbox = self.df_bbox.loc[self.image_files_list[idx]]
x0, y0, x1, y1 = int(bbox['x0']), int(bbox['y0']), int(bbox['x1']), int(bbox['y1'])
 label = np.zeros(( NUM_CLASSES,))
img = cv2.imread(img_name)
 img = img[y0:y1, x0:x1]
image = self.transform(image = img)['image']
if self.datatype == 'train':
return image, label
elif self.datatype == 'test':
return image, label, self.image_files_list[idx]
val_transform = Compose([
Resize(240, 240),
Normalize(mean= (.5,.5,.5), std= (.5,.5,.5)) ,
ToTensorV2() ,
] )<find_best_params>
bbox_df_train = pd.read_csv(BBOX_TRAIN ).set_index('Image')
 bbox_df_test = pd.read_csv(BBOX_TEST ).set_index('Image' )
model1=torch.jit.load('.. /input/dfdc-ensemble-final-2/dfdc_mixl_0.pt',map_location='cpu' ).cuda()
_=model1.eval()
model2=torch.jit.load('.. /input/dfdc-ensemble-final-2/dfdc_mixxl_0.pt',map_location='cpu' ).cuda()
_=model2.eval()
model3=torch.jit.load('.. /input/dfdc-ensemble-final-2/dfdc_se101_2.pt',map_location='cpu' ).cuda()
_=model3.eval()<choose_model_class>
bbox = bbox_df_train.loc['0000e88ab.jpg']
bottleneck = efn.EfficientNetB6(weights=None,include_top=False,pooling='avg')
 inp=Input(( 240,240,3))
x=bottleneck(inp)
 x=Dense(256 )(x)
 x=Dense(128, activation='relu' )(x)
 x=Dense(1,activation='sigmoid' )(x)
 model_b6=Model(inp,x)
 model_b6.load_weights('.. /input/dfdc-ensemble-final-2/nocrop_effb6_dense256_dense128_e5_wrong_activation_weights.h5' )<load_pretrained>
train_dataset = WhaleDataset(
 datafolder='.. /input/humpback-whale-identification/train/',
datatype='train',
df=train_df, df_bbox = bbox_df_train,
transform=data_transforms,
y=y 
)
 test_set = WhaleDataset(
 datafolder='.. /input/humpback-whale-identification/test/',
datatype='test', df_bbox = bbox_df_test,
transform=data_transforms_test
)
model_34 = load_model(".. /input/dfdc-ensemble-final-2/model_b3_300_4.h5")
 model_35 = load_model(".. /input/dfdc-ensemble-final-2/model_35.h5")
 model_b1_34 = load_model(".. /input/dfdc-ensemble-final-2/model_b1_200_5.h5")
 def get_birghtness(img):
return img/img.max()
def process_img(img,flip=False,birghtness=True):
imgs=[]
for x in range(10):
if flip:
imgs.append(get_birghtness(cv2.flip(img[:,x*240:(x+1)*240,:],1)))
 else:
imgs.append(get_birghtness(img[:,x*240:(x+1)*240,:]))
return np.array(imgs )<compute_test_metric>
batch_size = 32
num_workers = 0
train_loader = DataLoader(train_dataset, batch_size=batch_size, num_workers=num_workers, pin_memory=True)
 test_loader = DataLoader(test_set, batch_size=batch_size, num_workers=num_workers, pin_memory=True )
np.mean([p1,p2] )<feature_engineering>
model = Xception_base(5005, pretrained = None)
print(sum(preds)/len(preds))
for x,y in zip(preds,filenames):
submission.loc[submission['filename']==y,'label']=x<save_to_csv>
def save_checkpoint(state, is_best, fpath='checkpoint_5005.pth'):
torch.save(state, fpath)
 if is_best:
torch.save(state, 'best_model.pth' )
submission.to_csv('submission.csv', index=False)
 !rm -r videos<install_modules>
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters() , lr=0.0005)
 try:
checkpoint = torch.load('.. /input/cpoint/checkpoint_5005.pth')
 model.load_state_dict(checkpoint['state_dict'])
 start = checkpoint['epoch']
print(start)
 optimizer.load_state_dict(checkpoint['optimizer'])
 except:
start = 0
scheduler = lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1 )
!pip install.. /input/efficientnet/efficientnet-1.0.0-py3-none-any.whl<import_modules>
def cuda(x):
return x.cuda(non_blocking=True)if torch.cuda.is_available() else x
def eval_(output, target, maxk):
e_batch_size = target.size(0)
 _, pred = output.topk(maxk, 1, True, True)
 correct = pred.eq(target.topk(1, 1, True, True)[1].expand_as(pred))
res = []
k = maxk
correct_k = correct[:k].view(-1 ).float().sum(0)
 total = correct_k.item()
res.append(correct_k.mul_(100.0 / batch_size))
return res, total
import pandas as pd
import tensorflow as tf
import cv2
import glob
from tqdm.notebook import tqdm
import numpy as np
import os
import efficientnet.keras as efn
from keras.layers import *
from keras import Model
import matplotlib.pyplot as plt<load_pretrained>
n_epochs = start + 6
cur_epoch = 0
sum_true_pred = 0
for epoch in range(start, n_epochs):
train_loss = []
cur_epoch = epoch
save_checkpoint({'epoch': epoch,
'state_dict': model.state_dict() ,
'optimizer' : optimizer.state_dict() ,
}, False)
 for batch_i,(data, target)in tqdm_notebook(enumerate(train_loader), total = len(train_loader)) :
data, target = cuda(data), cuda(target)
 optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target.float())
 with open('results.txt', 'a')as file:
file.write(str(loss.item())+'
')
 sum_true_pred+=(eval_(output, target, 5)) [1]
train_loss.append(loss.item())
 loss.backward()
optimizer.step()
scheduler.step()
print(f'Epoch {epoch}, train loss: {np.mean(train_loss):.4f}')
 save_checkpoint({'epoch': cur_epoch,
'state_dict': model.state_dict() ,
'optimizer' : optimizer.state_dict() ,
}, False )
<set_options><EOS> 
sub = pd.read_csv('.. /input/humpback-whale-identification/sample_submission.csv')
 model.eval()
for(data, target, name)in tqdm_notebook(test_loader):
data = cuda(data)
 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(lab_encoder.inverse_transform(e.argsort() [-5:][::-1]))
sub.to_csv('submission.csv', index=False )