ImportError: cannot import name 'image_dataset_from_directory' from 'keras.preprocessing'

Viewed 4457

I used anaconda in ubuntu-20.04 operating systems.

Keras version is: 2.4.3

Tensorflow version: 2.2.0

I imported below packages,

import os
import keras
import numpy as np
import tensorflow as tf
from keras.models import Model
import matplotlib.pyplot as plt
from keras.optimizers import Adam
from keras.applications import MobileNet
from sklearn.metrics import confusion_matrix
from keras.layers.core import Dense, Activation
from keras.metrics import categorical_crossentropy
from sklearn.model_selection import train_test_split
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.mobilenet import preprocess_input
from keras.preprocessing import image_dataset_from_directory  //Here I faced error

Error is also below, please concern here,

ImportError                               Traceback (most recent call last)
<ipython-input-8-477695c122c5> in <module>
     13 from keras.preprocessing.image import ImageDataGenerator
     14 from keras.applications.mobilenet import preprocess_input
---> 15 from keras.preprocessing import image_dataset_from_directory

ImportError: cannot import name 'image_dataset_from_directory' from 'keras.preprocessing' (/home/imdadul/anaconda3/lib/python3.8/site-packages/keras/preprocessing/__init__.py)

Note: Please concern my problem and help me for solving this error.

1 Answers

First off, I cannot find any keras implementation of image_dataset_from_directory, but instead only the tensorflow.keras implementation, imported by writing: from tensorflow.keras.preprocessing import image_dataset_from_directory . In the keras-documentation, they refer to use tensorflow.keras as well.

However, since you are using Tensorflow 2.2.0, I am afraid that will not be possible either, as explained here. I would recommend installing Tensorflow 2.3.0, and it should work.

Related