# Epipolar data - Translation epi_trans = np.empty((0, 3)) for cam in usedCams: epi_trans_cam = getH5pyData(config, epiFilesDict, usedSeqs, 'epi_trans_xyz', 3, camera=cam) epi_trans = np.append(epi_trans, epi_trans_cam, axis=0) epi_trans_parms = calcNormParms(epi_trans, trainIdxs) # Save to File normParmsFile = config.getOutputFiles(normParmsFilesDict, recalcDataNorm) if normParmsFile is None: sys.exit() with h5py.File(normParmsFile, 'w') as f: f.create_dataset('rot_xyz', data=rot_parms) f.create_dataset('trans_xyz', data=trans_parms) f.create_dataset('trans_rtp', data=polar_parms) f.create_dataset('noisy_rot_xyz', data=imu_parms) f.create_dataset('epi_rot_xyz', data=epi_rot_parms) f.create_dataset('epi_trans_xyz', data=epi_trans_parms) print('^ Normalization Parameters Calculated') print()
print() print('v Standardizing Images') for cam in usedCams: for seqStr in kittiSeqs: seq = int(seqStr) print('%sReading Camera %s, Sequence %02d' % (' ' * 2, cam, seq)) calFile = config.getInputFiles(calFilesDict, seq) if (calFile is None): continue origImages = config.getInputFiles(origImageFilesDict, seq, cam) if (origImages is None): continue standardImages = config.getOutputFiles(standardImageFilesDict, recalcStandardImages, seq, cam) if (standardImages is None): continue standardImageFolder = config.getFolderRef(standardImages) # get cal matrix for sequence cameraCalMat = getCalMat(calFile, cam) map_x, map_y = getMaps(destImageShape, standardDict['overlap'], cameraCalMat) numImages = len(origImages) for idx, origImageName in enumerate(origImages): if showProgress: percentComplete = int(idx / numImages * 100)
for cam in usedCams: for seqStr in kittiSeqs: seq = int(seqStr) print('%sReading Camera %s, Sequence %02d' % (' ' * 2, cam, seq)) # Get image names origImageNames = config.getInputFiles(origImageFilesDict, seq, cam) # if (origImageNames is None): # continue # get calibration files calFile = config.getInputFiles(calFilesDict, seq) # if (calFile is None): # continue epiFile = config.getOutputFiles(epiFilesDict, recalcEpiData, seq, cam) # if epiFile is None: # continue # get calibration matrix cameraCalMat = getCalMat(calFile, cam) epi_rot_xyz = np.empty((0, 3)) epi_trans_xyz = np.empty((0, 3)) numImagePairs = len(origImageNames) - 1 # get first image keypoints and descriptors srcKp, srcDesc = getKpsAndDescs(SIFT_detector, origImageNames[0]) for idx, origImageName in enumerate(origImageNames[1:]):
norm_epi_rot = applyNorm(epi_rot, epi_rot_parms) # Epipolar data - Translation epi_trans = np.empty((0, 3)) for cam in usedCams: epi_trans_cam = getH5pyData(config, epiFilesDict, usedSeqs, 'epi_trans_xyz', 3, camera=cam) epi_trans = np.append(epi_trans, epi_trans_cam, axis=0) norm_epi_trans = applyNorm(epi_trans, epi_trans_parms) # Save to File normDataFile = config.getOutputFiles(normDataFilesDict, reapplyDataNorm) if normDataFile is not None: print('Saving Normalized Truth and IMU Data') with h5py.File(normDataFile, 'w') as f: f.create_dataset('rot_xyz', data=norm_truth_rot) f.create_dataset('trans_xyz', data=norm_truth_xyz) f.create_dataset('trans_rtp', data=norm_truth_polar) f.create_dataset('noisy_rot_xyz', data=norm_imu_rot) normEpiFile = config.getOutputFiles(normEpiFilesDict, reapplyDataNorm) if normEpiFile is not None: print('Saving Normalized Epipolar Data') with h5py.File(normEpiFile, 'w') as f: f.create_dataset('epi_rot_xyz', data=norm_epi_rot) f.create_dataset('epi_trans_xyz', data=norm_epi_trans)
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 # Save noisy rotation angles to h5py file with h5py.File(imuFile, 'w') as f: os.chmod(imuFile, 0o666) f.create_dataset('noisy_rot_xyz', data=noisy_rot_xyz)
splitEpiLayers = config.constraintParms['splitEpiLayers'] postConstraintsLayerSize = config.constraintParms['postConstraintsLayerSize'] defaultLossFunc = config.modelParms['defaultLossFunction'] lossRotScale = config.modelParms['lossRotScale'] optimizerType = config.modelParms['optimizerType'] model_yaml_filename = config.modelParms['modelYamlFilename'] # Callbacks checkpoint_filename = config.checkpointParms['filename'] history_filename = config.trainingParms['histFilename'] # Get Files modelYamlsDict = config.trainPaths['model'] modelYamls = config.getOutputFiles(modelYamlsDict, True) saveModelPath = config.getFolderRef(modelYamls) model_filepath = os.path.join(saveModelPath, model_yaml_filename) checkpointFilesDict = config.trainPaths['checkpoint'] checkpointFiles = config.getOutputFiles(checkpointFilesDict, True) saveCheckpointPath = config.getFolderRef(checkpointFiles) checkpoint_filepath = os.path.join(saveCheckpointPath, checkpoint_filename) historyFilesDict = config.trainPaths['history'] historyFiles = config.getOutputFiles(historyFilesDict, True) saveHistoryPath = config.getFolderRef(historyFiles) history_filepath = os.path.join(saveHistoryPath, history_filename) # Get Callbacks List callbacksList = getCallbacksList(config, history_filepath, checkpoint_filepath)
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: print('Percent Complete: %d%%' % percentComplete) srcImage = cv.cvtColor(cv.imread(standardImageName), cv.COLOR_BGR2GRAY) dstImage = cv.resize(srcImage, dsize=destImageShape[::-1], interpolation=cv.INTER_AREA)
# 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. # T_0_i = [ R_0_i , t_0_0toi ] # [ 0_(1x3), 1 ]
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)
for configFile in configFiles: config = ThesisConfig(configFile) # Parameters name = config.expName numOutputs = config.modelParms['numOutputs'] # Get Files evalFilesDict = config.resultPaths['evaluations'] figFilesDict = config.resultPaths['figures'] 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, evalType+'_predictions.hdf5') if os.path.exists(predictionsFile): # Load Predictions with h5py.File(predictionsFile, 'r') as f:
from src.helpers.cfg import ThesisConfig from src.helpers.helper_functions import getOptimizer from src.helpers.training_helpers import getCallbacksList, getGenerator, getTrainAndValGenerators from src.helpers.custom_loss import scaledMSE_RT import tensorflow as tf from keras import backend as K import keras.losses # Load Configuration configFile = 'exp_configs/scale_test_4.yaml' config = ThesisConfig(configFile) checkpoint_filename = config.checkpointParms['filename'] checkpointFilesDict = config.trainPaths['checkpoint'] checkpointFiles = config.getOutputFiles(checkpointFilesDict, True) saveCheckpointPath = config.getFolderRef(checkpointFiles) checkpoint_filepath = os.path.join(saveCheckpointPath, checkpoint_filename) numOutputs = config.modelParms['numOutputs'] lossRotScale = config.modelParms['lossRotScale'] defaultLossFunc = config.modelParms['defaultLossFunction'] if numOutputs > 3: lossFunc = scaledMSE_RT(lossRotScale) keras.losses.lossFunction = lossFunc else: lossFunc = defaultLossFunc modelPath = checkpoint_filepath.format(epoch=1) model = load_model(modelPath)
# Parameters kittiSeqs = config.kittiSeqs usedCams = config.usedCams usedSeqs = config.usedSeqs splitFracs = config.splitFracs turnThresh_rad = config.thesisKittiParms['turnThreshold'] * np.pi / 180 # Files truthFilesDict = config.kittiPrepared['truth'] 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
# '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: train_loss = np.array(f['loss']) min_train_loss = np.min(train_loss) min_train_loss_epoch = (epochs[np.argmin(train_loss)]) print('Min Training Loss: %s, Epoch %s' %