-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.py
120 lines (90 loc) · 2.7 KB
/
test.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Script for testing wavelet bases learned using a neural network approach.
"""
# Basic import(s)
import sys
import json
import numpy as np
import matplotlib.pyplot as plt
# Pytorch import(s)
import torch
from torch.autograd import Variable
# Project import(s)
from wavenet.generators import *
# Type definitions
dtype = torch.FloatTensor
# General definitions
seed = 22
# Main function definition.
def main ():
# Get paths for wavenets to test
paths = sys.argv[1:]
if not paths:
print "Please specify at least one wavenet to test, as:"
print " $ python {} <path> [...]".format(sys.argv[0])
return
# Select only paths with correct suffix
paths_filtered = filter(lambda path: path.endswith('.pt'), paths)
if len(paths_filtered) != len(paths):
print "Only selecting paths ending in `.pt`."
pass
paths = paths_filtered
if not paths:
print "No paths ending in `.pt` was found."
return
# Reproducibility
np.random.seed(seed)
torch.manual_seed(seed)
# Load wavenet model(s) from file
wavenets = map(torch.load, paths)
# Study wavenet(s)
for path, wavenet in zip(paths, wavenets):
print "== {}:".format(path)
tag = '__'.join(path.split('/')[-1].split('.')[0].split('__')[1:])
params = wavenet.params
input_shape = wavenet.input_shape
for idx in range(input_shape[0]):
# Compute basis function
c = np.zeros(input_shape)
c[idx] = 1
basis = wavenet.backward(c)
# Plot basis functions
plt.clf()
plt.plot(basis, drawstyle='steps-mid')
plt.ylim(-1,1)
plt.savefig('figures/basis__{}__idx{}.pdf'.format(tag, idx))
pass
# ...
pass
"""
# Loss
# @TODO: Move to `test.py` script.
plt.clf()
for key in losses[0].keys():
if key == 'compactness': continue
loss = [loss_[key] for loss_ in losses]
plt.semilogy(loss, label=key)
pass
plt.legend()
plt.savefig('tmp_losses.pdf')
# Study wavelet basis
# @TODO: Move to `test.py` script.
for i in range(input_shape[0]): # @TEMP: For 1D only
c = Variable(torch.zeros(input_shape).type(dtype), requires_grad=False)
c.data[i] = 1
basis = w.backward(c)
plt.clf()
plt.plot(np.zeros(len(basis)), color='gray', linewidth=1)
plt.plot(basis, drawstyle='steps-mid')
plt.ylim(-1,1)
plt.savefig('tmp_basis{}.pdf'.format(i))
pass
"""
# ...
return
# Main function call.
if __name__ == '__main__':
main()
pass