[英]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.
在他们的合作版本中,我进行了以下更改以添加一些转换技术。
__getitem__
method of class 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.