awslabs / mxboard

Logging MXNet data for visualization in TensorBoard.
Apache License 2.0
325 stars 47 forks source link

A tiny bug in making sprite image for embedding projector #35

Closed YusongLeng closed 5 years ago

YusongLeng commented 5 years ago

Hi,

I just found a ting bug in how the current mxboard generating the sprite image for embedding projector. This bug may result in wrong embedding-thumbnail association. A simple example borrowed from here is shown as below.

import numpy as np
import mxnet as mx
from mxnet import gluon
from mxboard import SummaryWriter

batch_size = 65

def transformer(data, label):
    data = data.reshape((-1,)).astype(np.float32)/255
    return data, label

train_data = gluon.data.DataLoader(
    gluon.data.vision.MNIST('./data', train=True, transform=transformer),
    batch_size=batch_size, shuffle=False, last_batch='discard')

initialized = False
embedding = None
labels = None
images = None

for i, (data, label) in enumerate(train_data):
    if i >= 1:
        # only fetch the first batche of images, i.e. 65 images -> 8*9 sprite image
        break
    if initialized:
        embedding = mx.nd.concat(*(embedding, data), dim=0)
        labels = mx.nd.concat(*(labels, label), dim=0)
        images = mx.nd.concat(*(images, data.reshape(batch_size, 1, 28, 28)), dim=0)
    else:
        embedding = data
        labels = label
        images = data.reshape(batch_size, 1, 28, 28)
        initialized = True

with SummaryWriter(logdir='./logs') as sw:
    sw.add_embedding(tag='mnist', embedding=embedding, labels=labels, images=images)

The above code takes 65 images of handwritten digits from the MNIST dataset and log them as embedding vectors with labels and original images. The generated sprite image is of grid *8 9**, which is not square:

sprite

But, the current TensorBoard requires a square sprite image for the embedding projector working correctly (see here). So, the above non-square sprite image will make the thumbnails misaligned with their embeddings:

screenshot 2019-01-14 at 18 10 49

(e.g. embedding at the center of label 2 is displayed with thumbnail '9')

This issue can be easily solved by changing just one line to constrain the produced sprite image to be precisely squared:

sprite

I tested it locally and would like to make a pull request. ;)

reminisce commented 5 years ago

Great finding. I saw something misaligned before but didn't know where went wrong. Thanks for figuring this out.

YusongLeng commented 5 years ago

Issue solved! Welcome to reopen if a new loophole is found.