-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
1704 lines (1602 loc) · 75.7 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>https://bixiaopeng0.github.io</id>
<title>晓鹏博客</title>
<updated>2020-01-08T14:39:37.510Z</updated>
<generator>https://github.com/jpmonette/feed</generator>
<link rel="alternate" href="https://bixiaopeng0.github.io"/>
<link rel="self" href="https://bixiaopeng0.github.io/atom.xml"/>
<subtitle>温故而知新</subtitle>
<logo>https://bixiaopeng0.github.io/images/avatar.png</logo>
<icon>https://bixiaopeng0.github.io/favicon.ico</icon>
<rights>All rights reserved 2020, 晓鹏博客</rights>
<entry>
<title type="html"><![CDATA[pytorch优化器及学习率]]></title>
<id>https://bixiaopeng0.github.io/post/pytorch-you-hua-qi-ji-xue-xi-lu</id>
<link href="https://bixiaopeng0.github.io/post/pytorch-you-hua-qi-ji-xue-xi-lu">
</link>
<updated>2020-01-07T07:55:02.000Z</updated>
<content type="html"><![CDATA[<h3 id="学习率衰减">学习率衰减</h3>
<p>可以通过打印optimizer,查看里面的具体参数,网络各层的学习率,权重衰减。另外,学习率更新一般是一个epoch更新一次,所以要放在optimizer.step()的上一级。</p>
<h4 id="steplr">StepLR</h4>
<pre><code class="language-python">scheduler = lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1)
scheduler.step()
print(epoch, 'lr={:.6f}'.format(scheduler.get_lr()[0]))#查看学习率
</code></pre>
<p>for example</p>
<p>0<epoch<30, lr = 0.05<br>
30<=epoch<60, lr = 0.005<br>
60<=epoch<90, lr = 0.0005</p>
<h4 id="multisteplr">MultiStepLR</h4>
<pre><code class="language-python">scheduler = lr_scheduler.MultiStepLR(optimizer, [30, 80], 0.1)#与StepLR相比,MultiStepLR可以设置指定的区间
scheduler.step()
</code></pre>
<h4 id="exponentiallr">ExponentialLR</h4>
<pre><code class="language-python">scheduler = lr_scheduler.ExponentialLR(optimizer, gamma=0.9)
scheduler.step()
</code></pre>
<h3 id="优化器">优化器</h3>
<h4 id="adam">Adam</h4>
<pre><code class="language-python">class torch.optim.Adam(params, lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)
#params(iterable):可用于迭代优化的参数或者定义参数组的dicts。
#lr (float, optional) :学习率(默认: 1e-3)
#betas (Tuple[float, float], optional):用于计算梯度的平均和平方的系数(默认: (0.9, 0.999))
#eps (float, optional):为了提高数值稳定性而添加到分母的一个项(默认: 1e-8)
#weight_decay (float, optional):权重衰减(如L2惩罚)(默认: 0)
</code></pre>
<h4 id="sgd">SGD</h4>
<pre><code class="language-python">optim.SGD(net.parameters(), lr=opt.lr, momentum=0.9,weight_decay=5e-4)
</code></pre>
<p>adam和加入动量的sgd中收敛结果一样,但是sgd动量为0的优化器收敛效果较差。<br>
待续---------------time2020/1/7</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[配置opencv dnn(cuda)以及openvino遇到的坑]]></title>
<id>https://bixiaopeng0.github.io/post/pei-zhi-opencv-dnncudayi-ji-openvino-yu-dao-de-keng</id>
<link href="https://bixiaopeng0.github.io/post/pei-zhi-opencv-dnncudayi-ji-openvino-yu-dao-de-keng">
</link>
<updated>2020-01-06T12:15:48.000Z</updated>
<content type="html"><![CDATA[<!--more-->!
<p>配置openvino遇到的坑</p>
<p>1、第一次下载的R1,0但是检测不到python,后来下载R3.1解决问题<br>
2、编译build_saples_msvc报错<br>
将一下内容设置为<br>
set MSBUILD_BIN=C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe<br>
set VS_PATH=<br>
set VS_VERSION=2015<br>
添加<br>
set MSBUILD_VERSION=14 2015<br>
set PLATFORM=x64</p>
<p>3、opencv编译报错<br>
重新编译opencv 添加IE库</p>
<p>4、cmake情况下会出现下载失败,先从日记里面把安装包下载好,然后在进行config</p>
<p>配置dnn——cuda</p>
<p>1、 使用环境</p>
<p>(1)Cuda10.0</p>
<p>(2)Cudnn7.5及以上(和cuda版本兼容)</p>
<p>(3)Vs版本:2017(2015经测试,编译失败)</p>
<p>(4)Cmake</p>
<p>2、 配置步骤</p>
<p>(1) 选择源码路径,然后config,见图一</p>
<p>(2) 勾选build_opencv_world, opencv_dnn_cuda with_cuda,将解压好的opencv_contribe的module路径添加进来,注contribe可以在github上下载最新的,见图二,三,四</p>
<p>(3) 第二次config,部分文件可能会下载失败,可以手动下载,放到指定文件夹下即可,从nvidia官网查找本机显卡算力,然后在cuda_arch_bin中保留本机显卡算力,删除其他,然后勾选cuda_fast_math,第三次configure 见图五</p>
<p>(4) Configure完成之后,从日记查看是否检测到本机的cuda和cudnn,如果检测到点击generate,见图六</p>
<p>(5) 打开vs工程,在release模型下点击ALL_BUILD,生成之后,右击INSTALL->仅用于项目->仅生成INSTALL</p>
<p>(6) 配置好opencv的环境,在代码里使用指定参数,即可调用cuda 见图七</p>
<p>图片是懒得传上图床了。。。。</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[opencv dnn模块]]></title>
<id>https://bixiaopeng0.github.io/post/opencv-dnn-mo-kuai</id>
<link href="https://bixiaopeng0.github.io/post/opencv-dnn-mo-kuai">
</link>
<updated>2019-10-12T13:15:22.000Z</updated>
<content type="html"><![CDATA[<!--more-->
<p>最近在研究opencv的dnn模块,简单的做一个记录,以备后面使用。</p>
<h3 id="dnn简介">dnn简介</h3>
<p> dnn是opencv中的深度神经网络模块,支持运行网络,但是不支持训练,实现图像场景中的图像分类,图像检测和图像分割,并且支持主流的深度学习框架。</p>
<h3 id="dnn尝试">dnn尝试</h3>
<p> 最近一端时间尝试了tensorflow,darknet还有caffe。调通了darknet的yolov3还有caffe的ssd_mobilnet检测网络,但是都是基于cpu的,所以速度可想而知,yolov3速度大约在1~2fps,ssd-mobilnet大约在9fps。</p>
<p> googlenet模型是从caffe官方github仓库里面下的,从网上下载了一张雨伞图片,测试还可可以,准确率蛮高的。</p>
<h3 id="资料来源">资料来源</h3>
<p> 代码主要是在github上,还有各个博客上找的。其实opencv也提供了相关demo,网上的代码也是基于demo改的,安装完opencv之后,在source/samples/dnn里面提供了相关代码,可以参考。</p>
<h3 id="问题记录">问题记录</h3>
<ul>
<li>首先是tensorflow下的ssd网络,如果调用opencv里面的权重是可以跑通的,但是自己生成的却不行,但是网上有大佬用自己训练的模型跑通了。</li>
<li>使用opencl加速,代码里有一个参数可以进行加速,但是使用之后就报错,从网上查了一下
<ul>
<li>opencv对显卡支持不好 -- github opencv issue</li>
<li>opencv只支持intel显卡-- 这个是前几年的文章看见的,现在支持了。</li>
</ul>
</li>
</ul>
<h3 id="总结">总结</h3>
<ul>
<li>个人感觉darknet是用c写的,caffe用c++写的,比较容易调通,调用过程基本上没有遇到困难,但是tensorflow和pytorch使用python写的,调用却不太顺利,是不是和语言有关系呢?</li>
<li>dnn模块软肋
<ul>
<li>只支持部分网络</li>
<li>gpu加速还是个问题,可能还不太成熟吧,博客上看见的也是用cpu调通的,但是速度这么慢,落地不行呀。</li>
</ul>
</li>
</ul>
<pre><code class="language-c++">##caffe-ssd mobilnet opencv3.4.1
#include<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/dnn.hpp>
using namespace std;
using namespace cv;
using namespace cv::dnn;
class Object
{
public:
Object();
Object(int index, float confidence, String name, Rect rect);
~Object();
public:
int index;
String name;
float confidence;
Rect rect;
private:
};
Object::Object() {
}
Object::Object(int index, float confidence, String name, Rect rect) {
this->index = index;
this->confidence = confidence;
this->name = name;
this->rect = rect;
}
Object::~Object() {
}
//----------------------------全局常量----------------------------------
//配置好protxt文件,网络结构描述文件
//配置好caffemodel文件,训练好的网络权重
const String PROTOTX_FILE = "no_bn.prototxt";
const String CAFFE_MODEL_FILE = "no_bn.caffemodel";
const String classNames[] = { "background", "jingling", "t16", "xiao_spark", "yu_mavic"};
const float CONF_THRESH = 0.5;
int main() {
//------------------------实例化网络----------------------------
Net mobileNetSSD = readNetFromCaffe(PROTOTX_FILE, CAFFE_MODEL_FILE);
if (mobileNetSSD.empty()) {
cerr << "加载网络失败!" << endl;
return -1;
}
TickMeter t;
VideoCapture video;
video.open("scr.mp4");
Mat srcImg;
video >> srcImg;
while (!srcImg.empty())
{
clock_t start_time,end_time;
start_time = clock();
//----------------------设置网络输入-----------------------
//将二维图像转换为CNN输入的张量Tensor,作为网络的输入
mobileNetSSD.setInput(blobFromImage(srcImg, 1.0 / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), true, false));
t.start();
//--------------------CNN网络前向计算----------------------
Mat netOut = mobileNetSSD.forward();
t.stop();
//----------------------解析计算结果-----------------------
vector<Object> detectObjects;
Mat detectionResult(netOut.size[2], netOut.size[3], CV_32F, netOut.ptr<float>());
for (int i = 0; i < detectionResult.rows; i++) {
//目标类别的索引
int objectIndex = detectionResult.at<float>(i, 1);
//检测结果置信度
float confidence = detectionResult.at<float>(i, 2);
//根据置信度阈值过滤掉置信度较小的目标
if (confidence<CONF_THRESH) {
continue;
}
//反归一化,得到图像坐标
int xLeftUp = static_cast<int>(detectionResult.at<float>(i, 3)*srcImg.cols);
int yLeftUp = static_cast<int>(detectionResult.at<float>(i, 4)*srcImg.rows);
int xRightBottom = static_cast<int>(detectionResult.at<float>(i, 5)*srcImg.cols);
int yRightBottom = static_cast<int>(detectionResult.at<float>(i, 6)*srcImg.rows);
//矩形框
Rect rect(Point{ xLeftUp,yLeftUp }, Point{ xRightBottom,yRightBottom });
//保存结果
detectObjects.push_back(Object{ objectIndex,confidence,classNames[objectIndex],rect });
}
//------------------------显示结果-----------------------------------
int count = 0;
for (auto& i : detectObjects) {
rectangle(srcImg, i.rect, Scalar(0, 255, 255), 2);
putText(srcImg, i.name, i.rect.tl(), 1, 1.8, Scalar(255, 0, 0), 2);
cout << "第" << count << "个目标:" << i.name << "\t" << i.rect << "\t" << i.confidence << endl;
count++;
}
end_time = clock();
imshow("MobileNet-SSD", srcImg);
waitKey(10);
cout << "FPS:" << CLOCKS_PER_SEC/(double)(end_time - start_time) << endl;
video >> srcImg;
}
waitKey(0);
}
</code></pre>
<pre><code class="language-c++">### opencv4.1 darknet-yolov3
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
using namespace cv;
using namespace cv::dnn;
void image_detection();
void video_detection();
float confidenceThreshold = 0.25;
String yolo_cfg = "yolov3-voc.cfg";
String yolo_model = "yolov3-voc_f.weights";
int main(int argc, char** argv)
{
//image_detection();
video_detection();
}
void image_detection() {
Net net = readNetFromDarknet(yolo_cfg, yolo_model);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
//net.setPreferableBackend(DNN_BACKEND_OPENCV); //启动GPU加速
//net.setPreferableTarget(DNN_TARGET_OPENCL); //启动GPU加速
std::vector<String> outNames = net.getUnconnectedOutLayersNames();
for (int i = 0; i < outNames.size(); i++) {
printf("output layer name : %s\n", outNames[i].c_str());
}
vector<string> classNamesVec;
//ifstream classNamesFile("object_detection_classes_yolov3.txt");
ifstream classNamesFile("myData.names");
if (classNamesFile.is_open())
{
string className = "";
while (std::getline(classNamesFile, className))
classNamesVec.push_back(className);
}
clock_t start_time;
start_time = clock();
// 加载图像
Mat frame = imread("scr.jpg");
//Mat frame = imread("cat.jpg");
Mat inputBlob = blobFromImage(frame, 1 / 255.F, Size(416, 416), Scalar(), true, false);
net.setInput(inputBlob);
// 检测
std::vector<Mat> outs;
net.forward(outs, outNames);
vector<double> layersTimings;
double freq = getTickFrequency() / 1000;
double time = net.getPerfProfile(layersTimings) / freq;
ostringstream ss;
cout << "detection time: " << time << " ms";
//putText(frame, ss.str(), Point(20, 20), 0, 0.5, Scalar(0, 0, 255));
vector<Rect> boxes;
vector<int> classIds;
vector<float> confidences;
for (size_t i = 0; i<outs.size(); ++i)
{
// Network produces output blob with a shape NxC where N is a number of
// detected objects and C is a number of classes + 4 where the first 4
// numbers are [center_x, center_y, width, height]
float* data = (float*)outs[i].data;
for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
{
Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
Point classIdPoint;
double confidence;
minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
if (confidence > confidenceThreshold)
{
int centerX = (int)(data[0] * frame.cols);
int centerY = (int)(data[1] * frame.rows);
int width = (int)(data[2] * frame.cols);
int height = (int)(data[3] * frame.rows);
int left = centerX - width / 2;
int top = centerY - height / 2;
classIds.push_back(classIdPoint.x);
confidences.push_back((float)confidence);
boxes.push_back(Rect(left, top, width, height));
}
}
}
vector<int> indices;
NMSBoxes(boxes, confidences, 0.5, 0.2, indices);
for (size_t i = 0; i < indices.size(); ++i)
{
int idx = indices[i];
Rect box = boxes[idx];
String className = classNamesVec[classIds[idx]] ;
putText(frame, className.c_str(), box.tl(), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 0, 0), 2, 8);
rectangle(frame, box, Scalar(0, 0, 255), 2, 8, 0);
}
cout <<"运行时间:" << ((double)(clock() - start_time) / CLOCKS_PER_SEC) * 1000 << "ms" << "\n" << endl;
imshow("YOLOv3-Detections", frame);
waitKey(0);
return;
}
void video_detection() {
Net net = readNetFromDarknet(yolo_cfg, yolo_model);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
//net.setPreferableBackend(DNN_BACKEND_OPENCV); //启动GPU加速
//net.setPreferableTarget(DNN_TARGET_OPENCL); //启动GPU加速
if (net.empty())
{
printf("Could not load net...\n");
return;
}
std::vector<String> outNames = net.getUnconnectedOutLayersNames();
for (int i = 0; i < outNames.size(); i++) {
printf("output layer name : %s\n", outNames[i].c_str());
}
vector<string> classNamesVec;
ifstream classNamesFile("myData.names");
if (classNamesFile.is_open())
{
string className = "";
while (std::getline(classNamesFile, className))
classNamesVec.push_back(className);
}
VideoCapture video_read;
video_read.open("scr2.mp4");
Mat frame;
video_read >> frame;
/*
VideoCapture capture;
capture.open("D:/vcprojects/images/fbb.avi");
if (!capture.isOpened()) {
printf("could not open the camera...\n");
return;
}
*/
while (!frame.empty())
{
//加载图像
//if (frame.empty())
// if (frame.channels() == 4)
// cvtColor(frame, frame, COLOR_BGRA2BGR);
Mat inputBlob = blobFromImage(frame, 1 / 255.F, Size(416, 416), Scalar(), true, false);
net.setInput(inputBlob, "data");
//Mat detectionMat = net.forward("detection_out");
//vector<double> layersTimings;
//double freq = getTickFrequency() / 1000;
//double time = net.getPerfProfile(layersTimings) / freq;
//ostringstream ss;
//ss << "FPS: " << 1000 / time << " ; time: " << time << " ms";
//putText(frame, ss.str(), Point(20, 20), 0, 0.5, Scalar(0, 0, 255));
// 检测
std::vector<Mat> outs;
net.forward(outs, outNames);
vector<double> layersTimings;
double freq = getTickFrequency() / 1000;
double time = net.getPerfProfile(layersTimings) / freq;
ostringstream ss;
ss << "detection time: " << time << " ms";
putText(frame, ss.str(), Point(20, 20), 0, 0.5, Scalar(0, 0, 255));
vector<Rect> boxes;
vector<int> classIds;
vector<float> confidences;
for (size_t i = 0; i < outs.size(); ++i)
{
// Network produces output blob with a shape NxC where N is a number of
// detected objects and C is a number of classes + 4 where the first 4
// numbers are [center_x, center_y, width, height]
float* data = (float*)outs[i].data;
for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
{
Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
Point classIdPoint;
double confidence;
minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
if (confidence > confidenceThreshold)
{
int centerX = (int)(data[0] * frame.cols);
int centerY = (int)(data[1] * frame.rows);
int width = (int)(data[2] * frame.cols);
int height = (int)(data[3] * frame.rows);
int left = centerX - width / 2;
int top = centerY - height / 2;
classIds.push_back(classIdPoint.x);
confidences.push_back((float)confidence);
boxes.push_back(Rect(left, top, width, height));
}
}
}
vector<int> indices;
NMSBoxes(boxes, confidences, 0.5, 0.2, indices);
for (size_t i = 0; i < indices.size(); ++i)
{
int idx = indices[i];
Rect box = boxes[idx];
String className = classNamesVec[classIds[idx]] + std::to_string(confidences[idx]);
putText(frame, className.c_str(), box.tl(), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 0, 0), 2, 8);
rectangle(frame, box, Scalar(0, 0, 255), 2, 8, 0);
}
/*for (int i = 0; i < detectionMat.rows; i++)
{
const int probability_index = 5;
const int probability_size = detectionMat.cols - probability_index;
float *prob_array_ptr = &detectionMat.at<float>(i, probability_index);
size_t objectClass = max_element(prob_array_ptr, prob_array_ptr + probability_size) - prob_array_ptr;
float confidence = detectionMat.at<float>(i, (int)objectClass + probability_index);
if (confidence > confidenceThreshold)
{
float x = detectionMat.at<float>(i, 0);
float y = detectionMat.at<float>(i, 1);
float width = detectionMat.at<float>(i, 2);
float height = detectionMat.at<float>(i, 3);
int xLeftBottom = static_cast<int>((x - width / 2) * frame.cols);
int yLeftBottom = static_cast<int>((y - height / 2) * frame.rows);
int xRightTop = static_cast<int>((x + width / 2) * frame.cols);
int yRightTop = static_cast<int>((y + height / 2) * frame.rows);
Rect object(xLeftBottom, yLeftBottom,
xRightTop - xLeftBottom,
yRightTop - yLeftBottom);
rectangle(frame, object, Scalar(0, 255, 0));
if (objectClass < classNamesVec.size())
{
ss.str("");
ss << confidence;
String conf(ss.str());
String label = String(classNamesVec[objectClass]) + ": " + conf;
int baseLine = 0;
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom),
Size(labelSize.width, labelSize.height + baseLine)),
Scalar(255, 255, 255), -1);
putText(frame, label, Point(xLeftBottom, yLeftBottom + labelSize.height),
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
}
}
}*/
imshow("YOLOv3: Detections", frame);
video_read >> frame;
if (waitKey(1) >= 0) break;
}
}
</code></pre>
<pre><code class="language-c++">#opencv3.4.1 caffe googlenet
#include<opencv2\opencv.hpp>
#include<vector>
#include<string>
using namespace cv;
using namespace dnn;
using namespace std;
String model_file = "bvlc_googlenet.caffemodel";
String model_txtfile = "deploy.prototxt";
String labels_file = "synset_words.txt";
vector<String>readLabels();
int main(int arc, char** argv) {
Mat src = imread("scr1.jpg");
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
//¶ÁÈ¡Ä£Ð͵ÄÀà±ð£šÎıŸ£©
vector<String> labels = readLabels();
//¶ÁÈ¡google_netµÄÄ£ÐͺÍÃèÊöÎÄŒþ
Net net = readNetFromCaffe(model_txtfile, model_file);
if (net.empty()) {
printf("read caffee model data failure\n");
return -1;
}
//œ«ÍŒÏñתΪgoogle_netÍøÂçÊäÈëµÄ¶ÔÏó£¬ÓÉÃèÊöÎÄŒþ¿ÉÖª£¬ÍŒÏñ³ßŽçͳһΪ224*224
Mat inputBlob = blobFromImage(src, 1.0, Size(224, 224), Scalar(104, 117, 123));
//œøÐÐÇ°ÏòŽ«²¥£¬ÓÉÃèÊöÎÄŒþ¿ÉÖª£¬µÚÒ»²ãÓÃÁË10žöŸí»ý²ã£¬ÌáÈ¡ÍŒÏñ10ÖÖ²»Í¬µÄÌØÕ÷
Mat prob;
for (int i = 0; i < 10; i++) {
net.setInput(inputBlob, "data");
prob = net.forward("prob");//×îºóÒ»²ãµÄÊä³öΪ¡°prob¡±
}
//Êä³ö
//µÃµœµÄžÅÂÊֵΪ1ÐÐ1000ÁеÄ
Mat promat = prob.reshape(1, 1);
Point classLoc;
double classProb;
minMaxLoc(promat, NULL, &classProb, NULL, &classLoc);
printf("current image classification: %s,probablity %f\n", labels.at(classLoc.x).c_str(), classProb);
putText(src, labels.at(classLoc.x), Point(20, 20), FONT_HERSHEY_COMPLEX, 1.0, Scalar(0, 0, 255), 2);
imshow("output", src);
waitKey(0);
return 0;
}
//¶ÁÈ¡Ä£Ð͵ÄÀà±ð£šÎıŸ£©
vector<String>readLabels() {
vector<String>classNames;
ifstream fp(labels_file);//Žò¿ªÎÄŒþ
if (!fp.is_open()) {//ÎÄŒþûŽò¿ª
printf("could not open the file ");
exit(-1);
}
string name;
while (!fp.eof()) {//ÎÄŒþû¶Áµœœáβ
getline(fp, name);//µÃµœÃ¿Ò»ÐУ¬·ÅµœnameÖÐ
if (name.length()) {//·Ç¿ÕÐÐ
classNames.push_back(name.substr(name.find(' ') + 1));//
}
}
fp.close();
return classNames;
}
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[linux环境配置]]></title>
<id>https://bixiaopeng0.github.io/post/linux-huan-jing-pei-zhi1</id>
<link href="https://bixiaopeng0.github.io/post/linux-huan-jing-pei-zhi1">
</link>
<updated>2019-10-07T05:27:04.000Z</updated>
<content type="html"><![CDATA[<ul>
<li>
<p>install googlepinyin</p>
<ul>
<li>卸载IBUS</li>
</ul>
<pre><code class="language-c">sudo apt-get purge ibus
</code></pre>
<ul>
<li>打开ubuntu软件中心,搜素fcitx并安装</li>
<li>sudo apt-get install fcitx-googlepinyin</li>
<li>将输入法系统换成fcitx</li>
<li>重启reboot</li>
<li>输入法简体和繁体相互切换 crtl+shift+f</li>
</ul>
</li>
<li>
<p>安装nvidia显卡驱动</p>
<ul>
<li>之前想通过下载驱动安装,但是安装的时候提示脚本失败,在网上有没找到相应的解决方法。</li>
<li>通过ubuntu驱动更新,自动安装</li>
<li>安装完了 reboot</li>
<li>在终端敲入 nvidia-smi 如果显示相应的内容则安装成功。</li>
</ul>
</li>
<li>
<p>typora</p>
<ul>
<li>直接进官网,会有相应的提示进行安装</li>
</ul>
</li>
<li>
<p>禁止ubuntu自动更新</p>
<ul>
<li>之前ubuntu自动更新,把显卡驱动整坏了,坑死我了</li>
<li>在软件更新里面,设置成never</li>
</ul>
</li>
<li>
<p>安装cuda</p>
<ul>
<li>
<p>sudo sh cuda_9.0.176_384.81_linux.run 如果提示不支持compiler 在命令后面加上-override</p>
</li>
<li>
<p>添加环境变量</p>
<ul>
<li>
<p>gedit ~/.bashrc</p>
</li>
<li>
<pre><code class="language-bash">export PATH=/usr/local/cuda-9.1/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-9.1/lib64:$LD_LIBRARY_PATH
</code></pre>
</li>
<li>
<pre><code class="language-bash">source ~/.bashrc
</code></pre>
</li>
</ul>
</li>
</ul>
</li>
<li>
<p>安装cudnn</p>
<ul>
<li>
<p>tar -xzvf cudnn-*</p>
<pre><code>sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
</code></pre>
</li>
</ul>
</li>
<li>
<p>anaconda</p>
<ul>
<li>
<pre><code>bash Anaconda3-4.2.0-Linux-x86_64.sh
</code></pre>
</li>
<li>
<p>在安装过程中添加环境变量</p>
</li>
<li>
<p>生效需要重启</p>
</li>
</ul>
</li>
<li>
<p>pycharm</p>
<ul>
<li>
<p>安装直接提取出来就行</p>
</li>
<li>
<p>创建快捷方式</p>
<ul>
<li>
<pre><code>cd /usr/share/applications
sudo gedit pycharm.desktop
</code></pre>
</li>
<li></li>
</ul>
</li>
</ul>
<p>[Desktop Entry]<br>
Version=1.0<br>
Type=Application<br>
Name=Pycharm<br>
Icon=/home/bi/Downloads/pycharm-community-2019.2.2/bin/pycharm.png<br>
Exec=sh /home/bi/Downloads/pycharm-community-2019.2.2/bin/pycharm.sh<br>
MimeType=application/x-py;<br>
Name[en_US]=pycharm</p>
<ul>
<li>
<p>gcc版本</p>
</li>
<li>
<pre><code>gcc -v 查看版本
</code></pre>
</li>
<li>
<pre><code class="language-c">gcc versions later than 6 are not supported!
gcc 降级
$ sudo apt-get install -y gcc-4.8
$ sudo apt-get install -y g++-4.8
$ cd /usr/bin
$ sudo rm gcc
$ sudo ln -s gcc-4.8 gcc
$ sudo rm g++
$ sudo ln -s g++-4.8 g++
</code></pre>
</li>
</ul>
</li>
<li>
<p>cmake cmake-gui</p>
<ul>
<li>sudo apt-get install cmake</li>
<li>sudo apt-get install cmake-qt-gui</li>
</ul>
</li>
<li>
<p>caffe</p>
<ul>
<li>最近ubnutu18.04安装caffe-ssd-mobilnet遇到各种坑,记录一下这个过程遇到的问题。</li>
<li>cmake时候,提示gcc版本过高,解决:gcc 降级</li>
<li>cmake过程,缺少依赖库,解决:安装相应的库</li>
<li>cmake过程,部分包与anaconda的包冲突,解决:在安装caffe时将anaconda的环境变量去掉。</li>
<li>编译过程中,到了90左右,报错了,显示boost未定义,这个是最终换成ubuntu16.04解决的。</li>
<li>在ubuntu16.04编译过程中,异常顺利,没有遇到之前的问题,不过也遇到其他一些问题
<ul>
<li>运行demo.py 提示错误 1vs 4....,把某个文件里面的caffe的注释取消即可,具体文件记不太清楚了。</li>
<li>在转换voc数据集到指定格式时,运行create_data.sh,提示wu_inspire这个类的未定义,但是删了这个类之后,其他类也没与问题啊,之前在其他网络用这个数据集,也没有问题的 ???</li>
<li>训练时,提示内存溢出,把batch_size改小一点即可。</li>
</ul>
</li>
</ul>
</li>
<li></li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Linux 指令]]></title>
<id>https://bixiaopeng0.github.io/post/linux-zhi-ling1</id>
<link href="https://bixiaopeng0.github.io/post/linux-zhi-ling1">
</link>
<updated>2019-09-24T07:06:30.000Z</updated>
<content type="html"><![CDATA[<!--more-->
<ul>
<li>
<p>运行.run文件</p>
<ul>
<li>chmod +x xx.run提供权限</li>
<li>./ xx.run 运行 最好前面加上权限sudo</li>
</ul>
</li>
<li>
<p>解压文件 tar -xzvf XXX.tgz</p>
</li>
<li>
<p>复制文件 cp xx xx</p>
</li>
<li>
<p>创建目录 eg: mkdir /usr/local/....</p>
</li>
<li>
<p>/usr:系统级目录 /usr/local 用户程序级目录</p>
</li>
<li>
<p>cd</p>
<ul>
<li>cd cuda 进入cuda目录</li>
</ul>
</li>
<li>
<p>crtl+空格 换输入法</p>
</li>
<li>
<p>查看nvidia板卡信息 watch -n 1 nvidia-smi<br>
待续。。。2019/09/24</p>
</li>
<li>
<p>reboot重启</p>
</li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[常犯的错误]]></title>
<id>https://bixiaopeng0.github.io/post/chang-fan-de-cuo-wu</id>
<link href="https://bixiaopeng0.github.io/post/chang-fan-de-cuo-wu">
</link>
<updated>2019-09-13T00:52:58.000Z</updated>
<content type="html"><![CDATA[<hr>
<p>title: '常犯的错误'<br>
date: 2019-09-13 08:52:58<br>
tags: []<br>
published: true<br>
hideInList: false<br>
feature: /post-images/chang-fan-de-cuo-wu.jpg</p>
<hr>
<h3 id="vs">vs</h3>
<ul>
<li>今天在调无人机代码,提示类UAV不存在,但是在头文件中是有引用的,然后调了好长时间才发现两个类的头文件宏定义判断是一样的。</li>
<li>代码比较多的时候。不要用using namespace,之前因为这个问题,opencv和windows的函数发生了冲突,然后一个一个的加上去的。</li>
</ul>
<h3 id="qt">qt</h3>
<ul>
<li>qt在中文支持不好,尽量不要用中文写。</li>
<li>命名不要用拼音,编号,否则二次开发比较麻烦。</li>
</ul>
<h3 id="opencv">opencv</h3>
<ul>
<li>
<p>多人开发,opencv版本要一致,这个之前也被坑过</p>
</li>
<li>
<p>今天在linux上配置opencv,搞了一天</p>
<ul>
<li>第一个错误是opencv在配置的时候,下载包失败,这个手动下载然后用cp放到指定目录即可。</li>
<li>之前配置opencv3.4.5一直用的cmake3.10,然而我同学用的3.12配置成功了,怀疑可能是版本问题,结果真的是。</li>
</ul>
</li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[ Socket传输图片]]></title>
<id>https://bixiaopeng0.github.io/post/socket-chuan-shu-tu-pian1</id>
<link href="https://bixiaopeng0.github.io/post/socket-chuan-shu-tu-pian1">
</link>
<updated>2019-09-07T09:28:54.000Z</updated>
<content type="html"><![CDATA[<!--more-->
<p>最近做一个项目,需要c++端和python端进行通信,c++将图片传到python端,然后python把处理完数据传回到c++端,然后就尝试了用socket通信,c++作为客户端,python作为服务器。</p>
<h2 id="文件流发送">文件流发送</h2>
<p>刚开始是用本地图片测试的,c++读取图片,发送到python端,然后python将图片数据写进硬盘,然后进行读取,然后测试</p>
<pre><code class="language-c++">#include<iostream>
#include<winsock.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
void initialization();
int main() {
int cnt = 0;
//定义长度变量
int send_len = 0;
int recv_len = 0;
//定义发送缓冲区和接受缓冲区
char send_buf[2000];
char recv_buf[2000];
//定义服务端套接字,接受请求套接字
SOCKET s_server;
//服务端地址客户端地址
SOCKADDR_IN server_addr;
initialization();
//填充服务端信息
server_addr.sin_family = AF_INET;
server_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(1234);
//创建套接字
s_server = socket(AF_INET, SOCK_STREAM, 0);
if (connect(s_server, (SOCKADDR *)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
cout << "服务器连接失败!" << endl;
WSACleanup();
}
else {
cout << "服务器连接成功!" << endl;
}
//发送图片
FILE *fq = fopen("2.jpg", "rb");
while (!feof(fq))
{
cout << cnt++ << endl;
send_len = fread(send_buf, 1, sizeof(send_buf), fq);
send(s_server, send_buf, 2000, 0);
}
send(s_server, "exit", 2000, 0);
//发送,接收数据
while (1) {
cout << "请输入发送信息:";
cin >> send_buf;
send_len = send(s_server, send_buf, 100, 0);
if (send_len < 0) {
cout << "发送失败!" << endl;
break;
}
recv_len = recv(s_server, recv_buf, 100, 0);
if (recv_len < 0) {
cout << "接受失败!" << endl;
break;
}
else {
cout << "服务端信息:" << recv_buf << endl;
}
}
//关闭套接字
closesocket(s_server);
//释放DLL资源
WSACleanup();
return 0;
}
void initialization() {
//初始化套接字库
WORD w_req = MAKEWORD(2, 2);//版本号
WSADATA wsadata;
int err;
err = WSAStartup(w_req, &wsadata);
if (err != 0) {
cout << "初始化套接字库失败!" << endl;
}
else {
cout << "初始化套接字库成功!" << endl;
}
//检测版本号
if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wHighVersion) != 2) {
cout << "套接字库版本号不符!" << endl;
WSACleanup();
}
else {
cout << "套接字库版本正确!" << endl;
}
//填充服务端地址信息
}
</code></pre>
<pre><code class="language-python">def deal_image(sock):
# print("Accept connection from {0}".format(addr)) # 查看发送端的ip和端口
cnt = 0
new_filename = "cat1.jpg"
fp = open("scr.jpg", 'wb')
pxiel = 0
#接受焦距信息
data = sock.recv(200)
string = data.decode('utf-8','ignore').split('\x00')[0]
if string[0] == 'p':
pxiel = int(string[1:3])
print(pxiel)
#接受图片数据
data = sock.recv(2000)
start_time = time.time()
while True:
# sock.send(cnt)
# print(len(data))
# print(data[0:4])
string = data.decode('utf-8','ignore').split('\x00')[0]
# print(type(data),type(string))
if string=='end':
print("over")
break
fp.write(data) # 写入图片数据
data = sock.recv(2000)
# s= dete_img.test_img(img_path).cpu().numpy()[0]
# sock.send(str(key_value[s]).encode('utf-8')) #.decode('utf-8')).encode('utf-8')
#print(s)
fp.close()
end_time = time.time()
#发送识别到的信息
str_list = ["1","2"]
str = "1,jingling,0.34,100,200,300,400,23,t16,0.34,100,200,300,400,23"
sock.send(str.encode())
str = "0.34,100,200,300,400,23"
sock.send(str.encode())
print(end_time-start_time)
sock.close()
</code></pre>
<h2 id="编码发送">编码发送</h2>
<p>后来意识到把图片数据写到硬盘和之前的文件夹通信一样,不是很安全,所以就想对视频中图片进行编码,然后服务器解码即可。</p>
<pre><code class="language-c++">vector<string> sendsocket::getSocketInfo(cv::Mat img, int p)
{
int x1 = (img.cols - img.rows) / 2;
cv::Rect rect = cv::Rect(x1, 0, img.rows, img.rows);