IndexError: index 1 is out of bounds for axis 0 with size 1

注:仅仅为了自己记录
该错误是索引超出了列表的长度的,比如创建了长度为1的数组a,而我的索引为在a[1]:

import numpy as np
a = np.empty(1)
print(a[1])
就会报错:

IndexError: index 1 is out of bounds for axis 0 with size 1
再比如我创建了长度为3的数组a, 而我的索引为a[5]:

import numpy as np
a = np.empty(3)
print(a[5])
就会报错:

IndexError: index 5 is out of bounds for axis 0 with size 3
1
(axis 0:表示是一维数组)
所以这时候就回去检查是自己的索引错了, 还是数组长度定义错了。

原文链接:https://blog.csdn.net/weixin_44493244/article/details/105968388

我试过上面的方法之后,不太行,所以我的错误不是出现在这里,在咨询过原代码作者后,发现是batch_size的问题,原代码中一次加载4张图片,我由于GPU内存原因设置为2了,所以出现了,错误,进一步缩小为1,后解决问题:

parser.add_argument('--batch_size', type=int, default=1,#8
                    help='batch size in total.')


def structure_visual(gt, masked, recons_gen, recons_gt, iter, size, save_dir):
    """ Show 4 generated structure feature maps in the training of structure generator.""" 

    # gap between each images
    gap = 2

    # height and width of result image
    #height = size * 4 + gap * 3
    #width = size * 4 + gap * 3
    height = size   #修改后
    width = size * 4 + gap * 3

    result = 255 * np.ones((height, width, 3), dtype=np.uint8) 

    #for i in range(4):
    for i in range(1):#修改后
        gt_i = ((gt[i] + 1.) * 127.5).astype(np.uint8)
        masked_i = ((masked[i] + 1.) * 127.5).astype(np.uint8)
        recons_gen_i = ((recons_gen[i] + 1.) * 127.5).astype(np.uint8)
        recons_gt_i = ((recons_gt[i] + 1.) * 127.5).astype(np.uint8)
        
        # fill the images into grid
        result[i*(size+gap):i*(size+gap)+size, 0*(size+gap):0*(size+gap)+size, ::-1] = gt_i
        result[i*(size+gap):i*(size+gap)+size, 1*(size+gap):1*(size+gap)+size, ::-1] = masked_i
        result[i*(size+gap):i*(size+gap)+size, 2*(size+gap):2*(size+gap)+size, ::-1] = recons_gen_i
        result[i*(size+gap):i*(size+gap)+size, 3*(size+gap):3*(size+gap)+size, ::-1] = recons_gt_i
        
    cv2.imwrite(os.path.join(save_dir, 'structure%d.png' % iter), result)

修改后问题解决。

你可能感兴趣的:(图像处理python,python)