MCPcopy Create free account
hub / github.com/PeizeSun/SparseR-CNN / DefaultPredictor

Class DefaultPredictor

detectron2/engine/defaults.py:160–224  ·  view source on GitHub ↗

Create a simple end-to-end predictor with the given config that runs on single device for a single input image. Compared to using the model directly, this class does the following additions: 1. Load checkpoint from `cfg.MODEL.WEIGHTS`. 2. Always take BGR image as the input and

Source from the content-addressed store, hash-verified

158
159
160class DefaultPredictor:
161 """
162 Create a simple end-to-end predictor with the given config that runs on
163 single device for a single input image.
164
165 Compared to using the model directly, this class does the following additions:
166
167 1. Load checkpoint from `cfg.MODEL.WEIGHTS`.
168 2. Always take BGR image as the input and apply conversion defined by `cfg.INPUT.FORMAT`.
169 3. Apply resizing defined by `cfg.INPUT.{MIN,MAX}_SIZE_TEST`.
170 4. Take one input image and produce a single output, instead of a batch.
171
172 If you'd like to do anything more fancy, please refer to its source code
173 as examples to build and use the model manually.
174
175 Attributes:
176 metadata (Metadata): the metadata of the underlying dataset, obtained from
177 cfg.DATASETS.TEST.
178
179 Examples:
180 ::
181 pred = DefaultPredictor(cfg)
182 inputs = cv2.imread("input.jpg")
183 outputs = pred(inputs)
184 """
185
186 def __init__(self, cfg):
187 self.cfg = cfg.clone() # cfg can be modified by model
188 self.model = build_model(self.cfg)
189 self.model.eval()
190 if len(cfg.DATASETS.TEST):
191 self.metadata = MetadataCatalog.get(cfg.DATASETS.TEST[0])
192
193 checkpointer = DetectionCheckpointer(self.model)
194 checkpointer.load(cfg.MODEL.WEIGHTS)
195
196 self.aug = T.ResizeShortestEdge(
197 [cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST], cfg.INPUT.MAX_SIZE_TEST
198 )
199
200 self.input_format = cfg.INPUT.FORMAT
201 assert self.input_format in ["RGB", "BGR"], self.input_format
202
203 def __call__(self, original_image):
204 """
205 Args:
206 original_image (np.ndarray): an image of shape (H, W, C) (in BGR order).
207
208 Returns:
209 predictions (dict):
210 the output of the model for one image only.
211 See :doc:`/tutorials/models` for details about the format.
212 """
213 with torch.no_grad(): # https://github.com/sphinx-doc/sphinx/issues/4258
214 # Apply pre-processing to image.
215 if self.input_format == "RGB":
216 # whether the model expects BGR inputs or RGB
217 original_image = original_image[:, :, ::-1]

Callers 4

__init__Method · 0.90
runMethod · 0.90
__init__Method · 0.90
runMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected