Title: AttributeError: 'str' object has no attribute 'META_ARCHITECTURE' in "Detectron2"
Tags: python-3.x;pytorch;object-detection;detectron
Question: I'm using detectron2 library for object detection, for this I try to train the custom dataset to make following is sequence. but it's showing me an AttributeError
Here is my code:
For libraries---
import torch
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import ColorMode, Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
import cv2
from google.colab.patches import cv2_imshow
# detectron2 dependencies
from detectron2.data import DatasetCatalog, MetadataCatalog
from detectron2.utils.visualizer import Visualizer
from detectron2.config import get_cfg
from detectron2 import model_zoo
from detectron2.utils.visualizer import ColorMode
# visualizer for display
import random
import cv2
import matplotlib.pyplot as plt
Function to elaborate:
def plot_samples(dataset_name, n = 3):
dataset_custom = DatasetCatalog.get(dataset_name)
dataset_custom_metadata = MetadataCatalog.get(dataset_name)
for i in random.sample(dataset_custom, n):
img = cv2.imread(i['file_name'])
v = Visualizer(img[:,:,75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0-1], metadata=dataset_custom_metadata, scale = 0.5)
v = v.draw_dataset_dict(i)
plt.figure(figsize=(5, 7))
plt.imshow(v.get_image())
plt.show()
def get_train_cfg(config_file_path, checkpoint_url, train_dataset_name, test_dataset_name, num_classes, device, output_dir):
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file(config_file_path))
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(checkpoint_url)
cfg.DATASETS.TRAIN = (train_dataset_name,)
cfg.DATASETS.TEST = (test_dataset_name,)
cfg.DATALOADER.NUM_WORKERS = 2
cfg.SOLVER.IMS_PER_BATCH = 2
cfg.SOLVER.BASE_LR = 0.00025
cfg.SOLVER.MAX_ITER = 1000
cfg.SOLVER.STEPS = []
cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes
cfg.MODEL = device
cfg.OUTPUT_DIR = output_dir
return cfg
Trainig code:
from detectron2.utils.logger import setup_logger
setup_logger()
from detectron2.data.datasets import register_coco_instances
from detectron2.engine import DefaultTrainer
import os
import pickle
config_file_path = 'COCO-Detection/faster_rcnn_R_50_FPN_1x.yaml'
checkpoint_url = 'COCO-Detection/faster_rcnn_R_50_FPN_1x.yaml'
output_dir = '/output/object_detection'
num_classes = 3
device = 'cuda' # cpu
#train define
train_dataset_name = 'sports_train'
train_images_path = '/content/drive/MyDrive/Folder/train'
train_json_path = '/content/drive/MyDrive/Folder/train.json'
# test define
test_dataset_name = 'sports_test'
test_images_path = '/content/drive/MyDrive/Folder/test'
test_json_path = '/content/drive/MyDrive/Folder/val.json'
cfg_save_path = 'OD_cfg.pickle'
if train_dataset_name and test_dataset_name in DatasetCatalog.list():
DatasetCatalog.remove(train_dataset_name)
DatasetCatalog.remove(test_dataset_name)
register_coco_instances(
name = train_dataset_name,
metadata={},
json_file = train_json_path,
image_root = train_images_path
register_coco_instances(
name = test_dataset_name,
metadata = {},
json_file = test_json_path,
image_root = test_images_path
#plot_samples(dataset_name = train_dataset_name, n = 3)
# training
def main():
cfg = get_train_cfg(config_file_path, checkpoint_url, train_dataset_name, test_dataset_name, num_classes, device, output_dir)
with open(cfg_save_path, 'wb') as f:
pickle.dump(cfg, f, protocol = pickle.HIGHEST_PROTOCOL)
os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
trainer = DefaultTrainer(cfg)
trainer.resume_or_load(resume=False)
trainer.train()
if __name__ == '__main__':
main()
Finally the error is showing like this:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-20-1d26c0f7e0b0&gt; in <module&gt;
62
63 if __name__ == '__main__':
---&gt; 64     main()
3 frames
/usr/local/lib/python3.7/dist-packages/detectron2/modeling/meta_arch/build.py in build_model(cfg)
19     Note that it does not load any weights from ``cfg``.
20     &quot;&quot;&quot;
---&gt; 21     meta_arch = cfg.MODEL.META_ARCHITECTURE
22     model = META_ARCH_REGISTRY.get(meta_arch)(cfg)
23     model.to(torch.device(cfg.MODEL.DEVICE))
AttributeError: 'str' object has no attribute 'META_ARCHITECTURE'
I don't understand class about the error and what could be the solution? And What should I do now? What about &quot;META_ARCHITECTURE&quot;. Please consider it......
Advance thanks.....



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:08/29/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python-3.x><pytorch><object-detection><detectron>', 'Title': 'AttributeError: \'str\' object has no attribute \'META_ARCHITECTURE\' in "Detectron2"', 'ViewCount': 13, 'id': 73524364, 'user_AboutMe': '<p>Nick name Mahfuz. Working with python 3, related technologies. Working in ICE Innovation Lab at Islamic University, Bangladesh. Involve in IEEE volunteer organization.</p>\n', 'user_AccountId': 18009414.0, 'user_DisplayName': 'Fuzi', 'user_Reputation': 531.0, 'user_Views': 8.0, 'user_id': 13089462.0}}