1.訓練文件的配置
將生成的csv和record文件都放在新建的mydata文件夾下,並打開object_detection文件夾下的data文件夾,復制一個后綴為.pbtxt的文件到mtdata文件夾下,並重命名為gaoyue.pbtxt
用記事本打開該文件,因為我只分了一類,所以將其他內容刪除,只剩下這一個類別,並將name改為gaoyue。
這時我們擁有的所有文件如下圖所示。
我們在object_detection文件夾下新建一個training文件夾,在里面新建一個記事本文件並命名為 ssd_mobilenet_v1_coco.config
打開,輸入以下代碼,按右邊注釋進行修改
# SSD with Mobilenet v1 configuration for MSCOCO Dataset. # Users should configure the fine_tune_checkpoint field in the train config as # well as the label_map_path and input_path fields in the train_input_reader and # eval_input_reader. Search for "PATH_TO_BE_CONFIGURED" to find the fields that # should be configured. model { ssd { num_classes: 1 # 你類別的數量,我這里只分了一類 box_coder { faster_rcnn_box_coder { y_scale: 10.0 x_scale: 10.0 height_scale: 5.0 width_scale: 5.0 } } matcher { argmax_matcher { matched_threshold: 0.5 unmatched_threshold: 0.5 ignore_thresholds: false negatives_lower_than_unmatched: true force_match_for_each_row: true } } similarity_calculator { iou_similarity { } } anchor_generator { ssd_anchor_generator { num_layers: 6 min_scale: 0.2 max_scale: 0.95 aspect_ratios: 1.0 aspect_ratios: 2.0 aspect_ratios: 0.5 aspect_ratios: 3.0 aspect_ratios: 0.3333 } } image_resizer { fixed_shape_resizer { height: 300 width: 300 } } box_predictor { convolutional_box_predictor { min_depth: 0 max_depth: 0 num_layers_before_predictor: 0 use_dropout: false dropout_keep_probability: 0.8 kernel_size: 1 box_code_size: 4 apply_sigmoid_to_scores: false conv_hyperparams { activation: RELU_6, regularizer { l2_regularizer { weight: 0.00004 } } initializer { truncated_normal_initializer { stddev: 0.03 mean: 0.0 } } batch_norm { train: true, scale: true, center: true, decay: 0.9997, epsilon: 0.001, } } } } feature_extractor { type: 'ssd_mobilenet_v1' min_depth: 16 depth_multiplier: 1.0 conv_hyperparams { activation: RELU_6, regularizer { l2_regularizer { weight: 0.00004 } } initializer { truncated_normal_initializer { stddev: 0.03 mean: 0.0 } } batch_norm { train: true, scale: true, center: true, decay: 0.9997, epsilon: 0.001, } } } loss { classification_loss { weighted_sigmoid { } } localization_loss { weighted_smooth_l1 { } } hard_example_miner { num_hard_examples: 3000 iou_threshold: 0.99 loss_type: CLASSIFICATION max_negatives_per_positive: 3 min_negatives_per_image: 0 } classification_weight: 1.0 localization_weight: 1.0 } normalize_loss_by_num_matches: true post_processing { batch_non_max_suppression { score_threshold: 1e-8 iou_threshold: 0.6 max_detections_per_class: 100 max_total_detections: 100 } score_converter: SIGMOID } } } train_config: { batch_size: 16 # 電腦好的話可以調高點,我電腦比較渣就調成16了 optimizer { rms_prop_optimizer: { learning_rate: { exponential_decay_learning_rate { initial_learning_rate: 0.004 decay_steps: 800720 decay_factor: 0.95 } } momentum_optimizer_value: 0.9 decay: 0.9 epsilon: 1.0 } } # Note: The below line limits the training process to 200K steps, which we # empirically found to be sufficient enough to train the pets dataset. This # effectively bypasses the learning rate schedule (the learning rate will # never decay). Remove the below line to train indefinitely. num_steps: 200000 # 訓練的steps data_augmentation_options { random_horizontal_flip { } } data_augmentation_options { ssd_random_crop { } } } train_input_reader: { tf_record_input_reader { input_path: "mydata/gaoyue_train.record" # 訓練的tfrrecord文件路徑 } label_map_path: "mydata/gaoyue.pbtxt" } eval_config: { num_examples: 8000 # 驗證集的數量 # Note: The below line limits the evaluation process to 10 evaluations. # Remove the below line to evaluate indefinitely. max_evals: 10 } eval_input_reader: { tf_record_input_reader { input_path: "mydata/gaoyue_test.record" # 驗證的tfrrecord文件路徑 } label_map_path: "mydata/gaoyue.pbtxt" shuffle: false num_readers: 1 }
新建后的文件顯示如下。
這時,我們訓練的准備工作就做好了。
2.訓練模型
在object_detection文件夾下打開Anaconda Prompt,輸入命令
python model_main.py --pipeline_config_path=training/ssd_mobilenet_v1_coco.config --model_dir=training --alsologtostderr
在訓練過程中如果出現no model named pycocotools的問題的話,請參考這個網址(http://www.mamicode.com/info-detail-2660241.html)解決。親測有效
即:
(1)從https://github.com/pdollar/coco.git 下載源碼,解壓至全英文路徑下。
(2)使用cmd進入解壓后的cocoapi-master/PythonAPI路徑下,輸入python setup.py build_ext --inplace。如果這一步有報錯,請打開set_up.py文件,將其中這兩個參數刪除。
即:
(3)上一步執行沒問題之后,繼續在cmd窗口運行命令:python setup.py build_ext install
訓練完成后,training文件夾下是這樣的情況
(如果想觀察訓練過程中參數的變化以及網絡的話,可以打開新的一個Anaconda Prompt cd到object_detection文件夾下
輸入命令:tensorboard --logdir=training),復制出現的網址即可。如圖所示\
如果顯示不出來的話,新建網頁在地址欄輸入http://localhost:6006/(后面的6006是我的端口號,根據你自己的輸入)
3.生成模型
定位到object_detection目錄下,打開Anaconda Promp輸入命令
python export_inference_graph.py \ --input_type image_tensor \ --pipeline_config_path training/ssd_mobilenet_v1_coco.config \ --trained_checkpoint_prefix training/model.ckpt-500 \ --output_directory gaoyue_detection
(注意這兩處標紅的地方,1. model.ckpt-500是指你訓練的輪數的文件,這里因為我只訓練了500輪,所以改成了500(如下圖中的500)
2. output_directory是輸出模型的路徑,最好是新建一個文件夾來存放模型,我新建了一個名為gaoyue_detection的模型)
命令執行完成后,打開gaoyue_detection文件夾,里面的內容如圖所示
表示執行成功,這樣,我們用自己數據集訓練的目標檢測模型就做好了
下一節會詳細說我們自己模型的驗證