Shilpaj commited on
Commit
077fb0c
·
verified ·
1 Parent(s): 9280d33

Feat: Files for application

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ assets/examples/plane.jpg filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,13 +1,16 @@
1
- ---
2
- title: ImageNet
3
- emoji: 📉
4
- colorFrom: pink
5
- colorTo: green
6
- sdk: gradio
7
- sdk_version: 5.9.1
8
- app_file: app.py
9
- pinned: false
10
- short_description: RestNet50 trained on ImageNet
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
1
+ # ResNet50 trained on ImageNet-1K
2
+
3
+ Model trained on ImageNet-1K with 1000 classes.
4
+
5
+ ## Model
6
+
7
+ `resnet50_imagenet1k.pth`
8
+
9
+
10
+ ## Usage
11
+
12
+ 1. Download the model from the link above.
13
+ 2. Use the model in your project.
14
+
15
+ ```python
16
+ ```
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ """
3
+ Application for ResNet50 trained on ImageNet-1K.
4
+ """
5
+ # Standard Library Imports
6
+ import gradio as gr
7
+
8
+ # Third Party Imports
9
+ import torch
10
+ from torchvision import models
11
+
12
+ # Local Imports
13
+ from inference import inference
14
+
15
+
16
+ def load_model(model_path: str):
17
+ """
18
+ Load the model.
19
+ """
20
+ # Load the pre-trained ResNet50 model from ImageNet
21
+ model = models.resnet50(pretrained=False)
22
+
23
+ # Load custom weights from a .pth file
24
+ state_dict = torch.load(model_path)
25
+
26
+ # Filter out unexpected keys
27
+ filtered_state_dict = {k: v for k, v in state_dict['model_state_dict'].items() if k in model.state_dict()}
28
+
29
+ # Load the filtered state dictionary into the model
30
+ model.load_state_dict(filtered_state_dict, strict=False)
31
+ return model
32
+
33
+
34
+ def load_classes():
35
+ """
36
+ Load the classes.
37
+ """
38
+ # Get ImageNet class names from ResNet50 weights
39
+ classes = models.ResNet50_Weights.IMAGENET1K_V2.meta["categories"]
40
+ return classes
41
+
42
+
43
+ def main():
44
+ """
45
+ Main function for the application.
46
+ """
47
+ # Load the model at startup
48
+ model = load_model("resnet50_imagenet1k.pth")
49
+
50
+ # Load the classes at startup
51
+ classes = load_classes()
52
+
53
+ with gr.Blocks() as demo:
54
+ gr.Markdown(
55
+ """
56
+ # ImageNet-1K trained on ResNet50v2
57
+ """
58
+ )
59
+
60
+ # #############################################################################
61
+ # ################################ GradCam Tab ################################
62
+ # #############################################################################
63
+ with gr.Tab("GradCam"):
64
+ gr.Markdown(
65
+ """
66
+ Visualize Class Activations Maps generated by the model's layer for the predicted class.
67
+ This is used to see what the model is actually looking at in the image.
68
+ """
69
+ )
70
+ with gr.Row():
71
+ # Update the image input dimensions
72
+ img_input = [gr.Image(label="Input Image", type="numpy", height=224)] # Changed dimensions
73
+ gradcam_outputs = [
74
+ gr.Label(label="Predictions"),
75
+ gr.Image(label="GradCAM Output", height=224) # Match input image height
76
+ ]
77
+
78
+ with gr.Row():
79
+ gradcam_inputs = [
80
+ gr.Slider(0, 1, value=0.5, label="Activation Map Transparency"),
81
+ gr.Slider(1, 10, value=3, step=1, label="Number of Top Predictions"),
82
+ gr.Slider(1, 6, value=4, step=1, label="Target Layer Number")
83
+ ]
84
+
85
+ gradcam_button = gr.Button("Generate GradCAM")
86
+ # Pass model to inference function using partial
87
+ from functools import partial
88
+ inference_fn = partial(inference, model=model, classes=classes)
89
+ gradcam_button.click(inference_fn, inputs=img_input + gradcam_inputs, outputs=gradcam_outputs)
90
+
91
+ gr.Markdown("## Examples")
92
+ gr.Examples(
93
+ examples=[
94
+ ["./assets/examples/dog.jpg", 0.5, 3, 4],
95
+ ["./assets/examples/cat.jpg", 0.5, 3, 4],
96
+ ["./assets/examples/frog.jpg", 0.5, 3, 4],
97
+ ["./assets/examples/bird.jpg", 0.5, 3, 4],
98
+ ["./assets/examples/shark-plane.jpg", 0.5, 3, 4],
99
+ ["./assets/examples/car.jpg", 0.5, 3, 4],
100
+ ["./assets/examples/truck.jpg", 0.5, 3, 4],
101
+ ["./assets/examples/horse.jpg", 0.5, 3, 4],
102
+ ["./assets/examples/plane.jpg", 0.5, 3, 4],
103
+ ["./assets/examples/ship.png", 0.5, 3, 4]
104
+ ],
105
+ inputs=img_input + gradcam_inputs,
106
+ fn=inference_fn,
107
+ outputs=gradcam_outputs
108
+ )
109
+
110
+ gr.close_all()
111
+ demo.launch(debug=True)
112
+
113
+
114
+ if __name__ == "__main__":
115
+ main()
assets/examples/bird.jpg ADDED
assets/examples/car.jpg ADDED
assets/examples/cat.jpg ADDED
assets/examples/dog.jpg ADDED
assets/examples/frog.jpg ADDED
assets/examples/horse.jpg ADDED
assets/examples/plane.jpg ADDED

Git LFS Details

  • SHA256: da6f28a859fa7137748904be0e7f4355f00fc66e600671c3448b9e9d7ce8f14b
  • Pointer size: 132 Bytes
  • Size of remote file: 2.52 MB
assets/examples/shark-plane.jpg ADDED
assets/examples/ship.png ADDED
assets/examples/truck.jpg ADDED
inference.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ """
3
+ Inference script for ResNet50 trained on ImageNet-1K.
4
+ """
5
+ # Standard Library Imports
6
+ import numpy as np
7
+ import torch
8
+ from collections import OrderedDict
9
+
10
+ # Third Party Imports
11
+ from torchvision import transforms
12
+ from torch.nn import functional as F
13
+ from torchvision.models import resnet50
14
+ from pytorch_grad_cam import GradCAM
15
+ from pytorch_grad_cam.utils.image import show_cam_on_image
16
+ from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
17
+
18
+
19
+ def inference(input_img,
20
+ model,
21
+ classes,
22
+ transparency=0.5,
23
+ number_of_top_classes=3,
24
+ target_layer_number=4):
25
+ """
26
+ Function to run inference on the input image
27
+ :param input_img: Image provided by the user
28
+ :param model: Model to use for inference
29
+ :param classes: Classes to use for inference
30
+ :param transparency: Percentage of cam overlap over the input image
31
+ :param number_of_top_classes: Number of top predictions for the input image
32
+ :param target_layer_number: Layer for which GradCam to be shown
33
+ """
34
+ # Save a copy of input img
35
+ org_img = input_img.copy()
36
+
37
+ # Calculate mean over each channel of input image
38
+ mean_r, mean_g, mean_b = np.mean(input_img[:, :, 0]/255.), np.mean(input_img[:, :, 1]/255.), np.mean(input_img[:, :, 2]/255.)
39
+
40
+ # Calculate Standard deviation over each channel
41
+ std_r, std_g, std_b = np.std(input_img[:, :, 0]/255.), np.std(input_img[:, :, 1]/255.), np.std(input_img[:, :, 2]/255.)
42
+
43
+ # Convert img to tensor and normalize it
44
+ _transform = transforms.Compose([
45
+ transforms.ToTensor(),
46
+ transforms.Normalize((mean_r, mean_g, mean_b), (std_r, std_g, std_b))
47
+ ])
48
+
49
+ # Preprocess the input image
50
+ input_tensor = _transform(input_img)
51
+
52
+ # Create a mini-batch as expected by the model
53
+ input_tensor = input_tensor.unsqueeze(0)
54
+
55
+ # Move the input and model to GPU if available
56
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
57
+ input_tensor = input_tensor.to(device)
58
+ model.to(device)
59
+
60
+ # Get Model Predictions
61
+ with torch.no_grad():
62
+ outputs = model(input_tensor)
63
+ probabilities = torch.softmax(outputs, dim=1)[0]
64
+ del outputs
65
+ confidences = {classes[i]: float(probabilities[i]) for i in range(1000)}
66
+
67
+ # Select the top classes based on user input
68
+ sorted_confidences = sorted(confidences.items(), key=lambda val: val[1], reverse=True)
69
+ show_confidences = OrderedDict(sorted_confidences[:number_of_top_classes])
70
+
71
+ # Map layer numbers to meaningful parts of the ResNet architecture
72
+ _layers = {
73
+ 1: model.conv1, # Initial convolution layer
74
+ 2: model.layer1[-1], # Last bottleneck of first residual block
75
+ 3: model.layer2[-1], # Last bottleneck of second residual block
76
+ 4: model.layer3[-1], # Last bottleneck of third residual block
77
+ 5: model.layer4[-1], # Last bottleneck of fourth residual block
78
+ 6: model.layer4[-1] # Changed from fc to last conv layer for better visualization
79
+ }
80
+
81
+ # Ensure valid layer selection
82
+ target_layer_number = min(max(target_layer_number, 1), 6)
83
+ target_layers = [_layers[target_layer_number]]
84
+
85
+ # Get the class activations from the selected layer
86
+ cam = GradCAM(model=model, target_layers=target_layers)
87
+
88
+ # Get the most probable class index
89
+ top_class = max(confidences.items(), key=lambda x: x[1])[0]
90
+ class_idx = classes.index(top_class)
91
+
92
+ # Generate GradCAM for the top predicted class
93
+ grayscale_cam = cam(input_tensor=input_tensor,
94
+ targets=[ClassifierOutputTarget(class_idx)],
95
+ aug_smooth=True,
96
+ eigen_smooth=True)
97
+ model.eval()
98
+ grayscale_cam = grayscale_cam[0, :]
99
+
100
+ # Overlay input image with Class activations
101
+ visualization = show_cam_on_image(org_img/255., grayscale_cam, use_rgb=True, image_weight=transparency)
102
+ return show_confidences, visualization
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio==3.38.1
2
+ grad-cam==1.6.1
3
+ numpy==1.25.2
4
+ torch==2.0.1+cpu
5
+ torchvision==0.15.2+cpu