Kaggle competition: Deepfake Detection Challenge
label_arr = train[CLASSES].values
group_id = train.PatientID.values
train_val_indexs = list(
 multi_label_stratified_group_k_fold(label_arr, group_id, N_FOLD, RANDAM_SEED))<feature_engineering>
TEST_DIR = "/kaggle/input/deepfake-detection-challenge/test_videos/"
CHECKPOINT = '/kaggle/input/kha-deepfake-dataset/checkpoint_mobilev3_alldata_1903_withfaceforensics_3epochs_.pth'
CHECKPOINT2 = '/kaggle/input/kha-deepfake-dataset/cpt_mbn_sqrimg_2503)2epochs_.pth'
CHECKPOINT3 = '/kaggle/input/kha-deepfake-dataset/checkpoint_b0_1803_0epochs_0.4498354019969702.pth'
CHECKPOINT4 = '/kaggle/input/kha-deepfake-dataset/cpt_xception_new_sqrimg_29030_epochs_3_moment.pth'
CHECKPOINT5 = '/kaggle/input/kha-deepfake-dataset/cpt_nasnet_bbd_29032_epochs_0_moment.pth'
CHECKPOINT6 = '/kaggle/input/kha-deepfake-dataset/cpt_effb1_sqrimg_25032_epochs_1_moment.pth'
CHECKPOINT7 = '/kaggle/input/kha-deepfake-dataset/cpt_mbn_LSTM16_300313epochs_0.21526583118569945.pth'
CHECKPOINT8 = '/kaggle/input/kha-deepfake-dataset/checkpoint_b0_LSTM16_2503_batchfirst1epochs_0.25805166250713446.pth'
CHECKPOINT9 = '/kaggle/input/kha-deepfake-dataset/cpt_nasnetnew_bbd_31031_epochs_0_moment.pth'
CHECKPOINT10 = '/kaggle/input/kha-deepfake-dataset/cpt_xception_sqrimg_25031_epochs_0_moment.pth'
CHECKPOINT11 = '/kaggle/input/kha-deepfake-dataset/cpt_resnet_bbd_31033_epochs_3_moment.pth'
CONVERT_RGB = True
MTCNN_BATCH = 15
GAP = 5
IMG_SIZE = 224
SCALE = 0.5
NUM_WORKERS = mp.cpu_count()
train["fold"] = -1
for fold_id,(trn_idx, val_idx)in enumerate(train_val_indexs):
train.loc[val_idx, "fold"] = fold_id
train.groupby("fold")[CLASSES].sum()<train_model>
package_path = '.. /input/kha-efficientnet/EfficientNet-PyTorch/'
sys.path.append(package_path)
def resize_images(img_id, input_dir, output_dir, resize_to=(640, 640), ext="png"):
img_path = input_dir / f"{img_id}.jpg"
save_path = output_dir / f"{img_id}.{ext}"
img = cv2.imread(str(img_path), cv2.IMREAD_GRAYSCALE)
 img = cv2.resize(img, resize_to)
 cv2.imwrite(str(save_path), img,)
 TEST_RESIZED = TMP / "test_{0}x{1}".format(*IMAGE_SIZE)
 TEST_RESIZED.mkdir(exist_ok=True)
 TEST_RESIZED
_ = Parallel(n_jobs=2, verbose=5 )([
delayed(resize_images )(img_id, TEST, TEST_RESIZED, IMAGE_SIZE, "png")
 for img_id in smpl_sub.StudyInstanceUID.values
] )<choose_model_class>
%%capture
!pip install /kaggle/input/khafacenet/facenet_pytorch-2.2.7-py3-none-any.whl
!pip install /kaggle/input/imutils/imutils-0.5.3
def get_activation(activ_name: str="relu"):

 act_dict = {
"relu": nn.ReLU(inplace=True),
"tanh": nn.Tanh() ,
"sigmoid": nn.Sigmoid() ,
"identity": nn.Identity() }
if activ_name in act_dict:
return act_dict[activ_name]
else:
raise NotImplementedError
class Conv2dBNActiv(nn.Module):

 def __init__(
 self, in_channels: int, out_channels: int,
kernel_size: int, stride: int=1, padding: int=0,
bias: bool=False, use_bn: bool=True, activ: str="relu" 
):

 super(Conv2dBNActiv, self ).__init__()
layers = []
layers.append(nn.Conv2d(
 in_channels, out_channels,
kernel_size, stride, padding, bias=bias))
if use_bn:
layers.append(nn.BatchNorm2d(out_channels))
layers.append(get_activation(activ))
self.layers = nn.Sequential(*layers)
 def forward(self, x):

 return self.layers(x)
 class SSEBlock(nn.Module):

 def __init__(self, in_channels: int):

 super(SSEBlock, self ).__init__()
self.channel_squeeze = nn.Conv2d(
 in_channels=in_channels, out_channels=1,
kernel_size=1, stride=1, padding=0, bias=False)
 self.sigmoid = nn.Sigmoid()
def forward(self, x):

 h = self.sigmoid(self.channel_squeeze(x))
return x * h
class SpatialAttentionBlock(nn.Module):

 def __init__(
 self, in_channels: int,
out_channels_list: tp.List[int], 
):

 super(SpatialAttentionBlock, self ).__init__()
self.n_layers = len(out_channels_list)
 channels_list = [in_channels] + out_channels_list
assert self.n_layers > 0
assert channels_list[-1] == 1
for i in range(self.n_layers - 1):
in_chs, out_chs = channels_list[i: i + 2]
layer = Conv2dBNActiv(in_chs, out_chs, 3, 1, 1, activ="relu")
 setattr(self, f"conv{i + 1}", layer)
 in_chs, out_chs = channels_list[-2:]
layer = Conv2dBNActiv(in_chs, out_chs, 3, 1, 1, activ="sigmoid")
 setattr(self, f"conv{self.n_layers}", layer)
 def forward(self, x):

 h = x
for i in range(self.n_layers):
h = getattr(self, f"conv{i + 1}" )(h)
 h = h * x
return h<choose_model_class>
class MultiHeadResNet200D(nn.Module):
def __init__(
 self, out_dims_head: tp.List[int]=[3, 4, 3, 1], pretrained=False 
):

 self.base_name = "resnet200d_320"
self.n_heads = len(out_dims_head)
 super(MultiHeadResNet200D, self ).__init__()
base_model = timm.create_model(
 self.base_name, num_classes=sum(out_dims_head), pretrained=False)
 in_features = base_model.num_features
if pretrained:
pretrained_model_path = '.. /input/startingpointschestx/resnet200d_320_chestx.pth'
state_dict = dict()
for k, v in torch.load(pretrained_model_path, map_location='cpu')["model"].items() :
if k[:6] == "model.":
k = k.replace("model.", "")
 state_dict[k] = v
base_model.load_state_dict(state_dict)
 base_model.reset_classifier(0, '')
 self.backbone = base_model
for i, out_dim in enumerate(out_dims_head):
layer_name = f"head_{i}"
layer = nn.Sequential(
 SpatialAttentionBlock(in_features, [64, 32, 16, 1]),
nn.AdaptiveAvgPool2d(output_size=1),
nn.Flatten(start_dim=1),
nn.Linear(in_features, in_features),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(in_features, out_dim))
setattr(self, layer_name, layer)
 def forward(self, x):

 h = self.backbone(x)
 hs = [
getattr(self, f"head_{i}" )(h)for i in range(self.n_heads)]
y = torch.cat(hs, axis=1)
 return y
m = MultiHeadResNet200D([3, 4, 3, 1], False)
 m = m.eval()
x = torch.rand(1, 3, 256, 256)
 with torch.no_grad() :
y = m(x)
 print("[forward test]")
 print("input:\t{}
output:\t{}".format(x.shape, y.shape))
del m; del x; del y
gc.collect()<categorify>
class LabeledImageDataset(data.Dataset):

 def __init__(
 self,
file_list: tp.List[
tp.Tuple[tp.Union[str, Path], tp.Union[int, float, np.ndarray]]],
transform_list: tp.List[tp.Dict], 
):

 self.file_list = file_list
self.transform = ImageTransformForCls(transform_list)
 def __len__(self):

 return len(self.file_list)
 def __getitem__(self, index):

 img_path, label = self.file_list[index]
img = self._read_image_as_array(img_path)
 img, label = self.transform(( img, label))
return img, label
def _read_image_as_array(self, path: str):

 img_arr = cv2.imread(str(path))
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2RGB)
 return img_arr<create_dataframe>
%matplotlib inline
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def get_dataloaders_for_inference(
 file_list: tp.List[tp.List], batch_size=64, 
):

 dataset = LabeledImageDataset(
 file_list,
transform_list=[
["Normalize", {
"always_apply": True, "max_pixel_value": 255.0,
"mean": ["0.4887381077884414"], "std": ["0.23064819430546407"]}],
["ToTensorV2", {"always_apply": True}],
])
 loader = data.DataLoader(
 dataset,
batch_size=batch_size, shuffle=False,
num_workers=2, pin_memory=True,
drop_last=False)
 return loader<categorify>
def conv_bn(inp, oup, stride, conv_layer=nn.Conv2d, norm_layer=nn.BatchNorm2d, nlin_layer=nn.ReLU):
return nn.Sequential(
 conv_layer(inp, oup, 3, stride, 1, bias=False),
norm_layer(oup),
nlin_layer(inplace=True)
)
 def conv_1x1_bn(inp, oup, conv_layer=nn.Conv2d, norm_layer=nn.BatchNorm2d, nlin_layer=nn.ReLU):
return nn.Sequential(
 conv_layer(inp, oup, 1, 1, 0, bias=False),
norm_layer(oup),
nlin_layer(inplace=True)
)
 class Hswish(nn.Module):
def __init__(self, inplace=True):
super(Hswish, self ).__init__()
self.inplace = inplace
def forward(self, x):
return x * F.relu6(x + 3., inplace=self.inplace)/ 6.
 class Hsigmoid(nn.Module):
def __init__(self, inplace=True):
super(Hsigmoid, self ).__init__()
self.inplace = inplace
def forward(self, x):
return F.relu6(x + 3., inplace=self.inplace)/ 6.
 class SEModule(nn.Module):
def __init__(self, channel, reduction=4):
super(SEModule, self ).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Sequential(
 nn.Linear(channel, channel // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=False),
Hsigmoid() 
)
 def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x ).view(b, c)
 y = self.fc(y ).view(b, c, 1, 1)
 return x * y.expand_as(x)
 class Identity(nn.Module):
def __init__(self, channel):
super(Identity, self ).__init__()
def forward(self, x):
return x
def make_divisible(x, divisible_by=8):
return int(np.ceil(x * 1./ divisible_by)* divisible_by)
 class MobileBottleneck(nn.Module):
def __init__(self, inp, oup, kernel, stride, exp, se=False, nl='RE'):
super(MobileBottleneck, self ).__init__()
assert stride in [1, 2]
assert kernel in [3, 5]
padding =(kernel - 1)// 2
self.use_res_connect = stride == 1 and inp == oup
conv_layer = nn.Conv2d
norm_layer = nn.BatchNorm2d
if nl == 'RE':
nlin_layer = nn.ReLU
elif nl == 'HS':
nlin_layer = Hswish
else:
raise NotImplementedError
if se:
SELayer = SEModule
else:
SELayer = Identity
self.conv = nn.Sequential(
 conv_layer(inp, exp, 1, 1, 0, bias=False),
norm_layer(exp),
nlin_layer(inplace=True),
conv_layer(exp, exp, kernel, stride, padding, groups=exp, bias=False),
norm_layer(exp),
SELayer(exp),
nlin_layer(inplace=True),
conv_layer(exp, oup, 1, 1, 0, bias=False),
norm_layer(oup), 
)
 def forward(self, x):
if self.use_res_connect:
return x + self.conv(x)
 else:
return self.conv(x)
 class MobileNetV3(nn.Module):
def __init__(self, n_class=1000, input_size=224, dropout=0.8, mode='small', width_mult=1.0):
super(MobileNetV3, self ).__init__()
input_channel = 16
last_channel = 1280
if mode == 'large':
mobile_setting = [
[3, 16, 16, False, 'RE', 1],
[3, 64, 24, False, 'RE', 2],
[3, 72, 24, False, 'RE', 1],
[5, 72, 40, True, 'RE', 2],
[5, 120, 40, True, 'RE', 1],
[5, 120, 40, True, 'RE', 1],
[3, 240, 80, False, 'HS', 2],
[3, 200, 80, False, 'HS', 1],
[3, 184, 80, False, 'HS', 1],
[3, 184, 80, False, 'HS', 1],
[3, 480, 112, True, 'HS', 1],
[3, 672, 112, True, 'HS', 1],
[5, 672, 160, True, 'HS', 2],
[5, 960, 160, True, 'HS', 1],
[5, 960, 160, True, 'HS', 1],
]
elif mode == 'small':
mobile_setting = [
[3, 16, 16, True, 'RE', 2],
[3, 72, 24, False, 'RE', 2],
[3, 88, 24, False, 'RE', 1],
[5, 96, 40, True, 'HS', 2],
[5, 240, 40, True, 'HS', 1],
[5, 240, 40, True, 'HS', 1],
[5, 120, 48, True, 'HS', 1],
[5, 144, 48, True, 'HS', 1],
[5, 288, 96, True, 'HS', 2],
[5, 576, 96, True, 'HS', 1],
[5, 576, 96, True, 'HS', 1],
]
else:
raise NotImplementedError
assert input_size % 32 == 0
last_channel = make_divisible(last_channel * width_mult)if width_mult > 1.0 else last_channel
self.features = [conv_bn(3, input_channel, 2, nlin_layer=Hswish)]
self.classifier = []
for k, exp, c, se, nl, s in mobile_setting:
output_channel = make_divisible(c * width_mult)
 exp_channel = make_divisible(exp * width_mult)
 self.features.append(MobileBottleneck(input_channel, output_channel, k, s, exp_channel, se, nl))
input_channel = output_channel
if mode == 'large':
last_conv = make_divisible(960 * width_mult)
 self.features.append(conv_1x1_bn(input_channel, last_conv, nlin_layer=Hswish))
self.features.append(nn.AdaptiveAvgPool2d(1))
self.features.append(nn.Conv2d(last_conv, last_channel, 1, 1, 0))
self.features.append(Hswish(inplace=True))
elif mode == 'small':
last_conv = make_divisible(576 * width_mult)
 self.features.append(conv_1x1_bn(input_channel, last_conv, nlin_layer=Hswish))
self.features.append(nn.AdaptiveAvgPool2d(1))
self.features.append(nn.Conv2d(last_conv, last_channel, 1, 1, 0))
self.features.append(Hswish(inplace=True))
else:
raise NotImplementedError
self.features = nn.Sequential(*self.features)
 self.classifier = nn.Sequential(
 nn.Dropout(p=dropout),
nn.Linear(last_channel, n_class), 
)
 self._initialize_weights()
def forward(self, x):
x = self.features(x)
 x = x.mean(3 ).mean(2)
 x = self.classifier(x)
 return x
def _initialize_weights(self):
for m in self.modules() :
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out')
 if m.bias is not None:
nn.init.zeros_(m.bias)
 elif isinstance(m, nn.BatchNorm2d):
nn.init.ones_(m.weight)
 nn.init.zeros_(m.bias)
 elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
 if m.bias is not None:
nn.init.zeros_(m.bias)
 def mobilenetv3(pretrained=False, **kwargs):
model = MobileNetV3(**kwargs)
 if pretrained:
state_dict = torch.load(CFG.pretrained)
 model.load_state_dict(state_dict, strict=True)
 return model
class ImageTransformBase:

 def __init__(self, data_augmentations: tp.List[tp.Tuple[str, tp.Dict]]):

 augmentations_list = [
self._get_augmentation(aug_name )(**params)
 for aug_name, params in data_augmentations]
self.data_aug = albumentations.Compose(augmentations_list)
 def __call__(self, pair: tp.Tuple[np.ndarray])-> tp.Tuple[np.ndarray]:

 raise NotImplementedError
def _get_augmentation(self, aug_name: str)-> tp.Tuple[ImageOnlyTransform, DualTransform]:

 if hasattr(albumentations, aug_name):
return getattr(albumentations, aug_name)
 else:
return eval(aug_name)
 class ImageTransformForCls(ImageTransformBase):

 def __init__(self, data_augmentations: tp.List[tp.Tuple[str, tp.Dict]]):

 super(ImageTransformForCls, self ).__init__(data_augmentations)
 def __call__(self, in_arrs: tp.Tuple[np.ndarray])-> tp.Tuple[np.ndarray]:

 img, label = in_arrs
augmented = self.data_aug(image=img)
 img = augmented["image"]
return img, label<load_pretrained>
net = mobilenetv3(mode='small', pretrained=False)
 net.classifier[1] = torch.nn.Linear(in_features=1280, out_features=1)
 net = net.to(device)
 state_dict = torch.load(CHECKPOINT)
 net.load_state_dict(state_dict)
 net.cuda()
net.eval()
def load_setting_file(path: str):

 with open(path)as f:
settings = yaml.safe_load(f)
 return settings
def set_random_seed(seed: int = 42, deterministic: bool = False):

 random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = deterministic
def run_inference_loop(stgs, model, loader, device):
model.to(device)
 model.eval()
pred_list = []
with torch.no_grad() :
for x, t in tqdm(loader):
y_preds1 = model(x.to(device))
y_preds2 = model(x.flip(-1 ).to(device))
y_preds =(y_preds1.sigmoid().to('cpu' ).numpy() *0.6 + y_preds2.sigmoid().to('cpu' ).numpy() *0.4)
 pred_list.append(y_preds)
 pred_arr = np.concatenate(pred_list)
 del pred_list
return pred_arr<set_options>
net2 = mobilenetv3(mode='small', pretrained=False)
 net2.classifier[1] = torch.nn.Linear(in_features=1280, out_features=1)
 net2 = net2.to(device)
 state_dict = torch.load(CHECKPOINT2)
 net2.load_state_dict(state_dict)
 net2.cuda()
net2.eval()
if not torch.cuda.is_available() :
device = torch.device("cpu")
 else:
device = torch.device("cuda")
 print(device )<load_pretrained>
net3 = EfficientNet.from_name("efficientnet-b0")
 net3._fc = torch.nn.Linear(in_features=net3._fc.in_features, out_features=1)
 net3.load_state_dict(torch.load(CHECKPOINT3))
net3.cuda()
net3.eval()
model_dir = TRAINED_MODEL
test_dir = TEST_RESIZED
test_file_list = [ 
(test_dir / f"{img_id}.png", [-1] * 11)
 for img_id in smpl_sub["StudyInstanceUID"].values]
test_loader = get_dataloaders_for_inference(test_file_list, batch_size=32)
 test_preds_arr = np.zeros(( N_FOLD , len(smpl_sub), N_CLASSES))
for fold_id in [0,1,2,3,4]:
print(f"[fold {fold_id}]")
 stgs = load_setting_file(".. /input/ranzer-models/settings.yml")
 stgs["model"]["params"]["pretrained"] = False
model = MultiHeadResNet200D(**stgs["model"]["params"])
 model_path = model_dir / f"best_model_fold{fold_id}.pth"
print(model_path)
 model.load_state_dict(torch.load(model_path, map_location=device))
test_pred = run_inference_loop(stgs, model,test_loader, device)
 test_preds_arr[fold_id] = test_pred
del model
torch.cuda.empty_cache()
gc.collect() 
<save_to_csv>
net6 = EfficientNet.from_name("efficientnet-b1")
 net6._fc = torch.nn.Linear(in_features=net6._fc.in_features, out_features=1)
 net6.load_state_dict(torch.load(CHECKPOINT6))
net6.cuda()
net6.eval()
if CONVERT_TO_RANK:
test_preds_arr = test_preds_arr.argsort(axis=1 ).argsort(axis=1)
 sub = smpl_sub.copy()
sub[CLASSES] = test_preds_arr.mean(axis=0)
 sub.to_csv("submission.csv", index=False )<load_pretrained>
class SeparableConv2d(nn.Module):
def __init__(self,in_channels,out_channels,kernel_size=1,stride=1,padding=0,dilation=1,bias=False):
super(SeparableConv2d,self ).__init__()
self.conv1 = nn.Conv2d(in_channels,in_channels,kernel_size,stride,padding,dilation,groups=in_channels,bias=bias)
 self.pointwise = nn.Conv2d(in_channels,out_channels,1,1,0,1,1,bias=bias)
 def forward(self,x):
x = self.conv1(x)
 x = self.pointwise(x)
 return x
class Block(nn.Module):
def __init__(self,in_filters,out_filters,reps,strides=1,start_with_relu=True,grow_first=True):
super(Block, self ).__init__()
if out_filters != in_filters or strides!=1:
self.skip = nn.Conv2d(in_filters,out_filters,1,stride=strides, bias=False)
 self.skipbn = nn.BatchNorm2d(out_filters)
 else:
self.skip=None
rep=[]
filters=in_filters
if grow_first:
rep.append(nn.ReLU(inplace=True))
rep.append(SeparableConv2d(in_filters,out_filters,3,stride=1,padding=1,bias=False))
rep.append(nn.BatchNorm2d(out_filters))
filters = out_filters
for i in range(reps-1):
rep.append(nn.ReLU(inplace=True))
rep.append(SeparableConv2d(filters,filters,3,stride=1,padding=1,bias=False))
rep.append(nn.BatchNorm2d(filters))
if not grow_first:
rep.append(nn.ReLU(inplace=True))
rep.append(SeparableConv2d(in_filters,out_filters,3,stride=1,padding=1,bias=False))
rep.append(nn.BatchNorm2d(out_filters))
if not start_with_relu:
rep = rep[1:]
else:
rep[0] = nn.ReLU(inplace=False)
 if strides != 1:
rep.append(nn.MaxPool2d(3,strides,1))
self.rep = nn.Sequential(*rep)
 def forward(self,inp):
x = self.rep(inp)
 if self.skip is not None:
skip = self.skip(inp)
 skip = self.skipbn(skip)
 else:
skip = inp
x+=skip
return x
class Xception(nn.Module):

 def __init__(self, num_classes=1000):

 super(Xception, self ).__init__()
self.num_classes = num_classes
self.conv1 = nn.Conv2d(3, 32, 3,2, 0, bias=False)
 self.bn1 = nn.BatchNorm2d(32)
 self.relu1 = nn.ReLU(inplace=True)
 self.conv2 = nn.Conv2d(32,64,3,bias=False)
 self.bn2 = nn.BatchNorm2d(64)
 self.relu2 = nn.ReLU(inplace=True)
 self.block1=Block(64,128,2,2,start_with_relu=False,grow_first=True)
 self.block2=Block(128,256,2,2,start_with_relu=True,grow_first=True)
 self.block3=Block(256,728,2,2,start_with_relu=True,grow_first=True)
 self.block4=Block(728,728,3,1,start_with_relu=True,grow_first=True)
 self.block5=Block(728,728,3,1,start_with_relu=True,grow_first=True)
 self.block6=Block(728,728,3,1,start_with_relu=True,grow_first=True)
 self.block7=Block(728,728,3,1,start_with_relu=True,grow_first=True)
 self.block8=Block(728,728,3,1,start_with_relu=True,grow_first=True)
 self.block9=Block(728,728,3,1,start_with_relu=True,grow_first=True)
 self.block10=Block(728,728,3,1,start_with_relu=True,grow_first=True)
 self.block11=Block(728,728,3,1,start_with_relu=True,grow_first=True)
 self.block12=Block(728,1024,2,2,start_with_relu=True,grow_first=False)
 self.conv3 = SeparableConv2d(1024,1536,3,1,1)
 self.bn3 = nn.BatchNorm2d(1536)
 self.relu3 = nn.ReLU(inplace=True)
 self.conv4 = SeparableConv2d(1536,2048,3,1,1)
 self.bn4 = nn.BatchNorm2d(2048)
 self.fc = nn.Linear(2048, num_classes)
 def features(self, input):
x = self.conv1(input)
 x = self.bn1(x)
 x = self.relu1(x)
 x = self.conv2(x)
 x = self.bn2(x)
 x = self.relu2(x)
 x = self.block1(x)
 x = self.block2(x)
 x = self.block3(x)
 x = self.block4(x)
 x = self.block5(x)
 x = self.block6(x)
 x = self.block7(x)
 x = self.block8(x)
 x = self.block9(x)
 x = self.block10(x)
 x = self.block11(x)
 x = self.block12(x)
 x = self.conv3(x)
 x = self.bn3(x)
 x = self.relu3(x)
 x = self.conv4(x)
 x = self.bn4(x)
 return x
def logits(self, features):
x = nn.ReLU(inplace=True )(features)
 x = F.adaptive_avg_pool2d(x,(1, 1))
x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
def xception(num_classes=1000, pretrained='imagenet'):
model = Xception(num_classes=num_classes)
 if pretrained:
settings = pretrained_settings['xception'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
 model = Xception(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']
model.last_linear = model.fc
del model.fc
return model
net4 = Xception()
net4.last_linear = net4.fc
del net4.fc
net4.last_linear = nn.Linear(in_features=2048, out_features=1, bias=True)
 net4.load_state_dict(torch.load(CHECKPOINT4))
net4 = net4.to(device)
 net4.cuda()
net4.eval()
net10 = Xception()
net10.last_linear = net10.fc
del net10.fc
net10.last_linear = nn.Linear(in_features=2048, out_features=1, bias=True)
 net10.load_state_dict(torch.load(CHECKPOINT10))
net10 = net10.to(device)
 net10.cuda()
net10.eval()
model_dir = TRAINED_MODEL
test_dir = TEST_RESIZED
test_file_list = [ 
(test_dir / f"{img_id}.png", [-1] * 11)
 for img_id in smpl_sub["StudyInstanceUID"].values]
test_loader = get_dataloaders_for_inference(test_file_list, batch_size=4)
 N_FOLD = len([1024])
 test_preds_arr = np.zeros(( N_FOLD , len(smpl_sub), N_CLASSES))
for i,fold_id in enumerate([1024]):
print(f"[fold {fold_id}]")
 stgs = load_setting_file(".. /input/ranzer-models/settings.yml")
 stgs["model"]["params"]["pretrained"] = False
model = MultiHeadResNet200D(**stgs["model"]["params"])
 model_path = model_dir / f"best_model_fold{fold_id}.pth"
print(model_path)
 model.load_state_dict(torch.load(model_path, map_location=device))
test_pred = run_inference_loop(stgs, model,test_loader, device)
 test_preds_arr[i] = test_pred
del model
torch.cuda.empty_cache()
gc.collect() 
<prepare_output>
class MaxPoolPad(nn.Module):
def __init__(self):
super(MaxPoolPad, self ).__init__()
self.pad = nn.ZeroPad2d(( 1, 0, 1, 0))
self.pool = nn.MaxPool2d(3, stride=2, padding=1)
 def forward(self, x):
x = self.pad(x)
 x = self.pool(x)
 x = x[:, :, 1:, 1:].contiguous()
return x
class AvgPoolPad(nn.Module):
def __init__(self, stride=2, padding=1):
super(AvgPoolPad, self ).__init__()
self.pad = nn.ZeroPad2d(( 1, 0, 1, 0))
self.pool = nn.AvgPool2d(3, stride=stride, padding=padding, count_include_pad=False)
 def forward(self, x):
x = self.pad(x)
 x = self.pool(x)
 x = x[:, :, 1:, 1:].contiguous()
return x
class SeparableConv2d(nn.Module):
def __init__(self, in_channels, out_channels, dw_kernel, dw_stride, dw_padding, bias=False):
super(SeparableConv2d, self ).__init__()
self.depthwise_conv2d = nn.Conv2d(in_channels, in_channels, dw_kernel,
stride=dw_stride,
padding=dw_padding,
bias=bias,
groups=in_channels)
 self.pointwise_conv2d = nn.Conv2d(in_channels, out_channels, 1, stride=1, bias=bias)
 def forward(self, x):
x = self.depthwise_conv2d(x)
 x = self.pointwise_conv2d(x)
 return x
class BranchSeparables(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, name=None, bias=False):
super(BranchSeparables, self ).__init__()
self.relu = nn.ReLU()
self.separable_1 = SeparableConv2d(in_channels, in_channels, kernel_size, stride, padding, bias=bias)
 self.bn_sep_1 = nn.BatchNorm2d(in_channels, eps=0.001, momentum=0.1, affine=True)
 self.relu1 = nn.ReLU()
self.separable_2 = SeparableConv2d(in_channels, out_channels, kernel_size, 1, padding, bias=bias)
 self.bn_sep_2 = nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.1, affine=True)
 self.name = name
def forward(self, x):
x = self.relu(x)
 if self.name == 'specific':
x = nn.ZeroPad2d(( 1, 0, 1, 0))(x)
 x = self.separable_1(x)
 if self.name == 'specific':
x = x[:, :, 1:, 1:].contiguous()
x = self.bn_sep_1(x)
 x = self.relu1(x)
 x = self.separable_2(x)
 x = self.bn_sep_2(x)
 return x
class BranchSeparablesStem(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, bias=False):
super(BranchSeparablesStem, self ).__init__()
self.relu = nn.ReLU()
self.separable_1 = SeparableConv2d(in_channels, out_channels, kernel_size, stride, padding, bias=bias)
 self.bn_sep_1 = nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.1, affine=True)
 self.relu1 = nn.ReLU()
self.separable_2 = SeparableConv2d(out_channels, out_channels, kernel_size, 1, padding, bias=bias)
 self.bn_sep_2 = nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.1, affine=True)
 def forward(self, x):
x = self.relu(x)
 x = self.separable_1(x)
 x = self.bn_sep_1(x)
 x = self.relu1(x)
 x = self.separable_2(x)
 x = self.bn_sep_2(x)
 return x
class BranchSeparablesReduction(BranchSeparables):
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, z_padding=1, bias=False):
BranchSeparables.__init__(self, in_channels, out_channels, kernel_size, stride, padding, bias)
 self.padding = nn.ZeroPad2d(( z_padding, 0, z_padding, 0))
def forward(self, x):
x = self.relu(x)
 x = self.padding(x)
 x = self.separable_1(x)
 x = x[:, :, 1:, 1:].contiguous()
x = self.bn_sep_1(x)
 x = self.relu1(x)
 x = self.separable_2(x)
 x = self.bn_sep_2(x)
 return x
class CellStem0(nn.Module):
def __init__(self, stem_filters, num_filters=42):
super(CellStem0, self ).__init__()
self.num_filters = num_filters
self.stem_filters = stem_filters
self.conv_1x1 = nn.Sequential()
self.conv_1x1.add_module('relu', nn.ReLU())
 self.conv_1x1.add_module('conv', nn.Conv2d(self.stem_filters, self.num_filters, 1, stride=1, bias=False))
self.conv_1x1.add_module('bn', nn.BatchNorm2d(self.num_filters, eps=0.001, momentum=0.1, affine=True))
self.comb_iter_0_left = BranchSeparables(self.num_filters, self.num_filters, 5, 2, 2)
 self.comb_iter_0_right = BranchSeparablesStem(self.stem_filters, self.num_filters, 7, 2, 3, bias=False)
 self.comb_iter_1_left = nn.MaxPool2d(3, stride=2, padding=1)
 self.comb_iter_1_right = BranchSeparablesStem(self.stem_filters, self.num_filters, 7, 2, 3, bias=False)
 self.comb_iter_2_left = nn.AvgPool2d(3, stride=2, padding=1, count_include_pad=False)
 self.comb_iter_2_right = BranchSeparablesStem(self.stem_filters, self.num_filters, 5, 2, 2, bias=False)
 self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False)
 self.comb_iter_4_left = BranchSeparables(self.num_filters, self.num_filters, 3, 1, 1, bias=False)
 self.comb_iter_4_right = nn.MaxPool2d(3, stride=2, padding=1)
 def forward(self, x):
x1 = self.conv_1x1(x)
 x_comb_iter_0_left = self.comb_iter_0_left(x1)
 x_comb_iter_0_right = self.comb_iter_0_right(x)
 x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right
x_comb_iter_1_left = self.comb_iter_1_left(x1)
 x_comb_iter_1_right = self.comb_iter_1_right(x)
 x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right
x_comb_iter_2_left = self.comb_iter_2_left(x1)
 x_comb_iter_2_right = self.comb_iter_2_right(x)
 x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right
x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0)
 x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1
x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0)
 x_comb_iter_4_right = self.comb_iter_4_right(x1)
 x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right
x_out = torch.cat([x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1)
 return x_out
class CellStem1(nn.Module):
def __init__(self, stem_filters, num_filters):
super(CellStem1, self ).__init__()
self.num_filters = num_filters
self.stem_filters = stem_filters
self.conv_1x1 = nn.Sequential()
self.conv_1x1.add_module('relu', nn.ReLU())
 self.conv_1x1.add_module('conv', nn.Conv2d(2*self.num_filters, self.num_filters, 1, stride=1, bias=False))
self.conv_1x1.add_module('bn', nn.BatchNorm2d(self.num_filters, eps=0.001, momentum=0.1, affine=True))
self.relu = nn.ReLU()
self.path_1 = nn.Sequential()
self.path_1.add_module('avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False))
self.path_1.add_module('conv', nn.Conv2d(self.stem_filters, self.num_filters//2, 1, stride=1, bias=False))
self.path_2 = nn.ModuleList()
self.path_2.add_module('pad', nn.ZeroPad2d(( 0, 1, 0, 1)))
 self.path_2.add_module('avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False))
self.path_2.add_module('conv', nn.Conv2d(self.stem_filters, self.num_filters//2, 1, stride=1, bias=False))
self.final_path_bn = nn.BatchNorm2d(self.num_filters, eps=0.001, momentum=0.1, affine=True)
 self.comb_iter_0_left = BranchSeparables(self.num_filters, self.num_filters, 5, 2, 2, name='specific', bias=False)
 self.comb_iter_0_right = BranchSeparables(self.num_filters, self.num_filters, 7, 2, 3, name='specific', bias=False)
 self.comb_iter_1_left = MaxPoolPad()
self.comb_iter_1_right = BranchSeparables(self.num_filters, self.num_filters, 7, 2, 3, name='specific', bias=False)
 self.comb_iter_2_left = AvgPoolPad()
self.comb_iter_2_right = BranchSeparables(self.num_filters, self.num_filters, 5, 2, 2, name='specific', bias=False)
 self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False)
 self.comb_iter_4_left = BranchSeparables(self.num_filters, self.num_filters, 3, 1, 1, name='specific', bias=False)
 self.comb_iter_4_right = MaxPoolPad()
def forward(self, x_conv0, x_stem_0):
x_left = self.conv_1x1(x_stem_0)
 x_relu = self.relu(x_conv0)
 x_path1 = self.path_1(x_relu)
 x_path2 = self.path_2.pad(x_relu)
 x_path2 = x_path2[:, :, 1:, 1:]
x_path2 = self.path_2.avgpool(x_path2)
 x_path2 = self.path_2.conv(x_path2)
 x_right = self.final_path_bn(torch.cat([x_path1, x_path2], 1))
x_comb_iter_0_left = self.comb_iter_0_left(x_left)
 x_comb_iter_0_right = self.comb_iter_0_right(x_right)
 x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right
x_comb_iter_1_left = self.comb_iter_1_left(x_left)
 x_comb_iter_1_right = self.comb_iter_1_right(x_right)
 x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right
x_comb_iter_2_left = self.comb_iter_2_left(x_left)
 x_comb_iter_2_right = self.comb_iter_2_right(x_right)
 x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right
x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0)
 x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1
x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0)
 x_comb_iter_4_right = self.comb_iter_4_right(x_left)
 x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right
x_out = torch.cat([x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1)
 return x_out
class FirstCell(nn.Module):
def __init__(self, in_channels_left, out_channels_left, in_channels_right, out_channels_right):
super(FirstCell, self ).__init__()
self.conv_1x1 = nn.Sequential()
self.conv_1x1.add_module('relu', nn.ReLU())
 self.conv_1x1.add_module('conv', nn.Conv2d(in_channels_right, out_channels_right, 1, stride=1, bias=False))
self.conv_1x1.add_module('bn', nn.BatchNorm2d(out_channels_right, eps=0.001, momentum=0.1, affine=True))
self.relu = nn.ReLU()
self.path_1 = nn.Sequential()
self.path_1.add_module('avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False))
self.path_1.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False))
self.path_2 = nn.ModuleList()
self.path_2.add_module('pad', nn.ZeroPad2d(( 0, 1, 0, 1)))
 self.path_2.add_module('avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False))
self.path_2.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False))
self.final_path_bn = nn.BatchNorm2d(out_channels_left * 2, eps=0.001, momentum=0.1, affine=True)
 self.comb_iter_0_left = BranchSeparables(out_channels_right, out_channels_right, 5, 1, 2, bias=False)
 self.comb_iter_0_right = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, bias=False)
 self.comb_iter_1_left = BranchSeparables(out_channels_right, out_channels_right, 5, 1, 2, bias=False)
 self.comb_iter_1_right = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, bias=False)
 self.comb_iter_2_left = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False)
 self.comb_iter_3_left = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False)
 self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False)
 self.comb_iter_4_left = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, bias=False)
 def forward(self, x, x_prev):
x_relu = self.relu(x_prev)
 x_path1 = self.path_1(x_relu)
 x_path2 = self.path_2.pad(x_relu)
 x_path2 = x_path2[:, :, 1:, 1:]
x_path2 = self.path_2.avgpool(x_path2)
 x_path2 = self.path_2.conv(x_path2)
 x_left = self.final_path_bn(torch.cat([x_path1, x_path2], 1))
x_right = self.conv_1x1(x)
 x_comb_iter_0_left = self.comb_iter_0_left(x_right)
 x_comb_iter_0_right = self.comb_iter_0_right(x_left)
 x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right
x_comb_iter_1_left = self.comb_iter_1_left(x_left)
 x_comb_iter_1_right = self.comb_iter_1_right(x_left)
 x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right
x_comb_iter_2_left = self.comb_iter_2_left(x_right)
 x_comb_iter_2 = x_comb_iter_2_left + x_left
x_comb_iter_3_left = self.comb_iter_3_left(x_left)
 x_comb_iter_3_right = self.comb_iter_3_right(x_left)
 x_comb_iter_3 = x_comb_iter_3_left + x_comb_iter_3_right
x_comb_iter_4_left = self.comb_iter_4_left(x_right)
 x_comb_iter_4 = x_comb_iter_4_left + x_right
x_out = torch.cat([x_left, x_comb_iter_0, x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1)
 return x_out
class NormalCell(nn.Module):
def __init__(self, in_channels_left, out_channels_left, in_channels_right, out_channels_right):
super(NormalCell, self ).__init__()
self.conv_prev_1x1 = nn.Sequential()
self.conv_prev_1x1.add_module('relu', nn.ReLU())
 self.conv_prev_1x1.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False))
self.conv_prev_1x1.add_module('bn', nn.BatchNorm2d(out_channels_left, eps=0.001, momentum=0.1, affine=True))
self.conv_1x1 = nn.Sequential()
self.conv_1x1.add_module('relu', nn.ReLU())
 self.conv_1x1.add_module('conv', nn.Conv2d(in_channels_right, out_channels_right, 1, stride=1, bias=False))
self.conv_1x1.add_module('bn', nn.BatchNorm2d(out_channels_right, eps=0.001, momentum=0.1, affine=True))
self.comb_iter_0_left = BranchSeparables(out_channels_right, out_channels_right, 5, 1, 2, bias=False)
 self.comb_iter_0_right = BranchSeparables(out_channels_left, out_channels_left, 3, 1, 1, bias=False)
 self.comb_iter_1_left = BranchSeparables(out_channels_left, out_channels_left, 5, 1, 2, bias=False)
 self.comb_iter_1_right = BranchSeparables(out_channels_left, out_channels_left, 3, 1, 1, bias=False)
 self.comb_iter_2_left = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False)
 self.comb_iter_3_left = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False)
 self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False)
 self.comb_iter_4_left = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, bias=False)
 def forward(self, x, x_prev):
x_left = self.conv_prev_1x1(x_prev)
 x_right = self.conv_1x1(x)
 x_comb_iter_0_left = self.comb_iter_0_left(x_right)
 x_comb_iter_0_right = self.comb_iter_0_right(x_left)
 x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right
x_comb_iter_1_left = self.comb_iter_1_left(x_left)
 x_comb_iter_1_right = self.comb_iter_1_right(x_left)
 x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right
x_comb_iter_2_left = self.comb_iter_2_left(x_right)
 x_comb_iter_2 = x_comb_iter_2_left + x_left
x_comb_iter_3_left = self.comb_iter_3_left(x_left)
 x_comb_iter_3_right = self.comb_iter_3_right(x_left)
 x_comb_iter_3 = x_comb_iter_3_left + x_comb_iter_3_right
x_comb_iter_4_left = self.comb_iter_4_left(x_right)
 x_comb_iter_4 = x_comb_iter_4_left + x_right
x_out = torch.cat([x_left, x_comb_iter_0, x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1)
 return x_out
class ReductionCell0(nn.Module):
def __init__(self, in_channels_left, out_channels_left, in_channels_right, out_channels_right):
super(ReductionCell0, self ).__init__()
self.conv_prev_1x1 = nn.Sequential()
self.conv_prev_1x1.add_module('relu', nn.ReLU())
 self.conv_prev_1x1.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False))
self.conv_prev_1x1.add_module('bn', nn.BatchNorm2d(out_channels_left, eps=0.001, momentum=0.1, affine=True))
self.conv_1x1 = nn.Sequential()
self.conv_1x1.add_module('relu', nn.ReLU())
 self.conv_1x1.add_module('conv', nn.Conv2d(in_channels_right, out_channels_right, 1, stride=1, bias=False))
self.conv_1x1.add_module('bn', nn.BatchNorm2d(out_channels_right, eps=0.001, momentum=0.1, affine=True))
self.comb_iter_0_left = BranchSeparablesReduction(out_channels_right, out_channels_right, 5, 2, 2, bias=False)
 self.comb_iter_0_right = BranchSeparablesReduction(out_channels_right, out_channels_right, 7, 2, 3, bias=False)
 self.comb_iter_1_left = MaxPoolPad()
self.comb_iter_1_right = BranchSeparablesReduction(out_channels_right, out_channels_right, 7, 2, 3, bias=False)
 self.comb_iter_2_left = AvgPoolPad()
self.comb_iter_2_right = BranchSeparablesReduction(out_channels_right, out_channels_right, 5, 2, 2, bias=False)
 self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False)
 self.comb_iter_4_left = BranchSeparablesReduction(out_channels_right, out_channels_right, 3, 1, 1, bias=False)
 self.comb_iter_4_right = MaxPoolPad()
def forward(self, x, x_prev):
x_left = self.conv_prev_1x1(x_prev)
 x_right = self.conv_1x1(x)
 x_comb_iter_0_left = self.comb_iter_0_left(x_right)
 x_comb_iter_0_right = self.comb_iter_0_right(x_left)
 x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right
x_comb_iter_1_left = self.comb_iter_1_left(x_right)
 x_comb_iter_1_right = self.comb_iter_1_right(x_left)
 x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right
x_comb_iter_2_left = self.comb_iter_2_left(x_right)
 x_comb_iter_2_right = self.comb_iter_2_right(x_left)
 x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right
x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0)
 x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1
x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0)
 x_comb_iter_4_right = self.comb_iter_4_right(x_right)
 x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right
x_out = torch.cat([x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1)
 return x_out
class ReductionCell1(nn.Module):
def __init__(self, in_channels_left, out_channels_left, in_channels_right, out_channels_right):
super(ReductionCell1, self ).__init__()
self.conv_prev_1x1 = nn.Sequential()
self.conv_prev_1x1.add_module('relu', nn.ReLU())
 self.conv_prev_1x1.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False))
self.conv_prev_1x1.add_module('bn', nn.BatchNorm2d(out_channels_left, eps=0.001, momentum=0.1, affine=True))
self.conv_1x1 = nn.Sequential()
self.conv_1x1.add_module('relu', nn.ReLU())
 self.conv_1x1.add_module('conv', nn.Conv2d(in_channels_right, out_channels_right, 1, stride=1, bias=False))
self.conv_1x1.add_module('bn', nn.BatchNorm2d(out_channels_right, eps=0.001, momentum=0.1, affine=True))
self.comb_iter_0_left = BranchSeparables(out_channels_right, out_channels_right, 5, 2, 2, name='specific', bias=False)
 self.comb_iter_0_right = BranchSeparables(out_channels_right, out_channels_right, 7, 2, 3, name='specific', bias=False)
 self.comb_iter_1_left = MaxPoolPad()
self.comb_iter_1_right = BranchSeparables(out_channels_right, out_channels_right, 7, 2, 3, name='specific', bias=False)
 self.comb_iter_2_left = AvgPoolPad()
self.comb_iter_2_right = BranchSeparables(out_channels_right, out_channels_right, 5, 2, 2, name='specific', bias=False)
 self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False)
 self.comb_iter_4_left = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, name='specific', bias=False)
 self.comb_iter_4_right =MaxPoolPad()
def forward(self, x, x_prev):
x_left = self.conv_prev_1x1(x_prev)
 x_right = self.conv_1x1(x)
 x_comb_iter_0_left = self.comb_iter_0_left(x_right)
 x_comb_iter_0_right = self.comb_iter_0_right(x_left)
 x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right
x_comb_iter_1_left = self.comb_iter_1_left(x_right)
 x_comb_iter_1_right = self.comb_iter_1_right(x_left)
 x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right
x_comb_iter_2_left = self.comb_iter_2_left(x_right)
 x_comb_iter_2_right = self.comb_iter_2_right(x_left)
 x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right
x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0)
 x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1
x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0)
 x_comb_iter_4_right = self.comb_iter_4_right(x_right)
 x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right
x_out = torch.cat([x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1)
 return x_out
class NASNetAMobile(nn.Module):

 def __init__(self, num_classes=1000, stem_filters=32, penultimate_filters=1056, filters_multiplier=2):
super(NASNetAMobile, self ).__init__()
self.num_classes = num_classes
self.stem_filters = stem_filters
self.penultimate_filters = penultimate_filters
self.filters_multiplier = filters_multiplier
filters = self.penultimate_filters // 24
self.conv0 = nn.Sequential()
self.conv0.add_module('conv', nn.Conv2d(in_channels=3, out_channels=self.stem_filters, kernel_size=3, padding=0, stride=2,
bias=False))
self.conv0.add_module('bn', nn.BatchNorm2d(self.stem_filters, eps=0.001, momentum=0.1, affine=True))
self.cell_stem_0 = CellStem0(self.stem_filters, num_filters=filters //(filters_multiplier ** 2))
self.cell_stem_1 = CellStem1(self.stem_filters, num_filters=filters // filters_multiplier)
 self.cell_0 = FirstCell(in_channels_left=filters, out_channels_left=filters//2,
in_channels_right=2*filters, out_channels_right=filters)
 self.cell_1 = NormalCell(in_channels_left=2*filters, out_channels_left=filters,
in_channels_right=6*filters, out_channels_right=filters)
 self.cell_2 = NormalCell(in_channels_left=6*filters, out_channels_left=filters,
in_channels_right=6*filters, out_channels_right=filters)
 self.cell_3 = NormalCell(in_channels_left=6*filters, out_channels_left=filters,
in_channels_right=6*filters, out_channels_right=filters)
 self.reduction_cell_0 = ReductionCell0(in_channels_left=6*filters, out_channels_left=2*filters,
in_channels_right=6*filters, out_channels_right=2*filters)
 self.cell_6 = FirstCell(in_channels_left=6*filters, out_channels_left=filters,
in_channels_right=8*filters, out_channels_right=2*filters)
 self.cell_7 = NormalCell(in_channels_left=8*filters, out_channels_left=2*filters,
in_channels_right=12*filters, out_channels_right=2*filters)
 self.cell_8 = NormalCell(in_channels_left=12*filters, out_channels_left=2*filters,
in_channels_right=12*filters, out_channels_right=2*filters)
 self.cell_9 = NormalCell(in_channels_left=12*filters, out_channels_left=2*filters,
in_channels_right=12*filters, out_channels_right=2*filters)
 self.reduction_cell_1 = ReductionCell1(in_channels_left=12*filters, out_channels_left=4*filters,
in_channels_right=12*filters, out_channels_right=4*filters)
 self.cell_12 = FirstCell(in_channels_left=12*filters, out_channels_left=2*filters,
in_channels_right=16*filters, out_channels_right=4*filters)
 self.cell_13 = NormalCell(in_channels_left=16*filters, out_channels_left=4*filters,
in_channels_right=24*filters, out_channels_right=4*filters)
 self.cell_14 = NormalCell(in_channels_left=24*filters, out_channels_left=4*filters,
in_channels_right=24*filters, out_channels_right=4*filters)
 self.cell_15 = NormalCell(in_channels_left=24*filters, out_channels_left=4*filters,
in_channels_right=24*filters, out_channels_right=4*filters)
 self.relu = nn.ReLU()
self.avg_pool = nn.AvgPool2d(7, stride=1, padding=0)
 self.dropout = nn.Dropout()
self.last_linear = nn.Linear(24*filters, self.num_classes)
 def features(self, input):
x_conv0 = self.conv0(input)
 x_stem_0 = self.cell_stem_0(x_conv0)
 x_stem_1 = self.cell_stem_1(x_conv0, x_stem_0)
 x_cell_0 = self.cell_0(x_stem_1, x_stem_0)
 x_cell_1 = self.cell_1(x_cell_0, x_stem_1)
 x_cell_2 = self.cell_2(x_cell_1, x_cell_0)
 x_cell_3 = self.cell_3(x_cell_2, x_cell_1)
 x_reduction_cell_0 = self.reduction_cell_0(x_cell_3, x_cell_2)
 x_cell_6 = self.cell_6(x_reduction_cell_0, x_cell_3)
 x_cell_7 = self.cell_7(x_cell_6, x_reduction_cell_0)
 x_cell_8 = self.cell_8(x_cell_7, x_cell_6)
 x_cell_9 = self.cell_9(x_cell_8, x_cell_7)
 x_reduction_cell_1 = self.reduction_cell_1(x_cell_9, x_cell_8)
 x_cell_12 = self.cell_12(x_reduction_cell_1, x_cell_9)
 x_cell_13 = self.cell_13(x_cell_12, x_reduction_cell_1)
 x_cell_14 = self.cell_14(x_cell_13, x_cell_12)
 x_cell_15 = self.cell_15(x_cell_14, x_cell_13)
 return x_cell_15
def logits(self, features):
x = self.relu(features)
 x = self.avg_pool(x)
 x = x.view(x.size(0), -1)
 x = self.dropout(x)
 x = self.last_linear(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
net5 = NASNetAMobile()
net5.last_linear = nn.Linear(in_features=1056, out_features=1, bias=True)
 net5.load_state_dict(torch.load(CHECKPOINT5))
net5 = net5.to(device)
 net5.cuda()
net5.eval()
net9 = NASNetAMobile()
net9.last_linear = nn.Linear(in_features=1056, out_features=1, bias=True)
 net9.load_state_dict(torch.load(CHECKPOINT9))
net9 = net9.to(device)
 net9.cuda()
net9.eval()
sub_2 = smpl_sub.copy()
sub_2[CLASSES] = test_preds_arr.mean(axis=0 )<feature_engineering>
class CFG:
seq_len=10
lstm_in = 16
lstm_out = 16
class LSTM_Model(nn.Module):
def __init__(self):
super(LSTM_Model, self ).__init__()
self.cnn_net = mobilenetv3(mode='small', pretrained=False)
 self.cnn_net.classifier[1] = nn.Linear(in_features=1280, out_features=1)
 self.cnn_net.classifier[1] = nn.Linear(in_features=1280, out_features=CFG.lstm_in)
 self.lstm = nn.LSTM(CFG.lstm_in, CFG.lstm_out, bidirectional=False, batch_first=True)
 self.reg_layer = nn.Sequential(nn.Dropout(0.5),
nn.Linear(CFG.lstm_out, 1))
def forward(self, x):
n_samples = x.shape[0]
x = x.view(-1, 3, 224, 224)
 x = self.cnn_net(x)
 x = x.view(n_samples, CFG.seq_len, -1)
 x,(h_n, h_c)= self.lstm(x)
 x = x[:,-1]
y = self.reg_layer(x)
 return y
net7 = LSTM_Model()
net7.load_state_dict(torch.load(CHECKPOINT7))
net7 = net7.to(device)
 net7.cuda()
net7.eval()
net8 = LSTM_Model()
net8.load_state_dict(torch.load(CHECKPOINT8))
net8 = net8.to(device)
 net8.cuda()
net8.eval()
sub[CLASSES] = 0.6 * sub[CLASSES] + 0.4 * sub_2[CLASSES]<save_to_csv>
sub.to_csv("submission.csv", index=False )<import_modules>
net11 = torchvision.models.resnet18(pretrained=False)
 net11.fc = nn.Linear(in_features=512, out_features=1, bias=True)
 net11.load_state_dict(torch.load(CHECKPOINT11))
net11 = net11.to(device)
 net11.cuda()
net11.eval()
import pandas as pd
from IPython.display import display
from sklearn.feature_selection import VarianceThreshold
from sklearn.ensemble import ExtraTreesClassifier
from sklearn import svm
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score, mean_squared_error
import numpy as np<categorify>
import albumentations
from albumentations.augmentations.transforms import ShiftScaleRotate, HorizontalFlip, RandomBrightnessContrast, MotionBlur, Blur, GaussNoise, JpegCompression
types = {'ID':np.uint32, 'target':np.uint8, 'VAR_0002':np.uint16, 'VAR_0003':np.uint16, 'VAR_0532':np.uint8, 'VAR_0533':np.uint8, 'VAR_0534':np.uint8,
'VAR_0535':np.uint8, 'VAR_0536':np.uint8, 'VAR_0537':np.uint8,'VAR_0538':np.uint8, 'VAR_0539':np.uint8, 'VAR_0540':np.uint8, 'VAR_0545':np.uint16,
'VAR_0546':np.uint16, 'VAR_0547':np.uint16, 'VAR_0548':np.uint16, 'VAR_0549':np.uint16, 'VAR_0550':np.uint16, 'VAR_0551':np.uint16,
'VAR_0552':np.uint8, 'VAR_0553':np.uint8, 'VAR_0554':np.uint16, 'VAR_0555':np.uint16, 'VAR_0556':np.uint16, 'VAR_0557':np.uint16,
'VAR_0558':np.uint16, 'VAR_0559':np.uint8, 'VAR_0560':np.uint8, 'VAR_0561':np.uint16, 'VAR_0562':np.uint8, 'VAR_0563':np.uint8,
'VAR_0564':np.uint8, 'VAR_0565':np.uint8, 'VAR_0566':np.uint8, 'VAR_0567':np.uint8, 'VAR_0568':np.uint8, 'VAR_0569':np.uint8,
'VAR_0570':np.uint16, 'VAR_0572':np.uint8, 'VAR_0580':np.uint8, 'VAR_0581':np.uint8, 'VAR_0582':np.uint8, 'VAR_0604':np.uint8,
'VAR_0605':np.uint8, 'VAR_0606':np.uint8, 'VAR_0617':np.uint8, 'VAR_0618':np.uint8, 'VAR_0619':np.uint8, 'VAR_0620':np.uint8,
'VAR_0621':np.uint8, 'VAR_0622':np.uint8, 'VAR_0623':np.uint8, 'VAR_0624':np.uint8, 'VAR_0625':np.uint8, 'VAR_0626':np.uint8,
'VAR_0627':np.uint8, 'VAR_0637':np.uint8, 'VAR_0638':np.uint8, 'VAR_0639':np.uint8, 'VAR_0640':np.uint8, 'VAR_0646':np.uint8,
'VAR_0647':np.uint8, 'VAR_0657':np.uint8, 'VAR_0658':np.uint8, 'VAR_0662':np.uint8, 'VAR_0663':np.uint8, 'VAR_0664':np.uint8, 'VAR_0665':np.uint8,
'VAR_0666':np.uint8,'VAR_0667':np.uint8, 'VAR_0668':np.uint8, 'VAR_0685':np.uint8, 'VAR_0686':np.uint8, 'VAR_0689':np.uint8, 'VAR_0690':np.uint8,
'VAR_0696':np.uint8, 'VAR_0697':np.uint8, 'VAR_0703':np.uint8, 'VAR_0708':np.uint8, 'VAR_0709':np.uint8, 'VAR_0710':np.uint8, 'VAR_0711':np.uint8,
'VAR_0712':np.uint8, 'VAR_0713':np.uint8, 'VAR_0714':np.uint8, 'VAR_0715':np.uint8, 'VAR_0716':np.uint8, 'VAR_0717':np.uint8, 'VAR_0718':np.uint8,
'VAR_0719':np.uint8, 'VAR_0720':np.uint8, 'VAR_0721':np.uint8, 'VAR_0722':np.uint8, 'VAR_0723':np.uint8, 'VAR_0724':np.uint8, 'VAR_0725':np.uint8,
'VAR_0726':np.uint8, 'VAR_0727':np.uint8, 'VAR_0728':np.uint8, 'VAR_0729':np.uint8, 'VAR_0730':np.uint8, 'VAR_0731':np.uint8, 'VAR_0732':np.uint8,
'VAR_0733':np.uint8, 'VAR_0734':np.uint8, 'VAR_0735':np.uint8, 'VAR_0736':np.uint8, 'VAR_0737':np.uint8, 'VAR_0738':np.uint8, 'VAR_0739':np.uint8,
'VAR_0740':np.uint8, 'VAR_0741':np.uint8, 'VAR_0742':np.uint8, 'VAR_0743':np.uint8, 'VAR_0744':np.uint8, 'VAR_0745':np.uint8, 'VAR_0746':np.uint8,
'VAR_0747':np.uint8, 'VAR_0748':np.uint8, 'VAR_0749':np.uint8, 'VAR_0750':np.uint8, 'VAR_0751':np.uint8, 'VAR_0752':np.uint8, 'VAR_0753':np.uint8,
'VAR_0754':np.uint8, 'VAR_0755':np.uint8, 'VAR_0756':np.uint8, 'VAR_0758':np.uint8, 'VAR_0759':np.uint8, 'VAR_0760':np.uint8, 'VAR_0761':np.uint8,
'VAR_0762':np.uint8, 'VAR_0763':np.uint8, 'VAR_0764':np.uint8, 'VAR_0765':np.uint8, 'VAR_0766':np.uint8, 'VAR_0767':np.uint8, 'VAR_0768':np.uint8,
'VAR_0769':np.uint8, 'VAR_0770':np.uint8, 'VAR_0771':np.uint8, 'VAR_0772':np.uint8, 'VAR_0773':np.uint8, 'VAR_0774':np.uint8, 'VAR_0775':np.uint8,
'VAR_0776':np.uint8, 'VAR_0777':np.uint8, 'VAR_0778':np.uint8, 'VAR_0779':np.uint8, 'VAR_0780':np.uint8, 'VAR_0781':np.uint8, 'VAR_0782':np.uint8,
'VAR_0783':np.uint8, 'VAR_0784':np.uint8, 'VAR_0785':np.uint8, 'VAR_0786':np.uint8, 'VAR_0787':np.uint8, 'VAR_0788':np.uint8, 'VAR_0789':np.uint8,
'VAR_0790':np.uint8, 'VAR_0791':np.uint8, 'VAR_0792':np.uint8, 'VAR_0793':np.uint8, 'VAR_0794':np.uint8, 'VAR_0795':np.uint8, 'VAR_0796':np.uint8,
'VAR_0797':np.uint8, 'VAR_0798':np.uint8, 'VAR_0799':np.uint8, 'VAR_0800':np.uint8, 'VAR_0801':np.uint8, 'VAR_0802':np.uint8, 'VAR_0803':np.uint8,
'VAR_0804':np.uint8, 'VAR_0805':np.uint8, 'VAR_0806':np.uint8, 'VAR_0807':np.uint8, 'VAR_0808':np.uint8, 'VAR_0809':np.uint8, 'VAR_0810':np.uint8,
'VAR_0812':np.uint8, 'VAR_0813':np.uint8, 'VAR_0814':np.uint8, 'VAR_0815':np.uint8, 'VAR_0816':np.uint8, 'VAR_0817':np.uint8, 'VAR_0818':np.uint8,
'VAR_0819':np.uint8, 'VAR_0820':np.uint8, 'VAR_0821':np.uint8, 'VAR_0822':np.uint8, 'VAR_0823':np.uint8, 'VAR_0824':np.uint8, 'VAR_0825':np.uint8,
'VAR_0826':np.uint8, 'VAR_0827':np.uint8, 'VAR_0828':np.uint8, 'VAR_0829':np.uint8, 'VAR_0830':np.uint8, 'VAR_0831':np.uint8, 'VAR_0832':np.uint8,
'VAR_0833':np.uint8, 'VAR_0834':np.uint8, 'VAR_0835':np.uint8, 'VAR_0836':np.uint8, 'VAR_0837':np.uint8, 'VAR_0838':np.uint8, 'VAR_0839':np.uint8,
'VAR_0841':np.uint8, 'VAR_0842':np.uint8, 'VAR_0843':np.uint8, 'VAR_0844':np.uint8, 'VAR_0845':np.uint8, 'VAR_0846':np.uint8, 'VAR_0847':np.uint8,
'VAR_0848':np.uint8, 'VAR_0849':np.uint8, 'VAR_0850':np.uint8, 'VAR_0851':np.uint8, 'VAR_0852':np.uint8, 'VAR_0853':np.uint8, 'VAR_0854':np.uint8,
'VAR_0855':np.uint8, 'VAR_0856':np.uint8, 'VAR_0857':np.uint8, 'VAR_0859':np.uint8, 'VAR_0877':np.uint8, 'VAR_0878':np.uint8, 'VAR_0879':np.uint8,
'VAR_0885':np.uint8, 'VAR_0886':np.uint8, 'VAR_0911':np.uint8, 'VAR_0914':np.uint8, 'VAR_0915':np.uint8, 'VAR_0916':np.uint8, 'VAR_0923':np.uint8,
'VAR_0924':np.uint8, 'VAR_0925':np.uint8, 'VAR_0926':np.uint8, 'VAR_0927':np.uint8, 'VAR_0940':np.uint8, 'VAR_0945':np.uint8, 'VAR_0947':np.uint8,
'VAR_0952':np.uint8, 'VAR_0954':np.uint8, 'VAR_0959':np.uint8, 'VAR_0962':np.uint8, 'VAR_0963':np.uint8, 'VAR_0969':np.uint8, 'VAR_0973':np.uint8,
'VAR_0974':np.uint8, 'VAR_0975':np.uint8, 'VAR_0983':np.uint8, 'VAR_0984':np.uint8, 'VAR_0985':np.uint8, 'VAR_0986':np.uint8, 'VAR_0987':np.uint8,
'VAR_0988':np.uint8, 'VAR_0989':np.uint8, 'VAR_0990':np.uint8, 'VAR_0991':np.uint8, 'VAR_0992':np.uint8, 'VAR_0993':np.uint8, 'VAR_0994':np.uint8,
'VAR_0995':np.uint8, 'VAR_0996':np.uint8, 'VAR_0997':np.uint8, 'VAR_0998':np.uint8, 'VAR_0999':np.uint8, 'VAR_1000':np.uint8, 'VAR_1001':np.uint8,
'VAR_1002':np.uint8, 'VAR_1003':np.uint8, 'VAR_1004':np.uint8, 'VAR_1005':np.uint8, 'VAR_1006':np.uint8, 'VAR_1007':np.uint8, 'VAR_1008':np.uint8,
'VAR_1009':np.uint8, 'VAR_1010':np.uint8, 'VAR_1011':np.uint8, 'VAR_1012':np.uint8, 'VAR_1013':np.uint8, 'VAR_1014':np.uint8, 'VAR_1015':np.uint8,
'VAR_1016':np.uint8, 'VAR_1017':np.uint8, 'VAR_1018':np.uint8, 'VAR_1019':np.uint8, 'VAR_1020':np.uint8, 'VAR_1021':np.uint8, 'VAR_1022':np.uint8,
'VAR_1023':np.uint8, 'VAR_1024':np.uint8, 'VAR_1025':np.uint8, 'VAR_1026':np.uint8, 'VAR_1027':np.uint8, 'VAR_1028':np.uint8, 'VAR_1029':np.uint8,
'VAR_1030':np.uint8, 'VAR_1031':np.uint8, 'VAR_1032':np.uint8, 'VAR_1033':np.uint8, 'VAR_1034':np.uint8, 'VAR_1035':np.uint8, 'VAR_1036':np.uint8,
'VAR_1037':np.uint8, 'VAR_1038':np.uint8, 'VAR_1039':np.uint8, 'VAR_1040':np.uint8, 'VAR_1041':np.uint8, 'VAR_1042':np.uint8, 'VAR_1043':np.uint8,
'VAR_1044':np.uint8, 'VAR_1045':np.uint8, 'VAR_1046':np.uint8, 'VAR_1047':np.uint8, 'VAR_1048':np.uint8, 'VAR_1049':np.uint8, 'VAR_1050':np.uint8,
'VAR_1051':np.uint8, 'VAR_1052':np.uint8, 'VAR_1053':np.uint8, 'VAR_1054':np.uint8, 'VAR_1055':np.uint8, 'VAR_1056':np.uint8, 'VAR_1057':np.uint8,
'VAR_1058':np.uint8, 'VAR_1059':np.uint8, 'VAR_1060':np.uint8, 'VAR_1061':np.uint8, 'VAR_1062':np.uint8, 'VAR_1063':np.uint8, 'VAR_1064':np.uint8,
'VAR_1065':np.uint8, 'VAR_1066':np.uint8, 'VAR_1067':np.uint8, 'VAR_1068':np.uint8, 'VAR_1069':np.uint8, 'VAR_1070':np.uint8, 'VAR_1071':np.uint8,
'VAR_1072':np.uint8, 'VAR_1073':np.uint8, 'VAR_1080':np.uint8, 'VAR_1108':np.uint8, 'VAR_1109':np.uint8, 'VAR_1161':np.uint8, 'VAR_1162':np.uint8,
'VAR_1163':np.uint8, 'VAR_1164':np.uint8, 'VAR_1165':np.uint8, 'VAR_1166':np.uint8, 'VAR_1167':np.uint8, 'VAR_1168':np.uint8, 'VAR_1175':np.uint8,
'VAR_1176':np.uint8, 'VAR_1177':np.uint8, 'VAR_1178':np.uint8, 'VAR_1185':np.uint8, 'VAR_1186':np.uint8, 'VAR_1187':np.uint8, 'VAR_1188':np.uint8,
'VAR_1189':np.uint8, 'VAR_1190':np.uint8, 'VAR_1191':np.uint8, 'VAR_1192':np.uint8, 'VAR_1193':np.uint8, 'VAR_1194':np.uint8, 'VAR_1195':np.uint8,
'VAR_1196':np.uint8, 'VAR_1197':np.uint8, 'VAR_1198':np.uint8, 'VAR_1212':np.uint8, 'VAR_1213':np.uint8, 'VAR_1217':np.uint8, 'VAR_1218':np.uint8,
'VAR_1224':np.uint8, 'VAR_1225':np.uint8, 'VAR_1226':np.uint8, 'VAR_1229':np.uint8, 'VAR_1230':np.uint8, 'VAR_1231':np.uint8, 'VAR_1232':np.uint8,
'VAR_1233':np.uint8, 'VAR_1234':np.uint8, 'VAR_1235':np.uint8, 'VAR_1236':np.uint8, 'VAR_1237':np.uint8, 'VAR_1238':np.uint8, 'VAR_1239':np.uint8,
'VAR_1267':np.uint8, 'VAR_1268':np.uint8, 'VAR_1269':np.uint8, 'VAR_1270':np.uint8, 'VAR_1271':np.uint8, 'VAR_1272':np.uint8, 'VAR_1273':np.uint8,
'VAR_1274':np.uint8, 'VAR_1275':np.uint8, 'VAR_1276':np.uint8, 'VAR_1277':np.uint8, 'VAR_1278':np.uint8, 'VAR_1279':np.uint8, 'VAR_1280':np.uint8,
'VAR_1281':np.uint8, 'VAR_1282':np.uint8, 'VAR_1283':np.uint8, 'VAR_1284':np.uint8, 'VAR_1285':np.uint8, 'VAR_1286':np.uint8, 'VAR_1287':np.uint8,
'VAR_1288':np.uint8, 'VAR_1289':np.uint8, 'VAR_1290':np.uint8, 'VAR_1291':np.uint8, 'VAR_1292':np.uint8, 'VAR_1293':np.uint8, 'VAR_1294':np.uint8,
'VAR_1295':np.uint8, 'VAR_1296':np.uint8, 'VAR_1297':np.uint8, 'VAR_1298':np.uint8, 'VAR_1299':np.uint8, 'VAR_1300':np.uint8, 'VAR_1301':np.uint8,
'VAR_1302':np.uint8, 'VAR_1303':np.uint8, 'VAR_1304':np.uint8, 'VAR_1305':np.uint8, 'VAR_1306':np.uint8, 'VAR_1307':np.uint8, 'VAR_1338':np.uint8,
'VAR_1339':np.uint8, 'VAR_1340':np.uint8, 'VAR_1345':np.uint8, 'VAR_1346':np.uint8, 'VAR_1347':np.uint8, 'VAR_1348':np.uint8, 'VAR_1349':np.uint8,
'VAR_1350':np.uint8, 'VAR_1351':np.uint8, 'VAR_1352':np.uint8, 'VAR_1359':np.uint8, 'VAR_1360':np.uint8, 'VAR_1361':np.uint8, 'VAR_1362':np.uint8,
'VAR_1363':np.uint8, 'VAR_1364':np.uint8, 'VAR_1365':np.uint8, 'VAR_1366':np.uint8, 'VAR_1367':np.uint8, 'VAR_1368':np.uint8, 'VAR_1369':np.uint8,
'VAR_1386':np.uint8, 'VAR_1387':np.uint8, 'VAR_1388':np.uint8, 'VAR_1389':np.uint8, 'VAR_1392':np.uint8, 'VAR_1393':np.uint8, 'VAR_1394':np.uint8,
'VAR_1395':np.uint8, 'VAR_1396':np.uint8, 'VAR_1404':np.uint8, 'VAR_1405':np.uint8, 'VAR_1406':np.uint8, 'VAR_1407':np.uint8, 'VAR_1408':np.uint8,
'VAR_1409':np.uint8, 'VAR_1410':np.uint8, 'VAR_1411':np.uint8, 'VAR_1412':np.uint8, 'VAR_1413':np.uint8, 'VAR_1414':np.uint8, 'VAR_1415':np.uint8,
'VAR_1416':np.uint8, 'VAR_1417':np.uint8, 'VAR_1427':np.uint8, 'VAR_1428':np.uint8, 'VAR_1429':np.uint8, 'VAR_1430':np.uint8, 'VAR_1431':np.uint8,
'VAR_1432':np.uint8, 'VAR_1433':np.uint8, 'VAR_1434':np.uint8, 'VAR_1435':np.uint8, 'VAR_1449':np.uint8, 'VAR_1450':np.uint8, 'VAR_1456':np.uint8,
'VAR_1457':np.uint8, 'VAR_1458':np.uint8, 'VAR_1459':np.uint8, 'VAR_1460':np.uint8, 'VAR_1461':np.uint8, 'VAR_1462':np.uint8, 'VAR_1463':np.uint8,
'VAR_1464':np.uint8, 'VAR_1465':np.uint8, 'VAR_1466':np.uint8, 'VAR_1467':np.uint8, 'VAR_1468':np.uint8, 'VAR_1469':np.uint8, 'VAR_1470':np.uint8,
'VAR_1471':np.uint8, 'VAR_1472':np.uint8, 'VAR_1473':np.uint8, 'VAR_1474':np.uint8, 'VAR_1475':np.uint8, 'VAR_1476':np.uint8, 'VAR_1477':np.uint8,
'VAR_1478':np.uint8, 'VAR_1479':np.uint8, 'VAR_1480':np.uint8, 'VAR_1481':np.uint8, 'VAR_1482':np.uint8, 'VAR_1532':np.uint8, 'VAR_1533':np.uint8,
'VAR_1534':np.uint8, 'VAR_1535':np.uint8, 'VAR_1537':np.uint8, 'VAR_1538':np.uint8, 'VAR_1539':np.uint8, 'VAR_1540':np.uint8, 'VAR_1542':np.uint8,
'VAR_1543':np.uint8, 'VAR_1544':np.uint8, 'VAR_1545':np.uint8, 'VAR_1546':np.uint8, 'VAR_1547':np.uint8, 'VAR_1548':np.uint8, 'VAR_1549':np.uint8,
'VAR_1551':np.uint8, 'VAR_1552':np.uint8, 'VAR_1553':np.uint8, 'VAR_1554':np.uint8, 'VAR_1556':np.uint8, 'VAR_1557':np.uint8, 'VAR_1558':np.uint8,
'VAR_1559':np.uint8, 'VAR_1561':np.uint8, 'VAR_1562':np.uint8, 'VAR_1563':np.uint8, 'VAR_1564':np.uint8, 'VAR_1565':np.uint8, 'VAR_1566':np.uint8,
'VAR_1567':np.uint8, 'VAR_1568':np.uint8, 'VAR_1569':np.uint8, 'VAR_1570':np.uint8, 'VAR_1571':np.uint8, 'VAR_1572':np.uint8, 'VAR_1574':np.uint8,
'VAR_1575':np.uint8, 'VAR_1576':np.uint8, 'VAR_1577':np.uint8, 'VAR_1578':np.uint8, 'VAR_1579':np.uint8, 'VAR_1583':np.uint8, 'VAR_1584':np.uint8,
'VAR_1585':np.uint8, 'VAR_1586':np.uint8, 'VAR_1587':np.uint8, 'VAR_1588':np.uint8, 'VAR_1589':np.uint8, 'VAR_1590':np.uint8, 'VAR_1591':np.uint8,
'VAR_1592':np.uint8, 'VAR_1593':np.uint8, 'VAR_1594':np.uint8, 'VAR_1595':np.uint8, 'VAR_1596':np.uint8, 'VAR_1597':np.uint8, 'VAR_1598':np.uint8,
'VAR_1599':np.uint8, 'VAR_1600':np.uint8, 'VAR_1601':np.uint8, 'VAR_1602':np.uint8, 'VAR_1603':np.uint8, 'VAR_1604':np.uint8, 'VAR_1605':np.uint8,
'VAR_1606':np.uint8, 'VAR_1607':np.uint8, 'VAR_1608':np.uint8, 'VAR_1609':np.uint8, 'VAR_1610':np.uint8, 'VAR_1656':np.uint8, 'VAR_1657':np.uint8,
'VAR_1658':np.uint8, 'VAR_1659':np.uint8, 'VAR_1660':np.uint8, 'VAR_1661':np.uint8, 'VAR_1662':np.uint8, 'VAR_1663':np.uint8, 'VAR_1664':np.uint8,
'VAR_1665':np.uint8, 'VAR_1666':np.uint8, 'VAR_1667':np.uint8, 'VAR_1668':np.uint8, 'VAR_1669':np.uint8, 'VAR_1670':np.uint8, 'VAR_1671':np.uint8,
'VAR_1672':np.uint8, 'VAR_1673':np.uint8, 'VAR_1674':np.uint8, 'VAR_1675':np.uint8, 'VAR_1676':np.uint8, 'VAR_1677':np.uint8, 'VAR_1678':np.uint8,
'VAR_1679':np.uint8, 'VAR_1680':np.uint8, 'VAR_1681':np.uint8, 'VAR_1682':np.uint8, 'VAR_1683':np.uint8, 'VAR_1713':np.uint8, 'VAR_1714':np.uint8,
'VAR_1721':np.uint8, 'VAR_1722':np.uint8, 'VAR_1723':np.uint8, 'VAR_1724':np.uint8, 'VAR_1725':np.uint8, 'VAR_1726':np.uint8, 'VAR_1727':np.uint8,
'VAR_1728':np.uint8, 'VAR_1740':np.uint8, 'VAR_1741':np.uint8, 'VAR_1742':np.uint8, 'VAR_1743':np.uint8, 'VAR_1744':np.uint8, 'VAR_1745':np.uint8,
'VAR_1746':np.uint8, 'VAR_1752':np.uint8, 'VAR_1753':np.uint8, 'VAR_1760':np.uint8, 'VAR_1761':np.uint8, 'VAR_1762':np.uint8, 'VAR_1763':np.uint8,
'VAR_1764':np.uint8, 'VAR_1765':np.uint8, 'VAR_1766':np.uint8, 'VAR_1767':np.uint8, 'VAR_1768':np.uint8, 'VAR_1769':np.uint8, 'VAR_1770':np.uint8,
'VAR_1771':np.uint8, 'VAR_1772':np.uint8, 'VAR_1773':np.uint8, 'VAR_1774':np.uint8, 'VAR_1775':np.uint8, 'VAR_1776':np.uint8, 'VAR_1777':np.uint8,
'VAR_1778':np.uint8, 'VAR_1779':np.uint8, 'VAR_1780':np.uint8, 'VAR_1781':np.uint8, 'VAR_1782':np.uint8, 'VAR_1783':np.uint8, 'VAR_1784':np.uint8,
'VAR_1785':np.uint8, 'VAR_1786':np.uint8, 'VAR_1787':np.uint8, 'VAR_1788':np.uint8, 'VAR_1789':np.uint8, 'VAR_1790':np.uint8, 'VAR_1791':np.uint8,
'VAR_1792':np.uint8, 'VAR_1793':np.uint8, 'VAR_1794':np.uint8, 'VAR_1843':np.uint8, 'VAR_1844':np.uint8, 'VAR_1853':np.uint8, 'VAR_1854':np.uint8,
'VAR_1855':np.uint8, 'VAR_1856':np.uint8, 'VAR_1857':np.uint8, 'VAR_1866':np.uint8, 'VAR_1867':np.uint8, 'VAR_1872':np.uint8, 'VAR_1873':np.uint8,
'VAR_1874':np.uint8, 'VAR_1875':np.uint8, 'VAR_1876':np.uint8, 'VAR_1877':np.uint8, 'VAR_1878':np.uint8, 'VAR_1879':np.uint8, 'VAR_1880':np.uint8,
'VAR_1881':np.uint8, 'VAR_1882':np.uint8, 'VAR_1883':np.uint8, 'VAR_1884':np.uint8, 'VAR_1885':np.uint8, 'VAR_1886':np.uint8, 'VAR_1887':np.uint8,
'VAR_1888':np.uint8, 'VAR_1903':np.uint8, 'VAR_1904':np.uint8, 'VAR_1905':np.uint8, 'VAR_1906':np.uint8, 'VAR_1907':np.uint8, 'VAR_1908':np.uint8,
'VAR_1909':np.uint8, 'VAR_1910':np.uint8, 'VAR_1920':np.uint8, 'VAR_1921':np.uint8, 'VAR_1925':np.uint8, 'VAR_1926':np.uint8, 'VAR_1927':np.uint8,
'VAR_1928':np.uint16, 'VAR_1930':np.uint16}<load_from_csv>
def predict_on_video(model, model2, model3, model4, model5, model6, model7, model8, model9, model10, model11, video_path):
try:
x, x_sqr, x_299_sqr, x_lstm, frame_skip = extract_frames(video_path)
 if x is None or x_sqr is None: return 0.5
else:
with torch.no_grad() :
y_pred = model(x.to(device))
y_pred = torch.sigmoid(y_pred.squeeze().mean())
 y_pred2 = model2(x_sqr.to(device))
y_pred2 = torch.sigmoid(y_pred2.squeeze().mean())
 y_pred3 = model3(x_sqr.to(device))
y_pred3 = torch.sigmoid(y_pred3.squeeze().mean())
 y_pred4 = model4(x_299_sqr.to(device))
y_pred4 = torch.sigmoid(y_pred4.squeeze().mean())
 y_pred5 = model5(x.to(device))
y_pred5 = torch.sigmoid(y_pred5.squeeze().mean())
 y_pred6 = model6(x_sqr.to(device))
y_pred6 = torch.sigmoid(y_pred6.squeeze().mean())
 seq_len = len(x_lstm)//2
x_sqr_1, x_sqr_2 = x_lstm[:seq_len], x_lstm[seq_len:]
skip = max(1, seq_len//15)
 start_indices = list(range(0, seq_len, skip))
gap = max(1, int(np.round(20/frame_skip)))
 for ss, start in enumerate(start_indices):
indices = list(range(start, 500, gap))
indices = [i%seq_len for i in indices]
indices = indices[:CFG.seq_len]
seqs1, seqs2 = x_sqr_1[indices].unsqueeze(0), x_sqr_2[indices].unsqueeze(0)
 if ss == 0:
batch_seqs_1 = seqs1
batch_seqs_2 = seqs2
else:
batch_seqs_1 = torch.cat(( batch_seqs_1, seqs1), 0)
 batch_seqs_2 = torch.cat(( batch_seqs_2, seqs2), 0)
 batch_seqs = torch.cat(( batch_seqs_1, batch_seqs_2), 0)
 y_pred7 = model7(batch_seqs.to(device))
y_pred7 = torch.sigmoid(y_pred7.squeeze().mean())
 y_pred8 = model8(batch_seqs.to(device))
y_pred8 = torch.sigmoid(y_pred8.squeeze().mean())
 y_pred9 = model9(x_sqr.to(device))
y_pred9 = torch.sigmoid(y_pred9.squeeze().mean())
 y_pred10 = model10(x_299_sqr.to(device))
y_pred10 = torch.sigmoid(y_pred10.squeeze().mean())
 y_pred11 = model11(x.to(device))
y_pred11 = torch.sigmoid(y_pred11.squeeze().mean())
 w = [3, 2, 8, 2, 2, 2, 4, 4, 2, 2, 3]
return(w[0]*y_pred.item() + w[1]*y_pred2.item() + w[2]*y_pred3.item() + w[3]*y_pred4.item()
+ w[4]*y_pred5.item() + w[5]*y_pred6.item() +
w[6]*y_pred7.item() + w[7]*y_pred8.item() +
w[8]*y_pred9.item() + w[9]*y_pred10.item() + w[10]*y_pred11.item())/sum(w)
 except Exception as e:
print("Prediction error on video", e)
 return 0.5
return 0.5
train = pd.read_csv(".. /input/springleaf-marketing-response/train.csv.zip")
 mixCol = [8,9,10,11,12,18,19,20,21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 38, 39, 40, 41, 42, 43, 44, 45,
73, 74, 98, 99, 100, 106, 107, 108, 156, 157, 158, 159, 166, 167, 168, 169, 176, 177, 178, 179, 180,
181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 202, 205, 206, 207,
208, 209, 210, 211, 212, 213, 214, 215, 216, 218, 219, 220, 221, 222, 223, 224, 225, 240, 371, 372, 373, 374,
375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395,
396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436,
437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457,
458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478,
479, 480, 481, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509,
510, 511, 512, 513, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 840]
alphaCol = [283, 305, 325, 352, 353, 354, 1934]
placeCol = [200, 274, 342]
dtCol = [75, 204, 217]
selectColumns = []
rmCol = mixCol+alphaCol+placeCol+dtCol
for i in range(1,1935):
if i not in rmCol:
selectColumns.append(i)
 cols = [str(n ).zfill(4)for n in selectColumns]
strColName = ['VAR_' + strNum for strNum in cols]<load_from_csv>
class FastMTCNN(object):
def __init__(self, resize=1, *args, **kwargs):
self.resize = resize
self.mtcnn = MTCNN(*args, **kwargs)
 def __call__(self, frames):
if self.resize != 1:
frames = [f.resize([int(d * self.resize)for d in f.size])for f in frames]
boxes, probs = self.mtcnn.detect(frames)
 boxes = [b.astype(int ).tolist() if b is not None and type(b)==np.ndarray else [] for b in boxes]
probs = [b.tolist() if b is not None and type(b)==np.ndarray else [] for b in probs]
return boxes, probs
fast_mtcnn = FastMTCNN(
 resize=SCALE,
margin=14,
keep_all=True,
device=device
)
nrows = 500
trainData = pd.read_csv(".. /input/springleaf-marketing-response/train.csv.zip", skiprows=[107], usecols=strColName, nrows=nrows, dtype=types)
 label = pd.read_csv(".. /input/springleaf-marketing-response/train.csv.zip", skiprows=[107], usecols=['target'], nrows=nrows)
 testData = pd.read_csv(".. /input/springleaf-marketing-response/test.csv.zip", skiprows=[107], usecols=strColName, nrows=nrows)
 numericFeatures = trainData._get_numeric_data()
removeNA = numericFeatures.fillna(0)
 sel = VarianceThreshold(threshold= (.8 *(1 -.8)))
 features = sel.fit_transform(removeNA)
 y = np.array(label ).ravel()
X_scaled = preprocessing.scale(features)
 normalizer = preprocessing.Normalizer().fit(X_scaled)
 X_norm = normalizer.transform(X_scaled)
 X_train, X_test, y_train, y_test = train_test_split(X_norm, y, test_size=0.33, random_state=42)
 y_train = np.array(y_train)
<train_model>
test_videos = sorted([x for x in os.listdir(TEST_DIR)if x[-4:] == ".mp4"])
 len(test_videos )
clf = svm.SVC(C=1.0, kernel='linear', degree=10, gamma=1.00, coef0=0.0, shrinking=True, probability=False,
tol=0.001, cache_size=1000, class_weight=None, verbose=False, max_iter=-1, random_state=None)
 clf.fit(X_train, y_train)
 predictions = clf.predict(X_test)
 print('roc_auc_score', roc_auc_score(y_test, predictions))
print('RMSE', mean_squared_error(y_test, predictions))<train_on_grid>
def predict_on_video_set(model, model2, model3, model4, model5, model6, model7, model8, model9, model10, model11, videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(model, model2, model3, model4, model5, model6, model7, model8, model9, model10, model11, os.path.join(TEST_DIR, filename))
print(i, y_pred)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions)
 predictions = predict_on_video_set(net, net2, net3, net4, net5, net6, net7, net8, net9, net10, net11, test_videos, num_workers=NUM_WORKERS )
testData = pd.read_csv(".. /input/springleaf-marketing-response/test.csv.zip", usecols=strColName, engine='python', dtype=types)
 numericFeatures = testData._get_numeric_data()
removeNA = numericFeatures.fillna(0)
 features = sel.transform(removeNA)
 y = np.array(label ).ravel()
X_scaled = preprocessing.scale(features)
 normalizer = preprocessing.Normalizer().fit(X_scaled)
 X_norm = normalizer.transform(X_scaled)
<predict_on_test>
predictions = np.clip(predictions, 0.005, 0.995)
 submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )