** Jupiter notebook 이용
MNIST 손글씨 이미지 분류하기
ㄴ 0∼9까지 10가지로 분류될 수 있는 손글씨
ㄴ 숫자 이미지 70,000개
ㄴ train-set 60,000개, test-set 10,000개로 구성
ㄴ 28x28 픽셀로 구성되어 있음
ㄴ 0∼255 사이의 숫자 행렬로 표현됨
mnist 데이터에서 5만 출력
mnist_imshow01.ipynb
import numpy as np
from keras.datasets import mnist # 손글씨 이미지 데이터
import matplotlib.pyplot as plt # 시각화
(x_train, y_train), (x_test, y_test) = mnist.load_data() # x_train :이미지, y_train : 라벨
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
print(x_train[0])
print(y_train[0])
plt.imshow(x_train[0], 'gray') # 0번째 값(숫자 5) 회색으로 출력
plt.show()
[[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 3 18 18 18 126 136 175 26 166 255 247 127 0 0 0 0]
[ 0 0 0 0 0 0 0 0 30 36 94 154 170 253 253 253 253 253 225 172 253 242 195 64 0 0 0 0]
[ 0 0 0 0 0 0 0 49 238 253 253 253 253 253 253 253 253 251 93 82 82 56 39 0 0 0 0 0]
[ 0 0 0 0 0 0 0 18 219 253 253 253 253 253 198 182 247 241 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 80 156 107 253 253 205 11 0 43 154 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 14 1 154 253 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 139 253 190 2 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 11 190 253 70 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 35 241 225 160 108 1 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 81 240 253 253 119 25 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 186 253 253 150 27 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 93 252 253 187 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 249 253 249 64 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 130 183 253 253 207 2 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 39 148 229 253 253 253 250 182 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 24 114 221 253 253 253 253 201 78 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 23 66 213 253 253 253 253 198 81 2 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 18 171 219 253 253 253 253 195 80 9 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 55 172 226 253 253 253 253 244 133 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 136 253 253 253 212 135 132 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
5
ㄴ 이미지는 0∼255 사이의 값을 갖는 28x28 크기의 NumPy array
ㄴ 이미지는 디지털화된 형태의 픽셀(pixel)들로 구성됨
ㄴ 각 픽셀은 0에서 255 사이의 값을 가질 수 있는데, 이는 색상의 강도(intensity)를 나타냄
ㄴ 0 이 가장 어두운 색이며, 숫자가 높을 수록 밝은 색
= > 0 : 검정색 / 255 : 흰색
mnist 데이터 전체 출력
mnist_imshow02.ipynb
import numpy as np
from keras.datasets import mnist
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = mnist.load_data() # x_train :이미지, y_train : 라벨
class_names = ['0', '1', '2', '3', '4',
'5', '6', '7', '8', '9']
# class_names = range
plt.figure(figsize = (10, 10))
for i in range(25):
plt.subplot(5, 5, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(x_train[i], cmap = plt.cm.binary)
plt.xlabel(class_names[y_train[i]])
#plt.imshow(x_train[0], 'gray')
plt.show()
FASHION MNIST
ㄴ train- set 60,000개, test-set 10,000개로 구성된 이미지 데이터 세트
ㄴ 10개 클래스의 레이블과 28 x 28 회색조 이미지
Label | Label Description |
0 | T-shirt/top |
1 | Trouser |
2 | Pullover |
3 | Dress |
4 | Coat |
5 | Sandal |
6 | Shirt |
7 | Sneaker |
8 | Bag |
9 | Ankle boot |
fashion_mnist 에서 0번째 데이터 출력
fashionMnist_imshow01.ipynb
import numpy as np
from keras.datasets import fashion_mnist
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() # x_train :이미지, y_train : 라벨
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
print(x_train[0])
print(y_train[0])
# plt.imshow(x_train[0], 'gray')
plt.imshow(x_train[10], 'gray')
plt.show()
[[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 13 73 0 0 1 4 0 0 0 0 1 1 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 3 0 36 136 127 62 54 0 0 0 1 3 4 0 0 3]
[ 0 0 0 0 0 0 0 0 0 0 0 0 6 0 102 204 176 134 144 123 23 0 0 0 0 12 10 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 155 236 207 178 107 156 161 109 64 23 77 130 72 15]
[ 0 0 0 0 0 0 0 0 0 0 0 1 0 69 207 223 218 216 216 163 127 121 122 146 141 88 172 66]
[ 0 0 0 0 0 0 0 0 0 1 1 1 0 200 232 232 233 229 223 223 215 213 164 127 123 196 229 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 183 225 216 223 228 235 227 224 222 224 221 223 245 173 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 193 228 218 213 198 180 212 210 211 213 223 220 243 202 0]
[ 0 0 0 0 0 0 0 0 0 1 3 0 12 219 220 212 218 192 169 227 208 218 224 212 226 197 209 52]
[ 0 0 0 0 0 0 0 0 0 0 6 0 99 244 222 220 218 203 198 221 215 213 222 220 245 119 167 56]
[ 0 0 0 0 0 0 0 0 0 4 0 0 55 236 228 230 228 240 232 213 218 223 234 217 217 209 92 0]
[ 0 0 1 4 6 7 2 0 0 0 0 0 237 226 217 223 222 219 222 221 216 223 229 215 218 255 77 0]
[ 0 3 0 0 0 0 0 0 0 62 145 204 228 207 213 221 218 208 211 218 224 223 219 215 224 244 159 0]
[ 0 0 0 0 18 44 82 107 189 228 220 222 217 226 200 205 211 230 224 234 176 188 250 248 233 238 215 0]
[ 0 57 187 208 224 221 224 208 204 214 208 209 200 159 245 193 206 223 255 255 221 234 221 211 220 232 246 0]
[ 3 202 228 224 221 211 211 214 205 205 205 220 240 80 150 255 229 221 188 154 191 210 204 209 222 228 225 0]
[ 98 233 198 210 222 229 229 234 249 220 194 215 217 241 65 73 106 117 168 219 221 215 217 223 223 224 229 29]
[ 75 204 212 204 193 205 211 225 216 185 197 206 198 213 240 195 227 245 239 223 218 212 209 222 220 221 230 67]
[ 48 203 183 194 213 197 185 190 194 192 202 214 219 221 220 236 225 216 199 206 186 181 177 172 181 205 206 115]
[ 0 122 219 193 179 171 183 196 204 210 213 207 211 210 200 196 194 191 195 191 198 192 176 156 167 177 210 92]
[ 0 0 74 189 212 191 175 172 175 181 185 188 189 188 193 198 204 209 210 210 211 188 188 194 192 216 170 0]
[ 2 0 0 0 66 200 222 237 239 242 246 243 244 221 220 193 191 179 182 182 181 176 166 168 99 58 0 0]
[ 0 0 0 0 0 0 0 40 61 44 72 41 35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
fashion_mnist 데이터 전체 출력
fashionMnist_imshow01.ipynb
import numpy as np
from keras.datasets import fashion_mnist
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() # x_train :이미지, y_train : 라벨
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# class_names = range
plt.figure(figsize = (10, 10))
for i in range(25):
plt.subplot(5, 5, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(x_train[i], cmap = plt.cm.binary)
plt.xlabel(class_names[y_train[i]])
#plt.imshow(x_train[0], 'gray')
plt.show()
CIFAR-10 이미지 분류
ㄴ 10개 클래스의 60000개 32x32 컬러 이미지로 구성되며 클래스당 6000개의 이미지
ㄴ 50000개의 훈련 이미지와 10000개의 테스트 이미지
Label | Description |
0 | airplane |
1 | automobile |
2 | bird |
3 | cat |
4 | deer |
5 | dog |
6 | frog |
7 | horse |
8 | ship |
9 | truck |
cifar10 데이터에서 0번째 데이터 출력
cifar10_imshow01.ipynb
import numpy as np
from keras.datasets import cifar10
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = cifar10.load_data() # x_train :이미지, y_train : 라벨
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
print(x_train[0])
print(y_train[0])
plt.imshow(x_train[0])
plt.show()
(50000, 32, 32, 3) (50000, 1)
(10000, 32, 32, 3) (10000, 1)
[[[ 59 62 63]
[ 43 46 45]
[ 50 48 43]
...
[158 132 108]
[152 125 102]
[148 124 103]]
[[ 16 20 20]
[ 0 0 0]
[ 18 8 0]
...
[123 88 55]
[119 83 50]
[122 87 57]]
[[ 25 24 21]
[ 16 7 0]
[ 49 27 8]
...
[118 84 50]
[120 84 50]
[109 73 42]]
...
[[208 170 96]
[201 153 34]
[198 161 26]
...
[160 133 70]
[ 56 31 7]
[ 53 34 20]]
[[180 139 96]
[173 123 42]
[186 144 30]
...
[184 148 94]
[ 97 62 34]
[ 83 53 34]]
[[177 144 116]
[168 129 94]
[179 142 87]
...
[216 184 140]
[151 118 84]
[123 92 72]]]
[6]
cifar10 데이터 전체 출력
cifar10_imshow02.ipynb
import numpy as np
from keras.datasets import cifar10
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = cifar10.load_data() # x_train :이미지, y_train : 라벨
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
# class_names = range
plt.figure(figsize = (10, 10))
for i in range(25):
plt.subplot(5, 5, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(x_train[i], cmap = plt.cm.binary)
plt.xlabel(class_names[y_train[i][0]])
#plt.imshow(x_train[0], 'gray')
plt.show()
CIFAR-100 이미지 분류
ㄴ 이 데이터 세트는 각각 600개의 이미지를 포함하는 100개의 클래스가 있다는 점을 제외하면 CIFAR-10과 동일함
label = ['apple', 'aquarium_fish', 'baby', 'bear', 'beaver', 'bed', 'bee', 'beetle',
'bicycle', 'bottle', 'bowl', 'boy', 'bridge', 'bus', 'butterfly', 'camel', 'can', 'castle',
'caterpillar', 'cattle', 'chair', 'chimpanzee', 'clock', 'cloud', 'cockroach', 'couch', 'crab',
'crocodile', 'cup', 'dinosaur', 'dolphin', 'elephant', 'flatfish', 'forest', 'fox', 'girl',
'hamster', 'house', 'kangaroo', 'computer_keyboard', 'lamp', 'lawn_mower', 'leopard', 'lion',
'lizard', 'lobster', 'man', 'maple_tree', 'motorcycle', 'mountain', 'mouse', 'mushroom', 'oak_tree',
'orange', 'orchid', 'otter', 'palm_tree', 'pear', 'pickup_truck', 'pine_tree', 'plain', 'plate',
'poppy', 'porcupine', 'possum', 'rabbit', 'raccoon', 'ray', 'road', 'rocket', 'rose',
'sea', 'seal', 'shark', 'shrew', 'skunk', 'skyscraper', 'snail', 'snake', 'spider', 'squirrel',
'streetcar', 'sunflower', 'sweet_pepper', 'table', 'tank', 'telephone', 'television', 'tiger',
'tractor', 'train', 'trout', 'tulip', 'turtle', 'wardrobe', 'whale', 'willow_tree', 'wolf', 'woman',
'worm']
cifar100 데이터에서 0번째 데이터 출력
cifar100_imshow01.ipynb
import numpy as np
from keras.datasets import cifar100
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = cifar100.load_data() # x_train :이미지, y_train : 라벨
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
print(x_train[0])
print(y_train[0])
plt.imshow(x_train[0])
plt.show()
(50000, 32, 32, 3) (50000, 1)
(10000, 32, 32, 3) (10000, 1)
[[[255 255 255]
[255 255 255]
[255 255 255]
...
[195 205 193]
[212 224 204]
[182 194 167]]
[[255 255 255]
[254 254 254]
[254 254 254]
...
[170 176 150]
[161 168 130]
[146 154 113]]
[[255 255 255]
[254 254 254]
[255 255 255]
...
[189 199 169]
[166 178 130]
[121 133 87]]
...
[[148 185 79]
[142 182 57]
[140 179 60]
...
[ 30 17 1]
[ 65 62 15]
[ 76 77 20]]
[[122 157 66]
[120 155 58]
[126 160 71]
...
[ 22 16 3]
[ 97 112 56]
[141 161 87]]
[[ 87 122 41]
[ 88 122 39]
[101 134 56]
...
[ 34 36 10]
[105 133 59]
[138 173 79]]]
[19]
cifar100 데이터 전체 출력
cifar100_imshow02.ipynb
import numpy as np
from keras.datasets import cifar100
(x_train, y_train), (x_test, y_test) = cifar100.load_data() # x_train :이미지, y_train : 라벨
class_names = ['apple', 'aquarium_fish', 'baby', 'bear', 'beaver', 'bed', 'bee', 'beetle',
'bicycle', 'bottle', 'bowl', 'boy', 'bridge', 'bus', 'butterfly', 'camel', 'can', 'castle',
'caterpillar', 'cattle', 'chair', 'chimpanzee', 'clock', 'cloud', 'cockroach', 'couch', 'crab',
'crocodile', 'cup', 'dinosaur', 'dolphin', 'elephant', 'flatfish', 'forest', 'fox', 'girl',
'hamster', 'house', 'kangaroo', 'computer_keyboard', 'lamp', 'lawn_mower', 'leopard', 'lion',
'lizard', 'lobster', 'man', 'maple_tree', 'motorcycle', 'mountain', 'mouse', 'mushroom', 'oak_tree',
'orange', 'orchid', 'otter', 'palm_tree', 'pear', 'pickup_truck', 'pine_tree', 'plain', 'plate',
'poppy', 'porcupine', 'possum', 'rabbit', 'raccoon', 'ray', 'road', 'rocket', 'rose',
'sea', 'seal', 'shark', 'shrew', 'skunk', 'skyscraper', 'snail', 'snake', 'spider', 'squirrel',
'streetcar', 'sunflower', 'sweet_pepper', 'table', 'tank', 'telephone', 'television', 'tiger',
'tractor', 'train', 'trout', 'tulip', 'turtle', 'wardrobe', 'whale', 'willow_tree', 'wolf', 'woman',
'worm']
import matplotlib.pyplot as plt
plt.figure(figsize = (10, 10))
for i in range(100):
plt.subplot(10, 10, i+1)
plt.subplots_adjust(
wspace=0.5, hspace=0.5
)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(x_train[i], cmap = plt.cm.binary)
plt.xlabel(class_names[y_train[i][0]])
#plt.imshow(x_train[0], 'gray')
plt.show()
CNN - mnist
tf01_cnn_mnist.ipynb
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D
# 1. 데이터
datasets = mnist
(x_train, y_train), (x_test, y_test) = datasets.load_data()
# print(datasets.load_data())
# 정규화 (Normalization) => 0 ~ 1 사이로 숫자 변환
x_train, x_test = x_train/255.0, x_test/255.0
# 합성곱 레이어 전 차원 수 맞춰주기
x_train = x_train.reshape(60000, 28, 28, 1)
x_test = x_test.reshape(10000, 28, 28, 1)
# 2. 모델 구성
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3),
input_shape=(28, 28, 1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax')) #mnist -> 10개 / 다중 구성 : softmax
# 3. 컴파일, 훈련
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=256)
# 4. 평가, 예측
loss, acc = model.evaluate(x_test, y_test)
print('loss : ', loss)
print('acc : ', acc)
loss : 0.058030109852552414
acc : 0.9872999787330627
CNN - mnist _MaxPooling2D
tf01_cnn_mnist_maxpooling.ipynb
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout #Dropout 추가
# 1. 데이터
datasets = mnist
(x_train, y_train), (x_test, y_test) = datasets.load_data()
# print(datasets.load_data())
# 정규화 (Normalization) => 0 ~ 1 사이로 숫자 변환
x_train, x_test = x_train/255.0, x_test/255.0
# 합성곱 레이어 전 차원 수 맞춰주기
x_train = x_train.reshape(60000, 28, 28, 1)
x_test = x_test.reshape(10000, 28, 28, 1)
# 2. 모델 구성
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(4, 4),
activation='relu',
input_shape=(28, 28, 1)))
model.add(MaxPooling2D(2, 2))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax')) #mnist -> 10개 / 다중 구성 : softmax
# 3. 컴파일, 훈련
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=256)
# 4. 평가, 예측
loss, acc = model.evaluate(x_test, y_test)
print('loss : ', loss)
print('acc : ', acc)
loss : 0.03955009579658508
acc : 0.9883000254631042
tf01_cnn_cifar10_maxpooling.ipynb - 성능 bad
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
from keras.datasets import cifar10
import time
# 1. 데이터
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
# reshape 필요없음
# (50000, 32, 32, 3) (50000, 1)
# (10000, 32, 32, 3) (10000, 1)
# 정규화(Normalization)
x_train = x_train/255.0
x_test = x_test/255.0
# 2. 모델구성
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(3, 3),
padding='same',
activation='relu',
input_shape=(32, 32, 3))) # 이미지 사이즈, 컬러
model.add(MaxPooling2D(2, 2))
model.add(Dropout(0.25))
model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(10, activation='softmax'))
# 3. 컴파일, 훈련
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
start_time = time.time()
model.fit(x_train, y_train, epochs=20, batch_size=128) # batch_size : 2의 제곱승
end_time = time.time() - start_time
# 4. 평가, 예측
loss, acc = model.evaluate(x_test, y_test)
print('걸린 시간 : ', end_time)
print('loss : ', loss)
print('acc : ', acc)
tf01_cnn_cifar10_maxpooling.ipynb - 성능 높이기
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
from keras.datasets import cifar10
import time
from keras.callbacks import EarlyStopping
from sklearn.model_selection import train_test_split
from keras.callbacks import ModelCheckpoint
#1. 데이터
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
# 정규화 (Nomalization) => 0 ~ 1 사이로 숫자 변환
x_train, x_test = x_train/255.0, x_test/255.0
#2. 데이터 모델 구성
model = Sequential()
model.add(Conv2D(filters = 64, kernel_size = (4,4),
padding = 'same',
activation = 'relu',
input_shape = (32,32,3))) #3은 칼라
model.add(MaxPooling2D(2,2))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3), padding = 'same', activation = 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3), padding = 'same', activation = 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3), padding = 'same', activation = 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation = 'relu'))
model.add(Dense(128, activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))
# EarlyStopping 인스턴스 생성
early_stopping = EarlyStopping(monitor='val_loss', patience=10)
# ModelCheckpoint 인스턴스 생성
model_checkpoint = ModelCheckpoint('./save/best_model.h5', monitor='val_loss', mode='min',
save_best_only=True)
# 2. 모델 컴파일, 훈련
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
start_time = time.time()
model.fit(x_train, y_train, epochs=1000, batch_size=128,
callbacks=[early_stopping, model_checkpoint], validation_split=0.2)
end_time = time.time()- start_time
#4. 평가, 예측
loss, acc = model.evaluate(x_test, y_test)
print('걸린 시간 : ', end_time)
print('loss : ', loss)
print('acc : ', acc)
** VSCode 이용
tf01_cnn_fashionMnist.py
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout # Dropout 추가
from keras.datasets import fashion_mnist
# 1. 데이터
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
# print(x_train.shape, y_train.shape) # (60000, 28, 28) (60000,)
# print(x_test.shape, y_test.shape) # (10000, 28, 28) (10000,)
# 모델링 하기 전에 reshape 해주기 (차원 늘리기 : 합성곱 레이어 전 차원 수 맞춰주기)
# reshape
x_train = x_train.reshape(60000, 28, 28, 1)
x_test = x_test.reshape(10000, 28, 28, 1)
# [실습]
# 정규화 (Normalization) --> 0 ~ 1 사이로 숫자 변환 (정규화 해야 잘 나옴)
x_train, x_test = x_train/255.0, x_test/255.0
# 2. 모델 구성
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(4, 4),
activation='relu',
input_shape=(28, 28, 1)))
# model.add(MaxPooling2D(2, 2)) # 차원을 반으로 줄여줌
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(2, 2))
# model.add(Dropout(0.2))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
# 3. 컴파일, 훈련
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=256)
# 4. 평가, 예측
loss, acc = model.evaluate(x_test, y_test)
print('loss : ', loss)
print('acc : ', acc)
#=====================결과======================#
#MaxPooling2D(2,2) 레이어 1, Dropout(0.2)
# loss : 0.25890305638313293
# acc : 0.916700005531311
# MaxPooling2D(2, 2) , Dropout(0.3)
# loss : 0.22996017336845398
# acc : 0.9229999780654907
ㄴ reshape 을 이용하여 차원을 늘려줘야함 = > 합성곱 전 차원 수 맞춰주기
# 정규화 (Normalization) --> 0 ~ 1 사이로 숫자 변환 (정규화 해야 잘 나옴)
x_train, x_test = x_train/255.0, x_test/255.0
ㄴ x_train, x_test 를 255.0 씩 나눠서 정규화 시킴 --> x만 해줘도 됨(y 는 안 해줘도 됨)
tf01_cnn_cifar100_max.py
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
from keras.datasets import cifar100
import time
# 1. 데이터
(x_train, y_train), (x_test, y_test) = cifar100.load_data()
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
# (50000, 32, 32, 3) (50000, 1)
# (10000, 32, 32, 3) (10000, 1)
# 정규화(Normalization)
x_train = x_train/255.0
x_test = x_test/255.0
# 2. 모델구성
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(3, 3),
padding='same', # 하이퍼파라미터
activation='relu', # 하이퍼파라미터
input_shape=(32, 32, 3)))
model.add(MaxPooling2D(2, 2))
model.add(Dropout(0.25))
model.add(Conv2D(128, (3, 3), padding='same',
activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(100, activation='softmax')) # cifar100 이므로 100
# 3. 컴파일, 훈련
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics='accuracy')
start_time = time.time()
model.fit(x_train, y_train, epochs=10,
batch_size=256)
end_time = time.time() - start_time
# 4. 평가, 예측
loss, acc = model.evaluate(x_test, y_test)
print('걸린 시간 : ', end_time)
print('loss : ', loss)
print('acc : ', acc)
# 걸린 시간 : 386.53373312950134
# loss : 2.373387098312378
# acc : 0.39500001072883606
'네이버클라우드 > AI' 카테고리의 다른 글
AI 5일차 (2023-05-12) 인공지능 기초 - Tokenizer 와 Embedding (0) | 2023.05.12 |
---|---|
AI 5일차 (2023-05-12) 인공지능 기초 - 자연어처리(NLP) 기초 (0) | 2023.05.12 |
AI 4일차 (2023-05-11) 인공지능 기초 - 이미지 분석 : CNN 모델 (0) | 2023.05.11 |
AI 3일차 (2023-05-10) 인공지능 기초 - Save model, Save weight, Model Check Point (0) | 2023.05.10 |
AI 3일차 (2023-05-10) 인공지능 기초 - Validation split, 과적합(Overfitting)과 Early Stopping (0) | 2023.05.10 |