Advanced 2d indexing in Theano to extract multiple pixels from an image

你说的曾经没有我的故事 提交于 2019-12-07 09:59:50

问题


Background

I have an image that I want to sample at a number (P) of x,y coordinates.

In Numpy I can use advanced indexing to do this via:

 n_points = n_image[ [n_pos[:,1],n_pos[:,0]] ]

This returns a vector of P pixels sampled from the image.

Question

How can I do this advanced indexing in Theano?

What I've tried

I've tried the corresponding code in theano:

t_points = t_image[ [t_pos[:,1],t_pos[:,0]] ]

this compiles and executes without any warning messages, but results in an output tensor of shape (2,8,100), so it looks like it is doing some variant of basic indexing returning lots of rows of the image, instead of extracting pixels.

Full code

import numpy as np
import theano.tensor as T
from theano import function, shared
import theano

P = 8 # Number of points to sample
n_image = np.zeros((100,100), dtype=np.int16)  # 100*100 image
n_pos = np.zeros( (P,2) , dtype=np.int32) # Coordinates within the image

# NUMPY Method
n_points = n_image[ [n_pos[:,1],n_pos[:,0]] ]

# THEANO method
t_pos = T.imatrix('t_pos')
t_image = shared( n_image )
t_points = t_image[ [t_pos[:,1],t_pos[:,0]] ]
my_fun = function( [t_pos], t_points)
t_points = my_fun(n_pos)

print n_points.shape
print t_points.shape

This prints (8,) for Numpy, and (2,8,100) for Theano.

My Theano version is 0.7.0.dev-RELEASE


回答1:


This appears to be a subtle difference between Theano and numpy advanced indexing (there are other differences that don't apply here).

Instead of

t_points = t_image[ [t_pos[:,1],t_pos[:,0]] ]

you need to use

t_points = t_image[ (t_pos[:,1],t_pos[:,0]) ]

Note the change from a list of lists to a tuple of lists. This variant also works in numpy so it's probably best to just use a tuple instead of a list there too.



来源:https://stackoverflow.com/questions/32226362/advanced-2d-indexing-in-theano-to-extract-multiple-pixels-from-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!