saveImage(img_dirs, "_remove_margin", img_remove_margin) # 扩充为正方形并缩小为256x256 img_new = normalization(img_remove_margin) saveImage(img_dirs, "_new", img_new) # 打印信息到输出台 printToConsole(start_time, f, count, total, 5) success += 1 except Exception as e: # 错误情况 saveError(e, out_dir, f) fail += 1 end_time = datetime.datetime.now() expend = end_time - start_time print( "\n\ntotal: %d\nsuccessful: %d\nskip: %d\nfailed: %d\nExpend time: %s" % (total, success, skip, fail, expend)) os.startfile(out_dir) if __name__ == '__main__': file_path = "C:\\Study\\test\\old" out_dir = "C:\\Study\\test\\old_no_norm_no_sucessxxx" data = "new_data.txt" feature, label = loadData(data) # 直方图归一化 # feature = handleHistogram(feature) w0 = ridgeRegression(feature, label, 0.5) handle(file_path, out_dir, (45, -45, 45, -45), w0)
def test(): inputNodes = 256 hiddenNodes = 1000 outputNodes = 150 # 输出的值个数 learningRate = 0.1 net = neuralNetwork(inputNodes, hiddenNodes, outputNodes, learningRate) # 读取训练数据集 data, labels = loadData("new_data.txt") data = handleHistogram(data) m, n = numpy.shape(data) ######## 训练 ######### iteration = 50 # 迭代次数 for it in range(iteration): for i in range(m): inputs = data[i, :] + 0.01 targets = numpy.zeros(outputNodes) + 0.0001 # 初始化输出节点,避免0 targets[int(labels[i, 0])] = 0.99 # 将数据的每个标签的值置为0.99(避免1), 数据标签为0-155 # 避免0和1是因为激活函数生成0和1是不可能 net.train(inputs, targets) # print(targets, int(labels[i, 0]), inputs) # break # break print("完成第 ", it, "次迭代") ############# 测试 ############## predict_y = [] result = [] # 存储test结果 for i in range(m): inputs = data[i, :] + 0.01 outputs = net.test(inputs) label = numpy.argmax(outputs) # 找出最大索引,若训练正确则索引即为标签值 predict_y.append(int(label)) # print(label, labels[i, 0]) # print(outputs) if label == labels[i, 0]: print("第 ", i, "成功!") result.append(1) else: print("第 ", i, "失败!") result.append(0) # break # 最终准确率 result_array = numpy.asarray(result) # asarray不会深度拷贝数组,占用内存少 print("识别率 = ", result_array.sum() / result_array.size) actual_x = [] # 绘制直线的x轴坐标 predict_x = [] # 绘制预测值的x坐标 for i in labels: actual_x.append(int(i[0])) predict_x.append(int(i[0])) actual_y = actual_x # 直线的y坐标 # 得到预测值 color = numpy.arctan2(predict_y, predict_x) # 绘制散点图 plt.scatter(predict_x, predict_y, s=10, c=color, alpha=1) # 设置坐标轴范围 plt.xlim([0, 150]) plt.ylim([0, 150]) plt.xlabel("actual value") plt.ylabel("prediction") plt.plot(actual_x, actual_y) plt.savefig("bp") plt.show()
# coding:UTF-8 # 2018-10-25 # https://blog.csdn.net/zchshhh/article/details/78215087 from api import handleHistogram, plotScatter, loadData from regression import ridgeRegression if __name__ == '__main__': print("loading data ...") feature, label = loadData("data.txt") feature = handleHistogram(feature, alpha=20000, is_total=True) # feature = handleHistogram(feature) w0 = ridgeRegression(feature, label, 0.5) plotScatter(feature, label, w0, [(0, 150), (0, 150)], "regression")
end_time = datetime.datetime.now() expend = end_time - start_time print("\n\ntotal: %d\nsuccessful: %d\nskip: %d\nfailed: %d\nExpend time: %s" %(total, success, skip, fail, expend)) os.startfile(out_dir) if __name__ == '__main__': file_path = r"C:\Study\test\bone\2" out_dir = "C:\\Study\\test\\softmax_norm_test" # weights_file = "weights.npy" # w0 = loadWeights(weights_file) # w0 = np.load(weights_file) # print(np.shape(w0)) # print(w0) inputfile = "data.txt" feature, label = loadData(inputfile) feature = handleHistogram(feature) # print(np.shape(feature), np.shape(label)) # print(feature) k = 256 w0 = train(feature, label, k, 100000, 0.1) fp = open("pickle_weight_test.dat", "wb") pickle.dump(w0, fp) fp.close() handle(file_path, out_dir, (45,-45,45,-45), w0) # # 分割评估 # file_path = "C:\\Study\\test\\100-gt" # 标准分割图像目录路径 # out_dir = "C:\\Study\\test\\est_results" #结果保存目录
# coding:UTF-8 # 2018-11-7 # 绘制像素均值-最佳阈值直方图 from api import loadData, getHistogramMean import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.use('Agg') if __name__ == '__main__': print("loading data ...") feature, label = loadData("new_data.txt") # feature, label = dataFilter(feature, label) line_x, line_y = [], [] x, y = [], [] m, n = np.shape(feature) for i in range(m): mean = getHistogramMean(feature[i, :]) x.append(int(mean)) y.append(int(label[i, :])) print(x, y) color = np.arctan2(y, x) # 绘制散点图 plt.scatter(x, y, s=10, c=color, alpha=1) # 设置坐标轴范围 plt.xlim([0, 150]) plt.ylim([0, 150]) line_x = x line_y = x plt.xlabel("mean value")