How to use Conv2D in multiple images input?

Viewed 489

I want to use multiple images as input of the network. And I want to add Conv2D layers, something like that:

from tensorflow.keras.layers import *
from tensorflow.keras.models import Sequential

model = Sequential([
    Input(shape=(1, 128, 128, 1)),
    Conv2D(32, 3),
    Flatten(),
])

But this code raises the error: Input 0 of layer conv2d_40 is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, 1, 128, 128, 1]

But the code below is working fine:

model = Sequential([
    Input(shape=(1, 512, 512, 1)),
    Dense(32),
    Flatten(),
])

I know, I can add multiple Input layers, but I want to know is there a way to make it like this?

I mean I want to use data of input shape [NUMBER_OF_IMAGES, WIDTH, HEIGHT, N_CHANNELS]

And NUMBER_OF_IMAGES is not amount of all images. This is an amount for current input

1 Answers

Conv2D expects input in 4D, you can't change that. I'm not exactly sure what you're trying to accomplish but you could use Conv3D instead:

from tensorflow.keras.layers import *
from tensorflow.keras.models import Sequential
import tensorflow as tf

model = Sequential([
    Input(shape=(None, 128, 128, 1)),
    Conv3D(32, kernel_size=(1, 3, 3)),
    Flatten()
])

multiple_images = tf.random.uniform((10, 10, 128, 128, 1), dtype=tf.float32)

model(multiple_images)
<tf.Tensor: shape=(10, 5080320), dtype=float32, numpy=
array([[-0.26742983, -0.09689523, -0.12120364, ..., -0.02987139,
         0.05515741,  0.12026916],
       [-0.18898709,  0.12448274, -0.17439063, ...,  0.23424357,
        -0.06001307, -0.13852882],
       [-0.14464797,  0.26356792, -0.34748033, ...,  0.07819699,
        -0.11639086,  0.10701762],
       ...,
       [-0.1536693 ,  0.13642962, -0.18564   , ...,  0.07165999,
        -0.0173855 , -0.04348694],
       [-0.32320747,  0.09207243, -0.22274591, ...,  0.11940736,
        -0.02635285, -0.1140241 ],
       [-0.21126074, -0.00094431, -0.10933039, ...,  0.06002581,
        -0.09649743,  0.09335127]], dtype=float32)>
Related