File size: 4,800 Bytes
8c953f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Debug script to test model loading and identify issues.

Run this to diagnose problems with model loading.

"""

import torch
import logging
from transformers import AutoModelForImageClassification, AutoImageProcessor
from huggingface_hub import list_repo_files, model_info

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

print("=" * 70)
print("INDIAN CURRENCY DETECTOR - MODEL LOADING DIAGNOSTICS")
print("=" * 70)

# 1. Check PyTorch
print("\n[1] PYTORCH CHECK")
print(f"    PyTorch version: {torch.__version__}")
print(f"    GPU available: {torch.cuda.is_available()}")
if torch.cuda.is_available():
    print(f"    GPU name: {torch.cuda.get_device_name(0)}")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"    Using device: {device}")

# 2. Check HuggingFace connectivity
print("\n[2] HUGGINGFACE CONNECTIVITY CHECK")
MODEL_ID = "Shubhamm007/indian-currency-classifier"
print(f"    Model ID: {MODEL_ID}")

try:
    print(f"    Checking model repository access...")
    info = model_info(MODEL_ID)
    print(f"    βœ“ Model found on HuggingFace!")
    print(f"    Model type: {info.model_name}")
    print(f"    Downloads: {info.downloads}")
    print(f"    Likes: {info.likes}")
except Exception as e:
    print(f"    βœ— Error accessing model: {e}")
    print(f"    Error type: {type(e).__name__}")
    print(f"\n    TROUBLESHOOTING:")
    print(f"    - Check your internet connection")
    print(f"    - Verify model ID is correct")
    print(f"    - Try: huggingface-cli login (if private model)")
    print(f"    - Check HuggingFace hub status at: https://status.huggingface.co/")

# 3. Try loading model
print("\n[3] MODEL LOADING TEST")
try:
    print(f"    Loading model: {MODEL_ID}")
    model = AutoModelForImageClassification.from_pretrained(
        MODEL_ID,
        trust_remote_code=True
    )
    print(f"    βœ“ Model loaded successfully!")
    print(f"    Model class: {model.__class__.__name__}")
    print(f"    Number of parameters: {sum(p.numel() for p in model.parameters()):,}")
    
    # Move to device
    model.to(device)
    model.eval()
    print(f"    βœ“ Model moved to {device} and set to eval mode")
    
except Exception as e:
    print(f"    βœ— Error loading model: {e}")
    print(f"    Error type: {type(e).__name__}")
    print(f"\n    TROUBLESHOOTING:")
    print(f"    - Check model repository exists")
    print(f"    - Try clearing cache: rm -rf ~/.cache/huggingface/hub/")
    print(f"    - Try fallback model: google/vit-base-patch16-224")

# 4. Try loading image processor
print("\n[4] IMAGE PROCESSOR TEST")
try:
    print(f"    Loading image processor for: {MODEL_ID}")
    processor = AutoImageProcessor.from_pretrained(MODEL_ID)
    print(f"    βœ“ Image processor loaded successfully!")
    print(f"    Processor class: {processor.__class__.__name__}")
except Exception as e:
    print(f"    βœ— Error loading processor: {e}")
    print(f"    Using fallback: Custom preprocessing")

# 5. Test inference
print("\n[5] INFERENCE TEST")
try:
    from PIL import Image
    import requests
    from io import BytesIO
    
    print(f"    Downloading sample image...")
    # Download a simple test image
    url = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg"
    response = requests.get(url, timeout=10)
    image = Image.open(BytesIO(response.content))
    
    print(f"    Image loaded: {image.size}")
    
    # Try prediction
    from torchvision import transforms
    
    # Preprocess
    transform = transforms.Compose([
        transforms.Resize((384, 384)),
        transforms.ToTensor(),
        transforms.Normalize(
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225]
        )
    ])
    
    image_tensor = transform(image).unsqueeze(0).to(device)
    
    # Run inference
    with torch.no_grad():
        outputs = model(image_tensor)
        logits = outputs.logits
    
    probabilities = torch.softmax(logits, dim=-1)
    predicted_class = torch.argmax(probabilities, dim=-1).item()
    confidence = probabilities[0][predicted_class].item() * 100
    
    print(f"    βœ“ Inference successful!")
    print(f"    Predicted class: {predicted_class}")
    print(f"    Confidence: {confidence:.2f}%")

except Exception as e:
    print(f"    βœ— Error during inference test: {e}")
    print(f"    Error type: {type(e).__name__}")

print("\n" + "=" * 70)
print("DIAGNOSTICS COMPLETE")
print("=" * 70)
print("\nIf you see βœ“ marks above, your setup is working correctly!")
print("If you see βœ— marks, check the troubleshooting steps for that section.")