Error in input shape for nodule detection model

class DataGenerator(tf.keras.utils.Sequence):
def init(self, images, bboxes, labels, batch_size):
self.images = images
self.bboxes = bboxes
self.labels = labels
self.batch_size = batch_size

def __len__(self):
    return len(self.images) // self.batch_size

def __getitem__(self, idx):
        start_idx = idx * self.batch_size
        end_idx = (idx + 1) * self.batch_size

        batch_images = self.images[start_idx:end_idx]
        batch_bboxes = self.bboxes[start_idx:end_idx]
        batch_labels = self.labels[start_idx:end_idx]

        # Preprocess the images and bounding boxes
        batch_images = tf.keras.applications.resnet50.preprocess_input(batch_images)
        batch_bboxes = tf.convert_to_tensor(batch_bboxes, dtype=tf.float32)
        print("shape",batch_images.shape,batch_bboxes,batch_labels)
        return (batch_images, batch_bboxes), (batch_labels, batch_bboxes)

#print(labels.shape) (99,1)
#print(train_images.shape) (90,512,512,3)
#print(train_bboxes.shape) (90,4) bounding box 4 coordinates

Create generators for the training and validation data

train_gen = DataGenerator(train_images, train_bboxes, labels[:90], batch_size=1)
val_gen = DataGenerator(val_images, val_bboxes, labels[:90], batch_size=1)

#model
import tensorflow as tf

Define the input tensors

backbone_input = tf.keras.layers.Input(shape=(512,512,3))
rois_input = tf.keras.layers.Input(shape=(None, 4))

Define the backbone model

backbone = tf.keras.applications.ResNet50V2(include_top=False, weights=‘imagenet’, input_tensor=backbone_input)
backbone_output = backbone.output
print(“backbone”,backbone_output)

Define the region proposal network (RPN)

rpn_conv = tf.keras.layers.Conv2D(512, (3, 3), activation=‘relu’, padding=‘same’)(backbone_output)
print(“rpiconv”,rpn_conv)
rpn_cls = tf.keras.layers.Conv2D(2, (1, 1), activation=‘softmax’, padding=‘valid’)(rpn_conv)
print(“rpicls”,rpn_cls)
rpn_reg = tf.keras.layers.Conv2D(4, (1, 1), activation=‘linear’, padding=‘valid’)(rpn_conv)
print(“rpnreg”,rpn_reg)

Define the region of interest (RoI) pooling layer

num_rois = tf.shape(rois_input)[1]
crop_size = (7, 7)

rois = tf.reshape(rois_input, [-1, 4])
print(“rois”,rois)
box_indices = tf.repeat(tf.range(num_rois), 4)
print(“box_indices”,box_indices)
roi_features = tf.image.crop_and_resize(backbone_output, rois, box_indices, crop_size)
print(“roifeatures”,roi_features)
roi_align = tf.keras.layers.MaxPooling2D(pool_size=crop_size, strides=crop_size)(roi_features)
print(“roialign”,roi_align)
roi_pooling = tf.keras.layers.Flatten()(roi_align)
print(“roipool”,roi_pooling)

Define the fully connected (FC) layers for classification and regression

fc = tf.keras.layers.Dense(1024, activation=‘relu’)(roi_pooling)
print(“fc”,fc)
#cls_output = tf.keras.layers.Dense(2, activation=‘softmax’, name=‘cls_output’)(fc)
cls_output = tf.keras.layers.Dense(1, activation=‘sigmoid’, name=‘cls_output’)(fc)
print(“clsout”,cls_output)
reg_output = tf.keras.layers.Dense(4, activation=‘linear’, name=‘reg_output’)(fc)
print(“regout”,reg_output)

Define the final model and compile it

model = tf.keras.models.Model(inputs=[backbone.input, rois_input], outputs=[cls_output, reg_output])

model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001, momentum=0.9),
loss={‘cls_output’: ‘binary_crossentropy’, ‘reg_output’: ‘mse’})

model.fit(train_gen, epochs=10, validation_data=val_gen)#error incompatible shape

#error
shape (1, 512, 512, 3) tf.Tensor([[104. 341. 116. 353.]], shape=(1, 4), dtype=float32) [[1]]
Epoch 1/10
WARNING:tensorflow:Model was constructed with shape (None, None, 4) for input KerasTensor(type_spec=TensorSpec(shape=(None, None, 4), dtype=tf.float32, name=‘input_10’), name=‘input_10’, description=“created by layer ‘input_10’”), but it was called on an input with incompatible shape (None, None).
WARNING:tensorflow:Model was constructed with shape (None, None, 4) for input KerasTensor(type_spec=TensorSpec(shape=(None, None, 4), dtype=tf.float32, name=‘input_10’), name=‘input_10’, description=“created by layer ‘input_10’”), but it was called on an input with incompatible shape (None, None).
shape (1, 512, 512, 3) tf.Tensor([[177. 367. 191. 381.]], shape=(1, 4), dtype=float32) [[1]]
shape (1, 512, 512, 3) tf.Tensor([[305. 172. 331. 198.]], shape=(1, 4), dtype=float32) [[1]]
Graph execution error