Skip to content

Commit

Permalink
updated retina net
Browse files Browse the repository at this point in the history
  • Loading branch information
scarlet committed May 16, 2019
1 parent 5b0ddfd commit 16a075e
Show file tree
Hide file tree
Showing 56 changed files with 14,401 additions and 5,326 deletions.
2 changes: 1 addition & 1 deletion .directory
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[Dolphin]
Timestamp=2019,4,25,22,44,42
Timestamp=2019,5,17,5,4,4
Version=4
23 changes: 0 additions & 23 deletions CHKIPCamera.py

This file was deleted.

23 changes: 0 additions & 23 deletions CXMIPCamera.py

This file was deleted.

Binary file added Embedding/danger.npy
Binary file not shown.
Binary file added Embedding/safe.npy
Binary file not shown.
Binary file added Embedding/train.npy
Binary file not shown.
72 changes: 1 addition & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,77 +36,7 @@ Please check [Dataset-Zoo](https://github.com/deepinsight/insightface/wiki/Datas

* Please check *src/data/face2rec2.py* on how to build a binary face dataset. Any public available *MTCNN* can be used to align the faces, and the performance should not change. We will improve the face normalisation step by full pose alignment methods recently.

### Train

1. Install `MXNet` with GPU support (Python 2.7).

```
pip install mxnet-cu90
```

2. Clone the InsightFace repository. We call the directory insightface as *`INSIGHTFACE_ROOT`*.

```
git clone --recursive https://github.com/deepinsight/insightface.git
```

3. Download the training set (`MS1M-Arcface`) and place it in *`$INSIGHTFACE_ROOT/datasets/`*. Each training dataset includes at least following 6 files:

```Shell
faces_emore/
train.idx
train.rec
property
lfw.bin
cfp_fp.bin
agedb_30.bin
```

The first three files are the training dataset while the last three files are verification sets.

4. Train deep face recognition models.
In this part, we assume you are in the directory *`$INSIGHTFACE_ROOT/recognition/`*.
```Shell
export MXNET_CPU_WORKER_NTHREADS=24
export MXNET_ENGINE_TYPE=ThreadedEnginePerDevice
```

Place and edit config file:
```Shell
cp sample_config.py config.py
vim config.py # edit dataset path etc..
```

We give some examples below. Our experiments were conducted on the Tesla P40 GPU.

(1). Train ArcFace with LResNet100E-IR.

```Shell
CUDA_VISIBLE_DEVICES='0,1,2,3' python -u train.py --network r100 --loss arcface --dataset emore
```
It will output verification results of *LFW*, *CFP-FP* and *AgeDB-30* every 2000 batches. You can check all options in *config.py*.
This model can achieve *LFW 99.80+* and *MegaFace 98.3%+*.

(2). Train CosineFace with LResNet50E-IR.

```Shell
CUDA_VISIBLE_DEVICES='0,1,2,3' python -u train.py --network r50 --loss cosface --dataset emore
```

(3). Train Softmax with LMobileNet-GAP.

```Shell
CUDA_VISIBLE_DEVICES='0,1,2,3' python -u train.py --network m1 --loss softmax --dataset emore
```

(4). Fine-turn the above Softmax model with Triplet loss.

```Shell
CUDA_VISIBLE_DEVICES='0,1,2,3' python -u train.py --network m1 --loss triplet --lr 0.005 --pretrained ./models/m1-softmax-emore,1
```


5. Verification results.
### Verification results.

*LResNet100E-IR* network trained on *MS1M-Arcface* dataset with ArcFace loss:

Expand Down
55 changes: 26 additions & 29 deletions face_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,44 @@
import mxnet as mx
import cv2
import time
from mtcnn_detector import MtcnnDetector

import face_preprocess
import face_image
from retinaface import RetinaFace
from face_preprocess import preprocess


class DetectorModel:
def __init__(self, args):
ctx = mx.cpu() if args.gpu == -1 else mx.gpu(args.gpu)
mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')

self.detector = RetinaFace(args.retina_model, 0, args.gpu, 'net3')
self.threshold = args.threshold
self.scales = args.scales
self.max_face_number = args.max_face_number
self.face_counter = 0
self.detector = MtcnnDetector(model_folder=mtcnn_path, ctx=ctx, num_worker=1,
minsize=args.mtcnn_minsize, factor=args.mtcnn_factor,
accurate_landmark=True, threshold=args.mtcnn_threshold)
self.counter = 0
self.image_size = args.image_size

def get_all_boxes(self, face_img, save_img=False):
face_num = self.max_face_number
ret = self.detector.detect_face(face_img, det_type=0)
if ret is None:
return []
def save_image(self, image):
cv2.imwrite('./Temp/{}-{}.jpg'.format(time.time(), self.counter),
image)
self.counter += 1

bbox, points = ret
def get_all_boxes(self, img, save_img=False):
faces, landmarks = self.detector.detect(img,
self.threshold,
scales=self.scales)

sorted_index = bbox[:, 0].argsort()
bbox = bbox[sorted_index]
points = points[sorted_index]
sorted_index = faces[:, 0].argsort()
faces = faces[sorted_index]
landmarks = landmarks[sorted_index]

aligned = []
for index in range(0, len(bbox[:face_num])):
item_bbox = bbox[index, 0:4]
item_points = points[index, :].reshape((2, 5)).T
nimg = face_preprocess.preprocess(
face_img, item_bbox, item_points, image_size='112,112')
# print('find', faces.shape[0], 'faces')
for i in range(len(faces[:self.max_face_number])):
nimg = preprocess(img,
faces[i],
landmarks[i],
image_size=self.image_size)

if save_img:
cv2.imwrite('./Temp/{}-{}.jpg'.
format(time.time(), self.face_counter), nimg)
self.face_counter += 1
self.save_image(nimg)

aligned.append(nimg)

return zip(aligned, bbox)
return zip(aligned, faces)
90 changes: 76 additions & 14 deletions face_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@
import cv2
import time
import sklearn
from time import sleep

import face_preprocess
import face_image


def get_model(ctx, image_size, model_str, layer):
_vec = model_str.split(',')
assert len(_vec) == 2
prefix = _vec[0]
epoch = int(_vec[1])
print('loading', prefix, epoch)
# print('loading', prefix, epoch)
sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
all_layers = sym.get_internals()
sym = all_layers[layer+'_output']
sym = all_layers[layer + '_output']
model = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))])
model.set_params(arg_params, aux_params)
Expand All @@ -34,23 +31,88 @@ def __init__(self, args):
assert len(_vec) == 2
image_size = (int(_vec[0]), int(_vec[1]))

self.model = get_model(ctx, image_size, args.model, 'fc1')
self.model = get_model(ctx, image_size, args.arcface_model, 'fc1')
self.face_counter = 0


def get_feature(self, aligned, from_disk=False):
def get_one_feature(self, aligned, from_disk=True):
if from_disk:
aligned = np.transpose(cv2.cvtColor(
aligned, cv2.COLOR_BGR2RGB), (2, 0, 1))
aligned = np.transpose(cv2.cvtColor(aligned, cv2.COLOR_BGR2RGB),
(2, 0, 1))
input_blob = np.expand_dims(aligned, axis=0)
data = mx.nd.array(input_blob)
db = mx.io.DataBatch(data=(data,))
db = mx.io.DataBatch(data=(data, ))
self.model.forward(db, is_train=False)
embedding = self.model.get_outputs()[0].asnumpy()
embedding = sklearn.preprocessing.normalize(embedding).flatten()
return embedding

def get_features_from_path(self, img_paths):
result = []
for counter, path in enumerate(img_paths):
# print(type(len(img_paths)))
# print(type(counter))
nimg = cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB)
aligned = np.transpose(nimg, (2, 0, 1))
embedding = None
for flipid in [0, 1]:
if flipid == 1 and self.args.flip == 1:
do_flip(aligned)

input_blob = np.expand_dims(aligned, axis=0)
data = mx.nd.array(input_blob)
db = mx.io.DataBatch(data=(data, ))
self.model.forward(db, is_train=False)
_embedding = self.model.get_outputs()[0].asnumpy()
# print(_embedding.shape)
if embedding is None:
embedding = _embedding
else:
embedding += _embedding
embedding = sklearn.preprocessing.normalize(embedding).flatten()
result.append(embedding)
# print()
print('特征转换已完成%2f%%' % (counter * 100 / len(img_paths)))
return result

def get_feature_from_raw(self, face_img):
# face_img is bgr image
ret = self.detector.detect_face_limited(face_img,
det_type=self.args.det)
if ret is None:
return None
bbox, points = ret
if bbox.shape[0] == 0:
return None
bbox = bbox[0, 0:4]
points = points[0, :].reshape((2, 5)).T
# print(bbox)
# print(points)
nimg = face_preprocess.preprocess(face_img,
bbox,
points,
image_size='112,112')

def get_one_feature(self, nimg):
return self.get_feature(
np.transpose(cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB), (2, 0, 1)))
# cv2.imshow(' ', nimg)
# cv2.waitKey(0)

nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
aligned = np.transpose(nimg, (2, 0, 1))
# print(nimg.shape)
embedding = None
for flipid in [0, 1]:
if flipid == 1:
if self.args.flip == 0:
break
do_flip(aligned)
input_blob = np.expand_dims(aligned, axis=0)
data = mx.nd.array(input_blob)
db = mx.io.DataBatch(data=(data, ))
self.model.forward(db, is_train=False)
_embedding = self.model.get_outputs()[0].asnumpy()
# print(_embedding.shape)
if embedding is None:
embedding = _embedding
else:
embedding += _embedding
embedding = sklearn.preprocessing.normalize(embedding).flatten()
return embedding
Loading

0 comments on commit 16a075e

Please sign in to comment.