资源简介

官方例子,深度学习专用,机器学习专用,代码简单,一看就会(keras inception v4 finetune)

资源截图

代码片段和文件信息

# -*- coding: utf-8 -*-

from keras.models import Sequential
from keras.optimizers import SGD
from keras.layers import Input Dense Convolution2D MaxPooling2D AveragePooling2D ZeroPadding2D Dropout Flatten merge Reshape Activation
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras import backend as K

from sklearn.metrics import log_loss

from load_cifar10 import load_cifar10_data

def conv2d_bn(x nb_filter nb_row nb_col
              border_mode=‘same‘ subsample=(1 1) bias=False):
    “““
    Utility function to apply conv + BN. 
    (Slightly modified from https://github.com/fchollet/keras/blob/master/keras/applications/inception_v3.py)
    “““
    if K.image_dim_ordering() == “th“:
        channel_axis = 1
    else:
        channel_axis = -1
    x = Convolution2D(nb_filter nb_row nb_col
                      subsample=subsample
                      border_mode=border_mode
                      bias=bias)(x)
    x = BatchNormalization(axis=channel_axis)(x)
    x = Activation(‘relu‘)(x)
    return x

def block_inception_a(input):
    if K.image_dim_ordering() == “th“:
        channel_axis = 1
    else:
        channel_axis = -1

    branch_0 = conv2d_bn(input 96 1 1)

    branch_1 = conv2d_bn(input 64 1 1)
    branch_1 = conv2d_bn(branch_1 96 3 3)

    branch_2 = conv2d_bn(input 64 1 1)
    branch_2 = conv2d_bn(branch_2 96 3 3)
    branch_2 = conv2d_bn(branch_2 96 3 3)

    branch_3 = AveragePooling2D((33) strides=(11) border_mode=‘same‘)(input)
    branch_3 = conv2d_bn(branch_3 96 1 1)

    x = merge([branch_0 branch_1 branch_2 branch_3] mode=‘concat‘ concat_axis=channel_axis)
    return x


def block_reduction_a(input):
    if K.image_dim_ordering() == “th“:
        channel_axis = 1
    else:
        channel_axis = -1

    branch_0 = conv2d_bn(input 384 3 3 subsample=(22) border_mode=‘valid‘)

    branch_1 = conv2d_bn(input 192 1 1)
    branch_1 = conv2d_bn(branch_1 224 3 3)
    branch_1 = conv2d_bn(branch_1 256 3 3 subsample=(22) border_mode=‘valid‘)

    branch_2 = MaxPooling2D((33) strides=(22) border_mode=‘valid‘)(input)

    x = merge([branch_0 branch_1 branch_2] mode=‘concat‘ concat_axis=channel_axis)
    return x


def block_inception_b(input):
    if K.image_dim_ordering() == “th“:
        channel_axis = 1
    else:
        channel_axis = -1

    branch_0 = conv2d_bn(input 384 1 1)

    branch_1 = conv2d_bn(input 192 1 1)
    branch_1 = conv2d_bn(branch_1 224 1 7)
    branch_1 = conv2d_bn(branch_1 256 7 1)

    branch_2 = conv2d_bn(input 192 1 1)
    branch_2 = conv2d_bn(branch_2 192 7 1)
    branch_2 = conv2d_bn(branch_2 224 1 7)
    branch_2 = conv2d_bn(branch_2 224 7 1)
    branch_2 = conv2d_bn(branch_2 256 1 7)

    branch_3 = AveragePooling2D((33) strides=(11) border_mode=‘same‘)(input)
    branch_3 = conv2d_bn(branch_3 128 1 1)

    x = merge([branch_0 branch_1 branch_2

评论

共有 条评论