QR code
The project uses unsupervised learning to detect anomalous QR codes and numbers.
In this application, we use the PatchCore algorithms to learn the good-only dataset. Then we use the K-Nearest Neighbors (KNN) algorithms to separate the newly seen good and ng data.
1. Prepare your dataset
# your dataset structure should be like this
data/
-train/
-good/
-*.jpg
-test/
-good/
-*.jpg
-ng/
-*.jpg
Notes
The number of train data should be > 10 items. If it is <10 items, please rotated the images to create more.
2. Usages
Training
Go to /projects/tsk_qrcode
Execute this command:
!python train.py --model_type patchcore --data_dir "<PATH_TO_TRAIN_FOLDER>" \
--generate_dataset "<NUMBER_OF_COPIES>" --split_ratio "<RATIO>"
NUMBER_OF_COPIES
: Input number of copies per image. Auto augment upto specified number of copy then generate dataset. Example: 1,2,3,4. if generate_dataset = 0, generate dataset is off.RATIO
: Input ratio (ranging from 0-1). Get random images from training dataset to generate validation dataset ifvalid
directory does not exist yet.
Note
PATH_TO_TRAIN_FOLDER
should be one-level above the image files, i.e. ../data/train
, not ../data/train/good
The model will be saved into results
folder.
Testing
Go to /projects/tsk_qrcode
Execute this command:
python predict.py --model_type patchcore --data_dir "<PATH_TO_TEST_FOLDER>" \
--threshold "<THRESHOLD_IMG_SCORE>" \
--is_pixel_score_used "<USE_THRESHOLD_PIXEL>" --threshold_pixel_score "<THRESHOLD_PIXEL_SCORE>"
PATH_TO_TEST_FOLDER
: should be where the image files areTHRESHOLD_IMG_SCORE
: Threshold value to define the result asgood
orNG
. DefaultNone
, IfNone
is used, the model automatically gets a threshold score from the training.USE_THRESHOLD_PIXEL
: use extra pixel_score valueTHRESHOLD_PIXEL_SCORE
:Threshold pixel value to define the result asgood
orNG
. Default:None
. IfNone
is used, the model automatically gets a threshold pixel score from the training.
Note
PATH_TO_TEST_FOLDER
should be where the image files are, i.e. ../data/test/good
Result images will be save into results
folder.
3. Improve the performance
If the prediction is not good, please consider either tuning the threshold or adding more data