Skip to content

Commit

Permalink
Moved from fvcore's PathManager to iopath's
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasehuang committed Mar 3, 2021
1 parent 6b0769b commit 34028a7
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 18 deletions.
1 change: 0 additions & 1 deletion fsdet/checkpoint/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from . import catalog as _UNUSED # register the handler
from .detection_checkpoint import DetectionCheckpointer

__all__ = ["DetectionCheckpointer"]
23 changes: 17 additions & 6 deletions fsdet/checkpoint/detection_checkpoint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pickle
from fsdet.utils.file_io import PathManager
from fvcore.common.checkpoint import Checkpointer
from fvcore.common.file_io import PathManager

import detectron2.utils.comm as comm
from detectron2.checkpoint.c2_model_loading import align_and_update_state_dicts
Expand All @@ -20,6 +20,7 @@ def __init__(self, model, save_dir="", *, save_to_disk=None, **checkpointables):
save_to_disk=is_main_process if save_to_disk is None else save_to_disk,
**checkpointables,
)
self.path_manager = PathManager

def _load_file(self, filename):
if filename.endswith(".pkl"):
Expand All @@ -46,12 +47,22 @@ def _load_model(self, checkpoint):
if checkpoint.get("matching_heuristics", False):
self._convert_ndarray_to_tensor(checkpoint["model"])
# convert weights by name-matching heuristics
model_state_dict = self.model.state_dict()
align_and_update_state_dicts(
model_state_dict,
checkpoint["model"] = align_and_update_state_dicts(
self.model.state_dict(),
checkpoint["model"],
c2_conversion=checkpoint.get("__author__", None) == "Caffe2",
)
checkpoint["model"] = model_state_dict
# for non-caffe2 models, use standard ways to load it
super()._load_model(checkpoint)
incompatible = super()._load_model(checkpoint)

model_buffers = dict(self.model.named_buffers(recurse=False))
for k in ["pixel_mean", "pixel_std"]:
# Ignore missing key message about pixel_mean/std.
# Though they may be missing in old checkpoints, they will be correctly
# initialized from config anyway.
if k in model_buffers:
try:
incompatible.missing_keys.remove(k)
except ValueError:
pass
return incompatible
2 changes: 1 addition & 1 deletion fsdet/data/meta_coco.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import numpy as np
from fvcore.common.file_io import PathManager
from pycocotools.coco import COCO

import contextlib
import io
import os
from detectron2.data import DatasetCatalog, MetadataCatalog
from detectron2.structures import BoxMode
from fsdet.utils.file_io import PathManager

"""
This file contains functions to parse COCO-format annotations into dicts in "Detectron2 format".
Expand Down
2 changes: 1 addition & 1 deletion fsdet/data/meta_lvis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fvcore.common.file_io import PathManager
from fsdet.utils.file_io import PathManager
from fvcore.common.timer import Timer

import logging
Expand Down
2 changes: 1 addition & 1 deletion fsdet/data/meta_pascal_voc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from fvcore.common.file_io import PathManager
from fsdet.utils.file_io import PathManager

import os
import xml.etree.ElementTree as ET
Expand Down
3 changes: 1 addition & 2 deletions fsdet/engine/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import os
from collections import OrderedDict
import torch
from fvcore.common.file_io import PathManager
from fvcore.nn.precise_bn import get_bn_modules
from torch.nn.parallel import DistributedDataParallel

Expand All @@ -25,6 +24,7 @@
verify_results,
)
from fsdet.modeling import build_model
from fsdet.utils.file_io import PathManager
from detectron2.data import (
MetadataCatalog,
build_detection_test_loader,
Expand All @@ -40,7 +40,6 @@
JSONWriter,
TensorboardXWriter,
)

from detectron2.utils.logger import setup_logger


Expand Down
3 changes: 2 additions & 1 deletion fsdet/engine/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import os
import time
import torch
from fvcore.common.file_io import PathManager

import detectron2.utils.comm as comm
from detectron2.config import global_cfg
from detectron2.engine.train_loop import HookBase
from detectron2.evaluation.testing import flatten_results_dict

from fsdet.utils.file_io import PathManager

__all__ = ["EvalHookFsdet"]


Expand Down
2 changes: 1 addition & 1 deletion fsdet/evaluation/coco_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import os
import torch
from collections import OrderedDict
from fvcore.common.file_io import PathManager
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from tabulate import tabulate
Expand All @@ -20,6 +19,7 @@
from detectron2.utils.logger import create_small_table

from fsdet.evaluation.evaluator import DatasetEvaluator
from fsdet.utils.file_io import PathManager


class COCOEvaluator(DatasetEvaluator):
Expand Down
2 changes: 1 addition & 1 deletion fsdet/evaluation/lvis_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import os
from collections import OrderedDict
import torch
from fvcore.common.file_io import PathManager

import detectron2.utils.comm as comm
from detectron2.data import MetadataCatalog
from detectron2.utils.logger import create_small_table

from fsdet.evaluation.coco_evaluation import instances_to_coco_json
from fsdet.evaluation.evaluator import DatasetEvaluator
from fsdet.utils.file_io import PathManager


class LVISEvaluator(DatasetEvaluator):
Expand Down
16 changes: 13 additions & 3 deletions fsdet/checkpoint/catalog.py → fsdet/utils/file_io.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
"""FS-Det Model Handler."""
from fvcore.common.file_io import PathHandler, PathManager
from iopath.common.file_io import HTTPURLHandler, OneDrivePathHandler, PathHandler
from iopath.common.file_io import PathManager as PathManagerBase

import logging
__all__ = ["PathManager", "PathHandler"]


PathManager = PathManagerBase()
"""
This is a detectron2 project-specific PathManager.
We try to stay away from global PathManager in fvcore as it
introduces potential conflicts among other libraries.
"""


class FsDetHandler(PathHandler):
Expand All @@ -23,4 +31,6 @@ def _open(self, path, mode="r", **kwargs):
return PathManager.open(self._get_local_path(path), mode, **kwargs)


PathManager.register_handler(HTTPURLHandler())
PathManager.register_handler(OneDrivePathHandler())
PathManager.register_handler(FsDetHandler())

0 comments on commit 34028a7

Please sign in to comment.