Multiply — Accumulate Operations (MACS) is originally a hardware index, but it is often used to evaluate the calculation speed of deep learning model in recent years. We can use TorchProfile to calculate. Here I directly use the calculation model in the torch package as follows:

from torchprofile import profile_macs
profile_macs(model,inputs)
Copy the code

The complete code is as follows:

import torch from torchvision import models from torchprofile import profile_macs import os ls=dir(models) ls.sort() res=[] for i in ls: if not (i.startswith('_')) and (i! ='utils.py'): res.append(i.split('.')[0]) print(res) macs_results={} for i in res: try: MACs=profile_macs(eval('models.'+i+'()'),inputs) macs_results[i]=MACs except Exception as e: passCopy the code

We can plot the result:

import matplotlib.pyplot as plt x_x = [] y_y = [] s = sorted(macs_results.items(), key=lambda x: x[1], reverse=False) for i in s: x_x.append(i[0]) y_y.append(i[1]) x = x_x y = y_y fig, ax = plt.subplots() ax.barh(x, y, color="deepskyblue") labels = ax.get_xticklabels() plt.setp(labels, rotation=0, horizontalalignment='right') for a, b in zip(x, y): plt.text(b+1, a, b, ha='center', Va ='center') ax.legend(["label"],loc="lower right") plt.rcparams ['font. Sans-serif '] = ['SimHei'] # Plt.ylabel ('model name') plt.xLabel ('MACs') plt.rcparams ['savefig.dpi'] = 300 # Plt.rcparams [' fig.figsize '] = (20.0, 1.0) # plt.title(" Models for MACs")Copy the code

The results are as follows: