Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<install_modules>
%matplotlib inline
!pip install.. /input/efficientnet-pyotrch/EfficientNet-PyTorch-master/ > /dev/null<install_modules>
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 )
!pip install.. /input/segmentation-models-pytorch/segmentation_models.pytorch-master/ > /dev/null<import_modules>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
class RANZCRDataset(torch.utils.data.Dataset):
def __init__(
 self,
df,
root,
ext,
path_col,
use_timm_aug=False,
transforms=None,
augmentations=None, 
):
super().__init__()
df = df.reset_index(drop=True ).copy()
self.transforms = transforms
self.augmentations = augmentations
self.root = root
self.use_timm_aug = use_timm_aug
self.image_names = self._prepare_image_names(df[path_col].tolist() , root, ext)
 def __len__(self):
return len(self.image_names)
 def _prepare_image_names(self, basenames: List[str], root: str, ext: str):
return [pjoin(root, el)+ ext for el in basenames]
def _prepare_img_target_from_idx(self, idx: int):
image_name = self.image_names[idx]
img = Image.open(image_name)
 if not self.use_timm_aug:
img = np.array(img)
 if self.augmentations is not None:
if self.use_timm_aug:
img = self.augmentations(img)
 else:
img = self.augmentations(image=img)["image"]
if self.use_timm_aug:
img = np.array(img)
 if self.transforms is not None:
img = self.transforms(img)
 return img
def __getitem__(self, index: int):
img = self._prepare_img_target_from_idx(index)
 return img<define_variables>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
EFFNETB6_EMB_DIM = 2304
EFFNETB5_EMB_DIM = 2048
EFFNETB4_EMB_DIM = 1792
EFFNETB3_EMB_DIM = 1536
EFFNETB1_EMB_DIM = 1280
RESNET50_EMB_DIM = 2048
REXNET200_EMB_DIM = 2560
VIT_EMB_DIM = 768
NF_RESNET50_EMB_DIM = 2048
EPS = 1e-6
class TaylorSoftmax(nn.Module):
def __init__(self, dim=1, n=2):
super(TaylorSoftmax, self ).__init__()
assert n % 2 == 0
self.dim = dim
self.n = n
def forward(self, x):
fn = torch.ones_like(x)
 denor = 1.
 for i in range(1, self.n+1):
denor *= i
fn = fn + x.pow(i)/ denor
out = fn / fn.sum(dim=self.dim, keepdims=True)
 return out
class CNNModel(nn.Module):
def __init__(
 self,
classifiier_config: Mapping[str, Any],
encoder_type: str,
device: str,
use_pretrained_encoder: bool = False,
path_to_chkp: Optional[str] = None,
use_taylorsoftmax: bool = False,
one_channel: bool = True, 
):
super().__init__()
if path_to_chkp is not None:
use_pretrained_encoder = False
if encoder_type == "rexnet_200":
self.encoder = timm.create_model(
 encoder_type, pretrained=use_pretrained_encoder 
)
 if one_channel:
self.encoder.stem.conv.in_channels = 1
weight = self.encoder.stem.conv.weight.mean(1, keepdim=True)
 self.encoder.stem.conv.weight = torch.nn.Parameter(weight)
 self.encoder.head.fc = nn.Identity()
nn_embed_size = REXNET200_EMB_DIM
elif encoder_type == "tf_efficientnet_b3_ns":
self.encoder = timm.create_model(
 encoder_type, pretrained=use_pretrained_encoder 
)
 if one_channel:
self.encoder.conv_stem.in_channels = 1
weight = self.encoder.conv_stem.weight.mean(1, keepdim=True)
 self.encoder.conv_stem.weight = torch.nn.Parameter(weight)
 self.encoder.classifier = nn.Identity()
nn_embed_size = EFFNETB3_EMB_DIM
elif encoder_type == "tf_efficientnet_b5_ns":
self.encoder = timm.create_model(
 encoder_type, pretrained=use_pretrained_encoder, num_classes=11 
)
 if path_to_chkp is not None:
print("Loading starting point")
 state_dict = load_effnet_b5_start_point(path_to_chkp)
 self.encoder.load_state_dict(state_dict)
 if one_channel:
self.encoder.conv_stem.in_channels = 1
weight = self.encoder.conv_stem.weight.mean(1, keepdim=True)
 self.encoder.conv_stem.weight = torch.nn.Parameter(weight)
 self.encoder.classifier = nn.Identity()
nn_embed_size = EFFNETB5_EMB_DIM
elif encoder_type == "resnet200d":
self.encoder = timm.create_model(
 encoder_type, pretrained=use_pretrained_encoder, num_classes=11 
)
 if path_to_chkp is not None:
print("Loading starting point")
 state_dict = load_resnet200d_start_point(path_to_chkp)
 self.encoder.load_state_dict(state_dict)
 if one_channel:
self.encoder.conv1[0].in_channels = 1
weight = self.encoder.conv1[0].weight.mean(1, keepdim=True)
 self.encoder.conv1[0].weight = torch.nn.Parameter(weight)
 self.encoder.fc = nn.Identity()
nn_embed_size = RESNET50_EMB_DIM
else:
raise ValueError(f"{encoder_type} is invalid model_type")
 classes_num = classifiier_config["classes_num"]
hidden_dims = classifiier_config["hidden_dims"]
second_dropout_rate = classifiier_config["second_dropout_rate"]
if classifiier_config["classifier_type"] == "relu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Linear(nn_embed_size, hidden_dims),
nn.ReLU() ,
nn.Dropout(p=first_dropout_rate),
nn.Linear(hidden_dims, hidden_dims),
nn.ReLU() ,
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "elu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Dropout(first_dropout_rate),
nn.Linear(nn_embed_size, hidden_dims),
nn.ELU() ,
nn.Dropout(second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "dima":
self.classifier = nn.Sequential(
 nn.BatchNorm1d(nn_embed_size),
nn.Linear(nn_embed_size, hidden_dims),
nn.BatchNorm1d(hidden_dims),
nn.PReLU(hidden_dims),
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "prelu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Dropout(first_dropout_rate),
nn.Linear(nn_embed_size, hidden_dims),
nn.PReLU(hidden_dims),
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "multiscale_relu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.big_dropout = nn.Dropout(p=0.5)
 self.classifier = nn.Sequential(
 nn.Linear(nn_embed_size, hidden_dims),
nn.ELU() ,
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "drop_linear":
self.classifier = nn.Sequential(
 nn.Dropout(p=second_dropout_rate),
nn.Linear(nn_embed_size, classes_num), 
)
 else:
raise ValueError("Invalid classifier_type")
 self.use_taylorsoftmax = use_taylorsoftmax
if self.use_taylorsoftmax:
self.taylorsoftmax = TaylorSoftmax()
self.classifier_type = classifiier_config["classifier_type"]
self.encoder_type = encoder_type
self.device = device
self.to(self.device)
 def forward(self, image):
x = self.encoder(image)
 if self.classifier_type == "multiscale_relu":
logits = torch.mean(
 torch.stack(
 [self.classifier(self.big_dropout(x)) for _ in range(5)],
dim=0, 
),
dim=0, 
)
 else:
logits = self.classifier(x)
 if self.use_taylorsoftmax:
logits = self.taylorsoftmax(logits ).log()
return logits
class CNNSegModel(nn.Module):
def __init__(
 self,
classifiier_config: Mapping[str, Any],
encoder_type: str,
encoder_config: Mapping[str, Any],
device: str,
path_to_chkp: Optional[str] = None,
use_taylorsoftmax: bool = False,
one_channel: bool = True,
enable_inference_mode: bool = False 
):
super().__init__()
if path_to_chkp is not None:
use_pretrained_encoder = False
if encoder_type == "timm-efficientnet-b5_unet":
self.encoder = smp.Unet(**encoder_config)
 if path_to_chkp is not None:
print("Loading starting point")
 state_dict = load_effnet_b5_start_point(path_to_chkp)
 self.encoder.encoder.load_state_dict(state_dict)
 if one_channel:
self.encoder.encoder.conv_stem.in_channels = 1
weight = self.encoder.encoder.conv_stem.weight.mean(1, keepdim=True)
 self.encoder.encoder.conv_stem.weight = torch.nn.Parameter(weight)
 self.encoder.classification_head[3] = nn.Identity()
nn_embed_size = 512
elif encoder_type == "densenet121_unet":
self.encoder = smp.Unet(**encoder_config)
 if path_to_chkp is not None:
print("Loading starting point")
 state_dict = load_densenet121_start_point(path_to_chkp)
 self.encoder.encoder.load_state_dict(state_dict)
 if one_channel:
self.encoder.encoder.features.conv0.in_channels = 1
weight = self.encoder.encoder.features.conv0.weight.mean(
 1, keepdim=True 
)
 self.encoder.encoder.features.conv0.weight = torch.nn.Parameter(
 weight 
)
 self.encoder.classification_head[3] = nn.Identity()
nn_embed_size = 1024
else:
raise ValueError(f"{encoder_type} is invalid model_type")
 classes_num = classifiier_config["classes_num"]
hidden_dims = classifiier_config["hidden_dims"]
second_dropout_rate = classifiier_config["second_dropout_rate"]
if classifiier_config["classifier_type"] == "relu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Linear(nn_embed_size, hidden_dims),
nn.ReLU() ,
nn.Dropout(p=first_dropout_rate),
nn.Linear(hidden_dims, hidden_dims),
nn.ReLU() ,
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "elu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Dropout(first_dropout_rate),
nn.Linear(nn_embed_size, hidden_dims),
nn.ELU() ,
nn.Dropout(second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "dima":
self.classifier = nn.Sequential(
 nn.BatchNorm1d(nn_embed_size),
nn.Linear(nn_embed_size, hidden_dims),
nn.BatchNorm1d(hidden_dims),
nn.PReLU(hidden_dims),
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "prelu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier = nn.Sequential(
 nn.Dropout(first_dropout_rate),
nn.Linear(nn_embed_size, hidden_dims),
nn.PReLU(hidden_dims),
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "multiscale_relu":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.big_dropout = nn.Dropout(p=0.5)
 self.classifier = nn.Sequential(
 nn.Linear(nn_embed_size, hidden_dims),
nn.ELU() ,
nn.Dropout(p=second_dropout_rate),
nn.Linear(hidden_dims, classes_num), 
)
 elif classifiier_config["classifier_type"] == "drop_linear":
self.classifier = nn.Sequential(
 nn.Dropout(p=second_dropout_rate),
nn.Linear(nn_embed_size, classes_num), 
)
 elif classifiier_config["classifier_type"] == "double_elu_mlp":
first_dropout_rate = classifiier_config["first_dropout_rate"]
self.classifier_prenet = nn.Sequential(
 nn.Dropout(first_dropout_rate),
nn.Linear(nn_embed_size, hidden_dims),
nn.ELU() , 
)
 self.classifier_hidden_class = nn.Sequential(
 nn.Dropout(second_dropout_rate),
nn.Linear(
 hidden_dims, classifiier_config["hidden_classes_num"] 
), 
)
 self.classifier_class_final = nn.Sequential(
 nn.Dropout(second_dropout_rate),
nn.Linear(
 hidden_dims + classifiier_config["hidden_classes_num"],
classes_num, 
), 
)
 else:
raise ValueError("Invalid classifier_type")
 self.use_taylorsoftmax = use_taylorsoftmax
if self.use_taylorsoftmax:
self.taylorsoftmax = TaylorSoftmax()
self.classifier_type = classifiier_config["classifier_type"]
self.enable_inference_mode = enable_inference_mode
self.encoder_type = encoder_type
self.device = device
self.to(self.device)
 def forward(self, image, enable_inference_mode=False):
enable_inference_mode = enable_inference_mode or self.enable_inference_mode
if enable_inference_mode:
embs = self.encoder.encoder(image)
 x = self.encoder.classification_head(embs[-1])
 else:
mask, x = self.encoder(image)
 if self.classifier_type == "multiscale_relu":
logits = torch.mean(
 torch.stack(
 [self.classifier(self.big_dropout(x)) for _ in range(5)],
dim=0, 
),
dim=0, 
)
 elif self.classifier_type == "double_elu_mlp":
pre_logits = self.classifier_prenet(x)
 class_logits = self.classifier_hidden_class(pre_logits)
 logits = self.classifier_class_final(
 torch.cat([pre_logits, class_logits], axis=-1)
)
 else:
logits = self.classifier(x)
 if self.use_taylorsoftmax:
logits = self.taylorsoftmax(logits ).log()
if enable_inference_mode:
return logits
else:
if self.classifier_type == "double_elu_mlp":
return mask, class_logits, logits
else:
return mask, logits<load_pretrained>
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 )
def get_validation_models(
 model_initilizer: Callable,
model_config: Mapping[str, Any],
model_ckp_dicts: List[OrderedDict],
device: str, 
):
t_models = []
for mcd in model_ckp_dicts:
t_model = model_initilizer(**model_config, device=device)
 t_model.load_state_dict(mcd)
 t_model = t_model.to(device)
 t_model.eval()
t_models.append(t_model)
 return t_models
def create_val_loaders(
 loader_initilizer: object,
loader_config: Mapping[str, Any],
dfs: List[str],
batch_size: int, 
):
t_loaders = []
for df in dfs:
t_dataset = loader_initilizer(df=df, **loader_config)
 t_loader = torch.utils.data.DataLoader(
 t_dataset,
batch_size=batch_size,
drop_last=False,
shuffle=False,
num_workers=os.cpu_count() // 2, 
)
 t_loaders.append(t_loader)
 return t_loaders
@torch.no_grad()
def cnn_model_predict(t_batch, t_model, t_device):
image = t_batch.to(t_device)
 logits = t_model(image)
 logits = logits.detach().cpu().numpy()
return logits
def predict_over_all_train(
 my_loaders, my_models, model_predict_func, device, do_concat=True 
):
logits = []
for loader, model in zip(my_loaders, my_models):
for batch in tqdm(loader):
logit = model_predict_func(batch, model, device)
 logits.append(logit)
 if do_concat:
logits = np.concatenate(logits)
 return logits 
<set_options>
frames_per_video = 64
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet )
%matplotlib inline<define_variables>
input_size = 224
SKIP_VAL = True<load_from_csv>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
def public_notebook() :
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 BATCH_SIZE = 64
TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
 class TestDataset(Dataset):
def __init__(self, df, transform=None):
self.df = df
self.file_names = df['StudyInstanceUID'].values
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
file_name = self.file_names[idx]
file_path = f'{TEST_PATH}/{file_name}.jpg'
image = cv2.imread(file_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform:
augmented = self.transform(image=image)
 image = augmented['image']
return image
def get_transforms(image_size=640):
return Compose([
Resize(image_size, image_size),
Normalize() ,
ToTensorV2() ,
])
 class ResNet200D(nn.Module):
def __init__(self, model_name='resnet200d'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
class SeResNet152D(nn.Module):
def __init__(self, model_name='seresnet152d'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
class RANZCRResNet200D(nn.Module):
def __init__(self, model_name='resnet200d', out_dim=11, pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, out_dim)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
def inference(models, test_loader, device):
tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for model in models:
with torch.no_grad() :
y_preds1 = model(images)
 y_preds2 = model(images.flip(-1))
y_preds =(
 y_preds1.sigmoid().to('cpu' ).numpy() +
y_preds2.sigmoid().to('cpu' ).numpy() 
)/ 2
avg_preds.append(y_preds)
 avg_preds = np.stack(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs, axis=1)
 return probs
models200D = []
model = ResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-public/resnet200d_320_CV9632.pth")['model'])
 model.eval()
model.to(device)
 models200D.append(model)
 models200D_2 = []
model = RANZCRResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-baseline-benchmark-public/resnet200d_fold0_cv953.pth", map_location='cuda:0'))
model.eval()
model.to(device)
 models200D_2.append(model)
 model = RANZCRResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-baseline-benchmark-public/resnet200d_fold1_cv955.pth", map_location='cuda:0'))
model.eval()
model.to(device)
 models200D_2.append(model)
 model = RANZCRResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-baseline-benchmark-public/resnet200d_fold2_cv955.pth", map_location='cuda:0'))
model.eval()
model.to(device)
 models200D_2.append(model)
 model = RANZCRResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-baseline-benchmark-public/resnet200d_fold3_cv957.pth", map_location='cuda:0'))
model.eval()
model.to(device)
 models200D_2.append(model)
 model = RANZCRResNet200D()
model.load_state_dict(torch.load(".. /input/resnet200d-baseline-benchmark-public/resnet200d_fold4_cv954.pth", map_location='cuda:0'))
model.eval()
model.to(device)
 models200D_2.append(model)
 model = SeResNet152D()
model.load_state_dict(torch.load('.. /input/seresnet152d-cv9615/seresnet152d_320_CV96.15.pth')['model'])
 model.eval()
model.to(device)
 models200D.append(model)
 test_dataset_512 = TestDataset(test, transform=get_transforms(image_size=512))
test_loader_512 = DataLoader(test_dataset_512, batch_size=BATCH_SIZE, shuffle=False, num_workers=os.cpu_count() // 2 , pin_memory=True)
 test_dataset_640 = TestDataset(test, transform=get_transforms(image_size=640))
test_loader_640 = DataLoader(test_dataset_640, batch_size=BATCH_SIZE, shuffle=False, num_workers=os.cpu_count() // 2 , pin_memory=True)
 predictions200d = inference(models200D, test_loader_640, device)
 predictions200d_2 = inference(models200D_2, test_loader_512, device)
 return predictions200d, predictions200d_2<load_from_csv>
class MyResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(MyResNeXt, self ).__init__(block=models.resnet.Bottleneck,
layers=[3, 4, 6, 3],
groups=32,
width_per_group=4)
 self.fc = nn.Linear(2048, 1 )
<define_variables>
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = MyResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint
RESIZE_SIZE = 640<load_from_csv>
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return y_pred[:n].mean().item()
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.481
PATH2DIR = '.. /input/ranzcr-clip-catheter-line-classification/'
os.listdir(PATH2DIR)
 train = pd.read_csv(pjoin(PATH2DIR, 'train.csv'))
sample_sub = pd.read_csv(pjoin(PATH2DIR, 'sample_submission.csv'))
split = np.load('.. /input/ranzcr-models/naive_cv_split.npy', allow_pickle=True )<define_variables>
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )
DEVICE = 'cuda'<load_pretrained>
predictions = predict_on_video_set(test_videos, num_workers=4 )
models_512 = []
ckp_names = glob('.. /input/ranzcr-models/timm_efficientnet_b5_unet_32bs_640res_lesslaugs_ls005_shedchanged_startpoint_difflrs_segbranch_125coefs_1e4noseg_bigholes_firstpseudo_swa_roc_auc_score/timm_efficientnet_b5_unet_32bs_640res_lesslaugs_ls005_shedchanged_startpoint_difflrs_segbranch_125coefs_1e4noseg_bigholes_firstpseudo_swa_roc_auc_score/*.pt')
 print(ckp_names)
 chckps = [torch.load(el, map_location='cpu')for el in ckp_names]
models_512.append(get_validation_models(
 model_initilizer=CNNSegModel,
model_config={
"classifiier_config": {
"classifier_type": "elu",
"classes_num": 11,
"hidden_dims": 1024,
"second_dropout_rate": 0.2,
"first_dropout_rate": 0.3,
},
"encoder_config":{
"in_channels":3,
"encoder_name":'timm-efficientnet-b5',
"encoder_weights":None,
"classes":2,
"activation":'sigmoid',
"aux_params":dict(
 pooling='avg',
dropout=None,
classes=4, 
)
 },
"encoder_type": "timm-efficientnet-b5_unet",
"use_taylorsoftmax": False,
"one_channel": True,
"enable_inference_mode": True
},
model_ckp_dicts=chckps,
device=DEVICE 
))
ckp_names = glob('.. /input/ranzcr-models/timm_efficientnet_b5_unet_32bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_morepseudo_swa_roc_auc_score/timm_efficientnet_b5_unet_32bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_morepseudo_swa_roc_auc_score/*.pt')
 print(ckp_names)
 chckps = [torch.load(el, map_location='cpu')for el in ckp_names]
models_512.append(get_validation_models(
 model_initilizer=CNNSegModel,
model_config={
"classifiier_config": {
"classifier_type": "elu",
"classes_num": 11,
"hidden_dims": 1024,
"second_dropout_rate": 0.2,
"first_dropout_rate": 0.3,
},
"encoder_config":{
"in_channels":3,
"encoder_name":'timm-efficientnet-b5',
"encoder_weights":None,
"classes":2,
"activation":'sigmoid',
"aux_params":dict(
 pooling='avg',
dropout=None,
classes=4, 
)
 },
"encoder_type": "timm-efficientnet-b5_unet",
"use_taylorsoftmax": False,
"one_channel": True,
"enable_inference_mode": True
},
model_ckp_dicts=chckps,
device=DEVICE 
))
ckp_names = glob('.. /input/ranzcr-models/timm_efficientnet_b5_unet_24bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_pseudo_swa_roc_auc_score/timm_efficientnet_b5_unet_24bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_pseudo_swa_roc_auc_score/*.pt')
 print(ckp_names)
 chckps = [torch.load(el, map_location='cpu')for el in ckp_names]
models_512.append(get_validation_models(
 model_initilizer=CNNSegModel,
model_config={
"classifiier_config": {
"classifier_type": "elu",
"classes_num": 11,
"hidden_dims": 1024,
"second_dropout_rate": 0.2,
"first_dropout_rate": 0.3,
},
"encoder_config":{
"in_channels":3,
"encoder_name":'timm-efficientnet-b5',
"encoder_weights":None,
"classes":2,
"activation":'sigmoid',
"aux_params":dict(
 pooling='avg',
dropout=None,
classes=4, 
)
 },
"encoder_type": "timm-efficientnet-b5_unet",
"use_taylorsoftmax": False,
"one_channel": True,
"enable_inference_mode": True
},
model_ckp_dicts=chckps,
device=DEVICE 
))
ckp_names = glob('.. /input/ranzcr-models/timm_efficientnet_b5_unet_24bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_swa_roc_auc_score/timm_efficientnet_b5_unet_24bs_640res_qubvelaugs_ls005_shedchanged_startpoint_difflrs_segbranch_113coefs_1e4noseg_bigholes_swa_roc_auc_score/*.pt')
 print(ckp_names)
 chckps = [torch.load(el, map_location='cpu')for el in ckp_names]
models_512.append(get_validation_models(
 model_initilizer=CNNSegModel,
model_config={
"classifiier_config": {
"classifier_type": "elu",
"classes_num": 11,
"hidden_dims": 1024,
"second_dropout_rate": 0.2,
"first_dropout_rate": 0.3,
},
"encoder_config":{
"in_channels":3,
"encoder_name":'timm-efficientnet-b5',
"encoder_weights":None,
"classes":2,
"activation":'sigmoid',
"aux_params":dict(
 pooling='avg',
dropout=None,
classes=4, 
)
 },
"encoder_type": "timm-efficientnet-b5_unet",
"use_taylorsoftmax": False,
"one_channel": True,
"enable_inference_mode": True
},
model_ckp_dicts=chckps,
device=DEVICE 
)) 
<categorify>
submission_df_resnext = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_resnext.to_csv("submission_resnext.csv", index=False )
if not SKIP_VAL:
val_dfs = [
train.iloc[split[i][1]] for i in range(5)
 ]
val_loaders = create_val_loaders(
 loader_initilizer=RANZCRDataset,
loader_config={
"root":'train_images_512_512',
"path_col": "StudyInstanceUID",
"ext": ".jpeg",
"transforms":T.ToTensor()
},
dfs=val_dfs,
batch_size=32 
)
 train_logits = predict_over_all_train(
 val_loaders,
models_512[0],
cnn_model_predict,
DEVICE 
)
 val_dfs = pd.concat(val_dfs ).reset_index(drop=True)
 oof_score = roc_auc_score(val_dfs.iloc[:,1:-1], train_logits)
 print(f"OOF score : {oof_score}" )<define_variables>
!pip install.. /input/deepfake-xception-trained-model/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet
def predict_test_with_multiple_models(
 my_models: List[List[torch.nn.Module]],
my_loaders: List[torch.utils.data.DataLoader],
predict_func: Callable,
device: str, 
):
logits = []
for my_loader in my_loaders:
temp_logits = []
for batch in tqdm(my_loader):
temp_logits_inner = []
for exp_models in my_models:
logit = np.stack(
 [predict_func(batch, m, device)for m in exp_models], axis=0 
)
 temp_logits_inner.append(logit)
 temp_logits.append(np.stack(temp_logits_inner, axis=0))
logits.append(np.concatenate(temp_logits, axis=2))
return np.stack(logits, axis=0 )<define_variables>
%matplotlib inline
warnings.filterwarnings("ignore" )
INF_BS = 32<create_dataframe>
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 len(test_videos )
all_test_loaders_512 = []
test_original = RANZCRDataset(**{
"df":sample_sub,
"root":'test_images_512_512',
"path_col": "StudyInstanceUID",
"ext": ".jpeg",
"transforms":T.ToTensor()
})
 all_test_loaders_512.append(torch.utils.data.DataLoader(
 test_original,
batch_size=INF_BS,
drop_last=False,
shuffle=False,
num_workers=os.cpu_count() // 2 
))
test_hf = RANZCRDataset(**{
"df":sample_sub,
"root":'test_images_512_512',
"path_col": "StudyInstanceUID",
"ext": ".jpeg",
"transforms":T.ToTensor() ,
"augmentations": albu.HorizontalFlip(p=1.0)
 })
 all_test_loaders_512.append(torch.utils.data.DataLoader(
 test_hf,
batch_size=INF_BS,
drop_last=False,
shuffle=False,
num_workers=os.cpu_count() // 2 
))
test_left_rot = RANZCRDataset(**{
"df":sample_sub,
"root":'test_images_512_512',
"path_col": "StudyInstanceUID",
"ext": ".jpeg",
"transforms":T.ToTensor() ,
"augmentations": albu.ShiftScaleRotate(
 shift_limit=0,
scale_limit=0,
rotate_limit=(15,16),
p=1.0 
)
 })
 all_test_loaders_512.append(torch.utils.data.DataLoader(
 test_left_rot,
batch_size=INF_BS,
drop_last=False,
shuffle=False,
num_workers=os.cpu_count() // 2 
))
test_right_rot = RANZCRDataset(**{
"df":sample_sub,
"root":'test_images_512_512',
"path_col": "StudyInstanceUID",
"ext": ".jpeg",
"transforms":T.ToTensor() ,
"augmentations": albu.ShiftScaleRotate(
 shift_limit=0,
scale_limit=0,
rotate_limit=(-16,-15),
p=1.0 
)
 })
 all_test_loaders_512.append(torch.utils.data.DataLoader(
 test_right_rot,
batch_size=INF_BS,
drop_last=False,
shuffle=False,
num_workers=os.cpu_count() // 2 
))<predict_on_test>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
test_logits_512 = predict_test_with_multiple_models(
 models_512,
all_test_loaders_512,
cnn_model_predict,
DEVICE 
)
<import_modules>
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 )
from scipy.special import expit<compute_test_metric>
frames_per_video = 64
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet )
test_logits = expit(test_logits_512 ).mean(0 ).mean(1 )<prepare_x_and_y>
input_size = 150
my_exp_1 = test_logits[0]
my_exp_2 = test_logits[1]
my_exp_3 = test_logits[2]
my_exp_4 = test_logits[3]<define_variables>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
blend =(
 my_exp_1**0.5 +
my_exp_2**0.5 +
my_exp_3**0.5 +
my_exp_3**0.5
)<feature_engineering>
model = get_model("xception", pretrained=False)
 model = nn.Sequential(*list(model.children())[:-1])
 class Pooling(nn.Module):
def __init__(self):
super(Pooling, self ).__init__()
self.p1 = nn.AdaptiveAvgPool2d(( 1,1))
self.p2 = nn.AdaptiveMaxPool2d(( 1,1))
def forward(self, x):
x1 = self.p1(x)
 x2 = self.p2(x)
 return(x1+x2)* 0.5
model[0].final_block.pool = nn.Sequential(nn.AdaptiveAvgPool2d(( 1,1)))
 class Head(torch.nn.Module):
def __init__(self, in_f, out_f):
super(Head, self ).__init__()
self.f = nn.Flatten()
self.l = nn.Linear(in_f, 512)
 self.d = nn.Dropout(0.5)
 self.o = nn.Linear(512, out_f)
 self.b1 = nn.BatchNorm1d(in_f)
 self.b2 = nn.BatchNorm1d(512)
 self.r = nn.ReLU()
def forward(self, x):
x = self.f(x)
 x = self.b1(x)
 x = self.d(x)
 x = self.l(x)
 x = self.r(x)
 x = self.b2(x)
 x = self.d(x)
 out = self.o(x)
 return out
class FCN(torch.nn.Module):
def __init__(self, base, in_f):
super(FCN, self ).__init__()
self.base = base
self.h1 = Head(in_f, 1)
 def forward(self, x):
x = self.base(x)
 return self.h1(x)
 net = []
model = FCN(model, 2048)
 model = model.cuda()
model.load_state_dict(torch.load('.. /input/deepfake-xception-trained-model/model.pth'))
net.append(model )
sample_sub.iloc[:,1:] = blend
sample_sub<count_unique_values>
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return y_pred[:n].mean().item()
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.481
sample_sub.nunique(axis=0 )<save_to_csv>
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )
!rm -rf test_images_512_512
sample_sub.to_csv('submission.csv', index=False)
 os.listdir('./' )<define_variables>
%%time
model.eval()
predictions = predict_on_video_set(test_videos, num_workers=4 )
ROOT = Path.cwd().parent
INPUT = ROOT / "input"
OUTPUT = ROOT / "output"
DATA = INPUT / "ranzcr-clip-catheter-line-classification"
TRAIN = DATA / "train"
TEST = DATA / "test"
TRAINED_MODEL = INPUT/ 'ranzer-models'
TMP = ROOT / "tmp"
TMP.mkdir(exist_ok=True)
 RANDAM_SEED = 1086
N_CLASSES = 11
FOLDS = [0, 1, 2, 3, 4]
N_FOLD = len(FOLDS)
 IMAGE_SIZE =(640, 640)
 CONVERT_TO_RANK = False
FAST_COMMIT = True
CLASSES = [
'ETT - Abnormal',
'ETT - Borderline',
'ETT - Normal',
'NGT - Abnormal',
'NGT - Borderline',
'NGT - Incompletely Imaged',
'NGT - Normal',
'CVC - Abnormal',
'CVC - Borderline',
'CVC - Normal',
'Swan Ganz Catheter Present'
]<load_from_csv>
submission_df_xception = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_xception.to_csv("submission_xception.csv", index=False )
for p in DATA.iterdir() :
print(p.name)
 train = pd.read_csv(DATA / "train.csv")
 smpl_sub = pd.read_csv(DATA / "sample_submission.csv" )<split>
submission_df = pd.DataFrame({"filename": test_videos} )
if FAST_COMMIT and len(smpl_sub)== 3582:
smpl_sub = smpl_sub.iloc[:64 * 2].reset_index(drop=True )<categorify>
submission_df["label"] = 0.70*submission_df_resnext["label"] + 0.30*submission_df_xception["label"]
def multi_label_stratified_group_k_fold(label_arr: np.array, gid_arr: np.array, n_fold: int, seed: int=42):

 np.random.seed(seed)
 random.seed(seed)
 start_time = time.time()
n_train, n_class = label_arr.shape
gid_unique = sorted(set(gid_arr))
n_group = len(gid_unique)
 gid2aid = dict(zip(gid_unique, range(n_group)))
 aid_arr = np.vectorize(lambda x: gid2aid[x] )(gid_arr)
 cnts_by_class = label_arr.sum(axis=0)
 col, row = np.array(sorted(enumerate(aid_arr), key=lambda x: x[1])).T
cnts_by_group = coo_matrix(
(np.ones(len(label_arr)) ,(row, col))
).dot(coo_matrix(label_arr)).toarray().astype(int)
 del col
del row
cnts_by_fold = np.zeros(( n_fold, n_class), int)
 groups_by_fold = [[] for fid in range(n_fold)]
group_and_cnts = list(enumerate(cnts_by_group))
np.random.shuffle(group_and_cnts)
 print("finished preparation", time.time() - start_time)
 for aid, cnt_by_g in sorted(group_and_cnts, key=lambda x: -np.std(x[1])) :
best_fold = None
min_eval = None
for fid in range(n_fold):
cnts_by_fold[fid] += cnt_by_g
fold_eval =(cnts_by_fold / cnts_by_class ).std(axis=0 ).mean()
cnts_by_fold[fid] -= cnt_by_g
if min_eval is None or fold_eval < min_eval:
min_eval = fold_eval
best_fold = fid
cnts_by_fold[best_fold] += cnt_by_g
groups_by_fold[best_fold].append(aid)
 print("finished assignment.", time.time() - start_time)
 gc.collect()
idx_arr = np.arange(n_train)
 for fid in range(n_fold):
val_groups = groups_by_fold[fid]
val_indexs_bool = np.isin(aid_arr, val_groups)
 train_indexs = idx_arr[~val_indexs_bool]
val_indexs = idx_arr[val_indexs_bool]
print("[fold {}]".format(fid), end=" ")
 print("n_group:(train, val)=({}, {})".format(n_group - len(val_groups), len(val_groups)) , end=" ")
 print("n_sample:(train, val)=({}, {})".format(len(train_indexs), len(val_indexs)))
 yield train_indexs, val_indexs<concatenate>
submission_df.to_csv("submission.csv", index=False )