fossasia / visdom

A flexible tool for creating, organizing, and sharing visualizations of live, rich data. Supports Torch and Numpy.
Apache License 2.0
10.02k stars 1.14k forks source link

Maybe visdom.line() has a bug #755

Open MelonEater opened 4 years ago

MelonEater commented 4 years ago

Bug Description When I use visdom.line() to draw a pic, the code give me the information "X and Y should be the same shape". So I go to see the source code, to find where I get wrong with. After thinking and testing, I think there may exist a bug. The bug code is here:

if Y.ndim == 2 and Y.shape[1] == 1:
    Y = Y.reshape(1, Y.shape[0])
    X = X.reshape(X.shape[0])

if Y.ndim == 2 and X.ndim == 1:
    X = np.tile(X, (Y.shape[1], 1)).transpose()

    assert X.shape == Y.shape, 'X and Y should be the same shape'

So if I send the function line(X=x_input, Y=y_input), where x_input is an 'N' tensor or 'Nx1' tensor , and y_input is an 'Nx1' tensor, that is I just want to draw a line. When x_input and y_input go through following code,y_input will be an '1xN' tensor, and x_input will be an 'N' tensor.

if Y.ndim == 2 and Y.shape[1] == 1:
    Y = Y.reshape(1, Y.shape[0])
    X = X.reshape(X.shape[0])

Then they can go through following code, after this, X become an 'NxN' tensor, so it will cause assert above.

if Y.ndim == 2 and X.ndim == 1:
    X = np.tile(X, (Y.shape[1], 1)).transpose()

So I think the code should be:

if Y.ndim == 2 and Y.shape[1] == 1:
    Y = Y.reshape(Y.shape[0])        # I modify here
    X = X.reshape(X.shape[0])

if Y.ndim == 2 and X.ndim == 1:
    X = np.tile(X, (Y.shape[1], 1)).transpose()

    assert X.shape == Y.shape, 'X and Y should be the same shape'

Reproduction Steps

  1. We can reproduce using following code:
    x = np.array([[1], [2],[3]])   # x is 3x1, or use x=np.array([1,2,3]) where x is 3
    y = np.array([[6], [6], [6]])  # y is 3x1
    vis = visdom.Visdom(server="http://localhost", port=8097)
    vis.line(y, x)
  2. Then the code will return
    File "C:\Users\MelonEater\Miniconda3\envs\denoi\lib\site-packages\visdom\__init__.py", line 1694, in line
    assert X.shape == Y.shape, 'X and Y should be the same shape'

Expected behavior See bug description.

Screenshots image

adgilbert commented 4 years ago

I also saw this issue

zhangp14 commented 4 years ago

same problem encountered when running python main.py from https://github.com/valerystrizh/pytorch-histogram-loss

but use your modified code, pytorch-histogram-loss runs sucessfully.

`if Y.ndim == 2 and Y.shape[1] == 1: Y = Y.reshape(Y.shape[0]) # I modify here X = X.reshape(X.shape[0])

if Y.ndim == 2 and X.ndim == 1: X = np.tile(X, (Y.shape[1], 1)).transpose()

assert X.shape == Y.shape, 'X and Y should be the same shape'`
bnbqq8 commented 3 years ago

Same issue. Inputs: image

And return: image

ShirAmir commented 3 years ago

There is indeed a bug in visdom\__init__.py. When Y is two dimensional and the second dimension is 1 (when plotting multiple points of a single line) Y and X are reshaped for no reason and the run is eliminated when asserting X and Y are of the same shape.

The quickest fix is to eliminate the following code lines in the line function:

 if Y.ndim == 2 and Y.shape[1] == 1:
                Y = Y.reshape(1, Y.shape[0])
                X = X.reshape(X.shape[0])

On the other hand, add multiple lines to your plot and avoid this issue altogether.

Is it possible to submit a pull request to fix this?