我正在尝试计算火炬张量数组的均值和标准差。我的数据集包含720个训练图像,每个图像都有4个界标,其中X和Y代表图像上的2D点。
to_tensor = transforms.ToTensor()
landmarks_arr = []
for i in range(len(train_dataset)):
landmarks_arr.append(to_tensor(train_dataset[i]['landmarks']))
mean = torch.mean(torch.stack(landmarks_arr, dim=0))#, dim=(0, 2, 3))
std = torch.std(torch.stack(landmarks_arr, dim=0)) #, dim=(0, 2, 3))
print(mean.shape)
print("mean is {} and std is {}".format(mean, std))
结果:
torch.Size([])
mean is nan and std is nan
上面有几个问题:
我有:
len(landmarks_arr)
720
和
landmarks_arr[0].shape
torch.Size([1, 4, 2])
和
landmarks_arr[0]
tensor([[[502.2869, 240.4949],
[688.0000, 293.0000],
[346.0000, 317.0000],
[560.8283, 322.6830]]], dtype=torch.float64)
答案 0 :(得分:2)
将[0,255]范围内的PIL图像或numpy.ndarray(H xW x C)转换为a 如果为PIL图像,则形状为(C xH x W)的Torch.FloatTensor在[0.0,1.0]范围内 属于以下模式之一(L,LA,P,I,F,RGB,YCbCr,RGBA,CMYK,1)或 numpy.ndarray的dtype = np.uint8
在其他情况下,不按比例返回张量。
由于您的地标值不是PIL图像,并且不在[0,255]之内,因此不会应用缩放。
您可以尝试类似的
for i in range(len(train_dataset)):
landmarks = to_tensor(train_dataset[i]['landmarks'])
landmarks[landmarks != landmarks] = 0 # this will set all nan to zero
landmarks_arr.append(landmarks)
在您的循环内。或在循环中声明nan以找到罪魁祸首:
for i in range(len(train_dataset)):
landmarks = to_tensor(train_dataset[i]['landmarks'])
assert(not torch.isnan(landmarks).any()), f'nan encountered in sample {i}' # will trigger if a landmark contains nan
landmarks_arr.append(landmarks)