Exemplo n.º 1
0
 def make_labels(self):
     self.labels["score"].text = str(self.score)
     self.labels["lines"].text = str(self.lines)
     self.labels["level"].text = str(self.current_level)
     self.pause_label = util.make_label(
         self.pause_text,
         32, 320, 240, (255, 255, 255, 255), True, None)
     self.init_blocks_label = util.make_label(
         self.init_blocks_text,
         12, 320, 200, (255, 255, 255, 255), True, None)
     for name, label in self.labels.items():
         pos = util.res(*label.original_pos)
         label.x = pos["w"]
         label.y = pos["h"]
         label.font_size = pos["wr"] * label.original_size
         self.labels[name] = label
Exemplo n.º 2
0
 def make_labels(self):
     self.labels = []
     for c in range(0, 9):
         color = (255, 255, 255, 255)
         if self.writer_index == c:
             color = (100, 255, 100, 255)
         self.labels.append(util.make_label(
             self.chars[self.character_ids[c]].replace(" ", "."),
             26, 20+c*26, 10, color, False, None))
     color = (255, 255, 255, 255)
     if self.writer_index == 9:
         color = (100, 255, 100, 255)
     self.labels.append(util.make_label(
         "OK",
         26, 260, 10, color, False, None))
     no_highscores = False
     if self.config_string not in self.highscores:
         no_highscores = True
     elif len(self.highscores[self.config_string]) == 0:
         no_highscores = True
     if no_highscores:
         self.labels.append(util.make_label(
             "No Highscores for this config yet!",
             12,
             320,
             400,
             (255, 255, 255, 255), True, None))
     else:
         number = 1
         height = 400
         fs = 12
         self.labels.append(util.make_label(
             "pos{}name{}score{}lines{}date".format(
                 " "*6,
                 " "*16,
                 " "*11,
                 " "*22
             ), fs, 16, height, (255, 255, 255, 255), False, None))
         height -= fs
         scores = self.highscores[self.config_string]
         for player_score in sorted(scores,
                                    key=lambda k: k["score"],
                                    reverse=True):
             self.labels.append(util.make_label(
                 "{} {} {} {} {}".format(
                     str(number).rjust(2, " "),
                     str(player_score["name"]).rjust(9, " "),
                     str(player_score["score"]).rjust(20, " "),
                     str(player_score["lines"]).rjust(15, " "),
                     str(self.dt(player_score["date"])).rjust(25, " ")
                 ), fs, 24, height, (255, 255, 255, 255), False, None))
             height -= fs
             number += 1
             if number > 30:
                 break
     self.labels.append(util.make_label(
         "Highscores", 40, 320, 440, (255, 255, 255, 255), True, None))
     self.labels.append(util.make_label(
         "Score: {}".format(self.score),
         26, 320, 10, (255, 255, 255, 255), False, None))
Exemplo n.º 3
0
def figure_def(pargs, kwargs, node):
    """Handle [% figure slug=slug img=img alt=alt caption=cap %] figure definition."""
    slug = kwargs["slug"]
    figure = util.get_config("figures")[slug]
    label = util.make_label("figure", figure.number)
    width = f'width="{figure.width}"' if figure.width else ""
    return dedent(f"""\
    <figure id="{figure.slug}">
      <img src="./{figure.img}" alt="{figure.alt}" {width}/>
      <figcaption markdown="1">{label}: {figure.caption}</figcaption>
    </figure>
    """)
Exemplo n.º 4
0
def section_ref(pargs, kwargs, node):
    """Handle [% x slug %] section reference."""
    if len(pargs) != 1:
        util.fail(f"Badly-formatted 'x' shortcode {pargs} in {node.filepath}")

    headings = util.get_config("headings")

    slug = pargs[0]
    heading = headings[slug]
    label = util.make_label("part", heading.number)
    anchor = f"#{slug}" if (len(heading.number) > 1) else ""

    return f'<a href="@root/{heading.fileslug}/{anchor}">{label}</a>'
Exemplo n.º 5
0
def _patch_heading(match):
    """Modify a single heading."""
    headings = util.get_config("headings")
    prefix = match.group(1)
    text = match.group(2)
    attributes = match.group(3) or ""
    slug = match.group(4)

    if slug is None:
        return f"{prefix} {text} {attributes}".rstrip()
    else:
        label = util.make_label("part", headings[slug].number)
        return f"{prefix} {label}: {text} {attributes}"
Exemplo n.º 6
0
def main():
    p = argparse.ArgumentParser(description='Display a kernel.')
    p.add_argument('-out', help='output to *.png file instead of viewing')
    p.add_argument('k', nargs='*', help='path to kernel(s)')
    args = p.parse_args()

    out = None

    for fn in args.k:
        print('Loading', fn)
        step, kernel = util.load_kernel(fn)
        print('  Step', step)
        print('  Kernel shape is', kernel.shape)
        print('  Min', np.min(kernel))
        print('  Max', np.max(kernel))
        print('  Mean', np.mean(kernel))
        print('  Sum', np.sum(kernel))
        print('  Sum of abs', np.sum(np.abs(kernel)))
        print('  RMS', np.sqrt(np.mean(kernel * kernel)))

        render = util.vis_hwoi(kernel, doubles=2)
        render = util.hstack([render, util.make_label(fn)], 5)

        if out is None:
            out = render
        else:
            out = util.vstack([out, render], 5)

    out = util.border(out, 5)

    if args.out is not None:
        util.save_image(args.out, out)
        print('Written to', args.out)
    else:
        print('Press ESC to close window.')

        def render_fn():
            return out

        util.viewer(None, render_fn)
Exemplo n.º 7
0
 def _replace(match):
     caption = match.group(1)
     slug = match.group(2)
     table = util.get_config("tables")[slug]
     label = util.make_label("table", table.number)
     return f'<div class="table"><table id="{slug}"><caption>{label}: {caption}</caption>'
Exemplo n.º 8
0
    """Handle [% t slug %] table reference."""
    # Shortcode used improperly.
    if len(pargs) != 1:
        util.fail(f"Badly-formatted 't' shortcode {pargs} in {node.filepath}")

    # Haven't collected information yet.
    if (tables := util.get_config("tables")) is None:
        return ""

    # Create cross-reference.
    slug = pargs[0]
    if slug not in tables:
        util.fail(
            f"Unknown table cross-reference slug {slug} in {node.filepath}")
    table = tables[slug]
    label = util.make_label("table", table.number)
    return f'<a class="tblref" href="@root/{table.fileslug}/#{slug}">{label}</a>'


@ivy.filters.register(ivy.filters.Filter.NODE_HTML)
def table_caption(text, node):
    """Get the caption in the right place."""
    def _replace(match):
        caption = match.group(1)
        slug = match.group(2)
        table = util.get_config("tables")[slug]
        label = util.make_label("table", table.number)
        return f'<div class="table"><table id="{slug}"><caption>{label}: {caption}</caption>'

    return util.TABLE_DIV.sub(_replace, text)
Exemplo n.º 9
0
def main(argv=()):
    if argv[1:]:
        raise ValueError("leftover arguments: %r" % argv[1:])

    config = H(data_dir=FLAGS.data_dir,
               base_output_dir=FLAGS.base_output_dir,
               basename=FLAGS.basename,
               resume=FLAGS.resume,
               trace_fraction=FLAGS.trace_fraction)

    if FLAGS.hpfile:
        with open(FLAGS.hpfile) as hpfile:
            hpstring = hpfile.read()
    else:
        hpstring = FLAGS.hp
    config.hp = H(util.parse_hp(hpstring))
    print str(config.hp)
    config.label = util.make_label(config)
    dirname = "%s_%s" % (datetime.datetime.now().isoformat(), config.label)
    dirname = dirname[:255]  # >:-(((((((((((((((((((((((((((((((((((((((((
    config.output_dir = os.path.join(config.base_output_dir, dirname)

    data = datasets.MscocoTF(config)
    config.hp.caption.depth = data.caption_depth

    # NOTE: all hyperparameters must be set at this point
    prepare_run_directory(config)

    model = models.Model(config.hp)

    config.hp.masker.image = config.hp.image  # -_-
    config.masker = maskers.make(config.hp.masker.kind, hp=config.hp.masker)

    config.global_step = tf.Variable(0, name="global_step", trainable=False)

    trainer = Trainer(data, model, config)
    tf.get_variable_scope().reuse_variables()
    evaluator = Evaluator(data, model, config)

    earlystopper = EarlyStopper(config, trainer.graph.lr_decay_op)
    supervisor = tf.train.Supervisor(logdir=config.output_dir, summary_op=None)
    with supervisor.managed_session() as session:
        while True:
            global_step = tf.train.global_step(session, config.global_step)

            if supervisor.should_stop():
                print "supervisor says should stop"
                break
            if earlystopper.should_stop(global_step):
                print "earlystopper says should stop"
                break

            trainer(session, supervisor)
            sys.stdout.write("\r%i " % global_step)
            sys.stdout.flush()

            if global_step % config.hp.validate.interval == 0:
                values = evaluator(session, supervisor)
                earlystopper.track(global_step, values.model.loss, session)
                print "%5i loss:%10f loss asked:%10f loss given:%10f lr:%6g best:%10f age:%6i" % (
                    global_step,
                    values.model.loss,
                    values.model.loss_asked,
                    values.model.loss_given,
                    # NOTE: earlystopper.best_loss is best of median filtered losses
                    trainer.graph.lr.eval(session),
                    earlystopper.best_loss,
                    earlystopper.age)

            if global_step >= config.hp.num_steps:
                print "hp.num_steps reached"
                break
Exemplo n.º 10
0
def _modify_headings(node):
    node.text = util.HEADING.sub(_patch_heading, node.text)
    headings = util.get_config("headings")
    if node.slug in headings:
        node.meta["major"] = util.make_label("part", headings[node.slug].number)
Exemplo n.º 11
0
 def __init__(self, config):
     super().__init__()
     self.config = config
     self.name = "game"
     self.desired_scene = "game"
     self.ready = False
     self.entities = []
     self.batch = pyglet.graphics.Batch()
     self.block_sizes = []
     self.blocks = {}
     self.bags = {}
     self.score = 0
     self.current_level = config["first_level"]
     self.paused = True
     self.pause_text = "Generating polyominoes"
     self.init_blocks_text = ""
     self.pause_label = util.make_label(
         self.pause_text,
         32, 320, 240, (255, 255, 255, 255), True, None)
     self.init_blocks_label = util.make_label(
         "", 12, 320, 200, (255, 255, 255, 255), True, None)
     self.shade = Shade((30, 30, 30, 150))
     self.entities.append(self.shade)
     self.lines = 0
     # labels
     height = 470
     fs = 14
     self.labels = {}
     for label in ["score", "lines", "level"]:
         label_config = [
             fs, 540, height, (255, 255, 255, 255), False, self.batch
         ]
         self.labels["text_{}".format(label)] = util.make_label(
             label.title(), *label_config)
         self.labels["text_{}".format(label)].original_pos = [540, height]
         self.labels["text_{}".format(label)].original_size = fs
         height -= fs
         label_config = [
             fs, 540, height, (255, 255, 255, 255), False, self.batch
         ]
         self.labels[label] = util.make_label("", *label_config)
         self.labels[label].original_pos = [540, height]
         self.labels[label].original_size = fs
         height -= fs
     # block grid
     self.block_grid = []
     for i in range(0, config["height"]):
         self.block_grid.append([])
         for _ in range(0, config["width"]):
             self.block_grid[i].append(None)
     # graphical grid size
     max_width = 540
     max_height = 480
     self.grid_size = int(min([
         max_height / (config["height"] + 1),
         max_width / (config["width"] + 2)
     ]))
     # walls
     wall = {
         "x": 0,
         "y": 480 - int(config["height"]*self.grid_size + self.grid_size),
         "width": int(self.grid_size),
         "height": int(config["height"]*self.grid_size)
     }
     self.entities.append(Wall(wall, self.batch))
     width = config["width"]*int(self.grid_size) + 2*int(self.grid_size)
     wall["x"] = width - int(self.grid_size)
     self.entities.append(Wall(wall, self.batch))
     wall["x"] = 0
     wall["width"] = width
     wall["height"] = int(self.grid_size)
     self.entities.append(Wall(wall, self.batch))
     # initial speed
     spl = config["speed_per_level"]
     speed = 10 / config["speed"] + spl * (self.current_level - 1)
     pyglet.clock.schedule_interval(self.game_loop, speed)
     # loop counter
     self.loop_counter = 0
Exemplo n.º 12
0
 def make_labels(self):
     self.labels = []
     # title
     self.labels.append(util.make_label(
         "Polyominomania", 66, 320, 440,
         (255, 255, 200, 255), True, None))
     # config list
     height = 350
     for item in self.list_items:
         color = (255, 255, 255, 255)
         if self.selected_in_list == item:
             color = (255, 255, 120, 255)
         name = item.replace(".json", "")[:32]
         if name != item.replace(".json", ""):
             name += ".."
         self.labels.append(util.make_label(
             name, 10, 10, height, color, False, None))
         height -= 10
     # if a valid config is found, show details about the config
     # else list the problem in red
     if self.valid_config:
         color = (255, 255, 255, 255)
         head_color = (200, 200, 255, 255)
         height = 350
         fs = 12
         # Basic information
         self.labels.append(util.make_label(
             "Information", 18, 270, height, head_color, False, None))
         height -= fs
         for part_of_desc in util.split(self.config["description"], 40):
             self.labels.append(util.make_label(
                 part_of_desc, fs, 270, height, color, False, None))
             height -= fs
         color = (200, 255, 200, 255)
         self.labels.append(util.make_label(
             self.config_log, fs, 270, height, color, False, None))
         height -= fs
         color = (255, 255, 255, 255)
         polyomino_string = "Polyominoes: {}".format(
             " ".join(self.config["polyominoes"]))
         for part_of_poly in util.split(polyomino_string, 40):
             self.labels.append(util.make_label(
                 part_of_poly,
                 fs, 270, height, color, False, None))
             height -= fs
         self.labels.append(util.make_label(
             "Lines needed per level: {}".format(
                 self.config["lines_per_level"]),
             fs, 270, height, color, False, None))
         height -= fs
         self.labels.append(util.make_label(
             "First level: {}".format(
                 self.config["first_level"]),
             fs, 270, height, color, False, None))
         height -= fs
         self.labels.append(util.make_label(
             "Next pieces: {}".format(
                 self.config["next_pieces"]),
             fs, 270, height, color, False, None))
         height -= fs
         self.labels.append(util.make_label(
             "Ghost piece: {}".format(
                 "visible" if self.config["ghost"] else "not visible"),
             fs, 270, height, color, False, None))
         height -= fs
         self.labels.append(util.make_label(
             "Grid size: {}x{}".format(
                 self.config["width"],
                 self.config["height"]),
             fs, 270, height, color, False, None))
         height -= 18
         # Scoring information
         self.labels.append(util.make_label(
             "Scoring", 18, 270, height, head_color, False, None))
         height -= fs
         for field in ["polyomino", "softdrop", "harddrop", "level_up"]:
             if self.config["scoring"][field] > 0:
                 self.labels.append(util.make_label(
                     "{} bonus: {}".format(
                         field.title().replace("_", " "),
                         self.config["scoring"][field]),
                     fs, 270, height, color, False, None))
                 height -= fs
         lines_string = "lines:"
         for i in range(1, len(self.config["scoring"]["lines"]) + 1):
             lines_string += " {}".format(
                 self.config["scoring"]["lines"][str(i)])
         for part_of_lines in util.split(lines_string, 40):
             self.labels.append(util.make_label(
                 part_of_lines,
                 fs, 270, height, color, False, None))
             height -= fs
         lines_level_string = "This does not increase per level"
         limit = len(self.config["scoring"]["lines_per_level"]) + 1
         for i in range(1, limit):
             if self.config["scoring"]["lines_per_level"][str(i)] > 0:
                 lines_level_string = "This does increase per level"
                 break
         self.labels.append(util.make_label(
             lines_level_string,
             fs, 270, height, color, False, None))
         height -= fs
     else:
         color = (255, 200, 200, 255)
         height = 350
         fs = 16
         # Show the error information
         for part_of_log in util.split(self.config_log, 36):
             self.labels.append(util.make_label(
                 part_of_log, fs, 270, height, color, False, None))
             height -= fs
     # Select button instructions
     self.labels.append(util.make_label(
         "Press Enter or Space to select",
         18, 270, 10, (255, 255, 255, 255), False, None))
Exemplo n.º 13
0
    """Handle [% f slug %] figure reference."""
    # Badly-formatted shortcode.
    if len(pargs) != 1:
        util.fail(f"Badly-formatted 'f' shortcode {pargs} in {node.filepath}")

    # Haven't collected information yet.
    if (figures := util.get_config("figures")) is None:
        return ""

    # Create cross-reference.
    slug = pargs[0]
    if slug not in figures:
        util.fail(
            f"Unknown figure cross-reference slug {slug} in {node.filepath}")
    figure = figures[slug]
    label = util.make_label("figure", figure.number)
    return f'<a class="figref" href="@root/{figure.fileslug}/#{slug}">{label}</a>'


@ivy.events.register(ivy.events.Event.EXIT_BUILD)
def copy_files():
    """Copy all referenced images files."""
    # Wrong part of the cycle.
    if (figures := util.get_config("figures")) is None:
        return

    # Copy files.
    for fig in figures.values():
        src, dst = util.make_copy_paths(fig.node, fig.img)
        shutil.copy(src, dst)
    def train(self, load_weight=False, print_every=500):
        MALE, FEMALE = 0, 1
        FAKE, REAL = 0, 1
        LAST_EPOCH = 0

        # If pretrained weights exist, load them and get last epoch and iteration
        if load_weight is True:
            LAST_EPOCH = int(util.read_log())
            self.load_pretrained(LAST_EPOCH)

        for epoch in range(LAST_EPOCH, self.num_epoch):
            self.set_optimizer(epoch)

            for iters, (image, label) in enumerate(self.dloader):
                # If a batch has only female or male images not mixed, just discard that batch.
                # It seems that it makes results even worse.

                if torch.sum(label == 0) == 0 or torch.sum(label == 1) == 0:
                    continue

                male_num = torch.sum(label == 0)
                female_num = torch.sum(label == 1)

                image, label = image.type(self.dtype), label.type(self.ltype)
                male_img, female_img = util.gender_divider(image, label)

                image, label = Variable(image), Variable(label)
                male_img, female_img = Variable(male_img), Variable(female_img)
                """
                1. Train D_M (Discriminator for male)

                Step 1. Hope D_M(male_img) == 1
                Step 2. Hope D_M(fake_img) == 0
                Step 3. Minimize classification loss
                """
                # D_M(male_img) == 1
                real_loss, fake_loss = 0, 0
                if male_num is not 0:
                    real_score, _ = self.D_M(male_img)
                    real_label = util.make_label(real_score.size(),
                                                 label=REAL,
                                                 noisy=True).type(self.dtype)
                    real_label = Variable(real_label)

                    # Loss for real male image
                    real_loss = self.criterion_gan(real_score, real_label)

                # Hope D_M(fake_img) == 0
                if female_num is not 0:
                    fake_img = self.G_FM(female_img)
                    fake_score, _ = self.D_M(fake_img)
                    fake_label = util.make_label(fake_score.size(),
                                                 label=FAKE,
                                                 noisy=False).type(self.dtype)
                    fake_label = Variable(fake_label)

                    # Loss for fake male image
                    fake_loss = self.criterion_gan(fake_score, fake_label)

                # Minimize classofication loss
                _, gender_score = self.D_M(image)
                cls_loss = self.criterion_cls(gender_score, label)

                # Final D_M loss(Multitask learning)
                D_loss = real_loss + fake_loss + cls_loss

                # Update
                self.all_optim_zero_grad()
                D_loss.backward()
                self.optim_D_M.step()
                """
                2. Train D_F (Discriminator for female)

                Step 1. Hope D_F(female_img) == 1
                Step 2. Hope D_F(fake_img) == 0
                Step 3. Minimize classification loss
                """
                # Hope D_F(female_img) == 1
                real_loss, fake_loss = 0, 0
                if female_num is not 0:
                    real_score, _ = self.D_F(female_img)
                    real_label = util.make_label(real_score.size(),
                                                 label=REAL,
                                                 noisy=True).type(self.dtype)
                    real_label = Variable(real_label)

                    # Loss for real female image
                    real_loss = self.criterion_gan(real_score, real_label)

                # Hope D_F(fake_img) == 0
                if male_num is not 0:
                    fake_img = self.G_MF(male_img)
                    fake_score, _ = self.D_F(fake_img)
                    fake_label = util.make_label(fake_score.size(),
                                                 label=FAKE,
                                                 noisy=False).type(self.dtype)
                    fake_label = Variable(fake_label)

                    # Loss for fake female image
                    fake_loss = self.criterion_gan(fake_score, fake_label)

                # Minimize classification loss
                _, gender_score = self.D_F(image)
                cls_loss = self.criterion_cls(gender_score, label)

                # Final D_F loss
                D_loss = real_loss + fake_loss + cls_loss

                # Get classification accuracy

                accuracy = util.get_cls_accuracy(gender_score.data, label.data)

                # Update
                self.all_optim_zero_grad()
                D_loss.backward()
                self.optim_D_F.step()
                """
                3. Traing G_MF, G_FM with process of
                   <1> Male(Real image) -> <2> Female(Fake image) -> <3> Male(Cycle)
                   
                Step 1. Hope D_F(<2>) == 1
                Step 2. Hope <2> to be classified as female
                Step 3. Hope <1> == <3>
                """
                if male_num is not 0:
                    fake_img = self.G_MF(male_img)
                    fake_score, gender_score = self.D_F(fake_img)

                    # Hope D_F((2)) == 1
                    real_label = util.make_label(fake_score.size(),
                                                 label=REAL,
                                                 noisy=False).type(self.dtype)
                    real_label = Variable(real_label)
                    gan_loss = self.criterion_gan(fake_score, real_label)

                    # Hope <2> to be classified as female
                    female_label = util.make_label(gender_score.size(0),
                                                   label=FEMALE,
                                                   noisy=False).type(
                                                       self.ltype)
                    female_label = Variable(female_label)
                    cls_loss = self.cls_lambda * self.criterion_cls(
                        gender_score, female_label)

                    # Hope <1> == <3>
                    cycle_img = self.G_FM(fake_img)
                    cyc_loss = self.cyc_lambda * self.criterion_cyc(
                        cycle_img, male_img)

                    # Final loss
                    G_loss = gan_loss + cls_loss + cyc_loss

                    # Update
                    self.all_optim_zero_grad()
                    G_loss.backward()
                    self.optim_G_MF.step()
                    self.optim_G_FM.step()
                """
                4. Traing G_MF, G_FM with process of
                   <1> Female(Real image) -> <2> Male(Fake image) -> <3> Female(Cycle)
                   
                Step 1. Hope D_M(<2>) == 1
                Step 2. Hope <2> to be classified as male
                Step 3. Hope <1> == <3>
                """
                if female_num is not 0:
                    fake_img = self.G_FM(female_img)
                    fake_score, gender_score = self.D_M(fake_img)

                    # Hope D_M(<2>) == 1
                    real_label = util.make_label(fake_score.size(),
                                                 label=REAL,
                                                 noisy=False).type(self.dtype)
                    real_label = Variable(real_label)
                    gan_loss = self.criterion_gan(fake_score, real_label)

                    # Hope <2> to be classified as male
                    male_label = util.make_label(gender_score.size(0),
                                                 label=MALE,
                                                 noisy=False).type(self.ltype)
                    male_label = Variable(male_label)
                    cls_loss = self.cls_lambda * self.criterion_cls(
                        gender_score, male_label)

                    # Hope <1> == <3>
                    cycle_img = self.G_MF(fake_img)
                    cyc_loss = self.cyc_lambda * self.criterion_cyc(
                        cycle_img, female_img)

                    # Final loss
                    G_loss = gan_loss + cls_loss + cyc_loss

                    # Update
                    self.all_optim_zero_grad()
                    G_loss.backward()
                    self.optim_G_MF.step()
                    self.optim_G_FM.step()

                if iters % print_every == 0:
                    util.print_result(epoch, iters, accuracy, D_loss, G_loss,
                                      gan_loss, cyc_loss, cls_loss)

            # Save parameters
            util.save_weight(self.D_M,
                             self.D_F,
                             self.G_MF,
                             self.G_FM,
                             num_epoch=epoch)
            util.write_log(epoch)