Beispiel #1
0
    def get(self,
            idx=None,
            model_args=None,
            random_aug=True,
            id=None,
            svg: SVG = None):

        if id is None:
            idx = idx % len(self.df)
            id = self.idx_to_id(idx)

        # new
        elif idx is None:
            idx = np.where((self.df['id'] == id).to_numpy())[0][0]
        # new

        if svg is None:
            svg = self._load_svg(id)

            svg = SVGDataset.preprocess(svg, augment=random_aug)

        t_sep, fillings = svg.to_tensor(
            concat_groups=False, PAD_VAL=self.PAD_VAL), svg.to_fillings()

        label = self.get_label(idx)

        return self.get_data(t_sep,
                             fillings,
                             model_args=model_args,
                             label=label)
Beispiel #2
0
    def visualize(self, model, output, train_vars, step, epoch, summary_writer,
                  visualization_dir):
        device = next(model.parameters()).device

        # Reconstruction
        for i, data in enumerate(train_vars.x_inputs_train):
            model_args = batchify((data[key] for key in self.model_args),
                                  device)
            commands_y, args_y = model.module.greedy_sample(*model_args)
            tensor_pred = SVGTensor.from_cmd_args(commands_y[0].cpu(),
                                                  args_y[0].cpu())

            try:
                svg_path_sample = SVG.from_tensor(
                    tensor_pred.data, viewbox=Bbox(256),
                    allow_empty=True).normalize().split_paths().set_color(
                        "random")
            except:
                continue

            tensor_target = data["tensor_grouped"][0].copy().drop_sos().unpad()
            svg_path_gt = SVG.from_tensor(
                tensor_target.data, viewbox=Bbox(
                    256)).normalize().split_paths().set_color("random")

            img = make_grid([svg_path_sample,
                             svg_path_gt]).draw(do_display=False,
                                                return_png=True,
                                                fill=False,
                                                with_points=False)
            summary_writer.add_image(f"reconstructions_train/{i}",
                                     TF.to_tensor(img), step)
Beispiel #3
0
    def get(self,
            idx=0,
            model_args=None,
            random_aug=True,
            id=None,
            svg: SVG = None):
        item = self.data[idx]
        if self.svg and self.svg_cmds:
            tens_scene = []
            tens_path = []
            if self.add_scene_svg and len(item['path']) != 0:
                tens_scene = self.simplify(SVG.from_tensor(
                    item['path'])).split_paths().to_tensor(concat_groups=False)

            if self.add_history_agent_svg and len(item['history_agent']) != 0:
                tens_path = self.normalize_history(
                    SVG.from_tensor(
                        item['history_agent'])).split_paths().to_tensor(
                            concat_groups=False)

            if self.add_scene_svg and len(item['path_type']) != 0:
                tens_scene = apply_colors(tens_scene, item['path_type'])

            if self.add_history_agent_svg and len(
                    item['history_agent_type']) != 0:
                tens_path = apply_colors(tens_path, item['history_agent_type'])

            MAX_NUM_AGENTS = self.MAX_NUM_AGENTS
            item['agents_availabilty'] = np.ones((item['agents_num']),
                                                 dtype=bool)
            if item['agents_availabilty'].shape[0] < MAX_NUM_AGENTS:
                item['agents_availabilty'] = np.concatenate(
                    (item['agents_availabilty'],
                     np.zeros((MAX_NUM_AGENTS - item['agents_num']),
                              dtype=bool)))

            del item['path']
            del item['path_type']
            del item['history_agent']
            del item['history_agent_type']

            if self.add_history_agent_svg and self.add_scene_svg:
                tens = tens_scene + tens_path
            elif not self.add_history_agent_svg:
                tens = tens_scene
            elif not self.add_scene_svg:
                tens = tens_path
            item['image'] = self.get_data(idx,
                                          tens,
                                          None,
                                          model_args=model_args,
                                          label=None)
        if item['image'] is None:
            return None
        return item
Beispiel #4
0
    def __init__(self, index, keyframe=False, svg=None):
        self.index = index
        self.keyframe = keyframe

        if svg is None:
            svg = SVG([], viewbox=Bbox(256))
        self.svg = svg

        self.kivy_bezierpaths = None
Beispiel #5
0
    def _load_svg(self, icon_id):
        svg = SVG.load_svg(os.path.join(self.data_dir, f"{icon_id}.svg"))

        if not self.already_preprocessed:
            svg.fill_(False)
            svg.normalize().zoom(0.9)
            svg.canonicalize()
            svg = svg.simplify_heuristic()

        return svg
    def get(self, idx=0, model_args=None, random_aug=True, id=None, svg: SVG=None):
        if id is None:
            idx = idx % len(self.df)
            id = self.idx_to_id(idx)

        if svg is None:
            tensors, fillings = self._load_tensor(id)
            t_sep = random.choice(tensors) if random_aug else tensors[0]
        else:
            t_sep, fillings = svg.to_tensor(concat_groups=False, PAD_VAL=self.PAD_VAL), svg.to_fillings()

        label = self.get_label(idx)

        return self.get_data(t_sep, fillings, model_args=model_args, label=label)
Beispiel #7
0
def preprocess_svg(svg_file, output_folder, meta_data):
    filename = os.path.splitext(os.path.basename(svg_file))[0]

    svg = SVG.load_svg(svg_file)
    svg.fill_(False)
    svg.normalize()
    svg.zoom(4)
    svg.canonicalize()
    svg = svg.simplify_heuristic()

    svg.save_svg(os.path.join(output_folder, f"{filename}.svg"))

    len_groups = [path_group.total_len() for path_group in svg.svg_path_groups]

    meta_data[filename] = {
        "id": filename,
        "total_len": sum(len_groups),
        "nb_groups": len(len_groups),
        "len_groups": len_groups,
        "max_len_group": max(len_groups)
    }
Beispiel #8
0
def decode(z):
    commands_y, args_y = model.greedy_sample(z=z)
    tensor_pred = SVGTensor.from_cmd_args(commands_y[0].cpu(), args_y[0].cpu())
    svg_path_sample = SVG.from_tensor(tensor_pred.data, viewbox=Bbox(256))

    return svg_path_sample
Beispiel #9
0
def normalized_path(svg_path):
    svg = SVG([svg_path.copy().to_group()], viewbox=Bbox(256)).normalize()
    return svg[0].path
Beispiel #10
0
def preprocess_svg_path(svg_path: SVGPath, force_smooth=False):
    svg = SVG([svg_path.to_group()], viewbox=Bbox(256)).normalize()
    svg.canonicalize()
    svg.filter_duplicates()
    svg = svg.simplify_heuristic(force_smooth=force_smooth)
    svg.normalize()
    svg.numericalize(256)

    return svg[0].path
Beispiel #11
0
    def load_frames(self, frames):
        self.frames = [Frame.load_dict(frame) for frame in frames]

        for frame in self.frames:
            frame.svg = SVG.load_svg(
                os.path.join(self.base_dir, f"{frame.index}.svg"))