[英]Pytorch/torchvision - How to increase limit of detectable objects
I am new to Pytorch and so far it has been incredible.我是 Pytorch 的新手,到目前为止它一直令人难以置信。 I am using it to count the number of pills in an image.
我正在使用它来计算图像中的药丸数量。 I have found that in the majority of my images the max number of objects that it detects is 100. For the picture below it reaches a max count of 100 with the confidence around.6.
我发现在我的大多数图像中,它检测到的最大对象数为 100。对于下面的图片,它的最大计数为 100,置信度约为 6。 After that it doesn't increase anymore even down to.1 confidence.
之后,它不再增加,甚至降至.1 信心。 I haven't been able to find anything in the docs or any other places online.
我无法在文档或在线任何其他地方找到任何内容。 I am using the fasterrcnn_resnet50_fpn model.
我正在使用fasterrcnn_resnet50_fpn model。 Below is the code that load the trained model and evaluate the image.
下面是加载经过训练的 model 并评估图像的代码。 Any tips or even different packages that would be able to count all objects would be super helpful.
任何能够计算所有对象的提示甚至不同的软件包都会非常有用。
## Loading the trained module
loaded_model = get_model(num_classes = 2)
loaded_model.load_state_dict(torch.load('Pillcount/model'))
os.chdir('../pytorchobjdet/vision')
class CountDataset(torch.utils.data.Dataset):
def __init__(self, root, data_file, transforms=None):
self.root = root
self.transforms = transforms
self.imgs = sorted(os.listdir(os.path.join(root, "count")))
self.path_to_data_file = data_file
def __getitem__(self, idx):
# load images and bounding boxes
img_path = os.path.join(self.root, "count", self.imgs[idx])
img = Image.open(img_path).convert("RGB")
box_list = parse_one_annot(self.path_to_data_file,
self.imgs[idx])
boxes = torch.as_tensor(box_list, dtype=torch.float32)
num_objs = len(box_list)
# there is only one class
labels = torch.ones((num_objs,), dtype=torch.int64)
image_id = torch.tensor([idx])
area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:,
0])
# suppose all instances are not crowd
iscrowd = torch.zeros((num_objs,), dtype=torch.int64)
target = {}
target["boxes"] = boxes
target["labels"] = labels
target["image_id"] = image_id
target["area"] = area
target["iscrowd"] = iscrowd
if self.transforms is not None:
img, target = self.transforms(img, target)
return img, target
def __len__(self):
return len(self.imgs)
dataset_count = CountDataset(root='../../Pill_Object_Detection',
data_file = "../../Pill_Object_Detection/count_labels.csv",
transforms = get_transform(train=False))
idx = 1
img, _ = dataset_count[idx]
#put the model in evaluation mode
loaded_model.eval()
with torch.no_grad():
prediction = loaded_model([img])
image = Image.fromarray(img.mul(255).permute(1, 2,0).byte().numpy())
draw = ImageDraw.Draw(image)
# draw groundtruth
count = 0
for element in range(len(prediction[0]["boxes"])):
boxes = prediction[0]["boxes"][element].cpu().numpy()
score = np.round(prediction[0]["scores"][element].cpu().numpy(),
decimals= 4)
if score > 0.6:
draw.rectangle([(boxes[0], boxes[1]), (boxes[2], boxes[3])],
outline ="red", width =3)
draw.text((boxes[0], boxes[1]), text = str(score))
count +=1
print(f'count = {count}')
image
The advice from the comment above was very helpful.上面评论中的建议非常有帮助。 I used the YOLO5vs model and it did an incredible job.
我使用了 YOLO5vs model,它做得非常好。 This tutorial had a super easy set up that had you upload the annotated images into roboflow, and then it had some google colab tutorials set up for almost all of the current object detectors out there.
本教程有一个超级简单的设置,您可以将带注释的图像上传到 roboflow,然后它为几乎所有当前的 object 检测器设置了一些 google colab 教程。 Here is the result.
这是结果。 I just need to give better quality training data but it did extremely well for the few pictures that I gave it.
我只需要提供质量更好的训练数据,但对于我提供的几张图片,它做得非常好。 It can count well over 150 objects in the same image no problem.
它可以在同一张图像中计算超过 150 个对象,这没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.