facebookresearch / dinov2

PyTorch code and models for the DINOv2 self-supervised learning method.
Apache License 2.0
8.79k stars 763 forks source link

Loading Pretrained Models Locally and Avoiding Redundant Downloads #91

Open aaiguy opened 1 year ago

aaiguy commented 1 year ago

How can I load the pretrained Dinov2 model from a local source so that it loads the model even when there is no internet connection and does not attempt to download it again from the server? The normal way to load the model is as follows: diov2_vitl14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitl14')

I tried below approach passing folder path of torch hub facebookresearch/dinov2 along with source as local but still it tries to download model from server diov2_vitl14 = torch.hub.load(r'C:\Users\john1411/.cache\torch\hub\facebookresearch_dinov2_main', 'dinov2_vitl14',source='local') downloaded model is located in this location "C:\Users\john1411.cache\torch\hub\checkpoints\dinov2_vitl14_pretrain.pth"

Sntz91 commented 1 year ago

Hi Aaiguy,

You can, for example, initialize the model as follows

from dinov2.models.vision_transformer import vit_large 

model = vit_large(
    patch_size=14,
    img_size=526,
    init_values=1.0,
    block_chunks=0
 )

Then you can use the following line to load pretrained models (where it is in the same directory and called dinov2_vitl14_pretrain.pth:

 model.load_state_dict(torch.load('dinov2_vitl14_pretrain.pth'))
aaiguy commented 1 year ago

Thanks this works :)

xcp2022beGood commented 1 year ago

Hi Aaiguy,

You can, for example, initialize the model as follows

from dinov2.models.vision_transformer import vit_large 

model = vit_large(
    patch_size=14,
    img_size=526,
    init_values=1.0,
    block_chunks=0
 )

Then you can use the following line to load pretrained models (where it is in the same directory and called dinov2_vitl14_pretrain.pth:

model.load_state_dict(torch.load('dinov2_vitl14_pretrain.pth'))

Hi friend: What is the tensor obtained after model processing? How can I use this tensor to see the result of my image after being processed by the model?

patricklabatut commented 1 year ago

it loads the model even when there is no internet connection and does not attempt to download it again from the server?

@aaiguy Copy the model weights to PyTorch Hub's cache under ~/.cache/torch/hub/checkpoints. Then torch.hub.load() will hit this cache and not try to download the weights again.

IDDT commented 1 year ago

The following worked:

wget -O dinov2_vits14.pth https://dl.fbaipublicfiles.com/dinov2/dinov2_vits14/dinov2_vits14_pretrain.pth
wget -O dinov2.zip https://github.com/facebookresearch/dinov2/archive/refs/heads/main.zip
unzip -q dinov2.zip && rm dinov2.zip && mv dinov2-main dinov2
model = torch.hub.load('dinov2', 'dinov2_vits14', source='local', pretrained=False)
model.load_state_dict(torch.load('dinov2_vits14.pth'))
sectionZ6 commented 1 month ago

@aaiguy because pytorch hub has a default model & checkpoint root path, you can solve it through torch.hub.set_dir("your model root path")