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

Keras Preprocessing Layer

~8 min · data

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

모델은 engineering 의 30%, data pipeline 이 나머지 70%. Keras 3 는 모델 graph *안에서* 돌아가는 preprocessing layer (image augmentation, text vectorization, normalization) 가 강력해 — 저장된 model 에 같이 따라가. 거기에 tf.data, torch.DataLoader, grain 붙이는 법까지.

전처리가 script 가 아니라 layer

production ML 을 가장 많이 깨먹는 건 모델이 아니라 *학습 때 전처리* 랑 *서빙 때 전처리* 사이 틈이야. train 코드랑 serve 코드가 따로 놀면 미묘하게 어긋나고 — 그게 train/serve skew. Keras 3 는 전처리를 아예 layer 로 만들어서 이 틈을 없애. Rescaling, Normalization, RandomFlip, RandomRotation, TextVectorization, IntegerLookup — 다 다른 layer 처럼 쌓는 진짜 layer 야. 모델 *안에* 박으면 saved .keras 파일이 똑같은 변환을 production 까지 그대로 들고 가. 공짜로.

학습 전에 adapt

stateful layer — Normalization 이랑 TextVectorization — 는 쓰기 전에 데이터를 *봐야* 해. .adapt(data) 한 번 호출하면 통계 (mean/variance, 또는 vocabulary) 를 학습해서 weight 로 굳혀. 그 뒤론 결정론적 — inference 때도 똑같은 변환을 영원히 적용해. Rescaling 같은 stateless layer 는 이 단계 건너뛰어 — 고정된 1/255 나누기엔 배울 게 없으니까. 아래 Code block 이 두 종류 다 보여주고 rescaler 를 model 에 박아.

박을 위치 두 군데

선택지 둘: 전처리를 모델 안에 (portable, deploy 안전 — 변환이 weight 따라 다님) 또는 tf.data pipeline 에 (학습 빠름 — CPU 에서 GPU step 이랑 병렬로 돌아서). 기준: deploy 에 꼭 필요한 learned 변환 (normalization, vocabulary) 은 *모델 안에*, 싸고 버려도 되는 학습 전용 작업 (random augmentation) 은 *data pipeline 에* — 속도가 중요하고 skew 위험이 0 인 곳에.

Code

stateful layer 는 adapt 후 모델에 박기·python
# Normalization: learns mean/variance from data
normalizer = layers.Normalization()
normalizer.adapt(training_data)  # Must see data first

# Rescaling: simple linear transform
rescaler = layers.Rescaling(1.0 / 255.0)

# TextVectorization: text → integer tokens
vectorizer = layers.TextVectorization(
    max_tokens=20000,
    output_sequence_length=200,
)
vectorizer.adapt(text_data)

# Put inside the model
model = keras.Sequential([
    rescaler,                                    # Preprocessing
    layers.Conv2D(32, 3, activation="relu"),  # Model layers
    layers.GlobalAveragePooling2D(),
    layers.Dense(10, activation="softmax"),
])

External links

Exercise

Rescaling + RandomFlip 을 첫 두 layer 로 가지는 모델 짜. 학습 / 저장 / 로드. raw [0,255] image 로 predict, rescaling 동작 확인 (에러 없이 sane 출력).

Progress

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

댓글 0

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

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