FileNotFoundError: No such file or directory (for Dogs and Cats code)
FileNotFoundError: No such file or directory (for Dogs and Cats code)
我是机器学习的新手,我正在学习 Google Colab 上的 Sentdex 教程。它应该是一个区分猫和狗图像的机器学习程序。但是,每当我 运行 我的代码时,我的 'file or directory.'
就会出问题
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\atlgwc16\PetImages/Dog'
老实说,我不知道 Google 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
正在遵循问题中引用的教程:
https://pythonprogramming.net/loading-custom-data-deep-learning-python-tensorflow-keras/
由于您使用的是 Google Colab,因此您可以将狗和猫图像的 Kaggle 数据集上传到 Google 云端硬盘。请参阅 Google 提供的 Google Colab Jupyter notebook,其中解释了如何执行此操作:
https://colab.research.google.com/notebooks/io.ipynb#scrollTo=u22w3BFiOveA
然后您可以从您的 Google Drive 访问文件(在本例中,是将其上传到 Google Drive 后的训练集),这与在您的计算机上访问本地文件的方式非常相似.
这是上面link中提供的例子:
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 笔记本。笔记本中的每个单元格都使用相同的 'session'。因此,如果您在一个单元格中导入 Python 模块,则可以在下一个单元格中使用它。就是这么神奇。
看起来像这样:
[单元格 1]
from google.colab import drive
drive.mount('/content/gdrive')
然后您将允许 Google Colab 访问您的 Google 驱动器。
[单元格 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.
上面发布的 link
编码愉快。
我为自己做的,它对我有用。
我使用本地驱动器(如硬盘)中的数据集。
注意:您的数据集文件夹必须是 zip 格式。
首先,按照我的方法,您将从本地 drive.I 使用 google colab 访问您的数据集。首先,在 google Colab 中创建一个 Jupyter notebook,然后 运行 逐步编写以下代码:
第一步:运行在你的笔记本中输入以下代码,并从你的硬盘或本地驱动器上传你的数据集
from google.Colab import files
uploaded = files.upload()
当过程 100% 完成并执行第二步时:
第二步:
复制并运行下面的代码,这一步将解压数据集
import zipfile
import io
zf = zipfile.ZipFile(io.BytesIO(uploaded['DogVsCat.zip']), "r")
zf.extractall()
第三步:运行导入所有需要的库的代码
import numpy as np
import os
import cv2
import matplotlib.pyplot as plt
这将为您导入所有必需的库。
第四步:
specify the path 指定路径执行以下步骤:
拳头:check the image for more ease of your
在左下角的文件夹图标上,单击突出显示的文件夹,您将看到解压缩的数据集,在我的例子中,我的数据集是“DogVsCat”,
注意:这里会看到zip和unzip两种数据集,你复制解压数据的路径。
右键单击它并从中复制路径,然后
运行 下面的代码:
DIRECTORY ='/content/DogVsCats'
CATEGORIES = ['cats', 'dogs']
注意:请在DIRECTORY(这个目录是我的路径)路径中添加你的路径,而不是我的路径。再次 运行 代码:
注意:请在 CATEGORIES 中添加您自己的文件夹名称,而不是我的文件夹名称以获取更多信息,请参见图片:
my dataset structure
最后创建火车数据
第五步:
data = []
for category in CATEGORIES:
path = os.path.join(DIRECTORY, category)
for img in os.listdir(path):
img_path = os.path.join(path, img)
label = CATEGORIES.index(category)
arr = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
new_arr = cv2.resize(arr, (60, 60))
data.append([new_arr, label])
第六步:
打印数据:
运行下面的代码给你看
data
第七步:洗牌你的数据:
import random
random.shuffle(data)
八步:
指定用于训练模型的特征和标签
X = []
y = []
for features, label in data:
X.append(features)
y.append(label)
X = np.array(X)
y = np.array(y)
第九步:打印特征
X
第十步:打印标签
y
请注意,由于时间不够,我无法与您分享所有代码。
注意:为了更清晰,请查看我的代码图片:
pic1-Of-My-Code
pic2-of-my-code
我是机器学习的新手,我正在学习 Google Colab 上的 Sentdex 教程。它应该是一个区分猫和狗图像的机器学习程序。但是,每当我 运行 我的代码时,我的 'file or directory.'
就会出问题FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\atlgwc16\PetImages/Dog'
老实说,我不知道 Google 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
正在遵循问题中引用的教程:
https://pythonprogramming.net/loading-custom-data-deep-learning-python-tensorflow-keras/
由于您使用的是 Google Colab,因此您可以将狗和猫图像的 Kaggle 数据集上传到 Google 云端硬盘。请参阅 Google 提供的 Google Colab Jupyter notebook,其中解释了如何执行此操作:
https://colab.research.google.com/notebooks/io.ipynb#scrollTo=u22w3BFiOveA
然后您可以从您的 Google Drive 访问文件(在本例中,是将其上传到 Google Drive 后的训练集),这与在您的计算机上访问本地文件的方式非常相似.
这是上面link中提供的例子:
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 笔记本。笔记本中的每个单元格都使用相同的 'session'。因此,如果您在一个单元格中导入 Python 模块,则可以在下一个单元格中使用它。就是这么神奇。
看起来像这样:
[单元格 1]
from google.colab import drive
drive.mount('/content/gdrive')
然后您将允许 Google Colab 访问您的 Google 驱动器。
[单元格 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.
上面发布的 link编码愉快。
我为自己做的,它对我有用。 我使用本地驱动器(如硬盘)中的数据集。 注意:您的数据集文件夹必须是 zip 格式。 首先,按照我的方法,您将从本地 drive.I 使用 google colab 访问您的数据集。首先,在 google Colab 中创建一个 Jupyter notebook,然后 运行 逐步编写以下代码:
第一步:运行在你的笔记本中输入以下代码,并从你的硬盘或本地驱动器上传你的数据集
from google.Colab import files
uploaded = files.upload()
当过程 100% 完成并执行第二步时:
第二步: 复制并运行下面的代码,这一步将解压数据集
import zipfile
import io
zf = zipfile.ZipFile(io.BytesIO(uploaded['DogVsCat.zip']), "r")
zf.extractall()
第三步:运行导入所有需要的库的代码
import numpy as np
import os
import cv2
import matplotlib.pyplot as plt
这将为您导入所有必需的库。
第四步: specify the path 指定路径执行以下步骤: 拳头:check the image for more ease of your
在左下角的文件夹图标上,单击突出显示的文件夹,您将看到解压缩的数据集,在我的例子中,我的数据集是“DogVsCat”, 注意:这里会看到zip和unzip两种数据集,你复制解压数据的路径。 右键单击它并从中复制路径,然后 运行 下面的代码:
DIRECTORY ='/content/DogVsCats'
CATEGORIES = ['cats', 'dogs']
注意:请在DIRECTORY(这个目录是我的路径)路径中添加你的路径,而不是我的路径。再次 运行 代码: 注意:请在 CATEGORIES 中添加您自己的文件夹名称,而不是我的文件夹名称以获取更多信息,请参见图片: my dataset structure 最后创建火车数据
第五步:
data = []
for category in CATEGORIES:
path = os.path.join(DIRECTORY, category)
for img in os.listdir(path):
img_path = os.path.join(path, img)
label = CATEGORIES.index(category)
arr = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
new_arr = cv2.resize(arr, (60, 60))
data.append([new_arr, label])
第六步:
打印数据: 运行下面的代码给你看
data
第七步:洗牌你的数据:
import random
random.shuffle(data)
八步: 指定用于训练模型的特征和标签
X = []
y = []
for features, label in data:
X.append(features)
y.append(label)
X = np.array(X)
y = np.array(y)
第九步:打印特征
X
第十步:打印标签
y
请注意,由于时间不够,我无法与您分享所有代码。
注意:为了更清晰,请查看我的代码图片: pic1-Of-My-Code pic2-of-my-code