Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
#AlexNet with batch normalization in Keras
#input image is 224x224
model = Sequential()
model.add(Convolution2D(64, 3, 11, 11, border_mode='full'))
model.add(MaxPooling2D(poolsize=(3, 3)))
model.add(Convolution2D(128, 64, 7, 7, border_mode='full'))
model.add(MaxPooling2D(poolsize=(3, 3)))
model.add(Convolution2D(192, 128, 3, 3, border_mode='full'))
model.add(MaxPooling2D(poolsize=(3, 3)))
model.add(Convolution2D(256, 192, 3, 3, border_mode='full'))
model.add(MaxPooling2D(poolsize=(3, 3)))
model.add(Dense(12*12*256, 4096, init='normal'))
model.add(Dense(4096, 4096, init='normal'))
model.add(Dense(4096, 1000, init='normal'))
Copy link

dongzhuoyao commented Jun 4, 2016

why your Convolution2D have four customized parameters? currently version's keras only need 3 params..

Copy link

jiapei100 commented Jul 27, 2016

Dense(4096, 4096, init='normal')
What does this line mean?
Surely it's better to clarify which is output_dim, and which is input_dim ....
As for the 4096, which is which???

Copy link

MandarKulkarni40 commented Aug 3, 2016


I have downloaded alexnet_weights.h5 file and defined the architecture as mentioned above. However, i get an error as follows

'You are trying to load a weight file containing 34 layers into a model with 19 layers.'

Is there any other weight file available for alexnet?
Please help me to resolve it.

Copy link

OleksandrMalinin commented Oct 20, 2016


Thank you for sharing this. Today it includes errors:

  1. After copy-paste: Exception: ('Invalid border mode for Convolution2D:', 'full').
  2. After changing 'full' to valid 'same' I get Exception: The first layer in a Sequential model must get an input_shape or batch_input_shape argument.
  3. After adding input shapes, TypeError: 'int' object is not callable for Convolution2D objects as they shouldn't take so many integers in arguments.

Could you please update code, so it will run with up-to-date versions of software.
Thank you!

Best regards,

Copy link

JonathanCMitchell commented Mar 15, 2017

I believe alexnet has two streams, and cannot be implemented with a sequential model and it must be implemented with the functional api. I only see one stream here.

Copy link

Spandan-Madan commented Mar 26, 2017

@JonathanCMitchell - Possible because there are two variants of alexnet. The original one has two streams, but the caffenet version is a single stream. I think this is the caffenet version!

Copy link

billchenxi commented Apr 10, 2017

Is there some example codes for using this?
like input = mired()...

Copy link

samadak commented Apr 21, 2017

Hi guys
I want to use alexnet for feature extraction.I was wondering if could tell me how to feed my as image into alexnet?

Copy link

elitezhe commented May 10, 2017

alexnet uses overlapping pooling, the first conv layer's pooling should use (3, 3) kernel with stride 2 according to the original paper.

model.add(MaxPooling2D(poolsize=(3, 3), strides=2))

Copy link

AdityaSoni19031997 commented Aug 29, 2017

why your Convolution2D have four customized parameters? currently version's keras only need 3 params..??

Copy link

webcsm commented Aug 30, 2017

You can find 2-stream AlexNet here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment