pytorch和tensorflow計(jì)算Flops和params的詳細(xì)過(guò)程
pytorch和tensorflow計(jì)算Flops和params
1.只計(jì)算params
net = model() # 定義好的網(wǎng)絡(luò)模型 total = sum([param.nelement() for param in net.parameters()]) print("Number of parameter: %.2fM" % total)
這是網(wǎng)上很常見(jiàn)的直接用自帶方法計(jì)算params,基本不會(huì)出錯(cuò)。勝在簡(jiǎn)潔。
2.計(jì)算flops和params
要計(jì)算flops,目前沒(méi)見(jiàn)到用自帶方法計(jì)算的,基本都是要安裝別的庫(kù)。
這邊我們安裝thop庫(kù)。
pip install thop # 安裝thop庫(kù)
import torch from thop import profile net = model() # 定義好的網(wǎng)絡(luò)模型 img1 = torch.randn(1, 3, 512, 512) img2 = torch.randn(1, 3, 512, 512) img3 = torch.randn(1, 3, 512, 512) macs, params = profile(net, (img1,img2,img3)) print('flops: ', 2*macs, 'params: ', params)
這邊和其他網(wǎng)上教程的區(qū)別便是,他們macs和flops不分。因?yàn)閙acs表示乘加累積操作數(shù),一個(gè)乘法加上一個(gè)加法才算一個(gè)macs。而flops表示浮點(diǎn)運(yùn)算次數(shù),每一個(gè)加、減、乘、除操作都算1FLOPs操作。所以很明顯,在數(shù)值上,1flops=2macs。此外,(img1,img2,img3)就表示你如果有三個(gè)輸入要輸入模型,就這樣寫。
另外,要注意,params只和模型參數(shù)量相關(guān),而和輸入tensor大小無(wú)關(guān)。但flops和輸入圖片大小是相關(guān)的.
3.tensorflow計(jì)算params和flops
此處是我找到的一些用于tensorflow計(jì)算params和flops的方法,僅供參考,不保證效果。
def get_flops_params(): sess = tf.compat.v1.Session() graph = sess.graph flops = tf.compat.v1.profiler.profile(graph, options=tf.compat.v1.profiler.ProfileOptionBuilder.float_operation()) params = tf.compat.v1.profiler.profile(graph, options=tf.compat.v1.profiler.ProfileOptionBuilder.trainable_variables_parameter()) print('FLOPs: {}; Trainable params: {}'.format(flops.total_float_ops, params.total_parameters)) def count2(): print(np.sum([np.prod(v.get_shape().as_list()) for v in tf.trainable_variables()])) def get_nb_params_shape(shape): ''' Computes the total number of params for a given shap. Works for any number of shapes etc [D,F] or [W,H,C] computes D*F and W*H*C. ''' nb_params = 1 for dim in shape: nb_params = nb_params * int(dim) return nb_params def count3(): tot_nb_params = 0 for trainable_variable in tf.trainable_variables(): shape = trainable_variable.get_shape() # e.g [D,F] or [W,H,C] current_nb_params = get_nb_params_shape(shape) tot_nb_params = tot_nb_params + current_nb_params print(tot_nb_params) import tensorflow.compat.v1 as tf tf.compat.v1.disable_eager_execution() from model import Model import keras.backend as K def get_flops(model): run_meta = tf.RunMetadata() opts = tf.profiler.ProfileOptionBuilder.float_operation() # We use the Keras session graph in the call to the profiler. flops = tf.profiler.profile(graph=K.get_session().graph, run_meta=run_meta, cmd='op', options=opts) return flops.total_float_ops # Prints the "flops" of the model. # .... Define your model here .... M = Model(BATCH_SIZE=1, INPUT_H=268, INPUT_W=360, is_training=False) print(get_flops(M))
到此這篇關(guān)于pytorch和tensorflow計(jì)算Flops和params的文章就介紹到這了,更多相關(guān)pytorch和tensorflow計(jì)算內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python通過(guò)Pillow實(shí)現(xiàn)圖片對(duì)比
這篇文章主要介紹了Python Pillow實(shí)現(xiàn)圖片對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04python flask web服務(wù)實(shí)現(xiàn)更換默認(rèn)端口和IP的方法
今天小編就為大家分享一篇python flask web服務(wù)實(shí)現(xiàn)更換默認(rèn)端口和IP的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07解決os.path.isdir() 判斷文件夾卻返回false的問(wèn)題
今天小編就為大家分享一篇解決os.path.isdir() 判斷文件夾卻返回false的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Python使用captcha庫(kù)制作帶參數(shù)輸入驗(yàn)證碼案例
這篇文章主要介紹了Python使用captcha庫(kù)制作驗(yàn)證碼,帶參數(shù)輸入,本文通過(guò)實(shí)例案例解析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05通過(guò)Python實(shí)現(xiàn)猜燈謎游戲的示例代碼
新的一年迎來(lái)了元宵節(jié),元宵佳節(jié)在陪伴家人的同時(shí),自然也少不了賞花燈,猜燈謎的項(xiàng)目。本文會(huì)通過(guò)Python實(shí)現(xiàn)這一游戲,需要的可以參考一下2022-02-02Python多進(jìn)程入門、分布式進(jìn)程數(shù)據(jù)共享實(shí)例詳解
這篇文章主要介紹了Python多進(jìn)程入門、分布式進(jìn)程數(shù)據(jù)共享,結(jié)合實(shí)例形式詳細(xì)分析了Python進(jìn)程操作技巧數(shù)據(jù)共享相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2019-06-06Python中byte字符串轉(zhuǎn)string的實(shí)現(xiàn)
本文主要介紹了Python中byte字符串轉(zhuǎn)string的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07