Python 导入类会导致运行时错误

Python 导入类会导致运行时错误,python,python-import,keras,Python,Python Import,Keras,我正试图和全班同学一起构建res网络 当运行resnet.py时,主函数运行,我得到了预期的结果。主要功能如下所示: def main(): model = ResNetBuilder.build_resnet_18((3, 224, 224), 1000) model.compile(loss="categorical_crossentropy", optimizer="sgd") model.summary() if __name__ == '__main__':

我正试图和全班同学一起构建res网络

当运行resnet.py时,主函数运行,我得到了预期的结果。主要功能如下所示:

def main():
    model = ResNetBuilder.build_resnet_18((3, 224, 224), 1000)
    model.compile(loss="categorical_crossentropy", optimizer="sgd")
    model.summary()
if __name__ == '__main__':
    main()
导入函数时,会出现运行时错误。最简单的例子:

from resnet import ResNetBuilder

from keras import backend as K
K.set_image_dim_ordering('th')

model = ResNetBuilder.build_resnet_18((3, 224, 224), 1000)
model.compile(loss="categorical_crossentropy", optimizer="sgd")
model.summary()
这些错误是关于层大小或除以零的运行时错误-我觉得我必须在导入方面做一些非常愚蠢的事情,但不能完全看到它

输出:

Using Theano backend.
Using gpu device 0: GeForce GTX 750 Ti (CNMeM is disabled, cuDNN 5105)
/......./site-packages/keras/backend/theano_backend.py:1500: UserWarning: DEPRECATION: the 'ds' parameter is not going to exist anymore as it is going to be replaced by the parameter 'ws'.
  mode='max')
/......./site-packages/keras/backend/theano_backend.py:1500: UserWarning: DEPRECATION: the 'st' parameter is not going to exist anymore as it is going to be replaced by the parameter 'stride'.
  mode='max')
/......./site-packages/keras/backend/theano_backend.py:1500: UserWarning: DEPRECATION: the 'padding' parameter is not going to exist anymore as it is going to be replaced by the parameter 'pad'.
  mode='max')
Traceback (most recent call last):
  File "testResnet.py", line 6, in <module>
    model = ResNetBuilder.build_resnet_18((3, 224, 224), 1000)
  File "........resnet.py", line 154, in build_resnet_18
    return ResNetBuilder.build(input_shape, num_outputs, basic_block, [2, 2, 2, 2])
  File "........resnet.py", line 139, in build
    block = _residual_block(block_fn, nb_filters=nb_filters, repetitions=r, is_first_layer=i == 0)(block)
  File "........resnet.py", line 77, in f
    input = block_function(nb_filters=nb_filters, init_subsample=init_subsample)(input)
  File "........resnet.py", line 90, in f
    return _shortcut(input, residual)
  File "........resnet.py", line 65, in _shortcut
    init="he_normal", border_mode="valid")(input)
  File "/......./site-packages/keras/engine/topology.py", line 514, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
  File "/......./site-packages/keras/engine/topology.py", line 572, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File "/......./site-packages/keras/engine/topology.py", line 152, in create_node
    output_shapes = to_list(outbound_layer.get_output_shape_for(input_shapes[0]))
  File "/......./site-packages/keras/layers/convolutional.py", line 451, in get_output_shape_for
    self.border_mode, self.subsample[0])
  File "/......./site-packages/keras/utils/np_utils.py", line 131, in conv_output_length
    return (output_length + stride - 1) // stride
ZeroDivisionError: integer division or modulo by zero
使用无后端。
使用gpu设备0:GeForce GTX 750 Ti(禁用CNMeM,cuDNN 5105)
/……站点包/keras/backend/theano_backend.py:1500:UserWarning:DEPRECATION:ds参数将不再存在,因为它将被参数“ws”替换。
mode='max')
/.../站点包/keras/backend/theano_backend.py:1500:UserWarning:DEPRECATION:st参数将不再存在,因为它将被参数“stride”替换。
mode='max')
/……站点包/keras/backend/theano_backend.py:1500:UserWarning:DEPRECATION:padding参数将不再存在,因为它将被参数“pad”替换。
mode='max')
回溯(最近一次呼叫最后一次):
文件“testResnet.py”,第6行,在
model=ResNetBuilder.build\u resnet\u 18((3224224),1000)
文件“……resnet.py”,第154行,内建\u resnet\u 18
返回ResNetBuilder.build(输入\u形状、num\u输出、基本\u块[2,2,2,2])
文件“……resnet.py”,第139行,内部版本
块=剩余块(块fn,nb\u过滤器=nb\u过滤器,重复次数=r,第一层=i==0)(块)
文件“...resnet.py”,第77行,在f中
输入=块函数(nb_过滤器=nb_过滤器,初始子样本=初始子样本)(输入)
文件“...resnet.py”,第90行,在f中
返回快捷方式(输入、剩余)
文件“...resnet.py”,第65行,在快捷方式中
init=“he\u normal”,border\u mode=“valid”)(输入)
文件“/……/site packages/keras/engine/topology.py”,第514行,在调用中__
添加\入站\节点(入站\层、节点\索引、张量\索引)
文件“/……/site packages/keras/engine/topology.py”,第572行,在添加节点中
创建节点(自、入站层、节点索引、张量索引)
文件“/……/site packages/keras/engine/topology.py”,第152行,在创建节点中
输出形状=到列表(出站层。获取输出形状(输入形状[0]))
文件“/……/site packages/keras/layers/convolutional.py”,第451行,在get_output_shape_中
self.border_模式,self.subsample[0])
文件“/…/site packages/keras/utils/np_utils.py”,第131行,conv_output_长度
返回(输出长度+步幅-1)//步幅
ZeroDivisionError:整数除法或模零除法

请回答您的问题,并添加一个完整的回溯,显示问题或问题发生的至少一个实例。您为什么要手动设置图像维度排序?Keras可以使用Theano或Tensorflow,它们有不同的排序约定-Theano使用[通道、行、列],Tensorflow使用[行、列、列]。这肯定是可行的,我已经检查了后端是否正确设置,方法是从类方法中打印。请释放您的问题并添加一个完整的回溯,显示问题或至少一个问题发生的实例。为什么您要手动设置图像维度顺序?Keras可以使用Theano或Tensorflow,它们具有不同的属性排序约定-Theano使用[channels,rows,cols],Tensorflow使用[rows,cols,channels]。这肯定是可行的,我已经通过在类方法中打印来检查后端是否正确设置