PawanratRung commited on
Commit
d979381
·
verified ·
1 Parent(s): bfc0b35

Create base.py

Browse files
3rdparty/densepose/converters/base.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Facebook, Inc. and its affiliates.
2
+
3
+ from typing import Any, Tuple, Type
4
+ import torch
5
+
6
+
7
+ class BaseConverter:
8
+ """
9
+ Converter base class to be reused by various converters.
10
+ Converter allows one to convert data from various source types to a particular
11
+ destination type. Each source type needs to register its converter. The
12
+ registration for each source type is valid for all descendants of that type.
13
+ """
14
+
15
+ @classmethod
16
+ def register(cls, from_type: Type, converter: Any = None):
17
+ """
18
+ Registers a converter for the specified type.
19
+ Can be used as a decorator (if converter is None), or called as a method.
20
+ Args:
21
+ from_type (type): type to register the converter for;
22
+ all instances of this type will use the same converter
23
+ converter (callable): converter to be registered for the given
24
+ type; if None, this method is assumed to be a decorator for the converter
25
+ """
26
+
27
+ if converter is not None:
28
+ cls._do_register(from_type, converter)
29
+
30
+ def wrapper(converter: Any) -> Any:
31
+ cls._do_register(from_type, converter)
32
+ return converter
33
+
34
+ return wrapper
35
+
36
+ @classmethod
37
+ def _do_register(cls, from_type: Type, converter: Any):
38
+ cls.registry[from_type] = converter # pyre-ignore[16]
39
+
40
+ @classmethod
41
+ def _lookup_converter(cls, from_type: Type) -> Any:
42
+ """
43
+ Perform recursive lookup for the given type
44
+ to find registered converter. If a converter was found for some base
45
+ class, it gets registered for this class to save on further lookups.
46
+ Args:
47
+ from_type: type for which to find a converter
48
+ Return:
49
+ callable or None - registered converter or None
50
+ if no suitable entry was found in the registry
51
+ """
52
+ if from_type in cls.registry: # pyre-ignore[16]
53
+ return cls.registry[from_type]
54
+ for base in from_type.__bases__:
55
+ converter = cls._lookup_converter(base)
56
+ if converter is not None:
57
+ cls._do_register(from_type, converter)
58
+ return converter
59
+ return None
60
+
61
+ @classmethod
62
+ def convert(cls, instance: Any, *args, **kwargs):
63
+ """
64
+ Convert an instance to the destination type using some registered
65
+ converter. Does recursive lookup for base classes, so there's no need
66
+ for explicit registration for derived classes.
67
+ Args:
68
+ instance: source instance to convert to the destination type
69
+ Return:
70
+ An instance of the destination type obtained from the source instance
71
+ Raises KeyError, if no suitable converter found
72
+ """
73
+ instance_type = type(instance)
74
+ converter = cls._lookup_converter(instance_type)
75
+ if converter is None:
76
+ if cls.dst_type is None: # pyre-ignore[16]
77
+ output_type_str = "itself"
78
+ else:
79
+ output_type_str = cls.dst_type
80
+ raise KeyError(f"Could not find converter from {instance_type} to {output_type_str}")
81
+ return converter(instance, *args, **kwargs)
82
+
83
+
84
+ IntTupleBox = Tuple[int, int, int, int]
85
+
86
+
87
+ def make_int_box(box: torch.Tensor) -> IntTupleBox:
88
+ int_box = [0, 0, 0, 0]
89
+ int_box[0], int_box[1], int_box[2], int_box[3] = tuple(box.long().tolist())
90
+ return int_box[0], int_box[1], int_box[2], int_box[3]