What's the reason of the error ValueError: Expected more than 1 value per channel?
What's the reason of the error ValueError: Expected more than 1 value per channel?
github repository of fast.ai
(因为代码提升了构建在 PyTorch 之上的库)
Please scroll the discussion a bit
我正在 运行 编写以下代码,但在尝试将数据传递给 predict_array 函数时出现错误
当我试图用它直接预测单个图像时,代码失败了,但是当同一图像位于 test
文件夹中时,它 运行 完美
from fastai.conv_learner import *
from planet import f2
PATH = 'data/shopstyle/'
metrics=[f2]
f_model = resnet34
def get_data(sz):
tfms = tfms_from_model(f_model, sz, aug_tfms=transforms_side_on, max_zoom=1.05)
return ImageClassifierData.from_csv(PATH, 'train', label_csv, tfms=tfms, suffix='.jpg', val_idxs=val_idxs, test_name='test')
def print_list(list_or_iterator):
return "[" + ", ".join( str(x) for x in list_or_iterator) + "]"
label_csv = f'{PATH}prod_train.csv'
n = len(list(open(label_csv)))-1
val_idxs = get_cv_idxs(n)
sz = 64
data = get_data(sz)
print("Loading model...")
learn = ConvLearner.pretrained(f_model, data, metrics=metrics)
learn.load(f'{sz}')
#learn.load("tmp")
print("Predicting...")
learn.precompute=False
trn_tfms, val_tfrms = tfms_from_model(f_model, sz)
#im = val_tfrms(open_image(f'{PATH}valid/4500132.jpg'))
im = val_tfrms(np.array(PIL.Image.open(f'{PATH}valid/4500132.jpg')))
preds = learn.predict_array(im[None])
p=list(zip(data.classes, preds))
print("predictions = " + print_list(p))
这是我得到的回溯
Traceback (most recent call last):
File "predict.py", line 34, in <module>
preds = learn.predict_array(im[None])
File "/home/ubuntu/fastai/courses/dl1/fastai/learner.py", line 266, in predict_array
def predict_array(self, arr): return to_np(self.model(V(T(arr).cuda())))
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__
result = self.forward(*input, **kwargs)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/container.py", line 67, in forward
input = module(input)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__
result = self.forward(*input, **kwargs)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/batchnorm.py", line 37, in forward
self.training, self.momentum, self.eps)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/functional.py", line 1011, in batch_norm
raise ValueError('Expected more than 1 value per channel when training, got input size {}'.format(size))
ValueError: Expected more than 1 value per channel when training, got input size [1, 1024]
我尝试过的东西
np.expand_dims(IMG,axis=0) or image = image[..., np.newaxis]
尝试了不同的读图方式
img = cv2.imread(img_path)
img = cv2.resize(img, dsize = (200,200))
img = np.einsum('ijk->kij', img)
img = np.expand_dims(img, axis =0)
img = torch.from_numpy(img)
learn.model(Variable(img.float()).cuda())
顺便说一句,错误仍然存在
ValueError: Expected more than 1 value per channel when training, got input size [1, 1024]
在Google 搜索 中也找不到任何参考..
如果我们使用特征批量归一化,它将在大小为 1 的批次上失败。
作为批量归一化计算:
y = (x - mean(x)) / (std(x) + eps)
如果我们每批有一个样本,那么 mean(x) = x
,输出将完全为零(忽略偏差)。我们不能用它来学习...
要使用经过训练的模型,请调用 model.eval() 以禁用进一步训练。这会阻止 BatchNorm 层更新它们的均值和方差,并只允许输入一个样本。如果需要,使用 model.train() 恢复训练模式。
我今天遇到了同样的问题。我的批量大小是五个。看起来我的数据集是最后一批只有 1,所以它导致批归一化层出错。
将批量大小更改为确保最后一批不等于 1 的值解决了我的问题。在我的例子中,我从 5 更改为 6
github repository of fast.ai (因为代码提升了构建在 PyTorch 之上的库)
Please scroll the discussion a bit
我正在 运行 编写以下代码,但在尝试将数据传递给 predict_array 函数时出现错误
当我试图用它直接预测单个图像时,代码失败了,但是当同一图像位于 test
文件夹中时,它 运行 完美
from fastai.conv_learner import *
from planet import f2
PATH = 'data/shopstyle/'
metrics=[f2]
f_model = resnet34
def get_data(sz):
tfms = tfms_from_model(f_model, sz, aug_tfms=transforms_side_on, max_zoom=1.05)
return ImageClassifierData.from_csv(PATH, 'train', label_csv, tfms=tfms, suffix='.jpg', val_idxs=val_idxs, test_name='test')
def print_list(list_or_iterator):
return "[" + ", ".join( str(x) for x in list_or_iterator) + "]"
label_csv = f'{PATH}prod_train.csv'
n = len(list(open(label_csv)))-1
val_idxs = get_cv_idxs(n)
sz = 64
data = get_data(sz)
print("Loading model...")
learn = ConvLearner.pretrained(f_model, data, metrics=metrics)
learn.load(f'{sz}')
#learn.load("tmp")
print("Predicting...")
learn.precompute=False
trn_tfms, val_tfrms = tfms_from_model(f_model, sz)
#im = val_tfrms(open_image(f'{PATH}valid/4500132.jpg'))
im = val_tfrms(np.array(PIL.Image.open(f'{PATH}valid/4500132.jpg')))
preds = learn.predict_array(im[None])
p=list(zip(data.classes, preds))
print("predictions = " + print_list(p))
这是我得到的回溯
Traceback (most recent call last):
File "predict.py", line 34, in <module>
preds = learn.predict_array(im[None])
File "/home/ubuntu/fastai/courses/dl1/fastai/learner.py", line 266, in predict_array
def predict_array(self, arr): return to_np(self.model(V(T(arr).cuda())))
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__
result = self.forward(*input, **kwargs)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/container.py", line 67, in forward
input = module(input)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__
result = self.forward(*input, **kwargs)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/batchnorm.py", line 37, in forward
self.training, self.momentum, self.eps)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/functional.py", line 1011, in batch_norm
raise ValueError('Expected more than 1 value per channel when training, got input size {}'.format(size))
ValueError: Expected more than 1 value per channel when training, got input size [1, 1024]
我尝试过的东西
np.expand_dims(IMG,axis=0) or image = image[..., np.newaxis]
尝试了不同的读图方式
img = cv2.imread(img_path) img = cv2.resize(img, dsize = (200,200)) img = np.einsum('ijk->kij', img) img = np.expand_dims(img, axis =0) img = torch.from_numpy(img) learn.model(Variable(img.float()).cuda())
顺便说一句,错误仍然存在
ValueError: Expected more than 1 value per channel when training, got input size [1, 1024]
在Google 搜索 中也找不到任何参考..
如果我们使用特征批量归一化,它将在大小为 1 的批次上失败。
作为批量归一化计算:
y = (x - mean(x)) / (std(x) + eps)
如果我们每批有一个样本,那么 mean(x) = x
,输出将完全为零(忽略偏差)。我们不能用它来学习...
要使用经过训练的模型,请调用 model.eval() 以禁用进一步训练。这会阻止 BatchNorm 层更新它们的均值和方差,并只允许输入一个样本。如果需要,使用 model.train() 恢复训练模式。
我今天遇到了同样的问题。我的批量大小是五个。看起来我的数据集是最后一批只有 1,所以它导致批归一化层出错。 将批量大小更改为确保最后一批不等于 1 的值解决了我的问题。在我的例子中,我从 5 更改为 6