From 034f4aa0a2e244e361e7f6b0b3548c8966de3e0f Mon Sep 17 00:00:00 2001 From: zhouxiaodong Date: Tue, 29 Sep 2020 15:39:55 +0800 Subject: [PATCH 1/2] negative train --- data/wider_face.py | 66 ++++++++++++++++++++++----------- detect.py | 12 +++--- layers/modules/multibox_loss.py | 2 +- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/data/wider_face.py b/data/wider_face.py index 22f56efdc..191f663e2 100644 --- a/data/wider_face.py +++ b/data/wider_face.py @@ -44,33 +44,55 @@ def __getitem__(self, index): labels = self.words[index] annotations = np.zeros((0, 15)) if len(labels) == 0: - return annotations - for idx, label in enumerate(labels): + # return annotations annotation = np.zeros((1, 15)) # bbox - annotation[0, 0] = label[0] # x1 - annotation[0, 1] = label[1] # y1 - annotation[0, 2] = label[0] + label[2] # x2 - annotation[0, 3] = label[1] + label[3] # y2 + annotation[0, 0] = 0 # x1 + annotation[0, 1] = 0 # y1 + annotation[0, 2] = 0 # x2 + annotation[0, 3] = 0 # y2 # landmarks - annotation[0, 4] = label[4] # l0_x - annotation[0, 5] = label[5] # l0_y - annotation[0, 6] = label[7] # l1_x - annotation[0, 7] = label[8] # l1_y - annotation[0, 8] = label[10] # l2_x - annotation[0, 9] = label[11] # l2_y - annotation[0, 10] = label[13] # l3_x - annotation[0, 11] = label[14] # l3_y - annotation[0, 12] = label[16] # l4_x - annotation[0, 13] = label[17] # l4_y - if (annotation[0, 4]<0): - annotation[0, 14] = -1 - else: - annotation[0, 14] = 1 - + annotation[0, 4] = -1 # l0_x + annotation[0, 5] = -1 # l0_y + annotation[0, 6] = -1 # l1_x + annotation[0, 7] = -1 # l1_y + annotation[0, 8] = -1 # l2_x + annotation[0, 9] = -1 # l2_y + annotation[0, 10] = -1 # l3_x + annotation[0, 11] = -1 # l3_y + annotation[0, 12] = -1 # l4_x + annotation[0, 13] = -1 # l4_y + annotation[0, 14] = 0 annotations = np.append(annotations, annotation, axis=0) - target = np.array(annotations) + target = np.array(annotations) + else: + for idx, label in enumerate(labels): + annotation = np.zeros((1, 15)) + # bbox + annotation[0, 0] = label[0] # x1 + annotation[0, 1] = label[1] # y1 + annotation[0, 2] = label[0] + label[2] # x2 + annotation[0, 3] = label[1] + label[3] # y2 + + # landmarks + annotation[0, 4] = label[4] # l0_x + annotation[0, 5] = label[5] # l0_y + annotation[0, 6] = label[7] # l1_x + annotation[0, 7] = label[8] # l1_y + annotation[0, 8] = label[10] # l2_x + annotation[0, 9] = label[11] # l2_y + annotation[0, 10] = label[13] # l3_x + annotation[0, 11] = label[14] # l3_y + annotation[0, 12] = label[16] # l4_x + annotation[0, 13] = label[17] # l4_y + if (annotation[0, 4]<0): + annotation[0, 14] = -1 + else: + annotation[0, 14] = 1 + + annotations = np.append(annotations, annotation, axis=0) + target = np.array(annotations) if self.preproc is not None: img, target = self.preproc(img, target) diff --git a/detect.py b/detect.py index 2e822400e..1f5909f88 100755 --- a/detect.py +++ b/detect.py @@ -23,7 +23,9 @@ parser.add_argument('--nms_threshold', default=0.4, type=float, help='nms_threshold') parser.add_argument('--keep_top_k', default=750, type=int, help='keep_top_k') parser.add_argument('-s', '--save_image', action="store_true", default=True, help='show detection results') -parser.add_argument('--vis_thres', default=0.6, type=float, help='visualization_threshold') +parser.add_argument('--vis_thres', default=0.8, type=float, help='visualization_threshold') +parser.add_argument('-img', '--image', default='./curve/test.jpg', + type=str, help='image') args = parser.parse_args() @@ -83,8 +85,8 @@ def load_model(model, pretrained_path, load_to_cpu): resize = 1 # testing begin - for i in range(100): - image_path = "./curve/test.jpg" + for i in range(1): + image_path = args.image img_raw = cv2.imread(image_path, cv2.IMREAD_COLOR) img = np.float32(img_raw) @@ -145,6 +147,7 @@ def load_model(model, pretrained_path, load_to_cpu): # show image if args.save_image: for b in dets: + print(b[0],b[1],b[2],b[3],b[4]) if b[4] < args.vis_thres: continue text = "{:.4f}".format(b[4]) @@ -164,5 +167,4 @@ def load_model(model, pretrained_path, load_to_cpu): # save image name = "test.jpg" - cv2.imwrite(name, img_raw) - + cv2.imwrite(name, img_raw) \ No newline at end of file diff --git a/layers/modules/multibox_loss.py b/layers/modules/multibox_loss.py index 096620480..71e5278b3 100644 --- a/layers/modules/multibox_loss.py +++ b/layers/modules/multibox_loss.py @@ -106,7 +106,7 @@ def forward(self, predictions, priors, targets): _, loss_idx = loss_c.sort(1, descending=True) _, idx_rank = loss_idx.sort(1) num_pos = pos.long().sum(1, keepdim=True) - num_neg = torch.clamp(self.negpos_ratio*num_pos, max=pos.size(1)-1) + num_neg = torch.clamp(self.negpos_ratio*num_pos+200, max=pos.size(1)-1)#避免pos为0时,neg也为0,可以自行设置 neg = idx_rank < num_neg.expand_as(idx_rank) # Confidence Loss Including Positive and Negative Examples From c6dae4cbc38070811d580a5b5ac2b7bab2acf168 Mon Sep 17 00:00:00 2001 From: zhouxiaodong Date: Tue, 29 Sep 2020 15:46:20 +0800 Subject: [PATCH 2/2] readme --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 1974b3640..9f61cbdc9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ +# Modify + + this repo main difference from original is add negative data train + + you can now add some image path without any annotions in you label.txt,then it can train now! + # RetinaFace in PyTorch A [PyTorch](https://pytorch.org/) implementation of [RetinaFace: Single-stage Dense Face Localisation in the Wild](https://arxiv.org/abs/1905.00641). Model size only 1.7M, when Retinaface use mobilenet0.25 as backbone net. We also provide resnet50 as backbone net to get better result. The official code in Mxnet can be found [here](https://github.com/deepinsight/insightface/tree/master/RetinaFace).