nikitadurasov / masksembles

Official repository for the paper "Masksembles for Uncertainty Estimation" (CVPR 2021).
https://www.norange.io/projects/masksembles/
MIT License
98 stars 15 forks source link

Number of ways to split should evenly divide the split dimension #7

Open tomersein opened 3 years ago

tomersein commented 3 years ago

Hello,

I'm trying to use the layer, and I'm facing the below error:

batch_size = 128
epochs = 5

model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)
InvalidArgumentError:  Number of ways to split should evenly divide the split dimension, but got split_dim 0 (size = 29) and num_split 4
     [[node sequential_1/masksembles2d/split (defined at /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py:1751) ]] [Op:__inference_distributed_function_14069]

My Network looks like this:

model = keras.Sequential(
    [
        keras.Input(shape=input_shape),
        layers.Conv2D(32, kernel_size=(3, 3), activation="elu"),
        Masksembles2D(4, 2.0), # adding Masksembles2D
        layers.MaxPooling2D(pool_size=(2, 2)),

        layers.Conv2D(64, kernel_size=(3, 3), activation="elu"),
        Masksembles2D(4, 2.0), # adding Masksembles2D
        layers.MaxPooling2D(pool_size=(2, 2)),

        layers.Flatten(),
        Masksembles1D(4, 2.), # adding Masksembles1D
        layers.Dense(num_classes, activation="softmax"),
    ]
)

model.summary()

My amount of data is 1170 records of train, and 0.1 to the validation.

What should I notice before using this model?

Thanks.

nikitadurasov commented 3 years ago

Hey Tomer,

I think the problem is that the last training (or validation) batch of your data doesn't actually have 128 samples inside but only 29 (check https://www.tensorflow.org/api_docs/python/tf/data/Dataset, drop_remainder argument).

The current implementation assumes that every batch has a number of samples that are divisible by the number of masks.

A possible workaround is to create tf dataset with drop_remainder option turned on or just check that number of training (and validation) samples is divisible by 4.

Best, Nikita