-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
317 lines (235 loc) · 10.7 KB
/
Copy pathtest.py
File metadata and controls
317 lines (235 loc) · 10.7 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# asking IA from Bing "python inference for a fastrcnn_fpn mode"
# and adapted and slightly modified by Alfonso Blanco
# Dataset https://universe.roboflow.com/team-roboflow/blood-cell-detection-1ekwu/dataset/3
# model to test https://universe.roboflow.com/team-roboflow/blood-cell-detection-1ekwu
import torch
import torchvision
from torchvision.transforms import functional as F
from PIL import Image, ImageDraw, ImageFont
import requests
from io import BytesIO
import matplotlib.pyplot as plt
import os
import re
# PARAMETERS
CONF_THRESHOLD=0.8
SHOW_OPTION = "YES"
imgpath="Blood-Cell-Detection-3\\test\\" # images test folder coco json
# Path to your saved weights file (.pth or .pt)
weights_path = "fasterrcnn_fpn.pth"
LabelClass="blood cell "
# asking IA Bing PIL draw rectangle width changes with rectangle width python
def draw_scaled_rectangle(draw, xy, outline, scale_factor=0.05):
x0, y0, x1, y1 = xy
rect_width = abs(x1 - x0)
rect_height = abs(y1 - y0)
stroke_width = max(1, int(min(rect_width, rect_height) * scale_factor))
draw.rectangle(xy, outline=outline, width=stroke_width)
import os
import torch
import torchvision
from torch.utils.data import DataLoader
from torchvision.models.detection import fasterrcnn_resnet50_fpn
from torchvision.transforms import functional as F
from PIL import Image
import json
# -----------------------------
# 1. Custom Dataset Class
# -----------------------------
class CustomDataset(torch.utils.data.Dataset):
def __init__(self, images_dir, annotations_file, transforms=None):
self.images_dir = images_dir
self.transforms = transforms
# Load COCO-style annotations
with open(annotations_file) as f:
coco_data = json.load(f)
self.images_info = coco_data["images"]
self.annotations = coco_data["annotations"]
# Map image_id -> list of annotations
self.image_to_anns = {}
for ann in self.annotations:
self.image_to_anns.setdefault(ann["image_id"], []).append(ann)
# Category mapping
self.cat_id_to_name = {cat["id"]: cat["name"] for cat in coco_data["categories"]}
def __getitem__(self, idx):
img_info = self.images_info[idx]
img_path = os.path.join(self.images_dir, img_info["file_name"])
filename= img_info["file_name"]
img = Image.open(img_path).convert("RGB")
anns = self.image_to_anns.get(img_info["id"], [])
boxes = []
labels = []
for ann in anns:
xmin, ymin, w, h = ann["bbox"]
boxes.append([xmin, ymin, xmin + w, ymin + h])
labels.append(ann["category_id"])
boxes = torch.as_tensor(boxes, dtype=torch.float32)
labels = torch.as_tensor(labels, dtype=torch.int64)
image_id = torch.tensor([img_info["id"]])
area = torch.as_tensor([ann["area"] for ann in anns], dtype=torch.float32)
iscrowd = torch.zeros((len(anns),), dtype=torch.int64)
target = {
"boxes": boxes,
"labels": labels,
"image_id": image_id,
"area": area,
"iscrowd": iscrowd
}
if self.transforms:
img = self.transforms(img)
return img, target, img_path, filename
def __len__(self):
return len(self.images_info)
# -----------------------------
# 2. Data Transforms
# -----------------------------
def get_transform(train):
transforms = []
transforms.append(F.to_tensor)
return torchvision.transforms.Compose([
torchvision.transforms.ToTensor()
])
# -----------------------------
# 1. Load Pretrained Model
# -----------------------------
import torch
from torchvision.models.detection import fasterrcnn_resnet50_fpn
# 1️⃣ Create the model architecture (no pretrained weights)
model = fasterrcnn_resnet50_fpn(weights=None) # For torchvision >= 0.13
# For older versions:
#model = fasterrcnn_resnet50_fpn(pretrained=False)
in_features = model.roi_heads.box_predictor.cls_score.in_features
# Replace head for custom classes
model.roi_heads.box_predictor = torchvision.models.detection.faster_rcnn.FastRCNNPredictor(in_features, num_classes=4)
# 2️⃣ Load the saved state dictionary
try:
state_dict = torch.load(weights_path, map_location="cpu") # or "cuda" if GPU
model.load_state_dict(state_dict)
print("✅ Weights loaded successfully from", weights_path)
except FileNotFoundError:
print(f"❌ File not found: {weights_path}")
except RuntimeError as e:
print(f"❌ Error loading weights: {e}")
model.eval() # Set to evaluation mode
# -----------------------------
# 2. Load and Preprocess Images
# -----------------------------
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
test_images = "Blood-Cell-Detection-3/test"
test_annotations = "Blood-Cell-Detection-3/test/_annotations.coco.json"
dataset_test = CustomDataset(test_images, test_annotations, get_transform(train=False))
data_loader_test = DataLoader(dataset_test, batch_size=1, shuffle=False, collate_fn=lambda x: tuple(zip(*x)))
ContPlateletsLabeled=0
ContRBCLabeled=0
ContWBCLabeled=0
ContPlateletsPredicted=0
ContRBCPredicted=0
ContWBCPredicted=0
for images, targets, images_path, filenames in data_loader_test:
boxes=targets[0]['boxes']
labels=targets[0]['labels']
filename=filenames[0]
filename=filename[0:16]
img = Image.open(images_path[0]).convert("RGB")
draw = ImageDraw.Draw(img)
try:
#font = ImageFont.truetype("arial.ttf", 16) # MOD
font_size = 50
font = ImageFont.truetype("arial.ttf", font_size)
except IOError:
print("ERROR FONT")
font = ImageFont.load_default()
ContPlatelets=0
ContRBC=0
ContWBC=0
for box, label in zip(boxes, labels):
x1, y1, x2, y2 = box
TextLabel= str(label.item())
if TextLabel=="1":
draw_scaled_rectangle(draw, (x1, y1, x2, y2), "red",scale_factor=0.05) # Platelets
ContPlatelets=ContPlatelets+1
else:
if TextLabel=="2":
draw_scaled_rectangle(draw, (x1, y1, x2, y2), "blue",scale_factor=0.05) # RBC
ContRBC=ContRBC+1
else:
draw_scaled_rectangle(draw, (x1, y1, x2, y2), "green",scale_factor=0.05) # WBC
ContWBC=ContWBC+1
draw.rectangle(((x1, y1), (x1 , y1)), fill="red", width=6)
ContPlateletsLabeled = ContPlateletsLabeled + ContPlatelets
ContRBCLabeled=ContRBCLabeled + ContRBC
ContWBCLabeled = ContWBCLabeled + ContWBC
filenameComplete = filename + " Platelets=" +str(ContPlatelets) + " RBC=" +str(ContRBC) + " WBC=" +str(ContWBC)
# PREDICTIONS
imgPred = Image.open(images_path[0]).convert("RGB")
# Transform to tensor
img_tensor = F.to_tensor(imgPred) # Converts to [C,H,W] and scales to [0,1]
# -----------------------------
# 3. Run Inference
# -----------------------------
with torch.no_grad():
predictions = model([img_tensor]) # Model expects a list of tensors
# Extract predictions for the first image
pred = predictions[0]
boxesPred = pred["boxes"]
labelsPred = pred["labels"]
scoresPred = pred["scores"]
# -----------------------------
# 4. Filter by Confidence
# -----------------------------
threshold = CONF_THRESHOLD
#print(scoresPred)
keep = scoresPred >= threshold
boxesPred = boxesPred[keep]
labelsPred = labelsPred[keep]
scoresPred = scoresPred[keep]
# -----------------------------
# 5. Draw Results
# -----------------------------
drawPred = ImageDraw.Draw(imgPred)
try:
#font = ImageFont.truetype("arial.ttf", 16) # MOD
font_size = 50
font = ImageFont.truetype("arial.ttf", font_size)
except IOError:
print("ERROR FONT")
font = ImageFont.load_default()
ContPlateletsPred=0
ContRBCPred=0
ContWBCPred=0
for boxPred, labelPred, scorePred in zip(boxesPred, labelsPred, scoresPred):
x1, y1, x2, y2 = boxPred
#draw.rectangle(((x1, y1), (x2, y2)), outline="red", width=6)
#print(labelPred)
TextLabelPred= str(labelPred.item())
if TextLabelPred=="1":
draw_scaled_rectangle(drawPred, (x1, y1, x2, y2), "red",scale_factor=0.05) # Platelets
ContPlateletsPred=ContPlateletsPred+1
else:
if TextLabelPred=="2":
draw_scaled_rectangle(drawPred, (x1, y1, x2, y2), "blue",scale_factor=0.05) # RBC
ContRBCPred=ContRBCPred+1
else:
draw_scaled_rectangle(drawPred, (x1, y1, x2, y2), "green",scale_factor=0.05) # WB
ContWBCPred=ContWBCPred+1
drawPred.rectangle(((x1, y1 ), (x1 , y1)), fill="red", width=6)
ContPlateletsPredicted= ContPlateletsPredicted + ContPlateletsPred
ContRBCPredicted= ContRBCPredicted + ContRBCPred
ContWBCPredicted= ContWBCPredicted + ContWBCPred
filenameCompletePred = " Platelets=" +str(ContPlateletsPred) + " RBC=" +str(ContRBCPred) + " WBC=" +str(ContWBCPred)
if SHOW_OPTION == "YES":
fig, axs = plt.subplots(1,2, figsize=(15,5))
axs[0].imshow(img); axs[0].set_title('Labeled: ' + filenameComplete); axs[0].axis('off')
axs[1].imshow(imgPred); axs[1].set_title(' Predicted: ' + filenameCompletePred ); axs[1].axis('off')
#plt.tight_layout();
plt.show()
TotBloodCellLabeled=0
TotBloodCellPredicted=0
print(" Platelets labeled = " + str(ContPlateletsLabeled) + " predicted = " + str(ContPlateletsPredicted))
print(" RBC labeled = " + str(ContRBCLabeled) + " predicted = " + str(ContRBCPredicted))
print(" WBC labeled = " + str(ContWBCLabeled) + " predicted = " + str(ContWBCPredicted))
TotBloodCellLabeled= ContPlateletsLabeled + ContRBCLabeled + ContWBCLabeled
TotBloodCellPredicted= ContPlateletsPredicted + ContRBCPredicted + ContWBCPredicted
print("")
print("")
print(" Total Blood Cells labeled = " + str(TotBloodCellLabeled) + " predicted = " + str(TotBloodCellPredicted))