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, tensorboard
    안성현
    1

    View full-size slide

  2. 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

    View full-size slide

  3. MNIST Images
    • 파일이 아님.
    • 하나의 이미지를 784개의 데이터로 표현
    3

    View full-size slide

  4. 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

    View full-size slide

  5. Tensorflow 설치하기
    -pip install --upgrade tensorflow
    -소스빌드해서 설치가능
    5

    View full-size slide

  6. MNIST Tensorflow 에서 불러오기
    from tensorflow.examples.tutorials.mnist import input_data
    mnist = input_data.read_data_sets("MNIST_data/")
    6

    View full-size slide

  7. 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

    View full-size slide

  8. 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

    View full-size slide

  9. urllib.error.URLError: 0SX) Python3.x SSL Error
    9

    View full-size slide

  10. tensorBoard 띄우기
    Usage : tensorboard --help
    $ usage: tensorboard [-h]
    [--logdir LOGDIR]
    [--debug DEBUG]
    [--nodebug]
    [--host HOST]
    [--port PORT]
    10

    View full-size slide

  11. 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

    View full-size slide

  12. CNN 구성
    https://hunkim.github.io/ml/
    12

    View full-size slide

  13. 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

    View full-size slide

  14. 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

    View full-size slide

  15. CNN-MNIST 결과 tensorboard 연결하기
    saver 를 이용해서 model 을 --logdir path 에 저장
    15

    View full-size slide

  16. CNN-MNIST 결과 tensorboard 연결하기
    • Data Point <= Label, Images
    • metadata file
    • Label : .tsv 파일
    • Images : 단일 sprite image, 8192 X 8192 까지 지원
    • projector_config.pbtxt
    • API 를 이용해서 생성
    16

    View full-size slide

  17. 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

    View full-size slide

  18. CNN-MNIST 결과 tensorboard 연결하기
    • https://github.com/jireh-father/tensorboard-embedding-visualization
    18

    View full-size slide

  19. Reference
    • https://tensorflowkorea.gitbooks.io/tensorflow-kr/content/g3doc/tutorials/mnist/beginners/
    • http://bcho.tistory.com/1154
    • https://www.google.co.kr/search?q=image+to+mnist+format&oq=image+to+mnisr+&aqs=chrome.1.69i57j0l2.6888j0j4&sourceid=chrom
    e&ie=UTF-8
    • http://goodtogreate.tistory.com/entry/Hands-on-TensorBoard-TensorFlow-Dev-Summit2017
    • 모두의 딥러닝 : https://hunkim.github.io/ml/
    • http://sanghyukchun.github.io/75/
    • http://yujuwon.tistory.com/entry/TENSORFLOWCNN2
    • http://yujuwon.tistory.com/entry/TENSORFLOW-Image-AutoEncoder
    19

    View full-size slide