pmneila/PyMaxflow

aexpansion_grid bugs?

Kuu6 opened this issue · 10 comments

Kuu6 commented

Hi @pmneila , I'm not sure if these are bugs or not, but I think yes.

In the written docummentation on the sourcecode of the function "aexpansion_grid" of the file "fastmin.py" is written that
"D must be an N+1-dimensional array with shape (L,S1,...,SN), where L is the number of labels considered."

but on the code, later on, at line 149 there is written:

num_labels = D.shape[-1]

Therefore I think D must be an array with shape (S1,..SN,L), with the L at the end. Also the C++ library seems to expect to get D in this way, as can be read in _maxflow.cpp line 1304:

D should be an\n N+1-dimensional array with shape (S_1,...,S_N,L).\n


Another topic to mention is the line 169 of "fastmin.py" , it is written:

for alpha in range(num_labels):

As num_labels is an integer, shouldn't it be like this?:

for alpha in range(0,num_labels):

Thanks for answering!
Kuu6

Hi.

You are absolutely right about the documentation. In the last version, the format of the D array of unary terms changed to hold the values for different labels of a pixel contiguous in memory. The documentation of the helper functions aexpansion_grid_step and abswap_grid_step changed accordingly, but the documentation of aexpansion_grid and abswap_grid didn't. Thank you for notice this and sorry for the inconvenience. I just made a new commit with the documentation fixed.

On the other hand, I do not understand your second issue. range(num_labels) and range(0, num_labels) are equivalent, since the "start" argument of range defaults to 0. The arguments of range are expected to be integers. Nevertheless, it is a good idea, but not necessary, to use xrange instead of range. I'll fix that for the next version.

Regards.

Kuu6 commented

Hi @pmneila,

Thanks for the clarification. About the second issue I'm still a newbie on python and I didn't know that you could use range with only one parameter.

Thanks again for answering!

Kuu6 commented

Hello again @pmneila ,

I'm sorry I'm bothering you again, but something is not working with the aexpansion_grid function.

I got this error:

File "C:\Python27\lib\site-packages\maxflow\fastmin.py", line 170, in aexpansion_grid
energy, _ = aexpansion_grid_step(alpha, D, V, labels)
TypeError: 'NoneType' object is not iterable

I've checked that D, V and labels are properly selected and are iterables types, what I get after executing this code before calling aexpansion_grid:

    print D.shape
    print initLabels.shape
    print V.shape

    print type(D)==type(V)
    print type(D)
    print type(V)
    print type(initLabels)

    labels = maxflow.fastmin.aexpansion_grid(D, V, 10, initLabels)

And the execution is:

D (406, 237, 3)
labels (406, 237)
V (3, 3)
True
<type 'numpy.ndarray'>
<type 'numpy.ndarray'>
<type 'numpy.ndarray'>

As you see, D, labels and V are of the same type, ndarray, and the dimensions match, labels is of the same dimension of the two first of D and V is an square matrix with the values for each label (in this case only 3 labels are considered). I even tried the function without indicating maxcycles and labels, but I got the same error.

The error message is not too descriptive but maybe you can find where the error is, let me know if you need more info about the parameters, D and V are just decimal numbers.

Thanks in advance for all your help!

@Kuu6,

Sorry for the delay.

I cannot reproduce the error. Could you tell me the data type of your arrays? You can access the data type with dtype::

print D.dtype
print V.dtype
Kuu6 commented

Thanks for answering @pmneila . The results of those calls are:

D  float64
V  float64

That's fine. I still cannot see the problem. Could you please send me (eg, via Dropbox) your D and V arrays in the npy format (with np.save)?

Kuu6 commented

I saved both matrix with np.save("D",D) and np.save("V",V), here is the link: https://dl.dropboxusercontent.com/u/19866030/matrix.zip

@Kuu6,

They work for me. I'm not receiving any error message. Maybe you changed something in the code?

In [1]: import maxflow.fastmin

In [2]: D = np.load("D.npy")

In [3]: V = np.load('V.npy')

In [4]: maxflow.fastmin.aexpansion_grid(D, V)
Cycle 0
Energy of the last cut (α=0): 37400.53824831328 (Improved!)
Energy of the last cut (α=1): 36786.469659640614 (Improved!)
Energy of the last cut (α=2): 36654.48617929603 (Improved!)
...more text...
Out[4]: 
array([[0, 0, 0, ..., 1, 1, 1],
       [0, 0, 0, ..., 1, 1, 1],
       [0, 0, 0, ..., 1, 1, 1],
       ..., 
       [0, 0, 0, ..., 1, 1, 1],
       [0, 0, 0, ..., 1, 1, 1],
       [0, 0, 0, ..., 1, 1, 1]], dtype=int8)

Try reseting all changes in your local working tree of the repository and reinstalling the library again:

$ cd path/to/PyMaxflow
$ git checkout . # Warning: this will remove all uncommitted changes you have made in PyMaxflow
$ python setup.py build
$ python setup.py install

And try running your code again…

Kuu6 commented

Ok @pmneila installing the master version again solved the problem :)

Thanks for all your help!!

That's great!

You're welcome.