访问Google Colab上的文件

时间:2018-12-19 15:17:22

标签: conv-neural-network google-colaboratory style-transfer

在通过运行以下命令安装驱动器之后,我正在使用Google Colaboratory IPython进行样式转换:

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

它已挂载,因此我尝试将CD插入目录,显示pwd和ls,但它没有显示正确的pwd

!cd "/content/drive/My Drive/"
!pwd
!ls

但不会将其安装到给定目录中,而仅将其安装到'content /'中

当我尝试使用以下代码中的“ load_image()函数访问某些图像时,

def load_image(img_path, max_size=400, Shape=None):
    image = Image.open(img_path).convert('RGB')
    if max(image.size) > max_size:
        size = max_size
    else:
        size = max(image.size)

    if shape is not None:
        size = shape

    in_transform = transforms.Compose([transforms.Resize(size),
                    transforms.ToTensor(),
                    transforms.Normalize((0.485, 0.456, 0.406), 
                                         (0.229, 0.224, 0.225))])

    image = in_transform(image)[:3,:,:].unsqueeze(0)

    return image
#load image content
content = load_image('content/drive/My Drive/uche.jpg')
style = load_image('content/drive/My Drive/uche.jpg')

但是当我尝试从目录中加载图像时,此代码引发了错误:

FileNotFoundError:[错误2]没有这样的文件或目录:'content / drive / My Drive / uche.jpg'

1 个答案:

答案 0 :(得分:0)

简短答案:要更改工作目录,请使用%cdos.chdir而不是!cd

背后的故事是!命令在子shell中执行,它具有运行代码的Python进程独立的工作目录。但是,您想要的是更改Python进程的工作目录。这就是os.chdir的工作方式,%cd是在笔记本电脑中使用的便捷别名。

把它们放在一起,我想你要写:

from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/My\ Drive