Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable negative date train #128

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
66 changes: 44 additions & 22 deletions data/wider_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 7 additions & 5 deletions detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand All @@ -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)
2 changes: 1 addition & 1 deletion layers/modules/multibox_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down