[2] No such file or directory如何解决?

复现《deep learning for computer vision with python》第二本的第9章的代码时出现问题

这是配置文件代码:

IMAGES_PATH = "../datasets/kaggle_dogs_vs_cats/train"

NUM_CLASSES = 2
NUM_VAL_IMAGES = 1250 * NUM_CLASSES
NUM_TEST_IMAGES = 1250 * NUM_CLASSES

TRAIN_HDF5 = "..datasets/kaggle_dogs_vs_cats/hdf5/train.hdf5"
VAL_HDF5 = "datasets/kaggle_dogs_vs_cats/hdf5/val.hdf5"
TEST_HDF5 = "..datasets/kaggle_dogs_vs_cats/hdf5/test.hdf5"

MODEL_PATH = "output/alexnet_dogs_vs_cats.model"
DATASET_MEAN = "output/dogs_vs_cats_mean.json"
OUTPUT_PATH = "output"



```、

这是运行程序代码:

```python
from config import dogs_vs_cats_config as config
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from pyimagesearch.preprocessing import AspectAwarePreprocessor
from pyimagesearch.io import HDF5DatasetWriter
from imutils import paths
import numpy as np
import progressbar
import json
import cv2
import os

trainPaths = list(paths.list_images(config.IMAGES_PATH))
trainLabels = [p.split(os.path.sep)[2].split(".")[0]
               for p in trainPaths]
le = LabelEncoder()
trainLabels = le.fit_transform(trainLabels)

split = train_test_split(trainPaths, trainLabels,
                         test_size=config.NUM_TEST_IMAGES, stratify=trainLabels,
                         random_state=42)
(trainPaths, testPaths, trainLabels, testLabels) = split

split = train_test_split(trainPaths, trainLabels,
                         test_size=config.NUM_VAL_IMAGES, stratify=trainLabels,
                         random_state=42)
(trainPaths, valPaths, trainLabels, valLabels) = split

datasets = [("train", trainPaths, trainLabels, config.TRAIN_HDF5),
            ("val", valPaths, valLabels, config.VAL_HDF5),
            ("test", testPaths, trainLabels, config.TEST_HDF5)]

aap = AspectAwarePreprocessor(256, 256)
(R, G, B) = ([], [], [])

for (dType, paths, labels, outputPath) in datasets:
    print("[INFO] building {}...".format(outputPath))
    writer = HDF5DatasetWriter((len(paths), 256, 256, 3), outputPath)

    widgets = ["Building Dataset: ", progressbar.Percentage(), " ",
               progressbar.Bar(), " ", progressbar.ETA()]
    pbar = progressbar.ProgressBar(maxval=len(paths),
                                   widgets=widgets).start()

    for (i, (path, label)) in enumerate(zip(paths, labels)):
        image = cv2.imread(path)
        image = aap.preprocess(image)
        if dType == "train":
            (b, g, r) = cv2.mean(image)[:3]
            R.append(r)
            G.append(g)
            B.append(b)
        writer.add([image], [label])
        pbar.update(i)

    pbar.finish()
    writer.close()

print("[INFO] serializing means...")
D = {"R": np.mean(R), "G": np.mean(G), "B": np.mean(B)}
f = open(config.DATASET_MEAN, "w")
f.write(json.dumps(D))
f.close()

然后在terminal中运行,报错python.exe: can't open file 'build_dogs_vs_cats.py': [Errno 2] No such file or directory
我怀疑是我还是文件路径上出了错。配置文件的代码也是按照书上写的。我也把数据集挪到该project下了。

运行程序就是 build_dogs_vs_cats.py这个文件。
这是文件结构,我把数据集也放到项目的目录里了。

img

请看👉 :解决模型加载的路径问题“No such file or directory”

build_dogs_vs_cats.py你在哪里有调用这个文件?

可以直接给你远程解决,安装向日葵

看你这个代码,其中IMAGES_PATH、TRAIN_HDF5、VAL_HDF5、TEST_HDF5 这四个的路径有错误的,有不一致的,调整一下应该就好。

是在terminal执行的python build_dogs_vs_cats.py吗?如果是的话,确认一下terminal 当前路径下有build_dogs_vs_cats.py文件。

找不到文件,看看是不是文件不存在或者文件名写错了