/TestStripDX

TestStripDX: An image processing framework for processing and extracting test strip results from a photo

Primary LanguageJupyter Notebook

TestStripDX

TestStripDX: An image processing framework for processing and extracting test strip results from a photo.
This repo walks you through how we prepare, train and run TestStripDX detector in the Cloud through Google Colab.
1_VluiXEpyenaqzuBkhtY3iA YOLOv4 is a computer vision model for optimal speed and accuracy of object detection.

Prepare dataset for training

Before training custom detector, we need to prepare a dataset with annotations to provide your target areas to the model. Here, we utilized online annotation tool from the Roboflow which no needs to download and easy to use and save datasets: https://roboflow.com/annotate
The dataset need to be as versatile as you can. For CoralDX, we utilized 40 pictures in images folder to train. And after annotating, the Roboflow will give a corresponding .txt file with the coordinates of your selected target areas.

1. Get started

2. Create new project (name it)

3. Annotate (group and name)

To annotate, use the second square tool in right white bar to square the target area, then group and name every target areas.
NOTE: Annotations are CASE SENSITIVE, so label all images used for training a model with the exact same labels.
Annotation sample:eed6415d98702e9ff0b3778f4e7b269

4. Assign

Assign images into train and valid datasets which are for training and validing the custom detector in 80%:20% ratio.
f0a4936550ebb131a5cf985d230dd0c

5. Generate dataset

To generate dataset, in preprocessing section. We resized images in 416* 416 which can accelerate the training before downing annotated dataset:
32fff1758cb5304017ab60be2cb7dec

For Augmentation, press Continue.
While generating dataset, your screen will look like this:
generating loading screen

6. Export and Download

After generating the dataset, click on the 'Export' option to export and download your dataset.
export option
In the pop-up dialog box, select 'YOLO Darknet' format and 'Download zip to computer' option.
export settings

Download the zipped dataset includes all images and related .txt files like shown in images folder: image

Train custom detector in Google Colab

Train Custom Model In Colab Before start the journey, make a copy of this Colab file.

  1. Enabling GPU within your notebook
    d0d318308b8a04ba3a94d1ec327a3c7
  2. Cloning and Building Darknet
    258e8e97c2ab3f8861b6bc6730b8291
  3. Download pre-trained YOLOv4 weights
    95b5950256875de9b8c8237f77b7194
  4. Define Helper Functions
    201a368b6de66408c48dcc2d70825ad
  5. Run Your Detections with Darknet and YOLOv4!
    9366cf94f1b88e48147cb28657518ca
  6. Uploading Local or Google Drive Files to Use
    We recommend to create a Google Drive Folder called yolov4 and put enerything into Google Drive for use
    6151825886ad39a3a628dc2449877fc
    The follwing list is the files need to upload into the Googlr Drive
    df1eb079741e20d8bb341fbc6a7d2cf
    Copy of YOLOv4.ipynb: copy of this Colab tutorial file
    images: images for test custom detector
    backup: create empty folder to store weights file
    obj.zip: change name of train folder to obj and compress
    test.zip: change name of valid folder to test and compress
    13395d487ed5d8a7a537eb924252d64
    yolov4-obj.cfg: configuration file
    1ebdf19dc1b69652f6bc676f18fff58
    obj.names: group names
    obj.data: directions of files
    Put group names in obj.names file and change the classes number for custom detector. Both file can be editedd from example files using Text Editor in cfg section.
    3cd74b5b0b69b4fcd2297129bdbbed5
    generate_train.py: configuration files to train our custom detector are the train.txt training images
    generate_test.py: configuration files to train our custom detector are the test.txt testing images
    b58c4c568501705e3c31a3fa9d3d08d classes.txt: group names
    Edit example file using Text Editor and put group names in.
  7. Start training
    image

Deploy .weights file into the Tensorflow

Download Tensorflow folder to local drive. We recommend to use Gitbash shell to deliver command and visual studio code as editor.

  1. Set up Conda environment
    We recommend to download Anaconda to set up tensorflow environment. Then deliver command in Gitbash shell to create and activate GPU or CPU.

Tensorflow CPU
conda env create -f conda-cpu.yml
conda activate yolov4-cpu

Tensorflow GPU
conda env create -f conda-gpu.yml
conda activate yolov4-gpu

  1. Download 'yolov4-obj_best.weights' file from backup folder.

  2. Use custom trained detector

Copy and paste your custom .weights file into the 'data' folder and copy and paste your custom .names into the 'data/classes/' folder.

The only change within the code you need to make in order for your custom model to work is on line 14 of 'core/config.py' file. Update the code to point at your custom .names file as seen below. (my custom .names file is called custom.names but yours might be named differently)
image

  1. Convert yolov4 detector to Tensorflow detector
    python save_model.py --weights ./data/yolov4.weights --output ./checkpoints/yolov4-416 --input_size 416 --model yolov4
    Paste this command into Gitbash.

  2. Crop and save target areas as new images
    python detect.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --images ./data/images/'your image name'.jpg --crop
    Imput this command into Gitbash, make sure replace'your image name' to your image name.

  3. Measure RGB values in Matlab.
    Use .m MATLAB file to measure cropped images, make sure to use correct directory.
    image

  4. Test TestStripDX.
    Predict, predict and crop images.
    3c7df9efa8480c71d55df8defe897db
    Note: The showing labels are not related to the actual reagents, but the showing labels are exact same for each images. So, we correct this in jupyter file. We will retrain the model to try to correct this error.
    Measure RGB values.
    The last step is to run the TeststripsDX file in Jupyter Note. Then, it will create an excel file. Please check the TeststripsDX folder for details including the code, result file, and testing dataset.
    8858ed4a3c1c72014144248a3c119ac