- 目的
构建一个卷积神经网络对性别以及美丽程度(beautiful or handsome)进行识别。 - 数据准备
模型中采用的数据来自百度搜索数据,为了效率可采用爬虫(大家可以在以下相关代码上进行更改):http://blog.sina.com.cn/s/blog_13927ddb50102w2m1.html,下图为采集的相关图片。 - 数据预处理
为了数据在输入模型时具有统一的大小,对数据进行了大小的转换,统一变成了200*150大小的图片。这里利用opencv进行实现。相关代码如下,根据长宽的不同变化有所不同:
网盘上有处理好的数据:链接:https://pan.baidu.com/s/1GYuiT6VRlO9BNJe_wZtlkw 密码:9ekpimport os import cv2 import math def rotate(img,angle): height = img.shape[0] width = img.shape[1] if angle%180 == 0: scale = 1 elif angle%90 == 0: scale = float(max(height, width))/min(height, width) else: scale = math.sqrt(pow(height,2)+pow(width,2))/min(height, width) rotateMat = cv2.getRotationMatrix2D((width/2, height/2), angle, scale) rotateImg = cv2.warpAffine(img, rotateMat, (width, height)) return rotateImg def convert2samll(path): path_f_b = path file_f_b = os.listdir(path_f_b) print(path_f_b) for file_name in file_f_b: img = cv2.imread(path_f_b+'/'+file_name) if type(img) == type(None): print(path_f_b+'/'+file_name) continue sp = img.shape if sp[0] > sp[1] or sp[0] == sp[1]: r_img = cv2.resize(img,(150,200)) cv2.imwrite(path_f_b+'/data/'+file_name,r_img) else: r_img = cv2.resize(img,(200,150)) r_img = rotate(r_img,90) r_img = cv2.resize(r_img,(150,200)) cv2.imwrite(path_f_b+'/data/'+file_name,r_img)
卷积神经网络简单的应用(一):目的与数据
最新推荐文章于 2023-07-24 10:23:27 发布