Unable to upload local files using Google Colab

I am getting error when trying to upload file to 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
---> 64 uploaded = files.upload()
     66 for fn in uploaded.keys():

My Browser: Google Chrome, Version 83.0.4103.116 (Official Build) (64-bit) (on Windows 10)

I found two questions asked previously. See below. However, neither of the solutions worked for me.

Here's the image of my Chrome settings: enter image description here

1 answer

  • answered 2020-07-05 16:16 PeterV

    I found out what I was doing wrong. I was trying to execute bunch of scripts in same cell in Colab. When it came to the files.upload(), it was causing the issue. I moved that segment into another cell and executed it. It worked fine. Looks like Colab does not like it when you do this.

    Here's code snippet I was referring to:

    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)
      if classes[0]>0.5:
        print(fn + " is a human")
        print(fn + " is a horse")