0

I have a 4D numpy array: (#images, height, width, channels) of RGB images. The exact dimensions are (46, 224, 224, 3).

I also have the grayscale versions of the same images. The exact dimensions are (46, 224, 224, 1).

I'm playing with Keras, trying to make an autoencoder to learn the image values from the grayscale ones, but the model complains about the dimensions, so I'd like to add the 2 extra channels to the grayscale array, but I can't seem to figure out how.

temp_gray_images = np.zeros(original_images.shape, dtype=np.float32)
temp_gray_images[:,:,:,0] = gray_images
ValueError: could not broadcast input array from shape (46,224,224,1) into shape (46,224,224)

So I then learned about numpy.hstack, and I opened a new terminal and tried to play with some dummy data and a number of dimensions I could handle, and was able to get the expected results. However, that doesn't seem to work with 4 dimensions.

temp_gray_images = np.hstack([gray_images, np.zeros([original_images.shape[0], original_images.shape[1], original_images.shape[2], 2])])
ValueError: all the input array dimensions except for the concatenation axis must match exactly

Which doesn't make sense, because I kept the first 3 dimensions the same size.

But ultimately the validation data doesn't match the train data because of the 2 extra channels in RGB vs. grayscale.

x_train shape: (46, 224, 224, 3)
xtest_shape shape: (46, 224, 224, 1)
autoencoder.fit(x_train, x_train, epochs=50, batch_size=256, shuffle=True, validation_data=(x_test, x_test))

Thank you.

Richard Żak
  • 814
  • 1
  • 11
  • 21
  • Does `temp_gray_images[:,:,:,0,None] = gray_images` work for you? Or `temp_gray_images[:,:,:,:1] = gray_images`? Or `temp_gray_images[:,:,:,0] = gray_images[:,:,:,0]`? – Paul Panzer Oct 12 '18 at 03:37

1 Answers1

0

Could this be what you are looking for ?

import numpy as np
x_train=np.random.randn(46, 224, 224, 1)
y=np.repeat(x_train,3,axis=3)
print(y.shape) #(46, 224, 224, 3)

repeat will duplicate the gray scale images into the required number of channels

Niteya Shah
  • 1,809
  • 1
  • 17
  • 30