Detecting, segmenting, and classifying materials inside vessels in images using a fully convolutional neural net, for chemistry laboratory and general setting.
Neural net that given an image, detects and segments and classifies the vessels (mainly transparent vessels) and the materials inside the vessels in the image (Figure 1). The net marks the vessel region, the filled region inside the vessel, and the specific region of various a phase of materials such as liquid, solid, foam, suspension, powder, granular... In addition, the net also predicts the region of the vessel labels cork and other parts (such as valves in separatory funnels). Note this is a semantic segmentation net based on PSP net.
See Paper Computer Vision for Recognition of Materials and Vessels in Chemistry Lab Settings and the Vector-LabPics Dataset for more details on the methods and dataset.
The net focus on detecting vessels and their content materials in images. The focus is on both chemistry lab setting and general everyday setting (beverage, kitchen..) but should work in any conditions or setting. The net should recognize any transparent vessel (bottle/glass /or lab vessel) and their content and some none transparent vessels in any general environment and setting. The accuracy of the net is relatively high in detecting and classifying vessels, filled regions, liquid regions, and solid regions. The classification accuracy for fine-grained material classes such as foams, powder, gels, etc., is lower. If you encounter cases on which the net performs badly, please send me the images so I can use them to improve the network.
Figure 1) Input images and output results of the net. Images taken from the NileRed youtube channel.
Input and output of the net
The input for the net is a standard image (Figure 1 right). The net output of the region of the vessel/fill level and other materials phases, and vessel parts in the image (Figure 1 left). For each class, the net will output a mask mask region of the image corresponding to this class in the image (Figure 1 left).
For using the trained net, no specific hardware is needed, but the net will run much faster on Nvidia GPU.
For training the net an Nvidia GPU is needed (the net was trained on Titan XP, and also on RTX 2070 with similar results)
Setup for running prediction
- Install Anaconda
- Install Pytorch
- Install OpenCV
- Download the code with trained model weight from [Here]
Running inference on image and predicting segment mask
- Download the code with trained model weight from here or here. or train the model yourself using the instructions of the Training section.
- Open the RunPredictionOnFolder.py script.
- Set the path to the folder where the images are stored to the: InputDir parameter (all the images in the input folder should be in .jpg or .png format)
- Set the output folder where the output will be stored to the: OutDir parameter.
- Run script.
- Output: predicted region for each input image and class would appear in the OutDir folder.
Note: RunPredictionOnFolder.py should run out of the box (as is) using the sample images and trained model provided.
- If you train the net yourself, set the path to your trained model in the Trained_model_path parameter
- If you have a Nvidia GPU and Cuda installed, set the UseGPU parameter to True (this will allow the net to achieve a much faster running time).
- Changing FreezeBatchNormStatistics parameter from False to True might change the segmentation quality for better or worst (and so does changing the image size)
Additional Running scripts:
RunPredictionOnVideo.py script: receive an Input video in InputVideo apply prediction overlay the prediction on the image and save it to video files.
RunPredictionWebCam.py script: Take image from web come run prediction overlay the prediction on the image and display on screen
There are two training options: one is to train using only with LabPics dataset, this is faster, simpler. The second training option is to use a combination of the LabPics dataset and Vessels classes from the COCO panoptic dataset (Such as bottles/glasses/jars..). This option is more complex to train and gives lower accuracy on the test set but gives a more robust net that work under a wider set of conditions.
Training simple (only LabPics)
- Download the LabPics data set from Here or here
- Open the Train.py script
- Set the path to the LabPics dataset main folder to the TrainFolderPath parameter.
- Run the script
- Output trained model will appear in the /log subfolder or any folder set in Trained model Path
Training second option (With LabPics dataset and Vessels from the COCO panoptic dataset)
Download the COCO panoptic dataset annotation and train images.
Converting COCO dataset into training data
- Open script TrainingDataGenerationCOCO/RunDataGeneration.py
- Set the COCO dataset image folder to the ImageDir parameter.
- Set the COCO panoptic annotation folder to the AnnotationDir parameter.
- Set the COCO panoptic .json file to the DataFile parameter.
- Set the output folder (where the generated data will be saved) to the OutDir parameter.
- Run script.
- Open the COCO_Train.py script
- Set the path to the LabPics dataset main folder to the LabPicsTrainFolderPath parameters.
- Set the path to the COCO generated data (OutDir, step 7) to the COCO_TrainDir paramter.
- Run the script
- Output trained model will appear in the /log_COCO subfolder or any folder set in Trained model Path
Code file structure
RunPredictionOnFolder.py: Run prediction on image using pre-trained image
Train.py: Training the net of the LabPics dataset
ChemReader.py: File reader for the LabPics dataset (used by the Train.py script)
FCN_NetModel.py: The class containing the neural net model.
Evaluator.py: Evaluate the net performance during training (Used by Train.py)
CategoryDictionary.py: List of classes and subclasses used by the net and LabPics dataset.
Logs folder: Folder where the trained models and training logs are stored.
InputImages Folder: Example input images for the net.
For second training mode (with COCO)
COCO_TRAIN.py: Training script for second training mode (with COCO).
CocoReader.py: Reader for the converted COCO data.
TrainingDataGenerationCOCO folder: Convert COCO dataset for training data.
Results on videos
Results on of the nets on videos can be seen here: https://www.youtube.com/playlist?list=PLRiTwBVzSM3B6MirlFl6fW0YQR4TtQmtJ
Train model for this net can be download from here.
The images for the LabPics dataset were supplied by the following sources Nessa Carson (@SuperScienceGrl Twitter), Chemical and Engineering Science chemistry in pictures, YouTube channels dedicated to chemistry experiments: NurdRage, NileRed, DougsLab, ChemPlayer, and Koen2All. Additional sources for images include Instagram channels chemistrylover_(Joana Kulizic),Chemistry.shz (Dr.Shakerizadeh-shirazi), MinistryOfChemistry, Chemistry And Me, ChemistryLifeStyle, vacuum_distillation, and Organic_Chemistry_Lab
Work was done in the Matter Lab (Alan Aspuru Guzik group) and the Vector institute Toronto.
The LabPics dataset was made by Mor Bismuth.