def test_cnn(test_image_name): test_img = mpimg.imread(test_image_name) pkl_file = open('params.pkl', 'rb') type = test_image_name.rsplit(".")[1] params = pickle.load(pkl_file) params1, params2, params3, params4, params5, params6, params7, params8, params9 = params h, w, ip_channels = test_img.shape out_channels = 64 filter_size = 3 padding = 1 layer_1 = Convolution(ip_channels, out_channels, filter_size, padding) layer_1.weights = params1[0] layer_1.bias=params1[1] relu_1 = ReLU() layer_2 = Convolution(out_channels, out_channels, filter_size, padding) layer_2.weights = params2[0] layer_2.bias = params2[1] relu_2 = ReLU() layer_3 = Convolution(out_channels, out_channels, filter_size, padding) layer_3.weights = params3[0] layer_3.bias = params3[1] relu_3 = ReLU() layer_4 = Convolution(out_channels, out_channels, filter_size, padding) layer_4.weights = params4[0] layer_4.bias = params4[1] relu_4 = ReLU() layer_5 = Convolution(out_channels, out_channels, filter_size, padding) layer_5.weights = params5[0] layer_5.bias = params5[1] relu_5 = ReLU() layer_6 = Convolution(out_channels, out_channels, filter_size, padding) layer_6.weights = params6[0] layer_6.bias = params6[1] relu_6 = ReLU() layer_7 = Convolution(out_channels, out_channels, filter_size, padding) layer_7.weights = params7[0] layer_7.bias = params7[1] relu_7 = ReLU() layer_8 = Convolution(out_channels, out_channels, filter_size, padding) layer_8.weights = params8[0] layer_8.bias = params8[1] relu_8 = ReLU() layer_9 = Convolution(out_channels, ip_channels, filter_size, padding) layer_9.weights = params9[0] layer_9.bias = params9[1] layers = [(layer_1, relu_1), (layer_2, relu_2), (layer_3, relu_3), (layer_4, relu_4), (layer_5, relu_5), (layer_6, relu_6), (layer_7, relu_7), (layer_8, relu_8), (layer_9, None)] cnn_obj = CNN(layers) out_img = cnn_obj.forward(test_img) output_file_name = test_image_name + "_sr" + str(h) + "." + type cv2.imwrite(output_file_name, out_img)
def train_cnn(self, image_name_list, epochs=1): self.train_data = [] self.train_data.extend(self.get_train_data(image_name_list)) shuffled_train_data = shuffle(self.train_data) _, _, ip_channels = shuffled_train_data[0][0].shape out_channels = 64 filter_size = 3 padding = 1 lr = 0.0001 layer_1 = Convolution(ip_channels, out_channels, filter_size, padding) relu_1 = ReLU() layer_2 = Convolution(out_channels, out_channels, filter_size, padding) relu_2 = ReLU() layer_3 = Convolution(out_channels, out_channels, filter_size, padding) relu_3 = ReLU() layer_4 = Convolution(out_channels, out_channels, filter_size, padding) relu_4 = ReLU() layer_5 = Convolution(out_channels, out_channels, filter_size, padding) relu_5 = ReLU() layer_6 = Convolution(out_channels, out_channels, filter_size, padding) relu_6 = ReLU() layer_7 = Convolution(out_channels, out_channels, filter_size, padding) relu_7 = ReLU() layer_8 = Convolution(out_channels, out_channels, filter_size, padding) relu_8 = ReLU() layer_9 = Convolution(out_channels, ip_channels, filter_size, padding) layers = [(layer_1, relu_1), (layer_2, relu_2), (layer_3, relu_3), (layer_4, relu_4), (layer_5, relu_5), (layer_6, relu_6), (layer_7, relu_7), (layer_8, relu_8), (layer_9, None)] cnn_obj = CNN(layers) for epoch in range(epochs): for x, y in self.train_data: cnn_obj = CNN(layers) loss, grads = cnn_obj.train_step(x, y) print("loss:", loss) params1, params2, params3, params4, params5, params6, params7, params8, params9 = grads dw1, db1 = params1 dw2, db2 = params2 dw3, db3 = params3 dw4, db4 = params4 dw5, db5 = params5 dw6, db6 = params6 dw7, db7 = params7 dw8, db8 = params8 dw9, db9 = params9 layer_1.weights = layer_1.weights - (lr * dw1) layer_1.bias = layer_1.bias - (lr * db1) layer_2.weights = layer_2.weights - (lr * dw2) layer_2.bias = layer_2.bias - (lr * db2) layer_3.weights = layer_3.weights - (lr * dw3) layer_3.bias = layer_3.bias - (lr * db3) layer_4.weights = layer_4.weights - (lr * dw4) layer_4.bias = layer_4.bias - (lr * db4) layer_5.weights = layer_5.weights - (lr * dw5) layer_5.bias = layer_5.bias - (lr * db5) layer_6.weights = layer_6.weights - (lr * dw6) layer_6.bias = layer_6.bias - (lr * db6) layer_7.weights = layer_7.weights - (lr * dw7) layer_7.bias = layer_7.bias - (lr * db7) layer_8.weights = layer_8.weights - (lr * dw8) layer_8.bias = layer_8.bias - (lr * db8) layer_9.weights = layer_9.weights - (lr * dw9) layer_9.bias = layer_9.bias - (lr * db9) layers = [(layer_1, relu_1), (layer_2, relu_2), (layer_3, relu_3), (layer_4, relu_4), (layer_5, relu_5), (layer_6, relu_6), (layer_7, relu_7), (layer_8, relu_8), (layer_9, None)] return cnn_obj.params, layer_9.A_curr