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