Spaces:
Sleeping
Sleeping
File size: 5,073 Bytes
8f59aab | 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 | #!/usr/bin/env python
"""
Diagnostic script - Kiểm tra tất cả lỗi trước khi chạy app
"""
import sys
import os
print("\n" + "="*70)
print("🔍 DIAGNOSTIC CHECK - Medical Image Segmentation App")
print("="*70)
# 1. Check Python version
print("\n1️⃣ Python Version:")
print(f" Version: {sys.version}")
if sys.version_info >= (3, 8):
print(" ✅ OK (>= 3.8)")
else:
print(" ❌ FAIL (need >= 3.8)")
sys.exit(1)
# 2. Check required modules
print("\n2️⃣ Checking Required Modules:")
required_modules = [
'torch',
'torchvision',
'transformers',
'gradio',
'numpy',
'PIL'
]
missing_modules = []
for module in required_modules:
try:
__import__(module)
print(f" ✅ {module}")
except ImportError as e:
print(f" ❌ {module}: {e}")
missing_modules.append(module)
if missing_modules:
print(f"\n❌ Missing modules: {', '.join(missing_modules)}")
print("Install with: pip install " + " ".join(missing_modules))
sys.exit(1)
# 3. Check model files
print("\n3️⃣ Checking Model Files:")
model_path = os.path.join(os.getcwd(), "segformer_trained_weights")
if os.path.exists(model_path):
print(f" ✅ Model path exists: {model_path}")
files = os.listdir(model_path)
print(f" Files in model dir: {files}")
if "pytorch_model.bin" in files:
print(" ✅ pytorch_model.bin found")
else:
print(" ⚠️ pytorch_model.bin NOT found")
if "config.json" in files:
print(" ✅ config.json found")
else:
print(" ⚠️ config.json NOT found")
else:
print(f" ❌ Model path NOT found: {model_path}")
# 4. Check samples directory
print("\n4️⃣ Checking Sample Images:")
samples_path = os.path.join(os.getcwd(), "samples")
if os.path.exists(samples_path):
sample_files = os.listdir(samples_path)
sample_count = len([f for f in sample_files if f.endswith('.png')])
print(f" ✅ Samples directory exists")
print(f" Found {sample_count} PNG images")
else:
print(f" ⚠️ Samples directory NOT found")
# 5. Try importing app modules
print("\n5️⃣ Testing App Imports:")
try:
import torch
print(" ✅ torch")
except ImportError as e:
print(f" ❌ torch: {e}")
sys.exit(1)
try:
import torch.nn.functional as F
print(" ✅ torch.nn.functional")
except ImportError as e:
print(f" ❌ torch.nn.functional: {e}")
sys.exit(1)
try:
import torchvision.transforms as TF
print(" ✅ torchvision.transforms")
except ImportError as e:
print(f" ❌ torchvision.transforms: {e}")
sys.exit(1)
try:
from transformers import SegformerForSemanticSegmentation
print(" ✅ transformers.SegformerForSemanticSegmentation")
except ImportError as e:
print(f" ❌ transformers: {e}")
sys.exit(1)
try:
import gradio as gr
print(" ✅ gradio")
except ImportError as e:
print(f" ❌ gradio: {e}")
sys.exit(1)
try:
from PIL import Image
print(" ✅ PIL.Image")
except ImportError as e:
print(f" ❌ PIL: {e}")
sys.exit(1)
# 6. Try loading the model
print("\n6️⃣ Testing Model Loading:")
try:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f" Device: {device}")
model = SegformerForSemanticSegmentation.from_pretrained(
model_path,
num_labels=4,
ignore_mismatched_sizes=True
)
model.to(device)
model.eval()
print(" ✅ Model loaded successfully")
print(f" Model parameters: {sum(p.numel() for p in model.parameters())/1e6:.1f}M")
except Exception as e:
print(f" ❌ Model loading failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
# 7. Test preprocessing
print("\n7️⃣ Testing Preprocessing:")
try:
preprocess = TF.Compose([
TF.Resize(size=(288, 288)),
TF.ToTensor(),
TF.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225), inplace=True),
])
print(" ✅ Preprocessing pipeline created")
except Exception as e:
print(f" ❌ Preprocessing failed: {e}")
sys.exit(1)
# 8. Test with dummy input
print("\n8️⃣ Testing Inference with Dummy Input:")
try:
with torch.no_grad():
dummy = torch.randn(1, 3, 288, 288).to(device)
output = model(pixel_values=dummy)
print(" ✅ Model forward pass successful")
print(f" Output shape: {output.logits.shape}")
except Exception as e:
print(f" ❌ Model inference failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
# 9. Check app.py syntax
print("\n9️⃣ Checking app.py Syntax:")
try:
with open("app.py", "r", encoding="utf-8") as f:
code = f.read()
compile(code, "app.py", "exec")
print(" ✅ app.py syntax OK")
except SyntaxError as e:
print(f" ❌ Syntax error: {e}")
sys.exit(1)
print("\n" + "="*70)
print("✅ ALL CHECKS PASSED - App should run successfully!")
print("="*70)
print("\n🚀 You can now run: python app.py\n")
|