简体   繁体   English

如何在pytorch对象检测中添加转换

[英]how to add transformation in pytorch object detection

I'm new to PyTorch & going through the PyTorch object detection documentation tutorial pytorch docx .我是 PyTorch 的新手,正在浏览 PyTorch 对象检测文档教程pytorch docx At their collab version, I made the below changes to add some transformation techniques.在他们的合作版本中,我进行了以下更改以添加一些转换技术。

  1. First change to the __getitem__ method of class PennFudanDataset(torch.utils.data.Dataset)首先修改 PennFudanDataset(torch.utils.data.Dataset) 类的__getitem__方法
if self.transforms is not None:
   img = self.transforms(img)     
   target = T.ToTensor()(target)
   return img, target

In actual documentation it is 
if self.transforms is not None:
   img, target = self.transforms(img, target)  

Secondly, at the get_transform(train) function.其次,在get_transform(train)函数处。

def get_transform(train):
  if train:
    transformed = T.Compose([             
           T.ToTensor(),
           T.GaussianBlur(kernel_size=5, sigma=(0.1, 2.0)),
          T.ColorJitter(brightness=[0.1, 0.2], contrast=[0.1, 0.2], saturation=[0, 0.2], hue=[0,0.5])
    ])
    return transformed

  else:
    return T.ToTensor()

**In the documentation it is-** 
def get_transform(train):
    transforms = []
    transforms.append(T.ToTensor())
    if train:
        transforms.append(T.RandomHorizontalFlip(0.5))
    return T.Compose(transforms)

While implementing the code, I'm getting the below error.在实现代码时,我收到以下错误。 I'm not able to get what I',m doing wrong.我无法得到我做错了什么。

TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataset.py", line 272, in __getitem__
    return self.dataset[self.indices[idx]]
  File "<ipython-input-41-94e93ff7a132>", line 72, in __getitem__
    target = T.ToTensor()(target)
  File "/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py", line 104, in __call__
    return F.to_tensor(pic)
  File "/usr/local/lib/python3.6/dist-packages/torchvision/transforms/functional.py", line 64, in to_tensor
    raise TypeError('pic should be PIL Image or ndarray. Got {}'.format(type(pic)))
TypeError: pic should be PIL Image or ndarray. Got <class 'dict'>

I believe the Pytorch transforms only work on images (PIL images or np arrays in this case) and not labels (which are dicts according to the trace).我相信 Pytorch 转换仅适用于图像(在这种情况下为 PIL 图像或 np 数组),而不适用于标签(根据跟踪是字典)。 As such, I don't think you need to "tensorify" the labels as in this line target = T.ToTensor()(target) in the __getitem__ function.因此,我认为您不需要像在__getitem__函数中的这一行target = T.ToTensor()(target)那样“张量化”标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pytorch object 检测 model 优化 - Pytorch object detection model optimization PyTorch 用于 Object 检测 - 图像增强 - PyTorch for Object detection - Image augmentation 命令错误退出状态 1:-Pytorch 对象检测 - Command Errored Exit Status 1: - Pytorch Object Detection 对 pytorch 中的数据集应用转换并将它们添加到数据中 - Applying transformation to data set in pytorch and add them to the data 如何对 pytorch 情绪检测 model 进行预测 - How to make prediction on pytorch emotion detection model 使用网络摄像头 PyTorch 运行对象检测模型时出错 - Error when running object detection model with webcam PyTorch 使用多 GPU 和多线程、Pytorch 的对象检测推理 - Object Detection inference using multi-gpu & multi threading, Pytorch PyTorch - 为 object 检测训练不平衡数据集(设置权重) - PyTorch - Train imbalanced dataset (set weights) for object detection PyTorch:如何对多个图像应用相同的随机变换? - PyTorch : How to apply the same random transformation to multiple image? 如何从 faster-RCNN 计算 F1 分数和其他分类指标? (PyTorch 中的对象检测) - How can I calculate the F1-score and other classification metrics from a faster-RCNN? (object detection in PyTorch)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM