首先先把数据集的图片路径保存在一个txt文件夹里面
- import os
- def generate(dir, label):
- listText = open( 'list.txt', 'a')
- for file in dir:
- fileType = os.path.split(file)
- if fileType[ 1] == '.txt':
- continue
- name = file + ' ' + str(int(label)) + '\n'
- listText.write(name)
- listText.close()
- outer_path = 'E:/lly/data/' # 这里是你的图片的目录
- if __name__ == '__main__':
- i = 1
- num = 0
- personlist = os.listdir(outer_path) # 列举文件夹
- personlist.sort()
- for person in personlist:
- personPath = outer_path+person + "/"
- fingerlist = os.listdir(personPath)
- fingerlist.sort()
- for finger in fingerlist:
- finallPATH=os.path.join(outer_path, person,finger)
- finallPATH=finallPATH.replace( '\\', '/')
- listText = open( 'image_list.txt', 'a')
- fileType = os.path.split(finallPATH)
- name = finallPATH+ '\n'
- listText.write(name)
- listText.close()
- i += 1
计算自己数据集的均值和方差:
- # -*- coding: utf-8 -*-**
- import numpy as np
- import cv2
- import random
- import os
- # calculate means and std 注意换行\n符号**
- # train.txt中每一行是图像的位置信息**
- path = 'C:/Users/lenovo/PycharmProjects/my/image_list.txt'
- means = [ 0, 0, 0]
- stdevs = [ 0, 0, 0]
- index = 1
- num_imgs = 0
- with open(path, 'r') as f:
- lines = f.readlines()
- # random.shuffle(lines)
- for line in lines:
- print(line)
- print( '{}/{}'.format(index, len(lines)))
- index += 1
- a = os.path.join(line)
- # print(a[:-1])
- num_imgs += 1
- img = cv2.imread(a[: -1])
- print(img, 22)
- img = np.asarray(img)
- img = img.astype(np.float32) / 255.
- for i in range( 3):
- means[i] += img[:, :, i].mean()
- stdevs[i] += img[:, :, i].std()
- print(num_imgs)
- means.reverse()
- stdevs.reverse()
- means = np.asarray(means) / num_imgs
- stdevs = np.asarray(stdevs) / num_imgs
- print( "normMean = {}".format(means))
- print( "normStd = {}".format(stdevs))
- print( 'transforms.Normalize(normMean = {}, normStd = {})'.format(means, stdevs))
参考:https://blog.csdn.net/weixin_38533896/article/details/85951903