-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_checker.py
More file actions
154 lines (118 loc) · 4.67 KB
/
Copy pathmodel_checker.py
File metadata and controls
154 lines (118 loc) · 4.67 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
# CSC 321, Assignment 4
#
# This is a script to check whether the outputs of your CycleGenerator, DCDiscriminator, and
# CycleGenerator models produce the expected outputs.
#
# NOTE THAT THIS MODEL CHECKER IS PROVIDED FOR CONVENIENCE ONLY, AND MAY PRODUCE FALSE NEGATIVES.
# DO NOT USE THIS AS THE ONLY WAY TO CHECK THAT YOUR MODEL IS CORRECT.
#
# Usage:
# ======
#
# python model_checker.py
#
import warnings
warnings.filterwarnings("ignore")
# Torch imports
import torch
from torch.autograd import Variable
# Numpy
import numpy as np
# Local imports
from models import DCGenerator, DCDiscriminator, CycleGenerator
# from torchsummary import summary
def count_parameters(model):
"""Finds the total number of trainable parameters in a model.
"""
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def sample_noise(dim):
"""
Generate a PyTorch Tensor of uniform random noise.
Input:
- batch_size: Integer giving the batch size of noise to generate.
- dim: Integer giving the dimension of noise to generate.
Output:
- A PyTorch Tensor of shape (1, dim, 1, 1) containing uniform
random noise in the range (-1, 1).
"""
return Variable(torch.rand(1, dim) * 2 - 1).unsqueeze(2).unsqueeze(3)
def check_dc_generator():
"""Checks the output and number of parameters of the DCGenerator class.
"""
state = torch.load('/home/love_you/Documents/Study/deep_learning/a4-code/a4-code-v2-updated/checker_files/dc_generator.pt')
# print(state['state_dict'].keys())
G = DCGenerator(noise_size=100, conv_dim=32)
# for name, param in G.named_parameters():
# print(name)
# summary(G, input_size=(100, 1, 1))
G.load_state_dict(state['state_dict'])
noise = state['input']
dc_generator_expected = state['output']
output = G(noise)
output_np = output.data.cpu().numpy()
if np.allclose(output_np, dc_generator_expected):
print('DCGenerator output: EQUAL')
else:
print('DCGenerator output: NOT EQUAL')
num_params = count_parameters(G)
expected_params = 370624
print('DCGenerator #params = {}, expected #params = {}, {}'.format(
num_params, expected_params, 'EQUAL' if num_params == expected_params else 'NOT EQUAL'))
print('-' * 80)
def check_dc_discriminator():
"""Checks the output and number of parameters of the DCDiscriminator class.
"""
state = torch.load('/home/love_you/Documents/Study/deep_learning/a4-code/a4-code-v2-updated/checker_files/dc_discriminator.pt')
# for key, value in state.items():
# print(key)
D = DCDiscriminator(conv_dim=32)
# summary(D, input_size=(3, 32, 32))
D.load_state_dict(state['state_dict'])
images = state['input']
dc_discriminator_expected = state['output']
output = D(images)
output_np = output.data.cpu().numpy()
if np.allclose(output_np, dc_discriminator_expected):
print('DCDiscriminator output: EQUAL')
else:
print("output_np: ", output_np.shape)
print("dc_discriminator_expected: ", dc_discriminator_expected.shape)
print('DCDiscriminator output: NOT EQUAL')
num_params = count_parameters(D)
expected_params = 167872
print('DCDiscriminator #params = {}, expected #params = {}, {}'.format(
num_params, expected_params, 'EQUAL' if num_params == expected_params else 'NOT EQUAL'))
print('-' * 80)
def check_cycle_generator():
"""Checks the output and number of parameters of the CycleGenerator class.
"""
state = torch.load('checker_files/cycle_generator.pt')
G_XtoY = CycleGenerator(conv_dim=32, init_zero_weights=False)
G_XtoY.load_state_dict(state['state_dict'])
images = state['input']
cycle_generator_expected = state['output']
output = G_XtoY(images)
output_np = output.data.cpu().numpy()
if np.allclose(output_np, cycle_generator_expected):
print('CycleGenerator output: EQUAL')
else:
print('CycleGenerator output: NOT EQUAL')
num_params = count_parameters(G_XtoY)
expected_params = 105856
print('CycleGenerator #params = {}, expected #params = {}, {}'.format(
num_params, expected_params, 'EQUAL' if num_params == expected_params else 'NOT EQUAL'))
print('-' * 80)
if __name__ == '__main__':
try:
check_dc_generator()
except Exception as e:
print(e)
#print('Crashed while checking DCGenerator. Maybe not implemented yet?')
try:
check_dc_discriminator()
except:
print('Crashed while checking DCDiscriminator. Maybe not implemented yet?')
# try:
# check_cycle_generator()
# except:
# print('Crashed while checking CycleGenerator. Maybe not implemented yet?')