https://arkouji.cocolog-nifty.com/blog/2019/02/sony-neural-net.html
1つ目のURLで静止画で推論をしたところ、こちらでは上手く26ラベルの推論がそれぞれできました。
https://jump1268.hatenablog.com/entry/2021/01/04/144112
しかし、上のブログを参考にして、オリジナルデータからWEBカメラからリアルタイムで推論をしましたが、何度も試しても同じラベル(I)しか表示されません。考えられる原因は何でしょうか?(パラメーターは若干変化していますが。。)
#ネットワーク層と学習結果
#実行結果
1回目 \cache_00000000_00000000.npy 2021-01-19 12:21:49,952 [nnabla][INFO]: Using DataSourceWithMemoryCache 2021-01-19 12:21:49,952 [nnabla][INFO]: DataSource with shuffle(False) 2021-01-19 12:21:49,952 [nnabla][INFO]: On-memory 2021-01-19 12:21:49,952 [nnabla][INFO]: Using DataIterator 2021-01-19 12:21:54,181 [nnabla][INFO]: Parameter load (<built-in function format>): C:\Users\username\Desktop\output\yubidata.files\20210118_165022\results.nnp load model [0. 0. 0. 0.00002985 0.00000001 0.00000944 0. 0.00000086 0.848121 0.00000203 0.00003129 0.00002938 0. 0.12772788 0.00000001 0.0000142 0.00139796 0. 0. 0. 0.00000012 0.00000001 0.00000001 0. 0.02260908 0.0000269 ] 8 I 2回目 \cache_00000000_00000000.npy 2021-01-19 12:21:59,198 [nnabla][INFO]: Using DataSourceWithMemoryCache 2021-01-19 12:21:59,198 [nnabla][INFO]: DataSource with shuffle(False) 2021-01-19 12:21:59,198 [nnabla][INFO]: On-memory 2021-01-19 12:21:59,198 [nnabla][INFO]: Using DataIterator 2021-01-19 12:22:03,469 [nnabla][INFO]: Parameter load (<built-in function format>): C:\Users\username\Desktop\output\yubidata.files\20210118_165022\results.nnp load model [0. 0. 0. 0.0000274 0.00000001 0.00000936 0. 0.00000072 0.8445436 0.00000197 0.00002842 0.00002868 0. 0.13168676 0.00000001 0.00001226 0.00130046 0. 0. 0. 0.00000013 0.00000001 0.00000001 0. 0.02233552 0.00002472] 8 I
#プログラム
python
1import nnabla as nn 2import nnabla.functions as F 3import nnabla.parametric_functions as PF 4from nnabla.utils.data_iterator import data_iterator_csv_dataset 5import os 6import cv2 7from datetime import datetime 8import pandas as pd 9import matplotlib.pyplot as plt 10import numpy as np 11from PIL import Image 12 13 14 15def network(x, y, test=False): 16 # Input:x -> 3,250,250 17 # Convolution -> 16,250,250 18 h = PF.convolution(x, 16, (3,3), (1,1), name='Convolution') 19 # ReLU 20 h = F.relu(h, True) 21 # BatchNormalization 22 h = PF.batch_normalization(h, (1,), 0.9, 0.0001, not test, name='BatchNormalization') 23 # MaxPooling -> 16,125,125 24 h = F.max_pooling(h, (2,2), (2,2)) 25 # Convolution_2 26 h = PF.convolution(h, 16, (3,3), (1,1), name='Convolution_2') 27 # ReLU_2 28 h = F.relu(h, True) 29 # BatchNormalization_2 30 h = PF.batch_normalization(h, (1,), 0.9, 0.0001, not test, name='BatchNormalization_2') 31 # MaxPooling_2 -> 16,62,62 32 h = F.max_pooling(h, (2,2), (2,2)) 33 # Convolution_3 34 h = PF.convolution(h, 16, (3,3), (1,1), name='Convolution_3') 35 # ReLU_3 36 h = F.relu(h, True) 37 # Convolution_4 38 h = PF.convolution(h, 16, (3,3), (1,1), name='Convolution_4') 39 # ReLU_4 40 h = F.relu(h, True) 41 # Convolution_5 42 h = PF.convolution(h, 16, (3,3), (1,1), name='Convolution_5') 43 # ReLU_5 44 h = F.relu(h, True) 45 # MaxPooling_3 -> 16,31,31 46 h = F.max_pooling(h, (2,2), (2,2)) 47 # Affine -> 100 48 h = PF.affine(h, (100,), name='Affine') 49 # ReLU_6 50 h = F.relu(h, True) 51 # Affine_2 52 h = PF.affine(h, (100,), name='Affine_2') 53 # ReLU_7 54 h = F.relu(h, True) 55 # Dropout 56 if not test: 57 h = F.dropout(h) 58 # Affine_3 -> 26 59 h = PF.affine(h, (26,), name='Affine_3') 60 # SoftmaxCrossEntropy -> 1 61 #h = F.softmax_cross_entropy(h, y) 62 h = F.softmax(h) 63 return h 64 65 66 67 68 69 70class_names = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] 71cap = cv2.VideoCapture(0) # 任意のカメラ番号に変更する 72 73 74new_dir_path = "./realtime/" 75os.makedirs(new_dir_path, exist_ok=True) 76 77 #カメラスタート 78while True: 79 80 ret, frame = cap.read() 81 # フレームが取得できない場合はループを抜ける 82 83 84 cv2.imshow("camera", frame) 85 86 k = cv2.waitKey(1)&0xff # キー入力を待つ 87 if k == ord('p'): 88 89 # 「p」キーで画像を保存 90 91 date = datetime.now().strftime("%Y%m%d_%H%M%S") 92 path = new_dir_path + date +".png" 93 cv2.imwrite(path, frame) 94 image_gs = cv2.imread(path) 95 96 path = new_dir_path + date +".png" 97 dst = cv2.resize(image_gs,(250,250)) 98 cv2.imwrite(path, dst) 99 100 f = pd.DataFrame(columns=["x:data","y:data"]) 101 xdata = path 102 ydata = 0 103 new_name = pd.Series([xdata,ydata],index=f.columns) 104 f = f.append(new_name, ignore_index=True) 105 f.to_csv('valu.csv',index=False,header = True ) 106 107 test_data = data_iterator_csv_dataset("C:\Users\username\Desktop\output\valu.csv",1,shuffle=False,normalize=True) 108 109 110 path = new_dir_path + "test" +".png" 111 cv2.imwrite(path, frame) 112 image_gs = cv2.imread(path) 113 114 path = new_dir_path + date +".png" 115 dst = cv2.resize(image_gs,(250,250)) 116 cv2.imwrite(path, dst) 117 118 f = pd.DataFrame(columns=["x:data","y:data"]) 119 xdata = path 120 ydata = 0 121 new_name = pd.Series([xdata,ydata],index=f.columns) 122 f = f.append(new_name, ignore_index=True) 123 f.to_csv('valu.csv',index=False,header = True ) 124 125 test_data = data_iterator_csv_dataset("C:\Users\username\Desktop\output\valu.csv",1,shuffle=False,normalize=True) 126 127 np.set_printoptions(suppress=True) 128 #ネットワークの構築 129 130 nn.clear_parameters() 131 x = nn.Variable((1,3,250,250)) 132 t = nn.Variable((1,1)) 133 y = network(x, t, test=True) 134 135 nn.load_parameters('C:\Users\username\Desktop\output\yubidata.files\20210118_165022\results.nnp') 136 print("load model") 137 138 for i in range(test_data.size): 139 140 x.d, t.d = test_data.next() 141 y.forward() 142 143 print(y.d[0]) 144 print(np.argmax(y.d[0])) 145 print(class_names[np.argmax(y.d[0])]) 146 147 elif k == ord('q'): 148 # 「q」キーが押されたら終了する 149 150 break 151 152 # キャプチャをリリースして、ウィンドウをすべて閉じる 153cap.release() 154cv2.destroyAllWindows() 155 156
回答1件
あなたの回答
tips
プレビュー