1 00:00:00,000 --> 00:00:06,420 We have Harry Moreno. He's going to talk about how one would identify sick cannabis plants using 2 00:00:06,420 --> 00:00:15,740 machine learning. Is your mic good? Hello. Hi. Keep it close. My name is Harry. All right. 3 00:00:15,800 --> 00:00:27,160 Thank you. So, hi. Hope you're enjoying DEF CON. And so we'll begin. This is machine 4 00:00:27,160 --> 00:00:39,160 learning for sick cannabis. How would you build a model to identify sick cannabis? I designed this 5 00:00:39,160 --> 00:00:47,940 with the intention of being very practical. So folks could take a photo of any sort of cannabis 6 00:00:47,940 --> 00:00:56,980 plant at an average distance from your smart phone. So who am I? I'm a software engineer. 7 00:00:57,780 --> 00:01:04,660 I'm not a data scientist. That's not my job title. I'm actually a software engineer. But I'm 8 00:01:04,660 --> 00:01:09,100 interested in data science and machine learning. And I thought this would be a pretty interesting 9 00:01:09,100 --> 00:01:16,120 problem, very practical problem to solve. I'm from New York. I'm an organizer at Kaggle New 10 00:01:16,120 --> 00:01:22,180 York City. Kaggle, for those who don't know, is a data science platform where people compete 11 00:01:22,760 --> 00:01:27,140 from around the world to be the best data scientist in the world. And I'm a data scientist 12 00:01:27,160 --> 00:01:34,820 on interesting data sets. So if you're in New York and want to do hands-on machine learning, 13 00:01:35,060 --> 00:01:43,480 please join us. What's the background here? So some context on what's possible, what's being 14 00:01:43,480 --> 00:01:54,920 done with A.I. in 2018. Artificial intelligence is becoming more pervasive and accessible. Two 15 00:01:54,920 --> 00:02:10,260 examples. Radiologists and interpreting x-rays. The say of the art is that we've built models 16 00:02:10,260 --> 00:02:19,760 that are as good or better than professional radiologists in terms of accuracy, as well as, I 17 00:02:19,760 --> 00:02:24,900 think, last year, a Stanford University professor who was a professor at the University of 18 00:02:24,900 --> 00:02:32,860 Michigan, published a model for skin cancer, diagnosing skin cancer that could run on a 19 00:02:32,860 --> 00:02:43,560 smartphone. So that's what's possible. And so what are we trying to do? We are trying 20 00:02:43,560 --> 00:02:52,880 to see if we can build something similar, but for diagnosing cannabis. The intended 21 00:02:52,880 --> 00:03:01,720 users are hobbyists, people that aren't doctors, aren't plant doctors, and they just want 22 00:03:01,720 --> 00:03:10,500 to grow a plant. Industrial players. So if you have a cannabis farm, you're very keen 23 00:03:10,500 --> 00:03:20,380 on knowing if your crops are sick. And the other problem, too, is that diagnosing a plant 24 00:03:20,380 --> 00:03:22,060 is a... 25 00:03:22,880 --> 00:03:30,760 requires a domain expert. So, for example, I'm not a domain expert in diagnosing plants, 26 00:03:30,940 --> 00:03:37,900 so I would want a model like this to help me. And so that's our goal. We want software 27 00:03:37,900 --> 00:03:45,760 that tells you if cannabis is sick by using your phone. So how do we make this...how do 28 00:03:45,760 --> 00:03:48,560 we make a model? How do we make a predictive model? 29 00:03:48,560 --> 00:03:57,760 The machine learning process is this. You gather data. You train on examples. You build 30 00:03:57,760 --> 00:04:05,560 a predictive model on the examples. We launch it. We publicize it so that people can use 31 00:04:05,560 --> 00:04:14,100 it. And then we iterate. So one of the things that we can do once we've deployed it is that 32 00:04:14,100 --> 00:04:18,540 as people use it, as people upload photos to get their... 33 00:04:18,560 --> 00:04:25,980 their predictions, that grows our data set. And the goal is to make a very, very accurate, 34 00:04:26,080 --> 00:04:37,220 free predictive model. So for step one, gathering data, we...myself and some other people in 35 00:04:37,220 --> 00:04:45,660 the community helped...helped scrape for sick cannabis, pictures of sick cannabis. 36 00:04:45,660 --> 00:04:53,480 And there are websites that...that do this. They're...that you upload your...your pictures 37 00:04:53,480 --> 00:04:57,220 of your sick plant, and other people tell you what might be wrong, like if it's purple 38 00:04:57,220 --> 00:05:02,840 or if it's yellow. It means different things. And to solve them, they have different solutions. 39 00:05:03,220 --> 00:05:14,400 So we built a scraper to collect sick cannabis photos. Similarly, we collected healthy cannabis 40 00:05:14,400 --> 00:05:14,940 photos. 41 00:05:15,660 --> 00:05:23,480 So this was a little bit easier because people like to show off their...their healthy plants 42 00:05:23,480 --> 00:05:26,240 and...and how well they're doing. So that was easier. 43 00:05:29,920 --> 00:05:39,920 Here we collected data for...a data set that would trick the plant. So if our goal is to 44 00:05:39,920 --> 00:05:45,640 build a...a model that can tell you if a plant is...a cannabis plant is sick or...or 45 00:05:45,640 --> 00:05:52,380 if it's healthy, another...a very practical concern is if I upload a photo, can...can 46 00:05:52,380 --> 00:05:57,780 the model tell me if there's even cannabis in the photo? So I added this third class 47 00:05:58,480 --> 00:06:06,720 that was basically composed of meme photos, the Caltech object data set with random everyday 48 00:06:06,720 --> 00:06:14,040 objects, and a lot of pictures of flowers and plants to see if the model could learn 49 00:06:16,320 --> 00:06:19,640 to...what...what...if the model could learn how to identify what is a cannabis plant 50 00:06:19,640 --> 00:06:27,980 first and then if it's sick or healthy. So this is sort of the design of the...of 51 00:06:27,980 --> 00:06:29,000 the...of the training. 52 00:06:34,700 --> 00:06:42,480 Built this other images data set. So after...at the end of this stage of gathering the data, 53 00:06:42,540 --> 00:06:43,740 we ended up with 3,000 images. 54 00:06:44,580 --> 00:06:50,700 1,000 sick, 1,000 healthy, 1,000 other. This is actually a...a very, very tiny data 55 00:06:50,700 --> 00:06:58,340 set in terms of...for doing this machine learning stuff. And...we'll see that...it...how it 56 00:06:58,340 --> 00:06:58,560 performs. 57 00:07:00,180 --> 00:07:05,580 If you want to follow along, what I pretty much did was I read section 5.3 in Deep Learning 58 00:07:05,580 --> 00:07:13,720 with Python. Here they go over...the augmentation, which helps with...mutating the...the...the 59 00:07:13,740 --> 00:07:19,240 data set so that you can get more mileage out of it, as well as...as well as transfer 60 00:07:19,240 --> 00:07:25,980 learning, which allows you to...begin building your model based off of, like, the cutting 61 00:07:25,980 --> 00:07:30,200 edge research that other people have published...publicized. 62 00:07:34,040 --> 00:07:43,620 So for practical concerns, I work off of a...a fairly old MacBook, and it has a GPU, but 63 00:07:43,740 --> 00:07:51,560 it's just very, very old. So...I used...Amazon Web Services, their SageMaker offering, which 64 00:07:51,560 --> 00:07:57,440 is a one-click deploy...so a GPU instance, and you get all of the libraries installed. 65 00:07:58,200 --> 00:08:05,840 The one GPU instance is a...is $1.50 an hour. The four GPUs, $10 an hour. So if you want 66 00:08:05,840 --> 00:08:09,200 to do this and you don't have a GPU, this is how you could do it. 67 00:08:10,560 --> 00:08:17,940 The specific technique that I used here was transfer learning, which basically we take the 68 00:08:17,940 --> 00:08:26,040 ImageNet winner. So ImageNet was another problem that was very, very popular. And now it's 69 00:08:26,040 --> 00:08:33,860 considered a solved problem, which was the challenge was object recognition. So that 70 00:08:33,860 --> 00:08:41,920 challenge was you're given tons of data and there's 1,000 classes and your model has to say 71 00:08:41,920 --> 00:08:48,120 what's in the photo. This is actually considered a solved problem now. And there are plenty of 72 00:08:48,120 --> 00:08:53,840 pre-trained models that you can build your own models off of and that's what this transfer 73 00:08:53,840 --> 00:09:03,840 learning technique is. We basically take the, so the full, imagine the, from top to bottom, 74 00:09:03,860 --> 00:09:11,520 bottom is the ImageNet solution. You would take that, remove the top layer and put in your own 75 00:09:11,520 --> 00:09:21,240 and that's what you're training. So these lower layers are reusable feature detectors that you can 76 00:09:21,240 --> 00:09:32,620 use. And so what are some of the results? There, we tried two architectures broadly. There's 77 00:09:32,620 --> 00:09:33,480 this ResNet50. 78 00:09:33,580 --> 00:09:33,840 ResNet50. 79 00:09:33,860 --> 00:09:38,860 ResNet is short for residual network. I don't want to get into the details, but I tried this one 80 00:09:38,860 --> 00:09:46,020 first because this is the one that reached human level accuracy on the ImageNet competition. 81 00:09:46,460 --> 00:09:57,060 And the accuracy that we got when building our model was about 60 percent validation accuracy. 82 00:09:59,740 --> 00:10:03,840 And then I tried another model, another architecture called VGG2. 83 00:10:03,840 --> 00:10:13,660 And this is coming out of Oxford. And this one achieved 80 percent accuracy. And this is pretty 84 00:10:13,660 --> 00:10:20,920 much what I ended up deploying, this second architecture. There, it's, I want to figure out 85 00:10:20,920 --> 00:10:27,220 why this is the case. Everything online tells me that ResNet should have been more accurate, 86 00:10:27,400 --> 00:10:33,180 but it seems like we just need more data. So if we had something like 20,000 or 50,000 images, 87 00:10:33,180 --> 00:10:42,920 I would like to reach, try this again with ResNet and see if that could get up to 95, 99 percent 88 00:10:42,920 --> 00:10:53,700 accuracy. But for now, with 3,000 images, we have VGG16. And then we deployed it to the 89 00:10:53,700 --> 00:11:02,960 cloud. This is running on EC2. There's no GPU in production. It takes about one second for inference 90 00:11:02,960 --> 00:11:09,960 to happen, for the prediction to, for you to get a prediction. And it's built with Python tools 91 00:11:09,960 --> 00:11:19,640 like Keras, Flask. And that's about it. We built a user interface. And you can check this out 92 00:11:19,640 --> 00:11:29,180 right now, chronic sickness.com. And if there's enough time, I'll demo it. But it's there, it's 93 00:11:29,180 --> 00:11:32,940 live. Pretty much, you upload a file. And you can easily download it. And then it's ready to go. 94 00:11:32,960 --> 00:11:40,900 file from your phone or from your browser, from your computer and you submit it and you get a 95 00:11:40,900 --> 00:11:48,200 probability of what the model thinks is the outcome. So for this example picture, it's 88 96 00:11:48,200 --> 00:11:57,120 percent, so it all adds up to one, the three classes. So it would predict that that picture is 97 00:11:57,120 --> 00:12:02,320 of healthy cannabis. And I encourage you to test it out, help us make it better. 98 00:12:03,960 --> 00:12:09,880 We want to iterate. So this is just the first step. 80 percent isn't that great. I feel like 99 00:12:09,880 --> 00:12:18,960 this, if we're putting radiologists and dermatologists out of work, this, I think, in my 100 00:12:18,960 --> 00:12:24,400 opinion, would be a simpler problem. So it's really just about getting more data. 3,000 images 101 00:12:24,400 --> 00:12:32,940 is way, way too small. We want to, it's already open source. We want to, if you want to 102 00:12:32,960 --> 00:12:37,900 do this, we want to do this. The key problem, though, is getting good labeled data. And 103 00:12:37,900 --> 00:12:44,400 that's very time intensive. It requires an expert. So we want to now build some sort of crowd 104 00:12:44,400 --> 00:12:50,720 sourcing platform where people can contribute when they have free time or as their interest 105 00:12:51,540 --> 00:12:56,980 brings them in. And as they lose interest, they can leave. But we want to preserve that sort of 106 00:12:56,980 --> 00:13:01,540 work in a crowd source sort of way. So look out for that. 107 00:13:02,960 --> 00:13:14,780 Future work. So the problem of diagnosing sick plants is a three class problem. Not as, 108 00:13:16,580 --> 00:13:22,380 it's hard to say, like, what should be more difficult or what are less difficult. But, for 109 00:13:22,380 --> 00:13:30,820 example, ImageNet was a 1,000 class challenge. And statistically, if you just made a random 110 00:13:30,820 --> 00:13:32,860 guess, it would be pretty hard. 111 00:13:32,960 --> 00:13:38,600 It's hard to get the right guess when there's lots of classes. So these other problems, if we 112 00:13:38,600 --> 00:13:45,600 could classify the exact disease of a plant is much more difficult than just what we have now, 113 00:13:45,680 --> 00:13:52,060 which is sick or not sick or not cannabis. And then it might be fun because there's lots of 114 00:13:52,060 --> 00:14:02,020 sites like this already to try to predict the strain of the cannabis just for fun. And that is 115 00:14:02,020 --> 00:14:02,940 many, many things. 116 00:14:02,960 --> 00:14:08,940 Well, one source told me that it was about 800 strains. So that's 117 00:14:08,940 --> 00:14:14,700 arguable as well. But it would be pretty interesting to build something like this with 118 00:14:14,700 --> 00:14:22,100 no access to the plant's genetics. It's purely its outward appearance. And seeing what 119 00:14:22,100 --> 00:14:32,580 computers think, what sort of strains they discover. And that's my talk. The site's available. 120 00:14:32,580 --> 00:14:36,920 It's available at ChronicSickness.com. And if you want to reach out to me, that's my site, 121 00:14:37,380 --> 00:14:44,160 HarryMoreno.com. And if you want to help, if you have access to a large data set of labeled 122 00:14:45,000 --> 00:14:56,440 cannabis photos, I really want to speak with you. Other than that, I want to build a crowd sourcing 123 00:14:56,440 --> 00:15:02,560 sort of platform so that people can contribute. And let's make it happen. 124 00:15:02,560 --> 00:15:11,080 I want to make a free predictive model for cannabis disease. That's free for everybody. 125 00:15:11,560 --> 00:15:16,240 That we can all build together and it benefits everybody. That's kind of what I want to build. 126 00:15:17,560 --> 00:15:18,380 And that's it. 127 00:15:27,460 --> 00:15:28,020 So... 128 00:15:28,560 --> 00:15:29,700 Do you want to take a question? 129 00:15:29,700 --> 00:15:37,780 I can take questions. By my clock, I have ten more minutes. But if people want to... 130 00:15:39,640 --> 00:15:45,720 Yeah. I guess questions. Any questions? Yeah. With the hat. 131 00:15:45,800 --> 00:15:52,740 Are you interested in doing more things with being able to put this in an automation project? 132 00:15:53,060 --> 00:15:57,420 Like, you know, plants have visual signs in there. 133 00:15:58,180 --> 00:16:03,180 We haven't returned it. Like, it needs water. Are you able to go in that direction at any point? 134 00:16:03,580 --> 00:16:07,980 Or is this purely just disease and, like, nutrient deficiency? 135 00:16:09,240 --> 00:16:17,960 Okay. So, I think the question is, do we want to build something that is for ailments other than diseases? 136 00:16:18,700 --> 00:16:22,900 Like, lack of water and... Yeah. Yeah. Like, it needs water and plants. 137 00:16:23,100 --> 00:16:25,400 Yeah. So, actually, when... 138 00:16:26,520 --> 00:16:31,140 So, maybe I'm not an expert in cannabis, but when I said disease, I was actually going... 139 00:16:31,140 --> 00:16:33,960 I was actually including those sort of ailments. 140 00:16:34,380 --> 00:16:40,480 Because this breakdown of 40 plant diseases is actually from groweateasy.com. 141 00:16:40,940 --> 00:16:46,620 And so we see things like boron deficiency, for example, light burn, which isn't a disease, 142 00:16:46,800 --> 00:16:51,960 but it's more of an issue that you'll have if you are just doing this in your closet or something. 143 00:16:56,420 --> 00:17:01,740 So, we could build both, but I think for proper diseases, it would actually be more difficult, 144 00:17:01,840 --> 00:17:03,440 and you would actually want to get some sort of botanist. 145 00:17:03,760 --> 00:17:12,440 I think from a more practical point of view, it'd be much easier to collect data on those things that you did mention. 146 00:17:12,660 --> 00:17:16,660 So, practical things like, is it lacking water? Does it need more sunlight? 147 00:17:17,400 --> 00:17:21,080 And is it too close to the generator? That's one that I learned. 148 00:17:21,960 --> 00:17:22,680 In this project. 149 00:17:24,620 --> 00:17:28,420 So, it's sort of like the software sort of meets you in the middle. 150 00:17:28,880 --> 00:17:33,140 So, if people want to build certain things, or we have the talent, 151 00:17:33,760 --> 00:17:37,220 we could certainly build a proper disease one, but I think it'd be most... 152 00:17:37,220 --> 00:17:41,340 Just way easier to do the common ailments. 153 00:17:42,180 --> 00:17:42,660 So. 154 00:17:43,660 --> 00:17:45,060 In the back. The hat. 155 00:17:45,280 --> 00:17:49,720 The current data model accounts for all 40 plant diseases, or does... 156 00:17:49,720 --> 00:17:51,380 Or is it... 157 00:17:55,420 --> 00:18:01,620 So, the question is, does the model... 158 00:18:02,360 --> 00:18:06,320 Yeah, chronic sickness.com, it does not do this. 159 00:18:06,480 --> 00:18:07,900 This is for future work. 160 00:18:09,660 --> 00:18:10,220 It's... 161 00:18:10,220 --> 00:18:15,340 We have a model that's about 80% accurate on just telling you if it's sick or not. 162 00:18:15,680 --> 00:18:16,460 And that's... 163 00:18:16,920 --> 00:18:18,780 First, we want to solve that problem. 164 00:18:19,000 --> 00:18:20,020 It should be... 165 00:18:20,020 --> 00:18:20,800 In my opinion... 166 00:18:20,800 --> 00:18:21,840 What I... 167 00:18:21,840 --> 00:18:27,740 I think that we should be able to build a model that's above human level accuracy. 168 00:18:28,060 --> 00:18:30,020 We could debate about that, what that means exactly. 169 00:18:30,320 --> 00:18:33,520 But we should be able to build that for sick, not sick. 170 00:18:33,740 --> 00:18:37,600 And then once that's solved, we can tackle more granular problems, 171 00:18:37,780 --> 00:18:40,980 like the specific ailment and the strains. 172 00:18:41,600 --> 00:18:42,440 Just for fun. 173 00:18:45,460 --> 00:18:46,020 Do... 174 00:18:46,020 --> 00:18:48,720 Do you have an API or something else? 175 00:18:51,980 --> 00:18:54,000 So, we don't have an API yet. 176 00:18:54,340 --> 00:18:59,360 It's just the chronic sickness.com where you can upload a photo. 177 00:18:59,640 --> 00:19:02,220 And there's a link to the GitHub so you can see the code. 178 00:19:02,980 --> 00:19:05,020 But the model is not publicized. 179 00:19:05,480 --> 00:19:05,700 It's... 180 00:19:05,700 --> 00:19:08,180 The model itself is about 110 megabytes. 181 00:19:09,480 --> 00:19:12,260 GitHub doesn't have support for large files. 182 00:19:12,440 --> 00:19:13,500 So, it's not there. 183 00:19:13,600 --> 00:19:14,320 But we can... 184 00:19:14,320 --> 00:19:16,180 We can speak offline about that. 185 00:19:16,180 --> 00:19:19,320 If people want an API, we could... 186 00:19:19,320 --> 00:19:20,300 We could build something like that. 187 00:19:21,360 --> 00:19:22,460 One more question. 188 00:19:23,660 --> 00:19:28,720 You had mentioned about your use of AWS SageMaker and the GPU situations. 189 00:19:29,260 --> 00:19:34,640 How long does training your model take when you're going to do training run with a GPU, 190 00:19:34,740 --> 00:19:37,900 without a GPU, based on your 3,000 files out there? 191 00:19:38,440 --> 00:19:39,000 Yeah. 192 00:19:39,140 --> 00:19:45,040 So, the GPU really helps with the specific technique called data augmentation. 193 00:19:46,420 --> 00:19:48,880 And so, you can do that for different types of data. 194 00:19:49,040 --> 00:19:52,400 So, in machine learning problems, you might have audio data or text data. 195 00:19:52,640 --> 00:19:54,360 In this problem, it's image data. 196 00:19:54,640 --> 00:20:03,580 And so, it turns out that we can do random projections off that data, like rotating and skewing it and zooming in. 197 00:20:03,960 --> 00:20:07,900 All of that is drastically accelerated with a GPU. 198 00:20:08,280 --> 00:20:13,600 So, in the book, it says that if you don't have a GPU, don't even try to do data augmentation. 199 00:20:14,240 --> 00:20:16,460 And so, there's that. 200 00:20:16,680 --> 00:20:21,020 And then, the actual model training time with a GPU for the... 201 00:20:21,020 --> 00:20:25,120 It's about an hour and a half for 3,000 images. 202 00:20:25,360 --> 00:20:26,520 And I think that would grow linearly. 203 00:20:28,240 --> 00:20:29,240 So, yeah. 204 00:20:29,380 --> 00:20:33,140 If we get 20,000 images, it might be like six-hour jobs. 205 00:20:33,400 --> 00:20:40,640 Do you expect it to scale a lot more with levels as you go and get to foreign diseases and strains and everything you're talking about? 206 00:20:40,820 --> 00:20:41,680 I think so. 207 00:20:41,900 --> 00:20:43,580 I think the model training is more... 208 00:20:43,600 --> 00:20:50,580 It grows linearly with your data set size, not so much with the number of classes you're trying to predict. 209 00:20:52,680 --> 00:20:57,860 So, if you have any more questions, the site is ChronicSickness.com. 210 00:20:58,000 --> 00:21:01,560 You can follow through to the GitHub and find my handle. 211 00:21:01,640 --> 00:21:04,720 And you can send me emails if you want, if you want to help or if you have suggestions. 212 00:21:05,000 --> 00:21:05,440 Thank you.