Theano/Lasagne/Nolearn 神经网络图像输入

Theano/Lasagne/Nolearn Neural Network Image Input

我正在处理图像分类任务,并决定使用 Lasagne + Nolearn 作为神经网络原型。 所有标准示例,如 MNIST 数字分类 运行 很好,但是当我尝试使用自己的图像时出现问题。

我想使用 3 通道图像,而不是灰度图像。 还有我试图从图像中获取数组的代码:

 img = Image.open(item)
 img = ImageOps.fit(img, (256, 256), Image.ANTIALIAS)
 img = np.asarray(img, dtype = 'float64') / 255.
 img = img.transpose(2,0,1).reshape(3, 256, 256)   
 X.append(img)

这里是NN的代码及其拟合:

X, y = simple_load("new")

X = np.array(X)
y = np.array(y)


net1 = NeuralNet(
    layers=[  # three layers: one hidden layer
        ('input', layers.InputLayer),
        ('hidden', layers.DenseLayer),
        ('output', layers.DenseLayer),
        ],
    # layer parameters:
    input_shape=(None, 65536),  # 96x96 input pixels per batch
    hidden_num_units=100,  # number of units in hidden layer
    output_nonlinearity=None,  # output layer uses identity function
    output_num_units=len(y),  # 30 target values

    # optimization method:
    update=nesterov_momentum,
    update_learning_rate=0.01,
    update_momentum=0.9,

    regression=True,  # flag to indicate we're dealing with regression problem


       max_epochs=400,  # we want to train this many epochs
        verbose=1,
        )

  net1.fit(X, y)

我收到了这样的异常:

Traceback (most recent call last):
  File "las_mnist.py", line 39, in <module>
    net1.fit(X[i], y[i])
  File "/usr/local/lib/python2.7/dist-packages/nolearn/lasagne.py", line 266, in fit
    self.train_loop(X, y)
  File "/usr/local/lib/python2.7/dist-packages/nolearn/lasagne.py", line 273, in train_loop
    X, y, self.eval_size)
  File "/usr/local/lib/python2.7/dist-packages/nolearn/lasagne.py", line 377, in train_test_split
    kf = KFold(y.shape[0], round(1. / eval_size))
IndexError: tuple index out of range

那么,您 "feed" 您的网络使用哪种格式的图像数据? 感谢您的回答或任何提示!

我也在 lasagne-users 论坛上问过,Oliver Duerr 在代码示例方面帮助了我很多: https://groups.google.com/forum/#!topic/lasagne-users/8ZA7hr2wKfM

如果你正在做分类,你需要修改一些东西:

  1. 在您的代码中您设置了 regression = True。要进行分类,请删除此行。
  2. 如果要输入 3 个不同的通道,请确保您的输入形状与 X 的形状匹配
  3. 因为你正在进行分类,所以你需要输出使用 softmax 非线性(目前你的身份不会帮助你进行分类)

    X, y = simple_load("new")
    
    X = np.array(X)
    y = np.array(y)
    
    net1 = NeuralNet(
        layers=[  # three layers: one hidden layer
            ('input', layers.InputLayer),
            ('hidden', layers.DenseLayer),
            ('output', layers.DenseLayer),
            ],
        # layer parameters:
        input_shape=(None, 3, 256, 256),  # TODO: change this
        hidden_num_units=100,  # number of units in hidden layer
        output_nonlinearity=lasagne.nonlinearities.softmax, # TODO: change this
        output_num_units=len(y),  # 30 target values
    
        # optimization method:
        update=nesterov_momentum,
        update_learning_rate=0.01,
        update_momentum=0.9,
    
        max_epochs=400,  # we want to train this many epochs
        verbose=1,
    

    )