|
|
|
''' |
|
@author: Nitish Bhardwaj |
|
Keras Testing model for getting features from the VGG net trained model |
|
''' |
|
|
|
from keras.models import Sequential, Model |
|
from keras.layers import Dense, Dropout, Activation, Flatten, Input |
|
from keras.layers import Convolution2D, MaxPooling2D,ZeroPadding2D |
|
from keras.optimizers import SGD |
|
import theano |
|
import os |
|
import cv2 |
|
import numpy as np |
|
from sklearn.decomposition import PCA |
|
|
|
|
|
# input image dimensions |
|
img_rows, img_cols = 224, 224 |
|
img_channels = 3 |
|
nb_classes = 10 |
|
image_path = 'bb.jpg' |
|
weights_path = 'weights.h5' |
|
|
|
|
|
|
|
#Keras deepnet VGG-trained model |
|
def load_trained_model(): |
|
|
|
trained_model = Sequential() |
|
trained_model.add(ZeroPadding2D((1,1),input_shape=(3,img_rows,img_cols))) |
|
trained_model.add(Convolution2D(64, 3, 3, activation='relu')) |
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(64, 3, 3, activation='relu')) |
|
trained_model.add(MaxPooling2D((2,2), strides=(2,2))) |
|
|
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(128, 3, 3, activation='relu')) |
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(128, 3, 3, activation='relu')) |
|
trained_model.add(MaxPooling2D((2,2), strides=(2,2))) |
|
|
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(256, 3, 3, activation='relu')) |
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(256, 3, 3, activation='relu')) |
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(256, 3, 3, activation='relu')) |
|
trained_model.add(MaxPooling2D((2,2), strides=(2,2))) |
|
|
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
trained_model.add(MaxPooling2D((2,2), strides=(2,2))) |
|
|
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
trained_model.add(ZeroPadding2D((1,1))) |
|
trained_model.add(Convolution2D(512, 3, 3, activation='relu')) |
|
trained_model.add(MaxPooling2D((2,2), strides=(2,2))) |
|
|
|
trained_model.add(Flatten()) |
|
trained_model.add(Dense(4096, activation='relu')) |
|
trained_model.add(Dropout(0.5)) |
|
trained_model.add(Dense(4096, activation='relu')) |
|
trained_model.add(Dropout(0.5)) |
|
trained_model.add(Dense(1000, activation='softmax')) |
|
trained_model.load_weights(weights_path) |
|
return trained_model |
|
|
|
|
|
# Load pretrained model and adding keras functional api to add more layers and to extract 256 features |
|
def cnn(): |
|
trained_model = load_trained_model() |
|
|
|
#VGG model |
|
main_input = Input(shape=(3,img_rows,img_cols), dtype='float32', name='main_input') |
|
zpad1 = ZeroPadding2D((1,1), input_shape=(3,img_rows,img_cols), name='zpad1')(main_input) |
|
conv1 = Convolution2D(64, 3, 3, name='conv1', activation='relu')(zpad1) |
|
zpad2 = ZeroPadding2D((1,1), name='zpad2')(conv1) |
|
conv2 = Convolution2D(64, 3, 3, name='conv2', activation='relu')(zpad2) |
|
maxp1 = MaxPooling2D((2,2), strides=(2,2), name='maxp1')(conv2) |
|
|
|
zpad3 = ZeroPadding2D((1,1), name='zpad3')(maxp1) |
|
conv3 = Convolution2D(128, 3, 3, name='conv3', activation='relu')(zpad3) |
|
zpad4 = ZeroPadding2D((1,1), name='zpad4')(conv3) |
|
conv4 = Convolution2D(128, 3, 3, name='conv4', activation='relu')(zpad4) |
|
maxp2 = MaxPooling2D((2,2), strides=(2,2), name='maxp2')(conv4) |
|
|
|
zpad5 = ZeroPadding2D((1,1), name='zpad5')(maxp2) |
|
conv5 = Convolution2D(256, 3, 3, name='conv5', activation='relu')(zpad5) |
|
zpad6 = ZeroPadding2D((1,1), name='zpad6')(conv5) |
|
conv6 = Convolution2D(256, 3, 3, name='conv6', activation='relu')(zpad6) |
|
zpad7 = ZeroPadding2D((1,1), name='zpad7')(conv6) |
|
conv7 = Convolution2D(256, 3, 3, name='conv7', activation='relu')(zpad7) |
|
maxp3 = MaxPooling2D((2,2), strides=(2,2), name='maxp3')(conv7) |
|
|
|
zpad8 = ZeroPadding2D((1,1), name='zpad8')(maxp3) |
|
conv8 = Convolution2D(512, 3, 3, name='conv8', activation='relu')(zpad8) |
|
zpad9 = ZeroPadding2D((1,1), name='zpad9')(conv8) |
|
conv9 = Convolution2D(512, 3, 3, name='conv9', activation='relu')(zpad9) |
|
zpad10 = ZeroPadding2D((1,1), name='zpad10')(conv9) |
|
conv10 = Convolution2D(512, 3, 3, name='conv10', activation='relu')(zpad10) |
|
maxp4 = MaxPooling2D((2,2), strides=(2,2), name='maxp4')(conv10) |
|
|
|
zpad11 = ZeroPadding2D((1,1), name='zpad11')(maxp4) |
|
conv11 = Convolution2D(512, 3, 3, name='conv11', activation='relu')(zpad11) |
|
zpad12 = ZeroPadding2D((1,1), name='zpad12')(conv11) |
|
conv12 = Convolution2D(512, 3, 3, name='conv12', activation='relu')(zpad12) |
|
zpad13 = ZeroPadding2D((1,1), name='zpad13')(conv12) |
|
conv13 = Convolution2D(512, 3, 3, name='conv13', activation='relu')(zpad13) |
|
maxp5 = MaxPooling2D((2,2), strides=(2,2), name='maxp5')(conv13) |
|
|
|
flatten = Flatten(name='flatten')(maxp5) |
|
dense1 = Dense(4096, activation='relu', name='dense1')(flatten) |
|
dropout1 = Dropout(0.5, name='dropout1')(dense1) |
|
dense2 = Dense(4096, activation='relu', name='dense2')(dropout1) |
|
dropout2 = Dropout(0.5, name='dropout2')(dense2) |
|
dense3 = Dense(1000, activation='relu', name='dense3')(dropout2) |
|
dropout3 = Dropout(0.5, name='dropout3')(dense3) |
|
dense4 = Dense(256, activation='relu', name='dense4')(dropout3) |
|
|
|
|
|
print "==Defining Model ==" |
|
base_model = Model(input=[main_input], output=[dense4]) |
|
for i,layer in enumerate(trained_model.layers): |
|
base_model.layers[i+1].set_weights(layer.get_weights()) |
|
|
|
print "==Compiling Model ==" |
|
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) |
|
base_model.compile(optimizer=sgd, loss='categorical_crossentropy') |
|
|
|
#Getting model for intermediate output |
|
model = Model(input=base_model.input, output=base_model.get_layer('dense4').output) |
|
return model |
|
|
|
|
|
|
|
def get_features(model,image): |
|
#Getting features from the given image |
|
test_image = [cv2.resize(image,(img_cols,img_rows))] |
|
test_image = np.transpose(test_image, [0,3,1,2]).astype('float32')[:,:,:,:] |
|
|
|
print "==Getting Features==" |
|
out = model.predict(test_image) |
|
# print out.shape |
|
return out |
|
|
|
|
|
|
|
#Loading DeepNet model |
|
model = cnn() |
|
|
|
#Reading image |
|
frame = cv2.imread(image_path) |
|
|
|
#printing 256 features |
|
print get_features(model, frame) |