【问题标题】:Problem with logits and labels size. Tensorflowlogits 和标签大小的问题。张量流
【发布时间】:2021-10-29 13:46:33
【问题描述】:

我尝试将顶层与基础模型分开训练。所有人都在使用model.predict_generator like 生成功能

bottleneck_features_train = model.predict_generator(
    train_generator, predict_size_train)
np.save(save_dir + 'bottleneck_features_train.npy', bottleneck_features_train)

train_data = np.load(mtx.save_dir + 'bottleneck_features_train.npy')

model.fit(train_data, ....
                    )

但现在我得到了巨大的数据集,无法将所有数据加载到内存中,所以我使用生成器flow_from_directory

def create_generator(root_path, batch_size):
datagen = ImageDataGenerator(rescale=1. / 255)
generator = datagen.flow_from_directory(
    root_path,
    target_size=(224, 224),
    batch_size=batch_size,
    class_mode="categorical",
    shuffle=True)

return generator
train_generator = create_generator(mtx.train_data_dir, mtx.batch_size)

model.fit(train_generator...

flow_from_directory 中的class_mode 是“分类”,损失函数too(categorical_crossentropy)

层数

model = Sequential()
model.add(Flatten(input_shape=(7, 7, 512)))
model.add(Dense(512, activation="relu"))

model.add(Dropout(0.7))

model.add(Dense(num_classes, activation='softmax'))

但是当我进行训练时,我得到了

logits 和标签必须是可广播的:logits_size=[24,32] labels_size=[4,32]

据我了解,图层中的形状或特征/标签的编码方式有问题。

更新 1:

flow_from_directory中的batch_size设置为1时它也可以工作。但准确度非常低。

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    试试

    model.add(Flatten(input_shape=(224,224,3)))
    

    【讨论】:

    • 好吧。它有效,但预测精度非常低。我解决了这个问题。因此,由于它是迁移学习,我们需要学习由预训练网络生成的预测,我们必须使用 (7,7,512) input_shape 制作输入层。因为 vgg16 生成的特征具有这种形状。因此,作为解决方案,我为功能制作了一个自定义生成器。这解决了在庞大数据集上训练的问题(实际上是由预训练模型生成的大量特征)
    猜你喜欢
    • 1970-01-01
    • 2016-04-03
    • 2017-08-16
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    相关资源
    最近更新 更多