【问题标题】:RESNET50 Input to reshape is a tensor with 1638400 values, but requires a multiple of 25088RESNET50 重塑的输入是一个具有 1638400 个值的张量,但需要 25088 的倍数
【发布时间】:2021-05-03 22:22:20
【问题描述】:

我有一个包含 8 个类别的 2513 张图像的数据集,我想在其上微调 ResNet50。这是我的代码:

import keras
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.layers import Dense, Activation, Reshape, Conv2D, Flatten, GlobalAveragePooling2D, Dropout
import numpy as np
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import SGD, Adam, Nadam

DATA_DIR = 'data/'

train_datagen = ImageDataGenerator(
    #rescale=1./255,
    #shear_range=0.2,
    #zoom_range=0.2,
    #horizontal_flip=True,
    validation_split=0.3
    )

train_generator = train_datagen.flow_from_directory(DATA_DIR,
    batch_size=50,
    class_mode='categorical',
    subset='training')

validation_generator = train_datagen.flow_from_directory(
    DATA_DIR, # same directory as training data
    batch_size=50,
    class_mode='categorical',
    subset='validation') # set as validation data

#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=33)



base_model = ResNet50(weights='imagenet', include_top=True)

head_model = base_model.get_layer("conv5_block1_1_conv").output
    
head_model = Dense(512, activation="relu")(head_model)
head_model = Dropout(0.5)(head_model)
head_model = Flatten()(head_model)
#base_out = Reshape((25088,))(base_out)
head_model = Dense(1, activation="sigmoid")(head_model)
# place the head FC model on top of the base model (this will become
# the actual model we will train)
model = Model(inputs=base_model.input, outputs=head_model)
model.summary()
# loop over all layers in the base model and freeze them so they will
# *not* be updated during the first training process
for layer in base_model.layers:
    layer.trainable = False
    
    
# sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)
adam = Adam(lr=0.001)
model.compile(optimizer= adam, loss='categorical_crossentropy', metrics=['accuracy'])

model.fit_generator(
    train_generator,
    steps_per_epoch = train_generator.samples // 32,
    validation_data = validation_generator, 
    validation_steps = validation_generator.samples // 32,
    epochs = 100)

model.save("asd.h5")

但是运行它会抛出这个错误:

InvalidArgumentError:reshape 的输入是一个具有 1638400 个值的张量,但请求的形状需要 25088 的倍数 [[node model_8/flatten_7/Reshape(定义在..)

我必须做些什么来修复它?

【问题讨论】:

    标签: python tensorflow machine-learning keras deep-learning


    【解决方案1】:

    您似乎需要考虑或确保几件事。首先,您使用其weightinclude_top 参数加载ResNet50,但没有定义input_shape,然后它的默认值为(224, 224, 3),同时,您定义你的生成器在flow_from_directory,你也没有设置image_sizeby default它设置(256, 256)color_mode = rgb。因此,您可能需要查看输入形状不匹配问题。 Check 这个也是。

    其次,正如您提到的,您有 8 个类可以对模型进行分类并将损失函数设置为 categorical_crossentropy,那么您的最后一层应该更像这样:

    # head_model = Dense(1, activation="sigmoid")(head_model) # No
    head_model = Dense(8, activation="softmax")(head_model)   # Yes
    

    在我们确保这些细节之后,模型应该会按预期运行。

    from tensorflow.keras.applications import ResNet50
    from tensorflow.keras.layers import Dense, Dropout, Flatten
    from tensorflow.keras import Model 
    
    base_model = ResNet50(weights='imagenet', include_top=True)
    head_model = base_model.get_layer("conv5_block1_1_conv").output
    head_model = Dense(512, activation="relu")(head_model)
    head_model = Dropout(0.5)(head_model)
    head_model = Flatten()(head_model)
    head_model = Dense(8, activation="softmax")(head_model)
    model = Model(inputs=base_model.input, outputs=head_model)
    
    import numpy as np 
    img = np.random.randint(0, 255, size=(2, 224, 224, 3))
    model.compile(optimizer= 'adam', loss='categorical_crossentropy', 
                   metrics=['accuracy'])
    pred = model.predict(img)
    pred.shape
    (2, 8)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多