Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CNN MNIST Tutorial

CNN MNIST Tutorial

AhnSeongHyun

February 27, 2018
Tweet

More Decks by AhnSeongHyun

Other Decks in Technology

Transcript

  1. MNIST • MNIST는 간단한 컴퓨터 비전 데이터셋 • train-images-idx3-ubyte.gz: training

    set images (9912422 bytes) train-labels-idx1-ubyte.gz: training set labels (28881 bytes) t10k-images-idx3-ubyte.gz: test set images (1648877 bytes) t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes 2
  2. MNIST Label • 글자 이미지가 실제 어떤 글자인지를 나타낸다. •

    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] => 각 인덱스가 숫자를 나타냄 • 1 = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0] , 9 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1] 4
  3. MNIST Tensorflow 에서 불러오기 # -*- coding:utf-8 -*- from tensorflow.examples.tutorials.mnist

    import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) for key in ['train', 'test', 'validation']: print("mnist.%s :" % key) minst_dataset= getattr(mnist, key) print("\tnum_examples : %d" % minst_dataset.num_examples) print("\timages count : %d" % len(minst_dataset.images)) print("\tlabels count : %d" % len(minst_dataset.labels)) print("\timages[0] len : %d" % len(minst_dataset.images[0])) print("\tlabels[0] len : %d" % len(minst_dataset.labels[0])) print("\tlabels[0] : %s" % str(minst_dataset.labels[0])) 7
  4. MNIST Tensorflow 에서 불러오기 from tensorflow.examples.tutorials.mnist import input_data mnist =

    input_data.read_data_sets("MNIST_data/", one_hot=True) for key in ['train', 'test', 'validation']: print("mnist.%s :" % key) minst_dataset= getattr(mnist, key) print("\tnum_examples : %d" % minst_dataset.num_examples) print("\timages count : %d" % len(minst_dataset.images)) print("\tlabels count : %d" % len(minst_dataset.labels)) print("\timages[0] len : %d" % len(minst_dataset.images[0])) print("\tlabels[0] len : %d" % len(minst_dataset.labels[0])) 8
  5. tensorBoard 띄우기 Usage : tensorboard --help $ usage: tensorboard [-h]

    [--logdir LOGDIR] [--debug DEBUG] [--nodebug] [--host HOST] [--port PORT] 10
  6. tensorboard 띄우기 $ tensorboard --logdir=log $ TensorBoard b'54' at http://anui-MacBook-Pro.local:6006

    - http://127.0.0.1:6006 접속 $ tensorboard --logdir=log --host 0.0.0.0 --port 6006 - 외부에서 접속가능 11
  7. CNN 구성 # L1 ImgIn shape=(?, 28, 28, 1) #

    필터의 크기 3X3, 32개의 필터 W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01)) # Conv -> (?, 28, 28, 32) # Pool -> (?, 14, 14, 32) # 이동거리 1 L1 = tf.nn.conv2d(X_img, W1, strides=[1, 1, 1, 1], padding='SAME') L1 = tf.nn.relu(L1) L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') ''' Tensor("Conv2D:0", shape=(?, 28, 28, 32), dtype=float32) Tensor("Relu:0", shape=(?, 28, 28, 32), dtype=float32) Tensor("MaxPool:0", shape=(?, 14, 14, 32), dtype=float32) ''' 13
  8. CNN 구성 # L2 ImgIn shape=(?, 14, 14, 32) W2

    = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01)) # Conv ->(?, 14, 14, 64) # Pool ->(?, 7, 7, 64) L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME') L2 = tf.nn.relu(L2) L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') L2_flat = tf.reshape(L2, [-1, 7 * 7 * 64]) W3 = tf.get_variable("W3", shape=[7 * 7 * 64, 10], initializer=tf.contrib.layers.xavier_initializer()) b = tf.Variable(tf.random_normal([10]), name='b') logits = tf.matmul(L2_flat, W3) + b cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) 14
  9. CNN-MNIST 결과 tensorboard 연결하기 • Data Point <= Label, Images

    • metadata file • Label : .tsv 파일 • Images : 단일 sprite image, 8192 X 8192 까지 지원 • projector_config.pbtxt • API 를 이용해서 생성 16
  10. embedding = tf.Variable(tf.zeros([1024, 10]), name="test_embedding") assignment = embedding.assign(logits) saver =

    tf.train.Saver() sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter(LOGDIR + "/embedding") writer.add_graph(sess.graph) config = tf.contrib.tensorboard.plugins.projector.ProjectorConfig() embedding_config = config.embeddings.add() embedding_config.tensor_name = embedding.name embedding_config.metadata_path = LOGDIR + 'labels_1024.tsv‘ # 메타데이터 지정 embedding_config.sprite.image_path = LOGDIR + 'sprite_1024.png‘ # sprite 이미지 파일 지정 embedding_config.sprite.single_image_dim.extend([28, 28]) # 이미지 사이즈 지정 tf.contrib.tensorboard.plugins.projector.visualize_embeddings(writer, config) if i % 500 == 0: sess.run(assignment, feed_dict={X: mnist.test.images[:1024], Y: mnist.test.labels[:1024]}) saver.save(sess, os.path.join(LOGDIR, "model.ckpt"), i) 17 생성 : projector_config.pbtxt