def masks(batch_size, scales=[1.]): batch_size += 64 - (batch_size % 64) generator = GridGenerator() artist = MaskGridArtist() for masks in generate_grids(batch_size, generator, artist=artist, scales=scales): yield masks[0].astype(np.float32)
def generated_3d_tags(tag_distribution, batch_size=128, artist=None, scale=1.): if artist is None: artist = MaskGridArtist() labels = tag_distribution.sample(batch_size) grids = draw_grids(labels, scales=[scale], artist=artist) assert len(grids) == 1 return labels, grids[0]
def grid_generator(curriculum_cb, batch_size=128, artist=None, scale=1.): if artist is None: artist = MaskGridArtist() while True: lecture = curriculum_cb.current_lecture() ids, configs, structure, grids = grids_from_lecture( lecture, batch_size, artist, scale) yield np.concatenate([ids, configs, structure], axis=1), grids
def grids_from_lecture(lecture, batch_size=128, artist=None, scale=1.): if artist is None: artist = MaskGridArtist() ids, configs, structure = lecture.grid_params(batch_size) grids = draw_grids(ids.astype(np.float32), configs.astype(np.float32), structure.astype(np.float32), scales=[scale], artist=artist) assert len(grids) == 1 return ids, configs, structure, grids[0]
def evaluate_loss(gt_files, name, loss_fn, loss_dict, visualise=True, bit_flips=0, translation=0, z_rotation=0): def outs_to_dict(outs, loss_dict): num_visual = len(loss_dict.visual) num_shape = len(loss_dict.shape) visual_dict = dict(zip(loss_dict.visual.keys(), outs[:num_visual])) shape_dict = dict(zip(loss_dict.shape.keys(), outs[num_visual:num_visual+num_shape])) print_dict = dict(zip(loss_dict.print.keys(), outs[num_visual+num_shape:])) return visual_dict, shape_dict, print_dict def alter_params(ids, configs): bs = len(configs) new_ids = [] for id in ids: flipped_bits = np.random.choice( NUM_MIDDLE_CELLS, bit_flips, replace=False) id[flipped_bits] += 1 id[id == 2] = 0 new_ids.append(id) angle = np.random.uniform(-np.pi, np.pi, (bs, 1)) configs[:, CONFIG_CENTER] += translation * \ np.concatenate((np.cos(angle), np.sin(angle)), axis=1) configs[:, CONFIG_ROTS[0]] += z_rotation return np.stack(new_ids), configs nb_views = 16 if type(visualise) == int: nb_views = visualise visualise = True batch_size = 8 loss = 0 num_batches = 0 for gt, ids, configs in gt_grids(gt_files, batch_size): configs[:, CONFIG_CENTER] = 0 ids, configs = alter_params(ids, configs) masks, = draw_grids(ids, configs, artist=MaskGridArtist()) outs = loss_fn(masks, gt) visual_dict, shape_dict, print_dict = outs_to_dict(outs[1:], loss_dict) if visualise: out_dict = {"1-masks": np_binary_mask(masks), "0-img": gt} visual_dict.update(out_dict) visualise_loss(visual_dict, shape_dict, print_dict, nb_views) visualise = False l = outs[0] loss += l num_batches += 1 loss /= num_batches return loss
def gen_mask_grids(nb_batches, batch_size=128, scales=[1.]): generator = GridGenerator() artist = MaskGridArtist() gen_grids = generate_grids(batch_size, generator, artist=artist, with_gird_params=True, scales=scales) if nb_batches == 'forever': counter = itertools.count() else: counter = range(nb_batches) for i in counter: masks = next(gen_grids) yield (masks[0].astype(floatX), ) + tuple(masks[1:])