You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何缩小Keras模型存储体积?VGG16迁移学习模型存储优化

How to Shrink Your Keras .h5 Model Size for GitHub (While Keeping It Functional)

Got it, let's break down how to get that 200MB VGG16 transfer learning model under GitHub's 100MB limit—without breaking its ability to make predictions. Here are four actionable, tested approaches:

1. Save Only Model Weights (Not the Full Model)

The default model.save() stores three things: the model architecture, weights, and optimizer state. If you already have code that defines your transfer learning model (i.e., you're reusing the VGG16 base and adding your custom top layers), you don't need to save the architecture again.

  • Save weights only:
    model.save_weights('vgg16_transfer_weights.h5')
    
  • Load later:
    First redefine your model architecture exactly as you did during training, then load the weights:
    # Recreate your VGG16 transfer learning model here
    from tensorflow.keras.applications import VGG16
    from tensorflow.keras.models import Model
    from tensorflow.keras.layers import Dense, Flatten
    
    base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224,224,3))
    x = base_model.output
    x = Flatten()(x)
    x = Dense(256, activation='relu')(x)
    predictions = Dense(your_num_classes, activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=predictions)
    
    # Load the saved weights
    model.load_weights('vgg16_transfer_weights.h5')
    

This cuts out the architecture and optimizer data, usually reducing the file size to 50-80MB (way under the limit).

2. Quantize the Model (Reduce Precision)

You can convert the model's 32-bit floating-point weights to 16-bit (or even 8-bit) with minimal impact on prediction accuracy. This halves (or more) the file size instantly.

Option A: Mixed Precision Keras Model

from tensorflow.keras.models import load_model
from tensorflow.keras import mixed_precision

# Load your original model
model = load_model('original_model.h5')

# Enable mixed precision (uses 16-bit for weights where possible)
mixed_precision.set_global_policy('mixed_float16')

# Recompile (only needed if you plan to retrain; skip if just predicting)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Save the quantized model
model.save('quantized_vgg16_model.h5')

Option B: Convert to TensorFlow Lite (TFLite) with Quantization

TFLite's default optimization applies 8-bit quantization, which shrinks size drastically:

import tensorflow as tf

# Convert the Keras model to quantized TFLite
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()

# Save the TFLite model
with open('quantized_vgg16.tflite', 'wb') as f:
    f.write(tflite_quant_model)

To use the TFLite model, you'll need the TensorFlow Lite Interpreter, but it's fully functional for predictions.

3. Exclude Optimizer State

If you don't plan to retrain the model (only use it for inference), you can skip saving the optimizer's state (which includes things like momentum values from training). This won't cut size as much as the above methods, but it's a quick win:

model.save('model_no_optimizer.h5', include_optimizer=False)

This removes the training-related optimizer data, reducing the file size by 10-20MB typically.

4. Use GitHub LFS (If All Else Fails)

If you need to keep the full 200MB model intact, GitHub's Large File Storage (LFS) is designed for this. It lets you store large files outside your main repository while still tracking them with Git.

  • Setup steps:
    1. Install Git LFS: git lfs install
    2. Track your .h5 file: git lfs track "*.h5"
    3. Commit and push normally:
      git add .gitattributes your_model.h5
      git commit -m "Add model via GitHub LFS"
      git push
      

GitHub gives 1GB of free LFS storage, so 200MB is well within the limit.


内容的提问来源于stack exchange,提问作者Akash Chandra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:03:54