只有size-1数组可以转换为Python标量,并设置具有序列错误的数组元素



我有一个总共1500张的数据集,每个人有500张人脸图像。我想通过CNN来预测人脸。我使用索引(mike.1.jpg(处理数据以获得名称。

看起来我的数组格式导致了错误,但不确定。这可能是CNN的参数或层吗?

以下是代码和错误。

# Generate dataset
def create_dataset():
face_classifier = cv2.CascadeClassifier("/Users/germplus/PycharmProjects"
"/MAIDS-Thesis-Project/haarcascade_frontalface_default.xml")
def image_cropped(image):
#         convert image from RGB to gray scale to reduce complexity
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Scale images with scaling factor eg: 1.3 and minimum neighbour eg:)
face = face_classifier.detectMultiScale(gray, 1.3, 5)
if face is ():
return None
#         crop the faces
for (x, y, w, h) in face:
cropped_face = image[y:y + h, x:x + w]
return cropped_face
#     connect to web or external camera
cam = cv2.VideoCapture(0)
#     Participant id
participant_image_name = participant
#   image id
image_id = 0
while True:
ret, frame = cam.read()
if image_cropped(frame) is not None:
image_id += 1
# resize face
face = cv2.resize(image_cropped(frame), (200, 200))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
# save images to file
file_path = "/Users/germplus/PycharmProjects/" 
"MAIDS-Thesis-Project/images/" 
+ str(participant_image_name) + '.' + str(image_id) + '.jpg'
cv2.imwrite(file_path, face)
#  font scale = 1
# thickness = 2
cv2.putText(face, str(image_id), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('Cropped face', face)
# stop taking samples if you press enter or if image samples are up to 1000
if cv2.waitKey(1) == 13 or int(image_id) == 500:
break
cam.release()
cv2.destroyAllWindows()
print("Sample images collection is completed.........")
def my_label(image_name):
name = image_name.split('.')[-3]
# names of participants in the research
if name == 'Xavi':
return np.array([1, 0, 0])
# return np.array([1, 0, 0, 0, 0, 0, 0])
elif name == 'mama_africa':
return np.array([0, 1, 0])
# return np.array([0, 1, 0, 0, 0, 0, 0])
elif name == 'Isaac':
return np.array([0, 0, 1])
# return np.array([0, 0, 1, 0, 0, 0, 0])
# elif name == Data_collection.participant:
#     return np.array([0, 0, 0, 1, 0, 0, 0])
# elif name == Data_collection.participant:
#     return np.array([0, 0, 0, 0, 1, 0, 0])
# elif name == Data_collection.participant:
#     return np.array([0, 0, 0, 0, 0, 1, 0])
# elif name == Data_collection.participant:
#     return np.array([0, 0, 0, 0, 0, 0, 1])

def my_data():
images = []
for img in tqdm(os.listdir("/Users/germplus/PycharmProjects/MAIDS-Thesis-Project/images")):
path = os.path.join("/Users/germplus/PycharmProjects/MAIDS-Thesis-Project/images", img)
img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img_data = cv2.resize(img_data, (50, 50))
images.append([np.array(img_data), my_label(img)])
shuffle(images)
return images

data = my_data()
# split data into train and testing
train = create_label.data[:1200]
test = create_label.data[1200:]
# x train in 0 index. -1 calculates the x-train number of train 50, 50 is the image shape.
# 1 is grayscale image
X_train = np.array([i[0] for i in train]).reshape(-1, 50, 50, 1)
print(f'X_train shape is {X_train.shape}')
# y train in 1 index -1 calculates the y-train number of train 50, 50 is the image shape
y_train = [i[1] for i in train]
X_test = np.array([i[0] for i in test]).reshape(-1, 50, 50, 1)
print(f'X_test shape is {X_test.shape}')
y_test = [i[1] for i in test]
print(X_train.ndim)
# DNN
# input_shape = input_data(shape=[50,50,1])
# model = tflearn.DNN
convnet = input_data(shape=[50,50,1])
convnet = conv_2d(convnet, 32, 5, activation='relu')
# 32 filters and stride=5 so that the filter will move 5 pixel or unit at a time
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 3, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate = 0.001, loss='categorical_crossentropy')
model = tflearn.DNN(convnet, tensorboard_verbose=1)
model.fit(X_train, y_train, n_epoch=12, validation_set=(X_test, y_test))

错误

Training samples: 1200
Validation samples: 300
--
2022-08-15 10:15:12.869457: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/germplus/PycharmProjects/MAIDS-Thesis-Project/model_fit.py", line 86, in <module>
model.fit(X_train, y_train, n_epoch=12, validation_set=(X_test, y_test) )
File "/Users/germplus/miniforge3/envs/mlp/lib/python3.8/site-packages/tflearn/models/dnn.py", line 196, in fit
self.trainer.fit(feed_dicts, val_feed_dicts=val_feed_dicts,
File "/Users/germplus/miniforge3/envs/mlp/lib/python3.8/site-packages/tflearn/helpers/trainer.py", line 341, in fit
snapshot = train_op._train(self.training_state.step,
File "/Users/germplus/miniforge3/envs/mlp/lib/python3.8/site-packages/tflearn/helpers/trainer.py", line 827, in _train
_, train_summ_str = self.session.run([self.train, self.summ_op],
File "/Users/germplus/miniforge3/envs/mlp/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 970, in run
result = self._run(None, fetches, feed_dict, options_ptr,
File "/Users/germplus/miniforge3/envs/mlp/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1163, in _run
np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
ValueError: setting an array element with a sequence.
Process finished with exit code 1

从您提供的代码中调试代码有点困难,原因如下:

  1. 我不确定堆栈跟踪中的行号如何与您的代码相对应。这是model_fit.py脚本的前122行吗
  2. 如果您能够清理代码并将其简化为运行代码所需的最少必要信息,那将有所帮助
  3. 似乎有一些缺失的代码可以解释这个错误。例如,我不确定您在第三段代码顶部引用的create_label对象来自哪里

格雷珀对你所得到的错误的回答是";最常见的是它来自于试图将NumPy数组转换成另一种格式";,例如:

import numpy as np
x = np.array([1.0,2.0,3.0])
np.int(x)
>>>TypeError: only size-1 arrays can be converted to Python scalars

StackExchange的回答还指出,当您正在使用的函数需要一个值,而您却将其传递给一个数组时,会引发该错误。

为了解决这个问题,我会仔细阅读您的代码,寻找对Numpy数组进行操作的地方(比如试图将浮点值更改为int或其他什么(,以及将Numpy数组传递给函数的地方。把所有东西都打印出来,这样你就可以确信它在做你认为的事情。

为了帮助自己编写和调试代码,我做了一件事,那就是启动一个Google Colab笔记本,测试简短的代码片段,以确保它们能达到我的预期。

顺便说一句,我没有使用NumPy或机器学习的大量经验,所以对这些输入持保留态度。

最新更新