def train(data_c, data_p, l, epoch, LR, w_size): print('lamda = ', 400 + l * 10) n = data_c.shape[0] c = data_c.shape[1] x = data_c # 以 K/S作为Y_fade y = data_p[l][1:].reshape(n, c) y = mf.math_model(y) - mf.math_model(data_p[l][0]) w = np.random.uniform(-1, 1, w_size) print('total_loss', total_loss(np.zeros(w_size), n, c, y, x)) for i in range(epoch): dw = np.zeros_like(w) for t in range(n): for j in range(c): for k in range(c - j - 1): dw += dloss(y[t][j], x[t][j], y[t][k + j + 1], x[t][k + j + 1], w) w = w - dw * LR if i % 1000 == 0: print('total_loss', total_loss(w, n, c, y, x), w, dw) return w
def Mix(compose, data_c, data_p): base_f = mf.math_model(data_p[0]) F = np.zeros_like(base_f) for i in range(compose.size): df = (mf.math_model(data_p[i * 3 + 2]) - base_f) / data_c[i][1] F += df * compose[i] return mf.i_math_model(F + base_f)
def get_dfs_KM(w, data_c, data_p): base_f = mf.math_model(data_p[0]) dfs = [] for i in range(data_c.shape[0]): df = (mf.math_model(data_p[i * 3 + 1]) - base_f - correct_func(data_c[i][0], w)) / data_c[i][0] df += (mf.math_model(data_p[i * 3 + 2]) - base_f - correct_func(data_c[i][1], w)) / data_c[i][1] df += (mf.math_model(data_p[i * 3 + 3]) - base_f - correct_func(data_c[i][2], w)) / data_c[i][2] dfs.append(df / 3) return np.array(dfs)
def color_diff_by_compose(fil, w_names, scale, data_c, data_p, compose1, compose2): # 按照filiter截取C,P c, p = mf.data_filiter(filiters[fil], data_c, data_p) p = p.T base_f = mf.math_model(p[0]) # 获取w,计算单位k/s值 w = np.load(w_names[fil]).T dfs = get_dfs_KM(w, c, p) return mf.color_diff( corrected_Mix(np.array([compose1]), w, dfs, base_f, scale[fil]), corrected_Mix(np.array([compose2]), w, dfs, base_f, scale[fil]))
def main(): epoch = 10000 LR = 0.001 w_size = 2 fil = 3 save_name_List = [ 'data_w_size6', 'data_w_size12', 'data_w_size18', 'data_w_size21_2' ] save_name = save_name_List[fil] # data_c 浓度数据,21种色浆 * 3次取点 # data_p 分光反射率数据 size: (1 + 21 * 3) * 31 , 1为基底 data_c = np.load('data_c.npy') data_p = np.load('data_p.npy') - 0.074 # 6 , 12 , 18 filiters = np.array( [[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0], [1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], [1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]) # 过滤后的数据 t_c, t_p = mf.data_filiter(filiters[fil], data_c, data_p) data_w = [] for i in range(31): data_w.append(train(t_c, t_p, i, epoch, LR, w_size)) print(data_w) np.save(save_name, data_w)
import matplotlib.pyplot as plt import Math_v2.Functions as mf import numpy as np data_c = np.load('data_c.npy') data_p = np.load('data_p.npy').T print(np.load('RealCompose_2.npy')) C = [ mf.reflectance2rgb(data_p[0]), mf.reflectance2rgb(data_p[0]), mf.reflectance2rgb(data_p[0]) ] for i in data_p[1:]: C.append(mf.reflectance2rgb(i) / 100.0) C = np.array(C).reshape(22, 3, 3) print(C) plt.figure() plt.imshow(C) plt.figsize = (150, 50 * 64) # ignore ticks plt.xticks([]) plt.yticks([]) # plt.legend() plt.savefig('梯度.pdf') plt.show()
def main(): R0 = 0.074 fil = 3 scale = [0.515, 0.515, 0.52, 0.8] extend_size = [3, 9, 15, 18] compose_size = [20, 220, 816, 1330] save_names = [ 'dataset_Corrected_size6', 'dataset_Corrected_size12', 'dataset_Corrected_size18', 'dataset_Corrected_size21' ] w_names = [ 'data_w_size6.npy', 'data_w_size12.npy', 'data_w_size18.npy', 'data_w_size21.npy', ] # data_c 浓度数据,21种色浆 * 3次取点 # data_p 分光反射率数据 size: (1 + 21 * 3) * 31 , 1为基底 data_c = np.load('data_c.npy') data_p = np.load('data_p.npy') ''' c, p = mf.data_filiter(filiters[fil], data_c, data_p) p = p.T base_f = mf.math_model(p[0]) w = np.load(w_names[fil]).T dfs = get_dfs_KM(w, c, p) dataset_size = compose_size[fil] * 2**17 sum = np.random.rand(dataset_size) a = sum * np.random.rand(dataset_size) sum -= a b = sum * np.random.rand(dataset_size) concentrations = np.concatenate((a.reshape(dataset_size,1), b.reshape(dataset_size,1), (sum - b).reshape(dataset_size,1), np.zeros((dataset_size,extend_size[fil]))),1) for i in tqdm(range(dataset_size)): np.random.shuffle(concentrations[i]) reflectance = corrected_Mix(concentrations,w,dfs,base_f,scale[fil]) np.savez(save_names[fil], concentrations = concentrations, reflectance = reflectance) ''' c, p = mf.data_filiter(filiters[fil], data_c, data_p) p = p.T - R0 # 计算基底K/S base_f = mf.math_model(p[0]) # 获取w,计算单位k/s值 w = np.load(w_names[fil]).T dfs = get_dfs_KM(w, c, p) RealCompose_2 = np.load('RealCompose_2.npy') RealIngredient_2 = np.load('RealIngredint_2.npy') Composes = np.append(RealCompose, RealCompose_2, axis=0) Ingredients = np.append(RealIngredient, RealIngredient_2, axis=0) print('\n using : ', w_names[fil]) Y1 = [] Y2 = [] Y3 = [] for com in range(Composes.shape[0]): compose = mf.data_filiter_(filiters[fil], Composes[com]) if Counter(compose)[0] == 18: y = mf.color_diff(Ingredients[com], Mix(compose, c, p + R0)) if y < 6: Y1.append(y) y = mf.color_diff( Ingredients[com], corrected_Mix(np.array([compose]), w, dfs, base_f, scale[fil]) + R0) if y < 6: Y2.append(y) y = mf.color_diff(Ingredients[com], Mix(compose, c, p) + R0) if y < 6: Y3.append(y) # Y1.append(mf.reflectance2rgb(Ingredients[com])) # Y2.append(mf.reflectance2rgb(Mix(compose,c,p + R0))) # Y3.append(mf.reflectance2rgb(corrected_Mix(np.array([compose]),w,dfs,base_f,scale[fil])+ R0)) # # # Y1 = np.array(Y1).reshape((21,1,3)) / 100.0 # Y2 = np.array(Y2).reshape((21,1,3)) / 100.0 # Y3 = np.array(Y3).reshape((21,1,3)) / 100.0 # base = np.array(mf.reflectance2rgb(data_p.T[0]))/100.0 # print(base) # one = [] # for i in range(21): # one.append(base) # one = np.array(one).reshape((21,1,3)) # Y = np.append(Y2,one,axis=1) # Y = np.append(Y, Y1, axis=1) # Y = np.append(Y, one, axis=1) # Y = np.append(Y, Y3, axis=1) plt.figure() X = np.arange(len(Y1)) plt.scatter(X, Y1, label='KM') plt.plot(X, np.repeat(sum(Y1) / len(Y1), len(Y1))) plt.text(1, sum(Y1) / len(Y1), sum(Y1) / len(Y1)) plt.scatter(X, Y3, marker='*', label='corrected_KM_1.0') plt.plot(X, np.repeat(sum(Y3) / len(Y3), len(Y3))) plt.text(1, sum(Y3) / len(Y3), sum(Y3) / len(Y3)) plt.scatter(X, Y2, marker='x', label='corrected_KM_2.0') plt.plot(X, np.repeat(sum(Y2) / len(Y2), len(Y2))) plt.text(1, sum(Y2) / len(Y2), sum(Y2) / len(Y2)) plt.xlabel('加料方案', fontproperties=font_set) plt.ylabel('色差', fontproperties=font_set) # plt.imshow(Y) # ignore ticks plt.xticks([]) plt.yticks([]) plt.legend() plt.show()
def corrected_Mix(compose, w, dfs, base_f, scale=0.56): total_c = np.sum(compose, 1) F = compose.dot(dfs) correct_F = F + correct_func(total_c * 0.78, w) * scale + base_f return mf.i_math_model(correct_F)