Cài đặt Tensorflow trên hệ điều hành Window 10

Hướng dẫn các bước cài đặt tensorflow trên window 10

----------------------------------------------------

Bước 1: Set up new Anaconda virtual environment
- Dowload and install Anaconda3-2021.05-Windows-x86_64.exe

Bước 2: Set up new Anaconda virtual environment
Open Command Prompt (On Start menu, click the Windows icon, then tape Com then click on Command Prompt app)
C:\> conda create -n tf-cpu pip python=3.6
C:\> conda activate tf-cpu

Bước 3: Install Tensorflow
(tf-cpu) C:\> conda install tensorflow=1.15
(tf-cpu) C:\> conda install -c anaconda protobuf
(tf-cpu) C:\> pip install tensorflow-estimator==1.15.0
(tf-cpu) C:\> pip install pillow lxml Cython contextlib2 jupyter matplotlib scikit-image pandas opencv-python apache-beam avro-python3 lvis pycocotools

Bước 4: Install model
- Extract to C:\tensorflow1\models
- Open file C:\tensorflow1\models\research\object_detection\utils\learning_schedules.py lines 167-169. Currently it is

rate_index = tf.reduce_max(tf.where(tf.greater_equal(global_step, boundaries),
                                      range(num_boundaries),
                                      [0] * num_boundaries))

- Wrap list() around the range() like this:

rate_index = tf.reduce_max(tf.where(tf.greater_equal(global_step, boundaries),
                                     list(range(num_boundaries)),
                                      [0] * num_boundaries))

- Download model_master at link https://github.com/tensorflow/models
- Then copy file: tf_record_creation_util.py to C:\tensorflow1\models\research\object_detection\dataset_tools

(tensorflow1) C:\tensorflow1\models\research> python setup.py build
(tensorflow1) C:\tensorflow1\models\research> python setup.py install

Bước 5: Export environment
(tf-cpu) C:\> set PYTHONPATH=C:\tensorflow1\models;C:\tensorflow1\models\research;C:\tensorflow1\models\research\slim
(tf-cpu) C:\tensorflow1\models\research> protoc object_detection/protos/*.proto --python_out=.

Bước 6: Install OpenCV and Mask_RCNN
- Download Mask RCNN  from link: https://github.com/akTwelve/Mask_RCNN
- Extract file  Mask_RCNN-master.zip to D:\OpenCV\

Bước 7: Install VIA
- Download file via-2.0.11.zip from link: https://www.robots.ox.ac.uk/~vgg/software/via/
- Extract file  via-2.0.11.zip tD:\OpenCV\aktwelve_mask_rcnn
- Copy all images for labeling to train & val folder
- Open file  D:\OpenCV\aktwelve_mask_rcnn/via.html to open tool label mask object (Only use type of Using Polyline for labeling)

Note: Size of image must be larger or equal 80 pixels

- After label all images in two folder train & val, do export annotations json file then convert json to mask and xml file by command:
(tf-cpu) D:\OpenCV\aktwelve_mask_rcnn\samples\balloon> python convert_vgg_for_tensorflow_detect_hexa_digits_bd.py --dataset=D:\OpenCV\aktwelve_mask_rcnn\datasets\myTrain

Bước 8: Train & Test model
- Create folder: C:\tensorflow1\models\research\object_detection\data_detect_model
- Create folder: C:\tensorflow1\models\research\object_detection\data_detect_mode\AnnotationsTrain
- Create folder: C:\tensorflow1\models\research\object_detection\data_detect_mode\AnnotationsVal
- Create folder: C:\tensorflow1\models\research\object_detection\data_detect_mode\JPEGImagesTrain
- Create folder: C:\tensorflow1\models\research\object_detection\data_detect_mode\JPEGImagesVal
- Copy all files from D:\OpenCV\aktwelve_mask_rcnn\datasets\myTrain\train\Annotations\ to C:\tensorflow1\models\research\object_detection\data_detect_mode\AnnotationsTrain
- Copy all images from D:\OpenCV\aktwelve_mask_rcnn\datasets\myTrain\train\ to C:\tensorflow1\models\research\object_detection\data_detect_mode\JPEGImagesTrain
- Copy all files from D:\OpenCV\aktwelve_mask_rcnn\datasets\myTrain\val\Annotations to C:\tensorflow1\models\research\object_detection\data_detect_mode\AnnotationsVal
- Copy all images from D:\OpenCV\aktwelve_mask_rcnn\datasets\myTrain\val\ to C:\tensorflow1\models\research\object_detection\data_detect_mode\JPEGImagesVal
- Copy and edit files: create_mask_rcnn_tf_record.py, label.pbtxt, mask_rcnn_inception_v2_coco.config into folder C:\tensorflow1\models\research\object_detection\data_detect_mode\

- Create train.record:
(tf-cpu) C:\tensorflow1\models\research\object_detection\data_detect_model> python create_mask_rcnn_tf_record.py --image_dir=C:\tensorflow1\models\research\object_detection\data_detect_model\JPEGImagesTrain --annotations_dir=C:\tensorflow1\models\research\object_detection\data_detect_model\AnnotationsTrain --label_map_path=C:\tensorflow1\models\research\object_detection\data_detect_model\label.pbtxt --output_dir=C:\tensorflow1\models\research\object_detection\data_detect_model\train.record

- Create test.record:
(tf-cpu) C:\tensorflow1\models\research\object_detection\data_detect_model> python create_mask_rcnn_tf_record.py --image_dir=C:\tensorflow1\models\research\object_detection\data_detect_model\JPEGImagesVal --annotations_dir=C:\tensorflow1\models\research\object_detection\data_detect_model\AnnotationsVal --label_map_path=C:\tensorflow1\models\research\object_detection\data_detect_model\label.pbtxt --output_dir=C:\tensorflow1\models\research\object_detection\data_detect_model\test.record

- Train model:
(tf-cpu) C:\tensorflow1\models\research\object_detection\data_detect_model> python train.py --logtostderr --train_dir=C:\tensorflow1\models\research\object_detection\data_detect_model\trainingRecord --pipeline_config_path=C:\tensorflow1\models\research\object_detection\data_detect_model\mask_rcnn_inception_v2_coco.config

- Export frozen_inference_graph.pb file:
(tf-cpu) C:\tensorflow1\models\research\object_detection\data_detect_model> python export_inference_graph.py --input_type image_tensor --pipeline_config_path data_detect_model\mask_rcnn_inception_v2_coco.config --trained_checkpoint_prefix data_detect_model\trainingRecord\model.ckpt-xxxxx --output_directory data_detect_model\inference_graph

- Test model:
(tf-cpu) D:\OpenCV\TestDNN\myTrain> python mask_rcnn.py --mask-rcnn mask-rcnn-coco --image 0f.jpg --confidence 0.85

- Result: 0: 0.9997, 19, 14, 30, 54; f: 0.9900, 65, 14, 37, 52

(Please contact us to get more scripts python, dataset images and frozen_inference_graph.pb file)

---------- The end ----------

Thank you!