def param_mask_binary_depth_map_generator(lecture, batch_size=128, antialiasing=4, depth_scale=1/4): ignore = -0.25 background = 0 black = 51 white = 255 need_size = (1 - ignore) scale = 255 / need_size bw_artist = BlackWhiteArtist(black, white, background, antialiasing) depth_map_artist = DepthMapArtist() while True: ids, configs, structure = [a.astype(np.float32) for a in lecture.grid_params(batch_size)] grids, = draw_grids(ids, configs, structure, artist=bw_artist) depth_maps, = draw_grids(ids, configs, structure, artist=depth_map_artist, scales=[depth_scale]) depth32 = (depth_maps / 255.).astype(np.float32) grids32 = (grids / scale + ignore).astype(np.float32) param = np.concatenate( lecture.normalize(ids, configs, structure), axis=1) yield param, grids32, depth32
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 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 generator_3d_tags_with_depth_map(tag_distribution, batch_size=128, antialiasing=4, depth_scale=1/4): ignore = -0.25 background = 0 black = 51 white = 255 need_size = (1 - ignore) scale = 255 / need_size bw_artist = BlackWhiteArtist(black, white, background, antialiasing) depth_map_artist = DepthMapArtist() while True: labels = tag_distribution.sample(batch_size) grids, = draw_grids(labels, artist=bw_artist) depth_maps, = draw_grids(labels, artist=depth_map_artist, scales=[depth_scale]) depth_map_f32 = (depth_maps / 255.).astype(np.float32) grids_f32 = (grids / scale + ignore).astype(np.float32) norm_labels = tag_distribution.normalize(labels) yield norm_labels, grids_f32, depth_map_f32
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 generator_3d_tags_with_depth_map(tag_distribution, batch_size=128, antialiasing=4, depth_scale=1 / 4): ignore = -0.25 background = 0 black = 51 white = 255 need_size = (1 - ignore) scale = 255 / need_size bw_artist = BlackWhiteArtist(black, white, background, antialiasing) depth_map_artist = DepthMapArtist() while True: labels = tag_distribution.sample(batch_size) grids, = draw_grids(labels, artist=bw_artist) depth_maps, = draw_grids(labels, artist=depth_map_artist, scales=[depth_scale]) depth_map_f32 = (depth_maps / 255.).astype(np.float32) grids_f32 = (grids / scale + ignore).astype(np.float32) norm_labels = tag_distribution.normalize(labels) yield norm_labels, grids_f32, depth_map_f32