Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<feature_engineering>
%matplotlib inline
warnings.filterwarnings("ignore" )
def Dn(df,cols):
for col in cols:
df[col+"n"] = df["DayNum"] - df[col]<define_variables>
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 )
d_cols = ['D1',"D2", 'D3', 'D4', 'D5', 'D6', 'D7', "D8", "D9", 'D10', 'D11', "D12",
'D13', 'D14', 'D15']
Dn(xs,d_cols)
 Dn(test_xs,d_cols)
 d_cols = [string +"n" for string in d_cols]
cols +=d_cols<data_type_conversions>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
def encode_CB2(df1,df2,uid):
newcol = "_".join(uid)
 df1[newcol] = df1[uid].astype(str ).apply(lambda x: '_'.join(x), axis=1)
 df2[newcol] = df2[uid].astype(str ).apply(lambda x: '_'.join(x), axis=1)
 temp_df = pd.concat([df1[newcol],df2[newcol]],axis=0)
 temp_df,_ = temp_df.factorize(sort=True)
 if temp_df.max() >32000:
df1[newcol+"_fact"] = temp_df[:len(df1)].astype('int32')
 df2[newcol+"_fact"] = temp_df[len(df1):].astype('int32')
 else:
df1[newcol+"_fact"] = temp_df[:len(df1)].astype('int16')
 df2[newcol+"_fact"] = temp_df[len(df1):].astype('int16')
 print(newcol+"_fact")
 return [newcol+"_fact"]<categorify>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
def encode_ag(df,df_te,uid,cols_ag,func_list,ag=True):
new_cols_ret = []
temp_df = pd.concat([df[uid+cols_ag],df_te[uid+cols_ag]],axis=0 ).reset_index(drop=True)
 temp_df[uid] = temp_df[uid].fillna(-9999)
 grouped = temp_df.groupby(uid)
 for func in func_list:
temp2_df = grouped[cols_ag].transform(func ).reset_index(drop=True)
 if ag:
new_cols = [col+"_"+"_".join(uid)+"_"+func+"2" for col in cols_ag]
else:
new_cols = ["_".join(uid)+"_FE2" for col in cols_ag]
new_cols_ret +=new_cols
df[new_cols] = temp2_df[0:len(df)].fillna(-9999 ).astype('float32')
 df_te[new_cols] = temp2_df[len(df):].fillna(-9999 ).reset_index(drop=True ).astype('float32')
 print(new_cols_ret)
 return new_cols_ret<count_missing_values>
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 )
uid = ["D1n","card1","addr1","P_emaildomain"]
print(xs[uid].isna().sum())
<categorify>
frames_per_video = 81
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 )
cols+= encode_CB2(xs,test_xs,["card1","addr1"])
 cols+= encode_CB2(xs,test_xs,uid)
 cols += encode_ag(xs,test_xs,["addr1"],["TransactionID"],["count"],ag=False)
 cols += encode_ag(xs,test_xs,["card1"],["TransactionID"],["count"],ag=False)
 cols += encode_ag(xs,test_xs,["card2"],["TransactionID"],["count"],ag=False)
 cols += encode_ag(xs,test_xs,["dist1"],["TransactionID"],["count"],ag=False)
 cols += encode_ag(xs,test_xs,["card1","addr1"],["TransactionID"],["count"],ag=False)
 cols += encode_ag(xs,test_xs,uid,["TransactionID"],["count"],ag=False)
 cols += encode_ag(xs,test_xs,["card1","D1n"],["TransactionID"],["count"],ag=False)
 cols += encode_ag(xs,test_xs,["P_emaildomain"],["TransactionID"],["count"],ag=False)
 cols += encode_ag(xs,test_xs,["TransactionAmt"],["TransactionID"],["count"],ag=False)
 cols += encode_ag(xs,test_xs,["cents"],["TransactionID"],["count"],ag=False)
 cols += encode_ag(xs,test_xs,["ProductCD"],["TransactionID"],["count"],ag=False)
 cols += encode_ag(xs,test_xs,["HrOfDay"],["TransactionID"],["count"],ag=False)
 cols_ag_av = ["D3n","M7","D9","D10n","D13n","D5n","D15n","D4n","D6n","D11n","HrOfDay","D14n","id_13"]
cols_ag_other = ["TransactionAmt","cents"]
cols_C = ["C"+str(i)for i in range(1,15)]
cols_M = ['M'+str(x)for x in range(1,10)if x!= 7]
cols += encode_ag(xs,test_xs,uid,cols_ag_av+cols_ag_other, ["std","mean"])
 cols += encode_ag(xs,test_xs,["card1","addr1","P_emaildomain"],cols_ag_av+cols_ag_other, ["std","mean"])
 cols += encode_ag(xs,test_xs,uid,cols_C, ["std","mean"])
 cols_ag_v = ["V9","V314","V3","V171","V81","V289","V88","V89","V271","V326","V7","V52","V1"]
cols += encode_ag(xs,test_xs,uid,cols_ag_v, ["std","mean"])
 cols_ag_numb = ["addr2","ProductCD","dist1","DeviceType"]
cols += encode_ag(xs,test_xs,uid,cols_ag_numb+cols_M, ["nunique"] )<concatenate>
input_size = 224
cols = set_approach(cols,["DayNum"] )<find_best_params>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
i=4
if i==4: get_feat_imp=True
get_feat_imp<prepare_x_and_y>
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 )
msk = np.arange(len(xs)) <0.8*len(xs)
 idxT = list(np.where(msk)[0])
 idxV = list(np.where(~msk)[0] )<categorify>
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
%%time
if buildLv:
get_feat_imp=False
n=5
preds = np.zeros(len(test_xs))
auc_lst = []
for i in range(n):
print("iteation",i)
 if i==4: get_feat_imp=True
a,temp = xgb_lvd_fun(get_feat_imp)
 preds += temp/n
auc_lst.append(a)
 del temp; x=gc.collect()
auc_lst = np.array(auc_lst)
 print(auc_lst.mean(axis=0))
print(auc_lst.std(axis=0))
format_test_preds(preds,test_xs,"baselineLV")
<predict_on_test>
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.5
%%time
if buildSkf:
preds = xgb_pred_fun()
format_test_preds(preds,test_xs,"baseline")
<split>
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 )
if AV:
df_dom = pd.concat([xs[cols], test_xs[cols]])
 is_test = pd.DataFrame([0]*len(xs)+ [1]*len(test_xs))
idxT,idxV = train_test_split(np.arange(len(df_dom)) ,test_size=0.2)
 del xs, test_xs; x=gc.collect()
clf = xgb.XGBClassifier(n_estimators=1000,
max_depth=8,
learning_rate= 0.05,
subsample= 0.6,
colsample_bytree= 0.4,
random_state = r.randint(0,9999),
use_label_encoder=False,
tree_method='gpu_hist')
 m = clf.fit(df_dom.iloc[idxT], is_test.iloc[idxT], eval_set=[(df_dom[cols].iloc[idxV],is_test.iloc[idxV])],
eval_metric= "auc", verbose=100, early_stopping_rounds=100)
 fi = xgb_fi(m, df_dom,df_dom)
 fi.to_csv("fi_av.csv",index=False)
 fi<init_hyperparams>
speed_test = False
GENES_REGEX = '^g-*|^c-'
N_SPLITS = 3
BATCH_SIZE=128
EPOCHS=200
LEARNING_RATE = 4e-1
LABEL_SMOOTHING = 1e-5
RLR_PATIENCE = 4
RLR_FACTOR =.7
ES_PATIENCE = 20 
<define_search_space>
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )
REGULARIZATION = 1
INPUT_SHAPE = 729
AUTOCOORD_L1, AUTOCOORD_L2 = 1e-3, 1e-2
AUTO_SIZE = 9**3
RESHAPE_SIZE =(9,9,9,1)
 AUTO_DROPOUT = 0
CONV_FILTER = 2
CONV_DROPOUT =.2
RESBLOCK_L1, RESBLOCK_L2 = 1e-12, 1e-11
DENSE_L1, DENSE_L2 = 1e-9, 1e-8
DENSE_DROPOUT =.4
OUTPUT_SIZE = 206 
<load_from_csv>
predictions = predict_on_video_set(test_videos, num_workers=4 )
def head(numpy_array):
return pd.DataFrame(numpy_array)
 train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_features.head()
genes = train_features.filter(regex=(GENES_REGEX)).to_numpy()
head(genes)
 targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv' ).drop(['sig_id'],axis=1 ).to_numpy()
non_scored_targets = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv' ).drop(['sig_id'],axis=1 ).to_numpy()
drug_targets = pd.get_dummies(pd.read_csv('.. /input/lish-moa/train_drug.csv' ).drop(['sig_id'],axis=1)).to_numpy()
all_targets = np.concatenate([targets, non_scored_targets], axis=-1)
 train_features.head()<categorify>
submission_df_resnext = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_resnext.to_csv("submission_resnext.csv", index=False )
def cleanFeatures(features):
features_ = features.copy()
cp_type = {'trt_cp': 0, 'ctl_vehicle': 1}
cp_dose = {'D1': 0, 'D2': 1}
features_['cp_type'] = features_['cp_type'].map(cp_type)
 features_['cp_dose'] = features_['cp_dose'].map(cp_dose)
 features_ = pd.get_dummies(features_, columns=['cp_time'])
 features_.drop(['sig_id'], inplace=True, axis=1)
 return features_
X_clean = cleanFeatures(train_features)
 X_clean<compute_test_metric>
!pip install.. /input/deepfake-xception-trained-model/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet
def rankGauss(x):
N = x.shape[0]
temp = x.argsort(axis=1)
 rank_x = temp.argsort(axis=1)/ N
rank_x -= rank_x.mean()
rank_x *= 2
efi_x = erfinv(rank_x)
 efi_x -= efi_x.mean()
return efi_x<data_type_conversions>
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 )
REGEX = '^cp_*'
X = rankGauss(genes)
 other_X = X_clean.filter(regex=(REGEX)).to_numpy()
X.shape, other_X.shape<import_modules>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
<split>
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 )
copyfile(src = ".. /input/minimalistic-v2/ml_stratifiers.py", dst = ".. /working/ml_stratifiers.py")
 def getMskfDataset(X, other_X, y, all_y, n_splits=N_SPLITS, SEED=7):
mskf = MultilabelStratifiedKFold(n_splits=n_splits, random_state=SEED, shuffle=True)
 for i,(train_idx, val_idx)in enumerate(mskf.split(X, y)) :
X_train, other_X_train, y_train = X[train_idx], other_X[train_idx], y[train_idx]
X_val, other_X_val, y_val = X[val_idx], other_X[val_idx], y[val_idx]
X_train_0, other_X_train_0, y_train_0 = X[train_idx], other_X[train_idx], all_y[train_idx]
X_val_0, other_X_val_0, y_val_0 = X[val_idx], other_X[val_idx], all_y[val_idx]
X_train_1, other_X_train_1, y_train_1 = X[train_idx], other_X[train_idx], y[train_idx]
X_val_1, other_X_val_1, y_val_1 = X[val_idx], other_X[val_idx], y[val_idx]
yield X_train_0, other_X_train_0, y_train_0, X_val_0, other_X_val_0, y_val_0, X_train_1, other_X_train_1, y_train_1, X_val_1, other_X_val_1, y_val_1<categorify>
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 )
<choose_model_class>
input_size = 150
class AutoCoordBlock(tf.keras.layers.Layer):
def __init__(self, dense_size=AUTO_SIZE, trainable=True):
super(AutoCoordBlock, self ).__init__()
self.dense_size= dense_size
self.l2 = tf.keras.regularizers.L2(l2=AUTOCOORD_L1)
 self.l1 = tf.keras.regularizers.L1(l1=AUTOCOORD_L2*1e-7)
 self.dense = tf.keras.layers.Dense(self.dense_size, kernel_regularizer=self.l2 ,bias_regularizer=self.l1, trainable=trainable)
 self.batch_norm = tf.keras.layers.BatchNormalization(trainable=trainable)
 self.relu = tf.keras.layers.ReLU()
self.dropout = tf.keras.layers.Dropout(AUTO_DROPOUT)
 self.reshape= tf.keras.layers.Reshape(RESHAPE_SIZE)
 @tf.function
def call(self, x, training=False):
x = self.dense(x, training=training)
 x = self.batch_norm(x,training=training)
 x = self.relu(x, training=training)
 x = self.reshape(x, training=training)
 x = self.dropout(x, training=training)
 return x
class ResBlock(tf.keras.layers.Layer):
def __init__(self, pre_filter_size, k3_filter_size, k1_filter_size, padding='same', is_first=False, trainable=True):
super(ResBlock, self ).__init__()
self.padding = padding
self.l2 = tf.keras.regularizers.L2(l2=RESBLOCK_L1)
 self.l1 = tf.keras.regularizers.L1(l1=RESBLOCK_L2)
 self.is_first = is_first
self.dropout = tf.keras.layers.Dropout(CONV_DROPOUT)
 if not is_first:
self.pre_filter_size = pre_filter_size
self.pre_conv = tf.keras.layers.Conv3D(self.pre_filter_size,(1,1,1), padding=self.padding, kernel_regularizer=self.l2 ,bias_regularizer=self.l1, trainable=trainable)
 self.batch_norm_pre = tf.keras.layers.BatchNormalization(trainable=trainable)
 self.relu_pre = tf.keras.layers.ReLU()
self.k1_filter_size = k1_filter_size
self.conv_k1 = tf.keras.layers.Conv3D(self.k1_filter_size,(5,5,5), padding=self.padding, kernel_regularizer=self.l2 ,bias_regularizer=self.l1, trainable=trainable)
 self.batch_norm_k1 = tf.keras.layers.BatchNormalization(trainable=trainable)
 self.relu_k1 = tf.keras.layers.ReLU()
self.k3_filter_size = k3_filter_size
self.conv_k3 = tf.keras.layers.Conv3D(self.k3_filter_size,(3,3,3), padding=self.padding, kernel_regularizer=self.l2 ,bias_regularizer=self.l1, trainable=trainable)
 self.batch_norm_k3 = tf.keras.layers.BatchNormalization(trainable=trainable)
 self.relu_k3 = tf.keras.layers.ReLU()
self.concat = tf.keras.layers.Concatenate(axis=-1)
 @tf.function
def call(self, x, training=False):
if not self.is_first:
x = self.pre_conv(x, training=training)
 x = self.batch_norm_pre(x,training=training)
 x = self.relu_pre(x, training=training)
 x = self.dropout(x, training = training)
 x_k1 = self.conv_k1(x, training=training)
 x_k1 = self.batch_norm_k1(x_k1, training=training)
 x_k1 = self.relu_k1(x_k1, training=training)
 x_k1 = self.dropout(x, training = training)
 x_k3 = self.conv_k3(x, training=training)
 x_k3 = self.batch_norm_k3(x_k3,training=training)
 x_k3 = self.relu_k3(x_k3, training=training)
 x_k3 = self.dropout(x, training = training)
 if not self.is_first:
x_ = self.concat([x_k3, x_k1, x])
 else:
x_ = self.concat([x_k3, x])
 return x_
class ReduceBlock(tf.keras.layers.Layer):
def __init__(self, trainable=True):
super(ReduceBlock, self ).__init__()
self.l2 = tf.keras.regularizers.L2(l2=RESBLOCK_L1)
 self.l1 = tf.keras.regularizers.L1(l1=RESBLOCK_L2)
 self.padding = 'valid'
self.flatten = tf.keras.layers.Flatten()
self.dropout = tf.keras.layers.Dropout(CONV_DROPOUT)
 self.filter_size_0 = 8*CONV_FILTER
self.conv_0 = tf.keras.layers.Conv3D(self.filter_size_0,(3,3,3), padding=self.padding, kernel_regularizer=self.l2 ,bias_regularizer=self.l1, trainable=trainable)
 self.batch_norm_0 = tf.keras.layers.BatchNormalization(trainable=trainable)
 self.relu_0 = tf.keras.layers.ReLU()
self.filter_size_1 = 16*CONV_FILTER
self.conv_1 = tf.keras.layers.Conv3D(self.filter_size_1,(3,3,3), padding=self.padding, kernel_regularizer=self.l2 ,bias_regularizer=self.l1, trainable=trainable)
 self.batch_norm_1 = tf.keras.layers.BatchNormalization(trainable=trainable)
 self.relu_1 = tf.keras.layers.ReLU()
self.filter_size_2 = 32*CONV_FILTER
self.conv_2 = tf.keras.layers.Conv3D(self.filter_size_2,(3,3,3), padding=self.padding, kernel_regularizer=self.l2 ,bias_regularizer=self.l1, trainable=trainable)
 self.batch_norm_2 = tf.keras.layers.BatchNormalization(trainable=trainable)
 self.relu_2 = tf.keras.layers.ReLU()
self.concat = tf.keras.layers.Concatenate(axis=-1)
 @tf.function
def call(self, x, training=False):
x_ = self.flatten(x)
 x = self.conv_0(x, training = training)
 x = self.batch_norm_0(x, training = training)
 x = self.relu_0(x, training = training)
 x = self.dropout(x, training = training)
 x = self.conv_1(x, training = training)
 x = self.batch_norm_1(x, training = training)
 x = self.relu_1(x, training = training)
 x = self.dropout(x, training = training)
 x = self.conv_2(x, training = training)
 x = self.batch_norm_2(x, training = training)
 x = self.relu_2(x, training = training)
 x = self.dropout(x, training = training)
 x = self.flatten(x)
 return self.concat([x, x_])
 class DenseBlock(tf.keras.layers.Layer):
def __init__(self, dense_size=512, x_is_flat=False, dropout=DENSE_DROPOUT):
super(DenseBlock, self ).__init__()
self.l2 = tf.keras.regularizers.L2(l2=DENSE_L1)
 self.l1 = tf.keras.regularizers.L1(l1=DENSE_L2)
 self.x_is_flat = x_is_flat
if not x_is_flat:
self.flatten = tf.keras.layers.Flatten()
self.concat = tf.keras.layers.Concatenate(axis=1)
 self.dense = tf.keras.layers.Dense(dense_size, kernel_regularizer=self.l2 ,bias_regularizer=self.l1)
 self.batch_norm = tf.keras.layers.BatchNormalization()
self.relu = tf.keras.layers.ReLU()
self.dropout = tf.keras.layers.Dropout(dropout)
 @tf.function
def call(self, x, other_x, training=False):
if not self.x_is_flat:
x_ = self.concat([self.flatten(x), tf.cast(other_x, 'float')])
 else:
x_ = self.concat([x, tf.cast(other_x, 'float')])
 x_ = self.dense(x_, training=training)
 x_ = self.batch_norm(x_,training=training)
 x_ = self.relu(x_, training=training)
 x_ = self.dropout(x_, training=training)
 return x_ 
<choose_model_class>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
class AutoCoordNet(tf.keras.Model):
def __init__(self, output_size=OUTPUT_SIZE, everything_trainable=True):
super(AutoCoordNet, self ).__init__()
self.auto_block = AutoCoordBlock(trainable=everything_trainable)
 self.res_block_0 = ResBlock(None, 1*CONV_FILTER, None, is_first=True, trainable=everything_trainable)
 self.res_block_1 = ResBlock(2*CONV_FILTER, 2*CONV_FILTER, 1*CONV_FILTER, trainable=everything_trainable)
 self.res_block_2 = ResBlock(4*CONV_FILTER, 4*CONV_FILTER, 2*CONV_FILTER, trainable=everything_trainable)
 self.reduce_block = ReduceBlock(trainable=everything_trainable)
 self.dense0 = DenseBlock(x_is_flat=True)
 self.dense1 = DenseBlock(dense_size=256, x_is_flat=True)
 self.output_layer = tf.keras.layers.Dense(output_size, activation='sigmoid')
 self.history = {'loss':[], 'val_loss':[] }
self.loss_metrics = tf.keras.metrics.BinaryCrossentropy()
def train(self, train_ds, validation_ds, epochs, reduce_lr, early_stopping):
self.onTrainBegin(reduce_lr, early_stopping)
 for epoch in range(1,epochs+1):
self.loss_metrics.reset_states()
start_time = time.time()
for step,(x_batch, other_x_batch, y_batch)in enumerate(train_ds):
self.train_step(x_batch, other_x_batch, y_batch)
 loss = self.loss_metrics.result().numpy()
self.history['loss'].append(loss)
 self.loss_metrics.reset_states()
for(val_x_batch, val_other_x_batch, val_y_batch)in validation_ds:
self.val_step(val_x_batch, val_other_x_batch, val_y_batch)
 val_loss = self.loss_metrics.result().numpy()
self.history['val_loss'].append(val_loss)
 print(f"Epoch {epoch}/{epochs} -","loss: %.4f,"%(float(loss)) ,"val_loss: %.4f,"%(float(val_loss)) , "time spent: %.2fs."%(time.time() -start_time))
self.onEpochEnd(epoch, val_loss)
 if self.stop_training == True:
return self.history
return self.history
def predict(self, ds):
y = []
for(x_batch, other_x_batch)in ds:
logits = self(x_batch, other_x_batch, training=False)
 y.extend(logits.numpy())
 return np.array(y)
 @tf.function
def train_step(self, x, other_x, y):
with tf.GradientTape() as tape:
y_pred = self(x, other_x, training=True)
 loss = self.compiled_loss(y, y_pred, regularization_losses=self.losses)
 gradients = tape.gradient(loss, self.trainable_variables)
 self.optimizer.apply_gradients(zip(gradients, self.trainable_variables))
self.loss_metrics.update_state(y, y_pred)
 @tf.function
def val_step(self, x, other_x, y):
val_logits = self(x, other_x, training=False)
 self.loss_metrics.update_state(y, val_logits)
 @tf.function
def call(self, x, other_x, training=False):
x = self.auto_block(x, training=training)
 x = self.res_block_0(x, training=training)
 x = self.res_block_1(x, training=training)
 x = self.res_block_2(x, training=training)
 x = self.reduce_block(x, training=training)
 x = self.dense0(x, other_x, training=training)
 x = self.dense1(x, other_x, training=training)
 return self.output_layer(x, training=training)
 @tf.function
def onTrainBegin(self, reduce_lr, early_stopping):
self.reduce_lr, self.early_stopping = reduce_lr, early_stopping
self.reduce_lr.set_model(self), self.early_stopping.set_model(self)
 self.reduce_lr.on_train_begin() , self.early_stopping.on_train_begin()
def auto_train(self, trainable):
self.auto_block.trainable = trainable
def res_train(self, trainable):
self.res_block_0.trainable = trainable
self.res_block_1.trainable = trainable
self.res_block_2.trainable = trainable
self.reduce_block.trainable = trainable
def dense_train(self, trainable):
self.dense0.trainable = trainable
self.dense1.trainable = trainable
self.output_layer.trainable = trainable
def onEpochEnd(self, epoch, val_loss):
logs = {'val_loss': val_loss}
self.reduce_lr.on_epoch_end(epoch,logs), self.early_stopping.on_epoch_end(epoch,logs)
 def setOutputSize(self, size):
self.output_layer = tf.keras.layers.Dense(size, activation='sigmoid' )<load_from_csv>
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 )
test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 test_genes = test_features.filter(regex=(GENES_REGEX)).to_numpy()
test_clean = cleanFeatures(test_features)
 X_test = rankGauss(test_genes)
 other_X_test = test_clean.filter(regex=(REGEX)).to_numpy()
test_ds = tf.data.Dataset.from_tensor_slices(( X_test, other_X_test)).batch(BATCH_SIZE)
<choose_model_class>
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.5
def getCallbacks() :
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(
 monitor='val_loss',
mode='min',
factor=RLR_FACTOR,
patience=RLR_PATIENCE,
verbose=2)
 early_stopping = tf.keras.callbacks.EarlyStopping(
 monitor='val_loss',
mode='min',
patience=ES_PATIENCE,
restore_best_weights=True,
verbose=2)
 return reduce_lr, early_stopping<split>
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 )
def getInfer() :
stratified_ds_generator = getMskfDataset(X, other_X, targets, all_targets)
 preds=[]
i = 0
for(X_train_0, other_X_train_0, y_train_0, X_val_0, other_X_val_0, y_val_0, X_train_1, other_X_train_1, y_train_1, X_val_1, other_X_val_1, y_val_1)in stratified_ds_generator:
i+=1
print(f'FOLD {i} starting...')
 train_ds_1 = tf.data.Dataset.from_tensor_slices(( X_train_1, other_X_train_1, y_train_1)).batch(BATCH_SIZE)
 val_ds_1 = tf.data.Dataset.from_tensor_slices(( X_val_1, other_X_val_1, y_val_1)).batch(BATCH_SIZE)
 model_1 = AutoCoordNet()
model_1.setOutputSize(OUTPUT_SIZE)
 model_1.compile(loss = tf.keras.losses.BinaryCrossentropy(label_smoothing = LABEL_SMOOTHING),
optimizer = tf.keras.optimizers.SGD(learning_rate=LEARNING_RATE, momentum=.9))
rlr, es = getCallbacks()
model_1.train(train_ds_1, val_ds_1, epochs=EPOCHS, reduce_lr=rlr, early_stopping=es)
 plotHistory(model_1)
 pred = model_1.predict(test_ds)
 preds.append(pred)
 infer = np.zeros(( test_features.shape[0],206))
for pred_ in preds:
infer += pred_
infer /= N_SPLITS
return infer<feature_engineering>
speed_test = False
infer = getInfer()
ctl_idx = np.where(test_features.iloc[:,1].to_numpy() == 'ctl_vehicle')
 infer[ctl_idx] = 0<save_to_csv>
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )
sub = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 sub.iloc[:,1:] = infer
sub.to_csv('submission.csv', index=False )<import_modules>
%%time
model.eval()
predictions = predict_on_video_set(test_videos, num_workers=4 )
!pip install -q efficientnet >> /dev/null
DATA_SETS_2020 = {'train' : '.. /input/siim-isic-melanoma-classification/train.csv',
'test' : '.. /input/siim-isic-melanoma-classification/test.csv',
'sub' : '.. /input/siim-isic-melanoma-classification/sample_submission.csv'}
SEED = 13
FOLDS = 5
IMG_SIZES = [384] * FOLDS
INC2019 = [True] * FOLDS
INC2018_2017 = [True] * FOLDS
SEGMENTED = False
BATCH_SIZES = [32] * FOLDS
EPOCHS = [15] * FOLDS
EFF_NETS = [6] * FOLDS
LR_MIN = 1e-6
VERBOSE = False
WGTS = [1/FOLDS] * FOLDS
ROT_ = 180.0
SHR_ = 8.0
HZOOM_ = 14.0
WZOOM_ = 14.0
HSHIFT_ = 14.0
WSHIFT_ = 14.0
ADD_HAIR_PROB = 0.5
ADD_HAIR = True
N_HAIRS = 10
CUTOUT_PROB = 0.25
CUTOUT = True
SOLARIZE_PROB = 0.25
EQUALIZE_PROB = 0.25
TTA = 11
DEVICE = 'TPU'
print("connecting to TPU...")
 if DEVICE == "TPU":
print("connecting to TPU...")
 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
print("Could not connect to TPU")
 tpu = None
if tpu:
try:
print("initializing TPU...")
 tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("TPU initialized")
 except _:
print("failed to initialize TPU")
 else:
DEVICE = "GPU"
if DEVICE != "TPU":
print("Using default strategy for CPU and single GPU")
 strategy = tf.distribute.get_strategy()
if DEVICE == "GPU":
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
 AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}' )<normalization>
submission_df_xception = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_xception.to_csv("submission_xception.csv", index=False )
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 def get_3x3_mat(lst):
return tf.reshape(tf.concat([lst],axis=0), [3,3])
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = get_3x3_mat([c1, s1, zero,
-s1, c1, zero,
zero, zero, one])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = get_3x3_mat([one, s2, zero,
zero, c2, zero,
zero, zero, one])
 zoom_matrix = get_3x3_mat([one/height_zoom, zero, zero,
zero, one/width_zoom, zero,
zero, zero, one])
 shift_matrix = get_3x3_mat([one, zero, height_shift,
zero, one, width_shift,
zero, zero, one])
 return K.dot(K.dot(rotation_matrix, shear_matrix),
K.dot(zoom_matrix, shift_matrix))
def transform(image, DIM=256):
XDIM = DIM%2
rot = ROT_ * tf.random.normal([1], dtype='float32')
 shr = SHR_ * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ HZOOM_
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ WZOOM_
h_shift = HSHIFT_ * tf.random.normal([1], dtype='float32')
 w_shift = WSHIFT_ * tf.random.normal([1], dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2, -DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2, DIM//2), [DIM])
 z = tf.ones([DIM*DIM], dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM//2+XDIM+1, DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d,[DIM, DIM,3] )<prepare_x_and_y>
submission_df = pd.DataFrame({"filename": test_videos} )
def dropout(image, DIM=256, PROBABILITY = 0.75, CT = 4, SZ = 0.2):
P = tf.cast(tf.random.uniform([],0,1)< PROBABILITY, tf.int32)
 if(P == 0)|(CT == 0)|(SZ == 0): return image
for k in range(CT):
x = tf.cast(tf.random.uniform([],0,DIM),tf.int32)
 y = tf.cast(tf.random.uniform([],0,DIM),tf.int32)
 WIDTH = tf.cast(SZ*DIM,tf.int32)* P
ya = tf.math.maximum(0,y-WIDTH//2)
 yb = tf.math.minimum(DIM,y+WIDTH//2)
 xa = tf.math.maximum(0,x-WIDTH//2)
 xb = tf.math.minimum(DIM,x+WIDTH//2)
 one = image[ya:yb,0:xa,:]
two = tf.zeros([yb-ya,xb-xa,3])
 three = image[ya:yb,xb:DIM,:]
middle = tf.concat([one,two,three],axis=1)
 image = tf.concat([image[0:ya,:,:],middle,image[yb:DIM,:,:]],axis=0)
 image = tf.reshape(image,[DIM,DIM,3])
 return image<categorify>
r1 = 0.46441
r2 = 0.52189
total = r1 + r2
r11 = r1/total
r22 = r2/total
def float_parameter(level, maxval):
return tf.cast(( level)* maxval / 10., tf.float32)
 def sample_level(n):
return tf.random.uniform(shape=[1], minval=0.1, maxval=n, dtype=tf.float32)
 def solarize_add(image, level, probability=1.0):
P = tf.cast(tf.random.uniform([],0,1)< probability, tf.int32)
 if P == 0:
return image
threshold = float_parameter(sample_level(level), 1)
 addition = float_parameter(sample_level(level), 0.5)
 rand_var = tf.random.uniform(shape=[], dtype=tf.float32)
 addition = tf.cond(rand_var > 0.5, lambda: addition, lambda: -addition)
 added_image = tf.cast(image, tf.float32)+ addition
added_image = tf.cast(tf.clip_by_value(added_image, 0, 1), tf.float32)
 return tf.where(image < threshold, added_image, image)
 def equalize(image, probability=1.0):
P = tf.cast(tf.random.uniform([],0,1)< probability, tf.int32)
 if P == 0:
return image
image = tf.cast(tf.math.scalar_mul(255, image), tf.uint8)
 def scale_channel(im, c):
im = tf.cast(im[:, :, c], tf.int32)
 histo = tf.histogram_fixed_width(im, [0, 255], nbins=256)
 nonzero = tf.where(tf.not_equal(histo, 0))
nonzero_histo = tf.reshape(tf.gather(histo, nonzero), [-1])
 step =(tf.reduce_sum(nonzero_histo)- nonzero_histo[-1])// 255
def build_lut(histo, step):
lut =(tf.cumsum(histo)+(step // 2)) // step
lut = tf.concat([[0], lut[:-1]], 0)
 return tf.clip_by_value(lut, 0, 255)
 result = tf.cond(tf.equal(step, 0),
lambda: im,
lambda: tf.gather(build_lut(histo, step), im))
return tf.cast(result, tf.uint8)
 s1 = scale_channel(image, 0)
 s2 = scale_channel(image, 1)
 s3 = scale_channel(image, 2)
 image = tf.stack([s1, s2, s3], 2)
 return tf.cast(tf.clip_by_value(tf.math.divide(image, 255), 0, 1), tf.float32)
 def autocontrast(image, probability=1.0):
P = tf.cast(tf.random.uniform([],0,1)< probability, tf.int32)
 if P == 0:
return image
image = tf.cast(tf.math.scalar_mul(255, image), tf.uint8)
 def scale_channel(image):
lo = tf.cast(tf.reduce_min(image), tf.float32)
 hi = tf.cast(tf.reduce_max(image), tf.float32)
 def scale_values(im):
scale = 255.0 /(hi - lo)
 offset = -lo * scale
im = tf.cast(im, tf.float32)* scale + offset
im = tf.clip_by_value(im, 0.0, 255.0)
 return tf.cast(im, tf.uint8)
 result = tf.cond(hi > lo, lambda: scale_values(image), lambda: image)
 return result
s1 = scale_channel(image[:, :, 0])
 s2 = scale_channel(image[:, :, 1])
 s3 = scale_channel(image[:, :, 2])
 image = tf.stack([s1, s2, s3], 2)
 return tf.cast(tf.clip_by_value(tf.math.divide(image, 255), 0, 1), tf.float32 )<categorify>
submission_df["label"] = r22*submission_df_resnext["label"] + r11*submission_df_xception["label"]
<prepare_x_and_y><EOS> 
submission_df.to_csv("submission.csv", index=False )