import numpy as np
from numpy import genfromtxt # ํ
์คํธ ํ์ผ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐฐ์ด๋ก ์์ฑํด์ค.
data = genfromtxt('../Computer-Vision-with-Python/DATA/bank_note_data.txt', delimiter=',') # delimiter : ,๋ก ๋๋์ด์ ธ์ ๋ฐฐ์ด์์ฑ๋จ.
labels = data[:, 4]
features = data[:,0:4]
X = features
y = labels
from sklearn.model_selection import train_test_split # ์ด๊ฑธ ์ฌ์ฉํ๋ฉด train set๊ณผ test set ๋๋์ ์์.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) # 33ํผ๋ test set์ผ๋ก ๋ค์ด๊ฐ.
from sklearn.preprocessing import MinMaxScaler # ๋ชจ๋ ๋ฐ์ดํฐ์ feature๋ค์ด ํน์ ๋ฒ์๋ก bound์ํฌ ์ ์์. -> ์ ๊ทํ๊ฐ ๊ฐ๋ฅ
scaler_object = MinMaxScaler()
scaler_object.fit(X_train) # ์ด ๊ณผ์ ์ ํตํด X_train์ ์ต์ ๊ฐ๊ณผ ์ต๋ ๊ฐ์ ์ฐพ์ ์ ์๋ค.
scaled_X_train = scaler_object.transform(X_train)
scaled_X_test = scaler_object.transform(X_test) # ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ํ์ต ์ํค์ง ์๋ ์ด์ ๋ overfit ๋ฐฉ์ง
from keras.models import Sequential # ์ด๊ฒ๋ค์ ์ด์ฉํด์ CNN์์ฑ
from keras.layers import Dense
model = Sequential()
model.add(Dense(4, input_dim=4, activation='relu')) # feature์ ๊ฐ์, activation function. ์ด๊ฑฐ๋ input layer
model.add(Dense(8, activation='relu')) # ์ด๊ฑฐ๋ hidden layer
model.add(Dense(1, activation='sigmoid')) # output layer
# ๋ชจ๋ธ์ ์ปดํ์ผํ๋ ๊ณผ์ . ํ์ต ๋ฐฉ์์ ๋ํ ํ๊ฒฝ์ค์ .
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# epochs : ๋ชจ๋ ๋ฐ์ดํฐ์ ํ์ต ํ์
# verbose : ์์ค ๋ณ์, ์์ค ๊ฐ์ ๋ํ๋ด์ค.
model.fit(scaled_X_train, y_train, epochs=50, verbose=2)
model.predict_classes(scaled_X_test) # ์ ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด X test์ ๋ํด ์์ธก ํด๋์ค๋ฅผ ๋ฑ์
from sklearn.metrics import confusion_matrix, classification_report # ๋ชจ๋ธ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด ๊ฐ์ ธ์ด
predictions = model.predict_classes(scaled_X_test)
print(classification_report(y_test, predictions))
๐ป ๊ฒฐ๊ณผ
from keras.datasets import mnist
import matplotlib.pyplot as plt
%matplotlib inline
(x_train, y_train), (x_test, y_test) = mnist.load_data()
single_image = x_train[0]
plt.imshow(single_image, cmap='gray_r') # ๊ฒ์ ํ์ ๋ฐ์
from keras.utils.np_utils import to_categorical # one hot encoding์ ํ๊ธฐ ์ํจ
y_cat_test = to_categorical(y_test, 10) # class์ ๊ฐ์ 10๊ฐ
y_cat_train = to_categorical(y_train, 10)
x_train = x_train / x_train.max() # ์๋์ผ๋ก ์ ๊ทํ ํ๋ ๋ฐฉ๋ฒ
x_test = x_test / x_test.max()
x_train = x_train.reshape(60000, 28, 28, 1) # ์ฑ๋์ ์ฐจ์์ ๋๋ ค์ค
x_test = x_test.reshape(10000, 28, 28, 1)
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten # Flatten์ 2์ฐจ์์ 1์ฐจ์์ผ๋ก ๋ฐ๊พธ์ด์ค
model = Sequential()
# Convolutional Layer
# ํํฐ์ ์๋ ๊ฐ๋จํ๋ฉด 32๊ฐ ๋ซ๋ค. ์ปค๋ ์ฌ์ด์ฆ๋ ๋ณดํต 3x3 or 4x4
model.add(Conv2D(filters=32, kernel_size=(4,4), input_shape=(28,28,1), activation='relu'))
# Pooling Layer
model.add(MaxPool2D(pool_size=(2,2)))
# 2D -> 1D
model.add(Flatten())
# Dense Layer
model.add(Dense(128, activation='relu')) # Hidden layer
model.add(Dense(10, activation='softmax')) # ์ด๊ฑฐ๋ ํด๋์ค ๋ถ๋ฅ
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
model.summary()
model.fit(x_train, y_cat_train, epochs=2)
model.evaluate(x_test, y_cat_test)
from sklearn.metrics import classification_report
predictions = model.predict_classes(x_test)
print(classification_report(y_test, predictions))
๐ป ๊ฒฐ๊ณผ
from keras.datasets import cifar10
import matplotlib.pyplot as plt
%matplotlib inline
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train / x_train.max()
x_test = x_test / x_test.max()
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten
y_cat_train = to_categorical(y_train, 10)
y_cat_test = to_categorical(y_test,10)
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(4,4), input_shape=(32,32,3), activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(filters=32, kernel_size=(4,4), input_shape=(32,32,3), activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
model.fit(x_train, y_cat_train, verbose=1, epochs=10)
model.evaluate(x_test, y_cat_test)
from sklearn.metrics import classification_report
predictions = model.predict_classes(x_test)
print(classification_report(y_test, predictions))
๐ป ๊ฒฐ๊ณผ