-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCLIPMH.py
More file actions
98 lines (45 loc) · 1.97 KB
/
CLIPMH.py
File metadata and controls
98 lines (45 loc) · 1.97 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import torch.nn as nn
import torch
import torchvision
import torch
import clip
from PIL import Image
import torch.nn.functional as F
device = "cuda" if torch.cuda.is_available() else "cpu"
class Model(nn.Module):
def __init__(self, args):
super(Model, self).__init__()
self.hash_bit = args.hash_bit
self.model, self.preprocess = clip.load("ViT-B/32", device=device)
# fusion
self.fc3 = nn.Linear(512, 256)
self.activation3 = nn.Tanh()
self.image_out = nn.Sequential(
self.fc3,self.activation3)
self.text_convert = nn.Linear(512, 256)
self.text_act = nn.Tanh()
self.text_out = nn.Sequential(self.text_convert, self.text_act)
self.coff = nn.Sequential(
nn.Linear(256*2,256*2, bias=False),
nn.Sigmoid())
self.final_hash = nn.Linear(256*2, self.hash_bit)
self.fus_act3 = nn.Tanh()
self.fus_dropout = nn.Dropout(0.1)
self.hash_output = nn.Sequential(
self.final_hash, self.fus_act3)
self.iter_num = 0
self.scale = 1
def forward(self, image,text):
# image = preprocess(Image.open("data/flickr25k/images/im1.jpg")).unsqueeze(0).to(device)
# text = clip.tokenize(["a diagram", "a dog", "a cat"]).to(device)
text = clip.tokenize(text).to(device)
image_features = self.model.encode_image(image).float()
text_features = self.model.encode_text(text).float()
# print(image_features.shape)
# print(text_features.shape)
image_out = self.image_out(image_features)
text_out = self.text_out(text_features)
feat_concat = torch.cat((image_out, text_out),1)
coff = self.coff(feat_concat)
feat_fusion = coff*feat_concat
return self.hash_output(feat_fusion)