meta_arch.py 1.5 KB
Newer Older
F
FDInSky 已提交
1 2 3 4 5
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
W
wangxinxin08 已提交
6 7
import paddle
import paddle.nn as nn
F
FDInSky 已提交
8 9 10 11 12 13 14
from ppdet.core.workspace import register
from ppdet.utils.data_structure import BufferDict

__all__ = ['BaseArch']


@register
W
wangxinxin08 已提交
15
class BaseArch(nn.Layer):
16
    def __init__(self):
F
FDInSky 已提交
17 18
        super(BaseArch, self).__init__()

19 20 21 22 23
    def forward(self, data, input_def, mode, input_tensor=None):
        if input_tensor is None:
            self.inputs = self.build_inputs(data, input_def)
        else:
            self.inputs = input_tensor
24
        self.inputs['mode'] = mode
25 26
        self.model_arch()

27
        if mode == 'train':
K
Kaipeng Deng 已提交
28
            out = self.get_loss()
29
        elif mode == 'infer':
30
            out = self.get_pred(input_tensor is None)
31
        else:
32 33
            out = None
            raise "Now, only support train and infer mode!"
34
        return out
F
FDInSky 已提交
35

36 37
    def build_inputs(self, data, input_def):
        inputs = {}
G
Guanghua Yu 已提交
38
        for i, k in enumerate(input_def):
W
wangguanzhong 已提交
39
            inputs[k] = data[i]
40 41
        return inputs

W
wangxinxin08 已提交
42
    def model_arch(self):
43 44
        raise NotImplementedError("Should implement model_arch method!")

K
Kaipeng Deng 已提交
45 46
    def get_loss(self, ):
        raise NotImplementedError("Should implement get_loss method!")
47

K
Kaipeng Deng 已提交
48 49
    def get_pred(self, ):
        raise NotImplementedError("Should implement get_pred method!")
50 51 52

    def get_export_model(self, input_tensor):
        return self.forward(None, None, 'infer', input_tensor)