facebookresearch / maskrcnn-benchmark

Fast, modular reference implementation of Instance Segmentation and Object Detection algorithms in PyTorch.
MIT License
9.29k stars 2.5k forks source link

Convert Group Norm models from Detectron to Maskrcnn-Benchmark #565

Closed ltnghia closed 5 years ago

ltnghia commented 5 years ago

❓ Questions and Help

I tried to convert Group Norm models from Detectron to Maskrcnn-Benchmark, but structure of them are so different in FPN, roi_heads. How can we load GN-Detectron models?

fmassa commented 5 years ago

Maybe @zimenglan-sysu-512 can help here given that he implemented GroupNorm?

zimenglan-sysu-512 commented 5 years ago

hi @ltnghia if u want to convert Group Norm in the imagenet pretrained model from Detectron, u can see these lines. btw, i don't have a try to convert GN in the coco-trained model from Detectron.

ltnghia commented 5 years ago

Great thanks to @zimenglan-sysu-512

zimenglan-sysu-512 commented 5 years ago

hi @ltnghia u can use load_c2_pkl_weights to see the keys of trained model (pkl file) from detectron:

def load_c2_pkl_weights(file_path):
    with open(file_path, "rb") as f:
        if torch._six.PY3:
            data = pickle.load(f, encoding="latin1")
        else:
            data = pickle.load(f)
    if "blobs" in data:
        weights = data["blobs"]
    else:
        weights = data
    return weights

and then map these key to maskrcnn-benchmark manually:

def see_and_map_keys(weights):
    fpns = []
    for k in weights.keys():
        if k.find("fpn") is not -1:
            fpns.append(k)
    rpns = []
    for k in weights.keys():
        if k.find("rpn") is not -1:
            rpns.append(k)
    roi_heads = []
    for k in weights.keys():
        if k.find("head") is not -1 or k.find("pred") is not -1:
            roi_heads.append(k)
    masks = []
    for k in weights.keys():
        if k.find("mask") is not -1 or k.find("logits") is not -1:
            masks.append(k)
   # map the keys here
   ......
ltnghia commented 5 years ago

@zimenglan-sysu-512: Thank you for your help.

fmassa commented 5 years ago

@ltnghia if you manage to make the conversion work, a PR adding support for it would be great!