Shingome/Image_Processing
0
1import numpy as np2import matplotlib.pyplot as plt3 4 5# Hyper arguments6INPUT_DIM = 647OUT_DIM = 58H1_DIM = 169H2_DIM = 1010 11ALPHA = 0.00512NUM_EPOCHS = 10013 14# Global15img_map = []16loss_arr = []17 18 19def relu(t):20 return np.maximum(t, 0)21 22 23def softmax(t):24 out = np.exp(t)25 return out / np.sum(out)26 27 28def sparse_cross_entropy(z, y):29 return -np.log(z[0, y])30 31 32def to_full(y, num_classes):33 y_full = np.zeros((1, num_classes))34 y_full[0, y] = 135 return y_full36 37 38def relu_deriv(t):39 return (t >= 0).astype(float)40 41 42def predict(x):43 t1 = x @ W1 + b144 h1 = relu(t1)45 t2 = h1 @ W2 + b246 h2 = relu(t2)47 t3 = h2 @ W3 + b348 z = softmax(t3)49 return z50 51 52def calc_accuracy():53 correct = 054 for y, x in dataset:55 z = predict(x)56 y_pred = np.argmax(z)57 if y_pred == y:58 correct += 159 acc = correct / len(dataset)60 return acc61 62 63def create_map():64 for x in image:65 z = predict(x)66 y_pred = np.argmax(z)67 img_map.append(y_pred)68 69 70if __name__ == "__main__":71 # Load infomation72 dataset = np.load('./../dataset.npy', allow_pickle=True)73 74 image = np.load('./../image.npy')75 76 # Random synapses77 W1 = np.random.rand(INPUT_DIM, H1_DIM)78 b1 = np.random.rand(1, H1_DIM)79 W2 = np.random.rand(H1_DIM, H2_DIM)80 b2 = np.random.rand(1, H2_DIM)81 W3 = np.random.rand(H2_DIM, OUT_DIM)82 b3 = np.random.rand(1, OUT_DIM)83 84 W1 = (W1 - 0.5) * 2 * np.sqrt(1/INPUT_DIM)85 b1 = (b1 - 0.5) * 2 * np.sqrt(1/INPUT_DIM)86 W2 = (W2 - 0.5) * 2 * np.sqrt(1/H1_DIM)87 b2 = (b2 - 0.5) * 2 * np.sqrt(1/H1_DIM)88 W3 = (W3 - 0.5) * 2 * np.sqrt(1/H2_DIM)89 b3 = (b3 - 0.5) * 2 * np.sqrt(1/H2_DIM)90 91 loss = 092 93 # Backpropagation94 for ep in range(NUM_EPOCHS):95 print(ep)96 np.random.shuffle(dataset)97 for i in range(len(dataset)):98 99 x = dataset[i][1]100 y = dataset[i][0]101 102 # Forward103 t1 = x @ W1 + b1104 h1 = relu(t1)105 t2 = h1 @ W2 + b2106 h2 = relu(t2)107 t3 = h2 @ W3 + b3108 z = softmax(t3)109 E = sparse_cross_entropy(z, y)110 111 # Backward112 y_full = to_full(y, OUT_DIM)113 dE_dt3 = z - y_full114 dE_dW3 = h2.T @ dE_dt3115 dE_db3 = np.sum(dE_dt3, axis=0, keepdims=True)116 dE_dh2 = dE_dt3 @ W3.T117 dE_dt2 = dE_dh2 * relu_deriv(t2)118 dE_dW2 = h1.T @ dE_dt2119 dE_db2 = np.sum(dE_dt2, axis=0, keepdims=True)120 dE_dh1 = dE_dt2 @ W2.T121 dE_dt1 = dE_dh1 * relu_deriv(t1)122 dE_dW1 = x.T @ dE_dt1123 dE_db1 = np.sum(dE_dt1, axis=0, keepdims=True)124 125 # Update126 W1 = W1 - ALPHA * dE_dW1127 b1 = b1 - ALPHA * dE_db1128 W2 = W2 - ALPHA * dE_dW2129 b2 = b2 - ALPHA * dE_db2130 W3 = W3 - ALPHA * dE_dW3131 b3 = b3 - ALPHA * dE_db3132 133 loss += E134 135 loss_arr.append(loss)136 loss = 0137 138 # Accuracy139 accuracy = calc_accuracy()140 print("Accuracy:", accuracy)141 142 # Map143 create_map()144 np.save('map', np.asarray(img_map))145 146 # Plot147 plt.plot(loss_arr)148 plt.show()149 150 # Save synapses151 np.savez('./../synapses', W1, b1, W2, b2, W3, b3)152 