在烤宽面条中通过数据扩充聚合预测
Aggregate predictions with data augmentation in lasagne
我正在研究 MNIST 数据集并使用数据增强来训练神经网络。我有一个 BatchIterator,它从每张图片中随机提取一个 24、24 子图像,并将其用作 NN 的输入。
就训练而言,一切顺利。但是对于预测,我想从给定图像中提取 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()
的测试部分修复它吗?
查看 code for nolearn.lasagne.BatchIterator
and how it is used by the nolearn.lasagne.NeuralNet
class, it looks like BatchIterator
s need to work when y
is not provided, i.e. in prediction mode. Note the call at line 520,其中提供了 X
,但没有为 y
提供任何值,因此它默认为 None
。
您的 CropIterator
当前假定 yb
始终是非 None
值。我不知道在未提供 yb
时做任何有用的事情是否有意义,但我假设您可以将 Xb
和 return None
转换为 y_new
如果 yb
是 None
.
我正在研究 MNIST 数据集并使用数据增强来训练神经网络。我有一个 BatchIterator,它从每张图片中随机提取一个 24、24 子图像,并将其用作 NN 的输入。
就训练而言,一切顺利。但是对于预测,我想从给定图像中提取 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()
的测试部分修复它吗?
查看 code for nolearn.lasagne.BatchIterator
and how it is used by the nolearn.lasagne.NeuralNet
class, it looks like BatchIterator
s need to work when y
is not provided, i.e. in prediction mode. Note the call at line 520,其中提供了 X
,但没有为 y
提供任何值,因此它默认为 None
。
您的 CropIterator
当前假定 yb
始终是非 None
值。我不知道在未提供 yb
时做任何有用的事情是否有意义,但我假设您可以将 Xb
和 return None
转换为 y_new
如果 yb
是 None
.