-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
62 lines (43 loc) · 1.51 KB
/
model.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
from carla.models.api import MLModel
from carla.data.api import Data
import pandas as pd
import numpy as np
import torch
from typing import Union
class BlackBox(MLModel):
def __init__(self, raw_model, data: Data) -> None:
super().__init__(data)
self._feature_input_order = list(data.df.columns)
self._raw_model = raw_model
@property
def feature_input_order(self):
return self._feature_input_order
@property
def backend(self):
return "sklearn"
@property
def raw_model(self):
return self._raw_model
def predict(self, x: Union[np.ndarray, pd.DataFrame]):
return self._raw_model.predict(x)
def predict_proba(self, x: Union[np.ndarray, pd.DataFrame]):
return self._raw_model.predict_proba(x)
class BlackBoxTorch(MLModel):
def __init__(self, raw_model, data: Data) -> None:
super().__init__(data)
self._feature_input_order = list(data.df.columns)
self._raw_model = raw_model
@property
def feature_input_order(self):
return self._feature_input_order
@property
def backend(self):
return "pytorch"
@property
def raw_model(self):
return self._raw_model
def predict(self, x: Union[np.ndarray, pd.DataFrame]):
return torch.argmax(self._raw_model(x), dim=1)
def predict_proba(self, x: Union[np.ndarray, pd.DataFrame]):
with torch.no_grad():
return self._raw_model(torch.FloatTensor(x.values))