【问题标题】:TypeError: slice indices must be integers or None or have an __index__ method in python 2.7TypeError:切片索引必须是整数或无或在 python 2.7 中有 __index__ 方法
【发布时间】:2020-09-11 00:42:55
【问题描述】:
for file in zip(frames_list[-round(0.2 * len(frames_list)):], masks_list[-round(0.2 * len(masks_list)):]):
    # Convert tensors to numpy arrays
    frame = frame_batches.next().numpy().astype(np.uint8)
    mask = mask_batches.next().numpy().astype(np.uint8)

    # Convert numpy arrays to images
    frame = Image.fromarray(frame)
    mask = Image.fromarray(mask)

    # Save frames and masks to correct directories
    frame.save(DATA_PATH + '{}_frames/{}'.format(dir_name, dir_name) + '/' + file[0])
    mask.save(DATA_PATH + '{}_masks/{}'.format(dir_name, dir_name) + '/' + file[1])

print("Saved {} frames to directory {}".format(len(frames_list), DATA_PATH))
print("Saved {} masks to directory {}".format(len(masks_list), DATA_PATH))

追溯

Traceback (most recent call last):
  File "/home/khawar/Desktop/Khawar_Seg/main.py", line 190, in <module>
    generate_image_folder_structure(frame_tensors, masks_tensors, frames_list, masks_list)
  File "/home/khawar/Desktop/Khawar_Seg/main.py", line 173, in generate_image_folder_structure
    for file in zip(frames_list[-round(0.2 * len(frames_list)):], masks_list[-round(0.2 * len(masks_list)):]):
TypeError: slice indices must be integers or None or have an __index__ method

【问题讨论】:

  • 出于好奇,frames_listlen(frames_list) 是什么?
  • ['0016E5_07983.png', '0016E5_08077.png', '0016E5_08067.png', '0016E5_08081.png', '0016E5_07993.png' and len of framelist 是 101
  • 您已更改帖子以包含更少的细节。保持frames_list[:-round(0.2 * len(frames_list))] 提供上下文很重要。
  • 是的。抱歉,我已经解决了一个错误。现在错误只是验证部分
  • 这样好多了!谢谢!

标签: python-2.7 tensorflow tensorflow2.0


【解决方案1】:

python 2.7 中的 round 函数返回 float 类型,但序列切片需要 int 作为参数。

>>> type(round(2.0))
<type 'float'>

>>> items = [0, 1, 2, 3, 4]
>>> items[round(2.0):]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: slice indices must be integers or None or have an __index__ method

# If we cast the rounded index to an int, it will work
>>> index = int(round(2.0))
>>> type(index)
<type 'int'>

>>> items[int(round(2.0)):]
[2, 3, 4]

因此,对于您的示例代码,您需要将索引转换为整数,然后再在切片中使用它们——for 循环的 [&lt;start&gt;:&lt;end&gt;] 部分。

frames_index = -int(round(0.2 * len(frames_list)))
masks_index = -int(round(0.2 * len(masks_list)))

for file in zip(frames_list[frames_index:], masks_list[masks_index:]):
    ...

为了便于阅读,我建议你使用一个函数来制作你的索引号:

def get_list_index(list_size):  # list_size is the len(<list>)
    float_value = round(0.2 * list_size)
    return -int(float_value)

frames_index = get_list_index(len(frames_list))
masks_index = get_list_index(len(masks_list))

for file in zip(frames_list[frames_index:], masks_list[masks_index:]):
    ...

编辑:

回答评论中的问题:

zip(frames_list[-round(0.2 * len(frames_list)):]中的文件:是什么意思?

: 在 python 切片表示法中将 start 索引与 end 索引分开。

例如,如果您有列表['a', 'b', 'c', 'd', 'e'],并且您只想获取从'b''d' 的部分,您可以使用从1 开始并以4 结束的切片 -- _1超过'd' 的索引。

>>> ['a', 'b', 'c', 'd', 'e'][1:4]
['b', 'c', 'd']

Python 允许您使用 negative 索引,因此您可以从右侧倒数。我们可以使用-1 而不是3 来编写相同的切片:

>>> ['a', 'b', 'c', 'd', 'e'][1:-1]
['b', 'c', 'd']

如果我们想要所有列表中的项目,从 'b' 开始一直到最后,我们可以将我们的正确索引更改为 None 或将其省略:

>>> ['a', 'b', 'c', 'd', 'e'][1:None]
['b', 'c', 'd', 'e']

>>> ['a', 'b', 'c', 'd', 'e'][1:]
['b', 'c', 'd', 'e']

【讨论】:

  • 请告诉我一件事。是现在吗?对于 zip(frames_list[-int(round(0.2 * len(frames_list)))]、masks_list[-int(round(0.2 * len(masks_list)))]) 中的文件:?
猜你喜欢
  • 2022-08-11
  • 2018-06-06
  • 2020-07-01
  • 2017-07-27
  • 2015-04-01
  • 2017-10-16
  • 2018-09-21
  • 1970-01-01
  • 2021-08-21
相关资源
最近更新 更多