Laboo. Top /2018/12/02/…

In the traditional programming, image recognition has always been a difficult point, although people can do it easily, but it is difficult to describe the process with logic and convert it into a program. The emergence of machine learning has made a breakthrough in image recognition technology, and the emergence of convolutional neural network has made image recognition to a higher level.

Convolutional neural network consists of one or more convolutional layers and a fully connected layer at the top. This structure enables convolutional neural network to utilize the two-dimensional structure of input data. Compared with other deep learning structures, convolutional neural networks can give better results in image and speech recognition.

Here, convolutional neural network is used for face gender recognition. TensorFlow machine learning library is used in the project.

The project address

face-gender-classification

Data collection and processing

The foundation of machine learning is massive amounts of data. I used to crawl 10,000 ID photos off the Internet, and now I’m using them as training data. A simple search for male (female) sexual identity photos can also be obtained from Google and tagged data. Since the photos I collected were untagged, IT took me a while to manually select 200 male and 200 female photos and label them.

In order to make the recognition more accurate, openCV was used in the project to crop out the face part of the image and scale to 28*28 size.

recognizer = cv2.CascadeClassifier("model/haarcascade_frontalface_default.xml")
crop(img_path):
    try:
        img = cv2.imread(img_path)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = recognizer.detectMultiScale(gray)
        if len(faces):
            x, y, w, h = faces[0]
            c_img = img[y:y + h, x:x + w]
            return cv2.resize(c_img, (28.28), interpolation=cv2.INTER_AREA)
    except:
        pass

    return None
Copy the code

For all the data, the result is as follows:

Training model

Read training data.

def read_img(files):
    arr = []
    for file in files:
        img = Image.open("%s" % file)
        pix = img.load()
        view = np.zeros((IMAGE_H, IMAGE_W, 1), dtype=np.float)
        for x in range(IMAGE_H):
            for y in range(IMAGE_W):
                r, g, b = pix[y, x]
                view[x, y, 0] = (r + g + b) // 3
        arr.append(view)
    return np.array(arr)
Copy the code

Here, the training image is grayed, and a small part of the training data is taken as the verification set.

Start creating the model.

model = keras.Sequential([
    keras.layers.Conv2D(32, (3.3), input_shape=(IMAGE_W, IMAGE_H, 1), strides=(1.1), activation='relu'),
    keras.layers.MaxPool2D(pool_size=(2.2)),
    keras.layers.Conv2D(64, (3.3), strides=(1.1), activation='relu'),
    keras.layers.MaxPool2D(pool_size=(2.2)),
    keras.layers.Flatten(),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(2, activation=tf.nn.softmax)
])
Copy the code

Select the appropriate optimizer and loss function to compile the model.

model.compile(optimizer=tf.train.AdamOptimizer(learning_rate=0.001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])
Copy the code

Start training the model.

model.fit(x=train_x,
          y=train_y,
          batch_size=32,
          epochs=30,
          verbose=1,
          callbacks=my_callbacks,
          validation_split=0.05,
          shuffle=True
          )
Copy the code

The test model

Here matplotlib is used to display test images and results.

predictions = model.predict(test_x)

class_names = ["Female"."Male"]

plt.figure(figsize=(12.6))
for i in range(min(9, len(test_y))):
    result = predictions[i]
    max_label = int(np.argmax(result))
    correct_label = int(np.argmax(test_y[i]))

    plt.subplot(3.6.2 * i + 1)
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    img = test_x.reshape(test_x.shape[0], IMAGE_W, IMAGE_H)[i]
    plt.imshow(img, cmap="gray")
    plt.xlabel("{} - the prob: {: 2.0 f} %".format(class_names[max_label], 100 * np.max(result)))

    plt.subplot(3.6.2 * i + 2)
    plt.grid(False)
    plt.yticks([])
    plt.ylim([0.1])
    bar = plt.bar(range(2), result)
    bar[max_label].set_color('red')
    bar[correct_label].set_color('green')

plt.show()
Copy the code

The probability of women
The probability of male

Welcome to follow my blog public number