-
Notifications
You must be signed in to change notification settings - Fork 21
/
base.py
105 lines (75 loc) · 2.71 KB
/
base.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'''
Base classes.
'''
__all__ = [
'DnnFeatureExtractorBase',
'ReconstructionBase',
]
from typing import Any, Type, Iterable, List, Dict, Tuple, Callable, Union, Optional
import os
import numpy as np
import torch
import torch.nn as nn
_tensor_t = Union[np.ndarray, torch.Tensor]
class DnnFeatureExtractorBase(object):
'''
Base class for PyTorch DNN feature extractors.
'''
def __init__(self, model: Optional[nn.Module] = None, model_cls: Optional[Type[nn.Module]] = None, layers: Iterable[str] = [], device: str = 'cpu', init_args={}):
self.model = model
self.model_cls = model_cls
self.layers = layers
self.device = torch.device(device)
self.init(**init_args)
if self.model is None:
raise RuntimeError('`self.model` is None. You should define it it `init()`.')
self.model.to(self.device)
def init(self) -> None:
'''
Custom initialization method.
`init_args` in `__init__()` is passed to this function.
'''
return None
def preprocess(self, x: Any) -> Any:
'''
Preprocesses the input for the DNN model.
'''
return x
def extract_features(self, x: Any) -> Dict[str, np.ndarray]:
'''
Extracts features from the given input using the DNN model.
'''
raise NotImplementedError("Subclass must implement extract_features method.")
def __call__(self, x: Any, **kwargs) -> Dict[str, _tensor_t]:
return self.extract_features(self.preprocess(x), **kwargs)
class ReconstructionBase(object):
'''
Base class for reconstruction.
'''
def __init__(self, model: Optional[nn.Module] = None, model_cls: Optional[Type[nn.Module]] = None, layers: Iterable[str] = [], device: str = 'cpu', init_args={}):
self.model = model
self.model_cls = model_cls
self.layers = layers
self.device = torch.device(device)
self.init(**init_args)
if self.model is None:
raise RuntimeError('`self.model` is None. You should define it it `init()`.')
self.model.to(self.device)
def init(self) -> None:
'''
Custom initialization method.
`init_args` in `__init__()` is passed to this function.
'''
return None
def preprocess(self, x: Any) -> Any:
'''
Preprocesses the input for the DNN model.
'''
return x
def reconstruct(self, x: Any) -> Any:
'''
Reconstruction from the given input.
'''
raise NotImplementedError("Subclass must implement reconstruct method.")
def __call__(self, x: Any, **kwargs) -> Any:
return self.reconstruct(self.preprocess(x), **kwargs)