簡體   English   中英

擬合model時ImageDataGenerator的形狀問題

[英]Shape problem of ImageDataGenerator when fitting the model

我想問你如何解決這個問題。 我有兩個圖片文件夾,一個作為火車組,另一個作為驗證。 我所做的是使用 ImageDataGenerator:

train_datagen = ImageDataGenerator(
        rescale=1./255,
        rotation_range=40, #integer degree range for random rotations
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)


# to have a better performance in accuracy measure I just rescale the test
test_datagen = ImageDataGenerator(rescale=1./255)

# apply ImageDataGenerator on requierd folde 
train_generator = train_datagen.flow_from_directory(
        '/content/drive/MyDrive/NN_HW2/training/training/',  # this is the target directory
        target_size=(150, 150),  # all images will be resized to 150x150
        batch_size=batch_size,
        class_mode='categorical')  # since I am in multicalss calssification 

# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
        '/content/drive/MyDrive/NN_HW2/validation/validation/',
        target_size=(150, 150),
        batch_size=batch_size,
        class_mode='categorical')

然后,我像這樣定義了我的 model:

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('softmax'))


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

因此,當我嘗試安裝 model 時,colab 向我提出以下錯誤:

model.fit_generator(
        train_generator,
        steps_per_epoch=1000//batch_size,
        epochs=25,
        validation_data=validation_generator,
        steps_per_epoch=500//batch_size
        )
Matrix size-incompatible: In[0]: [16,10], In[1]: [64,1]
     [[node gradient_tape/sequential_3/dense_7/MatMul (defined at <ipython-input-22-f61b6c381681>:7) ]] [Op:__inference_train_function_3405]

Function call stack:
train_function

謝謝你的時間

在下面的代碼中

model.fit_generator(
        train_generator,
        steps_per_epoch=1000//batch_size,
        epochs=25,
        validation_data=validation_generator,
        steps_per_epoch=500//batch_size
        )

您定義了 steps_per_epoch 兩次。 您要做的是保留第一個,刪除第二個並將其替換為

 validation_steps=500//batch_size 

或者,由於您使用的是生成器,您可以保留 steps_per_epoch=None 和 validation_steps=None。 model.fit_generator 正在貶值,所以只需使用 model.fit。

暫無
暫無

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

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