Project introduction

What can a puppy classifier do? \

With this classifier, all you need to do is upload a photo, and you can get the breed of puppy, and more information.

This is called “machine learning”, letting machines “learn” on their own. The classification task we’re going to do today is a process of supervised learning.

The main goal of supervised learning is to learn models from labeled training data in order to make predictions about unknown or future data.

Let me give you an example. Use “House Size” to predict “House price”

Image from (Ng – Machine Learning)

\

X- Size of house (puppy picture) Y- Price of house (puppy category)

As shown in the figure, we can fit an approximate line according to the existing data set (the coordinates on the figure).

Thus, with the new house size (1250), we can estimate the price of the house (220K).

With these simple basics, it’s time to get started.

Results show

The accuracy of the training set was 0.925, but that of the test set was only 0.7\

You can add more images or use data enhancement to reduce the overfitting.

\

Tested two pictures, all correctly identified! \

\

Write the ideas

The realization of the whole classifier can be divided into the following parts:

1 Prepare a data set

We can use crawler technology to save 4 types of images (Pekino, Labrador, Corgi, Teddy) locally. There were 840 images for the training set and 188 images for the test set.

2. Preprocessing of data sets

\

1) Uniform size of 100*100*3 (RGB color image) \

\


    

     # Uniform size core code
     
img = Image.open(img_path)
     
new_img = img.resize((100.100), Image.BILINEAR)
     
new_img.save(os.path.join(
     './dog_kinds_after/' + dog_name, jpgfile))
     

    
Copy the code

\

2) Since the data is downloaded by itself, it is necessary to make a label, which can extract the first number of the image name as a category. (Rename the image)

\


    

     kind = 
     0
     

     

     # Walk through the keba folder
     
images = os.listdir(images_path)
     

     for name 
     in images:
     
    image_path = images_path + 
     '/'
     
    os.rename(image_path + name, image_path + str(kind) +
     '_' + name.split(
     '. ') [0] +'.jpg')
     

    
Copy the code

3) Divide the data set

840 images were used as the training set and 188 images were used as the test set.

\

4) Convert the image to the type needed by the network

\


    

     # only put the code of the training set, the same operation as the test set.
    

    

     ima_train = os.listdir(
     './train')
     

     

     The image is actually a matrix (each pixel is a number between 0 and 255) (100*100*3)
    

    

     
     # 1. Convert the picture to a matrix

     def read_train_image(filename):
     
    img = Image.open(
     './train/'
      + filename).convert(
     'RGB'
     )
    
     return
      np.array(img)

x_train = []

     # 2. Put all image matrices in a list (840, 100, 100, 3)
     

     for
      i 
     in
      ima_train:
    x_train.append(read_train_image(i))
    

    

     x_train = np.array(x_train)

    

    

     # 3. Extract the KIND category as a tag
y_train = []

     for
      filename 
     in
      ima_train:
    y_train.append(int(filename.split(
     '_') [0
     ]))


     # tag (0/1/2/3) (840,)
     
y_train = np.array(y_train)


     # I renamed the image to (1/2/3/4), so I reduced all of them by 1
     

     # in order to be able to transform into a unique thermal matrix
     
y_train = y_train - 
     1
       


     # 4. Convert the label to a unique hot matrix
     

     # Convert category information into the form of unique thermal code (unique thermal code is conducive to the training of neural network)
     
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
print(y_test)

x_train = x_train.astype(
     'float32'
     )
x_test = x_test.astype(
     'float32'
     )

x_train /= 
     255
     
x_test /= 
     255
    

    

     print(x_train.shape)  
     # (840, 100, 100, 3)
     
print(y_train.shape)  
     # (840),
    
Copy the code

3. Build convolutional neural network

Keras is a deep learning library based on TensorFlow. Keras is a high-level neural network API written in pure Python, and also supports Python development only.

\

It is a reencapsulation of Tensorflow for quick practice, allowing us to quickly turn ideas into results without paying too much attention to the low-level details.

* * * *


    

     # 1. Build a modelModel = Sequential(parallel to VGG, just use it)# The convolution layer constructed here has a total of 32 convolution kernels with a size of 3*3 and the activation mode of RELU is adopted.
     

     # input_shape, which literally means dimension of input data.
     

     

     # The sequential model is used here, which is easier to understand
     

     Sequential models are like building blocks, building neural networks layer by layer
     

     
model.add(Conv2D(
     32, (
     3.3), activation=
     'relu', input_shape=(
     100.100.3)))
     
model.add(Conv2D(
     32, (
     3.3), activation=
     'relu'))
     
model.add(MaxPooling2D(pool_size=(
     2.2)))
     
model.add(Dropout(
     0.25))
     

     
model.add(Conv2D(
     64, (
     3.3), activation=
     'relu'))
     
model.add(Conv2D(
     64, (
     3.3), activation=
     'relu'))
     
model.add(MaxPooling2D(pool_size=(
     2.2)))
     
model.add(Dropout(
     0.25))
     

     The # Dropout layer prevents overfitting, and 25% of the data will be discarded at a time
     

     
model.add(Flatten())
     
model.add(Dense(
     256, activation=
     'relu'))
     
model.add(Dropout(
     0.5))
     
model.add(Dense(
     4, activation=
     'softmax'))
    
Copy the code

4 training

* * * *

The process of training is the process of optimal solution. * * * *

\

For the figure above, it is to find the nearest line (y = kx + b) through continuous iteration according to the data set, save the parameters k and b, and load them directly during prediction.

* * * *


    

     # build model
     
sgd = SGD(lr=
     0.01, decay=
     1e-6, momentum=
     0.9, nesterov=
     True)
     
model.compile(loss=
     'categorical_crossentropy', optimizer=sgd, metrics=[
     'accuracy'])
     

     

     There were 32 rounds
     

     # That's 840 images, 10 per session. That's 84 sessions
     
model.fit(x_train, y_train, batch_size=
     10, epochs=
     32)
     

     

     

     # Save the weight file (i.e. equivalent to "k and B parameters of housing price problem")
     
model.save_weights(
     './dog_weights.h5', overwrite=
     True)
     

     # Evaluation model
     
score = model.evaluate(x_test, y_test, batch_size=
     10)
     
print(score)
     

    
Copy the code

\

5 forecast

\

Now that k, b(parameters), and x(images of puppies) are known, k(categories) is done.

\


    

     # 1. Upload pictures
     
name = input(
     'The name of the uploaded image (for example: xx.jpg) is:')
     

     

     # 2. Preprocessing images (code omitted)
     

     

     # 3. Load the weight file
     
model.load_weights(
     'dog_weights.h5')
     

     

     # 4. Prediction categories
     
classes = model.predict_classes(x_test)[
     0]
     

     
target = [
     'step'.'Labrador'.'corgi'.'teddy']
     

     # 3- Teddy 2- Corgi 1- Labrador 0- Pekingba
     

     

     # 5. Print the result
     
print(
     The identification results are as follows: + target[classes])
    
Copy the code

Depend on the environment

1 Deep learning frameworks Keras and TensorFlow\

2 PIL Extension library (pre-processed image)

3 Pycharm/Jupyter notebook

conclusion