처음에 했던 내용은 AlexNet과 CNN에 대한 설명이었습니다.
시간이 허락하면 하려고 했었더 GoogLeNet에 대한 발표자료 입니다.
이 내용은 다음주에 다루도록 하겠습니다. 이 네트워크는 우리가 구글 이미지 검색, 이미지 분류 등에 실제로 사용되는 구조이기 때문에 알아두면 좋을 것 같아요.
코드를 올리기 전에 버츄어박스에 우분투를 설치하고
설치된 우분투에 아나콘다, 텐서플로우, 오픈시비를 설치하는
발표 자료를 올립니다. 꼭! 따라해보세요. (윈도우에서 버쳐 박스를 사용하는 분들은요)
그리고 나선 혼돈의 카오스였던, 어둠의 다크와 같았던 실습입니다.
먼저 파이썬의 기초적인 것을 다뤘었죠.
#
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 | """ Python 101 """ import numpy as np import tensorflow as tf import os from scipy import misc import matplotlib.pyplot as plt import cv2 # Get current folder cwd = os.getcwd() print "Current Folder is %s" %(cwd) # Load Image cat = misc.imread(cwd + "/cat.jpg") print "Type of cat is %s" % type(cat) print "Shape of cat is %s" % (cat.shape,) cat2 = cv2.imread(cwd + "/cat.jpg") print "Shape of cat2 is %s" % (cat2.shape,) cv2.imwrite("cat2.png", cat2) # Extract single channel cat_R = cat[:, :, 2] print "Type of cat is %s" % type(cat_R) print "Shape of cat is %s" % (cat_R.shape,) # Plot!! plt.figure(0) plt.imshow(cat) plt.title("Original Image") # plt.figure(1) # 1. # plt.imshow(cat_R) # 2. a = plt.matshow(cat_R, fignum=1, cmap=plt.get_cmap("gray")) plt.colorbar(a) # 3. # plt.colorbar(plt.matshow(cat_R, fignum=1, cmap=plt.get_cmap("gray"))) plt.title("Red Image") # Resize catsmall = misc.imresize(cat, [100, 100, 1]) print "\nsize of catsmall is %s" % (catsmall.shape,) print "type of catsmall is", type(catsmall) def rgb2gray(rgb): return np.dot(rgb[... , :3], [0.299, 0.587, 0.114]) catsmallgray = rgb2gray(catsmall) print "\nsize of catsmallgray is %s" % (catsmallgray.shape,) print "type of catsmallgray is", type(catsmallgray) # Convert to Vector // catrowvec = np.reshape(catsmallgray, (1, -1)) print "\nsize of catrowvec is %s" % (catrowvec.shape,) print "type of catrowvec is", type(catrowvec) # Convert to Matrix catmatrix = np.reshape(catrowvec, (100, -1)); print "\nsize of catmatrix is %s" % (catmatrix.shape,) print "type of catmatrix is", type(catmatrix) # Plot print "\n\n" plt.show() print "AAAA" | cs |
#
그리고 나선 텐서플로우 기초를 다뤘었습니다.
#
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | """ Basic TensorFlow Sungjoon Choi (sungjoon.choi@cpslab.snu.ac.kr) """ import numpy as np import tensorflow as tf hello = tf.constant("Hello, it's me.") print hello # Tensor("Const:0", shape=(), dtype=string) """ This will not show Hello, it's me. """ """ In order to make things happen, we need 'session'! """ sess = tf.Session() """ Run session with tf variable """ hello_out = sess.run(hello) print "Type of 'hello' is ", type(hello) print "Type of 'hello_out' is ", type(hello_out) print hello_out """ Until you run session, nothing happens! """ print "Until you run session, nothing happens!" """ There are other types as well 1. Constant types 2. Operators 3. Variables 4. Placeholder (Buffers) """ """ Constant types """ print "\nConstant types (numpy)" a = tf.constant(1.5) b = tf.constant(2.5) print " 'a': ", a, " Type is ", type(a) print " 'b': ", b, " Type is ", type(b) a_out = sess.run(a) b_out = sess.run(b) print " Type of 'a_out' is ", type(a_out) print " Type of 'b_out' is ", type(b_out) print " a_out is ", a_out, "b_out is ", b_out, "a_out+b_out is ", a_out+b_out """ Operators are also tf variables """ print "\nOperators (tf.add, tf.mul)" add = tf.add(a, b) print " 'add' is ", add, ' type is ', type(add) add_out = sess.run(add) print " 'add_out' is ", add_out, ' type is ', type(add_out) mul = tf.mul(a, b) print " 'mul' is ", mul, ' type is ', type(mul) mul_out = sess.run(mul) print " 'mul_out' is ", mul_out, ' type is ', type(mul_out) """ Variables & PlaceHolder """ print "\nVariables & PlaceHolders" X = np.random.rand(1, 20) Input = tf.placeholder(tf.float32, [None, 20]) Weight = tf.Variable(tf.random_normal([20, 10], stddev=0.5)) Bias = tf.Variable(tf.zeros([1, 10])) print " 'Weight': ", Weight, " Type is ", type(Weight) print " 'Bias': ", Bias, " Type is ", type(Bias) # Weight_out = sess.run(Weight) # <= This is not allowed! # print Weight.eval(sess) # <= This is not also allowed! (Do You Know Why??) """ Initialize Variables """ print "\nInitialize Variables" init = tf.initialize_all_variables() sess.run(init) print " 'Weight': ", Weight, " Type is ", type(Weight) print " 'Bias': ", Bias, " Type is ", type(Bias) print Weight.eval(sess) """ Operations with Variables and PlaceHolders """ print "\nOperations with Variables and PlaceHolders" oper = tf.matmul(Input, Weight) + Bias val = sess.run(oper, feed_dict={Input:X}) print " oper is ", oper, " type is ", type(oper) print " val is ", val, " type is ", type(val) """ Operators with PlaceHolder (This is very important !) (Remember 'feed_dict' !!!) """ print "\nOperators with PlaceHolder (tf.add, tf.mul)" x = tf.placeholder(tf.float32) y = tf.placeholder(tf.float32) add = tf.add(x, y) mul = tf.mul(x, y) add_out = sess.run(add, feed_dict={x:5.0, y:6.0}) mul_out = sess.run(mul, feed_dict={x:5.0, y:6.0}) print " addres: ", add_out, " type is ", type(add_out) print " mulres: ", mul_out, " type is ", type(mul_out) print "\n If you see this, then it's good. " | cs |
#
그리고 수업 시간에 말씀 드렸던, 코드 입니다.
이 코드가 돌아가면 기본적인 텐서플로우가 다 동작을 한다고 봐도 무관합니다.
#
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 | import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_data def init_weights(shape): return tf.Variable(tf.random_normal(shape, stddev=0.01)) def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden): l1a = tf.nn.relu(tf.nn.conv2d(X, w, [1, 1, 1, 1], 'SAME')) l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') l1 = tf.nn.dropout(l1, p_keep_conv) l2a = tf.nn.relu(tf.nn.conv2d(l1, w2, [1, 1, 1, 1], 'SAME')) l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') l2 = tf.nn.dropout(l2, p_keep_conv) l3a = tf.nn.relu(tf.nn.conv2d(l2, w3, [1, 1, 1, 1], 'SAME')) l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]]) l3 = tf.nn.dropout(l3, p_keep_conv) l4 = tf.nn.relu(tf.matmul(l3, w4)) l4 = tf.nn.dropout(l4, p_keep_hidden) pyx = tf.matmul(l4, w_o) return pyx mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels trX = trX.reshape(-1, 28, 28, 1) teX = teX.reshape(-1, 28, 28, 1) X = tf.placeholder("float", [None, 28, 28, 1]) Y = tf.placeholder("float", [None, 10]) w = init_weights([3, 3, 1, 32]) w2 = init_weights([3, 3, 32, 64]) w3 = init_weights([3, 3, 64, 128]) w4 = init_weights([128 * 4 * 4, 625]) w_o = init_weights([625, 10]) p_keep_conv = tf.placeholder("float") p_keep_hidden = tf.placeholder("float") py_x = model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden) cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y)) train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost) predict_op = tf.argmax(py_x, 1) sess = tf.Session() init = tf.initialize_all_variables() sess.run(init) for i in range(100): for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)): sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end], p_keep_conv: 0.8, p_keep_hidden: 0.5}) test_indices = np.arange(len(teX)) # Get A Test Batch np.random.shuffle(test_indices) test_indices = test_indices[0:256] print i, np.mean(np.argmax(teY[test_indices], axis=1) == sess.run(predict_op, feed_dict={X: teX[test_indices], Y: teY[test_indices], p_keep_conv: 1.0, p_keep_hidden: 1.0})) | cs |
#
'Enginius > Python&TensorFlow' 카테고리의 다른 글
GitHub basics (0) | 2016.04.11 |
---|---|
JupyterHub on AWS EC2 (0) | 2016.04.08 |
AWS EC2 Ubuntu 사용하기 + Jupyterhub 써보기 (0) | 2016.04.07 |
Basic Python Usage (0) | 2016.04.06 |
TensorFlow 맛보기 (2) | 2016.03.02 |