3 回答

TA貢獻1877條經(jīng)驗 獲得超6個贊
在這兩種情況下,您所說的 CNN 都在談?wù)撏患?,這是一種神經(jīng)網(wǎng)絡(luò)模型。只是預(yù)訓(xùn)練的模型已經(jīng)在其他一些數(shù)據(jù)上進行了訓(xùn)練,而不是您正在處理并嘗試分類的數(shù)據(jù)集。
這里通常使用的稱為遷移學(xué)習(xí)。與其凍結(jié)所有層,不如嘗試讓最后幾層保持打開狀態(tài),以便可以使用您自己的數(shù)據(jù)重新訓(xùn)練它們,以便預(yù)訓(xùn)練的模型也可以編輯其權(quán)重和偏差以滿足您的需求。您嘗試分類的數(shù)據(jù)集可能與預(yù)訓(xùn)練模型無關(guān)。
這是我自己工作的一個例子,還有一些額外的代碼,但你可以用你自己的代碼讓它工作,邏輯保持不變
#You extract the layer which you want to manipulate, usually the last few.
last_layer = pre_trained_model.get_layer(name_of_layer)
# Flatten the output layer to 1 dimension
x = layers.Flatten()(last_output)
# Add a fully connected layer with 1,024 hidden units and ReLU activation
x = layers.Dense(1024,activation='relu')(x)
# Add a dropout rate of 0.2
x = layers.Dropout(0.2)(x)
# Add a final sigmoid layer for classification
x = layers.Dense(1,activation='sigmoid')(x)
#Here we combine your newly added layers and the pre-trained model.
model = Model( pre_trained_model.input, x)
model.compile(optimizer = RMSprop(lr=0.0001),
loss = 'binary_crossentropy',
metrics = ['accuracy'])

TA貢獻1943條經(jīng)驗 獲得超7個贊
#You extract the layer which you want to manipulate, usually the last few.
last_layer = pre_trained_model.get_layer(name_of_layer)
# Flatten the output layer to 1 dimension
x = layers.Flatten()(last_output)
# Add a fully connected layer with 1,024 hidden units and ReLU activation
x = layers.Dense(1024,activation='relu')(x)
# Add a dropout rate of 0.2
x = layers.Dropout(0.2)(x)
# Add a final sigmoid layer for classification
x = layers.Dense(1,activation='sigmoid')(x)
#Here we combine your newly added layers and the pre-trained model.
model = Model( pre_trained_model.input, x)
model.compile(optimizer = RMSprop(lr=0.0001),
loss = 'binary_crossentropy',
metrics = ['accuracy'])

TA貢獻1785條經(jīng)驗 獲得超8個贊
除了@Ilknur Mustafa 提到的內(nèi)容之外,由于您的數(shù)據(jù)集可能與用于預(yù)訓(xùn)練的圖像不同,您可以嘗試重新訓(xùn)練預(yù)訓(xùn)練模型的最后幾層,而不是添加全新的層。下面的示例代碼除了輸出層之外沒有添加任何額外的可訓(xùn)練層。通過這種方式,您可以通過在現(xiàn)有權(quán)重上重新訓(xùn)練最后幾層來受益,而不是從頭開始訓(xùn)練。如果您沒有要訓(xùn)練的大型數(shù)據(jù)集,這可能會有所幫助。
# load model without classifier layers
vgg = VGG16(include_top=False, input_shape=(100, 100, 3), weights='imagenet', pooling='avg')
# make only last 2 conv layers trainable
for layer in vgg.layers[:-4]:
layer.trainable = False
# add output layer
out_layer = Dense(3, activation='softmax')(vgg.layers[-1].output)
model_pre_vgg = Model(vgg.input, out_layer)
# compile model
opt = SGD(lr=1e-5)
model_pre_vgg.compile(optimizer=opt, loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])
添加回答
舉報