본문 바로가기
ML

TensorFlow 도전기 (with Coursera 3일차)

by krheyjin 2021. 6. 5.
728x90
반응형

이번테마는 convolutional neural networks

1. 내가 대~충 이해한것들
-accuracy를 높이려면? 레이어층을 더 쌓아본다
-어떤 레이어 층을 쌓을까? Conv2D layers and MaxPooling2D layers in TensorFlow 를 이용.
-Convolutions(합성곱)

tf.keras.layers.Conv2D(
    filters, kernel_size, strides=(1, 1), padding='valid',
    data_format=None, dilation_rate=(1, 1), groups=1, activation=None,
    use_bias=True, kernel_initializer='glorot_uniform',
    bias_initializer='zeros', kernel_regularizer=None,
    bias_regularizer=None, activity_regularizer=None, kernel_constraint=None,
    bias_constraint=None, **kwargs
)

-pooling

tf.keras.layers.MaxPool2D(
    pool_size=(2, 2), strides=None, padding='valid', data_format=None,
    **kwargs
)

-흐음..여기까지 보는데 이해가 안가서 외부자료의 힘을 빌리기로함. 하기 포스팅이 알기 쉬웠음.
(나중에 퀴즈 나올때 이 포스팅을 잘 읽어보면서 풀면 답을 알수있었음)
http://taewan.kim/post/cnn/

 

CNN, Convolutional Neural Network 요약

Convolutional Neural Network, CNN을 정리합니다.

taewan.kim

-예전에 공부한 적이 있는 넘파이지만, 매우 가물가물하니 공식문서도 한번 읽어줌.
https://numpy.org/doc/stable/user/quickstart.html

 

NumPy quickstart — NumPy v1.20 Manual

NumPy provides familiar mathematical functions such as sin, cos, and exp. In NumPy, these are called “universal functions”(ufunc). Within NumPy, these functions operate elementwise on an array, producing an array as output. See also all, any, apply_alo

numpy.org

 

2. 이 장의 주요내용

model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(28, 28, 1)),
  tf.keras.layers.MaxPooling2D(2, 2),
  tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
  tf.keras.layers.MaxPooling2D(2,2),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(10, activation='softmax')
])

이장의 주요내용에 대해서 굉장히 이해하고 싶지만, 
어...좀 힘들다. 이걸 이해할 날이 오나?
어쨌든, 이미지 처리에 관한 예제들이 많이나오는데
이런식으로 값의 집합을 처리하여 여러가지 필터를 적용시켜주는 것은
이제 우리 주변에서 흔하게 찾아볼 수 있다. 
python을 이용해서 이런 이미지처리를 배우면
합성이나 누끼따기이 단 몇초만에 끝날 수 있게된다.
여기에서는 이런 값의 집합을 처리해서 학습후 
새로운 오브젝트에 대한 카테고라이징을 하는 것까지 나오지만
거의 대부분의 설명은 이미지처리에 대한 것이었다.
3차원의 이미지를 특징을 더 잘 나타낼수 있도록 가공하고
차원을 줄여가며 카테고라이즈 해 간다는 것 까지는 이해.


3. submit 
바로 이해하긴 힘들지만, 수료를 위해 과제를 제출해 본다.

In the videos you looked at how you would improve Fashion MNIST using Convolutions. For your exercise see if you can improve MNIST to 99.8% accuracy or more using only a single convolutional layer and a single MaxPooling 2D. You should stop training once the accuracy goes above this amount. It should happen in less than 20 epochs, so it's ok to hard code the number of epochs for training, but your training must end once it hits the above metric. If it doesn't, then you'll need to redesign your layers.
I've started the code for you -- you need to finish it!
When 99.8% accuracy has been hit, you should print out the string "Reached 99.8% accuracy so cancelling training!"

-이번에 배운거를 써먹으라는거랑, 콜백을 사용하라는 말을 한다. epoch를 20이하라고는 하나, 콜백을 때려줄테니 걍 19를 넣고 어느정도에서 멈추는지 보자.

여윽시 적는도중에 간간히 에러에러.
1) expected conv2d_2__input to have 4 dimensions, but got array with shape (10000, 28, 28)

차원이 하나 부족하다고 한다. 음????
콜백 넣고, 레이어 추가해줬는데 왜???하고 생각하던중. 아항

training_images=training_images.reshape(60000, 28, 28, 1)
training_images=training_images / 255.0
test_images = test_images.reshape(10000, 28, 28, 1)
test_images=test_images/255.0

트레이닝 이미지와 테스트이미지의 차원설정을 아얘 안넣었(노멀라이징도 슥삭)다는 사실을 알았다.
헤헷, 대게 하는 실수가 이런 실수이지만, 
에러도 한번 보았고, 이 부분이 어떤 부분이었는지 다시한번 찾아보도록한다.
이 부분은 강좌에서 이렇게 설명하고 있다.

Step 1 is to gather the data. You'll notice that there's a bit of a change here in that the training data needed to be reshaped. That's because the first convolution expects a single tensor containing everything, so instead of 60,000 28x28x1 items in a list, we have a single 4D list that is 60,000x28x28x1, and the same for the test images. If you don't do this, you'll get an error when training as the Convolutions do not recognize the shape.

3차원짜리 6만개(혹은1만개)가 아닌, 4차원짜리 하나가 필요하다고 한다.

콜백함수의 도움으로 11번만에 원하는 수치를 얻었다.

4. 3일차 끝

흐앙...어렵다~

728x90
반응형

댓글