无法在 Keras 中使用 model.fit()

Not able to use the model.fit() in Keras

请帮我 运行 代码。这是行不通的。几天来我一直在努力解决这个问题。 model_final.fit() 也不起作用。为什么这对方法显示相同的错误。这几乎就是全部代码了。

编辑:添加了 train_generator 和 validation_generator。

img_width, img_height = 400, 400
train_data_dir = "data/train"
validation_data_dir = "data/validation"
nb_train_samples = 4125
nb_validation_samples = 466
batch_size = 16,
epochs = 5

output_num_classes = 2

K.set_image_data_format('channels_last')
model = applications.VGG19(weights="imagenet", include_top=False, input_shape=(img_width, img_height, 3))

model.summary()

x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation="relu")(x)
predictions = Dense(output_num_classes, activation="softmax")(x)

model_final = Model(inputs=model.input, outputs=predictions)
model_final.compile(loss="categorical_crossentropy", 
                    optimizer=optimizers.SGD(learning_rate=0.0001, momentum=0.9),
                    metrics=['accuracy'])
train_datagen = ImageDataGenerator(rescale=1./255,
                                   zoom_range=0.3,
                                   width_shift_range=0.3,
                                   height_shift_range=0.3,
                                   rotation_range=30,
                                   horizontal_flip=True,
                                   fill_mode='nearest')

train_generator = train_datagen.flow_from_directory(train_data_dir,
                                                    target_size=(img_height, img_width),
                                                    batch_size=batch_size,
                                                    class_mode='categorical')

validation_datagen = ImageDataGenerator(rescale=1./255,
                                   zoom_range=0.3,
                                   width_shift_range=0.3,
                                   height_shift_range=0.3,
                                   rotation_range=30,
                                   horizontal_flip=True,
                                   fill_mode='nearest')

validation_generator = validation_datagen.flow_from_directory(validation_data_dir,
                                                              target_size=(img_height, img_width),
                                                              batch_size = batch_size,
                                                              class_mode='categorical')

以上代码为拟合前

model_final.fit_generator(train_generator, 
                          steps_per_epoch=nb_train_samples,
                          epochs = epochs,
                          validation_data = validation_generator,
                          validation_steps = nb_validation_samples)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:1844: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
  warnings.warn('`Model.fit_generator` is deprecated and '
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-87-b195f52ce8fb> in <module>()
      3                           epochs = epochs,
      4                           validation_data = validation_generator,
----> 5                           validation_steps = nb_validation_samples)

/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/iterator.py in __len__(self)
     66 
     67     def __len__(self):
---> 68         return (self.n + self.batch_size - 1) // self.batch_size  # round up
     69 
     70     def on_epoch_end(self):

TypeError: unsupported operand type(s) for +: 'int' and 'tuple' ```

我能够使用示例代码重现您的问题,如下所示

import tensorflow as tf
from tensorflow.keras.applications import ResNet50
import os
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential

from google.colab import drive
drive.mount('/content/drive')

train_dir  = '/content/drive/My Drive/Dogs_Vs_Cats/train'
test_dir = '/content/drive/My Drive/Dogs_Vs_Cats/test'

img_width, img_height = 224, 224
input_shape = (img_width, img_height, 3)

epochs = 8
batch_size = 32,

train_datagen = ImageDataGenerator(
    rescale = 1. /255,
    horizontal_flip = True)

test_datagen = ImageDataGenerator(
    rescale = 1. /255)

train_data = train_datagen.flow_from_directory(
    train_dir,
    target_size = (img_width, img_height),
    batch_size = batch_size,
    class_mode = 'binary')

test_data = test_datagen.flow_from_directory(
    test_dir,
    target_size = (img_width, img_height),
    class_mode = 'binary')


Conv_Base = ResNet50(include_top = False, weights = 'imagenet', input_shape = input_shape)

model = Sequential()
model.add(Conv_Base)
model.add(Flatten())
model.add(Dense(units = 256, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 1, activation = 'sigmoid'))
model.summary()

model.compile(loss = 'binary_crossentropy',
            optimizer = 'Adam',
            metrics = [tf.keras.metrics.Precision(), tf.keras.metrics.Recall()])

model.fit(
        train_data,
        steps_per_epoch = 10,
        epochs = 2,
        validation_data = test_data,
        verbose = 1,
        validation_steps = 32)

输出:

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
Found 2000 images belonging to 2 classes.
Found 1018 images belonging to 2 classes.
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
resnet50 (Functional)        (None, 7, 7, 2048)        23587712  
_________________________________________________________________
flatten (Flatten)            (None, 100352)            0         
_________________________________________________________________
dense (Dense)                (None, 256)               25690368  
_________________________________________________________________
dropout (Dropout)            (None, 256)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 257       
=================================================================
Total params: 49,278,337
Trainable params: 49,225,217
Non-trainable params: 53,120
_________________________________________________________________
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-5a5644117355> in <module>()
     60         validation_data = test_data,
     61         verbose = 1,
---> 62         validation_steps = 32)

3 frames
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/iterator.py in __len__(self)
     66 
     67     def __len__(self):
---> 68         return (self.n + self.batch_size - 1) // self.batch_size  # round up
     69 
     70     def on_epoch_end(self):

TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

解决方法:

正如@Dr 所建议的那样。史努比,这个问题是由于 commabatch_size 定义之后引起的。它将 integer 转换为 tuple 并且不受支持。

batch_size = 32, 更改为 batch_size = 32 已解决问题。

工作代码如下所示

import tensorflow as tf
from tensorflow.keras.applications import ResNet50
import os
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential

from google.colab import drive
drive.mount('/content/drive')

train_dir  = '/content/drive/My Drive/Dogs_Vs_Cats/train'
test_dir = '/content/drive/My Drive/Dogs_Vs_Cats/test'

img_width, img_height = 224, 224
input_shape = (img_width, img_height, 3)

epochs = 8
batch_size = 32    # removed comma


train_datagen = ImageDataGenerator(
    rescale = 1. /255,
    horizontal_flip = True)

test_datagen = ImageDataGenerator(
    rescale = 1. /255)

train_data = train_datagen.flow_from_directory(
    train_dir,
    target_size = (img_width, img_height),
    batch_size = batch_size,
    class_mode = 'binary')

test_data = test_datagen.flow_from_directory(
    test_dir,
    target_size = (img_width, img_height),
    class_mode = 'binary')


Conv_Base = ResNet50(include_top = False, weights = 'imagenet', input_shape = input_shape)

model = Sequential()
model.add(Conv_Base)
model.add(Flatten())
model.add(Dense(units = 256, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 1, activation = 'sigmoid'))
model.summary()

model.compile(loss = 'binary_crossentropy',
            optimizer = 'Adam',
            metrics = [tf.keras.metrics.Precision(), tf.keras.metrics.Recall()])

model.fit(
        train_data,
        steps_per_epoch = 10,
        epochs = 2,
        validation_data = test_data,
        verbose = 1,
        validation_steps = 32)

输出:

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
Found 2000 images belonging to 2 classes.
Found 1018 images belonging to 2 classes.
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
resnet50 (Functional)        (None, 7, 7, 2048)        23587712  
_________________________________________________________________
flatten_1 (Flatten)          (None, 100352)            0         
_________________________________________________________________
dense_2 (Dense)              (None, 256)               25690368  
_________________________________________________________________
dropout_1 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 1)                 257       
=================================================================
Total params: 49,278,337
Trainable params: 49,225,217
Non-trainable params: 53,120
_________________________________________________________________
Epoch 1/2
10/10 [==============================] - 107s 10s/step - loss: 9.5302 - precision_1: 0.6096 - recall_1: 0.6485 - val_loss: 14405.0479 - val_precision_1: 0.0000e+00 - val_recall_1: 0.0000e+00
Epoch 2/2
10/10 [==============================] - 68s 7s/step - loss: 2.2295 - precision_1: 0.8191 - recall_1: 0.8799 - val_loss: 3977.1418 - val_precision_1: 0.4912 - val_recall_1: 1.0000
<tensorflow.python.keras.callbacks.History at 0x7fbba005cc50>