Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Load weights từ checkpoint và re-evaluate model Keras Python

Mô tả

Load weights từ file checkpoint cụ thể vào model mới, sau đó evaluate trên test set.

Code ví dụ

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np

def create_model():
    model = keras.Sequential([
        layers.Dense(64, activation='relu', input_shape=(784,)),
        layers.Dense(10, activation='softmax')
    ])
    model.compile(optimizer='adam', 
                  loss='sparse_categorical_crossentropy', 
                  metrics=['accuracy'])
    return model

# Dummy data
x_test = np.random.rand(200, 784).astype('float32')
y_test = np.random.randint(0, 10, 200)

# Giả sử checkpoint_path đã tồn tại từ training trước
checkpoint_path = "training_2/cp-0004.ckpt"

# 1. TẠO MODEL MỚI (CÙNG KIẾN TRÚC)
model = create_model()
print("New model instance created")

# 2. LOAD WEIGHTS TỪ CHECKPOINT
model.load_weights(checkpoint_path)
print("Weights loaded from checkpoint")

# 3. RE-EVALUATE
print("Re-evaluating model...")
loss, acc = model.evaluate(x_test, y_test, verbose=0)
print(f"Restored model - Loss: {loss:.4f}, Accuracy: {acc:.4f} ({acc*100:.2f}%)")

# 4. PREDICTION
predictions = model.predict(x_test[:5], verbose=0)
print(f"Sample predictions: {np.argmax(predictions, axis=1)}")

Các cách load_weights()

# 1. Load từ checkpoint path (tự động tìm .index + .data)
model.load_weights('checkpoints/cp-0004.ckpt')

# 2. Load từ HDF5 weights file
model.load_weights('model_weights.h5')

# 3. Load với by_name=True (mặc định) - match theo tên layer
model.load_weights('weights.h5', by_name=True)

# 4. Bỏ qua layer không khớp (transfer learning)
model.load_weights('pretrained.h5', by_name=True, skip_mismatch=True)

# 5. Load partial weights (chỉ một số layer)
# Cần manual assignment:
# model.layers[0].set_weights(pretrained_layers[0].get_weights())

Kiểm tra weights đã load đúng

# So sánh với weights gốc (nếu có)
original_weights = [w.numpy() for w in original_model.weights]
loaded_weights = [w.numpy() for w in model.weights]

for i, (orig, loaded) in enumerate(zip(original_weights, loaded_weights)):
    if np.allclose(orig, loaded):
        print(f"Layer {i}: OK")
    else:
        print(f"Layer {i}: MISMATCH!")

# Hoặc check norm difference
for layer in model.layers:
    weights = layer.get_weights()
    if weights:
        print(f"{layer.name}: {len(weights)} tensors, shapes={[w.shape for w in weights]}")

Lỗi thường gặp

LỗiNguyên nhânGiải pháp
ValueError: Layer weight shape mismatchKiến trúc model khác (số units, input shape)Đảm bảo create_model() y hệt
FileNotFoundErrorCheckpoint path saiKiểm tra tf.train.latest_checkpoint()
Checkpoint has no variable...Load weights vào model chưa buildGọi model.build(input_shape) trước

Best Practice

  1. Luôn dùng factory function create_model() để đảm bảo architecture giống hệt.
  2. Save architecture config riêng: model.to_json() hoặc model.get_config().
  3. Dùng skip_mismatch=True cho transfer learning.
  4. Verify sau load: model.evaluate() hoặc check weight norms.