DawnC commited on
Commit
b9c642f
1 Parent(s): 1a17fe4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +630 -92
app.py CHANGED
@@ -39,37 +39,570 @@ import spaces
39
  import torch.cuda.amp
40
 
41
 
42
- @spaces.GPU(duration=30) # Request smaller GPU time chunk
43
- def get_device():
44
- """
45
- Initialize device configuration with automatic CPU fallback.
46
- Attempts GPU first, falls back to CPU if necessary.
47
- """
48
- print("Initializing device configuration...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- try:
51
- # Attempt GPU initialization with optimizations
52
- if torch.cuda.is_available():
53
- device = torch.device('cuda')
54
- torch.cuda.init()
55
- torch.set_float32_matmul_precision('medium')
56
 
57
- # Add CUDA optimizations
58
- torch.backends.cudnn.benchmark = True
59
- torch.backends.cudnn.deterministic = False
60
 
61
- print(f"Successfully initialized CUDA device: {torch.cuda.get_device_name(device)}")
62
- return device
63
 
64
- except (spaces.zero.gradio.HTMLError, RuntimeError) as e:
65
- print(f"GPU initialization error: {str(e)}")
66
 
67
- # CPU fallback with optimizations
68
- print("Using CPU mode")
69
- torch.set_num_threads(4) # Optimize CPU performance
70
- return torch.device('cpu')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
- device = get_device()
73
 
74
  history_manager = UserHistoryManager()
75
 
@@ -101,6 +634,52 @@ dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staff
101
  "Wire-Haired_Fox_Terrier"]
102
 
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  class MultiHeadAttention(nn.Module):
105
 
106
  def __init__(self, in_dim, num_heads=8):
@@ -198,15 +777,6 @@ def load_model(model_path, model_instance, device):
198
  print(f"Model loading error: {str(e)}")
199
  raise
200
 
201
- # Initialize model
202
- num_classes = len(dog_breeds)
203
-
204
- model = BaseModel(num_classes=num_classes, device=device)
205
-
206
- # 使用優化後的載入函數
207
- model = load_model("124_best_model_dog.pth", model, device)
208
- model.eval()
209
-
210
  # Image preprocessing function
211
  def preprocess_image(image):
212
  # If the image is numpy.ndarray turn into PIL.Image
@@ -222,53 +792,32 @@ def preprocess_image(image):
222
 
223
  return transform(image).unsqueeze(0)
224
 
225
- def initialize_yolo_model(device):
226
- try:
227
- model_yolo = YOLO('yolov8l.pt')
228
- if torch.cuda.is_available():
229
- model_yolo.to(device)
230
- print(f"YOLO model initialized on {device}")
231
- return model_yolo
232
- except Exception as e:
233
- print(f"Error initializing YOLO model: {str(e)}")
234
- print("Attempting to initialize YOLO model on CPU")
235
- return YOLO('yolov8l.pt')
236
-
237
- model_yolo = initialize_yolo_model(device)
238
-
239
- async def predict_single_dog(image):
240
  """
241
- Predicts the dog breed using only the classifier.
242
- Args:
243
- image: PIL Image or numpy array
244
- Returns:
245
- tuple: (top1_prob, topk_breeds, relative_probs)
246
  """
 
 
247
  image_tensor = preprocess_image(image).to(device)
248
 
249
  with torch.no_grad():
250
- # Get model outputs (只使用logits,不需要features)
251
- logits = model(image_tensor)[0] # 如果model仍返回tuple,取第一個元素
252
  probs = F.softmax(logits, dim=1)
253
-
254
- # Classifier prediction
255
  top5_prob, top5_idx = torch.topk(probs, k=5)
256
  breeds = [dog_breeds[idx.item()] for idx in top5_idx[0]]
257
  probabilities = [prob.item() for prob in top5_prob[0]]
258
-
259
- # Calculate relative probabilities
260
- sum_probs = sum(probabilities[:3]) # 只取前三個來計算相對概率
261
  relative_probs = [f"{(prob/sum_probs * 100):.2f}%" for prob in probabilities[:3]]
262
-
263
- # Debug output
264
- print("\nClassifier Predictions:")
265
- for breed, prob in zip(breeds[:5], probabilities[:5]):
266
- print(f"{breed}: {prob:.4f}")
267
-
268
  return probabilities[0], breeds[:3], relative_probs
269
 
270
 
 
271
  async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
 
272
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
273
  dogs = []
274
  boxes = []
@@ -500,22 +1049,13 @@ def show_details_html(choice, previous_output, initial_state):
500
  return format_warning_html(error_msg), gr.update(visible=True), initial_state
501
 
502
  def main():
503
- print("\n=== System Information ===")
504
- print(f"PyTorch Version: {torch.__version__}")
505
- print(f"CUDA Available: {torch.cuda.is_available()}")
506
- if torch.cuda.is_available():
507
- print(f"CUDA Version: {torch.version.cuda}")
508
- print(f"Current Device: {torch.cuda.current_device()}")
509
-
510
- # 清理 GPU 記憶體(如果可用)
511
- if torch.cuda.is_available():
512
- torch.cuda.empty_cache()
513
-
514
- device = get_device()
515
 
 
516
  with gr.Blocks(css=get_css_styles()) as iface:
517
- # Header HTML
518
-
519
  gr.HTML("""
520
  <header style='text-align: center; padding: 20px; margin-bottom: 20px;'>
521
  <h1 style='font-size: 2.5em; margin-bottom: 10px; color: #2D3748;'>
@@ -531,11 +1071,11 @@ def main():
531
  </header>
532
  """)
533
 
534
- # 先創建歷史組件實例(但不創建標籤頁)
535
  history_component = create_history_component()
536
 
537
  with gr.Tabs():
538
- # 1. 品種檢測標籤頁
539
  example_images = [
540
  'Border_Collie.jpg',
541
  'Golden_Retriever.jpeg',
@@ -543,9 +1083,12 @@ def main():
543
  'Samoyed.jpg',
544
  'French_Bulldog.jpeg'
545
  ]
546
- detection_components = create_detection_tab(predict, example_images)
 
 
 
547
 
548
- # 2. 品種比較標籤頁
549
  comparison_components = create_comparison_tab(
550
  dog_breeds=dog_breeds,
551
  get_dog_description=get_dog_description,
@@ -553,7 +1096,7 @@ def main():
553
  breed_noise_info=breed_noise_info
554
  )
555
 
556
- # 3. 品種推薦標籤頁
557
  recommendation_components = create_recommendation_tab(
558
  UserPreferences=UserPreferences,
559
  get_breed_recommendations=get_breed_recommendations,
@@ -561,8 +1104,7 @@ def main():
561
  history_component=history_component
562
  )
563
 
564
-
565
- # 4. 最後創建歷史記錄標籤頁
566
  create_history_tab(history_component)
567
 
568
  # Footer
@@ -595,9 +1137,5 @@ def main():
595
  return iface
596
 
597
  if __name__ == "__main__":
598
- print(f"CUDA available: {torch.cuda.is_available()}")
599
- if torch.cuda.is_available():
600
- print(f"Current device: {torch.cuda.current_device()}")
601
- print(f"Device name: {torch.cuda.get_device_name()}")
602
  iface = main()
603
  iface.launch()
 
39
  import torch.cuda.amp
40
 
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
+ # @spaces.GPU(duration=30) # Request smaller GPU time chunk
73
+ # def get_device():
74
+ # """
75
+ # Initialize device configuration with automatic CPU fallback.
76
+ # Attempts GPU first, falls back to CPU if necessary.
77
+ # """
78
+ # print("Initializing device configuration...")
79
 
80
+ # try:
81
+ # # Attempt GPU initialization with optimizations
82
+ # if torch.cuda.is_available():
83
+ # device = torch.device('cuda')
84
+ # torch.cuda.init()
85
+ # torch.set_float32_matmul_precision('medium')
86
 
87
+ # # Add CUDA optimizations
88
+ # torch.backends.cudnn.benchmark = True
89
+ # torch.backends.cudnn.deterministic = False
90
 
91
+ # print(f"Successfully initialized CUDA device: {torch.cuda.get_device_name(device)}")
92
+ # return device
93
 
94
+ # except (spaces.zero.gradio.HTMLError, RuntimeError) as e:
95
+ # print(f"GPU initialization error: {str(e)}")
96
 
97
+ # # CPU fallback with optimizations
98
+ # print("Using CPU mode")
99
+ # torch.set_num_threads(4) # Optimize CPU performance
100
+ # return torch.device('cpu')
101
+
102
+ # device = get_device()
103
+
104
+
105
+ # class MultiHeadAttention(nn.Module):
106
+
107
+ # def __init__(self, in_dim, num_heads=8):
108
+ # super().__init__()
109
+ # self.num_heads = num_heads
110
+ # self.head_dim = max(1, in_dim // num_heads)
111
+ # self.scaled_dim = self.head_dim * num_heads
112
+ # self.fc_in = nn.Linear(in_dim, self.scaled_dim)
113
+ # self.query = nn.Linear(self.scaled_dim, self.scaled_dim)
114
+ # self.key = nn.Linear(self.scaled_dim, self.scaled_dim)
115
+ # self.value = nn.Linear(self.scaled_dim, self.scaled_dim)
116
+ # self.fc_out = nn.Linear(self.scaled_dim, in_dim)
117
+
118
+ # def forward(self, x):
119
+ # N = x.shape[0]
120
+ # x = self.fc_in(x)
121
+ # q = self.query(x).view(N, self.num_heads, self.head_dim)
122
+ # k = self.key(x).view(N, self.num_heads, self.head_dim)
123
+ # v = self.value(x).view(N, self.num_heads, self.head_dim)
124
+
125
+ # energy = torch.einsum("nqd,nkd->nqk", [q, k])
126
+ # attention = F.softmax(energy / (self.head_dim ** 0.5), dim=2)
127
+
128
+ # out = torch.einsum("nqk,nvd->nqd", [attention, v])
129
+ # out = out.reshape(N, self.scaled_dim)
130
+ # out = self.fc_out(out)
131
+ # return out
132
+
133
+ # class BaseModel(nn.Module):
134
+ # def __init__(self, num_classes, device=None):
135
+ # super().__init__()
136
+ # if device is None:
137
+ # device = get_device()
138
+ # self.device = device
139
+ # print(f"Initializing model on device: {device}")
140
+
141
+ # self.backbone = efficientnet_v2_m(weights=EfficientNet_V2_M_Weights.IMAGENET1K_V1).to(self.device)
142
+ # self.feature_dim = self.backbone.classifier[1].in_features
143
+ # self.backbone.classifier = nn.Identity()
144
+
145
+ # self.num_heads = max(1, min(8, self.feature_dim // 64))
146
+ # self.attention = MultiHeadAttention(self.feature_dim, num_heads=self.num_heads).to(self.device)
147
+
148
+ # self.classifier = nn.Sequential(
149
+ # nn.LayerNorm(self.feature_dim),
150
+ # nn.Dropout(0.3),
151
+ # nn.Linear(self.feature_dim, num_classes)
152
+ # )
153
+
154
+ # self.to(device)
155
+
156
+ # def forward(self, x):
157
+ # if x.device != self.device:
158
+ # x = x.to(self.device)
159
+ # features = self.backbone(x)
160
+ # attended_features = self.attention(features)
161
+ # logits = self.classifier(attended_features)
162
+ # return logits, attended_features
163
+
164
+ # def load_model(model_path, model_instance, device):
165
+ # """
166
+ # Enhanced model loading function with device handling.
167
+ # Maintains original function signature for compatibility.
168
+ # """
169
+ # try:
170
+ # print(f"Loading model to device: {device}")
171
+
172
+ # # Load checkpoint with optimizations
173
+ # checkpoint = torch.load(
174
+ # model_path,
175
+ # map_location=device,
176
+ # weights_only=True
177
+ # )
178
+
179
+ # # Load model weights
180
+ # model_instance.load_state_dict(checkpoint['base_model'], strict=False)
181
+ # model_instance = model_instance.to(device)
182
+ # model_instance.eval()
183
+
184
+ # print("Model loading successful")
185
+ # return model_instance
186
+
187
+ # except RuntimeError as e:
188
+ # if "CUDA out of memory" in str(e):
189
+ # print("GPU memory exceeded, falling back to CPU")
190
+ # device = torch.device('cpu')
191
+ # model_instance = model_instance.cpu()
192
+
193
+ # # Retry loading on CPU
194
+ # checkpoint = torch.load(model_path, map_location='cpu')
195
+ # model_instance.load_state_dict(checkpoint['base_model'], strict=False)
196
+ # model_instance.eval()
197
+ # return model_instance
198
+
199
+ # print(f"Model loading error: {str(e)}")
200
+ # raise
201
+
202
+ # # Initialize model
203
+ # num_classes = len(dog_breeds)
204
+
205
+ # model = BaseModel(num_classes=num_classes, device=device)
206
+
207
+ # # 使用優化後的載入函數
208
+ # model = load_model("124_best_model_dog.pth", model, device)
209
+ # model.eval()
210
+
211
+ # # Image preprocessing function
212
+ # def preprocess_image(image):
213
+ # # If the image is numpy.ndarray turn into PIL.Image
214
+ # if isinstance(image, np.ndarray):
215
+ # image = Image.fromarray(image)
216
+
217
+ # # Use torchvision.transforms to process images
218
+ # transform = transforms.Compose([
219
+ # transforms.Resize((224, 224)),
220
+ # transforms.ToTensor(),
221
+ # transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
222
+ # ])
223
+
224
+ # return transform(image).unsqueeze(0)
225
+
226
+ # def initialize_yolo_model(device):
227
+ # try:
228
+ # model_yolo = YOLO('yolov8l.pt')
229
+ # if torch.cuda.is_available():
230
+ # model_yolo.to(device)
231
+ # print(f"YOLO model initialized on {device}")
232
+ # return model_yolo
233
+ # except Exception as e:
234
+ # print(f"Error initializing YOLO model: {str(e)}")
235
+ # print("Attempting to initialize YOLO model on CPU")
236
+ # return YOLO('yolov8l.pt')
237
+
238
+ # model_yolo = initialize_yolo_model(device)
239
+
240
+ # async def predict_single_dog(image):
241
+ # """
242
+ # Predicts the dog breed using only the classifier.
243
+ # Args:
244
+ # image: PIL Image or numpy array
245
+ # Returns:
246
+ # tuple: (top1_prob, topk_breeds, relative_probs)
247
+ # """
248
+ # image_tensor = preprocess_image(image).to(device)
249
+
250
+ # with torch.no_grad():
251
+ # # Get model outputs (只使用logits,不需要features)
252
+ # logits = model(image_tensor)[0] # 如果model仍返回tuple,取第一個元素
253
+ # probs = F.softmax(logits, dim=1)
254
+
255
+ # # Classifier prediction
256
+ # top5_prob, top5_idx = torch.topk(probs, k=5)
257
+ # breeds = [dog_breeds[idx.item()] for idx in top5_idx[0]]
258
+ # probabilities = [prob.item() for prob in top5_prob[0]]
259
+
260
+ # # Calculate relative probabilities
261
+ # sum_probs = sum(probabilities[:3]) # 只取前三個來計算相對概率
262
+ # relative_probs = [f"{(prob/sum_probs * 100):.2f}%" for prob in probabilities[:3]]
263
+
264
+ # # Debug output
265
+ # print("\nClassifier Predictions:")
266
+ # for breed, prob in zip(breeds[:5], probabilities[:5]):
267
+ # print(f"{breed}: {prob:.4f}")
268
+
269
+ # return probabilities[0], breeds[:3], relative_probs
270
+
271
+
272
+ # async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
273
+ # results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
274
+ # dogs = []
275
+ # boxes = []
276
+ # for box in results.boxes:
277
+ # if box.cls == 16: # COCO dataset class for dog is 16
278
+ # xyxy = box.xyxy[0].tolist()
279
+ # confidence = box.conf.item()
280
+ # boxes.append((xyxy, confidence))
281
+
282
+ # if not boxes:
283
+ # dogs.append((image, 1.0, [0, 0, image.width, image.height]))
284
+ # else:
285
+ # nms_boxes = non_max_suppression(boxes, iou_threshold)
286
+
287
+ # for box, confidence in nms_boxes:
288
+ # x1, y1, x2, y2 = box
289
+ # w, h = x2 - x1, y2 - y1
290
+ # x1 = max(0, x1 - w * 0.05)
291
+ # y1 = max(0, y1 - h * 0.05)
292
+ # x2 = min(image.width, x2 + w * 0.05)
293
+ # y2 = min(image.height, y2 + h * 0.05)
294
+ # cropped_image = image.crop((x1, y1, x2, y2))
295
+ # dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
296
+
297
+ # return dogs
298
+
299
+ # def non_max_suppression(boxes, iou_threshold):
300
+ # keep = []
301
+ # boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
302
+ # while boxes:
303
+ # current = boxes.pop(0)
304
+ # keep.append(current)
305
+ # boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
306
+ # return keep
307
+
308
+
309
+ # def calculate_iou(box1, box2):
310
+ # x1 = max(box1[0], box2[0])
311
+ # y1 = max(box1[1], box2[1])
312
+ # x2 = min(box1[2], box2[2])
313
+ # y2 = min(box1[3], box2[3])
314
+
315
+ # intersection = max(0, x2 - x1) * max(0, y2 - y1)
316
+ # area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
317
+ # area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
318
+
319
+ # iou = intersection / float(area1 + area2 - intersection)
320
+ # return iou
321
+
322
+
323
+
324
+ # def create_breed_comparison(breed1: str, breed2: str) -> dict:
325
+ # breed1_info = get_dog_description(breed1)
326
+ # breed2_info = get_dog_description(breed2)
327
+
328
+ # # 標準化數值轉換
329
+ # value_mapping = {
330
+ # 'Size': {'Small': 1, 'Medium': 2, 'Large': 3, 'Giant': 4},
331
+ # 'Exercise_Needs': {'Low': 1, 'Moderate': 2, 'High': 3, 'Very High': 4},
332
+ # 'Care_Level': {'Low': 1, 'Moderate': 2, 'High': 3},
333
+ # 'Grooming_Needs': {'Low': 1, 'Moderate': 2, 'High': 3}
334
+ # }
335
+
336
+ # comparison_data = {
337
+ # breed1: {},
338
+ # breed2: {}
339
+ # }
340
+
341
+ # for breed, info in [(breed1, breed1_info), (breed2, breed2_info)]:
342
+ # comparison_data[breed] = {
343
+ # 'Size': value_mapping['Size'].get(info['Size'], 2), # 預設 Medium
344
+ # 'Exercise_Needs': value_mapping['Exercise_Needs'].get(info['Exercise Needs'], 2), # 預設 Moderate
345
+ # 'Care_Level': value_mapping['Care_Level'].get(info['Care Level'], 2),
346
+ # 'Grooming_Needs': value_mapping['Grooming_Needs'].get(info['Grooming Needs'], 2),
347
+ # 'Good_with_Children': info['Good with Children'] == 'Yes',
348
+ # 'Original_Data': info
349
+ # }
350
+
351
+ # return comparison_data
352
+
353
+
354
+ # async def predict(image):
355
+ # """
356
+ # Main prediction function that handles both single and multiple dog detection.
357
+
358
+ # Args:
359
+ # image: PIL Image or numpy array
360
+
361
+ # Returns:
362
+ # tuple: (html_output, annotated_image, initial_state)
363
+ # """
364
+ # if image is None:
365
+ # return format_warning_html("Please upload an image to start."), None, None
366
+
367
+ # try:
368
+ # if isinstance(image, np.ndarray):
369
+ # image = Image.fromarray(image)
370
+
371
+ # # Detect dogs in the image
372
+ # dogs = await detect_multiple_dogs(image)
373
+ # color_scheme = get_color_scheme(len(dogs) == 1)
374
+
375
+ # # Prepare for annotation
376
+ # annotated_image = image.copy()
377
+ # draw = ImageDraw.Draw(annotated_image)
378
+
379
+ # try:
380
+ # font = ImageFont.truetype("arial.ttf", 24)
381
+ # except:
382
+ # font = ImageFont.load_default()
383
+
384
+ # dogs_info = ""
385
+
386
+ # # Process each detected dog
387
+ # for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
388
+ # color = color_scheme if len(dogs) == 1 else color_scheme[i % len(color_scheme)]
389
+
390
+ # # Draw box and label on image
391
+ # draw.rectangle(box, outline=color, width=4)
392
+ # label = f"Dog {i+1}"
393
+ # label_bbox = draw.textbbox((0, 0), label, font=font)
394
+ # label_width = label_bbox[2] - label_bbox[0]
395
+ # label_height = label_bbox[3] - label_bbox[1]
396
+
397
+ # # Draw label background and text
398
+ # label_x = box[0] + 5
399
+ # label_y = box[1] + 5
400
+ # draw.rectangle(
401
+ # [label_x - 2, label_y - 2, label_x + label_width + 4, label_y + label_height + 4],
402
+ # fill='white',
403
+ # outline=color,
404
+ # width=2
405
+ # )
406
+ # draw.text((label_x, label_y), label, fill=color, font=font)
407
+
408
+ # # Predict breed
409
+ # top1_prob, topk_breeds, relative_probs = await predict_single_dog(cropped_image)
410
+ # combined_confidence = detection_confidence * top1_prob
411
+
412
+ # # Format results based on confidence with error handling
413
+ # try:
414
+ # if combined_confidence < 0.2:
415
+ # dogs_info += format_error_message(color, i+1)
416
+ # elif top1_prob >= 0.45:
417
+ # breed = topk_breeds[0]
418
+ # description = get_dog_description(breed)
419
+ # # Handle missing breed description
420
+ # if description is None:
421
+ # # 如果沒有描述,創建一個基本描述
422
+ # description = {
423
+ # "Name": breed,
424
+ # "Size": "Unknown",
425
+ # "Exercise Needs": "Unknown",
426
+ # "Grooming Needs": "Unknown",
427
+ # "Care Level": "Unknown",
428
+ # "Good with Children": "Unknown",
429
+ # "Description": f"Identified as {breed.replace('_', ' ')}"
430
+ # }
431
+ # dogs_info += format_single_dog_result(breed, description, color)
432
+ # else:
433
+ # # 修改format_multiple_breeds_result的調用,包含錯誤處理
434
+ # dogs_info += format_multiple_breeds_result(
435
+ # topk_breeds,
436
+ # relative_probs,
437
+ # color,
438
+ # i+1,
439
+ # lambda breed: get_dog_description(breed) or {
440
+ # "Name": breed,
441
+ # "Size": "Unknown",
442
+ # "Exercise Needs": "Unknown",
443
+ # "Grooming Needs": "Unknown",
444
+ # "Care Level": "Unknown",
445
+ # "Good with Children": "Unknown",
446
+ # "Description": f"Identified as {breed.replace('_', ' ')}"
447
+ # }
448
+ # )
449
+ # except Exception as e:
450
+ # print(f"Error formatting results for dog {i+1}: {str(e)}")
451
+ # dogs_info += format_error_message(color, i+1)
452
+
453
+ # # Wrap final HTML output
454
+ # html_output = format_multi_dog_container(dogs_info)
455
+
456
+ # # Prepare initial state
457
+ # initial_state = {
458
+ # "dogs_info": dogs_info,
459
+ # "image": annotated_image,
460
+ # "is_multi_dog": len(dogs) > 1,
461
+ # "html_output": html_output
462
+ # }
463
+
464
+ # return html_output, annotated_image, initial_state
465
+
466
+ # except Exception as e:
467
+ # error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
468
+ # print(error_msg)
469
+ # return format_warning_html(error_msg), None, None
470
+
471
+
472
+ # def show_details_html(choice, previous_output, initial_state):
473
+ # """
474
+ # Generate detailed HTML view for a selected breed.
475
+
476
+ # Args:
477
+ # choice: str, Selected breed option
478
+ # previous_output: str, Previous HTML output
479
+ # initial_state: dict, Current state information
480
+
481
+ # Returns:
482
+ # tuple: (html_output, gradio_update, updated_state)
483
+ # """
484
+ # if not choice:
485
+ # return previous_output, gr.update(visible=True), initial_state
486
+
487
+ # try:
488
+ # breed = choice.split("More about ")[-1]
489
+ # description = get_dog_description(breed)
490
+ # html_output = format_breed_details_html(description, breed)
491
+
492
+ # # Update state
493
+ # initial_state["current_description"] = html_output
494
+ # initial_state["original_buttons"] = initial_state.get("buttons", [])
495
+
496
+ # return html_output, gr.update(visible=True), initial_state
497
+
498
+ # except Exception as e:
499
+ # error_msg = f"An error occurred while showing details: {e}"
500
+ # print(error_msg)
501
+ # return format_warning_html(error_msg), gr.update(visible=True), initial_state
502
+
503
+ # def main():
504
+ # print("\n=== System Information ===")
505
+ # print(f"PyTorch Version: {torch.__version__}")
506
+ # print(f"CUDA Available: {torch.cuda.is_available()}")
507
+ # if torch.cuda.is_available():
508
+ # print(f"CUDA Version: {torch.version.cuda}")
509
+ # print(f"Current Device: {torch.cuda.current_device()}")
510
+
511
+ # # 清理 GPU 記憶體(如果可用)
512
+ # if torch.cuda.is_available():
513
+ # torch.cuda.empty_cache()
514
+
515
+ # device = get_device()
516
+
517
+ # with gr.Blocks(css=get_css_styles()) as iface:
518
+ # # Header HTML
519
+
520
+ # gr.HTML("""
521
+ # <header style='text-align: center; padding: 20px; margin-bottom: 20px;'>
522
+ # <h1 style='font-size: 2.5em; margin-bottom: 10px; color: #2D3748;'>
523
+ # 🐾 PawMatch AI
524
+ # </h1>
525
+ # <h2 style='font-size: 1.2em; font-weight: normal; color: #4A5568; margin-top: 5px;'>
526
+ # Your Smart Dog Breed Guide
527
+ # </h2>
528
+ # <div style='width: 50px; height: 3px; background: linear-gradient(90deg, #4299e1, #48bb78); margin: 15px auto;'></div>
529
+ # <p style='color: #718096; font-size: 0.9em;'>
530
+ # Powered by AI • Breed Recognition • Smart Matching • Companion Guide
531
+ # </p>
532
+ # </header>
533
+ # """)
534
+
535
+ # # 先創建歷史組件實例(但不創建標籤頁)
536
+ # history_component = create_history_component()
537
+
538
+ # with gr.Tabs():
539
+ # # 1. 品種檢測標籤頁
540
+ # example_images = [
541
+ # 'Border_Collie.jpg',
542
+ # 'Golden_Retriever.jpeg',
543
+ # 'Saint_Bernard.jpeg',
544
+ # 'Samoyed.jpg',
545
+ # 'French_Bulldog.jpeg'
546
+ # ]
547
+ # detection_components = create_detection_tab(predict, example_images)
548
+
549
+ # # 2. 品種比較標籤頁
550
+ # comparison_components = create_comparison_tab(
551
+ # dog_breeds=dog_breeds,
552
+ # get_dog_description=get_dog_description,
553
+ # breed_health_info=breed_health_info,
554
+ # breed_noise_info=breed_noise_info
555
+ # )
556
+
557
+ # # 3. 品種推薦標籤頁
558
+ # recommendation_components = create_recommendation_tab(
559
+ # UserPreferences=UserPreferences,
560
+ # get_breed_recommendations=get_breed_recommendations,
561
+ # format_recommendation_html=format_recommendation_html,
562
+ # history_component=history_component
563
+ # )
564
+
565
+
566
+ # # 4. 最後創建歷史記錄標籤頁
567
+ # create_history_tab(history_component)
568
+
569
+ # # Footer
570
+ # gr.HTML('''
571
+ # <div style="
572
+ # display: flex;
573
+ # align-items: center;
574
+ # justify-content: center;
575
+ # gap: 20px;
576
+ # padding: 20px 0;
577
+ # ">
578
+ # <p style="
579
+ # font-family: 'Arial', sans-serif;
580
+ # font-size: 14px;
581
+ # font-weight: 500;
582
+ # letter-spacing: 2px;
583
+ # background: linear-gradient(90deg, #555, #007ACC);
584
+ # -webkit-background-clip: text;
585
+ # -webkit-text-fill-color: transparent;
586
+ # margin: 0;
587
+ # text-transform: uppercase;
588
+ # display: inline-block;
589
+ # ">EXPLORE THE CODE →</p>
590
+ # <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/PawMatchAI" style="text-decoration: none;">
591
+ # <img src="https://img.shields.io/badge/GitHub-PawMatch_AI-007ACC?logo=github&style=for-the-badge">
592
+ # </a>
593
+ # </div>
594
+ # ''')
595
+
596
+ # return iface
597
+
598
+ # if __name__ == "__main__":
599
+ # print(f"CUDA available: {torch.cuda.is_available()}")
600
+ # if torch.cuda.is_available():
601
+ # print(f"Current device: {torch.cuda.current_device()}")
602
+ # print(f"Device name: {torch.cuda.get_device_name()}")
603
+ # iface = main()
604
+ # iface.launch()
605
 
 
606
 
607
  history_manager = UserHistoryManager()
608
 
 
634
  "Wire-Haired_Fox_Terrier"]
635
 
636
 
637
+ def get_device():
638
+ """
639
+ Initialize device configuration with proper Zero GPU handling.
640
+ """
641
+ # Default to CPU - safer initial state
642
+ return torch.device('cpu')
643
+
644
+ # Modify the model initialization to be lazy
645
+ class LazyLoadModel:
646
+ def __init__(self):
647
+ self._model = None
648
+ self._device = None
649
+
650
+ @spaces.GPU(duration=30)
651
+ def get_model(self):
652
+ if self._model is None:
653
+ try:
654
+ self._device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
655
+ self._model = BaseModel(num_classes=len(dog_breeds), device=self._device)
656
+ checkpoint = torch.load("124_best_model_dog.pth", map_location=self._device)
657
+ self._model.load_state_dict(checkpoint['base_model'], strict=False)
658
+ self._model.eval()
659
+ except Exception as e:
660
+ print(f"Error initializing model: {e}")
661
+ self._device = torch.device('cpu')
662
+ self._model = BaseModel(num_classes=len(dog_breeds), device=self._device)
663
+ checkpoint = torch.load("124_best_model_dog.pth", map_location='cpu')
664
+ self._model.load_state_dict(checkpoint['base_model'], strict=False)
665
+ self._model.eval()
666
+ return self._model
667
+
668
+ class LazyLoadYOLO:
669
+ def __init__(self):
670
+ self._model = None
671
+
672
+ @spaces.GPU(duration=30)
673
+ def get_model(self):
674
+ if self._model is None:
675
+ try:
676
+ self._model = YOLO('yolov8l.pt')
677
+ except Exception as e:
678
+ print(f"Error initializing YOLO model: {e}")
679
+ raise
680
+ return self._model
681
+
682
+
683
  class MultiHeadAttention(nn.Module):
684
 
685
  def __init__(self, in_dim, num_heads=8):
 
777
  print(f"Model loading error: {str(e)}")
778
  raise
779
 
 
 
 
 
 
 
 
 
 
780
  # Image preprocessing function
781
  def preprocess_image(image):
782
  # If the image is numpy.ndarray turn into PIL.Image
 
792
 
793
  return transform(image).unsqueeze(0)
794
 
795
+ @spaces.GPU(duration=30)
796
+ async def predict_single_dog(image, lazy_model):
 
 
 
 
 
 
 
 
 
 
 
 
 
797
  """
798
+ Predicts the dog breed using only the classifier with proper GPU handling.
 
 
 
 
799
  """
800
+ model = lazy_model.get_model()
801
+ device = model.device
802
  image_tensor = preprocess_image(image).to(device)
803
 
804
  with torch.no_grad():
805
+ logits = model(image_tensor)[0]
 
806
  probs = F.softmax(logits, dim=1)
807
+
 
808
  top5_prob, top5_idx = torch.topk(probs, k=5)
809
  breeds = [dog_breeds[idx.item()] for idx in top5_idx[0]]
810
  probabilities = [prob.item() for prob in top5_prob[0]]
811
+
812
+ sum_probs = sum(probabilities[:3])
 
813
  relative_probs = [f"{(prob/sum_probs * 100):.2f}%" for prob in probabilities[:3]]
814
+
 
 
 
 
 
815
  return probabilities[0], breeds[:3], relative_probs
816
 
817
 
818
+ @spaces.GPU(duration=30)
819
  async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
820
+ model_yolo = lazy_yolo.get_model()
821
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
822
  dogs = []
823
  boxes = []
 
1049
  return format_warning_html(error_msg), gr.update(visible=True), initial_state
1050
 
1051
  def main():
1052
+ # 初始化延遲加載模型
1053
+ lazy_model = LazyLoadModel()
1054
+ lazy_yolo = LazyLoadYOLO()
 
 
 
 
 
 
 
 
 
1055
 
1056
+ # Gradio 介面構建
1057
  with gr.Blocks(css=get_css_styles()) as iface:
1058
+ # 標題部分
 
1059
  gr.HTML("""
1060
  <header style='text-align: center; padding: 20px; margin-bottom: 20px;'>
1061
  <h1 style='font-size: 2.5em; margin-bottom: 10px; color: #2D3748;'>
 
1071
  </header>
1072
  """)
1073
 
1074
+ # ���建歷史組件
1075
  history_component = create_history_component()
1076
 
1077
  with gr.Tabs():
1078
+ # 品種檢測標籤頁
1079
  example_images = [
1080
  'Border_Collie.jpg',
1081
  'Golden_Retriever.jpeg',
 
1083
  'Samoyed.jpg',
1084
  'French_Bulldog.jpeg'
1085
  ]
1086
+ detection_components = create_detection_tab(
1087
+ lambda img: predict(img, lazy_model, lazy_yolo),
1088
+ example_images
1089
+ )
1090
 
1091
+ # 品種比較標籤頁
1092
  comparison_components = create_comparison_tab(
1093
  dog_breeds=dog_breeds,
1094
  get_dog_description=get_dog_description,
 
1096
  breed_noise_info=breed_noise_info
1097
  )
1098
 
1099
+ # 品種推薦標籤頁
1100
  recommendation_components = create_recommendation_tab(
1101
  UserPreferences=UserPreferences,
1102
  get_breed_recommendations=get_breed_recommendations,
 
1104
  history_component=history_component
1105
  )
1106
 
1107
+ # 歷史記錄標籤頁
 
1108
  create_history_tab(history_component)
1109
 
1110
  # Footer
 
1137
  return iface
1138
 
1139
  if __name__ == "__main__":
 
 
 
 
1140
  iface = main()
1141
  iface.launch()