3 # Any copyright is dedicated to the Public Domain.
4 # https://creativecommons.org/publicdomain/zero/1.0/
6 # Written by Francois Fleuret <francois@fleuret.org>
8 import torch, torchvision
28 color_tokens = dict( [ (n, c) for n, c in zip(color_names, colors) ] )
30 ######################################################################
32 def generate(nb, height = 6, width = 8, max_nb_statements = 10):
37 nb = torch.randint(5, (1,)) + 1
38 shape_position = torch.randperm(height * width)[:nb]
39 shape_c = torch.randperm(5)[:nb] + 1
40 shape_i = shape_position.div(width, rounding_mode = 'floor')
41 shape_j = shape_position % width
43 img = [ 0 ] * height * width
44 for k in range(nb): img[shape_position[k]] = shape_c[k]
48 for r, c in [ (k, color_names[shape_c[k]]) for k in range(nb) ]:
49 s += [ f'there is {c}' ]
51 if shape_i[r] >= height - height//3: s += [ f'{c} bottom' ]
52 if shape_i[r] < height//3: s += [ f'{c} top' ]
53 if shape_j[r] >= width - width//3: s += [ f'{c} right' ]
54 if shape_j[r] < width//3: s += [ f'{c} left' ]
56 for t, d in [ (k, color_names[shape_c[k]]) for k in range(nb) ]:
57 if shape_i[r] > shape_i[t]: s += [ f'{c} below {d}' ]
58 if shape_i[r] < shape_i[t]: s += [ f'{c} above {d}' ]
59 if shape_j[r] > shape_j[t]: s += [ f'{c} right of {d}' ]
60 if shape_j[r] < shape_j[t]: s += [ f'{c} left of {d}' ]
62 nb_statements = torch.randint(max_nb_statements, (1,)) + 1
63 s = ' <sep> '.join([ s[k] for k in torch.randperm(len(s))[:nb_statements] ] )
64 s += ' <img> ' + ' '.join([ f'{color_names[n]}' for n in img ])
69 ######################################################################
71 def descr2img(descr, height = 6, width = 8):
75 return color_tokens[t]
77 return [ 128, 128, 128 ]
80 u = x.split('<img>', 1)
81 return u[1] if len(u) > 1 else ''
83 img = torch.full((len(descr), 3, height, width), 255)
84 d = [ img_descr(x) for x in descr ]
85 d = [ u.strip().split(' ')[:height * width] for u in d ]
86 d = [ u + [ '<unk>' ] * (height * width - len(u)) for u in d ]
87 d = [ [ token2color(t) for t in u ] for u in d ]
88 img = torch.tensor(d).permute(0, 2, 1)
89 img = img.reshape(img.size(0), 3, height, width)
93 ######################################################################
95 if __name__ == '__main__':
97 img = descr2img(descr)
98 print(descr, img.size())
99 torchvision.utils.save_image(img / 255.,
100 'example.png', nrow = 16, pad_value = 0.8)
104 start_time = time.perf_counter()
105 descr = generate(10000)
106 end_time = time.perf_counter()
107 print(f'{len(descr) / (end_time - start_time):.02f} samples per second')
109 ######################################################################