keras如何实现VGG16
小编给大家分享一下keras如何实现VGG16,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!
from keras.applications.vgg16 import VGG16#直接导入已经训练好的VGG16网络from keras.preprocessing.image import load_img#load_image作用是载入图片from keras.preprocessing.image import img_to_arrayfrom keras.applications.vgg16 import preprocess_inputfrom 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 Sequentialfrom keras.layers.core import Flatten,Dense,Dropoutfrom keras.layers.convolutional import Convolution2D,MaxPooling2D,ZeroPadding2Dfrom keras.optimizers import SGDimport numpy as np from keras.preprocessing import imagefrom keras.applications.imagenet_utils import preprocess_input, decode_predictionsimport timefrom keras import backend as KK.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, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 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.h6')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', target_size=(224, 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 VGG16print('Start build VGG16 -------') # 获取vgg16的卷积部分,如果要获取整个vgg16网络需要设置:include_top=Truemodel_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, activation='relu', name='fc2')(x)x = Dense(128, activation='relu', 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有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。