示例#1
0
def compareTruthToEpipolar(configFile, seq, cam, idxList=None, type='both'):

    config = ThesisConfig(configFile)

    # get epipolar data
    epiFilesDict = config.kittiPrepared['epipolar']
    epiFile = config.getInputFiles(epiFilesDict, seq, cam)
    with h5py.File(epiFile, 'r') as f:
        epi_rot_xyz = np.array(f['epi_rot_xyz'])
        epi_trans_xyz = np.array(f['epi_trans_xyz'])

    # get truth data
    truthFilesDict = config.kittiPrepared['truth']
    truthFile = config.getInputFiles(truthFilesDict, seq)
    with h5py.File(truthFile, 'r') as f:
        true_rot_xyz = np.array(f['rot_xyz'])
        true_trans_xyz = np.array(f['trans_xyz'])

    if idxList is None:
        idxList = range(len(true_rot_xyz))

    for img_idx in idxList:
        if type == 'rot' or type == 'both':
            epi_rot = epi_rot_xyz[img_idx] * 180 / np.pi
            true_rot = true_rot_xyz[img_idx] * 180 / np.pi
            print('Epi Rot  [%d]: [%12f, %12f, %12f]' %
                  (img_idx, epi_rot[0], epi_rot[1], epi_rot[2]))
            print('True Rot [%d]: [%12f, %12f, %12f]' %
                  (img_idx, true_rot[0], true_rot[1], true_rot[2]))
        if type == 'trans' or type == 'both':
            epi_trans = epi_trans_xyz[img_idx]
            true_trans = true_trans_xyz[img_idx]
            true_trans = true_trans / np.sqrt(np.sum(
                true_trans**2))  # Convert to unit vector
            print('Epi Trans  [%d]: [%12f, %12f, %12f]' %
                  (img_idx, epi_trans[0], epi_trans[1], epi_trans[2]))
            print('True Trans [%d]: [%12f, %12f, %12f]' %
                  (img_idx, true_trans[0], true_trans[1], true_trans[2]))
# if len(sys.argv)>1:
#     evalType = sys.argv[1]
# if evalType is None:
evalType = 'val'

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
logging.getLogger('tensorflow').setLevel(logging.FATAL)

# if True:
#     test = tests[0]
for test in tests:
    configFile = os.path.join('exp_configs', test + '.yaml')
    config = ThesisConfig(configFile)

    checkpointFilesDict = config.trainPaths['checkpoint']
    checkpointFiles = config.getInputFiles(checkpointFilesDict)

    print('Getting all epoch predictions for %s' % (test))
    totalEpochs = len(checkpointFiles)
    for epoch in range(1,totalEpochs+1):
        t = time.time()
        K.clear_session()
        print('  epoch %03d of %03d' % (epoch, totalEpochs))
        getPredictions(config, evalType, epochNum=epoch, batchSize=1, saveData=True);
        elapsed = time.time() - t
        print('    time: %s' % (elapsed))




示例#3
0
    'recalcStandardImages']
showFigures = config.expKittiParms['prepared']['showPlots']
showProgress = config.expKittiParms['prepared']['showProg']

# Files
origImageFilesDict = config.kittiOriginal['cams']
calFilesDict = config.kittiOriginal['cal']
standardImageFilesDict = config.kittiPrepared['standardImages']

# Get normalized corner points for each sequence
standardDict = {}
for cam in usedCams:
    for seqStr in kittiSeqs:
        seq = int(seqStr)

        origImageNames = config.getInputFiles(origImageFilesDict, seq, cam)
        if (origImageNames is None):
            continue

        calFile = config.getInputFiles(calFilesDict, seq)
        if (calFile is None):
            continue

        image = cv.imread(origImageNames[0])
        imageShape = image.shape[:2]

        cameraCalMat = getCalMat(calFile, cam)
        standardDict[seqStr] = getCornerStandard(imageShape, cameraCalMat)

# TODO: SAVE NORM DICT NOT FIGURE
# TODO: GENERATE FIGURE IN DIFFERENT FILE
示例#4
0
    'reapplyDataNorm']

# Files
splitFilesDict = config.kittiPrepared['split']
truthFilesDict = config.kittiPrepared['truth']
imuFilesDict = config.kittiPrepared['imu']
epiFilesDict = config.kittiPrepared['epipolar']
normParmsFilesDict = config.kittiNormalized['normParms']
normDataFilesDict = config.kittiNormalized['normData']
normEpiFilesDict = config.kittiNormalized['normEpi']

print()
print('v Normalizing Data')

# Read in Normalization Parameters
normParmsFile = config.getInputFiles(normParmsFilesDict)

truth_rot_parms = loadNormParms(normParmsFile, 'rot_xyz')
truth_xyz_parms = loadNormParms(normParmsFile, 'trans_xyz')
truth_polar_parms = loadNormParms(normParmsFile, 'trans_rtp')
imu_rot_parms = loadNormParms(normParmsFile, 'noisy_rot_xyz')
epi_rot_parms = loadNormParms(normParmsFile, 'epi_rot_xyz')
epi_trans_parms = loadNormParms(normParmsFile, 'epi_trans_xyz')

# Outputs

# Truth data - Rotation
truth_rot = getH5pyData(config, truthFilesDict, usedSeqs, 'rot_xyz', 3)
norm_truth_rot = applyNorm(truth_rot, truth_rot_parms)

# Truth data - Cartesian Translation
示例#5
0
kittiSeqs = config.kittiSeqs
usedCams = config.usedCams
recalcTruthData = config.expKittiParms['prepared']['runPrep']['recalcTruth']

# Files
poseFilesDict = config.kittiOriginal['truth']
truthFilesDict = config.kittiPrepared['truth']

print()
print('v Creating Truth Data')
for cam in usedCams:
    for seqStr in kittiSeqs:
        seq = int(seqStr)
        print('%sReading Camera %s, Sequence %02d' % (' ' * 2, cam, seq))

        poseFile = config.getInputFiles(poseFilesDict, seq)
        if (poseFile is None):
            continue

        truthFile = config.getOutputFiles(truthFilesDict, recalcTruthData, seq)
        if (truthFile is None):
            continue

        # Get true R and t from pose files

        #   Row i represents the ith pose of the left camera coordinate
        # system (z pointing forwards) via a 3x4 transformation matrix.
        # The matricies take a point in the ith coordinate system and project
        # it to the 0th coordinate system. The translational part corresponds
        # to the pose of the left camera in the ith frame with respect to the
        # 0th frame.
sampleRate = config.thesisKittiParms['sampleRate']  # Hz
arwError = config.expKittiParms['imu'][
    'arwError']  # deg/sqrt(hr) -- Based on Novatel UIMU-HG1700 # TODO: Cite novatel datasheet
angular_std = np.sqrt(
    arwError**2 * 1 / 3600 * 1 / sampleRate) * np.pi / 180  # radians
#                sqrt(  deg^2/hr   * hr/sec *      sec     ) * radians/deg
# VALUE IS APPROXIMATELY 0.002635 deg

print()
print('v Creating IMU Data')
for cam in usedCams:
    for seqStr in kittiSeqs:
        seq = int(seqStr)
        print('%sReading Camera %s, Sequence %02d' % (' ' * 2, cam, seq))

        truthFile = config.getInputFiles(truthFilesDict, seq)
        if (truthFile is None):
            continue

        imuFile = config.getOutputFiles(imuFilesDict, recalcIMUData, seq)
        if (imuFile is None):
            continue

        # Read in rotation truth poses
        with h5py.File(truthFile, 'r') as f:
            rot_xyz = np.array(f['rot_xyz'])  # radians

        # Add noise to rotation piece
        noise_xyz = np.random.randn(rot_xyz.shape[0], rot_xyz.shape[1])
        noisy_rot_xyz = rot_xyz + angular_std * noise_xyz
示例#7
0
standardImageFilesDict = config.kittiPrepared['standardImages']
normImageFilesDict = config.kittiNormalized['normImages']





print()
print('v Downsampling Images and Normalizing Pixels')

for cam in usedCams:
    for seqStr in kittiSeqs:
        seq = int(seqStr)
        print('%sReading Camera %s, Sequence %02d' % (' ' * 2, cam, seq))

        standardImages = config.getInputFiles(standardImageFilesDict, seq, cam)
        if (standardImages is None):
            continue

        normImages = config.getOutputFiles(normImageFilesDict, recalcNormImages, seq, cam)
        if (normImages is None):
            continue

        normImageFolder = config.getFolderRef(normImages)

        numImages = len(standardImages)
        for idx, standardImageName in enumerate(standardImages):

            if showProgress:
                percentComplete = int(idx/numImages*100)
                if divmod(idx,300)[1]==0:
示例#8
0
elif compareData == 'epipolar':
    test = 'CNN_test_13'
    configFile = os.path.join('exp_configs', test + '.yaml')
    config = ThesisConfig(configFile)

    # Parameters
    numOutputs = config.modelParms['numOutputs']

    # GET TRUTH DATA
    truthFilesDict = config.kittiPrepared['truth']

    true_xyz = np.empty((0, 6))
    for cam in config.usedCams:
        for seq in config.usedSeqs:
            truthFile = config.getInputFiles(truthFilesDict, seq)
            with h5py.File(truthFile, 'r') as f:
                true_rot_xyz = np.array(f['rot_xyz'])
                true_trans_xyz = np.array(f['trans_xyz'])
            true_trans_xyz_norm = true_trans_xyz / np.array(
                [np.sqrt(np.sum(true_trans_xyz**2, axis=1))]).T
            true = np.concatenate((true_rot_xyz, true_trans_xyz_norm), axis=1)
            true_xyz = np.append(true_xyz, true, axis=0)

    y_true_real = true_xyz

    # GET EPIPOLAR DATA
    epiFilesDict = config.kittiPrepared['epipolar']

    epi_xyz = np.empty((0, 6))
    epi_xyz_masked = np.empty((0, 6))
示例#9
0
def loadPredictions(tests, eType):
    errors_list = []
    preds_list = []
    truth_list = []
    filetruthrots_list = []

    for i, test in enumerate(tests):
        # if True:
        #     test = tests[0]
        configFile = 'exp_configs/%s.yaml' % test
        config = ThesisConfig(configFile)

        # Parameters
        name = config.expName
        numOutputs = config.modelParms['numOutputs']

        # Get Files
        evalFilesDict = config.resultPaths['evaluations']
        figFilesDict = config.resultPaths['figures']
        truthFilesDict = config.kittiPrepared['truth']

        figFolder = config.getFolderRef(
            config.getOutputFiles(figFilesDict, True))

        # Get Predictions Save File
        evalFolder = ''
        for pathSection in evalFilesDict['dir']:
            evalFolder = os.path.join(evalFolder, pathSection)
        predictionsFile = os.path.join(evalFolder, eType + '_predictions.hdf5')

        if os.path.exists(predictionsFile):

            # Load Predictions
            y_pred_real, y_true_real, evalType, min_val_epoch, min_val_loss = loadPredFile(
                predictionsFile)

            print('Min Validation Loss: %s, Epoch %s' %
                  (min_val_loss, min_val_epoch))

            # get test idxs
            # numTested = y_true_real.shape[0]
            splitFilesDict = config.kittiPrepared['split']
            splitFile = config.getInputFiles(splitFilesDict)
            with h5py.File(splitFile, 'r') as f:
                if evalType == 'test':
                    turnIdxs = np.array(f['testTurnIdxs'])
                    nonTurnIdxs = np.array(f['testNonTurnIdxs'])
                elif evalType == 'val':
                    turnIdxs = np.array(f['valTurnIdxs'])
                    nonTurnIdxs = np.array(f['valNonTurnIdxs'])
            idxs = np.sort(np.concatenate(
                (turnIdxs, nonTurnIdxs)))  #[:numTested]

            truthData = np.empty((0, 3))
            for seq in config.usedSeqs:
                truthFile = config.getInputFiles(truthFilesDict, seq)
                with h5py.File(truthFile, 'r') as f:
                    rot_xyz = np.array(f['rot_xyz'])
                truthData = np.append(truthData, rot_xyz, axis=0)
            file_truth_rots = truthData[idxs, :]

            # Calculate average loss in each direction
            errors = y_true_real - y_pred_real

            errors_list.append(errors)
            preds_list.append(y_pred_real)
            truth_list.append(y_true_real)
            filetruthrots_list.append(file_truth_rots)

        else:
            print('predictions file %s does not exist' % predictionsFile)

    return (errors_list, preds_list, truth_list, filetruthrots_list)
示例#10
0
splitFilesDict = config.kittiPrepared['split']

print()
print('v Splitting Dataset')

splitFile = config.getOutputFiles(splitFilesDict)
if splitFile is None:
    print('^ Dataset Splitting Complete')
    print()
    sys.exit()

# Check if file exists
# Get Sequences Counts and Turning Indexes
truthFilesList = []
for seq in usedSeqs:
    truthFilesList.append(config.getInputFiles(truthFilesDict, seq))

idxCvt = IndexConverter(truthFilesList, turnThresh_rad)

# SPLIT TURNING INDEXES

# Convert seq turning indexes to absolute indexes
turnAbsIdxs = idxCvt.cvtToIdxs(idxCvt.turnIdxs)

# Determine total number of image pair indexes for turning
numTurn = len(turnAbsIdxs)

# Determine number of image pairs for train, validation, and test for turning
numTrainTurn, numValTurn, numTestTurn = np.array(np.round(numTurn *
                                                          splitFracs),
                                                 dtype=np.int)
示例#11
0
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
logging.getLogger('tensorflow').setLevel(logging.FATAL)

# test = tests[0]
# if True:
#
for test in tests:
    configFile = os.path.join('exp_configs', test + '.yaml')
    config = ThesisConfig(configFile)

    numOutputs = config.modelParms['numOutputs']
    lossRotScale = config.modelParms['lossRotScale']
    defaultLossFunc = config.modelParms['defaultLossFunction']

    checkpointFilesDict = config.trainPaths['checkpoint']
    checkpointFiles = config.getInputFiles(checkpointFilesDict)
    if isinstance(checkpointFiles, str):
        checkpointFiles = np.array([checkpointFiles])
    checkpointFolder = config.getFolderRef(checkpointFiles)

    # Loss Function
    if numOutputs > 3:
        lossFunc = scaledMSE_RT(lossRotScale)
        keras.losses.lossFunction = lossFunc
    else:
        lossFunc = defaultLossFunc

    learning_rates = []
    epochs = []
    # FOR EACH MODEL
    totalEpochs = len(checkpointFiles)
示例#12
0
# configFileList = ['exp_configs/CNN_test_11.yaml',
#                   'exp_configs/CNN_test_12.yaml',
#                   'exp_configs/CNN_test_13.yaml',
#                   'exp_configs/CNN_test_14.yaml',
#                   'exp_configs/CNN_test_15.yaml',
#                   'exp_configs/CNN_test_16.yaml',
#                   'exp_configs/CNN_test_17.yaml']

configFileList = ['exp_configs/scale_test_3.yaml']

for configFile in configFileList:
    config = ThesisConfig(configFile)

    history_filename = config.trainingParms['histFilename']
    historyFilesDict = config.trainPaths['history']
    historyFiles = config.getInputFiles(historyFilesDict)
    saveHistoryPath = config.getFolderRef(historyFiles)
    history_filepath = os.path.join(saveHistoryPath, history_filename)
    saveFigFilesDict = config.resultPaths['figures']
    figureFile = config.getOutputFiles(saveFigFilesDict, True)
    saveFigFolder = config.getFolderRef(figureFile)
    saveFigFilename = config.experiment['experiment'][
        'name'] + config.resultPaths['figures']['type']
    saveFigFile = os.path.join(saveFigFolder, saveFigFilename)

    if os.path.exists(history_filepath):
        print()
        with h5py.File(history_filepath, 'r') as f:
            epochs = np.array(f['epochs'], dtype=np.int)
            numEpochs = len(epochs)
            if 'loss' in f:
示例#13
0
    defaultLossFunc = config.modelParms['defaultLossFunction']
    lossRotScale = config.modelParms['lossRotScale']

    # Read in sequence 10 input
    seq = 10
    cam = 0

    # FILE DICTIONARIES
    normImageFilesDict = config.kittiNormalized['normImages']
    truthFilesDict = config.kittiPrepared['truth']
    imuFilesDict = config.kittiPrepared['imu']
    normParmsFilesDict = config.kittiNormalized['normParms']
    checkpointFilesDict = config.trainPaths['checkpoint']

    # GET NORM PARAMETERS
    normParmsFile = config.getInputFiles(normParmsFilesDict)
    truth_rot_parms = loadNormParms(normParmsFile, 'rot_xyz')
    truth_xyz_parms = loadNormParms(normParmsFile, 'trans_xyz')
    truth_polar_parms = loadNormParms(normParmsFile, 'trans_rtp')
    imu_rot_parms = loadNormParms(normParmsFile, 'noisy_rot_xyz')

    # GET IMAGES
    firstImageNames = np.empty(0)
    secondImageNames = np.empty(0)
    imageNames = config.getInputFiles(normImageFilesDict, seq, cam)
    firstImageNames = np.append(firstImageNames, imageNames[:-1], axis=0)
    secondImageNames = np.append(secondImageNames, imageNames[1:], axis=0)

    # GET TRUTH DATA
    truthData = np.empty((0, 7))
    truthFile = config.getInputFiles(truthFilesDict, seq)