def evaluate_output(): p = PARAMETERS.get_parameters_for_dataset() # parse cntk output print ("Parsing CNTK output for image set: " + image_set) cntkImgsListPath = os.path.join(p.cntkFilesDir, image_set + ".txt") outParsedDir = os.path.join(p.cntkFilesDir, image_set + "_parsed") cntkOutputPath = os.path.join(p.cntkFilesDir, image_set + ".z") # write cntk output for each image to separate file makeDirectory(outParsedDir) parseCntkOutput(cntkImgsListPath, cntkOutputPath, outParsedDir, p.cntk_nrRois, p.cntk_featureDimensions[p.classifier], saveCompressed=True, skipCheck=True) # delete cntk output file which can be very large # deleteFile(cntkOutputPath) imdb = p.imdbs[image_set] net = DummyNet(4096, imdb.num_classes, outParsedDir) # create empty directory for evaluation files if type(imdb) == imdb_data: evalTempDir = None else: # pascal_voc implementation requires temporary directory for evaluation evalTempDir = os.path.join(p.procDir, "eval_mAP_" + image_set) makeDirectory(evalTempDir) deleteAllFilesInDirectory(evalTempDir, None) # compute mAPs evaluate_net(net, imdb, evalTempDir, None, p.classifier, p.nmsThreshold, boUsePythonImpl=True) print ("DONE.") return True
def visualize_output_rois(testing=False): p = PARAMETERS.get_parameters_for_dataset() # no need to change these parameters boUseNonMaximaSurpression = True visualizationDir = os.path.join(p.resultsDir, "visualizations") cntkParsedOutputDir = os.path.join(p.cntkFilesDir, image_set + "_parsed") makeDirectory(p.resultsDir) makeDirectory(visualizationDir) # loop over all images and visualize imdb = p.imdbs[image_set] for imgIndex in range(0, imdb.num_images): imgPath = imdb.image_path_at(imgIndex) imgWidth, imgHeight = imWidthHeight(imgPath) # evaluate classifier for all rois labels, scores = nnPredict(imgIndex, cntkParsedOutputDir, p.cntk_nrRois, len(p.classes), None) # remove the zero-padded rois scores = scores[:len(imdb.roidb[imgIndex]['boxes'])] labels = labels[:len(imdb.roidb[imgIndex]['boxes'])] # perform non-maxima surpression. note that the detected classes in the image is not affected by this. nmsKeepIndices = [] if boUseNonMaximaSurpression: nmsKeepIndices = applyNonMaximaSuppression( p.nmsThreshold, labels, scores, imdb.roidb[imgIndex]['boxes']) print( "Non-maxima surpression kept {:4} of {:4} rois (nmsThreshold={})" .format(len(nmsKeepIndices), len(labels), p.nmsThreshold)) # visualize results imgDebug = visualizeResults(imgPath, labels, scores, imdb.roidb[imgIndex]['boxes'], p.cntk_padWidth, p.cntk_padHeight, p.classes, nmsKeepIndices, boDrawNegativeRois=True) if not testing: imshow(imgDebug, waitDuration=0, maxDim=800) # imwrite(imgDebug, visualizationDir + "/" + str(imgIndex) + os.path.basename(imgPath)) print("DONE.") return True
def visualize_output_rois(testing=False): p = PARAMETERS.get_parameters_for_dataset() # no need to change these parameters boUseNonMaximaSurpression = True visualizationDir = os.path.join(p.resultsDir, "visualizations") cntkParsedOutputDir = os.path.join(p.cntkFilesDir, image_set + "_parsed") makeDirectory(p.resultsDir) makeDirectory(visualizationDir) # loop over all images and visualize imdb = p.imdbs[image_set] for imgIndex in range(0, imdb.num_images): imgPath = imdb.image_path_at(imgIndex) imgWidth, imgHeight = imWidthHeight(imgPath) # evaluate classifier for all rois labels, scores = nnPredict(imgIndex, cntkParsedOutputDir, p.cntk_nrRois, len(p.classes), None) # remove the zero-padded rois scores = scores[:len(imdb.roidb[imgIndex]['boxes'])] labels = labels[:len(imdb.roidb[imgIndex]['boxes'])] # perform non-maxima surpression. note that the detected classes in the image is not affected by this. nmsKeepIndices = [] if boUseNonMaximaSurpression: nmsKeepIndices = applyNonMaximaSuppression(p.nmsThreshold, labels, scores, imdb.roidb[imgIndex]['boxes']) print ("Non-maxima surpression kept {:4} of {:4} rois (nmsThreshold={})".format(len(nmsKeepIndices), len(labels), p.nmsThreshold)) # visualize results imgDebug = visualizeResults(imgPath, labels, scores, imdb.roidb[imgIndex]['boxes'], p.cntk_padWidth, p.cntk_padHeight, p.classes, nmsKeepIndices, boDrawNegativeRois=True) if not testing: imshow(imgDebug, waitDuration=0, maxDim = 800) # imwrite(imgDebug, visualizationDir + "/" + str(imgIndex) + os.path.basename(imgPath)) print ("DONE.") return True
from fastRCNN.test import test_net from imdb_data import imdb_data from cntk_helpers import makeDirectory, parseCntkOutput, DummyNet image_set = 'test' classifier = "nn" cntkFilesDir = "/home/slapbot/my_side_projects/drone-detection/Detection/FastRCNN/proc/Drones_500/cntkFiles/" print("Parsing CNTK output for image set: " + image_set) cntkImgsListPath = cntkFilesDir + image_set + ".txt" outParsedDir = cntkFilesDir + image_set + "_parsed/" cntkOutputPath = cntkFilesDir + image_set + ".z" cntk_nrRois = 500 cntk_featureDimensions = {"nn": 3} makeDirectory(outParsedDir) parseCntkOutput(cntkImgsListPath, cntkOutputPath, outParsedDir, cntk_nrRois, cntk_featureDimensions[classifier], saveCompressed=True, skipCheck=True) classes = ( '__background__', # always index 0 'drone', 'dummy') datasetName = "Drones" imgDir = "/home/slapbot/my_side_projects/drone-detection/DataSets/Drones/" roiDir = "/home/slapbot/my_side_projects/drone-detection/Detection/FastRCNN/proc/Drones_500/rois/" imdbs = dict() # database provider of images and image annotations
def generate_input_rois(testing=False): p = PARAMETERS.get_parameters_for_dataset() if not p.datasetName.startswith("pascalVoc"): # init makeDirectory(p.roiDir) roi_minDim = p.roi_minDimRel * p.roi_maxImgDim roi_maxDim = p.roi_maxDimRel * p.roi_maxImgDim roi_minNrPixels = p.roi_minNrPixelsRel * p.roi_maxImgDim*p.roi_maxImgDim roi_maxNrPixels = p.roi_maxNrPixelsRel * p.roi_maxImgDim*p.roi_maxImgDim for subdir in subDirs: makeDirectory(os.path.join(p.roiDir, subdir)) imgFilenames = getFilesInDirectory(os.path.join(p.imgDir, subdir), ".jpg") # loop over all images for imgIndex,imgFilename in enumerate(imgFilenames): roiPath = "{}/{}/{}.roi.txt".format(p.roiDir, subdir, imgFilename[:-4]) # load image print (imgIndex, len(imgFilenames), subdir, imgFilename) tstart = datetime.datetime.now() imgPath = os.path.join(p.imgDir, subdir, imgFilename) imgOrig = imread(imgPath) if imWidth(imgPath) > imHeight(imgPath): print (imWidth(imgPath) , imHeight(imgPath)) # get rois if boAddSelectiveSearchROIs: print ("Calling selective search..") rects, img, scale = getSelectiveSearchRois(imgOrig, p.ss_scale, p.ss_sigma, p.ss_minSize, p.roi_maxImgDim) #interpolation=cv2.INTER_AREA print (" Number of rois detected using selective search: " + str(len(rects))) else: rects = [] img, scale = imresizeMaxDim(imgOrig, p.roi_maxImgDim, boUpscale=True, interpolation=cv2.INTER_AREA) imgWidth, imgHeight = imArrayWidthHeight(img) # add grid rois if boAddRoisOnGrid: rectsGrid = getGridRois(imgWidth, imgHeight, p.grid_nrScales, p.grid_aspectRatios) print (" Number of rois on grid added: " + str(len(rectsGrid))) rects += rectsGrid # run filter print (" Number of rectangles before filtering = " + str(len(rects))) rois = filterRois(rects, imgWidth, imgHeight, roi_minNrPixels, roi_maxNrPixels, roi_minDim, roi_maxDim, p.roi_maxAspectRatio) if len(rois) == 0: #make sure at least one roi returned per image rois = [[5, 5, imgWidth-5, imgHeight-5]] print (" Number of rectangles after filtering = " + str(len(rois))) # scale up to original size and save to disk # note: each rectangle is in original image format with [x,y,x2,y2] rois = np.int32(np.array(rois) / scale) assert (np.min(rois) >= 0) assert (np.max(rois[:, [0,2]]) < imArrayWidth(imgOrig)) assert (np.max(rois[:, [1,3]]) < imArrayHeight(imgOrig)) np.savetxt(roiPath, rois, fmt='%d') print (" Time [ms]: " + str((datetime.datetime.now() - tstart).total_seconds() * 1000)) # clear imdb cache and other files if os.path.exists(p.cntkFilesDir): assert(p.cntkFilesDir.endswith("cntkFiles")) if not testing: userInput = input('--> INPUT: Press "y" to delete directory ' + p.cntkFilesDir + ": ") if userInput.lower() not in ['y', 'yes']: print ("User input is %s: exiting now." % userInput) exit(-1) shutil.rmtree(p.cntkFilesDir) time.sleep(0.1) # avoid access problems # create cntk representation for each image for image_set in image_sets: imdb = p.imdbs[image_set] print ("Number of images in set {} = {}".format(image_set, imdb.num_images)) makeDirectory(p.cntkFilesDir) # open files for writing cntkImgsPath, cntkRoiCoordsPath, cntkRoiLabelsPath, nrRoisPath = getCntkInputPaths(p.cntkFilesDir, image_set) with open(nrRoisPath, 'w') as nrRoisFile, \ open(cntkImgsPath, 'w') as cntkImgsFile, \ open(cntkRoiCoordsPath, 'w') as cntkRoiCoordsFile, \ open(cntkRoiLabelsPath, 'w') as cntkRoiLabelsFile: # for each image, transform rois etc to cntk format for imgIndex in range(0, imdb.num_images): if imgIndex % 50 == 0: print ("Processing image set '{}', image {} of {}".format(image_set, imgIndex, imdb.num_images)) currBoxes = imdb.roidb[imgIndex]['boxes'] currGtOverlaps = imdb.roidb[imgIndex]['gt_overlaps'] imgPath = imdb.image_path_at(imgIndex) imgWidth, imgHeight = imWidthHeight(imgPath) # all rois need to be scaled + padded to cntk input image size targetw, targeth, w_offset, h_offset, scale = roiTransformPadScaleParams(imgWidth, imgHeight, p.cntk_padWidth, p.cntk_padHeight) boxesStr = "" labelsStr = "" nrBoxes = len(currBoxes) for boxIndex, box in enumerate(currBoxes): rect = roiTransformPadScale(box, w_offset, h_offset, scale) boxesStr += getCntkRoiCoordsLine(rect, p.cntk_padWidth, p.cntk_padHeight) labelsStr += getCntkRoiLabelsLine(currGtOverlaps[boxIndex, :].toarray()[0], p.train_posOverlapThres, p.nrClasses) # if less than e.g. 2000 rois per image, then fill in the rest using 'zero-padding'. boxesStr, labelsStr = cntkPadInputs(nrBoxes, p.cntk_nrRois, p.nrClasses, boxesStr, labelsStr) # update cntk data nrRoisFile.write("{}\n".format(nrBoxes)) cntkImgsFile.write("{}\t{}\t0\n".format(imgIndex, imgPath)) cntkRoiCoordsFile.write("{} |rois{}\n".format(imgIndex, boxesStr)) cntkRoiLabelsFile.write("{} |roiLabels{}\n".format(imgIndex, labelsStr)) print ("DONE.") return True