-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgenerate.lua
More file actions
79 lines (66 loc) · 2.37 KB
/
generate.lua
File metadata and controls
79 lines (66 loc) · 2.37 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
--[[
This file is a modified version of the one from dcgan.torch
(see https://github.com/soumith/dcgan.torch/blob/master/generate.lua).
]]--
require 'image'
require 'nn'
local optnet = require 'optnet'
torch.setdefaulttensortype('torch.FloatTensor')
opt = {
batchSize = 64, -- number of samples to produce
noisetype = 'normal', -- type of noise distribution (uniform / normal).
net = '', -- path to the generator network
imsize = 1, -- used to produce larger images. 1 = 64px. 2 = 80px, 3 = 96px, ...
name = 'wasserstein', -- name of the file saved
gpu = 1, -- gpu mode. 0 = CPU, 1 = GPU
display = 6051, -- port for displaying image / 0 = false
nz = 100,
}
for k,v in pairs(opt) do opt[k] = tonumber(os.getenv(k)) or os.getenv(k) or opt[k] end
print(opt)
if opt.display == 0 then opt.display = false end
if opt.display then
disp = require 'display'
disp.configure({ hostname = '0.0.0.0', port = opt.display })
end
assert(opt.net ~= '', 'provide a generator model')
if opt.gpu > 0 then
require 'cunn'
require 'cudnn'
end
noise = torch.Tensor(opt.batchSize, opt.nz, opt.imsize, opt.imsize)
net = torch.load(opt.net)
-- for older models, there was nn.View on the top
-- which is unnecessary, and hinders convolutional generations.
if torch.type(net:get(1)) == 'nn.View' then
net:remove(1)
end
print(net)
if opt.noisetype == 'uniform' then
noise:uniform(-1, 1)
elseif opt.noisetype == 'normal' then
noise:normal(0, 1)
end
local sample_input = torch.randn(2, 100, 1, 1)
if opt.gpu > 0 then
net:cuda()
cudnn.convert(net, cudnn)
noise = noise:cuda()
sample_input = sample_input:cuda()
else
sample_input = sample_input:float()
net:float()
end
-- a function to setup double-buffering across the network.
-- this drastically reduces the memory needed to generate samples
optnet.optimizeMemory(net, sample_input)
local images = net:forward(noise)
print('Images size: ', images:size(1) .. ' x ' .. images:size(2) .. ' x ' .. images:size(3) .. ' x ' .. images:size(4))
images:add(1):mul(0.5)
print('Min, Max, Mean, Stdv', images:min(), images:max(), images:mean(), images:std())
image.save(opt.name .. '.png', image.toDisplayTensor({ input = images, nrow = 8 }))
print('Saved image to: ', opt.name .. '.png')
if opt.display then
disp.image(images)
print('Displayed image')
end