1.下載Dark Face數據集,使用track2.2_test_sample文件中圖片進行人臉檢測測試。
2.修改DSFD源碼中demo.py部分:
test_oneimage():
def test_oneimage():
# load net
# 影響網絡的自動求導機制,使網絡前向傳播后不進行求導和反向傳播(僅測試時使用)
torch.set_grad_enabled(False)
# 加載config配置參數
cfg = widerface_640
# 分類的類別數目---widerface.py
num_classes = len(WIDERFace_CLASSES) + 1 # +1 background
# 加載SSD網絡模型,返回一個SSD實例
net = build_ssd('test', cfg['min_dim'], num_classes) # initialize SSD
# 加載預訓練模型train_model
net.load_state_dict(torch.load(args.trained_model))
net.cuda()
# 表示進入評估模式,神經網絡中有train(),eval()兩種模式,使用eval()可關閉dropout
net.eval()
print('Finished loading model!')
# evaluation
cuda = args.cuda
transform = TestBaseTransform((104, 117, 123))
thresh=cfg['conf_thresh']
#save_path = args.save_folder
#num_images = len(testset)
# load data,從指定路徑加載待測圖像
'''
以此為界,前半部分為網絡模型加載和初始化,后半部分為單張圖片的人臉檢測。此處為測試圖片的路
徑設置過程。
修改部分,不適用arg.imag_root作為測試圖像路徑.
遍歷darkface數據集100張圖片,依次讀取并進行測試
'''
folder = './data/'
#img_id = 'face'
for i in range(100):
img = cv2.imread(folder + str(i) + '_fake_B.jpg', cv2.IMREAD_COLOR)
img_id = 'test' + str(i)
# 單張圖片的測試過程
max_im_shrink = ( (2000.0*2000.0) / (img.shape[0] * img.shape[1])) ** 0.5
shrink = max_im_shrink if max_im_shrink < 1 else 1
det0 = infer(net , img , transform , thresh , cuda , shrink)
det1 = infer_flip(net , img , transform , thresh , cuda , shrink)
# shrink detecting and shrink only detect big face
st = 0.5 if max_im_shrink >= 0.75 else 0.5 * max_im_shrink
det_s = infer(net , img , transform , thresh , cuda , st)
index = np.where(np.maximum(det_s[:, 2] - det_s[:, 0] + 1, det_s[:, 3] - det_s[:, 1] + 1) > 30)[0]
det_s = det_s[index, :]
# enlarge one times
factor = 2
bt = min(factor, max_im_shrink) if max_im_shrink > 1 else (st + max_im_shrink) / 2
det_b = infer(net , img , transform , thresh , cuda , bt)
# enlarge small iamge x times for small face
if max_im_shrink > factor:
bt *= factor
while bt < max_im_shrink:
det_b = np.row_stack((det_b, infer(net , img , transform , thresh , cuda , bt)))
bt *= factor
det_b = np.row_stack((det_b, infer(net , img , transform , thresh , cuda , max_im_shrink) ))
# enlarge only detect small face
if bt > 1:
index = np.where(np.minimum(det_b[:, 2] - det_b[:, 0] + 1, det_b[:, 3] - det_b[:, 1] + 1) < 100)[0]
det_b = det_b[index, :]
else:
index = np.where(np.maximum(det_b[:, 2] - det_b[:, 0] + 1, det_b[:, 3] - det_b[:, 1] + 1) > 30)[0]
det_b = det_b[index, :]
det = np.row_stack((det0, det1, det_s, det_b))
det = bbox_vote(det)
vis_detections(img , det , img_id, args.visual_threshold)
3.運行demo.py即可。能在arg.save_folder處得到100張dark_face人臉檢測結果。
補充:
DSFD只接收輸入格式為jpg的圖片,因此對darkface數據集進行批量轉換。
import os
from PIL import Image
dirname_read="/home/...DSFD/darkface_png/" # png格式圖片的輸入路徑
dirname_write="/home/...DSFD/data/" # jpg圖片的輸出路徑
names=os.listdir(dirname_read)
count=0
for name in names:
img=Image.open(dirname_read+name)
name=name.split(".")
if name[-1] == "png":
name[-1] = "jpg"
name = str.join(".", name)
#r,g,b,a=img.split()
#img=Image.merge("RGB",(r,g,b))
to_save_path = dirname_write + name
img.save(to_save_path)
count+=1
print(to_save_path, "------conut:", count)
else:
conti
審核編輯:湯梓紅
-
網絡
+關注
關注
14文章
7520瀏覽量
88633 -
源碼
+關注
關注
8文章
633瀏覽量
29140 -
數據集
+關注
關注
4文章
1205瀏覽量
24644
發布評論請先 登錄
相關推薦
評論