Peta数据集识别性别

本文详述了使用Peta数据集进行性别识别的过程,包括数据预处理(resize、rebalance、划分训练集和测试集)、格式转换以及训练模型。遇到的挑战有显存管理、损失函数不变及代码错误,通过调整学习率和批大小解决了问题,最终获得76%的准确率。
摘要由CSDN通过智能技术生成

数据集预处理

(1)对每一张图片进行resize, resize到特定的大小50*150

(2)rebalance处理,对少数类样本进行随机选择n张进行数据增强之后重新加入到dataset中。

(3)划分训练集和测试集,0.2

(4)对训练集进行数据增强。 扩大训练数据量。 (操作包括: 翻转,滤波等)

#-*- encoding: utf-8 -*-
 
import os, sys, cv2
import numpy as np
import random
 
 
image_cnt = 0
MIN_HEIGHT = 120			#处理的最小尺寸
MIN_WIDTH = 40
targetLabel = []
 
positive_cnt = 0
negative_cnt = 0
 
def readImage( filePath , targetDir ):	#制定标签
    global image_cnt, positive_cnt, negative_cnt
    global targetLabel
    if not os.path.isdir( filePath ):
        print('{} is not a dir'.format(filePath))
        return None
    listFile = os.listdir( filePath )
    labelDict = {
   }
    with open( filePath + 'Label.txt', 'r') as reader:
        for line in reader:
            lines = line.split()
            for i in range(1, len(lines)):
                if lines[i] == 'personalMale': #男设为1
                    label = 1
                elif lines[i] == 'personalFemale': #女设为0
                    label = 0
                else:
                    continue
                labelDict[lines[0]] = label
                break
 
    for i in range(len(listFile)):
        if len(listFile[i]) > 4 and (listFile[i][-4:] == '.bmp' or listFile[i][-4:] == '.jpg' or \
            listFile[i][-4:] == '.png' or listFile[i][-5:] == '.jpeg'):
            imageName = filePath + listFile[i]
            img = cv2.imread( imageName )
            if not img.data:
                continue
            height, width = img.shape[:2]
            if height < MIN_HEIGHT or width < MIN_WIDTH:
                continue
            fileName = str( image_cnt ) + '.jpeg'
            identity = listFile[i].find('_') 
            if  identity == -1:
                identity = len(listFile[i]) 
            idd = listFile[i][:identity]
            if labelDict.has_key( idd ) :
                targetLabel.append([ fileName, labelDict[idd]])
                if labelDict[idd] == 0:
                    negative_cnt += 1
                else:
                    positive_cnt += 1
                img = cv2.resize(img, (50, 150), interpolation=cv2.INTER_CUBIC) #60*60
                cv2.imwrite(targetDir + fileName, img)
                image_cnt += 1
            else:
                print('file {} do not have label'.format(listFile[i]) )
 
 
####### pyramid operator
def MinAndEnlarge(img, Minus_pixel = 3):	#定义放缩图片大小50*150
    img = img[(3*Minus_pixel):(150 - 3*Minus_pixel), Minus_pixel:(50 - Minus_pixel), :]
    img = cv2.resize(img, (50, 150), interpolation = cv2.INTER_CUBIC )
    return img
 
####### rotate operator
def Flip(img, operator = 1):
    if operator == 1:
        img = cv2.flip(img, 1)
    else:
        img = cv2.flip(img, 0)
    return img
 
####### median blurring the image
def Blur(img, kernel_size=5):
    img = cv2.medianBlur(img, kernel_size)
    return img
 
 

 
def saveLabel( targetDir ):		#存储标签
    global targetLabel
    with open(targetDir + 'label.txt', 'w') as writer:
        for i in range(len(targetLabel)):
            writer.write( str( targetLabel[i][0] ) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值