C.W.K.
Stream
Lesson 06 of 06 · published

실전 — augmentation 포함 image pipeline

~8 min · data

Level 0Keras 도제
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

이 track 전체를 스크립트 한 개로

이게 capstone — 이 track 에서 배운 모든 조각을 하나로 접은 완전한 vision pipeline 이야. layer 로서의 preprocessing (lesson 1), 학습 때 ON / inference 때 OFF 인 augmentation (lesson 2), backend-중립 fit() (lesson 3), cache/prefetch 붙은 폴더 로드 dataset (lesson 4) — 전부 model 정의 하나 + fit() 호출 하나로 collapse.

전처리가 model 안에 사는 이유

Rescaling 이랑 augmentation block 이 어디 있는지 봐 — 어떤 Conv2D 보다도 *앞*, 맨 첫 layer 야. 그 위치가 핵심 전부. 이제 model 은 raw [0, 255] 이미지를 입력으로 기대하고 normalization 을 내부에서 알아서 해. 따로 sync 맞출 전처리 스크립트도 없고, train/serve skew 가능성도 0 — saved .keras 파일이 곧 전처리야. augmentation 은 Random* block 이라 inference 때 자동으로 조용해져서, production 은 깔끔한 rescaling 만 보고 그 외엔 안 봐.

deploy = 파일 하나 복사

변환이 weight 따라 다니니까 deploy 가 마땅히 그래야 할 만큼 지루해 — .keras 파일을 serving 머신에 복사하고, keras.saving.load_model() 하고, raw 이미지로 predict(). 같이 ship 할 전처리 코드도, 재현할 환경도, drift 도 없어. 데이터를 옆에 볼트로 붙인 스크립트가 아니라 model 의 일등 시민으로 다룬 engineering 의 보상이야.

Code

End-to-end — 전처리 + augmentation 을 model 에 박기·python
import keras
from keras import layers

# Data augmentation layers (only active during training)
augmentation = keras.Sequential([
    layers.RandomFlip("horizontal"),
    layers.RandomRotation(0.1),
    layers.RandomZoom(0.1),
])

# Model with preprocessing built in
model = keras.Sequential([
    keras.Input(shape=(224, 224, 3)),
    layers.Rescaling(1.0 / 255.0),   # Normalize
    augmentation,                       # Augment
    layers.Conv2D(32, 3, activation="relu"),
    layers.MaxPooling2D(2),
    layers.Conv2D(64, 3, activation="relu"),
    layers.MaxPooling2D(2),
    layers.GlobalAveragePooling2D(),
    layers.Dense(128, activation="relu"),
    layers.Dense(5, activation="softmax"),
])

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
model.fit(train_ds, validation_data=val_ds, epochs=20)

External links

Exercise

전체 pipeline 빌드 — dog/cat 이미지 폴더 → image_dataset_from_directory → cache/prefetch → Rescaling + augmentation + small CNN 모델 → 학습 → 저장. .keras 파일 로드 + raw image 로 predict 해서 deploy.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.