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
from d2go.utils.testing.rcnn_helper import RCNNBaseTestCases
from d2go.utils.flop_calculator import dump_flops_info
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):
......@@ -19,6 +20,10 @@ class TestFlopCount(RCNNBaseTestCases.TemplateTestCase):
with tempfile.TemporaryDirectory(prefix="d2go_test") as 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")
self.assertTrue(os.path.isfile(outf))
......@@ -2,9 +2,9 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import glob
import tempfile
import unittest
import glob
import d2go.utils.abnormal_checker as ac
import torch
......@@ -35,9 +35,7 @@ class TestUtilsAbnormalChecker(unittest.TestCase):
model = Model()
with tempfile.TemporaryDirectory() as tmp_dir:
checker = ac.AbnormalLossChecker(
-1, writers=[ac.FileWriter(tmp_dir)]
)
checker = ac.AbnormalLossChecker(-1, writers=[ac.FileWriter(tmp_dir)])
cmodel = ac.AbnormalLossCheckerWrapper(model, checker)
losses = [5, 4, 3, 10, 9, 2, 5, 4]
......
......@@ -2,16 +2,16 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import numpy as np
import torch
import unittest
import numpy as np
import torch
from detectron2.modeling.box_regression import Box2BoxTransform
class TestBox2BoxTransform(unittest.TestCase):
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
"""
box2box_transform = Box2BoxTransform(weights=(1.0, 1.0, 1.0, 1.0))
......
......@@ -2,11 +2,11 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
import unittest
from detectron2.modeling.roi_heads.fast_rcnn import fast_rcnn_inference
import torch
from detectron2.layers import cat
from detectron2.modeling.roi_heads.fast_rcnn import fast_rcnn_inference
from detectron2.structures import Boxes
......@@ -37,10 +37,12 @@ class TestBoxWithNMSLimit(unittest.TestCase):
scores = [torch.rand(n, num_class + 1, generator=rng) for n in batch_splits]
ref_results, ref_kept_indices = fast_rcnn_inference(
boxes, scores, image_shapes,
boxes,
scores,
image_shapes,
score_thresh=score_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):
torch.testing.assert_allclose(
......@@ -78,7 +80,14 @@ class TestBoxWithNMSLimit(unittest.TestCase):
output_classes_include_bg_cls=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_bbox_nms = roi_bbox_nms.split(roi_batch_splits_nms.int().tolist())
......
......@@ -3,7 +3,8 @@ import unittest
from d2go.model_zoo import model_zoo
OSSRUN = os.getenv('OSSRUN') == '1'
OSSRUN = os.getenv("OSSRUN") == "1"
class TestD2GoModelZoo(unittest.TestCase):
@unittest.skipIf(not OSSRUN, "OSS test only")
......@@ -12,5 +13,6 @@ class TestD2GoModelZoo(unittest.TestCase):
for cfgfile in configs:
model = model_zoo.get(cfgfile, trained=True)
if __name__ == "__main__":
unittest.main()
......@@ -67,7 +67,7 @@ class TestModelingImagePooler(unittest.TestCase):
self.assertArrayEqual(pooled_box, [[2, 3, 5, 7]])
def test_image_pooler_scale_box_large_crop_only(self):
""" Crop bbox """
"""Crop bbox"""
H, W = 398, 224
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"]
......@@ -86,7 +86,7 @@ class TestModelingImagePooler(unittest.TestCase):
self.assertArrayEqual(sub_boxes.tensor, [[0, 0, 50, 40], [100, 20, 150, 80]])
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
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"]
......@@ -111,7 +111,7 @@ class TestModelingImagePooler(unittest.TestCase):
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):
""" Crop bbox that is scaled, resize the cropped box """
"""Crop bbox that is scaled, resize the cropped box"""
H, W = 398, 224
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"]
......
......@@ -11,7 +11,7 @@ from detectron2.layers import nms as box_nms
class TestNMS(unittest.TestCase):
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
"""
......@@ -60,7 +60,7 @@ class TestNMS(unittest.TestCase):
np.testing.assert_array_equal(keep_indices, np.array(gt_index))
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
"""
......
......@@ -2,10 +2,12 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
import unittest
from d2go.optimizer import build_optimizer_mapper
import d2go.runner.default_runner as default_runner
import torch
from d2go.optimizer import build_optimizer_mapper
class TestArch(torch.nn.Module):
def __init__(self):
......@@ -22,6 +24,7 @@ class TestArch(torch.nn.Module):
ret = self.avgpool(ret)
return ret
def _test_each_optimizer(cfg):
model = TestArch()
optimizer = build_optimizer_mapper(cfg, model)
......@@ -33,12 +36,12 @@ def _test_each_optimizer(cfg):
loss.backward()
optimizer.step()
class TestOptimizer(unittest.TestCase):
class TestOptimizer(unittest.TestCase):
def test_all_optimizers(self):
runner = default_runner.Detectron2GoRunner()
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 mult in multipliers:
......
......@@ -6,9 +6,9 @@ import logging
import unittest
import torch
from d2go.runner import GeneralizedRCNNRunner
from detectron2.modeling import build_anchor_generator, build_backbone
from detectron2.modeling.proposal_generator import rpn
from d2go.runner import GeneralizedRCNNRunner
logger = logging.getLogger(__name__)
......@@ -19,7 +19,7 @@ RPN_CFGS = {}
class TestRPNHeads(unittest.TestCase):
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)
......@@ -65,7 +65,7 @@ class TestRPNHeads(unittest.TestCase):
)
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)
......
......@@ -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).
"""
import torch
import logging
import torch
from d2go.distributed import launch
from d2go.setup import (
basic_argument_parser,
......
......@@ -41,7 +41,7 @@ class TrainOutput:
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:
cfg.OUTPUT_DIR = output_dir
logger.warning(
......@@ -70,16 +70,21 @@ def _get_trainer_callbacks(cfg: CfgNode) -> List[Callback]:
callbacks.append(QuantizationAwareTraining.from_config(cfg))
return callbacks
def _get_accelerator(use_cpu: bool) -> str:
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"
accelerator = _get_accelerator(use_cpu)
plugins = []
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 {
# 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
"plugins": plugins,
}
def do_train(
cfg: CfgNode, trainer: pl.Trainer, task: GeneralizedRCNNTask
) -> 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