aditya raj chopra
aditya raj chopra

Reputation: 92

How to change shape of 3 Dimensional picture grayscale to 4 Dimensional

So I am doing face_emotion recognition. To do this I am using this kaggle scirpt and running on kaggle GPUS, here is the link Kaggle Face Emotion Detection. I run the exact same, and the shape I get is: (27345,48,48,1). The 27345 maybe a off a bit, but it's like this, and I understand the 48,48,1 as being a picture 48x48 pixels wide grayscale, and I can get that picture easily. The problem that persists is that the tensor is, or the shape of the layer, it's 4 DIM, and I have a 3Dim, how do I fix this, will having (1,48,48,1) work? If yes then how to get this type of editing to numpy shape to this. I have the face extracted from face_recognition library in Python. I am using keras, and this is my so far script to detect emotion:

import face_recognition
import cv2
import tensorflow as tf
import numpy as np

image = cv2.imread("cv2IMG.png")
image1 = cv2.imread("new1.jpg")
face_locations = face_recognition.face_locations(image)
top, right, bottom, left = face_locations[0]
face_image = image[top:bottom, left:right]

encoding_1 = face_recognition.face_encodings(image)[0]

encoding_2 = face_recognition.face_encodings(image1)[1]

results = face_recognition.compare_faces([encoding_1], encoding_2,tolerance=0.50)
print(results)

face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2GRAY)
face_image = cv2.resize(face_image, (48,48))
model = tf.keras.models.load_model("./facial_1.h5")
predicted_class = np.argmax(model.predict(np.expand_dims(face_image, axis=4)))
emotions = ['Angry', 'Fear', 'Happy',
           'Sad', 'Surprise', 'Neutral']
print(predicted_class, emotions[predicted_class])

And the error I get:

2020-06-12 00:38:05.017598: 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-06-12 00:38:05.033490: 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.                                                                                   [True]                                                                                                         2020-06-12 00:38:16.949795: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll                                                                                                      2020-06-12 00:38:17.031755: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:    pciBusID: 0000:01:00.0 name: GeForce 410M computeCapability: 2.1                                                        coreClock: 1.147GHz coreCount: 1 deviceMemorySize: 512.00MiB deviceMemoryBandwidth: 11.92GiB/s                          2020-06-12 00:38:17.051462: 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-06-12 00:38:17.064299: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found                                                                2020-06-12 00:38:17.077456: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found                                                                  2020-06-12 00:38:17.093862: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found                                                                2020-06-12 00:38:17.111774: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found                                                            2020-06-12 00:38:17.130609: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found                                                            2020-06-12 00:38:17.149657: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found                                                                    2020-06-12 00:38:17.166646: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1592] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.   Skipping registering GPU devices...                                                                                     2020-06-12 00:38:17.206674: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:                                                                                            2020-06-12 00:38:17.221822: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102]                                    Traceback (most recent call last):                                                                                        File "findfaces.py", line 22, in <module>                                                                                 predicted_class = np.argmax(model.predict(np.expand_dims(face_image, axis=4)))                                        File "<__array_function__ internals>", line 6, in expand_dims                                                           File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\shape_base.py", line 597, in expand_dims                       axis = normalize_axis_tuple(axis, out_ndim)                                                                           File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\numeric.py", line 1327, in normalize_axis_tuple               axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])                                                File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\numeric.py", line 1327, in <listcomp>                         axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])                                              numpy.AxisError: axis 4 is out of bounds for array of dimension 3

This is the last line, and probably the one that matters, I have copied the kaggle code to my own notebook, so If any changes in training model, I can do. Also,If you want the whole error and warning and stuff, Please tell

EDIT: I tried doing:

predicted_class = np.argmax(model.predict(np.expand_dims(face_image[None, Ellipsis], axis=4)))

On the predicting line, and it gave me the following error:

2020-06-12 00:30:06.668284: 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-06-12 00:30:06.687940: 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.                                                                                   [True]                                                                                                                  2020-06-12 00:30:28.895148: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll                                                                                                      2020-06-12 00:30:29.026953: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:    pciBusID: 0000:01:00.0 name: GeForce 410M computeCapability: 2.1                                                        coreClock: 1.147GHz coreCount: 1 deviceMemorySize: 512.00MiB deviceMemoryBandwidth: 11.92GiB/s                          2020-06-12 00:30:29.057783: 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-06-12 00:30:29.083861: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found                                                                2020-06-12 00:30:29.100352: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found                                                                  2020-06-12 00:30:29.117565: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found                                                                2020-06-12 00:30:29.133633: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found                                                            2020-06-12 00:30:29.148899: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found                                                            2020-06-12 00:30:29.163812: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found                                                                    2020-06-12 00:30:29.174659: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1592] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.   Skipping registering GPU devices...                                                                                     2020-06-12 00:30:29.203522: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:                                                                                            2020-06-12 00:30:29.215529: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102]                                    Traceback (most recent call last):                                                                                        File "findfaces.py", line 22, in <module>                                                                                 predicted_class = np.argmax(model.predict(np.expand_dims(face_image[None, Ellipsis], axis=4)))                        File "<__array_function__ internals>", line 6, in expand_dims                                                           File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\shape_base.py", line 597, in expand_dims                       axis = normalize_axis_tuple(axis, out_ndim)                                                                           File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\numeric.py", line 1327, in normalize_axis_tuple               axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])                                                File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\numeric.py", line 1327, in <listcomp>                         axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])                                              numpy.AxisError: axis 4 is out of bounds for array of dimension 4   

Upvotes: 1

Views: 276

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36624

Try this:

predicted_class = np.argmax(model.predict(np.expand_dims(face_image[None, 
    Ellipsis], axis=-1)))

It will add a first dimension to your numpy array and it should work. If that doesn't work give me the shape of face_image right before this line.

Upvotes: 1

Related Questions