This is my seventh day of the August Genwen Challenge

preface

Today is to bring you the image recognition technology – dog classifier

Tool use

Development environment: Win10, PYTHon3.6 Development tools: Pycharm toolkit: Keras, Numpy, PIL

Results show

The accuracy of the training set is 0.925, but only 0.7 of the test set indicates overfitting. Some pictures can be added or data enhancement can be used to reduce overfitting.

Tested two pictures, all correctly identified!

Thought analysis

  • 1 Prepare a data set
  • 2. Preprocessing of data sets
  • 3. Build convolutional neural network
  • 4 training
  • 5 forecast

1. Prepare the 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

The unified size is 1001003 (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 As 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 model (similar to VGG, just use it)
model = Sequential()
# 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

That’s the end of this article. Thank you for watching. The next installment in our Python data analysis series will share Python tips and tricks

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

③ project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis video (suitable for small white learning)

⑤ A Roadmap for Learning Python

All done ~ see personal profile or private letter for complete source code.

Review past

Python implements “fake” data

Classic Quotations by Lu Xun, Python crawler

Python crawler douban hot topic