kernel_id int64 24.2k 23.3M | prompt stringlengths 8 1.85M | completetion stringlengths 1 182k | comp_name stringlengths 5 57 |
|---|---|---|---|
7,668,213 | <SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<train_model> | !pip install /kaggle/input/dfdcpackages/dlib-19.19.0-cp36-cp36m-linux_x86_64.whl | Deepfake Detection Challenge |
7,668,213 | <load_from_csv><EOS> | DATA_PREFIX = '/kaggle/input'
SKIP_FRAMES = 75
detector = dlib.cnn_face_detection_model_v1(os.path.join(DATA_PREFIX, 'dfdcpackages', 'mmod_human_face_detector.dat'))
sp = dlib.shape_predictor(os.path.join(DATA_PREFIX, 'dfdcpackages', 'shape_predictor_5_face_landmarks.dat'))
predictor = dlib.deep_fake_detection_model_v1... | Deepfake Detection Challenge |
7,736,225 | <SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<predict_on_test> | %matplotlib inline
warnings.filterwarnings("ignore" ) | Deepfake Detection Challenge |
7,736,225 | predictions = model.predict(test )<save_to_csv> | df_train0 = pd.read_json('.. /input/deepfake-detection-challenge/metadata0.json' ) | Deepfake Detection Challenge |
7,736,225 | 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 ) | Deepfake Detection Challenge |
7,736,225 | 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() ) | Deepfake Detection Challenge |
7,736,225 | 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 | Deepfake Detection Challenge |
7,736,225 | 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 ) | Deepfake Detection Challenge |
7,736,225 | 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_trai... | 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 ) | Deepfake Detection Challenge |
7,736,225 | 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')... | 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 ) | Deepfake Detection Challenge |
7,736,225 | 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(
monito... | Deepfake Detection Challenge | |
7,736,225 | 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 = isotrop... | Deepfake Detection Challenge |
7,736,225 | 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_p... | 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(pred... | Deepfake Detection Challenge |
7,736,225 | 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
| Deepfake Detection Challenge |
7,736,225 | 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)) ) | Deepfake Detection Challenge |
7,736,225 | 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 | Deepfake Detection Challenge |
7,736,225 | 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... | 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)... | Deepfake Detection Challenge |
7,736,225 | 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.me... | model = get_model("xception", pretrained=False)
model = nn.Sequential(*list(model.children())[:-1])
model[0].final_block.pool = nn.Sequential(nn.AdaptiveAvgPool2d(( 1,1)) ) | Deepfake Detection Challenge |
7,736,225 | 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
| Deepfake Detection Challenge |
7,736,225 | 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)) ) | Deepfake Detection Challenge |
7,736,225 | 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.al... | 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'] ) | Deepfake Detection Challenge |
7,736,225 | <choose_model_class><EOS> | submission_df.to_csv("submission.csv", index=False)
| Deepfake Detection Challenge |
8,252,685 | <SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<train_model> | from IPython.display import Image | Deepfake Detection Challenge |
8,252,685 | fw='/tmp/checkpoint'
handle_base='mobilenet_v2_100_%d'%img_size2
mhandle='https://tfhub.dev/google/imagenet/{}/classification/4'\
.format(handle_base)
hub_model=premodel(img_size2,1024,mhandle,num_classes,
'softmax','sparse_categorical_crossentropy')
history=hub_model.fit(x=x_train3,y=y_train3,batch_size=128,
epochs... | Image('.. /input/deepfake-kernel-data/google_cloud_compute_engine_launch_vm.png' ) | Deepfake Detection Challenge |
8,252,685 | hub_model.load_weights('/tmp/checkpoint')
hub_model.evaluate(x_test3,y_test3,verbose=0 )<set_options> | Image('.. /input/deepfake-kernel-data/google_cloud_vm.png' ) | Deepfake Detection Challenge |
8,252,685 | %matplotlib inline
sns.set(style='white', context='notebook', palette='deep' )<load_from_csv> | Image('.. /input/deepfake-kernel-data/lr_15e-2_epochs_42_patience_5.png' ) | Deepfake Detection Challenge |
8,252,685 | df_train = pd.read_csv(".. /input/digit-recognizer/train.csv")
df_test = pd.read_csv(".. /input/digit-recognizer/test.csv" )<prepare_x_and_y> | Image('.. /input/deepfake-kernel-data/lr_2e-3_epochs_10_patience_5.png' ) | Deepfake Detection Challenge |
8,252,685 | X_train = np.array(df_train.drop(['label'], axis=1), dtype="float32")/ 255.0
X_train = X_train.reshape(-1, 28, 28, 1)
Y_train = to_categorical(df_train['label'], num_classes = 10)
X_test = np.array(df_test, dtype="float32")/ 255.0
X_test = X_test.reshape(-1, 28, 28, 1 )<split> | Image('.. /input/deepfake-kernel-data/lr_2e-3_epochs_20_patience_5.png' ) | Deepfake Detection Challenge |
8,252,685 | X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.05, random_state=34 )<choose_model_class> | Image('.. /input/deepfake-kernel-data/lr_4e-3_epochs_12_patience_2.png' ) | Deepfake Detection Challenge |
8,252,685 | model = Sequential()
model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
model.add(BatchNormalization())
model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
model.add(MaxPool2D(pool_size=... | Image('.. /input/deepfake-kernel-data/lr_4e-3_epochs_30_patience_2.png' ) | Deepfake Detection Challenge |
8,252,685 | datagen = ImageDataGenerator(featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
datagen.fit(X_tra... | Image('.. /input/deepfake-kernel-data/google_cloud_vm_deepfake_training_screenshot.png' ) | Deepfake Detection Challenge |
8,252,685 | optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
model.compile(optimizer=optimizer,
loss="categorical_crossentropy",
metrics=["accuracy"])
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<train_model> | %matplotlib inline
warnings.filterwarnings("ignore" ) | Deepfake Detection Challenge |
8,252,685 | epochs=50
batch_size=128
history = model.fit(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs,
validation_data =(X_val,Y_val),
verbose = 2,
steps_per_epoch=X_train.shape[0] // batch_size,
callbacks=[learning_rate_reduction] )<predict_on_test> | 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 ) | Deepfake Detection Challenge |
8,252,685 | Y_pred = model.predict(X_val)
Y_pred_classes = np.argmax(Y_pred,axis = 1)
Y_true = np.argmax(Y_val,axis = 1 )<compute_test_metric> | print("PyTorch version:", torch.__version__)
print("CUDA version:", torch.version.cuda)
print("cuDNN version:", torch.backends.cudnn.version() ) | Deepfake Detection Challenge |
8,252,685 | errors =(Y_pred_classes - Y_true != 0)
Y_pred_classes_errors = Y_pred_classes[errors]
Y_pred_errors = Y_pred[errors]
Y_true_errors = Y_true[errors]
X_val_errors = X_val[errors]<compute_train_metric> | gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
gpu | Deepfake Detection Challenge |
8,252,685 | Y_pred_errors_prob = np.max(Y_pred_errors,axis = 1)
true_prob_errors = np.diagonal(np.take(Y_pred_errors, Y_true_errors, axis=1))
delta_pred_true_errors = Y_pred_errors_prob - true_prob_errors
sorted_dela_errors = np.argsort(delta_pred_true_errors)
most_important_errors = sorted_dela_errors[-6:]
display_errors(most_i... | 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 ) | Deepfake Detection Challenge |
8,252,685 | results = model.predict(X_test)
results = np.argmax(results, axis = 1)
results = pd.Series(results, name="Label" )<save_to_csv> | 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 ) | Deepfake Detection Challenge |
8,252,685 | submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"), results],axis = 1)
submission.to_csv("submission.csv",index=False )<install_modules> | input_size = 224 | Deepfake Detection Challenge |
8,252,685 | !pip3 install --no-dependencies.. /input/efficientnetcassava/Keras_Applications-1.0.8-py3-none-any.whl
!pip3 install --no-dependencies.. /input/efficientnetcassava/efficientnet-1.1.1-py3-none-any.whl<import_modules> | mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std ) | Deepfake Detection Challenge |
8,252,685 | import re
import numpy as np
import pandas as pd
import os
import json<import_modules> | 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 ) | Deepfake Detection Challenge |
8,252,685 | import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import losses
from sklearn.model_selection import train_test_split
from efficientnet.keras import EfficientNetB3 as EfficientNet<define_variables> | checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
model = MyResNeXt().to(gpu)
model.load_state_dict(checkpoint)
_ = model.eval()
del checkpoint | Deepfake Detection Challenge |
8,252,685 | AUTO = tf.data.experimental.AUTOTUNE
SIZE = 600
ORIGINAL_WIDTH = 800
ORIGINAL_HEIGHT = 600
CHANNELS = 3
BATCH_SIZE = 32<normalization> | 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 = isotrop... | Deepfake Detection Challenge |
8,252,685 | def decode_image(path):
image = tf.io.read_file(path)
image = tf.image.decode_jpeg(image, channels=3)
image =(tf.cast(image, tf.float32)/ 255.0)
image = tf.image.resize(image, [ORIGINAL_HEIGHT, ORIGINAL_WIDTH])
image = tf.reshape(image, [ORIGINAL_HEIGHT, ORIGINAL_WIDTH , CHANNELS])
return image
def normalize(x):
x... | 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(pred... | Deepfake Detection Challenge |
8,252,685 | load_dir = "/kaggle/input/cassava-leaf-disease-classification"
sub_df = pd.read_csv(load_dir + '/sample_submission.csv')
sub_df['paths'] = load_dir + "/test_images/" + sub_df.image_id<categorify> | speed_test = False | Deepfake Detection Challenge |
8,252,685 | def load_dataset(augment=False):
test_dataset =tf.data.Dataset.from_tensor_slices(sub_df.paths.values ).map(decode_image, num_parallel_calls=AUTO)
if augment:
test_dataset = test_dataset.map(lambda x: data_aug(x), num_parallel_calls=AUTO)
else:
test_dataset = test_dataset.map(lambda x:normalize(x))
return test_datase... | 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)) ) | Deepfake Detection Challenge |
8,252,685 | def load_model(i):
inputs = layers.Input(shape=(ORIGINAL_HEIGHT, ORIGINAL_WIDTH, 3))
model = Sequential([
EfficientNet(include_top=False,weights=None, input_tensor=inputs),
layers.GlobalAveragePooling2D(name="avg_pool"),
layers.BatchNormalization() ,
layers.Dropout(0.3, name="top_dropout"),
layers.Dense(5, activation="... | predictions = predict_on_video_set(test_videos, num_workers=4 ) | Deepfake Detection Challenge |
8,252,685 | n_models = 5
models = []
for i in range(n_models):
models.append(load_model(i))<predict_on_test> | submission_df_resnext = pd.DataFrame({"filename": test_videos, "label": predictions})
submission_df_resnext.to_csv("submission_resnext.csv", index=False ) | Deepfake Detection Challenge |
8,252,685 | preds = []
test_dataset = load_dataset()
for i in range(n_models):
preds.append(models[i].predict(test_dataset, verbose=1))
for i in range(10):
test_dataset_augmented = load_dataset(augment=True)
for i in range(n_models):
preds.append(models[i].predict(test_dataset_augmented, verbose=1))
preds = np.mean(preds, axis=0)... | !pip install.. /input/deepfake-xception-trained-model/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet | Deepfake Detection Challenge |
8,252,685 | sub_df['label'] = preds.argmax(axis=1)
sub_df.drop(columns='paths' ).to_csv('submission.csv', index=False)
!head submission.csv<set_options> | 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 ) | Deepfake Detection Challenge |
8,252,685 | warnings.filterwarnings('ignore')
<define_variables> | gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
gpu | Deepfake Detection Challenge |
8,252,685 | training_folder = '.. /input/cassava-leaf-disease-classification/train_images/'<load_from_csv> | 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 ) | Deepfake Detection Challenge |
8,252,685 | samples_df = pd.read_csv(".. /input/cassava-leaf-disease-classification/train.csv")
samples_df = shuffle(samples_df, random_state=42)
samples_df["filepath"] = training_folder+samples_df["image_id"]
samples_df.head()
<define_variables> | 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 ) | Deepfake Detection Challenge |
8,252,685 | training_percentage = 0.8
training_item_count = int(len(samples_df)*training_percentage)
validation_item_count = len(samples_df)-int(len(samples_df)*training_percentage)
training_df = samples_df[:training_item_count]
validation_df = samples_df[training_item_count:]
<define_variables> | input_size = 150 | Deepfake Detection Challenge |
8,252,685 | batch_size = 8
image_size = 512
input_shape =(image_size, image_size, 3)
dropout_rate = 0.4
classes_to_predict = sorted(training_df.label.unique() )<prepare_x_and_y> | mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std ) | Deepfake Detection Challenge |
8,252,685 | training_data = tf.data.Dataset.from_tensor_slices(( training_df.filepath.values, training_df.label.values))
validation_data = tf.data.Dataset.from_tensor_slices(( validation_df.filepath.values, validation_df.label.values))<categorify> | 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)
retur... | Deepfake Detection Challenge |
8,252,685 | def load_image_and_label_from_path(image_path, label):
img = tf.io.read_file(image_path)
img = tf.image.decode_jpeg(img, channels=3)
return img, label
AUTOTUNE = tf.data.experimental.AUTOTUNE
training_data = training_data.map(load_image_and_label_from_path, num_parallel_calls=AUTOTUNE)
validation_data = validation_d... | 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 = isotrop... | Deepfake Detection Challenge |
8,252,685 | adapt_data = tf.data.Dataset.from_tensor_slices(training_df.filepath.values)
def adapt_mode(image_path):
img = tf.io.read_file(image_path)
img = tf.image.decode_jpeg(img, channels=3)
img = layers.experimental.preprocessing.Rescaling(1.0 / 255 )(img)
return img
adapt_data = adapt_data.map(adapt_mode, num_parallel_ca... | 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(pred... | Deepfake Detection Challenge |
8,252,685 | data_augmentation_layers = tf.keras.Sequential(
[
layers.experimental.preprocessing.RandomCrop(height=image_size, width=image_size),
layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),
layers.experimental.preprocessing.RandomRotation(0.25),
layers.experimental.preprocessing.RandomZoom(( -0.2, 0)) ... | speed_test = False | Deepfake Detection Challenge |
8,252,685 | efficientnet = EfficientNetB3(weights=".. /input/keras-efficientnetb3-no-top-weights/efficientnetb3_notop.h5",
include_top=False,
input_shape=input_shape,
drop_connect_rate=dropout_rate)
inputs = Input(shape=input_shape)
augmented = data_augmentation_layers(inputs)
efficientnet = efficientnet(augmented)
pooling = l... | 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)) ) | Deepfake Detection Challenge |
8,252,685 | epochs = 20<choose_model_class> | %%time
model.eval()
predictions = predict_on_video_set(test_videos, num_workers=4 ) | Deepfake Detection Challenge |
8,252,685 | decay_steps = int(round(len(training_df)/batch_size)) *epochs
cosine_decay = CosineDecay(initial_learning_rate=1e-4, decay_steps=decay_steps, alpha=0.3)
callbacks = [ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]
model.compile(loss="sparse_categorical_crossentropy", optimizer=tf.ke... | submission_df_xception = pd.DataFrame({"filename": test_videos, "label": predictions})
submission_df_xception.to_csv("submission_xception.csv", index=False ) | Deepfake Detection Challenge |
8,252,685 | history = model.fit(training_data_batches,
epochs = epochs,
validation_data=validation_data_batches,
callbacks=callbacks )<load_pretrained> | submission_df = pd.DataFrame({"filename": test_videos})
submission_df["label"] = 0.5*submission_df_resnext["label"] + 0.5*submission_df_xception["label"] | Deepfake Detection Challenge |
8,252,685 | model.load_weights("./best_model.h5" )<save_to_csv> | submission_df.to_csv("submission.csv", index=False ) | Deepfake Detection Challenge |
8,666,237 | submission_df.to_csv("submission.csv", index=False )<set_options> | BASE_PATH = '/kaggle/input/deepfake-detection-challenge/'
TEST_VIDEO_PATH = BASE_PATH + 'test_videos/'
SAMP_PATH = BASE_PATH + 'sample_submission.csv'
test_img_list = glob.glob(f'{TEST_VIDEO_PATH}*.mp4' ) | Deepfake Detection Challenge |
8,666,237 | warnings.filterwarnings("ignore")
%matplotlib inline
<set_options> | class EffnetTest(nn.Module):
def __init__(self, version):
super(EffnetTest, self ).__init__()
self.model = EfficientNet.from_name(f"efficientnet-{version}", override_params={"num_classes":1})
self.model.fc = nn.Linear(512, 1)
self.model._norm_layer = nn.GroupNorm(num_groups=32, num_channels=3)
def forward(self, x):
... | Deepfake Detection Challenge |
8,666,237 | pd.set_option('display.max_columns', 500)
pd.set_option('display.max_rows', 500)
folder = '/kaggle/input/mercedes-benz-greener-manufacturing/'<load_from_csv> | class SimpleCNNInference:
def __init__(self, model_path, version='b6', img_size=200):
self.img_size = img_size
self.transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.Resize(size=(self.img_size, self.img_size)) ,
transforms.ToTensor() ,
transforms.Normalize(( 0.485, 0.456, 0.406),(0.229, 0.224, 0.225... | Deepfake Detection Challenge |
8,666,237 | train_df = pd.read_csv(folder + 'train.csv.zip')
test_df = pd.read_csv(folder + 'test.csv.zip')
sub_df = pd.read_csv(folder + 'sample_submission.csv.zip')
print('train_df: ', train_df.shape)
print('test_df: ', test_df.shape)
print('sub_df: ', sub_df.shape )<define_variables> | test_filenames = [string.split('/')[-1] for string in test_img_list]
detector = RetinaFaceDetector(weights="/kaggle/input/resnetretinaface/Resnet50_Final.pth")
inference = SimpleCNNInference(model_path="/kaggle/input/effnetb6-200-0741/EffnetB6_pytorch_group_imgface6_200_0.0741.pth",
version='b6', img_size=200)
infere... | Deepfake Detection Challenge |
8,666,237 | cat_cols = dfs_train.categoricals.tolist()<feature_engineering> | sub = pd.read_csv(SAMP_PATH)
sub['label'] = sub['filename'].map(final_predictions ).clip(0.01, 0.99 ).fillna(0.5 ) | Deepfake Detection Challenge |
8,666,237 | <filter><EOS> | sub.to_csv('submission.csv', index=False ) | Deepfake Detection Challenge |
7,694,669 | <SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<prepare_x_and_y> | %%time
%%capture
| Deepfake Detection Challenge |
7,694,669 | y = train_df['y']
train_df.drop(['y'], axis=1, inplace=True )<categorify> | 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 ) | Deepfake Detection Challenge |
7,694,669 | class MeanEncoding(BaseEstimator):
def __init__(self, feature, C=0.1):
self.C = C
self.feature = feature
def fit(self, X_train, y_train):
df = pd.DataFrame({'feature': X_train[self.feature], 'target': y_train} ).dropna()
self.global_mean = df.target.mean()
mean = df.groupby('feature' ).target.mean()
size = df.groupby... | print("PyTorch version:", torch.__version__)
print("CUDA version:", torch.version.cuda)
print("cuDNN version:", torch.backends.cudnn.version() ) | Deepfake Detection Challenge |
7,694,669 | for f in cat_cols:
me = MeanEncoding(f, C=0.99)
me.fit(train_df, y)
train_df = me.transform(train_df)
test_df = me.transform(test_df )<predict_on_test> | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
| Deepfake Detection Challenge |
7,694,669 | km = KMeans(n_clusters=2, random_state=13)
km.fit(pd.DataFrame(y))
y_clust = km.predict(pd.DataFrame(y))<count_values> | facedet = BlazeFace().to(device)
facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
_ = facedet.train(False ) | Deepfake Detection Challenge |
7,694,669 | pd.Series(y_clust ).value_counts(normalize=True )<split> | input_size = 256 | Deepfake Detection Challenge |
7,694,669 | X_train, X_val, y_train, y_val, y_train_clust, y_val_clust = train_test_split(
train_df, y, pd.Series(y_clust),
test_size=0.25,
stratify=y_clust,
random_state=777
)<count_values> | mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std ) | Deepfake Detection Challenge |
7,694,669 | y_train_clust.value_counts(normalize=True )<normalization> | def disable_grad(model):
for parameter in model.parameters() :
parameter.requires_grad = False
return model
def normalize(img):
y, x, _ = img.shape
if y > x and x < 256:
ratio_x = x / y
ratio_y = y / x
return cv2.resize(img,(256, int(ratio_y * 256)))
elif y < x and y < 256:
ratio_x = x / y
ratio_y = y / x
return cv2.r... | Deepfake Detection Challenge |
7,694,669 | scaler = StandardScaler()
scaler.fit(X_train)
X_train_sc = pd.DataFrame(scaler.transform(X_train))
X_val_sc = pd.DataFrame(scaler.transform(X_val))
test_df_sc = pd.DataFrame(scaler.transform(test_df))<normalization> | frames_per_video = 32
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 ) | Deepfake Detection Challenge |
7,694,669 | pca = PCA(n_components=2)
pca.fit(X_train_sc)
train_pca_transformed = pca.transform(X_train_sc )<compute_train_metric> | class MetaModel(nn.Module):
def __init__(self, models=None, device='cuda:0', extended=False):
super(MetaModel, self ).__init__()
self.extended = extended
self.device = device
self.models = models
self.len = len(models)
if self.extended:
self.bn = nn.BatchNorm1d(self.len)
self.relu = nn.ReLU()
self.dropout = nn.Dropou... | Deepfake Detection Challenge |
7,694,669 | lasso = LassoCV(max_iter=9999)
lasso.fit(X_train_sc, y_train)
lasso_train_pred = lasso.predict(X_train_sc)
lasso_val_pred = lasso.predict(X_val_sc)
print('train', metrics.r2_score(y_train, lasso_train_pred), 'val', metrics.r2_score(y_val, lasso_val_pred))<compute_train_metric> | MODELS_PATH = "/kaggle/input/deepfake-detection-model-20k/"
WEIGTHS_EXT = '.pth'
models = []
weigths = []
raw_data_stack = \
[
['0.8548137313946486 0.3376769562025044', 'efficientnet-b2'],
['EfficientNetb3 0.8573518024606384 0.34558522378585194', 'efficientnet-b3'],
['EfficientNetb4 0.8579110384582294 0.338391105307526... | Deepfake Detection Challenge |
7,694,669 | ridge = RidgeCV()
ridge.fit(X_train_sc, y_train)
ridge_train_pred = ridge.predict(X_train_sc)
ridge_val_pred = ridge.predict(X_val_sc)
print('train', metrics.r2_score(y_train, ridge_train_pred), 'val', metrics.r2_score(y_val, ridge_val_pred))<compute_train_metric> | 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 = normali... | Deepfake Detection Challenge |
7,694,669 | enet = ElasticNetCV()
enet.fit(X_train_sc, y_train)
enet_train_pred = enet.predict(X_train_sc)
enet_val_pred = enet.predict(X_val_sc)
print('train', metrics.r2_score(y_train, enet_train_pred), 'val', metrics.r2_score(y_val, enet_val_pred))<compute_train_metric> | def predict_on_video_single(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 = ... | Deepfake Detection Challenge |
7,694,669 | huber = HuberRegressor(alpha=0.05)
huber.fit(X_train_sc, y_train)
huber_train_pred = huber.predict(X_train_sc)
huber_val_pred = huber.predict(X_val_sc)
print('train', metrics.r2_score(y_train, huber_train_pred), 'val', metrics.r2_score(y_val, huber_val_pred))<compute_train_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(pred... | Deepfake Detection Challenge |
7,694,669 | rf = RandomForestRegressor(n_estimators=5)
rf.fit(X_train_sc, y_train)
rf_train_pred = rf.predict(X_train_sc)
rf_val_pred = rf.predict(X_val_sc)
print('train', metrics.r2_score(y_train, rf_train_pred), 'val', metrics.r2_score(y_val, rf_val_pred))<predict_on_test> | speed_test = False | Deepfake Detection Challenge |
7,694,669 | sub_df['y'] = np.round(np.exp(lasso.predict(test_df_sc)) , 4 )<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 min.Average per video: %f sec." %(elapsed / 60, elapsed / len(speedtest_videos)) ) | Deepfake Detection Challenge |
7,694,669 | sub_df.to_csv('sub.csv', index=False )<set_options> | predictions = predict_on_video_set(test_videos, num_workers=4 ) | Deepfake Detection Challenge |
7,694,669 | plt.ion()<load_from_csv> | submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
submission_df.to_csv("submission.csv", index=False ) | Deepfake Detection Challenge |
7,694,669 | <define_variables><EOS> | !rm -r reader && rm install.sh | Deepfake Detection Challenge |
8,531,644 | <SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<categorify> | %%capture
!pip install /kaggle/input/facenet-pytorch-vggface2/facenet_pytorch-2.2.9-py3-none-any.whl
!pip install /kaggle/input/xt-training/pynvml-8.0.4-py3-none-any.whl
!pip install /kaggle/input/xt-training/xt_training-1.4.0-py3-none-any.whl
!pip install /kaggle/input/imageio-ffmpeg/imageio_ffmpeg-0.3.0-py3-none-many... | Deepfake Detection Challenge |
8,531,644 | class ImageData(Dataset):
def __init__(self, df, data_dir, transform):
super().__init__()
self.df = df
self.data_dir = data_dir
self.transform = transform
def __len__(self):
return len(self.df)
def __getitem__(self, index):
img_name = self.df.id[index]
label = self.df.has_cactus[index]
img_path = os.path.join(self.dat... | import os
import glob
import numpy as np
import pandas as pd
import torch
from torch.utils.data import DataLoader, Subset
from torch import optim
from tqdm.notebook import tqdm
from matplotlib import pyplot as plt
import albumentations as A
from tqdm.notebook import tqdm
from xt_training import metrics, Runner
from xt_... | Deepfake Detection Challenge |
8,531,644 | epochs = 15
batch_size = 20
device = torch.device('cuda:0' )<categorify> | device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print('Running on device: {}'.format(device)) | Deepfake Detection Challenge |
8,531,644 | data_transf = transforms.Compose([transforms.ToPILImage() , transforms.ToTensor() ])
train_data = ImageData(df = df, data_dir = train_dir, transform = data_transf)
train_loader = DataLoader(dataset = train_data, batch_size = batch_size )<set_options> | class Ensemble(torch.nn.Module):
def __init__(self, unpack=None, permute=False, mapping=None, **kwargs):
super().__init__()
self.permute = permute
self.mapping = mapping
if unpack is None:
self.unpack = lambda x, i: x
else:
self.unpack = unpack
for name, model in kwargs.items() :
self.add_module(name, model)
def forwa... | Deepfake Detection Challenge |
8,531,644 | device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )<choose_model_class> | face_model1 = FaceClassifier(pretrained=False, base_model='resnext')
face_model1.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/face_model_best_alltrain_moreaug_20200317.pt'))
face_model1.classifier.fc = torch.nn.Sequential()
face_model2 = FaceClassifier(pretrained=False, base_model='resnet')
face... | Deepfake Detection Challenge |
8,531,644 | model = models.resnet50(pretrained=True)
model.cuda()
optimizer = optim.Adam(model.parameters() , lr=0.001)
loss_func = nn.CrossEntropyLoss()<train_model> | test_trans = A.ReplayCompose([
A.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) ,
A.Resize(160, 160, always_apply=True)
])
video_root = '/kaggle/input/deepfake-detection-challenge/'
test_dataset = VideoDataset(
video_root,
transform=test_trans,
out_transform=face_model_transform,
is_test=True,
sample_frames=-1... | Deepfake Detection Challenge |
8,531,644 | %%time
for epoch in tqdm(range(epochs)) :
for i,(images, labels)in enumerate(train_loader):
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
loss = loss_func(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if(i+1)% 500 == 0:
print('Epoch [{}/{}], Loss: {:.4f}'.fo... | video_model1 = FaceSequenceClassifier(mode='linear')
video_model1.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/video_model_best_alltrain_lessaug_20200319.pt'))
video_model2 = FaceSequenceClassifier(mode='linear')
video_model2.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/vid... | Deepfake Detection Challenge |
8,531,644 | submit = pd.read_csv('.. /input/sample_submission.csv')
test_data = ImageData(df = submit, data_dir = test_dir, transform = data_transf)
test_loader = DataLoader(dataset = test_data, shuffle=False )<categorify> | sample_dataset = VideoDataset(
video_root,
transform=test_trans,
out_transform=face_model_transform,
is_test=True,
sample_frames=-1,
shuffle=False,
stride=10,
n_frames=-1,
device=device,
reader='imutils',
path_include='train_sample_videos/',
)
sample_dataset.samples[0] =('/kaggle/input/deepfake-detection-challenge/t... | Deepfake Detection Challenge |
8,531,644 | predict = []
for batch_i,(data, target)in enumerate(test_loader):
data, target = data.to(device), target.to(device)
output = model(data)
_, pred = torch.max(output.data, 1)
predict.append(pred )<save_to_csv> | runner = Runner(model=video_model, device=device)
y_pred, _ = runner(test_loader, 'test', return_preds=True ) | Deepfake Detection Challenge |
8,531,644 | <import_modules><EOS> | for i, scale in enumerate(scales):
y_pred[:, i] = y_pred[:, i] * scale
labels = torch.nn.functional.softmax(y_pred.mean(dim=1), dim=1)[:, 1].numpy()
filenames = [os.path.basename(f)for f in test_dataset.video_files]
submission = pd.DataFrame({'filename': filenames, 'label': labels})
submission.to_csv('submission.csv',... | Deepfake Detection Challenge |
8,546,370 | <SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<load_from_csv> | %matplotlib inline
warnings.filterwarnings("ignore" ) | Deepfake Detection Challenge |
8,546,370 | train = pd.read_csv('.. /input/tabular-playground-series-jan-2021/train.csv')
test = pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv')
sub = pd.read_csv('.. /input/tabular-playground-series-jan-2021/sample_submission.csv' )<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 ) | Deepfake Detection Challenge |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.