EfficientNet 模型在 ImageNet-1k 上以 600x600 的分辨率进行训练。
EfficientNet 是一种移动端友好的纯卷积模型(ConvNet),它提出了一种新的缩放方法,该方法使用一个简单但高效的复合系数来均匀地缩放深度、宽度和分辨率的所有维度。
您可以将原始模型用于图像分类。
以下是如何使用此模型将 COCO 2017 数据集的图像分类为 1000 个 ImageNet 类别之一的方法:
import json
from PIL import Image
import torch
import torch_npu
from torchvision import transforms
from efficientnet_pytorch import EfficientNet
device = torch.device("npu:0")
model = EfficientNet.from_pretrained('efficientnet-b7', weights_path="./models/efficientnet-b7-dcc49843.pth")
# Preprocess image
tfms = transforms.Compose([transforms.Resize(224), transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),])
img = tfms(Image.open('./data/bus.jpg')).unsqueeze(0).to(device)
print(img.shape) # torch.Size([1, 3, 224, 224])
# Load ImageNet class names
labels_map = json.load(open('./examples/labels_map.txt'))
labels_map = [labels_map[str(i)] for i in range(1000)]
# Classify
model.eval().to(device)
with torch.no_grad():
outputs = model(img)
# Print predictions
print('-----')
for idx in torch.topk(outputs, k=5).indices.squeeze(0).tolist():
prob = torch.softmax(outputs, dim=1)[0, idx].item()
print('{label:<75} ({p:.2f}%)'.format(label=labels_map[idx], p=prob*100))