模型训练完毕,在win10系统下加载pb模型运行测试程序时,报错:
Process finished with exit code -1073741819 (0xC0000005)
因为我把整个测试图片都加载进来,和底库中的1000id特征进行对比,把batch size改为10,这个问题就解决了,又报另外的错误:
Process finished with exit code -1073740791 (0xC0000409)
后来发现是batch size 读取程序有问题,下面是改正后的代码:
# -*- coding: utf-8 -*-
import os
import argparse
import time
import tensorflow as tf
import cv2
from sklearn.preprocessing import normalize
import numpy as np
# 1:1000 test
class test_slim:
def __init__(self, batch_size=1):
self.yunce_path = r'F:\depth-face\test_all_id.tfrecords'
self.ids_path = 'ids_1k.tfrecords'
self.batch_size = batch_size
def pre_process_orbbec(self, example_proto):# 读取整个测试集-> model
features = {
'id': tf.FixedLenFeature([], tf.int64),
'label': tf.FixedLenFeature([], tf.int64),
'color_raw': tf.FixedLenFeature([], tf.string),
}
features = tf.parse_single_example(example_proto, features)
color = tf.image.decode_jpeg(features['color_raw'])
color = tf.cast(color, tf.float32)
#color = tf.image.crop_to_bounding_box(color, 0, 0, 56, 112)
ir = color
depth = color
label = tf.cast(features['label'], tf.int64)
# return color, ir, depth, label
return color, label
def get_orbbec_batch(self, batch_size): # 读取batch
dataset = tf.data.TFRecordDataset(self.yunce_path)
dataset = dataset.map(self.pre_process_orbbec)
dataset = dataset.batch(batch_size)
# dataset = dataset.apply(tf.contrib.data.batch_and_drop_remainder(batch_size))
dataset = dataset.repeat(100)
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()
return next_element
def cacu_roc(self,threshold, dis_list, issame_list):
predict_issame = np.less(dis_list, threshold).squeeze()
tp = np.sum(np.logical_and(predict_issame, issame_list))
fp = np.sum(np.logical_and(predict_issame, np.logical_not(issame_list)))
tn = np.sum(np.logical_and(np.logical_not(predict_issame), np.logical_not(issame_list)))
fn = np.sum(np.logical_and(np.logical_not(predict_issame), issame_list))
tpr = 0 if (tp + fn == 0) else float(tp) / float(tp + fn)
fpr = 0 if (fp + tn == 0) else float(fp) / float(fp + tn)
acc = float(tp + tn) / len(dis_list)
return acc, tpr, fpr
if __name__ == '__main__':
embedding_1k = np.load(r'F:\depth-face\ids_se_112_embedding.npy')#加载1000id embedding
#test_data = test_slim()
print('length of embedding1k is:', len(embedding_1k))
embedding_list = [] #特征列表
test_data = test_slim(batch_size=1)#一次取一张图片
threshold = 0.01
pred_right = 0
tp = 0
tn = 0
fp = 0
fn = 0
ids = list(np.arange(1000))
data_test_batch = test_data.get_orbbec_batch(batch_size=1)
print(data_test_batch)
# 加载pb文件
with tf.gfile.GFile(r'F:\depth-face\ckpt_resnet50_se\train_resnet50_se.pb', 'rb') as f:
restored_graph_def = tf.GraphDef()
restored_graph_def.ParseFromString(f.read())
tf.import_graph_def(restored_graph_def,
input_map=None,
return_elements=None,
name='')
graph = tf.get_default_graph()
input_node = graph.get_tensor_by_name('input:0')
output_node = graph.get_tensor_by_name('output:0')
print('output_node:',output_node)
#with tf.Session(config=tf.ConfigProto(allow_soft_place=True, gpu_options=tf.GPUOptions(allow_growth=True))) as sess:
with tf.Session() as sess:
for i in range(89298): # 89298为测试图片的总数
dis_list = []
color, label = sess.run(data_test_batch)#读取图片与对应的label
#print(color.shape)
color = color[0]
#print('color:',color)
#print('label:',label)
embedding = sess.run(output_node, feed_dict={input_node: [color]})
embedding = normalize(embedding)#将特征进行归一化
for index, em in enumerate(embedding_1k):#index就是对应图片特征的id
diff = np.subtract(embedding, em)
dis = np.sum(np.square(diff))
dis = np.sqrt(dis)
if index % 1000 == 0:
print(index)
dis_list.append(dis)
#print('length of dis_list is:', len(dis_list))
if np.min(dis_list) <= threshold:#预测图片与底库id的特征足够接近
min_val = np.min(dis_list)
print(min_val)
#pred_id = np.argmin(dis_list)
pred_id = dis_list.index(min_val)
print('pred_id', pred_id)
if pred_id in ids:
tp = tp + 1
else:
fp = fp + 1
else:
pred_id = np.argmin(dis_list)
if pred_id in ids:
fn = fn + 1
else:
tn = tn + 1
'''if pred_id == label:
pred_right = pred_right + 1'''
tpr = 0 if (tp + fn == 0) else float(tp) / float(tp + fn)
fpr = 0 if (fp + tn == 0) else float(fp) / float(fp + tn)
acc = float(tp + tn) / len(dis_list)
print('acc:', acc)
print('tpr:', tpr)
print('fpr:', fpr)
加更:后来又出现了这个错误,通过指定显卡解决了问题,参考:链接
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
以上对我个人是有效的,造成这个错误的原因很多,需要慢慢排查。