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
Beispiel #2
0
def generate_rois_visualization(testing=False):
    p = PARAMETERS.get_parameters_for_dataset()

    print("Load ROI co-ordinates and labels")
    cntkImgsPath, cntkRoiCoordsPath, cntkRoiLabelsPath, nrRoisPath = getCntkInputPaths(
        p.cntkFilesDir, image_set)
    imgPaths = getColumn(readTable(cntkImgsPath), 1)
    nrRealRois = [int(s) for s in readFile(nrRoisPath)]
    roiAllLabels = readCntkRoiLabels(cntkRoiLabelsPath, p.cntk_nrRois,
                                     len(p.classes), parseNrImages)
    if parseNrImages:
        imgPaths = imgPaths[:parseNrImages]
        nrRealRois = nrRealRois[:parseNrImages]
        roiAllLabels = roiAllLabels[:parseNrImages]
    roiAllCoords = readCntkRoiCoordinates(imgPaths, cntkRoiCoordsPath,
                                          p.cntk_nrRois, p.cntk_padWidth,
                                          p.cntk_padHeight, parseNrImages)
    assert (len(imgPaths) == len(roiAllCoords) == len(roiAllLabels) ==
            len(nrRealRois))

    # loop over all images and visualize
    for imgIndex, imgPath in enumerate(imgPaths):
        print("Visualizing image %d at %s..." % (imgIndex, imgPath))
        roiCoords = roiAllCoords[imgIndex][:nrRealRois[imgIndex]]
        roiLabels = roiAllLabels[imgIndex][:nrRealRois[imgIndex]]

        # perform non-maxima surpression. note that the detected classes in the image is not affected by this.
        nmsKeepIndices = []
        if boUseNonMaximaSurpression:
            imgWidth, imgHeight = imWidthHeight(imgPath)
            nmsKeepIndices = applyNonMaximaSuppression(nmsThreshold, roiLabels,
                                                       [0] * len(roiLabels),
                                                       roiCoords)
            print(
                "Non-maxima surpression kept {} of {} rois (nmsThreshold={})".
                format(len(nmsKeepIndices), len(roiLabels), nmsThreshold))

        # visualize results
        imgDebug = visualizeResults(imgPath,
                                    roiLabels,
                                    None,
                                    roiCoords,
                                    p.cntk_padWidth,
                                    p.cntk_padHeight,
                                    p.classes,
                                    nmsKeepIndices,
                                    boDrawNegativeRois=True)
        if not testing:
            imshow(imgDebug, waitDuration=0, maxDim=800)

    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
Beispiel #4
0
def run_fastrcnn_with_config_file(cntkBuildPath="cntk"):
    ####################################
    # Parameters
    ####################################
    p = PARAMETERS.get_parameters_for_dataset()
    cntkCmdStrPattern = cntkBuildPath + " configFile={0}/fastrcnn.cntk currentDirectory={0} {1}"

    # cntk arguments
    NumLabels = p.nrClasses

    NumTrainROIs = p.cntk_nrRois
    TrainROIDim = p.cntk_nrRois * 4
    TrainROILabelDim = p.cntk_nrRois * p.nrClasses

    NumTestROIs = p.cntk_nrRois
    TestROIDim = p.cntk_nrRois * 4
    TestROILabelDim = p.cntk_nrRois * p.nrClasses

    cntk_args = "NumLabels={} NumTrainROIs={}".format(NumLabels, NumTrainROIs)
    cntk_args += " TrainROIDim={} TrainROILabelDim={}".format(
        TrainROIDim, TrainROILabelDim)
    cntk_args += " NumTestROIs={}".format(NumTestROIs)
    cntk_args += " TestROIDim={} TestROILabelDim={}".format(
        TestROIDim, TestROILabelDim)

    # copy config file
    shutil.copy(os.path.join(p.cntkTemplateDir, "fastrcnn.cntk"),
                p.cntkFilesDir)
    # run cntk
    tstart = datetime.datetime.now()

    cmdStr = cntkCmdStrPattern.format(p.cntkFilesDir, cntk_args)
    print(cmdStr)
    pid = subprocess.Popen(cmdStr.split(" "), cwd=p.cntkFilesDir)
    pid.wait()
    print("Time running cntk [s]: " +
          str((datetime.datetime.now() - tstart).total_seconds()))

    # delete intermediate model files written during cntk training
    modelDir = os.path.join(p.cntkFilesDir, "Output")
    filenames = getFilesInDirectory(modelDir, postfix=None)
    for filename in filenames:
        if "Fast-RCNN.model." in filename:
            os.remove(os.path.join(modelDir, filename))
    assert pid.returncode == 0, "ERROR: cntk ended with exit code {}".format(
        pid.returncode)

    print("DONE.")
    return True
Beispiel #5
0
def evaluate_rois():
    p = PARAMETERS.get_parameters_for_dataset()
    overlaps = []
    roiCounts = []
    for subdir in subdirs:
        imgFilenames = getFilesInDirectory(os.path.join(p.imgDir, subdir),
                                           ".jpg")

        # loop over all iamges
        for imgIndex, imgFilename in enumerate(imgFilenames):
            if imgIndex % 20 == 0:
                print("Processing subdir '{}', image {} of {}".format(
                    subdir, imgIndex, len(imgFilenames)))
            # load ground truth
            imgPath = os.path.join(p.imgDir, subdir, imgFilename)
            imgWidth, imgHeight = imWidthHeight(imgPath)
            gtBoxes, gtLabels = readGtAnnotation(imgPath)
            gtBoxes = [Bbox(*rect) for rect in gtBoxes]

            # load rois and compute scale
            rois = readRois(p.roiDir, subdir, imgFilename)
            rois = [Bbox(*roi) for roi in rois]
            roiCounts.append(len(rois))

            # for each ground truth, compute if it is covered by an roi
            maxOverlap = -1
            for gtIndex, (gtLabel, gtBox) in enumerate(zip(gtLabels, gtBoxes)):
                assert (gtBox.max() <= max(imgWidth, imgHeight)
                        and gtBox.max() >= 0)
                gtLabel = gtLabel.decode('utf-8')
                if gtLabel in p.classes[1:]:
                    for roi in rois:
                        assert (roi.max() <= max(imgWidth, imgHeight)
                                and roi.max() >= 0)
                        overlap = bboxComputeOverlapVoc(gtBox, roi)
                        maxOverlap = max(maxOverlap, overlap)
                overlaps.append(maxOverlap)
    print("Average number of rois per image " +
          str(1.0 * sum(roiCounts) / len(overlaps)))

    # compute recall at different overlaps
    overlaps = np.array(overlaps, np.float32)
    for overlapThreshold in np.linspace(0, 1, 11):
        recall = 1.0 * sum(overlaps >= overlapThreshold) / len(overlaps)
        print("At threshold {:.2f}: recall = {:2.2f}".format(
            overlapThreshold, recall))
    return True
Beispiel #6
0
def run_fastrcnn_with_config_file(cntkBuildPath="cntk"):
    ####################################
    # Parameters
    ####################################
    p = PARAMETERS.get_parameters_for_dataset()
    cntkCmdStrPattern = cntkBuildPath + " configFile={0}/fastrcnn.cntk currentDirectory={0} {1}"

    # cntk arguments
    NumLabels = p.nrClasses

    NumTrainROIs = p.cntk_nrRois
    TrainROIDim = p.cntk_nrRois * 4
    TrainROILabelDim = p.cntk_nrRois * p.nrClasses

    NumTestROIs = p.cntk_nrRois
    TestROIDim = p.cntk_nrRois * 4
    TestROILabelDim = p.cntk_nrRois * p.nrClasses

    cntk_args = "NumLabels={} NumTrainROIs={}".format(NumLabels, NumTrainROIs)
    cntk_args += " TrainROIDim={} TrainROILabelDim={}".format(TrainROIDim, TrainROILabelDim)
    cntk_args += " NumTestROIs={}".format(NumTestROIs)
    cntk_args += " TestROIDim={} TestROILabelDim={}".format(TestROIDim, TestROILabelDim)

    # copy config file
    shutil.copy(os.path.join(p.cntkTemplateDir, "fastrcnn.cntk"), p.cntkFilesDir)
    # run cntk
    tstart = datetime.datetime.now()

    cmdStr = cntkCmdStrPattern.format(p.cntkFilesDir, cntk_args)
    print (cmdStr)
    pid = subprocess.Popen(cmdStr.split(" "), cwd=p.cntkFilesDir)
    pid.wait()
    print ("Time running cntk [s]: " + str((datetime.datetime.now() - tstart).total_seconds()))

    # delete intermediate model files written during cntk training
    modelDir = os.path.join(p.cntkFilesDir , "Output")
    filenames = getFilesInDirectory(modelDir, postfix = None)
    for filename in filenames:
        if "Fast-RCNN.model." in filename:
            os.remove(os.path.join(modelDir, filename))
    assert pid.returncode == 0, "ERROR: cntk ended with exit code {}".format(pid.returncode)

    print ("DONE.")
    return True
Beispiel #7
0
def evaluate_rois():
    p = PARAMETERS.get_parameters_for_dataset()
    overlaps = []
    roiCounts = []
    for subdir in subdirs:
        imgFilenames = getFilesInDirectory(os.path.join(p.imgDir, subdir), ".jpg")

        # loop over all iamges
        for imgIndex,imgFilename in enumerate(imgFilenames):
            if imgIndex % 20 == 0:
                print ("Processing subdir '{}', image {} of {}".format(subdir, imgIndex, len(imgFilenames)))
            # load ground truth
            imgPath = os.path.join(p.imgDir, subdir, imgFilename)
            imgWidth, imgHeight = imWidthHeight(imgPath)
            gtBoxes, gtLabels = readGtAnnotation(imgPath)
            gtBoxes = [Bbox(*rect) for rect in gtBoxes]

            # load rois and compute scale
            rois = readRois(p.roiDir, subdir, imgFilename)
            rois = [Bbox(*roi) for roi in rois]
            roiCounts.append(len(rois))

            # for each ground truth, compute if it is covered by an roi
            maxOverlap = -1
            for gtIndex, (gtLabel, gtBox) in enumerate(zip(gtLabels,gtBoxes)):
                assert (gtBox.max() <= max(imgWidth, imgHeight) and gtBox.max() >= 0)
                gtLabel = gtLabel.decode('utf-8')
                if gtLabel in p.classes[1:]:
                    for roi in rois:
                        assert (roi.max() <= max(imgWidth, imgHeight) and roi.max() >= 0)
                        overlap = bboxComputeOverlapVoc(gtBox, roi)
                        maxOverlap = max(maxOverlap, overlap)
                overlaps.append(maxOverlap)
    print ("Average number of rois per image " + str(1.0 * sum(roiCounts) / len(overlaps)))

    # compute recall at different overlaps
    overlaps = np.array(overlaps, np.float32)
    for overlapThreshold in np.linspace(0,1,11):
        recall = 1.0 * sum(overlaps >= overlapThreshold) / len(overlaps)
        print ("At threshold {:.2f}: recall = {:2.2f}".format(overlapThreshold, recall))
    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 generate_rois_visualization(testing=False):
    p = PARAMETERS.get_parameters_for_dataset()

    print ("Load ROI co-ordinates and labels")
    cntkImgsPath, cntkRoiCoordsPath, cntkRoiLabelsPath, nrRoisPath = getCntkInputPaths(p.cntkFilesDir, image_set)
    imgPaths = getColumn(readTable(cntkImgsPath), 1)
    nrRealRois = [int(s) for s in readFile(nrRoisPath)]
    roiAllLabels = readCntkRoiLabels(cntkRoiLabelsPath, p.cntk_nrRois, len(p.classes), parseNrImages)
    if parseNrImages:
        imgPaths = imgPaths[:parseNrImages]
        nrRealRois = nrRealRois[:parseNrImages]
        roiAllLabels = roiAllLabels[:parseNrImages]
    roiAllCoords = readCntkRoiCoordinates(imgPaths, cntkRoiCoordsPath, p.cntk_nrRois, p.cntk_padWidth, p.cntk_padHeight,
                                          parseNrImages)
    assert (len(imgPaths) == len(roiAllCoords) == len(roiAllLabels) == len(nrRealRois))

    # loop over all images and visualize
    for imgIndex, imgPath in enumerate(imgPaths):
        print ("Visualizing image %d at %s..." % (imgIndex, imgPath))
        roiCoords = roiAllCoords[imgIndex][:nrRealRois[imgIndex]]
        roiLabels = roiAllLabels[imgIndex][:nrRealRois[imgIndex]]

        # perform non-maxima surpression. note that the detected classes in the image is not affected by this.
        nmsKeepIndices = []
        if boUseNonMaximaSurpression:
            imgWidth, imgHeight = imWidthHeight(imgPath)
            nmsKeepIndices = applyNonMaximaSuppression(nmsThreshold, roiLabels, [0] * len(roiLabels), roiCoords)
            print ("Non-maxima surpression kept {} of {} rois (nmsThreshold={})".format(len(nmsKeepIndices),
                                                                                       len(roiLabels), nmsThreshold))

        # visualize results
        imgDebug = visualizeResults(imgPath, roiLabels, None, roiCoords, p.cntk_padWidth, p.cntk_padHeight,
                                    p.classes, nmsKeepIndices, boDrawNegativeRois=True)
        if not testing:
            imshow(imgDebug, waitDuration=0, maxDim=800)

    print ("DONE.")
    return True
from numpy import std
from numpy import amax
from numpy import abs

# multi-lstm
MULTI_LSTM = False
# pridict only Fs
singleFs = False
# drop the 1:predict-1 output
DROP = True
# data path
TrainData_Path = './data/prediction data-marina/vesselfollow'
TestData_Path = './data/prediction data-marina/vesselfollow-test'
# load dataset
# data = [RoVel1, RoVel2, RoVel3, RoVel4, RoVel5, depth, Fs1, Fs2]
TrainData = PARAMETERS.loaddata(TrainData_Path)
TestData = PARAMETERS.loaddata(TestData_Path)
inputdatafeature = 8
NUM_FEATURE = 2
# define training process
# Hyperparameters
Lr_initial = 0.001
BATCH_SIZE = 100
NUM_EPOCH = [10, 20, 40]
NUM_LSTM = [50, 60, 80]
NUM_Dense = [50, 60, 80]
NUM_LSTM2 = 0
# Train data parameters
HISTORY = [50, 60, 80]
PRIDICT_initial = 20
Beispiel #11
0
###############################################################
###############################################################
abs_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(abs_path, "..", ".."))

# file and stream names
train_map_filename = 'train.txt'
test_map_filename = 'test.txt'
rois_filename_postfix = '.rois.txt'
roilabels_filename_postfix = '.roilabels.txt'
features_stream_name = 'features'
roi_stream_name = 'rois'
label_stream_name = 'roiLabels'

p = PARAMETERS.get_parameters_for_dataset()
base_path = p.cntkFilesDir
num_channels = 3
image_height = p.cntk_padHeight
image_width = p.cntk_padWidth
num_classes = p.nrClasses
num_rois = p.cntk_nrRois
epoch_size = p.cntk_num_train_images
num_test_images = p.cntk_num_test_images
mb_size = p.cntk_mb_size
max_epochs = p.cntk_max_epochs
distributed_flg = p.distributed_flg
num_quantization_bits = p.num_quantization_bits
warm_up = p.warm_up
momentum_per_sample = p.cntk_momentum_per_sample
    values0 = dataset.values
    values0 = array([values0[:, 8], values0[:, 46]]).T

#values0 = read_csv(smoothedtest,index_col=None, header=0) # smoothed data = [Ft, Fs, D]
#values0 = values0.values
# normalize features
#scaler = MinMaxScaler(feature_range=(0, 1))
#if DROP == False:
#  values0 = tile(values0, (1,predict))
#  scaled = scaler.fit_transform(values0)
# scaled = scaled[:, :NUM_FEATURE]
#else:
#  scaled = scaler.fit_transform(values0)
#scaled = values0
# frame as supervised learning
reframed = PARAMETERS.series_to_supervised(values0, delay, predict)
# drop columns we don't want to predict
if DROP == True:
    reframed.drop(reframed.columns[delay * NUM_FEATURE:-NUM_FEATURE],
                  axis=1,
                  inplace=True)

if singleFs == True:
    reframed.drop(reframed.columns[-3], axis=1, inplace=True)
    reframed.drop(reframed.columns[-1], axis=1, inplace=True)
values = reframed.values
# split into input and outputs
groundtruth_X, groundtruth_y = values[:, :delay *
                                      NUM_FEATURE], values[:, delay *
                                                           NUM_FEATURE:]
# reshape input to be 3D [samples, timesteps, features]
delay = PARAMETERS.DELAY
predict = PARAMETERS.PREDICT

# load dataset
#values0 = PARAMETERS.loaddata(TRAINDATA_PATH)
values0 = read_csv(vesseldata,index_col=None, header=0) # vessel data = [ Fs, D]
values0 = values0.values
preriodic = ArtificialSignal.PeriodicSingnal(200)
# normalize features
#scaler = MinMaxScaler(feature_range=(0, 1))
#scaled = scaler.fit_transform(values0)
scaled = values0
if PeriodicTest == True:
    scaled = preriodic
# frame as supervised learning
reframed = PARAMETERS.series_to_supervised(scaled, delay, predict)
# drop columns we don't want to predict
if DROP == True:
    reframed.drop(reframed.columns[-predict*NUM_FEATURE:-NUM_FEATURE], axis=1, inplace=True)
    predict = 1
if singleFs == True:
#drop the Ft, D in the output
    reframed.drop(reframed.columns[-3], axis=1, inplace=True)
    reframed.drop(reframed.columns[-1], axis=1, inplace=True)
# split into train and test sets
values = reframed.values
train = values[:int(0.7*values.shape[0]), :]
test = values[int(0.7*values.shape[0]):, :]
# split into input and outputs
train_X0, train_y = train[:, :delay*NUM_FEATURE], train[:, delay*NUM_FEATURE:]
test_X0, test_y = test[:, :delay*NUM_FEATURE], test[:, delay*NUM_FEATURE:]
        "history\t"
        "epoch\t"
        "initialLR\t"
        "rmseFs\t"
        "std_Fs\t"
        "maxe_Fs\t")
for n in range(0, len(NUM_LSTM), 1):
    for d in range(0, len(NUM_Dense), 1):
        for h in range(0, len(HISTORY), 1):
            for e in range(0, len(NUM_EPOCH), 1):
                print('LSTM:', NUM_LSTM[n], 'Dense:', NUM_Dense[d], 'History:',
                      HISTORY[h], 'Epoch', NUM_EPOCH[e])
                count += 1
                PRIDICT = PRIDICT_initial
                # frame as supervised learning
                reframed = PARAMETERS.series_to_supervised(
                    scaled, HISTORY[h], PRIDICT)
                # drop columns we do not want to predict
                if DROP == True:
                    reframed.drop(reframed.columns[-PRIDICT *
                                                   NUM_FEATURE:-NUM_FEATURE],
                                  axis=1,
                                  inplace=True)
                    PRIDICT = 1
                # split into train and test sets
                values = reframed.values
                # for k in range(0, 5, 1):
                train = values[:int(0.7 * values.shape[0]), :]
                test = values[int(0.7 * values.shape[0]):, :]
                # train = values
                # test = values
                # split into input and outputs
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
Beispiel #16
0
###############################################################
###############################################################
abs_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(abs_path, "..", ".."))

# file and stream names
train_map_filename = 'train.txt'
test_map_filename = 'test.txt'
rois_filename_postfix = '.rois.txt'
roilabels_filename_postfix = '.roilabels.txt'
features_stream_name = 'features'
roi_stream_name = 'rois'
label_stream_name = 'roiLabels'

p = PARAMETERS.get_parameters_for_dataset()
base_path = p.cntkFilesDir
num_channels = 3
image_height = p.cntk_padHeight
image_width = p.cntk_padWidth
num_classes = p.nrClasses
num_rois = p.cntk_nrRois
epoch_size = p.cntk_num_train_images
num_test_images = p.cntk_num_test_images
mb_size = p.cntk_mb_size
max_epochs = p.cntk_max_epochs
momentum_time_constant = p.cntk_momentum_time_constant

# model specific variables (only AlexNet for now)
base_model = "AlexNet"
if base_model == "AlexNet":
Beispiel #17
0
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