##每周學一點--Python深度學習篇
##基于python和tensorflow的網(wǎng)絡微調(fine tunning) P117
1.在已訓練好的base network上添加自定義網(wǎng)絡伏嗜,
Conv2D, MaxPooling2D... + Flatten, Dense(自己添加的網(wǎng)絡匆绣,用于訓練)
code:
model = models.Sequential()
model.add(conv_base) #訓練好的卷積狡逢、池化網(wǎng)絡層
model.add(layers.Flatten()) #將上層的輸出展平到一維
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid')) #最后變成單一輸出,輸出值位0~1概率
2.凍結base network
code:
conv_base.trainable = False #將base network可傳播屬性設為false
3.訓練所添加網(wǎng)絡部分
code:
history = model.fit_generator(
train_generator,
steps_per_epoch=100,
epochs=30,
validation_data=validation_generator,
validation_steps=50)
4.解凍一些base network(從后往前)
通過設置參數(shù)layer.trainable = True
5.重新訓練整個網(wǎng)絡
#模型結構查看
model.summary()
##卷積神經(jīng)網(wǎng)絡可視化
#關于numpy.expand_dims(a, axis), 在axis參數(shù)位置添加一個維度泼诱?
import numpy as np
test = np.array([[1, 2, 3], [4, 5, 6]])
np.shape(test): (2, 3)
x = np.expand_dims(test, axis=0)
np.shape(x): (1, 2, 3)
y = np.expand_dims(test, axis=1)
np.shape(y): (2, 1, 3)
z = np.expand_dims(test, axis=2)
np.shape(z): (2, 3, 1)