# Day_24_01_DogsAndCats.py
import tensorflow as tf
import os
import shutil
from PIL import Image
from sklearn import preprocessing, model_selection
import numpy as np
# dogs_and_cats
# +-- train
# +-- small
# +-- train
# +-- cat
# +-- dog
# +-- validation
# +-- cat
# +-- dog
# +-- test
# +-- cat
# +-- dog
# 이미지 개와 고양이를 각각
# small/train 1000개, small/validation 500개, small/test 500개 복사하기
def make_dataset_folders():
def make_if_not(dst_folder):
if not os.path.exists(dst_folder):
os.mkdir(dst_folder)
# 부모폴더가 없어도 만들어줌
def make_if_not_2(dst_folder):
if not os.path.exists(dst_folder):
os.makedirs(dst_folder)
# make_if_not('dogs_and_cats/small')
#
# make_if_not('dogs_and_cats/small/train')
# make_if_not('dogs_and_cats/small/validation')
# make_if_not('dogs_and_cats/small/test')
#
# make_if_not('dogs_and_cats/small/train/cat')
# make_if_not('dogs_and_cats/small/train/dog')
# make_if_not('dogs_and_cats/small/validation/cat')
# make_if_not('dogs_and_cats/small/validation/dog')
# make_if_not('dogs_and_cats/small/test/cat')
# make_if_not('dogs_and_cats/small/test/dog')
# make_if_not('dogs_and_cats/small')
make_if_not_2('dogs_and_cats/small/train')
make_if_not_2('dogs_and_cats/small/validation')
make_if_not_2('dogs_and_cats/small/test')
make_if_not_2('dogs_and_cats/small/train/cat')
make_if_not_2('dogs_and_cats/small/train/dog')
make_if_not_2('dogs_and_cats/small/validation/cat')
make_if_not_2('dogs_and_cats/small/validation/dog')
make_if_not_2('dogs_and_cats/small/test/cat')
make_if_not_2('dogs_and_cats/small/test/dog')
def make_small_datasets():
def copy_animals(kind, start, end, dst_folder):
for i in range(start, end):
filename = '{}.{}.jpg'.format(kind, i)
src_path = os.path.join('dogs_and_cats/train', filename)
dst_path = os.path.join(dst_folder, filename)
shutil.copy(src_path, dst_path)
copy_animals('cat', 0, 1000, 'dogs_and_cats/small/train/cat')
copy_animals('dog', 0, 1000, 'dogs_and_cats/small/train/dog')
copy_animals('cat', 1000, 1500, 'dogs_and_cats/small/validation/cat')
copy_animals('dog', 1000, 1500, 'dogs_and_cats/small/validation/dog')
copy_animals('cat', 1500, 2000, 'dogs_and_cats/small/test/cat')
copy_animals('dog', 1500, 2000, 'dogs_and_cats/small/test/dog')
def generator_basic():
# 이미지 만들어내는 함수
# 이미지를 만들어냄 방법만 알고 있고 소스를 다시 연결해줘야함
gen = tf.keras.preprocessing.image.ImageDataGenerator()
# 3가지 방법 -> (x,y), pandas, 폴더에 있는것
# 우리 폴더 안에 있는 25000개의 데이터를 무제한으로 가져옴 -> 조심해야함
flow = gen.flow_from_directory('dogs_and_cats/small/train',
batch_size=7,
target_size=(224, 224), # resize 기능을 제공해줌
class_mode='binary') # 시그모이드에 어울리는 모드로 변환
# class_mode "categorical", "binary", "sparse"
for i, (x, y) in enumerate(flow):
print(x.shape, y.shape) # (32, 256, 256, 3) (32, 2) 위에서 batch_size 안해주면 32로
# 32개씩 Image 가져옴, y는 자동 셔플해서 one_hot 벡터로 가져옴
print(y[:3])
if i >= 2:
break
# hi
return
# make_dataset_folders()
# make_small_datasets()
generator_basic()
# ------------------- #
# d = {'name': 'joo', 'age': 26}
# a = [d, d, d]
# b = [{'name': 'joo', 'age': 26}] * 3 + [{'name': 'joo', 'age': 26}]
#
# print(a)
# print(b)
# ------------------- #
# return resnet_utils.Block(scope, bottleneck, [{
# 'depth': base_depth * 4,
# 'depth_bottleneck': base_depth,
# 'stride': 1
# }] * (num_units - 1) + [{
# 'depth': base_depth * 4,
# 'depth_bottleneck': base_depth,
# 'stride': stride
# }])