使用python计算自己数据集的mean和std

首先先把数据集的图片路径保存在一个txt文件夹里面


   
  1. import os
  2. def generate(dir, label):
  3. listText = open( 'list.txt', 'a')
  4. for file in dir:
  5. fileType = os.path.split(file)
  6. if fileType[ 1] == '.txt':
  7. continue
  8. name = file + ' ' + str(int(label)) + '\n'
  9. listText.write(name)
  10. listText.close()
  11. outer_path = 'E:/lly/data/' # 这里是你的图片的目录
  12. if __name__ == '__main__':
  13. i = 1
  14. num = 0
  15. personlist = os.listdir(outer_path) # 列举文件夹
  16. personlist.sort()
  17. for person in personlist:
  18. personPath = outer_path+person + "/"
  19. fingerlist = os.listdir(personPath)
  20. fingerlist.sort()
  21. for finger in fingerlist:
  22. finallPATH=os.path.join(outer_path, person,finger)
  23. finallPATH=finallPATH.replace( '\\', '/')
  24. listText = open( 'image_list.txt', 'a')
  25. fileType = os.path.split(finallPATH)
  26. name = finallPATH+ '\n'
  27. listText.write(name)
  28. listText.close()
  29. i += 1

计算自己数据集的均值和方差:


   
  1. # -*- coding: utf-8 -*-**
  2. import numpy as np
  3. import cv2
  4. import random
  5. import os
  6. # calculate means and std 注意换行\n符号**
  7. # train.txt中每一行是图像的位置信息**
  8. path = 'C:/Users/lenovo/PycharmProjects/my/image_list.txt'
  9. means = [ 0, 0, 0]
  10. stdevs = [ 0, 0, 0]
  11. index = 1
  12. num_imgs = 0
  13. with open(path, 'r') as f:
  14. lines = f.readlines()
  15. # random.shuffle(lines)
  16. for line in lines:
  17. print(line)
  18. print( '{}/{}'.format(index, len(lines)))
  19. index += 1
  20. a = os.path.join(line)
  21. # print(a[:-1])
  22. num_imgs += 1
  23. img = cv2.imread(a[: -1])
  24. print(img, 22)
  25. img = np.asarray(img)
  26. img = img.astype(np.float32) / 255.
  27. for i in range( 3):
  28. means[i] += img[:, :, i].mean()
  29. stdevs[i] += img[:, :, i].std()
  30. print(num_imgs)
  31. means.reverse()
  32. stdevs.reverse()
  33. means = np.asarray(means) / num_imgs
  34. stdevs = np.asarray(stdevs) / num_imgs
  35. print( "normMean = {}".format(means))
  36. print( "normStd = {}".format(stdevs))
  37. print( 'transforms.Normalize(normMean = {}, normStd = {})'.format(means, stdevs))

 

参考:https://blog.csdn.net/weixin_38533896/article/details/85951903

上一篇:08 机器学习 - Kmeans聚类算法原理


下一篇:K-means聚类算法及python代码实现