Current location - Recipe Complete Network - Complete vegetarian recipes - How to use Python to classify garbage
How to use Python to classify garbage
1 Introduction

It's July, and everyone must have been tortured by a recent new policy, that is, garbage sorting. "Shanghai Municipal Domestic Waste Management Regulations" has been formally implemented, and it is believed that it will also

But many friends, like me, haven't fully figured out which ones should be thrown in which category. I feel that I am learning garbage classification every day, which is really intoxicating.

I heard that an unfinished cup of bubble tea should be thrown like this.

1. First, pour the unfinished milk tea into the pool.

2. Pour pearls, fruits and other residues into wet garbage.

3. Throw the cup into the dry garbage.

4. Then there is the lid. If it is a hot drink with a lid (such as most hot drinks), the plastic lid can be classified as recyclable garbage.

Seeing this, does everyone suddenly don't want to drink milk tea, haha. But it doesn't matter. Although garbage should be sorted, milk tea can also be drunk.

So, here we want to discuss whether the methods of artificial intelligence and data science can help us better classify garbage. So we don't have to worry about not knowing which garbage to throw.

Worried about the trash can.

Two ideas

There may be more than one way to solve this problem. I'm just offering a few simple ideas here.

The first scheme can make the junk information into tabular data, and then use the traditional machine learning method.

The second scheme is to make all the garbage classification information into a knowledge map, and every query is like looking up information in a dictionary.

The third scheme can identify and classify garbage with the help of the current deep learning method. Every time we give a picture of garbage, let the model identify what kind it belongs to.

Category: Dry garbage, wet garbage and recyclable garbage in harmful waste.

3 image classification

Image classification is a classic application of deep learning. Its input is a picture, and after some processing, it enters a deep learning model, and it will return this picture.

Types of garbage in the house. Here we consider four categories: dry garbage, wet garbage, harmful waste or recyclable garbage.

(newspaper: recyclable garbage)

(Battery: hazardous waste)

(Disposable lunch box: dry garbage)

We classify objects in pictures, which belongs to the field of image processing and recognition. Convolutional neural network (CNN) is put forward in artificial intelligence to solve this kind of problem.

I will use the keras package and Tensorflow backend to build the model. Due to the temporary lack of samples in the training set, only one set of ideas and codes can be given here. Training model work

There is a wave of data collection before doing it.

Related recommendation: Python video tutorial

Let's first look at what the code looks like.

Import some necessary packages first.

Do some preparatory work.

Above, we initialized some variables, the batch size is128; Num_classes = 4, because the quantity to be classified is 4, including dry garbage, wet garbage and harmful waste.

Heck

Four categories of recycling garbage. Epochs is the number of times we must train. Next, img_rows, img _ cols = 28,28 We give the latitude of the picture.

Inches reshape(60000, 28, 28, 1), 60000 is the number of pictures (variable), 28 is the picture size (adjustable), 1 represents the channel, and the channel = 1?

Refers to black and white photos. The same is true for me. Plastic (10000, 28, 28, 1), except that the number of pictures is 10000.

In the last two lines, we convert the value of the target variable into a binary classification, which is represented by a vector (matrix). For example, dry garbage,

It means wet garbage or something.

Next is the modeling part.

We added convolution layer and pool layer entry model. The activation function is relu, which is almost widely used in convolutional neural networks and deep learning. We are between floors.

Descent has also been added to reduce over-fitting. Dense layers are used for category prediction.

After establishing the model, we need to verify the model online to ensure the accuracy.

At this point, our modeling and forecasting have been basically completed. A good model should constantly optimize it and improve its accuracy until it reaches an acceptable level.

This optimization process will not be discussed in depth here, but will be continued later.

4 abstract

It is worth mentioning that although this method can be realized, it is certainly complicated in actual operation, especially with high accuracy.

Moreover, when a picture contains several kinds of garbage, it will make the development of our classification model very complicated and increase the difficulty.

For example, we need to classify the garbage of a cup of milk tea. There are many kinds of garbage in the photo, which is quite big because it does not belong to a single category.

There will certainly be difficulties ahead, but please take the sharing here as a starting point.

After all, Li Bai also said, "One day, I will ride the wind and waves, sail straight up and cross the deep sea."