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