python - 无法加载在 Google Colaboratory 中创建的模型

标签 python keras google-colaboratory

我在 Google Colab 中使用 Keras 训练了 CNN,但是下载模型后,我无法在本地系统 (Windows) 中加载 hdf5 文件。

我以前也这样做过,从来没有遇到过任何问题。这是我收到的错误。

Traceback (most recent call last):
  File "D:/Work/binary-face-recognition/predict-test.py", line 27, in <module>
    model = load_model('model.hdf5')
  File "C:\Users\PARTHA.N\AppData\Local\Continuum\anaconda3\envs\face\lib\site-packages\keras\engine\saving.py", line 417, in load_model
    f = h5dict(filepath, 'r')
  File "C:\Users\PARTHA.N\AppData\Local\Continuum\anaconda3\envs\face\lib\site-packages\keras\utils\io_utils.py", line 186, in __init__
    self.data = h5py.File(path, mode=mode)
  File "C:\Users\PARTHA.N\AppData\Local\Continuum\anaconda3\envs\face\lib\site-packages\h5py\_hl\files.py", line 312, in __init__
    fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
  File "C:\Users\PARTHA.N\AppData\Local\Continuum\anaconda3\envs\face\lib\site-packages\h5py\_hl\files.py", line 142, in make_fid
    fid = h5f.open(name, flags, fapl=fapl)
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py\h5f.pyx", line 78, in h5py.h5f.open
OSError: Unable to open file (truncated file: eof = 38486016, sblock->base_addr = 0, stored_eof = 72708080)

为了加载模型,我正在执行基本 load_model 调用

from keras.models import load_model
model = load_model('model.hdf5')

要从 Google Colab 保存和下载模型,我使用以下代码片段。

# save model
model.save('model.hdf5')

# download model
from google.colab import files
files.download('model.hdf5')

下载文件后,我在 Google colb 的 files.download() 行收到以下错误。

----------------------------------------
Exception happened during processing of request from ('::ffff:172.28.0.1', 47132, 0, 0)
Traceback (most recent call last):
  File "/usr/lib/python3.6/socketserver.py", line 317, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python3.6/socketserver.py", line 348, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python3.6/socketserver.py", line 361, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.6/socketserver.py", line 721, in __init__
    self.handle()
  File "/usr/lib/python3.6/http/server.py", line 418, in handle
    self.handle_one_request()
  File "/usr/lib/python3.6/http/server.py", line 406, in handle_one_request
    method()
  File "/usr/lib/python3.6/http/server.py", line 639, in do_GET
    self.copyfile(f, self.wfile)
  File "/usr/lib/python3.6/http/server.py", line 800, in copyfile
    shutil.copyfileobj(source, outputfile)
  File "/usr/lib/python3.6/shutil.py", line 82, in copyfileobj
    fdst.write(buf)
  File "/usr/lib/python3.6/socketserver.py", line 800, in write
    self._sock.sendall(b)
ConnectionResetError: [Errno 104] Connection reset by peer
----------------------------------------

最佳答案

我确实遇到了同样的问题,我所做的不同之处是从文件管理器下载文件,同样工作得很好。

点击屏幕左上角 Colab 标志下方的小箭头,看起来像这样

this

然后您需要单击文件并导航到所需的文件,右键单击并下载所需的文件。这对我来说效果更好,并且没有连接错误,这是 files.download 方法中的情况

files

关于python - 无法加载在 Google Colaboratory 中创建的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54511066/

相关文章:

python - 如何使用 pandas 写入 Excel 文档的中间

python - 欧拉计划 3

python - 如何使用 mpl_toolkits.axes_grid1.inset_locator.mark_inset 控制 zorder 和裁剪?

deep-learning - 使用 Keras 合并两个不同网络的特征表示

python - Elmo 是词嵌入还是句子嵌入?

来自云托管 jupyter 的 pandas.read_clipboard?

python - 如何在 Google Colab 的 TPU 上训练 Keras 模型?我需要更改我的模型配置吗?(tinyYolo-v3)

python - 在多列 Pandas 上应用 lambda 行

python - 您可以在本地计算机上运行 Google Colab 吗?

python - 加载 TensorFlow 嵌入模型