簡體   English   中英

面臨關於 TensorFlow 中的 OwnedIteror 屬性錯誤的錯誤

[英]Facing error about OwnedIteror attribute error in TensorFlow

關於數據生成器迭代器,我面臨 TensorFlow 的問題。 我想使用順序 model 並從數據幀創建 dataframe 數據生成器。 現在我想訓練順序 model 並將這個數據生成器用作它的輸入,現在我面臨Attribute error

有什么辦法可以解決這個錯誤。

這是我使用 Keras 的圖像生成器的方法:

datagen = ImageDataGenerator()
data = datagen.flow_from_dataframe(
    dataframe=dataSet,
    directory=None,
    x_col="x_col",
    y_col='target',
    weight_col=None,
    target_size=(28, 28),
    color_mode="rgb",
    classes=None,
    class_mode="raw",
    batch_size=32,
    shuffle=True,
)

這是我的 model:

model = keras.models.Sequential()
model.add(keras.layers.Conv2D(28, (3,3), padding='same', input_shape=(32,32,3)))
model.add(keras.layers.Activation('relu'))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(512))
model.add(keras.layers.Activation('relu'))
model.add(keras.layers.Dense(2, activation='softmax'))

這是我編譯 model 的方法:

model.compile(optimizer='sgd', loss="categorical_crossentropy", metrics=["accuracy"])

我稱之為 model:

model.fit_generator(generator=data, epochs=10, verbose=1)

我的錯誤是:

AttributeError:在用戶代碼中:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:576 _reset_compile_cache  *
    self._compiled_trainable_state = self._get_trainable_state()

AttributeError: 'OwnedIterator' object has no attribute '_get_trainable_state'

在嘗試了這么多不同的圖像數據管道之后,我能夠解決這個答案。 有些得到屬性錯誤,有些得到其他東西。

datagen = tf.keras.preprocessing.image.ImageDataGenerator()
data = datagen.flow_from_dataframe(
    #Simple dataframe name which contains columns which has path to your image      files and target column with values like 0,1 and many other case.
    dataframe=train,
    # If you gave absolute path in dataframe column then you can put here as None else give directory name and then file names will be accessed from dataframe.
    directory=None,
    # Column name with image names or full absolute paths
    x_col="image_name",
    # Column name with target variables
    y_col="target",
    weight_col=None,
    # If you want to resize your target image with specific size then put it here
    target_size=(32, 32),
    # If converting from rgb to grayscale put here.
    color_mode="rgb",
    classes=None,
    # If you have numpy 1D array of labels which is usually the case then use raw, there are many options given please check docs.
    class_mode="raw",
    batch_size=32,
    shuffle=True
)

現在轉換此數據集后,您可以直接輸入 keras model。

model = keras.models.Sequential()
# Make sure pixel size should remain the same.
model.add(keras.layers.Conv2D(28, (3,3), padding='same', input_shape=(32,32,3)))
model.add(keras.layers.Activation('relu'))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(512))
model.add(keras.layers.Activation('relu'))
model.add(keras.layers.Dense(1, activation='softmax'))

# Proper loss function is very important because categorical required multiple classes while the problem which I was facing has only 2 classes and it was coded as 0 and 1. So, use of proper loss function is creating different error, such as shape error. 
model.compile(optimizer='sgd', loss=keras.losses.BinaryCrossentropy(), metrics=["accuracy"])

model.fit_generator(generator=data, epochs=10, verbose=1)

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM