Object detection of Blood Cells using IceVision
This article explores object detection with the BCCD (Blood Cell Count and Detection) dataset using IceVision.
Created on March 3|Last edited on December 5
Comment
IntroductionBackgroundGoalData PreparationData downloadEDAModelingBasic settingProcess of model constructionEvaluationResult of validation datasetResult of holdout testThoughtsFuture workImpression of W&B
Introduction

Background
The laboratory industry and medical institutions are experiencing a significant shortage of qualified medical laboratory professionals as the demand for lab-based diagnostic testing services grows daily. With the development of artificial intelligence technology, many medical institutions in the world have rapidly entered artificial intelligence field to assist doctors. Object detection technology has been widely used in the lab-based diagnostic testing field. Actually, the author has had opportunities to work with researchers at research institutions and private companies' R&D departments who are trying to use AI technology in the area of clinical testing. In clinical testing, there are a wide variety of test objects and test methods, so it is necessary for researchers to be able to develop object detection models quickly and easily.

This article explores object detection of the BCCD (Blood Cell Count and Detection) dataset. The BCCD Dataset is a small-scale dataset for blood cell detection under MIT license. The BCCD Dataset contains labeled cell images of RBC (Red Blood Cell), WBC (White Blood Cell), and Platelet. RBC, WBC, and Platelet are blood cells essential for the human body. WBC plays an important role in protecting the human body from various infectious diseases in the human immune system. It is said that an irregular number of WBCs and abnormal WBCs (such as Neutrophils, Eosinophils, Monocytes, and Lymphocytes) cause various severe diseases. So, WBC counting and WBC type classification are essential for doctors' diagnoses. Actually, there are some industrial automated cell morphology systems like MEDICA EasyCell® assistant Cell Image Analysis (Sysmex). As a starting point of artificial intelligence application to this field, this report focuses on detecting and classifying three types of blood cells (RBC, WBC, and Platelets).
IceVision was used for model building because the execution was easy, and covered lots of algorithms of object detection.
IceVision is the first agnostic computer vision framework to offer a curated collection with hundreds of high-quality pre-trained models from torchvision, MMLabs, and soon Pytorch Image Models. It orchestrates the end-to-end deep learning workflow allowing to train networks with easy-to-use robust high-performance libraries such as Pytorch-Lightning and Fastai (IceVision)

Goal
The goal of this report is to share an easy-to-follow flow of object detection, so that researchers can refer to this report as a starting point for their clinical laboratory test researches.
Data Preparation

Data download
The BCCD Dataset can be downloaded from https://github.com/Shenggan/BCCD_Dataset.
The structure of BCCD folder is following:
BCCD├── Annotations: BloodImage_00XYZ.xml (364 items)├── ImageSets # Contain four Main/*.txt which split the dataset└── JPEGImages: BloodImage_00XYZ.jpg (364 items)
EDA
Raw data check
To look through each image interactively, "Artifact" of Wb was used. After registering the BCCD dataset to Wb's Aritifact, "Table" of Wb was created to check count of each label in each image and to check each image interactively.
Distribution check
Distribution of each label's counts in training data, validation data, and test data was checked to ensure generalized performance. This dataset don't seem to have significant biases among training data, validation data, and test data.
Finding weird cases
Looking through some images, there are some ones which don't have precise labels (e.g. BloodImage_00134.jpg). As there are too many RBCs, labels of RBCs don't seem to be labeled precisely while those of WBCs and Platelets are labeled more cautiously. As RBC labels are not annotated correctly, evaluating with the only overall mAP would not be appropriate. So, in this project, APs of WBC and Platelets are checked cautiously in addition to mAP.
Modeling

Basic setting
- Augmentation
- As the intrinsic information of images don't change when flipping, the default augmentation setting of IceVision (horizontal flip, shift, scaling, rotating, RGB shift, random brightness contrast, bluring, croping) was applied to the dataset.
- Model type
- Some object detection models: Faster RCNN, Yolov5, Yolof, Vfnet, Retinanet, Deter, Deformale det, Fsaf, Centripetalent, and Efficientdet were tried. (SSD, Yolox, Retinanet, and Sbal were removed as those caused errors in version 0.12.0 of IceVision.)
- Optimization
- Three famous optimization algorithms Adam, NAdam and AdamW were tried.
- Evaluation metric
- As described in the previous section, it is important to check not only overall mAP but also AP of each label as some labels of RBCs are missing. So, so AP of each label was tracked in this report.
- COCO mAP, which is a popular one type of mAP in measuring the accuracy of object detection models, was used for this report.
COCO mAPAverage precision (AP) is a metric to measure the accuracy of object detection models. Average precision computes the average precision value for recall value over 0 to 1. mAP is the average value of APs for each class.In COCO mAP, the average AP over multiple IoU is calculated. AP@[.5:.95] corresponds to the average AP for IoU from 0.5 to 0.95 with a step size of 0.05. AP@[.5:.95] over 10 IoU levels on 80 categories is used for the COCO competition. AP@[.5:.95] was used as an evaluation metric for this report.💡 - Learning rate
- Learning rate is an important hyper-parameter as it controls the rate or speed of model learning. Lesile Smith (2015) proposed an idea regarding learning rate, called the learning rate finder. The idea is to reduce the amount of guesswork on picking a good starting learning rate. Fastai's learning rate finder was used in this report, and tried four methods ("minimum", "steep", "valley", "slide").

Fig2. example of a result of learn.lr_find()
Process of model construction
The final model was constructed as follows
- First, some model types with pre-trained backbones were tried as base models.
- Additional learning was applied to each model. The detailed setting of simulation can be seen in W&S Sweep setting page.
- Then, hyper parameter tuning was conducted to the best model type.
- Finally, the model was retrained with larger images and more epochs.
Sweep of W&B was used for model type search and hyper parameter tuning. The following script shows the process of model type comparison.
sweep_config = {'method':'random','name':'model','metric':{'name':'COCOMetric','goal':'maximize'},}parameters_dict = {'model_selection':{'values':["vfnet","retinanet","faster_rcnn","yolof","detr","deformale_detr","fsaf","centripetalnet","efficientdet","yolov5"]},'pre_size':{'value':(640,480)},'lr_type':{'values':['minimum','steep','valley', 'slide']},'opttype':{'values':['NAdam','Adam','AdamW']},####snip###}sweep_config['parameters'] = parameters_dictwandb.login(key=params.WANDB_API_KEY)sweep_id = wandb.sweep(sweep_config, project=params.WANDB_PROJECT)def train(config=None):run = wandb.init()config = wandb.config# data preparation (create_ds is a custom function for data preparation)train_ds,valid_ds,test_ds,parser = create_ds(imageSets_dir, annotations_dir,images_dir, config.img_size, config.pre_size, config.augment)# model definition (model_denifition is a custom function for model type definition)model,model_type = model_definition(selection=config.model_selection,parser=parser, img_size=config.img_size,backbones=config.backbones)# data load to modeltrain_dl = model_type.train_dl(train_ds, batch_size=config.batch_size, num_workers=config.num_workers, shuffle=False)valid_dl = model_type.valid_dl(valid_ds, batch_size=config.batch_size, num_workers=config.num_workers, shuffle=False)# model setting (COCOMetric_WBC(), COCOMetric_RBC(), and COCOMetric_Platelets() are custom class to calculate each class's AP)metrics = [COCOMetric(),COCOMetric_WBC(),COCOMetric_RBC(),COCOMetric_Platelets()]opt = opt_func_setting(config.opttype) # (opt_func_setting is a custom function to set optimizer)opt_func = partial(OptimWrapper, opt=opt) #https://docs.fast.ai/optimizer.html#optimizerlearn = model_type.fastai.learner(dls=[train_dl, valid_dl], model=model,metrics=metrics, opt_func=opt_func)# learning ratesuggested_lr = learn.lr_find(suggest_funcs=(minimum, steep, valley, slide),show_plot=False)suggested_funcs_dic = {'minimum':0,'steep':1,'valley':2, 'slide':3}base_lr = suggested_lr[suggested_funcs_dic[config.lr_type]]# call backscallbacks = [SaveModelCallback(monitor="COCOMetric"),WandbCallback(log_preds=False, log_model=False)]# fine tunelearn.fine_tune(config.epochs, base_lr= base_lr, freeze_epochs=config.freeze_epochs,cbs=callbacks)# loglog_final_metric(learn) # (log_final_metric is a custom function to log learners' metrics)wandb.agent(sweep_id,function=train,count=100)
1. Model type comparison
The following chart shows the result of the first model type search. Yolov5 seems to have the best performance in terms of COCO mAP and COCO APs of WBCs and Platelets. Although Yolov5 doesn't work well for detecting RBCs, it would be not huge issue in this stage of study because many RBC were not labeled correctly. It's surprising that Yolov5 is good at both speed and accuracy.
2. Hyper parameter tuning
3. Retraining with larger images and more epochs
Yolov5 was retrained with the best hyper parameters, larger images, and more epochs.
Evaluation

Result of validation dataset
The following table shows the prediction result of the validation dataset of the final model.
Although there are some cases where some WBCs cannot be detected, it seems good as a starting point. So, I moved on holdout test with the data which were not used in the training and validation phase.
Result of holdout test
Holdout test was conducted with the test data which were not used in training process to confirm the external validity. The following table shows the result. There doesn't seem a huge difference between the results of validation data and test data in terms of the evaluation metrics.
Looking through predicted images, precision of this model seems high. So, this model can be used for annotation tool to get more accurate data with less effort even if this cannot achieve full auto cell detection system.
Run: holdout test
1
Thoughts

In this report, I explored object detection with the BCCD dataset using IceVision. I hope that a flow of this report would be so useful that researchers can refer to as a starting point.
Future work
As the original dataset's ground truths are wrong, improvement of data annotation will be required. And, the data size should be increased more. Considering that it's time consuming for clinical laboratory technicians to do annotations without missing any labels, it may be effective to use the first simple model as an auxiliary service of annotation to get more data.
And, as there are too many RBCs, it may be better to focus on annotating only WBCs if only WBCs mainly matter for diagnosis. Not just only thinking how to build a good model but also how to build a system of collecting more high-quality data with an easily-built model would be important.
Impression of W&B
I describe my impressions of W&B at the end.
LIKE
- First things first, I like an experience of the reproducibility. Even if a new method is revolutionary, if it cannot be reproduced, it will not bring a value and not lead to the next innovation. From this perspective, I like the idea of ensuring a high level of transparency. I also like the concept of having a uniform output format of model evaluation and management as it promotes collaboration of works.
- If I pick up one my favorite feature, it would be the "Artifact". It enables me to check data interactively, and to track which model used which data in "Liniage". "Liniage" is simple, and makes our analysis more transparent and well-organized one.
- I also like the portion of open contents. W&B is useful for model building but also information gathering.
FEEDBACK
- It would be great if it gets easier to find examples of registering various type of data to Artifact in docs.
- To arrange artifacts, it would be better to have an option to delete multiple Artifact folders at once. Although there is an API to delete Artifacts, the folders were not erased when I tried.
- More flexibility of chart titles would improve report's readability.
Add a comment