【问题标题】:AttributeError: module 'resnet' has no attribute 'ResnetBuilder'AttributeError:模块“resnet”没有属性“ResnetBuilder”
【发布时间】:2019-11-19 03:00:45
【问题描述】:

您好,我尝试运行以下程序,但令人惊讶的是我得到以下错误:

AttributeError: module 'resnet' has no attribute 'ResnetBuilder'

在从GitHub 运行 ResNet 期间,但我不明白为什么会在以下部分发生:

 model = resnet.ResnetBuilder.build_resnet_18((img_channels, img_rows, img_cols), nb_classes)
 model.compile(loss='categorical_crossentropy',
                 optimizer='adam',

考虑到 ResnetBuilder 它已经在这里定义了:

class ResnetBuilder(object):
    @staticmethod
    def build(input_shape, num_outputs, block_fn, repetitions):
        """Builds a custom ResNet like architecture.
        Args:
            input_shape: The input shape in the form (nb_channels, nb_rows, nb_cols)
            num_outputs: The number of outputs at final softmax layer
            block_fn: The block function to use. This is either `basic_block` or `bottleneck`.
                The original paper used basic_block for layers < 50
            repetitions: Number of repetitions of various block units.
                At each block unit, the number of filters are doubled and the input size is halved
        Returns:
            The keras `Model`.
        """
        _handle_dim_ordering()
        if len(input_shape) != 3:
            raise Exception("Input shape should be a tuple (nb_channels, nb_rows, nb_cols)")

        # Permute dimension order if necessary
        if K.image_dim_ordering() == 'tf':
            input_shape = (input_shape[1], input_shape[2], input_shape[0])

        # Load function from str if needed.
        block_fn = _get_block(block_fn)

        input = Input(shape=input_shape)
        conv1 = _conv_bn_relu(filters=64, kernel_size=(7, 7), strides=(2, 2))(input)
        pool1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding="same")(conv1)

        block = pool1
        filters = 64
        for i, r in enumerate(repetitions):
            block = _residual_block(block_fn, filters=filters, repetitions=r, is_first_layer=(i == 0))(block)
            filters *= 2

        # Last activation
        block = _bn_relu(block)

        # Classifier block
        block_shape = K.int_shape(block)
        pool2 = AveragePooling2D(pool_size=(block_shape[ROW_AXIS], block_shape[COL_AXIS]),
                                 strides=(1, 1))(block)
        flatten1 = Flatten()(pool2)
        dense = Dense(units=num_outputs, kernel_initializer="he_normal",
                      activation="softmax")(flatten1)

        model = Model(inputs=input, outputs=dense)
        return model

    @staticmethod
    def build_resnet_18(input_shape, num_outputs):
        return ResnetBuilder.build(input_shape, num_outputs, basic_block, [2, 2, 2, 2])

    @staticmethod
    def build_resnet_34(input_shape, num_outputs):
        return ResnetBuilder.build(input_shape, num_outputs, basic_block, [3, 4, 6, 3])

    @staticmethod
    def build_resnet_50(input_shape, num_outputs):
        return ResnetBuilder.build(input_shape, num_outputs, bottleneck, [3, 4, 6, 3])

    @staticmethod
    def build_resnet_101(input_shape, num_outputs):
        return ResnetBuilder.build(input_shape, num_outputs, bottleneck, [3, 4, 23, 3])

    @staticmethod
    def build_resnet_152(input_shape, num_outputs):
        return ResnetBuilder.build(input_shape, num_outputs, bottleneck, [3, 8, 36, 3])

任何想法,如何解决?

【问题讨论】:

  • 你导入resnet了吗?试试from resnet import ResnetBuilder。它总是对我有用...
  • 你有自己的模块resnet吗?
  • @Engineero 不幸的是,这不起作用并返回:ImportError: cannot import name 'ResnetBuilder' from 'resnet' (/home/user/anaconda3/envs/HW/lib/python3.7/site-packages/resnet/__init__.py)
  • 等等,resnet 不是 pip 可安装的,那么这个挂在你的 site-packages 目录中的 resnet 是什么?您可能应该在您的工作目录或 python 路径中的某个地方有一个 resnet.py 文件的副本,这就是您需要从中导入的 resnet。
  • @Engineero 哦,这就是原因!我通过pip install resnet 安装它,我认为它将以正确的路径传输:D 那么如果不是这种情况,如何通过终端/Jupyter Notebook 从 GitHub 将resnet.py 正确保存在 python 路径中?

标签: python keras attributeerror resnet


【解决方案1】:

你可以试试别的包:

试试这个repo

!pip install image-classifiers

from classification_models import Classifiers
classifier, preprocess_input = Classifiers.get('resnet18')
model = classifier((224, 224, 3), weights='imagenet')

【讨论】:

    猜你喜欢
    • 2018-04-14
    • 2019-02-18
    • 1970-01-01
    • 2020-01-01
    • 2019-07-20
    • 2021-11-05
    • 2021-11-04
    • 2021-02-23
    相关资源
    最近更新 更多