timm 文件
SWSL ResNet
加入 Hugging Face 社群
並獲得增強的文件體驗
開始使用
SWSL ResNet
殘差網路,或稱 ResNets,透過參考層輸入來學習殘差函式,而不是學習無參考函式。殘差網路不希望每幾個堆疊的層直接擬合所需的底層對映,而是讓這些層擬合殘差對映。它們將 殘差塊 堆疊起來形成網路:例如,ResNet-50 使用這些塊有五十層。
此集合中的模型利用半弱監督學習來提高模型效能。該方法為影像、影片和細粒度分類的標準架構帶來了重要的增益。
請注意這些權重的 CC-BY-NC 4.0 許可證,僅限非商業用途。
我如何在一個影像上使用這個模型?
載入預訓練模型
>>> import timm
>>> model = timm.create_model('swsl_resnet18', pretrained=True)
>>> model.eval()
載入並預處理影像
>>> import urllib
>>> from PIL import Image
>>> from timm.data import resolve_data_config
>>> from timm.data.transforms_factory import create_transform
>>> config = resolve_data_config({}, model=model)
>>> transform = create_transform(**config)
>>> url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
>>> urllib.request.urlretrieve(url, filename)
>>> img = Image.open(filename).convert('RGB')
>>> tensor = transform(img).unsqueeze(0) # transform and add batch dimension
獲取模型預測結果
>>> import torch
>>> with torch.inference_mode():
... out = model(tensor)
>>> probabilities = torch.nn.functional.softmax(out[0], dim=0)
>>> print(probabilities.shape)
>>> # prints: torch.Size([1000])
獲取排名前 5 的預測類別名稱
>>> # Get imagenet class mappings
>>> url, filename = ("https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt", "imagenet_classes.txt")
>>> urllib.request.urlretrieve(url, filename)
>>> with open("imagenet_classes.txt", "r") as f:
... categories = [s.strip() for s in f.readlines()]
>>> # Print top categories per image
>>> top5_prob, top5_catid = torch.topk(probabilities, 5)
>>> for i in range(top5_prob.size(0)):
... print(categories[top5_catid[i]], top5_prob[i].item())
>>> # prints class names and probabilities like:
>>> # [('Samoyed', 0.6425196528434753), ('Pomeranian', 0.04062102362513542), ('keeshond', 0.03186424449086189), ('white wolf', 0.01739676296710968), ('Eskimo dog', 0.011717947199940681)]
將模型名稱替換為您想要使用的變體,例如 swsl_resnet18
。您可以在本頁頂部的模型摘要中找到 ID。
要使用此模型提取影像特徵,請遵循 timm 特徵提取示例,只需更改你想使用的模型名稱。
我如何微調這個模型?
你可以透過更改分類器(最後一層)來微調任何預訓練模型。
>>> model = timm.create_model('swsl_resnet18', pretrained=True, num_classes=NUM_FINETUNE_CLASSES)
要在自己的資料集上進行微調,你需要編寫一個訓練迴圈或修改 timm 的訓練指令碼以使用你的資料集。
我如何訓練這個模型?
你可以按照 timm 食譜指令碼來重新訓練一個新模型。
引用
@article{DBLP:journals/corr/abs-1905-00546,
author = {I. Zeki Yalniz and
Herv{\'{e}} J{\'{e}}gou and
Kan Chen and
Manohar Paluri and
Dhruv Mahajan},
title = {Billion-scale semi-supervised learning for image classification},
journal = {CoRR},
volume = {abs/1905.00546},
year = {2019},
url = {http://arxiv.org/abs/1905.00546},
archivePrefix = {arXiv},
eprint = {1905.00546},
timestamp = {Mon, 28 Sep 2020 08:19:37 +0200},
biburl = {https://dblp.org/rec/journals/corr/abs-1905-00546.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}