Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<predict_on_test>
%matplotlib inline
warnings.filterwarnings("ignore" )
predictions = model.predict(test )<save_to_csv>
df_train0 = pd.read_json('.. /input/deepfake-detection-challenge/metadata0.json' )
export = pd.DataFrame([np.argmax(prediction)for prediction in predictions])
 export.index += 1
export = export.reset_index()
export.columns = ['ImageId', 'Label']
export.to_csv('submission.csv', index=False )<import_modules>
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 len(test_videos )
from tensorflow.keras.preprocessing.image \ 
<load_from_csv>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
df_train=pd.read_csv('.. /input/digit-recognizer/train.csv')
 df_test=pd.read_csv('.. /input/digit-recognizer/test.csv')
 print([df_train.shape,df_test.shape])
 print(df_train.iloc[265,1:].values.reshape(28,28)[:,10] )<prepare_x_and_y>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
images=['%s%s'%('pixel',pixel_no)for pixel_no in range(0,784)]
x_train=df_train[images].values/255.
 x_train=x_train.reshape(-1,28,28,1)
 y_train=df_train['label'].values
x_test_out=df_test[images].values/255.
 x_test_out=x_test_out.reshape(-1,28,28,1 )<prepare_x_and_y>
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 )
num_classes=10; img_size,img_size2=28,96
N=df_train.shape[0]; n=int (.1*N)
 shuffle_ids=np.arange(N)
 np.random.RandomState(123 ).shuffle(shuffle_ids)
 x_train=x_train[shuffle_ids]; y_train=y_train[shuffle_ids]
x_test,x_valid,x_train=\
x_train[:n],x_train[n:2*n],x_train[2*n:]
y_test,y_valid,y_train=\
y_train[:n],y_train[n:2*n],y_train[2*n:]
df=pd.DataFrame(
 [[x_train.shape,x_valid.shape,x_test.shape],
[x_train.dtype,x_valid.dtype,x_test.dtype],
[y_train.shape,y_valid.shape,y_test.shape],
[y_train.dtype,y_valid.dtype,y_test.dtype]],
columns=['train','valid','test'],
index=['image shape','image type','label shape','label type'])
 df<choose_model_class>
frames_per_video = 20
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)
 mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
def model() :
model=tf.keras.Sequential()
model.add(tkl.Input(shape=(28,28,1)))
 model.add(tkl.BatchNormalization())
 model.add(tkl.Conv2D(28,(5,5),padding='same'))
model.add(tkl.LeakyReLU(alpha=.02))
model.add(tkl.MaxPooling2D(pool_size=(2,2)))
 model.add(tkl.Dropout (.2))
model.add(tkl.Conv2D(96,(5,5),padding='same'))
model.add(tkl.LeakyReLU(alpha=.02))
model.add(tkl.MaxPooling2D(strides=(2,2)))
 model.add(tkl.Dropout (.2))
model.add(tkl.Conv2D(128,(5,5)))
 model.add(tkl.LeakyReLU(alpha=.02))
model.add(tkl.MaxPooling2D(strides=(2,2)))
 model.add(tkl.Dropout (.2))
model.add(tkl.GlobalMaxPooling2D())
 model.add(tkl.Dense(1024))
model.add(tkl.LeakyReLU(alpha=.02))
model.add(tkl.Dropout (.5))
model.add(tkl.Dense(num_classes,activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='nadam',
metrics=['sparse_categorical_accuracy'])
 return model<train_model>
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 )
cnn_model=model()
checkpointer=tkc.ModelCheckpoint(
 filepath='/tmp/checkpoint',verbose=2,save_weights_only=True,
monitor='val_sparse_categorical_accuracy',mode='max',save_best_only=True)
 lr_reduction=tkc.ReduceLROnPlateau(
 monitor='val_loss',patience=15,verbose=2,factor=.8)
 early_stopping=tkc.EarlyStopping(
 monitor='val_loss',patience=75,verbose=2)
 history=cnn_model.fit(
 x_train,y_train,epochs=120,batch_size=128,
verbose=2,validation_data=(x_valid,y_valid),
callbacks=[checkpointer,lr_reduction,early_stopping] )<compute_test_metric>
cnn_model.load_weights('/tmp/checkpoint')
 scores=cnn_model.evaluate(x_test,y_test,verbose=0 )<train_model>
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
steps,epochs=int(len(x_train)/128),10
datagen=ImageDataGenerator(
 featurewise_center=True,
featurewise_std_normalization=True,
zoom_range=.2,shear_range=.2,rotation_range=30,
height_shift_range=.2,width_shift_range=.2)
 datagen.fit(x_train)
 history=cnn_model.\
fit(datagen.flow(x_train,y_train,batch_size=128),
steps_per_epoch=steps,epochs=epochs,verbose=2,
validation_data=datagen.flow(x_valid,y_valid,batch_size=16),
callbacks=[checkpointer,lr_reduction,early_stopping] )<compute_test_metric>
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 )
cnn_model.load_weights('/tmp/checkpoint')
 scores=cnn_model.evaluate(x_test,y_test,verbose=0 )<predict_on_test>
input_size = 224
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
predict_y_test_out=cnn_model.predict(x_test_out)
 predict_y_test_out=predict_y_test_out.argmax(axis=-1 )<save_to_csv>
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)) )
submission=pd.DataFrame(
 {'ImageId':range(1,len(predict_y_test_out)+1),
'Label':predict_y_test_out})
 print(submission[0:15].T)
 submission.to_csv('kaggle_digits_cnn.csv',index=False )<load_pretrained>
!pip install.. /input/deepfake-xception-trained-model/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet
os.environ['TFHUB_MODEL_LOAD_FORMAT']='COMPRESSED'
model=th.load('https://tfhub.dev/captain-pool/esrgan-tf2/1')
 func=model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
func.inputs[0].set_shape([1,img_size2//4,img_size2//4,3])
 converter=tf.lite.TFLiteConverter.from_concrete_functions([func])
 converter.optimizations=[tf.lite.Optimize.DEFAULT]
tflite_model=converter.convert()
with tf.io.gfile.GFile('ESRGAN.tflite','wb')as f:
f.write(tflite_model)
 esrgan_model_path='./ESRGAN.tflite'<prepare_x_and_y>
class Head(nn.Module):
def __init__(self, in_f, out_f):
super().__init__()
self.f = nn.Flatten()
self.l = nn.Linear(in_f, 512)
 self.b1 = nn.BatchNorm1d(in_f)
 self.d = nn.Dropout(0.25)
 self.o = nn.Linear(512, out_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(nn.Module):
def __init__(self, base, in_f):
super().__init__()
self.base = base
self.h1 = Head(in_f, 1)
 def forward(self, x):
x = self.base(x)
 return self.h1(x )
N3=10000; n3=int (.1*N3)
 x_train3=x_train[:N3]; y_train3=y_train[:N3]
x_valid3=x_valid[:n3]; y_valid3=y_valid[:n3]
x_test3=x_test[:n3]; y_test3=y_test[:n3]
x_train3=tf.repeat(x_train3,3,axis=3 ).numpy()
x_valid3=tf.repeat(x_valid3,3,axis=3 ).numpy()
x_test3=tf.repeat(x_test3,3,axis=3 ).numpy()
x_test3.shape,x_test3.mean()<normalization>
model = get_model("xception", pretrained=False)
 model = nn.Sequential(*list(model.children())[:-1])
 model[0].final_block.pool = nn.Sequential(nn.AdaptiveAvgPool2d(( 1,1)) )
def bicubic_resize(imgs,img_size=img_size2):
bicubic=tf.image.resize(
 imgs*255,[img_size,img_size],tf.image.ResizeMethod.BICUBIC)
 bicubic_contrast=tf.image.adjust_contrast(bicubic,.8)
 bicubic_contrast=tf.cast(bicubic_contrast,tf.uint8)
 return bicubic_contrast.numpy() /255<normalization>
input_size = 150
checkpoint = torch.load('.. /input/mymodels/model_niz.pth', map_location=gpu)
 model = FCN(model, 2048 ).to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint
x_train3=bicubic_resize(x_train3)
 x_valid3=bicubic_resize(x_valid3)
 x_test3=bicubic_resize(x_test3)
 x_test3.shape,x_test3.mean()<normalization>
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)) )
def esrgantf2_superresolution(
 img,super_size=img_size2,model_path=esrgan_model_path):
if img.mean() <1.: img=img*255.
 lr=tf.image.resize(img,[super_size//4,super_size//4])
 lr=tf.expand_dims(lr.numpy() [:,:,:3],axis=0)
 lr=tf.cast(lr,tf.float32)
 interpreter=tf.lite.Interpreter(model_path=model_path)
 interpreter.allocate_tensors()
input_details=interpreter.get_input_details()
output_details=interpreter.get_output_details()
interpreter.set_tensor(input_details[0]['index'],lr)
 interpreter.invoke()
output_data=interpreter.get_tensor(output_details[0]['index'])
 sr=tf.squeeze(output_data,axis=0)
 sr=tf.clip_by_value(sr,0,255)
 sr=tf.round(sr); sr=tf.cast(sr,tf.uint8)
 lr=tf.cast(tf.squeeze(lr,axis=0),tf.uint8)
 return lr,sr<normalization>
final_predictions = 0.5 *(np.array(predictions_resnext)+ np.array(predictions_xception))
submission_df = pd.DataFrame({'filename': test_videos, "label": final_predictions})
 plt.hist(submission_df['label'] )
<choose_model_class><EOS> 
submission_df.to_csv("submission.csv", index=False)