pythonkeraspytorchtf.keras

'_PrefetchDataset' object has no attribute 'shape' when calling resnet_v2.preprocess_input()


I keep running into this error when trying to preprocess my datasets:

AttributeError: '_PrefetchDataset' object has no attribute 'shape'. Did you mean: 'save'?

I'm currently using ResNet, but I've also tried the preprocess functions for MobileNet and NasNet. It almost looks like it's an issue with imagenet_utils.py, but I assume that's not the case.

Is there any way I can fix this other than switching to a new application?

This is my code:

trainingdata = keras.utils.image_dataset_from_directory(
    directory='archive/chest_xray/train/',
    labels='inferred',
    label_mode='int',
    batch_size=32,
    image_size=(512,512)
)

validationdata = keras.utils.image_dataset_from_directory(
    directory='archive/chest_xray/test/',
    labels='inferred',
    label_mode='int',
    batch_size=32,
    image_size=(512,512)
)

td = keras.applications.resnet_v2.preprocess_input(trainingdata)

Here is my full traceback:

Traceback (most recent call last):
  File "C:\Users\You\Documents\keras models\PNEUMONIA.PY", line 25, in <module>
    td = keras.applications.resnet_v2.preprocess_input(trainingdata)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\You\Documents\keras models\.venv\Lib\site-packages\keras\src\applications\resnet_v2.py", line 134, in preprocess_input
    return imagenet_utils.preprocess_input(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\You\Documents\keras models\.venv\Lib\site-packages\keras\src\applications\imagenet_utils.py", line 106, in preprocess_input
    return _preprocess_tensor_input(x, data_format=data_format, mode=mode)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\You\Documents\keras models\.venv\Lib\site-packages\keras\src\applications\imagenet_utils.py", line 254, in _preprocess_tensor_input
    ndim = len(x.shape)
               ^^^^^^^
AttributeError: '_PrefetchDataset' object has no attribute 'shape'. Did you mean: 'save'?

Solution

  • Found a workaround by adding keras.applications.mobilenet_v2.preprocess_input() as a layer in between an input layer and the premade model, like so:

    input = keras.layers.Input([224, 224, 3])
    
    pre = keras.applications.mobilenet_v2.preprocess_input(input)
    
    net = keras.applications.MobileNetV2(
        input_tensor = pre,
        alpha=1.0,
        include_top=True,
        weights=None,
        classes=3,
        classifier_activation="softmax",
        name=None,
    )
    model = keras.Model(inputs=input, outputs=net.output)
    

    Technically a different function than in the original question, but ultimately they both end up calling keras\src\applications\imagenet_utils.py and produce the same error when they do, so this should hopefully work for all preprocess_input functions that end up calling this.