PyImageSearch / Deep-Learning-for-Computer-Vision-with-Python

2 stars 1 forks source link

why I have this error "The truth value of an array with more than one element is ambiguous" when I start learning my neural network? #27

Closed BENHABI13 closed 1 year ago

BENHABI13 commented 1 year ago

I'm trying to detect the smile by neural networks in python but when I launch the learning of the network I get this error in return "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"

and this is my code in python:

data = [] labels = [] path=('/content/drive/MyDrive/smiles detection') for imagePath in sorted(list(paths.list_images(path))):

for imagePath in sorted('/content/drive/MyDrive/smiles detection/positives)'):

load the image, pre-process it, and store it in the data list

image = cv2.imread(imagePath)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = imutils.resize(image, width=28)
image = img_to_array(image)
data.append(image)

extract the class label from the image path and update the

labels list

label = imagePath.split(os.path.sep)[-3]
label = "smiling" if label == "positives" else "not_smiling"
labels.append(label)

data = np.array(data, dtype="float") / 255.0 labels = np.array(labels) le = LabelEncoder().fit(labels) labels = np_utils.to_categorical(le.transform(labels), 2)

classTotals = labels.sum(axis=0) classWeight = classTotals.max() / classTotals

(trainX, testX, trainY, testY) = train_test_split(data,labels, test_size=0.20, stratify=labels, random_state=42)

print("[INFO] compiling model...") model.compile(loss="binary_crossentropy", optimizer="adam",metrics=["accuracy"])

print("[INFO] training network...") H = model.fit((trainX, trainY), validation_data=(testX, testY),class_weight=classWeight, batch_size=64, epochs=15, verbose=1)

vikaskookna commented 1 year ago

Hi @BENHABI13, can you please try using

from tensorflow.keras.utils import to_categorical
labels = to_categorical(le.transform(labels), 2)

in place of

labels = np_utils.to_categorical(le.transform(labels), 2)
BENHABI13 commented 1 year ago

Hi @ vikaskookna , I have tested your solution but it generates the same error : ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() have a nice da

vikaskookna commented 1 year ago

Hi @BENHABI13, I think you're not using the right versions for scikit-learn or TensorFlow. Are you a member of PyImageSearch University, if not I would suggest joining for full support on tutorial codes and get pre-configured jupyter notebooks for all tutorials https://pyimagesearch.com/pyimagesearch-university/