import osimport numpy as npfrom keras.preprocessing.image import ImageDataGeneratorfrom keras.applications import Xception, VGG16, ResNet50conv_base = VGG16(weights='imagenet',include_top=False,input_shape=(224, 224, 3))base_dir = 'NewDCDatatset'train_dir = os.path.join(base_dir, 'Train')validation_dir = os.path.join(base_dir, 'Validation')test_dir = os.path.join(base_dir, 'Test')datagen = ImageDataGenerator(rescale=1./255)batch_size = 20def extract_features(directory, sample_count): features = np.zeros(shape=(sample_count, 7 , 7 , 512)) labels = np.zeros(shape=(sample_count)) generator = datagen.flow_from_directory(directory,target_size=(224, 224),batch_size=batch_size,class_mode='categorical') i = 0 for inputs_batch, labels_batch in generator: features_batch = conv_base.predict(inputs_batch) features[i * batch_size : (i + 1) * batch_size] = features_batch labels[i * batch_size : (i + 1) * batch_size] = labels_batch i += 1 if i * batch_size >= sample_count: break return features, labelstrain_features, train_labels = extract_features(train_dir, 9900*2)validation_features, validation_labels = extract_features(validation_dir, 1300*2)test_features, test_labels = extract_features(test_dir, 2600)train_features = np.reshape(train_features, (9900*2, 7 * 7 * 512))validation_features = np.reshape(validation_features, (2600, 7 * 7 * 512))test_features = np.reshape(test_features, (2600, 7 * 7 * 512))from keras import modelsfrom keras import layersfrom keras import optimizersmodel = models.Sequential()model.add(layers.Dense(256, activation='relu', input_dim=7 * 7 * 512))model.add(layers.Dropout(0.5))model.add(layers.Dense(2, activation='softmax'))model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['acc'])history = model.fit(train_features, train_labels,epochs=3,batch_size=50,shuffle=True)print(model.evaluate(test_features,test_labels))model.save('TLFACE.h5')
您好,这是我的代码,我遇到了上面的错误。我无法理解应该如何将其形状从 (20) 改为 (20,2)。实际上,我之前在进行二分类任务,现在我想进行多分类任务,但无法解决这个问题。任何帮助都将不胜感激,谢谢。以下是错误的详细信息。
"C:\Program Files\Python37\python.exe" C:/Users/SIBAU/Downloads/face-recognition-with-deep-learning-python-master/extract_text.pyUsing TensorFlow backend.2020-05-28 14:46:37.336572: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found2020-05-28 14:46:37.376451: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.2020-05-28 14:47:08.730218: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found2020-05-28 14:47:08.730502: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: UNKNOWN ERROR (303)2020-05-28 14:47:08.909845: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: BILAL-LAPTOP2020-05-28 14:47:08.910341: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: BILAL-LAPTOP2020-05-28 14:47:08.932583: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2Found 80 images belonging to 2 classes.Traceback (most recent call last): File "C:/Users/SIBAU/Downloads/face-recognition-with-deep-learning-python-master/extract_text.py", line 31, in <module> train_features, train_labels = extract_features(train_dir, 9900*2) File "C:/Users/SIBAU/Downloads/face-recognition-with-deep-learning-python-master/extract_text.py", line 25, in extract_features labels[i * batch_size : (i + 1) * batch_size] = labels_batchValueError: could not broadcast input array from shape (20,2) into shape (20)Process finished with exit code 1
回答:
如果您在进行多类分类(每个输入一个答案,答案可能是n种可能性之一),那么我认为可以通过以下方式解决问题:
generator = datagen.flow_from_directory(directory,target_size=(224, 224),batch_size=batch_size,class_mode='sparse')
根据文档,“categorical”是用于二维独热编码标签的,“binary”是用于一维二进制标签的,“sparse”是用于一维整数标签的。由于您不想要多标签分类,而是多类分类,请使用sparse。sparse的一维是批次维度,而独热编码的二维则是一个维度用于批次,另一个维度用于一组标签。
如果您实际上在进行多标签分类(每个输入有多个输出类,比如在一个图像中标记动物类型和水果类型),那么您可能需要:
labels = np.zeros(shape=(sample_count),2)
这允许每个输入有2个标签(在这种情况下)。