keras構建以vgg16為模板的模型

2021-10-07 02:57:02 字數 1284 閱讀 8481

構建模型

model_vgg = vgg16(include_top=false,weights=『imagenet』,inpute_shape=(48,48,3))

for layer in model_vgg.layers:

layer.trainable = false

model = flatten(name=『flatten』)(model_vgg.output)

model = dense(4096,activation=『relue』,name=『fc1』)(model)

model = dense(4096,activation=『relue』,name=『fc2』)(model)

model = dropot(0.5)(model)

model_vgg_mnist = model(inputs = model_vgg.input,outputs=model,name=『vgg16』)

model_vgg_mnist.summary()#列印結構

編譯模型sgd = sgd(lr = 0.05,decay=1e-5) model_vagg_mnist.compile(loss='categorical_corssentropy',optimizer=sgd,metrics=['acc'])

讀取資料

(x,train,y_train),(x_test,y_test) = mist.load_data(』…/test_data_home』)

x_train = [ cv2.cvtcolor(cv2.resize(i,(48,48)),cv2.color_gray2rgb) for i in x_train

]#將單通道的通過opencv轉化成rgb的三通道影象。

#將三維陣列的列表轉化為四維陣列arr[none]相當於新增乙個空間

x_train = np.concatenate([arr[np.newaxis] for arr in x_train]).astype(『float32』)

def tran_y(y):

y_ohe = np.zeros(10)

y_ohe[y] = 1

return y_ohe

y_train_ohe = np.array([tran_y(y_train[i]) for i in range(len(y_train) ])

model_vgg_mnist.fit(x_trian,y_train_ohe,validation_data=(x_test,y_test_ohe)),epochs = 100,batch_size = 100)

VGG16模型理解

vgg16作為很入門的cnn網路,同時也有很多基於vgg16的改進網路,比如用於語義分割的segnet等。1 輸入224x224x3的,經過64個卷積核的兩次卷積後,採用一次pooling。經過第一次卷積後,c1有 3x3x3 個可訓練引數 2 之後又經過兩次128的卷積核卷積之後,採用一次pool...

VGG16學習筆記

vgg16 一 摘要 二 convnet的配置 所有隱藏層之後,都配有relu rectified linear uint 修正線性單元 vgg 16中的16指的是在這個網路中包含16個卷積層和全連線層。此外還有vgg 19,由於vgg 16和vgg 19的表現幾乎誤無差,且vgg 16的引數較vg...

使用Keras提供的VGG16來提取feature

直接上code import os import h5py from tqdm import tqdm import tensorflow as tf from tensorflow.keras.preprocessing import image from tensorflow.keras.mod...