question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Int8 model export fail with ImportError: cannot import name 'metanet_pb2' from partially initialized module 'caffe2.proto'

See original GitHub issue

When I export the model to INT8, I get this error。I installed the latest code of d2go according to revision in #137,but still get this error

import os
import json
import numpy as np
import cv2
from detectron2.data.datasets import register_coco_instances
from detectron2.structures import BoxMode
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
from d2go.model_zoo import model_zoo
from d2go.utils.demo_predictor import DemoPredictor
import matplotlib.pyplot as plt
import random
from d2go.runner import GeneralizedRCNNRunner
import copy
from detectron2.data import build_detection_test_loader
from d2go.export.api import convert_and_export_predictor
from d2go.utils.testing.data_loader_helper import create_fake_detection_data_loader
from d2go.export.d2_meta_arch import patch_d2_meta_arch
import logging
from mobile_cv.predictor.api import create_predictor
from d2go.utils.demo_predictor import DemoPredictor

register_coco_instances("my_dataset_train", {}, "/home/liupeng/sdb1/Image/ScaleDetection/Train/humanDetec/Annotations/coco_info.json",
                        "/home/liupeng/sdb1/Image/ScaleDetection/Train/humanDetec/Images")

def prepare_for_launch():
    runner = GeneralizedRCNNRunner()
    cfg = runner.get_default_cfg()
    cfg.merge_from_file(model_zoo.get_config_file("faster_rcnn_fbnetv3a_C4.yaml"))
    cfg.MODEL_EMA.ENABLED = False
    cfg.DATASETS.TRAIN = ("my_dataset_train",)
    cfg.DATASETS.TEST = ("my_dataset_train",)
    cfg.DATALOADER.NUM_WORKERS = 2
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("faster_rcnn_fbnetv3a_C4.yaml")  # Let training initialize from model zoo
    cfg.SOLVER.IMS_PER_BATCH = 2
    cfg.SOLVER.BASE_LR = 0.00025  # pick a good LR
    cfg.SOLVER.MAX_ITER = 1    # 600 iterations seems good enough for this toy dataset; you will need to train longer for a practical dataset
    cfg.SOLVER.STEPS = []        # do not decay learning rate
    cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 256   # faster, and good enough for this toy dataset (default: 512)
    cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1  # only has one class (ballon). (see https://detectron2.readthedocs.io/tutorials/datasets.html#update-the-config-for-new-datasets)
    # NOTE: this config means the number of classes, but a few popular unofficial tutorials incorrect uses num_classes+1 here.
    os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
    return cfg, runner

cfg, runner = prepare_for_launch()
model = runner.build_model(cfg)
runner.do_train(cfg, model, resume=False)


# disable all the warnings
previous_level = logging.root.manager.disable
logging.disable(logging.INFO)

patch_d2_meta_arch()

cfg_name = 'faster_rcnn_fbnetv3a_dsmask_C4.yaml'
pytorch_model = model_zoo.get(cfg_name, trained=True)
pytorch_model.cpu()

with create_fake_detection_data_loader(224, 320, is_train=False) as data_loader:
    predictor_path = convert_and_export_predictor(
            model_zoo.get_config(cfg_name),
            copy.deepcopy(pytorch_model),
            "torchscript_int8@tracing",
            './',
            data_loader,
        )

# recover the logging level
logging.disable(previous_level)


model = create_predictor(predictor_path)

im = cv2.imread("/home/liupeng/sdb1/PythonProject/scaleDetection/detectron2go/datasets/humandetect/Images/train/20.png")

predictor = DemoPredictor(model)
outputs = predictor(im)

v = Visualizer(im[:, :, ::-1], MetadataCatalog.get("my_dataset_train"))
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
plt.imshow(out.get_image()[:, :, ::-1]

this are all pacakge i used:

Package Version


absl-py 1.0.0 aiohttp 3.8.1 aiosignal 1.2.0 antlr4-python3-runtime 4.8 appdirs 1.4.4 async-timeout 4.0.1 attrs 21.2.0 black 21.4b2 cachetools 4.2.4 certifi 2021.10.8 charset-normalizer 2.0.8 click 8.0.3 cloudpickle 2.0.0 cycler 0.11.0 Cython 0.29.24 d2go 0.0.1b20211201 detectron2 0.6 fonttools 4.28.2 frozenlist 1.2.0 fsspec 2021.11.1 future 0.18.2 fvcore 0.1.5.post20211023 google-auth 2.3.3 google-auth-oauthlib 0.4.6 grpcio 1.42.0 hydra-core 1.1.1 idna 3.3 importlib 1.0.4 importlib-metadata 4.8.2 importlib-resources 5.4.0 iopath 0.1.9 kiwisolver 1.3.2 Markdown 3.3.6 matplotlib 3.5.0 mkl-fft 1.3.1 mkl-random 1.2.2 mkl-service 2.4.0 mobile-cv 0.1 mock 4.0.3 multidict 5.2.0 mypy-extensions 0.4.3 numpy 1.21.2 oauthlib 3.1.1 olefile 0.46 omegaconf 2.1.1 opencv-python 4.5.4.60 packaging 21.3 parameterized 0.8.1 pathspec 0.9.0 Pillow 8.4.0 pip 21.2.4 portalocker 2.3.2 protobuf 3.19.1 pyasn1 0.4.8 pyasn1-modules 0.2.8 pycocotools 2.0.3 pyDeprecate 0.3.1 pydot 1.4.2 pyparsing 3.0.6 python-dateutil 2.8.2 pytorch-lightning 1.5.4 PyYAML 6.0 regex 2021.11.10 requests 2.26.0 requests-oauthlib 1.3.0 rsa 4.8 setuptools 58.0.4 setuptools-scm 6.3.2 six 1.16.0 tabulate 0.8.9 tensorboard 2.7.0 tensorboard-data-server 0.6.1 tensorboard-plugin-wit 1.8.0 termcolor 1.1.0 toml 0.10.2 tomli 1.2.2 torch 1.11.0.dev20211201+cu113 torchaudio 0.11.0.dev20211201+cu113 torchmetrics 0.6.0 torchvision 0.12.0.dev20211201+cu113 tqdm 4.62.3 typing-extensions 3.10.0.2 urllib3 1.26.7 Werkzeug 2.0.2 wheel 0.37.0 yacs 0.1.8 yarl 1.7.2 zipp 3.6.0



this is error log:

creating index… index created! detectron2.layers.Linear is in expected type (torch.nn.Linear),consider removing this code mock_quantization_type. /home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/torch/ao/quantization/observer.py:177: UserWarning: Please use quant_min and quant_max to specify the range for observers. reduce_range will be deprecated in a future release of PyTorch. warnings.warn( /home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/torch/ao/quantization/observer.py:901: UserWarning: floordiv is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the ‘trunc’ function NOT ‘floor’). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode=‘trunc’), or for actual floor division, use torch.div(a, b, rounding_mode=‘floor’). src_bin_begin // dst_bin_width, 0, self.dst_nbins - 1 /home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/torch/ao/quantization/observer.py:906: UserWarning: floordiv is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the ‘trunc’ function NOT ‘floor’). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode=‘trunc’), or for actual floor division, use torch.div(a, b, rounding_mode=‘floor’). src_bin_end // dst_bin_width, 0, self.dst_nbins - 1 Traceback (most recent call last): File “train.py”, line 114, in <module> predictor_path = convert_and_export_predictor( File “/home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/d2go/export/api.py”, line 151, in convert_and_export_predictor return export_predictor(cfg, pytorch_model, predictor_type, output_dir, data_loader) File “/home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/d2go/export/api.py”, line 174, in export_predictor return default_export_predictor( File “/home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/d2go/export/api.py”, line 222, in default_export_predictor export_config = pytorch_model.prepare_for_export(cfg, inputs, predictor_type) File “/home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/d2go/modeling/meta_arch/rcnn.py”, line 45, in prepare_for_export return func(self, cfg, *args, **kwargs) File “/home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/d2go/modeling/meta_arch/rcnn.py”, line 60, in default_rcnn_prepare_for_export from detectron2.export.caffe2_modeling import META_ARCH_CAFFE2_EXPORT_TYPE_MAP File “/home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/detectron2/export/caffe2_modeling.py”, line 14, in <module> from .c10 import Caffe2Compatible File “/home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/detectron2/export/c10.py”, line 14, in <module> from .shared import alias, to_device File “/home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/detectron2/export/shared.py”, line 12, in <module> import caffe2.python.utils as putils File “/home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/caffe2/python/init.py”, line 2, in <module> from caffe2.proto import caffe2_pb2 File “/home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/caffe2/proto/init.py”, line 11, in <module> from caffe2.proto import caffe2_pb2, metanet_pb2, torch_pb2 ImportError: cannot import name ‘metanet_pb2’ from partially initialized module ‘caffe2.proto’ (most likely due to a circular import) (/home/liupeng/anaconda3/envs/scaleDetection/lib/python3.8/site-packages/caffe2/proto/init.py)

Expected behavior: Is the package version mismatched?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
wat3rBrocommented, Dec 30, 2021

Hi the issue should be gone after https://github.com/facebookresearch/d2go/pull/154

1reaction
LP308210365commented, Dec 29, 2021

No,i didn’t

发自我的iPad

在 2021年12月29日,上午8:54,Daniel @.***> 写道:

@AaronReidCI @LP308210365 Hi! Could I know do you solve this problem ?

#137 (comment)

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot import name 'metanet_pb2' from partially initialized ...
When I export the model to INT8, I get this error。 ... ImportError: cannot import name 'metanetpb2' from partially initialized module 'caffe2.proto' (most ......
Read more >
ImportError: cannot import name '..' from partially initialized ...
I have an app with a form that has been working fine until now. I'm not sure what I did to cause this...
Read more >
Troubleshooting VM Import/Export
You attempted to import a differencing VHD, or there was an error in creating the VHD. Export your VM again and retry importing...
Read more >
Onnx export failed int8 model - quantization
You need first an onnx model which you later convert to caffe2. But if I get an error when exporting to onnx, how...
Read more >
cannot import name 'metanet_pb2' from partially initialized ...
ImportError : cannot import name 'metanet_pb2' from partially initialized module 'caffe2.proto' · 2 · 踩 · 1.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found