|
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_19(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(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(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(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) |
|
|
|
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_19('vgg19_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 Thank you for sharing this converted model files. I tested this model on imagenet data, but predicted labels do not make any sense, i.e. when I look up a predicted label index in the imagenet metadata file, the corresponding class description is definitely different from the image content.
Here are five cat images I used to test the above claim.
In imagenet, below are the classes I found related to 'cat'
Below are the top5 predictions (node_index, probability) for these two images:
For me, it seems that the output node indices do not directly correspond to the imagenet metadata labels, but if we can have the correct mapping then we know the right physical class of a given image ( see all these cat images have high response on output node 285, 281, 282). Do you have any idea where I can get the correct mapping file from the keras output node index to the imagenet class label?