0

I have a dataset of 1500 in total with 500 face images for each person. I want to pass it CNN to predict the faces. I processed the data using index ( mike.1.jpg) to get the names.

Looks like my array format is causing me the error but not sure. Could this be the CNN parameters or layers?

Below is the code and error.

# 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))


Error

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 Answer 1

0

It's a little hard to debug your code from what you've provided for a few reasons:

  1. I'm not sure how the line numbers in the stack trace correspond to your code. Are these the first ~122 lines of your model_fit.py script?
  2. If you could declutter your code and simplify it to the minimum necessary information required to run it, that would help.
  3. It seems like there is some missing code that might explain the error. E.g. I'm not sure where the create_label object that you reference at the top of the third chunk of code comes from.

The Grepper answer for the error you're getting says that "most often it comes from trying to convert a NumPy array to another format", e.g.:

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

This StackExchange answer also states that that error is raised when a function you are using expects a single value but you pass it an array instead.

To solve this, I would carefully go through your code looking for places where you do operations on Numpy arrays (like trying to change floats to ints or something) and places where you pass Numpy arrays to functions. Print everything out so that you're sure it's doing what you think it is.

One thing I do to help myself write and debug code is to launch a Google Colab notebook and test short snippets of code to make sure they are doing what I expect.

BTW, I don't have a ton of experience using NumPy or machine learning, so take this input with a grain of salt.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.