Example #1
0
    def get_image(self, path, additional):
        '''
        Returns a transformed image by its absolute path.
        If cache is used - transformed image will be loaded if available,
        and saved to cache if not.
        '''
        if not self.use_cache:
            image = Image.open(path)
            return self.composed_all_transformers(image, additional)

        for key, post_transformers in self.split_transformers:
            if self.cache.exists(path, key):
                try:
                    image = self.cache.get(path, key)
                    image = apply_transformers_and_cache(image,
                                                         additional,
                                                         path,
                                                         post_transformers,
                                                         self.cache,
                                                         cache_full_size=False,
                                                         base_key=key)
                    return image
                except:
                    hashed_key = md5(path)
                    corrupted_file = self.cache._file_path(key, hashed_key)
                    warnings.warn(CORUPTED_FILE_ERR.format(sys.exc_info()[0]))
                    self.cache.rem(path, key)

        all_transformers = self.split_transformers[-1][1]
        image = Image.open(path)
        image = apply_transformers_and_cache(image, additional, path,
                                             all_transformers, self.cache)
        return image
Example #2
0
def launch_experiment(gpu, flag_string):
    '''
    Launch an experiment and direct logs and results to a unique filepath.
    Alert of something goes wrong.
    :gpu: gpu to run this machine on.
    :flag_string: flags to use for this model run. Will be fed into
    scripts/main.py
    '''
    if not os.path.isdir(args.log_dir):
        os.makedirs(args.log_dir)

    log_name = md5(flag_string)
    log_stem = os.path.join(args.log_dir, log_name)
    log_path = '{}.txt'.format(log_stem)
    results_path = "{}.results".format(log_stem)

    experiment_string = "CUDA_VISIBLE_DEVICES={} python -u scripts/main.py {} --results_path {}".format(
        gpu, flag_string, results_path)

    # forward logs to logfile
    if "--resume" in flag_string and not args.rerun_experiments:
        pipe_str = ">>"
    else:
        pipe_str = ">"

    shell_cmd = "{} {} {} 2>&1".format(experiment_string, pipe_str, log_path)
    print("Launched exp: {}".format(shell_cmd))

    if not os.path.exists(results_path) or args.rerun_experiments:
        subprocess.call(shell_cmd, shell=True)

    return results_path, log_path
Example #3
0
 def rem(self, image_path, attr_key):
     hashed_key = md5(image_path)
     try:
         os.remove(self._file_path(attr_key, hashed_key))
     # Don't raise error if file not exists.
     except OSError:
         pass
Example #4
0
    def add(self, image_path, attr_key, image):
        hashed_key = md5(image_path)
        file_dir = self._file_dir(attr_key)
        if not os.path.exists(file_dir):
            os.makedirs(file_dir)

        image.save(self._file_path(attr_key, hashed_key))
Example #5
0
def get_identifier(args):
    """
    Get a key that represents a specific run. The key should be unique for each set of arguments
    specified for a run so that it can uniquely identify that run. The key will be used to save
    the state of the run.
    Args:
        args: a dictionary of the parameters specified for a run
    Returns:
        key : a string identifying that run
    """
    hash_args = copy.deepcopy(vars(args))
    ## Delete args that could be variable even if it is the same run.

    for attr in EXCLUDED_ARGS:
        if attr in hash_args:
            del hash_args[attr]

    ordered_args = collections.OrderedDict(sorted(hash_args.items(), key=lambda t: t[0]))
    parameters_string = ''.join([str(i) for i in ordered_args.values()])
    key = md5(parameters_string)
    return key
Example #6
0
 def get(self, image_path, attr_key):
     hashed_key = md5(image_path)
     return Image.open(self._file_path(attr_key, hashed_key))
Example #7
0
 def exists(self, image_path, attr_key):
     hashed_key = md5(image_path)
     return os.path.isfile(self._file_path(attr_key, hashed_key))