Skip to content

Commit bcc6f5d

Browse files
committed
Add script footwear recognition
1 parent dc73e72 commit bcc6f5d

File tree

3 files changed

+302
-0
lines changed

3 files changed

+302
-0
lines changed

image_recognition_footwear/src/image_recognition_footwear/Footwear_detection.ipynb

Lines changed: 178 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import torch
2+
import torch.nn as nn
3+
class Model(nn.Module):
4+
def __init__(self, in_channels, channel_1, channel_2, channel_3, \
5+
node_1, node_2, num_classes):
6+
super().__init__()
7+
####### Convolutional layers ######
8+
self.conv1 = nn.Sequential(
9+
nn.Conv2d(in_channels, channel_1, kernel_size=3, padding=1, stride=1),
10+
nn.BatchNorm2d(channel_1),
11+
nn.LeakyReLU(),
12+
nn.Conv2d(channel_1, channel_1, kernel_size=3, padding=1, stride=1),
13+
nn.BatchNorm2d(channel_1),
14+
nn.LeakyReLU(),
15+
nn.MaxPool2d(kernel_size=2, stride=2),
16+
)
17+
self.conv2 = nn.Sequential(
18+
nn.Conv2d(channel_1, channel_2, kernel_size=3, padding=1, stride=1),
19+
nn.BatchNorm2d(channel_2),
20+
nn.LeakyReLU(),
21+
nn.Conv2d(channel_2, channel_2, kernel_size=3, padding=1, stride=1),
22+
nn.BatchNorm2d(channel_2),
23+
nn.LeakyReLU(),
24+
nn.MaxPool2d(kernel_size=2, stride=2),
25+
)
26+
self.conv3 = nn.Sequential(
27+
nn.Conv2d(channel_2, channel_3, kernel_size=3, padding=1, stride=1),
28+
nn.BatchNorm2d(channel_3),
29+
nn.LeakyReLU(),
30+
nn.Conv2d(channel_3, channel_3, kernel_size=3, padding=1, stride=1),
31+
nn.BatchNorm2d(channel_3),
32+
nn.LeakyReLU(),
33+
nn.MaxPool2d(kernel_size=7, stride=2),
34+
)
35+
36+
######## Affine layers ########
37+
self.fc = nn.Sequential(
38+
nn.Flatten(),
39+
nn.Linear(channel_3, node_1),
40+
nn.BatchNorm1d(node_1),
41+
nn.Dropout(p=0.5),
42+
43+
nn.Linear(node_1, node_2),
44+
nn.BatchNorm1d(node_2),
45+
46+
nn.Linear(node_2, num_classes)
47+
)
48+
49+
def forward(self, x):
50+
x = self.conv1(x)
51+
x = self.conv2(x)
52+
x = self.conv3(x)
53+
54+
scores = self.fc(x)
55+
return scores
56+
57+
58+
59+
60+
61+
62+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import numpy as np
2+
from torchvision import transforms as T
3+
import torch
4+
from PIL import Image, ImageOps
5+
6+
def preprocess_RGB(img):
7+
"""preproces image:
8+
input is a PIL image.
9+
Output image should be pytorch tensor that is compatible with your model"""
10+
img = T.functional.resize(img, size=(32, 32), interpolation=Image.NEAREST)
11+
trans = T.Compose([T.ToTensor(),T.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
12+
img = trans(img)
13+
img = img.unsqueeze(0)
14+
15+
return img
16+
17+
def detection_RGB(img, model):
18+
"""Detection of foortwear:
19+
Input is a preprocessed image to provide to the model.
20+
Output should be binary classification [True, False], where True is the detection of the footwear."""
21+
model.eval()
22+
info = next(model.parameters()) # Retrieve the first parameter tensor from the iterator
23+
device = info.device
24+
dtype = info.dtype
25+
with torch.no_grad():
26+
img = img.to(device=device, dtype=dtype)
27+
scores = model(img)
28+
preds = torch.argmax(scores, axis=1)
29+
score_max_numpy = int(preds.cpu().detach().numpy())
30+
return score_max_numpy
31+
32+
def preprocess_grayscale(img):
33+
"""preproces image:
34+
input is a PIL image.
35+
Output image should be pytorch tensor that is compatible with your model"""
36+
img = ImageOps.grayscale(img)
37+
img = T.functional.resize(img, size=(28, 28), interpolation=Image.NEAREST)
38+
trans = T.Compose([T.ToTensor(),T.Normalize((0.5,), (0.5,))])
39+
img = trans(img)
40+
img = img.unsqueeze(0)
41+
42+
return img
43+
44+
def detection_grayscale(img, model):
45+
"""Detection of foortwear:
46+
Input is a preprocessed image to provide to the model.
47+
Output should be binary classification [True, False], where True is the detection of the footwear."""
48+
model.eval()
49+
info = next(model.parameters()) # Retrieve the first parameter tensor from the iterator
50+
device = info.device
51+
dtype = info.dtype
52+
with torch.no_grad():
53+
img = img.to(device=device, dtype=dtype)
54+
scores = model(img)
55+
preds = torch.argmax(scores, axis=1)
56+
score_max_numpy = int(preds.cpu().detach().numpy())
57+
if score_max_numpy == 5 or score_max_numpy == 7 or score_max_numpy == 9:
58+
#{'Sandal': 5,'Sneaker': 7,'Ankle boot': 9}
59+
detected = True
60+
else:
61+
detected = False
62+
return detected

0 commit comments

Comments
 (0)