I tried to select and quantify my model, but completing this operation seems to take more memory and time. What should I do

yinjun1131: <p>I am trying to learn the quantization work of the model to reduce the burden on my hardware equipment, but when I tried to use the quantized model, I found that both memory usage and inference time were worse than before. What should I do</p>

yinjun1131: <blockquote>
<p>Blockquote<br>
#!/usr/bin/env python</p>
</blockquote>
<h1><a name="codingutf8-1" class="anchor" href="#codingutf8-1"></a>coding=utf8</h1>
<p>import numpy as np<br>
import cv2<br>
from blazeface import BlazeFace,BlazeBlock<br>
import torch<br>
import time</p>
<p>import torch.ao.quantization.quantize_fx as quantize_fx</p>
<h1><a name="resize-2" class="anchor" href="#resize-2"></a>封装resize函数</h1>
<p>def resize_img_keep_ratio(img,target_size):<br>
# img = cv2.imread(img_name) # 读取图片<br>
old_size= img.shape[0:2] # 原始图像大小<br>
ratio = min(float(target_size[i])/(old_size[i]) for i in range(len(old_size))) # 计算原始图像宽高与目标图像大小的比例，并取其中的较小值<br>
new_size = tuple([int(i*ratio) for i in old_size]) # 根据上边求得的比例计算在保持比例前提下得到的图像大小<br>
img = cv2.resize(img,(new_size[1], new_size[0])) # 根据上边的大小进行放缩<br>
pad_w = target_size[1] - new_size[1] # 计算需要填充的像素数目（图像的宽这一维度上）<br>
pad_h = target_size[0] - new_size[0] # 计算需要填充的像素数目（图像的高这一维度上）<br>
top,bottom = pad_h//2, pad_h-(pad_h//2)<br>
left,right = pad_w//2, pad_w -(pad_w//2)<br>
img_new = cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT,None,(0,0,0))<br>
return img_new</p>
<p>def plot_detections(img, detections):</p>
<pre><code>if isinstance(detections, torch.Tensor):
    detections = detections.cpu().numpy()

if detections.ndim == 1:
    detections = np.expand_dims(detections, axis=0)

# print("Found %d faces" % detections.shape[0])
    
for i in range(detections.shape[0]):
    ymin = int(detections[i, 0] * img.shape[0])
    xmin = int(detections[i, 1] * img.shape[1])
    ymax = int(detections[i, 2] * img.shape[0])
    xmax = int(detections[i, 3] * img.shape[1])

    cv2.rectangle(img,(xmin,ymin),(xmax,ymax),(170,234,242),5,lineType=cv2.LINE_AA)

    # if with_keypoints:
    #     for k in range(6):
    #         kp_x = detections[i, 4 + k*2    ] * img.shape[1]
    #         kp_y = detections[i, 4 + k*2 + 1] * img.shape[0]
    #         circle = patches.Circle((kp_x, kp_y), radius=0.5, linewidth=1, 
    #                                 edgecolor="lightskyblue", facecolor="none", 
    #                                 alpha=detections[i, 16])
    #         ax.add_patch(circle)
</code></pre>
<p>gpu = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)<br>
gpu = torch.device(“cpu”)</p>
<h1><a name="front_net-blazefacetogpu-3" class="anchor" href="#front_net-blazefacetogpu-3"></a>front_net = BlazeFace().to(gpu)</h1>
<h1><a name="front_netload_weightsblazefacepth-4" class="anchor" href="#front_netload_weightsblazefacepth-4"></a>front_net.load_weights(“blazeface.pth”)</h1>
<h1><a name="front_netload_anchorsanchorsnpy-5" class="anchor" href="#front_netload_anchorsanchorsnpy-5"></a>front_net.load_anchors(“anchors.npy”)</h1>
<p>back_net = BlazeFace(back_model=True).to(gpu)<br>
back_net.load_weights(“blazefaceback.pth”)<br>
back_net.load_anchors(“anchorsback.npy”)</p>
<h1><a name="create-a-quantized-model-instance-6" class="anchor" href="#create-a-quantized-model-instance-6"></a>create a quantized model instance</h1>
<p>model_int8 = torch.quantization.quantize_dynamic(<br>
back_net,  # the original model<br>
qconfig_spec ={torch.nn.Conv2d,torch.nn.Linear,torch.nn.ReLU,torch.nn.MaxPool2d},  # a set of layers to dynamically quantize<br>
dtype=torch.quint8)  # the target dtype for quantized weights</p>
<h1><a name="back_neteval-7" class="anchor" href="#back_neteval-7"></a>back_net.eval()</h1>
<h1><a name="back_netqconfig-torchquantizationget_default_qconfigx86-8" class="anchor" href="#back_netqconfig-torchquantizationget_default_qconfigx86-8"></a>back_net.qconfig = torch.quantization.get_default_qconfig(‘x86’)</h1>
<h1><a name="cpu-9" class="anchor" href="#cpu-9"></a># 这里应该是要配cpu架构</h1>
<h1><a name="back_netqconfig-torchquantizationget_default_qconfigarm-10" class="anchor" href="#back_netqconfig-torchquantizationget_default_qconfigarm-10"></a># back_net.qconfig = torch.quantization.get_default_qconfig(‘arm’)</h1>
<h1><a name="model_fp32_fused-torchquantizationfuse_modulesback_netblazeblock-batchnorm-relu-11" class="anchor" href="#model_fp32_fused-torchquantizationfuse_modulesback_netblazeblock-batchnorm-relu-11"></a># model_fp32_fused = torch.quantization.fuse_modules(back_net,[[‘BlazeBlock’ , ‘batchnorm’, ‘relu’]])</h1>
<h1><a name="bn_preparetorch-quantizationprepareback_net-12" class="anchor" href="#bn_preparetorch-quantizationprepareback_net-12"></a>bn_prepare=torch .quantization.prepare(back_net)</h1>
<h1><a name="bn_preparetorchrandn13256256togpu-13" class="anchor" href="#bn_preparetorchrandn13256256togpu-13"></a>bn_prepare(torch.randn(1,3,256,256).to(gpu))</h1>
<h1><a name="model_int8-torchquantizationconvertbn_preparetogpu-14" class="anchor" href="#model_int8-torchquantizationconvertbn_preparetogpu-14"></a>model_int8 = torch.quantization.convert(bn_prepare.to(gpu))</h1>
<h1><a name="optionally-change-the-thresholds-15" class="anchor" href="#optionally-change-the-thresholds-15"></a>Optionally change the thresholds:</h1>
<h1><a name="front_netmin_score_thresh-075-16" class="anchor" href="#front_netmin_score_thresh-075-16"></a>front_net.min_score_thresh = 0.75</h1>
<h1><a name="front_netmin_suppression_threshold-03-17" class="anchor" href="#front_netmin_suppression_threshold-03-17"></a>front_net.min_suppression_threshold = 0.3</h1>
<h1><a name="url-rtsp19216820253399380sdp-18" class="anchor" href="#url-rtsp19216820253399380sdp-18"></a>url = “rtsp://192.168.20.253/399380.sdp”</h1>
<p>url = 0<br>
cap = cv2.VideoCapture(url) <span class="hashtag-raw">#设备号为0</span><br>
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(‘M’,‘J’,‘P’,‘G’))<br>
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)<br>
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)<br>
time123 = time.time()<br>
while(True):<br>
if cap.isOpened() == False:<br>
print(‘can not open camera’)<br>
break<br>
print((time.time()-time123 ))<br>
time123 = time.time()<br>
ret, frame = cap.read() <span class="hashtag-raw">#读取图像</span></p>
<pre><code>if ret == False: #图像读取失败则直接进入下一次循环
    continue


frame =  resize_img_keep_ratio(frame, (256, 256))


dd = torch.from_numpy(frame).unsqueeze(0).permute(0,3,1,2).to(torch.float32).to(gpu)
time1 = time.time()
detect = model_int8(dd)
# print(time.time()-time1)
# time1 = time.time()
# front_detections = back_net.predict_on_image(frame)
# print(time.time()-time1)
# plot_detections(frame,front_detections)

cv2.namedWindow("frame")
cv2.imshow('frame', frame)

mykey = cv2.waitKey(1)
#按q退出循环，0xFF是为了排除一些功能键对q的ASCII码的影响
if mykey &amp; 0xFF == ord('q'):
    break
</code></pre>
<p><span class="hashtag-raw">#释放资源</span><br>
cap.release()<br>
cv2.destroyAllWindows()</p>
<blockquote>
<p>Blockquote</p>
</blockquote>