드레:
코딩 뿌시기
드레:
전체 방문자
오늘
어제
  • 분류 전체보기 (268)
    • Python (74)
      • Python 기초 (42)
      • Numpy (8)
      • Pandas (22)
    • Machine Learning (31)
      • Machine Learning (1)
      • Deep Learning (27)
    • AWS (22)
      • RDS (3)
      • EC2 (9)
      • Lambda (8)
      • S3 (2)
    • MySQL (24)
    • Git (8)
    • Streamlit (12)
    • REST API (22)
    • Java (24)
    • Android (36)
    • Debugging (15)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • API
  • CNN
  • tensorflow
  • 액션바
  • 네이버 API
  • 딥러닝
  • AWS
  • Lambda
  • 서버리스
  • pandas
  • 안드로이드 스튜디오
  • fine tuning
  • volley
  • Streamlit
  • Java
  • AWS Lambda
  • JWT
  • github
  • Python
  • Transfer Learning
  • GET
  • serverless
  • EC2
  • Ann
  • Retrofit2
  • aws s3
  • flask
  • Callback
  • rest api
  • 깃이그노어

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
드레:

코딩 뿌시기

Tensorflow - CNN 모델 예제
Machine Learning/Deep Learning

Tensorflow - CNN 모델 예제

2022. 12. 31. 22:52

이미지를 보고 인간인지 말인지 분류하는 모델을 만들어보려 한다.

 

1. 연습용 이미지 다운로드 

!wget --no-check-certificate \
    https://storage.googleapis.com/laurencemoroney-blog.appspot.com/horse-or-human.zip \
    -O /tmp/horse-or-human.zip
    
!wget --no-check-certificate \
    https://storage.googleapis.com/laurencemoroney-blog.appspot.com/validation-horse-or-human.zip \
    -O /tmp/validation-horse-or-human.zip

 

 

2. 압축 풀기

import zipfile
# 파일을 불러옴
file = zipfile.ZipFile('/tmp/horse-or-human.zip')
# /tmp/horse-or-human에 모든 압축 풀기
file.extractall('/tmp/horse-or-human')

file = zipfile.ZipFile('/tmp/validation-horse-or-human.zip')
file.extractall('/tmp/validation-horse-or-human')

압축이 잘 풀린걸 확인했다.

import os

# 사진이 저장된 폴더의 경로를 변수로
train_horse_dir = '/tmp/horse-or-human/horses'
train_human_dir = '/tmp/horse-or-human/humans'
validation_horse_dir = '/tmp/validation-horse-or-human/horses'
validation_human_dir = '/tmp/validation-horse-or-human/humans'

# 디렉토리에 있는 목록 확인
os.listdir(train_horse_dir)

# 디렉토리에 있는 파일의 갯수 확인
len(os.listdir(train_horse_dir))
len(os.listdir(train_human_dir))
len(os.listdir(validation_horse_dir))
len(os.listdir(validation_human_dir))

 

 

 

3. CNN 모델 만들기

  • 연습용 이미지는 300 X 300 컬러 이미지
  • 사진의 결과는 말, 인간 둘 중 하나이므로 활성화함수로 sigmoid, 손실함수로  binary_crossentropy를 사용
import tensorflow as tf
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
from keras.models import Sequential

def build_model():
  model = Sequential()
  model.add( Conv2D(filters= 16, kernel_size= (3, 3), activation= 'relu',
                    input_shape= (300, 300, 3) ) )
  model.add( MaxPooling2D( pool_size= (2, 2), strides= 2 ) )
  model.add( Conv2D( 32, (3, 3), activation= 'relu' ) )
  model.add( MaxPooling2D( (2, 2), 2 ) )
  model.add( Conv2D( 64, (3, 3), activation= 'relu' ) )
  model.add( MaxPooling2D( (2, 2), 2 ) )

  model.add( Flatten() )
  model.add( Dense( units= 512, activation= 'relu' ) )
  model.add( Dense( units= 1, activation= 'sigmoid' ) )
  
  model.compile(optimizer= 'rmsprop', loss= 'binary_crossentropy', metrics= ['accuracy'])
  return model
  
model = build_model()

 

 

4. 이미지파일 변환

 

model.fit 함수에 들어가는 데이터는 numpy array여야 하는데, 현재 가지고 있는 데이터는 이미지 파일(png)이다. 

따라서, 이미지 파일을 numpy array로 변환시켜주는 ImageDataGenerator 라이브러리를 사용한다.

 

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale= 1/255.0)
validation_datagen = ImageDataGenerator(rescale= 1/255.0)

# 이미지가 들어있는 디렉토리의 정보와 이미지 사이즈정보와 몇개로 분류할지 정보를 알려준다.
# target_size와 모델의 input_shape은 가로 세로가 같아야 한다.
# class_mode는 2개로 분류할 때 binary, 3개 이상은 categorical 사용
train_generator = train_datagen.flow_from_directory( '/tmp/horse-or-human', 
                                                     target_size=(300, 300),
                                                     class_mode= 'binary' )
validation_generator = validation_datagen.flow_from_directory('/tmp/validation-horse-or-human',
                                                              target_size= (300, 300),
                                                              class_mode= 'binary')

# 이미지가 들어있던 디렉토리 별로 class를 나눈다.
# 알파벳순으로 인덱스를 매긴다.
train_generator.class_indices
>>> {'horses': 0, 'humans': 1}

 

 

5. 모델 학습

ImageDataGenerator의 flow_from_directory로 만든 train_generator와 validation_generator에는 X와 y값이 모두 들어있다.

따라서 학습 할 때 train_generator만 넣어주면 알아서 학습한다.

 

epoch_history = model.fit(train_generator, epochs= 15, 
                          validation_data= validation_generator)

결과를 보니 overfitting 된 것 같다.

 

 

 

6. 모델 평가

 

# 연습용이기 때문에, 테스트 데이터가 따로 없어 검증데이터를 넣었다.
model.evaluate(validation_generator)

# 차트로 epoch마다 정확도 확인해보기
import matplotlib.pyplot as plt

plt.plot(epoch_history.history['accuracy'])
plt.plot(epoch_history.history['val_accuracy'])
plt.legend(['train', 'validation'])
plt.show()

연습용으로 만든 모델이다 보니 성능은 별로 좋지 않고 overfitting 되었다.

 

# 이 모델을 이용해 이미지를 업로드하면 말인지 사람인지 예측하는 코드
import numpy as np
from google.colab import files
from tensorflow.keras.preprocessing import image

uploaded = files.upload()

for fn in uploaded.keys() :
  path = '/content/' + fn
  img = image.load_img(path, target_size=(300,300))
  x = image.img_to_array(img) / 255.0
  # print(x)
  # print(x.shape)

  x = np.expand_dims(x, axis = 0)

  # print(x.shape)

  images = np.vstack( [x] )
  classes = model.predict( images, batch_size = 10 )
  
  print(classes)

  if classes[0] > 0.5 :
    print(fn + " is a human")
  else :
    print(fn + " is a horse")

이 이미지를 업로드 했을 때 결과

 

'Machine Learning > Deep Learning' 카테고리의 다른 글

파일을 Train/Test 디렉토리로 나눠서 저장하는 방법  (0) 2023.01.02
Tensorflow - ImageDataGenerator를 이용한 이미지 전처리와 이미지 증강  (0) 2023.01.01
Convolution Neural Network(CNN, 합성곱 신경망) 개념  (0) 2022.12.31
Tensorflow - 레이블인코딩 된 y값을 원핫인코딩으로 바꾸기  (0) 2022.12.31
Tensorflow - model을 파일로 저장하고 불러오는 방법  (0) 2022.12.30
    'Machine Learning/Deep Learning' 카테고리의 다른 글
    • 파일을 Train/Test 디렉토리로 나눠서 저장하는 방법
    • Tensorflow - ImageDataGenerator를 이용한 이미지 전처리와 이미지 증강
    • Convolution Neural Network(CNN, 합성곱 신경망) 개념
    • Tensorflow - 레이블인코딩 된 y값을 원핫인코딩으로 바꾸기
    드레:
    드레:

    티스토리툴바