Spaces:
Running
on
Zero
Running
on
Zero
Upload 101 files
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +3 -0
- app.py +86 -0
- checkpoints/depth_anything_vitb14.pth +3 -0
- checkpoints/depth_anything_vitl14.pth +3 -0
- checkpoints/depth_anything_vits14.pth +3 -0
- depth_anything/blocks.py +153 -0
- depth_anything/dpt.py +171 -0
- depth_anything/util/transform.py +248 -0
- depth_anything_vitb14.pth +3 -0
- depth_anything_vitl14.pth +3 -0
- depth_anything_vits14.pth +3 -0
- examples/car.png +0 -0
- examples/flower.png +3 -0
- examples/hall.png +3 -0
- examples/person.png +3 -0
- examples/roller_coaster.png +0 -0
- requirements.txt +2 -0
- torchhub/facebookresearch_dinov2_main/CODE_OF_CONDUCT.md +80 -0
- torchhub/facebookresearch_dinov2_main/CONTRIBUTING.md +31 -0
- torchhub/facebookresearch_dinov2_main/LICENSE +400 -0
- torchhub/facebookresearch_dinov2_main/MODEL_CARD.md +201 -0
- torchhub/facebookresearch_dinov2_main/README.md +277 -0
- torchhub/facebookresearch_dinov2_main/conda.yaml +22 -0
- torchhub/facebookresearch_dinov2_main/dinov2/.DS_Store +0 -0
- torchhub/facebookresearch_dinov2_main/dinov2/__init__.py +7 -0
- torchhub/facebookresearch_dinov2_main/dinov2/configs/.DS_Store +0 -0
- torchhub/facebookresearch_dinov2_main/dinov2/configs/__init__.py +23 -0
- torchhub/facebookresearch_dinov2_main/dinov2/configs/eval/vitb14_pretrain.yaml +6 -0
- torchhub/facebookresearch_dinov2_main/dinov2/configs/eval/vitg14_pretrain.yaml +7 -0
- torchhub/facebookresearch_dinov2_main/dinov2/configs/eval/vitl14_pretrain.yaml +6 -0
- torchhub/facebookresearch_dinov2_main/dinov2/configs/eval/vits14_pretrain.yaml +6 -0
- torchhub/facebookresearch_dinov2_main/dinov2/configs/ssl_default_config.yaml +115 -0
- torchhub/facebookresearch_dinov2_main/dinov2/configs/train/vitg14.yaml +26 -0
- torchhub/facebookresearch_dinov2_main/dinov2/configs/train/vitl14.yaml +26 -0
- torchhub/facebookresearch_dinov2_main/dinov2/configs/train/vitl16_short.yaml +6 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/.DS_Store +0 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/__init__.py +11 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/adapters.py +29 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/augmentations.py +119 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/collate.py +50 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/datasets/__init__.py +8 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/datasets/decoders.py +32 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/datasets/extended.py +39 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/datasets/image_net.py +291 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/datasets/image_net_22k.py +303 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/loaders.py +223 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/masking.py +87 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/samplers.py +230 -0
- torchhub/facebookresearch_dinov2_main/dinov2/data/transforms.py +92 -0
- torchhub/facebookresearch_dinov2_main/dinov2/distributed/__init__.py +271 -0
.gitattributes
CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
examples/flower.png filter=lfs diff=lfs merge=lfs -text
|
37 |
+
examples/hall.png filter=lfs diff=lfs merge=lfs -text
|
38 |
+
examples/person.png filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from torchvision.transforms import Compose
|
4 |
+
import tempfile
|
5 |
+
from PIL import Image
|
6 |
+
import numpy as np
|
7 |
+
import cv2
|
8 |
+
import torch.nn.functional as F
|
9 |
+
|
10 |
+
from depth_anything.dpt import DPT_DINOv2
|
11 |
+
from depth_anything.util.transform import Resize, NormalizeImage, PrepareForNet
|
12 |
+
|
13 |
+
css = """
|
14 |
+
#img-display-container {
|
15 |
+
max-height: 50vh;
|
16 |
+
}
|
17 |
+
#img-display-input {
|
18 |
+
max-height: 40vh;
|
19 |
+
}
|
20 |
+
#img-display-output {
|
21 |
+
max-height: 40vh;
|
22 |
+
}
|
23 |
+
|
24 |
+
"""
|
25 |
+
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
26 |
+
model = DPT_DINOv2(encoder='vitl', features=256, out_channels=[256, 512, 1024, 1024]).to(DEVICE).eval()
|
27 |
+
model.load_state_dict(torch.load('checkpoints/depth_anything_vitl14.pth'))
|
28 |
+
|
29 |
+
title = "# Depth Anything"
|
30 |
+
description = """Official demo for **Depth Anything: Unleashing the Power of Large-Scale Unlabeled Data**.
|
31 |
+
|
32 |
+
Please refer to our [paper](), [project page](https://depth-anything.github.io), or [github](https://github.com/LiheYoung/Depth-Anything) for more details."""
|
33 |
+
|
34 |
+
transform = Compose([
|
35 |
+
Resize(
|
36 |
+
width=518,
|
37 |
+
height=518,
|
38 |
+
resize_target=False,
|
39 |
+
keep_aspect_ratio=True,
|
40 |
+
ensure_multiple_of=14,
|
41 |
+
resize_method='lower_bound',
|
42 |
+
image_interpolation_method=cv2.INTER_CUBIC,
|
43 |
+
),
|
44 |
+
NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
45 |
+
PrepareForNet(),
|
46 |
+
])
|
47 |
+
|
48 |
+
with gr.Blocks(css=css) as demo:
|
49 |
+
gr.Markdown(title)
|
50 |
+
gr.Markdown(description)
|
51 |
+
gr.Markdown("### Depth Prediction demo")
|
52 |
+
|
53 |
+
with gr.Row():
|
54 |
+
input_image = gr.Image(label="Input Image", type='numpy', elem_id='img-display-input').style(height="auto")
|
55 |
+
depth_image = gr.Image(label="Depth Map", elem_id='img-display-output')
|
56 |
+
raw_file = gr.File(label="16-bit raw depth (can be considered as disparity)")
|
57 |
+
submit = gr.Button("Submit")
|
58 |
+
|
59 |
+
def on_submit(image):
|
60 |
+
h, w = image.shape[:2]
|
61 |
+
|
62 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) / 255.0
|
63 |
+
image = transform({'image': image})['image']
|
64 |
+
image = torch.from_numpy(image).unsqueeze(0).to(DEVICE)
|
65 |
+
|
66 |
+
with torch.no_grad():
|
67 |
+
depth = model(image)
|
68 |
+
depth = F.interpolate(depth[None], (h, w), mode='bilinear', align_corners=False)[0, 0]
|
69 |
+
|
70 |
+
raw_depth = Image.fromarray(depth.cpu().numpy().astype('uint16'))
|
71 |
+
tmp = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
72 |
+
raw_depth.save(tmp.name)
|
73 |
+
|
74 |
+
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
75 |
+
depth = depth.cpu().numpy().astype(np.uint8)
|
76 |
+
colored_depth = cv2.applyColorMap(depth, cv2.COLORMAP_INFERNO)[:, :, ::-1]
|
77 |
+
|
78 |
+
return [colored_depth, tmp.name]
|
79 |
+
|
80 |
+
submit.click(on_submit, inputs=[input_image], outputs=[depth_image, raw_file])
|
81 |
+
examples = gr.Examples(examples=["examples/flower.png", "examples/roller_coaster.png", "examples/hall.png", "examples/car.png", "examples/person.png"],
|
82 |
+
inputs=[input_image])
|
83 |
+
|
84 |
+
|
85 |
+
if __name__ == '__main__':
|
86 |
+
demo.queue().launch()
|
checkpoints/depth_anything_vitb14.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:64ae214ae4e27424b644c49464c0aa243016f6f753d95097c8eb9ad0b9cb2d9b
|
3 |
+
size 389962664
|
checkpoints/depth_anything_vitl14.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6c6a383e33e51c5fdfbf31e7ebcda943973a9e6a1cbef1564afe58d7f2e8fe63
|
3 |
+
size 1341401882
|
checkpoints/depth_anything_vits14.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:59afe57cfd9f4284deaf5b753d954723d7136ae842fba3e068ba03537ca1e60e
|
3 |
+
size 99219880
|
depth_anything/blocks.py
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn as nn
|
2 |
+
|
3 |
+
|
4 |
+
def _make_scratch(in_shape, out_shape, groups=1, expand=False):
|
5 |
+
scratch = nn.Module()
|
6 |
+
|
7 |
+
out_shape1 = out_shape
|
8 |
+
out_shape2 = out_shape
|
9 |
+
out_shape3 = out_shape
|
10 |
+
if len(in_shape) >= 4:
|
11 |
+
out_shape4 = out_shape
|
12 |
+
|
13 |
+
if expand:
|
14 |
+
out_shape1 = out_shape
|
15 |
+
out_shape2 = out_shape*2
|
16 |
+
out_shape3 = out_shape*4
|
17 |
+
if len(in_shape) >= 4:
|
18 |
+
out_shape4 = out_shape*8
|
19 |
+
|
20 |
+
scratch.layer1_rn = nn.Conv2d(
|
21 |
+
in_shape[0], out_shape1, kernel_size=3, stride=1, padding=1, bias=False, groups=groups
|
22 |
+
)
|
23 |
+
scratch.layer2_rn = nn.Conv2d(
|
24 |
+
in_shape[1], out_shape2, kernel_size=3, stride=1, padding=1, bias=False, groups=groups
|
25 |
+
)
|
26 |
+
scratch.layer3_rn = nn.Conv2d(
|
27 |
+
in_shape[2], out_shape3, kernel_size=3, stride=1, padding=1, bias=False, groups=groups
|
28 |
+
)
|
29 |
+
if len(in_shape) >= 4:
|
30 |
+
scratch.layer4_rn = nn.Conv2d(
|
31 |
+
in_shape[3], out_shape4, kernel_size=3, stride=1, padding=1, bias=False, groups=groups
|
32 |
+
)
|
33 |
+
|
34 |
+
return scratch
|
35 |
+
|
36 |
+
|
37 |
+
class ResidualConvUnit(nn.Module):
|
38 |
+
"""Residual convolution module.
|
39 |
+
"""
|
40 |
+
|
41 |
+
def __init__(self, features, activation, bn):
|
42 |
+
"""Init.
|
43 |
+
|
44 |
+
Args:
|
45 |
+
features (int): number of features
|
46 |
+
"""
|
47 |
+
super().__init__()
|
48 |
+
|
49 |
+
self.bn = bn
|
50 |
+
|
51 |
+
self.groups=1
|
52 |
+
|
53 |
+
self.conv1 = nn.Conv2d(
|
54 |
+
features, features, kernel_size=3, stride=1, padding=1, bias=True, groups=self.groups
|
55 |
+
)
|
56 |
+
|
57 |
+
self.conv2 = nn.Conv2d(
|
58 |
+
features, features, kernel_size=3, stride=1, padding=1, bias=True, groups=self.groups
|
59 |
+
)
|
60 |
+
|
61 |
+
if self.bn==True:
|
62 |
+
self.bn1 = nn.BatchNorm2d(features)
|
63 |
+
self.bn2 = nn.BatchNorm2d(features)
|
64 |
+
|
65 |
+
self.activation = activation
|
66 |
+
|
67 |
+
self.skip_add = nn.quantized.FloatFunctional()
|
68 |
+
|
69 |
+
def forward(self, x):
|
70 |
+
"""Forward pass.
|
71 |
+
|
72 |
+
Args:
|
73 |
+
x (tensor): input
|
74 |
+
|
75 |
+
Returns:
|
76 |
+
tensor: output
|
77 |
+
"""
|
78 |
+
|
79 |
+
out = self.activation(x)
|
80 |
+
out = self.conv1(out)
|
81 |
+
if self.bn==True:
|
82 |
+
out = self.bn1(out)
|
83 |
+
|
84 |
+
out = self.activation(out)
|
85 |
+
out = self.conv2(out)
|
86 |
+
if self.bn==True:
|
87 |
+
out = self.bn2(out)
|
88 |
+
|
89 |
+
if self.groups > 1:
|
90 |
+
out = self.conv_merge(out)
|
91 |
+
|
92 |
+
return self.skip_add.add(out, x)
|
93 |
+
|
94 |
+
|
95 |
+
class FeatureFusionBlock(nn.Module):
|
96 |
+
"""Feature fusion block.
|
97 |
+
"""
|
98 |
+
|
99 |
+
def __init__(self, features, activation, deconv=False, bn=False, expand=False, align_corners=True, size=None):
|
100 |
+
"""Init.
|
101 |
+
|
102 |
+
Args:
|
103 |
+
features (int): number of features
|
104 |
+
"""
|
105 |
+
super(FeatureFusionBlock, self).__init__()
|
106 |
+
|
107 |
+
self.deconv = deconv
|
108 |
+
self.align_corners = align_corners
|
109 |
+
|
110 |
+
self.groups=1
|
111 |
+
|
112 |
+
self.expand = expand
|
113 |
+
out_features = features
|
114 |
+
if self.expand==True:
|
115 |
+
out_features = features//2
|
116 |
+
|
117 |
+
self.out_conv = nn.Conv2d(features, out_features, kernel_size=1, stride=1, padding=0, bias=True, groups=1)
|
118 |
+
|
119 |
+
self.resConfUnit1 = ResidualConvUnit(features, activation, bn)
|
120 |
+
self.resConfUnit2 = ResidualConvUnit(features, activation, bn)
|
121 |
+
|
122 |
+
self.skip_add = nn.quantized.FloatFunctional()
|
123 |
+
|
124 |
+
self.size=size
|
125 |
+
|
126 |
+
def forward(self, *xs, size=None):
|
127 |
+
"""Forward pass.
|
128 |
+
|
129 |
+
Returns:
|
130 |
+
tensor: output
|
131 |
+
"""
|
132 |
+
output = xs[0]
|
133 |
+
|
134 |
+
if len(xs) == 2:
|
135 |
+
res = self.resConfUnit1(xs[1])
|
136 |
+
output = self.skip_add.add(output, res)
|
137 |
+
|
138 |
+
output = self.resConfUnit2(output)
|
139 |
+
|
140 |
+
if (size is None) and (self.size is None):
|
141 |
+
modifier = {"scale_factor": 2}
|
142 |
+
elif size is None:
|
143 |
+
modifier = {"size": self.size}
|
144 |
+
else:
|
145 |
+
modifier = {"size": size}
|
146 |
+
|
147 |
+
output = nn.functional.interpolate(
|
148 |
+
output, **modifier, mode="bilinear", align_corners=self.align_corners
|
149 |
+
)
|
150 |
+
|
151 |
+
output = self.out_conv(output)
|
152 |
+
|
153 |
+
return output
|
depth_anything/dpt.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
|
4 |
+
from .blocks import FeatureFusionBlock, _make_scratch
|
5 |
+
import torch.nn.functional as F
|
6 |
+
|
7 |
+
|
8 |
+
def _make_fusion_block(features, use_bn, size = None):
|
9 |
+
return FeatureFusionBlock(
|
10 |
+
features,
|
11 |
+
nn.ReLU(False),
|
12 |
+
deconv=False,
|
13 |
+
bn=use_bn,
|
14 |
+
expand=False,
|
15 |
+
align_corners=True,
|
16 |
+
size=size,
|
17 |
+
)
|
18 |
+
|
19 |
+
|
20 |
+
class DPTHead(nn.Module):
|
21 |
+
def __init__(self, nclass, in_channels, features=256, use_bn=False, out_channels=[256, 512, 1024, 1024], use_clstoken=False):
|
22 |
+
super(DPTHead, self).__init__()
|
23 |
+
|
24 |
+
self.nclass = nclass
|
25 |
+
self.use_clstoken = use_clstoken
|
26 |
+
|
27 |
+
self.projects = nn.ModuleList([
|
28 |
+
nn.Conv2d(
|
29 |
+
in_channels=in_channels,
|
30 |
+
out_channels=out_channel,
|
31 |
+
kernel_size=1,
|
32 |
+
stride=1,
|
33 |
+
padding=0,
|
34 |
+
) for out_channel in out_channels
|
35 |
+
])
|
36 |
+
|
37 |
+
self.resize_layers = nn.ModuleList([
|
38 |
+
nn.ConvTranspose2d(
|
39 |
+
in_channels=out_channels[0],
|
40 |
+
out_channels=out_channels[0],
|
41 |
+
kernel_size=4,
|
42 |
+
stride=4,
|
43 |
+
padding=0),
|
44 |
+
nn.ConvTranspose2d(
|
45 |
+
in_channels=out_channels[1],
|
46 |
+
out_channels=out_channels[1],
|
47 |
+
kernel_size=2,
|
48 |
+
stride=2,
|
49 |
+
padding=0),
|
50 |
+
nn.Identity(),
|
51 |
+
nn.Conv2d(
|
52 |
+
in_channels=out_channels[3],
|
53 |
+
out_channels=out_channels[3],
|
54 |
+
kernel_size=3,
|
55 |
+
stride=2,
|
56 |
+
padding=1)
|
57 |
+
])
|
58 |
+
|
59 |
+
if use_clstoken:
|
60 |
+
self.readout_projects = nn.ModuleList()
|
61 |
+
for _ in range(len(self.projects)):
|
62 |
+
self.readout_projects.append(
|
63 |
+
nn.Sequential(
|
64 |
+
nn.Linear(2 * in_channels, in_channels),
|
65 |
+
nn.GELU()))
|
66 |
+
|
67 |
+
self.scratch = _make_scratch(
|
68 |
+
out_channels,
|
69 |
+
features,
|
70 |
+
groups=1,
|
71 |
+
expand=False,
|
72 |
+
)
|
73 |
+
|
74 |
+
self.scratch.stem_transpose = None
|
75 |
+
|
76 |
+
self.scratch.refinenet1 = _make_fusion_block(features, use_bn)
|
77 |
+
self.scratch.refinenet2 = _make_fusion_block(features, use_bn)
|
78 |
+
self.scratch.refinenet3 = _make_fusion_block(features, use_bn)
|
79 |
+
self.scratch.refinenet4 = _make_fusion_block(features, use_bn)
|
80 |
+
|
81 |
+
head_features_1 = features
|
82 |
+
head_features_2 = 32
|
83 |
+
|
84 |
+
if nclass > 1:
|
85 |
+
self.scratch.output_conv = nn.Sequential(
|
86 |
+
nn.Conv2d(head_features_1, head_features_1, kernel_size=3, stride=1, padding=1),
|
87 |
+
nn.ReLU(True),
|
88 |
+
nn.Conv2d(head_features_1, nclass, kernel_size=1, stride=1, padding=0),
|
89 |
+
)
|
90 |
+
else:
|
91 |
+
self.scratch.output_conv1 = nn.Conv2d(head_features_1, head_features_1 // 2, kernel_size=3, stride=1, padding=1)
|
92 |
+
|
93 |
+
self.scratch.output_conv2 = nn.Sequential(
|
94 |
+
nn.Conv2d(head_features_1 // 2, head_features_2, kernel_size=3, stride=1, padding=1),
|
95 |
+
nn.ReLU(True),
|
96 |
+
nn.Conv2d(head_features_2, 1, kernel_size=1, stride=1, padding=0),
|
97 |
+
nn.ReLU(True),
|
98 |
+
nn.Identity(),
|
99 |
+
)
|
100 |
+
|
101 |
+
def forward(self, out_features, patch_h, patch_w):
|
102 |
+
out = []
|
103 |
+
for i, x in enumerate(out_features):
|
104 |
+
if self.use_clstoken:
|
105 |
+
x, cls_token = x[0], x[1]
|
106 |
+
readout = cls_token.unsqueeze(1).expand_as(x)
|
107 |
+
x = self.readout_projects[i](torch.cat((x, readout), -1))
|
108 |
+
else:
|
109 |
+
x = x[0]
|
110 |
+
|
111 |
+
x = x.permute(0, 2, 1).reshape((x.shape[0], x.shape[-1], patch_h, patch_w))
|
112 |
+
|
113 |
+
x = self.projects[i](x)
|
114 |
+
x = self.resize_layers[i](x)
|
115 |
+
|
116 |
+
out.append(x)
|
117 |
+
|
118 |
+
layer_1, layer_2, layer_3, layer_4 = out
|
119 |
+
|
120 |
+
layer_1_rn = self.scratch.layer1_rn(layer_1)
|
121 |
+
layer_2_rn = self.scratch.layer2_rn(layer_2)
|
122 |
+
layer_3_rn = self.scratch.layer3_rn(layer_3)
|
123 |
+
layer_4_rn = self.scratch.layer4_rn(layer_4)
|
124 |
+
|
125 |
+
path_4 = self.scratch.refinenet4(layer_4_rn, size=layer_3_rn.shape[2:])
|
126 |
+
path_3 = self.scratch.refinenet3(path_4, layer_3_rn, size=layer_2_rn.shape[2:])
|
127 |
+
path_2 = self.scratch.refinenet2(path_3, layer_2_rn, size=layer_1_rn.shape[2:])
|
128 |
+
path_1 = self.scratch.refinenet1(path_2, layer_1_rn)
|
129 |
+
|
130 |
+
out = self.scratch.output_conv1(path_1)
|
131 |
+
out = F.interpolate(out, (int(patch_h * 14), int(patch_w * 14)), mode="bilinear", align_corners=True)
|
132 |
+
out = self.scratch.output_conv2(out)
|
133 |
+
|
134 |
+
return out
|
135 |
+
|
136 |
+
|
137 |
+
class DPT_DINOv2(nn.Module):
|
138 |
+
def __init__(self, encoder='vitl', features=256, out_channels=[256, 512, 1024, 1024], use_bn=False, use_clstoken=False, localhub=True):
|
139 |
+
super(DPT_DINOv2, self).__init__()
|
140 |
+
|
141 |
+
assert encoder in ['vits', 'vitb', 'vitl']
|
142 |
+
|
143 |
+
# in case the Internet connection is not stable, please load the DINOv2 locally
|
144 |
+
if localhub:
|
145 |
+
self.pretrained = torch.hub.load('torchhub/facebookresearch_dinov2_main', 'dinov2_{:}14'.format(encoder), source='local', pretrained=False)
|
146 |
+
# self.pretrained.load_state_dict(torch.load('checkpoints/dinov2_{:}14_pretrain.pth'.format(encoder)))
|
147 |
+
else:
|
148 |
+
self.pretrained = torch.hub.load('facebookresearch/dinov2', 'dinov2_{:}14'.format(encoder))
|
149 |
+
|
150 |
+
dim = self.pretrained.blocks[0].attn.qkv.in_features
|
151 |
+
|
152 |
+
self.depth_head = DPTHead(1, dim, features, use_bn, out_channels=out_channels, use_clstoken=use_clstoken)
|
153 |
+
|
154 |
+
def forward(self, x):
|
155 |
+
h, w = x.shape[-2:]
|
156 |
+
|
157 |
+
features = self.pretrained.get_intermediate_layers(x, 4, return_class_token=True)
|
158 |
+
|
159 |
+
patch_h, patch_w = h // 14, w // 14
|
160 |
+
|
161 |
+
depth = self.depth_head(features, patch_h, patch_w)
|
162 |
+
depth = F.interpolate(depth, size=(h, w), mode="bilinear", align_corners=True)
|
163 |
+
depth = F.relu(depth)
|
164 |
+
|
165 |
+
return depth.squeeze(1)
|
166 |
+
|
167 |
+
|
168 |
+
if __name__ == '__main__':
|
169 |
+
depth_anything = DPT_DINOv2()
|
170 |
+
depth_anything.load_state_dict(torch.load('checkpoints/depth_anything_dinov2_vitl14.pth'))
|
171 |
+
|
depth_anything/util/transform.py
ADDED
@@ -0,0 +1,248 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
from PIL import Image, ImageOps, ImageFilter
|
3 |
+
import torch
|
4 |
+
from torchvision import transforms
|
5 |
+
import torch.nn.functional as F
|
6 |
+
|
7 |
+
import numpy as np
|
8 |
+
import cv2
|
9 |
+
import math
|
10 |
+
|
11 |
+
|
12 |
+
def apply_min_size(sample, size, image_interpolation_method=cv2.INTER_AREA):
|
13 |
+
"""Rezise the sample to ensure the given size. Keeps aspect ratio.
|
14 |
+
|
15 |
+
Args:
|
16 |
+
sample (dict): sample
|
17 |
+
size (tuple): image size
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
tuple: new size
|
21 |
+
"""
|
22 |
+
shape = list(sample["disparity"].shape)
|
23 |
+
|
24 |
+
if shape[0] >= size[0] and shape[1] >= size[1]:
|
25 |
+
return sample
|
26 |
+
|
27 |
+
scale = [0, 0]
|
28 |
+
scale[0] = size[0] / shape[0]
|
29 |
+
scale[1] = size[1] / shape[1]
|
30 |
+
|
31 |
+
scale = max(scale)
|
32 |
+
|
33 |
+
shape[0] = math.ceil(scale * shape[0])
|
34 |
+
shape[1] = math.ceil(scale * shape[1])
|
35 |
+
|
36 |
+
# resize
|
37 |
+
sample["image"] = cv2.resize(
|
38 |
+
sample["image"], tuple(shape[::-1]), interpolation=image_interpolation_method
|
39 |
+
)
|
40 |
+
|
41 |
+
sample["disparity"] = cv2.resize(
|
42 |
+
sample["disparity"], tuple(shape[::-1]), interpolation=cv2.INTER_NEAREST
|
43 |
+
)
|
44 |
+
sample["mask"] = cv2.resize(
|
45 |
+
sample["mask"].astype(np.float32),
|
46 |
+
tuple(shape[::-1]),
|
47 |
+
interpolation=cv2.INTER_NEAREST,
|
48 |
+
)
|
49 |
+
sample["mask"] = sample["mask"].astype(bool)
|
50 |
+
|
51 |
+
return tuple(shape)
|
52 |
+
|
53 |
+
|
54 |
+
class Resize(object):
|
55 |
+
"""Resize sample to given size (width, height).
|
56 |
+
"""
|
57 |
+
|
58 |
+
def __init__(
|
59 |
+
self,
|
60 |
+
width,
|
61 |
+
height,
|
62 |
+
resize_target=True,
|
63 |
+
keep_aspect_ratio=False,
|
64 |
+
ensure_multiple_of=1,
|
65 |
+
resize_method="lower_bound",
|
66 |
+
image_interpolation_method=cv2.INTER_AREA,
|
67 |
+
):
|
68 |
+
"""Init.
|
69 |
+
|
70 |
+
Args:
|
71 |
+
width (int): desired output width
|
72 |
+
height (int): desired output height
|
73 |
+
resize_target (bool, optional):
|
74 |
+
True: Resize the full sample (image, mask, target).
|
75 |
+
False: Resize image only.
|
76 |
+
Defaults to True.
|
77 |
+
keep_aspect_ratio (bool, optional):
|
78 |
+
True: Keep the aspect ratio of the input sample.
|
79 |
+
Output sample might not have the given width and height, and
|
80 |
+
resize behaviour depends on the parameter 'resize_method'.
|
81 |
+
Defaults to False.
|
82 |
+
ensure_multiple_of (int, optional):
|
83 |
+
Output width and height is constrained to be multiple of this parameter.
|
84 |
+
Defaults to 1.
|
85 |
+
resize_method (str, optional):
|
86 |
+
"lower_bound": Output will be at least as large as the given size.
|
87 |
+
"upper_bound": Output will be at max as large as the given size. (Output size might be smaller than given size.)
|
88 |
+
"minimal": Scale as least as possible. (Output size might be smaller than given size.)
|
89 |
+
Defaults to "lower_bound".
|
90 |
+
"""
|
91 |
+
self.__width = width
|
92 |
+
self.__height = height
|
93 |
+
|
94 |
+
self.__resize_target = resize_target
|
95 |
+
self.__keep_aspect_ratio = keep_aspect_ratio
|
96 |
+
self.__multiple_of = ensure_multiple_of
|
97 |
+
self.__resize_method = resize_method
|
98 |
+
self.__image_interpolation_method = image_interpolation_method
|
99 |
+
|
100 |
+
def constrain_to_multiple_of(self, x, min_val=0, max_val=None):
|
101 |
+
y = (np.round(x / self.__multiple_of) * self.__multiple_of).astype(int)
|
102 |
+
|
103 |
+
if max_val is not None and y > max_val:
|
104 |
+
y = (np.floor(x / self.__multiple_of) * self.__multiple_of).astype(int)
|
105 |
+
|
106 |
+
if y < min_val:
|
107 |
+
y = (np.ceil(x / self.__multiple_of) * self.__multiple_of).astype(int)
|
108 |
+
|
109 |
+
return y
|
110 |
+
|
111 |
+
def get_size(self, width, height):
|
112 |
+
# determine new height and width
|
113 |
+
scale_height = self.__height / height
|
114 |
+
scale_width = self.__width / width
|
115 |
+
|
116 |
+
if self.__keep_aspect_ratio:
|
117 |
+
if self.__resize_method == "lower_bound":
|
118 |
+
# scale such that output size is lower bound
|
119 |
+
if scale_width > scale_height:
|
120 |
+
# fit width
|
121 |
+
scale_height = scale_width
|
122 |
+
else:
|
123 |
+
# fit height
|
124 |
+
scale_width = scale_height
|
125 |
+
elif self.__resize_method == "upper_bound":
|
126 |
+
# scale such that output size is upper bound
|
127 |
+
if scale_width < scale_height:
|
128 |
+
# fit width
|
129 |
+
scale_height = scale_width
|
130 |
+
else:
|
131 |
+
# fit height
|
132 |
+
scale_width = scale_height
|
133 |
+
elif self.__resize_method == "minimal":
|
134 |
+
# scale as least as possbile
|
135 |
+
if abs(1 - scale_width) < abs(1 - scale_height):
|
136 |
+
# fit width
|
137 |
+
scale_height = scale_width
|
138 |
+
else:
|
139 |
+
# fit height
|
140 |
+
scale_width = scale_height
|
141 |
+
else:
|
142 |
+
raise ValueError(
|
143 |
+
f"resize_method {self.__resize_method} not implemented"
|
144 |
+
)
|
145 |
+
|
146 |
+
if self.__resize_method == "lower_bound":
|
147 |
+
new_height = self.constrain_to_multiple_of(
|
148 |
+
scale_height * height, min_val=self.__height
|
149 |
+
)
|
150 |
+
new_width = self.constrain_to_multiple_of(
|
151 |
+
scale_width * width, min_val=self.__width
|
152 |
+
)
|
153 |
+
elif self.__resize_method == "upper_bound":
|
154 |
+
new_height = self.constrain_to_multiple_of(
|
155 |
+
scale_height * height, max_val=self.__height
|
156 |
+
)
|
157 |
+
new_width = self.constrain_to_multiple_of(
|
158 |
+
scale_width * width, max_val=self.__width
|
159 |
+
)
|
160 |
+
elif self.__resize_method == "minimal":
|
161 |
+
new_height = self.constrain_to_multiple_of(scale_height * height)
|
162 |
+
new_width = self.constrain_to_multiple_of(scale_width * width)
|
163 |
+
else:
|
164 |
+
raise ValueError(f"resize_method {self.__resize_method} not implemented")
|
165 |
+
|
166 |
+
return (new_width, new_height)
|
167 |
+
|
168 |
+
def __call__(self, sample):
|
169 |
+
width, height = self.get_size(
|
170 |
+
sample["image"].shape[1], sample["image"].shape[0]
|
171 |
+
)
|
172 |
+
|
173 |
+
# resize sample
|
174 |
+
sample["image"] = cv2.resize(
|
175 |
+
sample["image"],
|
176 |
+
(width, height),
|
177 |
+
interpolation=self.__image_interpolation_method,
|
178 |
+
)
|
179 |
+
|
180 |
+
if self.__resize_target:
|
181 |
+
if "disparity" in sample:
|
182 |
+
sample["disparity"] = cv2.resize(
|
183 |
+
sample["disparity"],
|
184 |
+
(width, height),
|
185 |
+
interpolation=cv2.INTER_NEAREST,
|
186 |
+
)
|
187 |
+
|
188 |
+
if "depth" in sample:
|
189 |
+
sample["depth"] = cv2.resize(
|
190 |
+
sample["depth"], (width, height), interpolation=cv2.INTER_NEAREST
|
191 |
+
)
|
192 |
+
|
193 |
+
if "semseg_mask" in sample:
|
194 |
+
# sample["semseg_mask"] = cv2.resize(
|
195 |
+
# sample["semseg_mask"], (width, height), interpolation=cv2.INTER_NEAREST
|
196 |
+
# )
|
197 |
+
sample["semseg_mask"] = F.interpolate(torch.from_numpy(sample["semseg_mask"]).float()[None, None, ...], (height, width), mode='nearest').numpy()[0, 0]
|
198 |
+
|
199 |
+
if "mask" in sample:
|
200 |
+
sample["mask"] = cv2.resize(
|
201 |
+
sample["mask"].astype(np.float32),
|
202 |
+
(width, height),
|
203 |
+
interpolation=cv2.INTER_NEAREST,
|
204 |
+
)
|
205 |
+
# sample["mask"] = sample["mask"].astype(bool)
|
206 |
+
|
207 |
+
# print(sample['image'].shape, sample['depth'].shape)
|
208 |
+
return sample
|
209 |
+
|
210 |
+
|
211 |
+
class NormalizeImage(object):
|
212 |
+
"""Normlize image by given mean and std.
|
213 |
+
"""
|
214 |
+
|
215 |
+
def __init__(self, mean, std):
|
216 |
+
self.__mean = mean
|
217 |
+
self.__std = std
|
218 |
+
|
219 |
+
def __call__(self, sample):
|
220 |
+
sample["image"] = (sample["image"] - self.__mean) / self.__std
|
221 |
+
|
222 |
+
return sample
|
223 |
+
|
224 |
+
|
225 |
+
class PrepareForNet(object):
|
226 |
+
"""Prepare sample for usage as network input.
|
227 |
+
"""
|
228 |
+
|
229 |
+
def __init__(self):
|
230 |
+
pass
|
231 |
+
|
232 |
+
def __call__(self, sample):
|
233 |
+
image = np.transpose(sample["image"], (2, 0, 1))
|
234 |
+
sample["image"] = np.ascontiguousarray(image).astype(np.float32)
|
235 |
+
|
236 |
+
if "mask" in sample:
|
237 |
+
sample["mask"] = sample["mask"].astype(np.float32)
|
238 |
+
sample["mask"] = np.ascontiguousarray(sample["mask"])
|
239 |
+
|
240 |
+
if "depth" in sample:
|
241 |
+
depth = sample["depth"].astype(np.float32)
|
242 |
+
sample["depth"] = np.ascontiguousarray(depth)
|
243 |
+
|
244 |
+
if "semseg_mask" in sample:
|
245 |
+
sample["semseg_mask"] = sample["semseg_mask"].astype(np.float32)
|
246 |
+
sample["semseg_mask"] = np.ascontiguousarray(sample["semseg_mask"])
|
247 |
+
|
248 |
+
return sample
|
depth_anything_vitb14.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:64ae214ae4e27424b644c49464c0aa243016f6f753d95097c8eb9ad0b9cb2d9b
|
3 |
+
size 389962664
|
depth_anything_vitl14.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6c6a383e33e51c5fdfbf31e7ebcda943973a9e6a1cbef1564afe58d7f2e8fe63
|
3 |
+
size 1341401882
|
depth_anything_vits14.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:59afe57cfd9f4284deaf5b753d954723d7136ae842fba3e068ba03537ca1e60e
|
3 |
+
size 99219880
|
examples/car.png
ADDED
examples/flower.png
ADDED
Git LFS Details
|
examples/hall.png
ADDED
Git LFS Details
|
examples/person.png
ADDED
Git LFS Details
|
examples/roller_coaster.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
torchhub/facebookresearch_dinov2_main/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Code of Conduct
|
2 |
+
|
3 |
+
## Our Pledge
|
4 |
+
|
5 |
+
In the interest of fostering an open and welcoming environment, we as
|
6 |
+
contributors and maintainers pledge to make participation in our project and
|
7 |
+
our community a harassment-free experience for everyone, regardless of age, body
|
8 |
+
size, disability, ethnicity, sex characteristics, gender identity and expression,
|
9 |
+
level of experience, education, socio-economic status, nationality, personal
|
10 |
+
appearance, race, religion, or sexual identity and orientation.
|
11 |
+
|
12 |
+
## Our Standards
|
13 |
+
|
14 |
+
Examples of behavior that contributes to creating a positive environment
|
15 |
+
include:
|
16 |
+
|
17 |
+
* Using welcoming and inclusive language
|
18 |
+
* Being respectful of differing viewpoints and experiences
|
19 |
+
* Gracefully accepting constructive criticism
|
20 |
+
* Focusing on what is best for the community
|
21 |
+
* Showing empathy towards other community members
|
22 |
+
|
23 |
+
Examples of unacceptable behavior by participants include:
|
24 |
+
|
25 |
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26 |
+
advances
|
27 |
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28 |
+
* Public or private harassment
|
29 |
+
* Publishing others' private information, such as a physical or electronic
|
30 |
+
address, without explicit permission
|
31 |
+
* Other conduct which could reasonably be considered inappropriate in a
|
32 |
+
professional setting
|
33 |
+
|
34 |
+
## Our Responsibilities
|
35 |
+
|
36 |
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37 |
+
behavior and are expected to take appropriate and fair corrective action in
|
38 |
+
response to any instances of unacceptable behavior.
|
39 |
+
|
40 |
+
Project maintainers have the right and responsibility to remove, edit, or
|
41 |
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42 |
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43 |
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44 |
+
threatening, offensive, or harmful.
|
45 |
+
|
46 |
+
## Scope
|
47 |
+
|
48 |
+
This Code of Conduct applies within all project spaces, and it also applies when
|
49 |
+
an individual is representing the project or its community in public spaces.
|
50 |
+
Examples of representing a project or community include using an official
|
51 |
+
project e-mail address, posting via an official social media account, or acting
|
52 |
+
as an appointed representative at an online or offline event. Representation of
|
53 |
+
a project may be further defined and clarified by project maintainers.
|
54 |
+
|
55 |
+
This Code of Conduct also applies outside the project spaces when there is a
|
56 |
+
reasonable belief that an individual's behavior may have a negative impact on
|
57 |
+
the project or its community.
|
58 |
+
|
59 |
+
## Enforcement
|
60 |
+
|
61 |
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62 |
+
reported by contacting the project team at <opensource-conduct@meta.com>. All
|
63 |
+
complaints will be reviewed and investigated and will result in a response that
|
64 |
+
is deemed necessary and appropriate to the circumstances. The project team is
|
65 |
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
66 |
+
Further details of specific enforcement policies may be posted separately.
|
67 |
+
|
68 |
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
69 |
+
faith may face temporary or permanent repercussions as determined by other
|
70 |
+
members of the project's leadership.
|
71 |
+
|
72 |
+
## Attribution
|
73 |
+
|
74 |
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
75 |
+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
76 |
+
|
77 |
+
[homepage]: https://www.contributor-covenant.org
|
78 |
+
|
79 |
+
For answers to common questions about this code of conduct, see
|
80 |
+
https://www.contributor-covenant.org/faq
|
torchhub/facebookresearch_dinov2_main/CONTRIBUTING.md
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Contributing to DINOv2
|
2 |
+
We want to make contributing to this project as easy and transparent as
|
3 |
+
possible.
|
4 |
+
|
5 |
+
## Pull Requests
|
6 |
+
We actively welcome your pull requests.
|
7 |
+
|
8 |
+
1. Fork the repo and create your branch from `main`.
|
9 |
+
2. If you've added code that should be tested, add tests.
|
10 |
+
3. If you've changed APIs, update the documentation.
|
11 |
+
4. Ensure the test suite passes.
|
12 |
+
5. Make sure your code lints.
|
13 |
+
6. If you haven't already, complete the Contributor License Agreement ("CLA").
|
14 |
+
|
15 |
+
## Contributor License Agreement ("CLA")
|
16 |
+
In order to accept your pull request, we need you to submit a CLA. You only need
|
17 |
+
to do this once to work on any of Meta's open source projects.
|
18 |
+
|
19 |
+
Complete your CLA here: <https://code.facebook.com/cla>
|
20 |
+
|
21 |
+
## Issues
|
22 |
+
We use GitHub issues to track public bugs. Please ensure your description is
|
23 |
+
clear and has sufficient instructions to be able to reproduce the issue.
|
24 |
+
|
25 |
+
Meta has a [bounty program](https://www.facebook.com/whitehat/) for the safe
|
26 |
+
disclosure of security bugs. In those cases, please go through the process
|
27 |
+
outlined on that page and do not file a public issue.
|
28 |
+
|
29 |
+
## License
|
30 |
+
By contributing to DINOv2, you agree that your contributions will be licensed
|
31 |
+
under the LICENSE file in the root directory of this source tree.
|
torchhub/facebookresearch_dinov2_main/LICENSE
ADDED
@@ -0,0 +1,400 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
Attribution-NonCommercial 4.0 International
|
3 |
+
|
4 |
+
=======================================================================
|
5 |
+
|
6 |
+
Creative Commons Corporation ("Creative Commons") is not a law firm and
|
7 |
+
does not provide legal services or legal advice. Distribution of
|
8 |
+
Creative Commons public licenses does not create a lawyer-client or
|
9 |
+
other relationship. Creative Commons makes its licenses and related
|
10 |
+
information available on an "as-is" basis. Creative Commons gives no
|
11 |
+
warranties regarding its licenses, any material licensed under their
|
12 |
+
terms and conditions, or any related information. Creative Commons
|
13 |
+
disclaims all liability for damages resulting from their use to the
|
14 |
+
fullest extent possible.
|
15 |
+
|
16 |
+
Using Creative Commons Public Licenses
|
17 |
+
|
18 |
+
Creative Commons public licenses provide a standard set of terms and
|
19 |
+
conditions that creators and other rights holders may use to share
|
20 |
+
original works of authorship and other material subject to copyright
|
21 |
+
and certain other rights specified in the public license below. The
|
22 |
+
following considerations are for informational purposes only, are not
|
23 |
+
exhaustive, and do not form part of our licenses.
|
24 |
+
|
25 |
+
Considerations for licensors: Our public licenses are
|
26 |
+
intended for use by those authorized to give the public
|
27 |
+
permission to use material in ways otherwise restricted by
|
28 |
+
copyright and certain other rights. Our licenses are
|
29 |
+
irrevocable. Licensors should read and understand the terms
|
30 |
+
and conditions of the license they choose before applying it.
|
31 |
+
Licensors should also secure all rights necessary before
|
32 |
+
applying our licenses so that the public can reuse the
|
33 |
+
material as expected. Licensors should clearly mark any
|
34 |
+
material not subject to the license. This includes other CC-
|
35 |
+
licensed material, or material used under an exception or
|
36 |
+
limitation to copyright. More considerations for licensors:
|
37 |
+
wiki.creativecommons.org/Considerations_for_licensors
|
38 |
+
|
39 |
+
Considerations for the public: By using one of our public
|
40 |
+
licenses, a licensor grants the public permission to use the
|
41 |
+
licensed material under specified terms and conditions. If
|
42 |
+
the licensor's permission is not necessary for any reason--for
|
43 |
+
example, because of any applicable exception or limitation to
|
44 |
+
copyright--then that use is not regulated by the license. Our
|
45 |
+
licenses grant only permissions under copyright and certain
|
46 |
+
other rights that a licensor has authority to grant. Use of
|
47 |
+
the licensed material may still be restricted for other
|
48 |
+
reasons, including because others have copyright or other
|
49 |
+
rights in the material. A licensor may make special requests,
|
50 |
+
such as asking that all changes be marked or described.
|
51 |
+
Although not required by our licenses, you are encouraged to
|
52 |
+
respect those requests where reasonable. More_considerations
|
53 |
+
for the public:
|
54 |
+
wiki.creativecommons.org/Considerations_for_licensees
|
55 |
+
|
56 |
+
=======================================================================
|
57 |
+
|
58 |
+
Creative Commons Attribution-NonCommercial 4.0 International Public
|
59 |
+
License
|
60 |
+
|
61 |
+
By exercising the Licensed Rights (defined below), You accept and agree
|
62 |
+
to be bound by the terms and conditions of this Creative Commons
|
63 |
+
Attribution-NonCommercial 4.0 International Public License ("Public
|
64 |
+
License"). To the extent this Public License may be interpreted as a
|
65 |
+
contract, You are granted the Licensed Rights in consideration of Your
|
66 |
+
acceptance of these terms and conditions, and the Licensor grants You
|
67 |
+
such rights in consideration of benefits the Licensor receives from
|
68 |
+
making the Licensed Material available under these terms and
|
69 |
+
conditions.
|
70 |
+
|
71 |
+
Section 1 -- Definitions.
|
72 |
+
|
73 |
+
a. Adapted Material means material subject to Copyright and Similar
|
74 |
+
Rights that is derived from or based upon the Licensed Material
|
75 |
+
and in which the Licensed Material is translated, altered,
|
76 |
+
arranged, transformed, or otherwise modified in a manner requiring
|
77 |
+
permission under the Copyright and Similar Rights held by the
|
78 |
+
Licensor. For purposes of this Public License, where the Licensed
|
79 |
+
Material is a musical work, performance, or sound recording,
|
80 |
+
Adapted Material is always produced where the Licensed Material is
|
81 |
+
synched in timed relation with a moving image.
|
82 |
+
|
83 |
+
b. Adapter's License means the license You apply to Your Copyright
|
84 |
+
and Similar Rights in Your contributions to Adapted Material in
|
85 |
+
accordance with the terms and conditions of this Public License.
|
86 |
+
|
87 |
+
c. Copyright and Similar Rights means copyright and/or similar rights
|
88 |
+
closely related to copyright including, without limitation,
|
89 |
+
performance, broadcast, sound recording, and Sui Generis Database
|
90 |
+
Rights, without regard to how the rights are labeled or
|
91 |
+
categorized. For purposes of this Public License, the rights
|
92 |
+
specified in Section 2(b)(1)-(2) are not Copyright and Similar
|
93 |
+
Rights.
|
94 |
+
d. Effective Technological Measures means those measures that, in the
|
95 |
+
absence of proper authority, may not be circumvented under laws
|
96 |
+
fulfilling obligations under Article 11 of the WIPO Copyright
|
97 |
+
Treaty adopted on December 20, 1996, and/or similar international
|
98 |
+
agreements.
|
99 |
+
|
100 |
+
e. Exceptions and Limitations means fair use, fair dealing, and/or
|
101 |
+
any other exception or limitation to Copyright and Similar Rights
|
102 |
+
that applies to Your use of the Licensed Material.
|
103 |
+
|
104 |
+
f. Licensed Material means the artistic or literary work, database,
|
105 |
+
or other material to which the Licensor applied this Public
|
106 |
+
License.
|
107 |
+
|
108 |
+
g. Licensed Rights means the rights granted to You subject to the
|
109 |
+
terms and conditions of this Public License, which are limited to
|
110 |
+
all Copyright and Similar Rights that apply to Your use of the
|
111 |
+
Licensed Material and that the Licensor has authority to license.
|
112 |
+
|
113 |
+
h. Licensor means the individual(s) or entity(ies) granting rights
|
114 |
+
under this Public License.
|
115 |
+
|
116 |
+
i. NonCommercial means not primarily intended for or directed towards
|
117 |
+
commercial advantage or monetary compensation. For purposes of
|
118 |
+
this Public License, the exchange of the Licensed Material for
|
119 |
+
other material subject to Copyright and Similar Rights by digital
|
120 |
+
file-sharing or similar means is NonCommercial provided there is
|
121 |
+
no payment of monetary compensation in connection with the
|
122 |
+
exchange.
|
123 |
+
|
124 |
+
j. Share means to provide material to the public by any means or
|
125 |
+
process that requires permission under the Licensed Rights, such
|
126 |
+
as reproduction, public display, public performance, distribution,
|
127 |
+
dissemination, communication, or importation, and to make material
|
128 |
+
available to the public including in ways that members of the
|
129 |
+
public may access the material from a place and at a time
|
130 |
+
individually chosen by them.
|
131 |
+
|
132 |
+
k. Sui Generis Database Rights means rights other than copyright
|
133 |
+
resulting from Directive 96/9/EC of the European Parliament and of
|
134 |
+
the Council of 11 March 1996 on the legal protection of databases,
|
135 |
+
as amended and/or succeeded, as well as other essentially
|
136 |
+
equivalent rights anywhere in the world.
|
137 |
+
|
138 |
+
l. You means the individual or entity exercising the Licensed Rights
|
139 |
+
under this Public License. Your has a corresponding meaning.
|
140 |
+
|
141 |
+
Section 2 -- Scope.
|
142 |
+
|
143 |
+
a. License grant.
|
144 |
+
|
145 |
+
1. Subject to the terms and conditions of this Public License,
|
146 |
+
the Licensor hereby grants You a worldwide, royalty-free,
|
147 |
+
non-sublicensable, non-exclusive, irrevocable license to
|
148 |
+
exercise the Licensed Rights in the Licensed Material to:
|
149 |
+
|
150 |
+
a. reproduce and Share the Licensed Material, in whole or
|
151 |
+
in part, for NonCommercial purposes only; and
|
152 |
+
|
153 |
+
b. produce, reproduce, and Share Adapted Material for
|
154 |
+
NonCommercial purposes only.
|
155 |
+
|
156 |
+
2. Exceptions and Limitations. For the avoidance of doubt, where
|
157 |
+
Exceptions and Limitations apply to Your use, this Public
|
158 |
+
License does not apply, and You do not need to comply with
|
159 |
+
its terms and conditions.
|
160 |
+
|
161 |
+
3. Term. The term of this Public License is specified in Section
|
162 |
+
6(a).
|
163 |
+
|
164 |
+
4. Media and formats; technical modifications allowed. The
|
165 |
+
Licensor authorizes You to exercise the Licensed Rights in
|
166 |
+
all media and formats whether now known or hereafter created,
|
167 |
+
and to make technical modifications necessary to do so. The
|
168 |
+
Licensor waives and/or agrees not to assert any right or
|
169 |
+
authority to forbid You from making technical modifications
|
170 |
+
necessary to exercise the Licensed Rights, including
|
171 |
+
technical modifications necessary to circumvent Effective
|
172 |
+
Technological Measures. For purposes of this Public License,
|
173 |
+
simply making modifications authorized by this Section 2(a)
|
174 |
+
(4) never produces Adapted Material.
|
175 |
+
|
176 |
+
5. Downstream recipients.
|
177 |
+
|
178 |
+
a. Offer from the Licensor -- Licensed Material. Every
|
179 |
+
recipient of the Licensed Material automatically
|
180 |
+
receives an offer from the Licensor to exercise the
|
181 |
+
Licensed Rights under the terms and conditions of this
|
182 |
+
Public License.
|
183 |
+
|
184 |
+
b. No downstream restrictions. You may not offer or impose
|
185 |
+
any additional or different terms or conditions on, or
|
186 |
+
apply any Effective Technological Measures to, the
|
187 |
+
Licensed Material if doing so restricts exercise of the
|
188 |
+
Licensed Rights by any recipient of the Licensed
|
189 |
+
Material.
|
190 |
+
|
191 |
+
6. No endorsement. Nothing in this Public License constitutes or
|
192 |
+
may be construed as permission to assert or imply that You
|
193 |
+
are, or that Your use of the Licensed Material is, connected
|
194 |
+
with, or sponsored, endorsed, or granted official status by,
|
195 |
+
the Licensor or others designated to receive attribution as
|
196 |
+
provided in Section 3(a)(1)(A)(i).
|
197 |
+
|
198 |
+
b. Other rights.
|
199 |
+
|
200 |
+
1. Moral rights, such as the right of integrity, are not
|
201 |
+
licensed under this Public License, nor are publicity,
|
202 |
+
privacy, and/or other similar personality rights; however, to
|
203 |
+
the extent possible, the Licensor waives and/or agrees not to
|
204 |
+
assert any such rights held by the Licensor to the limited
|
205 |
+
extent necessary to allow You to exercise the Licensed
|
206 |
+
Rights, but not otherwise.
|
207 |
+
|
208 |
+
2. Patent and trademark rights are not licensed under this
|
209 |
+
Public License.
|
210 |
+
|
211 |
+
3. To the extent possible, the Licensor waives any right to
|
212 |
+
collect royalties from You for the exercise of the Licensed
|
213 |
+
Rights, whether directly or through a collecting society
|
214 |
+
under any voluntary or waivable statutory or compulsory
|
215 |
+
licensing scheme. In all other cases the Licensor expressly
|
216 |
+
reserves any right to collect such royalties, including when
|
217 |
+
the Licensed Material is used other than for NonCommercial
|
218 |
+
purposes.
|
219 |
+
|
220 |
+
Section 3 -- License Conditions.
|
221 |
+
|
222 |
+
Your exercise of the Licensed Rights is expressly made subject to the
|
223 |
+
following conditions.
|
224 |
+
|
225 |
+
a. Attribution.
|
226 |
+
|
227 |
+
1. If You Share the Licensed Material (including in modified
|
228 |
+
form), You must:
|
229 |
+
|
230 |
+
a. retain the following if it is supplied by the Licensor
|
231 |
+
with the Licensed Material:
|
232 |
+
|
233 |
+
i. identification of the creator(s) of the Licensed
|
234 |
+
Material and any others designated to receive
|
235 |
+
attribution, in any reasonable manner requested by
|
236 |
+
the Licensor (including by pseudonym if
|
237 |
+
designated);
|
238 |
+
|
239 |
+
ii. a copyright notice;
|
240 |
+
|
241 |
+
iii. a notice that refers to this Public License;
|
242 |
+
|
243 |
+
iv. a notice that refers to the disclaimer of
|
244 |
+
warranties;
|
245 |
+
|
246 |
+
v. a URI or hyperlink to the Licensed Material to the
|
247 |
+
extent reasonably practicable;
|
248 |
+
|
249 |
+
b. indicate if You modified the Licensed Material and
|
250 |
+
retain an indication of any previous modifications; and
|
251 |
+
|
252 |
+
c. indicate the Licensed Material is licensed under this
|
253 |
+
Public License, and include the text of, or the URI or
|
254 |
+
hyperlink to, this Public License.
|
255 |
+
|
256 |
+
2. You may satisfy the conditions in Section 3(a)(1) in any
|
257 |
+
reasonable manner based on the medium, means, and context in
|
258 |
+
which You Share the Licensed Material. For example, it may be
|
259 |
+
reasonable to satisfy the conditions by providing a URI or
|
260 |
+
hyperlink to a resource that includes the required
|
261 |
+
information.
|
262 |
+
|
263 |
+
3. If requested by the Licensor, You must remove any of the
|
264 |
+
information required by Section 3(a)(1)(A) to the extent
|
265 |
+
reasonably practicable.
|
266 |
+
|
267 |
+
4. If You Share Adapted Material You produce, the Adapter's
|
268 |
+
License You apply must not prevent recipients of the Adapted
|
269 |
+
Material from complying with this Public License.
|
270 |
+
|
271 |
+
Section 4 -- Sui Generis Database Rights.
|
272 |
+
|
273 |
+
Where the Licensed Rights include Sui Generis Database Rights that
|
274 |
+
apply to Your use of the Licensed Material:
|
275 |
+
|
276 |
+
a. for the avoidance of doubt, Section 2(a)(1) grants You the right
|
277 |
+
to extract, reuse, reproduce, and Share all or a substantial
|
278 |
+
portion of the contents of the database for NonCommercial purposes
|
279 |
+
only;
|
280 |
+
|
281 |
+
b. if You include all or a substantial portion of the database
|
282 |
+
contents in a database in which You have Sui Generis Database
|
283 |
+
Rights, then the database in which You have Sui Generis Database
|
284 |
+
Rights (but not its individual contents) is Adapted Material; and
|
285 |
+
|
286 |
+
c. You must comply with the conditions in Section 3(a) if You Share
|
287 |
+
all or a substantial portion of the contents of the database.
|
288 |
+
|
289 |
+
For the avoidance of doubt, this Section 4 supplements and does not
|
290 |
+
replace Your obligations under this Public License where the Licensed
|
291 |
+
Rights include other Copyright and Similar Rights.
|
292 |
+
|
293 |
+
Section 5 -- Disclaimer of Warranties and Limitation of Liability.
|
294 |
+
|
295 |
+
a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
|
296 |
+
EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
|
297 |
+
AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
|
298 |
+
ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
|
299 |
+
IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
|
300 |
+
WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
301 |
+
PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
|
302 |
+
ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
|
303 |
+
KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
|
304 |
+
ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
|
305 |
+
|
306 |
+
b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
|
307 |
+
TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
|
308 |
+
NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
|
309 |
+
INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
|
310 |
+
COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
|
311 |
+
USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
|
312 |
+
ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
|
313 |
+
DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
|
314 |
+
IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
|
315 |
+
|
316 |
+
c. The disclaimer of warranties and limitation of liability provided
|
317 |
+
above shall be interpreted in a manner that, to the extent
|
318 |
+
possible, most closely approximates an absolute disclaimer and
|
319 |
+
waiver of all liability.
|
320 |
+
|
321 |
+
Section 6 -- Term and Termination.
|
322 |
+
|
323 |
+
a. This Public License applies for the term of the Copyright and
|
324 |
+
Similar Rights licensed here. However, if You fail to comply with
|
325 |
+
this Public License, then Your rights under this Public License
|
326 |
+
terminate automatically.
|
327 |
+
|
328 |
+
b. Where Your right to use the Licensed Material has terminated under
|
329 |
+
Section 6(a), it reinstates:
|
330 |
+
|
331 |
+
1. automatically as of the date the violation is cured, provided
|
332 |
+
it is cured within 30 days of Your discovery of the
|
333 |
+
violation; or
|
334 |
+
|
335 |
+
2. upon express reinstatement by the Licensor.
|
336 |
+
|
337 |
+
For the avoidance of doubt, this Section 6(b) does not affect any
|
338 |
+
right the Licensor may have to seek remedies for Your violations
|
339 |
+
of this Public License.
|
340 |
+
|
341 |
+
c. For the avoidance of doubt, the Licensor may also offer the
|
342 |
+
Licensed Material under separate terms or conditions or stop
|
343 |
+
distributing the Licensed Material at any time; however, doing so
|
344 |
+
will not terminate this Public License.
|
345 |
+
|
346 |
+
d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
|
347 |
+
License.
|
348 |
+
|
349 |
+
Section 7 -- Other Terms and Conditions.
|
350 |
+
|
351 |
+
a. The Licensor shall not be bound by any additional or different
|
352 |
+
terms or conditions communicated by You unless expressly agreed.
|
353 |
+
|
354 |
+
b. Any arrangements, understandings, or agreements regarding the
|
355 |
+
Licensed Material not stated herein are separate from and
|
356 |
+
independent of the terms and conditions of this Public License.
|
357 |
+
|
358 |
+
Section 8 -- Interpretation.
|
359 |
+
|
360 |
+
a. For the avoidance of doubt, this Public License does not, and
|
361 |
+
shall not be interpreted to, reduce, limit, restrict, or impose
|
362 |
+
conditions on any use of the Licensed Material that could lawfully
|
363 |
+
be made without permission under this Public License.
|
364 |
+
|
365 |
+
b. To the extent possible, if any provision of this Public License is
|
366 |
+
deemed unenforceable, it shall be automatically reformed to the
|
367 |
+
minimum extent necessary to make it enforceable. If the provision
|
368 |
+
cannot be reformed, it shall be severed from this Public License
|
369 |
+
without affecting the enforceability of the remaining terms and
|
370 |
+
conditions.
|
371 |
+
|
372 |
+
c. No term or condition of this Public License will be waived and no
|
373 |
+
failure to comply consented to unless expressly agreed to by the
|
374 |
+
Licensor.
|
375 |
+
|
376 |
+
d. Nothing in this Public License constitutes or may be interpreted
|
377 |
+
as a limitation upon, or waiver of, any privileges and immunities
|
378 |
+
that apply to the Licensor or You, including from the legal
|
379 |
+
processes of any jurisdiction or authority.
|
380 |
+
|
381 |
+
=======================================================================
|
382 |
+
|
383 |
+
Creative Commons is not a party to its public
|
384 |
+
licenses. Notwithstanding, Creative Commons may elect to apply one of
|
385 |
+
its public licenses to material it publishes and in those instances
|
386 |
+
will be considered the “Licensor.” The text of the Creative Commons
|
387 |
+
public licenses is dedicated to the public domain under the CC0 Public
|
388 |
+
Domain Dedication. Except for the limited purpose of indicating that
|
389 |
+
material is shared under a Creative Commons public license or as
|
390 |
+
otherwise permitted by the Creative Commons policies published at
|
391 |
+
creativecommons.org/policies, Creative Commons does not authorize the
|
392 |
+
use of the trademark "Creative Commons" or any other trademark or logo
|
393 |
+
of Creative Commons without its prior written consent including,
|
394 |
+
without limitation, in connection with any unauthorized modifications
|
395 |
+
to any of its public licenses or any other arrangements,
|
396 |
+
understandings, or agreements concerning use of licensed material. For
|
397 |
+
the avoidance of doubt, this paragraph does not form part of the
|
398 |
+
public licenses.
|
399 |
+
|
400 |
+
Creative Commons may be contacted at creativecommons.org.
|
torchhub/facebookresearch_dinov2_main/MODEL_CARD.md
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Model Card for DINOv2-S/B/L/g
|
2 |
+
|
3 |
+
These are Vision Transformer models trained following the method described in the paper:
|
4 |
+
"DINOv2: Learning Robust Visual Features without Supervision"
|
5 |
+
|
6 |
+
We provide 4 models: 1 ViT-g trained from scratch, and 3 ViT-S/B/L models distilled from the ViT-g.
|
7 |
+
|
8 |
+
## Model Details
|
9 |
+
The model takes an image as input and returns a class token and patch tokens.
|
10 |
+
|
11 |
+
The embedding dimension is:
|
12 |
+
- 384 for ViT-S.
|
13 |
+
- 768 for ViT-B.
|
14 |
+
- 1024 for ViT-L.
|
15 |
+
- 1536 for ViT-g.
|
16 |
+
|
17 |
+
The models follow a Transformer architecture, with a patch size of 14.
|
18 |
+
|
19 |
+
For a 224x224 image, this results in 1 class token + 256 patch tokens.
|
20 |
+
|
21 |
+
The models can accept larger images provided the image shapes are multiples of the patch size (14).
|
22 |
+
If this condition is not verified, the model will crop to the closest smaller multiple of the patch size.
|
23 |
+
|
24 |
+
### Model Description
|
25 |
+
|
26 |
+
- **Developed by:** Meta AI
|
27 |
+
- **Model type:** Vision Transformer
|
28 |
+
- **License:** CC-BY-NC
|
29 |
+
|
30 |
+
- **Repository:** https://github.com/facebookresearch/dinov2
|
31 |
+
- **Paper:** https://arxiv.org/abs/2304.07193
|
32 |
+
- **Demo:** https://dinov2.metademolab.com/
|
33 |
+
|
34 |
+
## Uses
|
35 |
+
|
36 |
+
The models are vision backbones providing multi-purpose features for downstream tasks.
|
37 |
+
|
38 |
+
### Direct Use
|
39 |
+
|
40 |
+
The models can be used without fine-tuning, with downstream classifiers as simple as linear layers, to obtain competitive results:
|
41 |
+
- on depth estimation, semantic segmentation, using linear layers.
|
42 |
+
- on image classification, using k-NN classifiers on the class token.
|
43 |
+
- on image classification, with logistic regression classifiers applied on the class token.
|
44 |
+
- on image classification, with a linear layer applied on the class token and the average of the patch tokens.
|
45 |
+
- on image retrieval using nearest neighbors.
|
46 |
+
|
47 |
+
### Downstream Use
|
48 |
+
|
49 |
+
It is technically possible to perform fine-tuning on the models, for small gains (we measured +2% on ImageNet-1k classification).
|
50 |
+
We recommend keeping this as a very last step and only when necessary, as the features already provide good performance out-of-the-box.
|
51 |
+
|
52 |
+
## Bias, Risks, and Limitations
|
53 |
+
|
54 |
+
Despite improvements thanks to the training method not using annotations, we still observe significant biases in our models toward rich households from Western countries.
|
55 |
+
|
56 |
+
### Recommendations
|
57 |
+
|
58 |
+
We expect fine-tuning will increase the biases in the features produced by the model as they will be tuned to the fine-tuning labels.
|
59 |
+
|
60 |
+
## How to Get Started with the Model
|
61 |
+
|
62 |
+
Use the code below to get started with the model.
|
63 |
+
|
64 |
+
```python
|
65 |
+
import torch
|
66 |
+
dinov2_vits14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vits14')
|
67 |
+
dinov2_vitb14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitb14')
|
68 |
+
dinov2_vitl14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitl14')
|
69 |
+
dinov2_vitg14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitg14')
|
70 |
+
```
|
71 |
+
|
72 |
+
## Training Details
|
73 |
+
|
74 |
+
### Training Data
|
75 |
+
|
76 |
+
- **Training data:** LVD-142M (see paper)
|
77 |
+
- **Training regime:** fp16 using PyTorch-FSDP mixed-precision.
|
78 |
+
|
79 |
+
### Training Procedure
|
80 |
+
|
81 |
+
- **Training objective:**
|
82 |
+
- DINO self-distillation loss with multi-crop
|
83 |
+
- iBOT masked-image modeling loss
|
84 |
+
- KoLeo regularization on [CLS] tokens
|
85 |
+
- **Architectures:**
|
86 |
+
- ViT-S (21M params): Patch size 14, embedding dimension 384, 6 heads, MLP FFN
|
87 |
+
- ViT-B (86M params): Patch size 14, embedding dimension 768, 12 heads, MLP FFN
|
88 |
+
- ViT-L (0.3B params): Patch size 14, embedding dimension 1024, 16 heads, MLP FFN
|
89 |
+
- ViT-g (1.1B params): Patch size 14, embedding dimension 1536, 24 heads, SwiGLU FFN
|
90 |
+
- **Distillation:**
|
91 |
+
- Distillation follows the standard DINOv2 pretraining procedure, except the teacher is a pretrained ViT-g, frozen.
|
92 |
+
|
93 |
+
## Evaluation
|
94 |
+
|
95 |
+
We refer users to the associated paper for the evaluation protocols.
|
96 |
+
|
97 |
+
<table>
|
98 |
+
<tr>
|
99 |
+
<th>model</th>
|
100 |
+
<th colspan="3">ImageNet-1k</th>
|
101 |
+
<th>NYU-Depth v2</th>
|
102 |
+
<th>SUN-RGBD</th>
|
103 |
+
<th>ADE20k</th>
|
104 |
+
<th>iNaturalist 2018</th>
|
105 |
+
<th>Oxford-H</th>
|
106 |
+
</tr>
|
107 |
+
<tr>
|
108 |
+
<th rowspan="2">task</th>
|
109 |
+
<th>classif. (acc)</th>
|
110 |
+
<th>classif. (acc)</th>
|
111 |
+
<th>classif. V2 (acc)</th>
|
112 |
+
<th>depth (RMSE)</th>
|
113 |
+
<th>depth (RMSE)</th>
|
114 |
+
<th>segm. (mAP)</th>
|
115 |
+
<th>classif. (acc)</th>
|
116 |
+
<th>retrieval (mAP)</th>
|
117 |
+
</tr>
|
118 |
+
<tr>
|
119 |
+
<!-- <th>^</th> -->
|
120 |
+
<th>k-NN</th>
|
121 |
+
<th>linear</th>
|
122 |
+
<th>linear</th>
|
123 |
+
<th>linear<br />4 layers</th>
|
124 |
+
<th>NYU-D transfer</th>
|
125 |
+
<th>multiscale</th>
|
126 |
+
<th>linear</th>
|
127 |
+
<th>nearest neighbor</th>
|
128 |
+
</tr>
|
129 |
+
<tr>
|
130 |
+
<td>ViT-S/14</td>
|
131 |
+
<td align="right">79.0%</td>
|
132 |
+
<td align="right">81.1%</td>
|
133 |
+
<td align="right">70.8%</td>
|
134 |
+
<td align="right">0.417</td>
|
135 |
+
<td align="right">0.431</td>
|
136 |
+
<td align="right">47.2</td>
|
137 |
+
<td align="right">69.5%</td>
|
138 |
+
<td align="right">43.2</td>
|
139 |
+
</tr>
|
140 |
+
<tr>
|
141 |
+
<td>ViT-B/14</td>
|
142 |
+
<td align="right">82.1%</td>
|
143 |
+
<td align="right">84.5%</td>
|
144 |
+
<td align="right">74.9%</td>
|
145 |
+
<td align="right">0.362</td>
|
146 |
+
<td align="right">0.400</td>
|
147 |
+
<td align="right">51.3</td>
|
148 |
+
<td align="right">76.3%</td>
|
149 |
+
<td align="right">49.5</td>
|
150 |
+
</tr>
|
151 |
+
<tr>
|
152 |
+
<td>ViT-L/14</td>
|
153 |
+
<td align="right">83.5%</td>
|
154 |
+
<td align="right">86.3%</td>
|
155 |
+
<td align="right">77.6%</td>
|
156 |
+
<td align="right">0.333</td>
|
157 |
+
<td align="right">0.396</td>
|
158 |
+
<td align="right">53.1</td>
|
159 |
+
<td align="right">79.8%</td>
|
160 |
+
<td align="right">54.0</td>
|
161 |
+
</tr>
|
162 |
+
<tr>
|
163 |
+
<td>ViT-g/14</td>
|
164 |
+
<td align="right">83.5%</td>
|
165 |
+
<td align="right">86.5%</td>
|
166 |
+
<td align="right">78.4%</td>
|
167 |
+
<td align="right">0.298</td>
|
168 |
+
<td align="right">0.362</td>
|
169 |
+
<td align="right">53.0</td>
|
170 |
+
<td align="right">81.6%</td>
|
171 |
+
<td align="right">52.3</td>
|
172 |
+
</tr>
|
173 |
+
</table>
|
174 |
+
|
175 |
+
## Environmental Impact
|
176 |
+
|
177 |
+
- **Hardware Type:** Nvidia A100
|
178 |
+
- **Hours used:** 22,000 for ViT-g, 4,500 for ViT-S distillation, 5,300 for ViT-B distillation, 8,000 for ViT-L distillation
|
179 |
+
- **Cloud Provider:** Private infra
|
180 |
+
- **Compute Region:** USA
|
181 |
+
- **Carbon Emitted:** 7t CO2eq
|
182 |
+
|
183 |
+
#### Hardware
|
184 |
+
|
185 |
+
Nvidia A100 GPUs
|
186 |
+
|
187 |
+
#### Software
|
188 |
+
|
189 |
+
PyTorch 2.0,
|
190 |
+
xFormers 0.0.18
|
191 |
+
|
192 |
+
**BibTeX**
|
193 |
+
|
194 |
+
```
|
195 |
+
@misc{oquab2023dinov2,
|
196 |
+
title={DINOv2: Learning Robust Visual Features without Supervision},
|
197 |
+
author={Oquab, Maxime and Darcet, Timothée and Moutakanni, Theo and Vo, Huy and Szafraniec, Marc and Khalidov, Vasil and Fernandez, Pierre and Haziza, Daniel and Massa, Francisco and El-Nouby, Alaaeldin and Howes, Russell and Huang, Po-Yao and Xu, Hu and Sharma, Vasu and Li, Shang-Wen and Galuba, Wojciech and Rabbat, Mike and Assran, Mido and Ballas, Nicolas and Synnaeve, Gabriel and Misra, Ishan and Jegou, Herve and Mairal, Julien and Labatut, Patrick and Joulin, Armand and Bojanowski, Piotr},
|
198 |
+
journal={arXiv:2304.07193},
|
199 |
+
year={2023}
|
200 |
+
}
|
201 |
+
```
|
torchhub/facebookresearch_dinov2_main/README.md
ADDED
@@ -0,0 +1,277 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# DINOv2: Learning Robust Visual Features without Supervision
|
2 |
+
|
3 |
+
**[Meta AI Research, FAIR](https://ai.facebook.com/research/)**
|
4 |
+
|
5 |
+
Maxime Oquab,
|
6 |
+
Timothée Darcet,
|
7 |
+
Théo Moutakanni,
|
8 |
+
Huy V. Vo,
|
9 |
+
Marc Szafraniec,
|
10 |
+
Vasil Khalidov,
|
11 |
+
Patrick Labatut,
|
12 |
+
Armand Joulin,
|
13 |
+
Piotr Bojanowski
|
14 |
+
|
15 |
+
[[`Paper`](https://arxiv.org/abs/2304.07193)] [[`Blog`](https://ai.facebook.com/blog/dino-v2-computer-vision-self-supervised-learning/)] [[`Demo`](https://dinov2.metademolab.com)] [[`BibTeX`](#citing-dinov2)]
|
16 |
+
|
17 |
+
PyTorch implementation and pretrained models for DINOv2. For details, see the paper: **[DINOv2: Learning Robust Visual Features without Supervision](https://arxiv.org/abs/2304.07193)**.
|
18 |
+
|
19 |
+
DINOv2 models produce high-performance visual features that can be directly employed with classifiers as simple as linear layers on a variety of computer vision tasks; these visual features are robust and perform well across domains without any requirement for fine-tuning. The models were pretrained on a dataset of 142 M images without using any labels or annotations.
|
20 |
+
|
21 |
+
https://github.com/facebookresearch/dinov2/assets/60359573/f168823e-7922-415a-b429-578badf5c356
|
22 |
+
|
23 |
+
<div align="center">
|
24 |
+
Visualization of the three first principal components of the patch features of all frames, mapped to RGB values.
|
25 |
+
</div>
|
26 |
+
|
27 |
+
## Pretrained models
|
28 |
+
|
29 |
+
<table style="margin: auto">
|
30 |
+
<tr>
|
31 |
+
<th>model</th>
|
32 |
+
<th># of<br />params</th>
|
33 |
+
<th>ImageNet<br />k-NN</th>
|
34 |
+
<th>ImageNet<br />linear</th>
|
35 |
+
<th>download</th>
|
36 |
+
</tr>
|
37 |
+
<tr>
|
38 |
+
<td>ViT-S/14 distilled</td>
|
39 |
+
<td align="right">21 M</td>
|
40 |
+
<td align="right">79.0%</td>
|
41 |
+
<td align="right">81.1%</td>
|
42 |
+
<td><a href="https://dl.fbaipublicfiles.com/dinov2/dinov2_vits14/dinov2_vits14_pretrain.pth">backbone only</a></td>
|
43 |
+
</tr>
|
44 |
+
<tr>
|
45 |
+
<td>ViT-B/14 distilled</td>
|
46 |
+
<td align="right">86 M</td>
|
47 |
+
<td align="right">82.1%</td>
|
48 |
+
<td align="right">84.5%</td>
|
49 |
+
<td><a href="https://dl.fbaipublicfiles.com/dinov2/dinov2_vitb14/dinov2_vitb14_pretrain.pth">backbone only</a></td>
|
50 |
+
</tr>
|
51 |
+
<tr>
|
52 |
+
<td>ViT-L/14 distilled</td>
|
53 |
+
<td align="right">300 M</td>
|
54 |
+
<td align="right">83.5%</td>
|
55 |
+
<td align="right">86.3%</td>
|
56 |
+
<td><a href="https://dl.fbaipublicfiles.com/dinov2/dinov2_vitl14/dinov2_vitl14_pretrain.pth">backbone only</a></td>
|
57 |
+
</tr>
|
58 |
+
<tr>
|
59 |
+
<td>ViT-g/14</td>
|
60 |
+
<td align="right">1,100 M</td>
|
61 |
+
<td align="right">83.5%</td>
|
62 |
+
<td align="right">86.5%</td>
|
63 |
+
<td><a href="https://dl.fbaipublicfiles.com/dinov2/dinov2_vitg14/dinov2_vitg14_pretrain.pth">backbone only</a></td>
|
64 |
+
</tr>
|
65 |
+
</table>
|
66 |
+
|
67 |
+
### Pretrained models via PyTorch Hub
|
68 |
+
|
69 |
+
Please follow the instructions [here](https://pytorch.org/get-started/locally/) to install PyTorch (the only required dependency for loading the model). Installing PyTorch with CUDA support is strongly recommended.
|
70 |
+
|
71 |
+
A corresponding [model card](MODEL_CARD.md) is included in the repository.
|
72 |
+
|
73 |
+
```python
|
74 |
+
import torch
|
75 |
+
|
76 |
+
dinov2_vits14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vits14')
|
77 |
+
dinov2_vitb14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitb14')
|
78 |
+
dinov2_vitl14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitl14')
|
79 |
+
dinov2_vitg14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitg14')
|
80 |
+
```
|
81 |
+
|
82 |
+
## Installation
|
83 |
+
|
84 |
+
The training and evaluation code requires PyTorch 2.0 and [xFormers](https://github.com/facebookresearch/xformers) 0.0.18 as well as a number of other 3rd party packages. Note that the code has only been tested with the specified versions and also expects a Linux environment. To setup all the required dependencies for training and evaluation, please follow the instructions below:
|
85 |
+
|
86 |
+
*[conda](https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html)* **(Recommended)** - Clone the repository and then create and activate a `dinov2` conda environment using the provided environment definition:
|
87 |
+
|
88 |
+
```shell
|
89 |
+
conda env create -f conda.yaml
|
90 |
+
conda activate dinov2
|
91 |
+
```
|
92 |
+
|
93 |
+
*[pip](https://pip.pypa.io/en/stable/getting-started/)* - Clone the repository and then use the provided `requirements.txt` to install the dependencies:
|
94 |
+
|
95 |
+
```shell
|
96 |
+
pip install -r requirements.txt
|
97 |
+
```
|
98 |
+
|
99 |
+
## Data preparation
|
100 |
+
|
101 |
+
### ImageNet-1k
|
102 |
+
|
103 |
+
The root directory of the dataset should hold the following contents:
|
104 |
+
|
105 |
+
- `<ROOT>/test/ILSVRC2012_test_00000001.JPEG`
|
106 |
+
- `<ROOT>/test/[..]`
|
107 |
+
- `<ROOT>/test/ILSVRC2012_test_00100000.JPEG`
|
108 |
+
- `<ROOT>/train/n01440764/n01440764_10026.JPEG`
|
109 |
+
- `<ROOT>/train/[...]`
|
110 |
+
- `<ROOT>/train/n15075141/n15075141_9993.JPEG`
|
111 |
+
- `<ROOT>/val/n01440764/ILSVRC2012_val_00000293.JPEG`
|
112 |
+
- `<ROOT>/val/[...]`
|
113 |
+
- `<ROOT>/val/n15075141/ILSVRC2012_val_00049174.JPEG`
|
114 |
+
- `<ROOT>/labels.txt`
|
115 |
+
|
116 |
+
The provided dataset implementation expects a few additional metadata files to be present under the extra directory:
|
117 |
+
|
118 |
+
- `<EXTRA>/class-ids-TRAIN.npy`
|
119 |
+
- `<EXTRA>/class-ids-VAL.npy`
|
120 |
+
- `<EXTRA>/class-names-TRAIN.npy`
|
121 |
+
- `<EXTRA>/class-names-VAL.npy`
|
122 |
+
- `<EXTRA>/entries-TEST.npy`
|
123 |
+
- `<EXTRA>/entries-TRAIN.npy`
|
124 |
+
- `<EXTRA>/entries-VAL.npy`
|
125 |
+
|
126 |
+
These metadata files can be generated (once) with the following lines of Python code:
|
127 |
+
|
128 |
+
```python
|
129 |
+
from dinov2.data.datasets import ImageNet
|
130 |
+
|
131 |
+
for split in ImageNet.Split:
|
132 |
+
dataset = ImageNet(split=split, root="<ROOT>", extra="<EXTRA>")
|
133 |
+
dataset.dump_extra()
|
134 |
+
```
|
135 |
+
|
136 |
+
Note that the root and extra directories do not have to be distinct directories.
|
137 |
+
|
138 |
+
### ImageNet-22k
|
139 |
+
|
140 |
+
Please adapt the [dataset class](dinov2/data/datasets/image_net_22k.py) to match your local setup.
|
141 |
+
|
142 |
+
<br />
|
143 |
+
|
144 |
+
:warning: To execute the commands provided in the next sections for training and evaluation, the `dinov2` package should be included in the Python module search path, i.e. simply prefix the command to run with `PYTHONPATH=.`.
|
145 |
+
|
146 |
+
## Training
|
147 |
+
|
148 |
+
### Fast setup: training DINOv2 ViT-L/16 on ImageNet-1k
|
149 |
+
|
150 |
+
Run DINOv2 training on 4 A100-80GB nodes (32 GPUs) in a SLURM cluster environment with submitit:
|
151 |
+
|
152 |
+
```shell
|
153 |
+
python dinov2/run/train/train.py \
|
154 |
+
--nodes 4 \
|
155 |
+
--config-file dinov2/configs/train/vitl16_short.yaml \
|
156 |
+
--output-dir <PATH/TO/OUTPUT/DIR> \
|
157 |
+
train.dataset_path=ImageNet:split=TRAIN:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>
|
158 |
+
```
|
159 |
+
|
160 |
+
Training time is approximately 1 day and the resulting checkpoint should reach 81.6% on k-NN eval and 82.9% on linear eval.
|
161 |
+
|
162 |
+
The training code saves the weights of the teacher in the `eval` folder every 12500 iterations for evaluation.
|
163 |
+
|
164 |
+
### Long setup: training DINOv2 ViT-L/14 on ImageNet-22k
|
165 |
+
|
166 |
+
Run DINOv2 training on 12 A100-80GB nodes (96 GPUs) in a SLURM cluster environment with submitit:
|
167 |
+
|
168 |
+
```shell
|
169 |
+
python dinov2/run/train/train.py \
|
170 |
+
--nodes 12 \
|
171 |
+
--config-file dinov2/configs/train/vitl14.yaml \
|
172 |
+
--output-dir <PATH/TO/OUTPUT/DIR> \
|
173 |
+
train.dataset_path=ImageNet22k:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>
|
174 |
+
```
|
175 |
+
|
176 |
+
Training time is approximately 3.3 days and the resulting checkpoint should reach 82.0% on k-NN eval and 84.5% on linear eval.
|
177 |
+
|
178 |
+
The training code saves the weights of the teacher in the `eval` folder every 12500 iterations for evaluation.
|
179 |
+
|
180 |
+
|
181 |
+
## Evaluation
|
182 |
+
|
183 |
+
The training code regularly saves the teacher weights. In order to evaluate the model, run the following evaluation on a single node:
|
184 |
+
|
185 |
+
### k-NN classification on ImageNet-1k
|
186 |
+
|
187 |
+
```shell
|
188 |
+
python dinov2/run/eval/knn.py \
|
189 |
+
--config-file <PATH/TO/OUTPUT/DIR>/config.yaml \
|
190 |
+
--pretrained-weights <PATH/TO/OUTPUT/DIR>/eval/training_24999/teacher_checkpoint.pth \
|
191 |
+
--output-dir <PATH/TO/OUTPUT/DIR>/eval/training_24999/knn \
|
192 |
+
--train-dataset ImageNet:split=TRAIN:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET> \
|
193 |
+
--val-dataset ImageNet:split=VAL:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>
|
194 |
+
```
|
195 |
+
|
196 |
+
### Logistic regression classification on ImageNet-1k
|
197 |
+
|
198 |
+
```shell
|
199 |
+
python dinov2/run/eval/log_regression.py \
|
200 |
+
--config-file <PATH/TO/OUTPUT/DIR>/config.yaml \
|
201 |
+
--pretrained-weights <PATH/TO/OUTPUT/DIR>/eval/training_24999/teacher_checkpoint.pth \
|
202 |
+
--output-dir <PATH/TO/OUTPUT/DIR>/eval/training_24999/logreg \
|
203 |
+
--train-dataset ImageNet:split=TRAIN:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET> \
|
204 |
+
--val-dataset ImageNet:split=VAL:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>
|
205 |
+
```
|
206 |
+
|
207 |
+
### Linear classification with data augmentation on ImageNet-1k
|
208 |
+
|
209 |
+
```shell
|
210 |
+
python dinov2/run/eval/linear.py \
|
211 |
+
--config-file <PATH/TO/OUTPUT/DIR>/config.yaml \
|
212 |
+
--pretrained-weights <PATH/TO/OUTPUT/DIR>/eval/training_24999/teacher_checkpoint.pth \
|
213 |
+
--output-dir <PATH/TO/OUTPUT/DIR>/eval/training_24999/linear \
|
214 |
+
--train-dataset ImageNet:split=TRAIN:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET> \
|
215 |
+
--val-dataset ImageNet:split=VAL:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>
|
216 |
+
```
|
217 |
+
|
218 |
+
We release the weights from evaluating the different models:
|
219 |
+
|
220 |
+
<table style="margin: auto">
|
221 |
+
<tr>
|
222 |
+
<th>model</th>
|
223 |
+
<th>ImageNet<br />top-1</th>
|
224 |
+
<th>linear evaluation</th>
|
225 |
+
</tr>
|
226 |
+
<tr>
|
227 |
+
<td>ViT-S/14 distilled</td>
|
228 |
+
<td align="right">81.1%</td>
|
229 |
+
<td><a href="https://dl.fbaipublicfiles.com/dinov2/dinov2_vits14/dinov2_vits14_linear_head.pth">linear head weights</a></td>
|
230 |
+
</tr>
|
231 |
+
<tr>
|
232 |
+
<td>ViT-B/14 distilled</td>
|
233 |
+
<td align="right">84.5%</td>
|
234 |
+
<td><a href="https://dl.fbaipublicfiles.com/dinov2/dinov2_vitb14/dinov2_vitb14_linear_head.pth">linear head weights</a></td>
|
235 |
+
</tr>
|
236 |
+
<tr>
|
237 |
+
<td>ViT-L/14 distilled</td>
|
238 |
+
<td align="right">86.3%</td>
|
239 |
+
<td><a href="https://dl.fbaipublicfiles.com/dinov2/dinov2_vitl14/dinov2_vitl14_linear_head.pth">linear head weights</a></td>
|
240 |
+
</tr>
|
241 |
+
<tr>
|
242 |
+
<td>ViT-g/14</td>
|
243 |
+
<td align="right">86.5%</td>
|
244 |
+
<td><a href="https://dl.fbaipublicfiles.com/dinov2/dinov2_vitg14/dinov2_vitg14_linear_head.pth">linear head weights</a></td>
|
245 |
+
</tr>
|
246 |
+
</table>
|
247 |
+
|
248 |
+
The performance of the provided pretrained model weights can be evaluated as follows on ImageNet-1k:
|
249 |
+
|
250 |
+
```shell
|
251 |
+
python dinov2/run/eval/linear.py \
|
252 |
+
--config-file dinov2/configs/eval/vitg14_pretrain.yaml \
|
253 |
+
--pretrained-weights https://dl.fbaipublicfiles.com/dinov2/dinov2_vitg14/dinov2_vitg14_pretrain.pth \
|
254 |
+
--train-dataset ImageNet:split=TRAIN:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET> \
|
255 |
+
--val-dataset ImageNet:split=VAL:root=<PATH/TO/DATASET>:extra=<PATH/TO/DATASET>
|
256 |
+
```
|
257 |
+
|
258 |
+
## License
|
259 |
+
|
260 |
+
DINOv2 code and model weights are released under the CC-BY-NC 4.0 license. See [LICENSE](LICENSE) for additional details.
|
261 |
+
|
262 |
+
## Contributing
|
263 |
+
|
264 |
+
See [contributing](CONTRIBUTING.md) and the [code of conduct](CODE_OF_CONDUCT.md).
|
265 |
+
|
266 |
+
## Citing DINOv2
|
267 |
+
|
268 |
+
If you find this repository useful, please consider giving a star :star: and citation :t-rex::
|
269 |
+
|
270 |
+
```
|
271 |
+
@misc{oquab2023dinov2,
|
272 |
+
title={DINOv2: Learning Robust Visual Features without Supervision},
|
273 |
+
author={Oquab, Maxime and Darcet, Timothée and Moutakanni, Theo and Vo, Huy V. and Szafraniec, Marc and Khalidov, Vasil and Fernandez, Pierre and Haziza, Daniel and Massa, Francisco and El-Nouby, Alaaeldin and Howes, Russell and Huang, Po-Yao and Xu, Hu and Sharma, Vasu and Li, Shang-Wen and Galuba, Wojciech and Rabbat, Mike and Assran, Mido and Ballas, Nicolas and Synnaeve, Gabriel and Misra, Ishan and Jegou, Herve and Mairal, Julien and Labatut, Patrick and Joulin, Armand and Bojanowski, Piotr},
|
274 |
+
journal={arXiv:2304.07193},
|
275 |
+
year={2023}
|
276 |
+
}
|
277 |
+
```
|
torchhub/facebookresearch_dinov2_main/conda.yaml
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: dinov2
|
2 |
+
channels:
|
3 |
+
- defaults
|
4 |
+
- pytorch
|
5 |
+
- nvidia
|
6 |
+
- xformers
|
7 |
+
- conda-forge
|
8 |
+
dependencies:
|
9 |
+
- python=3.9
|
10 |
+
- pytorch::pytorch=2.0.0
|
11 |
+
- pytorch::pytorch-cuda=11.7.0
|
12 |
+
- pytorch::torchvision=0.15.0
|
13 |
+
- omegaconf
|
14 |
+
- torchmetrics=0.10.3
|
15 |
+
- fvcore
|
16 |
+
- iopath
|
17 |
+
- xformers::xformers=0.0.18
|
18 |
+
- pip
|
19 |
+
- pip:
|
20 |
+
- git+https://github.com/facebookincubator/submitit
|
21 |
+
- --extra-index-url https://pypi.nvidia.com
|
22 |
+
- cuml-cu11
|
torchhub/facebookresearch_dinov2_main/dinov2/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
torchhub/facebookresearch_dinov2_main/dinov2/__init__.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
__version__ = "0.0.1"
|
torchhub/facebookresearch_dinov2_main/dinov2/configs/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
torchhub/facebookresearch_dinov2_main/dinov2/configs/__init__.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
import pathlib
|
8 |
+
|
9 |
+
from omegaconf import OmegaConf
|
10 |
+
|
11 |
+
|
12 |
+
def load_config(config_name: str):
|
13 |
+
config_filename = config_name + ".yaml"
|
14 |
+
return OmegaConf.load(pathlib.Path(__file__).parent.resolve() / config_filename)
|
15 |
+
|
16 |
+
|
17 |
+
dinov2_default_config = load_config("ssl_default_config")
|
18 |
+
|
19 |
+
|
20 |
+
def load_and_merge_config(config_name: str):
|
21 |
+
default_config = OmegaConf.create(dinov2_default_config)
|
22 |
+
loaded_config = load_config(config_name)
|
23 |
+
return OmegaConf.merge(default_config, loaded_config)
|
torchhub/facebookresearch_dinov2_main/dinov2/configs/eval/vitb14_pretrain.yaml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
student:
|
2 |
+
arch: vit_base
|
3 |
+
patch_size: 14
|
4 |
+
crops:
|
5 |
+
global_crops_size: 518 # this is to set up the position embeddings properly
|
6 |
+
local_crops_size: 98
|
torchhub/facebookresearch_dinov2_main/dinov2/configs/eval/vitg14_pretrain.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
student:
|
2 |
+
arch: vit_giant2
|
3 |
+
patch_size: 14
|
4 |
+
ffn_layer: swiglufused
|
5 |
+
crops:
|
6 |
+
global_crops_size: 518 # this is to set up the position embeddings properly
|
7 |
+
local_crops_size: 98
|
torchhub/facebookresearch_dinov2_main/dinov2/configs/eval/vitl14_pretrain.yaml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
student:
|
2 |
+
arch: vit_large
|
3 |
+
patch_size: 14
|
4 |
+
crops:
|
5 |
+
global_crops_size: 518 # this is to set up the position embeddings properly
|
6 |
+
local_crops_size: 98
|
torchhub/facebookresearch_dinov2_main/dinov2/configs/eval/vits14_pretrain.yaml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
student:
|
2 |
+
arch: vit_small
|
3 |
+
patch_size: 14
|
4 |
+
crops:
|
5 |
+
global_crops_size: 518 # this is to set up the position embeddings properly
|
6 |
+
local_crops_size: 98
|
torchhub/facebookresearch_dinov2_main/dinov2/configs/ssl_default_config.yaml
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MODEL:
|
2 |
+
WEIGHTS: ''
|
3 |
+
compute_precision:
|
4 |
+
grad_scaler: true
|
5 |
+
teacher:
|
6 |
+
backbone:
|
7 |
+
sharding_strategy: SHARD_GRAD_OP
|
8 |
+
mixed_precision:
|
9 |
+
param_dtype: fp16
|
10 |
+
reduce_dtype: fp16
|
11 |
+
buffer_dtype: fp32
|
12 |
+
dino_head:
|
13 |
+
sharding_strategy: SHARD_GRAD_OP
|
14 |
+
mixed_precision:
|
15 |
+
param_dtype: fp16
|
16 |
+
reduce_dtype: fp16
|
17 |
+
buffer_dtype: fp32
|
18 |
+
ibot_head:
|
19 |
+
sharding_strategy: SHARD_GRAD_OP
|
20 |
+
mixed_precision:
|
21 |
+
param_dtype: fp16
|
22 |
+
reduce_dtype: fp16
|
23 |
+
buffer_dtype: fp32
|
24 |
+
student:
|
25 |
+
backbone:
|
26 |
+
sharding_strategy: SHARD_GRAD_OP
|
27 |
+
mixed_precision:
|
28 |
+
param_dtype: fp16
|
29 |
+
reduce_dtype: fp16
|
30 |
+
buffer_dtype: fp32
|
31 |
+
dino_head:
|
32 |
+
sharding_strategy: SHARD_GRAD_OP
|
33 |
+
mixed_precision:
|
34 |
+
param_dtype: fp16
|
35 |
+
reduce_dtype: fp32
|
36 |
+
buffer_dtype: fp32
|
37 |
+
ibot_head:
|
38 |
+
sharding_strategy: SHARD_GRAD_OP
|
39 |
+
mixed_precision:
|
40 |
+
param_dtype: fp16
|
41 |
+
reduce_dtype: fp32
|
42 |
+
buffer_dtype: fp32
|
43 |
+
dino:
|
44 |
+
loss_weight: 1.0
|
45 |
+
head_n_prototypes: 65536
|
46 |
+
head_bottleneck_dim: 256
|
47 |
+
head_nlayers: 3
|
48 |
+
head_hidden_dim: 2048
|
49 |
+
koleo_loss_weight: 0.1
|
50 |
+
ibot:
|
51 |
+
loss_weight: 1.0
|
52 |
+
mask_sample_probability: 0.5
|
53 |
+
mask_ratio_min_max:
|
54 |
+
- 0.1
|
55 |
+
- 0.5
|
56 |
+
separate_head: false
|
57 |
+
head_n_prototypes: 65536
|
58 |
+
head_bottleneck_dim: 256
|
59 |
+
head_nlayers: 3
|
60 |
+
head_hidden_dim: 2048
|
61 |
+
train:
|
62 |
+
batch_size_per_gpu: 64
|
63 |
+
dataset_path: ImageNet:split=TRAIN
|
64 |
+
output_dir: .
|
65 |
+
saveckp_freq: 20
|
66 |
+
seed: 0
|
67 |
+
num_workers: 10
|
68 |
+
OFFICIAL_EPOCH_LENGTH: 1250
|
69 |
+
cache_dataset: true
|
70 |
+
centering: "centering" # or "sinkhorn_knopp"
|
71 |
+
student:
|
72 |
+
arch: vit_large
|
73 |
+
patch_size: 16
|
74 |
+
drop_path_rate: 0.3
|
75 |
+
layerscale: 1.0e-05
|
76 |
+
drop_path_uniform: true
|
77 |
+
pretrained_weights: ''
|
78 |
+
ffn_layer: "mlp"
|
79 |
+
block_chunks: 0
|
80 |
+
qkv_bias: true
|
81 |
+
proj_bias: true
|
82 |
+
ffn_bias: true
|
83 |
+
teacher:
|
84 |
+
momentum_teacher: 0.992
|
85 |
+
final_momentum_teacher: 1
|
86 |
+
warmup_teacher_temp: 0.04
|
87 |
+
teacher_temp: 0.07
|
88 |
+
warmup_teacher_temp_epochs: 30
|
89 |
+
optim:
|
90 |
+
epochs: 100
|
91 |
+
weight_decay: 0.04
|
92 |
+
weight_decay_end: 0.4
|
93 |
+
base_lr: 0.004 # learning rate for a batch size of 1024
|
94 |
+
lr: 0. # will be set after applying scaling rule
|
95 |
+
warmup_epochs: 10
|
96 |
+
min_lr: 1.0e-06
|
97 |
+
clip_grad: 3.0
|
98 |
+
freeze_last_layer_epochs: 1
|
99 |
+
scaling_rule: sqrt_wrt_1024
|
100 |
+
patch_embed_lr_mult: 0.2
|
101 |
+
layerwise_decay: 0.9
|
102 |
+
adamw_beta1: 0.9
|
103 |
+
adamw_beta2: 0.999
|
104 |
+
crops:
|
105 |
+
global_crops_scale:
|
106 |
+
- 0.32
|
107 |
+
- 1.0
|
108 |
+
local_crops_number: 8
|
109 |
+
local_crops_scale:
|
110 |
+
- 0.05
|
111 |
+
- 0.32
|
112 |
+
global_crops_size: 224
|
113 |
+
local_crops_size: 96
|
114 |
+
evaluation:
|
115 |
+
eval_period_iterations: 12500
|
torchhub/facebookresearch_dinov2_main/dinov2/configs/train/vitg14.yaml
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
dino:
|
2 |
+
head_n_prototypes: 131072
|
3 |
+
head_bottleneck_dim: 384
|
4 |
+
ibot:
|
5 |
+
separate_head: true
|
6 |
+
head_n_prototypes: 131072
|
7 |
+
train:
|
8 |
+
batch_size_per_gpu: 12
|
9 |
+
dataset_path: ImageNet22k
|
10 |
+
centering: sinkhorn_knopp
|
11 |
+
student:
|
12 |
+
arch: vit_giant2
|
13 |
+
patch_size: 14
|
14 |
+
drop_path_rate: 0.4
|
15 |
+
ffn_layer: swiglufused
|
16 |
+
block_chunks: 4
|
17 |
+
teacher:
|
18 |
+
momentum_teacher: 0.994
|
19 |
+
optim:
|
20 |
+
epochs: 500
|
21 |
+
weight_decay_end: 0.2
|
22 |
+
base_lr: 2.0e-04 # learning rate for a batch size of 1024
|
23 |
+
warmup_epochs: 80
|
24 |
+
layerwise_decay: 1.0
|
25 |
+
crops:
|
26 |
+
local_crops_size: 98
|
torchhub/facebookresearch_dinov2_main/dinov2/configs/train/vitl14.yaml
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
dino:
|
2 |
+
head_n_prototypes: 131072
|
3 |
+
head_bottleneck_dim: 384
|
4 |
+
ibot:
|
5 |
+
separate_head: true
|
6 |
+
head_n_prototypes: 131072
|
7 |
+
train:
|
8 |
+
batch_size_per_gpu: 32
|
9 |
+
dataset_path: ImageNet22k
|
10 |
+
centering: sinkhorn_knopp
|
11 |
+
student:
|
12 |
+
arch: vit_large
|
13 |
+
patch_size: 14
|
14 |
+
drop_path_rate: 0.4
|
15 |
+
ffn_layer: swiglufused
|
16 |
+
block_chunks: 4
|
17 |
+
teacher:
|
18 |
+
momentum_teacher: 0.994
|
19 |
+
optim:
|
20 |
+
epochs: 500
|
21 |
+
weight_decay_end: 0.2
|
22 |
+
base_lr: 2.0e-04 # learning rate for a batch size of 1024
|
23 |
+
warmup_epochs: 80
|
24 |
+
layerwise_decay: 1.0
|
25 |
+
crops:
|
26 |
+
local_crops_size: 98
|
torchhub/facebookresearch_dinov2_main/dinov2/configs/train/vitl16_short.yaml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# this corresponds to the default config
|
2 |
+
train:
|
3 |
+
dataset_path: ImageNet:split=TRAIN
|
4 |
+
batch_size_per_gpu: 64
|
5 |
+
student:
|
6 |
+
block_chunks: 4
|
torchhub/facebookresearch_dinov2_main/dinov2/data/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
torchhub/facebookresearch_dinov2_main/dinov2/data/__init__.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
from .adapters import DatasetWithEnumeratedTargets
|
8 |
+
from .loaders import make_data_loader, make_dataset, SamplerType
|
9 |
+
from .collate import collate_data_and_cast
|
10 |
+
from .masking import MaskingGenerator
|
11 |
+
from .augmentations import DataAugmentationDINO
|
torchhub/facebookresearch_dinov2_main/dinov2/data/adapters.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
from typing import Any, Tuple
|
8 |
+
|
9 |
+
from torch.utils.data import Dataset
|
10 |
+
|
11 |
+
|
12 |
+
class DatasetWithEnumeratedTargets(Dataset):
|
13 |
+
def __init__(self, dataset):
|
14 |
+
self._dataset = dataset
|
15 |
+
|
16 |
+
def get_image_data(self, index: int) -> bytes:
|
17 |
+
return self._dataset.get_image_data(index)
|
18 |
+
|
19 |
+
def get_target(self, index: int) -> Tuple[Any, int]:
|
20 |
+
target = self._dataset.get_target(index)
|
21 |
+
return (index, target)
|
22 |
+
|
23 |
+
def __getitem__(self, index: int) -> Tuple[Any, Tuple[Any, int]]:
|
24 |
+
image, target = self._dataset[index]
|
25 |
+
target = index if target is None else target
|
26 |
+
return image, (index, target)
|
27 |
+
|
28 |
+
def __len__(self) -> int:
|
29 |
+
return len(self._dataset)
|
torchhub/facebookresearch_dinov2_main/dinov2/data/augmentations.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
import logging
|
8 |
+
|
9 |
+
from torchvision import transforms
|
10 |
+
|
11 |
+
from .transforms import (
|
12 |
+
GaussianBlur,
|
13 |
+
make_normalize_transform,
|
14 |
+
)
|
15 |
+
|
16 |
+
|
17 |
+
logger = logging.getLogger("dinov2")
|
18 |
+
|
19 |
+
|
20 |
+
class DataAugmentationDINO(object):
|
21 |
+
def __init__(
|
22 |
+
self,
|
23 |
+
global_crops_scale,
|
24 |
+
local_crops_scale,
|
25 |
+
local_crops_number,
|
26 |
+
global_crops_size=224,
|
27 |
+
local_crops_size=96,
|
28 |
+
):
|
29 |
+
self.global_crops_scale = global_crops_scale
|
30 |
+
self.local_crops_scale = local_crops_scale
|
31 |
+
self.local_crops_number = local_crops_number
|
32 |
+
self.global_crops_size = global_crops_size
|
33 |
+
self.local_crops_size = local_crops_size
|
34 |
+
|
35 |
+
logger.info("###################################")
|
36 |
+
logger.info("Using data augmentation parameters:")
|
37 |
+
logger.info(f"global_crops_scale: {global_crops_scale}")
|
38 |
+
logger.info(f"local_crops_scale: {local_crops_scale}")
|
39 |
+
logger.info(f"local_crops_number: {local_crops_number}")
|
40 |
+
logger.info(f"global_crops_size: {global_crops_size}")
|
41 |
+
logger.info(f"local_crops_size: {local_crops_size}")
|
42 |
+
logger.info("###################################")
|
43 |
+
|
44 |
+
# random resized crop and flip
|
45 |
+
self.geometric_augmentation_global = transforms.Compose(
|
46 |
+
[
|
47 |
+
transforms.RandomResizedCrop(
|
48 |
+
global_crops_size, scale=global_crops_scale, interpolation=transforms.InterpolationMode.BICUBIC
|
49 |
+
),
|
50 |
+
transforms.RandomHorizontalFlip(p=0.5),
|
51 |
+
]
|
52 |
+
)
|
53 |
+
|
54 |
+
self.geometric_augmentation_local = transforms.Compose(
|
55 |
+
[
|
56 |
+
transforms.RandomResizedCrop(
|
57 |
+
local_crops_size, scale=local_crops_scale, interpolation=transforms.InterpolationMode.BICUBIC
|
58 |
+
),
|
59 |
+
transforms.RandomHorizontalFlip(p=0.5),
|
60 |
+
]
|
61 |
+
)
|
62 |
+
|
63 |
+
# color distorsions / blurring
|
64 |
+
color_jittering = transforms.Compose(
|
65 |
+
[
|
66 |
+
transforms.RandomApply(
|
67 |
+
[transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.2, hue=0.1)],
|
68 |
+
p=0.8,
|
69 |
+
),
|
70 |
+
transforms.RandomGrayscale(p=0.2),
|
71 |
+
]
|
72 |
+
)
|
73 |
+
|
74 |
+
global_transfo1_extra = GaussianBlur(p=1.0)
|
75 |
+
|
76 |
+
global_transfo2_extra = transforms.Compose(
|
77 |
+
[
|
78 |
+
GaussianBlur(p=0.1),
|
79 |
+
transforms.RandomSolarize(threshold=128, p=0.2),
|
80 |
+
]
|
81 |
+
)
|
82 |
+
|
83 |
+
local_transfo_extra = GaussianBlur(p=0.5)
|
84 |
+
|
85 |
+
# normalization
|
86 |
+
self.normalize = transforms.Compose(
|
87 |
+
[
|
88 |
+
transforms.ToTensor(),
|
89 |
+
make_normalize_transform(),
|
90 |
+
]
|
91 |
+
)
|
92 |
+
|
93 |
+
self.global_transfo1 = transforms.Compose([color_jittering, global_transfo1_extra, self.normalize])
|
94 |
+
self.global_transfo2 = transforms.Compose([color_jittering, global_transfo2_extra, self.normalize])
|
95 |
+
self.local_transfo = transforms.Compose([color_jittering, local_transfo_extra, self.normalize])
|
96 |
+
|
97 |
+
def __call__(self, image):
|
98 |
+
output = {}
|
99 |
+
|
100 |
+
# global crops:
|
101 |
+
im1_base = self.geometric_augmentation_global(image)
|
102 |
+
global_crop_1 = self.global_transfo1(im1_base)
|
103 |
+
|
104 |
+
im2_base = self.geometric_augmentation_global(image)
|
105 |
+
global_crop_2 = self.global_transfo2(im2_base)
|
106 |
+
|
107 |
+
output["global_crops"] = [global_crop_1, global_crop_2]
|
108 |
+
|
109 |
+
# global crops for teacher:
|
110 |
+
output["global_crops_teacher"] = [global_crop_1, global_crop_2]
|
111 |
+
|
112 |
+
# local crops:
|
113 |
+
local_crops = [
|
114 |
+
self.local_transfo(self.geometric_augmentation_local(image)) for _ in range(self.local_crops_number)
|
115 |
+
]
|
116 |
+
output["local_crops"] = local_crops
|
117 |
+
output["offsets"] = ()
|
118 |
+
|
119 |
+
return output
|
torchhub/facebookresearch_dinov2_main/dinov2/data/collate.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
import torch
|
8 |
+
import random
|
9 |
+
|
10 |
+
|
11 |
+
def collate_data_and_cast(samples_list, mask_ratio_tuple, mask_probability, dtype, n_tokens=None, mask_generator=None):
|
12 |
+
# dtype = torch.half # TODO: Remove
|
13 |
+
|
14 |
+
n_global_crops = len(samples_list[0][0]["global_crops"])
|
15 |
+
n_local_crops = len(samples_list[0][0]["local_crops"])
|
16 |
+
|
17 |
+
collated_global_crops = torch.stack([s[0]["global_crops"][i] for i in range(n_global_crops) for s in samples_list])
|
18 |
+
|
19 |
+
collated_local_crops = torch.stack([s[0]["local_crops"][i] for i in range(n_local_crops) for s in samples_list])
|
20 |
+
|
21 |
+
B = len(collated_global_crops)
|
22 |
+
N = n_tokens
|
23 |
+
n_samples_masked = int(B * mask_probability)
|
24 |
+
probs = torch.linspace(*mask_ratio_tuple, n_samples_masked + 1)
|
25 |
+
upperbound = 0
|
26 |
+
masks_list = []
|
27 |
+
for i in range(0, n_samples_masked):
|
28 |
+
prob_min = probs[i]
|
29 |
+
prob_max = probs[i + 1]
|
30 |
+
masks_list.append(torch.BoolTensor(mask_generator(int(N * random.uniform(prob_min, prob_max)))))
|
31 |
+
upperbound += int(N * prob_max)
|
32 |
+
for i in range(n_samples_masked, B):
|
33 |
+
masks_list.append(torch.BoolTensor(mask_generator(0)))
|
34 |
+
|
35 |
+
random.shuffle(masks_list)
|
36 |
+
|
37 |
+
collated_masks = torch.stack(masks_list).flatten(1)
|
38 |
+
mask_indices_list = collated_masks.flatten().nonzero().flatten()
|
39 |
+
|
40 |
+
masks_weight = (1 / collated_masks.sum(-1).clamp(min=1.0)).unsqueeze(-1).expand_as(collated_masks)[collated_masks]
|
41 |
+
|
42 |
+
return {
|
43 |
+
"collated_global_crops": collated_global_crops.to(dtype),
|
44 |
+
"collated_local_crops": collated_local_crops.to(dtype),
|
45 |
+
"collated_masks": collated_masks,
|
46 |
+
"mask_indices_list": mask_indices_list,
|
47 |
+
"masks_weight": masks_weight,
|
48 |
+
"upperbound": upperbound,
|
49 |
+
"n_masked_patches": torch.full((1,), fill_value=mask_indices_list.shape[0], dtype=torch.long),
|
50 |
+
}
|
torchhub/facebookresearch_dinov2_main/dinov2/data/datasets/__init__.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
from .image_net import ImageNet
|
8 |
+
from .image_net_22k import ImageNet22k
|
torchhub/facebookresearch_dinov2_main/dinov2/data/datasets/decoders.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
from io import BytesIO
|
8 |
+
from typing import Any
|
9 |
+
|
10 |
+
from PIL import Image
|
11 |
+
|
12 |
+
|
13 |
+
class Decoder:
|
14 |
+
def decode(self) -> Any:
|
15 |
+
raise NotImplementedError
|
16 |
+
|
17 |
+
|
18 |
+
class ImageDataDecoder(Decoder):
|
19 |
+
def __init__(self, image_data: bytes) -> None:
|
20 |
+
self._image_data = image_data
|
21 |
+
|
22 |
+
def decode(self) -> Image:
|
23 |
+
f = BytesIO(self._image_data)
|
24 |
+
return Image.open(f).convert(mode="RGB")
|
25 |
+
|
26 |
+
|
27 |
+
class TargetDecoder(Decoder):
|
28 |
+
def __init__(self, target: Any):
|
29 |
+
self._target = target
|
30 |
+
|
31 |
+
def decode(self) -> Any:
|
32 |
+
return self._target
|
torchhub/facebookresearch_dinov2_main/dinov2/data/datasets/extended.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
from typing import Any, Tuple
|
8 |
+
|
9 |
+
from torchvision.datasets import VisionDataset
|
10 |
+
|
11 |
+
from .decoders import TargetDecoder, ImageDataDecoder
|
12 |
+
|
13 |
+
|
14 |
+
class ExtendedVisionDataset(VisionDataset):
|
15 |
+
def __init__(self, *args, **kwargs) -> None:
|
16 |
+
super().__init__(*args, **kwargs) # type: ignore
|
17 |
+
|
18 |
+
def get_image_data(self, index: int) -> bytes:
|
19 |
+
raise NotImplementedError
|
20 |
+
|
21 |
+
def get_target(self, index: int) -> Any:
|
22 |
+
raise NotImplementedError
|
23 |
+
|
24 |
+
def __getitem__(self, index: int) -> Tuple[Any, Any]:
|
25 |
+
try:
|
26 |
+
image_data = self.get_image_data(index)
|
27 |
+
image = ImageDataDecoder(image_data).decode()
|
28 |
+
except Exception as e:
|
29 |
+
raise RuntimeError(f"can not read image for sample {index}") from e
|
30 |
+
target = self.get_target(index)
|
31 |
+
target = TargetDecoder(target).decode()
|
32 |
+
|
33 |
+
if self.transforms is not None:
|
34 |
+
image, target = self.transforms(image, target)
|
35 |
+
|
36 |
+
return image, target
|
37 |
+
|
38 |
+
def __len__(self) -> int:
|
39 |
+
raise NotImplementedError
|
torchhub/facebookresearch_dinov2_main/dinov2/data/datasets/image_net.py
ADDED
@@ -0,0 +1,291 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
import csv
|
8 |
+
from enum import Enum
|
9 |
+
import logging
|
10 |
+
import os
|
11 |
+
from typing import Callable, List, Optional, Tuple, Union
|
12 |
+
|
13 |
+
import numpy as np
|
14 |
+
|
15 |
+
from .extended import ExtendedVisionDataset
|
16 |
+
|
17 |
+
|
18 |
+
logger = logging.getLogger("dinov2")
|
19 |
+
_Target = int
|
20 |
+
|
21 |
+
|
22 |
+
class _Split(Enum):
|
23 |
+
TRAIN = "train"
|
24 |
+
VAL = "val"
|
25 |
+
TEST = "test" # NOTE: torchvision does not support the test split
|
26 |
+
|
27 |
+
@property
|
28 |
+
def length(self) -> int:
|
29 |
+
split_lengths = {
|
30 |
+
_Split.TRAIN: 1_281_167,
|
31 |
+
_Split.VAL: 50_000,
|
32 |
+
_Split.TEST: 100_000,
|
33 |
+
}
|
34 |
+
return split_lengths[self]
|
35 |
+
|
36 |
+
def get_dirname(self, class_id: Optional[str] = None) -> str:
|
37 |
+
return self.value if class_id is None else os.path.join(self.value, class_id)
|
38 |
+
|
39 |
+
def get_image_relpath(self, actual_index: int, class_id: Optional[str] = None) -> str:
|
40 |
+
dirname = self.get_dirname(class_id)
|
41 |
+
if self == _Split.TRAIN:
|
42 |
+
basename = f"{class_id}_{actual_index}"
|
43 |
+
else: # self in (_Split.VAL, _Split.TEST):
|
44 |
+
basename = f"ILSVRC2012_{self.value}_{actual_index:08d}"
|
45 |
+
return os.path.join(dirname, basename + ".JPEG")
|
46 |
+
|
47 |
+
def parse_image_relpath(self, image_relpath: str) -> Tuple[str, int]:
|
48 |
+
assert self != _Split.TEST
|
49 |
+
dirname, filename = os.path.split(image_relpath)
|
50 |
+
class_id = os.path.split(dirname)[-1]
|
51 |
+
basename, _ = os.path.splitext(filename)
|
52 |
+
actual_index = int(basename.split("_")[-1])
|
53 |
+
return class_id, actual_index
|
54 |
+
|
55 |
+
|
56 |
+
class ImageNet(ExtendedVisionDataset):
|
57 |
+
Target = Union[_Target]
|
58 |
+
Split = Union[_Split]
|
59 |
+
|
60 |
+
def __init__(
|
61 |
+
self,
|
62 |
+
*,
|
63 |
+
split: "ImageNet.Split",
|
64 |
+
root: str,
|
65 |
+
extra: str,
|
66 |
+
transforms: Optional[Callable] = None,
|
67 |
+
transform: Optional[Callable] = None,
|
68 |
+
target_transform: Optional[Callable] = None,
|
69 |
+
) -> None:
|
70 |
+
super().__init__(root, transforms, transform, target_transform)
|
71 |
+
self._extra_root = extra
|
72 |
+
self._split = split
|
73 |
+
|
74 |
+
self._entries = None
|
75 |
+
self._class_ids = None
|
76 |
+
self._class_names = None
|
77 |
+
|
78 |
+
@property
|
79 |
+
def split(self) -> "ImageNet.Split":
|
80 |
+
return self._split
|
81 |
+
|
82 |
+
def _get_extra_full_path(self, extra_path: str) -> str:
|
83 |
+
return os.path.join(self._extra_root, extra_path)
|
84 |
+
|
85 |
+
def _load_extra(self, extra_path: str) -> np.ndarray:
|
86 |
+
extra_full_path = self._get_extra_full_path(extra_path)
|
87 |
+
return np.load(extra_full_path, mmap_mode="r")
|
88 |
+
|
89 |
+
def _save_extra(self, extra_array: np.ndarray, extra_path: str) -> None:
|
90 |
+
extra_full_path = self._get_extra_full_path(extra_path)
|
91 |
+
os.makedirs(self._extra_root, exist_ok=True)
|
92 |
+
np.save(extra_full_path, extra_array)
|
93 |
+
|
94 |
+
@property
|
95 |
+
def _entries_path(self) -> str:
|
96 |
+
return f"entries-{self._split.value.upper()}.npy"
|
97 |
+
|
98 |
+
@property
|
99 |
+
def _class_ids_path(self) -> str:
|
100 |
+
return f"class-ids-{self._split.value.upper()}.npy"
|
101 |
+
|
102 |
+
@property
|
103 |
+
def _class_names_path(self) -> str:
|
104 |
+
return f"class-names-{self._split.value.upper()}.npy"
|
105 |
+
|
106 |
+
def _get_entries(self) -> np.ndarray:
|
107 |
+
if self._entries is None:
|
108 |
+
self._entries = self._load_extra(self._entries_path)
|
109 |
+
assert self._entries is not None
|
110 |
+
return self._entries
|
111 |
+
|
112 |
+
def _get_class_ids(self) -> np.ndarray:
|
113 |
+
if self._split == _Split.TEST:
|
114 |
+
assert False, "Class IDs are not available in TEST split"
|
115 |
+
if self._class_ids is None:
|
116 |
+
self._class_ids = self._load_extra(self._class_ids_path)
|
117 |
+
assert self._class_ids is not None
|
118 |
+
return self._class_ids
|
119 |
+
|
120 |
+
def _get_class_names(self) -> np.ndarray:
|
121 |
+
if self._split == _Split.TEST:
|
122 |
+
assert False, "Class names are not available in TEST split"
|
123 |
+
if self._class_names is None:
|
124 |
+
self._class_names = self._load_extra(self._class_names_path)
|
125 |
+
assert self._class_names is not None
|
126 |
+
return self._class_names
|
127 |
+
|
128 |
+
def find_class_id(self, class_index: int) -> str:
|
129 |
+
class_ids = self._get_class_ids()
|
130 |
+
return str(class_ids[class_index])
|
131 |
+
|
132 |
+
def find_class_name(self, class_index: int) -> str:
|
133 |
+
class_names = self._get_class_names()
|
134 |
+
return str(class_names[class_index])
|
135 |
+
|
136 |
+
def get_image_data(self, index: int) -> bytes:
|
137 |
+
entries = self._get_entries()
|
138 |
+
actual_index = entries[index]["actual_index"]
|
139 |
+
|
140 |
+
class_id = self.get_class_id(index)
|
141 |
+
|
142 |
+
image_relpath = self.split.get_image_relpath(actual_index, class_id)
|
143 |
+
image_full_path = os.path.join(self.root, image_relpath)
|
144 |
+
with open(image_full_path, mode="rb") as f:
|
145 |
+
image_data = f.read()
|
146 |
+
return image_data
|
147 |
+
|
148 |
+
def get_target(self, index: int) -> Optional[Target]:
|
149 |
+
entries = self._get_entries()
|
150 |
+
class_index = entries[index]["class_index"]
|
151 |
+
return None if self.split == _Split.TEST else int(class_index)
|
152 |
+
|
153 |
+
def get_targets(self) -> Optional[np.ndarray]:
|
154 |
+
entries = self._get_entries()
|
155 |
+
return None if self.split == _Split.TEST else entries["class_index"]
|
156 |
+
|
157 |
+
def get_class_id(self, index: int) -> Optional[str]:
|
158 |
+
entries = self._get_entries()
|
159 |
+
class_id = entries[index]["class_id"]
|
160 |
+
return None if self.split == _Split.TEST else str(class_id)
|
161 |
+
|
162 |
+
def get_class_name(self, index: int) -> Optional[str]:
|
163 |
+
entries = self._get_entries()
|
164 |
+
class_name = entries[index]["class_name"]
|
165 |
+
return None if self.split == _Split.TEST else str(class_name)
|
166 |
+
|
167 |
+
def __len__(self) -> int:
|
168 |
+
entries = self._get_entries()
|
169 |
+
assert len(entries) == self.split.length
|
170 |
+
return len(entries)
|
171 |
+
|
172 |
+
def _load_labels(self, labels_path: str) -> List[Tuple[str, str]]:
|
173 |
+
labels_full_path = os.path.join(self.root, labels_path)
|
174 |
+
labels = []
|
175 |
+
|
176 |
+
try:
|
177 |
+
with open(labels_full_path, "r") as f:
|
178 |
+
reader = csv.reader(f)
|
179 |
+
for row in reader:
|
180 |
+
class_id, class_name = row
|
181 |
+
labels.append((class_id, class_name))
|
182 |
+
except OSError as e:
|
183 |
+
raise RuntimeError(f'can not read labels file "{labels_full_path}"') from e
|
184 |
+
|
185 |
+
return labels
|
186 |
+
|
187 |
+
def _dump_entries(self) -> None:
|
188 |
+
split = self.split
|
189 |
+
if split == ImageNet.Split.TEST:
|
190 |
+
dataset = None
|
191 |
+
sample_count = split.length
|
192 |
+
max_class_id_length, max_class_name_length = 0, 0
|
193 |
+
else:
|
194 |
+
labels_path = "labels.txt"
|
195 |
+
logger.info(f'loading labels from "{labels_path}"')
|
196 |
+
labels = self._load_labels(labels_path)
|
197 |
+
|
198 |
+
# NOTE: Using torchvision ImageFolder for consistency
|
199 |
+
from torchvision.datasets import ImageFolder
|
200 |
+
|
201 |
+
dataset_root = os.path.join(self.root, split.get_dirname())
|
202 |
+
dataset = ImageFolder(dataset_root)
|
203 |
+
sample_count = len(dataset)
|
204 |
+
max_class_id_length, max_class_name_length = -1, -1
|
205 |
+
for sample in dataset.samples:
|
206 |
+
_, class_index = sample
|
207 |
+
class_id, class_name = labels[class_index]
|
208 |
+
max_class_id_length = max(len(class_id), max_class_id_length)
|
209 |
+
max_class_name_length = max(len(class_name), max_class_name_length)
|
210 |
+
|
211 |
+
dtype = np.dtype(
|
212 |
+
[
|
213 |
+
("actual_index", "<u4"),
|
214 |
+
("class_index", "<u4"),
|
215 |
+
("class_id", f"U{max_class_id_length}"),
|
216 |
+
("class_name", f"U{max_class_name_length}"),
|
217 |
+
]
|
218 |
+
)
|
219 |
+
entries_array = np.empty(sample_count, dtype=dtype)
|
220 |
+
|
221 |
+
if split == ImageNet.Split.TEST:
|
222 |
+
old_percent = -1
|
223 |
+
for index in range(sample_count):
|
224 |
+
percent = 100 * (index + 1) // sample_count
|
225 |
+
if percent > old_percent:
|
226 |
+
logger.info(f"creating entries: {percent}%")
|
227 |
+
old_percent = percent
|
228 |
+
|
229 |
+
actual_index = index + 1
|
230 |
+
class_index = np.uint32(-1)
|
231 |
+
class_id, class_name = "", ""
|
232 |
+
entries_array[index] = (actual_index, class_index, class_id, class_name)
|
233 |
+
else:
|
234 |
+
class_names = {class_id: class_name for class_id, class_name in labels}
|
235 |
+
|
236 |
+
assert dataset
|
237 |
+
old_percent = -1
|
238 |
+
for index in range(sample_count):
|
239 |
+
percent = 100 * (index + 1) // sample_count
|
240 |
+
if percent > old_percent:
|
241 |
+
logger.info(f"creating entries: {percent}%")
|
242 |
+
old_percent = percent
|
243 |
+
|
244 |
+
image_full_path, class_index = dataset.samples[index]
|
245 |
+
image_relpath = os.path.relpath(image_full_path, self.root)
|
246 |
+
class_id, actual_index = split.parse_image_relpath(image_relpath)
|
247 |
+
class_name = class_names[class_id]
|
248 |
+
entries_array[index] = (actual_index, class_index, class_id, class_name)
|
249 |
+
|
250 |
+
logger.info(f'saving entries to "{self._entries_path}"')
|
251 |
+
self._save_extra(entries_array, self._entries_path)
|
252 |
+
|
253 |
+
def _dump_class_ids_and_names(self) -> None:
|
254 |
+
split = self.split
|
255 |
+
if split == ImageNet.Split.TEST:
|
256 |
+
return
|
257 |
+
|
258 |
+
entries_array = self._load_extra(self._entries_path)
|
259 |
+
|
260 |
+
max_class_id_length, max_class_name_length, max_class_index = -1, -1, -1
|
261 |
+
for entry in entries_array:
|
262 |
+
class_index, class_id, class_name = (
|
263 |
+
entry["class_index"],
|
264 |
+
entry["class_id"],
|
265 |
+
entry["class_name"],
|
266 |
+
)
|
267 |
+
max_class_index = max(int(class_index), max_class_index)
|
268 |
+
max_class_id_length = max(len(str(class_id)), max_class_id_length)
|
269 |
+
max_class_name_length = max(len(str(class_name)), max_class_name_length)
|
270 |
+
|
271 |
+
class_count = max_class_index + 1
|
272 |
+
class_ids_array = np.empty(class_count, dtype=f"U{max_class_id_length}")
|
273 |
+
class_names_array = np.empty(class_count, dtype=f"U{max_class_name_length}")
|
274 |
+
for entry in entries_array:
|
275 |
+
class_index, class_id, class_name = (
|
276 |
+
entry["class_index"],
|
277 |
+
entry["class_id"],
|
278 |
+
entry["class_name"],
|
279 |
+
)
|
280 |
+
class_ids_array[class_index] = class_id
|
281 |
+
class_names_array[class_index] = class_name
|
282 |
+
|
283 |
+
logger.info(f'saving class IDs to "{self._class_ids_path}"')
|
284 |
+
self._save_extra(class_ids_array, self._class_ids_path)
|
285 |
+
|
286 |
+
logger.info(f'saving class names to "{self._class_names_path}"')
|
287 |
+
self._save_extra(class_names_array, self._class_names_path)
|
288 |
+
|
289 |
+
def dump_extra(self) -> None:
|
290 |
+
self._dump_entries()
|
291 |
+
self._dump_class_ids_and_names()
|
torchhub/facebookresearch_dinov2_main/dinov2/data/datasets/image_net_22k.py
ADDED
@@ -0,0 +1,303 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
from dataclasses import dataclass
|
8 |
+
from enum import Enum
|
9 |
+
from functools import lru_cache
|
10 |
+
from gzip import GzipFile
|
11 |
+
from io import BytesIO
|
12 |
+
from mmap import ACCESS_READ, mmap
|
13 |
+
import os
|
14 |
+
from typing import Any, Callable, List, Optional, Set, Tuple
|
15 |
+
import warnings
|
16 |
+
|
17 |
+
import numpy as np
|
18 |
+
|
19 |
+
from .extended import ExtendedVisionDataset
|
20 |
+
|
21 |
+
|
22 |
+
_Labels = int
|
23 |
+
|
24 |
+
_DEFAULT_MMAP_CACHE_SIZE = 16 # Warning: This can exhaust file descriptors
|
25 |
+
|
26 |
+
|
27 |
+
@dataclass
|
28 |
+
class _ClassEntry:
|
29 |
+
block_offset: int
|
30 |
+
maybe_filename: Optional[str] = None
|
31 |
+
|
32 |
+
|
33 |
+
@dataclass
|
34 |
+
class _Entry:
|
35 |
+
class_index: int # noqa: E701
|
36 |
+
start_offset: int
|
37 |
+
end_offset: int
|
38 |
+
filename: str
|
39 |
+
|
40 |
+
|
41 |
+
class _Split(Enum):
|
42 |
+
TRAIN = "train"
|
43 |
+
VAL = "val"
|
44 |
+
|
45 |
+
@property
|
46 |
+
def length(self) -> int:
|
47 |
+
return {
|
48 |
+
_Split.TRAIN: 11_797_647,
|
49 |
+
_Split.VAL: 561_050,
|
50 |
+
}[self]
|
51 |
+
|
52 |
+
def entries_path(self):
|
53 |
+
return f"imagenet21kp_{self.value}.txt"
|
54 |
+
|
55 |
+
|
56 |
+
def _get_tarball_path(class_id: str) -> str:
|
57 |
+
return f"{class_id}.tar"
|
58 |
+
|
59 |
+
|
60 |
+
def _make_mmap_tarball(tarballs_root: str, mmap_cache_size: int):
|
61 |
+
@lru_cache(maxsize=mmap_cache_size)
|
62 |
+
def _mmap_tarball(class_id: str) -> mmap:
|
63 |
+
tarball_path = _get_tarball_path(class_id)
|
64 |
+
tarball_full_path = os.path.join(tarballs_root, tarball_path)
|
65 |
+
with open(tarball_full_path) as f:
|
66 |
+
return mmap(fileno=f.fileno(), length=0, access=ACCESS_READ)
|
67 |
+
|
68 |
+
return _mmap_tarball
|
69 |
+
|
70 |
+
|
71 |
+
class ImageNet22k(ExtendedVisionDataset):
|
72 |
+
_GZIPPED_INDICES: Set[int] = {
|
73 |
+
841_545,
|
74 |
+
1_304_131,
|
75 |
+
2_437_921,
|
76 |
+
2_672_079,
|
77 |
+
2_795_676,
|
78 |
+
2_969_786,
|
79 |
+
6_902_965,
|
80 |
+
6_903_550,
|
81 |
+
6_903_628,
|
82 |
+
7_432_557,
|
83 |
+
7_432_589,
|
84 |
+
7_813_809,
|
85 |
+
8_329_633,
|
86 |
+
10_296_990,
|
87 |
+
10_417_652,
|
88 |
+
10_492_265,
|
89 |
+
10_598_078,
|
90 |
+
10_782_398,
|
91 |
+
10_902_612,
|
92 |
+
11_203_736,
|
93 |
+
11_342_890,
|
94 |
+
11_397_596,
|
95 |
+
11_589_762,
|
96 |
+
11_705_103,
|
97 |
+
12_936_875,
|
98 |
+
13_289_782,
|
99 |
+
}
|
100 |
+
Labels = _Labels
|
101 |
+
|
102 |
+
def __init__(
|
103 |
+
self,
|
104 |
+
*,
|
105 |
+
root: str,
|
106 |
+
extra: str,
|
107 |
+
transforms: Optional[Callable] = None,
|
108 |
+
transform: Optional[Callable] = None,
|
109 |
+
target_transform: Optional[Callable] = None,
|
110 |
+
mmap_cache_size: int = _DEFAULT_MMAP_CACHE_SIZE,
|
111 |
+
) -> None:
|
112 |
+
super().__init__(root, transforms, transform, target_transform)
|
113 |
+
self._extra_root = extra
|
114 |
+
|
115 |
+
entries_path = self._get_entries_path(root)
|
116 |
+
self._entries = self._load_extra(entries_path)
|
117 |
+
|
118 |
+
class_ids_path = self._get_class_ids_path(root)
|
119 |
+
self._class_ids = self._load_extra(class_ids_path)
|
120 |
+
|
121 |
+
self._gzipped_indices = ImageNet22k._GZIPPED_INDICES
|
122 |
+
self._mmap_tarball = _make_mmap_tarball(self._tarballs_root, mmap_cache_size)
|
123 |
+
|
124 |
+
def _get_entries_path(self, root: Optional[str] = None) -> str:
|
125 |
+
return "entries.npy"
|
126 |
+
|
127 |
+
def _get_class_ids_path(self, root: Optional[str] = None) -> str:
|
128 |
+
return "class-ids.npy"
|
129 |
+
|
130 |
+
def _find_class_ids(self, path: str) -> List[str]:
|
131 |
+
class_ids = []
|
132 |
+
|
133 |
+
with os.scandir(path) as entries:
|
134 |
+
for entry in entries:
|
135 |
+
root, ext = os.path.splitext(entry.name)
|
136 |
+
if ext != ".tar":
|
137 |
+
continue
|
138 |
+
class_ids.append(root)
|
139 |
+
|
140 |
+
return sorted(class_ids)
|
141 |
+
|
142 |
+
def _load_entries_class_ids(self, root: Optional[str] = None) -> Tuple[List[_Entry], List[str]]:
|
143 |
+
root = self.get_root(root)
|
144 |
+
entries: List[_Entry] = []
|
145 |
+
class_ids = self._find_class_ids(root)
|
146 |
+
|
147 |
+
for class_index, class_id in enumerate(class_ids):
|
148 |
+
path = os.path.join(root, "blocks", f"{class_id}.log")
|
149 |
+
class_entries = []
|
150 |
+
|
151 |
+
try:
|
152 |
+
with open(path) as f:
|
153 |
+
for line in f:
|
154 |
+
line = line.rstrip()
|
155 |
+
block, filename = line.split(":")
|
156 |
+
block_offset = int(block[6:])
|
157 |
+
filename = filename[1:]
|
158 |
+
|
159 |
+
maybe_filename = None
|
160 |
+
if filename != "** Block of NULs **":
|
161 |
+
maybe_filename = filename
|
162 |
+
_, ext = os.path.splitext(filename)
|
163 |
+
# assert ext == ".JPEG"
|
164 |
+
|
165 |
+
class_entry = _ClassEntry(block_offset, maybe_filename)
|
166 |
+
class_entries.append(class_entry)
|
167 |
+
except OSError as e:
|
168 |
+
raise RuntimeError(f'can not read blocks file "{path}"') from e
|
169 |
+
|
170 |
+
assert class_entries[-1].maybe_filename is None
|
171 |
+
|
172 |
+
for class_entry1, class_entry2 in zip(class_entries, class_entries[1:]):
|
173 |
+
assert class_entry1.block_offset <= class_entry2.block_offset
|
174 |
+
start_offset = 512 * class_entry1.block_offset
|
175 |
+
end_offset = 512 * class_entry2.block_offset
|
176 |
+
assert class_entry1.maybe_filename is not None
|
177 |
+
filename = class_entry1.maybe_filename
|
178 |
+
entry = _Entry(class_index, start_offset, end_offset, filename)
|
179 |
+
# Skip invalid image files (PIL throws UnidentifiedImageError)
|
180 |
+
if filename == "n06470073_47249.JPEG":
|
181 |
+
continue
|
182 |
+
entries.append(entry)
|
183 |
+
|
184 |
+
return entries, class_ids
|
185 |
+
|
186 |
+
def _load_extra(self, extra_path: str) -> np.ndarray:
|
187 |
+
extra_root = self._extra_root
|
188 |
+
extra_full_path = os.path.join(extra_root, extra_path)
|
189 |
+
return np.load(extra_full_path, mmap_mode="r")
|
190 |
+
|
191 |
+
def _save_extra(self, extra_array: np.ndarray, extra_path: str) -> None:
|
192 |
+
extra_root = self._extra_root
|
193 |
+
extra_full_path = os.path.join(extra_root, extra_path)
|
194 |
+
os.makedirs(extra_root, exist_ok=True)
|
195 |
+
np.save(extra_full_path, extra_array)
|
196 |
+
|
197 |
+
@property
|
198 |
+
def _tarballs_root(self) -> str:
|
199 |
+
return self.root
|
200 |
+
|
201 |
+
def find_class_id(self, class_index: int) -> str:
|
202 |
+
return str(self._class_ids[class_index])
|
203 |
+
|
204 |
+
def get_image_data(self, index: int) -> bytes:
|
205 |
+
entry = self._entries[index]
|
206 |
+
class_id = entry["class_id"]
|
207 |
+
class_mmap = self._mmap_tarball(class_id)
|
208 |
+
|
209 |
+
start_offset, end_offset = entry["start_offset"], entry["end_offset"]
|
210 |
+
try:
|
211 |
+
mapped_data = class_mmap[start_offset:end_offset]
|
212 |
+
data = mapped_data[512:] # Skip entry header block
|
213 |
+
|
214 |
+
if len(data) >= 2 and tuple(data[:2]) == (0x1F, 0x8B):
|
215 |
+
assert index in self._gzipped_indices, f"unexpected gzip header for sample {index}"
|
216 |
+
with GzipFile(fileobj=BytesIO(data)) as g:
|
217 |
+
data = g.read()
|
218 |
+
except Exception as e:
|
219 |
+
raise RuntimeError(f"can not retrieve image data for sample {index} " f'from "{class_id}" tarball') from e
|
220 |
+
|
221 |
+
return data
|
222 |
+
|
223 |
+
def get_target(self, index: int) -> Any:
|
224 |
+
return int(self._entries[index]["class_index"])
|
225 |
+
|
226 |
+
def get_targets(self) -> np.ndarray:
|
227 |
+
return self._entries["class_index"]
|
228 |
+
|
229 |
+
def get_class_id(self, index: int) -> str:
|
230 |
+
return str(self._entries[index]["class_id"])
|
231 |
+
|
232 |
+
def get_class_ids(self) -> np.ndarray:
|
233 |
+
return self._entries["class_id"]
|
234 |
+
|
235 |
+
def __getitem__(self, index: int) -> Tuple[Any, Any]:
|
236 |
+
with warnings.catch_warnings():
|
237 |
+
warnings.simplefilter("ignore")
|
238 |
+
return super().__getitem__(index)
|
239 |
+
|
240 |
+
def __len__(self) -> int:
|
241 |
+
return len(self._entries)
|
242 |
+
|
243 |
+
def _dump_entries(self, *args, **kwargs) -> None:
|
244 |
+
entries, class_ids = self._load_entries_class_ids(*args, **kwargs)
|
245 |
+
|
246 |
+
max_class_id_length, max_filename_length, max_class_index = -1, -1, -1
|
247 |
+
for entry in entries:
|
248 |
+
class_id = class_ids[entry.class_index]
|
249 |
+
max_class_index = max(entry.class_index, max_class_index)
|
250 |
+
max_class_id_length = max(len(class_id), max_class_id_length)
|
251 |
+
max_filename_length = max(len(entry.filename), max_filename_length)
|
252 |
+
|
253 |
+
dtype = np.dtype(
|
254 |
+
[
|
255 |
+
("class_index", "<u4"),
|
256 |
+
("class_id", f"U{max_class_id_length}"),
|
257 |
+
("start_offset", "<u4"),
|
258 |
+
("end_offset", "<u4"),
|
259 |
+
("filename", f"U{max_filename_length}"),
|
260 |
+
]
|
261 |
+
)
|
262 |
+
sample_count = len(entries)
|
263 |
+
entries_array = np.empty(sample_count, dtype=dtype)
|
264 |
+
for i, entry in enumerate(entries):
|
265 |
+
class_index = entry.class_index
|
266 |
+
class_id = class_ids[class_index]
|
267 |
+
start_offset = entry.start_offset
|
268 |
+
end_offset = entry.end_offset
|
269 |
+
filename = entry.filename
|
270 |
+
entries_array[i] = (
|
271 |
+
class_index,
|
272 |
+
class_id,
|
273 |
+
start_offset,
|
274 |
+
end_offset,
|
275 |
+
filename,
|
276 |
+
)
|
277 |
+
|
278 |
+
entries_path = self._get_entries_path(*args, **kwargs)
|
279 |
+
self._save_extra(entries_array, entries_path)
|
280 |
+
|
281 |
+
def _dump_class_ids(self, *args, **kwargs) -> None:
|
282 |
+
entries_path = self._get_entries_path(*args, **kwargs)
|
283 |
+
entries_array = self._load_extra(entries_path)
|
284 |
+
|
285 |
+
max_class_id_length, max_class_index = -1, -1
|
286 |
+
for entry in entries_array:
|
287 |
+
class_index, class_id = entry["class_index"], entry["class_id"]
|
288 |
+
max_class_index = max(int(class_index), max_class_index)
|
289 |
+
max_class_id_length = max(len(str(class_id)), max_class_id_length)
|
290 |
+
|
291 |
+
class_ids_array = np.empty(max_class_index + 1, dtype=f"U{max_class_id_length}")
|
292 |
+
for entry in entries_array:
|
293 |
+
class_index, class_id = entry["class_index"], entry["class_id"]
|
294 |
+
class_ids_array[class_index] = class_id
|
295 |
+
class_ids_path = self._get_class_ids_path(*args, **kwargs)
|
296 |
+
self._save_extra(class_ids_array, class_ids_path)
|
297 |
+
|
298 |
+
def _dump_extra(self, *args, **kwargs) -> None:
|
299 |
+
self._dump_entries(*args, *kwargs)
|
300 |
+
self._dump_class_ids(*args, *kwargs)
|
301 |
+
|
302 |
+
def dump_extra(self, root: Optional[str] = None) -> None:
|
303 |
+
return self._dump_extra(root)
|
torchhub/facebookresearch_dinov2_main/dinov2/data/loaders.py
ADDED
@@ -0,0 +1,223 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
import logging
|
8 |
+
from enum import Enum
|
9 |
+
from typing import Any, Callable, List, Optional, TypeVar
|
10 |
+
|
11 |
+
import torch
|
12 |
+
from torch.utils.data import Sampler
|
13 |
+
|
14 |
+
from .datasets import ImageNet, ImageNet22k
|
15 |
+
from .samplers import EpochSampler, InfiniteSampler, ShardedInfiniteSampler
|
16 |
+
|
17 |
+
|
18 |
+
logger = logging.getLogger("dinov2")
|
19 |
+
|
20 |
+
|
21 |
+
class SamplerType(Enum):
|
22 |
+
DISTRIBUTED = 0
|
23 |
+
EPOCH = 1
|
24 |
+
INFINITE = 2
|
25 |
+
SHARDED_INFINITE = 3
|
26 |
+
SHARDED_INFINITE_NEW = 4
|
27 |
+
|
28 |
+
|
29 |
+
def _make_bool_str(b: bool) -> str:
|
30 |
+
return "yes" if b else "no"
|
31 |
+
|
32 |
+
|
33 |
+
def _make_sample_transform(image_transform: Optional[Callable] = None, target_transform: Optional[Callable] = None):
|
34 |
+
def transform(sample):
|
35 |
+
image, target = sample
|
36 |
+
if image_transform is not None:
|
37 |
+
image = image_transform(image)
|
38 |
+
if target_transform is not None:
|
39 |
+
target = target_transform(target)
|
40 |
+
return image, target
|
41 |
+
|
42 |
+
return transform
|
43 |
+
|
44 |
+
|
45 |
+
def _parse_dataset_str(dataset_str: str):
|
46 |
+
tokens = dataset_str.split(":")
|
47 |
+
|
48 |
+
name = tokens[0]
|
49 |
+
kwargs = {}
|
50 |
+
|
51 |
+
for token in tokens[1:]:
|
52 |
+
key, value = token.split("=")
|
53 |
+
assert key in ("root", "extra", "split")
|
54 |
+
kwargs[key] = value
|
55 |
+
|
56 |
+
if name == "ImageNet":
|
57 |
+
class_ = ImageNet
|
58 |
+
if "split" in kwargs:
|
59 |
+
kwargs["split"] = ImageNet.Split[kwargs["split"]]
|
60 |
+
elif name == "ImageNet22k":
|
61 |
+
class_ = ImageNet22k
|
62 |
+
else:
|
63 |
+
raise ValueError(f'Unsupported dataset "{name}"')
|
64 |
+
|
65 |
+
return class_, kwargs
|
66 |
+
|
67 |
+
|
68 |
+
def make_dataset(
|
69 |
+
*,
|
70 |
+
dataset_str: str,
|
71 |
+
transform: Optional[Callable] = None,
|
72 |
+
target_transform: Optional[Callable] = None,
|
73 |
+
):
|
74 |
+
"""
|
75 |
+
Creates a dataset with the specified parameters.
|
76 |
+
|
77 |
+
Args:
|
78 |
+
dataset_str: A dataset string description (e.g. ImageNet:split=TRAIN).
|
79 |
+
transform: A transform to apply to images.
|
80 |
+
target_transform: A transform to apply to targets.
|
81 |
+
|
82 |
+
Returns:
|
83 |
+
The created dataset.
|
84 |
+
"""
|
85 |
+
logger.info(f'using dataset: "{dataset_str}"')
|
86 |
+
|
87 |
+
class_, kwargs = _parse_dataset_str(dataset_str)
|
88 |
+
dataset = class_(transform=transform, target_transform=target_transform, **kwargs)
|
89 |
+
|
90 |
+
logger.info(f"# of dataset samples: {len(dataset):,d}")
|
91 |
+
|
92 |
+
# Aggregated datasets do not expose (yet) these attributes, so add them.
|
93 |
+
if not hasattr(dataset, "transform"):
|
94 |
+
setattr(dataset, "transform", transform)
|
95 |
+
if not hasattr(dataset, "target_transform"):
|
96 |
+
setattr(dataset, "target_transform", target_transform)
|
97 |
+
|
98 |
+
return dataset
|
99 |
+
|
100 |
+
|
101 |
+
def _make_sampler(
|
102 |
+
*,
|
103 |
+
dataset,
|
104 |
+
type: Optional[SamplerType] = None,
|
105 |
+
shuffle: bool = False,
|
106 |
+
seed: int = 0,
|
107 |
+
size: int = -1,
|
108 |
+
advance: int = 0,
|
109 |
+
) -> Optional[Sampler]:
|
110 |
+
sample_count = len(dataset)
|
111 |
+
|
112 |
+
if type == SamplerType.INFINITE:
|
113 |
+
logger.info("sampler: infinite")
|
114 |
+
if size > 0:
|
115 |
+
raise ValueError("sampler size > 0 is invalid")
|
116 |
+
return InfiniteSampler(
|
117 |
+
sample_count=sample_count,
|
118 |
+
shuffle=shuffle,
|
119 |
+
seed=seed,
|
120 |
+
advance=advance,
|
121 |
+
)
|
122 |
+
elif type in (SamplerType.SHARDED_INFINITE, SamplerType.SHARDED_INFINITE_NEW):
|
123 |
+
logger.info("sampler: sharded infinite")
|
124 |
+
if size > 0:
|
125 |
+
raise ValueError("sampler size > 0 is invalid")
|
126 |
+
# TODO: Remove support for old shuffling
|
127 |
+
use_new_shuffle_tensor_slice = type == SamplerType.SHARDED_INFINITE_NEW
|
128 |
+
return ShardedInfiniteSampler(
|
129 |
+
sample_count=sample_count,
|
130 |
+
shuffle=shuffle,
|
131 |
+
seed=seed,
|
132 |
+
advance=advance,
|
133 |
+
use_new_shuffle_tensor_slice=use_new_shuffle_tensor_slice,
|
134 |
+
)
|
135 |
+
elif type == SamplerType.EPOCH:
|
136 |
+
logger.info("sampler: epoch")
|
137 |
+
if advance > 0:
|
138 |
+
raise NotImplementedError("sampler advance > 0 is not supported")
|
139 |
+
size = size if size > 0 else sample_count
|
140 |
+
logger.info(f"# of samples / epoch: {size:,d}")
|
141 |
+
return EpochSampler(
|
142 |
+
size=size,
|
143 |
+
sample_count=sample_count,
|
144 |
+
shuffle=shuffle,
|
145 |
+
seed=seed,
|
146 |
+
)
|
147 |
+
elif type == SamplerType.DISTRIBUTED:
|
148 |
+
logger.info("sampler: distributed")
|
149 |
+
if size > 0:
|
150 |
+
raise ValueError("sampler size > 0 is invalid")
|
151 |
+
if advance > 0:
|
152 |
+
raise ValueError("sampler advance > 0 is invalid")
|
153 |
+
return torch.utils.data.DistributedSampler(
|
154 |
+
dataset=dataset,
|
155 |
+
shuffle=shuffle,
|
156 |
+
seed=seed,
|
157 |
+
drop_last=False,
|
158 |
+
)
|
159 |
+
|
160 |
+
logger.info("sampler: none")
|
161 |
+
return None
|
162 |
+
|
163 |
+
|
164 |
+
T = TypeVar("T")
|
165 |
+
|
166 |
+
|
167 |
+
def make_data_loader(
|
168 |
+
*,
|
169 |
+
dataset,
|
170 |
+
batch_size: int,
|
171 |
+
num_workers: int,
|
172 |
+
shuffle: bool = True,
|
173 |
+
seed: int = 0,
|
174 |
+
sampler_type: Optional[SamplerType] = SamplerType.INFINITE,
|
175 |
+
sampler_size: int = -1,
|
176 |
+
sampler_advance: int = 0,
|
177 |
+
drop_last: bool = True,
|
178 |
+
persistent_workers: bool = False,
|
179 |
+
collate_fn: Optional[Callable[[List[T]], Any]] = None,
|
180 |
+
):
|
181 |
+
"""
|
182 |
+
Creates a data loader with the specified parameters.
|
183 |
+
|
184 |
+
Args:
|
185 |
+
dataset: A dataset (third party, LaViDa or WebDataset).
|
186 |
+
batch_size: The size of batches to generate.
|
187 |
+
num_workers: The number of workers to use.
|
188 |
+
shuffle: Whether to shuffle samples.
|
189 |
+
seed: The random seed to use.
|
190 |
+
sampler_type: Which sampler to use: EPOCH, INFINITE, SHARDED_INFINITE, SHARDED_INFINITE_NEW, DISTRIBUTED or None.
|
191 |
+
sampler_size: The number of images per epoch (when applicable) or -1 for the entire dataset.
|
192 |
+
sampler_advance: How many samples to skip (when applicable).
|
193 |
+
drop_last: Whether the last non-full batch of data should be dropped.
|
194 |
+
persistent_workers: maintain the workers Dataset instances alive after a dataset has been consumed once.
|
195 |
+
collate_fn: Function that performs batch collation
|
196 |
+
"""
|
197 |
+
|
198 |
+
sampler = _make_sampler(
|
199 |
+
dataset=dataset,
|
200 |
+
type=sampler_type,
|
201 |
+
shuffle=shuffle,
|
202 |
+
seed=seed,
|
203 |
+
size=sampler_size,
|
204 |
+
advance=sampler_advance,
|
205 |
+
)
|
206 |
+
|
207 |
+
logger.info("using PyTorch data loader")
|
208 |
+
data_loader = torch.utils.data.DataLoader(
|
209 |
+
dataset,
|
210 |
+
sampler=sampler,
|
211 |
+
batch_size=batch_size,
|
212 |
+
num_workers=num_workers,
|
213 |
+
pin_memory=True,
|
214 |
+
drop_last=drop_last,
|
215 |
+
persistent_workers=persistent_workers,
|
216 |
+
collate_fn=collate_fn,
|
217 |
+
)
|
218 |
+
|
219 |
+
try:
|
220 |
+
logger.info(f"# of batches: {len(data_loader):,d}")
|
221 |
+
except TypeError: # data loader has no length
|
222 |
+
logger.info("infinite data loader")
|
223 |
+
return data_loader
|
torchhub/facebookresearch_dinov2_main/dinov2/data/masking.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
import random
|
8 |
+
import math
|
9 |
+
import numpy as np
|
10 |
+
|
11 |
+
|
12 |
+
class MaskingGenerator:
|
13 |
+
def __init__(
|
14 |
+
self,
|
15 |
+
input_size,
|
16 |
+
num_masking_patches=None,
|
17 |
+
min_num_patches=4,
|
18 |
+
max_num_patches=None,
|
19 |
+
min_aspect=0.3,
|
20 |
+
max_aspect=None,
|
21 |
+
):
|
22 |
+
if not isinstance(input_size, tuple):
|
23 |
+
input_size = (input_size,) * 2
|
24 |
+
self.height, self.width = input_size
|
25 |
+
|
26 |
+
self.num_patches = self.height * self.width
|
27 |
+
self.num_masking_patches = num_masking_patches
|
28 |
+
|
29 |
+
self.min_num_patches = min_num_patches
|
30 |
+
self.max_num_patches = num_masking_patches if max_num_patches is None else max_num_patches
|
31 |
+
|
32 |
+
max_aspect = max_aspect or 1 / min_aspect
|
33 |
+
self.log_aspect_ratio = (math.log(min_aspect), math.log(max_aspect))
|
34 |
+
|
35 |
+
def __repr__(self):
|
36 |
+
repr_str = "Generator(%d, %d -> [%d ~ %d], max = %d, %.3f ~ %.3f)" % (
|
37 |
+
self.height,
|
38 |
+
self.width,
|
39 |
+
self.min_num_patches,
|
40 |
+
self.max_num_patches,
|
41 |
+
self.num_masking_patches,
|
42 |
+
self.log_aspect_ratio[0],
|
43 |
+
self.log_aspect_ratio[1],
|
44 |
+
)
|
45 |
+
return repr_str
|
46 |
+
|
47 |
+
def get_shape(self):
|
48 |
+
return self.height, self.width
|
49 |
+
|
50 |
+
def _mask(self, mask, max_mask_patches):
|
51 |
+
delta = 0
|
52 |
+
for _ in range(10):
|
53 |
+
target_area = random.uniform(self.min_num_patches, max_mask_patches)
|
54 |
+
aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio))
|
55 |
+
h = int(round(math.sqrt(target_area * aspect_ratio)))
|
56 |
+
w = int(round(math.sqrt(target_area / aspect_ratio)))
|
57 |
+
if w < self.width and h < self.height:
|
58 |
+
top = random.randint(0, self.height - h)
|
59 |
+
left = random.randint(0, self.width - w)
|
60 |
+
|
61 |
+
num_masked = mask[top : top + h, left : left + w].sum()
|
62 |
+
# Overlap
|
63 |
+
if 0 < h * w - num_masked <= max_mask_patches:
|
64 |
+
for i in range(top, top + h):
|
65 |
+
for j in range(left, left + w):
|
66 |
+
if mask[i, j] == 0:
|
67 |
+
mask[i, j] = 1
|
68 |
+
delta += 1
|
69 |
+
|
70 |
+
if delta > 0:
|
71 |
+
break
|
72 |
+
return delta
|
73 |
+
|
74 |
+
def __call__(self, num_masking_patches=0):
|
75 |
+
mask = np.zeros(shape=self.get_shape(), dtype=bool)
|
76 |
+
mask_count = 0
|
77 |
+
while mask_count < num_masking_patches:
|
78 |
+
max_mask_patches = num_masking_patches - mask_count
|
79 |
+
max_mask_patches = min(max_mask_patches, self.max_num_patches)
|
80 |
+
|
81 |
+
delta = self._mask(mask, max_mask_patches)
|
82 |
+
if delta == 0:
|
83 |
+
break
|
84 |
+
else:
|
85 |
+
mask_count += delta
|
86 |
+
|
87 |
+
return mask
|
torchhub/facebookresearch_dinov2_main/dinov2/data/samplers.py
ADDED
@@ -0,0 +1,230 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
import itertools
|
8 |
+
from typing import Any, Optional
|
9 |
+
import warnings
|
10 |
+
|
11 |
+
import numpy as np
|
12 |
+
import torch
|
13 |
+
from torch.utils.data.sampler import Sampler
|
14 |
+
|
15 |
+
import dinov2.distributed as distributed
|
16 |
+
|
17 |
+
|
18 |
+
class EpochSampler(Sampler):
|
19 |
+
def __init__(
|
20 |
+
self,
|
21 |
+
*,
|
22 |
+
size: int,
|
23 |
+
sample_count: int,
|
24 |
+
shuffle: bool = False,
|
25 |
+
seed: int = 0,
|
26 |
+
start: Optional[int] = None,
|
27 |
+
step: Optional[int] = None,
|
28 |
+
):
|
29 |
+
self._size = size
|
30 |
+
self._sample_count = sample_count
|
31 |
+
self._shuffle = shuffle
|
32 |
+
self._seed = seed
|
33 |
+
self._start = distributed.get_global_rank() if start is None else start
|
34 |
+
self._step = distributed.get_global_size() if step is None else step
|
35 |
+
self._epoch = 0
|
36 |
+
|
37 |
+
def __iter__(self):
|
38 |
+
count = (self._size + self._sample_count - 1) // self._sample_count
|
39 |
+
tiled_indices = np.tile(np.arange(self._sample_count), count)
|
40 |
+
if self._shuffle:
|
41 |
+
seed = self._seed * self._epoch if self._seed != 0 else self._epoch
|
42 |
+
rng = np.random.default_rng(seed)
|
43 |
+
iterable = rng.choice(tiled_indices, self._size, replace=False)
|
44 |
+
else:
|
45 |
+
iterable = tiled_indices[: self._size]
|
46 |
+
|
47 |
+
yield from itertools.islice(iterable, self._start, None, self._step)
|
48 |
+
|
49 |
+
def __len__(self):
|
50 |
+
return (self._size - self._start + self._step - 1) // self._step
|
51 |
+
|
52 |
+
def set_epoch(self, epoch):
|
53 |
+
self._epoch = epoch
|
54 |
+
|
55 |
+
|
56 |
+
def _get_numpy_dtype(size: int) -> Any:
|
57 |
+
return np.int32 if size <= 2**31 else np.int64
|
58 |
+
|
59 |
+
|
60 |
+
def _get_torch_dtype(size: int) -> Any:
|
61 |
+
return torch.int32 if size <= 2**31 else torch.int64
|
62 |
+
|
63 |
+
|
64 |
+
def _generate_randperm_indices(*, size: int, generator: torch.Generator):
|
65 |
+
"""Generate the indices of a random permutation."""
|
66 |
+
dtype = _get_torch_dtype(size)
|
67 |
+
# This is actually matching PyTorch's CPU implementation, see: https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/TensorFactories.cpp#L900-L921
|
68 |
+
perm = torch.arange(size, dtype=dtype)
|
69 |
+
for i in range(size):
|
70 |
+
j = torch.randint(i, size, size=(1,), generator=generator).item()
|
71 |
+
|
72 |
+
# Always swap even if no-op
|
73 |
+
value = perm[j].item()
|
74 |
+
perm[j] = perm[i].item()
|
75 |
+
perm[i] = value
|
76 |
+
yield value
|
77 |
+
|
78 |
+
|
79 |
+
class InfiniteSampler(Sampler):
|
80 |
+
def __init__(
|
81 |
+
self,
|
82 |
+
*,
|
83 |
+
sample_count: int,
|
84 |
+
shuffle: bool = False,
|
85 |
+
seed: int = 0,
|
86 |
+
start: Optional[int] = None,
|
87 |
+
step: Optional[int] = None,
|
88 |
+
advance: int = 0,
|
89 |
+
):
|
90 |
+
self._sample_count = sample_count
|
91 |
+
self._seed = seed
|
92 |
+
self._shuffle = shuffle
|
93 |
+
self._start = distributed.get_global_rank() if start is None else start
|
94 |
+
self._step = distributed.get_global_size() if step is None else step
|
95 |
+
self._advance = advance
|
96 |
+
|
97 |
+
def __iter__(self):
|
98 |
+
if self._shuffle:
|
99 |
+
iterator = self._shuffled_iterator()
|
100 |
+
else:
|
101 |
+
iterator = self._iterator()
|
102 |
+
|
103 |
+
yield from itertools.islice(iterator, self._advance, None)
|
104 |
+
|
105 |
+
def _iterator(self):
|
106 |
+
assert not self._shuffle
|
107 |
+
|
108 |
+
while True:
|
109 |
+
iterable = range(self._sample_count)
|
110 |
+
yield from itertools.islice(iterable, self._start, None, self._step)
|
111 |
+
|
112 |
+
def _shuffled_iterator(self):
|
113 |
+
assert self._shuffle
|
114 |
+
|
115 |
+
# Instantiate a generator here (rather than in the ctor) to keep the class
|
116 |
+
# picklable (requirement of mp.spawn)
|
117 |
+
generator = torch.Generator().manual_seed(self._seed)
|
118 |
+
|
119 |
+
while True:
|
120 |
+
iterable = _generate_randperm_indices(size=self._sample_count, generator=generator)
|
121 |
+
yield from itertools.islice(iterable, self._start, None, self._step)
|
122 |
+
|
123 |
+
|
124 |
+
# The following function is somewhat equivalent to _new_shuffle_tensor_slice below,
|
125 |
+
# but avoids a full in-place random permutation generation.
|
126 |
+
def _shuffle_tensor_slice(
|
127 |
+
*, tensor: torch.Tensor, start: int = 0, step: int = 1, generator: torch.Generator
|
128 |
+
) -> np.ndarray:
|
129 |
+
stop = len(tensor)
|
130 |
+
count = stop // step
|
131 |
+
drop_count = stop - step * count
|
132 |
+
if drop_count:
|
133 |
+
warnings.warn(f"# of dropped samples: {drop_count}")
|
134 |
+
|
135 |
+
dtype = _get_numpy_dtype(stop)
|
136 |
+
result = np.empty(count, dtype=dtype)
|
137 |
+
|
138 |
+
for i in range(count):
|
139 |
+
j = torch.randint(0, i + 1, size=(1,), generator=generator).item() if i > 0 else 0
|
140 |
+
|
141 |
+
result[i] = result[j]
|
142 |
+
result[j] = tensor[start + i * step].item()
|
143 |
+
|
144 |
+
return result
|
145 |
+
|
146 |
+
|
147 |
+
def _new_shuffle_tensor_slice(
|
148 |
+
*, tensor: torch.Tensor, start: int = 0, step: int = 1, generator: torch.Generator
|
149 |
+
) -> np.ndarray:
|
150 |
+
stop = len(tensor)
|
151 |
+
count = stop // step
|
152 |
+
dtype = torch.int64 # Needed for using randperm result as indices
|
153 |
+
count = stop // step
|
154 |
+
drop_count = stop - step * count
|
155 |
+
if drop_count:
|
156 |
+
warnings.warn(f"# of dropped samples: {drop_count}")
|
157 |
+
indices = torch.randperm(count, dtype=dtype, generator=generator)
|
158 |
+
return tensor[start::step][indices].numpy()
|
159 |
+
|
160 |
+
|
161 |
+
def _make_seed(seed: int, start: int, iter_count: int) -> int:
|
162 |
+
# NOTE: Tried a few variants (including iter_count << 32), this one worked best.
|
163 |
+
return seed + start + (iter_count << 24)
|
164 |
+
|
165 |
+
|
166 |
+
class ShardedInfiniteSampler(Sampler):
|
167 |
+
def __init__(
|
168 |
+
self,
|
169 |
+
*,
|
170 |
+
sample_count: int,
|
171 |
+
shuffle: bool = False,
|
172 |
+
seed: int = 0,
|
173 |
+
start: Optional[int] = None,
|
174 |
+
step: Optional[int] = None,
|
175 |
+
advance: int = 0,
|
176 |
+
use_new_shuffle_tensor_slice: bool = False,
|
177 |
+
):
|
178 |
+
self._sample_count = sample_count
|
179 |
+
self._seed = seed
|
180 |
+
self._shuffle = shuffle
|
181 |
+
self._start = distributed.get_global_rank() if start is None else start
|
182 |
+
self._step = distributed.get_global_size() if step is None else step
|
183 |
+
self._advance = advance
|
184 |
+
self._iter_count = 0
|
185 |
+
self._shuffle_tensor_slice_fn = (
|
186 |
+
_new_shuffle_tensor_slice if use_new_shuffle_tensor_slice else _shuffle_tensor_slice
|
187 |
+
)
|
188 |
+
|
189 |
+
def __iter__(self):
|
190 |
+
iter_count = self._advance // self._sample_count
|
191 |
+
if iter_count > 0:
|
192 |
+
self._advance -= iter_count * self._sample_count
|
193 |
+
self._iter_count += iter_count
|
194 |
+
|
195 |
+
if self._shuffle:
|
196 |
+
iterator = self._shuffled_iterator()
|
197 |
+
else:
|
198 |
+
iterator = self._iterator()
|
199 |
+
|
200 |
+
yield from itertools.islice(iterator, self._advance, None)
|
201 |
+
|
202 |
+
def _iterator(self):
|
203 |
+
assert not self._shuffle
|
204 |
+
|
205 |
+
while True:
|
206 |
+
iterable = range(self._sample_count)
|
207 |
+
yield from itertools.islice(iterable, self._start, None, self._step)
|
208 |
+
|
209 |
+
def _shuffled_iterator(self):
|
210 |
+
assert self._shuffle
|
211 |
+
|
212 |
+
# Instantiate a generator here (rather than in the ctor) to be keep the class
|
213 |
+
# picklable (requirement of mp.spawn)
|
214 |
+
generator = torch.Generator()
|
215 |
+
|
216 |
+
# Always shuffle everything first
|
217 |
+
generator.manual_seed(self._seed)
|
218 |
+
dtype = _get_torch_dtype(self._sample_count)
|
219 |
+
perm = torch.randperm(self._sample_count, dtype=dtype, generator=generator)
|
220 |
+
|
221 |
+
while True:
|
222 |
+
# Re-seed on each iteration to allow skipping whole permutations
|
223 |
+
seed = _make_seed(self._seed, self._start, self._iter_count)
|
224 |
+
generator.manual_seed(seed)
|
225 |
+
|
226 |
+
iterable = self._shuffle_tensor_slice_fn(
|
227 |
+
tensor=perm, start=self._start, step=self._step, generator=generator
|
228 |
+
)
|
229 |
+
yield from iterable
|
230 |
+
self._iter_count += 1
|
torchhub/facebookresearch_dinov2_main/dinov2/data/transforms.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
from typing import Sequence
|
8 |
+
|
9 |
+
import torch
|
10 |
+
from torchvision import transforms
|
11 |
+
|
12 |
+
|
13 |
+
class GaussianBlur(transforms.RandomApply):
|
14 |
+
"""
|
15 |
+
Apply Gaussian Blur to the PIL image.
|
16 |
+
"""
|
17 |
+
|
18 |
+
def __init__(self, *, p: float = 0.5, radius_min: float = 0.1, radius_max: float = 2.0):
|
19 |
+
# NOTE: torchvision is applying 1 - probability to return the original image
|
20 |
+
keep_p = 1 - p
|
21 |
+
transform = transforms.GaussianBlur(kernel_size=9, sigma=(radius_min, radius_max))
|
22 |
+
super().__init__(transforms=[transform], p=keep_p)
|
23 |
+
|
24 |
+
|
25 |
+
class MaybeToTensor(transforms.ToTensor):
|
26 |
+
"""
|
27 |
+
Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor, or keep as is if already a tensor.
|
28 |
+
"""
|
29 |
+
|
30 |
+
def __call__(self, pic):
|
31 |
+
"""
|
32 |
+
Args:
|
33 |
+
pic (PIL Image, numpy.ndarray or torch.tensor): Image to be converted to tensor.
|
34 |
+
Returns:
|
35 |
+
Tensor: Converted image.
|
36 |
+
"""
|
37 |
+
if isinstance(pic, torch.Tensor):
|
38 |
+
return pic
|
39 |
+
return super().__call__(pic)
|
40 |
+
|
41 |
+
|
42 |
+
# Use timm's names
|
43 |
+
IMAGENET_DEFAULT_MEAN = (0.485, 0.456, 0.406)
|
44 |
+
IMAGENET_DEFAULT_STD = (0.229, 0.224, 0.225)
|
45 |
+
|
46 |
+
|
47 |
+
def make_normalize_transform(
|
48 |
+
mean: Sequence[float] = IMAGENET_DEFAULT_MEAN,
|
49 |
+
std: Sequence[float] = IMAGENET_DEFAULT_STD,
|
50 |
+
) -> transforms.Normalize:
|
51 |
+
return transforms.Normalize(mean=mean, std=std)
|
52 |
+
|
53 |
+
|
54 |
+
# This roughly matches torchvision's preset for classification training:
|
55 |
+
# https://github.com/pytorch/vision/blob/main/references/classification/presets.py#L6-L44
|
56 |
+
def make_classification_train_transform(
|
57 |
+
*,
|
58 |
+
crop_size: int = 224,
|
59 |
+
interpolation=transforms.InterpolationMode.BICUBIC,
|
60 |
+
hflip_prob: float = 0.5,
|
61 |
+
mean: Sequence[float] = IMAGENET_DEFAULT_MEAN,
|
62 |
+
std: Sequence[float] = IMAGENET_DEFAULT_STD,
|
63 |
+
):
|
64 |
+
transforms_list = [transforms.RandomResizedCrop(crop_size, interpolation=interpolation)]
|
65 |
+
if hflip_prob > 0.0:
|
66 |
+
transforms_list.append(transforms.RandomHorizontalFlip(hflip_prob))
|
67 |
+
transforms_list.extend(
|
68 |
+
[
|
69 |
+
MaybeToTensor(),
|
70 |
+
make_normalize_transform(mean=mean, std=std),
|
71 |
+
]
|
72 |
+
)
|
73 |
+
return transforms.Compose(transforms_list)
|
74 |
+
|
75 |
+
|
76 |
+
# This matches (roughly) torchvision's preset for classification evaluation:
|
77 |
+
# https://github.com/pytorch/vision/blob/main/references/classification/presets.py#L47-L69
|
78 |
+
def make_classification_eval_transform(
|
79 |
+
*,
|
80 |
+
resize_size: int = 256,
|
81 |
+
interpolation=transforms.InterpolationMode.BICUBIC,
|
82 |
+
crop_size: int = 224,
|
83 |
+
mean: Sequence[float] = IMAGENET_DEFAULT_MEAN,
|
84 |
+
std: Sequence[float] = IMAGENET_DEFAULT_STD,
|
85 |
+
) -> transforms.Compose:
|
86 |
+
transforms_list = [
|
87 |
+
transforms.Resize(resize_size, interpolation=interpolation),
|
88 |
+
transforms.CenterCrop(crop_size),
|
89 |
+
MaybeToTensor(),
|
90 |
+
make_normalize_transform(mean=mean, std=std),
|
91 |
+
]
|
92 |
+
return transforms.Compose(transforms_list)
|
torchhub/facebookresearch_dinov2_main/dinov2/distributed/__init__.py
ADDED
@@ -0,0 +1,271 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
import os
|
8 |
+
import random
|
9 |
+
import re
|
10 |
+
import socket
|
11 |
+
from typing import Dict, List
|
12 |
+
|
13 |
+
import torch
|
14 |
+
import torch.distributed as dist
|
15 |
+
|
16 |
+
_LOCAL_RANK = -1
|
17 |
+
_LOCAL_WORLD_SIZE = -1
|
18 |
+
|
19 |
+
|
20 |
+
def is_enabled() -> bool:
|
21 |
+
"""
|
22 |
+
Returns:
|
23 |
+
True if distributed training is enabled
|
24 |
+
"""
|
25 |
+
return dist.is_available() and dist.is_initialized()
|
26 |
+
|
27 |
+
|
28 |
+
def get_global_size() -> int:
|
29 |
+
"""
|
30 |
+
Returns:
|
31 |
+
The number of processes in the process group
|
32 |
+
"""
|
33 |
+
return dist.get_world_size() if is_enabled() else 1
|
34 |
+
|
35 |
+
|
36 |
+
def get_global_rank() -> int:
|
37 |
+
"""
|
38 |
+
Returns:
|
39 |
+
The rank of the current process within the global process group.
|
40 |
+
"""
|
41 |
+
return dist.get_rank() if is_enabled() else 0
|
42 |
+
|
43 |
+
|
44 |
+
def get_local_rank() -> int:
|
45 |
+
"""
|
46 |
+
Returns:
|
47 |
+
The rank of the current process within the local (per-machine) process group.
|
48 |
+
"""
|
49 |
+
if not is_enabled():
|
50 |
+
return 0
|
51 |
+
assert 0 <= _LOCAL_RANK < _LOCAL_WORLD_SIZE
|
52 |
+
return _LOCAL_RANK
|
53 |
+
|
54 |
+
|
55 |
+
def get_local_size() -> int:
|
56 |
+
"""
|
57 |
+
Returns:
|
58 |
+
The size of the per-machine process group,
|
59 |
+
i.e. the number of processes per machine.
|
60 |
+
"""
|
61 |
+
if not is_enabled():
|
62 |
+
return 1
|
63 |
+
assert 0 <= _LOCAL_RANK < _LOCAL_WORLD_SIZE
|
64 |
+
return _LOCAL_WORLD_SIZE
|
65 |
+
|
66 |
+
|
67 |
+
def is_main_process() -> bool:
|
68 |
+
"""
|
69 |
+
Returns:
|
70 |
+
True if the current process is the main one.
|
71 |
+
"""
|
72 |
+
return get_global_rank() == 0
|
73 |
+
|
74 |
+
|
75 |
+
def _restrict_print_to_main_process() -> None:
|
76 |
+
"""
|
77 |
+
This function disables printing when not in the main process
|
78 |
+
"""
|
79 |
+
import builtins as __builtin__
|
80 |
+
|
81 |
+
builtin_print = __builtin__.print
|
82 |
+
|
83 |
+
def print(*args, **kwargs):
|
84 |
+
force = kwargs.pop("force", False)
|
85 |
+
if is_main_process() or force:
|
86 |
+
builtin_print(*args, **kwargs)
|
87 |
+
|
88 |
+
__builtin__.print = print
|
89 |
+
|
90 |
+
|
91 |
+
def _get_master_port(seed: int = 0) -> int:
|
92 |
+
MIN_MASTER_PORT, MAX_MASTER_PORT = (20_000, 60_000)
|
93 |
+
|
94 |
+
master_port_str = os.environ.get("MASTER_PORT")
|
95 |
+
if master_port_str is None:
|
96 |
+
rng = random.Random(seed)
|
97 |
+
return rng.randint(MIN_MASTER_PORT, MAX_MASTER_PORT)
|
98 |
+
|
99 |
+
return int(master_port_str)
|
100 |
+
|
101 |
+
|
102 |
+
def _get_available_port() -> int:
|
103 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
104 |
+
# A "" host address means INADDR_ANY i.e. binding to all interfaces.
|
105 |
+
# Note this is not compatible with IPv6.
|
106 |
+
s.bind(("", 0))
|
107 |
+
port = s.getsockname()[1]
|
108 |
+
return port
|
109 |
+
|
110 |
+
|
111 |
+
_TORCH_DISTRIBUTED_ENV_VARS = (
|
112 |
+
"MASTER_ADDR",
|
113 |
+
"MASTER_PORT",
|
114 |
+
"RANK",
|
115 |
+
"WORLD_SIZE",
|
116 |
+
"LOCAL_RANK",
|
117 |
+
"LOCAL_WORLD_SIZE",
|
118 |
+
)
|
119 |
+
|
120 |
+
|
121 |
+
def _collect_env_vars() -> Dict[str, str]:
|
122 |
+
return {env_var: os.environ[env_var] for env_var in _TORCH_DISTRIBUTED_ENV_VARS if env_var in os.environ}
|
123 |
+
|
124 |
+
|
125 |
+
def _is_slurm_job_process() -> bool:
|
126 |
+
return "SLURM_JOB_ID" in os.environ
|
127 |
+
|
128 |
+
|
129 |
+
def _parse_slurm_node_list(s: str) -> List[str]:
|
130 |
+
nodes = []
|
131 |
+
# Extract "hostname", "hostname[1-2,3,4-5]," substrings
|
132 |
+
p = re.compile(r"(([^\[]+)(?:\[([^\]]+)\])?),?")
|
133 |
+
for m in p.finditer(s):
|
134 |
+
prefix, suffixes = s[m.start(2) : m.end(2)], s[m.start(3) : m.end(3)]
|
135 |
+
for suffix in suffixes.split(","):
|
136 |
+
span = suffix.split("-")
|
137 |
+
if len(span) == 1:
|
138 |
+
nodes.append(prefix + suffix)
|
139 |
+
else:
|
140 |
+
width = len(span[0])
|
141 |
+
start, end = int(span[0]), int(span[1]) + 1
|
142 |
+
nodes.extend([prefix + f"{i:0{width}}" for i in range(start, end)])
|
143 |
+
return nodes
|
144 |
+
|
145 |
+
|
146 |
+
def _check_env_variable(key: str, new_value: str):
|
147 |
+
# Only check for difference with preset environment variables
|
148 |
+
if key in os.environ and os.environ[key] != new_value:
|
149 |
+
raise RuntimeError(f"Cannot export environment variables as {key} is already set")
|
150 |
+
|
151 |
+
|
152 |
+
class _TorchDistributedEnvironment:
|
153 |
+
def __init__(self):
|
154 |
+
self.master_addr = "127.0.0.1"
|
155 |
+
self.master_port = 0
|
156 |
+
self.rank = -1
|
157 |
+
self.world_size = -1
|
158 |
+
self.local_rank = -1
|
159 |
+
self.local_world_size = -1
|
160 |
+
|
161 |
+
if _is_slurm_job_process():
|
162 |
+
return self._set_from_slurm_env()
|
163 |
+
|
164 |
+
env_vars = _collect_env_vars()
|
165 |
+
if not env_vars:
|
166 |
+
# Environment is not set
|
167 |
+
pass
|
168 |
+
elif len(env_vars) == len(_TORCH_DISTRIBUTED_ENV_VARS):
|
169 |
+
# Environment is fully set
|
170 |
+
return self._set_from_preset_env()
|
171 |
+
else:
|
172 |
+
# Environment is partially set
|
173 |
+
collected_env_vars = ", ".join(env_vars.keys())
|
174 |
+
raise RuntimeError(f"Partially set environment: {collected_env_vars}")
|
175 |
+
|
176 |
+
if torch.cuda.device_count() > 0:
|
177 |
+
return self._set_from_local()
|
178 |
+
|
179 |
+
raise RuntimeError("Can't initialize PyTorch distributed environment")
|
180 |
+
|
181 |
+
# Slurm job created with sbatch, submitit, etc...
|
182 |
+
def _set_from_slurm_env(self):
|
183 |
+
# logger.info("Initialization from Slurm environment")
|
184 |
+
job_id = int(os.environ["SLURM_JOB_ID"])
|
185 |
+
node_count = int(os.environ["SLURM_JOB_NUM_NODES"])
|
186 |
+
nodes = _parse_slurm_node_list(os.environ["SLURM_JOB_NODELIST"])
|
187 |
+
assert len(nodes) == node_count
|
188 |
+
|
189 |
+
self.master_addr = nodes[0]
|
190 |
+
self.master_port = _get_master_port(seed=job_id)
|
191 |
+
self.rank = int(os.environ["SLURM_PROCID"])
|
192 |
+
self.world_size = int(os.environ["SLURM_NTASKS"])
|
193 |
+
assert self.rank < self.world_size
|
194 |
+
self.local_rank = int(os.environ["SLURM_LOCALID"])
|
195 |
+
self.local_world_size = self.world_size // node_count
|
196 |
+
assert self.local_rank < self.local_world_size
|
197 |
+
|
198 |
+
# Single node job with preset environment (i.e. torchrun)
|
199 |
+
def _set_from_preset_env(self):
|
200 |
+
# logger.info("Initialization from preset environment")
|
201 |
+
self.master_addr = os.environ["MASTER_ADDR"]
|
202 |
+
self.master_port = os.environ["MASTER_PORT"]
|
203 |
+
self.rank = int(os.environ["RANK"])
|
204 |
+
self.world_size = int(os.environ["WORLD_SIZE"])
|
205 |
+
assert self.rank < self.world_size
|
206 |
+
self.local_rank = int(os.environ["LOCAL_RANK"])
|
207 |
+
self.local_world_size = int(os.environ["LOCAL_WORLD_SIZE"])
|
208 |
+
assert self.local_rank < self.local_world_size
|
209 |
+
|
210 |
+
# Single node and GPU job (i.e. local script run)
|
211 |
+
def _set_from_local(self):
|
212 |
+
# logger.info("Initialization from local")
|
213 |
+
self.master_addr = "127.0.0.1"
|
214 |
+
self.master_port = _get_available_port()
|
215 |
+
self.rank = 0
|
216 |
+
self.world_size = 1
|
217 |
+
self.local_rank = 0
|
218 |
+
self.local_world_size = 1
|
219 |
+
|
220 |
+
def export(self, *, overwrite: bool) -> "_TorchDistributedEnvironment":
|
221 |
+
# See the "Environment variable initialization" section from
|
222 |
+
# https://pytorch.org/docs/stable/distributed.html for the complete list of
|
223 |
+
# environment variables required for the env:// initialization method.
|
224 |
+
env_vars = {
|
225 |
+
"MASTER_ADDR": self.master_addr,
|
226 |
+
"MASTER_PORT": str(self.master_port),
|
227 |
+
"RANK": str(self.rank),
|
228 |
+
"WORLD_SIZE": str(self.world_size),
|
229 |
+
"LOCAL_RANK": str(self.local_rank),
|
230 |
+
"LOCAL_WORLD_SIZE": str(self.local_world_size),
|
231 |
+
}
|
232 |
+
if not overwrite:
|
233 |
+
for k, v in env_vars.items():
|
234 |
+
_check_env_variable(k, v)
|
235 |
+
|
236 |
+
os.environ.update(env_vars)
|
237 |
+
return self
|
238 |
+
|
239 |
+
|
240 |
+
def enable(*, set_cuda_current_device: bool = True, overwrite: bool = False, allow_nccl_timeout: bool = False):
|
241 |
+
"""Enable distributed mode
|
242 |
+
|
243 |
+
Args:
|
244 |
+
set_cuda_current_device: If True, call torch.cuda.set_device() to set the
|
245 |
+
current PyTorch CUDA device to the one matching the local rank.
|
246 |
+
overwrite: If True, overwrites already set variables. Else fails.
|
247 |
+
"""
|
248 |
+
|
249 |
+
global _LOCAL_RANK, _LOCAL_WORLD_SIZE
|
250 |
+
if _LOCAL_RANK >= 0 or _LOCAL_WORLD_SIZE >= 0:
|
251 |
+
raise RuntimeError("Distributed mode has already been enabled")
|
252 |
+
torch_env = _TorchDistributedEnvironment()
|
253 |
+
torch_env.export(overwrite=overwrite)
|
254 |
+
|
255 |
+
if set_cuda_current_device:
|
256 |
+
torch.cuda.set_device(torch_env.local_rank)
|
257 |
+
|
258 |
+
if allow_nccl_timeout:
|
259 |
+
# This allows to use torch distributed timeout in a NCCL backend
|
260 |
+
key, value = "NCCL_ASYNC_ERROR_HANDLING", "1"
|
261 |
+
if not overwrite:
|
262 |
+
_check_env_variable(key, value)
|
263 |
+
os.environ[key] = value
|
264 |
+
|
265 |
+
dist.init_process_group(backend="nccl")
|
266 |
+
dist.barrier()
|
267 |
+
|
268 |
+
# Finalize setup
|
269 |
+
_LOCAL_RANK = torch_env.local_rank
|
270 |
+
_LOCAL_WORLD_SIZE = torch_env.local_world_size
|
271 |
+
_restrict_print_to_main_process()
|