利用千层面的数据增加来汇总预测

时间:2015-08-20 09:00:17

标签: theano lasagne nolearn

我正在研究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()ybNone等于/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()

如何在private Integer somefunction(){ Integer order = count.compute(key, (String k, Integer v) -> { if (v == null) return 1; else { return v + 1; } }); return order-1; } 的测试部分修复它?

1 个答案:

答案 0 :(得分:1)

查看nolearn.lasagne.NeuralNet类的code for nolearn.lasagne.BatchIteratorhow it is usedBatchIterator在未提供y时需要工作,即预测模式。请注意提供X的{​​{3}}处的呼叫,但y没有给出值,因此默认为None

您的CropIterator目前假定yb始终为非None值。我不知道在没有提供yb的情况下执行任何有用的操作是否有意义但我认为您可以转换Xb并返回None y_new ybNone