-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataGenClick.py
143 lines (104 loc) · 3.79 KB
/
dataGenClick.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
import gc
import multiprocessing as mp
import os
import sys
import numpy as np
import pandas as pd
import torch
from clickDetector import clickDetector
from infowavegan import WaveGANGenerator
if __name__ == "__main__":
genPath = sys.argv[1]
print(sys.argv)
print(genPath)
# PREAMBLE
nCPU = int(mp.cpu_count())
nCPU //= 2
chunksize = 500
print("nCPU: ", nCPU)
print("affinity: ", len(os.sched_getaffinity(0)))
nCat = 5
Nts = 40
N = 2500
print("Param lengths: ", nCat, Nts, N)
bits = list(range(nCat))
ts = np.linspace(-12.5, 12.5, Nts)
if not torch.cuda.is_available():
raise RuntimeError("No CUDA!")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Device used :", device)
batchSize = N
print("Using gpu batches of size ", batchSize)
Gi = WaveGANGenerator(slice_len=2 ** 16)
weights = torch.load(genPath, map_location='cpu')
Gi.load_state_dict(weights)
G = torch.nn.DataParallel(Gi)
G.to(f'cuda:{G.device_ids[0]}')
_z = torch.FloatTensor(N, 100 - nCat).uniform_(-1, 1).to(device)
c = torch.FloatTensor(N, nCat).to(device)
z = torch.cat((c, _z), dim=1)
z[:, :nCat] = 0
print("Memory: total, reserved, allocated: ",
torch.cuda.get_device_properties(0).total_memory,
torch.cuda.memory_reserved(0),
torch.cuda.memory_allocated(0)
)
print("Opening pool with", nCPU, "CPUs")
pool = mp.Pool(nCPU)
# 2-way INTERACTION
res = []
# baseline: [00000]
print("Generating baseline data")
genData = []
for zPart in z.split(batchSize, dim=0):
genData.append(G(zPart).detach().cpu().flatten(start_dim=1, end_dim=2).numpy())
res.append([pool.map_async(clickDetector, np.concatenate(genData),
chunksize=chunksize)])
del genData
gc.collect()
for bit in bits:
print(f"Generating for bit {bit}, N={N}, {len(ts)} values of t...")
print("Memory: total, reserved, allocated: ",
torch.cuda.get_device_properties(0).total_memory,
torch.cuda.memory_reserved(0),
torch.cuda.memory_allocated(0)
)
z[:, :nCat] = 0
dummy = []
for t in ts:
z[:, bit] = t
# Workaround due to GPU memory limits
genData = []
for zPart in z.split(batchSize, dim=0):
genData.append(G(zPart).detach().cpu().flatten(start_dim=1, end_dim=2).numpy())
dummy.append(pool.map_async(clickDetector, np.concatenate(genData),
chunksize=chunksize))
del genData
gc.collect()
print(f"Generated for bit {bit}, N={N}, {len(ts)} values of t...")
res.append(dummy)
gc.collect()
print("Waiting on results")
# Call in order
clicks = -1 * np.ones(shape=(len(bits) + 1, len(ts), N), dtype=int)
for i, bitL in enumerate(res):
for j, TT in enumerate(bitL):
result = TT.get()
clicks[i, j, :] = [el[1] for el in result]
res[i][j] = [el[0] for el in result]
pool.close()
pool.join()
bits.insert(0, -1)
print("Saving")
np.save("data/single" + genPath[7:-5].replace("/", "") + "Bits.npy",
bits)
np.save("data/single" + genPath[7:-5].replace("/", "") + "Ts.npy",
ts)
np.save("data/single" + genPath[7:-5].replace("/", "") + "Zs.npy",
z[:, nCat:].detach().cpu())
np.save("data/single" + genPath[7:-5].replace("/", "") + "Clicks.npy",
clicks)
# save the ICIs as hd5
df1 = pd.DataFrame(res, index=bits, columns=ts)
df1.to_hdf("data/single" + genPath[7:-5].replace("/", "") + ".hdf5",
f"{genPath[7:-5].replace('/', '')}")