[英]List Index out of range.. works on google colab but not on local machine?
我正在尝试在我的本地机器上重新创建这个项目。 它旨在在 Google Colab 上运行,我在那里重新创建了它,它运行良好。 我现在想尝试在本地机器上运行它,所以我安装了所有必需的包,anaconda,Juypter Notebook 等。
当我来到处理图像的部分时:
# Loops through imagepaths to load images and labels into arrays
for path in imagepaths:
img = cv2.imread(path) # Reads image and returns np.array
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Converts into the corret colorspace (GRAY)
img = cv2.resize(img, (320, 120)) # Reduce image size so training can be faster
X.append(img)
#Processing label in image path
category = path.split("/")[3]
label = int(category.split("_")[0][1])
y.append(label)
它引发以下错误:
IndexError: list index out of range
代码大部分没有改变,数据集是一样的。 唯一的区别是我在本地运行 vs google colab。 我在网上搜索,有人说做 len(path) 来验证(在我的情况下)它上升到 [3],它确实(它的大小为 33)。
此处的代码已更改:
我没有使用这条线,因为我没有使用 google colab:
from google.colab import files
这部分代码中使用了“文件”:
# We need to get all the paths for the images to later load them
imagepaths = []
# Go through all the files and subdirectories inside a folder and save path to images inside list
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
path = os.path.join(root, name)
if path.endswith("png"): # We want only the images
imagepaths.append(path)
print(len(imagepaths)) # If > 0, then a PNG image was loaded
在我的本地机器上,我删除了from google.colab...
行,并正常运行其他所有内容。 上面的代码片段中使用了关键字 files,但是在运行它时我没有抛出任何错误。 **NOTE len(path) on Jupyter shows 33, len(path) on Google shows 16..?**
有谁知道问题可能是什么? 我不认为它来自删除那一行代码。 如果是这样,你建议我做什么来解决它?
您的本地计算机在Windows
上运行,而colab
在linux
上运行,并且两者的路径分隔符不同。 现在你需要更换
category = path.split("/")[3]
和
category = path.split("\\")[2]
你的代码应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.