Beispiel #1
0
Datei: init.py Projekt: vb99/aim
def init(overwrite=False):
    # Init repo if doesn't exist and return repo instance
    repo = AimRepo.get_working_repo()
    if not repo:
        repo = AimRepo(os.getcwd())
        repo.init()

    # Check if repo index is empty or not
    # Reset index or commit according to `overwrite` argument
    if not repo.is_index_empty():
        if overwrite:
            repo.reset_index()
        else:
            repo.commit(str(uuid.uuid1()), int(time.time()))
Beispiel #2
0
    def _parse_env_vars(self, env_vars):
        env_vars = env_vars or ''
        env_vars_arr = env_vars.split(' ')
        filtered_env_vars = []

        automated = False
        automated_branch = None
        automated_commit = None
        for e in env_vars_arr:
            if AIM_AUTOMATED_EXEC_ENV_VAR in e:
                automated = True
            elif AIM_BRANCH_ENV_VAR in e:
                _, _, automated_branch = e.rpartition('=')
            else:
                filtered_env_vars.append(e)

        if automated:
            if not automated_branch:
                automated_branch = AIM_DEFAULT_BRANCH_NAME
            automated_commit = AimRepo.generate_commit_hash()

            filtered_env_vars.append('{}={}'.format(AIM_BRANCH_ENV_VAR,
                                                    automated_branch))
            filtered_env_vars.append('{}={}'.format(AIM_COMMIT_ENV_VAR,
                                                    automated_commit))
            filtered_env_vars.append('{}={}'.format(AIM_PROCESS_ENV_VAR,
                                                    self.process_uuid))
            filtered_env_vars.append('{}=1'.format(AIM_AUTOMATED_EXEC_ENV_VAR))

        return {
            'env_vars': ' '.join(filtered_env_vars),
            'automated': automated,
            'automated_branch': automated_branch,
            'automated_commit': automated_commit,
        }
Beispiel #3
0
def get_repo():
    global repo
    if repo is None:
        # Get aim repo from working directory
        repo = AimRepo.get_working_repo()
        if repo is not None:
            # Finalize and close storage at program exit
            atexit.register(repo.close_records_storage)
    return repo
Beispiel #4
0
def init(repo):
    re_init = False
    # Check whether repo already exists
    if repo is not None:
        re_init = click.confirm('Aim repository is already initialized. ' +
                                'Do you want to re-initialize it?')
        if not re_init:
            return
        # Reinitialize repo -> clear old one and init empty repo
        repo.rm()

    repo = AimRepo(os.getcwd())

    if repo.init():
        if re_init:
            click.echo(
                'Re-initialized empty Aim repository in {}'.format(repo))
        else:
            click.echo('Initialized empty Aim repository in {}'.format(repo))
Beispiel #5
0
def de_entry_point(ctx):
    repo = ctx.obj or AimRepo.get_working_repo()
    if repo is None:
        repo_init_alert()
        exit()

    ctx.obj = repo

    if not AimContainer.is_docker_installed():
        docker_requirement_alert()
        exit()
Beispiel #6
0
def get_repo():
    # Get ENV VARS
    branch_name = os.getenv(AIM_BRANCH_ENV_VAR)
    commit_hash = os.getenv(AIM_COMMIT_ENV_VAR)

    global repo
    if repo is None:
        # Get aim repo from working directory
        repo = AimRepo.get_working_repo(branch_name, commit_hash)
        if repo is not None:
            # Finalize and close storage at program exit
            atexit.register(repo.close_records_storage)
    return repo
Beispiel #7
0
    def _save_record(repo: AimRepo,
                     artifact: Artifact,
                     record: Record,
                     dir_path: str = None):
        if record.binary_type is Artifact.IMAGE:
            # Get image name and abs path
            img_name_time = math.floor(time.time() * 1000)
            img_name_random = random_str(10)
            img_name = '{time}__{random}.jpg'.format(time=img_name_time,
                                                     random=img_name_random)
            res = repo.store_image(img_name, record.cat)

            # Save image at specified path
            artifact.save_blobs(res['path'], res['abs_path'])
        elif record.binary_type == Artifact.MODEL:
            # Get model name, directory and zip archive paths
            file_res = repo.store_model_file(record.name, record.cat)

            # Save model at specified path
            model_save_res = artifact.save_blobs(file_res)

            res = repo.store_model(record.name, record.data['model_name'],
                                   record.data['epoch'], record.data['meta'],
                                   model_save_res, record.cat)

            # Archive model directory
            repo.archive_dir(res['zip_path'], res['dir_path'])
        elif record.binary_type == Artifact.PROTOBUF:
            writer_type = RecordWriter.AIMRECORDS_WRITER
            write_mode = 'w' if record.is_singular else 'a'
            writer = RecordWriter.get_writer(writer_type, repo.records_storage)
            writer.write(artifact.get_inst_unique_name(), write_mode,
                         record.content)
            res = repo.store_artifact(record.name, record.cat, record.data,
                                      writer_type, record.binary_type)
        else:
            if record.binary_type == Artifact.JSON:
                writer = RecordWriter.JSON_WRITER
            else:
                writer = RecordWriter.JSON_LOG_WRITER
            file_name = '{}.log'.format(record.name)
            res = repo.store_file(file_name, record.name, record.cat,
                                  record.data, dir_path)
            write_mode = 'w' if record.is_singular else 'a'
            writer = RecordWriter.get_writer(writer)
            writer.write(res['abs_path'], write_mode, record.content)

        return res
Beispiel #8
0
    def save(self, repo: AimRepo, artifact: Artifact) -> bool:
        """
        Stores serialized instance into .aim repo
        """
        item = artifact.serialize()
        res = None

        if isinstance(item, Record):
            res = ArtifactWriter._save_record(repo, artifact, item)
        elif isinstance(item, RecordCollection):
            dir_path, dir_rel_path = repo.store_dir(item.name, item.cat,
                                                    item.data)
            res = []
            for record in item.records:
                # Store dir files
                res.append(
                    ArtifactWriter._save_record(repo, artifact, record,
                                                dir_rel_path))

        # Save dict
        return res
Beispiel #9
0
def error_logger():
    repo = AimRepo(os.getcwd())
    if repo is not None:
        setup()
        return logging.getLogger('error')
    return None
Beispiel #10
0
def activity_logger():
    repo = AimRepo(os.getcwd())
    if repo is not None:
        setup()
        return logging.getLogger('activity')
    return None
Beispiel #11
0
 def __init__(self):
     repo = AimRepo(os.getcwd())
     log_path = os.path.join(repo.get_logs_dir(), AIM_SDK_ERROR_LOG)
     logging.FileHandler.__init__(self, log_path, 'a')
Beispiel #12
0
def commit(repo, message, code):
    if repo is None:
        click.echo('Repository does not exist')
        return

    commit_hash = AimRepo.generate_commit_hash()
    message = message.strip() or int(time.time())

    # Check if there is anything to commit
    if repo.is_index_empty():
        click.echo('Nothing to commit')
        return

    branch_name = branch_hash = None
    if code:
        try:
            from aim.version_control.factory import Factory
        except Exception:
            click.echo('Git executable not found')
            return

        vc = Factory.create(Factory.GIT)

        # Check if version control repo exists
        if vc is None or vc.get_repo() is None:
            click.echo('No git repository (or any parent up to mount ' +
                       'point /) found. Initialize git repository ' +
                       'by running `git init`')
            return

        # Check untracked files
        if len(vc.get_untracked_files()):
            click.echo('You have untracked files, please add them to the ' +
                       'index before committing your changes by running ' +
                       '`git add -A`')
            return

        # Check if HEAD exists
        if not vc.get_head_hash():
            click.echo('Needed a single revision. ' +
                       'You do not have the git initial commit yet')
            return

        # Commit changes to a new created branch and return branch name
        branch_name, branch_hash = vc.commit_changes_to_branch(
            message, commit_hash)

        # Get the latest branch
        latest_branch = repo.get_latest_vc_branch() or {}
        latest_branch = latest_branch.get('branch')
        if latest_branch is None:
            latest_branch = vc.get_head_hash()

        # Get diff between current commit and latest commit(or HEAD)
        diff = vc.get_diff_text(latest_branch, branch_name)
        repo.save_diff(diff)

    # Commit
    commit_res = repo.commit(commit_hash, message, branch_name, branch_hash)

    click.echo(
        click.style('[{b}/{c} commit]'.format(b=commit_res['branch'],
                                              c=commit_res['commit']),
                    fg='yellow'))
    click.echo(message)
Beispiel #13
0
def init(overwrite=False, autocommit=True):
    # Automated commit
    automated_exec = os.getenv(AIM_AUTOMATED_EXEC_ENV_VAR)
    if autocommit and not automated_exec:
        automated_exec = True
        init_commit_hash = AimRepo.generate_commit_hash()
        init_branch_name = AimRepo.get_active_branch_if_exists() \
                           or AIM_DEFAULT_BRANCH_NAME
        set_automated_env_vars(init_commit_hash, init_branch_name)

    # Get Aim environment variables
    branch_name = os.getenv(AIM_BRANCH_ENV_VAR)
    commit_hash = os.getenv(AIM_COMMIT_ENV_VAR)

    # Init repo if doesn't exist and return repo instance
    repo = AimRepo.get_working_repo(branch_name, commit_hash)
    if not repo:
        repo = AimRepo(os.getcwd(), branch_name, commit_hash)
        repo.init()

    if not automated_exec:
        # Check if repo index is empty or not
        # Reset index or commit according to `overwrite` argument
        if not repo.is_index_empty():
            if overwrite:
                repo.reset_index()
            else:
                repo.commit(AimRepo.generate_commit_hash(), int(time.time()))
    else:
        # TODO: handle automated training overwrite
        pass

    # Handle aim commit
    if automated_exec:
        # Init commit
        repo.commit_init()

        # Finish commit
        import atexit
        atexit.register(repo.commit_finish)
Beispiel #14
0
def cli_entry_point(ctx, verbose):
    if verbose:
        click.echo('Verbose mode is on')

    # Init repo instance
    ctx.obj = AimRepo.get_working_repo()