I am trying to build an AI image classifier in Python using a youtube guide. When I run my program (unfinished) it does not open up the image

40 views Asked by At

I am trying to build an AI image classifier using a youtube guide for a school project. Here is the link: https://www.youtube.com/watch?v=oEKg_jiV1Ng&t=727s

At this stage, I am not done, but when I run my main.py I get the following error:

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'

I also get this, but I don't think it matters much to the project personally. (Maybe it does, I just assume since it works in the video it should still work as the video is recent.) :

:\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)

On the video, when he runs it at this stage it prints the keys and runs fine.

Here is my full code:

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

I tried: Copy and pasting the youtubers code and using exactly that, swapping out paths, Changing folders, Changing image names, changing how its set up, googling errors, etc. I know the image isn't a directory, so I understand that, I just don't get what to change. Any feedback would be greatly appreciated.

1

There are 1 answers

0
Ritik Dutta On

In the video, the person is using Linux. From your code, I can see you are using Windows. The path in Linux and Windows works different ways, so you have to tweak the folder path that works with Windows. In Windows the path works with backslash '\', and in Linux the path works with forward slash '/'.