Imports


In [2]:
import sys 
import os
sys.path.append(os.path.join(os.getcwd(), '../Code/'))

In [1]:
import argparse
import configparser

In [3]:
from Experiments.Training import Train

Main


In [4]:
# Command line arguments
Command = 'training'
ConfigID = 'Template'

In [9]:
# Reading the config File
config = configparser.ConfigParser()
config.read('../Code/Experiments/ConfigFiles/'+ConfigID+'.ini')


Out[9]:
['../Code/Experiments/ConfigFiles/Template.ini']

In [16]:
int(config['TRAINING']['epochs'])


Out[16]:
10

In [21]:
# Training
if (Command == 'training') and ('TRAINING' in config):
    exec('from DataSets.'+config['TRAINING']['dataset']+' import Dataset')
    exec('from Models.'+config['TRAINING']['model']+' import model')
    Train(ConfigID, Dataset, model, config['TRAINING']['loss'], config['TRAINING']['optimizer'], int(config['TRAINING']['batchsize']), int(config['TRAINING']['epochs']))


Loading the data set...
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-21-0025b8d517da> in <module>()
      3     exec('from DataSets.'+config['TRAINING']['dataset']+' import Dataset')
      4     exec('from Models.'+config['TRAINING']['model']+' import model')
----> 5     Train(ConfigID, Dataset, model, config['TRAINING']['loss'], config['TRAINING']['optimizer'], int(config['TRAINING']['batchsize']), int(config['TRAINING']['epochs']))

~/Documents/msc-project/Notebooks/../Code/Experiments/Training.py in Train(ID, Dataset, model, loss, optimizer, batchSize, epochs)
     25     # Load data set
     26     print('Loading the data set...')
---> 27     dataset = Dataset()
     28 
     29     # Build model

~/Documents/msc-project/Notebooks/../Code/DataSets/LadickyTrain.py in __init__(self, subset, seed, batch_res, scale, flip, rotation, color, brightness)
     15         '''
     16 
---> 17         self.dataset = h5py.File(file,'r')
     18 
     19         # Load images and normals (index, height, width, channels)

~/Applications/miniconda3/lib/python3.6/site-packages/h5py/_hl/files.py in __init__(self, name, mode, driver, libver, userblock_size, swmr, **kwds)
    269 
    270                 fapl = make_fapl(driver, libver, **kwds)
--> 271                 fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
    272 
    273                 if swmr_support:

~/Applications/miniconda3/lib/python3.6/site-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
     99         if swmr and swmr_support:
    100             flags |= h5f.ACC_SWMR_READ
--> 101         fid = h5f.open(name, flags, fapl=fapl)
    102     elif mode == 'r+':
    103         fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py/_objects.pyx in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/_objects.c:2846)()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/_objects.c:2804)()

h5py/h5f.pyx in h5py.h5f.open (/home/ilan/minonda/conda-bld/h5py_1496889914775/work/h5py/h5f.c:2123)()

OSError: Unable to open file (Unable to open file: name = 'datasets/mat/ladicky.mat', errno = 2, error message = 'no such file or directory', flags = 0, o_flags = 0)

Code


In [1]:
%%writefile ../Code/run.py
import os
import argparse
import configparser
from Experiments.Training import Train
from Experiments.Prediction import Predict
from Experiments.Evaluation import Evaluate

if __name__ == '__main__':
    # Command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("Command", help="training or prediction")
    parser.add_argument("ConfigID", help="ID corresponding to Experiments/ConfigFiles/ID.ini")
    args = parser.parse_args()
    # Reading the config File
    config = configparser.ConfigParser()
    config.read('Experiments/ConfigFiles/'+args.ConfigID+'.ini')
    # Output directory
    if not os.path.exists('Experiments/Outputs/'):
        os.makedirs('Experiments/Outputs/')
    # MAT directory
    if not os.path.exists('DataSets/MAT/'):
        os.makedirs('DataSets/MAT/')
        print('Warning: No data set is found in DataSets/MAT/')
    # Training
    if (args.Command == 'training') and ('TRAINING' in config):
        if os.path.exists('Experiments/Outputs/'+ args.ConfigID + '.h5'):
            print('Trained model (Experiments/Outputs/'+ args.ConfigID + '.h5)'+' already exists. Please (re)move this file before trying again.')
        else:
            if not os.path.exists('Experiments/Outputs/'):
                os.makedirs('Experiments/Outputs/')
            exec('from DataSets.'+config['TRAINING']['dataset']+' import Dataset')
            exec('from Models.'+config['TRAINING']['model']+' import model')
            Train(args.ConfigID, Dataset, model, config['TRAINING']['loss'], config['TRAINING']['optimizer'], int(config['TRAINING']['batchsize']), int(config['TRAINING']['epochs']))
    # Prediction
    elif (args.Command == 'prediction') and ('PREDICTION' in config):
        clean = True # No file is left from past predictions
        if os.path.exists('Experiments/Outputs/'+ args.ConfigID + '.mat'):
            clean = False
            print('Predicted normals file (Experiments/Outputs/'+ args.ConfigID + '.mat)'+' already exists. Please (re)move this file before trying again.')
        if os.path.exists('Experiments/Outputs/'+args.ConfigID+'/'):
            clean = False
            print('Predicted normal maps (Experiments/Outputs/'+ args.ConfigID + '/)'+' already exist. Please (re)move this directory before trying again.')
        if clean:
            os.makedirs('Experiments/Outputs/'+args.ConfigID+'/')
            exec('from DataSets.'+config['PREDICTION']['dataset']+' import Dataset')
            Predict(args.ConfigID, Dataset, config['PREDICTION']['resize']=='True')
    # Evaluation
    elif (args.Command == 'evaluation'):
        if os.path.exists('Experiments/Outputs/'+ args.ConfigID + '.eval'):
            print('Evaluation results (Experiments/Outputs/'+ args.ConfigID + '.eval)'+' already exists. Please (re)move this file before trying again.')
        else:
            if not os.path.exists('Experiments/Outputs/'+ args.ConfigID + '.mat'):
                print('Predicted normals file (Experiments/Outputs/'+ args.ConfigID + '.mat)'+' is missing.')
            else:
                from scipy.io import loadmat
                mat = loadmat('Experiments/Outputs/'+ args.ConfigID + '.mat')
                Evaluate(args.ConfigID, mat['Normals'], mat['Predictions'])
    else:
        print("Invalid command or config file.")


Overwriting ../Code/run.py

In [ ]: