Example #1
0
    def clear_screenshots(self):
        """
        ## Clear screenshots
        """
        path = pathlib.Path(self.info.screenshots_path)
        if path.exists():
            util.rm_tree(path)

        path.mkdir(parents=True)
Example #2
0
    def clear_checkpoints(self):
        """
        ## Clear old checkpoints

        We run this when running a new fresh trial
        """
        path = pathlib.Path(self.info.checkpoint_path)
        if path.exists():
            util.rm_tree(path)
Example #3
0
    def clear_summaries(self):
        """
        ## Clear TensorBoard summaries

        We run this when running a new fresh trial
        """
        path = pathlib.Path(self.info.summary_path)
        if path.exists():
            util.rm_tree(path)
Example #4
0
    def save_checkpoint_numpy(self,
                              session: tf.Session,
                              global_step: int):
        """
        ## Save model as a set of numpy arrays
        """

        checkpoints_path = pathlib.Path(self.info.checkpoint_path)
        if not checkpoints_path.exists():
            checkpoints_path.mkdir()

        checkpoint_path = checkpoints_path / str(global_step)
        assert not checkpoint_path.exists()

        checkpoint_path.mkdir()

        values = session.run(self.__variables)

        # Save each variable
        files = {}
        for variable, value in zip(self.__variables, values):
            file_name = tf_util.variable_name_to_file_name(
                tf_util.strip_variable_name(variable.name))
            file_name = f"{file_name}.npy"
            files[variable.name] = file_name

            np.save(str(checkpoint_path / file_name), value)

        # Save header
        with open(str(checkpoint_path / "info.json"), "w") as f:
            f.write(json.dumps(files))

        # Delete old checkpoints
        for c in checkpoints_path.iterdir():
            if c.name != checkpoint_path.name:
                util.rm_tree(c)
Example #5
0
def save_embeddings(*,
                    path: Path,
                    embeddings: np.ndarray,
                    images: Optional[np.ndarray],
                    labels: Optional[List[str]]):
    if path.exists():
        util.rm_tree(path)
    path.mkdir(parents=True)

    sess = tf.compat.v1.InteractiveSession()
    name = random_string()

    embeddings_variable = tf.Variable(embeddings, trainable=False, name=name)

    tf.global_variables_initializer().run()

    saver = tf.compat.v1.train.Saver()
    writer = tf.compat.v1.summary.FileWriter(str(path), sess.graph)

    config = projector.ProjectorConfig()
    embed = config.embeddings.add()
    embed.tensor_name = embeddings_variable.name

    if labels is not None:
        embed.metadata_path = 'metadata.tsv'
        save_labels(path / 'metadata.tsv', labels)

    if images is not None:
        embed.sprite.image_path = "sprites.png"
        save_sprite_image(path / 'sprites.png', images)

        embed.sprite.single_image_dim.extend([images.shape[1], images.shape[2]])

    projector.visualize_embeddings(writer, config)

    saver.save(sess, str(path / 'a_model.ckpt'), global_step=0)