All right. 1
So the first thing that you need to do is install something called PIP. So PIP is a package management system 2
that is used to install and manage packages that are written in Python and PIP is a little bit like 3
CooaPods, but for Python packages. 4
So if you go onto the Python Package Index at pypi.python.org, then you'll be able to see a list 5
of all the Python packages. And as you can see, currently, there's over 100,000 packages that you can download. 6
So downloading it from here and maintaining it and keeping it updated is kind of tiresome. 7
So instead, we're going to be using PIP to do that. 8
So Macs will come with Python preinstalled. 9
And if you install virtual environments in the past or if you've downloaded Python to a Python 3, then 10
you probably already have PIP on your system. But it's pretty easy to check to see if you have PIP installed, 11
all you need to do is just write pip -v and you can see which version you've got. 12
And if you don't have PIP installed, then it'll tell you that it doesn't exist. 13
So I've already got PIP installed 9.0.1. 14
Now, if you don't have PIP installed, it's also incredibly easy to install it. 15
So if you're unfamiliar with Python and Linux commands, then it's probably easier just to head over to 16
this URL, which are linked to in this lesson, and all you have to do is just download this by right-clicking 17
save link as, and I'm going to save it on my desktop. 18
Now, once you've got it downloaded, then you need to change directory to where it's located. 19
So in my case, I've downloaded to my desktop. And once I'm inside my desktop, then all I need to do to 20
install PIP is just to run it using Python. 21
Now, I'm going to add the sudo keyword in front of it which stands for superuser do, so that it will 22
ask me for my password, but it gives me admin privileges. 23
So you're going to write sudo, then you're going to write python, 24
so this is what you're going to use to run that, get-pip.py file that you've downloaded. 25
So just review your line of code and make sure it says exactly the same as mine, and then hit enter. 26
And at this point, it's going to ask you for a password. 27
So once you have successfully installed PIP, then you should see this line where it says, "Successfully 28
installed version 9.0.1. 29
So yours might differ from mine. 30
It'll probably be higher than this because you'll be doing it in the future. 31
So once that's done, we're going to use PIP to install a virtual environment. 32
Now, why do we need a virtual environment? 33
Well, so in order to convert those models in Caffe or Keras into a .mlmodel file, we're going 34
to need a package from Apple which is called CoremlTools. 35
Now, it's an open-source package, but it requires Python 2.7 in order to work. 36
Now, you may or may not know, 37
but Python has quite a bit of problem in terms of its versioning. So the Python team has brought out 38
the latest version of Python which is Python 3 ,or more specifically, Python 3.6. 39
But there's a lot of people who have already created and spent a lot of effort maintaining open-source 40
tools that were built using Python 2. 41
So there's quite a bit of contention and this problem has divided the Python community. So it's almost 42
like, remember back when we had a Swift 1, which got upgraded, Swift 2, Swift 3, there are quite a lot of 43
code changes. And, thankfully, the people who created open-source frameworks for Swift 2 and Swift 1 44
were able to update them to Swift 3, and now Swift 4. 45
But in the case of Python, there's actually now two distinct camps, people who are updating the frameworks 46
of Python 3 and people who have decided to keep it in Python 2. 47
So in the case of CoremlTools, Apple decided that it's only going to work with Python 2.7. 48
So to prevent you from having a load of headaches, we're going to set up a virtual environment which 49
allows to create a directory on our laptop and we can specify the version of Python used just for that 50
directory. 51
So it's a way of isolating a particular directory and explicitly specifying the version of Python that 52
we're going to use. 53
And this way we minimize errors and we give ourselves the best chance of doing this 54
conversion as smoothly as possible. 55
So even if you're really familiar with Python, even if you've been coding for 40 years, please just follow 56
along with me for the time being, 57
and once you've made it work and gone to grips with the tools then feel free to chop and change however 58
you wish. 59
So enough talking. 60
Now that we've installed PIP, we're going to use it to install a virtual environment package. 61
So I'm going to navigate back to my root directory by writing a cd, change directory, and the tilde 62
symbol, and hit enter. 63
Now, I am back in my root directory, as you can tell, with that tillde sign. 64
So here I'm going to write 65
pip install virtual env. So make sure that you've got the correct spelling, and then hit enter. 66
So if you get an error like this where it says, "Permission denied," then all you need to do is just to 67
add the "sudo" keyword in front of it. 68
So, now instead of pip install virtual env, it's sudo pip install env. 69
And once successfully installed, you should get a success message and a version of your virtual end 70
and it should be the same or higher than what I've got here. 71
All right, so we've got our virtual environment package installed, 72
now it's time to set up our virtual environment. 73
Now, in order to do this, the best practice is probably to set up an environment directory where you can 74
manage all of your various environments. 75
So I'm going to do that in my root directory, 76
so the one that's represented by the tilde symbol. And here, I'm going to use the make directory command 77
to make a new folder called environments, and hit enter. 78
Now, for those you guys who are more familiar with the graphic user interface or prefer the GUI, then 79
if you have a look inside your user folder in Finder, then you should be able to see a folder that was 80
newly created called environments. 81
So, now that we've got this Environment's folder up here, we're going to cd or change directory in there. 82
So cd environments. 83
So, now we are inside Environments and this is the place where we're going to create our virtual environment 84
in Python 2.7. 85
So to do that, we're going to invoke virtualenv --python=/usr/bin/python2.7 86
And we're going to call that directory: python27. 87
So what this line does is it uses the virtual environment package to create a directory called python27 88
inside the Environments directory, and it's going to set it up to run on Python 2.7. 89
So, now if I hit enter, you'll see that it's going to take a little while. And once it's done, if you have 90
a look inside environments, you now have this directory called python27. 91
So to start a virtual environment, all you need to write is source python27/bin/activate. 92
Hit enter. 93
And you can see you are now running on python27. And we can check that by writing python -- version. 94
Hit enter and you can see we are running Python 2.7.10. 95
So awesome. 96
Now, in order to exit the Python 2.7 environment, all you need to do is just type "deactivate." And you can 97
see, we are back now in our Environments directory, and we are no longer inside the python27 virtual 98
environment. 99
Now, let's say in the future, you decide to create a Python 3.6 virtual environment and you can check 100
to see python --version. And you see 101
now we are running on Python 3.6.1. 102
So previously, we were running 2.7, and now we're running 3.6, 103
and this is all done by using virtual environments. 104
Now, let's get back to our main topic: 105
Installing CoremlTools. 106
So let's deactivate our Python 3.6 and get back into our Python 2.7. 107
So source, then python27/bin/activate while inside your Environments folder. 108
So hit enter and you should see we're now back into Python 2.7 virtual environment. 109
So while we're inside a Python 2.7 virtual environment, we are going to install our CoremlTools, and it's 110
as easy as writing pip install -U coremltools. 111
Hit enter 112
and it should take a little while, 113
not too long. And now it's done. 114
And if you're interested in what that "-u" does, it simply helps you install the latest version 115
of CoremlTools. 116
So whereas if you just wrote pip install coremltools, if you already had it on your system, it would 117
say, "Already installed. 118
This is the version." But by adding that modifier -u, 119
then even if you have CoremlTools, it'll go and check and grab the latest version of it and help you install 120
it. 121
All right. 122
And so that's all you need to do. 123
So deactivate to get out of Python 2.7 virtual environment and we are done. 124
We have installed CoremlTools using PIP and Python and we are now ready to convert our model into 125
a mlmodel file. 126
As always, if you have any trouble doing any of that, be sure to start a new thread in the Q & A and we'll 127
try and help you out. 128
So I'll see you on the next lesson.