bugGNU Octave - Bugs: bug #46774, image package: im2col produces...

 
 

bug #46774: image package: im2col produces different results compared with Matlab

Submitter:  Luis Mendo <lmendo>
Submitted:  Tue 29 Dec 2015 12:24:49 PM UTC
   
 
Category:  Octave Package Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Luis Mendo Open/Closed:  * Closed
Release:  * other Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Wed 10 Feb 2016 08:41:59 PM UTC, comment #3: 

Thanks for your feedback, Carnë.

I found these bugs because I was deliberately testing corner cases. I'm designing a code-golf language based on Matlab (see this link if curious: https://esolangs.org/wiki/MATL). The compiler (or rather transpiler) produces Matlab code, meant to be compatible with Octave as well. When I introduce a function (in this case, the equivalent to `im2col`), I try to test corner cases to see if it works well, and specially to detect different behaviour when running the compiled code on Matlab and on Octave.

Yes, it seems Matlab collapses all dimensions beyond the first. I'm not sure that can be considered a bug, though.

Luis Mendo <lmendo>
Sun 07 Feb 2016 11:11:28 PM UTC, comment #2: 

Just curious, did you found these bug by accident or do you have code that relies on these Matlab corner cases?

In short, I fixed issue (2) for Matlab compatibility. However, I will not change (1) and (3) since they are required to support images of N dimensions. Either Matlab does not support images with higher number of dimensions or there's a bug on their code. If you have an account with Mathworks, could you fill a bug report with them?

== (1) Multidimensional arrays. ==

Octave's im2col handles N dimensional input. Matlab does not throw an error but its output is obviously incorrect. I'm guessing that either Matlab's im2col is not meant to use with arrays with more than 2 dimensions, or it has a bug.

Can you please fill a bug report against Matlab? I have seen this happening before, so my guess is that somewhere in their code they do "[rows, cols] = size (image)" and then actually use it, forgetting that columns becomes the cumulative product of all other dimensions.

== (2) Block size exceeds input array size ==

The fix for this was just removing that input check and to automatically becomes Matlab compatible. Unfortunately, there is a bug in Octave which prevents automatic broadcasting with dimensions of length zero https://savannah.gnu.org/bugs/?47085 so I had to regress back to use bsxfun.  Anyway, It is fixed here

http://hg.code.sf.net/p/octave/image/rev/0fc684589226


== (3) Block size exceeds input array size in dimension greater than 2 with 'distinct' option ==

If it is not possible to make complete blocks of the image, then it is treated as if padded with zeros. This seems to be the same for Matlab. I don't know why Matlab does not do the same for the other dimensions but considering the other bugs you're reporting, I'm guessing it's really not coded to support more dimensions.

Note that Octave does support more dimensions which Matlab does not at all and fixing this would impact on that.


x = repmat (magic (3), [1 1 2])
im2col (x, [1 2 2], "distinct")
im2col (x, [1 2 3], "distinct")


Carnë Draug <carandraug>
Group Member
Thu 14 Jan 2016 03:15:29 PM UTC, comment #1: 

I have attached a function that intercepts `im2col` to reproduce Matlab's behaviour

Luis Mendo <lmendo>
Tue 29 Dec 2015 12:24:49 PM UTC, original submission:  

Some of these are admittedly corner cases, but the output is different.

(1) Multidimensional arrays. Example code:


x = cat(3, magic(3), -magic(3));
im2col(x, [1 2])


Octave output:

ans =
   8   3   4   1   5   9  -8  -3  -4  -1  -5  -9
   1   5   9   6   7   2  -1  -5  -9  -6  -7  -2


Matlab output:

ans =
     8     3     4     1     5     9     6     7     2    -8    -3    -4    -1    -5    -9
     1     5     9     6     7     2    -8    -3    -4    -1    -5    -9    -6    -7    -2


My interpretation is that Matlab collapses all dimensions beyond the first. Specifically, this reproduces Matlab's output in Octave (and in Matlab):


im2col(reshape(x, size(x,1), []), [1 2])



(2) Block size exceeds input array size. Example code:


x = 1:8;
im2col(x, [2 1])


Octave output: gives error:


error: im2col: no dimension of A can be greater than BLOCK_SIZE in sliding


Matlab output:


ans =
   Empty matrix: 2-by-0



(3) Block size exceeds input array size in dimension greater than 2 with 'distinct' option. Example code:


x = magic(3);
im2col(x, [1 2 4], 'distinct')


Octave output:


ans =

   8   3   4   6   7   2
   1   5   9   0   0   0
   0   0   0   0   0   0
   0   0   0   0   0   0
   0   0   0   0   0   0
   0   0   0   0   0   0
   0   0   0   0   0   0
   0   0   0   0   0   0


Matlab output: gives error:


In an assignment  A(:) = B, the number of elements in A and B must be the same.
Error in im2col (line 70)
            x(:) = aa(i*block(1)+rows,j*block(2)+cols);


Luis Mendo <lmendo>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #36030:  im2col_comp.m added by lmendo (207B - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by lmendo (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-02-07 carandraug StatusNone Fixed
        Open/ClosedOpen Closed
        Release4.0.0 other
        Operating SystemMicrosoft Windows Any
        Summaryim2col produces different results compared with Matlab image package: im2col produces different results compared with Matlab
    2016-01-14 lmendo Attached File- Added im2col_comp.m, #36030

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code