1
00:00:00,020 --> 00:00:03,855
The first thing we will do is
download the set of images.

2
00:00:03,855 --> 00:00:07,050
They're stored as a zip file
containing 3,000 images,

3
00:00:07,050 --> 00:00:08,940
2,000 of which we'll
use for training,

4
00:00:08,940 --> 00:00:10,665
and 1,000 for testing.

5
00:00:10,665 --> 00:00:13,110
Once they're downloaded,
you'll need access to

6
00:00:13,110 --> 00:00:14,760
the underlying
operating system of

7
00:00:14,760 --> 00:00:17,385
the virtual machine on
which this Colab runs.

8
00:00:17,385 --> 00:00:20,115
This is available in
the OS namespace.

9
00:00:20,115 --> 00:00:22,830
This code will unzip
the cats and dogs data

10
00:00:22,830 --> 00:00:26,130
that you just downloaded,
into the directory.

11
00:00:26,130 --> 00:00:29,250
In there, the sub-directories
will be recreated,

12
00:00:29,250 --> 00:00:31,800
because they're stored
that were in the zip file.

13
00:00:31,800 --> 00:00:33,720
The next thing is
for us to set up

14
00:00:33,720 --> 00:00:35,400
our directories as variables,

15
00:00:35,400 --> 00:00:37,890
so we can point
the generators at them.

16
00:00:37,890 --> 00:00:41,090
Now, that we have the
directories as variables,

17
00:00:41,090 --> 00:00:43,670
we can pass them to
the os.listdir

18
00:00:43,670 --> 00:00:46,475
to take the files from
those directories,

19
00:00:46,475 --> 00:00:49,405
and load them into Python lists.

20
00:00:49,405 --> 00:00:51,260
We can see there's a list of

21
00:00:51,260 --> 00:00:54,815
file names like cat253.jpg, etc.

22
00:00:54,815 --> 00:00:55,325
On the files tab,

23
00:00:55,325 --> 00:00:57,350
we can also inspect
the file system and

24
00:00:57,404 --> 00:01:00,000
we can see our cats
versus dogs folder,

25
00:01:00,000 --> 00:01:01,180
within which we have

26
00:01:01,180 --> 00:01:03,300
training and validation
sub-directories,

27
00:01:03,300 --> 00:01:06,345
which each contain cats
and dogs directories.

28
00:01:06,345 --> 00:01:09,310
The images, of course,
are stored in there.

29
00:01:09,310 --> 00:01:12,259
If we then count the images
in each directory,

30
00:01:12,259 --> 00:01:15,500
we can be sure that we have
the right amounts of images.

31
00:01:15,500 --> 00:01:17,180
As we can see, we have a thousand

32
00:01:17,180 --> 00:01:18,580
of each animal in Training

33
00:01:18,580 --> 00:01:20,530
and 500 of each in Tests

34
00:01:20,530 --> 00:01:22,620
for total of 3000.

35
00:01:22,620 --> 00:01:25,400
Next up, we can visualize
some of the data,

36
00:01:25,400 --> 00:01:27,275
so we can see how diverse it is.

37
00:01:27,275 --> 00:01:29,405
This code would set a matplotlib

38
00:01:29,405 --> 00:01:32,410
which is a Python library
for drawing graphics.

39
00:01:32,410 --> 00:01:35,330
This code will pick up
some random cats and dogs

40
00:01:35,330 --> 00:01:36,710
and draw them in a grid.

41
00:01:36,710 --> 00:01:38,780
We can then see once it's drawn

42
00:01:38,780 --> 00:01:41,285
that there's a lot of
diversity in these images.

43
00:01:41,285 --> 00:01:42,980
There's different
colors of animal,

44
00:01:42,980 --> 00:01:45,200
there's different location
within the picture,

45
00:01:45,200 --> 00:01:47,580
and there's even sometimes
multiple items in the picture,

46
00:01:47,580 --> 00:01:49,805
like the lady here holding a cat.

47
00:01:49,805 --> 00:01:53,400
We can then run it
again to see some more.

48
00:01:55,520 --> 00:01:58,190
This image containing
multiple cats

49
00:01:58,190 --> 00:02:00,050
is particularly challenging.

50
00:02:00,050 --> 00:02:03,425
Okay. Now, let's build
our neural network.

51
00:02:03,425 --> 00:02:07,470
We'll import TensorFlow, and
then we'll define our model.

52
00:02:07,490 --> 00:02:09,764
We'll print the summary,

53
00:02:09,764 --> 00:02:11,630
and here you can see
the output shape

54
00:02:11,630 --> 00:02:13,795
of how the image passed
through the layers,

55
00:02:13,795 --> 00:02:15,230
and gradually reducing in

56
00:02:15,230 --> 00:02:17,540
size through the
convolution and pooling.

57
00:02:17,540 --> 00:02:20,080
Here, is where we
compile our model,

58
00:02:20,080 --> 00:02:22,680
defining the loss function
and the optimizer.

59
00:02:22,680 --> 00:02:25,645
Here, is where we set
up the two generators,

60
00:02:25,645 --> 00:02:26,930
pointing them at the training

61
00:02:26,930 --> 00:02:29,090
and validation sub-directories.

62
00:02:29,090 --> 00:02:31,655
These contains
sub-directories of their own,

63
00:02:31,655 --> 00:02:33,265
each with cats and dogs.

64
00:02:33,265 --> 00:02:35,595
When I run it, you'll
see the printout.

65
00:02:35,595 --> 00:02:38,265
It found 2,000 images
in two classes,

66
00:02:38,265 --> 00:02:39,690
and that's the training, and

67
00:02:39,690 --> 00:02:42,615
1,000 images in two classes,
that's the testing.

68
00:02:44,805 --> 00:02:46,685
Now, we'll start the training

69
00:02:46,685 --> 00:02:48,125
and we'll watch it progress.

70
00:02:48,125 --> 00:02:51,180
It should take 3-3.5 minutes.

71
00:02:59,780 --> 00:03:03,400
When it's done, you can see
the accuracy is about 73%

72
00:03:03,400 --> 00:03:04,180
and it's not bad.

73
00:03:04,180 --> 00:03:06,145
It's not great but it's not bad.

74
00:03:06,145 --> 00:03:08,590
Okay. So let's take a look at

75
00:03:08,590 --> 00:03:10,660
how the model predicts
on some images.

76
00:03:10,660 --> 00:03:12,280
So I'm going to choose
these five images

77
00:03:12,280 --> 00:03:14,170
from my hard drive
and upload them.

78
00:03:14,170 --> 00:03:16,210
Once they're uploaded,
you can see that

79
00:03:16,210 --> 00:03:18,550
the classifier gives me
a prediction on each.

80
00:03:18,550 --> 00:03:20,000
So let's now compare.

81
00:03:20,000 --> 00:03:22,650
This first one is really
impressive and as you can see,

82
00:03:22,650 --> 00:03:24,630
the dog is in
a very small part of

83
00:03:24,630 --> 00:03:26,220
the image and there's lots of

84
00:03:26,220 --> 00:03:27,960
other features like
trees, mountains,

85
00:03:27,960 --> 00:03:30,200
skies and lakes, and the dog

86
00:03:30,200 --> 00:03:32,150
also has its face turned away,

87
00:03:32,150 --> 00:03:34,435
but the classifier
still got it right.

88
00:03:34,435 --> 00:03:36,380
This one is obviously a dog,

89
00:03:36,380 --> 00:03:37,985
but again it's
partially obscured.

90
00:03:37,985 --> 00:03:39,095
You can't see the body.

91
00:03:39,095 --> 00:03:42,500
It's just a white mass and
the face while pronounced,

92
00:03:42,500 --> 00:03:46,025
is also hidden amongst
the fur, but it got it right.

93
00:03:46,025 --> 00:03:47,785
This one's a bit more obvious,

94
00:03:47,785 --> 00:03:49,220
but it's also a very different

95
00:03:49,220 --> 00:03:50,795
looking dog than
the previous one.

96
00:03:50,795 --> 00:03:54,110
The eyes, nose and mouth are
all part of a dark patch,

97
00:03:54,110 --> 00:03:57,200
but the neural network
still recognize the dog.

98
00:03:57,200 --> 00:03:59,015
This one's also very impressive.

99
00:03:59,015 --> 00:04:02,125
There are two cats in the image
and one is mostly hidden,

100
00:04:02,125 --> 00:04:03,875
but that didn't confuse the model

101
00:04:03,875 --> 00:04:06,365
that correctly
recognized the cat here.

102
00:04:06,365 --> 00:04:08,255
Strangely enough though,

103
00:04:08,255 --> 00:04:10,355
this is the one that
the model got wrong.

104
00:04:10,355 --> 00:04:12,980
Maybe it's because we
can only see one eye,

105
00:04:12,980 --> 00:04:15,050
or maybe the sharply
defined branches

106
00:04:15,050 --> 00:04:17,580
to the left confused the model.

107
00:04:17,680 --> 00:04:19,910
So take a look,

108
00:04:19,910 --> 00:04:21,710
think about what you
would do to go through

109
00:04:21,710 --> 00:04:24,235
this workbooks at this point
and have some fun with it.

110
00:04:24,235 --> 00:04:26,210
Find some images of cats and dogs

111
00:04:26,210 --> 00:04:27,470
and upload them to the notebook

112
00:04:27,470 --> 00:04:29,060
and get it to classify them,

113
00:04:29,060 --> 00:04:31,460
then see if you can
find one that you would

114
00:04:31,460 --> 00:04:34,160
expect to be easy that
the prediction gets wrong.

115
00:04:34,160 --> 00:04:36,200
Once you've done that,
see if you can edit

116
00:04:36,200 --> 00:04:39,390
that image in a way
that gets it to work.