from PIL import Image from torchvision import prototype as P img = Image.open("test/assets/encode_jpeg/grace_hopper_517x606.jpg") # Initialize model weights = P.models.ResNet50_Weights.IMAGENET1K_V2 model = P.models.resnet50(weights=weights) model.eval()
# Initialize inference transforms preprocess = weights.transforms() # Apply inference preprocessing transforms batch = preprocess(img).unsqueeze(0) prediction = model(batch).squeeze(0).softmax(0) # Make predictions label = prediction.argmax().item() score = prediction[label].item() # Use meta to get the labels category_name = weights.meta['categories'][label] print(f"{category_name}: {100 * score}%")