forked from ageron/handson-ml3
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path14_deep_computer_vision_with_cnns.qmd
941 lines (786 loc) · 39.8 KB
/
14_deep_computer_vision_with_cnns.qmd
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
---
title: Setup
jupyter: python3
---
**Chapter 14 – Deep Computer Vision Using Convolutional Neural Networks**
_This notebook contains all the sample code and solutions to the exercises in chapter 14._
<table align="left">
<td>
<a href="https://colab.research.google.com/github/ageron/handson-ml3/blob/main/14_deep_computer_vision_with_cnns.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
</td>
<td>
<a target="_blank" href="https://kaggle.com/kernels/welcome?src=https://github.com/ageron/handson-ml3/blob/main/14_deep_computer_vision_with_cnns.ipynb"><img src="https://kaggle.com/static/images/open-in-kaggle.svg" /></a>
</td>
</table>
This project requires Python 3.7 or above:
```{python}
#| id: TFSU3FCOpKzu
#| id: TFSU3FCOpKzu
import sys
assert sys.version_info >= (3, 7)
```
It also requires Scikit-Learn ≥ 1.0.1:
```{python}
#| id: YqCwW7cMpKzw
#| id: YqCwW7cMpKzw
from packaging import version
import sklearn
assert version.parse(sklearn.__version__) >= version.parse("1.0.1")
```
And TensorFlow ≥ 2.8:
```{python}
#| id: 0Piq5se2pKzx
#| id: 0Piq5se2pKzx
import tensorflow as tf
assert version.parse(tf.__version__) >= version.parse("2.8.0")
```
As we did in earlier chapters, let's define the default font sizes to make the figures prettier:
```{python}
#| id: 8d4TH3NbpKzx
#| id: 8d4TH3NbpKzx
import matplotlib.pyplot as plt
plt.rc('font', size=14)
plt.rc('axes', labelsize=14, titlesize=14)
plt.rc('legend', fontsize=14)
plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=10)
```
This chapter can be very slow without a GPU, so let's make sure there's one, or else issue a warning:
```{python}
#| id: Ekxzo6pOpKzy
#| id: Ekxzo6pOpKzy
# Is this notebook running on Colab or Kaggle?
IS_COLAB = "google.colab" in sys.modules
IS_KAGGLE = "kaggle_secrets" in sys.modules
if not tf.config.list_physical_devices('GPU'):
print("No GPU was detected. Neural nets can be very slow without a GPU.")
if IS_COLAB:
print("Go to Runtime > Change runtime and select a GPU hardware "
"accelerator.")
if IS_KAGGLE:
print("Go to Settings > Accelerator and select GPU.")
```
# Convolutional Layers
## Implementing Convolutional Layers With Keras
Let's load two sample images, rescale their pixel values to 0-1, and center crop them to small 70×120 images:
```{python}
#| id: I-kXsWgDpKz0
#| id: I-kXsWgDpKz0
from sklearn.datasets import load_sample_images
import tensorflow as tf
images = load_sample_images()["images"]
images = tf.keras.layers.CenterCrop(height=70, width=120)(images)
images = tf.keras.layers.Rescaling(scale=1 / 255)(images)
```
```{python}
#| id: btpkyo8ZpKz0
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: btpkyo8ZpKz0
#| outputId: da87408f-5e8f-4c2e-c21f-4b73028d64a2
images.shape
```
```{python}
#| id: Jv6KYhPzpKz0
#| id: Jv6KYhPzpKz0
tf.random.set_seed(42) # extra code – ensures reproducibility
conv_layer = tf.keras.layers.Conv2D(filters=32, kernel_size=7)
fmaps = conv_layer(images)
```
```{python}
#| id: w-mtSoL_pKz1
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: w-mtSoL_pKz1
#| outputId: 8cf6409d-6c0d-4d44-ceaa-4cc9faab21b7
fmaps.shape
```
```{python}
#| id: ttMBSh9RpKz1
#| colab: {base_uri: 'https://localhost:8080/', height: 511}
#| id: ttMBSh9RpKz1
#| outputId: 2b6ccb30-f9b9-451c-86e1-a248da78acd2
# extra code – displays the two output feature maps for each image
plt.figure(figsize=(15, 9))
for image_idx in (0, 1):
for fmap_idx in (0, 1):
plt.subplot(2, 2, image_idx * 2 + fmap_idx + 1)
plt.imshow(fmaps[image_idx, :, :, fmap_idx], cmap="gray")
plt.axis("off")
plt.show()
```
As you can see, randomly generated filters typically act like edge detectors, which is great since that's a useful tool in image processing, and that's the type of filters that a convolutional layer typically starts with. Then, during training, it gradually learns improved filters to recognize useful patterns for the task.
Now let's use zero-padding:
```{python}
#| id: HHUI5jsNpKz1
#| id: HHUI5jsNpKz1
conv_layer = tf.keras.layers.Conv2D(filters=32, kernel_size=7,
padding="same")
fmaps = conv_layer(images)
```
```{python}
#| id: iyfeZ38EpKz2
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: iyfeZ38EpKz2
#| outputId: b42fd198-58c3-4ee5-8158-6252e4eca01d
fmaps.shape
```
```{python}
#| id: 80Umgdm1pKz2
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: 80Umgdm1pKz2
#| outputId: ae8d2cf0-5b8e-4b83-d19b-db483caecb13
# extra code – shows that the output shape when we set strides=2
conv_layer = tf.keras.layers.Conv2D(filters=32, kernel_size=7, padding="same",
strides=2)
fmaps = conv_layer(images)
fmaps.shape
```
```{python}
#| id: jisXP9jfpKz2
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: jisXP9jfpKz2
#| outputId: adfe3825-4724-4f89-e984-a75988b02927
# extra code – this utility function can be useful to compute the size of the
# feature maps output by a convolutional layer. It also returns
# the number of ignored rows or columns if padding="valid", or the
# number of zero-padded rows or columns if padding="same"."""
import numpy as np
def conv_output_size(input_size, kernel_size, strides=1, padding="valid"):
if padding=="valid":
z = input_size - kernel_size + strides
output_size = z // strides
num_ignored = z % strides
return output_size, num_ignored
else:
output_size = (input_size - 1) // strides + 1
num_padded = (output_size - 1) * strides + kernel_size - input_size
return output_size, num_padded
conv_output_size(np.array([70, 120]), kernel_size=7, strides=2, padding="same")
```
Let's now look at the weights:
```{python}
#| id: vH_xhNDVpKz2
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: vH_xhNDVpKz2
#| outputId: cc8e813f-7250-4c12-b168-8f1eb64ab9aa
kernels, biases = conv_layer.get_weights()
kernels.shape
```
```{python}
#| id: zXIgK5tMpKz2
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: zXIgK5tMpKz2
#| outputId: 53e1abff-6329-4ccd-ff6e-4ad5f53bad21
biases.shape
```
```{python}
#| id: ik87xvJhpKz3
#| id: ik87xvJhpKz3
# extra code – shows how to use the tf.nn.conv2d() operation
tf.random.set_seed(42)
filters = tf.random.normal([7, 7, 3, 2])
biases = tf.zeros([2])
fmaps = tf.nn.conv2d(images, filters, strides=1, padding="SAME") + biases
```
Let's manually create two filters full of zeros, except for a vertical line of 1s in the first filter, and a horizontal one in the second filter (just like in Figure 14–5). The two output feature maps highlight vertical lines and horizontal lines, respectively. In practice you will probably never need to create filters manually, since the convolutional layers will learn them automatically.
```{python}
#| id: 7jSGHqKMpKz3
#| colab: {base_uri: 'https://localhost:8080/', height: 520}
#| id: 7jSGHqKMpKz3
#| outputId: c9e51288-dbc8-45db-dfed-c3faf49e5195
# extra code – shows how to manually create two filters to get images similar
# to those in Figure 14–5.
plt.figure(figsize=(15, 9))
filters = np.zeros([7, 7, 3, 2])
filters[:, 3, :, 0] = 1
filters[3, :, :, 1] = 1
fmaps = tf.nn.conv2d(images, filters, strides=1, padding="SAME") + biases
for image_idx in (0, 1):
for fmap_idx in (0, 1):
plt.subplot(2, 2, image_idx * 2 + fmap_idx + 1)
plt.imshow(fmaps[image_idx, :, :, fmap_idx], cmap="gray")
plt.axis("off")
plt.show()
```
Notice the dark lines at the top and bottom of the two images on the left, and on the left and right of the two images on the right? Can you guess what these are? Why were they not present in the previous figure?
You guessed it! These are artifacts due to the fact that we used zero padding in this case, while we did not use zero padding to create the feature maps in the previous figure. Because of zero padding, the two feature maps based on the vertical line filter (i.e., the two left images) could not fully activate near the top and bottom of the images. Similarly, the two feature maps based on the horizontal line filter (i.e., the two right images) could not fully activate near the left and right of the images.
# Pooling Layers
## Implementing Pooling Layers With Keras
**Max pooling**
```{python}
#| id: v4qYbnjKpKz4
#| id: v4qYbnjKpKz4
max_pool = tf.keras.layers.MaxPool2D(pool_size=2)
```
```{python}
#| id: Niwcuaw_pKz4
#| id: Niwcuaw_pKz4
output = max_pool(images)
```
```{python}
#| id: sZo5TrZ6pKz4
#| colab: {base_uri: 'https://localhost:8080/', height: 366}
#| id: sZo5TrZ6pKz4
#| outputId: 471b4713-527a-41e7-820d-ed09f24a2195
# extra code – this cells shows what max pooling with stride = 2 looks like
import matplotlib as mpl
fig = plt.figure(figsize=(12, 8))
gs = mpl.gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[2, 1])
ax1 = fig.add_subplot(gs[0, 0])
ax1.set_title("Input")
ax1.imshow(images[0]) # plot the 1st image
ax1.axis("off")
ax2 = fig.add_subplot(gs[0, 1])
ax2.set_title("Output")
ax2.imshow(output[0]) # plot the output for the 1st image
ax2.axis("off")
plt.show()
```
**Depth-wise pooling**
```{python}
#| id: ECn_PnixpKz4
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: ECn_PnixpKz4
#| outputId: ca0cd587-9a20-40cd-b69d-56ce9be47f32
# extra code – shows how to use the max_pool() op; only works on the CPU
np.random.seed(42)
fmaps = np.random.rand(2, 70, 120, 60)
with tf.device("/cpu:0"):
output = tf.nn.max_pool(fmaps, ksize=(1, 1, 1, 3), strides=(1, 1, 1, 3),
padding="VALID")
output.shape
```
```{python}
#| id: G9rV71mrpKz4
#| id: G9rV71mrpKz4
class DepthPool(tf.keras.layers.Layer):
def __init__(self, pool_size=2, **kwargs):
super().__init__(**kwargs)
self.pool_size = pool_size
def call(self, inputs):
shape = tf.shape(inputs) # shape[-1] is the number of channels
groups = shape[-1] // self.pool_size # number of channel groups
new_shape = tf.concat([shape[:-1], [groups, self.pool_size]], axis=0)
return tf.reduce_max(tf.reshape(inputs, new_shape), axis=-1)
```
```{python}
#| id: lEHRtmhXpKz5
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: lEHRtmhXpKz5
#| outputId: 16fa295e-72a1-43a6-b3c6-eb6694a1bc4f
# extra code – shows that this custom layer gives the same result as max_pool()
np.allclose(DepthPool(pool_size=3)(fmaps), output)
```
```{python}
#| id: hMJoKQxPpKz5
#| colab: {base_uri: 'https://localhost:8080/', height: 225}
#| id: hMJoKQxPpKz5
#| outputId: 8b5e494d-a7f6-4341-ba6c-2d82f2f45adc
# extra code – computes and displays the output of the depthwise pooling layer
depth_output = DepthPool(pool_size=3)(images)
plt.figure(figsize=(12, 8))
plt.subplot(1, 2, 1)
plt.title("Input")
plt.imshow(images[0]) # plot the 1st image
plt.axis("off")
plt.subplot(1, 2, 2)
plt.title("Output")
plt.imshow(depth_output[0, ..., 0], cmap="gray") # plot 1st image's output
plt.axis("off")
plt.show()
```
**Global Average Pooling**
```{python}
#| id: KW52BwBypKz5
#| id: KW52BwBypKz5
global_avg_pool = tf.keras.layers.GlobalAvgPool2D()
```
The following layer is equivalent:
```{python}
#| id: nG_X-OuTpKz5
#| id: nG_X-OuTpKz5
global_avg_pool = tf.keras.layers.Lambda(
lambda X: tf.reduce_mean(X, axis=[1, 2]))
```
```{python}
#| id: Ygy0q39xpKz5
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: Ygy0q39xpKz5
#| outputId: 39084b74-1687-458d-dd82-84277f3bd221
global_avg_pool(images)
```
# CNN Architectures
**Tackling Fashion MNIST With a CNN**
```{python}
#| id: 1IXwgw_0pKz6
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: 1IXwgw_0pKz6
#| outputId: 4cd7176d-5b2d-4bab-efd3-5ad967a2e43b
# extra code – loads the mnist dataset, add the channels axis to the inputs,
# scales the values to the 0-1 range, and splits the dataset
mnist = tf.keras.datasets.fashion_mnist.load_data()
(X_train_full, y_train_full), (X_test, y_test) = mnist
X_train_full = np.expand_dims(X_train_full, axis=-1).astype(np.float32) / 255
X_test = np.expand_dims(X_test.astype(np.float32), axis=-1) / 255
X_train, X_valid = X_train_full[:-5000], X_train_full[-5000:]
y_train, y_valid = y_train_full[:-5000], y_train_full[-5000:]
```
```{python}
#| id: 34upiak4pKz6
#| id: 34upiak4pKz6
from functools import partial
tf.random.set_seed(42) # extra code – ensures reproducibility
DefaultConv2D = partial(tf.keras.layers.Conv2D, kernel_size=3, padding="same",
activation="relu", kernel_initializer="he_normal")
model = tf.keras.Sequential([
DefaultConv2D(filters=64, kernel_size=7, input_shape=[28, 28, 1]),
tf.keras.layers.MaxPool2D(),
DefaultConv2D(filters=128),
DefaultConv2D(filters=128),
tf.keras.layers.MaxPool2D(),
DefaultConv2D(filters=256),
DefaultConv2D(filters=256),
tf.keras.layers.MaxPool2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(units=128, activation="relu",
kernel_initializer="he_normal"),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(units=64, activation="relu",
kernel_initializer="he_normal"),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(units=10, activation="softmax")
])
```
```{python}
#| id: KZbWeIBYpKz6
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: KZbWeIBYpKz6
#| outputId: fd2181cd-3092-4f03-96ff-b573a39b21ef
# extra code – compiles, fits, evaluates, and uses the model to make predictions
model.compile(loss="sparse_categorical_crossentropy", optimizer="nadam",
metrics=["accuracy"])
history = model.fit(X_train, y_train, epochs=10,
validation_data=(X_valid, y_valid))
score = model.evaluate(X_test, y_test)
X_new = X_test[:10] # pretend we have new images
y_pred = model.predict(X_new)
```
## LeNet-5
The famous LeNet-5 architecture had the following layers:
Layer | Type | Maps | Size | Kernel size | Stride | Activation
-------|-----------------|------|----------|-------------|--------|-----------
Out | Fully connected | – | 10 | – | – | RBF
F6 | Fully connected | – | 84 | – | – | tanh
C5 | Convolution | 120 | 1 × 1 | 5 × 5 | 1 | tanh
S4 | Avg pooling | 16 | 5 × 5 | 2 × 2 | 2 | tanh
C3 | Convolution | 16 | 10 × 10 | 5 × 5 | 1 | tanh
S2 | Avg pooling | 6 | 14 × 14 | 2 × 2 | 2 | tanh
C1 | Convolution | 6 | 28 × 28 | 5 × 5 | 1 | tanh
In | Input | 1 | 32 × 32 | – | – | –
There were a few tweaks here and there, which don't really matter much anymore, but in case you are interested, here they are:
* MNIST images are 28 × 28 pixels, but they are zero-padded to 32 × 32 pixels and normalized before being fed to the network. The rest of the network does not use any padding, which is why the size keeps shrinking as the image progresses through the network.
* The average pooling layers are slightly more complex than usual: each neuron computes the mean of its inputs, then multiplies the result by a learnable coefficient (one per map) and adds a learnable bias term (again, one per map), then finally applies the activation function.
* Most neurons in C3 maps are connected to neurons in only three or four S2 maps (instead of all six S2 maps). See table 1 (page 8) in the [original paper](https://homl.info/lenet5) for details.
* The output layer is a bit special: instead of computing the matrix multiplication of the inputs and the weight vector, each neuron outputs the square of the Euclidian distance between its input vector and its weight vector. Each output measures how much the image belongs to a particular digit class. The cross-entropy cost function is now preferred, as it penalizes bad predictions much more, producing larger gradients and converging faster.
# Implementing a ResNet-34 CNN Using Keras
```{python}
#| id: p9EoM1dTpKz7
#| id: p9EoM1dTpKz7
DefaultConv2D = partial(tf.keras.layers.Conv2D, kernel_size=3, strides=1,
padding="same", kernel_initializer="he_normal",
use_bias=False)
class ResidualUnit(tf.keras.layers.Layer):
def __init__(self, filters, strides=1, activation="relu", **kwargs):
super().__init__(**kwargs)
self.activation = tf.keras.activations.get(activation)
self.main_layers = [
DefaultConv2D(filters, strides=strides),
tf.keras.layers.BatchNormalization(),
self.activation,
DefaultConv2D(filters),
tf.keras.layers.BatchNormalization()
]
self.skip_layers = []
if strides > 1:
self.skip_layers = [
DefaultConv2D(filters, kernel_size=1, strides=strides),
tf.keras.layers.BatchNormalization()
]
def call(self, inputs):
Z = inputs
for layer in self.main_layers:
Z = layer(Z)
skip_Z = inputs
for layer in self.skip_layers:
skip_Z = layer(skip_Z)
return self.activation(Z + skip_Z)
```
```{python}
#| id: _0qA-kSkpKz7
#| id: _0qA-kSkpKz7
model = tf.keras.Sequential([
DefaultConv2D(64, kernel_size=7, strides=2, input_shape=[224, 224, 3]),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.MaxPool2D(pool_size=3, strides=2, padding="same"),
])
prev_filters = 64
for filters in [64] * 3 + [128] * 4 + [256] * 6 + [512] * 3:
strides = 1 if filters == prev_filters else 2
model.add(ResidualUnit(filters, strides=strides))
prev_filters = filters
model.add(tf.keras.layers.GlobalAvgPool2D())
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10, activation="softmax"))
```
# Using Pretrained Models from Keras
```{python}
#| id: wbS9p1FnpKz7
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: wbS9p1FnpKz7
#| outputId: a3d0e499-1036-478e-85af-06e882e25e21
model = tf.keras.applications.ResNet50(weights="imagenet")
```
```{python}
#| id: _QhYKi22pKz8
#| id: _QhYKi22pKz8
images = load_sample_images()["images"]
images_resized = tf.keras.layers.Resizing(height=224, width=224,
crop_to_aspect_ratio=True)(images)
```
```{python}
#| id: usbPpqkqpKz8
#| id: usbPpqkqpKz8
inputs = tf.keras.applications.resnet50.preprocess_input(images_resized)
```
```{python}
#| id: M-IYqzqRpKz8
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: M-IYqzqRpKz8
#| outputId: 5e89ff3b-8afb-4d34-a769-dbe2d70983b5
Y_proba = model.predict(inputs)
Y_proba.shape
```
```{python}
#| id: '-uWvslEcpKz8'
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: '-uWvslEcpKz8'
#| outputId: 5390390e-2edb-4bc3-b1aa-ff14bc2abfe3
top_K = tf.keras.applications.resnet50.decode_predictions(Y_proba, top=3)
for image_index in range(len(images)):
print(f"Image #{image_index}")
for class_id, name, y_proba in top_K[image_index]:
print(f" {class_id} - {name:12s} {y_proba:.2%}")
```
```{python}
#| id: alc_cnxVpKz8
#| colab: {base_uri: 'https://localhost:8080/', height: 0}
#| id: alc_cnxVpKz8
#| outputId: 9918157b-9826-4c6d-8e64-9edaff7a8dc7
# extra code – displays the cropped and resized images
plt.figure(figsize=(10, 6))
for idx in (0, 1):
plt.subplot(1, 2, idx + 1)
plt.imshow(images_resized[idx] / 255)
plt.axis("off")
plt.show()
```
# Pretrained Models for Transfer Learning
```{python}
#| id: mbktvHOXpKz8
#| colab: {base_uri: 'https://localhost:8080/', height: 208, referenced_widgets: [2839afc6cb6d4a50b0bdad1fcb7f39d1, 1c08c78c0d484eed9638ad2b757ab584, eefd1a01ef1c46e09ffbd97ad25377cf, d142189db76a4681a22f38ae252e4ebc, d441368305704ab9a3bdbe762ab340a4, 57cbb645792f45adbfab9b29aa708809, b681dc2200ad4ee397a46602e8f4f654, 0401482a18a94f22b95d5321bfa6f414, 54a90429726b4d848358cafae87ad893, 8f0660be3bf44dd48fd42cd52a507e32, f8ef3c06db574e3f88dc9a8c0bcd22ab]}
#| id: mbktvHOXpKz8
#| outputId: ee28b6fc-e112-4d2a-ad11-6bbb88c56a38
import tensorflow_datasets as tfds
dataset, info = tfds.load("tf_flowers", as_supervised=True, with_info=True)
dataset_size = info.splits["train"].num_examples
class_names = info.features["label"].names
n_classes = info.features["label"].num_classes
```
```{python}
#| id: 769isDkDpKz8
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: 769isDkDpKz8
#| outputId: 891b3c57-1212-4959-b24f-574ad366cf4d
dataset_size
```
```{python}
#| id: Nok5SNbEpKz9
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: Nok5SNbEpKz9
#| outputId: f79e7c41-6454-4ae7-a497-a60024938480
class_names
```
```{python}
#| id: D50TeDylpKz9
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: D50TeDylpKz9
#| outputId: f795a3b2-1170-49e2-8508-de275c0f1861
n_classes
```
```{python}
#| id: M-lgeD08pKz9
#| id: M-lgeD08pKz9
test_set_raw, valid_set_raw, train_set_raw = tfds.load(
"tf_flowers",
split=["train[:10%]", "train[10%:25%]", "train[25%:]"],
as_supervised=True)
```
```{python}
#| id: niSFaiTgpKz9
#| colab: {base_uri: 'https://localhost:8080/', height: 592}
#| id: niSFaiTgpKz9
#| outputId: 45879b7b-31f5-43c8-dcbd-9a1865867a17
# extra code – displays the first 9 images in the validation set
plt.figure(figsize=(12, 10))
index = 0
for image, label in valid_set_raw.take(9):
index += 1
plt.subplot(3, 3, index)
plt.imshow(image)
plt.title(f"Class: {class_names[label]}")
plt.axis("off")
plt.show()
```
All three datasets contain individual images. We need to batch them, but for this we first need to ensure they all have the same size, or else batching will not work. We can use a `Resizing` layer for this. We must also call the `tf.keras.applications.xception.preprocess_input()` function to preprocess the images appropriately for the Xception model. We will also add shuffling and prefetching to the training dataset.
```{python}
#| id: Bnz0n9XApKz9
#| id: Bnz0n9XApKz9
tf.keras.backend.clear_session() # extra code – resets layer name counter
batch_size = 32
preprocess = tf.keras.Sequential([
tf.keras.layers.Resizing(height=224, width=224, crop_to_aspect_ratio=True),
tf.keras.layers.Lambda(tf.keras.applications.xception.preprocess_input)
])
train_set = train_set_raw.map(lambda X, y: (preprocess(X), y))
train_set = train_set.shuffle(1000, seed=42).batch(batch_size).prefetch(1)
valid_set = valid_set_raw.map(lambda X, y: (preprocess(X), y)).batch(batch_size)
test_set = test_set_raw.map(lambda X, y: (preprocess(X), y)).batch(batch_size)
```
Let's take a look again at the first 9 images from the validation set: they're all 224x224 now, with values ranging from -1 to 1:
```{python}
#| id: ZL3c3i4opKz9
#| colab: {base_uri: 'https://localhost:8080/', height: 700}
#| id: ZL3c3i4opKz9
#| outputId: 38847d8d-8822-41a3-cfb2-27479aa5debe
# extra code – displays the first 9 images in the first batch of valid_set
plt.figure(figsize=(12, 12))
for X_batch, y_batch in valid_set.take(1):
for index in range(9):
plt.subplot(3, 3, index + 1)
plt.imshow((X_batch[index] + 1) / 2) # rescale to 0–1 for imshow()
plt.title(f"Class: {class_names[y_batch[index]]}")
plt.axis("off")
plt.show()
```
```{python}
#| id: Ib0cA8Y1pKz9
#| id: Ib0cA8Y1pKz9
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip(mode="horizontal", seed=42),
tf.keras.layers.RandomRotation(factor=0.05, seed=42),
tf.keras.layers.RandomContrast(factor=0.2, seed=42)
])
```
Try running the following cell multiple times to see different random data augmentations:
```{python}
#| id: w6GH5_vupKz-
#| colab: {base_uri: 'https://localhost:8080/', height: 700}
#| id: w6GH5_vupKz-
#| outputId: eeb2c924-2f4f-4aa1-bea9-951bebef4bf0
# extra code – displays the same first 9 images, after augmentation
plt.figure(figsize=(12, 12))
for X_batch, y_batch in valid_set.take(1):
X_batch_augmented = data_augmentation(X_batch, training=True)
for index in range(9):
plt.subplot(3, 3, index + 1)
# We must rescale the images to the 0-1 range for imshow(), and also
# clip the result to that range, because data augmentation may
# make some values go out of bounds (e.g., RandomContrast in this case).
plt.imshow(np.clip((X_batch_augmented[index] + 1) / 2, 0, 1))
plt.title(f"Class: {class_names[y_batch[index]]}")
plt.axis("off")
plt.show()
```
Now let's load the pretrained model, without its top layers, and replace them with our own, for the flower classification task:
```{python}
#| id: lRyCgvaKpKz-
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: lRyCgvaKpKz-
#| outputId: a825e173-8b1d-4217-a1c4-5491b49c3e82
tf.random.set_seed(42) # extra code – ensures reproducibility
base_model = tf.keras.applications.xception.Xception(weights="imagenet",
include_top=False)
avg = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)
output = tf.keras.layers.Dense(n_classes, activation="softmax")(avg)
model = tf.keras.Model(inputs=base_model.input, outputs=output)
```
```{python}
#| id: KBlyG6ElpKz-
#| id: KBlyG6ElpKz-
for layer in base_model.layers:
layer.trainable = False
```
Let's train the model for a few epochs, while keeping the base model weights fixed:
```{python}
#| id: GGxK2yPcpKz-
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: GGxK2yPcpKz-
#| outputId: 6b64214a-e104-4b6c-9b7a-3388fc9aa15f
optimizer = tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.9)
model.compile(loss="sparse_categorical_crossentropy", optimizer=optimizer,
metrics=["accuracy"])
history = model.fit(train_set, validation_data=valid_set, epochs=3)
```
```{python}
#| id: GvGMiJMLpKz-
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: GvGMiJMLpKz-
#| outputId: 91f2c96c-c058-45e0-e428-66fa6076ad56
for indices in zip(range(33), range(33, 66), range(66, 99), range(99, 132)):
for idx in indices:
print(f"{idx:3}: {base_model.layers[idx].name:22}", end="")
print()
```
Now that the weights of our new top layers are not too bad, we can make the top part of the base model trainable again, and continue training, but with a lower learning rate:
```{python}
#| id: GEUNGlhvpKz_
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: GEUNGlhvpKz_
#| outputId: c622a91d-f634-4443-b87e-8d46defdb578
for layer in base_model.layers[56:]:
layer.trainable = True
optimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9)
model.compile(loss="sparse_categorical_crossentropy", optimizer=optimizer,
metrics=["accuracy"])
history = model.fit(train_set, validation_data=valid_set, epochs=10)
```
# Classification and Localization
```{python}
#| id: k_7rd9hopKz_
#| id: k_7rd9hopKz_
tf.random.set_seed(42) # extra code – ensures reproducibility
base_model = tf.keras.applications.xception.Xception(weights="imagenet",
include_top=False)
avg = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)
class_output = tf.keras.layers.Dense(n_classes, activation="softmax")(avg)
loc_output = tf.keras.layers.Dense(4)(avg)
model = tf.keras.Model(inputs=base_model.input,
outputs=[class_output, loc_output])
model.compile(loss=["sparse_categorical_crossentropy", "mse"],
loss_weights=[0.8, 0.2], # depends on what you care most about
optimizer=optimizer, metrics=["accuracy"])
```
```{python}
#| id: gGGaA3SJpKz_
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: gGGaA3SJpKz_
#| outputId: 2e525486-d886-4ba1-c123-1c8cdf7f1b8a
# extra code – fits the model using random target bounding boxes (in real life
# you would need to create proper targets instead)
def add_random_bounding_boxes(images, labels):
fake_bboxes = tf.random.uniform([tf.shape(images)[0], 4])
return images, (labels, fake_bboxes)
fake_train_set = train_set.take(5).repeat(2).map(add_random_bounding_boxes)
model.fit(fake_train_set, epochs=2)
```
# Extra Material – How mAP Relates to Precision/Recall
```{python}
#| id: fgjxsrkLpKz_
#| id: fgjxsrkLpKz_
def maximum_precisions(precisions):
return np.flip(np.maximum.accumulate(np.flip(precisions)))
```
```{python}
#| id: pB2kJkHrpKz_
#| colab: {base_uri: 'https://localhost:8080/', height: 287}
#| id: pB2kJkHrpKz_
#| outputId: fd9f2bc1-ae06-4c60-8a0c-f4bec6577efa
recalls = np.linspace(0, 1, 11)
precisions = [0.91, 0.94, 0.96, 0.94, 0.95, 0.92, 0.80, 0.60, 0.45, 0.20, 0.10]
max_precisions = maximum_precisions(precisions)
mAP = max_precisions.mean()
plt.plot(recalls, precisions, "ro--", label="Precision")
plt.plot(recalls, max_precisions, "bo-", label="Max Precision")
plt.xlabel("Recall")
plt.ylabel("Precision")
plt.plot([0, 1], [mAP, mAP], "g:", linewidth=3, label="mAP")
plt.grid(True)
plt.axis([0, 1, 0, 1])
plt.legend(loc="lower center")
plt.show()
```
# Exercises
## 1. to 8.
1. These are the main advantages of a CNN over a fully connected DNN for image classification:
* Because consecutive layers are only partially connected and because it heavily reuses its weights, a CNN has many fewer parameters than a fully connected DNN, which makes it much faster to train, reduces the risk of overfitting, and requires much less training data.
* When a CNN has learned a kernel that can detect a particular feature, it can detect that feature anywhere in the image. In contrast, when a DNN learns a feature in one location, it can detect it only in that particular location. Since images typically have very repetitive features, CNNs are able to generalize much better than DNNs for image processing tasks such as classification, using fewer training examples.
* Finally, a DNN has no prior knowledge of how pixels are organized; it does not know that nearby pixels are close. A CNN's architecture embeds this prior knowledge. Lower layers typically identify features in small areas of the images, while higher layers combine the lower-level features into larger features. This works well with most natural images, giving CNNs a decisive head start compared to DNNs.
2. Let's compute how many parameters the CNN has.
* Since its first convolutional layer has 3 × 3 kernels, and the input has three channels (red, green, and blue), each feature map has 3 × 3 × 3 weights, plus a bias term. That's 28 parameters per feature map. Since this first convolutional layer has 100 feature maps, it has a total of 2,800 parameters. The second convolutional layer has 3 × 3 kernels and its input is the set of 100 feature maps of the previous layer, so each feature map has 3 × 3 × 100 = 900 weights, plus a bias term. Since it has 200 feature maps, this layer has 901 × 200 = 180,200 parameters. Finally, the third and last convolutional layer also has 3 × 3 kernels, and its input is the set of 200 feature maps of the previous layers, so each feature map has 3 × 3 × 200 = 1,800 weights, plus a bias term. Since it has 400 feature maps, this layer has a total of 1,801 × 400 = 720,400 parameters. All in all, the CNN has 2,800 + 180,200 + 720,400 = 903,400 parameters.<br/>
* Now let's compute how much RAM this neural network will require (at least) when making a prediction for a single instance. First let's compute the feature map size for each layer. Since we are using a stride of 2 and `"same"` padding, the horizontal and vertical dimensions of the feature maps are divided by 2 at each layer (rounding up if necessary). So, as the input channels are 200 × 300 pixels, the first layer's feature maps are 100 × 150, the second layer's feature maps are 50 × 75, and the third layer's feature maps are 25 × 38. Since 32 bits is 4 bytes and the first convolutional layer has 100 feature maps, this first layer takes up 4 × 100 × 150 × 100 = 6 million bytes (6 MB). The second layer takes up 4 × 50 × 75 × 200 = 3 million bytes (3 MB). Finally, the third layer takes up 4 × 25 × 38 × 400 = 1,520,000 bytes (about 1.5 MB). However, once a layer has been computed, the memory occupied by the previous layer can be released, so if everything is well optimized, only 6 + 3 = 9 million bytes (9 MB) of RAM will be required (when the second layer has just been computed, but the memory occupied by the first layer has not been released yet). But wait, you also need to add the memory occupied by the CNN's parameters! We computed earlier that it has 903,400 parameters, each using up 4 bytes, so this adds 3,613,600 bytes (about 3.6 MB). The total RAM required is therefore (at least) 12,613,600 bytes (about 12.6 MB).<br/>
* Lastly, let's compute the minimum amount of RAM required when training the CNN on a mini-batch of 50 images. During training TensorFlow uses backpropagation, which requires keeping all values computed during the forward pass until the reverse pass begins. So we must compute the total RAM required by all layers for a single instance and multiply that by 50. At this point, let's start counting in megabytes rather than bytes. We computed before that the three layers require respectively 6, 3, and 1.5 MB for each instance. That's a total of 10.5 MB per instance, so for 50 instances the total RAM required is 525 MB. Add to that the RAM required by the input images, which is 50 × 4 × 200 × 300 × 3 = 36 million bytes (36 MB), plus the RAM required for the model parameters, which is about 3.6 MB (computed earlier), plus some RAM for the gradients (we will neglect this since it can be released gradually as backpropagation goes down the layers during the reverse pass). We are up to a total of roughly 525 + 36 + 3.6 = 564.6 MB, and that's really an optimistic bare minimum.
3. If your GPU runs out of memory while training a CNN, here are five things you could try to solve the problem (other than purchasing a GPU with more RAM):
* Reduce the mini-batch size.
* Reduce dimensionality using a larger stride in one or more layers.
* Remove one or more layers.
* Use 16-bit floats instead of 32-bit floats.
* Distribute the CNN across multiple devices.
4. A max pooling layer has no parameters at all, whereas a convolutional layer has quite a few (see the previous questions).
5. A local response normalization layer makes the neurons that most strongly activate inhibit neurons at the same location but in neighboring feature maps, which encourages different feature maps to specialize and pushes them apart, forcing them to explore a wider range of features. It is typically used in the lower layers to have a larger pool of low-level features that the upper layers can build upon.
6. The main innovations in AlexNet compared to LeNet-5 are that it is much larger and deeper, and it stacks convolutional layers directly on top of each other, instead of stacking a pooling layer on top of each convolutional layer. The main innovation in GoogLeNet is the introduction of _inception modules_, which make it possible to have a much deeper net than previous CNN architectures, with fewer parameters. ResNet's main innovation is the introduction of skip connections, which make it possible to go well beyond 100 layers. Arguably, its simplicity and consistency are also rather innovative. SENet's main innovation was the idea of using an SE block (a two-layer dense network) after every inception module in an inception network or every residual unit in a ResNet to recalibrate the relative importance of feature maps. Xception's main innovation was the use of depthwise separable convolutional layers, which look at spatial patterns and depthwise patterns separately. Lastly, EfficientNet's main innotation was the compound scaling method, to efficiently scale a model to a larger compute budget.
7. Fully convolutional networks are neural networks composed exclusively of convolutional and pooling layers. FCNs can efficiently process images of any width and height (at least above the minimum size). They are most useful for object detection and semantic segmentation because they only need to look at the image once (instead of having to run a CNN multiple times on different parts of the image). If you have a CNN with some dense layers on top, you can convert these dense layers to convolutional layers to create an FCN: just replace the lowest dense layer with a convolutional layer with a kernel size equal to the layer's input size, with one filter per neuron in the dense layer, and using `"valid"` padding. Generally the stride should be 1, but you can set it to a higher value if you want. The activation function should be the same as the dense layer's. The other dense layers should be converted the same way, but using 1 × 1 filters. It is actually possible to convert a trained CNN this way by appropriately reshaping the dense layers' weight matrices.
8. The main technical difficulty of semantic segmentation is the fact that a lot of the spatial information gets lost in a CNN as the signal flows through each layer, especially in pooling layers and layers with a stride greater than 1. This spatial information needs to be restored somehow to accurately predict the class of each pixel.
## 9. High Accuracy CNN for MNIST
_Exercise: Build your own CNN from scratch and try to achieve the highest possible accuracy on MNIST._
The following model uses 2 convolutional layers, followed by 1 pooling layer, then dropout 25%, then a dense layer, another dropout layer but with 50% dropout, and finally the output layer. It reaches about 99.2% accuracy on the test set. This places this model roughly in the top 20% in the [MNIST Kaggle competition](https://www.kaggle.com/c/digit-recognizer/) (if we ignore the models with an accuracy greater than 99.79% which were most likely trained on the test set, as explained by Chris Deotte in [this post](https://www.kaggle.com/c/digit-recognizer/discussion/61480)). Can you do better? To reach 99.5 to 99.7% accuracy on the test set, you need to add image augmentation, batch norm, use a learning schedule such as 1-cycle, and possibly create an ensemble.
```{python}
#| id: 6tdKYb9PpK0B
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: 6tdKYb9PpK0B
#| outputId: 37baf840-d76d-4d94-f692-524eef47a041
mnist = tf.keras.datasets.mnist.load_data()
(X_train_full, y_train_full), (X_test, y_test) = mnist
X_train_full = X_train_full / 255.
X_test = X_test / 255.
X_train, X_valid = X_train_full[:-5000], X_train_full[-5000:]
y_train, y_valid = y_train_full[:-5000], y_train_full[-5000:]
X_train = X_train[..., np.newaxis]
X_valid = X_valid[..., np.newaxis]
X_test = X_test[..., np.newaxis]
```
```{python}
#| id: uDchCzo3pK0B
#| colab: {base_uri: 'https://localhost:8080/'}
#| id: uDchCzo3pK0B
#| outputId: 5e68d152-cd84-4451-db6c-d69fe7f18cc7
tf.keras.backend.clear_session()
tf.random.set_seed(42)
np.random.seed(42)
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, kernel_size=3, padding="same",
activation="relu", kernel_initializer="he_normal"),
tf.keras.layers.Conv2D(64, kernel_size=3, padding="same",
activation="relu", kernel_initializer="he_normal"),
tf.keras.layers.MaxPool2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Dense(128, activation="relu",
kernel_initializer="he_normal"),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation="softmax")
])
model.compile(loss="sparse_categorical_crossentropy", optimizer="nadam",
metrics=["accuracy"])
model.fit(X_train, y_train, epochs=10, validation_data=(X_valid, y_valid))
model.evaluate(X_test, y_test)
```
## 10. Use transfer learning for large image classification
_Exercise: Use transfer learning for large image classification, going through these steps:_
* _Create a training set containing at least 100 images per class. For example, you could classify your own pictures based on the location (beach, mountain, city, etc.), or alternatively you can use an existing dataset (e.g., from TensorFlow Datasets)._
* _Split it into a training set, a validation set, and a test set._
* _Build the input pipeline, including the appropriate preprocessing operations, and optionally add data augmentation._
* _Fine-tune a pretrained model on this dataset._
See the Flowers example above.
## 11.
_Exercise: Go through TensorFlow's [Style Transfer tutorial](https://homl.info/styletuto). It is a fun way to generate art using Deep Learning._
Simply open the Colab and follow its instructions.