pytorch加載自定義網(wǎng)絡(luò)權(quán)重的實(shí)現(xiàn)
在將自定義的網(wǎng)絡(luò)權(quán)重加載到網(wǎng)絡(luò)中時(shí),報(bào)錯(cuò):
AttributeError: 'dict' object has no attribute 'seek'. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.
我們一步一步分析。
模型網(wǎng)絡(luò)權(quán)重保存額代碼是:torch.save(net.state_dict(),'net.pkl')
(1)查看獲取模型權(quán)重的源碼:
pytorch源碼:net.state_dict()
def state_dict(self, destination=None, prefix='', keep_vars=False):
r"""Returns a dictionary containing a whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are
included. Keys are corresponding parameter and buffer names.
Returns:
dict:
a dictionary containing a whole state of the module
Example::
>>> module.state_dict().keys()
['bias', 'weight']
"""
將網(wǎng)絡(luò)中所有的狀態(tài)保存到一個(gè)字典中了,我自己構(gòu)建的就是一個(gè)字典,沒問題!
(2)查看保存模型權(quán)重的源碼:
pytorch源碼:torch.save()
def save(obj, f, pickle_module=pickle, pickle_protocol=DEFAULT_PROTOCOL):
"""Saves an object to a disk file.
See also: :ref:`recommend-saving-models`
Args:
obj: saved object
f: a file-like object (has to implement write and flush) or a string
containing a file name
pickle_module: module used for pickling metadata and objects
pickle_protocol: can be specified to override the default protocol
.. warning::
If you are using Python 2, torch.save does NOT support StringIO.StringIO
as a valid file-like object. This is because the write method should return
the number of bytes written; StringIO.write() does not do this.
Please use something like io.BytesIO instead.
函數(shù)功能是將字典保存為磁盤文件(二進(jìn)制數(shù)據(jù)),那么我們在torch.load()時(shí),就是在內(nèi)存中加載二進(jìn)制數(shù)據(jù),這就是報(bào)錯(cuò)點(diǎn)。
解決方案:將字典保存為BytesIO文件之后,模型再net.load_state_dict()
#b為自定義的字典 torch.save(b,'new.pkl') net.load_state_dict(torch.load(b))
解決方法很簡單,主要記錄解決思路。
以上這篇pytorch加載自定義網(wǎng)絡(luò)權(quán)重的實(shí)現(xiàn)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python編程源碼報(bào)錯(cuò)解決方法總結(jié)經(jīng)驗(yàn)分享
這篇文章主要介紹了在平時(shí)Python編程工作中一些源碼報(bào)錯(cuò)的解決方法總結(jié)經(jīng)驗(yàn)分享,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
python 自動(dòng)化將markdown文件轉(zhuǎn)成html文件的方法
這篇文章主要介紹了python 自動(dòng)化將markdown文件轉(zhuǎn)成html文件的方法的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Python一行代碼快速實(shí)現(xiàn)程序進(jìn)度條示例
這篇文章主要為大家介紹了Python一行代碼快速實(shí)現(xiàn)程序進(jìn)度條示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Union在Python類型注解中的應(yīng)用與最佳實(shí)踐
Union” 在中文中通常翻譯為“聯(lián)合”,在數(shù)學(xué)和邏輯學(xué)中,它指的是兩個(gè)或多個(gè)集合的并集,在 Python 的類型注解中,Union 類型表示一個(gè)變量可以是多種類型中的任意一種,這與數(shù)學(xué)中的并集概念相似,本文介紹了Union在Python類型注解中的應(yīng)用與最佳實(shí)踐2024-09-09
使用Python自動(dòng)化自定義字體混淆信息的方法實(shí)例
今天小編就為大家分享一篇關(guān)于使用Python自動(dòng)化自定義字體混淆信息的方法實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
Opencv圖像添加椒鹽噪聲、高斯濾波去除噪聲原理以及手寫Python代碼實(shí)現(xiàn)方法
椒鹽噪聲的特征非常明顯,為圖像上有黑色和白色的點(diǎn),下面這篇文章主要給大家介紹了關(guān)于Opencv圖像添加椒鹽噪聲、高斯濾波去除噪聲原理以及手寫Python代碼實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09

