1 #!/usr/bin/env python-for-pytorch
4 # flatland is a simple 2d physical simulator
6 # Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
7 # Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 # This file is part of flatland
11 # flatland is free software: you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License version 3 as
13 # published by the Free Software Foundation.
15 # flatland is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with flatland. If not, see <http://www.gnu.org/licenses/>.
28 from _ext import flatland
30 ######################################################################
32 parser = argparse.ArgumentParser(
33 description = 'Dummy test of the flatland sequence generation.',
34 formatter_class = argparse.ArgumentDefaultsHelpFormatter
37 parser.add_argument('--seed',
38 type = int, default = 0,
39 help = 'Random seed, < 0 is no seeding')
41 parser.add_argument('--width',
42 type = int, default = 80,
45 parser.add_argument('--height',
46 type = int, default = 80,
47 help = 'Image height')
49 parser.add_argument('--nb_shapes',
50 type = int, default = 8,
51 help = 'Image height')
53 parser.add_argument('--nb_sequences',
54 type = int, default = 8,
55 help = 'How many sequences to generate')
57 parser.add_argument('--nb_images_per_sequences',
58 type = int, default = 16,
59 help = 'How many images per sequence')
61 parser.add_argument('--randomize_colors',
62 action='store_true', default=True,
63 help = 'Should the shapes be of different colors')
65 parser.add_argument('--randomize_shape_size',
66 action='store_true', default=False,
67 help = 'Should the shapes be of different size')
69 args = parser.parse_args()
72 torch.manual_seed(args.seed)
74 ######################################################################
76 def sequences_to_image(x, gap = 1, gap_color = (0, 128, 255)):
79 nb_sequences = x.size(0)
80 nb_images_per_sequences = x.size(1)
83 if x.size(2) != nb_channels:
84 print('Can only handle 3 channel tensors.')
90 result = torch.ByteTensor(nb_channels,
91 gap + nb_sequences * (height + gap),
92 gap + nb_images_per_sequences * (width + gap))
94 result.copy_(torch.Tensor(gap_color).view(-1, 1, 1).expand_as(result))
96 for s in range(0, nb_sequences):
97 for i in range(0, nb_images_per_sequences):
98 result.narrow(1, gap + s * (height + gap), height) \
99 .narrow(2, gap + i * (width + gap), width) \
102 result_numpy = result.cpu().byte().transpose(0, 2).transpose(0, 1).numpy()
104 return Image.fromarray(result_numpy, 'RGB')
106 ######################################################################
108 x = flatland.generate_sequence(False,
110 args.nb_images_per_sequences,
111 args.height, args.width,
113 args.randomize_shape_size,
114 args.randomize_colors)
116 sequences_to_image(x, gap = 3, gap_color = (0, 150, 200)).save('sequences.png')
118 print('Saved sequences.png.')