Anakin
Anakin

Reputation: 2010

Converting images to one-hot array and back returns black images

I have a numpy array of numpy array of 3 grayscale images with just 0 and 255 values (shape: (3, 512, 512)). I turned them to one-hot encodings into 2 classes using tf.one_hot (shape: (3, 512, 512, 2)). Now, I am trying to get them back to original form through np.argmax. But when I read the images, they are all black.

    labels = []
    labels.append(label_1)
    labels.append(label_2)
    labels.append(label_3)
    labels = np.asarray(labels)

    print(labels.shape)                # (3, 512, 512)

    sess = tf.InteractiveSession()

    labels = tf.one_hot(labels, 2)     # (3, 512, 52, 2)
    print(labels.shape)
    #print(labels)

    labels = labels.eval()             # to convert to numpy array from tensor

    imgs = np.argmax(labels, axis=3)   # also tried tf.argmax
    print(imgs.shape)                  # (3, 512, 512)

    for i in range(imgs.shape[0]):
        image = imgs[i]
        print(img.shape)               # (512, 512)
        indices = np.where(image > 0) 
        print(indices)                 # array([], dtype=int64), array([], dtype=int64)
        print(indices)
        image = Image.fromarray(image, 'L')
        image.show()                   # black images, values all zero

I am sure I am missing something pretty simple but can't figure it out. Any help will be appreciated. Thanks.

Edit:

I checked for indices where the array has non-zero values. But all values seem to be zeros. So, not an issue of displaying the image. I think the problem is with argmax but I do not know what.

Upvotes: 1

Views: 1345

Answers (1)

Pavel Komarov
Pavel Komarov

Reputation: 1246

Argmax returns the index where the maximum occurs over some dimension. In your case you are taking the argmax over a dimension of length 2, so it returns either a 0 or a 1 for every location in your (3, 512, 512) array. Both options are very dark on the 8-bit scale and will appear black! You have to make sure you display the binary images as binary.

I haven't used this library before, but maybe try mode='I'? or some of the other modes here https://pillow.readthedocs.io/en/3.1.x/handbook/concepts.html#concept-modes. The return type of that argmax is important.

Upvotes: 1

Related Questions