Ejemplo n.º 1
0
 def load_best_accuracy(self) -> float:
     best_accuracy = 0.
     try:
         model_state = load_model_state(self.dataset_name,
                                        self.model.__class__.__name__)
         loaded_model = copy.deepcopy(self.model)
         loaded_model.load_state_dict(model_state)
         loaded_model.eval()
         for param in loaded_model.parameters():
             param.requires_grad = False
             param.volatile = True
         best_accuracy = calc_accuracy(loaded_model, self.train_loader)
         del loaded_model
     except Exception as e:
         print(
             f"Couldn't estimate the best accuracy for {self.model.__class__.__name__}. Reset to 0."
         )
     return best_accuracy
Ejemplo n.º 2
0
def build_model(opts, vocab, image_size, checkpoint_start_from=None):
    if checkpoint_start_from is not None:
        log.info("Load checkpoint as initialization: {}".format(checkpoint_start_from))
        checkpoint = torch.load(checkpoint_start_from)
        # kwarg aka keyword arguments
        kwargs = checkpoint['model_kwargs']
        model = getattr(models, opts["arch"])(**kwargs)
        raw_state_dict = checkpoint['model_state']
        state_dict = {}
        for k, v in raw_state_dict.items():
            if k.startswith('module.'):
                k = k[7:]
            state_dict[k] = v
        model = load_model_state(model, state_dict, strict=False)
    else:
        kwargs = deepcopy(opts["options"])
        kwargs["vocab"] = vocab
        kwargs["image_size"] = image_size
        model = getattr(models, opts["arch"])(**kwargs)
    return model, kwargs
Ejemplo n.º 3
0
    if args.interactive and args.sample_dir:
        print(
            'Incompatible options. Please specify either --interactive or --sample_dir, not both at the same time'
        )
        exit(-1)

    if not args.interactive and not args.sample_dir:
        print('No action. Please specify either --interactive or --sample_dir')
        exit(-1)

    database = sqlite3.connect(args.database)
    searcher = Searcher.load(args.index)

    small_encoder = SmallEncoder().cuda()
    load_model_state(args.small_encoder, small_encoder)
    large_encoder = LargeEncoder().cuda()
    load_model_state(args.large_encoder, large_encoder)
    large_encoder.eval()
    small_encoder.eval()

    if args.interactive:
        interactive(small_encoder, large_encoder, searcher, database,
                    args.augment)
        exit(0)

    if args.sample_dir:
        process_dir(small_encoder, large_encoder, searcher, database,
                    args.augment, args.sample_dir)
        exit(0)