conv-neural-network - 访问 google colab 上的文件
问题描述
在通过运行安装驱动器后,我正在使用 Google Colaboratory IPython 进行样式转换:
from google.colab import drive
drive.mount('/drive')
它已安装,所以我尝试 cd 进入一个目录,显示 pwd 和 ls 但它没有显示正确的 pwd
!cd "/content/drive/My Drive/"
!pwd
!ls
但它不会 cd 进入给定的目录,它只会 cd 进入 '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:[Errno 2] 没有这样的文件或目录:'content/drive/My Drive/uche.jpg'
解决方案
简短回答:要更改工作目录,请使用%cd
oros.chdir
而不是!cd
.
背后的故事是!
命令在一个子shell中执行,它有独立于运行代码的Python进程的工作目录。但是,您想要的是更改 Python 进程的工作目录。这就是os.chdir
将要做的,并且%cd
是在笔记本中工作的方便别名。
放在一起,我想你想写:
from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/My\ Drive
推荐阅读
- xcode - 命令 PhaseScriptExecution 因 Alamofire 的非零退出代码而失败
- python - 是否有 Python 函数可以通过同时操作数组中的许多项来解决或支持?
- java - 制作一个 Spring Boot 应用程序以从现有的 oracle 数据库加载数据?
- api - '指定的值不能转换为 ReleaseStartMetadata 类型。使用 powershell 触发 VSTS 发布 API 时显示的消息
- javascript - 导入 google api 字体以响应“预加载”属性
- c# - 验证选择是一个范围
- odoo - 如何以在 Odoo 服务器上上传的格式下载文件?
- java - 如何使用 AWS lambda 授权 gmail API
- c# - 类型约束,类型存储在变量中
- php - 如何在没有 sql 查询的情况下获取唯一数据