Toybrick

关于裁剪后的mobilenet ssd模型转换问题

ylc123

注册会员

积分
146
楼主
发表于 2020-6-2 09:20:11    查看: 6102|回复: 5 | [复制链接]    打印 | 只看该作者
本帖最后由 ylc123 于 2020-6-3 15:00 编辑

你好,
       我对自己裁剪后的网络运行咱们的test.py时遇到这个问题,请问这个是什么原因或者怎么调试呢?
       谢谢!




问题已解决。
http://t.rock-chips.com/forum.php?mod=viewthread&tid=1275&page=1&extra=#pid8967

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

jefferyzhang

版主

积分
12942
沙发
发表于 2020-6-2 09:57:13 | 只看该作者
1. 首先你自己这个网络能不能被tf正常推理
2. 其次转换脚本里输入输出是不是变了,配置对了没
回复

使用道具 举报

ylc123

注册会员

积分
146
板凳
 楼主| 发表于 2020-6-2 10:28:17 | 只看该作者
jefferyzhang 发表于 2020-6-2 09:57
1. 首先你自己这个网络能不能被tf正常推理
2. 其次转换脚本里输入输出是不是变了,配置对了没 ...

   你好,
1.我用的是caffe模型,自己测试检测没有问题的。
2.输入大小确实有改变,mobilenet裁剪了一半的卷积,ssd裁剪了一半的金字塔层。
3.输出是仿照原始ssd的,是detection_out
回复

使用道具 举报

ylc123

注册会员

积分
146
地板
 楼主| 发表于 2020-6-2 11:56:40 | 只看该作者
jefferyzhang 发表于 2020-6-2 09:57
1. 首先你自己这个网络能不能被tf正常推理
2. 其次转换脚本里输入输出是不是变了,配置对了没 ...

你好,请问咱们的测试demo中的priorbox.txt是如何产生的呢?
回复

使用道具 举报

jefferyzhang

版主

积分
12942
5#
发表于 2020-6-2 11:59:48 | 只看该作者
ylc123 发表于 2020-6-2 11:56
你好,请问咱们的测试demo中的priorbox.txt是如何产生的呢?

训练时候是啥,这里就是啥啊,算出来的anchor boxes啊。
回复

使用道具 举报

ylc123

注册会员

积分
146
6#
 楼主| 发表于 2020-6-2 14:50:10 | 只看该作者
本帖最后由 ylc123 于 2020-6-2 15:09 编辑

   你好,又有问题了-.-1.我是在示例的caffe目录里新建了一个自己的mobilenet ssd文件夹,但为什么输出的是TensorFlow,如图箭头1和2。
2.自己去掉detectionout层,然后根据论坛里的https://github.com/Pinnh/NPU_CaffeSSD网址提取出pribox,并保存,运行时出现了输入不匹配问题,如箭头3所指,而且输出和模型实际输出不匹配,即为空(忘记resize图片大小为自己模型的大小,resize之后,input不匹配问题消失,但出现W Unhandle status: the input shape of reshape layer mbox_conf_reshape_165 is not 4-Ddone
--> Building model
W The target_platform is not set in config, using default target platform rk1808.)。
3.自己的理解是dataset.txt里面是测试图片的路径?那为什么代码里面还会有多处写读取测试图片?不是很理解。
4.代码如下,是根据咱们的vgg ssd中的套用的。
   谢谢解答。
  1. import os
  2. import math
  3. import numpy as np
  4. import cv2
  5. from rknn.api import RKNN

  6. import PIL.Image as Image
  7. import PIL.ImageDraw as ImageDraw
  8. import PIL.ImageFont as ImageFont

  9. np.set_printoptions(threshold=np.inf)

  10. CLASSES = ('background','head')
  11.         
  12. NUM_CLS = 2

  13. CONF_THRESH = 0.5
  14. NMS_THRESH = 0.45

  15. def IntersectBBox(box1, box2):
  16.     if box1[0]>box2[2] or box1[2]<box2[0] or box1[1]>box2[3] or box1[3]<box2[1]:
  17.         return 0
  18.     else:
  19.         area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
  20.         area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])

  21.         xx1 = max(box1[0], box2[0])
  22.         yy1 = max(box1[1], box2[1])
  23.         xx2 = min(box1[2], box2[2])
  24.         yy2 = min(box1[3], box2[3])

  25.         w = max(0, xx2-xx1)
  26.         h = max(0, yy2-yy1)

  27.         ovr = w*h / (area1 + area2 - w*h)
  28.         return ovr


  29. def ssd_post_process(conf_data, loc_data):

  30.     prior_data = np.loadtxt('priorbox_ylc.txt', dtype = np.float32)

  31.     prior_bboxes = prior_data[:len(loc_data)]
  32.     prior_variances = prior_data[len(loc_data):]

  33.     prior_num = int(len(loc_data) / 4) # 8732
  34.    
  35.     conf_data = conf_data.reshape(-1,2)
  36.    
  37.     idx_class_conf = []
  38.     bboxes = []
  39.    
  40.     # conf
  41.     for prior_idx in range(0,prior_num):
  42.         max_val = np.max(conf_data[prior_idx])
  43.         max_idx = np.argmax(conf_data[prior_idx])
  44.         if max_val > CONF_THRESH and max_idx != 0:
  45.             idx_class_conf.append([prior_idx, max_idx, max_val])
  46.    
  47.     #print(len(idx_class_conf))   
  48.             
  49.     # boxes
  50.     for i in range(0,prior_num):
  51.         prior_w = prior_bboxes[4*i+2] - prior_bboxes[4*i]
  52.         prior_h = prior_bboxes[4*i+3] - prior_bboxes[4*i+1]
  53.         prior_center_x = (prior_bboxes[4*i+2] + prior_bboxes[4*i]) / 2
  54.         prior_center_y = (prior_bboxes[4*i+3] + prior_bboxes[4*i+1]) / 2
  55.       
  56.         bbox_center_x = prior_variances[4*i+0] * loc_data[4*i+0] * prior_w + prior_center_x
  57.         bbox_center_y = prior_variances[4*i+1] * loc_data[4*i+1] * prior_h + prior_center_y
  58.         bbox_w        = math.exp(prior_variances[4*i+2] * loc_data[4*i+2]) * prior_w
  59.         bbox_h        = math.exp(prior_variances[4*i+3] * loc_data[4*i+3]) * prior_h
  60.         
  61.         tmp = []
  62.         tmp.append(max(min(bbox_center_x - bbox_w / 2., 1), 0))
  63.         tmp.append(max(min(bbox_center_y - bbox_h / 2., 1), 0))
  64.         tmp.append(max(min(bbox_center_x + bbox_w / 2., 1), 0))
  65.         tmp.append(max(min(bbox_center_y + bbox_h / 2., 1), 0))
  66.         bboxes.append(tmp)
  67.    
  68.     print('idx_class_conf = ',len(idx_class_conf))
  69.    
  70.     #nms
  71.     cur_class_num = 0
  72.     idx_class_conf_ = []
  73.     for i in range(0, len(idx_class_conf)):
  74.         keep = True
  75.         k = 0
  76.         while k < cur_class_num:
  77.             if keep:
  78.                 ovr = IntersectBBox(bboxes[idx_class_conf[i][0]], bboxes[idx_class_conf_[k][0]])
  79.                 if idx_class_conf_[k][1]==idx_class_conf[i][1] and ovr > NMS_THRESH:
  80.                     if idx_class_conf_[k][2]<idx_class_conf[i][2]:
  81.                         idx_class_conf_.pop(k)
  82.                         idx_class_conf_.append(idx_class_conf[i])
  83.                     keep = False
  84.                     break
  85.                 k += 1
  86.             else:
  87.                 break
  88.         if keep:
  89.             idx_class_conf_.append(idx_class_conf[i])
  90.             cur_class_num += 1
  91.                                                                   
  92.     print(idx_class_conf_)
  93.       
  94.     box_class_score = []     
  95.    
  96.     for i in range(0, len(idx_class_conf_)):
  97.         bboxes[idx_class_conf_[i][0]].append(idx_class_conf_[i][1])
  98.         bboxes[idx_class_conf_[i][0]].append(idx_class_conf_[i][2])
  99.         box_class_score.append( bboxes[idx_class_conf_[i][0]])
  100.         
  101.     img = cv2.imread('./123.jpg')
  102.     img_pil = Image.fromarray(img)
  103.     draw = ImageDraw.Draw(img_pil)

  104.     font = ImageFont.load_default()
  105.    
  106.     for i in range(0, len(box_class_score)):
  107.         x1 = int(box_class_score[i][0]*img.shape[1])
  108.         y1 = int(box_class_score[i][1]*img.shape[0])
  109.         x2 = int(box_class_score[i][2]*img.shape[1])
  110.         y2 = int(box_class_score[i][3]*img.shape[0])
  111.         color = (0, int(box_class_score[i][4]/20.0*255), 255)
  112.         draw.line([(x1, y1), (x1, y2), (x2 , y2),
  113.              (x2 , y1), (x1, y1)], width=2, fill=color)
  114.         display_str = CLASSES[box_class_score[i][4]] +  ":" + str(box_class_score[i][5])
  115.         display_str_height = np.ceil((1 + 2 * 0.05) * font.getsize(display_str)[1])+1
  116.         
  117.         if y1 > display_str_height:
  118.             text_bottom = y1
  119.         else:
  120.             text_bottom = y1 + display_str_height
  121.         
  122.         text_width, text_height = font.getsize(display_str)
  123.         margin = np.ceil(0.05 * text_height)
  124.         draw.rectangle([(x1, text_bottom-text_height-2*margin), (x1+text_width, text_bottom)],fill=color)
  125.         draw.text((x1+margin, text_bottom-text_height-margin), display_str, fill='black', font=font)

  126.     np.copyto(img, np.array(img_pil))
  127.     cv2.imwrite("result.jpg", img)

  128. if __name__ == '__main__':


  129.     # Create RKNN object
  130.     rknn = RKNN(verbose=False)

  131.     # pre-process config
  132.     print('--> config model')
  133.     rknn.config(channel_mean_value='103.94 116.78 123.68 1', reorder_channel='2 1 0')
  134.     print('done')

  135.     # Load tensorflow model
  136.     print('--> Loading model')
  137.     ret = rknn.load_caffe(model='./mobilenet_v2_ssd_truncated2.prototxt',
  138.                           proto='caffe',
  139.                           blobs='./mobilenet_iter_38200.caffemodel')
  140.     if ret != 0:
  141.         print("---------------------------------------------")
  142.         print('Load model failed! Ret = {}'.format(ret))
  143.         exit(ret)
  144.     print('done')

  145.     # Build model
  146.     print('--> Building model')
  147.     ret = rknn.build(do_quantization=False, dataset='./dataset.txt')
  148.     if ret != 0:
  149.         print('Build model failed!')
  150.         exit(ret)
  151.     print('done')

  152.     # Export rknn model
  153.     print('--> Export RKNN model')
  154.     ret = rknn.export_rknn('./mobilenet_v2_ssd.rknn')
  155.     if ret != 0:
  156.         print('Export rknn failed!')
  157.         exit(ret)
  158.     print('done')

  159.     # Set inputs
  160.     img = cv2.imread('./123.jpg')
  161.     img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

  162.     print('--> Init runtime environment')
  163.     ret = rknn.init_runtime()
  164.     if ret != 0:
  165.         print('Init runtime environment failed')
  166.         exit(ret)
  167.     print('done')

  168.     # Inference
  169.     print('--> Running model')
  170.     outputs = rknn.inference(inputs=[img])
  171.     print('outputs = ',len(outputs),type(outputs),len(outputs[0].reshape((-1, 1))),len(outputs[1].reshape((-1, 1))))
  172.     print('done')

  173.     outputs[0] = outputs[0].reshape((-1, 1))
  174.     outputs[1] = outputs[1].reshape((-1, 1))
  175.     ssd_post_process(outputs[1], outputs[0])

  176.     rknn.release()
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

产品中心 购买渠道 开源社区 Wiki教程 资料下载 关于Toybrick


快速回复 返回顶部 返回列表