NizamuddinMandekar commited on
Commit
94586b6
·
1 Parent(s): d4ceb1f

inital commit

Browse files
Files changed (3) hide show
  1. app.py +77 -0
  2. model.py +28 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_model
7
+ from timeit import default_timer as timer
8
+ from typing import Tuple, Dict
9
+
10
+ # Setup class names
11
+ class_names = ['Real', 'Fake']
12
+
13
+ ### 2. Model and transforms preparation ###
14
+
15
+ # Create model
16
+ model, model_transforms = create_model(
17
+ num_classes=2,
18
+ )
19
+
20
+ # Load saved weights
21
+ model.load_state_dict(
22
+ torch.load(
23
+ f="RealityCheck.pth",
24
+ map_location=torch.device("cpu"), # load to CPU
25
+ )
26
+ )
27
+
28
+ ### 3. Predict function ###
29
+
30
+ # Create predict function
31
+ def predict(img) -> Tuple[Dict, float]:
32
+ """Transforms and performs a prediction on img and returns prediction and time taken.
33
+ """
34
+ # Start the timer
35
+ start_time = timer()
36
+
37
+ # Transform the target image and add a batch dimension
38
+ img = model_transforms(img).unsqueeze(0)
39
+
40
+ # Put model into evaluation mode and turn on inference mode
41
+ model.eval()
42
+ with torch.inference_mode():
43
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
44
+ pred_probs = torch.softmax(model(img), dim=1)
45
+
46
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
47
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
48
+
49
+ # Calculate the prediction time
50
+ pred_time = round(timer() - start_time, 5)
51
+
52
+ # Return the prediction dictionary and prediction time
53
+ return pred_labels_and_probs, pred_time
54
+
55
+ ### 4. Gradio app ###
56
+
57
+ # Create title, description and article strings
58
+ title = "Reality Check"
59
+ description = "An computer vision model to classify images as Real Or Fake(Ai Generated). The Model can classify the images with 95% Accuracy"
60
+ article = "Created at [Real Or Fake]"
61
+
62
+ # Create examples list from "examples/" directory
63
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
64
+
65
+ # Create the Gradio demo
66
+ demo = gr.Interface(fn=predict, # mapping function from input to output
67
+ inputs=gr.Image(type="pil"), # what are the inputs?
68
+ outputs=[gr.Label(num_top_classes=2, label="Predictions"), # what are the outputs?
69
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
70
+ # Create examples list from "examples/" directory
71
+ examples=example_list,
72
+ title=title,
73
+ description=description,
74
+ article=article)
75
+
76
+ # Launch the demo!
77
+ demo.launch()
model.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torchvision
4
+ from torch import nn
5
+
6
+ def create_model(num_classes:int=2,
7
+ seed:int=42):
8
+ weights=torchvision.models.ResNet50_Weights.DEFAULT
9
+ transforms=weights.transforms
10
+ model=torchvision.models.resnet50(weights=weights)
11
+
12
+ for param in model.parameters():
13
+ param.requires_grad=False
14
+
15
+ torch.manual_seed(42)
16
+ model.fc= torch.nn.Sequential(
17
+ torch.nn.Linear(2048,1000),
18
+ torch.nn.ReLU(),
19
+ torch.nn.Linear(1000,500),
20
+ torch.nn.Dropout(),
21
+ torch.nn.Linear(in_features=500,
22
+ out_features=output_shape,
23
+ bias=True)
24
+
25
+ )
26
+
27
+ return model,transforms
28
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.3.0
2
+ torchvision==0.18.0
3
+ gradio==4.32.2