keras ValueError:生成器的输出应为元组'(x,y,sample_weight)'或'(x,y)',找到:[[[[0.08627451 0.10980393 0.10980393]][[0.08627451 0.10980393 0.10980393]]这是一个很好的例子,

qfe3c7zg  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(90)

我是tensorflow 的初学者,在尝试迁移学习的时候,遇到了一个错误Value error,大家知道bug在哪里吗?这段代码和VGG16的迁移学习有关,基本上我只是创建了自己的MLP层,然后做了微调

import os
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense, Dropout, ZeroPadding2D
from tensorflow.keras.layers import Convolution2D, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import applications
import numpy as np
def get_length(Path, Pattern):
     # Pattern: name of the subdirectory
     Length = len(os.listdir(os.path.join(Path, Pattern)))
     return Length
train_data_dir = ''
validation_data_dir = ''
img_width, img_height = 224, 224
epochs = 150
batch_size = 8
LR = 0.00001
Len_C1_Train = get_length(train_data_dir,'AFF')
Len_C2_Train = get_length(train_data_dir,'NFF')
Len_C1_Val = get_length(validation_data_dir,'AFF')
Len_C2_Val = get_length(validation_data_dir,'NFF')
model = applications.VGG16(include_top=False, weights='imagenet')
datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = datagen.flow_from_directory(train_data_dir,
                                              target_size=(img_width, img_height),
                                              batch_size=batch_size,
                                              class_mode=None,
                                              shuffle=False)  
# Extracting the features from the loaded images
features_train = model.predict_generator(train_generator,
                                         (Len_C1_Train+Len_C2_Train) // batch_size, 

max_queue_size=1)
val_generator = datagen.flow_from_directory(validation_data_dir,
                                            target_size=(img_width, img_height),
                                            batch_size=batch_size,
                                            class_mode=None,
                                            shuffle=False)  
# Extracting the features from the loaded images
features_val = model.predict_generator(val_generator,
                                      (Len_C1_Val+Len_C2_Val) // batch_size, max_queue_size=1)        
    
train_data = features_train
train_labels = np.array([0] * int(Len_C1_Train) + [1] * int(Len_C2_Train))
validation_data = features_val
validation_labels = np.array([0] * int(Len_C1_Val) + [1] * int(Len_C2_Val))
#  Building the MLP model
model2=Sequential()
model2.add(Dense(128, activation='relu'))
model2.add(Dropout(0.5))
model2.add(Dense(1, activation='softmax'))
model_total = Sequential([model, model2])
model.trainable = False
model_total.compile(loss='binary_crossentropy', optimizer = Adam(lr = LR), metrics=['binary_accuracy'])
3j86kqsm

3j86kqsm1#

VGG16的输出形状为(batch_size, height, width, channels)。因此,在使用Dense图层之前,应先应用Flatten图层。请尝试以下操作:

model2=Sequential()
model2.add(Flatten())
model2.add(Dense(128, activation='relu'))
model2.add(Dropout(0.5))
model2.add(Dense(1, activation='softmax'))
model_total = Sequential([model, model2])

相关问题