• 服务医学,基于目标检测模型实现细胞检测识别


    在我以往的认知里面,显微镜下面的世界是很神奇的存在,可能只平时接触到的绝大多数是都是宏观的世界吧,看到微观世界里面各色各样的生物、细胞就会觉得很神奇,电子显微镜往往都是医生来操作观察的,对于采样、病理切片等任务比较繁重的时候,人工就会显得捉襟见肘了,这时候AI就可以提供帮助了,基于大量的医学数据进行学习拟合,得到的模型甚至能够超越领域专家,这些在很多主营医疗业务的公司里面都得到的实现,本文今天并不是谈很宏大的东西,就是做细胞数据集的目标检测,医学相关的数据感觉很有意思,后面有机会我也会多去做这个领域的项目,

    首先看下效果图:

     这里面一共标注了三种细胞类型:

    1. 红细胞
    2. 血小板
    3. 白细胞

    英文表示分别为:

    1. RBC
    2. Platelets
    3. WBC

    接下来看下数据集:

     最近都在做一些轻量级的模型,这里模型选用的是MobileNetV2-YOLOv3-Lite的,详情如下所示:

    1. [net]
    2. # Training
    3. batch=128
    4. subdivisions=2
    5. width=320
    6. height=320
    7. channels=3
    8. momentum=0.9
    9. decay=4e-5
    10. angle=0
    11. saturation = 1.5
    12. exposure = 1.5
    13. hue=.1
    14. learning_rate=0.001
    15. burn_in=1000
    16. max_batches = 50200
    17. policy=steps
    18. steps=40000,45000
    19. scales=.1,.1
    20. [convolutional]
    21. filters=32
    22. size=3
    23. stride=2
    24. pad=1
    25. batch_normalize=1
    26. activation=relu
    27. [convolutional]
    28. filters=32
    29. size=1
    30. stride=1
    31. pad=1
    32. batch_normalize=1
    33. activation=relu
    34. [convolutional]
    35. filters=32
    36. size=3
    37. groups=32
    38. stride=1
    39. pad=1
    40. batch_normalize=1
    41. activation=relu
    42. [convolutional]
    43. filters=16
    44. size=1
    45. stride=1
    46. pad=1
    47. batch_normalize=1
    48. activation=linear
    49. [convolutional]
    50. filters=96
    51. size=1
    52. stride=1
    53. pad=1
    54. batch_normalize=1
    55. activation=relu
    56. [convolutional]
    57. filters=96
    58. size=3
    59. groups=96
    60. stride=2
    61. pad=1
    62. batch_normalize=1
    63. activation=relu
    64. [convolutional]
    65. filters=24
    66. size=1
    67. stride=1
    68. pad=1
    69. batch_normalize=1
    70. activation=linear
    71. [convolutional]
    72. filters=144
    73. size=1
    74. stride=1
    75. pad=1
    76. batch_normalize=1
    77. activation=relu
    78. [convolutional]
    79. filters=144
    80. size=3
    81. groups=144
    82. stride=1
    83. pad=1
    84. batch_normalize=1
    85. activation=relu
    86. [convolutional]
    87. filters=24
    88. size=1
    89. stride=1
    90. pad=1
    91. batch_normalize=1
    92. activation=linear
    93. [shortcut]
    94. from=-4
    95. activation=linear
    96. [convolutional]
    97. filters=144
    98. size=1
    99. stride=1
    100. pad=1
    101. batch_normalize=1
    102. activation=relu
    103. [convolutional]
    104. filters=144
    105. size=3
    106. groups=144
    107. stride=2
    108. pad=1
    109. batch_normalize=1
    110. activation=relu
    111. [convolutional]
    112. filters=32
    113. size=1
    114. stride=1
    115. pad=1
    116. batch_normalize=1
    117. activation=linear
    118. [convolutional]
    119. filters=192
    120. size=1
    121. stride=1
    122. pad=1
    123. batch_normalize=1
    124. activation=relu
    125. [convolutional]
    126. filters=192
    127. size=3
    128. groups=192
    129. stride=1
    130. pad=1
    131. batch_normalize=1
    132. activation=relu
    133. [convolutional]
    134. filters=32
    135. size=1
    136. stride=1
    137. pad=1
    138. batch_normalize=1
    139. activation=linear
    140. [shortcut]
    141. from=-4
    142. activation=linear
    143. [convolutional]
    144. filters=192
    145. size=1
    146. stride=1
    147. pad=1
    148. batch_normalize=1
    149. activation=relu
    150. [convolutional]
    151. filters=192
    152. size=3
    153. groups=192
    154. stride=1
    155. pad=1
    156. batch_normalize=1
    157. activation=relu
    158. [convolutional]
    159. filters=32
    160. size=1
    161. stride=1
    162. pad=1
    163. batch_normalize=1
    164. activation=linear
    165. [shortcut]
    166. from=-4
    167. activation=linear
    168. [convolutional]
    169. filters=192
    170. size=1
    171. stride=1
    172. pad=1
    173. batch_normalize=1
    174. activation=relu
    175. [convolutional]
    176. filters=192
    177. size=3
    178. groups=192
    179. stride=1
    180. pad=1
    181. batch_normalize=1
    182. activation=relu
    183. [convolutional]
    184. filters=64
    185. size=1
    186. stride=1
    187. pad=1
    188. batch_normalize=1
    189. activation=linear
    190. [convolutional]
    191. filters=384
    192. size=1
    193. stride=1
    194. pad=1
    195. batch_normalize=1
    196. activation=relu
    197. [convolutional]
    198. filters=384
    199. size=3
    200. groups=384
    201. stride=1
    202. pad=1
    203. batch_normalize=1
    204. activation=relu
    205. [convolutional]
    206. filters=64
    207. size=1
    208. stride=1
    209. pad=1
    210. batch_normalize=1
    211. activation=linear
    212. [shortcut]
    213. from=-4
    214. activation=linear
    215. [convolutional]
    216. filters=384
    217. size=1
    218. stride=1
    219. pad=1
    220. batch_normalize=1
    221. activation=relu
    222. [convolutional]
    223. filters=384
    224. size=3
    225. groups=384
    226. stride=1
    227. pad=1
    228. batch_normalize=1
    229. activation=relu
    230. [convolutional]
    231. filters=64
    232. size=1
    233. stride=1
    234. pad=1
    235. batch_normalize=1
    236. activation=linear
    237. [shortcut]
    238. from=-4
    239. activation=linear
    240. [convolutional]
    241. filters=384
    242. size=1
    243. stride=1
    244. pad=1
    245. batch_normalize=1
    246. activation=relu
    247. [convolutional]
    248. filters=384
    249. size=3
    250. groups=384
    251. stride=1
    252. pad=1
    253. batch_normalize=1
    254. activation=relu
    255. [convolutional]
    256. filters=64
    257. size=1
    258. stride=1
    259. pad=1
    260. batch_normalize=1
    261. activation=linear
    262. [shortcut]
    263. from=-4
    264. activation=linear
    265. [convolutional]
    266. filters=384
    267. size=1
    268. stride=1
    269. pad=1
    270. batch_normalize=1
    271. activation=relu
    272. [convolutional]
    273. filters=384
    274. size=3
    275. groups=384
    276. stride=2
    277. pad=1
    278. batch_normalize=1
    279. activation=relu
    280. [convolutional]
    281. filters=96
    282. size=1
    283. stride=1
    284. pad=1
    285. batch_normalize=1
    286. activation=linear
    287. [convolutional]
    288. filters=576
    289. size=1
    290. stride=1
    291. pad=1
    292. batch_normalize=1
    293. activation=relu
    294. [convolutional]
    295. filters=576
    296. size=3
    297. groups=576
    298. stride=1
    299. pad=1
    300. batch_normalize=1
    301. activation=relu
    302. [convolutional]
    303. filters=96
    304. size=1
    305. stride=1
    306. pad=1
    307. batch_normalize=1
    308. activation=linear
    309. [shortcut]
    310. from=-4
    311. activation=linear
    312. [convolutional]
    313. filters=576
    314. size=1
    315. stride=1
    316. pad=1
    317. batch_normalize=1
    318. activation=relu
    319. [convolutional]
    320. filters=576
    321. size=3
    322. groups=576
    323. stride=1
    324. pad=1
    325. batch_normalize=1
    326. activation=relu
    327. [convolutional]
    328. filters=96
    329. size=1
    330. stride=1
    331. pad=1
    332. batch_normalize=1
    333. activation=linear
    334. [shortcut]
    335. from=-4
    336. activation=linear
    337. [convolutional]
    338. filters=576
    339. size=1
    340. stride=1
    341. pad=1
    342. batch_normalize=1
    343. activation=relu
    344. [convolutional]
    345. filters=576
    346. size=3
    347. groups=576
    348. stride=2
    349. pad=1
    350. batch_normalize=1
    351. activation=relu
    352. [convolutional]
    353. filters=160
    354. size=1
    355. stride=1
    356. pad=1
    357. batch_normalize=1
    358. activation=linear
    359. [convolutional]
    360. filters=960
    361. size=1
    362. stride=1
    363. pad=1
    364. batch_normalize=1
    365. activation=relu
    366. [convolutional]
    367. filters=960
    368. size=3
    369. groups=960
    370. stride=1
    371. pad=1
    372. batch_normalize=1
    373. activation=relu
    374. [convolutional]
    375. filters=160
    376. size=1
    377. stride=1
    378. pad=1
    379. batch_normalize=1
    380. activation=linear
    381. [shortcut]
    382. from=-4
    383. activation=linear
    384. [convolutional]
    385. filters=960
    386. size=1
    387. stride=1
    388. pad=1
    389. batch_normalize=1
    390. activation=relu
    391. [convolutional]
    392. filters=960
    393. size=3
    394. groups=960
    395. stride=1
    396. pad=1
    397. batch_normalize=1
    398. activation=relu
    399. [convolutional]
    400. filters=160
    401. size=1
    402. stride=1
    403. pad=1
    404. batch_normalize=1
    405. activation=linear
    406. [shortcut]
    407. from=-4
    408. activation=linear
    409. ### SPP ###
    410. [maxpool]
    411. stride=1
    412. size=3
    413. [route]
    414. layers=-2
    415. [maxpool]
    416. stride=1
    417. size=5
    418. [route]
    419. layers=-4
    420. [maxpool]
    421. stride=1
    422. size=9
    423. [route]
    424. layers=-1,-3,-5,-6
    425. ### End SPP ###
    426. #################################
    427. [convolutional]
    428. filters=288
    429. size=1
    430. stride=1
    431. pad=1
    432. batch_normalize=1
    433. activation=relu
    434. [convolutional]
    435. filters=288
    436. size=3
    437. groups=288
    438. stride=1
    439. pad=1
    440. batch_normalize=1
    441. activation=relu
    442. [convolutional]
    443. filters=96
    444. size=1
    445. stride=1
    446. pad=1
    447. batch_normalize=1
    448. activation=relu
    449. [convolutional]
    450. filters=384
    451. size=1
    452. stride=1
    453. pad=1
    454. batch_normalize=1
    455. activation=relu
    456. [convolutional]
    457. size=1
    458. stride=1
    459. pad=1
    460. filters=24
    461. activation=linear
    462. [yolo]
    463. mask = 3,4,5
    464. anchors = 26, 48, 67, 84, 72,175, 189,126, 137,236, 265,259
    465. classes=3
    466. num=6
    467. jitter=.1
    468. ignore_thresh = .5
    469. truth_thresh = 1
    470. random=1
    471. #################
    472. scale_x_y = 1.0
    473. iou_thresh=0.213
    474. cls_normalizer=1.0
    475. iou_normalizer=0.07
    476. iou_loss=ciou
    477. nms_kind=greedynms
    478. beta_nms=0.6
    479. ##################################
    480. [route]
    481. layers= 65
    482. [upsample]
    483. stride=2
    484. [route]
    485. layers=-1,48
    486. #################################
    487. [convolutional]
    488. filters=80
    489. size=1
    490. stride=1
    491. pad=1
    492. batch_normalize=1
    493. activation=relu
    494. [convolutional]
    495. filters=288
    496. size=1
    497. stride=1
    498. pad=1
    499. batch_normalize=1
    500. activation=relu
    501. [convolutional]
    502. filters=288
    503. size=3
    504. groups=288
    505. stride=1
    506. pad=1
    507. batch_normalize=1
    508. activation=relu
    509. [convolutional]
    510. filters=192
    511. size=1
    512. stride=1
    513. pad=1
    514. batch_normalize=1
    515. activation=relu
    516. [convolutional]
    517. filters=288
    518. size=1
    519. stride=1
    520. pad=1
    521. batch_normalize=1
    522. activation=relu
    523. [convolutional]
    524. size=1
    525. stride=1
    526. pad=1
    527. filters=24
    528. activation=linear
    529. [yolo]
    530. mask = 0,1,2
    531. anchors = 26, 48, 67, 84, 72,175, 189,126, 137,236, 265,259
    532. classes=3
    533. num=6
    534. jitter=.1
    535. ignore_thresh = .5
    536. truth_thresh = 1
    537. random=1
    538. #################
    539. scale_x_y = 1.0
    540. iou_thresh=0.213
    541. cls_normalizer=1.0
    542. iou_normalizer=0.07
    543. iou_loss=ciou
    544. nms_kind=greedynms
    545. beta_nms=0.6

    同样一共需要修改两处参数配置

    【第一处】

     【第二处】

     

    主要就是修改里面classes和filters这两个关键字段即可:
          classes就是你要检测的目标对象数量,这里我要检测的目标对象数量是:3

          filters的数值计算公式为:  filters=(classes+5)*,在这里就是  (3+5)*3=24

    接下来编写cell.names和cell.data文件用于框架训练需要。

    cell.names如下所示:

    1. RBC
    2. Platelets
    3. WBC

    cell.data如下所示:

    1. classes= 3
    2. train = /home/objDet/cell/train.txt
    3. valid = /home/objDet/cell/test.txt
    4. names = /home/objDet/cell/x.names
    5. backup = /home/objDet/cell/model/yolo

    基于Darknet框架可以一键启动训练,命令如下:

    1. chmod +x darknet
    2. nohup ./darknet detector train x.data MobileNetV2-YOLOv3-Lite.cfg >> yolo.out &

    入股对数据集格式有疑问的可以翻看前面的文章,前面已经详细介绍过了,所以后面的文章就不在赘述了。

    随机选择数据测试结果如下:

     这里训练完成后, 我同样对整个训练过程的日志进行了可视化

    我在上一篇文章《AI助力智能安检,基于目标检测模型实现X光安检图像智能检测分析》中,编写了原始日志的解析代码,如下:

    1. def parseLogs(data="yolo.out"):
    2. """
    3. 解析原始日志
    4. """
    5. with open(data) as f:
    6. data_list=[one.strip() for one in f.readlines() if one.strip()]
    7. print("data_list_length: ", len(data_list))
    8. start=0
    9. for i in range(len(data_list)):
    10. if "hours left" in data_list[i]:
    11. start=i
    12. break
    13. print("start: ", start)
    14. s=start+1
    15. res_list=[]
    16. while s<len(data_list):
    17. if "hours left" in data_list[s]:
    18. one_epoch=data_list[start:s]
    19. res_list.append(one_epoch)
    20. start=s
    21. s+=1
    22. else:
    23. s+=1
    24. print("res_list_length: ", len(res_list))
    25. for one_line in res_list[0]:
    26. print(one_line)

    今天发现了可以有更简单的写法,代码如下:

    1. def extractNeedLog(log_file,new_log_file,key_word):
    2. """
    3. 提取所需要的日志数据
    4. """
    5. with open(log_file, 'r') as f:
    6. with open(new_log_file, 'w') as train_log:
    7. for line in f:
    8. if key_word in line:
    9. train_log.write(line)
    10. f.close()
    11. train_log.close()

    因为Darknet框架的训练日志是非常规则的文本形式,可以根据简单的关键词匹配即可完成所需信息的提取操作,不需要像我之前那样先分离了单个epoch的日志,之后再二次提取。

    分离提取了loss日志和metric日志,这里对其进行可视化如下:

    loss曲线

     学习率曲线

     时耗曲线

     图像累积量曲线

     其实这个没有必要可视化,简单分析就知道是一条直线了,因为每次选取的图片数是固定的,乘以总次数自然就是一条正比例直线了。

    剩余时间曲线

     这个是模型训练过程中不断打印输出的剩余训练时间的曲线,总体趋势降低就是对的了,中间有起伏跟服务器硬件有关系。

    接下来是训练过程中的一些评估指标的可视化。

     我是把觉得能可视化的都给可视化了,其实真实可能并不需要这么多,只需要关注几个重点需要关注的指标即可,比如:IOU,Obj、Class等等。

  • 相关阅读:
    Windows系统补丁管理工具
    现网常见问题及解决思路
    怎么把一个excel拆分成多个
    如何办理软件企业认定证书
    MFC Windows 程序设计[310]之混搭个性按钮组群(附源码)
    LeetCode第 85 场双周赛总结
    汇编语言学习笔记及总结
    【Leetcode】1381. Design a Stack With Increment Operation
    mysql是怎么运行的-笔记
    【Java】应用层协议HTTP和HTTPS
  • 原文地址:https://blog.csdn.net/Together_CZ/article/details/126041456