Spaces:
Running
on
T4
Running
on
T4
File size: 2,636 Bytes
a73717c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import torch
from torch import nn
import numpy as np
# from datas.dataset_3d import *
from torch.nn import functional as F
class Normalize(nn.Module):
def __init__(self, dim: int) -> None:
super().__init__()
self.dim = dim
def forward(self, x):
return torch.nn.functional.normalize(x, dim=self.dim, p=2)
class LinearLayer(nn.Module):
def __init__(self, dim_in, dim_out, k):
super(LinearLayer, self).__init__()
self.fc = nn.ModuleList([nn.Linear(dim_in, dim_out) for i in range(k)])
def forward(self, tokens):
for i in range(len(tokens)):
if len(tokens[i].shape) == 3:
tokens[i] = tokens[i].transpose(0,1)
tokens[i] = self.fc[i](tokens[i][:, 1:, :])
else:
B, C, H, W = tokens[i].shape
tokens[i] = self.fc[i](tokens[i].view(B, C, -1).permute(0, 2, 1).contiguous())
return tokens
class PromptLearner(nn.Module):
def __init__(self, dim_in, dim_out) -> None:
super().__init__()
self.meta_net = nn.Sequential(
nn.Conv2d(dim_in, dim_in * 4, kernel_size=3, padding=1),
# nn.BatchNorm2d(dim_in * 4),
nn.ReLU(inplace=True),
nn.MaxPool2d(2), # 112 * 112
nn.Conv2d(dim_in * 4, dim_in * 16, kernel_size=3, padding=1),
# nn.BatchNorm2d(dim_in * 16),
nn.ReLU(inplace=True),
nn.MaxPool2d(2), # 56 * 56
nn.Conv2d(dim_in * 16, dim_in * 64, kernel_size=3, padding=1),
# nn.BatchNorm2d(dim_in * 64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2), # 28 * 28
nn.Conv2d(dim_in * 64, dim_in * 256, kernel_size=3, padding=1),
# nn.BatchNorm2d(dim_in * 256),
nn.ReLU(inplace=True),
nn.MaxPool2d(2), # 14 * 14
nn.Conv2d(dim_in * 256, dim_in * 1024, kernel_size=3, padding=1),
# nn.BatchNorm2d(dim_in * 1024),
nn.ReLU(inplace=True),
nn.MaxPool2d(2), # 7 * 7
nn.Conv2d(dim_in * 1024, dim_out, kernel_size=5, padding=0),
# nn.BatchNorm2d(dim_out),
# nn.ReLU(inplace=True),
)
self.base_prompts = nn.Parameter(torch.randn((9, dim_out)),requires_grad=True)
def forward(self, input):
B,C,H,W = input.shape
img_prompts = self.meta_net(input)
# print(input.shape, img_prompts.shape)
img_prompts = img_prompts.reshape(B,4096,9).transpose(-2,-1)
output = torch.cat([self.base_prompts.expand(B,-1,-1), img_prompts], dim=1)
return output |