keras.models.load_model(“”)gives error on h5f.open()[已关闭]

vmdwslir  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(90)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
22天前关闭
Improve this question
当使用keras.models.load时,它会在h5f.open(name, flags, fapl=fapl)上抛出一个错误,并显示OSError: Unable to open file (file signature not found)
DNNModel文件代码

import random
import numpy as np
import tensorflow as tf
from keras.layers import Dense, Dropout
from keras.models import Sequential
from keras.regularizers import l1, l2
from keras.optimizers import Adam

def set_seeds(seed = 100):
    random.seed(seed)
    np.random.seed(seed)
    tf.random.set_seed(seed)
    
def cw(df):
    c0, c1 = np.bincount(df["dir"])
    w0 = (1/c0) * (len(df)) / 2
    w1 = (1/c1) * (len(df)) / 2
    return {0:w0, 1:w1}

optimizer = Adam(lr = 0.0001)

def create_model(hl = 2, hu = 100, dropout = False, rate = 0.3, regularize = False,
                 reg = l1(0.0005), optimizer = optimizer, input_dim = None):
    if not regularize:
        reg = None
    model = Sequential()
    model.add(Dense(hu, input_dim = input_dim, activity_regularizer = reg ,activation = "relu"))
    if dropout: 
        model.add(Dropout(rate, seed = 100))
    for layer in range(hl):
        model.add(Dense(hu, activation = "relu", activity_regularizer = reg))
        if dropout:
            model.add(Dropout(rate, seed = 100))
    model.add(Dense(1, activation = "sigmoid"))
    model.compile(loss = "binary_crossentropy", optimizer = optimizer, metrics = ["accuracy"])
    return model

字符串
加载模型和参数

# Loading the model
import keras
model = keras.models.load_model("C:/Users/Hussein Ali/Desktop/d/DNNModel.py")


错误类型:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[1], line 3
      1 # Loading the model
      2 import keras
----> 3 model = keras.models.load_model("C:/Users/Hussein Ali/Desktop/d/DNNModel.py")

File C:\anaconda\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File C:\anaconda\lib\site-packages\h5py\_hl\files.py:533, in File.__init__(self, name, mode, driver, libver, userblock_size, swmr, rdcc_nslots, rdcc_nbytes, rdcc_w0, track_order, fs_strategy, fs_persist, fs_threshold, fs_page_size, page_buf_size, min_meta_keep, min_raw_keep, locking, alignment_threshold, alignment_interval, **kwds)
    525     fapl = make_fapl(driver, libver, rdcc_nslots, rdcc_nbytes, rdcc_w0,
    526                      locking, page_buf_size, min_meta_keep, min_raw_keep,
    527                      alignment_threshold=alignment_threshold,
    528                      alignment_interval=alignment_interval,
    529                      **kwds)
    530     fcpl = make_fcpl(track_order=track_order, fs_strategy=fs_strategy,
    531                      fs_persist=fs_persist, fs_threshold=fs_threshold,
    532                      fs_page_size=fs_page_size)
--> 533     fid = make_fid(name, mode, userblock_size, fapl, fcpl, swmr=swmr)
    535 if isinstance(libver, tuple):
    536     self._libver = libver

File C:\anaconda\lib\site-packages\h5py\_hl\files.py:226, in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
    224     if swmr and swmr_support:
    225         flags |= h5f.ACC_SWMR_READ
--> 226     fid = h5f.open(name, flags, fapl=fapl)
    227 elif mode == 'r+':
    228     fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

File h5py\_objects.pyx:54, in h5py._objects.with_phil.wrapper()

File h5py\_objects.pyx:55, in h5py._objects.with_phil.wrapper()

File h5py\h5f.pyx:106, in h5py.h5f.open()

OSError: Unable to open file (file signature not found)

cgh8pdjw

cgh8pdjw1#

model可以表示几个不同的元素。

  • python变量model
  • 模型的权重保存在文件H5

你混淆了这些概念。
如果您想从DNNModel.py加载代码,请使用标准import

import DNNModel

model = DNNModel.create_model()

字符串
但是这给出了新的模型,而模型中没有预训练的weights,并且需要很长的时间来训练它。
因此,我们使用文件H5来保留模型中预训练的weights
稍后我们再次加载它来创建预训练的weights模型,
我们就不用浪费时间再训练它了

models.save_model('my_model.h5')   # save pretrained model in file
model = keras.models.load_model('my_model.h5')  # load pretrained model from file

的数据

相关问题