딥러닝

CNN 이용하여 정교한 이미지 분류 (강아지, 고양이 분류)

HooSL 2022. 3. 29. 13:17

/tmp 컬럼에, 2000개의 이미지를 다운로드 받아서 저장합니다.

 

!wget --no-check-certificate \
  https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip \
  -O /tmp/cats_and_dogs_filtered.zip

 

압축풀기

import tensorflow as tf
import os
#압축풀기
import zipfile
filename = '/tmp/cats_and_dogs_filtered.zip'
zip_ref = zipfile.ZipFile(filename,mode='r')
zip_ref.extractall('/tmp/cats_and_dogs_filtered')
zip_ref.close()

 

데이터 억세스할 경로를 만든다.

train_cats_dir = '/tmp/cats_and_dogs_filtered/cats_and_dogs_filtered/train/cats'
train_dogs_dir = '/tmp/cats_and_dogs_filtered/cats_and_dogs_filtered/train/dogs'

validation_cats_dir = '/tmp/cats_and_dogs_filtered/cats_and_dogs_filtered/validation/cats'
validation_dogs_dir = '/tmp/cats_and_dogs_filtered/cats_and_dogs_filtered/validation/dogs'

 

파일 확인해보기

train_cats_names = os.listdir(train_cats_dir)
train_dogs_names = os.listdir(train_dogs_dir)

validation_cats_names = os.listdir(validation_cats_dir)
validation_dogs_names = os.listdir(validation_dogs_dir)

print(train_cats_names)
print(train_dogs_names)
print(validation_cats_names)
print(validation_dogs_names)

 

트레이닝 이미지와 밸리데이션 이미지를 각각 몇개씩인지 확인해 본다.

len(train_cats_names)
len(train_dogs_names)
len(validation_cats_names)
len(validation_dogs_names)

 

강아지와 고양이 8개씩 화면에 이미지를 표시해봅니다.

%matplotlib inline

import matplotlib.image as mpimg
import matplotlib.pyplot as plt

# Parameters for our graph; we'll output images in a 4x4 configuration
nrows = 4
ncols = 4

pic_index = 0 # Index for iterating over images
# Set up matplotlib fig, and size it to fit 4x4 pics
fig = plt.gcf()
fig.set_size_inches(ncols*4, nrows*4)

pic_index+=8

next_cat_pix = [os.path.join(train_cats_dir, fname) 
                for fname in train_cats_names[ pic_index-8:pic_index] 
               ]

next_dog_pix = [os.path.join(train_dogs_dir, fname) 
                for fname in train_dogs_names[ pic_index-8:pic_index]
               ]

for i, img_path in enumerate(next_cat_pix+next_dog_pix):
  # Set up subplot; subplot indices start at 1
  sp = plt.subplot(nrows, ncols, i + 1)
  sp.axis('Off') # Don't show axes (or gridlines)

  img = mpimg.imread(img_path)
  plt.imshow(img)

plt.show()

이미지의 사이즈를 150x150, 칼라(rgb) 로 처리해서 모델링하는 함수를 만듭니다.

#라이브러리 import
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense

#모델링 함수
def build_model():
  model = Sequential()
  model.add(Conv2D(filters=16,kernel_size=(3,3),activation='relu',input_shape=(150,150,3)))
  model.add(MaxPooling2D(pool_size=(2,2),strides=2))

  model.add(Conv2D(filters=32,kernel_size=(3,3),activation='relu'))
  model.add(MaxPooling2D(pool_size=(2,2),strides=2))

  model.add(Conv2D(filters=64,kernel_size=(3,3),activation='relu'))
  model.add(MaxPooling2D(pool_size=(2,2),strides=2))

  model.add(Flatten())

  model.add(Dense(units=512,activation='relu'))
  model.add(Dense(units=1,activation='sigmoid'))

  return model
  
  #모델 생성
  model = build_model()
  
  #모델 요약
  model.summary()

 

RMSprop 으로 컴파일합니다.

from tensorflow.keras.optimizers import RMSprop,Adam,Adagrad,Adadelta #RMSprop 보폭

model.compile(loss='binary_crossentropy',
              optimizer=RMSprop(learning_rate=0.001),
              metrics=['accuracy'])

Data Preprocessing Image Data Generator 사용하기

from tensorflow.keras.preprocessing.image import ImageDataGenerator

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

train_generator = train_datagen.flow_from_directory('/tmp/cats_and_dogs_filtered/cats_and_dogs_filtered/train',target_size=(150,150),class_mode='binary')
validation_generator = validation_datagen.flow_from_directory('/tmp/cats_and_dogs_filtered/cats_and_dogs_filtered/validation',target_size=(150,150),class_mode='binary')

 

모델 훈련 15epochs

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

 

모델 평가

model.evaluate(validation_generator)

 

실제 다른 사진을 넣고 비교해보기

import numpy as np

from google.colab import files
from keras.preprocessing import image

uploaded=files.upload()

for fn in uploaded.keys():
 
  # predicting images
  path='/content/' + fn
  img=image.load_img(path, target_size=(150, 150))
  
  x=image.img_to_array(img)
  x=np.expand_dims(x, axis=0)
  images = np.vstack([x])
  
  classes = model.predict(images, batch_size=10)
  
  print(classes[0])
  
  if classes[0]>0.5:
    print(fn + " is a dog")
    
  else:
    print(fn + " is a cat")