【问题标题】:Google Colab can't access drive contentGoogle Colab 无法访问云端硬盘内容
【发布时间】:2019-06-13 14:39:32
【问题描述】:

即使我将我的 Google Drive(以及其中的数据集)定义为 google colab,但是当我运行我的代码时,我给出了这个错误:FileNotFoundError: [Errno 2] No such file or directory: 'content/drive/My Drive /....

我已经在 google colab 中定义了 google drive,我可以通过 google colab 访问它,但是当我运行我的代码时,我给出了这个错误

from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
model=Sequential()
model.add(Convolution2D(32,3,3,input_shape=(64,64,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Convolution2D(32,3,3,activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(output_dim=128,activation='relu'))
model.add(Dense(output_dim=1,activation='sigmoid'))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])

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(
    directory='content/drive/My Drive/Convolutional_Neural_Networks/dataset/training_set',
    target_size=(64,64),
    batch_size=32,
    class_mode='binary')
test_set=test_datagen.flow_from_directory(
    directory='content/drive/My Drive/Convolutional_Neural_Networks/dataset/test_set',
    target_size=(64,64),
    batch_size=32,
    class_mode='binary')

#train
model.fit_generator(
    training_set,
    samples_per_epoch=8000,
    nb_epoch=2,
    validation_data=test_set,
    nb_val_samples=1000)

import numpy as np
from keras.preprocessing import image
test_image=image.load_img('sunn.jpg',target_size=(64,64))
test_image=image.img_to_array(test_image)
test_image=np.expand_dims(test_image,axis=0)
result=model.predict(test_image)
training_set.class_indices
if result[0][0] >= 0.5:
    prediction='dog'
else:
    prediction='cat'
print(prediction)

【问题讨论】:

    标签: keras deep-learning conv-neural-network google-colaboratory


    【解决方案1】:

    挂载后,进入数据集文件夹。

    cd content/drive/My Drive/Convolutional_Neural_Networks/dataset/
    

    不要使用 !。 然后将你的目录设置为./training_set

    【讨论】:

    • 您能解释一下为什么会这样吗?我想 !为了执行 cd 之类的 bash 命令是必要的?
    • 我遇到了同样的问题,所以尝试删除 !这有效。如果你注意到了,你只能在一个单元格中放上 bash 命令。我相信这就是 colab 的 jupyter notebook 的工作原理。
    • 是的,您可以在每个单元格中使用一个 bash 命令,而无需使用 !。
    【解决方案2】:

    我认为您在 /content/drive... 路径中缺少前导 /

    通常通过以下方式挂载您的云端硬盘文件

    from google.colab import drive
    drive.mount('/content/drive')
    

    https://colab.research.google.com/notebooks/io.ipynb#scrollTo=u22w3BFiOveA

    【讨论】:

      【解决方案3】:

      我一直在尝试,对于那些好奇的人来说,我无法将目录中的流与谷歌驱动器内的文件夹一起使用。协作文件环境不读取路径并给出“文件夹不存在”错误。我一直在尝试解决问题并搜索堆栈,类似的问题已发布在这里 Google collaborative 和这里 Deep learnin on Google Colab: loading large image dataset is very long, how to accelerate the process? ,没有有效的解决方案,出于某种原因,许多人对提出问题的人投了反对票。

      我发现在 google colab 中读取 20k 张图片的唯一解决方案是上传它们然后处理它们,这样做浪费了两个可悲的小时。这是有道理的,谷歌用 ids 识别驱动器内的东西,从目录流要求它同时识别数据集和具有文件夹绝对路径的类,与谷歌驱动器识别方法不兼容。替代方案可能是使用谷歌云环境而不是我想并付费。我们免费获得了很多。这是我新手对情况的理解,如有错误请指正。

      edit1:我能够在 google collab 上使用目录中的流,google 确实也使用路径识别事物,问题是如果你使用 os.getcwd(),它不能正常工作,如果你使用它会告诉您当前工作目录是“/content”,而实际上是“/content/drive/My Drive/foldersinsideyourdrive/...../folderthathasyourcollabnotebook/。如果您在 traingenerator 中更改路径,使其包含这个设置,并忽略操作系统,它可以工作。但是,即使使用目录中的流,内存也有问题,无论如何都无法训练我的 cnn,但可能只是发生在我身上。

      【讨论】:

        【解决方案4】:

        从 google.colab 导入驱动器 drive.mount('/content/drive')

        使用上面的代码,您可以在 colab 中加载您的驱动器, 何时加载图像使用:

        directory='drive/My Drive/Convolutional_Neural_Networks/dataset/test_set',
        

        不是这个:

        directory='content/drive/My Drive/Convolutional_Neural_Networks/dataset/test_set',
        

        对于 keras imagedatagenerator 数据集 strcut:

        
        

        【讨论】:

          【解决方案5】:

          所以,我从 Colab 的默认命令开始

          from google.colab import drive
          drive.mount('/gdrive', force_remount=True)
          

          我所做的主要改变就在这里

          img_width, img_height = 64, 64
          train_data_dir = '/gdrive/My Drive/Colab Notebooks/dataset/training_set'
          validation_data_dir = '/gdrive/My Drive/Colab Notebooks/dataset/test_set'
          
          from tensorflow.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)
          
          train_generator = train_datagen.flow_from_directory(
           train_data_dir,
           target_size=(64, 64),
           batch_size=32,
           class_mode='binary')
          
          validation_generator = test_datagen.flow_from_directory(
           validation_data_dir,
           target_size=(64, 64),
           batch_size=32,
           class_mode='binary')
          
          classifier.fit_generator(
           train_generator,
           steps_per_epoch=8000, # Number of images in train set
           epochs=25,
           validation_data=validation_generator,
           validation_steps=2000)
          

          这对我有用,我希望这对某人有所帮助。 附:忽略缩进。

          【讨论】:

            【解决方案6】:

            由于某种原因,您必须 %cd 进入您的 google 驱动器文件夹,然后执行您的代码才能访问驱动器中的文件或在那里写入文件。

            首先安装你的谷歌驱动器:

            from google.colab import drive
            drive.mount('/gdrive', force_remount=True)
            

            然后 cd 进入你的谷歌驱动器,然后运行你的代码:

            %cd /content/drive/My\ Drive/
            directory='./Convolutional_Neural_Networks/dataset/training_set'
            

            【讨论】:

              【解决方案7】:

              尝试删除“内容”,经过 1 小时的故障排除后,它对我有用

              cd drive/My Drive/dissertation
              

              【讨论】:

                猜你喜欢
                • 2017-07-12
                • 2019-03-23
                • 2021-08-12
                • 2012-07-12
                • 1970-01-01
                • 2017-09-11
                • 1970-01-01
                • 1970-01-01
                • 2021-10-09
                相关资源
                最近更新 更多