Python: Gender Identification (Deep Learning Development) Part 1

Data collection

This time as data for deep learning Not only the datasets installed in Keras as standard, such as MNIST and Cifar10 Learn with Keras using images you download online.

For gender identification, the summary uses a dataset called lfs at http://vis-www.cs.umass.edu/lfw/.

Also for research purposes at https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/ For example, the imdb dataset.

It also comes with metadata and is very easy to handle and is perfect for review.

Data cleansing

First, load and display the image.

#Import CV2 and NumPy to use OpenCV. Also import Matplotlib for image display
import cv2
import matplotlib.pyplot as plt
import numpy as np

#Reading with OpenCV. Let's specify the path as an argument
img = cv2.imread('./6100_gender_recognition_data/female/Adriana_Lima_0001.jpg')

#Process the data read by OpenCV so that it can be displayed by matplotlib. b,g,Since it is in r order, r,g,Retransform to b
b,g,r = cv2.split(img) 
img = cv2.merge([r,g,b])

#Output image using matplotlib
plt.imshow(img)
plt.show()

image.png

Image reduction

When doing machine learning, we rarely use images as they are.

If you capture an image that is too detailed, the learning time will be very long. Also, in general, not all images obtained from the Internet etc. are the same size.

Therefore, resize using the function of CV2.

import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('./6100_gender_recognition_data/female/Adriana_Lima_0001.jpg')

#I will try to make it 50 x 50 size. Specify the image and dimension in the argument
my_img = cv2.resize(img, (50, 50))

#I'm using imwrite to save the image
cv2.imwrite('resize.jpg', my_img)
img = plt.imread('resize.jpg')
plt.imshow(img)
plt.show()

image.png

Color space transformation

Color is represented by three elements: red, green, and blue.

In machine learning, this is often combined into one for convenience due to computer performance. In other words, it is expressed in black and white.

import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('./6100_gender_recognition_data/female/Adriana_Lima_0001.jpg')

#You can convert to a monochrome image by setting the second argument of cvtColor to RGB2GRAY.
my_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

cv2.imwrite('cvtColor.jpg', my_img)
img = plt.imread('cvtColor.jpg')
plt.imshow(img)

#Matplotlib recognizes the first element as green, so use gray to display it in black and white.
plt.gray()
plt.show()

image.png

Summary

Review of basic operation of OpenCV

import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('./6100_gender_recognition_data/female/Angelina_Jolie_0001.jpg')

my_img = cv2.resize(img, (50, 50))
my_img = cv2.cvtColor(my_img, cv2.COLOR_RGB2GRAY)

cv2.imwrite('final.jpg', my_img)
img = plt.imread('final.jpg')
plt.imshow(img)
plt.gray()
plt.show()

image.png

Recommended Posts

Python: Gender Identification (Deep Learning Development) Part 1
Python: Gender Identification (Deep Learning Development) Part 2
Python Deep Learning
Deep learning × Python
Python: Deep Learning Practices
Python: Deep Learning Tuning
Video frame interpolation by deep learning Part1 [Python]
Python: Supervised Learning: Hyperparameters Part 1
Python: Supervised Learning: Hyperparameters Part 2
Cat breed identification with deep learning
Try deep learning with TensorFlow Part 2
(python) Deep Learning Library Chainer Basics Basics
python learning
Deep Learning
"Deep Learning from scratch" Self-study memo (Part 12) Deep learning
[Python / Machine Learning] Why Deep Learning # 1 Perceptron Neural Network
Machine learning starting with Python Personal memorandum Part2
[Python] Learning Note 1
Python learning notes
Report_Deep Learning (Part 2)
Report_Deep Learning (Part 1)
QGIS + Python Part 1
Machine learning starting with Python Personal memorandum Part1
Deep Learning Memorandum
[Python] Udemy Image Judgment AI App Development Part1
Start Deep learning
EV3 x Python Machine Learning Part 2 Linear Regression
Report_Deep Learning (Part 2)
Python learning site
[Note] Python, when starting machine learning / deep learning [Links]
Python learning day 4
Stock Price Forecast Using Deep Learning (TensorFlow) -Part 2-
Python: Deep Learning in Natural Language Processing: Basics
Python learning (supplement)
Python: Scraping Part 1
Python vs Ruby "Deep Learning from scratch" Summary
python learning notes
Python3 Beginning Part 1
First deep learning in C #-Imitating implementation in Python-
Python: Scraping Part 2
Build a machine learning application development environment with Python
Deep Learning from the mathematical basics Part 2 (during attendance)
Deep Learning Experienced in Python Chapter 2 (Materials for Journals)
Python class (Python learning memo ⑦)
"Object-oriented" learning with python
Python module (Python learning memo ④)
Reinforcement learning 1 Python installation
Python ~ Grammar speed learning ~
Python: Unsupervised Learning: Basics
Deep learning / activation functions
Deep Learning from scratch
Python package development notes
Deep learning 1 Practice of deep learning
Python development environment construction
Deep learning / cross entropy
First Deep Learning ~ Preparation ~
First Deep Learning ~ Solution ~
[AI] Deep Metric Learning
Private Python learning procedure
Learning Python with ChemTHEATER 02
I tried deep learning