Theano中的高级2d索引可从图像中提取多个像素 [英] Advanced 2d indexing in Theano to extract multiple pixels from an image

查看:60
本文介绍了Theano中的高级2d索引可从图像中提取多个像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在x,y坐标(P)处采样的图像.

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

在Numpy中,我可以通过以下方式使用高级索引:

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

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

这将返回从图像采样的P个像素的向量.

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

如何在Theano中进行高级索引编制?

How can I do this advanced indexing in Theano?

我已经在theano中尝试了相应的代码:

I've tried the corresponding code in theano:

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

此代码可以在不发出任何警告消息的情况下进行编译和执行,但会导致输出张量为(2,8,100),因此它看起来像是在做一些基本的索引编制工作,返回了大量的图像行,而不是提取像素

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.

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

这将为Numpy打印(8,),为Theano打印(2,8,100).

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

我的Theano版本是0.7.0.dev-RELEASE

My Theano version is 0.7.0.dev-RELEASE

推荐答案

这似乎是Theano和numpy高级索引之间的细微差别(这里没有其他差别).

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

代替

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

您需要使用

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

请注意从列表列表到列表元组的更改.此变体也可以在numpy中使用,因此最好只使用一个元组而不是那里的一个列表.

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.

这篇关于Theano中的高级2d索引可从图像中提取多个像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆