def model_test(model, input_data, label_data, is_debug=False, line=0.1): correct_num = 0 st2_num = 0 st3_num = 0 total_num = len(input_data) st1 = np.zeros(64) st2 = np.zeros(64) st3 = np.zeros(64) data_sca = np.zeros(64) right_location_num = 0 for step in range(len(input_data)): if is_debug: print( '\n<----------------------------------------------------------', step) x = input_data[step:(step + 1)] y = label_data[step:(step + 1)] max_y_index = 0 # 求y的最大值下标 for i in range(len(y[0])): if y[0][i] >= y[0][max_y_index]: max_y_index = i data_sca[max_y_index] = data_sca[max_y_index] + 1 # x = np.array(x).reshape(1, 1, 64) # y = np.array(y).reshape(1, 1, 64) x = torch.FloatTensor(x).cuda(0) y = torch.ByteTensor(y).cuda(0) prediction = model(x) predict = torch.sigmoid(prediction) > line t = label_data[step:(step + 1)] # t= t = np.array(t[0]).tolist() pd = predict.view(-1).data.cpu().numpy() pd = np.array(pd).tolist() result = torch.eq(y, predict) res = result.view(-1).data.cpu().numpy() res = np.array(res).tolist() if is_debug: print('target: ', t) print('predict: ', pd) print('difference:', res) if is_satisfied_standard2(pd, max_y_index): st2_num = st2_num + 1 st2[max_y_index] = st2[max_y_index] + 1 if is_debug: print('st2_right') if is_satisfied_standard3(pd, max_y_index): st3_num = st3_num + 1 st3[max_y_index] = st3[max_y_index] + 1 if is_debug: print('st3_right') result = torch.eq(y, predict) accuracy = torch.sum(result) / torch.sum(torch.ones(y.shape)) accuracy = accuracy.data.cpu().numpy() correct = torch.eq(torch.sum(~result), 0) if is_debug: print('accuracy: ', accuracy) print('correct: {}'.format(correct)) if correct == 1: # right_index = int(max_y_index/10) st1[max_y_index] = st1[max_y_index] + 1 correct_num = correct_num + 1 # 标准1,完全匹配 if is_debug: print( '-------------------------------------------------------------->\n' ) if is_debug: print('total:', (step + 1), ' | correct_num:', correct_num, '| complete_correct_rate:', correct_num / total_num, '| st2_num: ', st2_num, ' |st2_rate: ', st2_num / total_num, '| st3_num: ', st3_num, ' |st3_rate: ', st3_num / total_num) print('st1 : ', st1) print('data_sca : ', data_sca) # right_distribute.distribute_cv(st1, data_sca, 36, 'cnn_st1完全正确预测分布') right_distribute.distribute_cv(st2, data_sca, 36, 'cnn_st2相对预测正确率') right_distribute.distribute_cv(st3, data_sca, 36, 'cnn_st3相对预测正确率') return correct_num / total_num, st2_num / total_num, st3_num / total_num
if (abs(max_y_index - max_predict_index) < 3): relative_correct_num = relative_correct_num + 1 sca_r[max_y_index] = sca_r[max_y_index] + 1 # print('max_y_index : ', max_y_index, ' max_predict_index: ', max_predict_index) result = torch.eq(y, predict) accuracy = torch.sum(result) / torch.sum(torch.ones(y.shape)) accuracy = accuracy.data.cpu().numpy() correct = torch.eq(torch.sum(~result), 0) # if math.fabs(0.98437 - accuracy) > 0.0001: # print('accuracy: ', accuracy) # print('correct: {}'.format(correct)) if correct == 1: # right_index = int(max_y_index/10) sca[max_y_index] = sca[max_y_index] + 1 # sca_r[max_y_index] = sca_r[max_y_index] + 1 correct_num = correct_num + 1 # print('-------------------------------------------------------------->\n') print('total:', (step + 1), ' | correct_num:', correct_num, '| complete_correct_rate:', correct_num / total_num, '| relative_correct_num : ', relative_correct_num, '| relative_correct_rate: ', relative_correct_num / total_num) print('sca : ', sca) print('sca_r: ', sca_r) print('data_sca:', data_sca) right_distribute.distribute_cv(sca, data_sca, 64) right_distribute.distribute_cv(sca_r, data_sca, 64) # print('prediction: ', prediction.data.cpu().numpy().flatten())
def model_test(model_path): print('test') model = torch.load(model_path) # input_data, label_data = radar_data.load_pg_test_data() _, _1, input_data, label_data = radar_data.load_pg_data_by_range( 0, SEQ_LEN) # _, _1, input_data, label_data = radar_data.load_road_data() input_data = radar_data.reduce_data_length(input_data, 0, SEQ_LEN) label_data = radar_data.reduce_data_length(label_data, 0, SEQ_LEN) # L = len(input_data) correct_num = 0 relative_correct_num = 0 total_num = len(input_data) sca = np.zeros(SEQ_LEN) sca_r = np.zeros(SEQ_LEN) data_sca = np.zeros(SEQ_LEN) right_location_num = 0 zero_num = 0 for step in range(len(input_data)): print(step, '------------------------------------------------------------>') x = input_data[step:(step + 1)] y = label_data[step:(step + 1)] max_y_index = 0 # 求y的最大值下标 for i in range(len(y[0])): if y[0][i] >= y[0][max_y_index]: max_y_index = i # if not (max_y_index >= 18 and max_y_index <= 22): # 去掉低的情况 # total_num = total_num - 1 # continue data_sca[max_y_index] = data_sca[max_y_index] + 1 x = np.array(x).reshape(1, SEQ_LEN, 1) y = np.array(y).reshape(1, SEQ_LEN, 1) x = torch.FloatTensor(x).cuda(0) y = torch.ByteTensor(y).cuda(0) prediction, _ = model(x, None) predict = torch.sigmoid(prediction) > 0.08 max_predict_index = 0 mm = 0 for i in range(1, len(predict.view(-1))): if predict.view(-1)[i] > mm: max_predict_index = i mm = predict.view(-1)[i] if max_predict_index == 0: zero_num = zero_num + 1 t = label_data[step:(step + 1)] # t= t = np.array(t[0]).tolist() pd = predict.view(-1).data.cpu().numpy() pd = np.array(pd).tolist() result = torch.eq(y, predict) res = result.view(-1).data.cpu().numpy() res = np.array(res).tolist() print('target: ', t) print('predict: ', pd) print('difference:', res) # # if (abs(max_y_index - max_predict_index) < 3): # print('step: ', step) # # sca_r[max_y_index] = sca_r[max_y_index] + 1 # relative_correct_num = relative_correct_num + 1 # 在某个点上有物体完全预测正确 # if t[0][max_y_index] == pd[max_y_index]: # right_location_num = right_location_num+1 # 在某个位置前后偏离两个位置 if max_y_index >= 2 and max_y_index <= 62: if t[max_y_index] == pd[max_y_index] or t[max_y_index] == pd[max_y_index - 1] \ or t[max_y_index] == pd[max_y_index + 1] \ or t[max_y_index] == pd[max_y_index + 2] \ or t[max_y_index] == pd[max_y_index - 2] \ or t[max_y_index] == pd[max_y_index - 1]: print('relative right') right_location_num = right_location_num + 1 sca_r[max_y_index] = sca_r[max_y_index] + 1 else: if t[max_y_index] == pd[max_y_index]: right_location_num = right_location_num + 1 sca_r[max_y_index] = sca_r[max_y_index] + 1 accuracy = torch.sum(result) / torch.sum(torch.ones(y.shape)) accuracy = accuracy.data.cpu().numpy() correct = torch.eq(torch.sum(~result), 0) print('accuracy: ', accuracy) print('correct: {}'.format(correct)) if correct == 1: # right_index = int(max_y_index/10) sca[max_y_index] = sca[max_y_index] + 1 correct_num = correct_num + 1 print( '<------------------------------------------------------------------\n' ) print( 'total:', (step + 1), ' | correct_num:', correct_num, '| complete_correct_rate:', correct_num / total_num, # '| relative_correct_num : ', relative_correct_num, '| relative_correct_rate: ', # relative_correct_num / total_num, ' |right_location_rate: ', right_location_num / total_num, ' |zero_num:', zero_num, ' |zero_num_rate: ', zero_num / total_num) print('sca : ', sca) print('sca_r: ', sca_r) print('data_sca:', data_sca) right_distribute.distribute_cv(sca, data_sca, SEQ_LEN) right_distribute.distribute_cv(sca_r, data_sca, SEQ_LEN)
def model_test(model, input_data, label_data, is_debug=False, line=0.1): total_num = len(input_data) correct_num = 0 st2_num = 0 st3_num = 0 total_num = len(input_data) st1 = np.zeros(64) st2 = np.zeros(64) st3 = np.zeros(64) data_sca = np.zeros(64) zero_num = 0 for step in range(len(input_data)): if is_debug: print( '\n<----------------------------------------------------------', step) x = input_data[step:(step + 1)] y = label_data[step:(step + 1)] max_y_index = 0 # 求y的最大值下标 for i in range(len(y[0])): if y[0][i] >= y[0][max_y_index]: max_y_index = i # if not (max_y_index >= 18 and max_y_index <= 22): # 去掉低的情况 # total_num = total_num - 1 # continue data_sca[max_y_index] = data_sca[max_y_index] + 1 x = np.array(x).reshape(1, SEQ_LEN, 1) y = np.array(y).reshape(1, SEQ_LEN, 1) x = torch.FloatTensor(x).cuda(0) y = torch.ByteTensor(y).cuda(0) prediction, _ = model(x, None) predict = torch.sigmoid(prediction) > line max_predict_index = 0 mm = 0 for i in range(1, len(predict.view(-1))): if predict.view(-1)[i] > mm: max_predict_index = i mm = predict.view(-1)[i] if max_predict_index == 0: zero_num = zero_num + 1 t = label_data[step:(step + 1)] # t= t = np.array(t[0]).tolist() pd = predict.view(-1).data.cpu().numpy() pd = np.array(pd).tolist() result = torch.eq(y, predict) res = result.view(-1).data.cpu().numpy() res = np.array(res).tolist() if is_debug: print('target: ', t) print('predict: ', pd) print('difference:', res) # 在某个位置前后偏离两个位置 if standard_define.is_satisfied_standard2(pd, max_y_index): st2_num = st2_num + 1 st2[max_y_index] = st2[max_y_index] + 1 if standard_define.is_satisfied_standard3(pd, max_y_index): st3_num = st3_num + 1 st3[max_y_index] = st3[max_y_index] + 1 accuracy = torch.sum(result) / torch.sum(torch.ones(y.shape)) accuracy = accuracy.data.cpu().numpy() correct = torch.eq(torch.sum(~result), 0) if is_debug: print('accuracy: ', accuracy) print('correct: {}'.format(correct)) if correct == 1: # right_index = int(max_y_index/10) st1[max_y_index] = st1[max_y_index] + 1 correct_num = correct_num + 1 # 标准1,完全匹配 if is_debug: print( '-------------------------------------------------------------->\n' ) if is_debug: print('total:', (step + 1), ' | correct_num:', correct_num, '| complete_correct_rate:', correct_num / total_num, '| st2_num: ', st2_num, ' |st2_rate: ', st2_num / total_num, '| st3_num: ', st3_num, ' |st3_rate: ', st3_num / total_num) print('st1 : ', st1) print('data_sca : ', data_sca) right_distribute.distribute_cv(st1, data_sca, 36, 'st1完全正确预测分布') right_distribute.distribute_cv(st2, data_sca, 36, 'st2相对预测正确率') right_distribute.distribute_cv(st3, data_sca, 36, 'st3相对预测正确率') return correct_num / total_num, st2_num / total_num, st3_num / total_num
def model_test(model_path): model = torch.load(model_path) # input_data, label_data = radar_data.load_pg_test_data() _, _1, input_data, label_data = radar_data.load_pg_data_by_range(15, 30) # L = len(input_data) correct_num = 0 relative_correct_num = 0 total_num = len(input_data) sca = np.zeros(64) sca_r = np.zeros(64) data_sca = np.zeros(64) for step in range(len(input_data)): print('\n<----------------------------------------------------------', step) x = input_data[step:(step + 1)] y = label_data[step:(step + 1)] max_y_index = 0 # 求y的最大值下标 for i in range(len(y[0])): if y[0][i] >= y[0][max_y_index]: max_y_index = i # if max_y_index >35: # print('error: ', y) # break # if max_y_index > 30: # total_num = total_num - 1 # continue # data_index = int(max_y_index/10) data_sca[max_y_index] = data_sca[max_y_index] + 1 data_sca[max_y_index] = data_sca[max_y_index] + 1 x = np.array(x).reshape(1, 64, 1) # y = np.array(y).reshape(1, 64, 1) x = torch.FloatTensor(x).cuda(0) y = torch.ByteTensor(y).cuda(0) prediction, _ = model(x, None) _, max = torch.max(prediction.data, 1) index = 0 mm = prediction[0][0] for i in range(1, len(prediction[0])): if prediction[0][i] > mm: index = i mm = prediction[0][i] predict = [j - j for j in range(0, 64)] predict[index] = 1 predict = [predict] predict = torch.ByteTensor(predict).cuda(0) # predict = torch.sigmoid(prediction) > 0.5 # predict = prediction print('y: ', y.data.cpu().numpy().tolist()) print('predict:', predict.data.cpu().numpy().tolist()) print(torch.eq(y, predict)) max_predict_index = torch.max(predict, 1)[1].data.cpu().numpy()[0] if (abs(max_y_index - max_predict_index) < 3): sca_r[max_y_index] = sca_r[max_y_index] + 1 relative_correct_num = relative_correct_num + 1 result = torch.eq(y, predict) accuracy = torch.sum(result) / torch.sum(torch.ones(y.shape)) accuracy = accuracy.data.cpu().numpy() correct = torch.eq(torch.sum(~result), 0) print('accuracy: ', accuracy) print('correct: {}'.format(correct)) if correct == 1: # right_index = int(max_y_index/10) sca[max_y_index] = sca[max_y_index] + 1 correct_num = correct_num + 1 print( '-------------------------------------------------------------->\n' ) print('total:', (step + 1), ' | correct_num:', correct_num, '| complete_correct_rate:', correct_num / total_num, '| relative_correct_num : ', relative_correct_num, '| relative_correct_rate: ', relative_correct_num / total_num) print('sca : ', sca) print('data_sca : ', data_sca) right_distribute.distribute_cv(sca, data_sca, 64) right_distribute.distribute_cv(sca_r, data_sca, 64)
def model_test(model_path): model = torch.load(model_path) # input_data, label_data = radar_data.load_pg_test_data() # _, _1, input_data, label_data = radar_data.load_pg_data_by_range(0, 64) _, _1, input_data, label_data = radar_data.rerange_road_data() # L = len(input_data) # train_data_input, train_data_label = radar_data.load_pg_val_data() correct_num = 0 relative_correct_num = 0 total_num = len(input_data) sca = np.zeros(64) sca_r = np.zeros(64) data_sca = np.zeros(64) right_location_num = 0 for step in range(len(input_data)): print('\n<----------------------------------------------------------', step) x = input_data[step:(step + 1)] y = label_data[step:(step + 1)] max_y_index = 0 # 求y的最大值下标 for i in range(len(y[0])): if y[0][i] >= y[0][max_y_index]: max_y_index = i # if max_y_index >35: # print('error: ', y) # break # if max_y_index > 30: # total_num = total_num - 1 # continue # data_index = int(max_y_index/10) data_sca[max_y_index] = data_sca[max_y_index] + 1 x = np.array(x).reshape(1, 1, 64) y = np.array(y).reshape(1, 1, 64) x = torch.FloatTensor(x).cuda(0) y = torch.ByteTensor(y).cuda(0) prediction = model(x) _, max = torch.max(prediction.data, 1) index = 0 # mm = prediction[0][0] # for i in range(1, len(prediction[0])): # if prediction[0][i] > mm: # index = i # mm = prediction[0][i] # predict = [j - j for j in range(0, 64)] # predict[index] = 1 # predict = [predict] # predict = torch.ByteTensor(predict).cuda(0) predict = torch.sigmoid(prediction) > 0.15 # predict = prediction # print('y: ', y) # print('predict:', predict) # print(torch.eq(y, predict)) t = label_data[step:(step + 1)] # t= t = np.array(t[0]).tolist() pd = predict.view(-1).data.cpu().numpy() pd = np.array(pd).tolist() result = torch.eq(y, predict) res = result.view(-1).data.cpu().numpy() res = np.array(res).tolist() print('target: ', t) print('predict: ', pd) print('difference:', res) max_predict_index = torch.max(predict, 1)[1].data.cpu().numpy()[0] if (abs(max_y_index - max_predict_index) < 2): # sca_r[max_y_index] = sca_r[max_y_index] + 1 relative_correct_num = relative_correct_num + 1 if max_y_index >= 2 and max_y_index <= 62: if t[max_y_index] == pd[max_y_index] or t[max_y_index] == pd[max_y_index - 1] \ or t[max_y_index] == pd[max_y_index + 1] \ or t[max_y_index] == pd[max_y_index - 1] : # or t[max_y_index] == pd[max_y_index + 2] \ # or t[max_y_index] == pd[max_y_index - 2]: print('relative right') right_location_num = right_location_num + 1 sca_r[max_y_index] = sca_r[max_y_index] + 1 else: if t[max_y_index] == pd[max_y_index]: right_location_num = right_location_num + 1 sca_r[max_y_index] = sca_r[max_y_index] + 1 result = torch.eq(y, predict) accuracy = torch.sum(result) / torch.sum(torch.ones(y.shape)) accuracy = accuracy.data.cpu().numpy() correct = torch.eq(torch.sum(~result), 0) print('accuracy: ', accuracy) print('correct: {}'.format(correct)) if correct == 1: # right_index = int(max_y_index/10) sca[max_y_index] = sca[max_y_index] + 1 correct_num = correct_num + 1 print('-------------------------------------------------------------->\n') print('total:', (step + 1), ' | correct_num:', correct_num, '| complete_correct_rate:', correct_num / total_num, # '| relative_correct_num : ', relative_correct_num, '| relative_correct_rate: ', # relative_correct_num / total_num, ' |right_location_rate: ', right_location_num / total_num) print('sca : ', sca) print('data_sca : ', data_sca) right_distribute.distribute_cv(sca, data_sca, 64) right_distribute.distribute_cv(sca_r, data_sca, 64)
def model_test(model_path): model = torch.load(model_path) # input_data, label_data = radar_data.load_pg_test_data() _, _1, input_data, label_data = radar_data.load_pg_data_by_range(0, 64) # input_data, label_data = radar_data.load_val_data( # 'D:\home\zeewei\projects\\77GRadar\data\\all\\all_val_data.npy') # L = len(input_data) correct_num = 0 relative_correct_num = 0 total_num = len(input_data) sca = np.zeros(64) sca_r = np.zeros(64) data_sca = np.zeros(64) right_location_num = 0 zero_num = 0 for step in range(len(input_data)): print('\n<----------------------------------------------------------', step) x = input_data[step:(step + 1)] y = label_data[step:(step + 1)] max_y_index = 0 # 求y的最大值下标 for i in range(len(y[0])): if y[0][i] >= y[0][max_y_index]: max_y_index = i data_sca[max_y_index] = data_sca[max_y_index] + 1 x = np.array(x).reshape(1, 1, 64) y = np.array(y).reshape(1, 1, 64) x = torch.FloatTensor(x).cuda(0) y = torch.ByteTensor(y).cuda(0) prediction = model(x) _, max = torch.max(prediction.data, 1) predict = torch.sigmoid(prediction) > 0.015625 max_predict_index = 0 mm = 0 for i in range(1, len(predict.view(-1))): if predict.view(-1)[i] > mm: max_predict_index = i mm = predict.view(-1)[i] if max_predict_index == 0: zero_num = zero_num + 1 t = label_data[step:(step + 1)] # t= t = np.array(t[0]).tolist() tt = [] for item in t: if item == 0: tt.append(0) else: tt.append(1) t = tt pd = predict.view(-1).data.cpu().numpy() pd = np.array(pd).tolist() result = torch.eq(y, predict) res = result.view(-1).data.cpu().numpy() res = np.array(res).tolist() print('target: ', t) print('predict: ', pd) print('difference:', res) max_predict_index = torch.max(predict, 1)[1].data.cpu().numpy()[0] # 在某个位置前后偏离两个位置 if max_y_index >= 2 and max_y_index <= 62: if t[max_y_index] == pd[max_y_index] or t[max_y_index] == pd[max_y_index - 1] \ or t[max_y_index] == pd[max_y_index + 1] \ or t[max_y_index] == pd[max_y_index - 1]: # or t[max_y_index] == pd[max_y_index + 2] \ # or t[max_y_index] == pd[max_y_index - 2] \ print('relative right') right_location_num = right_location_num + 1 sca_r[max_y_index] = sca_r[max_y_index] + 1 else: if t[max_y_index] == pd[max_y_index]: right_location_num = right_location_num + 1 sca_r[max_y_index] = sca_r[max_y_index] + 1 result = torch.eq(y, predict) accuracy = torch.sum(result) / torch.sum(torch.ones(y.shape)) accuracy = accuracy.data.cpu().numpy() correct = torch.eq(torch.sum(~result), 0) print('accuracy: ', accuracy) print('correct: {}'.format(correct)) if correct == 1: # right_index = int(max_y_index/10) sca[max_y_index] = sca[max_y_index] + 1 correct_num = correct_num + 1 print('-------------------------------------------------------------->\n') print('total:', (step + 1), ' \n| correct_num:', correct_num, ' \n| complete_correct_rate:', correct_num / total_num, ' \n|right_location_rate: ', right_location_num / total_num, ' \n|zero_num:', zero_num, ' \n|zero_num_rate: ', zero_num / total_num) print('sca : ', sca) print('sca_r: ', sca_r) print('data_sca:', data_sca) right_distribute.distribute_cv(sca, data_sca, 64) right_distribute.distribute_cv(sca_r, data_sca, 64)