项目场景:
提示:这里是图像-关键点标签,制作数据集生成器时的一个bug
项目场景:
图像和标签数据制作数据集生成器:
数据集预处理过程中,需要改变图像的尺寸,因为标签数据是坐标群,需要和图像同步变化。这时候,有图像变化前后的比例来计算坐标变化后的结果。由于,采用的图像变换是torchvision封装的transforms中的transforms变换方法,返回的数据为torch.Tensor。返回的size为torch.Size
。
运行中,函数 __getitem__
报错:
def __getitem__(self, idx):
img_id = self._image_paths[idx]
img_id = os.path.join(self.root_dir, img_id)
image = Image.open(img_id).convert('RGB') # (宽,高,通道数)= (w, h, c)
imgSize = image.size # 原始图像宽高
landmarks = np.asfortranarray(self._labels[idx]) # (x, y, 显隐)=(宽,高,显隐性)
category = self._categories.index(self._cates[idx]) # 0,1,2,3,4
if self.transform_img:
image = self.transform_img(image) # 返回torch.Size([3, 256, 256])
else:
image.resize((256, 256)) # 使用resize
afterSize = image.shape[1:].numpy() # 缩放后图像的宽高
print(imgSize, afterSize)
bi = afterSize / imgSize
landmarks[:, 0:2] = landmarks[:, 0:2] * bi
return image, landmarks, category
报错内容:
File "E:/TanhaiyansCode/kpdem_with_pytorch/dataset/dataset_by_PIL.py", line 78, in __getitem__
afterSize = image.shape[1:].numpy() # 缩放后图像的宽高
AttributeError: 'torch.Size' object has no attribute 'numpy'
问题描述
提示:这里描述项目中遇到的问题:
例如:数据传输过程中数据不时出现丢失的情况,偶尔会丢失一部分数据
APP 中接收数据代码:
@Override
public void run() {
bytes = mmInStream.read(buffer);
mHandler.obtainMessage(READ_DATA, bytes, -1, buffer).sendToTarget();
}
原因分析:
定位错误代码:
afterSize = image.shape[1:].numpy() # 缩放后图像的宽高
错误码:AttributeError: 'torch.Size' object has no attribute 'numpy'
image是torch.Tensor类型, image.shape返回的是torch.Size,只有torch.Tensor有numpy()方法。所以需要把.numpy()放到image后面,才能用。
解决方案:
只有torch.Tensor有numpy()方法,就先转换为numpy数组,再取size。
代码修改:
afterSize = image.numpy().shape[1:] # 缩放后图像的宽高