当我拟合模型时,我得到这个错误:ValueError: logits和标签必须具有相同的形状((None, 1000) vs (None, 1))。我不知道如何修理它。下面是生成它的代码:
Directory = 'dataset'
Classes = ['with_mask', 'without_mask']
train_data =[]
def make_training_data():
for category in Classes:
path = os.path.join(Directory, category)
class_nums = Classes.index(category) #transforms with_mask and without_mask into 1 and 0
for img in os.listdir(path):
try:
img_path = cv2.imread(os.path.join(path, img))
res_img = cv2.resize(img_path, (224, 224))
train_data.append([res_img, class_nums])
except Exception as e:
pass
#shuffle data
import random
random.shuffle(train_data)
#create dependent and independent variables
x= [] #data
y = [] #labels
for features, labels in train_data:
x.append(features)
y.append(labels)
#reshaping data into numpy arrays that the models understand
x=np.array(x).reshape(-1, 224, 224, 3)
Y = np.array(y)
#normalizing the data
X =x/255.0
model = tf.keras.applications.mobilenet.MobileNet()
base_input = model.layers[0].input
base_output = model.layers[-4].output
#adding other layers
flat_layer = layers.Flatten()(base_output)
final_output =layers.Dense(1)(flat_layer) #output is either 1 or 0
final_output= layers.Activation('sigmoid')(final_output)
new_model = keras.Model(inputs =base_input, outputs = final_output)
为您的模型试试
base_model= tf.keras.applications.mobilenet.MobileNet( include_top=False, input_shape=(224,224,3), pooling='max', weights='imagenet',dropout=.4)
x=base_model.output
final_output=layers.Dense(1, activation='sigmoid')(x)
new_model = keras.Model(inputs =base_model.input, outputs = final_output)
new_model.compile(Adam(lr=.001), loss='tf.keras.losses.BinaryCrossentropy', metrics=['accuracy'])