前提・実現したいこと
https://qiita.com/neriai/items/bd7bc36ec42c8ef65b2e
TensorFlowを使ってDir en greyの顔分類器を作ってみた を参考に自分の顔とそれ以外の顔を分類できるようにしたいと思ってます。
発生している問題・エラーメッセージ
⑧,⑨の学習実行編まではうまくいったのですが、⑩の顔分類試験編でエラーが発生してしまします
学習時のコードは https://teratail.com/questions/234773 に乗せているのと同じです
error1
1Traceback (most recent call last): 2 File "C:\Users?????\Anaconda3\envs\opencvtest001\evaltest.py", line 130, in <module> 3 evaluation('C:/Users/?????/Anaconda3/envs/opencvtest001/testface001.jpg', 'C:/Users/?????/Anaconda3/envs/opencvtest001/model3.ckpt') 4 File "C:\Users?????\Anaconda3\envs\opencvtest001\evaltest.py", line 89, in evaluation 5 saver = tf.train.Saver() 6 File "C:\Users?????\Anaconda3\envs\opencvtest001\lib\site-packages\tensorflow_core\python\training\saver.py", line 806, in __init__ 7 "When eager execution is enabled, `var_list` must specify a list " 8RuntimeError: When eager execution is enabled, `var_list` must specify a list or dict of variables to save
[試したこと]にも書きましたが、また別のエラーが発生。
error2
1Traceback (most recent call last): 2 File "C:\Users?????\Anaconda3\envs\opencvtest001\evaltest.py", line 130, in <module> 3 evaluation('C:/Users/?????/Anaconda3/envs/opencvtest001/testface001.jpg', 'C:/Users/?????/Anaconda3/envs/opencvtest001/model3.ckpt') 4 File "C:\Users?????\Anaconda3\envs\opencvtest001\evaltest.py", line 92, in evaluation 5 sess.run(tf.global_variables_initializer()) 6 File "C:\Users?????\Anaconda3\envs\opencvtest001\lib\site-packages\tensorflow_core\python\client\session.py", line 956, in run 7 run_metadata_ptr) 8 File "C:\Users?????\Anaconda3\envs\opencvtest001\lib\site-packages\tensorflow_core\python\client\session.py", line 1105, in _run 9 raise RuntimeError('The Session graph is empty. Add operations to the ' 10RuntimeError: The Session graph is empty. Add operations to the graph before calling run(). 11
該当のソースコード
python
1 2#! -*- coding: utf-8 -*- 3 4import sys ,os 5import numpy as np 6import cv2 7import tensorflow as tf 8import tensorflow.python.platform 9#import os 10import random 11import testmain02 12import tensorflow.compat.v1 as tf 13 14from tensorflow.python.framework import ops 15 16# OpenCVのデフォルトの顔の分類器のpath 17# cascade_path = '/usr/local/Cellar/opencv3/3.2.0/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml' 18cascade_path = 'C:/Users/?????/Anaconda3/envs/opencvtest001/haarcascades/haarcascade_frontalface_default.xml' 19faceCascade = cv2.CascadeClassifier(cascade_path) 20 21# 識別ラベルと各ラベル番号に対応する名前 22HUMAN_NAMES = { 23 0: u"me", 24 1: u"others" 25} 26 27def evaluation(img_path, ckpt_path): 28 # GraphのReset(らしいが、何をしているのかよくわかっていない…) 29 #tf.reset_default_graph() 30 ops.reset_default_graph() 31 32 # ファイルを開く 33 f = open(img_path, 'r') 34 img = cv2.imread(img_path, cv2.IMREAD_COLOR) 35 36 # モノクロ画像に変換 37 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 38 face = faceCascade.detectMultiScale(gray, 1.1, 3) 39 40 if len(face) > 0: 41 for rect in face: 42 # 加工した画像に何でもいいので適当な名前をつけたかった。日付秒数とかでいいかも 43 random_str = str(random.random()) 44 45 # 顔部分を赤線で書こう 46 cv2.rectangle(img, tuple(rect[0:2]), tuple(rect[0:2]+rect[2:4]), (0, 0, 255), thickness=2) 47 48 # 顔部分を赤線で囲った画像の保存先 49 face_detect_img_path = 'C:/Users/?????/Anaconda3/envs/opencvtest001/tensorrect' + random_str + '.jpg' 50 51 # 顔部分を赤線で囲った画像の保存 52 cv2.imwrite(face_detect_img_path, img) 53 x = rect[0] 54 y = rect[1] 55 w = rect[2] 56 h = rect[3] 57 58 # 検出した顔を切り抜いた画像を保存 59 #cv2.imwrite('{ディレクトリパス}' + random_str + '.jpg', img[y:y+h, x:x+w]) 60 61 # TensorFlowへ渡す切り抜いた顔画像 62 target_image_path = 'C:/Users/?????/Anaconda3/envs/opencvtest001/tensorrect' + random_str + '.jpg' 63 else: 64 # 顔が見つからなければ処理終了 65 print ('image:No Face') 66 return 67 f.close() 68 f = open(target_image_path, 'r') 69 70 # データを入れる配列 71 image = [] 72 img = cv2.imread(target_image_path) 73 img = cv2.resize(img, (28, 28)) 74 75 # 画像情報を一列にした後、0-1のfloat値にする 76 image.append(img.flatten().astype(np.float32)/255.0) 77 # numpy形式に変換し、TensorFlowで処理できるようにする 78 image = np.asarray(image) 79 80 # 入力画像に対して、各ラベルの確率を出力して返す(main.pyより呼び出し) 81 #logits = main.inference(image, 1.0) 82 logits = testmain02.inference(image, 1.0) 83 84 # We can just use 'c.eval()' without passing 'sess' 85 sess = tf.InteractiveSession() 86 87 # restore(パラメーター読み込み)の準備 88 saver = tf.train.Saver() 89 90 # 変数の初期化 91 sess.run(tf.global_variables_initializer()) 92 93 if ckpt_path: 94 # 学習後のパラメーターの読み込み 95 saver.restore(sess, ckpt_path) 96 97 # sess.run(logits)と同じ 98 softmax = logits.eval() 99 100 # 判定結果 101 result = softmax[0] 102 103 # 判定結果を%にして四捨五入 104 rates = [round(n * 100.0, 1) for n in result] 105 humans = [] 106 107 # ラベル番号、名前、パーセンテージのHashを作成 108 for index, rate in enumerate(rates): 109 name = HUMAN_NAMES[index] 110 humans.append({ 111 'label': index, 112 'name': name, 113 'rate': rate 114 }) 115 116 # パーセンテージの高い順にソート 117 rank = sorted(humans, key=lambda x: x['rate'], reverse=True) 118 119 # 結果をコンソールに出力 120 print (rank) 121 122 # 判定結果と加工した画像のpathを返す 123 return [rank, os.path.basename(img_path), random_str + '.jpg'] 124 125# コマンドラインからのテスト用 126# if __name__ == '__main__': 127# evaluation('C:/Users/?????/Anaconda3/envs/opencvtest001/testface001.jpg', 'C:/Users/?????/Anaconda3/envs/opencvtest001/model2.ckpt') 128if __name__ == '__main__': 129 evaluation('C:/Users/?????/Anaconda3/envs/opencvtest001/testface001.jpg', 'C:/Users/?????/Anaconda3/envs/opencvtest001/model2.ckpt')
試したこと
tensorflowのバージョン2.0なので一部変更を加えた部分もあります。
色々調べてsaver.pyのvar_list をNoneからlist もしくは dictに変更すればいいと書いてあったので変更した後のエラーが上記のエラー2です。
補足情報(FW/ツールのバージョンなど)
windows 10
python3.7.0
tensorflow 2.0
tensorboard 2.0
annaconda3
opencv 4.1.0

あなたの回答
tips
プレビュー