tensorflow怎么加载预训练权重'model_weights.h5'
时间: 2024-01-11 10:04:10 浏览: 139
使用TensorFlow加载预训练权重可以通过以下步骤进行:
1. 导入必要的库
```
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import ...
```
2. 构建模型
```
inputs = tf.keras.Input(shape=(input_shape))
x = ...
outputs = ...
model = tf.keras.Model(inputs=inputs, outputs=outputs)
```
3. 加载预训练权重
```
model.load_weights('model_weights.h5')
```
完整代码示例:
```
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense
# 构建模型
inputs = Input(shape=(224, 224, 3))
x = Conv2D(64, (3, 3), activation='relu', padding='same')(inputs)
x = MaxPooling2D((2, 2))(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2))(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2))(x)
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
outputs = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
# 加载预训练权重
model.load_weights('model_weights.h5')
```
阅读全文