-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
164 lines (119 loc) · 5.5 KB
/
inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import argparse
import numpy as np
from PIL import Image
import cv2
import torch
from tqdm import tqdm
# Assuming model_ft is defined elsewhere in your code
# model_ft = ...
# Define the label_col
label_col = np.array(['Age-Young', 'Age-Adult', 'Age-Old', 'Gender-Female',
'Hair-Length-Short', 'Hair-Length-Long', 'Hair-Length-Bald',
'UpperBody-Length-Short', 'UpperBody-Color-Black',
'UpperBody-Color-Blue', 'UpperBody-Color-Brown',
'UpperBody-Color-Green', 'UpperBody-Color-Grey',
'UpperBody-Color-Orange', 'UpperBody-Color-Pink',
'UpperBody-Color-Purple', 'UpperBody-Color-Red',
'UpperBody-Color-White', 'UpperBody-Color-Yellow',
'UpperBody-Color-Other', 'LowerBody-Length-Short',
'LowerBody-Color-Black', 'LowerBody-Color-Blue',
'LowerBody-Color-Brown', 'LowerBody-Color-Green',
'LowerBody-Color-Grey', 'LowerBody-Color-Orange',
'LowerBody-Color-Pink', 'LowerBody-Color-Purple', 'LowerBody-Color-Red',
'LowerBody-Color-White', 'LowerBody-Color-Yellow',
'LowerBody-Color-Other', 'LowerBody-Type-Trousers&Shorts',
'LowerBody-Type-Skirt&Dress', 'Accessory-Backpack', 'Accessory-Bag',
'Accessory-Glasses-Normal', 'Accessory-Glasses-Sun', 'Accessory-Hat'])
def preprocess_image(image_path, resize=(224, 224)):
# Open image with OpenCV
image = cv2.imread(image_path)
# Make sure image is in RGB format (3 channels)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Resize if needed
if resize is not None:
image = cv2.resize(image, resize)
# Normalize using mean and std
mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225)
normalized_img = (image / 255.0 - mean) / std
# Convert NumPy array to PyTorch tensor
img_tensor = torch.from_numpy(normalized_img).permute(2, 0, 1).float()
return img_tensor
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def perform_inference(model, image_path):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()
predicted_results = []
normalized_image = preprocess_image(image_path)
normalized_image_tensor = normalized_image.to(device)
normalized_image_tensor = normalized_image_tensor.unsqueeze(0)
with torch.no_grad():
output = model(normalized_image_tensor)
# print(output)
predicted_probs = output.cpu().numpy().astype(float)
predicted_probs = sigmoid(predicted_probs)
predicted_results = predicted_probs[0] >0.5
pos = np.where(predicted_results==1)[0]
return {"labels" :label_col[pos],"prob":predicted_probs[0][pos]}
def get_label_from_index(index):
return label_col[index]
import cv2
import numpy as np
import matplotlib.pyplot as plt
# ... (previous code remains unchanged)
def perform_inference_with_visualization(model, image_path, output_path):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()
# Create an empty white image
white_image = np.ones((256, 256, 3), dtype=np.uint8) * 255
# Load the person image
person_image = cv2.imread(image_path)
person_image = cv2.cvtColor(person_image, cv2.COLOR_BGR2RGB)
# Resize the person image to fit within the white image
person_image = cv2.resize(person_image, (128, 64))
# Calculate the position to center the person image in the white image
y_offset = (256 - person_image.shape[0]) // 2
x_offset = (256 - person_image.shape[1]) // 2
# Place the person image on the white image
white_image[y_offset:y_offset + person_image.shape[0], x_offset:x_offset + person_image.shape[1]] = person_image
predicted_results = []
normalized_image = preprocess_image(image_path)
normalized_image_tensor = normalized_image.to(device)
normalized_image_tensor = normalized_image_tensor.unsqueeze(0)
with torch.no_grad():
output = model(normalized_image_tensor)
predicted_probs = output.cpu().numpy().astype(float)
predicted_probs = sigmoid(predicted_probs)
predicted_results = predicted_probs[0] > 0.5
pos = np.where(predicted_results == 1)[0]
labels = label_col[pos]
probs = predicted_probs[0][pos]
# Draw text labels on the image
for label, prob in zip(labels, probs):
text = f"{label}: {prob:.2f}"
cv2.putText(white_image, text, ( person_image.shape[0], person_image.shape[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
# Display the result image
plt.imshow(white_image)
plt.axis('off')
plt.show()
# Save the result image
cv2.imwrite(output_path, cv2.cvtColor(white_image, cv2.COLOR_RGB2BGR))
return {"labels": labels, "prob": probs}
# ... (main function remains unchanged)
def main():
parser = argparse.ArgumentParser(description='Perform inference on an image using a trained PyTorch model.')
parser.add_argument('--model_path', type=str, default='./models/ResNet18_best_model.pth', help='Path to the trained PyTorch model file')
parser.add_argument('--image_path', type=str, required=True, help='Path to the input image for inference')
args = parser.parse_args()
print(args.model_path)
print(args.image_path)
# Load the model
model_ft = torch.load(args.model_path)
# Perform inference on the input image
results = perform_inference(model_ft, args.image_path)
print("Predicted results:", results)
if __name__ == "__main__":
main()