File size: 2,203 Bytes
b84549f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import torch
from abc import ABC, abstractmethod
from utils.common.file import ensure_dir
from utils.common.log import logger
import time
from typing import List


class BaseModel(ABC):
    def __init__(self,
                 name: str,
                 models_dict_path: str,
                 device: str):
        
        self.name = name
        self.models_dict_path = models_dict_path
        self.models_dict = torch.load(models_dict_path, map_location=device)
        
        self.device = device
        
        assert set(self.get_required_model_components()) <= set(list(self.models_dict.keys()))

        self.to(device)
        
        logger.info(f'[model] init model: {dict(name=name, components=self.get_required_model_components())}')
        logger.debug(self.models_dict)
    
    @abstractmethod
    def get_required_model_components(self) -> List[str]:
        pass
    
    @abstractmethod
    def get_accuracy(self, test_loader, *args, **kwargs):
        pass

    @abstractmethod
    def infer(self, x, *args, **kwargs):
        pass
    
    def save_model(self, p: str):
        logger.debug(f'[model] save model: {self.name}')
        ensure_dir(p)
        torch.save(self.models_dict, p)
    
    def load_model(self, p: str):
        logger.debug(f'[model] load model: {self.name}, from {p}')
        self.models_dict = torch.load(p, map_location=self.device)
    
    def to(self, device):
        logger.debug(f'[model] to device: {device}')
        for k, v in self.models_dict.items():
            try:
                self.models_dict[k] = v.to(device)
            except Exception as e:
                pass

    def to_eval_mode(self, verbose=False):
        if verbose:
            logger.info(f'[model] to eval mode')
        for k, v in self.models_dict.items():
            try:
                self.models_dict[k].eval()
            except Exception as e:
                pass

    def to_train_mode(self, verbose=False):
        if verbose:
            logger.info(f'[model] to train mode')
        for k, v in self.models_dict.items():
            try:
                self.models_dict[k].train()
            except Exception as e:
                pass