Exemplo n.º 1
0
def images_iter(directory, mode):
    mode = 'dev' if mode == 'validation' else mode
    directory = os.path.join(directory, mode, 'images')
    for root, dirs, files in os.walk(directory):
        if root == directory:
            assert not files
        else:
            assert not dirs
            for filename in files:
                assert filename[:len(mode) + 1] == mode + '-'
                identifier = filename[len(mode) + 1: -6]
                assert identifier[-2:] in ('-0', '-1', '-2', '-3')
                with open(os.path.join(root, filename), 'rb') as filehandle:
                    image = np.array(object=Image.open(fp=filehandle))
                    world1 = World.from_image(image[:, :100, :])
                    world2 = World.from_image(image[:, 150:250, :])
                    world3 = World.from_image(image[:, 300:, :])
                yield identifier, (world1, world2, world3)
Exemplo n.º 2
0
def images_iter(directory, mode, parts=None):
    split = 'val' if mode == 'validation' else mode
    if parts is not None:
        split += parts[mode]
    directory = os.path.join(directory, 'images', split)
    for root, dirs, files in os.walk(directory):
        assert root == directory
        assert not dirs
        for n in range(len(files)):
            filename = 'CLEVR_{}_{:0>6}.png'.format(split, n)
            files.remove(filename)
            with open(os.path.join(root, filename), 'rb') as filehandle:
                image = Image.open(fp=filehandle)
                world = World.from_image(image)
            yield world
Exemplo n.º 3
0
 def deserialize_value(value_name, value_type, read_file, word2id=None):
     if value_type == 'int':
         value = read_file(value_name + '.txt')
         value = [int(x) for x in value.split()]
         return value
     elif value_type == 'float':
         value = read_file(value_name + '.txt')
         value = [float(x) for x in value.split()]
         return value
     elif value_type == 'vector':
         value = read_file(value_name + '.txt')
         value = [[float(x) for x in vector.split(',')]
                  for vector in value.split()]
         return value
     elif value_type == 'text':
         assert word2id
         value = read_file(value_name + '.txt')
         value = [[word2id[word] for word in text.split(' ')]
                  for text in value.split('\n')[:-1]]
         return value
     elif value_type == 'world':
         value = []
         n = 0
         while True:
             image_bytes = read_file('{}-{}.bmp'.format(value_name, n),
                                     binary=True)
             if image_bytes is None:
                 break
             image_bytes = BytesIO(image_bytes)
             image = Image.open(image_bytes)
             value.append(World.from_image(image))
             n += 1
         return value
     elif value_type == 'model':
         value = read_file(value_name + '.json')
         value = json.loads(value)
         return value
Exemplo n.º 4
0
 def deserialize_value(value_name,
                       value_type,
                       read_file,
                       num_concat_worlds=0,
                       word2id=None):
     value_type, alts = alternatives_type(value_type=value_type)
     if value_type == 'int':
         value = read_file(value_name + '.txt')
         if alts:
             value = [[int(x) for x in xs.split(';')]
                      for xs in value.split('\n')[:-1]]
         else:
             value = [int(x) for x in value.split('\n')[:-1]]
         return value
     elif value_type == 'float':
         value = read_file(value_name + '.txt')
         if alts:
             value = [[float(x) for x in xs.split(';')]
                      for xs in value.split('\n')[:-1]]
         else:
             value = [float(x) for x in value.split('\n')[:-1]]
         return value
     elif value_type == 'vector(int)':
         value = read_file(value_name + '.txt')
         if alts:
             value = [[[int(x) for x in vector.split(',')]
                       for vector in vectors.split(';')]
                      for vectors in value.split('\n')[:-1]]
         else:
             value = [[int(x) for x in vector.split(',')]
                      for vector in value.split('\n')[:-1]]
         return value
     elif value_type == 'vector(float)':
         value = read_file(value_name + '.txt')
         if alts:
             value = [[[float(x) for x in vector.split(',')]
                       for vector in vectors.split(';')]
                      for vectors in value.split('\n')[:-1]]
         else:
             value = [[float(x) for x in vector.split(',')]
                      for vector in value.split('\n')[:-1]]
         return value
     elif value_type == 'text':
         assert word2id
         value = read_file(value_name + '.txt')
         if alts:
             value = [[[word2id[word] for word in text.split(' ')]
                       for text in texts.split('\n')]
                      for texts in value.split('\n\n')[:-1]]
         else:
             value = [[word2id[word] for word in text.split(' ')]
                      for text in value.split('\n')[:-1]]
         return value
     elif value_type == 'world':
         if num_concat_worlds:
             size = ceil(sqrt(num_concat_worlds))
             image_bytes = read_file(value_name + '.bmp', binary=True)
             assert image_bytes is not None
             image_bytes = BytesIO(image_bytes)
             image = Image.open(image_bytes)
             worlds = World.from_image(image)
             height = worlds.shape[0] // ceil(num_concat_worlds / size)
             assert worlds.shape[0] % ceil(num_concat_worlds / size) == 0
             width = worlds.shape[1] // size
             assert worlds.shape[1] % size == 0
             value = []
             for y in range(ceil(num_concat_worlds / size)):
                 for x in range(size if y < num_concat_worlds //
                                size else num_concat_worlds % size):
                     value.append(worlds[y * height:(y + 1) * height,
                                         x * width:(x + 1) * width, :])
         else:
             value = []
             n = 0
             while True:
                 image_bytes = read_file('{}-{}.bmp'.format(value_name, n),
                                         binary=True)
                 if image_bytes is None:
                     break
                 image_bytes = BytesIO(image_bytes)
                 image = Image.open(image_bytes)
                 value.append(World.from_image(image))
                 n += 1
         return value
     elif value_type == 'model':
         value = read_file(value_name + '.json')
         value = json.loads(value)
         return value