Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x FileNotFoundError:[Errno 2]没有这样的文件或目录——即使我使用的是完整路径_Python 3.x_Keras_Google Colaboratory - Fatal编程技术网

Python 3.x FileNotFoundError:[Errno 2]没有这样的文件或目录——即使我使用的是完整路径

Python 3.x FileNotFoundError:[Errno 2]没有这样的文件或目录——即使我使用的是完整路径,python-3.x,keras,google-colaboratory,Python 3.x,Keras,Google Colaboratory,我在GoogleColab中使用Python3。我一直收到错误FileNotFoundError:[Errno 2]没有这样的文件或目录,即使我非常确定我放置的路径是正确的,并验证文件夹是否存在 我最初使用了pip安装keras和pip安装tensorflow。我已尝试使用\而不是\作为路径 # Importing the Keras libraries and packages from keras.models import Sequential from keras.layers impo

我在GoogleColab中使用Python3。我一直收到错误FileNotFoundError:[Errno 2]没有这样的文件或目录,即使我非常确定我放置的路径是正确的,并验证文件夹是否存在

我最初使用了pip安装keras和pip安装tensorflow。我已尝试使用\而不是\作为路径

# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
# Initialising the CNN
classifier = Sequential()
# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Step 3 - Flattening
classifier.add(Flatten())
# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Part 2 - Fitting the CNN to the images
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('E:\Project\dataset\training_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('E:\Project\dataset\test_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
classifier.fit_generator(training_set,
steps_per_epoch = 8000,
epochs = 25,
validation_data = test_set,
validation_steps = 2000)
# Part 3 - Making new predictions
import numpy as np
from keras.preprocessing import image
test_image = image.load_img('E:\Project\dataset\single_prediction\cat_or_dog_1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
    prediction = 'dog'
else:
    prediction = 'cat'

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-7-757ca18e13d5> in <module>()
     30 target_size = (64, 64),
     31 batch_size = 32,
---> 32 class_mode = 'binary')
     33 test_set = test_datagen.flow_from_directory('E:\Project\dataset\test_set',
     34 target_size = (64, 64),

1 frames
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/directory_iterator.py in __init__(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation, dtype)
    104         if not classes:
    105             classes = []
--> 106             for subdir in sorted(os.listdir(directory)):
    107                 if os.path.isdir(os.path.join(directory, subdir)):
    108                     classes.append(subdir)

FileNotFoundError: [Errno 2] No such file or directory: 'E:\\Project\\dataset\training_set'

我认为你不能直接从你的本地机器上访问Google colab笔记本中的文件。您必须通过Google Drive或直接从本地计算机上传文件到Colab


有关更多详细信息,请参阅。

将Google drive装载到Goolge Colab,并保存Google drive中所需的所有文件。然后将所有文件的位置更改为保存在Google Drive中的文件