最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytorch和tensorflow計算Flops和params的詳細過程

 更新時間:2022年08月17日 15:17:45   作者:qq_40840829  
這篇文章主要介紹了pytorch和tensorflow計算Flops和params,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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)文章

最新評論

遂平县| 油尖旺区| 宁陕县| 锦州市| 五华县| 本溪市| 福安市| 永靖县| 东乌珠穆沁旗| 虎林市| 濉溪县| 伊川县| 永兴县| 湘西| 泉州市| 济宁市| 雅安市| 习水县| 新晃| 江津市| 巴东县| 开平市| 灵川县| 杭州市| 农安县| 永年县| 昭平县| 蒙城县| 津南区| 宁都县| 北辰区| 岳阳县| 宜昌市| 泽库县| 霍邱县| 长寿区| 九江市| 福州市| 安龙县| 象山县| 卫辉市|