1import torch
2from torchvision import models, transforms
3from PIL import Image
4
5# Load model
6model = models.mobilenet_v3_small()
7model.classifier = torch.nn.Sequential(
8 torch.nn.Linear(576, 256),
9 torch.nn.Hardswish(),
10 torch.nn.Dropout(0.3),
11 torch.nn.Linear(256, 7)
12)
13checkpoint = torch.load("mobilenetv3_currency.pth", map_location="cpu")
14model.load_state_dict(checkpoint['model_state_dict'])
15model.eval()
16
17# Preprocess
18transform = transforms.Compose([
19 transforms.Resize(256),
20 transforms.CenterCrop(224),
21 transforms.ToTensor(),
22 transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
23])
24
25# Predict
26img = Image.open("your_currency_photo.jpg").convert("RGB")
27input_tensor = transform(img).unsqueeze(0)
28with torch.no_grad():
29 output = model(input_tensor)
30 class_names = ["₹10", "₹100", "₹20", "₹200", "₹2000", "₹50", "₹500"]
31 prediction = class_names[output.argmax(1).item()]
32 confidence = torch.softmax(output, 1).max().item()
33 print(f"Detected: {prediction} ({confidence:.1%})")
1from ultralytics import YOLO
2
3model = YOLO("yolov8n_currency_best.pt")
4results = model("currency_photo.jpg")
5results[0].show()
1import onnxruntime as ort
2import numpy as np
3from PIL import Image
4
5# CNN Classifier
6session = ort.InferenceSession("mobilenetv3_currency.onnx")
7img = Image.open("currency.jpg").resize((224, 224))
8img_array = np.array(img).astype(np.float32) / 255.0
9img_array = (img_array - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225]
10input_data = img_array.transpose(2, 0, 1)[np.newaxis, ...]
11outputs = session.run(None, {"input": input_data})
12class_names = ["₹10", "₹100", "₹20", "₹200", "₹2000", "₹50", "₹500"]
13print(f"Predicted: {class_names[np.argmax(outputs[0])]}")