Hello
Before I write the question I'd like to thank you for helping me understand and solve the issue.
Here is the problem;
I have a python code to detect any object
(which most of them don't have pre trained models) on an image regardless of their type.
(this task itself is my objective, I haven't found any source code that does it)
Here is my code:
import torch, torchvision
print(torch.__version__, torch.cuda.is_available())
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
import numpy as np
import cv2
import random
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
from PIL import Image
im = Image.open("input.jpg")
im.show()
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
predictor = DefaultPredictor(cfg)
outputs = predictor(im)
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
im = v.get_image()[:, :, ::-1]
im.show()
the line below keeps prompting errors;
outputs = predictor(im)
the error is this;
File "/home/reza/Code/test1/test1.py", line 28, in <module>
outputs = predictor(im)
File "/home/reza/Code/test1/detectron2/detectron2/engine/defaults.py", line 320, in __call__
image = self.aug.get_transform(original_image).apply_image(original_image)
File "/home/reza/Code/test1/detectron2/detectron2/data/transforms/transform.py", line 113, in apply_image
assert img.shape[:2] == (self.h, self.w)
File "/home/reza/Code/test1/venv/lib/python3.10/site-packages/PIL/Image.py", line 529, in __getattr__
raise AttributeError(name)
AttributeError: shape. Did you mean: 'save'?
What I have tried:
I solved this issue by replacing all
image.shape
calls with
image.size
calls as it returns the height and weight of the image.
It keeps prompting similar errors.
the next error is
image = self.aug.get_transform(original_image).apply_image(original_image)
File "/home/reza/Code/test1/detectron2/detectron2/data/transforms/transform.py", line 117, in apply_image
if img.dtype == np.uint8:
saying
the object doesn't have an img.dtype
it seems like that these error are not going to end.
the last attribute error I encountered was the
strides attribute. which I cannot replace this attribute.
I'm assuming the problem is some place else and I shouldn't keep editing the detectron source code.