Spaces:
Runtime error
Runtime error
howard-hou
commited on
Commit
·
c1fe8ee
1
Parent(s):
786e086
Delete modeling.py
Browse files- modeling.py +0 -75
modeling.py
DELETED
@@ -1,75 +0,0 @@
|
|
1 |
-
from transformers import CLIPVisionModel
|
2 |
-
import torch
|
3 |
-
import torch.nn as nn
|
4 |
-
import torch.nn.functional as F
|
5 |
-
from dataclasses import dataclass
|
6 |
-
from rwkv.model import RWKV
|
7 |
-
|
8 |
-
class UpdatableRWKV(RWKV):
|
9 |
-
def __init__(self, *args, **kwargs):
|
10 |
-
super().__init__(*args, **kwargs)
|
11 |
-
|
12 |
-
def update_emb_weight(self, new_value):
|
13 |
-
self.w.update({"emb.weight", new_value})
|
14 |
-
|
15 |
-
@dataclass
|
16 |
-
class VisualEncoderConfig:
|
17 |
-
n_embd: int = 2048
|
18 |
-
vision_tower_name: str = 'openai/clip-vit-large-patch14-336'
|
19 |
-
grid_size: int = -1 # -1: no grid pooling, 0: take cls token, 1: global avg pooling, 2, 3, 4, ...: grid pooling
|
20 |
-
|
21 |
-
class VisualEncoder(nn.Module):
|
22 |
-
def __init__(self, args):
|
23 |
-
super().__init__()
|
24 |
-
self.args = args
|
25 |
-
self.vit = CLIPVisionModel.from_pretrained(args.vision_tower_name)
|
26 |
-
self.proj = nn.Linear(self.vit.config.hidden_size, args.n_embd, bias=False)
|
27 |
-
|
28 |
-
def encode_images(self, images):
|
29 |
-
B, N, C, H, W = images.shape
|
30 |
-
images = images.view(B*N, C, H, W)
|
31 |
-
image_features = self.vit(images).last_hidden_state
|
32 |
-
L, D = image_features.shape[1], image_features.shape[2]
|
33 |
-
# rerange [B*N, L, D] -> [B, N, L, D]
|
34 |
-
image_features = image_features.view(B, N, L, D)[:, 0, :, :]
|
35 |
-
image_features = self.grid_pooling(image_features)
|
36 |
-
return self.proj(image_features)
|
37 |
-
|
38 |
-
def grid_pooling(self, image_features):
|
39 |
-
if self.args.grid_size == -1: # no grid pooling
|
40 |
-
return image_features
|
41 |
-
if self.args.grid_size == 0: # take cls token
|
42 |
-
return image_features[:, 0:1, :]
|
43 |
-
if self.args.grid_size == 1: # global avg pooling
|
44 |
-
return image_features.mean(dim=1, keepdim=True)
|
45 |
-
cls_features = image_features[:, 0:1, :]
|
46 |
-
image_features = image_features[:, 1:, :] #drop cls token
|
47 |
-
B, L, D = image_features.shape
|
48 |
-
H_or_W = int(L**0.5)
|
49 |
-
image_features = image_features.view(B, H_or_W, H_or_W, D)
|
50 |
-
grid_stride = H_or_W // self.args.grid_size
|
51 |
-
image_features = F.avg_pool2d(image_features.permute(0, 3, 1, 2),
|
52 |
-
padding=0,
|
53 |
-
kernel_size=grid_stride,
|
54 |
-
stride=grid_stride)
|
55 |
-
image_features = image_features.permute(0, 2, 3, 1).view(B, -1, D)
|
56 |
-
return torch.cat((cls_features, image_features), dim=1)
|
57 |
-
|
58 |
-
|
59 |
-
class EmbeddingMixer(nn.Module):
|
60 |
-
def __init__(self, original_embedding, num_image_embeddings=4096):
|
61 |
-
super().__init__()
|
62 |
-
image_embedding = torch.zeros(num_image_embeddings,
|
63 |
-
original_embedding.shape[1],
|
64 |
-
device=original_embedding.device,
|
65 |
-
dtype=original_embedding.dtype)
|
66 |
-
self.embedding = torch.cat((original_embedding, image_embedding), dim=0)
|
67 |
-
self.image_start_index = len(original_embedding)
|
68 |
-
|
69 |
-
def set_image_embeddings(self, image_embeddings):
|
70 |
-
assert len(image_embeddings.shape) == 2, "image_embeddings should be 2D"
|
71 |
-
end_index = self.image_start_index + image_embeddings.shape[0]
|
72 |
-
self.embedding[self.image_start_index:end_index] = image_embeddings
|
73 |
-
|
74 |
-
def get_input_embeddings(self):
|
75 |
-
return self.embedding
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|