【行人重识别】fast-reid复现(20210111)

参考代码:

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

0.环境

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

安装apex(不要直接通过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.准备数据

参考https://blog.csdn.net/qq_35975447/article/details/106664593

数据目录结构如下:

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

2.修改

2.1 RandomErasing

我的torchvision是0.3.0版本的,所以需要修改一些地方。如果torchvision不是高版本的,需要

修改三处:

(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类型

# "./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(多数据训练的)

# 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

 由于sbs后面会调用bot的yaml文件,所以这里要注释掉bot中yaml的pretrain_model(路径:configs/Base-bagtricks.yml):

3.训练

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

对比最后一项,可以看到mAP是可以复现对应结果的。

复现BOT R101-ibn(默认参数,步骤还是要按之前的来):

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

猜你喜欢

转载自blog.csdn.net/qq_35975447/article/details/112482765
今日推荐