Commit 82295dbf authored by Yanghan Wang's avatar Yanghan Wang Committed by Facebook GitHub Bot
Browse files

enable black for mobile-vision

Summary:
https://fb.workplace.com/groups/pythonfoundation/posts/2990917737888352

Remove `mobile-vision` from opt-out list; leaving `mobile-vision/SNPE` opted out because of 3rd-party code.

arc lint --take BLACK --apply-patches --paths-cmd 'hg files mobile-vision'

allow-large-files

Reviewed By: sstsai-adl

Differential Revision: D30721093

fbshipit-source-id: 9e5c16d988b315b93a28038443ecfb92efd18ef8
parent a56c7e15
import os
import tempfile import tempfile
from d2go.utils.testing.rcnn_helper import RCNNBaseTestCases
from d2go.utils.flop_calculator import dump_flops_info from d2go.utils.flop_calculator import dump_flops_info
from d2go.utils.testing.data_loader_helper import create_fake_detection_data_loader from d2go.utils.testing.data_loader_helper import create_fake_detection_data_loader
import os from d2go.utils.testing.rcnn_helper import RCNNBaseTestCases
class TestFlopCount(RCNNBaseTestCases.TemplateTestCase): class TestFlopCount(RCNNBaseTestCases.TemplateTestCase):
...@@ -19,6 +20,10 @@ class TestFlopCount(RCNNBaseTestCases.TemplateTestCase): ...@@ -19,6 +20,10 @@ class TestFlopCount(RCNNBaseTestCases.TemplateTestCase):
with tempfile.TemporaryDirectory(prefix="d2go_test") as output_dir: with tempfile.TemporaryDirectory(prefix="d2go_test") as output_dir:
dump_flops_info(self.test_model, inputs, output_dir) dump_flops_info(self.test_model, inputs, output_dir)
for fname in ["flops_str_mobilecv", "flops_str_fvcore", "flops_table_fvcore"]: for fname in [
"flops_str_mobilecv",
"flops_str_fvcore",
"flops_table_fvcore",
]:
outf = os.path.join(output_dir, fname + ".txt") outf = os.path.join(output_dir, fname + ".txt")
self.assertTrue(os.path.isfile(outf)) self.assertTrue(os.path.isfile(outf))
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import glob
import tempfile import tempfile
import unittest import unittest
import glob
import d2go.utils.abnormal_checker as ac import d2go.utils.abnormal_checker as ac
import torch import torch
...@@ -35,9 +35,7 @@ class TestUtilsAbnormalChecker(unittest.TestCase): ...@@ -35,9 +35,7 @@ class TestUtilsAbnormalChecker(unittest.TestCase):
model = Model() model = Model()
with tempfile.TemporaryDirectory() as tmp_dir: with tempfile.TemporaryDirectory() as tmp_dir:
checker = ac.AbnormalLossChecker( checker = ac.AbnormalLossChecker(-1, writers=[ac.FileWriter(tmp_dir)])
-1, writers=[ac.FileWriter(tmp_dir)]
)
cmodel = ac.AbnormalLossCheckerWrapper(model, checker) cmodel = ac.AbnormalLossCheckerWrapper(model, checker)
losses = [5, 4, 3, 10, 9, 2, 5, 4] losses = [5, 4, 3, 10, 9, 2, 5, 4]
......
...@@ -2,17 +2,17 @@ ...@@ -2,17 +2,17 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import numpy as np
import torch
import unittest import unittest
import numpy as np
import torch
from detectron2.modeling.box_regression import Box2BoxTransform from detectron2.modeling.box_regression import Box2BoxTransform
class TestBox2BoxTransform(unittest.TestCase): class TestBox2BoxTransform(unittest.TestCase):
def test_box2box_transform(self): def test_box2box_transform(self):
""" Match unit test UtilsBoxesTest.TestBboxTransformRandom in """Match unit test UtilsBoxesTest.TestBboxTransformRandom in
caffe2/operators/generate_proposals_op_util_boxes_test.cc caffe2/operators/generate_proposals_op_util_boxes_test.cc
""" """
box2box_transform = Box2BoxTransform(weights=(1.0, 1.0, 1.0, 1.0)) box2box_transform = Box2BoxTransform(weights=(1.0, 1.0, 1.0, 1.0))
bbox = torch.from_numpy( bbox = torch.from_numpy(
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
import unittest import unittest
from detectron2.modeling.roi_heads.fast_rcnn import fast_rcnn_inference import torch
from detectron2.layers import cat from detectron2.layers import cat
from detectron2.modeling.roi_heads.fast_rcnn import fast_rcnn_inference
from detectron2.structures import Boxes from detectron2.structures import Boxes
...@@ -37,10 +37,12 @@ class TestBoxWithNMSLimit(unittest.TestCase): ...@@ -37,10 +37,12 @@ class TestBoxWithNMSLimit(unittest.TestCase):
scores = [torch.rand(n, num_class + 1, generator=rng) for n in batch_splits] scores = [torch.rand(n, num_class + 1, generator=rng) for n in batch_splits]
ref_results, ref_kept_indices = fast_rcnn_inference( ref_results, ref_kept_indices = fast_rcnn_inference(
boxes, scores, image_shapes, boxes,
scores,
image_shapes,
score_thresh=score_thresh, score_thresh=score_thresh,
nms_thresh=nms_thresh, nms_thresh=nms_thresh,
topk_per_image=detections_per_im topk_per_image=detections_per_im,
) )
for result, kept_index, score in zip(ref_results, ref_kept_indices, scores): for result, kept_index, score in zip(ref_results, ref_kept_indices, scores):
torch.testing.assert_allclose( torch.testing.assert_allclose(
...@@ -78,7 +80,14 @@ class TestBoxWithNMSLimit(unittest.TestCase): ...@@ -78,7 +80,14 @@ class TestBoxWithNMSLimit(unittest.TestCase):
output_classes_include_bg_cls=False, output_classes_include_bg_cls=False,
legacy_plus_one=False, legacy_plus_one=False,
) )
roi_score_nms, roi_bbox_nms, roi_class_nms, roi_batch_splits_nms, roi_keeps_nms, roi_keeps_size_nms = nms_outputs # noqa (
roi_score_nms,
roi_bbox_nms,
roi_class_nms,
roi_batch_splits_nms,
roi_keeps_nms,
roi_keeps_size_nms,
) = nms_outputs # noqa
roi_score_nms = roi_score_nms.split(roi_batch_splits_nms.int().tolist()) roi_score_nms = roi_score_nms.split(roi_batch_splits_nms.int().tolist())
roi_bbox_nms = roi_bbox_nms.split(roi_batch_splits_nms.int().tolist()) roi_bbox_nms = roi_bbox_nms.split(roi_batch_splits_nms.int().tolist())
......
...@@ -3,7 +3,8 @@ import unittest ...@@ -3,7 +3,8 @@ import unittest
from d2go.model_zoo import model_zoo from d2go.model_zoo import model_zoo
OSSRUN = os.getenv('OSSRUN') == '1' OSSRUN = os.getenv("OSSRUN") == "1"
class TestD2GoModelZoo(unittest.TestCase): class TestD2GoModelZoo(unittest.TestCase):
@unittest.skipIf(not OSSRUN, "OSS test only") @unittest.skipIf(not OSSRUN, "OSS test only")
...@@ -12,5 +13,6 @@ class TestD2GoModelZoo(unittest.TestCase): ...@@ -12,5 +13,6 @@ class TestD2GoModelZoo(unittest.TestCase):
for cfgfile in configs: for cfgfile in configs:
model = model_zoo.get(cfgfile, trained=True) model = model_zoo.get(cfgfile, trained=True)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
...@@ -67,7 +67,7 @@ class TestModelingImagePooler(unittest.TestCase): ...@@ -67,7 +67,7 @@ class TestModelingImagePooler(unittest.TestCase):
self.assertArrayEqual(pooled_box, [[2, 3, 5, 7]]) self.assertArrayEqual(pooled_box, [[2, 3, 5, 7]])
def test_image_pooler_scale_box_large_crop_only(self): def test_image_pooler_scale_box_large_crop_only(self):
""" Crop bbox """ """Crop bbox"""
H, W = 398, 224 H, W = 398, 224
all_boxes = Boxes(torch.Tensor([[50, 40, 100, 80], [150, 60, 200, 120]])) all_boxes = Boxes(torch.Tensor([[50, 40, 100, 80], [150, 60, 200, 120]]))
image = rh.get_batched_inputs(1, (H, W), (H, W), all_boxes)[0]["image"] image = rh.get_batched_inputs(1, (H, W), (H, W), all_boxes)[0]["image"]
...@@ -86,7 +86,7 @@ class TestModelingImagePooler(unittest.TestCase): ...@@ -86,7 +86,7 @@ class TestModelingImagePooler(unittest.TestCase):
self.assertArrayEqual(sub_boxes.tensor, [[0, 0, 50, 40], [100, 20, 150, 80]]) self.assertArrayEqual(sub_boxes.tensor, [[0, 0, 50, 40], [100, 20, 150, 80]])
def test_image_pooler_scale_box_large_crop_and_scale(self): def test_image_pooler_scale_box_large_crop_and_scale(self):
""" Crop bbox that is scaled """ """Crop bbox that is scaled"""
H, W = 398, 224 H, W = 398, 224
all_boxes = Boxes(torch.Tensor([[50, 40, 100, 80], [150, 60, 200, 120]])) all_boxes = Boxes(torch.Tensor([[50, 40, 100, 80], [150, 60, 200, 120]]))
image = rh.get_batched_inputs(1, (H, W), (H, W), all_boxes)[0]["image"] image = rh.get_batched_inputs(1, (H, W), (H, W), all_boxes)[0]["image"]
...@@ -111,7 +111,7 @@ class TestModelingImagePooler(unittest.TestCase): ...@@ -111,7 +111,7 @@ class TestModelingImagePooler(unittest.TestCase):
self.assertArrayEqual(sub_boxes.tensor, [[15, 8, 65, 48], [115, 28, 165, 88]]) self.assertArrayEqual(sub_boxes.tensor, [[15, 8, 65, 48], [115, 28, 165, 88]])
def test_image_pooler_scale_box_large_crop_scale_and_resize(self): def test_image_pooler_scale_box_large_crop_scale_and_resize(self):
""" Crop bbox that is scaled, resize the cropped box """ """Crop bbox that is scaled, resize the cropped box"""
H, W = 398, 224 H, W = 398, 224
all_boxes = Boxes(torch.Tensor([[50, 40, 100, 80], [150, 60, 200, 120]])) all_boxes = Boxes(torch.Tensor([[50, 40, 100, 80], [150, 60, 200, 120]]))
image = rh.get_batched_inputs(1, (H, W), (H, W), all_boxes)[0]["image"] image = rh.get_batched_inputs(1, (H, W), (H, W), all_boxes)[0]["image"]
......
...@@ -11,8 +11,8 @@ from detectron2.layers import nms as box_nms ...@@ -11,8 +11,8 @@ from detectron2.layers import nms as box_nms
class TestNMS(unittest.TestCase): class TestNMS(unittest.TestCase):
def test_nms_cpu(self): def test_nms_cpu(self):
""" Match unit test UtilsNMSTest.TestNMS in """Match unit test UtilsNMSTest.TestNMS in
caffe2/operators/generate_proposals_op_util_nms_test.cc caffe2/operators/generate_proposals_op_util_nms_test.cc
""" """
inputs = ( inputs = (
...@@ -60,8 +60,8 @@ class TestNMS(unittest.TestCase): ...@@ -60,8 +60,8 @@ class TestNMS(unittest.TestCase):
np.testing.assert_array_equal(keep_indices, np.array(gt_index)) np.testing.assert_array_equal(keep_indices, np.array(gt_index))
def test_nms1_cpu(self): def test_nms1_cpu(self):
""" Match unit test UtilsNMSTest.TestNMS1 in """Match unit test UtilsNMSTest.TestNMS1 in
caffe2/operators/generate_proposals_op_util_nms_test.cc caffe2/operators/generate_proposals_op_util_nms_test.cc
""" """
boxes = torch.from_numpy( boxes = torch.from_numpy(
......
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
import unittest import unittest
from d2go.optimizer import build_optimizer_mapper
import d2go.runner.default_runner as default_runner import d2go.runner.default_runner as default_runner
import torch
from d2go.optimizer import build_optimizer_mapper
class TestArch(torch.nn.Module): class TestArch(torch.nn.Module):
def __init__(self): def __init__(self):
...@@ -22,6 +24,7 @@ class TestArch(torch.nn.Module): ...@@ -22,6 +24,7 @@ class TestArch(torch.nn.Module):
ret = self.avgpool(ret) ret = self.avgpool(ret)
return ret return ret
def _test_each_optimizer(cfg): def _test_each_optimizer(cfg):
model = TestArch() model = TestArch()
optimizer = build_optimizer_mapper(cfg, model) optimizer = build_optimizer_mapper(cfg, model)
...@@ -33,12 +36,12 @@ def _test_each_optimizer(cfg): ...@@ -33,12 +36,12 @@ def _test_each_optimizer(cfg):
loss.backward() loss.backward()
optimizer.step() optimizer.step()
class TestOptimizer(unittest.TestCase):
class TestOptimizer(unittest.TestCase):
def test_all_optimizers(self): def test_all_optimizers(self):
runner = default_runner.Detectron2GoRunner() runner = default_runner.Detectron2GoRunner()
cfg = runner.get_default_cfg() cfg = runner.get_default_cfg()
multipliers = [None, [{'conv': 0.1}]] multipliers = [None, [{"conv": 0.1}]]
for optimizer_name in ["SGD", "AdamW", "SGD_MT", "AdamW_MT"]: for optimizer_name in ["SGD", "AdamW", "SGD_MT", "AdamW_MT"]:
for mult in multipliers: for mult in multipliers:
......
...@@ -6,9 +6,9 @@ import logging ...@@ -6,9 +6,9 @@ import logging
import unittest import unittest
import torch import torch
from d2go.runner import GeneralizedRCNNRunner
from detectron2.modeling import build_anchor_generator, build_backbone from detectron2.modeling import build_anchor_generator, build_backbone
from detectron2.modeling.proposal_generator import rpn from detectron2.modeling.proposal_generator import rpn
from d2go.runner import GeneralizedRCNNRunner
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -19,7 +19,7 @@ RPN_CFGS = {} ...@@ -19,7 +19,7 @@ RPN_CFGS = {}
class TestRPNHeads(unittest.TestCase): class TestRPNHeads(unittest.TestCase):
def test_build_rpn_heads(self): def test_build_rpn_heads(self):
""" Make sure rpn heads run """ """Make sure rpn heads run"""
self.assertGreater(len(rpn.RPN_HEAD_REGISTRY._obj_map), 0) self.assertGreater(len(rpn.RPN_HEAD_REGISTRY._obj_map), 0)
...@@ -65,7 +65,7 @@ class TestRPNHeads(unittest.TestCase): ...@@ -65,7 +65,7 @@ class TestRPNHeads(unittest.TestCase):
) )
def test_build_rpn_heads_with_rotated_anchor_generator(self): def test_build_rpn_heads_with_rotated_anchor_generator(self):
""" Make sure rpn heads work with rotated anchor generator""" """Make sure rpn heads work with rotated anchor generator"""
self.assertGreater(len(rpn.RPN_HEAD_REGISTRY._obj_map), 0) self.assertGreater(len(rpn.RPN_HEAD_REGISTRY._obj_map), 0)
......
...@@ -6,9 +6,9 @@ Binary to evaluate predictor-based model (consist of models in deployable format ...@@ -6,9 +6,9 @@ Binary to evaluate predictor-based model (consist of models in deployable format
torchscript, caffe2, etc.) using Detectron2Go system (dataloading, evaluation, etc). torchscript, caffe2, etc.) using Detectron2Go system (dataloading, evaluation, etc).
""" """
import torch
import logging import logging
import torch
from d2go.distributed import launch from d2go.distributed import launch
from d2go.setup import ( from d2go.setup import (
basic_argument_parser, basic_argument_parser,
......
...@@ -41,7 +41,7 @@ class TrainOutput: ...@@ -41,7 +41,7 @@ class TrainOutput:
def maybe_override_output_dir(cfg: CfgNode, output_dir: Optional[str]) -> None: def maybe_override_output_dir(cfg: CfgNode, output_dir: Optional[str]) -> None:
"""Overrides the output directory if `output_dir` is not None. """ """Overrides the output directory if `output_dir` is not None."""
if output_dir is not None and output_dir != cfg.OUTPUT_DIR: if output_dir is not None and output_dir != cfg.OUTPUT_DIR:
cfg.OUTPUT_DIR = output_dir cfg.OUTPUT_DIR = output_dir
logger.warning( logger.warning(
...@@ -70,16 +70,21 @@ def _get_trainer_callbacks(cfg: CfgNode) -> List[Callback]: ...@@ -70,16 +70,21 @@ def _get_trainer_callbacks(cfg: CfgNode) -> List[Callback]:
callbacks.append(QuantizationAwareTraining.from_config(cfg)) callbacks.append(QuantizationAwareTraining.from_config(cfg))
return callbacks return callbacks
def _get_accelerator(use_cpu: bool) -> str: def _get_accelerator(use_cpu: bool) -> str:
return "ddp_cpu" if use_cpu else "ddp" return "ddp_cpu" if use_cpu else "ddp"
def get_trainer_params(cfg: CfgNode, num_machines: int, num_processes: int) -> Dict[str, Any]: def get_trainer_params(
cfg: CfgNode, num_machines: int, num_processes: int
) -> Dict[str, Any]:
use_cpu = cfg.MODEL.DEVICE.lower() == "cpu" use_cpu = cfg.MODEL.DEVICE.lower() == "cpu"
accelerator = _get_accelerator(use_cpu) accelerator = _get_accelerator(use_cpu)
plugins = [] plugins = []
if accelerator: if accelerator:
plugins.append(DDPPlugin(find_unused_parameters=cfg.MODEL.DDP_FIND_UNUSED_PARAMETERS)) plugins.append(
DDPPlugin(find_unused_parameters=cfg.MODEL.DDP_FIND_UNUSED_PARAMETERS)
)
return { return {
# training loop is bounded by max steps, use a large max_epochs to make # training loop is bounded by max steps, use a large max_epochs to make
...@@ -102,6 +107,7 @@ def get_trainer_params(cfg: CfgNode, num_machines: int, num_processes: int) -> D ...@@ -102,6 +107,7 @@ def get_trainer_params(cfg: CfgNode, num_machines: int, num_processes: int) -> D
"plugins": plugins, "plugins": plugins,
} }
def do_train( def do_train(
cfg: CfgNode, trainer: pl.Trainer, task: GeneralizedRCNNTask cfg: CfgNode, trainer: pl.Trainer, task: GeneralizedRCNNTask
) -> Dict[str, str]: ) -> Dict[str, str]:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment