1

我正在研究 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()

4

1 回答 1

1

查看代码nolearn.lasagne.BatchIterator以及类如何使用nolearn.lasagne.NeuralNet,看起来s 在未提供BatchIterator时需要工作,即在预测模式下。请注意第520 行y的调用where提供了但没有给出值,因此它默认为.XyNone

CropIterator当前假设yb始终是非None值。我不知道在没有提供时做任何有用的事情是否有意义,但yb我假设你可以转换Xb并返回if is Noney_newybNone

于 2015-08-20T10:00:05.627 回答