/
3.4-yolov2-train-kangaroo-dataset.ipynb
1598 lines (1598 loc) · 852 KB
/
3.4-yolov2-train-kangaroo-dataset.ipynb
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
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 袋鼠 (Kangaroo) 物體偵測 - YOLOv2模型訓練與調整\n",
"\n",
"在[3.0: YOLO物體偵測演算法概念與介紹](https://github.com/erhwenkuo/deep-learning-with-keras-notebooks/blob/master/3.0-yolo-algorithm-introduction.ipynb)的文章裡介紹了YOLO演算法一些重要概念與其它物體偵測演算法的不同之處。\n",
"\n",
"這一篇文章則是要手把手地介紹使用[basic-yolo-keras](https://github.com/erhwenkuo/basic-yolo-keras)來將Darknet預訓練的模型進行再訓練與調整來偵測袋鼠 (Kangaroo) 圖像物體。\n",
"\n",
"![kangaroo](https://camo.githubusercontent.com/492062618889bc9ad9ee57c1af60dcd1bf8334a5/68747470733a2f2f692e696d6775722e636f6d2f76363036565a582e6a7067)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## basic-yolo-keras 專案說明\n",
"\n",
"[basic-yolo-keras](https://github.com/experiencor/basic-yolo-keras)包含在Keras中使用Tensorflow後端的YOLOv2演算方的實現。它支持訓練YOLOv2網絡與不同的網絡結構,如MobileNet和InceptionV3。\n",
"\n",
"由於原始的專案[experiencor/basic-yolo-keras](https://github.com/experiencor/basic-yolo-keras)主要以英文來說明演算法的實現與再訓練, 對於一些剛入門深度學習的學習者來說,有一些不容易理解與入手的情況。\n",
"\n",
"因此在這個專案的方向在於文件說明的中文化以外,同時也會以一些便於使用與學習的角度進行源碼的調整與修正。\n",
"\n",
"### 需求\n",
"\n",
"- [Keras](https://github.com/fchollet/keras)\n",
"- [Tensorflow](https://www.tensorflow.org/)\n",
"- [Numpy](http://www.numpy.org/)\n",
"- [h5py](http://www.h5py.org/) (For Keras model serialization.)\n",
"- [Pillow](https://pillow.readthedocs.io/) (For rendering test results.)\n",
"- [Python 3](https://www.python.org/)\n",
"- [pydot-ng](https://github.com/pydot/pydot-ng) (Optional for plotting model.)\n",
"\n",
"### 安裝\n",
"\n",
"```bash\n",
"git clone https://github.com/erhwenkuo/basic-yolo-keras.git\n",
"cd basic-yolo-keras\n",
"\n",
"pip install numpy h5py pillow\n",
"pip install tensorflow-gpu # CPU-only: conda install -c conda-forge tensorflow\n",
"pip install keras # Possibly older release: conda install keras\n",
"...\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 資料集說明\n",
"\n",
"[[experiencor/kangaroo](https://github.com/experiencor/kangaroo)]是Dat Tran.收集到的一個數據集。裡頭包含了183個圖像和註釋的數據集。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 專案的檔案路徑佈局\n",
"\n",
"1. 從[YOLO官方網站](http://pjreddie.com/darknet/yolo/)下載Darknet模型的設置檔與權重檔到專案根目錄。例如:使用MS COCO資料集訓練的預訓練模型\n",
" * 下載[YOLOv2 608x608 設置檔(yolo.cfg)](https://github.com/pjreddie/darknet/blob/master/cfg/yolo.cfg)\n",
" * 下載[YOLOv2 608x608 權重檔(yolo.weights)](https://pjreddie.com/media/files/yolo.weights)\n",
"2. 在`basic-yolo-keras`的目錄裡產生一個子目錄`data`與`data/racoon`\n",
"3. 從[experiencor/kangaroo](https://github.com/experiencor/kangaroo)的Github上點撃\"Download ZIP\"\n",
"4. 解壓縮`kangaroo-master.zip`並複製其中的`annotations`與`images`兩個目錄到`basic-yolo-keras/data`的目錄下\n",
"\n",
"最後你的目錄結構看起來像這樣: (這裡只列出來在這個範例會用到的相關檔案與目錄)\n",
"```\n",
"basic-yolo-keras/\n",
"├── xxxx.ipynb\n",
"├── yolo.weights\n",
"├── backend.py\n",
"├── preprocessing.py\n",
"├── utils.py\n",
"├── font/\n",
"│ └── FiraMono-Medium.otf\n",
"├── yolo.weights\n",
"└── data/\n",
" └── kangaroo/\n",
" ├── annotations/\n",
" └── images/\n",
" \n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### STEP 1. 載入相關函式庫"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-26T12:33:44.138409Z",
"start_time": "2017-11-26T12:33:41.531465Z"
},
"code_folding": [],
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"# Utilities相關函式庫\n",
"import os\n",
"import random\n",
"from tqdm import tqdm\n",
"\n",
"# 多維向量處理相關函式庫\n",
"import numpy as np\n",
"\n",
"# 讓Keras只使用GPU來進行訓練\n",
"os.environ[\"CUDA_DEVICE_ORDER\"] = \"PCI_BUS_ID\"\n",
"os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"\"\n",
"\n",
"# 圖像處理相關函式庫\n",
"import cv2\n",
"import imgaug as ia\n",
"from imgaug import augmenters as iaa\n",
"import colorsys\n",
"import matplotlib.pyplot as plt\n",
"from PIL import Image, ImageDraw, ImageFont\n",
"%matplotlib inline\n",
"\n",
"# 序列/反序列化相關函式庫\n",
"import pickle\n",
"\n",
"# 深度學習相關函式庫\n",
"from keras.models import Sequential, Model\n",
"from keras.layers import Reshape, Activation, Conv2D, Input, MaxPooling2D, BatchNormalization, Flatten, Dense, Lambda\n",
"from keras.layers.advanced_activations import LeakyReLU\n",
"from keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard\n",
"from keras.optimizers import SGD, Adam, RMSprop\n",
"from keras.layers.merge import concatenate\n",
"import keras.backend as K\n",
"import tensorflow as tf\n",
"\n",
"# 專案相關函式庫\n",
"from preprocessing import parse_annotation, BatchGenerator\n",
"from utils import WeightReader, decode_netout, draw_boxes, normalize\n",
"from utils import draw_bgr_image_boxes, draw_rgb_image_boxes, draw_pil_image_boxes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 設定相關設定與參數"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# 專案的根目錄路徑\n",
"ROOT_DIR = os.getcwd()\n",
"\n",
"# 訓練/驗證用的資料目錄\n",
"DATA_PATH = os.path.join(ROOT_DIR, \"data\")\n",
"\n",
"# 資料集目錄\n",
"DATA_SET_PATH = os.path.join(DATA_PATH, \"kangaroo\")\n",
"\n",
"# 資料集標註檔目錄\n",
"ANNOTATIONS_PATH = os.path.join(DATA_SET_PATH, \"annotations\")\n",
"\n",
"# 資料集圖像檔目錄\n",
"IMAGES_PATH = os.path.join(DATA_SET_PATH, \"images\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 定義圖像的類別\n",
"\n",
"在這個圖像資料集裡只有一種類別\"kangaroo\"\n",
"```\n",
"<annotation>\n",
"\t<folder>Kangaroo</folder>\n",
"\t<filename>00001.jpg</filename>\n",
"\t<path>/home/andy/Desktop/Kangaroo/stock-photo-two-kids-in-the-zoo-feeding-kangaroo-296180786.jpg</path>\n",
"\t<source>\n",
"\t\t<database>Unknown</database>\n",
"\t</source>\n",
"\t<size>\n",
"\t\t<width>450</width>\n",
"\t\t<height>319</height>\n",
"\t\t<depth>3</depth>\n",
"\t</size>\n",
"\t<segmented>0</segmented>\n",
"\t<object>\n",
"\t\t<name>kangaroo</name> ## <----- 圖像類別名稱\n",
"\t\t<pose>Unspecified</pose>\n",
"\t\t<truncated>0</truncated>\n",
"\t\t<difficult>0</difficult>\n",
"\t\t<bndbox>\n",
"\t\t\t<xmin>233</xmin>\n",
"\t\t\t<ymin>89</ymin>\n",
"\t\t\t<xmax>386</xmax>\n",
"\t\t\t<ymax>262</ymax>\n",
"\t\t</bndbox>\n",
"\t</object>\n",
"\t<object>\n",
"\t\t<name>kangaroo</name> ## <----- 圖像類別名稱\n",
"\t\t<pose>Unspecified</pose>\n",
"\t\t<truncated>0</truncated>\n",
"\t\t<difficult>0</difficult>\n",
"\t\t<bndbox>\n",
"\t\t\t<xmin>134</xmin>\n",
"\t\t\t<ymin>105</ymin>\n",
"\t\t\t<xmax>341</xmax>\n",
"\t\t\t<ymax>253</ymax>\n",
"\t\t</bndbox>\n",
"\t</object>\n",
"</annotation>\n",
"\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['kangaroo']\n"
]
}
],
"source": [
"# 圖像類別的Label-encoding\n",
"map_classes = {0: 'kangaroo'}\n",
"\n",
"# 取得所有圖像的圖像類別列表\n",
"labels=list(map_classes.values())\n",
"\n",
"print(labels)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 設定YOLOv2模型的設定與參數"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-26T12:33:52.507849Z",
"start_time": "2017-11-26T12:33:52.487930Z"
},
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"LABELS = labels # 圖像類別\n",
"\n",
"IMAGE_H, IMAGE_W = 416, 416 # 模型輸入的圖像長寬\n",
"GRID_H, GRID_W = 13 , 13\n",
"BOX = 5\n",
"CLASS = len(LABELS)\n",
"CLASS_WEIGHTS = np.ones(CLASS, dtype='float32')\n",
"OBJ_THRESHOLD = 0.3\n",
"NMS_THRESHOLD = 0.3 # NMS非極大值抑制 , 說明(https://chenzomi12.github.io/2016/12/14/YOLO-nms/)\n",
"ANCHORS = [0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828]\n",
"\n",
"NO_OBJECT_SCALE = 1.0\n",
"OBJECT_SCALE = 5.0\n",
"COORD_SCALE = 1.0\n",
"CLASS_SCALE = 1.0\n",
"\n",
"BATCH_SIZE = 16\n",
"WARM_UP_BATCHES = 0\n",
"TRUE_BOX_BUFFER = 50"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Darknet預訓練權重檔與訓練/驗證資料目錄"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-26T12:33:56.177021Z",
"start_time": "2017-11-26T12:33:56.172746Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"wt_path = 'yolo.weights' \n",
"\n",
"train_image_folder = IMAGES_PATH\n",
"train_annot_folder = ANNOTATIONS_PATH\n",
"valid_image_folder = IMAGES_PATH\n",
"valid_annot_folder = ANNOTATIONS_PATH"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### STEP 2. 構建YOLOv2網絡結構模型"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-26T12:33:58.179391Z",
"start_time": "2017-11-26T12:33:58.175696Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"# the function to implement the orgnization layer (thanks to github.com/allanzelener/YAD2K)\n",
"def space_to_depth_x2(x):\n",
" return tf.space_to_depth(x, block_size=2)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-26T12:34:01.523076Z",
"start_time": "2017-11-26T12:34:00.007828Z"
},
"code_folding": [],
"collapsed": true
},
"outputs": [],
"source": [
"input_image = Input(shape=(IMAGE_H, IMAGE_W, 3))\n",
"true_boxes = Input(shape=(1, 1, 1, TRUE_BOX_BUFFER , 4))\n",
"\n",
"# Layer 1\n",
"x = Conv2D(32, (3,3), strides=(1,1), padding='same', name='conv_1', use_bias=False)(input_image)\n",
"x = BatchNormalization(name='norm_1')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"x = MaxPooling2D(pool_size=(2, 2))(x)\n",
"\n",
"# Layer 2\n",
"x = Conv2D(64, (3,3), strides=(1,1), padding='same', name='conv_2', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_2')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"x = MaxPooling2D(pool_size=(2, 2))(x)\n",
"\n",
"# Layer 3\n",
"x = Conv2D(128, (3,3), strides=(1,1), padding='same', name='conv_3', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_3')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 4\n",
"x = Conv2D(64, (1,1), strides=(1,1), padding='same', name='conv_4', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_4')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 5\n",
"x = Conv2D(128, (3,3), strides=(1,1), padding='same', name='conv_5', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_5')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"x = MaxPooling2D(pool_size=(2, 2))(x)\n",
"\n",
"# Layer 6\n",
"x = Conv2D(256, (3,3), strides=(1,1), padding='same', name='conv_6', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_6')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 7\n",
"x = Conv2D(128, (1,1), strides=(1,1), padding='same', name='conv_7', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_7')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 8\n",
"x = Conv2D(256, (3,3), strides=(1,1), padding='same', name='conv_8', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_8')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"x = MaxPooling2D(pool_size=(2, 2))(x)\n",
"\n",
"# Layer 9\n",
"x = Conv2D(512, (3,3), strides=(1,1), padding='same', name='conv_9', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_9')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 10\n",
"x = Conv2D(256, (1,1), strides=(1,1), padding='same', name='conv_10', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_10')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 11\n",
"x = Conv2D(512, (3,3), strides=(1,1), padding='same', name='conv_11', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_11')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 12\n",
"x = Conv2D(256, (1,1), strides=(1,1), padding='same', name='conv_12', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_12')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 13\n",
"x = Conv2D(512, (3,3), strides=(1,1), padding='same', name='conv_13', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_13')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"skip_connection = x\n",
"\n",
"x = MaxPooling2D(pool_size=(2, 2))(x)\n",
"\n",
"# Layer 14\n",
"x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_14', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_14')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 15\n",
"x = Conv2D(512, (1,1), strides=(1,1), padding='same', name='conv_15', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_15')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 16\n",
"x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_16', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_16')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 17\n",
"x = Conv2D(512, (1,1), strides=(1,1), padding='same', name='conv_17', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_17')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 18\n",
"x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_18', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_18')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 19\n",
"x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_19', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_19')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 20\n",
"x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_20', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_20')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 21\n",
"skip_connection = Conv2D(64, (1,1), strides=(1,1), padding='same', name='conv_21', use_bias=False)(skip_connection)\n",
"skip_connection = BatchNormalization(name='norm_21')(skip_connection)\n",
"skip_connection = LeakyReLU(alpha=0.1)(skip_connection)\n",
"skip_connection = Lambda(space_to_depth_x2)(skip_connection)\n",
"\n",
"x = concatenate([skip_connection, x])\n",
"\n",
"# Layer 22\n",
"x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_22', use_bias=False)(x)\n",
"x = BatchNormalization(name='norm_22')(x)\n",
"x = LeakyReLU(alpha=0.1)(x)\n",
"\n",
"# Layer 23\n",
"x = Conv2D(BOX * (4 + 1 + CLASS), (1,1), strides=(1,1), padding='same', name='conv_23')(x)\n",
"output = Reshape((GRID_H, GRID_W, BOX, 4 + 1 + CLASS))(x)\n",
"\n",
"# small hack to allow true_boxes to be registered when Keras build the model \n",
"# for more information: https://github.com/fchollet/keras/issues/2790\n",
"output = Lambda(lambda args: args[0])([output, true_boxes])\n",
"\n",
"model = Model([input_image, true_boxes], output)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-26T12:34:03.819802Z",
"start_time": "2017-11-26T12:34:03.786125Z"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"__________________________________________________________________________________________________\n",
"Layer (type) Output Shape Param # Connected to \n",
"==================================================================================================\n",
"input_1 (InputLayer) (None, 416, 416, 3) 0 \n",
"__________________________________________________________________________________________________\n",
"conv_1 (Conv2D) (None, 416, 416, 32) 864 input_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_1 (BatchNormalization) (None, 416, 416, 32) 128 conv_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_1 (LeakyReLU) (None, 416, 416, 32) 0 norm_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"max_pooling2d_1 (MaxPooling2D) (None, 208, 208, 32) 0 leaky_re_lu_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_2 (Conv2D) (None, 208, 208, 64) 18432 max_pooling2d_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_2 (BatchNormalization) (None, 208, 208, 64) 256 conv_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_2 (LeakyReLU) (None, 208, 208, 64) 0 norm_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"max_pooling2d_2 (MaxPooling2D) (None, 104, 104, 64) 0 leaky_re_lu_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_3 (Conv2D) (None, 104, 104, 128 73728 max_pooling2d_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_3 (BatchNormalization) (None, 104, 104, 128 512 conv_3[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_3 (LeakyReLU) (None, 104, 104, 128 0 norm_3[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_4 (Conv2D) (None, 104, 104, 64) 8192 leaky_re_lu_3[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_4 (BatchNormalization) (None, 104, 104, 64) 256 conv_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_4 (LeakyReLU) (None, 104, 104, 64) 0 norm_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_5 (Conv2D) (None, 104, 104, 128 73728 leaky_re_lu_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_5 (BatchNormalization) (None, 104, 104, 128 512 conv_5[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_5 (LeakyReLU) (None, 104, 104, 128 0 norm_5[0][0] \n",
"__________________________________________________________________________________________________\n",
"max_pooling2d_3 (MaxPooling2D) (None, 52, 52, 128) 0 leaky_re_lu_5[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_6 (Conv2D) (None, 52, 52, 256) 294912 max_pooling2d_3[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_6 (BatchNormalization) (None, 52, 52, 256) 1024 conv_6[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_6 (LeakyReLU) (None, 52, 52, 256) 0 norm_6[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_7 (Conv2D) (None, 52, 52, 128) 32768 leaky_re_lu_6[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_7 (BatchNormalization) (None, 52, 52, 128) 512 conv_7[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_7 (LeakyReLU) (None, 52, 52, 128) 0 norm_7[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_8 (Conv2D) (None, 52, 52, 256) 294912 leaky_re_lu_7[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_8 (BatchNormalization) (None, 52, 52, 256) 1024 conv_8[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_8 (LeakyReLU) (None, 52, 52, 256) 0 norm_8[0][0] \n",
"__________________________________________________________________________________________________\n",
"max_pooling2d_4 (MaxPooling2D) (None, 26, 26, 256) 0 leaky_re_lu_8[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_9 (Conv2D) (None, 26, 26, 512) 1179648 max_pooling2d_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_9 (BatchNormalization) (None, 26, 26, 512) 2048 conv_9[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_9 (LeakyReLU) (None, 26, 26, 512) 0 norm_9[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_10 (Conv2D) (None, 26, 26, 256) 131072 leaky_re_lu_9[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_10 (BatchNormalization) (None, 26, 26, 256) 1024 conv_10[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_10 (LeakyReLU) (None, 26, 26, 256) 0 norm_10[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_11 (Conv2D) (None, 26, 26, 512) 1179648 leaky_re_lu_10[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_11 (BatchNormalization) (None, 26, 26, 512) 2048 conv_11[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_11 (LeakyReLU) (None, 26, 26, 512) 0 norm_11[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_12 (Conv2D) (None, 26, 26, 256) 131072 leaky_re_lu_11[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_12 (BatchNormalization) (None, 26, 26, 256) 1024 conv_12[0][0] \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"__________________________________________________________________________________________________\n",
"leaky_re_lu_12 (LeakyReLU) (None, 26, 26, 256) 0 norm_12[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_13 (Conv2D) (None, 26, 26, 512) 1179648 leaky_re_lu_12[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_13 (BatchNormalization) (None, 26, 26, 512) 2048 conv_13[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_13 (LeakyReLU) (None, 26, 26, 512) 0 norm_13[0][0] \n",
"__________________________________________________________________________________________________\n",
"max_pooling2d_5 (MaxPooling2D) (None, 13, 13, 512) 0 leaky_re_lu_13[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_14 (Conv2D) (None, 13, 13, 1024) 4718592 max_pooling2d_5[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_14 (BatchNormalization) (None, 13, 13, 1024) 4096 conv_14[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_14 (LeakyReLU) (None, 13, 13, 1024) 0 norm_14[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_15 (Conv2D) (None, 13, 13, 512) 524288 leaky_re_lu_14[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_15 (BatchNormalization) (None, 13, 13, 512) 2048 conv_15[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_15 (LeakyReLU) (None, 13, 13, 512) 0 norm_15[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_16 (Conv2D) (None, 13, 13, 1024) 4718592 leaky_re_lu_15[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_16 (BatchNormalization) (None, 13, 13, 1024) 4096 conv_16[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_16 (LeakyReLU) (None, 13, 13, 1024) 0 norm_16[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_17 (Conv2D) (None, 13, 13, 512) 524288 leaky_re_lu_16[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_17 (BatchNormalization) (None, 13, 13, 512) 2048 conv_17[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_17 (LeakyReLU) (None, 13, 13, 512) 0 norm_17[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_18 (Conv2D) (None, 13, 13, 1024) 4718592 leaky_re_lu_17[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_18 (BatchNormalization) (None, 13, 13, 1024) 4096 conv_18[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_18 (LeakyReLU) (None, 13, 13, 1024) 0 norm_18[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_19 (Conv2D) (None, 13, 13, 1024) 9437184 leaky_re_lu_18[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_19 (BatchNormalization) (None, 13, 13, 1024) 4096 conv_19[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_21 (Conv2D) (None, 26, 26, 64) 32768 leaky_re_lu_13[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_19 (LeakyReLU) (None, 13, 13, 1024) 0 norm_19[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_21 (BatchNormalization) (None, 26, 26, 64) 256 conv_21[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_20 (Conv2D) (None, 13, 13, 1024) 9437184 leaky_re_lu_19[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_21 (LeakyReLU) (None, 26, 26, 64) 0 norm_21[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_20 (BatchNormalization) (None, 13, 13, 1024) 4096 conv_20[0][0] \n",
"__________________________________________________________________________________________________\n",
"lambda_1 (Lambda) (None, 13, 13, 256) 0 leaky_re_lu_21[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_20 (LeakyReLU) (None, 13, 13, 1024) 0 norm_20[0][0] \n",
"__________________________________________________________________________________________________\n",
"concatenate_1 (Concatenate) (None, 13, 13, 1280) 0 lambda_1[0][0] \n",
" leaky_re_lu_20[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_22 (Conv2D) (None, 13, 13, 1024) 11796480 concatenate_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"norm_22 (BatchNormalization) (None, 13, 13, 1024) 4096 conv_22[0][0] \n",
"__________________________________________________________________________________________________\n",
"leaky_re_lu_22 (LeakyReLU) (None, 13, 13, 1024) 0 norm_22[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv_23 (Conv2D) (None, 13, 13, 30) 30750 leaky_re_lu_22[0][0] \n",
"__________________________________________________________________________________________________\n",
"reshape_1 (Reshape) (None, 13, 13, 5, 6) 0 conv_23[0][0] \n",
"__________________________________________________________________________________________________\n",
"input_2 (InputLayer) (None, 1, 1, 1, 50, 0 \n",
"__________________________________________________________________________________________________\n",
"lambda_2 (Lambda) (None, 13, 13, 5, 6) 0 reshape_1[0][0] \n",
" input_2[0][0] \n",
"==================================================================================================\n",
"Total params: 50,578,686\n",
"Trainable params: 50,558,014\n",
"Non-trainable params: 20,672\n",
"__________________________________________________________________________________________________\n"
]
}
],
"source": [
"model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### STEP 3. 載入預訓練的模型權重"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Load the weights originally provided by YOLO**"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-26T12:34:06.976188Z",
"start_time": "2017-11-26T12:34:06.232200Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"weight_reader = WeightReader(wt_path) # 初始讀取Darknet預訓練權重檔物件"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-26T12:34:11.559043Z",
"start_time": "2017-11-26T12:34:08.310987Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"handle norm_1 start\n",
"shape: (32,)\n",
"handle norm_1 completed\n",
"handle conv_1 start\n",
"handle conv_1 completed\n",
"handle norm_2 start\n",
"shape: (64,)\n",
"handle norm_2 completed\n",
"handle conv_2 start\n",
"handle conv_2 completed\n",
"handle norm_3 start\n",
"shape: (128,)\n",
"handle norm_3 completed\n",
"handle conv_3 start\n",
"handle conv_3 completed\n",
"handle norm_4 start\n",
"shape: (64,)\n",
"handle norm_4 completed\n",
"handle conv_4 start\n",
"handle conv_4 completed\n",
"handle norm_5 start\n",
"shape: (128,)\n",
"handle norm_5 completed\n",
"handle conv_5 start\n",
"handle conv_5 completed\n",
"handle norm_6 start\n",
"shape: (256,)\n",
"handle norm_6 completed\n",
"handle conv_6 start\n",
"handle conv_6 completed\n",
"handle norm_7 start\n",
"shape: (128,)\n",
"handle norm_7 completed\n",
"handle conv_7 start\n",
"handle conv_7 completed\n",
"handle norm_8 start\n",
"shape: (256,)\n",
"handle norm_8 completed\n",
"handle conv_8 start\n",
"handle conv_8 completed\n",
"handle norm_9 start\n",
"shape: (512,)\n",
"handle norm_9 completed\n",
"handle conv_9 start\n",
"handle conv_9 completed\n",
"handle norm_10 start\n",
"shape: (256,)\n",
"handle norm_10 completed\n",
"handle conv_10 start\n",
"handle conv_10 completed\n",
"handle norm_11 start\n",
"shape: (512,)\n",
"handle norm_11 completed\n",
"handle conv_11 start\n",
"handle conv_11 completed\n",
"handle norm_12 start\n",
"shape: (256,)\n",
"handle norm_12 completed\n",
"handle conv_12 start\n",
"handle conv_12 completed\n",
"handle norm_13 start\n",
"shape: (512,)\n",
"handle norm_13 completed\n",
"handle conv_13 start\n",
"handle conv_13 completed\n",
"handle norm_14 start\n",
"shape: (1024,)\n",
"handle norm_14 completed\n",
"handle conv_14 start\n",
"handle conv_14 completed\n",
"handle norm_15 start\n",
"shape: (512,)\n",
"handle norm_15 completed\n",
"handle conv_15 start\n",
"handle conv_15 completed\n",
"handle norm_16 start\n",
"shape: (1024,)\n",
"handle norm_16 completed\n",
"handle conv_16 start\n",
"handle conv_16 completed\n",
"handle norm_17 start\n",
"shape: (512,)\n",
"handle norm_17 completed\n",
"handle conv_17 start\n",
"handle conv_17 completed\n",
"handle norm_18 start\n",
"shape: (1024,)\n",
"handle norm_18 completed\n",
"handle conv_18 start\n",
"handle conv_18 completed\n",
"handle norm_19 start\n",
"shape: (1024,)\n",
"handle norm_19 completed\n",
"handle conv_19 start\n",
"handle conv_19 completed\n",
"handle norm_20 start\n",
"shape: (1024,)\n",
"handle norm_20 completed\n",
"handle conv_20 start\n",
"handle conv_20 completed\n",
"handle norm_21 start\n",
"shape: (64,)\n",
"handle norm_21 completed\n",
"handle conv_21 start\n",
"handle conv_21 completed\n",
"handle norm_22 start\n",
"shape: (1024,)\n",
"handle norm_22 completed\n",
"handle conv_22 start\n",
"handle conv_22 completed\n",
"handle conv_23 start\n",
"len: 2\n",
"handle conv_23 completed\n"
]
}
],
"source": [
"weight_reader.reset()\n",
"nb_conv = 23 # 總共有23層的卷積層\n",
"\n",
"for i in range(1, nb_conv+1):\n",
" conv_layer = model.get_layer('conv_' + str(i))\n",
" \n",
" # 在conv_1~conv_22的卷積組合裡都包含了\"conv + norm\"二層, 只有conv_23是獨立一層 \n",
" if i < nb_conv: \n",
" print(\"handle norm_\" + str(i) + \" start\") \n",
" norm_layer = model.get_layer('norm_' + str(i)) # 取得BatchNormalization層\n",
" \n",
" size = np.prod(norm_layer.get_weights()[0].shape) # 取得BatchNormalization層的參數量\n",
" print(\"shape: \", norm_layer.get_weights()[0].shape)\n",
" \n",
" beta = weight_reader.read_bytes(size)\n",
" gamma = weight_reader.read_bytes(size)\n",
" mean = weight_reader.read_bytes(size)\n",
" var = weight_reader.read_bytes(size)\n",
" weights = norm_layer.set_weights([gamma, beta, mean, var])\n",
" print(\"handle norm_\" + str(i) + \" completed\")\n",
" \n",
" if len(conv_layer.get_weights()) > 1:\n",
" print(\"handle conv_\" + str(i) + \" start\") \n",
" print(\"len:\",len(conv_layer.get_weights()))\n",
" bias = weight_reader.read_bytes(np.prod(conv_layer.get_weights()[1].shape))\n",
" kernel = weight_reader.read_bytes(np.prod(conv_layer.get_weights()[0].shape))\n",
" kernel = kernel.reshape(list(reversed(conv_layer.get_weights()[0].shape)))\n",
" kernel = kernel.transpose([2,3,1,0])\n",
" conv_layer.set_weights([kernel, bias])\n",
" print(\"handle conv_\" + str(i) + \" completed\")\n",
" else:\n",
" print(\"handle conv_\" + str(i) + \" start\") \n",
" kernel = weight_reader.read_bytes(np.prod(conv_layer.get_weights()[0].shape))\n",
" kernel = kernel.reshape(list(reversed(conv_layer.get_weights()[0].shape)))\n",
" kernel = kernel.transpose([2,3,1,0])\n",
" conv_layer.set_weights([kernel])\n",
" print(\"handle conv_\" + str(i) + \" completed\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### STEP 4. 設定要微調(fine-tune)的模型層級權重\n",
"\n",
"**Randomize weights of the last layer**\n",
"\n",
"由於在YOLOv2的模型中, 最後一層卷積層決定了最後的輸出, 讓我們重新來調整與訓練這一層的卷積層來讓預訓練的模型可以讓我們進行所需要的微調。\n",
"詳細的概念與說明,見 [1.5: 使用預先訓練的卷積網絡模型](https://github.com/erhwenkuo/deep-learning-with-keras-notebooks/blob/master/1.5-use-pretrained-model-2.ipynb)。"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-22T14:08:00.245248Z",
"start_time": "2017-11-22T14:08:00.215495Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"layer = model.layers[-4] # 找出最後一層的卷積層\n",
"weights = layer.get_weights()\n",
"\n",
"new_kernel = np.random.normal(size=weights[0].shape)/(GRID_H*GRID_W)\n",
"new_bias = np.random.normal(size=weights[1].shape)/(GRID_H*GRID_W)\n",
"\n",
"layer.set_weights([new_kernel, new_bias]) # 重初始化權重"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### STEP 5. 模型訓練"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**YOLOv2訓練用的損失函數:**"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-02-01T20:44:50.211553",
"start_time": "2017-02-01T20:44:50.206006"
}
},
"source": [
"$$\\begin{multline}\n",
"\\lambda_\\textbf{coord}\n",
"\\sum_{i = 0}^{S^2}\n",
" \\sum_{j = 0}^{B}\n",
" L_{ij}^{\\text{obj}}\n",
" \\left[\n",
" \\left(\n",
" x_i - \\hat{x}_i\n",
" \\right)^2 +\n",
" \\left(\n",
" y_i - \\hat{y}_i\n",
" \\right)^2\n",
" \\right]\n",
"\\\\\n",
"+ \\lambda_\\textbf{coord} \n",
"\\sum_{i = 0}^{S^2}\n",
" \\sum_{j = 0}^{B}\n",
" L_{ij}^{\\text{obj}}\n",
" \\left[\n",
" \\left(\n",
" \\sqrt{w_i} - \\sqrt{\\hat{w}_i}\n",
" \\right)^2 +\n",
" \\left(\n",
" \\sqrt{h_i} - \\sqrt{\\hat{h}_i}\n",
" \\right)^2\n",
" \\right]\n",
"\\\\\n",
"+ \\sum_{i = 0}^{S^2}\n",
" \\sum_{j = 0}^{B}\n",
" L_{ij}^{\\text{obj}}\n",
" \\left(\n",
" C_i - \\hat{C}_i\n",
" \\right)^2\n",
"\\\\\n",
"+ \\lambda_\\textrm{noobj}\n",
"\\sum_{i = 0}^{S^2}\n",
" \\sum_{j = 0}^{B}\n",
" L_{ij}^{\\text{noobj}}\n",
" \\left(\n",
" C_i - \\hat{C}_i\n",
" \\right)^2\n",
"\\\\\n",
"+ \\sum_{i = 0}^{S^2}\n",
"L_i^{\\text{obj}}\n",
" \\sum_{c \\in \\textrm{classes}}\n",
" \\left(\n",
" p_i(c) - \\hat{p}_i(c)\n",
" \\right)^2\n",
"\\end{multline}$$"