Skip to content

Instantly share code, notes, and snippets.

@harveyslash
Created January 17, 2017 13:23
Show Gist options
  • Save harveyslash/33c4c1478436aab75bfe5adfba1fb7fd to your computer and use it in GitHub Desktop.
Save harveyslash/33c4c1478436aab75bfe5adfba1fb7fd to your computer and use it in GitHub Desktop.
vgg_mean = np.array([123.68, 116.779, 103.939], dtype=np.float32).reshape((3,1,1))
def addConvBlock(model, layers, filters):
for i in range(layers):
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(filters, 3, 3, activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
def addFCBlock(model):
model.add(Dense(4096, activation='relu'))
# model.add(BatchNormalization())
model.add(Dropout(0.5))
def create():
model = Sequential()
model.add(Lambda(vgg_preprocess, input_shape=(3,224,224)))
addConvBlock(model,2, 64)
addConvBlock(model,2, 128)
addConvBlock(model,3, 256)
addConvBlock(model,3, 512)
addConvBlock(model,3, 512)
model.add(Flatten())
addFCBlock(model)
addFCBlock(model)
model.add(Dense(1000, activation='softmax'))
model.load_weights("/home/harsh/.keras/models/vgg16.h5")
return model
def vgg_preprocess(x):
x = x - vgg_mean
return x[:, ::-1] # reverse axis rgb->bgr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment