Machine learning 规范化传递给torch.transforms.Compose函数的图像
如何找到传递给转换的值。在PyTorch中规范化函数?另外,在我的代码中,我应该在哪里进行转换呢 由于规范化数据集是一项众所周知的任务,我希望应该有某种脚本自动完成这项任务。至少我在PyTorch论坛上找不到它Machine learning 规范化传递给torch.transforms.Compose函数的图像,machine-learning,deep-learning,computer-vision,pytorch,normalize,Machine Learning,Deep Learning,Computer Vision,Pytorch,Normalize,如何找到传递给转换的值。在PyTorch中规范化函数?另外,在我的代码中,我应该在哪里进行转换呢 由于规范化数据集是一项众所周知的任务,我希望应该有某种脚本自动完成这项任务。至少我在PyTorch论坛上找不到它 transformed_dataset=MothLandmarksDataset(csv_file='moth_gt.csv', 根目录_dir='', transform=transforms.Compose([ 重新缩放(256), 随机作物(224), 归一化(平均值=[0.485
transformed_dataset=MothLandmarksDataset(csv_file='moth_gt.csv',
根目录_dir='',
transform=transforms.Compose([
重新缩放(256),
随机作物(224),
归一化(平均值=[0.485,0.456,0.406],
标准=[0.229,0.224,0.225]),
ToTensor()
]))
对于范围内的i(len(转换的_数据集)):
样本=转换的_数据集[i]
打印(i,样本['image'].size(),样本['landmarks'].size())
如果i==3:
打破
我知道这些当前值不属于我的数据集,而是属于ImageNet,但使用它们,我实际上得到了一个错误:
TypeError回溯(最近一次调用)
在里面
10
11表示范围内的i(len(转换的_数据集)):
--->12样本=转换的_数据集[i]
13
14打印(i,样本['image'].size(),样本['landmarks'].size())
in _uGetItem_uuu(self,idx)
30
31如果自我转换:
--->32样本=自转换(样本)
33
34返回样本
调用中的~/anaconda3/lib/python3.7/site-packages/torchvision/transforms/transforms.py(self,img)
59定义呼叫(自我,img):
60表示在self.transforms中的t:
--->61 img=t(img)
62返回img
63
调用中的~/anaconda3/lib/python3.7/site-packages/torchvision/transforms/transforms.py(self,tensor)
210张量:归一化张量图像。
211 """
-->212返回F归一化(张量、自平均值、自标准值、自在位)
213
214定义报告(自我):
归一化中的~/anaconda3/lib/python3.7/site-packages/torchvision/transforms/functional.py(张量、平均值、标准值、就地)
278 """
279如果不是火炬,是张量(张量):
-->280 raise TypeError('tensor应该是torch tensor.Got{}.'.format(type(tensor)))
281
282如果张量.ndimension()!=三:
类型错误:张量应该是火炬张量。得到了。
基本上有三个问题:
mean=0。
标准=0。
nb_样本=0。
对于dataloader中的数据:
打印(类型(数据))
批次样本=数据大小(0)
data.shape(0)
数据=数据.视图(批处理样本,数据.大小(1),-1)
平均值+=数据。平均值(2)。总和(0)
标准+=数据标准(2)和(0)
nb_样品+=批量_样品
平均值/=nb_样本
std/=铌铀样品
错误是:
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在里面
5对于dataloader中的数据:
6打印(类型(数据))
---->7批样品=数据大小(0)
8.
9.数据形状(0)
AttributeError:“dict”对象没有属性“size”
这是打印(数据)结果:
{'image]:张量([[0.2961,0.2941,0.2941,…,0.2460,0.2456,0.2431],
[0.2953, 0.2977, 0.2980, ..., 0.2442, 0.2431, 0.2431],
[0.2941, 0.2941, 0.2980, ..., 0.2471, 0.2471, 0.2448],
...,
[0.3216, 0.3216, 0.3216, ..., 0.2482, 0.2471, 0.2471],
[0.3216, 0.3241, 0.3253, ..., 0.2471, 0.2471, 0.2450],
[0.3216, 0.3216, 0.3216, ..., 0.2471, 0.2452, 0.2431]],
[[0.2961, 0.2941, 0.2941, ..., 0.2460, 0.2456, 0.2431],
[0.2953, 0.2977, 0.2980, ..., 0.2442, 0.2431, 0.2431],
[0.2941, 0.2941, 0.2980, ..., 0.2471, 0.2471, 0.2448],
...,
[0.3216, 0.3216, 0.3216, ..., 0.2482, 0.2471, 0.2471],
[0.3216, 0.3241, 0.3253, ..., 0.2471, 0.2471, 0.2450],
[0.3216, 0.3216, 0.3216, ..., 0.2471, 0.2452, 0.2431]],
[[0.2961, 0.2941, 0.2941, ..., 0.2460, 0.2456, 0.2431],
[0.2953, 0.2977, 0.2980, ..., 0.2442, 0.2431, 0.2431],
[0.2941, 0.2941, 0.2980, ..., 0.2471, 0.2471, 0.2448],
...,
[0.3216, 0.3216, 0.3216, ..., 0.2482, 0.2471, 0.2471],
[0.3216, 0.3241, 0.3253, ..., 0.2471, 0.2471, 0.2450],
[0.3216, 0.3216, 0.3216, ..., 0.2471, 0.2452, 0.2431]]],
[[[0.3059, 0.3093, 0.3140, ..., 0.3373, 0.3363, 0.3345],
[0.3059, 0.3093, 0.3165, ..., 0.3412, 0.3389, 0.3373],
[0.3098, 0.3131, 0.3176, ..., 0.3450, 0.3412, 0.3412],
...,
[0.2931, 0.2966, 0.2931, ..., 0.2549, 0.2539, 0.2510],
[0.2902, 0.2902, 0.2902, ..., 0.2510, 0.2510, 0.2502],
[0.2864, 0.2900, 0.2863, ..., 0.2510, 0.2510, 0.2510]],
[[0.3059, 0.3093, 0.3140, ..., 0.3373, 0.3363, 0.3345],
[0.3059, 0.3093, 0.3165, ..., 0.3412, 0.3389, 0.3373],
[0.3098, 0.3131, 0.3176, ..., 0.3450, 0.3412, 0.3412],
...,
[0.2931, 0.2966, 0.2931, ..., 0.2549, 0.2539, 0.2510],
[0.2902, 0.2902, 0.2902, ..., 0.2
sample = {'image': image, 'landmarks': landmarks}
if self.transform:
sample = self.transform(sample)
mean = 0.0
std = 0.0
nb_samples = 0.0
for data in dataloader:
images, landmarks = data["image"], data["landmarks"]
batch_samples = images.size(0)
images_data = images.view(batch_samples, images.size(1), -1)
mean += images_data.mean(2).sum(0)
std += images_data.std(2).sum(0)
nb_samples += batch_samples
mean /= nb_samples
std /= nb_samples