“TypeError: unhashable type: 'Dimension'”

我有以下从 github 获得的 ResNet 3D 架构。它是 R3D 的 Keras 实现。该架构旨在训练视频分类模型

当我在视频上训练网络时,出现以下错误:


Error: unhashable type: 'Dimension'


  File "<ipython-input-29-788d091a6763>", line 1961, in main

    trained_model_name)

  File "<ipython-input-29-788d091a6763>", line 1805, in train

    model = train_load_model(model_type, training_condition, sample_input.shape, nb_classes)

  File "<ipython-input-29-788d091a6763>", line 1684, in train_load_model

    model = Resnet3DBuilder.build_resnet_50((96, 96, 96, 1), 20)

  File "<ipython-input-29-788d091a6763>", line 1543, in build_resnet_50

    [3, 4, 6, 3], reg_factor=reg_factor)

  File "<ipython-input-29-788d091a6763>", line 1501, in build

    )(block)

  File "<ipython-input-29-788d091a6763>", line 1372, in f

    )(input)

  File "<ipython-input-29-788d091a6763>", line 1419, in f

    )(input)

  File "<ipython-input-29-788d091a6763>", line 1334, in f

    activation = _bn_relu(input)

  File "<ipython-input-29-788d091a6763>", line 1300, in _bn_relu

    norm = BatchNormalization(axis=CHANNEL_AXIS)(input)

  File "/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py", line 440, in __call__

    self.assert_input_compatibility(inputs)

  File "/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py", line 345, in assert_input_compatibility

    x_shape[int(axis)] not in {value, None}):

我有以下 Tensorflow、Keras 和 Python 版本:

  • 张量流:1.15.0

  • 困难:2.2.4

  • 蟒蛇:3.6

你能告诉我如何解决这个错误吗?我看到将 Dimension 转换为 int 解决了其他地方的问题,但我不知道应该在这里转换什么。


饮歌长啸
浏览 137回答 1
1回答

慕运维8079593

为了解决这个问题,我们需要将每个形状的访问都转换为 int。示例: residual.shape[CHANNEL_AXIS]需要重写int(residual.shape[CHANNEL_AXIS])新版本代码如下:## Resnet 3D architecture# Taken from https://github.com/JihongJu/keras-resnet3d/blob/master/resnet3d/resnet3d.pydef _bn_relu(input):&nbsp; &nbsp; """Helper to build a BN -> relu block (by @raghakot)."""&nbsp; &nbsp; norm = BatchNormalization(axis=CHANNEL_AXIS)(input)&nbsp; &nbsp; return Activation("relu")(norm)def _conv_bn_relu3D(**conv_params):&nbsp; &nbsp; filters = conv_params["filters"]&nbsp; &nbsp; kernel_size = conv_params["kernel_size"]&nbsp; &nbsp; strides = conv_params.setdefault("strides", (1, 1, 1))&nbsp; &nbsp; kernel_initializer = conv_params.setdefault(&nbsp; &nbsp; &nbsp; &nbsp; "kernel_initializer", "he_normal")&nbsp; &nbsp; padding = conv_params.setdefault("padding", "same")&nbsp; &nbsp; kernel_regularizer = conv_params.setdefault("kernel_regularizer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l2(1e-4))&nbsp; &nbsp; def f(input):&nbsp; &nbsp; &nbsp; &nbsp; conv = Conv3D(filters=filters, kernel_size=kernel_size,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strides=strides, kernel_initializer=kernel_initializer,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding=padding,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_regularizer=kernel_regularizer)(input)&nbsp; &nbsp; &nbsp; &nbsp; return _bn_relu(conv)&nbsp; &nbsp; return fdef _bn_relu_conv3d(**conv_params):&nbsp; &nbsp; """Helper to build a&nbsp; BN -> relu -> conv3d block."""&nbsp; &nbsp; filters = conv_params["filters"]&nbsp; &nbsp; kernel_size = conv_params["kernel_size"]&nbsp; &nbsp; strides = conv_params.setdefault("strides", (1, 1, 1))&nbsp; &nbsp; kernel_initializer = conv_params.setdefault("kernel_initializer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "he_normal")&nbsp; &nbsp; padding = conv_params.setdefault("padding", "same")&nbsp; &nbsp; kernel_regularizer = conv_params.setdefault("kernel_regularizer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l2(1e-4))&nbsp; &nbsp; def f(input):&nbsp; &nbsp; &nbsp; &nbsp; activation = _bn_relu(input)&nbsp; &nbsp; &nbsp; &nbsp; return Conv3D(filters=filters, kernel_size=kernel_size,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strides=strides, kernel_initializer=kernel_initializer,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding=padding,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_regularizer=kernel_regularizer)(activation)&nbsp; &nbsp; return fdef _shortcut3d(input, residual):&nbsp; &nbsp; """3D shortcut to match input and residual and merges them with "sum"."""&nbsp; &nbsp; stride_dim1 = math.ceil(int(input.shape[DIM1_AXIS]) \&nbsp; &nbsp; &nbsp; &nbsp; / int(residual.shape[DIM1_AXIS]))&nbsp; &nbsp; stride_dim2 = math.ceil(int(input.shape[DIM2_AXIS]) \&nbsp; &nbsp; &nbsp; &nbsp; / int(residual.shape[DIM2_AXIS]))&nbsp; &nbsp; stride_dim3 = math.ceil(int(input.shape[DIM3_AXIS]) \&nbsp; &nbsp; &nbsp; &nbsp; / int(residual.shape[DIM3_AXIS]))&nbsp; &nbsp; equal_channels = int(residual.shape[CHANNEL_AXIS]) \&nbsp; &nbsp; &nbsp; &nbsp; == int(input.shape[CHANNEL_AXIS])&nbsp; &nbsp; shortcut = input&nbsp; &nbsp; if stride_dim1 > 1 or stride_dim2 > 1 or stride_dim3 > 1 \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; or not equal_channels:&nbsp; &nbsp; &nbsp; &nbsp; shortcut = Conv3D(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filters=int(residual.shape[CHANNEL_AXIS]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_size=(1, 1, 1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strides=(stride_dim1, stride_dim2, stride_dim3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_initializer="he_normal", padding="valid",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_regularizer=l2(1e-4)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )(input)&nbsp; &nbsp; return add([shortcut, residual])def _residual_block3d(block_function, filters, kernel_regularizer, repetitions,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is_first_layer=False):&nbsp; &nbsp; def f(input):&nbsp; &nbsp; &nbsp; &nbsp; for i in range(repetitions):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strides = (1, 1, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i == 0 and not is_first_layer:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strides = (2, 2, 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input = block_function(filters=filters, strides=strides,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kernel_regularizer=kernel_regularizer,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;is_first_block_of_first_layer=(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;is_first_layer and i == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)(input)&nbsp; &nbsp; &nbsp; &nbsp; return input&nbsp; &nbsp; return fdef basic_block(filters, strides=(1, 1, 1), kernel_regularizer=l2(1e-4),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is_first_block_of_first_layer=False):&nbsp; &nbsp; """Basic 3 X 3 X 3 convolution blocks. Extended from raghakot's 2D impl."""&nbsp; &nbsp; def f(input):&nbsp; &nbsp; &nbsp; &nbsp; if is_first_block_of_first_layer:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # don't repeat bn->relu since we just did bn->relu->maxpool&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conv1 = Conv3D(filters=filters, kernel_size=(3, 3, 3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;strides=strides, padding="same",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kernel_initializer="he_normal",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kernel_regularizer=kernel_regularizer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)(input)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conv1 = _bn_relu_conv3d(filters=filters,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_size=(3, 3, 3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strides=strides,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_regularizer=kernel_regularizer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )(input)&nbsp; &nbsp; &nbsp; &nbsp; residual = _bn_relu_conv3d(filters=filters, kernel_size=(3, 3, 3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kernel_regularizer=kernel_regularizer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)(conv1)&nbsp; &nbsp; &nbsp; &nbsp; return _shortcut3d(input, residual)&nbsp; &nbsp; return fdef bottleneck(filters, strides=(1, 1, 1), kernel_regularizer=l2(1e-4),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;is_first_block_of_first_layer=False):&nbsp; &nbsp; """Basic 3 X 3 X 3 convolution blocks. Extended from raghakot's 2D impl."""&nbsp; &nbsp; def f(input):&nbsp; &nbsp; &nbsp; &nbsp; if is_first_block_of_first_layer:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # don't repeat bn->relu since we just did bn->relu->maxpool&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conv_1_1 = Conv3D(filters=filters, kernel_size=(1, 1, 1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strides=strides, padding="same",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_initializer="he_normal",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_regularizer=kernel_regularizer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )(input)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conv_1_1 = _bn_relu_conv3d(filters=filters, kernel_size=(1, 1, 1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;strides=strides,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kernel_regularizer=kernel_regularizer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)(input)&nbsp; &nbsp; &nbsp; &nbsp; conv_3_3 = _bn_relu_conv3d(filters=filters, kernel_size=(3, 3, 3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kernel_regularizer=kernel_regularizer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)(conv_1_1)&nbsp; &nbsp; &nbsp; &nbsp; residual = _bn_relu_conv3d(filters=filters * 4, kernel_size=(1, 1, 1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kernel_regularizer=kernel_regularizer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)(conv_3_3)&nbsp; &nbsp; &nbsp; &nbsp; return _shortcut3d(input, residual)&nbsp; &nbsp; return fdef _handle_data_format():&nbsp; &nbsp; global DIM1_AXIS&nbsp; &nbsp; global DIM2_AXIS&nbsp; &nbsp; global DIM3_AXIS&nbsp; &nbsp; global CHANNEL_AXIS&nbsp; &nbsp; if K.image_data_format() == 'channels_last':&nbsp; &nbsp; &nbsp; &nbsp; print("here CHANNELS last")&nbsp; &nbsp; &nbsp; &nbsp; DIM1_AXIS = 1&nbsp; &nbsp; &nbsp; &nbsp; DIM2_AXIS = 2&nbsp; &nbsp; &nbsp; &nbsp; DIM3_AXIS = 3&nbsp; &nbsp; &nbsp; &nbsp; CHANNEL_AXIS = 4&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; CHANNEL_AXIS = 1&nbsp; &nbsp; &nbsp; &nbsp; DIM1_AXIS = 2&nbsp; &nbsp; &nbsp; &nbsp; DIM2_AXIS = 3&nbsp; &nbsp; &nbsp; &nbsp; DIM3_AXIS = 4def _get_block(identifier):&nbsp; &nbsp; if isinstance(identifier, six.string_types):&nbsp; &nbsp; &nbsp; &nbsp; res = globals().get(identifier)&nbsp; &nbsp; &nbsp; &nbsp; if not res:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise ValueError('Invalid {}'.format(identifier))&nbsp; &nbsp; &nbsp; &nbsp; return res&nbsp; &nbsp; return identifierclass Resnet3DBuilder(object):&nbsp; &nbsp; """ResNet3D."""&nbsp; &nbsp; @staticmethod&nbsp; &nbsp; def build(input_shape, num_outputs, block_fn, repetitions, reg_factor):&nbsp; &nbsp; &nbsp; &nbsp; """Instantiate a vanilla ResNet3D keras model.&nbsp; &nbsp; &nbsp; &nbsp; # Arguments&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input_shape: Tuple of input shape in the format&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (conv_dim1, conv_dim2, conv_dim3, channels) if dim_ordering='tf'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (filter, conv_dim1, conv_dim2, conv_dim3) if dim_ordering='th'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_outputs: The number of outputs at the final softmax layer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; block_fn: Unit block to use {'basic_block', 'bottlenack_block'}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repetitions: Repetitions of unit blocks&nbsp; &nbsp; &nbsp; &nbsp; # Returns&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model: a 3D ResNet model that takes a 5D tensor (volumetric images&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in batch) as input and returns a 1D vector (prediction) as output.&nbsp; &nbsp; &nbsp; &nbsp; """&nbsp; &nbsp; &nbsp; &nbsp; _handle_data_format()&nbsp; &nbsp; &nbsp; &nbsp; if len(input_shape) != 4:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise ValueError("Input shape should be a tuple "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"(conv_dim1, conv_dim2, conv_dim3, channels) "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"for tensorflow as backend or "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"(channels, conv_dim1, conv_dim2, conv_dim3) "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"for theano as backend")&nbsp; &nbsp; &nbsp; &nbsp; block_fn = _get_block(block_fn)&nbsp; &nbsp; &nbsp; &nbsp; input = Input(shape=input_shape)&nbsp; &nbsp; &nbsp; &nbsp; # first conv&nbsp; &nbsp; &nbsp; &nbsp; conv1 = _conv_bn_relu3D(filters=64, kernel_size=(7, 7, 7),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strides=(2, 2, 2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_regularizer=l2(reg_factor)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )(input)&nbsp; &nbsp; &nbsp; &nbsp; pool1 = MaxPooling3D(pool_size=(3, 3, 3), strides=(2, 2, 2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;padding="same")(conv1)&nbsp; &nbsp; &nbsp; &nbsp; # repeat blocks&nbsp; &nbsp; &nbsp; &nbsp; block = pool1&nbsp; &nbsp; &nbsp; &nbsp; filters = 64&nbsp; &nbsp; &nbsp; &nbsp; for i, r in enumerate(repetitions):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; block = _residual_block3d(block_fn, filters=filters,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_regularizer=l2(reg_factor),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repetitions=r, is_first_layer=(i == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )(block)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filters *= 2&nbsp; &nbsp; &nbsp; &nbsp; # last activation&nbsp; &nbsp; &nbsp; &nbsp; block_output = _bn_relu(block)&nbsp; &nbsp; &nbsp; &nbsp; # average poll and classification&nbsp; &nbsp; &nbsp; &nbsp; pool2 = AveragePooling3D(pool_size=(int(block.shape[DIM1_AXIS]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int(block.shape[DIM2_AXIS]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int(block.shape[DIM3_AXIS])),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;strides=(1, 1, 1))(block_output)&nbsp; &nbsp; &nbsp; &nbsp; flatten1 = Flatten()(pool2)&nbsp; &nbsp; &nbsp; &nbsp; if num_outputs > 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dense = Dense(units=num_outputs,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_initializer="he_normal",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activation="softmax",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_regularizer=l2(reg_factor))(flatten1)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dense = Dense(units=num_outputs,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_initializer="he_normal",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activation="sigmoid",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kernel_regularizer=l2(reg_factor))(flatten1)&nbsp; &nbsp; &nbsp; &nbsp; model = Model(inputs=input, outputs=dense)&nbsp; &nbsp; &nbsp; &nbsp; return model&nbsp; &nbsp; @staticmethod&nbsp; &nbsp; def build_resnet_18(input_shape, num_outputs, reg_factor=1e-4):&nbsp; &nbsp; &nbsp; &nbsp; """Build resnet 18."""&nbsp; &nbsp; &nbsp; &nbsp; return Resnet3DBuilder.build(input_shape, num_outputs, basic_block,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[2, 2, 2, 2], reg_factor=reg_factor)&nbsp; &nbsp; @staticmethod&nbsp; &nbsp; def build_resnet_34(input_shape, num_outputs, reg_factor=1e-4):&nbsp; &nbsp; &nbsp; &nbsp; """Build resnet 34."""&nbsp; &nbsp; &nbsp; &nbsp; return Resnet3DBuilder.build(input_shape, num_outputs, basic_block,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[3, 4, 6, 3], reg_factor=reg_factor)&nbsp; &nbsp; @staticmethod&nbsp; &nbsp; def build_resnet_50(input_shape, num_outputs, reg_factor=1e-4):&nbsp; &nbsp; &nbsp; &nbsp; """Build resnet 50."""&nbsp; &nbsp; &nbsp; &nbsp; return Resnet3DBuilder.build(input_shape, num_outputs, bottleneck,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[3, 4, 6, 3], reg_factor=reg_factor)&nbsp; &nbsp; @staticmethod&nbsp; &nbsp; def build_resnet_101(input_shape, num_outputs, reg_factor=1e-4):&nbsp; &nbsp; &nbsp; &nbsp; """Build resnet 101."""&nbsp; &nbsp; &nbsp; &nbsp; return Resnet3DBuilder.build(input_shape, num_outputs, bottleneck,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[3, 4, 23, 3], reg_factor=reg_factor)&nbsp; &nbsp; @staticmethod&nbsp; &nbsp; def build_resnet_152(input_shape, num_outputs, reg_factor=1e-4):&nbsp; &nbsp; &nbsp; &nbsp; """Build resnet 152."""&nbsp; &nbsp; &nbsp; &nbsp; return Resnet3DBuilder.build(input_shape, num_outputs, bottleneck,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[3, 8, 36, 3], reg_factor=reg_factor)最新版本的 Keras 和 Tensorflow 不会出现此问题,但我需要保留这两个库的旧版本,因为我的其他脚本无法在最新版本的 Tensorflow/Keras 上运行。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python