pythontheanoconv-neural-networklasagnetensor

How to extract numpy arrays from Theano tensor?


We are working on an implementation of 3D Convolutional Neural Networks for segmentation of 3D medical images.

We have built a network with Lasagne and Theano, which successfully builds a 5D tensor. We want to extract actual 'images' as 3D numpy arrays from this tensor to see what the segmented maps actually look like.

We get the output like this:

prediction = lasagne.layers.get_output(layer)

Then define loss, updates, etc.

And define the theano function like this:

train_fn = theano.function([input_var, target_var], loss, updates=updates)

We then train a network in a for loop:

for epoch in range(10):
  loss = train_fn(train_data, train_seg)
  print("Epoch %d: Loss %g" % (epoch + 1, loss))

We have tried using the eval function like this:

print(eval('prediction[2]'))

which outputs:

Subtensor{int64}.0

But what we actually want to get are the actual outputs of the network (based on our inputs, they should be of size 24*160*160), so the output that the loss function takes to compare it with our test data. Can anyone help us?


Solution

  • Prediction is just a theano tensor. what you have to do is call it through theano function like how you did with the loss variable.

    ex.

    prediction = lasagne.layers.get_output(theano tensor)  
    f = theano.function([Theano tensor],prediction)
    X must be your data 
    maps = f(X)