定义初始化():
# These are set to the default names from exported models, update as needed.
ageModelFilename = "models/age/model.pb"
genderModelFilename = "models/gender/model.pb"
ageGraphDef = tf.compat.v1.GraphDef()
genderGraphDef = tf.compat.v1.GraphDef()
# Import the TF graph
try:
with tf.io.gfile.GFile(ageModelFilename, 'rb') as f:
ageGraphDef.ParseFromString(f.read())
ageGraph = tf.import_graph_def(ageGraphDef, name='age')
with tf.io.gfile.GFile(genderModelFilename, 'rb') as f:
genderGraphDef.ParseFromString(f.read())
genderGraph = tf.import_graph_def(genderGraphDef, name='gender')
except Exception as e:
print(e)
# # Get the input size of the model
with tf.compat.v1.Session(graph=ageGraph) as ageSess:
ageInputTensorshape = ageSess.graph.get_tensor_by_name('Placeholder:0').shape.as_list()
ageNetworkInputSize = ageInputTensorshape[1]
with tf.compat.v1.Session(graph=genderGraph) as genderSess:
genderInputTensorshape = genderSess.graph.get_tensor_by_name('Placeholder:0').shape.as_list()
genderNetworkInputSize = genderInputTensorshape[1]
return ageGraph, ageCategories, ageNetworkInputSize, genderGraph, genders, genderNetworkInputSize
def main(ageGraph、ageCategories、ageNetworkInputSize、genderGraph、genders、genderNetworkInputSize):
imagesPath = 'test'
imageFiles = [glob.glob(imagesPath + '/' + e) for e in ['*.jpg', '*.png']]
flatListImages = [item for sublist in imageFiles for item in sublist]
for imageFile in flatListImages:
try:
image = Image.open(imageFile)
# I do some image pre-processing
augmentedImage = preprocess(image, ageNetworkInputSize, ageNetworkInputSize)
# These names are part of the model and cannot be changed.
outputLayer = 'loss:0'
inputNode = 'Placeholder:0'
try:
with tf.compat.v1.Session(graph=ageGraph) as ageSess:
ageProbTensor = ageSess.graph.get_tensor_by_name('age/' + outputLayer)
agePredictions = ageSess.run(ageProbTensor, {'age/' + inputNode: [augmentedImage] })
with tf.compat.v1.Session(graph=genderGraph) as genderSess:
genderProbTensor = genderSess.graph.get_tensor_by_name('gender/' + outputLayer)
genderPredictions = genderSess.run(genderProbTensor, {'gender/' + inputNode: [augmentedImage] })
except Exception as e:
print (e)
# Print the highest probability label
age_highest_probability_index = np.argmax(agePredictions)
gender_highest_probability_index = np.argmax(genderPredictions)
classifiedAgeCategory = ageCategories[age_highest_probability_index]
classifiedGenderCategory = genders[gender_highest_probability_index]
except Exception as e:
print(e)
continue
错误:“名称‘age/loss:0’指的是不存在的张量。图中不存在操作‘age/loss’。” >
我需要在同一个程序中加载年龄和性别模型,请告诉我 我哪里做错了?