1import torch, timm, numpy as np
2import albumentations as A
3from albumentations.pytorch import ToTensorV2
4from huggingface_hub import hf_hub_download
5from PIL import Image
6
7weights = hf_hub_download('Vansh180/plastic-waste-classifier', 'plastic_classifier_best.pt')
8model = timm.create_model('convnext_base.fb_in22k_ft_in1k_384', pretrained=False, num_classes=6)
9model.load_state_dict(torch.load(weights, map_location='cpu'))
10model.eval()
11
12transform = A.Compose([
13 A.Resize(416, 416), A.CenterCrop(384, 384),
14 A.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225]), ToTensorV2(),
15])
16CLASS_NAMES = ['HDPE','LDPE','Other','PET','PP','PS']
17img = np.array(Image.open('plastic.jpg').convert('RGB'))
18t = transform(image=img)['image'].unsqueeze(0)
19with torch.no_grad():
20 probs = torch.softmax(model(t), dim=1)[0]
21print(CLASS_NAMES[probs.argmax()], f'{probs.max():.1%}')