tensorflow 文件未找到错误:[Errno 2]没有这样的文件或目录:'/Users/Chris/Downloads/kaggle_original_data\\cat.0.jpg'

7xzttuei  于 2023-10-23  发布在  其他
关注(0)|答案(3)|浏览(103)

我在Windows 10系统上使用TensorFlow 2.3.1(Python 3.8,在Anaconda中)。我尝试运行机器学习脚本,但在存储数据时遇到了一些问题。我想这与Unix/Windows中的斜杠/反斜杠差异有关?谁能给予一点提示来解决这个问题。先谢了。

# The path to the directory where the original
# dataset was uncompressed
original_dataset_dir = '/Users/Chris/Downloads/kaggle_original_data'

# The directory where we will
# store our smaller dataset
base_dir = '/Users/Chris/Downloads/cats_and_dogs_small'
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 validation cat pictures
test_cats_dir = os.path.join(test_dir, 'cats')
os.mkdir(test_cats_dir)

# Directory with our validation 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(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.copyfile(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, fname)
    shutil.copyfile(src, 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, fname)
    shutil.copyfile(src, dst)
    
# Copy next 500 dog images to validation_dogs_dir
fnames = ['dog.{}.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_dogs_dir, fname)
    shutil.copyfile(src, dst)
    
# Copy next 500 dog images to test_dogs_dir
fnames = ['dog.{}.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_dogs_dir, fname)
    shutil.copyfile(src, dst)
pqwbnv8z

pqwbnv8z1#

Linux使用正斜杠表示路径,Windows使用反斜杠表示路径
但是,反斜杠是Python的转义字符,因此在处理路径时实际上必须将其加倍

>>> import os
>>> os.getcwd()
'C:\\Python37'

您正在以Linux设置original_dataset_dirbase_dir的路径,而它们应该是这样的:* 搜索“C:\Users\Chris\Downloads\kaggle_original_data'**

wtlkbnrh

wtlkbnrh2#

尝试在original_dataset_dirbase_dir上使用os.path.abspath

original_dataset_dir = os.path.abspath(original_dataset_dir)
base_dir = os.path.abspath(base_dir)
3lxsmp7m

3lxsmp7m3#

import os, shutil 

original_dataset_dir = 'C:\\Users\\Aga\\Downloads\\kagglecatsanddogs_5340\\PetImages'
cat_dir = original_dataset_dir + '\\Cat'
dog_dir = original_dataset_dir + '\\Dog'

base_dir='C:\\Users\\Aga\\Downloads\\cats_and_dogs_small'
os.mkdir(base_dir)

train_dir=os.path.join(base_dir, 'train') #eğitim setinin dizini
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)

train_cats_dir=os.path.join(train_dir, 'cats')
os.mkdir(train_cats_dir)

train_dogs_dir= os.path.join(train_dir, 'dogs') 
os.mkdir(train_dogs_dir)

validation_cats_dir=os.path.join(validation_dir, 'cats')
os.mkdir(validation_cats_dir)

validation_dogs_dir=os.path.join(validation_dir, 'dogs')
os.mkdir(validation_dogs_dir)

test_cats_dir=os.path.join(test_dir, 'cats')
os.mkdir(test_cats_dir)

test_dogs_dir=os.path.join(test_dir, 'dogs')
os.mkdir(test_dogs_dir)

fnames = ['{}.jpg'.format(i) for i in range(1000)]
for fname in fnames:
    src = os.path.join(cat_dir, fname)
    dst = os.path.join(train_cats_dir, fname)
    shutil.copyfile(src, dst)

fnames =['{}.jpg'. format(i) for i in range (1000, 1500)]
for fname in fnames:
    src=os.path.join(cat_dir, fname)
    dst=os.path.join(validation_cats_dir, fname)
    shutil.copyfile(src,dst)    
    
fnames =['{}.jpg'. format(i) for i in range (1500, 2000)]
for fname in fnames:
    src=os.path.join(cat_dir, fname)
    dst=os.path.join(test_cats_dir, fname)
    shutil.copyfile(src,dst)    
    
    
fnames =['{}.jpg'. format(i) for i in range (1000)]
for fname in fnames:
    src=os.path.join(dog_dir, fname)
    dst=os.path.join(train_dogs_dir, fname)
    shutil.copyfile(src,dst)
    
fnames =['{}.jpg'. format(i) for i in range (1000, 1500)]
for fname in fnames:
    src=os.path.join(dog_dir, fname)
    dst=os.path.join(validation_dogs_dir, fname)
    shutil.copyfile(src,dst)    
    
fnames =['{}.jpg'. format(i) for i in range (1500, 2000)]
for fname in fnames:
    src=os.path.join(dog_dir, fname)
    dst=os.path.join(test_dogs_dir, fname)
    shutil.copyfile(src,dst)    

#hi, this is how i solved the problem.

相关问题