I use CNN in Keras to build a model and now I want to use all the images to make the final prediction. This is the dimensions of my inputs and labels:
X_pred, y_pred=next(pred_generator)
X_pred.shape, y_pred.shape
result:
((132, 64, 64, 3), (132, 64, 64, 1))
This is the code for prediction:
pred_64= model.predict(X_pred)
pred_64.shape
result:
(132, 64, 64, 1)
As it is obvious, there 132 predicted images with the size of 64X64. I want to merge every 4 images to create individual image with the size of 128X128. Finally, I should have 33 images by merging every 4 images produced from model.predict.
This is an example about the order of images when they are saved in 128X128 dimension.
After adding the swapaxes code the images will be rotated as below:
pred_128 = pred_64.reshape(66, 128, 64, 1).swapaxes(1, 2).reshape(33, 128, 128, 1)
I added np.transpose
as below. It helps to correct the rotation but image 2 replaced image 3 and vice versa. Could you please help me to solve that?
pred_128 = pred_64.reshape(66, 128, 64, 1).swapaxes(1, 2).reshape(33, 128, 128, 1)
pred_128= np.transpose(pred_128, [0, 2, 1, 3])
I still hope we can fix the problem using reshapes, but in the meantime here is a more brute force solution:
# Initializing counters
i = 0 # Old image number
j = 0 # New image number
# Pre-allocate new images array
pred_128 = np.zeros((33, 128, 128, 1))
# Loop over new images
while j < 33:
pred_128 [j, :64, :64, 0] = pred_64[0+i, :, :, 0] # Upper left
pred_128 [j, :64, 64:, 0] = pred_64[1+i, :, :, 0] # Upper right
pred_128 [j, 64:, :64, 0] = pred_64[2+i, :, :, 0] # Lower left
pred_128 [j, 64:, 64:, 0] = pred_64[3+i, :, :, 0] # Lower right
# Add to counters
i += 4
j += 1