设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 重新 试卷 文件
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

使用深度学习检测疟疾(3)

发布时间:2019-05-24 20:19 所属栏目:21 来源:Dipanjan (dj) Sarkar
导读:这些图片尺寸并不相同,因为血涂片和细胞图像是基于人、测试方法、图片方向不同而不同的。让我们总结我们的训练数据集的统计信息来决定最佳的图像尺寸(牢记,我们根本不会碰测试数据集)。 import cv2 from concur

这些图片尺寸并不相同,因为血涂片和细胞图像是基于人、测试方法、图片方向不同而不同的。让我们总结我们的训练数据集的统计信息来决定最佳的图像尺寸(牢记,我们根本不会碰测试数据集)。

  1. import cv2
  2. from concurrent import futures
  3. import threading
  4.  
  5. def get_img_shape_parallel(idx, img, total_imgs):
  6. if idx % 5000 == 0 or idx == (total_imgs - 1):
  7. print('{}: working on img num: {}'.format(threading.current_thread().name,
  8. idx))
  9. return cv2.imread(img).shape
  10. ex = futures.ThreadPoolExecutor(max_workers=None)
  11. data_inp = [(idx, img, len(train_files)) for idx, img in enumerate(train_files)]
  12. print('Starting Img shape computation:')
  13. train_img_dims_map = ex.map(get_img_shape_parallel,
  14. [record[0] for record in data_inp],
  15. [record[1] for record in data_inp],
  16. [record[2] for record in data_inp])
  17. train_img_dims = list(train_img_dims_map)
  18. print('Min Dimensions:', np.min(train_img_dims, axis=0))
  19. print('Avg Dimensions:', np.mean(train_img_dims, axis=0))
  20. print('Median Dimensions:', np.median(train_img_dims, axis=0))
  21. print('Max Dimensions:', np.max(train_img_dims, axis=0))
  22.  
  23.  
  24. # Output
  25. Starting Img shape computation:
  26. ThreadPoolExecutor-0_0: working on img num: 0
  27. ThreadPoolExecutor-0_17: working on img num: 5000
  28. ThreadPoolExecutor-0_15: working on img num: 10000
  29. ThreadPoolExecutor-0_1: working on img num: 15000
  30. ThreadPoolExecutor-0_7: working on img num: 17360
  31. Min Dimensions: [46 46 3]
  32. Avg Dimensions: [132.77311215 132.45757733 3.]
  33. Median Dimensions: [130. 130. 3.]
  34. Max Dimensions: [385 394 3]

我们应用并行处理来加速图像读取,并且基于汇总统计结果,我们将每幅图片的尺寸重新调整到 125x125 像素。让我们载入我们所有的图像并重新调整它们为这些固定尺寸。

  1. IMG_DIMS = (125, 125)
  2.  
  3. def get_img_data_parallel(idx, img, total_imgs):
  4. if idx % 5000 == 0 or idx == (total_imgs - 1):
  5. print('{}: working on img num: {}'.format(threading.current_thread().name,
  6. idx))
  7. img = cv2.imread(img)
  8. img = cv2.resize(img, dsize=IMG_DIMS,
  9. interpolation=cv2.INTER_CUBIC)
  10. img = np.array(img, dtype=np.float32)
  11. return img
  12.  
  13. ex = futures.ThreadPoolExecutor(max_workers=None)
  14. train_data_inp = [(idx, img, len(train_files)) for idx, img in enumerate(train_files)]
  15. val_data_inp = [(idx, img, len(val_files)) for idx, img in enumerate(val_files)]
  16. test_data_inp = [(idx, img, len(test_files)) for idx, img in enumerate(test_files)]
  17.  
  18. print('Loading Train Images:')
  19. train_data_map = ex.map(get_img_data_parallel,
  20. [record[0] for record in train_data_inp],
  21. [record[1] for record in train_data_inp],
  22. [record[2] for record in train_data_inp])
  23. train_data = np.array(list(train_data_map))
  24.  
  25. print('\nLoading Validation Images:')
  26. val_data_map = ex.map(get_img_data_parallel,
  27. [record[0] for record in val_data_inp],
  28. [record[1] for record in val_data_inp],
  29. [record[2] for record in val_data_inp])
  30. val_data = np.array(list(val_data_map))
  31.  
  32. print('\nLoading Test Images:')
  33. test_data_map = ex.map(get_img_data_parallel,
  34. [record[0] for record in test_data_inp],
  35. [record[1] for record in test_data_inp],
  36. [record[2] for record in test_data_inp])
  37. test_data = np.array(list(test_data_map))
  38.  
  39. train_data.shape, val_data.shape, test_data.shape
  40.  
  41.  
  42. # Output
  43. Loading Train Images:
  44. ThreadPoolExecutor-1_0: working on img num: 0
  45. ThreadPoolExecutor-1_12: working on img num: 5000
  46. ThreadPoolExecutor-1_6: working on img num: 10000
  47. ThreadPoolExecutor-1_10: working on img num: 15000
  48. ThreadPoolExecutor-1_3: working on img num: 17360
  49.  
  50. Loading Validation Images:
  51. ThreadPoolExecutor-1_13: working on img num: 0
  52. ThreadPoolExecutor-1_18: working on img num: 1928
  53.  
  54. Loading Test Images:
  55. ThreadPoolExecutor-1_5: working on img num: 0
  56. ThreadPoolExecutor-1_19: working on img num: 5000
  57. ThreadPoolExecutor-1_8: working on img num: 8267
  58. ((17361, 125, 125, 3), (1929, 125, 125, 3), (8268, 125, 125, 3))

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读