Scheaven
2021-09-18 291deeb1fcf45dbf39a24aa72a213ff3fd6b3405
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
U
P´æ`Tã@sŒddlZddlZddlZddlmmZddlmZddl    m
Z
ddl Z ddlZddl m Z ddlmZGdd„deƒZGdd    „d    ƒZdS)
éN)Ú build_model)Ú Checkpointer)Úsimplify)ÚOperatorExportTypesc@s$eZdZdd„Zdd„Zdd„ZdS)Ú
ReID_ModelcCs||_t|ƒ|_dS©N)ÚcfgÚDefaultPredictorÚ    predictor)Úselfr©r úT/data/disk1/workspace/06_reid/01_fast_reid/02_fast_reid_inference/tools/predictor.pyÚ__init__szReID_Model.__init__cCs| |¡}|S)zù
 
        Args:
            original_image (np.ndarray): an image of shape (H, W, C) (in BGR order).
                This is the format used by OpenCV.
 
        Returns:
            predictions (np.ndarray): normalized feature of the model.
        )r
)r Zoriginal_imageÚ predictionsr r r Ú run_on_images
zReID_Model.run_on_imagecCs|j ¡}dSr)r
Úto_onnx)r rr r r Ú
torch2onnx2szReID_Model.torch2onnxN)Ú__name__Ú
__module__Ú __qualname__rrrr r r r rsrc@s8eZdZdZdd„Zdd„Zdd„Zdd    „Zd
d „Zd S) r    aB
    Create a simple end-to-end predictor with the given config.
    The predictor takes an BGR image, resizes it to the specified resolution,
    runs the model and produces a dict of predictions.
    This predictor takes care of model loading and input preprocessing for you.
    If you'd like to do anything more fancy, please refer to its source code
    as examples to build and use the model manually.
    Attributes:
    Examples:
    .. code-block:: python
        pred = DefaultPredictor(cfg)
        inputs = cv2.imread("input.jpg")
        outputs = pred(inputs)
    cCsn| ¡|_|j ¡d|jjj_t|jƒ|_|j ¡D]
}d|_    q6|j 
¡|j  ¡t |jƒ  |jj¡dS)NF)ÚclonerÚdefrostÚMODELÚBACKBONEÚPRETRAINrÚmodelÚ
parametersÚ requires_gradÚcudaÚevalrÚloadZWEIGHTS)r rÚparamr r r rEs
 
 
 
zDefaultPredictor.__init__c
Csjt ¡X| ¡}|j ¡| |¡}tjddtd|ƒt |¡}|     ¡j
}|W5QR£SQRXdS)zº
        Args:
            image (torch.tensor): an image tensor of shape (B, C, H, W).
        Returns:
            predictions (torch.tensor): the output features of the model
        i)Ú    edgeitemsz ------------
N) ÚtorchÚno_gradrrrÚset_printoptionsÚprintÚFÚ    normalizeÚcpuÚdata)r ÚimageZimagesrZ    pred_featr r r Ú__call__Qs
 
 
 
 
 
zDefaultPredictor.__call__cCsht dd|jjjd|jjjd¡ ¡}| |j|¡}t|ƒ\}}|     |¡}|sXt
dƒ‚t   |d¡dS)Néérz,Simplified ONNX model could not be validatedz fastreid.onnx) r#ÚrandnrZINPUTZ    SIZE_TESTrÚexport_onnx_modelrrÚremove_initializer_from_inputÚAssertionErrorÚonnxZ
save_model)r ÚinputsÚ
onnx_modelZ
model_simpÚcheckr r r rhs ( 
 zDefaultPredictor.to_onnxc
s°t|tjjƒst‚dd„}| |¡t ¡>t ¡*}tj    j
|||t j dt      | ¡¡}W5QRXW5QRXt    j ¡‰dddg}t‡fdd„|Dƒƒsžt‚t    j ||¡}|S)    zâ
        Trace and export a model to onnx format.
        Args:
            model (nn.Module):
            inputs (torch.Tensor): the model will be called by `model(*inputs)`
        Returns:
            an onnx model
        cSs|jr
t‚dSr)Útrainingr2)Úmoduler r r Ú _check_eval†sz7DefaultPredictor.export_onnx_model.<locals>._check_eval)Úoperator_export_typeZextract_constant_to_initializerZeliminate_unused_initializerZfuse_bn_into_convc3s|]}|ˆkVqdSrr )Ú.0Úp©Z
all_passesr r Ú    <genexpr>›sz5DefaultPredictor.export_onnx_model.<locals>.<genexpr>)Ú
isinstancer#ÚnnÚModuler2Úapplyr$ÚioÚBytesIOr3ÚexportrÚONNX_ATEN_FALLBACKZload_from_stringÚgetvalueÚ    optimizerZget_available_passesÚallÚoptimize)r rr4r9Úfr5Zpassesr r=r r0ys"    
 
 
ü"
 
z"DefaultPredictor.export_onnx_modelcCsb|jdkrtdƒdS|jj}i}|D]}|||j<q&|jjD]}|j|kr>| ||j¡q>|S)NézKModel with ir_version below 4 requires to include initilizer in graph input)Ú
ir_versionr&ÚgraphÚinputÚnameÚ initializerÚremove)r rr4Z name_to_inputrOrQr r r r1 s
ÿ  
z.DefaultPredictor.remove_initializer_from_inputN)    rrrÚ__doc__rr,rr0r1r r r r r    5s  'r    )Zcv2rCr#Ztorch.nn.functionalr@Ú
functionalr'Zmodeling.meta_arch.buildrZutils.checkpointrr3ZonnxsimrÚ
torch.onnxrÚobjectrr    r r r r Ú<module>s    "