[英]How to train a model given in keras.applications from scratch by randomly initialising weights?
我的理解是 keras 模型具有使用 imagenet 數據集預先訓練的權重。 出於學習目的,我想用隨機初始化的權重從頭開始訓練。
首先我從 keras 加載模型。 在這里,我沒有包含我在一些示例中看到的weights='imagenet'
參數。 如果我不包括這個參數,這是否意味着模型權重是隨機初始化的?
import os, sys
from keras.utils.vis_utils import plot_model
from keras.applications import VGG16
from keras.layers import Input
from keras.optimizers import SGD
base_model = VGG16(, include_top=False, input_tensor=Input(shape = (224,224,3)))
base_model.summary()
plot_model(base_model, to_file=model_diagram_path, show_shapes=True)
接下來,我將輸出層添加回模型。 我復制了原始 VGG16 模型中給出的相同結構。
from keras.layers.core import Flatten
from keras.layers.core import Dense
from keras.utils.vis_utils import plot_model
from keras.models import Model
# Create head part
head_model = base_model.output
head_model = Flatten(name='flatten')(head_model)
head_model = Dense(4096,activation='relu')(head_model)
head_model = Dense(4096,activation='relu')(head_model)
head_model = Dense(len(class_names),activation='softmax')(head_model)
# Attach head to model
model = Model(inputs=base_model.input, outputs = head_model)
model_diagram_path = 'vgg16-output-modified.png'
plot_model(model, to_file=model_diagram_path, show_shapes=True)
這種方法是否允許我從頭開始訓練模型? 如果不是,正確的方法是什么?
要從頭開始訓練,您應該為weights
參數傳遞None
。
base_model = VGG16(weights=None, include_top=False, input_tensor=Input(shape = (224,224,3)))
調用上述行后,您應該看到下載尚未開始。
根據他們的Github Source , weights
的默認參數是'imagenet'
所以跳過這個參數並通過調用你的模型
base_model = VGG16(include_top=False, input_tensor=Input(shape = (224,224,3)))
仍將下載和加載 Imagenet 權重。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.