import json import time from PIL import Image import torch from torchvision.transforms import transforms device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = torch.load("path/to/your/model.pth") model.to(device) model.eval() transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) with open("tags_8034.json", "r") as f: tags = json.load(f) tags = sorted(tags) tags.append("placeholder0") tags.append("explicit") tags.append("questionable") tags.append("safe") image_path = "path/to/your/image.jpg" start = time.time() img = Image.open(image_path).convert('RGB') img.thumbnail((448, 448), Image.LANCZOS) tensor = transform(img).unsqueeze(0).to(device) with torch.no_grad(): out = model(tensor) probabilities = torch.nn.functional.sigmoid(out[0]) indices = torch.where(probabilities > 0.3)[0] values = probabilities[indices] for i in range(indices.size(0)): print(tags[indices[i]], values[i].item()) end = time.time() print(f'Executed in {end - start} seconds') print("\n\n", end="")