Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -19,6 +19,7 @@ warnings.filterwarnings('ignore')
|
|
19 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
20 |
|
21 |
sbcl = create_model("Weights/weights.tar")
|
|
|
22 |
|
23 |
face_detector = get_model("resnet50_2020-07-20", max_size=1024, device=device)
|
24 |
face_detector.eval()
|
@@ -44,13 +45,13 @@ def predict_image(inp):
|
|
44 |
return {'No face detected!': 1}, None
|
45 |
|
46 |
with torch.no_grad():
|
47 |
-
img = torch.tensor(face_list).to(device).float()/255
|
48 |
pred = sbcl(img).softmax(1)[:, 1].cpu().data.numpy().tolist()[0]
|
49 |
-
confidences = {'Real': 1-pred, 'Fake': pred}
|
50 |
|
51 |
grayscale_cam = cam_sbcl(input_tensor=img, targets=targets, aug_smooth=True)
|
52 |
grayscale_cam = grayscale_cam[0, :]
|
53 |
-
cam_image = show_cam_on_image(face_list[0].transpose(1, 2, 0)/255, grayscale_cam, use_rgb=True)
|
54 |
|
55 |
return confidences, cam_image
|
56 |
|
@@ -59,7 +60,7 @@ def predict_video(inp):
|
|
59 |
face_list, idx_list = extract_frames(inp, 10, face_detector)
|
60 |
|
61 |
with torch.no_grad():
|
62 |
-
img = torch.tensor(face_list).to(device).float()/255
|
63 |
pred = sbcl(img).softmax(1)[:, 1]
|
64 |
|
65 |
pred_list = []
|
@@ -77,9 +78,9 @@ def predict_video(inp):
|
|
77 |
most_fake = np.argmax(pred_res)
|
78 |
grayscale_cam = cam_sbcl(input_tensor=img[most_fake].unsqueeze(0), targets=targets, aug_smooth=True)
|
79 |
grayscale_cam = grayscale_cam[0, :]
|
80 |
-
cam_image = show_cam_on_image(face_list[most_fake].transpose(1, 2, 0)/255, grayscale_cam, use_rgb=True)
|
81 |
|
82 |
-
return {'Real': 1-pred, 'Fake': pred}, cam_image
|
83 |
|
84 |
with gr.Blocks(title="Deepfake Detection CL", theme='upsatwal/mlsc_tiet', css="""
|
85 |
@import url('https://fonts.googleapis.com/css?family=Source+Code+Pro:200');
|
@@ -176,7 +177,7 @@ with gr.Blocks(title="Deepfake Detection CL", theme='upsatwal/mlsc_tiet', css=""
|
|
176 |
inputs=input_video,
|
177 |
outputs=output_image_video,
|
178 |
fn=predict_video,
|
179 |
-
cache_examples=
|
180 |
)
|
181 |
btn_video.click(predict_video, inputs=input_video, outputs=[label_probs_video, output_image_video], api_name="/predict_video")
|
182 |
|
|
|
19 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
20 |
|
21 |
sbcl = create_model("Weights/weights.tar")
|
22 |
+
sbcl.to(device)
|
23 |
|
24 |
face_detector = get_model("resnet50_2020-07-20", max_size=1024, device=device)
|
25 |
face_detector.eval()
|
|
|
45 |
return {'No face detected!': 1}, None
|
46 |
|
47 |
with torch.no_grad():
|
48 |
+
img = torch.tensor(face_list).to(device).float() / 255
|
49 |
pred = sbcl(img).softmax(1)[:, 1].cpu().data.numpy().tolist()[0]
|
50 |
+
confidences = {'Real': 1 - pred, 'Fake': pred}
|
51 |
|
52 |
grayscale_cam = cam_sbcl(input_tensor=img, targets=targets, aug_smooth=True)
|
53 |
grayscale_cam = grayscale_cam[0, :]
|
54 |
+
cam_image = show_cam_on_image(face_list[0].transpose(1, 2, 0) / 255, grayscale_cam, use_rgb=True)
|
55 |
|
56 |
return confidences, cam_image
|
57 |
|
|
|
60 |
face_list, idx_list = extract_frames(inp, 10, face_detector)
|
61 |
|
62 |
with torch.no_grad():
|
63 |
+
img = torch.tensor(face_list).to(device).float() / 255
|
64 |
pred = sbcl(img).softmax(1)[:, 1]
|
65 |
|
66 |
pred_list = []
|
|
|
78 |
most_fake = np.argmax(pred_res)
|
79 |
grayscale_cam = cam_sbcl(input_tensor=img[most_fake].unsqueeze(0), targets=targets, aug_smooth=True)
|
80 |
grayscale_cam = grayscale_cam[0, :]
|
81 |
+
cam_image = show_cam_on_image(face_list[most_fake].transpose(1, 2, 0) / 255, grayscale_cam, use_rgb=True)
|
82 |
|
83 |
+
return {'Real': 1 - pred, 'Fake': pred}, cam_image
|
84 |
|
85 |
with gr.Blocks(title="Deepfake Detection CL", theme='upsatwal/mlsc_tiet', css="""
|
86 |
@import url('https://fonts.googleapis.com/css?family=Source+Code+Pro:200');
|
|
|
177 |
inputs=input_video,
|
178 |
outputs=output_image_video,
|
179 |
fn=predict_video,
|
180 |
+
cache_examples=True,
|
181 |
)
|
182 |
btn_video.click(predict_video, inputs=input_video, outputs=[label_probs_video, output_image_video], api_name="/predict_video")
|
183 |
|