centernet trains its own data set ubuntu16.04, pytorch1.1.0, cuda10.0


Centernet github address: https://github.com/xingyizhou/CenterNet

1. Environment configuration

1.1 torch installation

conda create -n centernet_2020 python=3.7
source activate centernet_2020
conda install pytorch=1.1 torchvision cudatoolkit=10.0 -c pytorch

Sometimes conda download fails or is very slow, you can try the following command, change the version number or remove torchvision without installation:

pip install torch==1.0.0 torchvision==0.2.1 -i https://mirror.baidu.com/pypi/simple

1.2 Dependent installation

cd CenterNet-master
pip install -r requirements.txt

1.3 COCOAPI installation

git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
python setup.py install --user

1.4 DCNV2 compilation

cd $CenterNet_ROOT/src/lib/models/networks/DCNv2
./make.sh

Error:

Traceback (most recent call last):
  File "build.py", line 3, in <module>
    from torch.utils.ffi import create_extension
  File "/data_1/Anaconda1105/envs/centernet_2020/lib/python3.7/site-packages/torch/utils/ffi/__init__.py", line 1, in <module>
    raise ImportError("torch.utils.ffi is deprecated. Please use cpp extensions instead.")
ImportError: torch.utils.ffi is deprecated. Please use cpp extensions instead.
Traceback (most recent call last):
  File "build_double.py", line 3, in <module>
    from torch.utils.ffi import create_extension
  File "/data_1/Anaconda1105/envs/centernet_2020/lib/python3.7/site-packages/torch/utils/ffi/__init__.py", line 1, in <module>
    raise ImportError("torch.utils.ffi is deprecated. Please use cpp extensions instead.")
ImportError: torch.utils.ffi is deprecated. Please use cpp extensions instead.

Because pytorch1.1torch.utils.ffi has been deprecated.
Download the latest DCNV2
in the directory ./CenterNet-master/src/lib/models/

mv DCNv2 DCNv2-src
git clone https://github.com/CharlesShang/DCNv2
cd DCNv2
./make.sh

After the compilation is successful, the bottom will print
Processing dependencies for DCNv20.1
Finished processing dependencies for DCNv20.1
Since nms is not required, there is no need to compile external
(it’s a bit fun here, the text will be colored between the two groups of ==)

2. Data preparation:

2.1 Introduction to voc data folder

My data is in voc format and needs to be converted to coco format
Annotations
#stored is marked xml JPEGImages #stored is pictures
The number of files under the two folders needs to be the same!

2.2 voc2coco_2020.py script


To convert to voc, you only need to convert the xml in Annotations into a file in coco format json to open the voc2coco_2020.py script, the bottom line, you only need to modify the xml_path to your own folder path

The voc2coco_2020.py script link is as follows:

https://blog.csdn.net/yang332233/article/details/97205112
After running, a train.json file will be generated under the current directory. It cannot be opened because it is very large. But we need to open to view category information,
because category information is recorded under this json.
Method 1: It is to display cat train.json directly on the terminal, if the category information is at the end, you can see it, if it is not at the end, you need to view 2.


Method 2: Install a jq plug-in to open. Specific reference: https://blog.csdn.net/yang332233/article/details/97205120?ops_request_misc=%7B%22request%5Fid%22%3A%22160515033819725222412279%22%2C%22scm%22%3A%2220140713.130102334.pc% . 7D 5Fblog%% & 22 is the request_id = 160515033819725222412279 & biz_id = 0 = & utm_medium distribute.pc_search_result.none Task-Blog-2- blogfirst_rank_v1 ~ rank_blog_v1-2-97205120.pc_v1_rank_blog_v1 & utm_term = Coco & SPM = 1018.2118.3001.4450
perform
cat train.json | jq> train_jq.json.
then You can directly open train_jq.json, drag to the bottom to view the categories and tags:
"categories": [
{
"supercategory": "none",
"id": 1,
"name": "car"
},
{
"supercategory"

"name": "bird"
},
{
"supercategory": "none",
"id": 3,
"name": "dog"
}
. . .
This needs to be filled in the code. You need to pay attention here, because there is also a test file. Generally, the above process is installed to generate test.json or val.json. There is a problem here is that this script generates json based on xml, and a new one is encountered The category is added to the back, a lot of xml in the test, according to the current folder xml. So the generated categories may be different from the order in train.json!
I simply copied and renamed train.json to test.json or val.json. Then when running training, let him not test (see 3.4 for untested modifications), because we have our own test data set and test scripts for offline testing.

3. Train your own data code modification

3.1 Data storage

When we generate the json file, we come to the CenterNet project, create a new folder under the CenterNet-master/data folder, the name is the name of your data set (MyDataTest)
and then create two folders in this folder ( The annotations store the json file we generated before; the name is arbitrary, such as train.json, test.json, val.json, the following code will be changed (see 3.2.7), images store all the pictures, including training The test verifies three, all)

3.2 Own data class---copy coco.py to modify

1. In the CenterNet-master/src/lib/datasets/dataset/ folder, copy coco.py and
open my_test.py from the name my_test.py to modify:
3.2.1 line13: class COCO is modified to class my_test
3.2.2 line14: num_classes = 13 #Note that the background class is not included.
3.2.3 line15: default_resolution = [512, 512] Modify the training image size you need
3.2.4 line16, 18: Change the mean variance to your own, or you don’t need to change
3.2 .5 Line22: super(COCO, self). Replace COCO in init () with your own class name my_test
3.2.6 Line23, 24: modify your own data path

self.data_dir = os.path.join(opt.data_dir, 'coco')
self.img_dir = os.path.join(self.data_dir, '{}2017'.format(split))

Change the name of your own data folder as follows:

self.data_dir = os.path.join(opt.data_dir, 'MyDataTest')
self.img_dir = os.path.join(self.data_dir, 'images')

3.2.7 line26-37: modify your own json file name:

if split == 'test':
      self.annot_path = os.path.join(
          self.data_dir, 'annotations', 
          'test.json').format(split)
    else:
      if opt.task == 'exdet':
        self.annot_path = os.path.join(
          self.data_dir, 'annotations', 
          'train.json').format(split)
      else:
        self.annot_path = os.path.join(
          self.data_dir, 'annotations', 
          'train.json').format(split)

3.2.8 line39: Change the category name and category id to your own

self.class_name = [
      '__background__', 'class_1', 'class_2', 'class_3', 'class_4', 'class_5',
      'class_6', 'class_7', 'class_8', 'class_9', 'class_10', 'class_11',
      'class_12', 'class_13']

self._valid_ids = [
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12, 13]

3.3 dataset_factory.py modification

Add the data set to CenterNet-master/src/lib/datasets/dataset_factory.py
Line14 Add: from .dataset.my_test import my_test
Line29 Add:'my_test': my_test
format is'the name of the Python file you created before': yourself The name of the class (data set)

3.4 /src/lib/opts.py modification

3.4.1 Add your own data set

self.parser.add_argument('--dataset', default='coco',
                             help='coco | kitti | coco_hp | pascal')

changed to:

self.parser.add_argument('--dataset', default='my_test',
                             help='coco | kitti | coco_hp | pascal |my_test')

3.4.2 line336: Modify the default data set used by the ctdet task as the newly added data set, as follows (modify the resolution, number of categories, mean, variance, data set name):

'ctdet': {'default_resolution': [512, 512], 'num_classes': 37, 
                'mean': [0.408, 0.447, 0.470], 'std': [0.289, 0.274, 0.278],
                'dataset': 'objvehicle_small'},

3.4.3 Several important parameters are available for selection and modification
In addition, there are several important parameters for selection and modification in opts:

'--print_iter', type=int, default=0,  #默认0,可以给出数字每隔多少打印

('--val_intervals', type=int, default=5, #这里默认5个epoch测试,不想测试的话调500000
                             help='number of epochs to run validation.')

Choice of backbone network:

self.parser.add_argument('--arch', default='dla_34', 
                         help='model architecture. Currently tested'
                              'res_18 | res_101 | resdcn_18 | resdcn_101 |'
                              'dlav0_34 | dla_34 | hourglass')

Note that some of the backbone networks selected here will encounter the problem of failure to download the pre-training model. The reason is that the author has not written it well and needs to change the hash.

3.5 CenterNet-master/src/lib/utils/debugger.py modification

Line 458 adds:

my_test_class_name = [
      'cheliang', 'chewei', 'chelian', 'dibiao_20', 'sanjiaojia',
      'qizhibiaozhi', 'motorbike', 'dibiao_0', 'dibiao_qd', 'xiaochebiaozhipai', 'tingchebiaozhipai',
      'fanguangbeixin', 'dibiao_10'
]

Line45 adds:

elif num_classes == 80 or dataset == 'coco':
      self.names = coco_class_name
    elif num_classes == 13 or dataset == 'my_test':
      self.names = my_test_class_name
    elif num_classes == 20 or dataset == 'pascal':
      self.names = pascal_class_name

4. Training command python main.py ctdet --exp_id my_test --batch_size 4 --lr 0.001 --gpus 1 --num_workers 8

cd CenterNet-master/src
python main.py ctdet --exp_id my_test --batch_size 4 --lr 0.001  --gpus 1 --num_workers 8

Continue the last training and add --resume directly:

python main.py ctdet --exp_id my_test --batch_size 4 --lr 0.001  --gpus 1 --num_workers 8 --resume

5. Run forward code --- test or view the effect

https://blog.csdn.net/yang332233/article/details/109007342 is
the code at the bottom.

6. Run error troubleshooting

6.1
CenterNet-master/src/lib/models/networks/DCNv2/dcn_v2.py
ModuleNotFoundError: No module named'_ext'
Solution: Use it
in the directory [DCNv2] file

python setup.py install develop

6.2 Solve the problem that the filename in xml is different from the file name,
xml_change_filename.py can solve it. (I may encounter problems on my own side)

Guess you like

Origin blog.csdn.net/yang332233/article/details/109713327