Home > Study > Linux > Day4 : Deep Learning

Day4 : Deep Learning
Study Language

πŸ“Œ Deep Learningλž€?


νΌμ…‰νŠΈλ‘ (Perceptron)은 생물학적 λ‰΄λŸ°μ„ μˆ˜ν•™μ μœΌλ‘œ λͺ¨λΈλ§ν•œ 인곡 λ‰΄λŸ° λͺ¨λΈλ‘œ,…


πŸ”§ ꡬ쑰 (Perceptron Structure)

μž…λ ₯(x) β†’ κ°€μ€‘μΉ˜(w) β†’ 가쀑합(βˆ‘) β†’ ν™œμ„±ν™” ν•¨μˆ˜(f) β†’ 좜λ ₯(y)

πŸ› οΈ μž‘μ—…ν•  디렉토리 생성 및 ν™˜κ²½ μ„€μ •


# 1. μž‘μ—… 디렉토리 생성
mkdir F_MNIST                  # 디렉토리 이름: F_MNIST
cd F_MNIST                     # ν•΄λ‹Ή λ””λ ‰ν† λ¦¬λ‘œ 이동

# 2. 가상 ν™˜κ²½ 생성 및 ν™œμ„±ν™”
python3 -m venv .fmnist        # 가상 ν™˜κ²½ 생성 (폴더 이름: .fmnist)
source .fmnist/bin/activate    # 가상 ν™˜κ²½ ν™œμ„±ν™”

# 3. νŒ¨ν‚€μ§€ μ„€μΉ˜
pip install -U pip             # pip μ΅œμ‹  λ²„μ „μœΌλ‘œ μ—…κ·Έλ ˆμ΄λ“œ
pip install tensorflow         # TensorFlow (λ”₯λŸ¬λ‹ ν”„λ ˆμž„μ›Œν¬)
pip install matplotlib         # Matplotlib (μ‹œκ°ν™” 라이브러리)
pip install PyQt5              # PyQt5 (Matplotlib GUI λ°±μ—”λ“œμš©)
pip install scikit_learn       # scikit-learn (λ¨Έμ‹ λŸ¬λ‹ 및 평가 도ꡬ)

# 4. Qt GUI λ°±μ—”λ“œ μ„€μ • (Wayland ν™˜κ²½μ—μ„œ ν•„μˆ˜)
export QT_QPA_PLATFORM=wayland # Qt GUIλ₯Ό Waylandμ—μ„œ 정상 λ™μž‘ν•˜κ²Œ μ„€μ •

πŸ‘¨β€πŸ’» μ‹€μŠ΅


πŸ’‘ Code : Fashion MNIST

import tensorflow as tf
from tensorflow import keras

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

# dataset load
fashion_mnist = keras.datasets.fashion_mnist

# spilt data (train / test)
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

print(train_images.shape)
print(train_labels.shape)
print(test_images.shape)
print(test_labels.shape)

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
               
matplotlib.use('Qt5Agg')
NUM=20
plt.figure(figsize=(15,15))
plt.subplots_adjust(hspace=1)
for idx in range(NUM):
    sp = plt.subplot(5,5,idx+1)
    plt.imshow(train_images[idx])
    plt.title(f'{class_names[train_labels[idx]]}')
plt.show()

plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()

# κ°„λ‹¨ν•œ 이미지 μ „μ²˜λ¦¬ (for ANN)
train_images = train_images / 255.0
test_images = test_images / 255.0

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

plt.figure(figsize=(10,8))
for i in range(20):
    plt.subplot(4,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
plt.show()

model = keras.Sequential ([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax'),
])

model.summary()

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=20)

predictions = model.predict(test_images)

predictions[0]

np.argmax(predictions[0])

test_labels[0]

def plot_image(i, predictions_array, true_label, img):
  predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img, cmap=plt.cm.binary)

  predicted_label = np.argmax(predictions_array)
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'

  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
  predictions_array, true_label = predictions_array[i], true_label[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])
  thisplot = plt.bar(range(10), predictions_array, color="#777777")
  plt.ylim([0, 1])
  predicted_label = np.argmax(predictions_array)

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')

num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plot_image(i, predictions, test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plot_value_array(i, predictions, test_labels)
plt.show()

from sklearn.metrics import accuracy_score
print('accuracy score : ', accuracy_score(tf.math.argmax(predictions, -1), test_labels))

aaaaaa