'tensorflow.keras.preprocessing'

2020-08-18  本文已影响0人  1037号森林里一段干木头

tensorflow=2.2.0,在运行下面的代码时出现问题

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import regularizers
import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
import numpy as np

filepath=r"C:\Users\bxzyz\Desktop\OCV\img-gen"

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    directory=filepath,
    validation_split=0.2,
    subset="training",
    labels='inferred',
    seed=100,
    label_mode='categorical',
    batch_size=32,
    image_size=(75, 35))
validation_ds = tf.keras.preprocessing.image_dataset_from_directory(
    directory=filepath,
    validation_split=0.2,
    subset='validation',
    labels='inferred',
    seed=100,
    label_mode='categorical',
    batch_size=32,
    image_size=(75, 35))

输出

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-7bae8c18aaee> in <module>
      9 filepath=r"C:\Users\bxzyz\Desktop\OCV\img-gen"
     10 
---> 11 train_ds = tf.keras.preprocessing.image_dataset_from_directory(
     12     directory=filepath,
     13     validation_split=0.2,

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

原因,2.1or2.2稳定版本的tensorflow没有这个函数:
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.
安装的时候有这个错误提示

image.png

解决办法:

pip install tf-nightly

######################################################
注:安装tf-nightly后,GPU能用tf.keras.preprocessing.image_dataset_from_directory就不能用,反之,tf.keras.preprocessing.image_dataset_from_directory能用,GPU就不能用了,不断安装卸载不下10次了.........

上一篇下一篇

猜你喜欢

热点阅读