我目前正在学习 TensorFlow 课程,作为我作业的一部分,我将编写几行代码。首先在 Google Colab 上的临时文件夹中给出一个 zip 文件,我必须创建一个目录和子目录 TRAINING 和 TESTING。然后我必须定义一个函数,它将源目录和目标目录作为变量并将文件复制到给定 split_size 的文件中。我最初在定义函数时遇到了问题,但有人帮助了我。然而,当我实际尝试运行它时,出现错误:
FileNotFoundError: [Errno 2] No such file or directory: 'cat.2275.jpg'
由于我不知道如何修复它,我将向您展示我的代码行,如果您能帮助我诊断问题,我将不胜感激。我想我在制作目录时犯了一个错误。
path_cats_and_dogs = f"{getcwd()}/../tmp2/cats-and-dogs.zip"
shutil.rmtree('/tmp')
local_zip = path_cats_and_dogs
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp')
zip_ref.close()
# Use os.mkdir to create your directories
# You will need a directory for cats-v-dogs, and subdirectories for training
# and testing. These in turn will need subdirectories for 'cats' and 'dogs'
try:
base_dir = f"{getcwd()}/../tmp2/cats-and-dogs.zip"
CAT_SOURCE_DIR = os.path.join(base_dir, 'cat')
DOG_SOURCE_DIR = os.path.join(base_dir, 'dog')
train_dir = os.path.join(base_dir, 'training')
validation_dir = os.path.join(base_dir, 'testing')
TRAINING_CATS_DIR = os.path.join(train_dir,'cats')
TRAINING_DOGS_DIR = os.path.join(train_dir, 'dogs')
TESTING_CATS_DIR = os.path.join(validation_dir, 'cats')
TESTING_DOGS_DIR = os.path.join(validation_dir, 'dogs')
except OSError:
pass
# Write a python function called split_data which takes
# a SOURCE directory containing the files
# a TRAINING directory that a portion of the files will be copied to
# a TESTING directory that a portion of the files will be copie to
# a SPLIT SIZE to determine the portion
# The files should also be randomized, so that the training set is a random
# X% of the files, and the test set is the remaining files
# SO, for example, if SOURCE is PetImages/Cat, and SPLIT SIZE is .9
# Then 90% of the images in PetImages/Cat will be copied to the TRAINING dir
# and 10% of the images will be copied to the TESTING dir
# Also -- All images should be checked, and if they have a zero file length,
# they will not be copied over
#
# os.listdir(DIRECTORY) gives you a listing of the contents of that directory
# os.path.getsize(PATH) gives you the size of the file
# copyfile(source, destination) copies a file from source to destination
# random.sample(list, len(list)) shuffles a list
def split_data(SOURCE, TRAINING, TESTING, SPLIT_SIZE):
source_files = [f for f in os.listdir(SOURCE) if os.path.getsize(f) > 0]
random.shuffle(source_files)
total = len(source_files)
to_training = source_files[0: int(total * SPLIT_SIZE)]
to_test = source_files[int(total * SPLIT_SIZE):]
for f in to_training:
copyfile(os.path.join(SOURCE, f), TRAINING)
for f in to_test:
copyfile(os.path.join(SOURCE, f), TESTING)
assert len(source_files) == len(to_training) + len(to_test)
CAT_SOURCE_DIR = "/tmp/PetImages/Cat/"
TRAINING_CATS_DIR = "/tmp/cats-v-dogs/training/cats/"
TESTING_CATS_DIR = "/tmp/cats-v-dogs/testing/cats/"
DOG_SOURCE_DIR = "/tmp/PetImages/Dog/"
TRAINING_DOGS_DIR = "/tmp/cats-v-dogs/training/dogs/"
TESTING_DOGS_DIR = "/tmp/cats-v-dogs/testing/dogs/"
split_size = .9
split_data(CAT_SOURCE_DIR, TRAINING_CATS_DIR, TESTING_CATS_DIR, split_size)
split_data(DOG_SOURCE_DIR, TRAINING_DOGS_DIR, TESTING_DOGS_DIR, split_size)
当我运行这些代码时,我收到此错误:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-32-c5dad92d1bc6> in <module>
36
37 split_size = .9
---> 38 split_data(CAT_SOURCE_DIR, TRAINING_CATS_DIR, TESTING_CATS_DIR, split_size)
39 split_data(DOG_SOURCE_DIR, TRAINING_DOGS_DIR, TESTING_DOGS_DIR, split_size)
<ipython-input-32-c5dad92d1bc6> in split_data(SOURCE, TRAINING, TESTING, SPLIT_SIZE)
17 # random.sample(list, len(list)) shuffles a list
18 def split_data(SOURCE, TRAINING, TESTING, SPLIT_SIZE):
---> 19 source_files = [f for f in os.listdir(SOURCE) if os.path.getsize(f) > 0]
20 random.shuffle(source_files)
21 total = len(source_files)
<ipython-input-32-c5dad92d1bc6> in <listcomp>(.0)
17 # random.sample(list, len(list)) shuffles a list
18 def split_data(SOURCE, TRAINING, TESTING, SPLIT_SIZE):
---> 19 source_files = [f for f in os.listdir(SOURCE) if os.path.getsize(f) > 0]
20 random.shuffle(source_files)
21 total = len(source_files)
/usr/lib/python3.6/genericpath.py in getsize(filename)
48 def getsize(filename):
49 """Return the size of a file, reported by os.stat()."""
---> 50 return os.stat(filename).st_size
51
52
FileNotFoundError: [Errno 2] No such file or directory: 'cat.2275.jpg'
我在这个论坛上查看过类似的帖子,人们建议给出的路径是相对路径而不是直接路径。我尝试在我的情况下对其进行调整,但要么无法正确执行,要么无法正常工作。你能帮我找出问题吗?如果是定义路径的问题还是其他什么?
非常感谢, 乔安娜
PS:所有的# 都是我的作业说明。