pytorch和tensorflow計算Flops和params的詳細(xì)過程
pytorch和tensorflow計算Flops和params
1.只計算params
net = model() # 定義好的網(wǎng)絡(luò)模型
total = sum([param.nelement() for param in net.parameters()])
print("Number of parameter: %.2fM" % total)
這是網(wǎng)上很常見的直接用自帶方法計算params,基本不會出錯。勝在簡潔。
2.計算flops和params
要計算flops,目前沒見到用自帶方法計算的,基本都是要安裝別的庫。
這邊我們安裝thop庫。
pip install thop # 安裝thop庫
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不分。因為macs表示乘加累積操作數(shù),一個乘法加上一個加法才算一個macs。而flops表示浮點運算次數(shù),每一個加、減、乘、除操作都算1FLOPs操作。所以很明顯,在數(shù)值上,1flops=2macs。此外,(img1,img2,img3)就表示你如果有三個輸入要輸入模型,就這樣寫。
另外,要注意,params只和模型參數(shù)量相關(guān),而和輸入tensor大小無關(guān)。但flops和輸入圖片大小是相關(guān)的.
3.tensorflow計算params和flops
此處是我找到的一些用于tensorflow計算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計算Flops和params的文章就介紹到這了,更多相關(guān)pytorch和tensorflow計算內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python flask web服務(wù)實現(xiàn)更換默認(rèn)端口和IP的方法
今天小編就為大家分享一篇python flask web服務(wù)實現(xiàn)更換默認(rèn)端口和IP的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
解決os.path.isdir() 判斷文件夾卻返回false的問題
今天小編就為大家分享一篇解決os.path.isdir() 判斷文件夾卻返回false的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Python使用captcha庫制作帶參數(shù)輸入驗證碼案例
這篇文章主要介紹了Python使用captcha庫制作驗證碼,帶參數(shù)輸入,本文通過實例案例解析給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
Python多進程入門、分布式進程數(shù)據(jù)共享實例詳解
這篇文章主要介紹了Python多進程入門、分布式進程數(shù)據(jù)共享,結(jié)合實例形式詳細(xì)分析了Python進程操作技巧數(shù)據(jù)共享相關(guān)實現(xiàn)技巧與注意事項,需要的朋友可以參考下2019-06-06
Python中byte字符串轉(zhuǎn)string的實現(xiàn)
本文主要介紹了Python中byte字符串轉(zhuǎn)string的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

