Skip to content

Commit

Permalink
reapply linter & upgrade black to 24.3.0
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #5275

Differential Revision: D56913399
  • Loading branch information
Yanghan Wang authored and facebook-github-bot committed May 2, 2024
1 parent bce6d72 commit bd8f473
Show file tree
Hide file tree
Showing 41 changed files with 267 additions and 459 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ jobs:
# flake8-bugbear flake8-comprehensions are useful but not available internally
run: |
python -m pip install --upgrade pip
python -m pip install flake8==3.8.1 isort==4.3.21
python -m pip install black==22.3.0
python -m pip install flake8==3.12.3 isort==4.3.21
python -m pip install black==24.3.0
flake8 --version
- name: Lint
run: |
Expand Down
1 change: 0 additions & 1 deletion configs/Misc/torchvision_imagenet_R_50.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""


import torch
from torch import nn
from torch.nn import functional as F
Expand Down
2 changes: 1 addition & 1 deletion configs/common/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
checkpointer=dict(period=5000, max_to_keep=100), # options for PeriodicCheckpointer
eval_period=5000,
log_period=20,
device="cuda"
device="cuda",
# ...
)
27 changes: 10 additions & 17 deletions demo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import argparse
import glob
import multiprocessing as mp
import numpy as np
import os
import tempfile
import time
import warnings

import cv2
import numpy as np
import tqdm

from detectron2.config import get_cfg
Expand All @@ -32,9 +31,7 @@ def setup_cfg(args):
# Set score_threshold for builtin models
cfg.MODEL.RETINANET.SCORE_THRESH_TEST = args.confidence_threshold
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = args.confidence_threshold
cfg.MODEL.PANOPTIC_FPN.COMBINE.INSTANCES_CONFIDENCE_THRESH = (
args.confidence_threshold
)
cfg.MODEL.PANOPTIC_FPN.COMBINE.INSTANCES_CONFIDENCE_THRESH = args.confidence_threshold
cfg.freeze()
return cfg

Expand All @@ -47,9 +44,7 @@ def get_parser():
metavar="FILE",
help="path to config file",
)
parser.add_argument(
"--webcam", action="store_true", help="Take inputs from webcam."
)
parser.add_argument("--webcam", action="store_true", help="Take inputs from webcam.")
parser.add_argument("--video-input", help="Path to video file.")
parser.add_argument(
"--input",
Expand Down Expand Up @@ -118,9 +113,11 @@ def main() -> None:
logger.info(
"{}: {} in {:.2f}s".format(
path,
"detected {} instances".format(len(predictions["instances"]))
if "instances" in predictions
else "finished",
(
"detected {} instances".format(len(predictions["instances"]))
if "instances" in predictions
else "finished"
),
time.time() - start_time,
)
)
Expand All @@ -130,9 +127,7 @@ def main() -> None:
assert os.path.isdir(args.output), args.output
out_filename = os.path.join(args.output, os.path.basename(path))
else:
assert (
len(args.input) == 1
), "Please specify a directory with args.output"
assert len(args.input) == 1, "Please specify a directory with args.output"
out_filename = args.output
visualized_output.save(out_filename)
else:
Expand All @@ -159,9 +154,7 @@ def main() -> None:
num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
basename = os.path.basename(args.video_input)
codec, file_ext = (
("x264", ".mkv")
if test_opencv_video_format("x264", ".mkv")
else ("mp4v", ".mp4")
("x264", ".mkv") if test_opencv_video_format("x264", ".mkv") else ("mp4v", ".mp4")
)
if codec == ".mp4v":
warnings.warn("x264 codec not available, switching to mp4v")
Expand Down
1 change: 0 additions & 1 deletion detectron2/checkpoint/c2_model_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import re
from typing import Dict, List
import torch
from tabulate import tabulate


def convert_basic_c2_names(original_keys):
Expand Down
47 changes: 27 additions & 20 deletions detectron2/data/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def build_batch_data_loader(
collate_fn=operator.itemgetter(0), # don't batch, but yield individual elements
worker_init_fn=worker_init_reset_seed,
generator=generator,
**kwargs
**kwargs,
) # yield individual mapped dict
data_loader = AspectRatioGroupedDataset(data_loader, batch_size)
if collate_fn is None:
Expand Down Expand Up @@ -408,12 +408,14 @@ def _build_weighted_sampler(cfg, enable_category_balance=False):
name: get_detection_dataset_dicts(
[name],
filter_empty=cfg.DATALOADER.FILTER_EMPTY_ANNOTATIONS,
min_keypoints=cfg.MODEL.ROI_KEYPOINT_HEAD.MIN_KEYPOINTS_PER_IMAGE
if cfg.MODEL.KEYPOINT_ON
else 0,
proposal_files=cfg.DATASETS.PROPOSAL_FILES_TRAIN
if cfg.MODEL.LOAD_PROPOSALS
else None,
min_keypoints=(
cfg.MODEL.ROI_KEYPOINT_HEAD.MIN_KEYPOINTS_PER_IMAGE
if cfg.MODEL.KEYPOINT_ON
else 0
),
proposal_files=(
cfg.DATASETS.PROPOSAL_FILES_TRAIN if cfg.MODEL.LOAD_PROPOSALS else None
),
)
for name in cfg.DATASETS.TRAIN
}
Expand Down Expand Up @@ -466,9 +468,9 @@ def _train_loader_from_config(cfg, mapper=None, *, dataset=None, sampler=None):
dataset = get_detection_dataset_dicts(
cfg.DATASETS.TRAIN,
filter_empty=cfg.DATALOADER.FILTER_EMPTY_ANNOTATIONS,
min_keypoints=cfg.MODEL.ROI_KEYPOINT_HEAD.MIN_KEYPOINTS_PER_IMAGE
if cfg.MODEL.KEYPOINT_ON
else 0,
min_keypoints=(
cfg.MODEL.ROI_KEYPOINT_HEAD.MIN_KEYPOINTS_PER_IMAGE if cfg.MODEL.KEYPOINT_ON else 0
),
proposal_files=cfg.DATASETS.PROPOSAL_FILES_TRAIN if cfg.MODEL.LOAD_PROPOSALS else None,
)
_log_api_usage("dataset." + cfg.DATASETS.TRAIN[0])
Expand Down Expand Up @@ -522,7 +524,7 @@ def build_detection_train_loader(
aspect_ratio_grouping=True,
num_workers=0,
collate_fn=None,
**kwargs
**kwargs,
):
"""
Build a dataloader for object detection with some default features.
Expand Down Expand Up @@ -574,7 +576,7 @@ def build_detection_train_loader(
aspect_ratio_grouping=aspect_ratio_grouping,
num_workers=num_workers,
collate_fn=collate_fn,
**kwargs
**kwargs,
)


Expand All @@ -589,21 +591,26 @@ def _test_loader_from_config(cfg, dataset_name, mapper=None):
dataset = get_detection_dataset_dicts(
dataset_name,
filter_empty=False,
proposal_files=[
cfg.DATASETS.PROPOSAL_FILES_TEST[list(cfg.DATASETS.TEST).index(x)] for x in dataset_name
]
if cfg.MODEL.LOAD_PROPOSALS
else None,
proposal_files=(
[
cfg.DATASETS.PROPOSAL_FILES_TEST[list(cfg.DATASETS.TEST).index(x)]
for x in dataset_name
]
if cfg.MODEL.LOAD_PROPOSALS
else None
),
)
if mapper is None:
mapper = DatasetMapper(cfg, False)
return {
"dataset": dataset,
"mapper": mapper,
"num_workers": cfg.DATALOADER.NUM_WORKERS,
"sampler": InferenceSampler(len(dataset))
if not isinstance(dataset, torchdata.IterableDataset)
else None,
"sampler": (
InferenceSampler(len(dataset))
if not isinstance(dataset, torchdata.IterableDataset)
else None
),
}


Expand Down
31 changes: 10 additions & 21 deletions detectron2/data/datasets/cityscapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
import json
import logging
import multiprocessing as mp
import numpy as np
import os
from itertools import chain

import numpy as np
import pycocotools.mask as mask_util
from PIL import Image

from detectron2.structures import BoxMode
from detectron2.utils.comm import get_world_size
from detectron2.utils.file_io import PathManager
from detectron2.utils.logger import setup_logger
from PIL import Image

try:
import cv2 # noqa
Expand All @@ -40,9 +39,7 @@ def _get_cityscapes_files(image_dir, gt_dir):
assert basename.endswith(suffix), basename
basename = basename[: -len(suffix)]

instance_file = os.path.join(
city_gt_dir, basename + "gtFine_instanceIds.png"
)
instance_file = os.path.join(city_gt_dir, basename + "gtFine_instanceIds.png")
label_file = os.path.join(city_gt_dir, basename + "gtFine_labelIds.png")
json_file = os.path.join(city_gt_dir, basename + "gtFine_polygons.json")

Expand Down Expand Up @@ -79,9 +76,7 @@ def load_cityscapes_instances(image_dir, gt_dir, from_json=True, to_polygons=Tru
pool = mp.Pool(processes=max(mp.cpu_count() // get_world_size() // 2, 4))

ret = pool.map(
functools.partial(
_cityscapes_files_to_dict, from_json=from_json, to_polygons=to_polygons
),
functools.partial(_cityscapes_files_to_dict, from_json=from_json, to_polygons=to_polygons),
files,
)
logger.info("Loaded {} images from {}".format(len(ret), image_dir))
Expand Down Expand Up @@ -110,9 +105,7 @@ def load_cityscapes_semantic(image_dir, gt_dir):
ret = []
# gt_dir is small and contain many small files. make sense to fetch to local first
gt_dir = PathManager.get_local_path(gt_dir)
for image_file, _, label_file, json_file in _get_cityscapes_files(
image_dir, gt_dir
):
for image_file, _, label_file, json_file in _get_cityscapes_files(image_dir, gt_dir):
label_file = label_file.replace("labelIds", "labelTrainIds")

with PathManager.open(json_file, "r") as f:
Expand Down Expand Up @@ -216,9 +209,7 @@ def _cityscapes_files_to_dict(files, from_json, to_polygons):
elif isinstance(poly_wo_overlaps, MultiPolygon):
poly_list = poly_wo_overlaps.geoms
else:
raise NotImplementedError(
"Unknown geometric structure {}".format(poly_wo_overlaps)
)
raise NotImplementedError("Unknown geometric structure {}".format(poly_wo_overlaps))

poly_coord = []
for poly_el in poly_list:
Expand Down Expand Up @@ -272,9 +263,9 @@ def _cityscapes_files_to_dict(files, from_json, to_polygons):
if to_polygons:
# This conversion comes from D4809743 and D5171122,
# when Mask-RCNN was first developed.
contours = cv2.findContours(
mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
)[-2]
contours = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[
-2
]
polygons = [c.reshape(-1).tolist() for c in contours if len(c) >= 3]
# opencv's can produce invalid polygons
if len(polygons) == 0:
Expand Down Expand Up @@ -318,9 +309,7 @@ def main() -> None:
)
logger.info("Done loading {} samples.".format(len(dicts)))

thing_classes = [
k.name for k in labels if k.hasInstances and not k.ignoreInEval
]
thing_classes = [k.name for k in labels if k.hasInstances and not k.ignoreInEval]
meta = Metadata().set(thing_classes=thing_classes)

else:
Expand Down
Loading

0 comments on commit bd8f473

Please sign in to comment.