Harinivas-28 commited on
Commit
bc7d6c8
·
verified ·
1 Parent(s): 72b5f14

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +111 -1
README.md CHANGED
@@ -14,4 +14,114 @@ tags:
14
  - image
15
  - detection
16
  - density-map
17
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  - image
15
  - detection
16
  - density-map
17
+ ---
18
+
19
+ ## Model Architecture used
20
+ ``` python
21
+ import torch
22
+ import torch.nn as nn
23
+
24
+ class VGG16(nn.Module):
25
+ def __init__(self):
26
+ super(VGG16, self).__init__()
27
+ self.features = nn.Sequential(
28
+ nn.Conv2d(3, 64, kernel_size=3, padding=1),
29
+ nn.ReLU(inplace=True),
30
+ nn.Conv2d(64, 64, kernel_size=3, padding=1),
31
+ nn.ReLU(inplace=True),
32
+ nn.MaxPool2d(kernel_size=2, stride=2),
33
+
34
+ nn.Conv2d(64, 128, kernel_size=3, padding=1),
35
+ nn.ReLU(inplace=True),
36
+ nn.Conv2d(128, 128, kernel_size=3, padding=1),
37
+ nn.ReLU(inplace=True),
38
+ nn.MaxPool2d(kernel_size=2, stride=2),
39
+
40
+ nn.Conv2d(128, 256, kernel_size=3, padding=1),
41
+ nn.ReLU(inplace=True),
42
+ nn.Conv2d(256, 256, kernel_size=3, padding=1),
43
+ nn.ReLU(inplace=True),
44
+ nn.Conv2d(256, 256, kernel_size=3, padding=1),
45
+ nn.ReLU(inplace=True),
46
+ nn.MaxPool2d(kernel_size=2, stride=2),
47
+
48
+ nn.Conv2d(256, 512, kernel_size=3, padding=1),
49
+ nn.ReLU(inplace=True),
50
+ nn.Conv2d(512, 512, kernel_size=3, padding=1),
51
+ nn.ReLU(inplace=True),
52
+ nn.Conv2d(512, 512, kernel_size=3, padding=1),
53
+ nn.ReLU(inplace=True),
54
+ nn.MaxPool2d(kernel_size=2, stride=2),
55
+
56
+ nn.Conv2d(512, 512, kernel_size=3, padding=1),
57
+ nn.ReLU(inplace=True),
58
+ nn.Conv2d(512, 512, kernel_size=3, padding=1),
59
+ nn.ReLU(inplace=True),
60
+ nn.Conv2d(512, 512, kernel_size=3, padding=1),
61
+ nn.ReLU(inplace=True),
62
+ nn.MaxPool2d(kernel_size=2, stride=2),
63
+ )
64
+ self.classifier = nn.Sequential(
65
+ nn.Linear(512 * 7 * 7, 4096),
66
+ nn.ReLU(inplace=True),
67
+ nn.Dropout(),
68
+ nn.Linear(4096, 4096),
69
+ nn.ReLU(inplace=True),
70
+ nn.Dropout(),
71
+ nn.Linear(4096, 1) # Outputting head count as a single value
72
+ )
73
+
74
+ def forward(self, x):
75
+ x = self.features(x)
76
+ x = torch.flatten(x, 1)
77
+ x = self.classifier(x)
78
+ return x
79
+
80
+ ```
81
+
82
+ ## Model Usage
83
+ ``` python
84
+ # Preprocessing function
85
+ def preprocess_image(image, channels=6):
86
+ transform = transforms.Compose([
87
+ transforms.Resize((224, 224)),
88
+ transforms.ToTensor()
89
+ ])
90
+ image_tensor = transform(image)
91
+ # Simulating 6-channel input if required
92
+ if channels == 6:
93
+ image_tensor = torch.cat([image_tensor, image_tensor], dim=0)
94
+ return image_tensor.unsqueeze(0).to(device)
95
+
96
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
97
+ # Load model
98
+ def load_model(selected_model):
99
+ model = None
100
+ model_path = None
101
+ if selected_model == 'VGG16':
102
+ model = models.VGG16()
103
+ model_path = "vgg16_headcount.pth"
104
+ else:
105
+ model = models.ResNet50()
106
+ model_path = "resnet50_headcount.pth"
107
+ model.load_state_dict(torch.load(model_path, map_location=device, weights_only=True))
108
+ model.to(device)
109
+ model.eval()
110
+ print(f"{selected_model}.Heavy Model loaded successfully")
111
+ return model
112
+
113
+ # Prediction Function
114
+ def process_image(image, model):
115
+ preprocess = transforms.Compose([
116
+ transforms.Resize((224, 224)),
117
+ transforms.ToTensor(),
118
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
119
+ ])
120
+ input_tensor = preprocess(image).unsqueeze(0)
121
+ input_tensor = input_tensor.to(device)
122
+ with torch.no_grad():
123
+ output = model(input_tensor)
124
+ predicted_count = output.item()
125
+ print(f"Predicted Headcount: {predicted_count}")
126
+ return math.ceil(predicted_count)
127
+ ```