Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -540,27 +540,35 @@ import asyncio
|
|
540 |
import traceback
|
541 |
|
542 |
def get_device():
|
543 |
-
print("Initializing
|
544 |
-
if not torch.cuda.is_available():
|
545 |
-
print("CUDA is not available, using CPU")
|
546 |
-
return torch.device('cpu')
|
547 |
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
564 |
return torch.device('cpu')
|
565 |
|
566 |
device = get_device()
|
@@ -631,12 +639,12 @@ class BaseModel(nn.Module):
|
|
631 |
self.device = device
|
632 |
print(f"Initializing model on device: {device}")
|
633 |
|
634 |
-
self.backbone = efficientnet_v2_m(weights=EfficientNet_V2_M_Weights.IMAGENET1K_V1).to(device)
|
635 |
self.feature_dim = self.backbone.classifier[1].in_features
|
636 |
self.backbone.classifier = nn.Identity()
|
637 |
|
638 |
self.num_heads = max(1, min(8, self.feature_dim // 64))
|
639 |
-
self.attention = MultiHeadAttention(self.feature_dim, num_heads=self.num_heads).to(device)
|
640 |
|
641 |
self.classifier = nn.Sequential(
|
642 |
nn.LayerNorm(self.feature_dim),
|
@@ -647,7 +655,8 @@ class BaseModel(nn.Module):
|
|
647 |
self.to(device)
|
648 |
|
649 |
def forward(self, x):
|
650 |
-
x
|
|
|
651 |
features = self.backbone(x)
|
652 |
attended_features = self.attention(features)
|
653 |
logits = self.classifier(attended_features)
|
@@ -682,9 +691,19 @@ def preprocess_image(image):
|
|
682 |
|
683 |
return transform(image).unsqueeze(0)
|
684 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
685 |
|
686 |
-
model_yolo =
|
687 |
-
model_yolo.to(device)
|
688 |
|
689 |
async def predict_single_dog(image):
|
690 |
"""
|
@@ -950,12 +969,18 @@ def show_details_html(choice, previous_output, initial_state):
|
|
950 |
return format_warning_html(error_msg), gr.update(visible=True), initial_state
|
951 |
|
952 |
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
953 |
if torch.cuda.is_available():
|
954 |
torch.cuda.empty_cache()
|
955 |
-
|
956 |
-
|
957 |
-
print(f"CUDA initialized: {torch.cuda.is_initialized()}")
|
958 |
-
print(f"Current device: {torch.cuda.current_device() if torch.cuda.is_available() else 'CPU'}")
|
959 |
|
960 |
with gr.Blocks(css=get_css_styles()) as iface:
|
961 |
# Header HTML
|
|
|
540 |
import traceback
|
541 |
|
542 |
def get_device():
|
543 |
+
print("Initializing device configuration...")
|
|
|
|
|
|
|
544 |
|
545 |
+
# 首先嘗試使用 CUDA,但要更謹慎地處理初始化
|
546 |
+
if torch.cuda.is_available():
|
547 |
+
try:
|
548 |
+
# 設置環境變量,告訴 PyTorch 在沒有 GPU 時自動回退到 CPU
|
549 |
+
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:512'
|
550 |
+
|
551 |
+
device = torch.device('cuda')
|
552 |
+
# 使用 try-except 來處理 GPU 信息獲取
|
553 |
+
try:
|
554 |
+
print(f"Using GPU: {torch.cuda.get_device_name(0)}")
|
555 |
+
print(f"GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB")
|
556 |
+
except Exception as e:
|
557 |
+
print("GPU detected but couldn't get detailed information")
|
558 |
+
|
559 |
+
# 進行一個小的測試計算來驗證 GPU 功能
|
560 |
+
test_tensor = torch.rand(1).to(device)
|
561 |
+
_ = test_tensor * test_tensor
|
562 |
+
print("GPU test calculation successful")
|
563 |
+
|
564 |
+
return device
|
565 |
+
|
566 |
+
except Exception as e:
|
567 |
+
print(f"GPU initialization failed: {str(e)}")
|
568 |
+
print("Falling back to CPU")
|
569 |
+
return torch.device('cpu')
|
570 |
+
else:
|
571 |
+
print("CUDA not available, using CPU")
|
572 |
return torch.device('cpu')
|
573 |
|
574 |
device = get_device()
|
|
|
639 |
self.device = device
|
640 |
print(f"Initializing model on device: {device}")
|
641 |
|
642 |
+
self.backbone = efficientnet_v2_m(weights=EfficientNet_V2_M_Weights.IMAGENET1K_V1).to(self.device)
|
643 |
self.feature_dim = self.backbone.classifier[1].in_features
|
644 |
self.backbone.classifier = nn.Identity()
|
645 |
|
646 |
self.num_heads = max(1, min(8, self.feature_dim // 64))
|
647 |
+
self.attention = MultiHeadAttention(self.feature_dim, num_heads=self.num_heads).to(self.device)
|
648 |
|
649 |
self.classifier = nn.Sequential(
|
650 |
nn.LayerNorm(self.feature_dim),
|
|
|
655 |
self.to(device)
|
656 |
|
657 |
def forward(self, x):
|
658 |
+
if x.device != self.device:
|
659 |
+
x = x.to(self.device)
|
660 |
features = self.backbone(x)
|
661 |
attended_features = self.attention(features)
|
662 |
logits = self.classifier(attended_features)
|
|
|
691 |
|
692 |
return transform(image).unsqueeze(0)
|
693 |
|
694 |
+
def initialize_yolo_model(device):
|
695 |
+
try:
|
696 |
+
model_yolo = YOLO('yolov8l.pt')
|
697 |
+
if torch.cuda.is_available():
|
698 |
+
model_yolo.to(device)
|
699 |
+
print(f"YOLO model initialized on {device}")
|
700 |
+
return model_yolo
|
701 |
+
except Exception as e:
|
702 |
+
print(f"Error initializing YOLO model: {str(e)}")
|
703 |
+
print("Attempting to initialize YOLO model on CPU")
|
704 |
+
return YOLO('yolov8l.pt')
|
705 |
|
706 |
+
model_yolo = initialize_yolo_model(device)
|
|
|
707 |
|
708 |
async def predict_single_dog(image):
|
709 |
"""
|
|
|
969 |
return format_warning_html(error_msg), gr.update(visible=True), initial_state
|
970 |
|
971 |
def main():
|
972 |
+
print("\n=== System Information ===")
|
973 |
+
print(f"PyTorch Version: {torch.__version__}")
|
974 |
+
print(f"CUDA Available: {torch.cuda.is_available()}")
|
975 |
+
if torch.cuda.is_available():
|
976 |
+
print(f"CUDA Version: {torch.version.cuda}")
|
977 |
+
print(f"Current Device: {torch.cuda.current_device()}")
|
978 |
+
|
979 |
+
# 清理 GPU 記憶體(如果可用)
|
980 |
if torch.cuda.is_available():
|
981 |
torch.cuda.empty_cache()
|
982 |
+
|
983 |
+
device = get_device()
|
|
|
|
|
984 |
|
985 |
with gr.Blocks(css=get_css_styles()) as iface:
|
986 |
# Header HTML
|