descr = [ ]
for n in range(nb):
- nb_shapes = torch.randint(len(color_tokens) - 1, (1,)) + 1
- shape_position = torch.randperm(height * width)[:nb_shapes]
- shape_c = torch.randperm(len(color_tokens) - 1)[:nb_shapes] + 1
- shape_i = shape_position.div(width, rounding_mode = 'floor')
- shape_j = shape_position % width
+
+ nb_squares = torch.randint(len(color_tokens) - 1, (1,)) + 1
+ square_position = torch.randperm(height * width)[:nb_squares]
+ square_c = torch.randperm(len(color_tokens) - 1)[:nb_squares] + 1
+ square_i = square_position.div(width, rounding_mode = 'floor')
+ square_j = square_position % width
img = [ 0 ] * height * width
- for k in range(nb_shapes): img[shape_position[k]] = shape_c[k]
+ for k in range(nb_squares): img[square_position[k]] = square_c[k]
+
+ # generates all the true relations
s = [ ]
- for r, c in [ (k, color_names[shape_c[k]]) for k in range(nb_shapes) ]:
+ for r, c in [ (k, color_names[square_c[k]]) for k in range(nb_squares) ]:
s += [ f'there is {c}' ]
- if shape_i[r] >= height - height//3: s += [ f'{c} bottom' ]
- if shape_i[r] < height//3: s += [ f'{c} top' ]
- if shape_j[r] >= width - width//3: s += [ f'{c} right' ]
- if shape_j[r] < width//3: s += [ f'{c} left' ]
+ if square_i[r] >= height - height//3: s += [ f'{c} bottom' ]
+ if square_i[r] < height//3: s += [ f'{c} top' ]
+ if square_j[r] >= width - width//3: s += [ f'{c} right' ]
+ if square_j[r] < width//3: s += [ f'{c} left' ]
+
+ for t, d in [ (k, color_names[square_c[k]]) for k in range(nb_squares) ]:
+ if square_i[r] > square_i[t]: s += [ f'{c} below {d}' ]
+ if square_i[r] < square_i[t]: s += [ f'{c} above {d}' ]
+ if square_j[r] > square_j[t]: s += [ f'{c} right of {d}' ]
+ if square_j[r] < square_j[t]: s += [ f'{c} left of {d}' ]
- for t, d in [ (k, color_names[shape_c[k]]) for k in range(nb_shapes) ]:
- if shape_i[r] > shape_i[t]: s += [ f'{c} below {d}' ]
- if shape_i[r] < shape_i[t]: s += [ f'{c} above {d}' ]
- if shape_j[r] > shape_j[t]: s += [ f'{c} right of {d}' ]
- if shape_j[r] < shape_j[t]: s += [ f'{c} left of {d}' ]
+ # pick at most max_nb_statements at random
nb_statements = torch.randint(max_nb_statements, (1,)) + 1
s = ' <sep> '.join([ s[k] for k in torch.randperm(len(s))[:nb_statements] ] )
s += ' <img> ' + ' '.join([ f'{color_names[n]}' for n in img ])
+
descr += [ s ]
return descr
if __name__ == '__main__':
descr = generate(5)
+ for d in descr:
+ print(d)
+ print()
+
img = descr2img(descr)
- print(descr, img.size())
+ print(img.size())
+
torchvision.utils.save_image(img / 255.,
- 'example.png', nrow = 16, pad_value = 0.8)
+ 'picoclvr_example.png', nrow = 16, pad_value = 0.8)
import time