Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure the PyTorch environment

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to configure the PyTorch environment". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Environment configuration

Environmental requirements:

Python > = 3.5pytorch==0.4.1or 1.1.0 or 1.0.0 (1.0.0 used by the author is also possible) tensorboardX (optional)

Configuration:

Turn off the batch norm for cudnn. Open the torch/nn/functional.py file, find the line torch.batch_norm, and change the torch.backends.cudnn.enabled option to False. Clone project CenterNet_ROOT=/path/to/clone/CenterNet

Git clone https://github.com/zzzxxxttt/pytorch_simple_CenterNet_45 $CenterNet_ROOT

Install cocoAPIcd $CenterNet_ROOT/lib/cocoapi/PythonAPI

Make

Python setup.py install-user

Compile deformable convolution DCN if you are using pytorch0.4.1, rename $CenterNet_ROOT/lib/DCNv2_old to $CenterNet_ROOT/lib/DCNv2. If you are using pytorch2.1.0 or 1.0.0, rename $CenterNet_ROOT/lib/DCNv2_new to $CenterNet_ROOT/lib/DCNv2. Then start compiling cd $CenterNet_ROOT/lib/DCNv2

. / make.sh

Compile NMScd $CenterNet_ROOT/lib/nms

Make

For datasets in COCO format, the download link is at: http://cocodataset.org/#download. Put annotations, train2017, val2017, test2017 on $CenterNet_ROOT/data/coco

For datasets in Pascal VOC format, download the dataset after VOC has been converted to COCO:

Network disk link: https://pan.baidu.com/share/init?surl=z6BtsKPHh3MnbfT25Y4wYw

Password: 4iu2

After downloading, put annotations, images, VOCdevkit on $CenterNet_ROOT/data/voc

PS: the above two are official datasets. If you make your own dataset, you can read on.

If you choose Hourglass-104 as the backbone, download the Hourglass pre-training model provided by CornerNet:

Network disk link: https://pan.baidu.com/s/1tp9-5CAGwsX3VUSdV276Fg

Password: y1z4

Put the downloaded weight checkpoint.t7 into $CenterNet_ROOT/ckpt/pretrain.

two。 Configure your own dataset

The code provided in this version is configured for the official COCO or official VOC dataset, so there are some details that need to be changed.

As the author is accustomed to VOC format dataset, so take Pascal VOC format as an example, modify their own dataset.

The author has only one class, 'dim target', so modify it according to one class, and other categories are easy to modify.

2.1 VOC category modification changes 16 lines in datasets/pascal.py: VOC_NAMES = ['_ background__', "aeroplane", "bicycle", "bird", "boat"

"bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog"

"horse", "motorbike", "person", "pottedplant", "sheep", "sofa"

"train", "tvmonitor"]

Change it to the name of your own category:

VOC_NAMES = ['_ background__', 'dim target']

Put line 33 in datasets/pascal.py:

Num_classes=20 is modified to its own corresponding number of categories num_classes=1

Change line 35 of the datasets/pascal.py:

Self.valid_ids = 21 in np.arange (1,21, dtype=np.int32) is modified to the number of categories + 1

2.2 annotations

The VOC format dataset does not have the json file required in annotations, which needs to be rebuilt.

The following is a script from VOC to COCO format, which needs to change the names of xml path and json file.

Import xml.etree.ElementTree as ET

Import os

Import json

Coco = dict ()

Coco ['images'] = []

Coco ['type'] =' instances'

Coco ['annotations'] = []

Coco ['categories'] = []

Category_set = dict ()

Image_set = set ()

Category_item_id = 0

Image_id = 20200000000

Annotation_id = 0

Def addCatItem (name):

Global category_item_id

Category_item = dict ()

Category_item ['supercategory'] =' none'

Category_item_id + = 1

Category_item ['id'] = category_item_id

Category_item ['name'] = name

Coco ['categories'] .append (category_item)

Category_ set [name] = category_item_id

Return category_item_id

Def addImgItem (file_name, size):

Global image_id

If file_name is None:

Raise Exception ('Could not find filename tag in xml file.')

If size ['width'] is None:

Raise Exception ('Could not find width tag in xml file.')

If size ['height'] is None:

Raise Exception ('Could not find height tag in xml file.')

Image_id + = 1

Image_item = dict ()

Image_item ['id'] = image_id

Image_item ['file_name'] = file_name

Image_item ['width'] = size [' width']

Image_item ['height'] = size [' height']

Coco ['images'] .append (image_item)

Image_set.add (file_name)

Return image_id

Def addAnnoItem (object_name, image_id, category_id, bbox):

Global annotation_id

Annotation_item = dict ()

Annotation_item ['segmentation'] = []

Seg = []

# bbox [] is x,y,w,h

# left_top

Seg.append (bbox [0])

Seg.append (bbox [1])

# left_bottom

Seg.append (bbox [0])

Seg.append (bbox [1] + bbox [3])

# right_bottom

Seg.append (bbox [0] + bbox [2])

Seg.append (bbox [1] + bbox [3])

# right_top

Seg.append (bbox [0] + bbox [2])

Seg.append (bbox [1])

Annotation_item ['segmentation'] .append (seg)

Annotation_item ['area'] = bbox [2] * bbox [3]

Annotation_item ['iscrowd'] = 0

Annotation_item ['ignore'] = 0

Annotation_item ['image_id'] = image_id

Annotation_item ['bbox'] = bbox

Annotation_item ['category_id'] = category_id

Annotation_id + = 1

Annotation_item ['id'] = annotation_id

Coco ['annotations'] .append (annotation_item)

Def parseXmlFiles (xml_path):

For f in os.listdir (xml_path):

If not f.endswith ('.xml'):

Continue

Real_file_name = f.split (".") [0] + ".jpg"

Bndbox = dict ()

Size = dict ()

Current_image_id = None

Current_category_id = None

File_name = None

Size ['width'] = None

Size ['height'] = None

Size ['depth'] = None

Xml_file = os.path.join (xml_path, f)

Print (xml_file)

Tree = ET.parse (xml_file)

Root = tree.getroot ()

If root.tag! = 'annotation':

Raise Exception (

'pascal voc xml root element should be annotation, rather than {}'

.format (root.tag))

# elem is

For elem in root:

Current_parent = elem.tag

Current_sub = None

Object_name = None

If elem.tag = 'folder':

Continue

If elem.tag = 'filename':

File_name = real_file_name # elem.text

If file_name in category_set:

Raise Exception ('file_name duplicated')

# add img item only after parse tag

Elif current_image_id is None and file_name is not None and size [

'width'] is not None:

# print (file_name, "=", image_set)

If file_name not in image_set:

Current_image_id = addImgItem (file_name, size)

Print ('add image with {} and {}' .format (file_name, size))

Else:

Pass

# raise Exception ('duplicated image: {}' .format (file_name))

# subelem is

For subelem in elem:

Bndbox ['xmin'] = None

Bndbox ['xmax'] = None

Bndbox ['ymin'] = None

Bndbox ['ymax'] = None

Current_sub = subelem.tag

If current_parent = = 'object' and subelem.tag = =' name':

Object_name = subelem.text

If object_name not in category_set:

Current_category_id = addCatItem (object_name)

Else:

Current_category_id = category_ set [object _ name]

Elif current_parent = 'size':

If size [subelem.tag] is not None:

Raise Exception ('xml structure broken at size tag.')

Size [subelem.tag] = int (subelem.text)

# option is, when subelem is

For option in subelem:

If current_sub = 'bndbox':

If bndbox [option.tag] is not None:

Raise Exception (

'xml structure corrupted at bndbox tag.')

Bndbox [option.tag] = int (option.text)

# only after parse the tag

If bndbox ['xmin'] is not None:

If object_name is None:

Raise Exception ('xml structure broken at bndbox tag')

If current_image_id is None:

Raise Exception ('xml structure broken at bndbox tag')

If current_category_id is None:

Raise Exception ('xml structure broken at bndbox tag')

Bbox = []

# x

Bbox.append (bndbox ['xmin'])

# y

Bbox.append (bndbox ['ymin'])

# w

Bbox.append (bndbox ['xmax']-bndbox [' xmin'])

# h

Bbox.append (bndbox ['ymax']-bndbox [' ymin'])

Print ('add annotation with {}, {}' .format (

Object_name, current_image_id, current_category_id

Bbox))

AddAnnoItem (object_name, current_image_id

Current_category_id, bbox)

If _ _ name__ = ='_ _ main__':

Xml_path ='. / annotations/test'

Json_file ='. / pascal_test2020.json'

#'. / pascal_trainval0712.json'

ParseXmlFiles (xml_path)

Json.dump (coco, open (json_file,'w'))

Note that the naming of the json file is determined by the contents of lines 44 to 48 in the datasets/pascal.py.

Self.data_dir = os.path.join (data_dir, 'voc')

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

_ ann_name = {'train':' trainval0712', 'val':' test2007'}

Self.annot_path = os.path.join (self.data_dir, 'annotations',' pascal_%s.json'% _ ann_ name [split])

Here, the author modifies these fields to facilitate naming:

Self.data_dir = os.path.join (data_dir, 'voc') #. / data/voc

Self.img_dir = os.path.join (self.data_dir, 'images') #. / data/voc/images

_ ann_name = {'train':' train2020', 'val':' test2020'}

# means that a dataset in json format is required

Self.annot_path = os.path.join (

Self.data_dir, 'annotations',' pascal_%s.json'% _ ann_ name [split])

Therefore, the naming of json is required to be prepared in the following format:

#. / data/voc/annotations

#-pascal_train2020

#-pascal_test2020

The overall format of the dataset is:

-data

-voc

-annotations

-pascal_train2020.json

-pascal_test2020.json

-images

-* .jpg

-VOCdevkit (this folder is mainly used for evaluation)

-VOC2007

-Annotations

-* .xml

-JPEGImages

-* .jpg

-ImageSets

-Main

-train.txt

-val.txt

-trainval.txt

-test.txt

2.3 other

In rows 21-22 of datasets/pascal.py, the standard deviation and variance are best replaced with the standard deviation and variance of your own dataset.

VOC_MEAN = [0.485, 0.456, 0.406]

VOC_STD = [0.229, 0.224, 0.225]

3. Training and testing 3.1 training orders

There are many training commands, so you can write a shell script to complete it.

Python train.py-- log_name pascal_resdcn18_384_dp\

-- dataset pascal\

-- arch resdcn_18\

-- img_size 384\

-- lr 1.25e-4\

-- lr_step 450.60\

-- batch_size 32\

-num_epochs 70\

-- num_workers 10

Log name represents the name of the log recorded.

Dataset sets pascal to represent the use of pascal voc format.

Arch represents the type of backbone selected, and there are the following:

Large_hourglasssmall_hourglassresdcn_18resdcn_34resdcn_50resdcn_101resdcn_152

Img size controls the length and width of the picture.

Lr and lr_step control the size and change of learning rate.

Batch size is the number of pictures processed in a batch.

Num epochs represents the total number of times a dataset has been learned.

Num workers represents how many threads are opened to load the dataset.

3.2 Test commands

The test command is very simple, and it is important to note that the img size should be consistent with the setting set during training.

Python test.py-- log_name pascal_resdcn18_384_dp\

-- dataset pascal\

-- arch resdcn_18\

-- img_size 384

Flip test belongs to TTA (Test Time Augmentation), which can improve mAP to some extent.

# flip test

Python test.py-- log_name pascal_resdcn18_384_dp\

-- dataset pascal\

-- arch resdcn_18\

-- img_size 384\

-- test_flip

4. Result

The following are the results obtained by the author with different image resolutions and TTA methods on COCO and VOC datasets.

COCO:ModelTraining image sizemAPHourglass-104 (DP) 51239.9/42.3/45.0Hourglass-104 (DDP) 51240.5/42.6/45.3PascalVOC:ModelTraining image sizemAPResDCN-18 (DDP) 38471.19/72.99ResDCN-18 (DDP) 51272.76 PyTorch 75.69 how to configure the environment is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report