You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried to reproduce the Code from the Tutorial and the Training ran through and produced an Output, but when I tried to predict with the weights from the training, I got only results when I lowered the confidence score to 0.05, which are basically no predictions.
Instructions To Reproduce the Issue:
from detectron2.structures import BoxMode
from detectron2.data import DatasetCatalog, MetadataCatalog
import os
import json
#import cv2
import numpy as np
import random
#from detectron2.utils.visualizer import Visualizer
import matplotlib.pyplot as plt
import torch
from detectron2.engine import DefaultTrainer
from detectron2.config import get_cfg
from detectron2 import model_zoo
def get_balloon_dicts(img_dir):
json_file = os.path.join(img_dir, "via_region_data.json")
with open(json_file) as f:
imgs_anns = json.load(f)
dataset_dicts = []
for idx, v in enumerate(imgs_anns.values()):
record = {}
filename = os.path.join(img_dir, v["filename"])
height, width = plt.imread(filename).shape[:2]
record["file_name"] = filename
record["image_id"] = idx
record["height"] = height
record["width"] = width
annos = v["regions"]
objs = []
for _, anno in annos.items():
assert not anno["region_attributes"]
anno = anno["shape_attributes"]
px = anno["all_points_x"]
py = anno["all_points_y"]
poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]
poly = [p for x in poly for p in x]
obj = {
"bbox": [np.min(px), np.min(py), np.max(px), np.max(py)],
"bbox_mode": BoxMode.XYXY_ABS,
"segmentation": [poly],
"category_id": 0,
}
objs.append(obj)
record["annotations"] = objs
dataset_dicts.append(record)
return dataset_dicts
if __name__ == "__main__":
data_dir = '/Users/moritz/Downloads/balloon/'
for d in ["train", "val"]:
DatasetCatalog.register("balloon_" + d, lambda d=d: get_balloon_dicts(data_dir + d))
MetadataCatalog.get("balloon_" + d).set(thing_classes=["balloon"])
balloon_metadata = MetadataCatalog.get("balloon_train")
#dataset_dicts = get_balloon_dicts("/Users/moritz/Downloads/balloon/train")
# for d in random.sample(dataset_dicts, 3):
# img = cv2.imread(d["file_name"])
# visualizer = Visualizer(img[:, :, ::-1], metadata=balloon_metadata, scale=0.5)
# out = visualizer.draw_dataset_dict(d)
# plt.imshow( out.get_image()[:, :, ::-1])
# plt.show()
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.DATASETS.TRAIN = ("balloon_train",)
cfg.MODEL.DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
cfg.DATASETS.TEST = ()
cfg.DATALOADER.NUM_WORKERS = 2
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml") # Let training initialize from model zoo
cfg.SOLVER.IMS_PER_BATCH = 2 # This is the real "batch size" commonly known to deep learning people
cfg.SOLVER.BASE_LR = 0.00025 # pick a good LR
cfg.SOLVER.MAX_ITER = 300 # 300 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 = 128 # The "RoIHead batch size". 128 is 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)
cfg.OUTPUT_DIR = './output'
# 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)
trainer = DefaultTrainer(cfg)
trainer.resume_or_load(resume=False)
trainer.train()
### I ran the training on a gpu cluster first and put the input into the script below
from detectron2.config import get_cfg
import os
from detectron2.engine import DefaultPredictor
from detectron2 import model_zoo
import train
import random
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
import cv2
import matplotlib.pyplot as plt
from detectron2.utils.visualizer import ColorMode
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.WEIGHTS = '/Users/moritz/model_0000299.pth' # path to the model we just trained
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set a custom testing threshold
cfg.MODEL.DEVICE = 'cpu'
predictor = DefaultPredictor(cfg)
dataset_dicts = train.get_balloon_dicts("/Users/moritz/Downloads/balloon/val")
balloon_metadata = MetadataCatalog.get("balloon_train")
for d in random.sample(dataset_dicts, 5):
im = cv2.imread(d["file_name"])
outputs = predictor(im) # format is documented at https://detectron2.readthedocs.io/tutorials/models.html#model-output-format
v = Visualizer(im[:, :, ::-1],
metadata=balloon_metadata,
scale=0.5,
instance_mode=ColorMode.IMAGE_BW # remove the colors of unsegmented pixels. This option is only available for segmentation models
)
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
plt.imshow(out.get_image()[:, :, ::-1])
plt.show()
print(outputs)
Expected behavior:
Since the code is an exact copy of the tutorial code I was expecting the predictions to be working. But I only get predictions, if I lower the confidence score threshold to 0.05 which are no real predictions. I do not really understand, why the code is not working. I also tried with saving the weights every 10 iterations and trying different weights, but the result, was more or less the same.
Environment:
I don't think the error is due to an installation or version error because, the code is running on the gpu and on cpu and locally on the cpu and produces no error messages in all cases.
Paste the output of the following command:
my environment on the laptop:
I tried to reproduce the Code from the Tutorial and the Training ran through and produced an Output, but when I tried to predict with the weights from the training, I got only results when I lowered the confidence score to 0.05, which are basically no predictions.
Instructions To Reproduce the Issue:
Expected behavior:
Since the code is an exact copy of the tutorial code I was expecting the predictions to be working. But I only get predictions, if I lower the confidence score threshold to 0.05 which are no real predictions. I do not really understand, why the code is not working. I also tried with saving the weights every 10 iterations and trying different weights, but the result, was more or less the same.
Environment:
I don't think the error is due to an installation or version error because, the code is running on the gpu and on cpu and locally on the cpu and produces no error messages in all cases.
Paste the output of the following command:
my environment on the laptop:
If your issue looks like an installation issue / environment issue,
please first check common issues in https://detectron2.readthedocs.io/tutorials/install.html#common-installation-issues
The text was updated successfully, but these errors were encountered: