From 1d49fa4d3aeb8e311a145d6dbd3d5c640d7d16da Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Thu, 12 May 2022 08:28:24 +0200 Subject: [PATCH] Update. --- picoclvr.py | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/picoclvr.py b/picoclvr.py index 2342016..1215764 100755 --- a/picoclvr.py +++ b/picoclvr.py @@ -34,34 +34,40 @@ def generate(nb, height = 6, width = 8, max_nb_statements = 10): 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 = ' '.join([ s[k] for k in torch.randperm(len(s))[:nb_statements] ] ) s += ' ' + ' '.join([ f'{color_names[n]}' for n in img ]) + descr += [ s ] return descr @@ -94,10 +100,15 @@ def descr2img(descr, height = 6, width = 8): 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 -- 2.39.5