Using our trained model against a specific image

Is there a better, less verbose perhaps, way than this …

img = open_image(PATH/'valid/in/112.jpg')
train_tfms, val_tfms = get_transforms()
img = apply_tfms(val_tfms.append(normalize_funcs(*imagenet_stats)), img, size=224)

backbone = create_body(arch(models.resnet34), -2)
head = create_head(num_features(backbone) * 2, 2)
m = nn.Sequential(backbone, head)

log_probs = m(

preds = torch.argmax(log_probs, dim=1)
# => tensor([1])

This works but I can’t help imagining that there is a better way (especially when it comes to the 3 lines above to build the model which are essentially pulled out of ConvLearner.

btw, I really love the open_image function … makes grabbing the image and using it as a tensor so easy :slight_smile:

Hoping this thread can be used by folks to submit what they believe is the best way to run a single example through their models (I highly suspect that mine is the one).


5 posts were merged into an existing topic: How to run a trained model against a custom image?