[英]How to fix Python “IndexError: list index out of range” with CNN code
我正在尝试在 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]
然后我得到错误
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
此错误的原因是什么,我该如何解决? 原始代码是一个运行良好的 Resnet50,即代码是
d1 = res_blk('group0', d1, [ 64, 64, 256], [1, 3, 1], 3, strides=1, freeze=freeze)
我在您包含的链接中发现的关键位是“每个 ResNet 块都是两层深(用于 ResNet 18、34 等小型网络)或 3 层深(ResNet 50、101、152)。”。
您提供的代码适用于 ResNet 50、101 和 152 所需的三层,但据我所知,此示例不适用更简单的两层方法。
可能需要做更多的调整,但您应该能够只删除最后一层,类似于我在下面的内容,否则在 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.