-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate.py
158 lines (119 loc) · 4.49 KB
/
generate.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
import numpy as np
import argparse, json, math
import os, glob
import flow, utils, source
import torch, torchvision
from torch import nn
from encoder import rans, coder
from utils import cdfDiscreteLogitstic, cdfMixDiscreteLogistic
from matplotlib import pyplot as plt
#torch.manual_seed(42)
parser = argparse.ArgumentParser(description="")
parser.add_argument("-folder", default=None, help="Path to load the trained model")
parser.add_argument("-cuda", type=int, default=-1, help="Which device to use with -1 standing for CPU, number bigger than -1 is N.O. of GPU.")
parser.add_argument("-baseScale", type=float, default=-2.0, help="exp scaling of distribution's logscale to achieve better quality")
parser.add_argument("-best", action='store_false', help="if load the best model")
parser.add_argument("-num", type=int, default=10, help="num of image to demo")
args = parser.parse_args()
device = torch.device("cpu" if args.cuda < 0 else "cuda:" + str(args.cuda))
if args.folder is None:
raise Exception("No loading")
else:
rootFolder = args.folder
if rootFolder[-1] != '/':
rootFolder += '/'
with open(rootFolder + "parameter.json", 'r') as f:
config = json.load(f)
locals().update(config)
target = config['target']
repeat = config['repeat']
nMixing = config['nMixing']
batch = config['batch']
# update batch using passing parameter
batch = args.num ** 2
if target == "CIFAR":
# Define dimensions
targetSize = [3, 32, 32]
dimensional = 2
channel = targetSize[0]
blockLength = targetSize[-1]
# Define nomaliziation and decimal
decimal = flow.ScalingNshifting(256, -128)
rounding = utils.roundingWidentityGradient
elif target == "ImageNet32":
# Define dimensions
targetSize = [3, 32, 32]
dimensional = 2
channel = targetSize[0]
blockLength = targetSize[-1]
# Define nomaliziation and decimal
decimal = flow.ScalingNshifting(256, -128)
rounding = utils.roundingWidentityGradient
elif target == "ImageNet64":
# Define dimensions
targetSize = [3, 64, 64]
dimensional = 2
channel = targetSize[0]
blockLength = targetSize[-1]
# Define nomaliziation and decimal
decimal = flow.ScalingNshifting(256, -128)
rounding = utils.roundingWidentityGradient
# decide which model to load
if args.best:
name = max(glob.iglob(os.path.join(rootFolder, '*.saving')), key=os.path.getctime)
else:
name = max(glob.iglob(os.path.join(rootFolder, 'savings', '*.saving')), key=os.path.getctime)
# load the model
print("load saving at " + name)
loadedF = torch.load(name, map_location=device)
if 'easyMera' in name:
layerList = loadedF.layerList[:(4 * repeat)]
layerList = [layerList[no] for no in range(4 * repeat)]
elif '1to2Mera' in name:
layerList = loadedF.layerList[:(2 * repeat)]
layerList = [layerList[no] for no in range(2 * repeat)]
else:
raise Exception("model not define")
# Define nomaliziation and decimal
if 'easyMera' in name:
decimal = flow.ScalingNshifting(256, -128)
elif '1to2Mera' in name:
decimal = flow.ScalingNshifting(256, 0)
else:
raise Exception("model not define")
if 'simplePrior_False' in name:
meanNNlist = [loadedF.meanNNlist[0]]
scaleNNlist = [loadedF.scaleNNlist[0]]
else:
meanNNlist = None
scaleNNlist = None
rounding = utils.roundingWidentityGradient
prior = loadedF.prior
# Building MERA mode
if 'easyMera' in name:
f = flow.SimpleMERA(blockLength, layerList, meanNNlist, scaleNNlist, repeat, None, nMixing, decimal=decimal, rounding=utils.roundingWidentityGradient).to(device)
elif '1to2Mera' in name:
f = flow.OneToTwoMERA(blockLength, layerList, meanNNlist, scaleNNlist, repeat, None, nMixing, decimal=decimal, rounding=utils.roundingWidentityGradient).to(device)
f.prior = prior
samples = f.sample(batch)
# define renorm fn
def back01(tensor):
ten = tensor.clone().float()
ten = ten.view(ten.shape[0] * ten.shape[1], -1)
ten -= ten.min(1, keepdim=True)[0]
ten /= ten.max(1, keepdim=True)[0]
ten = ten.view(tensor.shape)
return ten
# another renorm fn
def clip(tensor, l=0, h=255):
return torch.clamp(tensor, l, h).int()
# yet another renorm fn
def batchNorm(tensor, base=1.0):
m = nn.BatchNorm2d(tensor.shape[1], affine=False)
return m(tensor).float() + base
renormFn = lambda x: back01(batchNorm(x))
samples = renormFn(samples).detach()
grid_img = torchvision.utils.make_grid(samples, nrow=args.num)
plt.figure(figsize=(12, 12))
plt.imshow(grid_img.permute(1, 2, 0))
plt.show()