[Pedestrian re-identification] fast-reid recurrence (20210111)

Reference Code:

https://github.com/JDAI-CV/fast-reid

0. Environment

ubuntu16.04
cuda9.0
python3.6
torch==1.1.0
torchvision==0.3.0
Cython
yacs
tensorboard
future
termcolor
sklearn
tqdm
opencv-python==4.1.0.25
matplotlib
scikit-image
numpy==1.16.4

Install apex (do not install directly via pip):

git clone https://www.github.com/nvidia/apex
cd apex
# python setup.py install --cuda_ext --cpp_ext 
pip install -v --no-cache-dir ./

1. Prepare the data

Refer to https://blog.csdn.net/qq_35975447/article/details/106664593

The data directory structure is as follows:

fast-reid
	datasets
		Market-1501-v15.09.15
			bounding_box_train
			bounding_box_test
			query

2. Modify

2.1 RandomErasing

My torchvision is version 0.3.0, so I need to modify some places. If torchvision is not a high version, you need

Three modifications:

(1)fastreid/data/transforms/build.py line 66-77

# fastreid/data/transforms/build.py line 66-77

    # res.append(ToTensor()) # gmt modify

        if do_rea:
            res.append(RandomErasing(probability=rea_prob, mean=rea_value))  # gmt modify
        if do_rpt:
            res.append(RandomPatch(prob_happen=rpt_prob))
    else:
        size_test = cfg.INPUT.SIZE_TEST
        res.append(T.Resize(size_test, interpolation=3))
        # res.append(ToTensor()) # gmt modify
    res.append(ToTensor()) # gmt modify
    return T.Compose(res)

(2) fastreid/data/transforms/transforms.py line 7

# fastreid/data/transforms/transforms.py line 7
# __all__ = ['ToTensor', 'RandomPatch', 'AugMix', ]   # gmt modify
__all__ = ['ToTensor', 'RandomErasing', 'RandomPatch', 'AugMix', ]  # gmt modify

(3)fastreid/data/transforms/transforms.py line 44-86

# fastreid/data/transforms/transforms.py line 44-86

class RandomErasing(object):
    """ Randomly selects a rectangle region in an image and erases its pixels.
        'Random Erasing Data Augmentation' by Zhong et al.
        See https://arxiv.org/pdf/1708.04896.pdf
    Args:
        probability: The probability that the Random Erasing operation will be performed.
        sl: Minimum proportion of erased area against input image.
        sh: Maximum proportion of erased area against input image.
        r1: Minimum aspect ratio of erased area.
        mean: Erasing value.
    """

    def __init__(self, probability=0.5, sl=0.02, sh=0.4, r1=0.3, mean=255 * (0.49735, 0.4822, 0.4465)):
        self.probability = probability
        self.mean = mean
        self.sl = sl
        self.sh = sh
        self.r1 = r1

    def __call__(self, img):
        img = np.asarray(img, dtype=np.float32).copy()
        if random.uniform(0, 1) > self.probability:
            return img

        for attempt in range(100):
            area = img.shape[0] * img.shape[1]
            target_area = random.uniform(self.sl, self.sh) * area
            aspect_ratio = random.uniform(self.r1, 1 / self.r1)

            h = int(round(math.sqrt(target_area * aspect_ratio)))
            w = int(round(math.sqrt(target_area / aspect_ratio)))

            if w < img.shape[1] and h < img.shape[0]:
                x1 = random.randint(0, img.shape[0] - h)
                y1 = random.randint(0, img.shape[1] - w)
                if img.shape[2] == 3:
                    img[x1:x1 + h, y1:y1 + w, 0] = self.mean[0]
                    img[x1:x1 + h, y1:y1 + w, 1] = self.mean[1]
                    img[x1:x1 + h, y1:y1 + w, 2] = self.mean[2]
                else:
                    img[x1:x1 + h, y1:y1 + w, 0] = self.mean[0]
                return img
        return img

2.2 CUDA float type

# "./fastreid/layers/circle_softmax.py", line 38
pred_class_logits = targets * s_p + (1.0 - targets) * s_n

# 改为:
pred_class_logits = targets.float() * s_p.float() + (1.0 - targets.float()) * s_n.float()

2.3 cross_entroy_loss

# fastreid/modeling/losses/cross_entroy_loss.py line 50

non_zero_cnt = max(loss.nonzero().size(0), 1)  # gmt add
# non_zero_cnt = max(loss.nonzero(as_tuple=False).size(0), 1)

2.4 data/commom.py (multi-data training)

# line 26-27
self.pids = sorted(list(pid_set))
self.cams = sorted(list(cam_set))

#改为:

self.pids = sorted(list(pid_set), key=lambda x: str(x))
self.cams = sorted(list(cam_set), key=lambda x: str(x))

2.5 config

 Since the yaml file of the bot will be called after the sbs, the pretrain_model of the yaml in the bot (path: configs/Base-bagtricks.yml) should be commented out here:

3. Training

CUDA_VISIBLE_DEVICES="0,1" python ./tools/train_net.py --config-file='./configs/Market1501/sbs_R101-ibn.yml'

 

Comparing the last item, you can see that mAP can reproduce the corresponding results.

Reproduce BOT R101-ibn (default parameters, the steps still have to be the previous ones):

CUDA_VISIBLE_DEVICES="0" python ./tools/train_net.py --config-file='./configs/Market1501/bagtricks_R101-ibn.yml'

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_35975447/article/details/112482765