keras-team / keras-io

Keras documentation, hosted live at keras.io
Apache License 2.0
2.76k stars 2.04k forks source link

No attribute 'image_dataset_from_directory' #12

Closed baksheev closed 4 years ago

baksheev commented 4 years ago

Hi! I was going through the guide and I got the error: AttributeError: module 'tensorflow.keras.preprocessing' has no attribute 'image_dataset_from_directory'

Here https://github.com/keras-team/keras-io/blob/a3bb3dc49b8eb0ebbe8a5d91329f8378eacdd7d4/examples/vision/image_classification_from_scratch.py#L83

I have no idea how to fix it and why it's not working. Can you help me?

Package versions:

swghosh commented 4 years ago

The specific function (tf.keras.preprocessing.image_dataset_from_directory) is not available under TensorFlow v2.1.x or v2.2.0 yet. It is only available with the tf-nightly builds and is existent in the source code of the master branch. https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image_dataset_from_directory

For more information about installing nightly builds of TensorFlow, you can refer to https://www.tensorflow.org/install or use

pip install tf-nightly

Thanks.

swghosh commented 4 years ago

After reviewing some parts of the new keras.io website, I could also find other examples and source code that involves function calls and reference to classes only available with tf-nightly (or in the master branch of TensorFlow). As also observed in, https://keras.io/examples/keras_recipes/quasi_svm/ where the tf.keras.layers.experimental.RandomFourierFeatures have been used.

https://github.com/keras-team/keras-io/blob/a3bb3dc49b8eb0ebbe8a5d91329f8378eacdd7d4/examples/keras_recipes/quasi_svm.py#L33 https://github.com/keras-team/keras-io/blob/a3bb3dc49b8eb0ebbe8a5d91329f8378eacdd7d4/examples/keras_recipes/quasi_svm.py#L39-L47

/cc: @fchollet Is the whole documentation hosted at keras.io website designed around tf-nightly as of now?

fchollet commented 4 years ago

@fchollet Is the whole documentation hosted at keras.io website designed around tf-nightly as of now?

Yes. These features will be in TF 2.3. Past 2.3 we don't expect that there will be discrepancies between the docs and the latest released API.

swghosh commented 4 years ago

Thanks for the clarification.

bsdis commented 4 years ago

When is TF 2.3 expected to be released more or less?

swghosh commented 4 years ago

TensorFlow 2.2 was just released one and half weeks before. Although, there is no definitive announcement about the exact release date of next release cycle, the TensorFlow community usually releases major version updates like once in 5-6 months. Meanwhile, most of the code on this repo can be runt using the tf-nightly package as well as most examples should be fine when directly runt on TensorFlow 2.2 itself.

If you are experiencing any issues, please try:

pip install tf-nightly # nightly builds of the tensorflow master branch released each night

And, you should be up and doing in no time!

pranabdas457 commented 4 years ago

I am also getting the same error. Can anyone help me how to resolve this?

module 'tensorflow.keras.preprocessing' has no attribute 'image_dataset_from_directory'

swghosh commented 4 years ago

Hi @pranabdas457

TensorFlow 2.2 was just released one and half weeks before. Although, there is no definitive announcement about the exact release date of next release cycle, the TensorFlow community usually releases major version updates like once in 5-6 months. Meanwhile, most of the code on this repo can be runt using the tf-nightly package as well as most examples should be fine when directly runt on TensorFlow 2.2 itself.

If you are experiencing any issues, please try:

pip install tf-nightly # nightly builds of the tensorflow master branch released each night

And, you should be up and doing in no time!

Please try installing the tf-nightly package in case you're trying to use image_dataset_from_directory function in your code.

lilly-chen commented 4 years ago

Hi i was gettin "module 'tensorflow.keras.preprocessing' has no attribute 'image_dataset_from_directory'" so i run the pip install tf-nightly.

but now i am getting a new error message cannot import name 'image_dataset_from_directory' from 'tensorflow.keras.preprocessing.image' (/Users/xxx/anaconda3/lib/python3.7/site-packages/tensorflow/keras/preprocessing/image/init.py)

appreciate any suggestion to fix this! Thanks

christian-unoxx commented 4 years ago

Hi i was gettin "module 'tensorflow.keras.preprocessing' has no attribute 'image_dataset_from_directory'" so i run the pip install tf-nightly.

but now i am getting a new error message cannot import name 'image_dataset_from_directory' from 'tensorflow.keras.preprocessing.image' (/Users/xxx/anaconda3/lib/python3.7/site-packages/tensorflow/keras/preprocessing/image/init.py)

appreciate any suggestion to fix this! Thanks

I also experienced this the other day. (I haven't tried it again since then, though).

shilan commented 4 years ago

Now that tensorflow 2.3 is released, image_dataset_from_directory actually works.

TByte007 commented 4 years ago

Does that mean that if I use PlaidML there is no chance I can use those functions ?

swghosh commented 4 years ago

Does that mean that if I use PlaidML there is no chance I can use those functions ?

Keras has deprecated support for multi backends for quite some time now. The only option here to try out the every new feature that Keras API offers is to stick to the TensorFlow backend a.k.a. tf.keras.

Look into transitioning your codebase to tf.keras and TensorFlow 2.0 for all the goodness that comes with Eager execution and high level Deep Learning APIs.

TByte007 commented 4 years ago

I wish I can transition my GPU too ;) So what's the point of Keras and why not just use TF directly ? I think I'm missing some point :(

swghosh commented 4 years ago

I wish I can transition my GPU too ;) So what's the point of Keras and why not just use TF directly ? I think I'm missing some point :(

Seems like you're deep learning on an AMD GPU with Plaid ML + Keras! And, you're correct TensorFlow 2.0 is tightly coupled with Keras API and hence, using tf.keras from tensorflow_v2 is the current default for Keras!

https://github.com/keras-team/keras#multi-backend-keras-and-tfkeras

TByte007 commented 4 years ago

I wish I can transition my GPU too ;) So what's the point of Keras and why not just use TF directly ? I think I'm missing some point :(

Seems like you're deep learning on an AMD GPU with Plaid ML + Keras! And, you're correct TensorFlow 2.0 is tightly coupled with Keras API and hence, using tf.keras from tensorflow_v2 is the current default for Keras!

https://github.com/keras-team/keras

Yes I currently only have AMD GPUs and I wrote GA OpenCL kernel for it it was working fine but I wanted to see what other people are doing and I was sure there are LOT better implementation than mine for machine learning (and better optimized kernels). But everything I find is NVIDIA bound - it;s like NVIDIA is paying developers NOT to use anything else and it's really frustrating and it's making me not to want to even see NVIDIA product in my life ;) I can always go buy a new GPU but Now i'm pissed at NVIDIA lol.

swghosh commented 4 years ago

@TByte007 Probably, you should try ROCm TensorFlow which is a community supported TF port delivering implementations so that we can run TF2.x on AMD GPUs.

https://github.com/ROCmSoftwarePlatform/tensorflow-upstream

TByte007 commented 4 years ago

@TByte007 Probably, you should try ROCm TensorFlow which is a community supported TF port delivering implementations so that we can run TF2.x on AMD GPUs.

https://github.com/ROCmSoftwarePlatform/tensorflow-upstream

That's what I'm about to try given the chance, the problem is that ROCm is not supported under Windows and I cant fit my AMD cards (or even one card) in the Linux machine I have (well , it's never easy I guess). So far PlaidML-Keras under windows works fine and faster than my implementation but sooner or later I have to switch.

chasecockrell commented 3 years ago

Its pretty sad that this team can't even provide a working tutorial

MustafaAlperenYILDIRIM commented 3 years ago

The specific function (tf.keras.preprocessing.image_dataset_from_directory) is not available under TensorFlow v2.1.x or v2.2.0 yet. It is only available with the tf-nightly builds and is existent in the source code of the master branch. https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image_dataset_from_directory

For more information about installing nightly builds of TensorFlow, you can refer to https://www.tensorflow.org/install or use

pip install tf-nightly

Thanks.

tf-nightly library fixed the error. Thanks

pathikg commented 3 years ago

@fchollet Is the whole documentation hosted at keras.io website designed around tf-nightly as of now?

Yes. These features will be in TF 2.3. Past 2.3 we don't expect that there will be discrepancies between the docs and the latest released API.

The same error is still there in Tensorflow Version 2.4

surak commented 3 years ago

Yes. These features will be in TF 2.3. Past 2.3 we don't expect that there will be discrepancies between the docs and the latest released API. The same error is still there in Tensorflow Version 2.4

Can confirm. The example in https://www.tensorflow.org/tutorials/load_data/images uses keras.utils, and both are missing:

>>> tf.__version__
'2.4.1'
>>> import tensorflow.keras.utils
>>> dir(tf.keras.utils)
['CustomObjectScope', 'GeneratorEnqueuer', 'OrderedEnqueuer', 'Progbar', 'Sequence', 'SequenceEnqueuer', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_sys', 'custom_object_scope', 'deserialize_keras_object', 'get_custom_objects', 'get_file', 'get_registered_name', 'get_registered_object', 'get_source_inputs', 'model_to_dot', 'normalize', 'pack_x_y_sample_weight', 'plot_model', 'register_keras_serializable', 'serialize_keras_object', 'to_categorical', 'unpack_x_y_sample_weight']

>>> import tensorflow.keras.preprocessing
2021-10-17 16:13:03.445692: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
>>> dir(tensorflow.keras.preprocessing)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_sys', 'image', 'image_dataset_from_directory', 'sequence', 'text', 'text_dataset_from_directory', 'timeseries_dataset_from_array']
natelee-tw commented 2 years ago

The same issue too in TF2.5.2

tf.__version__
'2.5.2'
dir(tf.keras.utils)
['CustomObjectScope', 'GeneratorEnqueuer', 'OrderedEnqueuer', 'Progbar', 'Sequence', 'SequenceEnqueuer', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_sys', 'custom_object_scope', 'deserialize_keras_object', 'experimental', 'get_custom_objects', 'get_file', 'get_registered_name', 'get_registered_object', 'get_source_inputs', 'model_to_dot', 'normalize', 'pack_x_y_sample_weight', 'plot_model', 'register_keras_serializable', 'serialize_keras_object', 'to_categorical', 'unpack_x_y_sample_weight']
JMIdeaMaker commented 2 years ago

It's March and the problems are still here...

pandysong commented 2 years ago

Same issue here on mac

>>> tf.__version__
'2.3.0'

>>> dir(tf.keras.utils)
['CustomObjectScope', 'GeneratorEnqueuer', 'HDF5Matrix', 'OrderedEnqueuer', 'Progbar', 'Sequence', 'SequenceEnqueuer', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_sys', 'convert_all_kernels_in_model', 'custom_object_scope', 'deserialize_keras_object', 'get_custom_objects', 'get_file', 'get_registered_name', 'get_registered_object', 'get_source_inputs', 'model_to_dot', 'multi_gpu_model', 'normalize', 'pack_x_y_sample_weight', 'plot_model', 'register_keras_serializable', 'serialize_keras_object', 'to_categorical', 'unpack_x_y_sample_weight']
esraa-abdelmaksoud commented 1 year ago

I've got the same problem using the version '2.4.0', but the problem was solved with the following: tf.keras.preprocessing.image_dataset_from_directory()