我正在尝试使用 YouTube 指南构建人工智能图像分类器。当我运行我的程序(未完成)时,它不会打开图像。 (Python)

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

我正在尝试使用 YouTube 指南为学校项目构建人工智能图像分类器。这是链接:https://www.youtube.com/watch?v=oEKg_jiV1Ng&t=727s

在这个阶段,我还没有完成,但是当我运行 main.py 时,我收到以下错误:

Traceback (most recent call last):
  File "c:\xxx\xx\xx\xx\newai\main.py", line 19, in <module>
    for img_path in os.listdir(os.path.join(dir_, category)):
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:/xx/xx/xx/xx/newai\\Data\\Blue-Squares\\BlueSquare (1).jpg'

我也明白这一点,但我认为这对项目个人来说并不重要。 (也许是这样,我只是假设因为它在视频中有效,所以它应该仍然有效,因为视频是最近的。):

:\XX\XX\XX\XX\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
C:\XX\XX\XX\XX\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)

在视频中,当他在这个阶段运行它时,它会打印按键并且运行良好。

这是我的完整代码:

from img2vec_pytorch import Img2Vec
import os
from PIL import Image

# prepare data

img2vec = Img2Vec()

data_dir = 'C:/XX/XX/XX/XX/newai'
train_dir = os.path.join(data_dir, r'Data', r'Blue-Squares')
val_dir = os.path.join(data_dir, r'Data', r'Red-Triangles')

data = {}

for j, dir_ in enumerate([train_dir, val_dir]):
    features = []
    labels = []
    for category in os.listdir(dir_):
        for img_path in os.listdir(os.path.join(dir_, category)):
            img_path_ = os.path.join(dir_, category, img_path)
            img = Image.open(img_path_)

            img_features = img2vec.get_vec(img)

            features.append(img_features)
            labels.append(category)

    data[['training_data', 'validation_data'][j]] = features
    data[['training_labels', 'validation_labels'][j]] = labels


print(data.keys())

# train model

# test performance

# save the model

我尝试过: 复制并粘贴 youtubers 代码并完全使用该代码,交换路径,更改文件夹,更改图像名称,更改其设置方式,谷歌搜索错误等。我知道图像不是目录,所以我明白,我只是不知道要改变什么。任何反馈将不胜感激。

python scikit-learn pytorch youtube python-imaging-library
1个回答
0
投票

在视频中,这个人使用的是Linux操作系统,从你的代码中我可以看到你使用的是Windows,Linux和Windows中的路径工作方式不同,所以你必须调整适用于Windows的文件夹路径,例如在Windows中路径使用反斜杠“”,在 Linux 中路径使用正斜杠“/”。

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