简体   繁体   中英

How would I go about changing the input_shape for Alex net (with tf-keras)?

Here is the code:

def AlexNet(log_path, save_path, input_shape):
# code source => 'engmrk.com/alexnet-implementation-using-keras'
# model definition
model = models.Sequential()

# 1st Convolutional Layer
model.add(layers.Conv2D(filters=96, input_shape=(INPUT_SHAPE), kernel_size=(11,11), strides=(4,4), padding='valid'))
model.add(layers.Activation('relu'))
# Max Pooling
model.add(layers.MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))

# 2nd Convolutional Layer
model.add(layers.Conv2D(filters=256, kernel_size=(11,11), strides=(1,1), padding='valid'))
model.add(layers.Activation('relu'))
# Max Pooling
model.add(layers.MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))

# 3rd Convolutional Layer
model.add(layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='valid'))
model.add(layers.Activation('relu'))

# 4th Convolutional Layer
model.add(layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='valid'))
model.add(layers.Activation('relu'))

# 5th Convolutional Layer
model.add(layers.Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), padding='valid'))
model.add(layers.Activation('relu'))
# Max Pooling
model.add(layers.MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))

# Passing it to a Fully Connected layer
model.add(layers.Flatten())
# 1st Fully Connected Layer
model.add(layers.Dense(4096, input_shape=(input_shape[0] * input_shape[1] * input_shape[2],))
model.add(layers.Activation('relu'))
# Add Dropout to prevent overfitting
model.add(layers.Dropout(0.4))

# 2nd Fully Connected Layer
model.add(layers.Dense(4096))
model.add(layers.Activation('relu'))
# Add Dropout
model.add(layers.Dropout(0.4))

# 3rd Fully Connected Layer
model.add(layers.Dense(1000))
model.add(layers.Activation('relu'))
# Add Dropout
model.add(layers.Dropout(0.4))

# Output Layer
model.add(layers.Dense(17))
model.add(layers.Activation('softmax'))

model.summary()

When I tried, just changing the input shape to (91, 74, 3), I got this error:

ValueError: Negative dimension size caused by subtracting 11 from 10 for '{{node conv2d_1/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](max_pooling2d/Identity, conv2d_1/Conv2D/ReadVariableOp)' with input shapes: [?,19,10,96], [11,11,96,256].

Now how would I go about changing, for example, the strides and or kernel_size if I wanted to do AlexNet with a dataset of RGB (91 * 74) images which have the input shape --> (91, 74, 3).

Is this even possible, if so how?

Thank you....

Convolutional layers and average pooling or max pooling layers reduce the size of the image w, h as you go deeper into the network, if you start with a small image it might end up having negative dimensions which doesn't make any sense. if you want to start with small images try removing some of the max_pool layers or use padding="same" on the conv layers or just reduce the depth of the network's architecture.

example: after the first layer the shape is gonna be (21, 16, 96) .
How to calculate the output: for valid padding it is ((W - F) / S) +1 for same padding the output shape is the same as the input shape except the number of channels

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM