model.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:object-detection-with-deep-learning 作者: neerajdixit 项目源码 文件源码
def get_model():
    """
        Defines the CNN model architecture and returns the model.
        The architecture is the same as I developed for project 2
        https://github.com/neerajdixit/Traffic-Sign-classifier-with-Deep-Learning
        with an additional normalization layer in front and
        a final fully connected layer of size 5 since we have 5 different type of objects in our data set.
    """

    # Create a Keras sequential model
    model = Sequential()
    #model.add(Cropping2D(cropping=((50,20), (0,0)), input_shape=(160,320,3)))
    # Add a normalization layer to normalize between -0.5 and 0.5.
    model.add(Lambda(lambda x: x / 255. - .5,input_shape=(im_x,im_y,im_z), name='norm'))
    # Add a convolution layer with Input = 32x32x3. Output = 30x30x6. Strides 1 and VALID padding.
    # Perform RELU activation 
    model.add(Convolution2D(6, 3, 3, subsample=(1, 1), border_mode="valid", activation='relu', name='conv1'))
    # Add a convolution layer with Input = 30x30x6. Output = 28x28x9. Strides 1 and VALID padding.
    # Perform RELU activation 
    model.add(Convolution2D(9, 3, 3, subsample=(1, 1), border_mode="valid", activation='relu', name='conv2'))
    # Add Pooling layer with Input = 28x28x9. Output = 14x14x9. 2x2 kernel, Strides 2 and VALID padding
    model.add(MaxPooling2D(pool_size=(2, 2), border_mode='valid', name='pool1'))
    # Add a convolution layer with Input 14x14x9. Output = 12x12x12. Strides 1 and VALID padding.
    # Perform RELU activation 
    model.add(Convolution2D(12, 3, 3, subsample=(1, 1), border_mode="valid", activation='relu', name='conv3'))
    # Add a convolution layer with Input = 30x30x6. Output = 28x28x9. Strides 1 and VALID padding.
    # Perform RELU activation 
    model.add(Convolution2D(16, 3, 3, subsample=(1, 1), border_mode="valid", activation='relu', name='conv4'))
    # Add Pooling layer with Input = 10x10x16. Output = 5x5x16. 2x2 kernel, Strides 2 and VALID padding
    model.add(MaxPooling2D(pool_size=(2, 2), border_mode='valid', name='pool2'))
    # Flatten. Input = 5x5x16. Output = 400.
    model.add(Flatten(name='flat1'))
    # Add dropout layer with 0.2  
    model.add(Dropout(0.2, name='dropout1'))
    # Add Fully Connected layer. Input = 400. Output = 220
    # Perform RELU activation 
    model.add(Dense(220, activation='relu', name='fc1'))
    # Add Fully Connected layer. Input = 220. Output = 43
    # Perform RELU activation 
    model.add(Dense(43, activation='relu', name='fc2'))
    # Add Fully Connected layer. Input = 43. Output = 5
    # Perform RELU activation 
    model.add(Dense(5, name='fc3'))
    # Configure the model for training with Adam optimizer
    # "mean squared error" loss objective and accuracy metrics
    # Learning rate of 0.001 was chosen because this gave best performance after testing other values
    model.compile(optimizer=Adam(lr=0.001), loss="mse", metrics=['accuracy'])
    return model
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号