Esempio n. 1
0
def main():
    # Set Tensorflow verbosity
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

    # Get configuration
    config = get_args()

    # Optimize the parameters
    if config.optimize:
        print('Creating save directories...')
        create_dirs([config.optimizer_path, config.model_path])
        print('Initializing optimizer...')
        # optimizer = HyperEngineOptimizer(config)
        print('Optimizing...')
        # optimizer.optimize()

    # Set up test/train environment
    if config.infer or config.train:
        print('Building model...')
        params = generate_params(config)
        model = Tiramisu(params)
        if config.train:
            print('Creating save directories...')
            create_dirs([config.model_path])
            print('Initializing trainer...')
            trainer = Trainer(config)
            trainer.train()
        if config.infer:
            print('Creating save directories...')
            create_dirs([config.prediction_path])
            print('Initializing predicter...')
            predicter = Predicter(config)
            predicter.predict()

    # Prepare submission
    if config.rle:
        print('Creating submission save directory...')
        create_dirs([config.submission_path])
        prepare_submission(config.prediction_path, config.submission_path,
                           'submission')

    print('Done!')
Esempio n. 2
0
import matplotlib.animation as manimation
from mpl_toolkits.basemap import Basemap

## import torch modules
import torch
import torch.optim as optim

# import our model and dataloader
from src.utils.parser import get_args
from src.utils.utils import createfolder, remove_file, Adam16
from src.utils.loss import Criterion, LOSS
from src.utils.visulize import plot_input, plot_target
from src.runs.GRUs import get_dataloader, get_model, get_optimizer, test

if __name__ == "__main__":
    args = get_args()
    pd.set_option('precision', 4)
    # set seed
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    # Get the model
    model = get_model(args=args)

    # set optimizer
    optimizer = get_optimizer(args=args, model=model)
    param_pt = os.path.join(args.params_folder, 'params_10.pt')
    checkpoint = torch.load(param_pt, map_location=args.device)

    model.load_state_dict(checkpoint['model_state_dict'])
Esempio n. 3
0
def test_io():
    # Set Tensorflow verbosity
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '4'

    config = get_args()
    print('Creating save directories...')
    create_dirs([
        './image_tests/', './image_tests/tiled/',
        './image_tests/tiled/images/', './image_tests/tiled/masks/',
        './image_tests/detiled', './image_tests/detiled/images/',
        './image_tests/detiled/masks/', config.submission_path
    ])
    print('Initializing data loader...')
    data_loader = DataGenerator(config)

    # Progress bar
    batches = tqdm(range(data_loader.num_iterations_debug),
                   total=data_loader.num_iterations_debug,
                   desc="Processing Batches ")

    # Use session to evaluate real tensors
    with tf.Session() as sess:
        # Initialize data generator
        data_loader.initialize(sess, 'debug')
        next_element = data_loader.get_input()
        for i in batches:
            # Get a batch and tile it
            image, mask, name = sess.run(tile_data(*next_element))
            # Save tiled images
            for i in range(config.batch_size):
                cv2.imwrite(
                    os.path.join('./image_tests/tiled/images',
                                 name[i].decode('utf-8')),
                    255 * image[i, :, :])
                cv2.imwrite(
                    os.path.join('./image_tests/tiled/masks',
                                 name[i].decode('utf-8')), 255 * mask[i, :, :])
            # Convert images back to tensors because that's how the de-tiler expects them
            image = tf.convert_to_tensor(image)
            mask = tf.convert_to_tensor(mask)
            name = tf.convert_to_tensor(name)
            # De-tile images
            image, mask, name = sess.run(data_crop(image, mask, name))
            # Save de-tiled images
            for i in range(config.batch_size):
                cv2.imwrite(
                    os.path.join('./image_tests/detiled/images',
                                 name[i].decode('utf-8')),
                    255 * image[i, :, :])
                cv2.imwrite(
                    os.path.join('./image_tests/detiled/masks',
                                 name[i].decode('utf-8')), 255 * mask[i, :, :])

    # Close progress bar
    batches.close()
    # Compute RLEs for saved masks
    prepare_submission('./image_tests/detiled/masks/', config.submission_path,
                       'test')
    compare_rle('./image_tests/', './rle_tests/')
    compare_iou('./rle_tests/')

    print('Done!')
Esempio n. 4
0

def compare_iou(config):
    input_path = config.submission_path
    # Load test images
    test_path = get_max_filename(input_path, 'test', '.csv')
    test = pd.read_csv(test_path)
    test_images, test_image_names = csv_to_image(test)
    if config.compute_iou:
        # Load source images
        source = pd.read_csv('./data/train.csv')
        source_images, _ = csv_to_image(source)
        # Compute IoU values
        iou_a = mean_iou(source_images, test_images)
        iou_b = vector_iou(source_images, test_images)
        print('Mean IoU A: ' + str(iou_a))
        print('Mean IoU B: ' + str(iou_b))
    if config.rle_to_image:
        create_dirs(['./converted_images/'])
        save_images(test_images, test_image_names, './converted_images/')


if __name__ == '__main__':
    # If executing from this folder, change the working directory to the parent to manipulate project files
    if os.path.dirname(os.path.abspath(__file__)) == os.getcwd():
        project_root = os.path.abspath('..')
        os.chdir(project_root)
    # Get args and pass them to compare_rle
    config = get_args()
    compare_iou(config)