FileNotFoundError:没有这样的文件或目录(对于Dogs和Cats代码)

问题描述 投票:-1回答:1

我是机器学习的新手,我正在关注Google Colab上的Sentdex教程。它应该是一个区分猫和狗图像的ML程序。但是,每当我运行我的代码时,我的“文件或目录”都会出错。

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\atlgwc16\\PetImages/Dog'

老实说,我不知道谷歌Colab存储文件的位置,所以我不知道在哪里放置图像文件夹。

到目前为止,这是我的完整代码:

import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm

DATADIR = "C:\Users\atlgwc16\PetImages"
CATEGORIES = ["Dog", "Cat"]

for category in CATEGORIES:
  path = os.path.join(DATADIR, category) 
  for img in os.listdir(path):
    img_array = cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
    plt.imshow(img_array, cmap = 'gray')
    plt.show()

    break
python file machine-learning google-colaboratory
1个回答
0
投票

正如问题中引用的那样遵循教程:

https://pythonprogramming.net/loading-custom-data-deep-learning-python-tensorflow-keras/

由于您使用的是Google Colab,因此您可以将狗和猫图像的Kaggle数据集上传到Google云端硬盘。请参阅Google提供的Google Colab Jupyter笔记本,其中说明了如何执行此操作:

https://colab.research.google.com/notebooks/io.ipynb#scrollTo=u22w3BFiOveA

然后,您可以访问Google云端硬盘中的文件(在这种情况下,将文档集上传到Google云端硬盘后),就像访问计算机本地文件一样。

这是上面链接中提供的示例:

with open('/content/gdrive/My Drive/foo.txt', 'w') as f:
  f.write('Hello Google Drive!')
!cat /content/gdrive/My\ Drive/foo.txt

因此,由于您使用的是Google Colab,因此您需要调整Sentdex教程中的代码,以便更好地使用您正在创建的笔记本。 Google Colab使用Jupyter笔记本电脑。笔记本中的每个单元都运行相同的“会话”。因此,如果您在一个单元格中导入Python模块,则可以在下一个单元格中使用它。它就像那样神奇。

它看起来像这样:

[CELL 1]

from google.colab import drive
drive.mount('/content/gdrive')

然后,您将允许Google Colab访问您的Google云端硬盘。

[CELL 2]

import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm

DATADIR = '/content/gdrive/My Drive/PetImages/'

#^See?#
# You would need to go to Google Drive and create the 'PetImages' folder at the top level of your Google Drive. You would upload the data set to the PetImages folder creating a 'Dog' subfolder and a 'Cat' subfolder.

CATEGORIES = ["Dog", "Cat"]

for category in CATEGORIES:  # do dogs and cats
    path = os.path.join(DATADIR,category)  # create path to dogs and cats
    for img in os.listdir(path):  # iterate over each image per dogs and cats
        img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)  # convert to array
        plt.imshow(img_array, cmap='gray')  # graph it
        plt.show()  # display!

        break  # we just want one for now so break
    break  #...and one more!

将数据集正确上传到Google云端硬盘并使用特殊的google.colab模块后,您应该可以轻松访问您的培训数据。 Google Colab是一个基于云的工具,用于创建Jupyter笔记本和运行Python程序。因此,虽然类似于在您的计算机上本地运行Python程序,但它并不完全相同。如果您想在云中完全使用它,将有助于了解Google Colab的工作原理 - 使用GDrive存储文件而不是您自己的计算机。请参阅我在Google上面发布的链接。

快乐的编码。

© www.soinside.com 2019 - 2024. All rights reserved.