Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved from fvcore's PathManager to iopath's #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]
26 changes: 0 additions & 26 deletions fsdet/checkpoint/catalog.py

This file was deleted.

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
56 changes: 56 additions & 0 deletions fsdet/utils/file_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from iopath.common.file_io import HTTPURLHandler, OneDrivePathHandler, PathHandler
from iopath.common.file_io import PathManager as PathManagerBase

__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 Detectron2Handler(PathHandler):
"""
Resolve anything that's hosted under detectron2's namespace.
"""

PREFIX = "detectron2://"
S3_DETECTRON2_PREFIX = "https://dl.fbaipublicfiles.com/detectron2/"

def _get_supported_prefixes(self):
return [self.PREFIX]

def _get_local_path(self, path):
name = path[len(self.PREFIX) :]
return PathManager.get_local_path(self.S3_DETECTRON2_PREFIX + name)

def _open(self, path, mode="r", **kwargs):
return PathManager.open(self._get_local_path(path), mode, **kwargs)


class FsDetHandler(PathHandler):
"""
Resolve anything that's in FsDet model zoo.
"""

PREFIX = "fsdet://"
URL_PREFIX = "http://dl.yf.io/fs-det/models/"

def _get_supported_prefixes(self):
return [self.PREFIX]

def _get_local_path(self, path):
name = path[len(self.PREFIX) :]
return PathManager.get_local_path(self.URL_PREFIX + name)

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(Detectron2Handler())
PathManager.register_handler(FsDetHandler())