이미지 인식을 하기 위해서 아래 그림 처럼 처리하면됩니다.

(아래 그림에서는 빠졌는데 Dropout를 처리해주면 더 좋은 결과를 얻을수 있습니다.)






[사례]

CNN Demo :  https://cs.stanford.edu/people/karpathy/convnetjs/demo/cifar10.html

CT 영상에서 폐결절 환자의 폐암 진단 알고리즘 : https://www.slideshare.net/GYLee3/ss-72966495


[학습 자료 ] Convolutional Neural Networks

홍콩 과기대 김성훈 교수 (https://hunkim.github.io/ml/)


Ex)간단히 명령어 해석

conv2d = tf.nn.conv2d(image, weight, strides=[1,1,1,1], padding='SAME')

*image.shape   -- (1,7,6,3) : 1(이미지갯수 만일"-1"이면 갯수 정하지 않음) 7x6크기x 3(RGB)

*weight.shape  -- (5,4,3,2) : 5x4크기x 3(RGB) x2(Filter 갯수)

*strides[1,1,1,1] --  1(?),,1(x축 이동),1(y축 이동), 1(?)

*padding ='SAME' -- CNN후 대상 이미지와 같은 사이즈 결과물 만들기


--------------------------------------------------------------------------------------------------------------------------------

CNN MNIST : 99% https://github.com/hunkim/DeepLearningZeroToAll/blob/master/lab-11-2-mnist_deep_cnn.py

[해석]

X = tf.placeholder(tf.float32, [None, 784])    #  None(이미지 갯수 정의하지 않음), 784(784 pixel/1 image)

X_img = tf.reshape(X, [-1, 28, 28, 1])           # -1(이미지 갯수 정의하지 않음), 28x28(size), 1(black/white)

Y = tf.placeholder(tf.float32, [None, 10])      # None(이미지 갯수 정의하지 않음), 0~9까지의 숫자


############## Convolutional layer two layer로 구성

#Layer 1 Image(?,28,28,1)

W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01)) #3x3(size) , 1(Black&white) , 32 of filters


L1 = tf.nn.conv2d(X_img, W1, strides=[1, 1, 1, 1], padding='SAME') #strides 1(?),1(x축),1(y축),1(?), padding=SAME CNN후 대상과 같은 크기 만들기

L1 = tf.nn.relu(L1)

L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') #ksize(kernerl size) 1(?)2x2(szie),1(?), strides1(?),2(x축),2(y축),1(?)

L1 = tf.nn.dropout(L1, keep_prob=keep_prob)


#Layer 2 Image(?,14,14,32)

W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01))  #3x3, 32(앞 layer의 filter 수), 64(이번 layer 정한 filter수)


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 = tf.nn.dropout(L2, keep_prob=keep_prob)

#Max Pool 후 image(?,7,7,64)


L2_flat = tf.reshape(L2, [-1, 7 * 7 * 64])     #one line으로 펼치기 위함. -1(N개의 값), 1 image 당 7*7*64(=3136)개의 값


############## Fully connected two layer로 구성

# First layer 출력을 625로 설정

W3 = tf.get_variable("W3", shape=[7 * 7 * 64, 625], initializer=tf.contrib.layers.xavier_initializer()) #7*7*64갯수, 625출력

b3 = tf.Variable(tf.random_normal([625]))

L3 = tf.nn.relu(tf.matmul(L2_flat, W3) + b3)

L3 = tf.nn.dropout(L3, keep_prob=keep_prob)



# Second Layer(Final)  625 inputs -> 10 outputs(0~9까지의 숫자)

W4 = tf.get_variable("W4", shape=[625, 10], initializer=tf.contrib.layers.xavier_initializer())

b4 = tf.Variable(tf.random_normal([10]))

logits = tf.matmul(L4, W5) + b5


# define cost/loss & optimizer

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)


-------------------------------------------------------------------------------------------------------------------------------

위 CNN 예제를 Class로 변경 예제 : https://github.com/hunkim/DeepLearningZeroToAll/blob/master/lab-11-3-mnist_cnn_class.py


-------------------------------------------------------------------------------------------------------------------------------


클래스 만들때 참조. tf.layers 예제 : https://github.com/hunkim/DeepLearningZeroToAll/blob/master/lab-11-4-mnist_cnn_layers.py

# Convolutional Layer #1

conv1 = tf.layers.conv2d(inputs=X_img,filters=32,kernel_size=[3,3],padding="SAME",activation=tf.nn.relu)

pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], padding="SAME", strides=2)

dropout1 = tf.layers.dropout(inputs=pool1,rate=0.7, training=self.training)


# Convolutional Layer #2

conv2 = tf.layers.conv2d(inputs=dropout1,filters=64,kernel_size=[3,3],padding="SAME",activation=tf.nn.relu)

flat = tf.reshape(dropout3, [-1, 128 * 4 * 4])

dense4 = tf.layers.dense(inputs=flat, units=625, activation=tf.nn.relu)

dropout4 = tf.layers.dropout(inputs=dense4, rate=0.5, training=self.training)

...

-------------------------------------------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------------------------------------------


또 다른 실습 코드 : 포스팅 하단에 보면 존재. 50번 반복에 정확도 95.5%

https://medium.com/@jongdae.lim/%EA%B8%B0%EA%B3%84-%ED%95%99%EC%8A%B5-machine-learning-%EC%9D%80-%EC%A6%90%EA%B2%81%EB%8B%A4-part-3-928a841a3aa


-------------------------------------------------------------------------------------------------------------------------------


다른 학습 자료 : 조대협씨 연예인 얼굴 인식 모델을 만들어 보자. http://bcho.tistory.com/1178

-------------------------------------------------------------------------------------------------------------------------------



[실습] 새 분류기 만들기 

무료 CIFAR10 Data(6,000장 새 사진& 52,000장 새가 아닌 사진) : https://www.cs.toronto.edu/~kriz/cifar.html  

Caltech-UCSD Birds-200-2011 Data (12,000장 새 사진) : http://www.vision.caltech.edu/visipedia/CUB-200-2011.html



+ Recent posts