-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_builder.py
289 lines (239 loc) · 12.2 KB
/
model_builder.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import torch
import torch.nn as nn
import random
import numpy as np
import argparse
import os
import re
import json
import shutil
import logging
import sys
from util import project_root
sys.path.append(project_root)
sys.path.append(os.path.join(project_root, 'community/'))
from tqdm import tqdm
from torchvision import transforms
from torch.nn import functional as F
from collections import defaultdict
import torchvision.datasets as datasets
import util
import agents2
import models
from util import *
from community.ConceptWhitening.features import construct_CW_features
from community.ConvNet.features import construct_Cnn_features
from community.ProtoPNet.model import construct_PPNet
# map of which agent is compatible with which perceptual module.
agent_compat_lists = {
'RnnSenderGS': ["ProtoWrapper", "ProtoBWrapper",
"CwWrapper",
"CnnWrapper", "CnnBWrapper"],
'FLRnnSenderGS': ["ProtoWrapper", "ProtoBWrapper",
"CwWrapper",
"CnnWrapper", "CnnBWrapper"],
'OLRnnSenderGS': ["ProtoWrapper", "CwWrapper"],
'MultiHeadRnnSenderGS': ["ProtoWrapper", "CwWrapper"],
'MultiHeadRnnSenderGS2': ["ProtoWrapper", "CwWrapper"],
'ProtoSenderGS': ["ProtoWrapper", "ProtoBWrapper"],
'ProtoSender2GS': ["ProtoWrapper2"],
'ProtoSender3GS': ["ProtoWrapper2"],
'Top1ReifiedRnnSenderGS': ["ProtoWrapper"],
'RnnReceiverGS': ["ProtoWrapper", "ProtoBWrapper", "MultiHeadProtoWrapper",
"CwWrapper",
"CnnWrapper", "CnnBWrapper"],
'FLRnnReceiverGS': ["ProtoWrapper", "ProtoBWrapper", "MultiHeadProtoWrapper",
"CwWrapper",
"CnnWrapper", "CnnBWrapper"],
'ProtoReceiver2GS': ["ProtoWrapper2"],
'Top1ReifiedRnnReceiverGS': ["ProtoWrapper"],
}
percept_compat_lists = defaultdict(list)
percepts = []
for agent in list(agent_compat_lists.keys()):
for percept in agent_compat_lists[agent]:
percept_compat_lists[percept].append(agent)
percepts.append(percept)
percepts = list(set(percepts))
for p in percepts:
percept_compat_lists[p] = list(set(percept_compat_lists[p]))
# arch_key - class name from models.py
# ckpt_key - string index into state for architecture checkpoint
# base_cnn_key - string index into state for torchvision.models class name for base CNN model
def build_perceptual_wrapper(state, arch_key, ckpt_key, base_cnn_key, mean_key, std_key):
# Normalization parameters
mean = state[mean_key]
std = state[std_key]
# ===== init perceptual module.
# Some wrappers use the same base model so initialize the base model first.
if "Proto" in state[arch_key]:
from ProtoPNet.settings import prototype_activation_function, add_on_layers_type, num_data_workers
img_size = state.get('img_size', 224)
if 'send' in arch_key:
ppc = state['sender_prototypes_per_class']
else:
ppc = state['recv_prototypes_per_class']
# shouldn't matter in either case since we load ProtoPNet state dict
pretrained = 'ProtoB' not in state[arch_key]
if state[ckpt_key] != '':
model_state = torch.load(state[ckpt_key])
try:
proto_channels = model_state['prototype_shape'][1]
except KeyError as e:
# checkpoint from old version of code
proto_channels = 128
else:
logging.warning("Auto-selected prototype channels to 128!")
# Let constructor decide
proto_channels = 128
prototype_shape = (state['num_classes'] * ppc, proto_channels, 1, 1)
enc = construct_PPNet(base_architecture=state[base_cnn_key],
pretrained=pretrained,
img_size=img_size,
prototype_shape=prototype_shape,
num_classes=state['num_classes'],
prototype_activation_function=prototype_activation_function,
add_on_layers_type=add_on_layers_type)
if state[ckpt_key] != '':
logging.info(f"Load {arch_key} checkpoint from {state[ckpt_key]}")
# model_state = torch.load(state[ckpt_key], map_location=state['device'])
model_state = torch.load(state[ckpt_key])
enc.load_state_dict(model_state['state_dict'])
elif state[arch_key] == "CwWrapper":
logging.info(f"Load {arch_key} checkpoint from {state[ckpt_key]}")
model_state = torch.load(state[ckpt_key])
model_state["num_classes"] = state["num_classes"]
enc = construct_CW_features(model_state)
elif "CnnB" in state[arch_key]:
logging.info(f"Load {arch_key} checkpoint from {state[ckpt_key]}")
model_state = torch.load(state[ckpt_key])
model_state['num_classes'] = state['num_classes']
model_state['cnn_pretrained'] = False
enc = construct_Cnn_features(model_state)
else:
# no longer support TODO: remove
if state['cnn_pretrained']:
logging.info(f"Load {arch_key} checkpoint from torchvision.models")
else:
logging.info(f"Pretrained off.")
model_state = {
'architecture': state[base_cnn_key],
'num_classes': 1000, # use ImageNet weights in this configuration
'cnn_pretrained': state['cnn_pretrained'],
'state_dict': None,
}
enc = construct_Cnn_features(model_state)
enc = enc.to(state['device'])
enc_multi = torch.nn.DataParallel(enc)
# ===== wrapper
wrapper_arch = models.__dict__[state[arch_key]]
if state[arch_key] == "ProtoWrapper2":
wrapper = wrapper_arch(enc, enc_multi, topk=state['topk'], mean=mean, std=std).to(state['device'])
else:
wrapper = wrapper_arch(enc, enc_multi, mean=mean, std=std).to(state['device'])
return wrapper
def build_complete_sender(state):
assert state['sender_percept_arch'] in agent_compat_lists[state['sender_arch']], \
f"The perceptual architecture {state['sender_percept_arch']} is not compatible with {state['sender_arch']}"
percept_wrapper = build_perceptual_wrapper(state,
'sender_percept_arch',
'sender_percept_ckpt',
'sender_base_cnn',
'sender_mean',
'sender_std')
sender_arch = agents2.__dict__[state['sender_arch']]
if "MultiHeadRnnSenderGS" in state['sender_arch']:
sender = sender_arch(input_size=state['sender_input_dim'],
structure_size=state['sender_structure_dim'],
heads=state['max_len'],
vocab_size=state['vocab_size'],
hidden_size=state['hidden_dim'],
max_len=state['max_len'],
embed_dim=state['embed_dim'],
straight_through=state['gs_st'],
cell=state['sender_cell'],
trainable_temperature=state['learnable_temperature']).to(state['device'])
elif "Top1ReifiedRnnSenderGS" in state['sender_arch']:
sender = sender_arch(input_size=state['sender_input_dim'],
sender_symbols=percept_wrapper.model.prototype_vectors,
hidden_size=state['hidden_dim'],
vocab_size=state['vocab_size'],
max_len=state['max_len'],
embed_dim=state['embed_dim'],
cell=state['sender_cell'],
straight_through=state['gs_st'],
trainable_temperature=state['learnable_temperature']).to(state['device'])
else:
sender = sender_arch(input_size=state['sender_input_dim'],
vocab_size=state['vocab_size'],
hidden_size=state['hidden_dim'],
max_len=state['max_len'],
embed_dim=state['embed_dim'],
straight_through=state['gs_st'],
cell=state['sender_cell'],
trainable_temperature=state['learnable_temperature']).to(state['device'])
if state['sender_ckpt'] != '':
logging.info(f"Loading sender agent checkpoint.")
sender.load_state_dict(torch.load(state['sender_ckpt']))
return percept_wrapper, sender
def build_complete_receiver(state):
assert state['recv_percept_arch'] in agent_compat_lists[state['recv_arch']], \
f"The perceptual architecture {state['recv_percept_arch']} is not compatible with {state['recv_arch']}"
percept_wrapper = build_perceptual_wrapper(state,
'recv_percept_arch',
'recv_percept_ckpt',
'recv_base_cnn',
'recv_mean',
'recv_std')
recv_arch = agents2.__dict__[state['recv_arch']]
recv_agent_arch = agents2.__dict__[state.get('recv_agent_arch', 'DistractedReceiverAgent')]
recv_agent = recv_agent_arch(state['recv_input_dim'], state['hidden_dim'])
if "Top1ReifiedRnnReceiverGS" in state['recv_arch']:
receiver = recv_arch(percept=percept_wrapper,
signal_agent=recv_agent,
vocab_size=state['vocab_size'],
embed_dim=state['embed_dim'],
hidden_size=state['hidden_dim'],
cell=state['receiver_cell']).to(state['device'])
else:
receiver = recv_arch(recv_agent,
vocab_size=state['vocab_size'],
embed_dim=state['embed_dim'],
hidden_size=state['hidden_dim'],
cell=state['receiver_cell']).to(state['device'])
if state['recv_ckpt'] != '':
logging.info(f"Loading receiver agent checkpoint.")
receiver.load_state_dict(torch.load(state['recv_ckpt']))
return percept_wrapper, receiver
def full_system_from_row(row, log=print, test=True, from_epoch=None):
state = row
if from_epoch is None:
from_epoch = int(state['best_epoch'])
save_dir = os.path.join(state['save_dir'], str(state['run_id']))
latest_ckpt_file = get_last_semiotic_model_file(save_dir, by_epoch=from_epoch)
if latest_ckpt_file:
# take push model instead if it is available
state['sender_percept_ckpt'] = os.path.join(save_dir, latest_ckpt_file)
print(f"Loading {sender_encoder_path} based on best epoch {curr_epoch}")
basename = os.path.join(state['save_dir'], str(state['run_id']), f"sender_e{from_epoch}")
if os.path.exists(basename + '.pt'):
state['sender_ckpt'] = basename + '.pt'
else:
state['sender_ckpt'] = basename + '.pth'
basename = os.path.join(state['save_dir'], str(state['run_id']), f"receiver_e{from_epoch}")
if os.path.exists(basename + '.pt'):
state['recv_ckpt'] = basename + '.pt'
else:
state['recv_ckpt'] = basename + '.pth'
sender_wrapper, sender = build_complete_sender(state)
recv_wrapper, receiver = build_complete_receiver(state)
for model in [sender_wrapper.model, sender_wrapper.model_multi,
recv_wrapper.model, recv_wrapper.model_multi,
sender, receiver]:
model = model.to(state['device'])
if test:
for model in [sender_wrapper.model, sender_wrapper.model_multi,
recv_wrapper.model, recv_wrapper.model_multi,
sender, receiver]:
model.eval()
return sender_wrapper, sender, recv_wrapper, receiver