Reshape output dimensions to fit Keras model

140 views Asked by At

I have a Keras model for keypoints detection of a cat dataset. For each colored image, there are 3 key points with the corresponding 3 heatmaps. The input image of the model is 64,64,3 The corresponding output is of shape 3,64,64.

I am preparing the the cropped and resized heatmaps in the following function:

def crop_heatmaps():

   dataset['cropped_heatmaps'] = []
   
   for i in range(len(dataset['heatmaps'])):

        cropped_heats = []
        heatmaps = dataset['heatmaps'][i]
        bb = dataset['bbs'][i]
        

        x1 = max(bb[0] - 20, 0) #avoid negative coordinates of the extended bounding box
        y1 = max(bb[1] - 20, 0)
        x2 = bb[2] + 20
        y2 = bb[3] + 20

        for heat in heatmaps:

            cropped_heat = heat[y1:y2, x1:x2]
            resized_heat = cv2.resize(cropped_heat, (64, 64))
            #plt.imshow(resized_heat)

            cropped_heats.append(resized_heat)

            cropped_heatmaps = np.array(cropped_heats)
            
            dataset['cropped_heatmaps'].append(cropped_heats)

I created 2 dataImageGenerators for input and output and zipped them together.

train_generator = zip(img_train_generator, heatmaps_train_generator)

history = model.fit((pair for pair in train_generator),
                    epochs=30,
                    validation_data=(),
                    verbose=1
                  )

When fitting the model I am getting this error : Incompatible shapes: [128,64,3,64] vs. [128,3,64,64]

The model looks like this:

input_1 (InputLayer) [(None, 64, 64, 3)] 0


block1_conv1 (Conv2D) (None, 64, 64, 64) 1792


block1_conv2 (Conv2D) (None, 64, 64, 64) 36928


block1_pool (MaxPooling2D) (None, 32, 32, 64) 0


block2_conv1 (Conv2D) (None, 32, 32, 128) 73856


block2_conv2 (Conv2D) (None, 32, 32, 128) 147584


bottleneck_1 (Conv2D) (None, 32, 32, 160) 5243040


bottleneck_2 (Conv2D) (None, 32, 32, 160) 25760


upsample_1 (Conv2DTranspose) (None, 64, 64, 3) 1920

I tried

np.reshape(cropped_heatmaps,(64,64,3))

but it did not work. How can I reshape the heatmaps to get an 64,64,3? (3 channels)

1

There are 1 answers

1
Timur U On

if your want to change axis 1 and 2 ip possible to use: np.moveaxis(x,1,2)

sample:

import numpy as np

x = np.zeros((128,64,3,32))
print(x.shape)

y = np.moveaxis(x,1,2)
print(y.shape)

outs:

(128, 64, 3, 32)
(128, 3, 64, 32)
>>> 

with (64,64,3) to (3,64,64)

may to use:

import numpy as np

x = np.zeros((64,64,3))
print(x.shape)

y = np.moveaxis(x,-1,0)
print(y.shape)

outs:

(64, 64, 3)
(3, 64, 64)
>>>