Spaces:
Sleeping
Sleeping
bikram
commited on
Commit
Β·
80602df
1
Parent(s):
d08383c
Added new classifier
Browse files
__pycache__/utils.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/utils.cpython-310.pyc and b/__pycache__/utils.cpython-310.pyc differ
|
|
|
models/{nepali_english_classifier_n.h5 β dev_roman_classifier.pth}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:54be1436e92b8e32bfa6f62b59a5af27181999ae8f9e08452f1fe699e8b6fef7
|
| 3 |
+
size 95411068
|
models/{label_encoder_n.pkl β dev_roman_label_encoder.pkl}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9ca7716f756b0397c777fa79fa61d6e64c174fe03c598c97b50889e0c587700f
|
| 3 |
+
size 298
|
utils.py
CHANGED
|
@@ -374,24 +374,27 @@ def predict_ne(image_path, device="cpu"):
|
|
| 374 |
# load label encoder
|
| 375 |
|
| 376 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 377 |
-
model = ResNetClassifier(num_classes=
|
| 378 |
# model.eval()
|
| 379 |
transform = transforms.Compose([
|
| 380 |
-
transforms.Resize(
|
|
|
|
| 381 |
transforms.ToTensor(),
|
| 382 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 383 |
-
|
|
|
|
|
|
|
| 384 |
image = Image.open(image_path).convert('RGB')
|
| 385 |
image_tensor = transform(image).unsqueeze(0).to(device)
|
| 386 |
-
|
|
|
|
|
|
|
| 387 |
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
with torch.no_grad():
|
| 389 |
output = model(image_tensor)
|
| 390 |
_, predicted = torch.max(output, 1)
|
| 391 |
-
|
| 392 |
-
if predicted.item() == 0:
|
| 393 |
-
return "Nepali"
|
| 394 |
-
elif predicted.item() == 1:
|
| 395 |
-
return "English"
|
| 396 |
-
else:
|
| 397 |
-
return predicted.item()
|
|
|
|
| 374 |
# load label encoder
|
| 375 |
|
| 376 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 377 |
+
model = ResNetClassifier(num_classes=4).to(device)
|
| 378 |
# model.eval()
|
| 379 |
transform = transforms.Compose([
|
| 380 |
+
transforms.Resize(256), # Resize shorter side to 256
|
| 381 |
+
transforms.CenterCrop(224), # Crop center 224x224 patch
|
| 382 |
transforms.ToTensor(),
|
| 383 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 384 |
+
std=[0.229, 0.224, 0.225])
|
| 385 |
+
])
|
| 386 |
+
|
| 387 |
image = Image.open(image_path).convert('RGB')
|
| 388 |
image_tensor = transform(image).unsqueeze(0).to(device)
|
| 389 |
+
|
| 390 |
+
# loading model weights/state_dict
|
| 391 |
+
model.load_state_dict(torch.load('models/dev_roman_classifier.pth', map_location=device))
|
| 392 |
model.eval()
|
| 393 |
+
|
| 394 |
+
# loading label encoder
|
| 395 |
+
with open('models/dev_roman_label_encoder.pkl', 'rb') as f:
|
| 396 |
+
le = pickle.load(f)
|
| 397 |
with torch.no_grad():
|
| 398 |
output = model(image_tensor)
|
| 399 |
_, predicted = torch.max(output, 1)
|
| 400 |
+
return le.inverse_transform([predicted.item()])[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|