-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path18-01-transfer-learning.html
1258 lines (806 loc) · 31 KB
/
18-01-transfer-learning.html
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
<!DOCTYPE html>
<html>
<head>
<title>18.1 - Transfer Learning [Andrei Bursuc]</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="./assets/katex.min.css">
<link rel="stylesheet" type="text/css" href="./assets/slides.css">
<link rel="stylesheet" type="text/css" href="./assets/grid.css">
</head>
<body>
<textarea id="source">
layout: true
<!-- .center.footer[Andrei BURSUC | Transfer learning and self-supervised learning | @abursuc] -->
.center.footer[Marc LELARGE and Andrei BURSUC | Deep Learning Do It Yourself | 18.1 Transfer Learning]
---
class: center, middle, title-slide
count: false
## Transfer Learning and Self-Supervised Learning
# 18.1 Transfer Learning
<br/>
<br/>
.bold[Andrei Bursuc ]
<br/>
url: https://dataflowr.github.io/website/
.citation[
With slides from A. Karpathy, F. Fleuret, G. Louppe, C. Ollion, O. Grisel, Y. Avrithis ...]
---
class: middle, center
# Outline
## Transfer learning
## Off-the shelf networks
## Fine-tuning
## (Task) transfer learning
## Multi-task learning
## Domain adaptation
## Self-supervised learning
---
class: middle, center
# Transfer Learning
---
class: middle
# Transfer learning
- Assume two datasets $S$ and $T$
- Dataset $S$ is fully annotated, plenty of images and we can train a model $CNN_S$ on it
- Dataset $T$ is not as much annotated and/or with fewer images
+ annotations of $T$ do not necessarily overlap with $S$
- We can use the model $CNN_S$ to learn a better $CNN_T$
- This is transfer learning
---
class: middle
# Transfer learning
- Even if our dataset $T$ is not large, we can train a CNN for it
- Pre-train a CNN on the dataset $S$
- The we can do:
+ fine-tuning
+ use CNN as feature extractor
---
class: middle, center
.Q[.big[Why using a pre-trained CNN (.italic[off-the-shelf]) would be a good idea?]]
---
# Image ranking by CNN features
.center.width-100[![](images/part18/cnn_features_1.png)]
- $3$-chanel RGB input, $224 \times 224$
- AlexNet pre-trained on ImageNet for classification
.citation[A. Krizhevksy et al., Imagenet Classification with Deep Convolutional Neural Networks, NIPS 2012]
---
count: false
# Image ranking by CNN features
.center.width-100[![](images/part18/cnn_features_2.png)]
- $3$-chanel RGB input, $224 \times 224$
- AlexNet pre-trained on ImageNet for classification
- last fully connected layer ($fc\_6$): _global descriptor_ dimension $k=4096$
.citation[A. Krizhevksy et al., Imagenet Classification with Deep Convolutional Neural Networks, NIPS 2012]
---
# VGG-16
.center.width-60[![](images/part18/vgg.png)]
.citation[K. Simonyan and A. Zisserman, Very deep convolutional networks for large-scale image recognition, NIPS 2014]
---
# VGG-16
```md
Activation maps Parameters
INPUT: [224x224x3] = 150K 0
CONV3-64: [224x224x64] = 3.2M (3x3x3)x64 = 1,728
CONV3-64: [224x224x64] = 3.2M (3x3x64)x64 = 36,864
POOL2: [112x112x64] = 800K 0
CONV3-128: [112x112x128] = 1.6M (3x3x64)x128 = 73,728
CONV3-128: [112x112x128] = 1.6M (3x3x128)x128 = 147,456
POOL2: [56x56x128] = 400K 0
CONV3-256: [56x56x256] = 800K (3x3x128)x256 = 294,912
CONV3-256: [56x56x256] = 800K (3x3x256)x256 = 589,824
CONV3-256: [56x56x256] = 800K (3x3x256)x256 = 589,824
POOL2: [28x28x256] = 200K 0
CONV3-512: [28x28x512] = 400K (3x3x256)x512 = 1,179,648
CONV3-512: [28x28x512] = 400K (3x3x512)x512 = 2,359,296
CONV3-512: [28x28x512] = 400K (3x3x512)x512 = 2,359,296
POOL2: [14x14x512] = 100K 0
CONV3-512: [14x14x512] = 100K (3x3x512)x512 = 2,359,296
CONV3-512: [14x14x512] = 100K (3x3x512)x512 = 2,359,296
CONV3-512: [14x14x512] = 100K (3x3x512)x512 = 2,359,296
POOL2: [7x7x512] = 25K 0
FC: [1x1x4096] = 4096 7x7x512x4096 = 102,760,448
FC: [1x1x4096] = 4096 4096x4096 = 16,777,216
FC: [1x1x1000] = 1000 4096x1000 = 4,096,000
TOTAL activations: 24M x 4 bytes ~= 93MB / image (x2 for backward)
TOTAL parameters: 138M x 4 bytes ~= 552MB (x2 for plain SGD, x4 for Adam)
```
.credit[Slide credit: C. Ollion & O. Grisel]
.citation[K. Simonyan and A. Zisserman, Very deep convolutional networks for large-scale image recognition, NIPS 2014]
---
count: false
# VGG-16
```md
Activation maps Parameters
INPUT: [224x224x3] = 150K 0
CONV3-64: [224x224x64] = 3.2M (3x3x3)x64 = 1,728
CONV3-64: [224x224x64] = 3.2M (3x3x64)x64 = 36,864
POOL2: [112x112x64] = 800K 0
CONV3-128: [112x112x128] = 1.6M (3x3x64)x128 = 73,728
CONV3-128: [112x112x128] = 1.6M (3x3x128)x128 = 147,456
POOL2: [56x56x128] = 400K 0
CONV3-256: [56x56x256] = 800K (3x3x128)x256 = 294,912
CONV3-256: [56x56x256] = 800K (3x3x256)x256 = 589,824
CONV3-256: [56x56x256] = 800K (3x3x256)x256 = 589,824
POOL2: [28x28x256] = 200K 0
CONV3-512: [28x28x512] = 400K (3x3x256)x512 = 1,179,648
CONV3-512: [28x28x512] = 400K (3x3x512)x512 = 2,359,296
CONV3-512: [28x28x512] = 400K (3x3x512)x512 = 2,359,296
POOL2: [14x14x512] = 100K 0
CONV3-512: [14x14x512] = 100K (3x3x512)x512 = 2,359,296
CONV3-512: [14x14x512] = 100K (3x3x512)x512 = 2,359,296
CONV3-512: [14x14x512] = 100K (3x3x512)x512 = 2,359,296
POOL2: [7x7x512] = 25K 0
FC: [1x1x4096] = 4096 7x7x512x4096 = 102,760,448
*FC: [1x1x4096] = 4096 4096x4096 = 16,777,216
FC: [1x1x1000] = 1000 4096x1000 = 4,096,000
TOTAL activations: 24M x 4 bytes ~= 93MB / image (x2 for backward)
TOTAL parameters: 138M x 4 bytes ~= 552MB (x2 for plain SGD, x4 for Adam)
```
.credit[Slide credit: C. Ollion & O. Grisel]
.citation[K. Simonyan and A. Zisserman, Very deep convolutional networks for large-scale image recognition, NIPS 2014]
---
# Image ranking by CNN features
.center.width-70[![](images/part18/cnn_features_3.png)]
- query images
.citation[A. Krizhevksy et al., Imagenet Classification with Deep Convolutional Neural Networks, NIPS 2012]
---
count: false
# Image ranking by CNN features
.center.width-70[![](images/part18/cnn_features_4.png)]
- query images : nearest neighbors in ImageNet according to Euclidean distance
.citation[A. Krizhevksy et al., Imagenet Classification with Deep Convolutional Neural Networks, NIPS 2012]
---
# Sampling information from feature maps
.center.width-90[![](images/part18/mac_1.png)]
- VGG-16 last convolutional layer, $k=512$
.citation[G. Tolias et al., Particular object retrieval with integral max-pooling, ICLR 2016]
---
count: false
# Sampling information from feature maps
.center.width-90[![](images/part18/mac_2.png)]
- VGG-16 last convolutional layer, $k=512$
- global spatial max-pooling/sum
.citation[G. Tolias et al., Particular object retrieval with integral max-pooling, ICLR 2016]
---
count: false
# Sampling information from feature maps
.center.width-90[![](images/part18/mac_3.png)]
- VGG-16 last convolutional layer, $k=512$
- global spatial max-pooling/sum
- $\ell\_2$ normalization, PCA-whitening, $\ell\_2$ normalization
- _MAC_: maximum activation from convolutions
.citation[G. Tolias et al., Particular object retrieval with integral max-pooling, ICLR 2016]
---
count: false
# VGG-16
```md
Activation maps Parameters
INPUT: [224x224x3] = 150K 0
CONV3-64: [224x224x64] = 3.2M (3x3x3)x64 = 1,728
CONV3-64: [224x224x64] = 3.2M (3x3x64)x64 = 36,864
POOL2: [112x112x64] = 800K 0
CONV3-128: [112x112x128] = 1.6M (3x3x64)x128 = 73,728
CONV3-128: [112x112x128] = 1.6M (3x3x128)x128 = 147,456
POOL2: [56x56x128] = 400K 0
CONV3-256: [56x56x256] = 800K (3x3x128)x256 = 294,912
CONV3-256: [56x56x256] = 800K (3x3x256)x256 = 589,824
CONV3-256: [56x56x256] = 800K (3x3x256)x256 = 589,824
POOL2: [28x28x256] = 200K 0
CONV3-512: [28x28x512] = 400K (3x3x256)x512 = 1,179,648
CONV3-512: [28x28x512] = 400K (3x3x512)x512 = 2,359,296
CONV3-512: [28x28x512] = 400K (3x3x512)x512 = 2,359,296
POOL2: [14x14x512] = 100K 0
CONV3-512: [14x14x512] = 100K (3x3x512)x512 = 2,359,296
CONV3-512: [14x14x512] = 100K (3x3x512)x512 = 2,359,296
CONV3-512: [14x14x512] = 100K (3x3x512)x512 = 2,359,296
*POOL2: [7x7x512] = 25K 0
FC: [1x1x4096] = 4096 7x7x512x4096 = 102,760,448
FC: [1x1x4096] = 4096 4096x4096 = 16,777,216
FC: [1x1x1000] = 1000 4096x1000 = 4,096,000
TOTAL activations: 24M x 4 bytes ~= 93MB / image (x2 for backward)
TOTAL parameters: 138M x 4 bytes ~= 552MB (x2 for plain SGD, x4 for Adam)
```
.credit[Slide credit: C. Ollion & O. Grisel]
.citation[K. Simonyan and A. Zisserman, Very deep convolutional networks for large-scale image recognition, NIPS 2014]
---
# Global max-pooling: matching
.center.width-50[![](images/part18/mac_results_1.png)]
- receptive fields of 5 components of MAC vectors that contribute most to image similarity
.citation[G. Tolias et al., Particular object retrieval with integral max-pooling, ICLR 2016]
---
# Global max-pooling: matching
.center.width-90[![](images/part18/mac_results_2.png)]
- receptive fields of 5 components of MAC vectors that contribute most to image similarity
.citation[G. Tolias et al., Particular object retrieval with integral max-pooling, ICLR 2016]
---
# Global max-pooling: matching
.center.width-60[![](images/part18/mac_results_3.png)]
- receptive fields of 5 components of MAC vectors that contribute most to image similarity
.citation[G. Tolias et al., Particular object retrieval with integral max-pooling, ICLR 2016]
---
# Regional max-pooling (R-MAC)
.center.width-90[![](images/part18/r-mac_1.png)]
- VGG-16 last convolutional layer, $k=512$
- fixed mulitscale overlapping regions
.citation[G. Tolias et al., Particular object retrieval with integral max-pooling, ICLR 2016]
---
count: false
# Regional max-pooling (R-MAC)
.center.width-90[![](images/part18/r-mac_2.png)]
- VGG-16 last convolutional layer, $k=512$
- fixed mulitscale overlapping regions, spatial _max_-pooling
.citation[G. Tolias et al., Particular object retrieval with integral max-pooling, ICLR 2016]
---
count: false
# Regional max-pooling (R-MAC)
.center.width-90[![](images/part18/r-mac_3.png)]
- VGG-16 last convolutional layer, $k=512$
- fixed mulitscale overlapping regions, spatial _max_-pooling
- $\ell\_2$ normalization, PCA-whitening, $\ell\_2$ normalization
.citation[G. Tolias et al., Particular object retrieval with integral max-pooling, ICLR 2016]
---
count: false
# Regional max-pooling (R-MAC)
.center.width-90[![](images/part18/r-mac_4.png)]
- VGG-16 last convolutional layer, $k=512$
- fixed mulitscale overlapping regions, spatial _max_-pooling
- $\ell\_2$ normalization, PCA-whitening, $\ell\_2$ normalization
- _sum_-pooling over all descriptors, $\ell\_2$ normalization
.citation[G. Tolias et al., Particular object retrieval with integral max-pooling, ICLR 2016]
---
class: middle, center
.Q[.big[Can we do more with a bit of training?]]
---
class: middle
.bigger[Training a task-specific linear classifier on top of CNN features led to SoTA or nearly-SoTA results]
.center.width-70[![](images/part18/razavian.png)]
.citation[A. Razavian et al., CNN Features off-the-shelf: an Astounding Baseline for Recognition, arXiv 2014]
---
<br/><br/><br/><br/>
.center.width-60[![](images/part18/carlsson.png)]
--
count: false
##.center[.red[2021 edit: Not quite there yet]]
---
class: middle, center
# Fine-tuning
---
class: middle
# Fine-tuning
- Assume the parameters of $CNN_S$ are already a good start near our final local optimum
- Use them as the initial parameters for our new CNN for the target dataset
- This is a good solution when the dataset $T$ is relatively big
+ e.g. for Imagenet $S$ with $1\text{M}$ images, $T$ with a few thousand images
---
# Fine-tuning
.center[<img src="images/part18/finetuning.png" style="width: 600px;" />]
- Depending on the size of $T$ decide which layer to freeze and which to finetune/replace
- Use lower learning rate when fine-tuning: about $\frac{1}{10}$ of original learning rate
+ for new layers use agressive learning rate
- If $S$ and $T$ are very similar,fine-tune only fully-connected layers
- If datasets are different and you have enough data, fine-tune all layers
---
# Selecting which layers to freeze
.left-column[
<br/>
.center[Visualize highly exciting patterns across layers]
]
.right-column[.center[<img src="images/part18/deep_pipeline_5.png" height="120px">]]
.reset-column[
]
.center.width-70[![](images/part18/cnn_vis_6.png)]
.citation[M. Zeiler & R. Fergus, Visualizing and Understanding Convolutional Networks, ECCV 2014]
---
# Fine-tuning a ResNet?
.center.width-100[![](images/part18/resnet.png)]
---
count: false
# Fine-tuning a ResNet?
.center.width-100[![](images/part18/resnet.png)]
Preferably select layers to freeze main block
.center.width-70[![](images/part18/resnet_3.png)]
---
class: middle, center
.bigger[Selecting which layer to freeze is less of a problem nowadays.<br/> Common practice is to either *freeze entire network* or *freeze only first two blocks* (object detection) or *fine-tune entire network*.]
---
class: middle
.center.width-60[![](images/part18/bread_butter.jpg)]
.bigger[*Pre-trained networks* are the bread and butter in computer vision (at large) solutions, both academic and industrial.]
.bigger[Other communities are starting to follow this practice.]
---
class: middle
## .center[Pre-trained networks are the bread and butter of computer vision: <br/>_semantic segmentation_]
.grid[
.kol-6-12[
.center.width-80[![](images/part18/segnet.png)]
.caption[SegNet]
.center.width-80[![](images/part18/deeplabv3.png)]
.caption[DeepLabV3]
]
.kol-6-12[
.center.width-80[![](images/part18/unet.png)]
.caption[U-Net]
.center.width-80[![](images/part18/pspnet.png)]
.caption[PSPNet]
]
]
---
class: middle, center
# (Task) Transfer learning
---
class: middle
# Taskonomy: Disentangling Task Transfer Learning
.citation[A. Zamir et al., Taskonomy: Disentangling Task Transfer Learning, CVPR 2018]
---
class: middle
## Question
- Some vision tasks are more inter-related than others:
+ depth estimation could help surface normals estimation?
+ scene layout could help object detection?
- for some task annotation data can be more easily obtainable than others
- could we find fully computational approach for modeling the structure of the space of visual tasks?
.citation[A. Zamir et al., Taskonomy: Disentangling Task Transfer Learning, CVPR 2018]
---
# Task bank/dictionary
.grid[
.kol-6-12[
<br/> <br/> <br/>
- Task Bank
+ 26 semantic, 2d, 3d and other tasks
- Dataset
+ 4M real images
+ each image has the GT label for all tasks
- Task specific networks
+ 26 x
]
.kol-6-12[
<!-- .center[<img src="images/part18/task_dictionary.png" style="width: 400px;" />] -->
.center.width-75[![](images/part18/task_dictionary.png)]
]
]
---
# Task bank/dictionary
.center[<iframe width="900" height="450" src="https://www.youtube.com/embed/SUq1CiX-KzM" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>]
.citation[A. Zamir et al., Taskonomy: Disentangling Task Transfer Learning, CVPR 2018]
---
# Common architecture
.center[<img src="images/part18/taskonomy_architecture.png" style="width: 650px;" />]
- encoder: ResNet50
- transfer function: 2 conv layers
- decoder: 15 _fully convolutional_ layers / 2-3 _fully connected_ layers
.citation[A. Zamir et al., Taskonomy: Disentangling Task Transfer Learning, CVPR 2018]
---
# Common architecture
.center[<img src="images/part18/taskonomy_architecture.png" style="width: 650px;" />]
- _full training_ (gold standard): $120k$ training, $16k$ validation, $17k$ testing
- _fine-tuning_: $1 - 16k$
.citation[A. Zamir et al., Taskonomy: Disentangling Task Transfer Learning, CVPR 2018]
---
# Computational model
.center.width-100[![](images/part18/taskonomy_overview.png)]
.citation[A. Zamir et al., Taskonomy: Disentangling Task Transfer Learning, CVPR 2018]
---
# Computed taxonomies
.center[<img src="images/part18/taskonomy_computed_taxonomies.png" style="width: 800px;" />]
.citation[A. Zamir et al., Taskonomy: Disentangling Task Transfer Learning, CVPR 2018]
---
class: middle, center
# Multi-Task Learning (MTL)
---
class: middle
.center.width-75[![](images/part18/mtl-tesla-01-rsz.jpg)]
.credit[Image credit: A. Karpathy]
---
class: middle
.center.width-75[![](images/part18/mtl-tesla-02-rsz.png)]
.credit[Image credit: A. Karpathy]
---
class: middle
.bigger[In an autonomous vehicle there are plenty of perception tasks that need to be simultaneously adddressed.]
.hidden[
.bigger[We have two options:
1. train multiple models (one for each type of labels)
2. inject all knowledge in a single model]
]
---
count:false
class: middle
.bigger[In an autonomous vehicle there are plenty of perception tasks that need to be simultaneously adddressed.]
.bigger[We have two options:
1. train multiple models (one for each task)
.hidden[
2. inject all knowledge in a single model]
]
---
count:false
class: middle
.bigger[In an autonomous vehicle there are plenty of perception tasks that need to be simultaneously adddressed.]
.bigger[We have two options:
1. train multiple models (one for each task)
2. inject all knowledge in a single model]
---
count:false
class: middle
.bigger[In an autonomous vehicle there are plenty of perception tasks that need to be simultaneously adddressed.]
.bigger[We have two options:
1. train multiple models (one for each task)
2. **inject all knowledge in a single model**]
---
# Multi-Task Learning
.center.width-90[![](images/part18/mtl_2.png)]
.bigger[In MTL we usually have a .italic[shared backbone] (encoder) and multiple .italic["heads" ], $1+$ for each task.]
.bigger[The encoder learns useful features for all tasks, while the .italic["heads" ] are specialized.]
.credit[Image credit: A. Kendall]
---
# Multi-Task Learning
.grid[
.kol-9-12[
.center.width-100[![](images/part18/mtl_2.png)]
]
.kol-3-12[
<br/>
<br/>
.bigger[$$ \mathcal{L}\_{total} = \sum\_i w\_i \mathcal{L}\_i$$]
.bigger[$$ \sum\_i w\_i = 1$$]
]
]
.bigger[Each task has its own loss and the total loss is a weighted combination.]
.credit[Image credit: A. Kendall]
---
class: middle
## .center[Multi-Task Learning in PyTorch]
.grid[
.kol-6-12[
```
class MTLNet(nn.Module):
def __init__(self):
super(MTLNet, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(input_size, encoder_size),
nn.ReLU()
)
self.head1 = nn.Sequential(
nn.Linear(encoder_size, h1_size),
nn.ReLU(),
nn.Linear(h1_size, output1_size)
)
self.head2 = nn.Sequential(
nn.Linear(encoder_size, h2_size),
nn.ReLU(),
nn.Linear(h2_size, output2_size)
)
def forward(self, x):
shared_features = self.encoder(x)
out1 = self.head1(shared_features)
out2 = self.head2(shared_features)
return out1, out2
```
]
.kol-6-12[
]
]
---
count: false
class: middle
## .center[Multi-Task Learning in PyTorch]
.grid[
.kol-6-12[
```
class MTLNet(nn.Module):
def __init__(self):
super(MTLNet, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(input_size, encoder_size),
nn.ReLU()
)
self.head1 = nn.Sequential(
nn.Linear(encoder_size, h1_size),
nn.ReLU(),
nn.Linear(h1_size, output1_size)
)
self.head2 = nn.Sequential(
nn.Linear(encoder_size, h2_size),
nn.ReLU(),
nn.Linear(h2_size, output2_size)
)
def forward(self, x):
* shared_features = self.encoder(x)
* out1 = self.head1(shared_features)
* out2 = self.head2(shared_features)
* return out1, out2
```
]
.kol-6-12[
]
]
---
count: false
class: middle
## .center[Multi-Task Learning in PyTorch]
.grid[
.kol-6-12[
```
class MTLNet(nn.Module):
def __init__(self):
super(MTLNet, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(input_size, encoder_size),
nn.ReLU()
)
self.head1 = nn.Sequential(
nn.Linear(encoder_size, h1_size),
nn.ReLU(),
nn.Linear(h1_size, output1_size)
)
self.head2 = nn.Sequential(
nn.Linear(encoder_size, h2_size),
nn.ReLU(),
nn.Linear(h2_size, output2_size)
)
def forward(self, x):
* shared_features = self.encoder(x)
* out1 = self.head1(shared_features)
* out2 = self.head2(shared_features)
* return out1, out2
```
]
.kol-6-12[
```
for i, (x, y_task1, y_task2) in enumerate(train_loader):
...
y_pred1, y_pred2 = mtl_net(x)
loss1 = criterion(y_pred1, y_task1)
loss2 = criterion(y_pred2, y_task2)
loss = weight1 * loss1 + weight2 * loss2
optimizer.zero_grad()
loss.backward()
optimizer.step()
...
```
]
]
---
count: false
class: middle
## .center[Multi-Task Learning in PyTorch]
.grid[
.kol-6-12[
```
class MTLNet(nn.Module):
def __init__(self):
super(MTLNet, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(input_size, encoder_size),
nn.ReLU()
)
self.head1 = nn.Sequential(
nn.Linear(encoder_size, h1_size),
nn.ReLU(),
nn.Linear(h1_size, output1_size)
)
self.head2 = nn.Sequential(
nn.Linear(encoder_size, h2_size),
nn.ReLU(),
nn.Linear(h2_size, output2_size)
)
def forward(self, x):
* shared_features = self.encoder(x)
* out1 = self.head1(shared_features)
* out2 = self.head2(shared_features)
* return out1, out2
```
]
.kol-6-12[
```
for i, (x, y_task1, y_task2) in enumerate(train_loader):
...
y_pred1, y_pred2 = mtl_net(x)
* loss1 = criterion(y_pred1, y_task1)
* loss2 = criterion(y_pred2, y_task2)
* loss = weight1 * loss1 + weight2 * loss2
optimizer.zero_grad()
* loss.backward()
optimizer.step()
...
```
]
]
---
class: middle
.grid[
.kol-6-12[
.center.width-80[![](images/part18/hydra-net.jpg)]
]
.kol-6-12[
<br/><br/>
<br/><br/>
<br/><br/>
.bigger[
- Usually there are $2-5$ tasks per network
- In some cases, on autonomous vehicles you can have up to $50$ tasks per network.
]
]
]
.credit[Image credit: A. Karpathy]
---
class:middle
.big.green[Pros]
.bigger[
- highly practical solution
- shared encoder can be shared among many tasks
- MTL can lead to better regularization .cites[[Caruana (1993)]]
]
.hidden[
.big.red[Cons]
.bigger[
- balancing difficulties and impact of each task is non-trivial:
- normalize gradients from all heads .cites[[Chen et al. (2018)]]
- learn per task weights .cites[[Kendall et al. (2018), Leang et al. (2020)]]
- learn a scheduling for training heads .cites[[Leang et al. (2020)]]
]
]
.citation[R. Caruana, Multitask learning: A knowledge-based source of inductive bias, ICML 1993 .hidden[<br/> Z. Chen et al., Gradnorm: Gradient normalization for adaptive loss balancing in deep multitask networks, ICML 2018. <br/> A. Kendall et al., Multi-task learning using uncertainty to weigh losses for scene geometry and semantics, CVPR 2018 <br/>I. Leang, Dynamic Task Weighting Methods for Multi-task Networks in Autonomous Driving Systems, ITSC 2020]]