keras實現VGG16方式(預測一張圖片)
阿新 • • 發佈:2020-07-08
我就廢話不多說了,大家還是直接看程式碼吧~
from keras.applications.vgg16 import VGG16#直接匯入已經訓練好的VGG16網路 from keras.preprocessing.image import load_img#load_image作用是載入圖片 from keras.preprocessing.image import img_to_array from keras.applications.vgg16 import preprocess_input from keras.applications.vgg16 import decode_predictions model = VGG16() image = load_img('D:\\photo\\dog.jpg',target_size=(224,224))#引數target_size用於設定目標的大小,如此一來無論載入的原影象大小如何,都會被標準化成統一的大小,這樣做是為了向神經網路中方便地輸入資料所需的。 image = img_to_array(image)#函式img_to_array會把影象中的畫素資料轉化成NumPy中的array,這樣資料才可以被Keras所使用。 #神經網路接收一張或多張影象作為輸入,也就是說,輸入的array需要有4個維度: samples,rows,columns,and channels。由於我們僅有一個 sample(即一張image),我們需要對這個array進行reshape操作。 image = image.reshape((1,image.shape[0],image.shape[1],image.shape[2])) image = preprocess_input(image)#對影象進行預處理 y = model.predict(image)#預測影象的類別 label = decode_predictions(y)#Keras提供了一個函式decode_predictions(),用以對已經得到的預測向量進行解讀。該函式返回一個類別列表,以及類別中每個類別的預測概率, label = label[0][0] print('%s(%.2f%%)'%(label[1],label[2]*100)) # print(model.summary())
from keras.models import Sequential from keras.layers.core import Flatten,Dense,Dropout from keras.layers.convolutional import Convolution2D,MaxPooling2D,ZeroPadding2D from keras.optimizers import SGD import numpy as np from keras.preprocessing import image from keras.applications.imagenet_utils import preprocess_input,decode_predictions import time from keras import backend as K K.set_image_dim_ordering('th') def VGG_16(weights_path=None): model = Sequential() model.add(ZeroPadding2D((1,1),input_shape=(3,224,224))) model.add(Convolution2D(64,(3,3),activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(64,activation='relu')) model.add(MaxPooling2D((2,2),strides=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128,1))) model.add(Convolution2D(256,1))) model.add(Convolution2D(512,2))) model.add(Flatten()) model.add(Dense(4096,activation='relu')) model.add(Dropout(0.5)) model.add(Dense(4096,activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1000,activation='softmax')) if weights_path: model.load_weights(weights_path,by_name=True) return model model = VGG_16(weights_path='F:\\Kaggle\\vgg16_weights.h5') sgd = SGD(lr=0.1,decay=1e-6,momentum=0.9,nesterov=True) model.compile(optimizer=sgd,loss='categorical_crossentropy') t0 = time.time() img = image.load_img('D:\\photo\\dog.jpg',224)) x = image.img_to_array(img) # 三維(224,224,3) x = np.expand_dims(x,axis=0) # 四維(1,224,224,3)#因為keras要求的維度是這樣的,所以要增加一個維度 x = preprocess_input(x) # 預處理 print(x.shape) y_pred = model.predict(x) # 預測概率 t1 = time.time() print("測試圖:",decode_predictions(y_pred)) # 輸出五個最高概率(類名,語義概念,預測概率) print("耗時:",str((t1 - t0) * 1000),"ms")
這是兩種不同的方式,第一種是直接使用vgg16的引數,需要在執行時下載,第二種是我們已經下載好的權重,直接在引數中輸入我們的路徑即可。
補充知識:keras加經典網路的預訓練模型(以VGG16為例)
我就廢話不多說了,大家還是直接看程式碼吧~
# 使用VGG16模型 from keras.applications.vgg16 import VGG16 print('Start build VGG16 -------') # 獲取vgg16的卷積部分,如果要獲取整個vgg16網路需要設定:include_top=True model_vgg16_conv = VGG16(weights='imagenet',include_top=False) model_vgg16_conv.summary() # 建立自己的輸入格式 # if K.image_data_format() == 'channels_first': # input_shape = (3,img_width,img_height) # else: # input_shape = (img_width,img_height,3) input = Input(input_shape,name = 'image_input') # 注意,Keras有個層就是Input層 # 將vgg16模型原始輸入轉換成自己的輸入 output_vgg16_conv = model_vgg16_conv(input) # output_vgg16_conv是包含了vgg16的卷積層,下面我需要做二分類任務,所以需要新增自己的全連線層 x = Flatten(name='flatten')(output_vgg16_conv) x = Dense(4096,activation='relu',name='fc1')(x) x = Dense(512,name='fc2')(x) x = Dense(128,name='fc3')(x) x = Dense(1,activation='softmax',name='predictions')(x) # 最終創建出自己的vgg16模型 my_model = Model(input=input,output=x) # 下面的模型輸出中,vgg16的層和引數不會顯示出,但是這些引數在訓練的時候會更改 print('\nThis is my vgg16 model for the task') my_model.summary()
以上這篇keras實現VGG16方式(預測一張圖片)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。