qubvel / segmentation_models

Segmentation models with pretrained backbones. Keras and TensorFlow Keras.
MIT License
4.66k stars 1.03k forks source link

Index error when training on greyscale data #557

Open andytmcn opened 1 year ago

andytmcn commented 1 year ago

I'm training a dataset using greyscale data normalized between 0 and 1 with a shape 1024,256,1. Following the documentation the input is converted to 3 layers using

BACKBONE = 'resnet18'
base_model = sm.Unet(backbone_name=BACKBONE, encoder_weights='imagenet', 
                      classes=1, activation='sigmoid')

inp = Input(shape=(None, None, 1))
l1 = Conv2D(3, (1, 1))(inp) # map N channels data to 3 channels
out = base_model(l1)

model = Model(inp, out, name=base_model.name)

With the Backbone set to resnet, training is successful. However when I try another backbone such as efficientnet I get the following error

    model, history = main(model, train_gen, val_gen, callbacks, epochs = epochs)  # next section explains the use of sys.exit

  File "/.../...py", line 247, in main
    history = model.fit(train_gen, epochs=epochs, validation_data=val_gen,

  File "/.../lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None

  File "/.../...py", line 97, in __getitem__
    x = self.preprocessing(x)

  File "/.../lib/python3.9/site-packages/segmentation_models/__init__.py", line 43, in wrapper
    return func(*args, **new_kwargs)

  File "/.../lib/python3.9/site-packages/segmentation_models/__init__.py", line 34, in wrapper
    return func(*args, **kwargs)

  File "/.../lib/python3.9/site-packages/classification_models/models_factory.py", line 78, in wrapper
    return func(*args, **new_kwargs)

  File "/.../lib/python3.9/site-packages/classification_models/models/senet.py", line 435, in preprocess_input
    return imagenet_utils.preprocess_input(x, mode='torch', **kwargs)

IndexError: index 1 is out of bounds for axis 3 with size 1