import os
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.applications import Xception, VGG16, ResNet50
conv_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 = 20
def 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, labels
train_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 models
from keras import layers
from keras import optimizers
model = 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.py
Using 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 found
2020-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 found
2020-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-LAPTOP
2020-05-28 14:47:08.910341: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: BILAL-LAPTOP
2020-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: AVX2
Found 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_batch
ValueError: could not broadcast input array from shape (20,2) into shape (20)
Process finished with exit code 1