logo

[computer-vision]

컴퓨터 비전과 딥러닝 기초 복습

 

실습 준비

  • 시작 메뉴에서 miniforge prompt 실행
  • jupyter notebook 입력 후 엔터
  • 브라우저에서 주피터 노트북 열림
    • 자동으로 열리지 않으면 http://localhost:8888 로 시작하는 주소 복사해서 브라우저에 붙여넣기
  • Desktop 폴더 클릭
  • New → Python 3 클릭
 

코드 실행

  • 아래 코드 셀을 복사해서 붙여넣기
  • 셀 왼쪽의 ▶ 아이콘 클릭 또는 Shift + Enter 키 입력하여 코드 실행
import os
os.environ["KERAS_BACKEND"] = "torch"
import keras
import numpy as np

# 데이터 준비: MNIST 숫자 이미지에서 0과 1만 사용
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
def filter_binary(x, y, neg_cls, pos_cls):
    cond = (y == neg_cls) | (y == pos_cls)
    x_bin = x[cond]
    y_bin = np.where(y[cond] == neg_cls, 0, 1)
    return x_bin, y_bin

x_train_binary, y_train_binary = filter_binary(x_train, y_train, 0, 1)

# 모형 정의
model = keras.models.Sequential(  # 딥러닝 모형의 구성 요소를 순서대로 정의
    [
        keras.layers.Rescaling(1/255), #  입력 값의 범위를 0~255에서 0.0 ~ 1.0으로
        keras.layers.Flatten(), #  이미지 평탄화
        keras.layers.Dense(1, activation='sigmoid') # 이미지마다 1개의 출력
    ]
)

# 훈련 설정
model.compile(optimizer=keras.optimizers.SGD(learning_rate=0.0001),
              loss='binary_crossentropy', metrics=['accuracy'])

# 훈련
model.fit(x_train_binary, y_train_binary, epochs=5, batch_size=32)

# 테스트 데이터 준비
x_test_binary, y_test_binary = filter_binary(x_test, y_test, 0, 1)

# 평가
model.evaluate(x_test_binary, y_test_binary)
 

복습 퀴즈

Previous
테스트