-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.py
81 lines (63 loc) · 2.38 KB
/
sample.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
import argparse
import torch
from model import RNNPredictNet
from utils import *
import wand.color
import wand.image
import imageio
USE_CUDA = torch.cuda.is_available()
parser = argparse.ArgumentParser()
parser.add_argument('--filename', type=str, default='sample',
help='filename of .svg file to output, without .svg')
parser.add_argument('--sample_length', type=int, default=800,
help='number of strokes to sample')
parser.add_argument(
'--scale_factor',
type=int,
default=10,
help='factor to scale down by for svg output. smaller means bigger output')
parser.add_argument('--model_dir', type=str, default='save',
help='directory to save model to')
sample_args = parser.parse_args()
with open(os.path.join(sample_args.model_dir, 'config.pkl'), 'rb') as f:
saved_args = pickle.load(f)
model = RNNPredictNet(saved_args)
model.load_state_dict(torch.load(sample_args.model_dir + '/model.pth')['model'])
model.eval()
if USE_CUDA:
model = model.cuda()
def sample_stroke():
[strokes, params] = model.sample(sample_args.sample_length)
draw_strokes(
strokes,
factor=sample_args.scale_factor,
svg_filename=sample_args.filename + '.normal.svg')
draw_strokes_random_color(
strokes,
factor=sample_args.scale_factor,
svg_filename=sample_args.filename + '.color.svg')
draw_strokes_random_color(
strokes,
factor=sample_args.scale_factor,
per_stroke_mode=False,
svg_filename=sample_args.filename + '.multi_color.svg')
draw_strokes_eos_weighted(
strokes,
params,
factor=sample_args.scale_factor,
svg_filename=sample_args.filename + '.eos_pdf.svg')
draw_strokes_pdf(
strokes,
params,
factor=sample_args.scale_factor,
svg_filename=sample_args.filename + '.pdf.svg')
with wand.image.Image() as image:
with open(sample_args.filename + '.normal.svg', 'rb') as svg_file:
image.read(blob=svg_file.read())
png_image = image.make_blob("png32")
with open(sample_args.filename + '.png', "wb") as out:
out.write(png_image)
img = imageio.imread(sample_args.filename + '.png')
return [strokes, params], img
if __name__ == '__main__':
sample_stroke()