728x90
반응형
SMALL
ImageDataGenerator
tf.keras.preprocessing.image.ImageDataGenerator(
featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zca_epsilon=1e-06,
rotation_range=0,
width_shift_range=0.0,
height_shift_range=0.0,
brightness_range=None,
shear_range=0.0,
zoom_range=0.0,
channel_shift_range=0.0,
fill_mode='nearest',
cval=0.0,
horizontal_flip=False,
vertical_flip=False,
rescale=None,
preprocessing_function=None,
data_format=None,
validation_split=0.0,
interpolation_order=1,
dtype=None
)
validation data 생성
train_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=20, width_shift_range=0.2,
height_shift_range=0.2, horizontal_flip=True,
validation_split=0.2)
val_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)
train_data = train_datagen.flow_from_directory(train_path,
target_size=(224, 224), color_mode='rgb',
batch_size=16, class_mode='categorical',
subset = 'training')
val_data = val_datagen.flow_from_directory(train_path, target_size=(224, 224),
color_mode='rgb', batch_size=16,
class_mode='categorical',
subset = 'validation')
flow(x, y)
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
y_train = np_utils.to_categorical(y_train, num_classes)
y_test = np_utils.to_categorical(y_test, num_classes)
datagen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
# 특성별 정규화에 필요한 수치를 계산
# (영위상 성분분석 백색화를 적용하는 경우, 표준편차, 평균, 그리고 주성분이 이에 해당)
datagen.fit(x_train)
# 실시간 데이터 증강을 사용해 배치에 대해서 모델 학습:
model.fit_generator(datagen.flow(x_train, y_train, batch_size=32),
steps_per_epoch=len(x_train) / 32, epochs=epochs)
# "수동"인 예시
for e in range(epochs):
print('Epoch', e)
batches = 0
for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=32):
model.fit(x_batch, y_batch)
batches += 1
if batches >= len(x_train) / 32:
# we need to break the loop by hand because
# the generator loops indefinitely
break
flow_from_directory(directory)
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
'data/train',
target_size=(150, 150),
batch_size=32,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
'data/validation',
target_size=(150, 150),
batch_size=32,
class_mode='binary')
model.fit_generator(
train_generator,
steps_per_epoch=2000,
epochs=50,
validation_data=validation_generator,
validation_steps=800)
https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator
728x90
반응형
LIST
'Python Library > Keras' 카테고리의 다른 글
[Keras] ImageDataGenerator class weight (0) | 2023.09.08 |
---|---|
[Keras] tflite 변환 (0) | 2022.11.24 |
[Keras] 배치 정규화 (Batch Normalization) (0) | 2022.07.31 |
[Keras] Functional 모델 (0) | 2022.07.30 |
[Keras] ModuleNotFoundError: No module named 'keras' (0) | 2022.03.31 |