ValueError:无法为张量u'InputData / X:0'输入形状为((?,48,48,1)'的形状(48,48,1)的值

时间:2018-08-26 07:51:26

标签: numpy opencv tensorflow computer-vision face-recognition

self.network = input_data(shape=[None, SIZE_FACE, SIZE_FACE, 1])
self.network = conv_2d(self.network, 64, 5, activation='relu')
#self.network = local_response_normalization(self.network)
self.network = max_pool_2d(self.network, 3, strides=2)
self.network = conv_2d(self.network, 64, 5, activation='relu')
self.network = max_pool_2d(self.network, 3, strides=2)
self.network = conv_2d(self.network, 128, 4, activation='relu')
self.network = dropout(self.network, 0.3)
self.network = fully_connected(self.network, 3072,activation='relu')
    self.network = fully_connected(
        self.network, len(EMOTIONS), activation='softmax')
    self.network = regression(
        self.network,
        optimizer='momentum',
        loss='categorical_crossentropy'
    )
    self.model = tflearn.DNN(
        self.network,
        checkpoint_path=SAVE_DIRECTORY + '/emotion_recognition',
        max_checkpoints=1,
        tensorboard_verbose=2
    )
    self.load_model()
def predict(self, image):
    #if image is None:
     #   return None
    #image = image.reshape([-1, SIZE_FACE, SIZE_FACE, 1])
    return self.model.predict(image)


import cv2
import sys


import numpy as np

cascade_classifier = cv2.CascadeClassifier(CASC_PATH)


def brighten(data, b):
    datab = data * b
    return datab


def format_image(image):
    if len(image.shape) > 2 and image.shape[2] == 3:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    else:
        image = cv2.imdecode(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    faces = cascade_classifier.detectMultiScale(
        image,
        scaleFactor=1.3,
        minNeighbors=5
    )
    # None is we don't found an image
    if not len(faces) > 0:
        return None
    max_area_face = faces[0]
    for face in faces:
        if face[2] * face[3] > max_area_face[2] * max_area_face[3]:
            max_area_face = face
    # Chop image to face
    face = max_area_face
    image = image[face[1]:(face[1] + face[2]), face[0]:(face[0] + face[3])]
    # Resize image to network size
    try:
        image = cv2.resize(image, (SIZE_FACE, SIZE_FACE),
                           interpolation=cv2.INTER_CUBIC) / 255.
    except Exception:
        print("[+] Problem during resize")
        return None
    # cv2.imshow("Lol", image)
    # cv2.waitKey(0)
    image = image.reshape([-1, SIZE_FACE, SIZE_FACE, 1])
    print(image.shape)
    return image


# Load Model
network = EmotionRecognition()
network.build_network()

video_capture = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX

feelings_faces = []
for index, emotion in enumerate(EMOTIONS):
    feelings_faces.append(cv2.imread('./emojis/' + emotion + '.png', -1))

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    # Predict result with network
    result = network.predict(format_image(frame))

    # Draw face in frame
    # for (x,y,w,h) in faces:
    #   cv2.rectangle(frame, (x,y), (x+w,y+h), (255,0,0), 2)

    # Write results in frame
    if result is not None:
        for index, emotion in enumerate(EMOTIONS):
            cv2.putText(frame, emotion, (10, index * 20 + 20),
                        cv2.FONT_HERSHEY_PLAIN, 0.5, (0, 255, 0), 1)
            cv2.rectangle(frame, (130, index * 20 + 10), (130 +
                                                          int(result[0][index] * 100), (index + 1) * 20 + 4), (255, 0, 0), -1)

        face_image = feelings_faces[np.argmax(result[0])]

        # Ugly transparent fix

        for c in range(0, 3):
            frame[200:320, 10:130, c] = face_image[:, :, c] * \
                (face_image[:, :, 3] / 255.0) + frame[200:320,
                                                      10:130, c] * (1.0 - face_image[:, :, 3] / 255.0)
    # Display the resulting frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
> first i was working on tensorflow version 1.7 but now i upgraded it to 1.10 still getting error:-
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tflearn/initializations.py:119:UniformUnitScaling.init (from tensorflow.python.ops.init_ops) is deprecated and will be removed in a future version.Instructions for updating:Use 
     

tf.initializers.variance_scaling而不是distribution = uniform to   得到同等的行为。

     

警告:tensorflow:来自   /usr/local/lib/python3.6/dist-packages/tflearn/objectives.py:66:calling   使用keep_dims的reduce_sum(来自tensorflow.python.ops.math_ops)是   已弃用,并将在以后的版本中删除。   更新: keep_dims已过时,请改用keepdims

I had already reshaped using( image = image.reshape([-1, SIZE_FACE, SIZE_FACE, 1])
print(image.shape))and getting output(1,48,48,1)
     

,但是转到 model.predict时会引发错误:-

     

无法为张量u'InputData / X:0'输入shape(48,48,1)的值,   形状为'(?,48,48,1)'

using these-

absl-py==0.2.0
astor==0.6.2
bleach==1.5.0
gast==0.2.0
grpcio==1.11.0
html5lib==0.9999999
Keras==2.1.5
Markdown==2.6.11
numpy==1.14.2
opencv-python==3.4.0.12
pandas==0.22.0
Pillow==5.1.0
protobuf==3.5.2.post1
python-dateutil==2.7.2
pytz==2018.4
PyYAML==3.12
scikit-learn==0.19.1
scipy==1.0.1
six==1.11.0
sklearn==0.0
tensorboard==1.7.0
tensorflow==1.7.0
termcolor==1.1.0
tflearn==0.3.2
Werkzeug==0.14.1

0 个答案:

没有答案