最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

使用keras导入densenet模型

IT圈 admin 29浏览 0评论

2024年3月22日发(作者:巨朝雨)

def transition_block(x, reduction, name):

"""A transition block.

# Arguments

x: input tensor.

reduction: float, compression rate at transition layers.

name: string, block label.

# Returns

output tensor for the block.

"""

bn_axis = 3 if _data_format() == 'channels_last' else 1

x = ormalization(axis=bn_axis, epsilon=1.001e-5,

name=name + '_bn')(x)

x = tion('relu', name=name + '_relu')(x)

x = 2D(int(_shape(x)[bn_axis] * reduction), 1,

use_bias=False,

name=name + '_conv')(x)

x = ePooling2D(2, strides=2, name=name + '_pool')(x)

return x

def conv_block(x, growth_rate, name):

"""A building block for a dense block.

# Arguments

x: input tensor.

growth_rate: float, growth rate at dense layers.

name: string, block label.

# Returns

Output tensor for the block.

"""

bn_axis = 3 if _data_format() == 'channels_last' else 1

x1 = ormalization(axis=bn_axis,

epsilon=1.001e-5,

name=name + '_0_bn')(x)

x1 = tion('relu', name=name + '_0_relu')(x1)

x1 = 2D(4 * growth_rate, 1,

use_bias=False,

name=name + '_1_conv')(x1)

x1 = ormalization(axis=bn_axis, epsilon=1.001e-5,

name=name + '_1_bn')(x1)

x1 = tion('relu', name=name + '_1_relu')(x1)

x1 = 2D(growth_rate, 3,

padding='same',

use_bias=False,

name=name + '_2_conv')(x1)

x = enate(axis=bn_axis, name=name + '_concat')([x, x1])

return x

def DenseNet(blocks,

include_top=True,

weights='imagenet',

input_tensor=None,

input_shape=None,

pooling=None,

classes=1000):

"""Instantiates the DenseNet architecture.

Optionally loads weights pre-trained on ImageNet.

Note that the data format convention used by the model is

the one specified in your Keras config at `~/.keras/`.

# Arguments

blocks: numbers of building blocks for the four dense layers.

include_top: whether to include the fully-connected

layer at the top of the network.

weights: one of `None` (random initialization),

'imagenet' (pre-training on ImageNet),

or the path to the weights file to be loaded.

input_tensor: optional Keras tensor

(i.e. output of `()`)

to use as image input for the model.

input_shape: optional shape tuple, only to be specified

if `include_top` is False (otherwise the input shape

has to be `(224, 224, 3)` (with `channels_last` data format)

or `(3, 224, 224)` (with `channels_first` data format).

It should have exactly 3 inputs channels.

pooling: optional pooling mode for feature extraction

when `include_top` is `False`.

- `None` means that the output of the model will be

the 4D tensor output of the

last convolutional layer.

- `avg` means that global average pooling

will be applied to the output of the

last convolutional layer, and thus

the output of the model will be a 2D tensor.

- `max` means that global max pooling will

be applied.

classes: optional number of classes to classify images

into, only to be specified if `include_top` is True, and

if no `weights` argument is specified.

# Returns

A Keras model instance.

# Raises

ValueError: in case of invalid argument for `weights`,

or invalid input shape.

"""

if not (weights in {'imagenet', None} or (weights)):

raise ValueError('The `weights` argument should be either '

'`None` (random initialization), `imagenet` '

'(pre-training on ImageNet), '

'or the path to the weights file to be loaded.')

if weights == 'imagenet' and include_top and classes != 1000:

raise ValueError('If using `weights` as imagenet with `include_top`'

' as true, `classes` should be 1000')

# Determine proper input shape

input_shape = _obtain_input_shape(input_shape,

default_size=224,

min_size=221,

data_format=_data_format(),

require_flatten=include_top,

weights=weights)

if input_tensor is None:

img_input = (shape=input_shape)

else:

if not _keras_tensor(input_tensor):

img_input = (tensor=input_tensor, shape=input_shape)

else:

img_input = input_tensor

bn_axis = 3 if _data_format() == 'channels_last' else 1

x = dding2D(padding=((3, 3), (3, 3)))(img_input)

x = 2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)

x = ormalization(

axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x)

x = tion('relu', name='conv1/relu')(x)

x = dding2D(padding=((1, 1), (1, 1)))(x)

x = ling2D(3, strides=2, name='pool1')(x)

x = dense_block(x, blocks[0], name='conv2')

x = transition_block(x, 0.5, name='pool2')

x = dense_block(x, blocks[1], name='conv3')

x = transition_block(x, 0.5, name='pool3')

x = dense_block(x, blocks[2], name='conv4')

x = transition_block(x, 0.5, name='pool4')

x = dense_block(x, blocks[3], name='conv5')

x = ormalization(

axis=bn_axis, epsilon=1.001e-5, name='bn')(x)

if include_top:

x = AveragePooling2D(name='avg_pool')(x)

x = (classes, activation='softmax', name='fc1000')(x)

else:

if pooling == 'avg':

x = AveragePooling2D(name='avg_pool')(x)

elif pooling == 'max':

x = MaxPooling2D(name='max_pool')(x)

# Ensure that the model takes into account

# any potential predecessors of `input_tensor`.

if input_tensor is not None:

inputs = _source_inputs(input_tensor)

else:

inputs = img_input

# Create model.

if blocks == [6, 12, 24, 16]:

model = (inputs, x, name='densenet121')

elif blocks == [6, 12, 32, 32]:

model = (inputs, x, name='densenet169')

elif blocks == [6, 12, 48, 32]:

model = (inputs, x, name='densenet201')

else:

model = (inputs, x, name='densenet')

# Load weights.

if weights == 'imagenet':

if include_top:

if blocks == [6, 12, 24, 16]:

weights_path = keras__file(

'densenet121_weights_tf_dim_ordering_tf_kernels.h5',

DENSENET121_WEIGHT_PATH,

cache_subdir='models',

file_hash='0962ca643bae20f9b6771cb844dca3b0')

elif blocks == [6, 12, 32, 32]:

weights_path = keras__file(

'densenet169_weights_tf_dim_ordering_tf_kernels.h5',

DENSENET169_WEIGHT_PATH,

cache_subdir='models',

file_hash='bcf9965cf5064a5f9eb6d7dc69386f43')

elif blocks == [6, 12, 48, 32]:

weights_path = keras__file(

'densenet201_weights_tf_dim_ordering_tf_kernels.h5',

DENSENET201_WEIGHT_PATH,

cache_subdir='models',

file_hash='7bb75edd58cb43163be7e0005fbe95ef')

else:

if blocks == [6, 12, 24, 16]:

weights_path = keras__file(

'densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5',

DENSENET121_WEIGHT_PATH_NO_TOP,

cache_subdir='models',

file_hash='4912a53fbd2a69346e7f2c0b5ec8c6d3')

elif blocks == [6, 12, 32, 32]:

weights_path = keras__file(

'densenet169_weights_tf_dim_ordering_tf_kernels_notop.h5',

DENSENET169_WEIGHT_PATH_NO_TOP,

cache_subdir='models',

file_hash='5e4cf834ce40ab4dfa58c6')

elif blocks == [6, 12, 48, 32]:

weights_path = keras__file(

'densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5',

DENSENET201_WEIGHT_PATH_NO_TOP,

cache_subdir='models',

file_hash='1c2de60ee40562448dbac34a0737e798')

_weights(weights_path)

elif weights is not None:

_weights(weights)

return model

def DenseNet121(include_top=True,

weights='imagenet',

input_tensor=None,

input_shape=None,

pooling=None,

classes=1000):

return DenseNet([6, 12, 24, 16],

include_top, weights,

input_tensor, input_shape,

pooling, classes)

def DenseNet169(include_top=True,

weights='imagenet',

input_tensor=None,

input_shape=None,

pooling=None,

classes=1000):

return DenseNet([6, 12, 32, 32],

include_top, weights,

input_tensor, input_shape,

pooling, classes)

def DenseNet201(include_top=True,

weights='imagenet',

input_tensor=None,

input_shape=None,

pooling=None,

2024年3月22日发(作者:巨朝雨)

def transition_block(x, reduction, name):

"""A transition block.

# Arguments

x: input tensor.

reduction: float, compression rate at transition layers.

name: string, block label.

# Returns

output tensor for the block.

"""

bn_axis = 3 if _data_format() == 'channels_last' else 1

x = ormalization(axis=bn_axis, epsilon=1.001e-5,

name=name + '_bn')(x)

x = tion('relu', name=name + '_relu')(x)

x = 2D(int(_shape(x)[bn_axis] * reduction), 1,

use_bias=False,

name=name + '_conv')(x)

x = ePooling2D(2, strides=2, name=name + '_pool')(x)

return x

def conv_block(x, growth_rate, name):

"""A building block for a dense block.

# Arguments

x: input tensor.

growth_rate: float, growth rate at dense layers.

name: string, block label.

# Returns

Output tensor for the block.

"""

bn_axis = 3 if _data_format() == 'channels_last' else 1

x1 = ormalization(axis=bn_axis,

epsilon=1.001e-5,

name=name + '_0_bn')(x)

x1 = tion('relu', name=name + '_0_relu')(x1)

x1 = 2D(4 * growth_rate, 1,

use_bias=False,

name=name + '_1_conv')(x1)

x1 = ormalization(axis=bn_axis, epsilon=1.001e-5,

name=name + '_1_bn')(x1)

x1 = tion('relu', name=name + '_1_relu')(x1)

x1 = 2D(growth_rate, 3,

padding='same',

use_bias=False,

name=name + '_2_conv')(x1)

x = enate(axis=bn_axis, name=name + '_concat')([x, x1])

return x

def DenseNet(blocks,

include_top=True,

weights='imagenet',

input_tensor=None,

input_shape=None,

pooling=None,

classes=1000):

"""Instantiates the DenseNet architecture.

Optionally loads weights pre-trained on ImageNet.

Note that the data format convention used by the model is

the one specified in your Keras config at `~/.keras/`.

# Arguments

blocks: numbers of building blocks for the four dense layers.

include_top: whether to include the fully-connected

layer at the top of the network.

weights: one of `None` (random initialization),

'imagenet' (pre-training on ImageNet),

or the path to the weights file to be loaded.

input_tensor: optional Keras tensor

(i.e. output of `()`)

to use as image input for the model.

input_shape: optional shape tuple, only to be specified

if `include_top` is False (otherwise the input shape

has to be `(224, 224, 3)` (with `channels_last` data format)

or `(3, 224, 224)` (with `channels_first` data format).

It should have exactly 3 inputs channels.

pooling: optional pooling mode for feature extraction

when `include_top` is `False`.

- `None` means that the output of the model will be

the 4D tensor output of the

last convolutional layer.

- `avg` means that global average pooling

will be applied to the output of the

last convolutional layer, and thus

the output of the model will be a 2D tensor.

- `max` means that global max pooling will

be applied.

classes: optional number of classes to classify images

into, only to be specified if `include_top` is True, and

if no `weights` argument is specified.

# Returns

A Keras model instance.

# Raises

ValueError: in case of invalid argument for `weights`,

or invalid input shape.

"""

if not (weights in {'imagenet', None} or (weights)):

raise ValueError('The `weights` argument should be either '

'`None` (random initialization), `imagenet` '

'(pre-training on ImageNet), '

'or the path to the weights file to be loaded.')

if weights == 'imagenet' and include_top and classes != 1000:

raise ValueError('If using `weights` as imagenet with `include_top`'

' as true, `classes` should be 1000')

# Determine proper input shape

input_shape = _obtain_input_shape(input_shape,

default_size=224,

min_size=221,

data_format=_data_format(),

require_flatten=include_top,

weights=weights)

if input_tensor is None:

img_input = (shape=input_shape)

else:

if not _keras_tensor(input_tensor):

img_input = (tensor=input_tensor, shape=input_shape)

else:

img_input = input_tensor

bn_axis = 3 if _data_format() == 'channels_last' else 1

x = dding2D(padding=((3, 3), (3, 3)))(img_input)

x = 2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)

x = ormalization(

axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x)

x = tion('relu', name='conv1/relu')(x)

x = dding2D(padding=((1, 1), (1, 1)))(x)

x = ling2D(3, strides=2, name='pool1')(x)

x = dense_block(x, blocks[0], name='conv2')

x = transition_block(x, 0.5, name='pool2')

x = dense_block(x, blocks[1], name='conv3')

x = transition_block(x, 0.5, name='pool3')

x = dense_block(x, blocks[2], name='conv4')

x = transition_block(x, 0.5, name='pool4')

x = dense_block(x, blocks[3], name='conv5')

x = ormalization(

axis=bn_axis, epsilon=1.001e-5, name='bn')(x)

if include_top:

x = AveragePooling2D(name='avg_pool')(x)

x = (classes, activation='softmax', name='fc1000')(x)

else:

if pooling == 'avg':

x = AveragePooling2D(name='avg_pool')(x)

elif pooling == 'max':

x = MaxPooling2D(name='max_pool')(x)

# Ensure that the model takes into account

# any potential predecessors of `input_tensor`.

if input_tensor is not None:

inputs = _source_inputs(input_tensor)

else:

inputs = img_input

# Create model.

if blocks == [6, 12, 24, 16]:

model = (inputs, x, name='densenet121')

elif blocks == [6, 12, 32, 32]:

model = (inputs, x, name='densenet169')

elif blocks == [6, 12, 48, 32]:

model = (inputs, x, name='densenet201')

else:

model = (inputs, x, name='densenet')

# Load weights.

if weights == 'imagenet':

if include_top:

if blocks == [6, 12, 24, 16]:

weights_path = keras__file(

'densenet121_weights_tf_dim_ordering_tf_kernels.h5',

DENSENET121_WEIGHT_PATH,

cache_subdir='models',

file_hash='0962ca643bae20f9b6771cb844dca3b0')

elif blocks == [6, 12, 32, 32]:

weights_path = keras__file(

'densenet169_weights_tf_dim_ordering_tf_kernels.h5',

DENSENET169_WEIGHT_PATH,

cache_subdir='models',

file_hash='bcf9965cf5064a5f9eb6d7dc69386f43')

elif blocks == [6, 12, 48, 32]:

weights_path = keras__file(

'densenet201_weights_tf_dim_ordering_tf_kernels.h5',

DENSENET201_WEIGHT_PATH,

cache_subdir='models',

file_hash='7bb75edd58cb43163be7e0005fbe95ef')

else:

if blocks == [6, 12, 24, 16]:

weights_path = keras__file(

'densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5',

DENSENET121_WEIGHT_PATH_NO_TOP,

cache_subdir='models',

file_hash='4912a53fbd2a69346e7f2c0b5ec8c6d3')

elif blocks == [6, 12, 32, 32]:

weights_path = keras__file(

'densenet169_weights_tf_dim_ordering_tf_kernels_notop.h5',

DENSENET169_WEIGHT_PATH_NO_TOP,

cache_subdir='models',

file_hash='5e4cf834ce40ab4dfa58c6')

elif blocks == [6, 12, 48, 32]:

weights_path = keras__file(

'densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5',

DENSENET201_WEIGHT_PATH_NO_TOP,

cache_subdir='models',

file_hash='1c2de60ee40562448dbac34a0737e798')

_weights(weights_path)

elif weights is not None:

_weights(weights)

return model

def DenseNet121(include_top=True,

weights='imagenet',

input_tensor=None,

input_shape=None,

pooling=None,

classes=1000):

return DenseNet([6, 12, 24, 16],

include_top, weights,

input_tensor, input_shape,

pooling, classes)

def DenseNet169(include_top=True,

weights='imagenet',

input_tensor=None,

input_shape=None,

pooling=None,

classes=1000):

return DenseNet([6, 12, 32, 32],

include_top, weights,

input_tensor, input_shape,

pooling, classes)

def DenseNet201(include_top=True,

weights='imagenet',

input_tensor=None,

input_shape=None,

pooling=None,

发布评论

评论列表 (0)

  1. 暂无评论