Beispiel #1
0
 def _process_model_folder(path):
     try:
         model_configs[key] = load_json(str(path / "config.json"))
         try:
             # not all models have associated thresholds
             model_threshs[key] = load_json(
                 str(path / "thresholds.json"))
         except FileNotFoundError:
             pass
     finally:
         select_model(key)
         plugin.progress_bar.hide()
Beispiel #2
0
 def _model_folder_change(_path: str):
     path = Path(_path)
     key = CUSTOM_MODEL, path
     try:
         if not path.is_dir():
             return
         model_configs[key] = load_json(str(path / "config.json"))
         model_threshs[key] = load_json(str(path / "thresholds.json"))
     except FileNotFoundError:
         pass
     finally:
         select_model(key)
Beispiel #3
0
    def __init__(self, config, name=None, basedir='.'):
        super().__init__(config=config, name=name, basedir=basedir)
        threshs = dict(prob=None, nms=None)
        if basedir is not None:
            try:
                threshs = load_json(str(self.logdir / 'thresholds.json'))
                print("Loading thresholds from 'thresholds.json'.")
                if threshs.get('prob') is None or not (0 < threshs.get('prob')
                                                       < 1):
                    print(
                        "- Invalid 'prob' threshold (%s), using default value."
                        % str(threshs.get('prob')))
                    threshs['prob'] = None
                if threshs.get('nms') is None or not (0 < threshs.get('nms') <
                                                      1):
                    print(
                        "- Invalid 'nms' threshold (%s), using default value."
                        % str(threshs.get('nms')))
                    threshs['nms'] = None
            except FileNotFoundError:
                if config is None and len(tuple(self.logdir.glob('*.h5'))) > 0:
                    print(
                        "Couldn't load thresholds from 'thresholds.json', using default values. "
                        "(Call 'optimize_thresholds' to change that.)")

        self.thresholds = dict(
            prob=0.5 if threshs['prob'] is None else threshs['prob'],
            nms=0.4 if threshs['nms'] is None else threshs['nms'],
        )
        print(
            "Using default values: prob_thresh={prob:g}, nms_thresh={nms:g}.".
            format(prob=self.thresholds.prob, nms=self.thresholds.nms))
Beispiel #4
0
    def _set_logdir(self):
        if self.name is None:
            self.name = datetime.datetime.now().strftime(
                "%Y-%m-%d-%H-%M-%S.%f")
        self.logdir = self.basedir / self.name

        config_file = self.logdir / 'config.json'
        if self.config is None:
            if config_file.exists():
                config_dict = load_json(str(config_file))
                self.config = Config(**config_dict)
                if not self.config.is_valid():
                    invalid_attr = self.config.is_valid(True)[1]
                    raise ValueError('Invalid attributes in loaded config: ' +
                                     ', '.join(invalid_attr))
            else:
                raise FileNotFoundError("config file doesn't exist: %s" %
                                        str(config_file.resolve()))
        else:
            if self.logdir.exists():
                warnings.warn(
                    'output path for model already exists, files may be overwritten: %s'
                    % str(self.logdir.resolve()))
            self.logdir.mkdir(parents=True, exist_ok=True)
            save_json(vars(self.config), str(config_file))
Beispiel #5
0
def set_bioseg_model(basedir,
                     model_name,
                     BioConfig,
                     load_tf_file='',
                     load_model=False,
                     seed=0):
    mkdir(basedir)

    print('------------------  BioSeg setup  -------------------------')
    print('basedir/model name : ', basedir + model_name)
    print('model file : ',
          basedir + model_name + '/' + BioConfig.train_checkpoint)
    print()

    if seed > 0:
        tf.set_random_seed(seed)

    if load_model:
        print('** Loading Model Config **')
        print()
        if os.path.exists(basedir + model_name + '/config.json'):

            print('Loading previous config file : ')
            config_dict = load_json(basedir + model_name + '/config.json')
            BioConfig = BioSegConfig(np.array([]), **config_dict)
            print(basedir + model_name + '/config.json')
            if load_tf_file == '':  #load default best model in folder
                load_tf_file = basedir + model_name + '/' + BioConfig.train_checkpoint

    print('** Create Model **')
    print()
    print(BioConfig)

    model = BioSeg_standard(BioConfig, model_name, basedir=basedir)
    model.prepare_for_training()

    if os.path.exists(load_tf_file):
        print('Loading model weights : ', load_tf_file)
        model.keras_model.load_weights(load_tf_file)
    else:
        print('Model with random initialization')

    return model
Beispiel #6
0
 def load(path):
     return Config(**load_json(path))