-
-
Save nitish11/73ba862753929e08b3b319ff1e8c9c09 to your computer and use it in GitHub Desktop.
Revisions
-
nitish11 revised this gist
Oct 20, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ model and usage demo: see `vgg-16_keras.py` weights: [vgg16_weights.h5](https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmRCM0ROVHc/view?usp=sharing) Extended VGG model to get 256 features : see `vgg-extended-functional-api.py` * Loaded trained VGG model with weights given in above link * Added Extra layers(Dense layers of keras, also called Fully connected layer) in the VGG net by using keras functional API to extract features as per our requirement. -
nitish11 revised this gist
Oct 20, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ model and usage demo: see `vgg-16_keras.py` weights: [vgg16_weights.h5](https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmRCM0ROVHc/view?usp=sharing) Extended VGG model : see `vgg-extended-functional-api.py` * Loaded trained VGG model with weights given in above link * Added Extra layers(Dense layers of keras, also called Fully connected layer) in the VGG net by using keras functional API to extract features as per our requirement. -
nitish11 revised this gist
Oct 20, 2016 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -23,4 +23,5 @@ weights: [vgg16_weights.h5](https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmR Extended VGG model : see `vgg-extended-functional-api-py` * Loaded trained VGG model with weights given in above link * Added Extra layers(Dense layers of keras, also called Fully connected layer) in the VGG net by using keras functional API to extract features as per our requirement. -
nitish11 revised this gist
Oct 20, 2016 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -20,3 +20,7 @@ Please cite the paper if you use the models. model and usage demo: see `vgg-16_keras.py` weights: [vgg16_weights.h5](https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmRCM0ROVHc/view?usp=sharing) Extended VGG model : see `vgg-extended-functional-api-py` * Added Extra layers(Dense layers of keras) in the VGG net by using keras functional API to extract features as per our requirement. -
nitish11 revised this gist
Oct 20, 2016 . 1 changed file with 163 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,163 @@ ''' @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) -
baraldilorenzo revised this gist
Jan 16, 2016 . 1 changed file with 12 additions and 8 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -10,37 +10,37 @@ def VGG_16(weights_path=None): 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')) @@ -53,14 +53,18 @@ def VGG_16(weights_path=None): model.load_weights(weights_path) return model if __name__ == "__main__": im = cv2.resize(cv2.imread('cat.jpg'), (224, 224)).astype(np.float32) 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) # Test pretrained model model = VGG_16('vgg16_weights.h5') sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='categorical_crossentropy') out = model.predict(im) print np.argmax(out) -
baraldilorenzo revised this gist
Nov 9, 2015 . 1 changed file with 18 additions and 20 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -6,56 +6,54 @@ 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), stride=(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), stride=(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), stride=(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), stride=(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), stride=(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) return model if __name__ == "__main__": im = cv2.resize(cv2.imread('cat.jpg'), (224, 224)) im = im.transpose((2,0,1)) -
baraldilorenzo revised this gist
Sep 6, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -17,6 +17,6 @@ Please cite the paper if you use the models. ###Contents: model and usage demo: see `vgg-16_keras.py` weights: [vgg16_weights.h5](https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmRCM0ROVHc/view?usp=sharing) -
baraldilorenzo revised this gist
Sep 6, 2015 . 2 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes. -
baraldilorenzo created this gist
Sep 6, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ ##VGG16 model for Keras This is the [Keras](http://keras.io/) model of the 16-layer network used by the VGG team in the ILSVRC-2014 competition. It has been obtained by directly converting the [Caffe model](https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-readme-md) provived by the authors. Details about the network architecture can be found in the following arXiv paper: Very Deep Convolutional Networks for Large-Scale Image Recognition K. Simonyan, A. Zisserman arXiv:1409.1556 In the paper, the VGG-16 model is denoted as configuration `D`. It achieves 7.5% top-5 error on ILSVRC-2012-val, 7.4% top-5 error on ILSVRC-2012-test. Please cite the paper if you use the models. ###Contents: model and usage demo: see `main.py` weights: [vgg16_weights.h5](https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmRCM0ROVHc/view?usp=sharing) 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ 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 cv2, numpy as np def VGG_16(weights_path=None): model = Sequential() model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(64, 3, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(64, 64, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), stride=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128, 64, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128, 128, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), stride=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256, 128, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256, 256, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256, 256, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), stride=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 256, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 512, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), stride=(2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512, 512, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), stride=(2,2))) model.add(Flatten()) model.add(Dense(512*7*7, 4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(4096, 4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(4096, 1000, activation='softmax')) if weights_path: model.load_weights(weights_path) return model if __name__ == "__main__": im = cv2.resize(cv2.imread('cat.jpg'), (224, 224)) im = im.transpose((2,0,1)) im = np.expand_dims(im, axis=0) # Test pretrained model model = VGG_16('vgg16_weights.h5') sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='categorical_crossentropy') out = model.predict(im)