This repository implements a computer vision based coin counter with deep learning method. Use the detection model to predict the position of all coins in the image, and then use the classification model to classify all coins. According to the amount represented by different coins, the total amount of all coins is automatically calculated.
- Python 3.7.11 conda environment
- PyTorch 1.8.2 LTS with CUDA 11.1
git clone https://github.com/ChienHsuan/coin-counter.git
cd coin-counter
pip install -r requirements.txt
Collect the New Taiwan Dollar (NT$) coin dataset, and use LabelImg to label the dataset. In this repository, there are totally 300 images in the dataset, including 260 training set images, 20 validation set images, and 20 test set images. The sample image of the dataset is shown below. Moreover, arrange the image paths as follows:
/USER-DEFINED-PATH/coins/
├── Train/
│ ├── coins_0.jpg
│ ├── coins_1.jpg
│ ├── ...
├── Validation/...
└── Test/...
Download the ImageNet pre-trained weights of MobileNetV2 and ResNet-50 models from this link. In addition, arrange the model paths as follows:
coin-counter/pre-trained_models/
├── mb2-imagenet-71_8.pth
├── mobilenet_v2-b0353104.pth
└── resnet50-19c8e357.pth
All training procedures include detection model training and classification model training. Use MobileNetV2 SSDLite as detection model. MobileNetV2 and ResNet-50 are used as classification models.
Use the MobileNetV2 as backbone, and couple the backbone with the SSDLite detection network. It is turned into the MobileNetV2 SSDLite detection model, and acts as a coin detector. To train the detection model, execute the command:
bash train.sh
The path to the trained MobileNetV2 SSDLite detection model is as follows:
/coin-counter/trained-models/
└── mb2_ssdlite-Epoch-{epoch}-Loss-{val_loss}.pth
Use MobileNetV2 and ResNet-50 classification models as coin classifier. To train a classification model, execute the command:
python classification.py
The paths for the trained MobileNetV2 and ResNet-50 classification models are as follows:
/coin-counter/trained-models/
├── resnet50.pth
└── mobilenetv2.pth
Change the dataset and model related arguments in training scripts as required.
Use the evaluation metric Average Precision (AP) to evaluate the prediction accuracy of the detection model. Use classification accuracy to evaluate the coin classification accuracy of the classification model.
bash eval.sh
Change the dataset and model related arguments in test scripts as required.
The following are the experiment results of the MobileNetV2 SSDLite detection model.
Model | AP[IoU=0.5] | AP[IoU=0.7] | AP[IoU=0.9] |
---|---|---|---|
MobileNetV2 SSDLite | 1.0 | 1.0 | 0.736 |
The following are the experiment results of the MobileNetV2 and ResNet-50 classification models.
coin type | accuracy |
---|---|
coin 1 | 100% |
coin 5 | 100% |
coin 10 | 100% |
coin 50 | 100% |
coin type | accuracy |
---|---|
coin 1 | 100% |
coin 5 | 100% |
coin 10 | 100% |
coin 50 | 100% |
The following are the coin recognition results of the test set images.
See the documentation in docs/
for more details.
The MIT License (MIT)
Copyright (c) 2022 Chien-Hsuan Yen
Single Shot MultiBox Detector Implementation in Pytorch
LabelImg