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

时间:2019-02-04 06:24:02

标签: python keras google-colaboratory

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

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

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')

下载文件后,在files.download()行的Google colb中出现以下错误。

----------------------------------------
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
----------------------------------------

3 个答案:

答案 0 :(得分:1)

我确实遇到了同样的问题,不同的是我从文件管理器中下载了文件,并且同样工作正常。

单击屏幕左上角位于colab标志下方的小箭头,类似于

this

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

files

答案 1 :(得分:0)

好像互联网连接出现问题。我切换到另一个WiFi,它工作正常。

这个问题现在听起来很愚蠢,但请保留下来,并回答这个问题,希望对遇到相同问题的人有所帮助。

答案 2 :(得分:0)

来自description here

  

“对等方重置连接”是TCP / IP的等同于   电话回钩。礼貌不仅仅是不回信,   一挂。但这不是真正的FIN-ACK期望   礼貌的TCP / IP会话