chbsaikiran commited on
Commit
4ef73c8
·
1 Parent(s): d34a787

Code with resnet50 model binary and app.py

Browse files
Files changed (3) hide show
  1. README.md +32 -0
  2. app.py +52 -0
  3. model_32.pth +3 -0
README.md CHANGED
@@ -11,3 +11,35 @@ short_description: ResNet50 model trained on ImageNet Dataset of 1000 classes
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
+
15
+ <pre>
16
+ Epoch: [10] Total time: 0:34:49
17
+ Test: [ 0/196] eta: 0:15:22 loss: 2.3581 (2.3581) acc1: 63.6719 (63.6719) acc5: 89.0625 (89.0625) time: 4.7075 data: 4.4695 max mem: 12905
18
+ Test: [100/196] eta: 0:00:29 loss: 3.3925 (2.8562) acc1: 38.2812 (51.5161) acc5: 66.7969 (79.0068) time: 0.2379 data: 0.0003 max mem: 12905
19
+ Test: Total time: 0:00:54
20
+ Test: Acc@1 47.506 Acc@5 74.236
21
+
22
+ Epoch: [11] Total time: 0:34:49
23
+ Test: [ 0/196] eta: 0:15:56 loss: 2.4094 (2.4094) acc1: 64.4531 (64.4531) acc5: 89.0625 (89.0625) time: 4.8820 data: 4.6439 max mem: 12905
24
+ Test: [100/196] eta: 0:00:30 loss: 3.4837 (2.9939) acc1: 36.3281 (49.0215) acc5: 67.1875 (76.7481) time: 0.2523 data: 0.0147 max mem: 12905
25
+ Test: Total time: 0:00:54
26
+ Test: Acc@1 46.916 Acc@5 73.774
27
+
28
+ Epoch: [12] Total time: 0:34:49
29
+ Test: [ 0/196] eta: 0:16:04 loss: 2.1682 (2.1682) acc1: 69.1406 (69.1406) acc5: 92.5781 (92.5781) time: 4.9221 data: 4.6836 max mem: 12905
30
+ Test: [100/196] eta: 0:00:29 loss: 3.5695 (2.8364) acc1: 35.1562 (52.0614) acc5: 63.6719 (79.2659) time: 0.2545 data: 0.0168 max mem: 12905
31
+ Test: Total time: 0:00:54
32
+ Test: Acc@1 48.304 Acc@5 74.626
33
+
34
+ Epoch: [13] Total time: 0:34:48
35
+ Test: [ 0/196] eta: 0:17:28 loss: 2.2632 (2.2632) acc1: 68.3594 (68.3594) acc5: 89.4531 (89.4531) time: 5.3511 data: 5.1130 max mem: 12905
36
+ Test: [100/196] eta: 0:00:30 loss: 3.5760 (2.9239) acc1: 39.4531 (52.5101) acc5: 66.7969 (78.9720) time: 0.2380 data: 0.0003 max mem: 12905
37
+ Test: Total time: 0:00:54
38
+ Test: Acc@1 48.908 Acc@5 74.982
39
+
40
+ Epoch: [14] Total time: 0:34:49
41
+ Test: [ 0/196] eta: 0:16:47 loss: 2.2360 (2.2360) acc1: 65.6250 (65.6250) acc5: 90.2344 (90.2344) time: 5.1387 data: 4.9008 max mem: 12905
42
+ Test: [100/196] eta: 0:00:30 loss: 3.4053 (2.8317) acc1: 38.6719 (52.1117) acc5: 69.1406 (79.7030) time: 0.2428 data: 0.0051 max mem: 12905
43
+ Test: Total time: 0:00:54
44
+ Test: Acc@1 48.958 Acc@5 75.700
45
+ </pre>
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision.transforms as transforms
3
+ from torchvision import models
4
+ import gradio as gr
5
+ from PIL import Image
6
+
7
+ # Load the ResNet50 model
8
+ model = models.resnet50()
9
+ model.fc = torch.nn.Linear(model.fc.in_features, 1000) # Update for 1000 classes
10
+ model.load_state_dict(torch.load("model_32.pth", map_location="cpu"))
11
+ model.eval()
12
+
13
+ # ImageNet class labels
14
+ imagenet_classes = [line.strip() for line in open("imagenet_classes.txt")]
15
+
16
+ # Define image preprocessing
17
+ def preprocess_image(image):
18
+ transform = transforms.Compose([
19
+ transforms.Resize((224, 224)),
20
+ transforms.ToTensor(),
21
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
22
+ ])
23
+ return transform(image).unsqueeze(0)
24
+
25
+ # Define prediction function
26
+ def predict(image):
27
+ image = preprocess_image(image)
28
+ with torch.no_grad():
29
+ outputs = model(image)
30
+ probabilities = torch.nn.functional.softmax(outputs[0], dim=0)
31
+ top5_prob, top5_catid = torch.topk(probabilities, 5)
32
+
33
+ results = []
34
+ for i in range(top5_prob.size(0)):
35
+ results.append((imagenet_classes[top5_catid[i]], top5_prob[i].item()))
36
+ return results
37
+
38
+ # Gradio Interface
39
+ def classify_image(image):
40
+ results = predict(image)
41
+ return {"Class": [r[0] for r in results], "Probability": [r[1] for r in results]}
42
+
43
+ interface = gr.Interface(
44
+ fn=classify_image,
45
+ inputs=gr.Image(type="pil"),
46
+ outputs=gr.Label(num_top_classes=5),
47
+ title="ResNet50 Image Classifier",
48
+ description="Upload an image to get the top 5 predictions using a ResNet50 model trained on ImageNet."
49
+ )
50
+
51
+ if __name__ == "__main__":
52
+ interface.launch()
model_32.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:acf3cff2eb11da67bfcad4323a784db7305f1c0afd7e3a6bf411c38903d0f51b
3
+ size 204824981