-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaptionInferenceLight.py
173 lines (147 loc) · 5.69 KB
/
CaptionInferenceLight.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import torch
import clip
from PIL import Image
import numpy as np
import torch.nn as nn
import copy
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)
input_map = "TopoMapsTest/(Egypt)5183642015873277691/15054064.jpg" # change path to historical map of your choice
class Combined_model(nn.Module):
def __init__(
self,
model_maptype,
model_location,
model_century,
model_note,
model_area,
model_topic,
):
super(Combined_model, self).__init__()
self.model_maptype = model_maptype
self.model_location = model_location
self.model_century = model_century
self.model_note = model_note
self.model_area = model_area
self.model_topic = model_topic
def forward(self, x):
maptypes = ["topographic map", "pictorial map"]
text = clip.tokenize(maptypes).to(device)
logits_per_image, logits_per_text = self.model_maptype(x, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
maptype = maptypes[np.argmax(probs)]
if maptype == "topographic map":
locations = [
"greece",
"italy",
"iberian peninsula",
"france",
"eastern hemisphere",
"europe",
"middle east",
"asia minor",
"germany",
"british isles",
"world",
"egypt",
"part of italy",
"part of france",
"part of germany",
"india",
"holy land",
"asia",
"caucasus",
"sri lanka",
"south america",
"americas",
"switzerland",
"scandinavia",
"netherlands",
"africa",
"part of greece",
]
text = clip.tokenize(locations).to(device)
logits_per_image, logits_per_text = self.model_location(x, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
location = locations[np.argmax(probs)]
centuries = ["19th century", "18th century", "17th century", "16th century"]
text = clip.tokenize(centuries).to(device)
logits_per_image, logits_per_text = self.model_century(x, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
century = centuries[np.argmax(probs)]
notes = [
"hand colored",
"hand colored with decorative elements and pictorial relief",
"pictorial relief",
"hand colored with pictorial relief",
"engraved",
"decorative elements and pictorial relief",
]
text = clip.tokenize(notes).to(device)
logits_per_image, logits_per_text = self.model_note(x, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
note = notes[np.argmax(probs)]
return maptype, location, century, note
elif maptype == "pictorial map":
areas = ["united states", "world"]
text = clip.tokenize(areas).to(device)
logits_per_image, logits_per_text = self.model_area(x, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
area = areas[np.argmax(probs)]
topics = [
"flight network",
"news during world war 2",
"world war 2",
"transport routes",
"tourist sights",
"playing card",
"satirical representation",
"people",
"educational drawings",
"food and agriculture",
"animals",
"military",
"stamps",
]
text = clip.tokenize(topics).to(device)
logits_per_image, logits_per_text = self.model_topic(x, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
topic = topics[np.argmax(probs)]
return maptype, area, topic
model_maptype = copy.deepcopy(model)
model_location = copy.deepcopy(model)
model_century = copy.deepcopy(model)
model_note = copy.deepcopy(model)
model_area = copy.deepcopy(model)
model_topic = copy.deepcopy(model)
def freeze_network(model):
for p in model.parameters():
p.requires_grad = False
return model
model_path_maptype = "CLIPMapType.pt"
model_maptype.load_state_dict(torch.load(model_path_maptype, map_location=device))
freeze_network(model_maptype)
model_path_location = "CLIPLocationTopo.pt"
model_location.load_state_dict(torch.load(model_path_location, map_location=device))
freeze_network(model_location)
model_path_century = "CLIPCentury.pt"
model_century.load_state_dict(torch.load(model_path_century, map_location=device))
freeze_network(model_century)
model_path_note = "CLIPStyle.pt"
model_note.load_state_dict(torch.load(model_path_note, map_location=device))
freeze_network(model_note)
model_path_area = "CLIPLocationPict.pt"
model_area.load_state_dict(torch.load(model_path_area, map_location=device))
freeze_network(model_area)
model_path_topic = "CLIPTopic.pt"
model_topic.load_state_dict(torch.load(model_path_topic, map_location=device))
freeze_network(model_topic)
results = []
image = preprocess(Image.open(input_map)).unsqueeze(0).to(device)
combined_model = Combined_model(
model_maptype, model_location, model_century, model_note, model_area, model_topic
)
combined_model.eval()
with torch.no_grad():
results = combined_model(image)
print(results)