首页 > 解决方案 > 无法使用 Google Colab 上传本地文件

问题描述

尝试将文件上传到 Google Colab 时出现错误。

Upload widget is only available when the cell has been executed in the current browser session.
Please rerun this cell to enable.
---------------------------------------------------------------------------
MessageError                              Traceback (most recent call last)
<ipython-input-18-1bbed4ac709e> in <module>()
     62 from keras.preprocessing import image
     63 
---> 64 uploaded = files.upload()
     65 
     66 for fn in uploaded.keys():

我的浏览器: Google Chrome,版本 83.0.4103.116(官方版本)(64 位)(在 Windows 10 上)

我发现以前问过两个问题。见下文。但是,这两种解决方案都不适合我。

这是我的 Chrome 设置的图像: 在此处输入图像描述

标签: google-chromegoogle-colaboratory

解决方案


我发现我做错了什么。我试图在 Colab 的同一个单元格中执行一堆脚本。当涉及到 files.upload() 时,它导致了问题。我将该段移动到另一个单元格并执行它。它工作得很好。当你这样做时,看起来 Colab 不喜欢它。

这是我指的代码片段:

# RUNNING THE MODEL
print('RUNNING THE MODEL')
import numpy as np
from google.colab import files
from keras.preprocessing import image

uploaded = files.upload()

for fn in uploaded.keys():
 
  # predicting images
  path = '/content/' + fn
  img = image.load_img(path, target_size=(300, 300))
  x = image.img_to_array(img)
  x = np.expand_dims(x, axis=0)

  images = np.vstack([x])
  classes = model.predict(images, batch_size=10)
  print(classes[0])
  if classes[0]>0.5:
    print(fn + " is a human")
  else:
    print(fn + " is a horse")

推荐阅读