简体   繁体   English

如何使用 CNN 代码修复 Python “IndexError: list index out of range”

[英]How to fix Python “IndexError: list index out of range” with CNN code

I'm trying to construct a ResNet34 Encoder as part of my CNN with the following function on Python 3.7.我正在尝试在 Python 3.7 上使用以下 function 构建一个ResNet34 编码器作为我的 CNN 的一部分。

import tensorflow as tf

from tensorpack import *
from tensorpack.models import BatchNorm, BNReLU, Conv2D, MaxPooling, FixedUnPooling
from tensorpack.tfutils.summary import add_moving_summary, add_param_summary
from .utils import *
import sys

sys.path.append("..") # adds higher directory to python modules path.
try: # HACK: import beyond current level, may need to restructure
    from config import Config
except ImportError:
    assert False, 'Fail to import config.py'
def res_blk(name, l, ch, ksize, count, split=1, strides=1, freeze=False):
    ch_in = l.get_shape().as_list()
    with tf.variable_scope(name):
        for i in range(0, count):
            with tf.variable_scope('block' + str(i)):  
                x = l if i == 0 else BNReLU('preact', l)
                x = Conv2D('conv1', x, ch[0], ksize[0], activation=BNReLU)
                x = Conv2D('conv2', x, ch[1], ksize[1], split=split, 
                                strides=strides if i == 0 else 1, activation=BNReLU)
                x = Conv2D('conv3', x, ch[2], ksize[2], activation=tf.identity)
                if (strides != 1 or ch_in[1] != ch[2]) and i == 0:
                    l = Conv2D('convshortcut', l, ch[2], 1, strides=strides)
                x = tf.stop_gradient(x) if freeze else x
                l = l + x
        l = BNReLU('bnlast',l)  
    return l

def encoder(i, freeze):
    d1 = Conv2D('conv0',  i, 64, 7, padding='valid', strides=1, activation=BNReLU)
    d1 = res_blk('group0', d1, [ 64,  64], [3, 3], 3, strides=1, freeze=freeze)

    d2 = res_blk('group1', d1, [128, 128], [3, 3], 4, strides=2, freeze=freeze)
    d2 = tf.stop_gradient(d2) if freeze else d2

    d3 = res_blk('group2', d2, [256, 256], [3, 3], 6, strides=2, freeze=freeze)
    d3 = tf.stop_gradient(d3) if freeze else d3

    d4 = res_blk('group3', d3, [512, 512], [3, 3], 3, strides=2, freeze=freeze)
    d4 = tf.stop_gradient(d4) if freeze else d4

    d4 = Conv2D('conv_bot',  d4, 1024, 1, padding='same')
    return [d1, d2, d3, d4]

Then i get the error然后我得到错误

 line 67, in encoder
    d1 = res_blk('group0', d1, [ 64,  64], [3, 3], 3, strides=1, freeze=freeze)                       
  File "....", line 34, in res_blk
    x = Conv2D('conv3', x, ch[2], ksize[2], activation=tf.identity)
IndexError: list index out of range

What is the cause of this error and how can i fix it?此错误的原因是什么,我该如何解决? The original code was a Resnet50 which worked fine ie the code would be原始代码是一个运行良好的 Resnet50,即代码是

d1 = res_blk('group0', d1, [ 64, 64, 256], [1, 3, 1], 3, strides=1, freeze=freeze)

The key bit I found in the link you included is that "Each ResNet block is either two layers deep (used in small networks like ResNet 18, 34) or 3 layers deep (ResNet 50, 101, 152).".我在您包含的链接中发现的关键位是“每个 ResNet 块都是两层深(用于 ResNet 18、34 等小型网络)或 3 层深(ResNet 50、101、152)。”。

The code you provided is catoring for the three layers required for ResNet 50, 101 and 152, but not accomodating the simpler two layer approach is this example, as far as I can tell.您提供的代码适用于 ResNet 50、101 和 152 所需的三层,但据我所知,此示例不适用更简单的两层方法。

There may be some more tweaking to do, but you should be able to just remove the final layer, similar to I have below, that would've otherwise been necessary in 50+ layer ResNet architectures.可能需要做更多的调整,但您应该能够只删除最后一层,类似于我在下面的内容,否则在 50 层以上的 ResNet 架构中这是必需的。

import tensorflow as tf

from tensorpack import *
from tensorpack.models import BatchNorm, BNReLU, Conv2D, MaxPooling, FixedUnPooling
from tensorpack.tfutils.summary import add_moving_summary, add_param_summary
from .utils import *
import sys

sys.path.append("..") # adds higher directory to python modules path.
try: # HACK: import beyond current level, may need to restructure
    from config import Config
except ImportError:
    assert False, 'Fail to import config.py'
def res_blk(name, l, ch, ksize, count, split=1, strides=1, freeze=False):
    ch_in = l.get_shape().as_list()
    with tf.variable_scope(name):
        for i in range(0, count):
            with tf.variable_scope('block' + str(i)):  
                x = l if i == 0 else BNReLU('preact', l)
                x = Conv2D('conv1', x, ch[0], ksize[0], activation=BNReLU)
                x = Conv2D('conv2', x, ch[1], ksize[1], split=split, 
                                strides=strides if i == 0 else 1, activation=BNReLU)
                x = tf.stop_gradient(x) if freeze else x
                l = l + x
        l = BNReLU('bnlast',l)  
    return l

def encoder(i, freeze):
    d1 = Conv2D('conv0',  i, 64, 7, padding='valid', strides=1, activation=BNReLU)
    d1 = res_blk('group0', d1, [ 64,  64], [3, 3], 3, strides=1, freeze=freeze)

    d2 = res_blk('group1', d1, [128, 128], [3, 3], 4, strides=2, freeze=freeze)
    d2 = tf.stop_gradient(d2) if freeze else d2

    d3 = res_blk('group2', d2, [256, 256], [3, 3], 6, strides=2, freeze=freeze)
    d3 = tf.stop_gradient(d3) if freeze else d3

    d4 = res_blk('group3', d3, [512, 512], [3, 3], 3, strides=2, freeze=freeze)
    d4 = tf.stop_gradient(d4) if freeze else d4

    d4 = Conv2D('conv_bot',  d4, 1024, 1, padding='same')
    return [d1, d2, d3, d4]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 IndexError:列出此代码的索引超出范围 - How to fix IndexError: list index out of range for this code 如何在下面的代码中修复“IndexError:list index out of range” - How to fix “IndexError: list index out of range” in the following piece of code 如何在我的代码中修复“IndexError:列表索引超出范围”? - How to fix “IndexError: List index is out of range” in my code? 如何修复Python中的“ IndexError:列表分配索引超出范围”错误? - How to fix “IndexError: list assignment index out of range” error in Python? 如何修复 Python 上的“IndexError: list index out of range” - How to fix "IndexError: list index out of range" on Python 如何修复python中的“IndexError:列表索引超出范围”? - How to fix 'IndexError: list index out of range' in python? 如何修复 Python 中的“IndexError: list index out of range” - How do you fix 'IndexError: list index out of range' in Python 如何在Python中修复“ IndexError:列表索引超出范围” - How to fix “IndexError: list index out of range” in python 当代码缺失值时,如何修复Web抓取Python代码“ IndexError:列表索引超出范围” - How to fix web scraping Python code “IndexError: list index out of range” when the code hits missing values 如何解决“ IndexError:列表索引超出范围” - How to fix “IndexError: list index out of range”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM