问题描述
我正在使用视频教程中的文件。一开始,它开始通过将输入图像数据的文件复制到各个文件夹中来传播它们。该代码在教程中有效,但我想知道为什么会出现以下错误:
[Errno 22] 无效参数:'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogkaggle\train\cat.1.jpg'
这是代码。首先它创建目录。(catdogkaggle\train 包含输入图像):
import os,shutil
# The path to the directory where the original
# dataset was uncompressed
original_dataset_dir = 'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogkaggle\train'
# The directory where we will
# store our smaller dataset
base_dir = 'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogkaggle\catORdog'
os.mkdir(base_dir)
# Directories for our training,# validation and test splits
train_dir = os.path.join(base_dir,'train')
os.mkdir(train_dir)
validation_dir = os.path.join(base_dir,'validation')
os.mkdir(validation_dir)
test_dir = os.path.join(base_dir,'test')
os.mkdir(test_dir)
# Directory with our training cat pictures
train_cats_dir = os.path.join(train_dir,'cats')
os.mkdir(train_cats_dir)
# Directory with our training dog pictures
train_dogs_dir = os.path.join(train_dir,'dogs')
os.mkdir(train_dogs_dir)
# Directory with our validation cat pictures
validation_cats_dir = os.path.join(validation_dir,'cats')
os.mkdir(validation_cats_dir)
# Directory with our validation dog pictures
validation_dogs_dir = os.path.join(validation_dir,'dogs')
os.mkdir(validation_dogs_dir)
# Directory with our test cat pictures
test_cats_dir = os.path.join(test_dir,'cats')
os.mkdir(test_cats_dir)
# Directory with our test dog pictures
test_dogs_dir = os.path.join(test_dir,'dogs')
os.mkdir(test_dogs_dir)
然后将图像复制到最近创建的文件夹中:
# copy first 1000 cat images to train_cats_dir
fnames = ['cat.{}.jpg'.format(i) for i in range(1,1000)]
for fname in fnames:
src = os.path.join(original_dataset_dir,fname)
dst = os.path.join(train_cats_dir,fname)
shutil.copyfile(src,dst)
# copy next 500 cat images to validation_cats_dir
fnames = ['cat.{}.jpg'.format(i) for i in range(1000,1500)]
for fname in fnames:
src = os.path.join(original_dataset_dir,fname)
dst = os.path.join(validation_cats_dir,fname)
shutil.copy(src,dst)
# copy next 500 cat images to test_cats_dir
fnames = ['cat.{}.jpg'.format(i) for i in range(1500,2000)]
for fname in fnames:
src = os.path.join(original_dataset_dir,fname)
dst = os.path.join(test_cats_dir,dst)
# copy first 1000 dog images to train_dogs_dir
fnames = ['dog.{}.jpg'.format(i) for i in range(1000)]
for fname in fnames:
src = os.path.join(original_dataset_dir,fname)
dst = os.path.join(train_dogs_dir,dst)
# copy next 500 dog images to validation_dogs_dir
fnames = ['dog.{}.jpg'.format(i) for i in range(1000,fname)
dst = os.path.join(validation_dogs_dir,dst)
# copy next 500 dog images to test_dogs_dir
fnames = ['dog.{}.jpg'.format(i) for i in range(1500,fname)
dst = os.path.join(test_dogs_dir,dst)
当我运行这部分时,出现以下错误:
OSError: [Errno 22] 无效参数: 'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogkaggle\train\cat.1.jpg'
解决方法
您使用的是 Windows,这就是为什么您需要转义反斜杠或使用 raw strings 来存储文件路径,即:
original_dataset_dir = r'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train'
base_dir = r'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\catORdog'
或
original_dataset_dir = 'D:\\Machine Learning\\Deep Learning\\SRU-deeplearning-workshop-master\\catdogKaggle\\train'
base_dir = 'D:\\Machine Learning\\Deep Learning\\SRU-deeplearning-workshop-master\\catdogKaggle\\catORdog'
,
"\t" 字符具有特殊含义 [TAB]。 要么将所有反斜杠加倍,以逃避单斜杠。 或者您可以使用如下所示的原始字符串。
original_dataset_dir = 'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train'
print(original_dataset_dir)
original_dataset_dir = r'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train'
print(original_dataset_dir)
输出:
D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle rain
D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train