Views
No views yet
1import requests
2import torch
3from PIL import Image
4
5from transformers import EfficientFormerImageProcessor, EfficientFormerForImageClassificationWithTeacher
6
7# Load a COCO image of two cats to test the model
8url = "http://images.cocodataset.org/val2017/000000039769.jpg"
9image = Image.open(requests.get(url, stream=True).raw)
10
11# Load preprocessor and pretrained model
12model_name = "huggingface/efficientformer-l3-300"
13processor = EfficientFormerImageProcessor.from_pretrained(model_name)
14model = EfficientFormerForImageClassificationWithTeacher.from_pretrained(model_name)
15
16# Preprocess input image
17inputs = processor(images=image, return_tensors="pt")
18
19# Inference
20with torch.no_grad():
21 outputs = model(**inputs)
22
23# Print the top ImageNet1k class prediction
24logits = outputs.logits
25scores = torch.nn.functional.softmax(logits, dim=1)
26top_pred_class = torch.argmax(scores, dim=1)
27print(f"Predicted class: {top_pred_class}")1@article{li2022efficientformer,
2 title={EfficientFormer: Vision Transformers at MobileNet Speed},
3 author={Li, Yanyu and Yuan, Geng and Wen, Yang and Hu, Eric and Evangelidis, Georgios and Tulyakov, Sergey and Wang, Yanzhi and Ren, Jian},
4 journal={arXiv preprint arXiv:2206.01191},
5 year={2022}
6}