Exemplo n.º 1
0
def convert_vdb_with_yt(datafilename,
                        outfilename,
                        level,
                        variable_out,
                        log_the_variable=False,
                        variable_tol=None,
                        renorm=True,
                        renorm_box=True,
                        renorm_box_size=10.0):
    # load your selected data file and grab the data
    ds = yt.load(datafilename)
    dd = ds.all_data()

    all_data = ds.covering_grid(level=level,
                                left_edge=ds.domain_left_edge,
                                dims=ds.domain_dimensions *
                                ds.refine_by**level)

    # to take the log or to not take the log, that is the question
    if log_the_variable is True:
        pointdata = np.log10(all_data[variable_out].v)
        if variable_tol is not None:
            variable_tol = np.log10(variable_tol)
    else:
        pointdata = (all_data[variable_out].v)

    # rescale from 0->1 for plotting
    if renorm:
        minp = pointdata.min()
        maxp = pointdata.max()
        pointdata = (pointdata - minp) / (maxp - minp)
        if variable_tol is not None:
            variable_tol = (variable_tol - minp) / (maxp - minp)

    # take out threshold data -> set to 0
    if variable_tol is not None:
        pointdata[pointdata < variable_tol] = 0.0

    # generate vdb
    domain_box = vdb.FloatGrid()
    domain_box.background = 0.0

    domain_box.copyFromArray(pointdata, ijk=(0, 0, 0), tolerance=0)

    # rescale to voxel size
    if renorm_box:
        vsize = renorm_box_size / float(
            pointdata.shape[0])  # assumes square box/shifting to x-axis units!
        domain_box.transform = vdb.createLinearTransform(
            voxelSize=vsize)  # tolist is for formatting

    #print('Writing vdb file...')
    outvdbname = outfilename + '_' + variable_out + '_one_level_is_' + str(
        level).zfill(3) + '.vdb'
    vdb.write(outvdbname, grids=domain_box)
    #print('... done with writing vdb file to ' + outvdbname)

    return outvdbname
def main():
    args = get_args()

    discrete_voxels_file = args.discrete_voxels_file
    gt_rendering_path = args.gt_rendering_path
    output_vdb_path = args.output_vdb_path

    if gt_rendering_path:
        gt_rendering = pyopenvdb.read(gt_rendering_path, "rendering")
    else:
        gt_rendering = None

    prediction = predict_one_3d(
        discrete_voxels_file=discrete_voxels_file,
        filled_vdb_path=args.filled_vdb_path,
        materials_filename=args.materials_file,
        model_filename=os.path.join(args.model_folder, args.model_weights),
        model_config_filename=os.path.join(args.model_folder,
                                           args.model_config),
    )
    pyopenvdb.write(output_vdb_path, [prediction])

    if gt_rendering is not None:
        prediction_acc = prediction.getConstAccessor()
        diff_list = []
        for item in gt_rendering.citerOnValues():
            if item.count == 1:
                target = item.value
                prediction, prediction_active = prediction_acc.probeValue(
                    item.min)
                if prediction_active:
                    diff_list.append([target, prediction])
        if diff_list:
            array = numpy.array(diff_list)
        else:
            array = numpy.zeros((1, 2, 3))
        gt_rendering = array[:, 0, :]
        prediction = array[:, 1, :]

        print("RMSE:", rms(gt_rendering, prediction))
        print("RMSE SRGB:",
              rms(linear_to_sRGB(gt_rendering), linear_to_sRGB(prediction)))
Exemplo n.º 3
0
    filepath = dir_path + "/grid." + str(frame).zfill(5)

    with open(filepath) as f:
        for line in f:
            if (" " in line) and ("[" in line) and ("]" in line) and ("density:" in line):
                index = line.split(" ")[0].split("[")[1].split("]")[0]
                i = int(index.split(",")[0])
                j = int(index.split(",")[1])
                k = int(index.split(",")[2])
                if (flip_y_z):
                    j = int(index.split(",")[2])
                    k = int(index.split(",")[1])

                density = float(line.split("density:")[1].split(" ")[0])
                velocity = line.split("v:")[1].split("[")[1].split("]")[0]
                vel_x = float(velocity.split(",")[0])
                vel_y = float(velocity.split(",")[1])
                vel_z = float(velocity.split(",")[2])
                if (flip_y_z):
                    vel_y = float(velocity.split(",")[2])
                    vel_z = float(velocity.split(",")[1])

                ijk = (i, j, k)

                density_accessor.setValueOn(ijk, density)
                v_accessor.setValueOn(ijk, (vel_x, vel_y, vel_z))

    del density_accessor
    vdb.write(out_path + "/vdb" + str(frame).zfill(5) + ".vdb", grids=[density_grid, v_grid])
    vSize = 1 / float(resolution[0])

    # Keep track of level 0 voxel size
    if level == minLevel:
        largestVSize = vSize

    # Scale and translate
    dataMatrix = [[vSize, 0, 0, 0], [0, vSize, 0, 0], [0, 0, vSize, 0],
                  [
                      -vSize / 2 - largestVSize, -vSize / 2 - largestVSize,
                      -vSize / 2 - largestVSize, 1
                  ]]
    maskMatrix = [[vSize, 0, 0, 0], [0, vSize, 0, 0], [0, 0, vSize, 0],
                  [
                      vSize / 2 - largestVSize, vSize / 2 - largestVSize,
                      vSize / 2 - largestVSize, 1
                  ]]
    dataCube.transform = vdb.createLinearTransform(dataMatrix)
    maskCube.transform = vdb.createLinearTransform(maskMatrix)

    # Write out the generated VDB
    output = []
    dataCube.name = "density"
    maskCube.name = "mask"
    output.append(maskCube)
    output.append(dataCube)
    outFilePath = "%s/%s_level%d.vdb" % (outFileDir, variable, level)
    vdb.write(outFilePath, grids=output)

    # Give feedback to see progress
    print "Finished level " + str(level)
Exemplo n.º 5
0
import pyopenvdb as vdb
import pymeshpotato.mpmesh as mepo
import pymeshpotato.mpvolume as mpvol
import pymeshpotato.mputils as mputils

cube = vdb.FloatGrid()
cube.fill(min=(-19, -19, -19), max=(19, 19, 19), value=10.0)

sphere1 = vdb.createLevelSetSphere(radius=20, center=(0.0, 0.0, 0.0))
sphere2 = vdb.createLevelSetSphere(radius=20, center=(10.5, 4, 3))
sphere3 = mpvol.ImplicitSphere(1.0, mputils.getMPVec3([0, 0, 0]))
volume1 = mpvol.VDBVolumeGrid.ptr(sphere1)
volume2 = mpvol.VDBVolumeGrid.ptr(sphere2)
volume3 = mpvol.VDBVolumeGrid.ptr(cube)
clamp_volume1 = mpvol.Clamp.ptr(volume1, 0, -1)
clamp_volume2 = mpvol.Clamp.ptr(volume2, 0, -1)
addedVolume = mpvol.AddVolumeFloat.ptr(clamp_volume1, clamp_volume2)
#print addedVolume.eval([3,4,5])
#coordbbox = mpvol.CoordBBox(
minBB = mpvol.getVDBCoord([-40, -40, -40])
maxBB = mpvol.getVDBCoord([40, 40, 40])
vdbvolume = mpvol.makeVDBGrid(addedVolume, mpvol.getVDBCoordBBox(minBB, maxBB),
                              0.5)
vdbvolume2 = mpvol.makeVDBGrid(sphere3, mpvol.getVDBCoordBBox(minBB, maxBB),
                               0.05)
vdb.write("testVolume.vdb", grids=[vdbvolume, vdbvolume2, cube])
Exemplo n.º 6
0
import pyopenvdb as vdb
import numpy

# Generate a random array of floats
array = numpy.random.rand(50, 50, 50)

# Copy values from a three-dimensional array of doubles
# into a grid of floats.
grid = vdb.FloatGrid()
print('Copying')
grid.copyFromArray(array)
assert grid.activeVoxelCount() == array.size
print(grid.evalActiveVoxelBoundingBox())
grid.name = 'float'

# Write to VDB file
vdb.write('floats.vdb', grids=[grid])
Exemplo n.º 7
0
import pyopenvdb as vdb
import pymeshpotato.mpmesh as mepo
import pymeshpotato.mpvolume as mpvol
import pymeshpotato.mputils as mputils
cube = vdb.FloatGrid()
cube.fill(min=(-19,-19,-19), max=(19,19,19), value=10.0)

sphere1 = vdb.createLevelSetSphere(radius=20, center=(0.0, 0.0, 0.0))
sphere2 = vdb.createLevelSetSphere(radius=20, center=(10.5, 4, 3))
sphere3 = mpvol.ImplicitSphere(1.0, mputils.getMPVec3([0,0,0]))
volume1 = mpvol.VDBVolumeGrid.ptr(sphere1)
volume2 = mpvol.VDBVolumeGrid.ptr(sphere2)
volume3 = mpvol.VDBVolumeGrid.ptr(cube)
clamp_volume1 = mpvol.Clamp.ptr(volume1, 0, -1)
clamp_volume2 = mpvol.Clamp.ptr(volume2, 0, -1)
addedVolume = mpvol.AddVolumeFloat.ptr(clamp_volume1, clamp_volume2)
#print addedVolume.eval([3,4,5])
#coordbbox = mpvol.CoordBBox(
minBB = mpvol.getVDBCoord([-40, -40, -40])
maxBB = mpvol.getVDBCoord([40, 40, 40])
vdbvolume = mpvol.makeVDBGrid(addedVolume, mpvol.getVDBCoordBBox(minBB, maxBB) ,0.5)
vdbvolume2 = mpvol.makeVDBGrid(sphere3, mpvol.getVDBCoordBBox(minBB, maxBB) , 0.05)
vdb.write("testVolume.vdb", grids=[vdbvolume, vdbvolume2, cube])
def main():
    args = get_args()

    dataset_base_dir = os.path.abspath(os.path.split(args.dataset)[0])
    dataset_name = os.path.splitext(os.path.split(args.dataset)[1])[0]

    with open(args.dataset) as f:
        dataset_data = yaml.full_load(f)

    model_folder = os.path.normpath(args.model_folder)

    with open(os.path.join(model_folder, args.model_config), "r") as json_file:
        model_config = json.load(json_file)

    _, nn_model_name = os.path.split(model_folder)
    results_output_directory = os.path.join(args.base_output_directory,
                                            dataset_name, nn_model_name)

    model_data = {
        "name": nn_model_name,
        "config": model_config,
    }

    model_weights_filepath = os.path.join(model_folder, args.model_weights)
    model_weights_file_md5 = md5sum_path(model_weights_filepath)
    output_yaml_file = os.path.join(results_output_directory, "summary.yml")

    if os.path.exists(output_yaml_file):
        with open(output_yaml_file, "r") as yaml_file:
            previous_prediction_data = yaml.full_load(yaml_file)
    else:
        previous_prediction_data = None

    results_data = {}
    dataset = dataset_data.get("items", {})

    predictions = {}
    prediction_masks = {}
    gt_renderings = {}
    to_predict_dataset = {}

    predicted_items = set()

    for item_key, dataset_item in dataset.items():
        dataset_item["is_2D"] = is_2D_file(
            get_dataset_item_volume_path(dataset_base_dir, dataset_item))

        output_prediction_path = os.path.join(
            results_output_directory,
            "{}.{}".format(item_key,
                           "exr" if dataset_item["is_2D"] else "vdb"),
        )
        dataset_item["output_prediction_path"] = output_prediction_path

        previous_prediction_md5 = ((previous_prediction_data.get(
            "items", {}).get(item_key, {}).get("model_weights_file_md5"))
                                   if previous_prediction_data else None)
        md5_recalculate = (not args.skip_md5_check and previous_prediction_data
                           and previous_prediction_md5 is not None and
                           previous_prediction_md5 != model_weights_file_md5)

        if not os.path.exists(
                output_prediction_path) or args.recalculate or md5_recalculate:
            to_predict_dataset[item_key] = dataset_item
        else:
            render_filename = get_dataset_item_render_path(
                dataset_base_dir, dataset_item)
            if dataset_item["is_2D"]:
                predictions[item_key] = read_image(output_prediction_path)
                gt_renderings[item_key] = read_image(render_filename)
            else:
                predictions[item_key] = pyopenvdb.read(output_prediction_path,
                                                       "prediction")
                gt_renderings[item_key] = pyopenvdb.read(
                    render_filename, "rendering")

    prediction_timings = {}
    if len(to_predict_dataset.keys()):
        predictions_new, gt_renderings_new, prediction_masks_new = predict_volume_appearance(
            dataset=to_predict_dataset,
            dataset_base_dir=dataset_base_dir,
            materials_filename=dataset_data["materials_file"],
            model_config=model_config,
            model_filename=model_weights_filepath,
            stencils_only=False,
            timings=prediction_timings,
            verbose_logging=args.verbose,
        )
        for item_key, prediction in predictions_new.items():
            dataset_item = dataset[item_key]
            folder_path, file_name = os.path.split(
                dataset_item["output_prediction_path"])
            ensure_dir(folder_path)
            if dataset_item["is_2D"]:
                dump_image(image=prediction,
                           filepath=dataset_item["output_prediction_path"])
            else:
                pyopenvdb.write(dataset_item["output_prediction_path"],
                                [prediction])
            predicted_items.add(item_key)
        predictions.update(predictions_new)
        if prediction_masks_new:
            prediction_masks.update(prediction_masks_new)
        gt_renderings.update(gt_renderings_new)

    if args.verbose:
        print("Prediction timings:")
        pprint(prediction_timings)

    # That's temporary for the deadline - normalize diff images
    max_diff_pos, max_diff_neg = 0.0, 0.0

    for item_key, dataset_item in dataset.items():
        prediction = predictions[item_key]
        gt_rendering = gt_renderings[item_key]

        if not dataset_item["is_2D"]:
            prediction_grid = prediction
            prediction_acc = prediction_grid.getConstAccessor()
            try:
                prediction_mask_accessor = prediction_masks[
                    item_key].getConstAccessor()
            except KeyError:
                prediction_mask_accessor = None

            coords_list = []
            diff_list = []

            for item in gt_rendering.citerOnValues():
                if item.count == 1:
                    target = item.value
                    prediction, prediction_active = prediction_acc.probeValue(
                        item.min)
                    if prediction_mask_accessor:
                        mask_value = prediction_mask_accessor.getValue(
                            item.min)
                    else:
                        mask_value = True
                    if prediction_active and mask_value:
                        coords_list.append(item.min)
                        diff_list.append([target, prediction])
            if diff_list:
                array = numpy.array(diff_list)
            else:
                array = numpy.zeros((1, 2, 3))
            gt_rendering = array[:, 0, :]
            prediction = array[:, 1, :]

            diff = prediction - gt_rendering
            diff_vis_scaling = 4.0
            diff_vis_array = plt.get_cmap(get_colormap("error"))(
                ((diff_vis_scaling * diff) / 2.0 + 0.5).mean(axis=1))

            diff_positive = numpy.maximum(diff, 0.0)
            diff_negative = numpy.minimum(diff, 0.0)
            max_diff_pos = max(max_diff_pos, diff_positive.mean(axis=1).max())
            max_diff_neg = max(max_diff_neg,
                               (-1.0 * diff_negative.mean(axis=1)).max())

            diff_grid = pyopenvdb.Vec3SGrid((-1, -1, -1))
            diff_grid.transform = prediction_grid.transform
            diff_grid.name = "diff_red-green"
            diff_grid_accessor = diff_grid.getAccessor()
            for coord, diff_vis_value in zip(coords_list, diff_vis_array):
                diff_grid_accessor.setValueOn(
                    coord,
                    (diff_vis_value[0], diff_vis_value[1], diff_vis_value[2]))

            diff_dE = get_difference_metric("ciede2000")(gt_rendering,
                                                         prediction)
            diff_dE_vis_scaling = 20.0
            diff_dE_vis_array = plt.get_cmap(get_colormap("ciede2000"))(
                diff_dE / diff_dE_vis_scaling)

            diff_dE_grid = pyopenvdb.Vec3SGrid((-1, -1, -1))
            diff_dE_grid.transform = prediction_grid.transform
            diff_dE_grid.name = "diff_dE2000_20max"
            diff_dE_grid_accessor = diff_dE_grid.getAccessor()
            for coord, diff_vis_value in zip(coords_list,
                                             diff_dE_vis_array[0]):
                diff_dE_grid_accessor.setValueOn(
                    coord,
                    (diff_vis_value[0], diff_vis_value[1], diff_vis_value[2]))
            pyopenvdb.write(dataset_item["output_prediction_path"],
                            [prediction_grid, diff_grid, diff_dE_grid])

        rmse_linear = float(
            get_difference_metric("rms")(gt_rendering, prediction))
        rmse_srgb = float(
            get_difference_metric("rms")(linear_to_sRGB(gt_rendering),
                                         linear_to_sRGB(prediction)))
        if dataset_item["is_2D"]:
            ssim_linear = float(
                get_difference_metric("ssim")(gt_rendering, prediction))
            ssim_srgb = float(
                get_difference_metric("ssim")(linear_to_sRGB(gt_rendering),
                                              linear_to_sRGB(prediction)))
        else:
            ssim_linear = 0.0
            ssim_srgb = 0.0

        print(item_key, "RMSE:", rmse_linear)
        print(item_key, "RMSE SRGB:", rmse_srgb)
        print(item_key, "SSIM:", ssim_linear)
        print(item_key, "SSIM SRGB:", ssim_srgb)

        volume_filename = get_dataset_item_volume_path(dataset_base_dir,
                                                       dataset_item)
        render_filename = get_dataset_item_render_path(dataset_base_dir,
                                                       dataset_item)

        results_data[item_key] = {
            "volume_filename": os.path.abspath(volume_filename),
            "render_filename": os.path.abspath(render_filename),
            "prediction_filename": os.path.abspath(output_prediction_path),
            "base_image_name": dataset_item.get("base_image_name", ""),
            "rmse_linear": rmse_linear,
            "rmse_srgb": rmse_srgb,
            "ssim_linear": ssim_linear,
            "ssim_srgb": ssim_srgb,
            "model_weights_file_md5": model_weights_file_md5,
        }

    print("max_diff_pos=", max_diff_pos, "max_diff_neg=", max_diff_neg)

    with open(output_yaml_file, "w") as f:
        yaml.dump({"model": model_data, "items": results_data}, f)