TypeError: slice indices must be integers or None or have an __index__ method in python 2.7
TypeError: slice indices must be integers or None or have an __index__ method in python 2.7
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
python 2.7 returns 中的 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 循环的 [<start>:<end>]
部分。
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:]):
...
编辑:
回答您评论中的问题:
what is the meaning of :
in for file in zip(frames_list[-round(0.2 * len(frames_list)):]
?
:
将 start 索引与 end 索引分开 python 切片表示法。
例如,如果您有列表 ['a', 'b', 'c', 'd', 'e']
并且您只想获取从 'b'
到 'd'
的部分,您将使用从 [=26= 开始的切片] 并以 4
结束——比 'd'
的索引多 _1。
>>> ['a', 'b', 'c', 'd', 'e'][1:4]
['b', 'c', 'd']
Python 允许您使用 negative 索引,因此您可以从右侧倒数。我们可以使用 -1
而不是 3
:
来编写相同的切片
>>> ['a', 'b', 'c', 'd', 'e'][1:-1]
['b', 'c', 'd']
如果我们想要 all 列表中的项目,从 'b'
开始到最后,我们可以将我们的右索引更改为 None
或将其省略:
>>> ['a', 'b', 'c', 'd', 'e'][1:None]
['b', 'c', 'd', 'e']
>>> ['a', 'b', 'c', 'd', 'e'][1:]
['b', 'c', 'd', 'e']
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
python 2.7 returns 中的 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 循环的 [<start>:<end>]
部分。
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:]):
...
编辑:
回答您评论中的问题:
what is the meaning of
:
in for file inzip(frames_list[-round(0.2 * len(frames_list)):]
?
:
将 start 索引与 end 索引分开 python 切片表示法。
例如,如果您有列表 ['a', 'b', 'c', 'd', 'e']
并且您只想获取从 'b'
到 'd'
的部分,您将使用从 [=26= 开始的切片] 并以 4
结束——比 'd'
的索引多 _1。
>>> ['a', 'b', 'c', 'd', 'e'][1:4]
['b', 'c', 'd']
Python 允许您使用 negative 索引,因此您可以从右侧倒数。我们可以使用 -1
而不是 3
:
>>> ['a', 'b', 'c', 'd', 'e'][1:-1]
['b', 'c', 'd']
如果我们想要 all 列表中的项目,从 'b'
开始到最后,我们可以将我们的右索引更改为 None
或将其省略:
>>> ['a', 'b', 'c', 'd', 'e'][1:None]
['b', 'c', 'd', 'e']
>>> ['a', 'b', 'c', 'd', 'e'][1:]
['b', 'c', 'd', 'e']