Detectron2是Facebook AI Research的检测和分割框架,其主要基于PyTorch实现,但具有更模块化设计,因此它是灵活且便于扩展的,具体简介可见Github库和Meta AI Blog Post。
1 2 3 4 5 6 7
@misc{wu2019detectron2, author = {Yuxin Wu and Alexander Kirillov and Francisco Massa and Wan-Yen Lo and Ross Girshick}, title = {Detectron2}, howpublished = {\url{https://github.com/facebookresearch/detectron2}}, year = {2019} }
1. Detectron2安装
首先官方要求的环境条件如下:
Linux or macOS with Python ≥ 3.6
PyTorch ≥ 1.8 and torchvision that matches the PyTorch installation. Install them together at pytorch.org to make sure of this
OpenCV is optional but needed by demo and visualization
gcc & g++ ≥ 5.4 are required
ninja is optional but recommended for faster build
python -m pip install 'git+https://github.com/facebookresearch/detectron2.git' # (add --user if you don't have permission) # Or, to install it from a local clone: git clone https://github.com/facebookresearch/detectron2.git python -m pip install -e detectron2 # On macOS, you may need to prepend the above commands with a few environment variables: CC=clang CXX=clang++ ARCHFLAGS="-arch x86_64" python -m pip install ...
def_get_data_all_instance_meta(): thing_ids = [k["id"] for k in DATA_ALL_CATEGORIES] thing_dataset_id_to_contiguous_id = {k: i for i, k inenumerate(thing_ids)} thing_classes = [k["name"] for k in DATA_ALL_CATEGORIES] ret = { "thing_dataset": thing_dataset_id_to_contiguous_id, "thing_classes": thing_classes, } return ret
def_get_builtin_metadata(dataset_name): if dataset_name == "nwpu_all": return _get_data_instance_meta(DATA_ALL_CATEGORIES)
defregister_all(root): for dataset_name, splits_per_dataset in DATA_SPLITS.items(): for key, (image_root, json_file) in splits_per_dataset.items(): assert os.path.exists(os.path.join(root, json_file)) register_coco_instances( key, _get_builtin_metadata(dataset_name), os.path.join(root, json_file) if"://"notin json_file else json_file, os.path.join(root, image_root), )
defregister_coco_instances(name, metadata, json_file, image_root): """ Args: name (str): the name that identifies a dataset, e.g. "coco_2014_train". metadata (dict): extra metadata associated with this dataset. You can leave it as an empty dict. json_file (str): path to the json instance annotation file. image_root (str or path-like): directory which contains all the images. """ assertisinstance(name, str), name assertisinstance(json_file, (str, os.PathLike)), json_file assertisinstance(image_root, (str, os.PathLike)), image_root # 1. register a function which returns dicts DatasetCatalog.register(name, lambda: load_coco_json(json_file, image_root, name))
# 2. Optionally, add metadata about this dataset, # since they might be useful in evaluation, visualization or logging MetadataCatalog.get(name).set( json_file=json_file, image_root=image_root, evaluator_type="coco", **metadata )
for data in random.sample(datasets_dicts, 1): img = utils.read_image(data["file_name"]) visual = Visualizer(img, metadata=MetadataCatalog.get("nwpu_all_trainval"),scale=0.5) vis = visual.draw_dataset_dict(data) cv2.imshow("window", vis.get_image()[:, :, ::-1]) cv2.waitKey()
from detectron2.config import get_cfg import detectron2.data.transforms as T from detectron2.model_zoo import model_zoo from detectron2.data import build_detection_train_loader from detectron2.data import DatasetMapper # the default mapper
from detectron2.data import detection_utils as utils import detectron2.data.transforms as T
defmapper(dataset_dict): dataset_dict = copy.deepcopy(dataset_dict) # it will be modified by code below # can use other ways to read image image = utils.read_image(dataset_dict["file_name"], format="BGR") # "Data Augmentation" auginput = T.AugInput(image) transform = T.Resize((800, 800))(auginput) image = torch.from_numpy(auginput.image.transpose(2, 0, 1)) annos = [ utils.transform_instance_annotations(annotation, [transform], image.shape[1:]) for annotation in dataset_dict.pop("annotations") ] return { # create the format that the model expects "image": image, "instances": utils.annotations_to_instances(annos, image.shape[1:]) }
defregister_hooks(self, hooks: List[Optional[HookBase]]) -> None: hooks = [h for h in hooks if h isnotNone] for h in hooks: assertisinstance(h, HookBase) h.trainer = weakref.proxy(self) self._hooks.extend(hooks)
deftrain(self, start_iter: int, max_iter: int): self.iter = self.start_iter = start_iter self.max_iter = max_iter with EventStorage(start_iter) as self.storage: try: self.before_train() for self.iterinrange(start_iter, max_iter): self.before_step() self.run_step() self.after_step() finally: self.after_train()
defbefore_train(self): for h in self._hooks: h.before_train()
defafter_train(self): self.storage.iter = self.iter for h in self._hooks: h.after_train()
defbefore_step(self): self.storage.iter = self.iter for h in self._hooks: h.before_step()
defafter_step(self): for h in self._hooks: h.after_step()
defrun_step(self): raise NotImplementedError
defstate_dict(self): pass
defload_state_dict(self, state_dict): pass
简化一点,它将一个训练过程抽象成:
1 2 3 4 5 6 7
hook.before_train() foriterinrange(start_iter, max_iter): hook.before_step() trainer.run_step() hook.after_step() iter += 1 hook.after_train()
from detectron2.config import LazyCall as L from detectron2.modeling.backbone import RegNet from detectron2.modeling.backbone.regnet import SimpleStem, ResBottleneckBlock