텐서플로우 모델 구축과 학습 방법 완벽 가이드

1. 모델 및 레이어 구축

이 가이드에서는 Keras의 Subclassing API를 활용하여 모델을 설계합니다. tf.keras.Model 클래스를 상속받아 새로운 모델을 정의하고, 학습 및 평가 과정을 수동으로 구현하는 방식을 사용합니다. 이 방법은 유연성이 높으며 PyTorch, Chainer 등 다른 딥러닝 프레임워크와도 유사한 패턴을 공유합니다.

1.1 기본 모델 구조

class CustomModel(tf.keras.Model):
    def __init__(self):
        super(CustomModel, self).__init__()
        # 레이어 초기화 코드 작성
        # 예: self.conv1 = tf.keras.layers.Conv2D(32, 3)
        #     self.fc1 = tf.keras.layers.Dense(10)

    def call(self, inputs):
        # 순전파 로직 구현
        # x = self.conv1(inputs)
        # return self.fc1(x)
        return output

# 모델 인스턴스 생성
model = CustomModel()

model.variables 속성을 통해 모델 내 모든 변수를 조회할 수 있습니다.

model = CustomModel()
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)

for epoch in range(50):
    with tf.GradientTape() as tape:
        predictions = model(train_data)
        loss = tf.reduce_mean(tf.keras.losses.MSE(train_labels, predictions))
    
    gradients = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(gradients, model.trainable_variables))

print(model.variables)

1.2 모델 주요 속성

  • model.layers: 모델 구조를 구성하는 레이어 목록 반환
  • model.inputs: 모델 입력 텐서 목록 반환
  • model.outputs: 모델 출력 텐서 목록 반환
  • model.summary(): 모델 구조 요약 정보 출력

1.3 모델 평가

predictions = model.predict(test_dataset)

accuracy_metric = tf.keras.metrics.SparseCategoricalAccuracy()
accuracy_metric.update_state(y_true=test_labels, y_pred=predictions)
print(f"테스트 정확도: {accuracy_metric.result():.4f}")

2. CNN 네트워크와 전이 학습

2.1 전이 학습 활용

# 사전 학습된 MobileNetV2 모델 로드
base_model = tf.keras.applications.MobileNetV2(
    weights='imagenet', 
    include_top=False,
    input_shape=(224, 224, 3)
)

# 새로운 분류 레이어 추가
model = tf.keras.Sequential([
    base_model,
    tf.keras.layers.GlobalAveragePooling2D(),
    tf.keras.layers.Dense(5, activation='softmax')
])

주요 매개변수:

  • input_shape: 입력 이미지 크기 (기본 224x224x3)
  • include_top: 최상위 분류 레이어 포함 여부 (기본 True)
  • weights: 사전 학습 가중치 ('imagenet' 또는 None)
  • classes: 분류 클래스 수 (기본 1000)

3. Keras 파이프라인과 사용자 정의 모델

3.1 Sequential API

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')
])

3.2 Functional API

inputs = tf.keras.Input(shape=(28, 28, 1))
x = tf.keras.layers.Conv2D(32, 3, activation='relu')(inputs)
x = tf.keras.layers.MaxPooling2D()(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(64, activation='relu')(x)
outputs = tf.keras.layers.Dense(10, activation='softmax')(x)

model = tf.keras.Model(inputs=inputs, outputs=outputs)

3.3 모델 컴파일, 학습 및 평가

# 모델 컴파일
model.compile(
    optimizer=tf.keras.optimizers.Adam(),
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

# 모델 학습
history = model.fit(
    train_images, 
    train_labels,
    epochs=10,
    batch_size=32,
    validation_split=0.2
)

# 모델 평가
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"테스트 정확도: {test_acc:.4f}")

3.4 모델 저장 및 복원

가중치만 저장

# 가중치 저장
model.save_weights('model_weights.h5')

# 가중치 복원
model.load_weights('model_weights.h5')

전체 모델 저장

# 전체 모델 저장
model.save('full_model.h5')

# 모델 복원
restored_model = tf.keras.models.load_model('full_model.h5')

3.5 사용자 정의 레이어, 손실 함수 및 평가 지표

사용자 정의 레이어

class CustomDenseLayer(tf.keras.layers.Layer):
    def __init__(self, units=32):
        super(CustomDenseLayer, self).__init__()
        self.units = units
    
    def build(self, input_shape):
        self.w = self.add_weight(
            shape=(input_shape[-1], self.units),
            initializer='glorot_uniform',
            trainable=True
        )
        self.b = self.add_weight(
            shape=(self.units,),
            initializer='zeros',
            trainable=True
        )
    
    def call(self, inputs):
        return tf.matmul(inputs, self.w) + self.b

사용자 정의 손실 함수

class CustomMSE(tf.keras.losses.Loss):
    def call(self, y_true, y_pred):
        return tf.reduce_mean(tf.square(y_true - y_pred))

사용자 정의 평가 지표

class CustomAccuracy(tf.keras.metrics.Metric):
    def __init__(self):
        super(CustomAccuracy, self).__init__()
        self.correct = self.add_weight(name='correct', initializer='zeros')
        self.total = self.add_weight(name='total', initializer='zeros')
    
    def update_state(self, y_true, y_pred, sample_weight=None):
        predictions = tf.argmax(y_pred, axis=-1)
        matches = tf.cast(tf.equal(y_true, predictions), tf.float32)
        self.correct.assign_add(tf.reduce_sum(matches))
        self.total.assign_add(tf.cast(tf.size(y_true), tf.float32))
    
    def result(self):
        return self.correct / self.total

4. 주요 기능 모듈

4.1 콜백(Callbacks)

from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard

# 체크포인트 콜백
checkpoint = ModelCheckpoint(
    'model_{epoch:02d}_{val_accuracy:.2f}.h5',
    monitor='val_accuracy',
    save_best_only=True,
    mode='max'
)

# 텐서보드 콜백
tensorboard = TensorBoard(log_dir='./logs')

# 학습에 콜백 적용
history = model.fit(
    train_data, train_labels,
    epochs=20,
    validation_split=0.2,
    callbacks=[checkpoint, tensorboard]
)

4.2 tf.data 데이터셋 구축 및 전처리

# 메모리 데이터로 데이터셋 생성
dataset = tf.data.Dataset.from_tensor_slices((features, labels))

# 데이터 전처리 파이프라인
dataset = dataset.shuffle(buffer_size=1000)
dataset = dataset.batch(32)
dataset = dataset.map(lambda x, y: (tf.image.resize(x, (224, 224)), y))
dataset = dataset.prefetch(tf.data.AUTOTUNE)

# 데이터셋 반복 학습
for batch_data, batch_labels in dataset:
    # 학습 로직
    pass

태그: TensorFlow keras Subclassing API CNN MobileNetV2

6월 14일 19:54에 게시됨