We have Harry Moreno. He's going to talk about how one would identify sick cannabis plants using machine learning. Is your mic good? Hello. Hi. Keep it close. My name is Harry. All right. Thank you. So, hi. Hope you're enjoying DEF CON. And so we'll begin. This is machine learning for sick cannabis. How would you build a model to identify sick cannabis? I designed this with the intention of being very practical. So folks could take a photo of any sort of cannabis plant at an average distance from your smart phone. So who am I? I'm a software engineer. I'm not a data scientist. That's not my job title. I'm actually a software engineer. But I'm interested in data science and machine learning. And I thought this would be a pretty interesting problem, very practical problem to solve. I'm from New York. I'm an organizer at Kaggle New York City. Kaggle, for those who don't know, is a data science platform where people compete from around the world to be the best data scientist in the world. And I'm a data scientist on interesting data sets. So if you're in New York and want to do hands-on machine learning, please join us. What's the background here? So some context on what's possible, what's being done with A.I. in 2018. Artificial intelligence is becoming more pervasive and accessible. Two examples. Radiologists and interpreting x-rays. The say of the art is that we've built models that are as good or better than professional radiologists in terms of accuracy, as well as, I think, last year, a Stanford University professor who was a professor at the University of Michigan, published a model for skin cancer, diagnosing skin cancer that could run on a smartphone. So that's what's possible. And so what are we trying to do? We are trying to see if we can build something similar, but for diagnosing cannabis. The intended users are hobbyists, people that aren't doctors, aren't plant doctors, and they just want to grow a plant. Industrial players. So if you have a cannabis farm, you're very keen on knowing if your crops are sick. And the other problem, too, is that diagnosing a plant is a... requires a domain expert. So, for example, I'm not a domain expert in diagnosing plants, so I would want a model like this to help me. And so that's our goal. We want software that tells you if cannabis is sick by using your phone. So how do we make this...how do we make a model? How do we make a predictive model? The machine learning process is this. You gather data. You train on examples. You build a predictive model on the examples. We launch it. We publicize it so that people can use it. And then we iterate. So one of the things that we can do once we've deployed it is that as people use it, as people upload photos to get their... their predictions, that grows our data set. And the goal is to make a very, very accurate, free predictive model. So for step one, gathering data, we...myself and some other people in the community helped...helped scrape for sick cannabis, pictures of sick cannabis. And there are websites that...that do this. They're...that you upload your...your pictures of your sick plant, and other people tell you what might be wrong, like if it's purple or if it's yellow. It means different things. And to solve them, they have different solutions. So we built a scraper to collect sick cannabis photos. Similarly, we collected healthy cannabis photos. So this was a little bit easier because people like to show off their...their healthy plants and...and how well they're doing. So that was easier. Here we collected data for...a data set that would trick the plant. So if our goal is to build a...a model that can tell you if a plant is...a cannabis plant is sick or...or if it's healthy, another...a very practical concern is if I upload a photo, can...can the model tell me if there's even cannabis in the photo? So I added this third class that was basically composed of meme photos, the Caltech object data set with random everyday objects, and a lot of pictures of flowers and plants to see if the model could learn to...what...what...if the model could learn how to identify what is a cannabis plant first and then if it's sick or healthy. So this is sort of the design of the...of the...of the training. Built this other images data set. So after...at the end of this stage of gathering the data, we ended up with 3,000 images. 1,000 sick, 1,000 healthy, 1,000 other. This is actually a...a very, very tiny data set in terms of...for doing this machine learning stuff. And...we'll see that...it...how it performs. If you want to follow along, what I pretty much did was I read section 5.3 in Deep Learning with Python. Here they go over...the augmentation, which helps with...mutating the...the...the data set so that you can get more mileage out of it, as well as...as well as transfer learning, which allows you to...begin building your model based off of, like, the cutting edge research that other people have published...publicized. So for practical concerns, I work off of a...a fairly old MacBook, and it has a GPU, but it's just very, very old. So...I used...Amazon Web Services, their SageMaker offering, which is a one-click deploy...so a GPU instance, and you get all of the libraries installed. The one GPU instance is a...is $1.50 an hour. The four GPUs, $10 an hour. So if you want to do this and you don't have a GPU, this is how you could do it. The specific technique that I used here was transfer learning, which basically we take the ImageNet winner. So ImageNet was another problem that was very, very popular. And now it's considered a solved problem, which was the challenge was object recognition. So that challenge was you're given tons of data and there's 1,000 classes and your model has to say what's in the photo. This is actually considered a solved problem now. And there are plenty of pre-trained models that you can build your own models off of and that's what this transfer learning technique is. We basically take the, so the full, imagine the, from top to bottom, bottom is the ImageNet solution. You would take that, remove the top layer and put in your own and that's what you're training. So these lower layers are reusable feature detectors that you can use. And so what are some of the results? There, we tried two architectures broadly. There's this ResNet50. ResNet50. ResNet is short for residual network. I don't want to get into the details, but I tried this one first because this is the one that reached human level accuracy on the ImageNet competition. And the accuracy that we got when building our model was about 60 percent validation accuracy. And then I tried another model, another architecture called VGG2. And this is coming out of Oxford. And this one achieved 80 percent accuracy. And this is pretty much what I ended up deploying, this second architecture. There, it's, I want to figure out why this is the case. Everything online tells me that ResNet should have been more accurate, but it seems like we just need more data. So if we had something like 20,000 or 50,000 images, I would like to reach, try this again with ResNet and see if that could get up to 95, 99 percent accuracy. But for now, with 3,000 images, we have VGG16. And then we deployed it to the cloud. This is running on EC2. There's no GPU in production. It takes about one second for inference to happen, for the prediction to, for you to get a prediction. And it's built with Python tools like Keras, Flask. And that's about it. We built a user interface. And you can check this out right now, chronic sickness.com. And if there's enough time, I'll demo it. But it's there, it's live. Pretty much, you upload a file. And you can easily download it. And then it's ready to go. file from your phone or from your browser, from your computer and you submit it and you get a probability of what the model thinks is the outcome. So for this example picture, it's 88 percent, so it all adds up to one, the three classes. So it would predict that that picture is of healthy cannabis. And I encourage you to test it out, help us make it better. We want to iterate. So this is just the first step. 80 percent isn't that great. I feel like this, if we're putting radiologists and dermatologists out of work, this, I think, in my opinion, would be a simpler problem. So it's really just about getting more data. 3,000 images is way, way too small. We want to, it's already open source. We want to, if you want to do this, we want to do this. The key problem, though, is getting good labeled data. And that's very time intensive. It requires an expert. So we want to now build some sort of crowd sourcing platform where people can contribute when they have free time or as their interest brings them in. And as they lose interest, they can leave. But we want to preserve that sort of work in a crowd source sort of way. So look out for that. Future work. So the problem of diagnosing sick plants is a three class problem. Not as, it's hard to say, like, what should be more difficult or what are less difficult. But, for example, ImageNet was a 1,000 class challenge. And statistically, if you just made a random guess, it would be pretty hard. It's hard to get the right guess when there's lots of classes. So these other problems, if we could classify the exact disease of a plant is much more difficult than just what we have now, which is sick or not sick or not cannabis. And then it might be fun because there's lots of sites like this already to try to predict the strain of the cannabis just for fun. And that is many, many things. Well, one source told me that it was about 800 strains. So that's arguable as well. But it would be pretty interesting to build something like this with no access to the plant's genetics. It's purely its outward appearance. And seeing what computers think, what sort of strains they discover. And that's my talk. The site's available. It's available at ChronicSickness.com. And if you want to reach out to me, that's my site, HarryMoreno.com. And if you want to help, if you have access to a large data set of labeled cannabis photos, I really want to speak with you. Other than that, I want to build a crowd sourcing sort of platform so that people can contribute. And let's make it happen. I want to make a free predictive model for cannabis disease. That's free for everybody. That we can all build together and it benefits everybody. That's kind of what I want to build. And that's it. So... Do you want to take a question? I can take questions. By my clock, I have ten more minutes. But if people want to... Yeah. I guess questions. Any questions? Yeah. With the hat. Are you interested in doing more things with being able to put this in an automation project? Like, you know, plants have visual signs in there. We haven't returned it. Like, it needs water. Are you able to go in that direction at any point? Or is this purely just disease and, like, nutrient deficiency? Okay. So, I think the question is, do we want to build something that is for ailments other than diseases? Like, lack of water and... Yeah. Yeah. Like, it needs water and plants. Yeah. So, actually, when... So, maybe I'm not an expert in cannabis, but when I said disease, I was actually going... I was actually including those sort of ailments. Because this breakdown of 40 plant diseases is actually from groweateasy.com. And so we see things like boron deficiency, for example, light burn, which isn't a disease, but it's more of an issue that you'll have if you are just doing this in your closet or something. So, we could build both, but I think for proper diseases, it would actually be more difficult, and you would actually want to get some sort of botanist. I think from a more practical point of view, it'd be much easier to collect data on those things that you did mention. So, practical things like, is it lacking water? Does it need more sunlight? And is it too close to the generator? That's one that I learned. In this project. So, it's sort of like the software sort of meets you in the middle. So, if people want to build certain things, or we have the talent, we could certainly build a proper disease one, but I think it'd be most... Just way easier to do the common ailments. So. In the back. The hat. The current data model accounts for all 40 plant diseases, or does... Or is it... So, the question is, does the model... Yeah, chronic sickness.com, it does not do this. This is for future work. It's... We have a model that's about 80% accurate on just telling you if it's sick or not. And that's... First, we want to solve that problem. It should be... In my opinion... What I... I think that we should be able to build a model that's above human level accuracy. We could debate about that, what that means exactly. But we should be able to build that for sick, not sick. And then once that's solved, we can tackle more granular problems, like the specific ailment and the strains. Just for fun. Do... Do you have an API or something else? So, we don't have an API yet. It's just the chronic sickness.com where you can upload a photo. And there's a link to the GitHub so you can see the code. But the model is not publicized. It's... The model itself is about 110 megabytes. GitHub doesn't have support for large files. So, it's not there. But we can... We can speak offline about that. If people want an API, we could... We could build something like that. One more question. You had mentioned about your use of AWS SageMaker and the GPU situations. How long does training your model take when you're going to do training run with a GPU, without a GPU, based on your 3,000 files out there? Yeah. So, the GPU really helps with the specific technique called data augmentation. And so, you can do that for different types of data. So, in machine learning problems, you might have audio data or text data. In this problem, it's image data. And so, it turns out that we can do random projections off that data, like rotating and skewing it and zooming in. All of that is drastically accelerated with a GPU. So, in the book, it says that if you don't have a GPU, don't even try to do data augmentation. And so, there's that. And then, the actual model training time with a GPU for the... It's about an hour and a half for 3,000 images. And I think that would grow linearly. So, yeah. If we get 20,000 images, it might be like six-hour jobs. 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? I think so. I think the model training is more... It grows linearly with your data set size, not so much with the number of classes you're trying to predict. So, if you have any more questions, the site is ChronicSickness.com. You can follow through to the GitHub and find my handle. And you can send me emails if you want, if you want to help or if you have suggestions. Thank you.