![](/img/trans.png)
[英]TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape'
[英]AttributeError: 'DirectoryIterator' object has no attribute 'shape' Tensorflow CNN
我正在嘗試使用fit_generator
來安裝 model。 一切正常,但它拋出了上面的錯誤。 我正在使用 Keras 2.2.4 和 TF 2.0.0。 以前的所有其他 CNN 都可以工作,但它不喜歡圖像生成器。
# import relevant functions
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D
# rotate, and shift data
image_gen = ImageDataGenerator(rotation_range=20,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=True,
fill_mode='nearest')
# build the model
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3,3),input_shape=image_shape, activation='relu',))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2D(filters=64, kernel_size=(3,3),input_shape=image_shape, activation='relu',))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2D(filters=64, kernel_size=(3,3),input_shape=image_shape, activation='relu',))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# set up early stopping
early_stopping = EarlyStopping(monitor='val_loss', patience=2)
batch_size=16
# build train and test generators
train_image_gen = image_gen.flow_from_directory(train_path,
target_size = image_shape[:2],
color_mode='rgb',
batch_size=batch_size,
class_mode='binary')
# don't shuffle test data
test_image_gen = image_gen.flow_from_directory(test_path,
target_size = image_shape[:2],
color_mode='rgb',
batch_size=batch_size,
class_mode='binary',
shuffle=False)
# fit on the generators
results = model.fit_generator(train_image_gen, epochs=20,
validation_data=test_image_gen,
callbacks=[early_stopping])
從我筆記本的快照中,除了“model.fit_generator”步驟之外,一切正常。
當我運行最后一步時,我得到了錯誤。
train_image_gen
和test_image_gen
object 似乎是正確的,正確引用了二進制圖像分類問題的目錄。
目錄也設置正確:
我已經重新運行它,它似乎與 PlaidML 庫和生成器有問題。
在做任何其他工作之前,我的第一個導入如下:
import plaidml.keras
plaidml.keras.install_backend()
from keras import backend as K
這是一個允許使用 GPU 的庫,並大大加快了我使用過的所有其他 CNN 的速度。 由於某種原因,它不適用於生成器作為輸入。 這就是區別。 否則,plaidML 庫允許人們非常輕松地使用他們的 AMD 或 Nvidia GPU。
這是我第一次遇到圖書館的問題。
有興趣的可以看一下鏈接:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.