0
1
00:00:00,840 --> 00:00:01,130
All right.
1

2
00:00:01,170 --> 00:00:07,690
So once you've downloaded that package, you should see it as a ZIP file in your download,
2

3
00:00:07,740 --> 00:00:13,950
so just go ahead and unzip it, and you should see this folder called Flower Classification Model and
3

4
00:00:13,950 --> 00:00:20,130
it should contain three files: deploy.prototxt, flowers-labels.txt, 
4

5
00:00:20,130 --> 00:00:21,510
and oxford102.caffemodel.
5

6
00:00:21,510 --> 00:00:27,270
This is all that we're going to need in order to create our .mlmodel for our Flower Classification Model.
6

7
00:00:27,270 --> 00:00:32,820
So in order to convert this Caffe Model into a CoreML Readable Model, we're going to need to create
7

8
00:00:32,910 --> 00:00:34,440
a script in Python.
8

9
00:00:34,500 --> 00:00:38,150
So don't worry you don't need to know a lot of Python and just follow along with me
9

10
00:00:38,160 --> 00:00:40,040
if you're completely new to the language.
10

11
00:00:40,170 --> 00:00:43,870
So in order to do that, we're first going to need a text editor or a code editor.
11

12
00:00:43,980 --> 00:00:47,520
So if you already have a code editor that you quite like, then just go ahead and use that,
12

13
00:00:47,520 --> 00:00:52,230
so something like Sublime or my favorite Atom will be a good fit for this job.
13

14
00:00:52,320 --> 00:00:57,420
But if you don't have a text editor, then I recommend you should just go to atom.io and download Atom for
14

15
00:00:57,420 --> 00:00:57,870
free.
15

16
00:00:57,870 --> 00:01:02,820
So once you open Atom, in the left navigation bar, you should see your desktop by default.
16

17
00:01:02,820 --> 00:01:09,030
Now, if you don't, you can also go to File, Add Project folder, and select whichever folder it is that you
17

18
00:01:09,030 --> 00:01:10,400
are developing out of.
18

19
00:01:10,410 --> 00:01:18,540
So here, if you right-click on desktop, you can create a new folder, and we're going to call it Flower Classification.
19

20
00:01:19,350 --> 00:01:26,280
And in here, we're going to create a new file, and that file is going to be called
20

21
00:01:26,550 --> 00:01:27,880
convert-script.py.
21

22
00:01:28,020 --> 00:01:33,180
Now, in order to load up those files that we downloaded and convert it using our convert script, as well
22

23
00:01:33,180 --> 00:01:39,300
as CoremlTools, we're first going to need to put those three files that we downloaded into the same
23

24
00:01:39,300 --> 00:01:43,890
directory as our convert-script, so that's the flour classification directory.
24

25
00:01:43,890 --> 00:01:48,560
So if we open up this Flower Classification Directory, you can see it at the moment only contains
25

26
00:01:48,570 --> 00:01:55,140
convert-script. py, which we just created, and all we can do is just drag these three files that we've unzipped
26

27
00:01:55,470 --> 00:01:58,840
into this new folder called Flower Classification.
27

28
00:01:58,980 --> 00:02:05,110
So this way the convert-script rests in the same directory as the model files.
28

29
00:02:05,130 --> 00:02:10,380
So, now if we head back into Atom, you can see that now they all appear and they're the same hierarchical
29

30
00:02:10,380 --> 00:02:10,940
level,
30

31
00:02:11,010 --> 00:02:15,800
so we can begin writing our Python script to convert our Flower Classification Model.
31

32
00:02:16,020 --> 00:02:21,270
The first thing that we're going to need is we're going to need to import the CoremlTools Library.
32

33
00:02:21,300 --> 00:02:24,510
So this is what we downloaded and installed in the last lesson.
33

34
00:02:24,690 --> 00:02:29,660
If you haven't done the last lesson, please go back because, otherwise, it won't work without it.
34

35
00:02:30,000 --> 00:02:34,560
So once we import CoremlTools, then we're going to be able to use some of the code and some of the
35

36
00:02:34,560 --> 00:02:40,110
methods that are inside that package. And if you have a look at the documentation for CoremlTools, then we
36

37
00:02:40,110 --> 00:02:42,000
can see what that method looks like.
37

38
00:02:42,000 --> 00:02:44,020
So I've linked to this file in this lesson,
38

39
00:02:44,190 --> 00:02:49,740
but you can also go there by just going to pythonhosted.org/coremltools. And you can see that
39

40
00:02:49,770 --> 00:02:55,410
it gives you a little bit of explaining what CoremlTools is, and what it does, and how to install it.
40

41
00:02:55,440 --> 00:02:56,750
Although, we've already done that,
41

42
00:02:56,760 --> 00:03:00,210
now the part that we're interested in is the Model Conversion.
42

43
00:03:00,420 --> 00:03:04,770
And if you have a look in the left-hand side here, you can see that there's Converters, Models, and Utilities.
43

44
00:03:05,270 --> 00:03:10,080
We're more concerned with the Converters, and more specifically, the Caffe Converter.
44

45
00:03:10,200 --> 00:03:15,450
So if you click on that, it'll take you to the converter that's going to help you convert Caffe models.
45

46
00:03:15,780 --> 00:03:24,190
And you can see that the method is using coremltools.converters.caffe.convert,
46

47
00:03:24,330 --> 00:03:27,320
and then you've got a whole bunch of parameters that it takes,
47

48
00:03:27,360 --> 00:03:29,870
some are required and some are optional.
48

49
00:03:29,940 --> 00:03:37,320
So the parameters that we're going to be concerned with is, firstly, the model which is a string that--which is
49

50
00:03:37,320 --> 00:03:43,380
going to be a string that describes the path to the particular model, so that the converter tool can
50

51
00:03:43,380 --> 00:03:46,810
navigate to the model that you want to convert.
51

52
00:03:46,830 --> 00:03:53,520
Now, the second parameter that we're going to be using is this one called class_labels, and this is a
52

53
00:03:53,580 --> 00:04:00,810
string that describes the file path to a document where all the classes are named.
53

54
00:04:00,930 --> 00:04:06,960
So if you have a look in here, we've got this flower-labels.txt and if you open it, you can see that
54

55
00:04:06,960 --> 00:04:11,760
it's basically just a list of all the classes or all the names of the flowers.
55

56
00:04:11,790 --> 00:04:17,520
And the third thing that we're going to need is this Image_ Input_names as a string or as an array of
56

57
00:04:17,520 --> 00:04:18,410
strings,
57

58
00:04:18,480 --> 00:04:25,190
and this is the name of the input blob/s of the cafe model. And the Caffe, usually by default,
58

59
00:04:25,200 --> 00:04:27,510
this is the string called data.
59

60
00:04:27,510 --> 00:04:30,880
So if we head back into Atom and we open deploy.prototxt,
60

61
00:04:30,990 --> 00:04:35,440
you can see that you've got this key called input and its value is data.
61

62
00:04:35,700 --> 00:04:39,050
And this is, by default, true for most Caffe models,
62

63
00:04:39,060 --> 00:04:44,250
but if you're using a different Caffe model just go in and check to see what that value actually is.
63

64
00:04:44,250 --> 00:04:48,810
So let's put it all together and let's create our conversion method.
64

65
00:04:48,810 --> 00:04:54,480
The first thing I'm going to do is I'm going to create a variable called Caffe model and this is going
65

66
00:04:54,480 --> 00:04:57,870
to be set to the file path of my Caffe model.
66

67
00:04:57,870 --> 00:05:03,270
And in this case, because all three of my model files are actually in the same directory as my convert script,
67

68
00:05:03,420 --> 00:05:07,770
then I only need to specify their names, instead of specifying a path.
68

69
00:05:07,770 --> 00:05:12,070
So instead of something like dot, slash, blah-blah, slash, blah-blah, slash, blah-blah,
69

70
00:05:12,330 --> 00:05:20,100
all I need to do is just to write the name of where that model lies. And that model is called 
70

71
00:05:20,160 --> 00:05:28,800
"oxford102.caffemodel," and the other thing I need to specify is the deploy.prototxt.
71

72
00:05:28,890 --> 00:05:35,250
And this is basically a file that describes the architecture of the Caffe model so that it can be understood.
72

73
00:05:35,490 --> 00:05:38,600
So we're going to specify a file path to that as well
73

74
00:05:38,640 --> 00:05:42,910
and you can see it's called "deploy.prototxt."
74

75
00:05:42,930 --> 00:05:45,600
It's really important that that text is not fully spelled out.
75

76
00:05:45,750 --> 00:05:47,800
It's like the .txt extension.
76

77
00:05:48,000 --> 00:05:54,090
So it's really important just to make sure that you don't have any typos here because anything that
77

78
00:05:54,120 --> 00:05:58,740
is not the same as what you see on screen is going to cause you some problems down the line.
78

79
00:05:58,740 --> 00:06:00,920
So just double and triple check.
79

80
00:06:00,960 --> 00:06:07,430
Now, the next variable I'm going to create is called labels and this is going to refer to that 
80

81
00:06:07,440 --> 00:06:10,780
flower-labels file that contains all the names of the classes.
81

82
00:06:10,800 --> 00:06:15,760
So this is going to be called "flower-labels.txt."
82

83
00:06:15,870 --> 00:06:18,730
And again, it's T-X-T, rather than T-E-X- T.
83

84
00:06:18,750 --> 00:06:20,280
So just make sure you check that.
84

85
00:06:20,640 --> 00:06:23,440
And let's double-check to make sure that these files are the same.
85

86
00:06:23,440 --> 00:06:25,490
oxford102.caffemodel.
86

87
00:06:25,650 --> 00:06:26,250
Oh, there we go.
87

88
00:06:26,250 --> 00:06:27,640
We've already got a typo there.
88

89
00:06:27,650 --> 00:06:33,500
So oxford102.caffemodel and deploy.prototxt.
89

90
00:06:33,520 --> 00:06:36,040
Geez, I make a lot of typos.
90

91
00:06:36,100 --> 00:06:39,540
And then, it's a "flower-labels.txt.
91

92
00:06:39,570 --> 00:06:41,250
So check your own file if you're coding along.
92

93
00:06:41,250 --> 00:06:46,410
It's really, really easy to make typos, especially when you don't have good old code helping you in pointing
93

94
00:06:46,410 --> 00:06:47,840
out the errors.
94

95
00:06:47,850 --> 00:06:53,130
All right. So, now that we've got these two variables, Caffe model and label, we're finally going to invoke
95

96
00:06:53,400 --> 00:06:59,990
that conversion method, and I'm going to store the outputs of the conversion in a variable called
96

97
00:07:00,020 --> 00:07:01,010
coreml_model.
97

98
00:07:01,050 --> 00:07:09,360
And this is going to be set to equal to coremltools.converters.caffe.convert
98

99
00:07:12,220 --> 00:07:21,640
and the parameters I'm going to use in here are the Caffe model that we created up here,
99

100
00:07:23,800 --> 00:07:25,150
also--
100

101
00:07:25,270 --> 00:07:28,480
Also the class_labels,
101

102
00:07:31,010 --> 00:07:40,280
and finally, that last parameter which is called image_input_names='data.'
102

103
00:07:40,490 --> 00:07:44,140
So that comes from here, remember?
103

104
00:07:44,420 --> 00:07:47,960
And these are the names of those parameters
104

105
00:07:48,670 --> 00:07:50,150
which you can see in here.
105

106
00:07:50,200 --> 00:07:57,820
So we're using the model, we're using the image_input_names, and we're also using the class_labels.
106

107
00:07:57,820 --> 00:08:04,150
All right, so just check to make sure that you don't have any typos like I did but, otherwise, once that
107

108
00:08:04,150 --> 00:08:10,960
conversion is done and the output of that conversion has been stored inside this variable, we're going
108

109
00:08:10,960 --> 00:08:14,490
to save that variable into a .mlmodel file.
109

110
00:08:14,530 --> 00:08:19,940
So we're going to write coreml_model.save, and you can give it a name,
110

111
00:08:19,990 --> 00:08:25,500
so I'm going to call it 'FlowerClassifier.mlmodel.
111

112
00:08:25,540 --> 00:08:30,460
Now, the extension is really important. It has to be .mlmodel because this is the only format that's
112

113
00:08:30,460 --> 00:08:31,710
readable by CoreML.
113

114
00:08:31,810 --> 00:08:36,280
But the name of the mlmodel, you can call anything you like, but make sure that you call it something
114

115
00:08:36,280 --> 00:08:40,900
descriptive and something that makes sense to other people or, you know, if you can't think of anything
115

116
00:08:40,900 --> 00:08:43,550
better, then just call it FlowerClassifier.mlmodel.
116

117
00:08:43,600 --> 00:08:46,950
That way, you'll be able to do exactly what I'm doing in the later lessons.
117

118
00:08:47,260 --> 00:08:53,710
All right. So once that's done and we've double and triple checked everything and everything looks good,
118

119
00:08:54,100 --> 00:08:55,660
then we're just going to hit save.
119

120
00:08:55,750 --> 00:08:58,370
And now, we're going to run it in our Terminal.
120

121
00:08:58,390 --> 00:09:05,590
So if I pull up the Terminal and, remember, I need to run that script inside a Python 2.7 virtual environment.
121

122
00:09:05,590 --> 00:09:08,910
Now, if you remember the path, then it's pretty easy to just cd into it.
122

123
00:09:09,040 --> 00:09:16,810
But a slightly quicker and easier way sometimes is just by typing cd, and then dragging the file directory
123

124
00:09:17,020 --> 00:09:24,130
that you want to navigate to into the Terminal, and you can see that it outputs the entire file path.
124

125
00:09:24,130 --> 00:09:27,340
And if you hit enter, you're now inside environments.
125

126
00:09:27,340 --> 00:09:32,920
So inside here, we are going to activate our Python 2.7 environment.
126

127
00:09:32,950 --> 00:09:43,910
So, remember, it's source python--what did we call it? python27/bin/activate, hit enter.
127

128
00:09:44,110 --> 00:09:47,110
We are now running under python27.
128

129
00:09:47,110 --> 00:09:51,730
So the next thing I want to do is I want to head into this Flower Classification Directory so that we
129

130
00:09:51,730 --> 00:09:53,840
can run our convert-script.py.
130

131
00:09:54,100 --> 00:09:58,440
So to do that,I'm going to use that trick that I showed you early on, rather than navigating around.
131

132
00:09:58,600 --> 00:10:03,870
So I'm just going to write cd, and then drag that into there, and then hit enter.
132

133
00:10:03,910 --> 00:10:06,680
And now, I'm inside Flower Classification.
133

134
00:10:06,790 --> 00:10:07,370
So cool.
134

135
00:10:07,450 --> 00:10:14,160
So, now I am at this level in my directory and I'm going to run the convert-script.py.
135

136
00:10:14,160 --> 00:10:17,590
All right. So I'm just going to drag that out so that it doesn't wrap that line,
136

137
00:10:17,860 --> 00:10:28,090
and then in order to run that Python script, all I need to do is type python convert-script.py,
137

138
00:10:28,090 --> 00:10:30,450
and just double-check to make sure that you spell everything correctly.
138

139
00:10:30,460 --> 00:10:34,060
python convert-script.py looks exactly the same.
139

140
00:10:34,060 --> 00:10:34,330
All right.
140

141
00:10:34,330 --> 00:10:39,830
Hit enter. And now, it might take a little while for it to do its job.
141

142
00:10:40,000 --> 00:10:45,160
So this is a good point to go and grab a cup of tea and come back, and it should be done.
142

143
00:10:45,190 --> 00:10:50,240
Depending on the processing speed of the computer, this can take anywhere between five to ten minutes.
143

144
00:10:50,260 --> 00:10:50,560
All right.
144

145
00:10:50,560 --> 00:10:52,270
So I've made myself a cup of tea.
145

146
00:10:52,300 --> 00:10:53,520
so if you hear any sipping sounds,
146

147
00:10:53,530 --> 00:10:54,600
don't be alarmed.
147

148
00:10:54,610 --> 00:10:57,090
So when in England, drink plenty of tea.
148

149
00:10:57,180 --> 00:11:02,410
Now, as you can see this process has completed, and you can tell because your prompt is back, so you can
149

150
00:11:02,410 --> 00:11:04,180
see my name and the dollar sign.
150

151
00:11:04,180 --> 00:11:05,460
So that's your prompt.
151

152
00:11:05,460 --> 00:11:08,930
Now, if you had any problems with the conversion, you'll probably get an error.
152

153
00:11:08,980 --> 00:11:12,080
It basically won't look the same as mine.
153

154
00:11:12,220 --> 00:11:19,600
So if you have an error, the most likely thing is that your script has a typo in it,
154

155
00:11:19,600 --> 00:11:23,590
especially if your error is something like "Error on line 51," et cetera.
155

156
00:11:23,710 --> 00:11:29,800
So check back, compare your script against my script, or I've actually provided the script as a download
156

157
00:11:29,890 --> 00:11:34,020
in this lesson, so you can just simply download it and copy and paste it in.
157

158
00:11:34,120 --> 00:11:39,070
And that way, you should be able to avoid any small typos that you might have encountered.
158

159
00:11:39,070 --> 00:11:43,960
All right. So once it's done, if you have a look inside your Flower Classification class, you should now
159

160
00:11:43,960 --> 00:11:47,300
be able to see your 
160

161
00:11:47,320 --> 00:11:48,760
FlowerClassifier.mlmodel.
161

162
00:11:48,760 --> 00:11:53,530
So this is what we're going to be using in the next lesson in order to bring all the capabilities of
162

163
00:11:53,530 --> 00:11:56,340
Flower Classification into our app.
163

164
00:11:56,410 --> 00:11:57,820
So I'll see you on the next lesson.
