■ 노이즈 제거 오토 인코더를 만드는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import matplotlib.pyplot as pp import numpy as np import tensorflow as tf import tensorflow.examples.tutorials.mnist as mnist def DrawResult(originalImageNDArray, noisyImageNDArray, outputImageNDArray): pp.matshow(np.reshape(originalImageNDArray, (28, 28)), cmap = pp.get_cmap("gray")) pp.title("Original Image") pp.colorbar() pp.matshow(np.reshape(noisyImageNDArray, (28, 28)), cmap = pp.get_cmap("gray")) pp.title("Input Image") pp.colorbar() pp.matshow(np.reshape(outputImageNDArray, (28, 28)), cmap = pp.get_cmap("gray")) pp.title("Reconstructed Image") pp.colorbar() pp.show() # 네트워크 파라미터 InputLayerNodeCount = 784 hiddenLayer1NodeCount = 256 hiddenLayer2NodeCount = 256 outputLayerNodeCount = 784 epochCount = 100 batchSize = 100 displayStep = 10 print("MNIST 데이터 로드를 시작합니다.") mnistDatasets = mnist.input_data.read_data_sets("data", one_hot = True) print("MNIST 데이터 로드를 종료합니다.") # 네트워크 모델을 정의한다. inputLayerTensor = tf.placeholder("float", [None, InputLayerNodeCount ]) correctOutputTensor = tf.placeholder("float", [None, outputLayerNodeCount]) dropoutRateTensor = tf.placeholder("float") hiddenLayer1WeightVariable = tf.Variable(tf.random_normal([InputLayerNodeCount , hiddenLayer1NodeCount])) hiddenLayer2WeightVariable = tf.Variable(tf.random_normal([hiddenLayer1NodeCount, hiddenLayer2NodeCount])) outputLayerWeightVariable = tf.Variable(tf.random_normal([hiddenLayer2NodeCount, outputLayerNodeCount ])) hiddenLayer1BiasVariable = tf.Variable(tf.random_normal([hiddenLayer1NodeCount])) hiddenLayer2BiasVariable = tf.Variable(tf.random_normal([hiddenLayer2NodeCount])) outputLayerBiasVariable = tf.Variable(tf.random_normal([outputLayerNodeCount ])) hiddenLayer1OutputTensor = tf.nn.sigmoid(tf.add(tf.matmul(inputLayerTensor, hiddenLayer1WeightVariable), hiddenLayer1BiasVariable)) hiddenLayer1OutputTensorDropout = tf.nn.dropout(hiddenLayer1OutputTensor, dropoutRateTensor) hiddenLayer2OutputTensor = tf.nn.sigmoid(tf.add(tf.matmul(hiddenLayer1OutputTensorDropout, hiddenLayer2WeightVariable), hiddenLayer2BiasVariable)) hiddenLayer2OutputTensorDropout = tf.nn.dropout(hiddenLayer2OutputTensor, dropoutRateTensor) outputLayerOutputTensor = tf.nn.sigmoid(tf.matmul(hiddenLayer2OutputTensorDropout, outputLayerWeightVariable) + outputLayerBiasVariable) costTensor = tf.reduce_mean(tf.pow(outputLayerOutputTensor - correctOutputTensor, 2)) optmizerOperation = tf.train.RMSPropOptimizer(0.01).minimize(costTensor) # 그래를 실행한다. with tf.Session() as session: session.run(tf.global_variables_initializer()) print("훈련을 시작합니다.") for epoch in range(epochCount): batchCount = int(mnistDatasets.train.num_examples / batchSize) totalCost = 0. for batch in range(batchCount): batchInputNDArray, batchCorrectOutputNDArray = mnistDatasets.train.next_batch(batchSize) noisyBatchInputNDArray = batchInputNDArray + 0.3 * np.random.randn(batchSize, 784) feedDictionary = {inputLayerTensor : noisyBatchInputNDArray, correctOutputTensor : batchInputNDArray, dropoutRateTensor : 0.8} session.run(optmizerOperation, feed_dict = feedDictionary) totalCost += session.run(costTensor, feed_dict = feedDictionary) if epoch % displayStep == 0: print("Epoch : %02d/%02d, 평균 비용 : %.6f" % (epoch, epochCount, totalCost / batchCount)) print("테스트를 시작합니다.") testIndex = np.random.randint(mnistDatasets.test.images.shape[0], size = 1) originalImageNDArray = mnistDatasets.test.images[testIndex, :] testImageNDArray = mnistDatasets.test.images[testIndex, :] testImageLabel = np.argmax(mnistDatasets.test.labels[testIndex, :], 1) print("테스트 이미지 라벨 : %d" % (testImageLabel)) noisyTestImageNDArray = testImageNDArray + 0.3 * np.random.randn(1, 784) outputImageNDArray = session.run(outputLayerOutputTensor, feed_dict = {inputLayerTensor : noisyTestImageNDArray, dropoutRateTensor : 1}) DrawResult(originalImageNDArray, noisyTestImageNDArray, outputImageNDArray) print("훈련을 다시 시작합니다.") |