Reputation: 463
Hello StackOverFlow Team: I built a model based on (Vgg_Face_Model) with weights loaded (vgg_face_weights.h5). Note that I use tensorflow-gpu = 2.1.0 , and keras=2.3.1 , with Anaconda 3 create it as interpreter and used with pycharm But the code shows an error in the part :
input_descriptor = [model.predict(face), img]
The code is:
def face_recognizer(face, db_descriptors):
# face = cv2.imread(img)
# face = cv2.resize(face, (IMG_Size, IMG_Size))
t0 = time.perf_counter()
face = np.array(face).reshape(-1, IMG_Size, IMG_Size, 3)
###### here error #################################
input_descriptor = [model.predict(face), img]
###################################################
K_nn_result = K_nn_Classifier(input_descriptor[0], db_descriptors, 5)
input_result = Knn_Distance_Score(K_nn_result)
if input_result[0] <= 10:
identity = 'stranger'
else:
identity = input_result[1]
# print('Done in',time.perf_counter()-t0)
return input_result, identity
def PrepareModels(self):
global mpFaceDetection, FaceDetector, model
mpFaceDetection = mp.solutions.face_detection
FaceDetector = mpFaceDetection.FaceDetection()
model = loadModel()
Model is:
import os
from pathlib import Path
# from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.models import Model, Sequential, load_model
from tensorflow.keras.layers import Input, Convolution2D, ZeroPadding2D, MaxPooling2D, Flatten, Dense, Dropout, \
Activation
import gdown
# ---------------------------------------
def Vgg_Face_Model():
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(224, 224, 3)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Convolution2D(4096, (7, 7), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(4096, (1, 1), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(2622, (1, 1)))
model.add(Flatten())
model.add(Activation('softmax'))
return model
def loadModel():
model = Vgg_Face_Model()
# -----------------------------------
home = str(Path.home())
if os.path.isfile(home + '/.deepface/weights/vgg_face_weights.h5') != True:
print("vgg_face_weights.h5 will be downloaded...")
url = 'https://drive.google.com/uc?id=1CPSeum3HpopfomUEK1gybeuIVoeJT_Eo'
output = home + '/.deepface/weights/vgg_face_weights.h5'
gdown.download(url, output, quiet=False)
# -----------------------------------
model.load_weights(home + '/.deepface/weights/vgg_face_weights.h5')
# -----------------------------------
# TO-DO: why?
vgg_model_descriptor = Model(inputs=model.layers[0].input, outputs=model.layers[-2].output)
return vgg_model_descriptor
# model = loadModel()
output:
Tensor Tensor("flatten/Reshape:0", shape=(?, 2622), dtype=float32) is not an element of this graph.'
Upvotes: 0
Views: 505
Reputation: 28
from tensorflow.python.keras.backend import set_session
sess = tf.Session()
#This is a global session and graph
graph = tf.get_default_graph()
set_session(sess)
#now where you are calling the model
global sess
global graph
with graph.as_default():
set_session(sess)
input_descriptor = [model.predict(face), img]
Upvotes: 1