Last active
June 28, 2017 04:49
-
-
Save jianning-li/d950c33f93dc211bc2a2cee8a38d0131 to your computer and use it in GitHub Desktop.
VGG16 nets for image classification using pre-trained weights with tensorflow backend(for theano backend please refer to 'VGG-16 pre-trained model for Keras' @baraldilorenzo)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| """ | |
| Created on Mon Jun 26 19:55:37 2017 | |
| @author: lijianning | |
| """ | |
| # -*- coding: utf-8 -*- | |
| 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 h5py | |
| import cv2, numpy as np | |
| def VGG_16(): | |
| model = Sequential() | |
| model.add(ZeroPadding2D((1,1),input_shape=(224,224,3))) | |
| 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')) | |
| return model | |
| def load_weights(weights_path,model): | |
| f = h5py.File(weights_path) | |
| for k in range(f.attrs['nb_layers']): | |
| for k in range(f.attrs['nb_layers']): | |
| break | |
| g = f['layer_{}'.format(k)] | |
| weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])] | |
| model.layers[k].set_weights(weights) | |
| f.close() | |
| return model | |
| model1=VGG_16() | |
| model2=load_weights('D:/vgg16_imageClassify/vgg16_weights.h5',model1) | |
| im = cv2.imread('cat1.jpg').astype(np.float32) | |
| im=cv2.resize(im,(224,224)) | |
| im[:,:,0] -= 103.939 | |
| im[:,:,1] -= 116.779 | |
| im[:,:,2] -= 123.68 | |
| #im = im.transpose((2,0,1)) | |
| im = np.expand_dims(im, axis=0) | |
| #model2.compile(optimizer=sgd, loss='mse') | |
| out = model2.predict(im) | |
| np.argmax(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment