first commit
This commit is contained in:
196
midas/backbones/beit.py
Normal file
196
midas/backbones/beit.py
Normal file
@@ -0,0 +1,196 @@
|
||||
import timm
|
||||
import torch
|
||||
import types
|
||||
|
||||
import numpy as np
|
||||
import torch.nn.functional as F
|
||||
|
||||
from .utils import forward_adapted_unflatten, make_backbone_default
|
||||
from timm.models.beit import gen_relative_position_index
|
||||
from torch.utils.checkpoint import checkpoint
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def forward_beit(pretrained, x):
|
||||
return forward_adapted_unflatten(pretrained, x, "forward_features")
|
||||
|
||||
|
||||
def patch_embed_forward(self, x):
|
||||
"""
|
||||
Modification of timm.models.layers.patch_embed.py: PatchEmbed.forward to support arbitrary window sizes.
|
||||
"""
|
||||
x = self.proj(x)
|
||||
if self.flatten:
|
||||
x = x.flatten(2).transpose(1, 2)
|
||||
x = self.norm(x)
|
||||
return x
|
||||
|
||||
|
||||
def _get_rel_pos_bias(self, window_size):
|
||||
"""
|
||||
Modification of timm.models.beit.py: Attention._get_rel_pos_bias to support arbitrary window sizes.
|
||||
"""
|
||||
old_height = 2 * self.window_size[0] - 1
|
||||
old_width = 2 * self.window_size[1] - 1
|
||||
|
||||
new_height = 2 * window_size[0] - 1
|
||||
new_width = 2 * window_size[1] - 1
|
||||
|
||||
old_relative_position_bias_table = self.relative_position_bias_table
|
||||
|
||||
old_num_relative_distance = self.num_relative_distance
|
||||
new_num_relative_distance = new_height * new_width + 3
|
||||
|
||||
old_sub_table = old_relative_position_bias_table[:old_num_relative_distance - 3]
|
||||
|
||||
old_sub_table = old_sub_table.reshape(1, old_width, old_height, -1).permute(0, 3, 1, 2)
|
||||
new_sub_table = F.interpolate(old_sub_table, size=(new_height, new_width), mode="bilinear")
|
||||
new_sub_table = new_sub_table.permute(0, 2, 3, 1).reshape(new_num_relative_distance - 3, -1)
|
||||
|
||||
new_relative_position_bias_table = torch.cat(
|
||||
[new_sub_table, old_relative_position_bias_table[old_num_relative_distance - 3:]])
|
||||
|
||||
key = str(window_size[1]) + "," + str(window_size[0])
|
||||
if key not in self.relative_position_indices.keys():
|
||||
self.relative_position_indices[key] = gen_relative_position_index(window_size)
|
||||
|
||||
relative_position_bias = new_relative_position_bias_table[
|
||||
self.relative_position_indices[key].view(-1)].view(
|
||||
window_size[0] * window_size[1] + 1,
|
||||
window_size[0] * window_size[1] + 1, -1) # Wh*Ww,Wh*Ww,nH
|
||||
relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww
|
||||
return relative_position_bias.unsqueeze(0)
|
||||
|
||||
|
||||
def attention_forward(self, x, resolution, shared_rel_pos_bias: Optional[torch.Tensor] = None):
|
||||
"""
|
||||
Modification of timm.models.beit.py: Attention.forward to support arbitrary window sizes.
|
||||
"""
|
||||
B, N, C = x.shape
|
||||
|
||||
qkv_bias = torch.cat((self.q_bias, self.k_bias, self.v_bias)) if self.q_bias is not None else None
|
||||
qkv = F.linear(input=x, weight=self.qkv.weight, bias=qkv_bias)
|
||||
qkv = qkv.reshape(B, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4)
|
||||
q, k, v = qkv.unbind(0) # make torchscript happy (cannot use tensor as tuple)
|
||||
|
||||
q = q * self.scale
|
||||
attn = (q @ k.transpose(-2, -1))
|
||||
|
||||
if self.relative_position_bias_table is not None:
|
||||
window_size = tuple(np.array(resolution) // 16)
|
||||
attn = attn + self._get_rel_pos_bias(window_size)
|
||||
if shared_rel_pos_bias is not None:
|
||||
attn = attn + shared_rel_pos_bias
|
||||
|
||||
attn = attn.softmax(dim=-1)
|
||||
attn = self.attn_drop(attn)
|
||||
|
||||
x = (attn @ v).transpose(1, 2).reshape(B, N, -1)
|
||||
x = self.proj(x)
|
||||
x = self.proj_drop(x)
|
||||
return x
|
||||
|
||||
|
||||
def block_forward(self, x, resolution, shared_rel_pos_bias: Optional[torch.Tensor] = None):
|
||||
"""
|
||||
Modification of timm.models.beit.py: Block.forward to support arbitrary window sizes.
|
||||
"""
|
||||
if self.gamma_1 is None:
|
||||
x = x + self.drop_path(self.attn(self.norm1(x), resolution, shared_rel_pos_bias=shared_rel_pos_bias))
|
||||
x = x + self.drop_path(self.mlp(self.norm2(x)))
|
||||
else:
|
||||
x = x + self.drop_path(self.gamma_1 * self.attn(self.norm1(x), resolution,
|
||||
shared_rel_pos_bias=shared_rel_pos_bias))
|
||||
x = x + self.drop_path(self.gamma_2 * self.mlp(self.norm2(x)))
|
||||
return x
|
||||
|
||||
|
||||
def beit_forward_features(self, x):
|
||||
"""
|
||||
Modification of timm.models.beit.py: Beit.forward_features to support arbitrary window sizes.
|
||||
"""
|
||||
resolution = x.shape[2:]
|
||||
|
||||
x = self.patch_embed(x)
|
||||
x = torch.cat((self.cls_token.expand(x.shape[0], -1, -1), x), dim=1)
|
||||
if self.pos_embed is not None:
|
||||
x = x + self.pos_embed
|
||||
x = self.pos_drop(x)
|
||||
|
||||
rel_pos_bias = self.rel_pos_bias() if self.rel_pos_bias is not None else None
|
||||
for blk in self.blocks:
|
||||
if self.grad_checkpointing and not torch.jit.is_scripting():
|
||||
x = checkpoint(blk, x, shared_rel_pos_bias=rel_pos_bias)
|
||||
else:
|
||||
x = blk(x, resolution, shared_rel_pos_bias=rel_pos_bias)
|
||||
x = self.norm(x)
|
||||
return x
|
||||
|
||||
|
||||
def _make_beit_backbone(
|
||||
model,
|
||||
features=[96, 192, 384, 768],
|
||||
size=[384, 384],
|
||||
hooks=[0, 4, 8, 11],
|
||||
vit_features=768,
|
||||
use_readout="ignore",
|
||||
start_index=1,
|
||||
start_index_readout=1,
|
||||
):
|
||||
backbone = make_backbone_default(model, features, size, hooks, vit_features, use_readout, start_index,
|
||||
start_index_readout)
|
||||
|
||||
backbone.model.patch_embed.forward = types.MethodType(patch_embed_forward, backbone.model.patch_embed)
|
||||
backbone.model.forward_features = types.MethodType(beit_forward_features, backbone.model)
|
||||
|
||||
for block in backbone.model.blocks:
|
||||
attn = block.attn
|
||||
attn._get_rel_pos_bias = types.MethodType(_get_rel_pos_bias, attn)
|
||||
attn.forward = types.MethodType(attention_forward, attn)
|
||||
attn.relative_position_indices = {}
|
||||
|
||||
block.forward = types.MethodType(block_forward, block)
|
||||
|
||||
return backbone
|
||||
|
||||
|
||||
def _make_pretrained_beitl16_512(pretrained, use_readout="ignore", hooks=None):
|
||||
model = timm.create_model("beit_large_patch16_512", pretrained=pretrained)
|
||||
|
||||
hooks = [5, 11, 17, 23] if hooks is None else hooks
|
||||
|
||||
features = [256, 512, 1024, 1024]
|
||||
|
||||
return _make_beit_backbone(
|
||||
model,
|
||||
features=features,
|
||||
size=[512, 512],
|
||||
hooks=hooks,
|
||||
vit_features=1024,
|
||||
use_readout=use_readout,
|
||||
)
|
||||
|
||||
|
||||
def _make_pretrained_beitl16_384(pretrained, use_readout="ignore", hooks=None):
|
||||
model = timm.create_model("beit_large_patch16_384", pretrained=pretrained)
|
||||
|
||||
hooks = [5, 11, 17, 23] if hooks is None else hooks
|
||||
return _make_beit_backbone(
|
||||
model,
|
||||
features=[256, 512, 1024, 1024],
|
||||
hooks=hooks,
|
||||
vit_features=1024,
|
||||
use_readout=use_readout,
|
||||
)
|
||||
|
||||
|
||||
def _make_pretrained_beitb16_384(pretrained, use_readout="ignore", hooks=None):
|
||||
model = timm.create_model("beit_base_patch16_384", pretrained=pretrained)
|
||||
|
||||
hooks = [2, 5, 8, 11] if hooks is None else hooks
|
||||
return _make_beit_backbone(
|
||||
model,
|
||||
features=[96, 192, 384, 768],
|
||||
hooks=hooks,
|
||||
use_readout=use_readout,
|
||||
)
|
||||
106
midas/backbones/levit.py
Normal file
106
midas/backbones/levit.py
Normal file
@@ -0,0 +1,106 @@
|
||||
import timm
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import numpy as np
|
||||
|
||||
from .utils import activations, get_activation, Transpose
|
||||
|
||||
|
||||
def forward_levit(pretrained, x):
|
||||
pretrained.model.forward_features(x)
|
||||
|
||||
layer_1 = pretrained.activations["1"]
|
||||
layer_2 = pretrained.activations["2"]
|
||||
layer_3 = pretrained.activations["3"]
|
||||
|
||||
layer_1 = pretrained.act_postprocess1(layer_1)
|
||||
layer_2 = pretrained.act_postprocess2(layer_2)
|
||||
layer_3 = pretrained.act_postprocess3(layer_3)
|
||||
|
||||
return layer_1, layer_2, layer_3
|
||||
|
||||
|
||||
def _make_levit_backbone(
|
||||
model,
|
||||
hooks=[3, 11, 21],
|
||||
patch_grid=[14, 14]
|
||||
):
|
||||
pretrained = nn.Module()
|
||||
|
||||
pretrained.model = model
|
||||
pretrained.model.blocks[hooks[0]].register_forward_hook(get_activation("1"))
|
||||
pretrained.model.blocks[hooks[1]].register_forward_hook(get_activation("2"))
|
||||
pretrained.model.blocks[hooks[2]].register_forward_hook(get_activation("3"))
|
||||
|
||||
pretrained.activations = activations
|
||||
|
||||
patch_grid_size = np.array(patch_grid, dtype=int)
|
||||
|
||||
pretrained.act_postprocess1 = nn.Sequential(
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size(patch_grid_size.tolist()))
|
||||
)
|
||||
pretrained.act_postprocess2 = nn.Sequential(
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size((np.ceil(patch_grid_size / 2).astype(int)).tolist()))
|
||||
)
|
||||
pretrained.act_postprocess3 = nn.Sequential(
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size((np.ceil(patch_grid_size / 4).astype(int)).tolist()))
|
||||
)
|
||||
|
||||
return pretrained
|
||||
|
||||
|
||||
class ConvTransposeNorm(nn.Sequential):
|
||||
"""
|
||||
Modification of
|
||||
https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/levit.py: ConvNorm
|
||||
such that ConvTranspose2d is used instead of Conv2d.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, in_chs, out_chs, kernel_size=1, stride=1, pad=0, dilation=1,
|
||||
groups=1, bn_weight_init=1):
|
||||
super().__init__()
|
||||
self.add_module('c',
|
||||
nn.ConvTranspose2d(in_chs, out_chs, kernel_size, stride, pad, dilation, groups, bias=False))
|
||||
self.add_module('bn', nn.BatchNorm2d(out_chs))
|
||||
|
||||
nn.init.constant_(self.bn.weight, bn_weight_init)
|
||||
|
||||
@torch.no_grad()
|
||||
def fuse(self):
|
||||
c, bn = self._modules.values()
|
||||
w = bn.weight / (bn.running_var + bn.eps) ** 0.5
|
||||
w = c.weight * w[:, None, None, None]
|
||||
b = bn.bias - bn.running_mean * bn.weight / (bn.running_var + bn.eps) ** 0.5
|
||||
m = nn.ConvTranspose2d(
|
||||
w.size(1), w.size(0), w.shape[2:], stride=self.c.stride,
|
||||
padding=self.c.padding, dilation=self.c.dilation, groups=self.c.groups)
|
||||
m.weight.data.copy_(w)
|
||||
m.bias.data.copy_(b)
|
||||
return m
|
||||
|
||||
|
||||
def stem_b4_transpose(in_chs, out_chs, activation):
|
||||
"""
|
||||
Modification of
|
||||
https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/levit.py: stem_b16
|
||||
such that ConvTranspose2d is used instead of Conv2d and stem is also reduced to the half.
|
||||
"""
|
||||
return nn.Sequential(
|
||||
ConvTransposeNorm(in_chs, out_chs, 3, 2, 1),
|
||||
activation(),
|
||||
ConvTransposeNorm(out_chs, out_chs // 2, 3, 2, 1),
|
||||
activation())
|
||||
|
||||
|
||||
def _make_pretrained_levit_384(pretrained, hooks=None):
|
||||
model = timm.create_model("levit_384", pretrained=pretrained)
|
||||
|
||||
hooks = [3, 11, 21] if hooks == None else hooks
|
||||
return _make_levit_backbone(
|
||||
model,
|
||||
hooks=hooks
|
||||
)
|
||||
39
midas/backbones/next_vit.py
Normal file
39
midas/backbones/next_vit.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import timm
|
||||
|
||||
import torch.nn as nn
|
||||
|
||||
from pathlib import Path
|
||||
from .utils import activations, forward_default, get_activation
|
||||
|
||||
from ..external.next_vit.classification.nextvit import *
|
||||
|
||||
|
||||
def forward_next_vit(pretrained, x):
|
||||
return forward_default(pretrained, x, "forward")
|
||||
|
||||
|
||||
def _make_next_vit_backbone(
|
||||
model,
|
||||
hooks=[2, 6, 36, 39],
|
||||
):
|
||||
pretrained = nn.Module()
|
||||
|
||||
pretrained.model = model
|
||||
pretrained.model.features[hooks[0]].register_forward_hook(get_activation("1"))
|
||||
pretrained.model.features[hooks[1]].register_forward_hook(get_activation("2"))
|
||||
pretrained.model.features[hooks[2]].register_forward_hook(get_activation("3"))
|
||||
pretrained.model.features[hooks[3]].register_forward_hook(get_activation("4"))
|
||||
|
||||
pretrained.activations = activations
|
||||
|
||||
return pretrained
|
||||
|
||||
|
||||
def _make_pretrained_next_vit_large_6m(hooks=None):
|
||||
model = timm.create_model("nextvit_large")
|
||||
|
||||
hooks = [2, 6, 36, 39] if hooks == None else hooks
|
||||
return _make_next_vit_backbone(
|
||||
model,
|
||||
hooks=hooks,
|
||||
)
|
||||
13
midas/backbones/swin.py
Normal file
13
midas/backbones/swin.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import timm
|
||||
|
||||
from .swin_common import _make_swin_backbone
|
||||
|
||||
|
||||
def _make_pretrained_swinl12_384(pretrained, hooks=None):
|
||||
model = timm.create_model("swin_large_patch4_window12_384", pretrained=pretrained)
|
||||
|
||||
hooks = [1, 1, 17, 1] if hooks == None else hooks
|
||||
return _make_swin_backbone(
|
||||
model,
|
||||
hooks=hooks
|
||||
)
|
||||
34
midas/backbones/swin2.py
Normal file
34
midas/backbones/swin2.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import timm
|
||||
|
||||
from .swin_common import _make_swin_backbone
|
||||
|
||||
|
||||
def _make_pretrained_swin2l24_384(pretrained, hooks=None):
|
||||
model = timm.create_model("swinv2_large_window12to24_192to384_22kft1k", pretrained=pretrained)
|
||||
|
||||
hooks = [1, 1, 17, 1] if hooks == None else hooks
|
||||
return _make_swin_backbone(
|
||||
model,
|
||||
hooks=hooks
|
||||
)
|
||||
|
||||
|
||||
def _make_pretrained_swin2b24_384(pretrained, hooks=None):
|
||||
model = timm.create_model("swinv2_base_window12to24_192to384_22kft1k", pretrained=pretrained)
|
||||
|
||||
hooks = [1, 1, 17, 1] if hooks == None else hooks
|
||||
return _make_swin_backbone(
|
||||
model,
|
||||
hooks=hooks
|
||||
)
|
||||
|
||||
|
||||
def _make_pretrained_swin2t16_256(pretrained, hooks=None):
|
||||
model = timm.create_model("swinv2_tiny_window16_256", pretrained=pretrained)
|
||||
|
||||
hooks = [1, 1, 5, 1] if hooks == None else hooks
|
||||
return _make_swin_backbone(
|
||||
model,
|
||||
hooks=hooks,
|
||||
patch_grid=[64, 64]
|
||||
)
|
||||
52
midas/backbones/swin_common.py
Normal file
52
midas/backbones/swin_common.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import torch
|
||||
|
||||
import torch.nn as nn
|
||||
import numpy as np
|
||||
|
||||
from .utils import activations, forward_default, get_activation, Transpose
|
||||
|
||||
|
||||
def forward_swin(pretrained, x):
|
||||
return forward_default(pretrained, x)
|
||||
|
||||
|
||||
def _make_swin_backbone(
|
||||
model,
|
||||
hooks=[1, 1, 17, 1],
|
||||
patch_grid=[96, 96]
|
||||
):
|
||||
pretrained = nn.Module()
|
||||
|
||||
pretrained.model = model
|
||||
pretrained.model.layers[0].blocks[hooks[0]].register_forward_hook(get_activation("1"))
|
||||
pretrained.model.layers[1].blocks[hooks[1]].register_forward_hook(get_activation("2"))
|
||||
pretrained.model.layers[2].blocks[hooks[2]].register_forward_hook(get_activation("3"))
|
||||
pretrained.model.layers[3].blocks[hooks[3]].register_forward_hook(get_activation("4"))
|
||||
|
||||
pretrained.activations = activations
|
||||
|
||||
if hasattr(model, "patch_grid"):
|
||||
used_patch_grid = model.patch_grid
|
||||
else:
|
||||
used_patch_grid = patch_grid
|
||||
|
||||
patch_grid_size = np.array(used_patch_grid, dtype=int)
|
||||
|
||||
pretrained.act_postprocess1 = nn.Sequential(
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size(patch_grid_size.tolist()))
|
||||
)
|
||||
pretrained.act_postprocess2 = nn.Sequential(
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size((patch_grid_size // 2).tolist()))
|
||||
)
|
||||
pretrained.act_postprocess3 = nn.Sequential(
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size((patch_grid_size // 4).tolist()))
|
||||
)
|
||||
pretrained.act_postprocess4 = nn.Sequential(
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size((patch_grid_size // 8).tolist()))
|
||||
)
|
||||
|
||||
return pretrained
|
||||
249
midas/backbones/utils.py
Normal file
249
midas/backbones/utils.py
Normal file
@@ -0,0 +1,249 @@
|
||||
import torch
|
||||
|
||||
import torch.nn as nn
|
||||
|
||||
|
||||
class Slice(nn.Module):
|
||||
def __init__(self, start_index=1):
|
||||
super(Slice, self).__init__()
|
||||
self.start_index = start_index
|
||||
|
||||
def forward(self, x):
|
||||
return x[:, self.start_index:]
|
||||
|
||||
|
||||
class AddReadout(nn.Module):
|
||||
def __init__(self, start_index=1):
|
||||
super(AddReadout, self).__init__()
|
||||
self.start_index = start_index
|
||||
|
||||
def forward(self, x):
|
||||
if self.start_index == 2:
|
||||
readout = (x[:, 0] + x[:, 1]) / 2
|
||||
else:
|
||||
readout = x[:, 0]
|
||||
return x[:, self.start_index:] + readout.unsqueeze(1)
|
||||
|
||||
|
||||
class ProjectReadout(nn.Module):
|
||||
def __init__(self, in_features, start_index=1):
|
||||
super(ProjectReadout, self).__init__()
|
||||
self.start_index = start_index
|
||||
|
||||
self.project = nn.Sequential(nn.Linear(2 * in_features, in_features), nn.GELU())
|
||||
|
||||
def forward(self, x):
|
||||
readout = x[:, 0].unsqueeze(1).expand_as(x[:, self.start_index:])
|
||||
features = torch.cat((x[:, self.start_index:], readout), -1)
|
||||
|
||||
return self.project(features)
|
||||
|
||||
|
||||
class Transpose(nn.Module):
|
||||
def __init__(self, dim0, dim1):
|
||||
super(Transpose, self).__init__()
|
||||
self.dim0 = dim0
|
||||
self.dim1 = dim1
|
||||
|
||||
def forward(self, x):
|
||||
x = x.transpose(self.dim0, self.dim1)
|
||||
return x
|
||||
|
||||
|
||||
activations = {}
|
||||
|
||||
|
||||
def get_activation(name):
|
||||
def hook(model, input, output):
|
||||
activations[name] = output
|
||||
|
||||
return hook
|
||||
|
||||
|
||||
def forward_default(pretrained, x, function_name="forward_features"):
|
||||
exec(f"pretrained.model.{function_name}(x)")
|
||||
|
||||
layer_1 = pretrained.activations["1"]
|
||||
layer_2 = pretrained.activations["2"]
|
||||
layer_3 = pretrained.activations["3"]
|
||||
layer_4 = pretrained.activations["4"]
|
||||
|
||||
if hasattr(pretrained, "act_postprocess1"):
|
||||
layer_1 = pretrained.act_postprocess1(layer_1)
|
||||
if hasattr(pretrained, "act_postprocess2"):
|
||||
layer_2 = pretrained.act_postprocess2(layer_2)
|
||||
if hasattr(pretrained, "act_postprocess3"):
|
||||
layer_3 = pretrained.act_postprocess3(layer_3)
|
||||
if hasattr(pretrained, "act_postprocess4"):
|
||||
layer_4 = pretrained.act_postprocess4(layer_4)
|
||||
|
||||
return layer_1, layer_2, layer_3, layer_4
|
||||
|
||||
|
||||
def forward_adapted_unflatten(pretrained, x, function_name="forward_features"):
|
||||
b, c, h, w = x.shape
|
||||
|
||||
exec(f"glob = pretrained.model.{function_name}(x)")
|
||||
|
||||
layer_1 = pretrained.activations["1"]
|
||||
layer_2 = pretrained.activations["2"]
|
||||
layer_3 = pretrained.activations["3"]
|
||||
layer_4 = pretrained.activations["4"]
|
||||
|
||||
layer_1 = pretrained.act_postprocess1[0:2](layer_1)
|
||||
layer_2 = pretrained.act_postprocess2[0:2](layer_2)
|
||||
layer_3 = pretrained.act_postprocess3[0:2](layer_3)
|
||||
layer_4 = pretrained.act_postprocess4[0:2](layer_4)
|
||||
|
||||
unflatten = nn.Sequential(
|
||||
nn.Unflatten(
|
||||
2,
|
||||
torch.Size(
|
||||
[
|
||||
h // pretrained.model.patch_size[1],
|
||||
w // pretrained.model.patch_size[0],
|
||||
]
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
if layer_1.ndim == 3:
|
||||
layer_1 = unflatten(layer_1)
|
||||
if layer_2.ndim == 3:
|
||||
layer_2 = unflatten(layer_2)
|
||||
if layer_3.ndim == 3:
|
||||
layer_3 = unflatten(layer_3)
|
||||
if layer_4.ndim == 3:
|
||||
layer_4 = unflatten(layer_4)
|
||||
|
||||
layer_1 = pretrained.act_postprocess1[3: len(pretrained.act_postprocess1)](layer_1)
|
||||
layer_2 = pretrained.act_postprocess2[3: len(pretrained.act_postprocess2)](layer_2)
|
||||
layer_3 = pretrained.act_postprocess3[3: len(pretrained.act_postprocess3)](layer_3)
|
||||
layer_4 = pretrained.act_postprocess4[3: len(pretrained.act_postprocess4)](layer_4)
|
||||
|
||||
return layer_1, layer_2, layer_3, layer_4
|
||||
|
||||
|
||||
def get_readout_oper(vit_features, features, use_readout, start_index=1):
|
||||
if use_readout == "ignore":
|
||||
readout_oper = [Slice(start_index)] * len(features)
|
||||
elif use_readout == "add":
|
||||
readout_oper = [AddReadout(start_index)] * len(features)
|
||||
elif use_readout == "project":
|
||||
readout_oper = [
|
||||
ProjectReadout(vit_features, start_index) for out_feat in features
|
||||
]
|
||||
else:
|
||||
assert (
|
||||
False
|
||||
), "wrong operation for readout token, use_readout can be 'ignore', 'add', or 'project'"
|
||||
|
||||
return readout_oper
|
||||
|
||||
|
||||
def make_backbone_default(
|
||||
model,
|
||||
features=[96, 192, 384, 768],
|
||||
size=[384, 384],
|
||||
hooks=[2, 5, 8, 11],
|
||||
vit_features=768,
|
||||
use_readout="ignore",
|
||||
start_index=1,
|
||||
start_index_readout=1,
|
||||
):
|
||||
pretrained = nn.Module()
|
||||
|
||||
pretrained.model = model
|
||||
pretrained.model.blocks[hooks[0]].register_forward_hook(get_activation("1"))
|
||||
pretrained.model.blocks[hooks[1]].register_forward_hook(get_activation("2"))
|
||||
pretrained.model.blocks[hooks[2]].register_forward_hook(get_activation("3"))
|
||||
pretrained.model.blocks[hooks[3]].register_forward_hook(get_activation("4"))
|
||||
|
||||
pretrained.activations = activations
|
||||
|
||||
readout_oper = get_readout_oper(vit_features, features, use_readout, start_index_readout)
|
||||
|
||||
# 32, 48, 136, 384
|
||||
pretrained.act_postprocess1 = nn.Sequential(
|
||||
readout_oper[0],
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size([size[0] // 16, size[1] // 16])),
|
||||
nn.Conv2d(
|
||||
in_channels=vit_features,
|
||||
out_channels=features[0],
|
||||
kernel_size=1,
|
||||
stride=1,
|
||||
padding=0,
|
||||
),
|
||||
nn.ConvTranspose2d(
|
||||
in_channels=features[0],
|
||||
out_channels=features[0],
|
||||
kernel_size=4,
|
||||
stride=4,
|
||||
padding=0,
|
||||
bias=True,
|
||||
dilation=1,
|
||||
groups=1,
|
||||
),
|
||||
)
|
||||
|
||||
pretrained.act_postprocess2 = nn.Sequential(
|
||||
readout_oper[1],
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size([size[0] // 16, size[1] // 16])),
|
||||
nn.Conv2d(
|
||||
in_channels=vit_features,
|
||||
out_channels=features[1],
|
||||
kernel_size=1,
|
||||
stride=1,
|
||||
padding=0,
|
||||
),
|
||||
nn.ConvTranspose2d(
|
||||
in_channels=features[1],
|
||||
out_channels=features[1],
|
||||
kernel_size=2,
|
||||
stride=2,
|
||||
padding=0,
|
||||
bias=True,
|
||||
dilation=1,
|
||||
groups=1,
|
||||
),
|
||||
)
|
||||
|
||||
pretrained.act_postprocess3 = nn.Sequential(
|
||||
readout_oper[2],
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size([size[0] // 16, size[1] // 16])),
|
||||
nn.Conv2d(
|
||||
in_channels=vit_features,
|
||||
out_channels=features[2],
|
||||
kernel_size=1,
|
||||
stride=1,
|
||||
padding=0,
|
||||
),
|
||||
)
|
||||
|
||||
pretrained.act_postprocess4 = nn.Sequential(
|
||||
readout_oper[3],
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size([size[0] // 16, size[1] // 16])),
|
||||
nn.Conv2d(
|
||||
in_channels=vit_features,
|
||||
out_channels=features[3],
|
||||
kernel_size=1,
|
||||
stride=1,
|
||||
padding=0,
|
||||
),
|
||||
nn.Conv2d(
|
||||
in_channels=features[3],
|
||||
out_channels=features[3],
|
||||
kernel_size=3,
|
||||
stride=2,
|
||||
padding=1,
|
||||
),
|
||||
)
|
||||
|
||||
pretrained.model.start_index = start_index
|
||||
pretrained.model.patch_size = [16, 16]
|
||||
|
||||
return pretrained
|
||||
221
midas/backbones/vit.py
Normal file
221
midas/backbones/vit.py
Normal file
@@ -0,0 +1,221 @@
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import timm
|
||||
import types
|
||||
import math
|
||||
import torch.nn.functional as F
|
||||
|
||||
from .utils import (activations, forward_adapted_unflatten, get_activation, get_readout_oper,
|
||||
make_backbone_default, Transpose)
|
||||
|
||||
|
||||
def forward_vit(pretrained, x):
|
||||
return forward_adapted_unflatten(pretrained, x, "forward_flex")
|
||||
|
||||
|
||||
def _resize_pos_embed(self, posemb, gs_h, gs_w):
|
||||
posemb_tok, posemb_grid = (
|
||||
posemb[:, : self.start_index],
|
||||
posemb[0, self.start_index:],
|
||||
)
|
||||
|
||||
gs_old = int(math.sqrt(len(posemb_grid)))
|
||||
|
||||
posemb_grid = posemb_grid.reshape(1, gs_old, gs_old, -1).permute(0, 3, 1, 2)
|
||||
posemb_grid = F.interpolate(posemb_grid, size=(gs_h, gs_w), mode="bilinear")
|
||||
posemb_grid = posemb_grid.permute(0, 2, 3, 1).reshape(1, gs_h * gs_w, -1)
|
||||
|
||||
posemb = torch.cat([posemb_tok, posemb_grid], dim=1)
|
||||
|
||||
return posemb
|
||||
|
||||
|
||||
def forward_flex(self, x):
|
||||
b, c, h, w = x.shape
|
||||
|
||||
pos_embed = self._resize_pos_embed(
|
||||
self.pos_embed, h // self.patch_size[1], w // self.patch_size[0]
|
||||
)
|
||||
|
||||
B = x.shape[0]
|
||||
|
||||
if hasattr(self.patch_embed, "backbone"):
|
||||
x = self.patch_embed.backbone(x)
|
||||
if isinstance(x, (list, tuple)):
|
||||
x = x[-1] # last feature if backbone outputs list/tuple of features
|
||||
|
||||
x = self.patch_embed.proj(x).flatten(2).transpose(1, 2)
|
||||
|
||||
if getattr(self, "dist_token", None) is not None:
|
||||
cls_tokens = self.cls_token.expand(
|
||||
B, -1, -1
|
||||
) # stole cls_tokens impl from Phil Wang, thanks
|
||||
dist_token = self.dist_token.expand(B, -1, -1)
|
||||
x = torch.cat((cls_tokens, dist_token, x), dim=1)
|
||||
else:
|
||||
if self.no_embed_class:
|
||||
x = x + pos_embed
|
||||
cls_tokens = self.cls_token.expand(
|
||||
B, -1, -1
|
||||
) # stole cls_tokens impl from Phil Wang, thanks
|
||||
x = torch.cat((cls_tokens, x), dim=1)
|
||||
|
||||
if not self.no_embed_class:
|
||||
x = x + pos_embed
|
||||
x = self.pos_drop(x)
|
||||
|
||||
for blk in self.blocks:
|
||||
x = blk(x)
|
||||
|
||||
x = self.norm(x)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
def _make_vit_b16_backbone(
|
||||
model,
|
||||
features=[96, 192, 384, 768],
|
||||
size=[384, 384],
|
||||
hooks=[2, 5, 8, 11],
|
||||
vit_features=768,
|
||||
use_readout="ignore",
|
||||
start_index=1,
|
||||
start_index_readout=1,
|
||||
):
|
||||
pretrained = make_backbone_default(model, features, size, hooks, vit_features, use_readout, start_index,
|
||||
start_index_readout)
|
||||
|
||||
# We inject this function into the VisionTransformer instances so that
|
||||
# we can use it with interpolated position embeddings without modifying the library source.
|
||||
pretrained.model.forward_flex = types.MethodType(forward_flex, pretrained.model)
|
||||
pretrained.model._resize_pos_embed = types.MethodType(
|
||||
_resize_pos_embed, pretrained.model
|
||||
)
|
||||
|
||||
return pretrained
|
||||
|
||||
|
||||
def _make_pretrained_vitl16_384(pretrained, use_readout="ignore", hooks=None):
|
||||
model = timm.create_model("vit_large_patch16_384", pretrained=pretrained)
|
||||
|
||||
hooks = [5, 11, 17, 23] if hooks == None else hooks
|
||||
return _make_vit_b16_backbone(
|
||||
model,
|
||||
features=[256, 512, 1024, 1024],
|
||||
hooks=hooks,
|
||||
vit_features=1024,
|
||||
use_readout=use_readout,
|
||||
)
|
||||
|
||||
|
||||
def _make_pretrained_vitb16_384(pretrained, use_readout="ignore", hooks=None):
|
||||
model = timm.create_model("vit_base_patch16_384", pretrained=pretrained)
|
||||
|
||||
hooks = [2, 5, 8, 11] if hooks == None else hooks
|
||||
return _make_vit_b16_backbone(
|
||||
model, features=[96, 192, 384, 768], hooks=hooks, use_readout=use_readout
|
||||
)
|
||||
|
||||
|
||||
def _make_vit_b_rn50_backbone(
|
||||
model,
|
||||
features=[256, 512, 768, 768],
|
||||
size=[384, 384],
|
||||
hooks=[0, 1, 8, 11],
|
||||
vit_features=768,
|
||||
patch_size=[16, 16],
|
||||
number_stages=2,
|
||||
use_vit_only=False,
|
||||
use_readout="ignore",
|
||||
start_index=1,
|
||||
):
|
||||
pretrained = nn.Module()
|
||||
|
||||
pretrained.model = model
|
||||
|
||||
used_number_stages = 0 if use_vit_only else number_stages
|
||||
for s in range(used_number_stages):
|
||||
pretrained.model.patch_embed.backbone.stages[s].register_forward_hook(
|
||||
get_activation(str(s + 1))
|
||||
)
|
||||
for s in range(used_number_stages, 4):
|
||||
pretrained.model.blocks[hooks[s]].register_forward_hook(get_activation(str(s + 1)))
|
||||
|
||||
pretrained.activations = activations
|
||||
|
||||
readout_oper = get_readout_oper(vit_features, features, use_readout, start_index)
|
||||
|
||||
for s in range(used_number_stages):
|
||||
value = nn.Sequential(nn.Identity(), nn.Identity(), nn.Identity())
|
||||
exec(f"pretrained.act_postprocess{s + 1}=value")
|
||||
for s in range(used_number_stages, 4):
|
||||
if s < number_stages:
|
||||
final_layer = nn.ConvTranspose2d(
|
||||
in_channels=features[s],
|
||||
out_channels=features[s],
|
||||
kernel_size=4 // (2 ** s),
|
||||
stride=4 // (2 ** s),
|
||||
padding=0,
|
||||
bias=True,
|
||||
dilation=1,
|
||||
groups=1,
|
||||
)
|
||||
elif s > number_stages:
|
||||
final_layer = nn.Conv2d(
|
||||
in_channels=features[3],
|
||||
out_channels=features[3],
|
||||
kernel_size=3,
|
||||
stride=2,
|
||||
padding=1,
|
||||
)
|
||||
else:
|
||||
final_layer = None
|
||||
|
||||
layers = [
|
||||
readout_oper[s],
|
||||
Transpose(1, 2),
|
||||
nn.Unflatten(2, torch.Size([size[0] // 16, size[1] // 16])),
|
||||
nn.Conv2d(
|
||||
in_channels=vit_features,
|
||||
out_channels=features[s],
|
||||
kernel_size=1,
|
||||
stride=1,
|
||||
padding=0,
|
||||
),
|
||||
]
|
||||
if final_layer is not None:
|
||||
layers.append(final_layer)
|
||||
|
||||
value = nn.Sequential(*layers)
|
||||
exec(f"pretrained.act_postprocess{s + 1}=value")
|
||||
|
||||
pretrained.model.start_index = start_index
|
||||
pretrained.model.patch_size = patch_size
|
||||
|
||||
# We inject this function into the VisionTransformer instances so that
|
||||
# we can use it with interpolated position embeddings without modifying the library source.
|
||||
pretrained.model.forward_flex = types.MethodType(forward_flex, pretrained.model)
|
||||
|
||||
# We inject this function into the VisionTransformer instances so that
|
||||
# we can use it with interpolated position embeddings without modifying the library source.
|
||||
pretrained.model._resize_pos_embed = types.MethodType(
|
||||
_resize_pos_embed, pretrained.model
|
||||
)
|
||||
|
||||
return pretrained
|
||||
|
||||
|
||||
def _make_pretrained_vitb_rn50_384(
|
||||
pretrained, use_readout="ignore", hooks=None, use_vit_only=False
|
||||
):
|
||||
model = timm.create_model("vit_base_resnet50_384", pretrained=pretrained)
|
||||
|
||||
hooks = [0, 1, 8, 11] if hooks == None else hooks
|
||||
return _make_vit_b_rn50_backbone(
|
||||
model,
|
||||
features=[256, 512, 768, 768],
|
||||
size=[384, 384],
|
||||
hooks=hooks,
|
||||
use_vit_only=use_vit_only,
|
||||
use_readout=use_readout,
|
||||
)
|
||||
Reference in New Issue
Block a user