簡體   English   中英

列表索引超出范圍.. 適用於 google colab 但不適用於本地機器?

[英]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上運行,而colablinux上運行,並且兩者的路徑分隔符不同。 現在你需要更換

category = path.split("/")[3]

category = path.split("\\")[2]

你的代碼應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM