|
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) |
Hello @baraldilorenzo
I tried to run this model with the weight file on only 10 images. but it failed showing the following error message.
I would highly appreciate if you please advise.
Traceback (most recent call last):
File "imagenet_cnn_call.py", line 59, in
vgg_19_model.compile(optimizer=sgd, loss='categorical_crossentropy')
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 408, in compile
self.y_train = self.get_output(train=True)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/containers.py", line 128, in get_output
return self.layers[-1].get_output(train)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 949, in get_output
X = self.get_input(train)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 159, in get_input
previous_output = self.previous.get_output(train=train)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 624, in get_output
X = self.get_input(train)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 159, in get_input
previous_output = self.previous.get_output(train=train)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 949, in get_output
X = self.get_input(train)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 159, in get_input
previous_output = self.previous.get_output(train=train)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 624, in get_output
X = self.get_input(train)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 159, in get_input
previous_output = self.previous.get_output(train=train)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 949, in get_output
X = self.get_input(train)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 159, in get_input
previous_output = self.previous.get_output(train=train)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 822, in get_output
return K.flatten(X)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 272, in flatten
x = tf.reshape(x, [-1, np.prod(x.get_shape()[1:].as_list())])
File "/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 2481, in prod
out=out, keepdims=keepdims)
File "/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py", line 35, in _prod
return umr_prod(a, axis, dtype, out, keepdims)
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'