Ammar2k commited on
Commit
93267bd
·
1 Parent(s): 5cb107d

first commit

Browse files
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import torch
4
+
5
+ from model import create_vit
6
+ from timeit import default_timer as timer
7
+ from typing import Tuple, Dict
8
+
9
+ class_names = ["buildings", "forest", "glacier", "mountain", "sea", "street"]
10
+
11
+ vit_model, vit_transforms = create_vit(num_classes=len(class_names),
12
+ seed=42)
13
+
14
+ vit_model.load_state_dict(
15
+ torch.load(
16
+ f="pretrained_vit_feature_extractor_scene_recognition.pth",
17
+ map_location=torch.device("cpu")
18
+ )
19
+ )
20
+
21
+ def predict(img):
22
+ start_timer = timer()
23
+
24
+ img = vit_transforms(img).unsqueeze(0)
25
+
26
+ vit_model.eval()
27
+ with torch.inference_mode():
28
+ pred_prob = torch.softmax(vit_model(img), dim=1)
29
+
30
+ pred_labels_and_probs = {class_names[i]: float(pred_prob[0][i]) for i in range(len(class_names))}
31
+
32
+ pred_time = round(timer() - start_timer, 5)
33
+
34
+ return pred_labels_and_probs, pred_time
35
+
36
+ title = "Scene Recognition: Intel Image Classification"
37
+ description = "A ViT feature extractor Computer Vision model to classify images of scenes from 1 out of 6 classes."
38
+ article = "Access project repository at [GitHub](https://github.com/Ammar2k/intel_image_classification)"
39
+
40
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
41
+
42
+ demo = gr.Interface(fn=predict,
43
+ inputs=gr.Image(type="pil"),
44
+ outputs=[gr.Label(num_top_classes=6, label="Predictions"),
45
+ gr.Number(label="Prediction time(s)")],
46
+ examples=example_list,
47
+ title=title,
48
+ description=description,
49
+ article=article
50
+ )
51
+
52
+ demo.launch()
examples/20080.jpg ADDED
examples/20087.jpg ADDED
examples/20140.jpg ADDED
examples/20176.jpg ADDED
examples/20225.jpg ADDED
examples/20247.jpg ADDED
model.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ def create_vit(num_classes: int=6,
5
+ seed: int=42):
6
+ weights = torchvision.models.ViT_B_16_Weights.DEFAULT
7
+
8
+ transforms = weights.transforms()
9
+
10
+ model = torchvision.models.vit_b_16(weights=weights)
11
+
12
+ for param in model.parameters():
13
+ param.requires_grad = False
14
+
15
+ torch.manual_seed(seed)
16
+ model.heads = torch.nn.Sequential(torch.nn.LayerNorm(normalized_shape=768),
17
+ torch.nn.Linear(in_features=768, out_features=num_classes))
18
+ return model, transforms
pretrained_vit_feature_extractor_scene_recognition.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6a388a90788d45eee524052ed07aede4d9c39a540951c87b269224d907b47005
3
+ size 343286657
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.0.0
2
+ torchvision==0.15.0
3
+ gradio==3.34.0