forked from Azure/ObjectDetectionUsingCntk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6_scoreImage.py
124 lines (105 loc) · 5.27 KB
/
6_scoreImage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
import sys, os, importlib, random, json
import PARAMETERS
from helpers_cntk import *
locals().update(importlib.import_module("PARAMETERS").__dict__)
####################################
# Parameters
####################################
imgPath = r"C:/Users/pabuehle/Desktop/newImgs/WIN_20160803_11_30_07_Pro.jpg"
#choose which classifier to use
classifier = 'svm'
svm_experimentName = 'exp1'
# no need to change these parameters
boAddSelectiveSearchROIs = True
boAddGridROIs = True
boFilterROIs = True
boUseNonMaximaSurpression = True
####################################
# Main
####################################
random.seed(0)
# load cntk model
print("Loading DNN..")
tstart = datetime.datetime.now()
model_path = os.path.join(modelDir, "frcn_" + classifier + ".model")
if not os.path.exists(model_path):
raise Exception("Model {} not found.".format(model_path))
model = load_model(model_path)
print("Time loading DNN [ms]: " + str((datetime.datetime.now() - tstart).total_seconds() * 1000))
# load trained svm
if classifier == "svm":
print("Loading svm weights..")
tstart = datetime.datetime.now()
svmWeights, svmBias, svmFeatScale = loadSvm(trainedSvmDir, svm_experimentName)
print("Time loading svm [ms]: " + str((datetime.datetime.now() - tstart).total_seconds() * 1000))
else:
svmWeights, svmBias, svmFeatScale = (None, None, None)
# compute ROIs
tstart = datetime.datetime.now()
imgOrig = imread(imgPath)
currRois = computeRois(imgOrig, boAddSelectiveSearchROIs, boAddGridROIs, boFilterROIs, ss_kvals, ss_minSize,
ss_max_merging_iterations, ss_nmsThreshold,
roi_minDimRel, roi_maxDimRel, roi_maxImgDim, roi_maxAspectRatio, roi_minNrPixelsRel,
roi_maxNrPixelsRel, grid_nrScales, grid_aspectRatios, grid_downscaleRatioPerIteration, grid_stepSizeRel)
currRois = currRois[:cntk_nrRois] # only keep first cntk_nrRois rois
print("Time roi computation [ms]: " + str((datetime.datetime.now() - tstart).total_seconds() * 1000))
# prepare DNN inputs
tstart = datetime.datetime.now()
imgPadded = imresizeAndPad(imgOrig, cntk_padWidth, cntk_padHeight)
_, _, roisCntk = getCntkInputs(imgPath, currRois, None, train_posOverlapThres, nrClasses, cntk_nrRois, cntk_padWidth, cntk_padHeight)
arguments = {
model.arguments[0]: [np.ascontiguousarray(np.array(imgPadded, dtype=np.float32).transpose(2, 0, 1))], # convert to CNTK's HWC format
model.arguments[1]: [np.array(roisCntk, np.float32)]
}
print("Time cnkt input generation [ms]: " + str((datetime.datetime.now() - tstart).total_seconds() * 1000))
# run DNN model
print("Running model..")
tstart = datetime.datetime.now()
dnnOutputs = model.eval(arguments)[0]
dnnOutputs = dnnOutputs[:len(currRois)] # remove the zero-padded rois
print("Time running model [ms]: " + str((datetime.datetime.now() - tstart).total_seconds() * 1000))
# score all ROIs
tstart = datetime.datetime.now()
labels, scores = scoreRois(classifier, dnnOutputs, svmWeights, svmBias, svmFeatScale, len(classes),
decisionThreshold = vis_decisionThresholds[classifier])
print("Time making prediction [ms]: " + str((datetime.datetime.now() - tstart).total_seconds() * 1000))
# perform non-maxima surpression
tstart = datetime.datetime.now()
nmsKeepIndices = []
if boUseNonMaximaSurpression:
nmsKeepIndices = applyNonMaximaSuppression(nmsThreshold, labels, scores, currRois)
print("Non-maxima surpression kept {:4} of {:4} rois (nmsThreshold={})".format(
len(nmsKeepIndices), len(labels), nmsThreshold))
print("Time non-maxima surpression [ms]: " + str((datetime.datetime.now() - tstart).total_seconds() * 1000))
# visualize results
imgDebug = visualizeResults(imgPath, labels, scores, currRois, classes, nmsKeepIndices,
boDrawNegativeRois=False, boDrawNmsRejectedRois=False)
imshow(imgDebug, waitDuration=0, maxDim=800)
# create json-encoded string of all detections
outDict = [{"label": str(l), "score": str(s), "nms": str(False), "left": str(r[0]), "top": str(r[1]), "right": str(r[2]), "bottom": str(r[3])} for l,s, r in zip(labels, scores, currRois)]
for i in nmsKeepIndices:
outDict[i]["nms"] = str(True)
outJsonString = json.dumps(outDict)
print("Json-encoded detections: " + outJsonString[:120] + "...")
print("DONE.")
#--- optional code ---#
# write all detections to file, and show how to read in again to visualize
# writeDetectionsFile("detections.tsv", outDict, classes)
# labels2, scores2, currRois2, nmsKeepIndices2 = parseDetectionsFile("detections.tsv", lutClass2Id)
# imgDebug2 = visualizeResults(imgPath, labels2, scores2, currRois2, classes, nmsKeepIndices2, # identical to imgDebug
# boDrawNegativeRois=False, boDrawNmsRejectedRois=False)
# imshow(imgDebug2, waitDuration=0, maxDim=800)
# extract crop of the highest scored ROI
# maxScore = -float("inf")
# maxScoreRoi = []
# for index, (label,score) in enumerate(zip(labels,scores)):
# if score > maxScore and label > 0: #and index in nmsKeepIndices:
# maxScore = score
# maxScoreRoi = currRois[index]
# if maxScoreRoi == []:
# print("WARNING: not a single object detected")
# else:
# imgCrop = imgOrig[maxScoreRoi[1]:maxScoreRoi[3], maxScoreRoi[0]:maxScoreRoi[2], :]
# imwrite(imgCrop, outCropDir + os.path.basename(imgPath))
# imshow(imgCrop)