last update 24.12.06
ImageDataGenerator를 사용os.makedirs('/content/augmented-images')
os.makedirs('/content/augmented-images/yes') # 종양이 있는 이미지 저장
os.makedirs('/content/augmented-images/no') # 종양이 없는 이미지 저장
os.makedirs: 디렉토리 생성해주는 역할# 2-1. 데이터 증강 함수 정의
def augment_data(file_dir, n_generated_samples, save_to_dir):
# 2-2. 데이터 증강 기법 설정
data_gen = ImageDataGenerator(rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
brightness_range=(0.3, 1.0),
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest'
)
# 2-3. 원본 데이터 증강 및 저장
for filename in listdir(file_dir):
image = cv2.imread(file_dir + '/' + filename)
# reshape the image
image = image.reshape((1,)+image.shape)
save_prefix = 'aug_' + filename[:-4]
# 증강 데이터 생성
i=0
for batch in data_gen.flow(x=image, batch_size=1, save_to_dir=save_to_dir,save_prefix=save_prefix, save_format='jpg'):
i += 1
# 증강 중단 조건
if i > n_generated_samples:
break
n_generated_samplesImageDataGenerator: Keras 데이터 증강 클래스| 파라미터 | 설명 |
|---|---|
rotation_range=10 | • 이미지를 ±10도 이내로 무작위 회전 • 다양한 각도에서 학습 가능 |
width_shift_range=0.1, height_shift_range=0.1 | • 이미지의 가로 및 세로를 10% 이내로 무작위로 이동 • 다양한 위치에서 학습 가능 |
shear_range=0.1 | • 이미지를 기울임 • 이미지 왜곡에 견고한 모델 학습 가능 |
brightness_range=(0.3, 1.0) | • 이미지 밝기를 30%~100% 사이에서 무작위로 조정 |
horizontal_flip=True, vertical_flip=True | • 이미지를 수평 및 수직으로 무작위로 뒤집음 |
fill_mode='nearest' | • 증강으로 인해 생긴 빈 공간을 가장 가까운 픽셀 값으로 채움 |
augment_data(file_dir='/content/brain_tumor_dataset/yes',n_generated_samples=6, save_to_dir='/content/augmented-images/yes')
augment_data(file_dir='/content/brain_tumor_dataset/no', n_generated_samples=9, save_to_dir='/content/augmented-images/no')
import shutil
# 압축 생성
shutil.make_archive('/content/augmented-images', 'zip', '/content/augmented-images')
from google.colab import files
# 압축 파일 다운로드
files.download('/content/augmented-images.zip')