DawnC commited on
Commit
1b921c8
1 Parent(s): 3cf7045

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -488
app.py CHANGED
@@ -37,472 +37,9 @@ from device_manager import DeviceManager, adaptive_gpu
37
  import asyncio
38
  import traceback
39
 
40
- # model_yolo = YOLO('yolov8l.pt')
41
-
42
- # history_manager = UserHistoryManager()
43
-
44
- # dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staffordshire_Terrier",
45
- # "Appenzeller", "Australian_Terrier", "Bedlington_Terrier", "Bernese_Mountain_Dog", "Bichon_Frise",
46
- # "Blenheim_Spaniel", "Border_Collie", "Border_Terrier", "Boston_Bull", "Bouvier_Des_Flandres",
47
- # "Brabancon_Griffon", "Brittany_Spaniel", "Cardigan", "Chesapeake_Bay_Retriever",
48
- # "Chihuahua", "Dachshund", "Dandie_Dinmont", "Doberman", "English_Foxhound", "English_Setter",
49
- # "English_Springer", "EntleBucher", "Eskimo_Dog", "French_Bulldog", "German_Shepherd",
50
- # "German_Short-Haired_Pointer", "Gordon_Setter", "Great_Dane", "Great_Pyrenees",
51
- # "Greater_Swiss_Mountain_Dog","Havanese", "Ibizan_Hound", "Irish_Setter", "Irish_Terrier",
52
- # "Irish_Water_Spaniel", "Irish_Wolfhound", "Italian_Greyhound", "Japanese_Spaniel",
53
- # "Kerry_Blue_Terrier", "Labrador_Retriever", "Lakeland_Terrier", "Leonberg", "Lhasa",
54
- # "Maltese_Dog", "Mexican_Hairless", "Newfoundland", "Norfolk_Terrier", "Norwegian_Elkhound",
55
- # "Norwich_Terrier", "Old_English_Sheepdog", "Pekinese", "Pembroke", "Pomeranian",
56
- # "Rhodesian_Ridgeback", "Rottweiler", "Saint_Bernard", "Saluki", "Samoyed",
57
- # "Scotch_Terrier", "Scottish_Deerhound", "Sealyham_Terrier", "Shetland_Sheepdog", "Shiba_Inu",
58
- # "Shih-Tzu", "Siberian_Husky", "Staffordshire_Bullterrier", "Sussex_Spaniel",
59
- # "Tibetan_Mastiff", "Tibetan_Terrier", "Walker_Hound", "Weimaraner",
60
- # "Welsh_Springer_Spaniel", "West_Highland_White_Terrier", "Yorkshire_Terrier",
61
- # "Affenpinscher", "Basenji", "Basset", "Beagle", "Black-and-Tan_Coonhound", "Bloodhound",
62
- # "Bluetick", "Borzoi", "Boxer", "Briard", "Bull_Mastiff", "Cairn", "Chow", "Clumber",
63
- # "Cocker_Spaniel", "Collie", "Curly-Coated_Retriever", "Dhole", "Dingo",
64
- # "Flat-Coated_Retriever", "Giant_Schnauzer", "Golden_Retriever", "Groenendael", "Keeshond",
65
- # "Kelpie", "Komondor", "Kuvasz", "Malamute", "Malinois", "Miniature_Pinscher",
66
- # "Miniature_Poodle", "Miniature_Schnauzer", "Otterhound", "Papillon", "Pug", "Redbone",
67
- # "Schipperke", "Silky_Terrier", "Soft-Coated_Wheaten_Terrier", "Standard_Poodle",
68
- # "Standard_Schnauzer", "Toy_Poodle", "Toy_Terrier", "Vizsla", "Whippet",
69
- # "Wire-Haired_Fox_Terrier"]
70
-
71
-
72
- # class MultiHeadAttention(nn.Module):
73
-
74
- # def __init__(self, in_dim, num_heads=8):
75
- # super().__init__()
76
- # self.num_heads = num_heads
77
- # self.head_dim = max(1, in_dim // num_heads)
78
- # self.scaled_dim = self.head_dim * num_heads
79
- # self.fc_in = nn.Linear(in_dim, self.scaled_dim)
80
- # self.query = nn.Linear(self.scaled_dim, self.scaled_dim)
81
- # self.key = nn.Linear(self.scaled_dim, self.scaled_dim)
82
- # self.value = nn.Linear(self.scaled_dim, self.scaled_dim)
83
- # self.fc_out = nn.Linear(self.scaled_dim, in_dim)
84
-
85
- # def forward(self, x):
86
- # N = x.shape[0]
87
- # x = self.fc_in(x)
88
- # q = self.query(x).view(N, self.num_heads, self.head_dim)
89
- # k = self.key(x).view(N, self.num_heads, self.head_dim)
90
- # v = self.value(x).view(N, self.num_heads, self.head_dim)
91
-
92
- # energy = torch.einsum("nqd,nkd->nqk", [q, k])
93
- # attention = F.softmax(energy / (self.head_dim ** 0.5), dim=2)
94
-
95
- # out = torch.einsum("nqk,nvd->nqd", [attention, v])
96
- # out = out.reshape(N, self.scaled_dim)
97
- # out = self.fc_out(out)
98
- # return out
99
-
100
- # class BaseModel(nn.Module):
101
- # def __init__(self, num_classes, device='cuda' if torch.cuda.is_available() else 'cpu'):
102
- # super().__init__()
103
- # self.device = device
104
- # self.backbone = efficientnet_v2_m(weights=EfficientNet_V2_M_Weights.IMAGENET1K_V1)
105
- # self.feature_dim = self.backbone.classifier[1].in_features
106
- # self.backbone.classifier = nn.Identity()
107
-
108
- # self.num_heads = max(1, min(8, self.feature_dim // 64))
109
- # self.attention = MultiHeadAttention(self.feature_dim, num_heads=self.num_heads)
110
-
111
- # self.classifier = nn.Sequential(
112
- # nn.LayerNorm(self.feature_dim),
113
- # nn.Dropout(0.3),
114
- # nn.Linear(self.feature_dim, num_classes)
115
- # )
116
-
117
- # self.to(device)
118
-
119
- # def forward(self, x):
120
- # x = x.to(self.device)
121
- # features = self.backbone(x)
122
- # attended_features = self.attention(features)
123
- # logits = self.classifier(attended_features)
124
- # return logits, attended_features
125
-
126
- # # Initialize model
127
- # num_classes = len(dog_breeds)
128
- # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
129
-
130
- # # Initialize base model
131
- # model = BaseModel(num_classes=num_classes, device=device).to(device)
132
-
133
- # # Load model path
134
- # model_path = '124_best_model_dog.pth'
135
- # checkpoint = torch.load(model_path, map_location=device)
136
-
137
- # # Load model state
138
- # model.load_state_dict(checkpoint['base_model'], strict=False)
139
- # model.eval()
140
-
141
- # # Image preprocessing function
142
- # def preprocess_image(image):
143
- # # If the image is numpy.ndarray turn into PIL.Image
144
- # if isinstance(image, np.ndarray):
145
- # image = Image.fromarray(image)
146
-
147
- # # Use torchvision.transforms to process images
148
- # transform = transforms.Compose([
149
- # transforms.Resize((224, 224)),
150
- # transforms.ToTensor(),
151
- # transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
152
- # ])
153
-
154
- # return transform(image).unsqueeze(0)
155
-
156
- # async def predict_single_dog(image):
157
- # """
158
- # Predicts the dog breed using only the classifier.
159
- # Args:
160
- # image: PIL Image or numpy array
161
- # Returns:
162
- # tuple: (top1_prob, topk_breeds, relative_probs)
163
- # """
164
- # image_tensor = preprocess_image(image).to(device)
165
-
166
- # with torch.no_grad():
167
- # # Get model outputs (只使用logits,不需要features)
168
- # logits = model(image_tensor)[0] # 如果model仍返回tuple,取第一個元素
169
- # probs = F.softmax(logits, dim=1)
170
-
171
- # # Classifier prediction
172
- # top5_prob, top5_idx = torch.topk(probs, k=5)
173
- # breeds = [dog_breeds[idx.item()] for idx in top5_idx[0]]
174
- # probabilities = [prob.item() for prob in top5_prob[0]]
175
-
176
- # # Calculate relative probabilities
177
- # sum_probs = sum(probabilities[:3]) # 只取前三個來計算相對概率
178
- # relative_probs = [f"{(prob/sum_probs * 100):.2f}%" for prob in probabilities[:3]]
179
-
180
- # # Debug output
181
- # print("\nClassifier Predictions:")
182
- # for breed, prob in zip(breeds[:5], probabilities[:5]):
183
- # print(f"{breed}: {prob:.4f}")
184
-
185
- # return probabilities[0], breeds[:3], relative_probs
186
-
187
-
188
- # async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
189
- # results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
190
- # dogs = []
191
- # boxes = []
192
- # for box in results.boxes:
193
- # if box.cls == 16: # COCO dataset class for dog is 16
194
- # xyxy = box.xyxy[0].tolist()
195
- # confidence = box.conf.item()
196
- # boxes.append((xyxy, confidence))
197
-
198
- # if not boxes:
199
- # dogs.append((image, 1.0, [0, 0, image.width, image.height]))
200
- # else:
201
- # nms_boxes = non_max_suppression(boxes, iou_threshold)
202
-
203
- # for box, confidence in nms_boxes:
204
- # x1, y1, x2, y2 = box
205
- # w, h = x2 - x1, y2 - y1
206
- # x1 = max(0, x1 - w * 0.05)
207
- # y1 = max(0, y1 - h * 0.05)
208
- # x2 = min(image.width, x2 + w * 0.05)
209
- # y2 = min(image.height, y2 + h * 0.05)
210
- # cropped_image = image.crop((x1, y1, x2, y2))
211
- # dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
212
-
213
- # return dogs
214
-
215
- # def non_max_suppression(boxes, iou_threshold):
216
- # keep = []
217
- # boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
218
- # while boxes:
219
- # current = boxes.pop(0)
220
- # keep.append(current)
221
- # boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
222
- # return keep
223
-
224
-
225
- # def calculate_iou(box1, box2):
226
- # x1 = max(box1[0], box2[0])
227
- # y1 = max(box1[1], box2[1])
228
- # x2 = min(box1[2], box2[2])
229
- # y2 = min(box1[3], box2[3])
230
-
231
- # intersection = max(0, x2 - x1) * max(0, y2 - y1)
232
- # area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
233
- # area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
234
-
235
- # iou = intersection / float(area1 + area2 - intersection)
236
- # return iou
237
-
238
-
239
-
240
- # def create_breed_comparison(breed1: str, breed2: str) -> dict:
241
- # breed1_info = get_dog_description(breed1)
242
- # breed2_info = get_dog_description(breed2)
243
-
244
- # # 標準化數值轉換
245
- # value_mapping = {
246
- # 'Size': {'Small': 1, 'Medium': 2, 'Large': 3, 'Giant': 4},
247
- # 'Exercise_Needs': {'Low': 1, 'Moderate': 2, 'High': 3, 'Very High': 4},
248
- # 'Care_Level': {'Low': 1, 'Moderate': 2, 'High': 3},
249
- # 'Grooming_Needs': {'Low': 1, 'Moderate': 2, 'High': 3}
250
- # }
251
-
252
- # comparison_data = {
253
- # breed1: {},
254
- # breed2: {}
255
- # }
256
-
257
- # for breed, info in [(breed1, breed1_info), (breed2, breed2_info)]:
258
- # comparison_data[breed] = {
259
- # 'Size': value_mapping['Size'].get(info['Size'], 2), # 預設 Medium
260
- # 'Exercise_Needs': value_mapping['Exercise_Needs'].get(info['Exercise Needs'], 2), # 預設 Moderate
261
- # 'Care_Level': value_mapping['Care_Level'].get(info['Care Level'], 2),
262
- # 'Grooming_Needs': value_mapping['Grooming_Needs'].get(info['Grooming Needs'], 2),
263
- # 'Good_with_Children': info['Good with Children'] == 'Yes',
264
- # 'Original_Data': info
265
- # }
266
-
267
- # return comparison_data
268
-
269
-
270
- # async def predict(image):
271
- # """
272
- # Main prediction function that handles both single and multiple dog detection.
273
-
274
- # Args:
275
- # image: PIL Image or numpy array
276
-
277
- # Returns:
278
- # tuple: (html_output, annotated_image, initial_state)
279
- # """
280
- # if image is None:
281
- # return format_warning_html("Please upload an image to start."), None, None
282
-
283
- # try:
284
- # if isinstance(image, np.ndarray):
285
- # image = Image.fromarray(image)
286
-
287
- # # Detect dogs in the image
288
- # dogs = await detect_multiple_dogs(image)
289
- # color_scheme = get_color_scheme(len(dogs) == 1)
290
-
291
- # # Prepare for annotation
292
- # annotated_image = image.copy()
293
- # draw = ImageDraw.Draw(annotated_image)
294
-
295
- # try:
296
- # font = ImageFont.truetype("arial.ttf", 24)
297
- # except:
298
- # font = ImageFont.load_default()
299
-
300
- # dogs_info = ""
301
-
302
- # # Process each detected dog
303
- # for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
304
- # color = color_scheme if len(dogs) == 1 else color_scheme[i % len(color_scheme)]
305
-
306
- # # Draw box and label on image
307
- # draw.rectangle(box, outline=color, width=4)
308
- # label = f"Dog {i+1}"
309
- # label_bbox = draw.textbbox((0, 0), label, font=font)
310
- # label_width = label_bbox[2] - label_bbox[0]
311
- # label_height = label_bbox[3] - label_bbox[1]
312
-
313
- # # Draw label background and text
314
- # label_x = box[0] + 5
315
- # label_y = box[1] + 5
316
- # draw.rectangle(
317
- # [label_x - 2, label_y - 2, label_x + label_width + 4, label_y + label_height + 4],
318
- # fill='white',
319
- # outline=color,
320
- # width=2
321
- # )
322
- # draw.text((label_x, label_y), label, fill=color, font=font)
323
-
324
- # # Predict breed
325
- # top1_prob, topk_breeds, relative_probs = await predict_single_dog(cropped_image)
326
- # combined_confidence = detection_confidence * top1_prob
327
-
328
- # # Format results based on confidence with error handling
329
- # try:
330
- # if combined_confidence < 0.2:
331
- # dogs_info += format_error_message(color, i+1)
332
- # elif top1_prob >= 0.45:
333
- # breed = topk_breeds[0]
334
- # description = get_dog_description(breed)
335
- # # Handle missing breed description
336
- # if description is None:
337
- # # 如果沒有描述,創建一個基本描述
338
- # description = {
339
- # "Name": breed,
340
- # "Size": "Unknown",
341
- # "Exercise Needs": "Unknown",
342
- # "Grooming Needs": "Unknown",
343
- # "Care Level": "Unknown",
344
- # "Good with Children": "Unknown",
345
- # "Description": f"Identified as {breed.replace('_', ' ')}"
346
- # }
347
- # dogs_info += format_single_dog_result(breed, description, color)
348
- # else:
349
- # # 修改format_multiple_breeds_result的調用,包含錯誤處理
350
- # dogs_info += format_multiple_breeds_result(
351
- # topk_breeds,
352
- # relative_probs,
353
- # color,
354
- # i+1,
355
- # lambda breed: get_dog_description(breed) or {
356
- # "Name": breed,
357
- # "Size": "Unknown",
358
- # "Exercise Needs": "Unknown",
359
- # "Grooming Needs": "Unknown",
360
- # "Care Level": "Unknown",
361
- # "Good with Children": "Unknown",
362
- # "Description": f"Identified as {breed.replace('_', ' ')}"
363
- # }
364
- # )
365
- # except Exception as e:
366
- # print(f"Error formatting results for dog {i+1}: {str(e)}")
367
- # dogs_info += format_error_message(color, i+1)
368
-
369
- # # Wrap final HTML output
370
- # html_output = format_multi_dog_container(dogs_info)
371
-
372
- # # Prepare initial state
373
- # initial_state = {
374
- # "dogs_info": dogs_info,
375
- # "image": annotated_image,
376
- # "is_multi_dog": len(dogs) > 1,
377
- # "html_output": html_output
378
- # }
379
-
380
- # return html_output, annotated_image, initial_state
381
-
382
- # except Exception as e:
383
- # error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
384
- # print(error_msg)
385
- # return format_warning_html(error_msg), None, None
386
-
387
-
388
- # def show_details_html(choice, previous_output, initial_state):
389
- # """
390
- # Generate detailed HTML view for a selected breed.
391
-
392
- # Args:
393
- # choice: str, Selected breed option
394
- # previous_output: str, Previous HTML output
395
- # initial_state: dict, Current state information
396
-
397
- # Returns:
398
- # tuple: (html_output, gradio_update, updated_state)
399
- # """
400
- # if not choice:
401
- # return previous_output, gr.update(visible=True), initial_state
402
-
403
- # try:
404
- # breed = choice.split("More about ")[-1]
405
- # description = get_dog_description(breed)
406
- # html_output = format_breed_details_html(description, breed)
407
-
408
- # # Update state
409
- # initial_state["current_description"] = html_output
410
- # initial_state["original_buttons"] = initial_state.get("buttons", [])
411
-
412
- # return html_output, gr.update(visible=True), initial_state
413
-
414
- # except Exception as e:
415
- # error_msg = f"An error occurred while showing details: {e}"
416
- # print(error_msg)
417
- # return format_warning_html(error_msg), gr.update(visible=True), initial_state
418
-
419
- # def main():
420
- # with gr.Blocks(css=get_css_styles()) as iface:
421
- # # Header HTML
422
-
423
- # gr.HTML("""
424
- # <header style='text-align: center; padding: 20px; margin-bottom: 20px;'>
425
- # <h1 style='font-size: 2.5em; margin-bottom: 10px; color: #2D3748;'>
426
- # 🐾 PawMatch AI
427
- # </h1>
428
- # <h2 style='font-size: 1.2em; font-weight: normal; color: #4A5568; margin-top: 5px;'>
429
- # Your Smart Dog Breed Guide
430
- # </h2>
431
- # <div style='width: 50px; height: 3px; background: linear-gradient(90deg, #4299e1, #48bb78); margin: 15px auto;'></div>
432
- # <p style='color: #718096; font-size: 0.9em;'>
433
- # Powered by AI • Breed Recognition • Smart Matching • Companion Guide
434
- # </p>
435
- # </header>
436
- # """)
437
-
438
- # # 先創建歷史組件實例(但不創建標籤頁)
439
- # history_component = create_history_component()
440
-
441
- # with gr.Tabs():
442
- # # 1. 品種檢測標籤頁
443
- # example_images = [
444
- # 'Border_Collie.jpg',
445
- # 'Golden_Retriever.jpeg',
446
- # 'Saint_Bernard.jpeg',
447
- # 'Samoyed.jpg',
448
- # 'French_Bulldog.jpeg'
449
- # ]
450
- # detection_components = create_detection_tab(predict, example_images)
451
-
452
- # # 2. 品種比較標籤頁
453
- # comparison_components = create_comparison_tab(
454
- # dog_breeds=dog_breeds,
455
- # get_dog_description=get_dog_description,
456
- # breed_health_info=breed_health_info,
457
- # breed_noise_info=breed_noise_info
458
- # )
459
-
460
- # # 3. 品種推薦標籤頁
461
- # recommendation_components = create_recommendation_tab(
462
- # UserPreferences=UserPreferences,
463
- # get_breed_recommendations=get_breed_recommendations,
464
- # format_recommendation_html=format_recommendation_html,
465
- # history_component=history_component
466
- # )
467
-
468
-
469
- # # 4. 最後創建歷史記錄標籤頁
470
- # create_history_tab(history_component)
471
-
472
- # # Footer
473
- # gr.HTML('''
474
- # <div style="
475
- # display: flex;
476
- # align-items: center;
477
- # justify-content: center;
478
- # gap: 20px;
479
- # padding: 20px 0;
480
- # ">
481
- # <p style="
482
- # font-family: 'Arial', sans-serif;
483
- # font-size: 14px;
484
- # font-weight: 500;
485
- # letter-spacing: 2px;
486
- # background: linear-gradient(90deg, #555, #007ACC);
487
- # -webkit-background-clip: text;
488
- # -webkit-text-fill-color: transparent;
489
- # margin: 0;
490
- # text-transform: uppercase;
491
- # display: inline-block;
492
- # ">EXPLORE THE CODE →</p>
493
- # <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/PawMatchAI" style="text-decoration: none;">
494
- # <img src="https://img.shields.io/badge/GitHub-PawMatch_AI-007ACC?logo=github&style=for-the-badge">
495
- # </a>
496
- # </div>
497
- # ''')
498
-
499
- # return iface
500
-
501
- # if __name__ == "__main__":
502
- # iface = main()
503
- # iface.launch()
504
-
505
 
 
506
 
507
  dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staffordshire_Terrier",
508
  "Appenzeller", "Australian_Terrier", "Bedlington_Terrier", "Bernese_Mountain_Dog", "Bichon_Frise",
@@ -532,10 +69,6 @@ dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staff
532
  "Wire-Haired_Fox_Terrier"]
533
 
534
 
535
- device_mgr = DeviceManager()
536
-
537
- history_manager = UserHistoryManager()
538
-
539
  class MultiHeadAttention(nn.Module):
540
 
541
  def __init__(self, in_dim, num_heads=8):
@@ -565,9 +98,9 @@ class MultiHeadAttention(nn.Module):
565
  return out
566
 
567
  class BaseModel(nn.Module):
568
-
569
  def __init__(self, num_classes, device='cuda' if torch.cuda.is_available() else 'cpu'):
570
  super().__init__()
 
571
  self.backbone = efficientnet_v2_m(weights=EfficientNet_V2_M_Weights.IMAGENET1K_V1)
572
  self.feature_dim = self.backbone.classifier[1].in_features
573
  self.backbone.classifier = nn.Identity()
@@ -581,6 +114,8 @@ class BaseModel(nn.Module):
581
  nn.Linear(self.feature_dim, num_classes)
582
  )
583
 
 
 
584
  def forward(self, x):
585
  x = x.to(self.device)
586
  features = self.backbone(x)
@@ -590,21 +125,19 @@ class BaseModel(nn.Module):
590
 
591
  # Initialize model
592
  num_classes = len(dog_breeds)
 
593
 
594
  # Initialize base model
595
- model = BaseModel(num_classes=num_classes)
596
- model = device_mgr.to_device(model)
597
  # Load model path
598
  model_path = '124_best_model_dog.pth'
599
- checkpoint = torch.load(model_path, map_location=device_mgr.get_device(), weights_only=True)
600
 
601
  # Load model state
602
  model.load_state_dict(checkpoint['base_model'], strict=False)
603
  model.eval()
604
 
605
- model_yolo = YOLO('yolov8l.pt')
606
- model_yolo = device_mgr.to_device(model_yolo)
607
-
608
  # Image preprocessing function
609
  def preprocess_image(image):
610
  # If the image is numpy.ndarray turn into PIL.Image
@@ -619,35 +152,40 @@ def preprocess_image(image):
619
  ])
620
 
621
  return transform(image).unsqueeze(0)
622
-
623
 
624
- @adaptive_gpu(duration=30)
625
  async def predict_single_dog(image):
626
- """單獨的狗預測函數"""
627
- image_tensor = preprocess_image(image)
628
- image_tensor = device_mgr.to_device(image_tensor)
 
 
 
 
 
629
 
630
  with torch.no_grad():
631
- outputs = model(image_tensor)
632
- logits = outputs[0] if isinstance(outputs, tuple) else outputs
633
  probs = F.softmax(logits, dim=1)
634
 
 
635
  top5_prob, top5_idx = torch.topk(probs, k=5)
636
  breeds = [dog_breeds[idx.item()] for idx in top5_idx[0]]
637
  probabilities = [prob.item() for prob in top5_prob[0]]
638
 
639
- sum_probs = sum(probabilities[:3])
 
640
  relative_probs = [f"{(prob/sum_probs * 100):.2f}%" for prob in probabilities[:3]]
641
 
 
642
  print("\nClassifier Predictions:")
643
  for breed, prob in zip(breeds[:5], probabilities[:5]):
644
  print(f"{breed}: {prob:.4f}")
645
 
646
  return probabilities[0], breeds[:3], relative_probs
647
-
648
- @adaptive_gpu(duration=30)
649
  async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
650
- """複數狗預測函數"""
651
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
652
  dogs = []
653
  boxes = []
@@ -673,7 +211,6 @@ async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
673
  dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
674
 
675
  return dogs
676
-
677
 
678
  def non_max_suppression(boxes, iou_threshold):
679
  keep = []
 
37
  import asyncio
38
  import traceback
39
 
40
+ model_yolo = YOLO('yolov8l.pt')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ history_manager = UserHistoryManager()
43
 
44
  dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staffordshire_Terrier",
45
  "Appenzeller", "Australian_Terrier", "Bedlington_Terrier", "Bernese_Mountain_Dog", "Bichon_Frise",
 
69
  "Wire-Haired_Fox_Terrier"]
70
 
71
 
 
 
 
 
72
  class MultiHeadAttention(nn.Module):
73
 
74
  def __init__(self, in_dim, num_heads=8):
 
98
  return out
99
 
100
  class BaseModel(nn.Module):
 
101
  def __init__(self, num_classes, device='cuda' if torch.cuda.is_available() else 'cpu'):
102
  super().__init__()
103
+ self.device = device
104
  self.backbone = efficientnet_v2_m(weights=EfficientNet_V2_M_Weights.IMAGENET1K_V1)
105
  self.feature_dim = self.backbone.classifier[1].in_features
106
  self.backbone.classifier = nn.Identity()
 
114
  nn.Linear(self.feature_dim, num_classes)
115
  )
116
 
117
+ self.to(device)
118
+
119
  def forward(self, x):
120
  x = x.to(self.device)
121
  features = self.backbone(x)
 
125
 
126
  # Initialize model
127
  num_classes = len(dog_breeds)
128
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
129
 
130
  # Initialize base model
131
+ model = BaseModel(num_classes=num_classes, device=device).to(device)
132
+
133
  # Load model path
134
  model_path = '124_best_model_dog.pth'
135
+ checkpoint = torch.load(model_path, map_location=device)
136
 
137
  # Load model state
138
  model.load_state_dict(checkpoint['base_model'], strict=False)
139
  model.eval()
140
 
 
 
 
141
  # Image preprocessing function
142
  def preprocess_image(image):
143
  # If the image is numpy.ndarray turn into PIL.Image
 
152
  ])
153
 
154
  return transform(image).unsqueeze(0)
 
155
 
 
156
  async def predict_single_dog(image):
157
+ """
158
+ Predicts the dog breed using only the classifier.
159
+ Args:
160
+ image: PIL Image or numpy array
161
+ Returns:
162
+ tuple: (top1_prob, topk_breeds, relative_probs)
163
+ """
164
+ image_tensor = preprocess_image(image).to(device)
165
 
166
  with torch.no_grad():
167
+ # Get model outputs (只使用logits,不需要features)
168
+ logits = model(image_tensor)[0] # 如果model仍返回tuple,取第一個元素
169
  probs = F.softmax(logits, dim=1)
170
 
171
+ # Classifier prediction
172
  top5_prob, top5_idx = torch.topk(probs, k=5)
173
  breeds = [dog_breeds[idx.item()] for idx in top5_idx[0]]
174
  probabilities = [prob.item() for prob in top5_prob[0]]
175
 
176
+ # Calculate relative probabilities
177
+ sum_probs = sum(probabilities[:3]) # 只取前三個來計算相對概率
178
  relative_probs = [f"{(prob/sum_probs * 100):.2f}%" for prob in probabilities[:3]]
179
 
180
+ # Debug output
181
  print("\nClassifier Predictions:")
182
  for breed, prob in zip(breeds[:5], probabilities[:5]):
183
  print(f"{breed}: {prob:.4f}")
184
 
185
  return probabilities[0], breeds[:3], relative_probs
186
+
187
+
188
  async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
 
189
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
190
  dogs = []
191
  boxes = []
 
211
  dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
212
 
213
  return dogs
 
214
 
215
  def non_max_suppression(boxes, iou_threshold):
216
  keep = []