Upload folder using huggingface_hub
Browse files- .gitignore +2 -0
- README.md +4 -0
- requirements.txt +7 -0
- script.py +102 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
__pycache__
|
2 |
+
submission.csv
|
README.md
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# SAFE Example Submission
|
2 |
+
|
3 |
+
The key requirements is to have a `script.py` file in the top level directory of the repo.
|
4 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
av
|
3 |
+
torchvision
|
4 |
+
torchcodec
|
5 |
+
datasets
|
6 |
+
pandas
|
7 |
+
tqdm
|
script.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from datasets import load_dataset
|
3 |
+
import numpy as np
|
4 |
+
import tqdm.auto as tqdm
|
5 |
+
import os
|
6 |
+
import io
|
7 |
+
import torch
|
8 |
+
import time
|
9 |
+
import av
|
10 |
+
import torch
|
11 |
+
import numpy as np
|
12 |
+
|
13 |
+
# Import your model and anything else you want
|
14 |
+
# You can even install other packages included in your repo
|
15 |
+
# However, during the evaluation the container will not have access to the internet.
|
16 |
+
# So you must include everything you need in your model repo.
|
17 |
+
|
18 |
+
|
19 |
+
import torch
|
20 |
+
|
21 |
+
# from torchcodec.decoders import VideoDecoder
|
22 |
+
|
23 |
+
# def preprocess_v1(file_like):
|
24 |
+
# file_like.seek(0)
|
25 |
+
# decoder = VideoDecoder(file_like)
|
26 |
+
# frames = decoder[0:-1:20]
|
27 |
+
# frames = frames.float() / 255.0
|
28 |
+
# return frames
|
29 |
+
|
30 |
+
|
31 |
+
def preprocess(file_like):
|
32 |
+
# Open the video file
|
33 |
+
file_like.seek(0)
|
34 |
+
container = av.open(file_like)
|
35 |
+
frames = []
|
36 |
+
every = 10
|
37 |
+
for i, frame in enumerate(container.decode(video=0)):
|
38 |
+
if i % every == 0:
|
39 |
+
frame_array = frame.to_ndarray(format="rgb24")
|
40 |
+
frame_tensor = torch.from_numpy(frame_array).permute(2, 0, 1).float()
|
41 |
+
frames.append(frame_tensor)
|
42 |
+
|
43 |
+
video_tensor = torch.stack(frames)
|
44 |
+
return video_tensor
|
45 |
+
|
46 |
+
|
47 |
+
class Model(torch.nn.Module):
|
48 |
+
def __init__(self):
|
49 |
+
super(Model, self).__init__()
|
50 |
+
self.fc1 = torch.nn.Linear(10, 5)
|
51 |
+
self.threshold = 0.0
|
52 |
+
|
53 |
+
def forward(self, x):
|
54 |
+
## generates a random float the same size as x
|
55 |
+
return torch.randn(x.shape[0]).to(x.device)
|
56 |
+
|
57 |
+
|
58 |
+
# load the dataset. dataset will be automatically downloaded to /tmp/data during evaluation
|
59 |
+
DATASET_PATH = "/tmp/data"
|
60 |
+
dataset_remote = load_dataset(DATASET_PATH, split="test", streaming=True)
|
61 |
+
|
62 |
+
|
63 |
+
# load your model
|
64 |
+
device = "cuda:0"
|
65 |
+
model = Model().to(device)
|
66 |
+
|
67 |
+
|
68 |
+
# iterate over the dataset
|
69 |
+
out = []
|
70 |
+
for el in tqdm.tqdm(dataset_remote):
|
71 |
+
|
72 |
+
# start_time = time.time()
|
73 |
+
|
74 |
+
# each element is a dict
|
75 |
+
# el["video"]["bytes"] contains bytes from reading the raw file
|
76 |
+
# el["video"]["path"] containts the filename. This is just for reference and you cant actually load it
|
77 |
+
|
78 |
+
# if you are using libraries that expect a file. You can use BytesIO object
|
79 |
+
try:
|
80 |
+
file_like = io.BytesIO(el["video"]["bytes"])
|
81 |
+
tensor = preprocess(file_like)
|
82 |
+
|
83 |
+
with torch.no_grad():
|
84 |
+
# soft decision (such as log likelihood score)
|
85 |
+
# positive score correspond to synthetic prediction
|
86 |
+
# negative score correspond to real prediction
|
87 |
+
score = model(tensor[None].to(device)).cpu().item()
|
88 |
+
|
89 |
+
# we require a hard decision to be submited. so you need to pick a threshold
|
90 |
+
pred = "generated" if score > model.threshold else "real"
|
91 |
+
|
92 |
+
# append your prediction
|
93 |
+
# "id" and "pred" are required. "score" will not be used in scoring but we encourage you to include it. We'll use it for analysis of the results
|
94 |
+
|
95 |
+
out.append(dict(id=el["id"], pred=pred, score=score))
|
96 |
+
except Exception as e:
|
97 |
+
print(e)
|
98 |
+
print("failed", el["id"])
|
99 |
+
out.append(dict(id=el["id"]))
|
100 |
+
|
101 |
+
# save the final result and that's it
|
102 |
+
pd.DataFrame(out).to_csv("submission.csv", index=False)
|