-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_comms.py
More file actions
176 lines (130 loc) · 5.17 KB
/
analyze_comms.py
File metadata and controls
176 lines (130 loc) · 5.17 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
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
import torch
import torch.nn as nn
import random
import numpy as np
import argparse
import os
import re
import json
import shutil
import pandas as pd
import sys
import logging
import util
from util import get_time_stamp, load_json, save_json, project_root, DATA_ROOT, SAVE_ROOT, loader_to_message_data, pickle_write
sys.path.append(project_root)
from tqdm import tqdm
from torchvision import transforms
from torch.nn import functional as F
from community.topsim import topographic_similarity
from community.info_theory import gap_mi_first_second, calc_entropy
from model_builder import build_complete_sender, build_complete_receiver, full_system_from_row
from optim import disable_parameter_requires_grad
from dataloader_dali import distractor_train_loader, distractor_test_loader
# =====================
parser = argparse.ArgumentParser()
parser.add_argument('--config', required=True, help='path to configuration')
parser.add_argument('--gpuid', nargs='+', type=str, default="0")
args = vars(parser.parse_args())
os.environ['CUDA_VISIBLE_DEVICES'] = args['gpuid'][0]
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device={device}:{os.environ['CUDA_VISIBLE_DEVICES']}")
state = util.load_json(args['config'])
state = util.apply_env(state, log=print)
state['seed'] = int(state['seed'])
np.random.seed(state['seed'])
torch.manual_seed(state['seed'])
torch.backends.cudnn.deterministic = True
if "run_id" in list(state.keys()):
run_id = state["run_id"] if state["run_id"] != "" else util.get_time_stamp()
run_id = str(run_id)
else:
run_id = util.get_time_stamp()
state['analysis_dir'] = os.path.join(state['analysis_dir'], run_id)
if not os.path.exists(state['analysis_dir']):
os.makedirs(state['analysis_dir'])
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',
handlers=[
logging.FileHandler(os.path.join(state['analysis_dir'], 'analyze_comms.log')),
logging.StreamHandler()
],
level=logging.DEBUG)
log = logging.getLogger('analyze_comms')
config_basename = args['config'].split('/')[-1]
shutil.copyfile(args['config'], os.path.join(state['analysis_dir'], config_basename))
# always remove
canary_path = os.path.join(state['analysis_dir'], "canary.txt")
if os.path.exists(canary_path):
os.remove(canary_path)
name = state['name']
# =====================
db = pd.DataFrame()
db = db.append(pd.Series(data={
'name': name
}, name=name))
curr_epoch = state['best_epoch']
approach = state['approach']
split = state['split']
state['device'] = device
state['train_dir'] = state['train_push_dir']
state['seed'] = int(state['seed'])
log.info(f"Split: {state['split']}")
# returns eval() models
sender_wrapper, sender, receiver_wrapper, receiver = full_system_from_row(state, from_epoch=curr_epoch, test=True)
for model in [sender_wrapper, sender, receiver_wrapper, receiver]:
disable_parameter_requires_grad(model)
try:
if curr_epoch > state['apply_symbols_epoch']:
receiver.apply_symbols = True
except KeyError:
print('Not a reified architecture.')
# Use the joined loader to avoid caching for receiver images that aren't used anyway
if state['split'] == 'train':
curr_loader = distractor_train_loader(state, 224, None, None)
else:
curr_loader = distractor_test_loader(state, 224, None, None)
# ==========================
in_vectors = []
messages = []
actuals = []
preds = []
# play signal game with preprocessed feats
# n = len(curr_loader.cache)
md = loader_to_message_data(curr_loader, (sender_wrapper, receiver_wrapper), (sender, receiver), max_msgs=state['max_msgs'])
messages = md['messages']
in_vectors = md['in_vectors']
in_structs = md['in_structs']
# print("acc: ", state['recv_acc'])
ts = topographic_similarity(messages, in_vectors)
print('ts: ', ts)
db.at[name, f'{split}_top_sim'] = ts[0]
db.at[name, f'{split}_top_sim_pvalue'] = ts[1]
if state['sender_percept_arch'] != 'CnnBWrapper':
ds = topographic_similarity(messages, in_structs)
print('ds: ', ds)
db.at[name, f'{split}_dis_sim'] = ds[0]
db.at[name, f'{split}_dis_sim_pvalue'] = ds[1]
H = calc_entropy(messages)
db.at[name, 'entropy'] = H
print('entropy: ', H)
if approach == 'proto':
m = state['prototypes_per_class']
else:
m = 10
db.at[name, 'needs_update'] = False
db_path = os.path.join(state['analysis_dir'], f"{name}.pkl")
db.to_pickle(db_path)
cts_path = os.path.join(state['analysis_dir'], f"{name}-cts.pkl")
pickle_write(cts_path, md['class_to_symbols'])
vectors_path = os.path.join(state['analysis_dir'], f"{name}-vectors.pkl")
pickle_write(vectors_path, in_vectors)
structs_path = os.path.join(state['analysis_dir'], f"{name}-structures.pkl")
pickle_write(structs_path, in_structs)
messages_path = os.path.join(state['analysis_dir'], f"{name}-messages.pkl")
pickle_write(messages_path, messages)
md_path = os.path.join(state['analysis_dir'], f"{name}-full_message_data.pkl")
torch.save(md, md_path)
# md_path = os.path.join(state['analysis_dir'], f"{name}-full_message_data.pkl")
# torch.save(md, md_path)
with open(canary_path, 'w') as cf:
cf.write("We made it.\n")