我正在研究MNIST数据集,并使用数据增强来训练神经网络。我有一个BatchIterator,它从每张图片中随机提取24,24个子图像,并将其用作神经网络的输入。
就训练而言,一切都很顺利。但对于预测,我想从给定的图像中提取5个子图像,并平均预测,但我不能让它工作:
这是我的BatchIterator:
class CropIterator(BatchIterator):
def __init__(self, batch_size, crop=4, testing=False):
super(CropIterator, self).__init__(batch_size)
self.testing = testing
self.crop = crop
def transform(self, Xb, yb):
crop = self.crop
batch_size, channels, width, height = Xb.shape
if not self.testing:
y_new = yb
X_new = np.zeros([batch_size, channels, width - crop, height - crop]).astype(np.float32)
for i in range(batch_size):
x = np.random.randint(0, crop+1)
y = np.random.randint(0, crop+1)
X_new[i] = Xb[i, :, x:x+width-crop, y:y+height-crop]
else:
X_new = np.zeros([5 * batch_size, channels, width - crop, height - crop]).astype(np.float32)
y_new = np.zeros(5 * batch_size).astype(np.int32)
for i in range(batch_size):
for idx, position in enumerate([(0,0), (0, crop), (crop, 0), (crop, crop), (crop//2, crop//2)]):
# all extreme cropppings + the middle one
x_idx = position[0]
y_idx = position[1]
X_new[5*i+idx, :] = Xb[i, :, x_idx:x_idx+width-crop, y_idx:y_idx+height-crop]
y_new[5*i+idx] = yb[i]
return X_new, y_new
拟合我的网络训练数据工作,但当我做net.predict(X_test)
时,我得到一个错误,因为CropIterator.transform()
是,我相信,yb
等于None
。
下面是完整的调用栈:
/usr/local/lib/python2.7/site-packages/nolearn/lasagne/base.pyc in predict(self, X)
526 return self.predict_proba(X)
527 else:
--> 528 y_pred = np.argmax(self.predict_proba(X), axis=1)
529 if self.use_label_encoder:
530 y_pred = self.enc_.inverse_transform(y_pred)
/usr/local/lib/python2.7/site-packages/nolearn/lasagne/base.pyc in predict_proba(self, X)
518 def predict_proba(self, X):
519 probas = []
--> 520 for Xb, yb in self.batch_iterator_test(X):
521 probas.append(self.apply_batch_func(self.predict_iter_, Xb))
522 return np.vstack(probas)
/usr/local/lib/python2.7/site-packages/nolearn/lasagne/base.pyc in __iter__(self)
78 else:
79 yb = None
---> 80 yield self.transform(Xb, yb)
81
82 @property
<ipython-input-56-59463a9f9924> in transform(self, Xb, yb)
33 y_idx = position[1]
34 X_new[5*i+idx, :] = Xb[i, :, x_idx:x_idx+width-crop, y_idx:y_idx+height-crop]
---> 35 y_new[5*i+idx] = yb[i]
36 return X_new, y_new
37
TypeError: 'NoneType' object has no attribute '__getitem__'
关于如何在CropIterator.transform()
的测试部分修复它的任何想法?
查看nolearn.lasagne.BatchIterator
的代码以及nolearn.lasagne.NeuralNet
类如何使用它,看起来BatchIterator
s需要在不提供y
时工作,即在预测模式下。请注意520行的调用,其中提供了X
,但没有给出y
的值,因此它默认为None
。
你的CropIterator
目前假设yb
总是一个非None
值。我不知道当yb
不提供时做任何有用的事情是否有意义,但我假设您可以转换Xb
并返回None
为y_new
,如果yb
是None
。