keras源碼engine中toplogy.py定義了加載權重的函數:load_weights(self, filepath, by_name=False)
其中默認by_name爲False,這時候加載權重按照網絡拓撲結構加載,適合直接使用keras中自帶的網絡模型,如VGG16
VGG19/resnet50等,源碼描述如下:
If `by_name` is False (default) weights are loaded
based on the network's topology, meaning the architecture
should be the same as when the weights were saved.
Note that layers that don't have weights are not taken
into account in the topological ordering, so adding or
removing layers is fine as long as they don't have weights.
若將by_name改爲True則加載權重按照layer的name進行,layer的name相同時加載權重,適合用於改變了
模型的相關結構或增加了節點但利用了原網絡的主體結構情況下使用,源碼描述如下:
If `by_name` is True, weights are loaded into layers
only if they share the same name. This is useful
for fine-tuning or transfer-learning models where
some of the layers have changed.
在進行邊緣檢測時,利用VGG網絡的主體結構,網絡中增加反捲積層,這時加載權重應該使用
model.load_weights(filepath,by_name=True)