not-lain commited on
Commit
51abdcc
1 Parent(s): 248f463

switched to pipeline

Browse files
Files changed (2) hide show
  1. app.py +7 -60
  2. resnetinceptionv1_epoch_32.pth +0 -3
app.py CHANGED
@@ -10,29 +10,11 @@ import cv2
10
  from pytorch_grad_cam import GradCAM
11
  from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
12
  from pytorch_grad_cam.utils.image import show_cam_on_image
13
-
14
  with zipfile.ZipFile("examples.zip","r") as zip_ref:
15
  zip_ref.extractall(".")
16
 
17
- DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu'
18
-
19
- mtcnn = MTCNN(
20
- select_largest=False,
21
- post_process=False,
22
- device=DEVICE
23
- ).to(DEVICE).eval()
24
-
25
- model = InceptionResnetV1(
26
- pretrained="vggface2",
27
- classify=True,
28
- num_classes=1,
29
- device=DEVICE
30
- )
31
-
32
- checkpoint = torch.load("resnetinceptionv1_epoch_32.pth", map_location=torch.device('cpu'))
33
- model.load_state_dict(checkpoint['model_state_dict'])
34
- model.to(DEVICE)
35
- model.eval()
36
 
37
  EXAMPLES_FOLDER = 'examples'
38
  examples_names = os.listdir(EXAMPLES_FOLDER)
@@ -48,55 +30,20 @@ for example_name in examples_names:
48
  np.random.shuffle(examples) # shuffle
49
 
50
  def predict(input_image:Image.Image, true_label:str):
51
- """Predict the label of the input_image"""
52
- face = mtcnn(input_image)
53
- if face is None:
54
- raise Exception('No face detected')
55
- face = face.unsqueeze(0) # add the batch dimension
56
- face = F.interpolate(face, size=(256, 256), mode='bilinear', align_corners=False)
57
-
58
- # convert the face into a numpy array to be able to plot it
59
- prev_face = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
60
- prev_face = prev_face.astype('uint8')
61
-
62
- face = face.to(DEVICE)
63
- face = face.to(torch.float32)
64
- face = face / 255.0
65
- face_image_to_plot = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
66
-
67
- target_layers=[model.block8.branch1[-1]]
68
- use_cuda = True if torch.cuda.is_available() else False
69
- cam = GradCAM(model=model, target_layers=target_layers, use_cuda=use_cuda)
70
- targets = [ClassifierOutputTarget(0)]
71
-
72
- grayscale_cam = cam(input_tensor=face, targets=targets, eigen_smooth=True)
73
- grayscale_cam = grayscale_cam[0, :]
74
- visualization = show_cam_on_image(face_image_to_plot, grayscale_cam, use_rgb=True)
75
- face_with_mask = cv2.addWeighted(prev_face, 1, visualization, 0.5, 0)
76
-
77
- with torch.no_grad():
78
- output = torch.sigmoid(model(face).squeeze(0))
79
- prediction = "real" if output.item() < 0.5 else "fake"
80
-
81
- real_prediction = 1 - output.item()
82
- fake_prediction = output.item()
83
-
84
- confidences = {
85
- 'real': real_prediction,
86
- 'fake': fake_prediction
87
- }
88
  return confidences, true_label, face_with_mask
89
 
90
  interface = gr.Interface(
91
  fn=predict,
92
  inputs=[
93
- gr.inputs.Image(label="Input Image", type="pil"),
94
  "text"
95
  ],
96
  outputs=[
97
- gr.outputs.Label(label="Class"),
98
  "text",
99
- gr.outputs.Image(label="Face with Explainability")
100
  ],
101
  examples=[[examples[i]["path"], examples[i]["label"]] for i in range(10)]
102
  ).launch()
 
10
  from pytorch_grad_cam import GradCAM
11
  from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
12
  from pytorch_grad_cam.utils.image import show_cam_on_image
13
+ from transformers import pipeline
14
  with zipfile.ZipFile("examples.zip","r") as zip_ref:
15
  zip_ref.extractall(".")
16
 
17
+ pipe = pipeline(model="not-lain/deepfake",trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  EXAMPLES_FOLDER = 'examples'
20
  examples_names = os.listdir(EXAMPLES_FOLDER)
 
30
  np.random.shuffle(examples) # shuffle
31
 
32
  def predict(input_image:Image.Image, true_label:str):
33
+ out = pipe.predict(input_image)
34
+ confidences,face_with_mask = out["confidences"], out["face_with_mask"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  return confidences, true_label, face_with_mask
36
 
37
  interface = gr.Interface(
38
  fn=predict,
39
  inputs=[
40
+ gr.Image(label="Input Image", type="filepath"),
41
  "text"
42
  ],
43
  outputs=[
44
+ gr.Label(label="Class"),
45
  "text",
46
+ gr.Image(label="Face with Explainability")
47
  ],
48
  examples=[[examples[i]["path"], examples[i]["label"]] for i in range(10)]
49
  ).launch()
resnetinceptionv1_epoch_32.pth DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:794ebe83c6a7d7959c30c175030b4885e2b9fa175f1cc3e582236595d119f52b
3
- size 282395989