Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theano/Lasagne/Nolearn Neural Network Image Input

I am working on image classification tasks and decided to use Lasagne + Nolearn for neural networks prototype. All standard examples like MNIST numbers classification run well, but problems appear when I try to work with my own images.

I want to use 3-channel images, not grayscale. And there is the code where I'm trying to get arrays from images:

 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)

Here is the code of NN and its fitting:

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)

I recieve exceptions like this one:

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

So, in which format do you "feed" your networks with image data? Thanks for answers or any tips!

like image 463
Rachnog Avatar asked Apr 17 '15 18:04

Rachnog


2 Answers

If you're doing classification you need to modify a couple of things:

  1. In your code you have set regression = True. To do classification remove this line.
  2. Ensure that your input shape matches the shape of X if want to input 3 distinct channels
  3. Because you are doing classification you need the output to use a softmax nonlinearity (at the moment you have the identity which will not help you with classification)

    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,
    

    )

like image 93
unicorn_poet Avatar answered Oct 20 '22 21:10

unicorn_poet


I also asked it in lasagne-users forum and Oliver Duerr helped me a lot with code sample: https://groups.google.com/forum/#!topic/lasagne-users/8ZA7hr2wKfM

like image 30
Rachnog Avatar answered Oct 20 '22 23:10

Rachnog