Skip to content

Instantly share code, notes, and snippets.

@JBed
Last active April 7, 2022 19:29
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save JBed/c2fb3ce8ed299f197eff to your computer and use it in GitHub Desktop.
Save JBed/c2fb3ce8ed299f197eff to your computer and use it in GitHub Desktop.
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(BatchNormalization((64,226,226)))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(3, 3)))
model.add(Convolution2D(128, 64, 7, 7, border_mode='full'))
model.add(BatchNormalization((128,115,115)))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(3, 3)))
model.add(Convolution2D(192, 128, 3, 3, border_mode='full'))
model.add(BatchNormalization((128,112,112)))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(3, 3)))
model.add(Convolution2D(256, 192, 3, 3, border_mode='full'))
model.add(BatchNormalization((128,108,108)))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(3, 3)))
model.add(Flatten())
model.add(Dense(12*12*256, 4096, init='normal'))
model.add(BatchNormalization(4096))
model.add(Activation('relu'))
model.add(Dense(4096, 4096, init='normal'))
model.add(BatchNormalization(4096))
model.add(Activation('relu'))
model.add(Dense(4096, 1000, init='normal'))
model.add(BatchNormalization(1000))
model.add(Activation('softmax'))
@dongzhuoyao
Copy link

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

@jiapei100
Copy link

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???

@MandarKulkarni40
Copy link

Hi

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.

@OleksandrMalinin
Copy link

Hi,

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,
Alex

@JonathanCMitchell
Copy link

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.

@Spandan-Madan
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!

@billchenxi
Copy link

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

@samadak
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?

@elitezhe
Copy link

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))

@AdityaSoni19031997
Copy link

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

@webcsm
Copy link

webcsm commented Aug 30, 2017

You can find 2-stream AlexNet here: http://dandxy89.github.io/ImageModels/alexnet/

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