Ejemplo n.º 1
0
def init():
    MyDebugger.pre_fix = os.path.join(MyDebugger.pre_fix, "debug")
    debugger = MyDebugger(f"region_tiling",
                          fix_rand_seed=config.rand_seed,
                          save_print_to_file=False)
    plotter = Plotter()
    return debugger, plotter
def show_all_onering_neibors():
    # Driver code
    MyDebugger.pre_fix = os.path.join(MyDebugger.pre_fix, "debug")
    debugger = MyDebugger(f"gen_tile_graph_{config.env_name}", fix_rand_seed=0)
    plotter = Plotter()
    data_env = config.environment

    result_tiles = []
    align_tags = []
    for base_tile in data_env.proto_tiles:
        for align_tile in data_env.proto_tiles:
            neighbour_tiles, align_tag = get_all_tiles(base_tile,
                                                       align_tile,
                                                       integer_align=True)
            for i in range(len(neighbour_tiles)):
                plotter.draw_contours(
                    debugger.file_path(f"{align_tag[i]}.png"), [
                        base_tile.get_plot_attribute(),
                        neighbour_tiles[i].get_plot_attribute()
                    ])
Ejemplo n.º 3
0
def generate_3d_mesh(debugger: MyDebugger, output_filename: str,
                     input_folder: str, thickness: float):
    """
    generate a 3D mesh of the given contour with the given thickness
    :param debugger: the debugger to provide directory for obj to be stored
    :param output_filename: filename (excluding the extension)
    :param contour: the contour to create 3d object with
    :param thickness: the thickness of 3d object mesh
    :return: None
    """
    filename_path = os.path.abspath(input_folder)
    assert os.path.isfile(filename_path)
    contour = read_2d_obj(input_folder)

    if output_filename[-4:] != '.obj':
        output_filename = output_filename + '.obj'
    destination = debugger.file_path(output_filename)
    with open(destination, 'w') as obj_file:
        point_to_vertex = {}
        for index, point in enumerate(contour):
            point_to_vertex[tuple(point)] = (index * 2 + 1, index * 2 + 2)
            print(f'v {point[0]} {point[1]} 0', file=obj_file)
            print(f'v {point[0]} {point[1]} {thickness}', file=obj_file)

        contour_poly = Polygon(contour)
        triangles = triangulate(contour_poly)
        for triangle in triangles:
            if len(triangles) > 1:
                triangle_bound = LineString(triangle.exterior)
                if not triangle_bound.within(contour_poly):
                    continue
            *points, _ = triangle.exterior.coords
            face_1, face_2 = zip(*[point_to_vertex[point] for point in points])
            for face in (face_1[::-1], face_2):
                print('f ' + ' '.join([str(i) for i in face]), file=obj_file)
        for index, point in enumerate(contour):
            lower_point, upper_point = point_to_vertex[tuple(point)]
            lower_prev, upper_prev = point_to_vertex[tuple(contour[index - 1])]
            print('f ' + ' '.join([
                str(point) for point in (upper_prev, lower_point, upper_point)
            ]),
                  file=obj_file)
            print('f ' + ' '.join([
                str(point) for point in (upper_prev, lower_prev, lower_point)
            ]),
                  file=obj_file)
Ejemplo n.º 4
0
from util.debugger import MyDebugger
from interfaces.qt_plot import Plotter
import os
from inputs import config
from solver.ml_solver.trainer import Trainer
import torch
from solver.ml_solver.ml_solver import ML_Solver
import numpy as np
from graph_networks.networks.TilinGNN import TilinGNN

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

if __name__ == "__main__":
    MyDebugger.pre_fix = os.path.join(MyDebugger.pre_fix, "debug")
    debugger = MyDebugger(f"training_{config.experiment_id}",
                          fix_rand_seed=config.rand_seed,
                          save_print_to_file=False)
    plotter = Plotter()
    data_env = config.environment
    data_env.load_complete_graph(config.complete_graph_size)

    #### Network
    network = TilinGNN(
        adj_edge_features_dim=data_env.complete_graph.total_feature_dim,
        network_depth=config.network_depth,
        network_width=config.network_width).to(device)

    ## solver
    ml_solver = ML_Solver(debugger,
                          device,
                          data_env.complete_graph,
Ejemplo n.º 5
0
2. Draw the shape that you like
3. Transform by the shapes (Z/X, -/=, UP/DOWN/LEFT/RIGHT) or edit the shapes in Edit mode
4. Crop superset by Key E
5. Solve the problem by Key S (with number of trials)

Procedure for solving silhouette shapes:
1. Load the txt file in the ./silhouette folder by Key O
2. Transform by the shapes (Z/X, -/=, UP/DOWN/LEFT/RIGHT) or edit the shapes in Edit mode
3. Crop superset by Key E
4. Solve the problem by Key S (with number of trials)
'''

if __name__ == "__main__":
    MyDebugger.pre_fix = os.path.join(MyDebugger.pre_fix, "debug")
    debugger = MyDebugger("UI_interface",
                          fix_rand_seed=0,
                          save_print_to_file=False)
    plotter = Plotter()
    data_env = config.environment
    data_env.load_complete_graph(config.complete_graph_size)

    solver = ML_Solver(debugger,
                       device,
                       data_env.complete_graph,
                       None,
                       num_prob_maps=1)
    solver.load_saved_network(config.network_path)

    app = QApplication(sys.argv)
    draw = DrawerWidgets(data_env.complete_graph, debugger, plotter, solver,
                         app)
Ejemplo n.º 6
0
from util.debugger import MyDebugger
from interfaces.qt_plot import Plotter
import torch
from inputs import config
import os
from solver.ml_solver.ml_solver import ML_Solver
from tiling.tile_factory import solve_silhouette_dir

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

if __name__ == "__main__":
    MyDebugger.pre_fix = os.path.join(MyDebugger.pre_fix, "debug")
    debugger = MyDebugger(
        f"evaluation_{config.env_name}_{os.path.basename(config.silhouette_path)}",
        fix_rand_seed=config.rand_seed,
        save_print_to_file=True)
    plotter = Plotter()
    environment = config.environment
    environment.load_complete_graph(config.complete_graph_size)

    solver = ML_Solver(debugger,
                       device,
                       environment.complete_graph,
                       None,
                       num_prob_maps=config.num_output_probs_maps)

    solver.load_saved_network(config.network_path, evaluation=False)

    average_score = solve_silhouette_dir(config.silhouette_path,
                                         environment.complete_graph,
                                         solver,
Ejemplo n.º 7
0
from util.debugger import MyDebugger
from interfaces.qt_plot import Plotter
from graph_networks.networks.TilinGNN import TilinGNN
import os
from inputs import config
from solver.ml_solver.trainer import Trainer
import torch
from solver.ml_solver.ml_solver import ML_Solver
import numpy as np

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

if __name__ == "__main__":
    MyDebugger.pre_fix = os.path.join(MyDebugger.pre_fix, "debug")
    debugger = MyDebugger(
        f"gen_data_{config.env_name}-ring{config.complete_graph_size}-{config.number_of_data}",
        fix_rand_seed=config.rand_seed,
        save_print_to_file=False)
    plotter = Plotter()
    data_env = config.environment
    data_env.load_complete_graph(config.complete_graph_size)

    trainer = Trainer(debugger,
                      plotter,
                      device,
                      None,
                      data_path=config.dataset_path)

    trainer.create_data(data_env.complete_graph,
                        low=config.shape_size_lower_bound,
                        high=config.shape_size_upper_bound,
                        max_vertices=config.max_vertices,
Ejemplo n.º 8
0
    x_max, x_min, y_max, y_min = get_polygon_bound(polygons)

    #### calculate scale and translate
    scale = min(widget.width() / (x_max - x_min),
                widget.height() / (y_max - y_min))
    scale = scale * (1 - fig_conf.white_padding)
    translate = (widget.width() / 2 - scale * (x_min + x_max) / 2, \
                 widget.height() / 2 - scale * (y_min + y_max) / 2)

    return scale, translate


def get_polygon_bound(polygons):
    ##### get spatial range of all tiles
    x_min = np.min([min(polygon[:, 0]) for polygon in polygons])
    x_max = np.max([max(polygon[:, 0]) for polygon in polygons])
    y_min = np.min([min(polygon[:, 1]) for polygon in polygons])
    y_max = np.max([max(polygon[:, 1]) for polygon in polygons])
    return x_max, x_min, y_max, y_min


if __name__ == '__main__':
    MyDebugger.pre_fix = os.path.join(MyDebugger.pre_fix, "debug")
    debugger = MyDebugger(["xuhao", "khh"])
    plotter = Plotter()
    for i in range(10):
        triangle = np.array([[0, 0], [0, 1], [1, 0]])
        triangle2 = np.array([[0, 0], [0, -1], [1, 0]])
        plotter.draw_contours(debugger.file_path('layout.png'),
                              [('blue', triangle), ('lightgreen', triangle2)])
            neighbour_tiles, align_tag = get_all_tiles(base_tile,
                                                       align_tile,
                                                       integer_align=True)
            for i in range(len(neighbour_tiles)):
                plotter.draw_contours(
                    debugger.file_path(f"{align_tag[i]}.png"), [
                        base_tile.get_plot_attribute(),
                        neighbour_tiles[i].get_plot_attribute()
                    ])


if __name__ == '__main__':
    # Driver code
    MyDebugger.pre_fix = os.path.join(MyDebugger.pre_fix, "debug")
    debugger = MyDebugger(f"gen_tile_graph_{config.env_name}",
                          fix_rand_seed=0,
                          save_print_to_file=False)
    plotter = Plotter()
    data_env = config.environment

    for num_rings in range(1, 3):
        file_name = "complete_graph_ring{}.pkl".format(num_rings)

        # generate complete brick_layouts
        tiles = find_candidate_tile_locations(
            num_rings=num_rings,
            base_tile=data_env.proto_tiles[0],
            align_tiles=data_env.proto_tiles)

        print(f"{len(tiles)}  tiles created!")
        graph = TileGraph(data_env.tile_count,
Ejemplo n.º 10
0
        for index, point in enumerate(contour):
            lower_point, upper_point = point_to_vertex[tuple(point)]
            lower_prev, upper_prev = point_to_vertex[tuple(contour[index - 1])]
            print('f ' + ' '.join([
                str(point) for point in (upper_prev, lower_point, upper_point)
            ]),
                  file=obj_file)
            print('f ' + ' '.join([
                str(point) for point in (upper_prev, lower_prev, lower_point)
            ]),
                  file=obj_file)


if __name__ == "__main__":
    MyDebugger.pre_fix = os.path.join(MyDebugger.pre_fix, "debug")
    debugger = MyDebugger("brick_layout_test", fix_rand_seed=0)
    plotter = Plotter()
    # data_env = config.environment
    # data_env.load_complete_graph(1)
    #
    # node_feature, collide_edge_index, collide_edge_features, align_edge_index, align_edge_features, gt, re_index = tiling.TileFactory.gen_one_train_data(
    #     plotter, data_env.complete_graph, low=2, high=10)
    # brick_layout = tiling.brick_layout.BrickLayout(debugger, data_env.complete_graph, node_feature, collide_edge_index, collide_edge_features,
    #                            align_edge_index,align_edge_features, gt, re_index)
    #
    # # brick_layout.target = target
    # brick_layout.show_complete_graph(plotter, f"complete_graph.png")
    # brick_layout.show_ground_truth(plotter,   f"GT.png")
    # brick_layout.predict = brick_layout.ground_truth
    #
    # brick_layout.save_predict_as_objs("tile")
Ejemplo n.º 11
0
def train_model(model: nn.Module,
                loss_fn,
                debugger: MyDebugger,
                data_train: torch.Tensor,
                data_test: torch.Tensor,
                label_test: np.array,
                data_query: torch.Tensor,
                label_query: np.array,
                lr: float = 1e-3,
                batch_size: int = 32,
                training_epoch: int = 200,
                model_saving_epoch: int = 20,
                data_loading_workers: int = 4,
                optimizer_type=Adam,
                tolerance_epoch=5):
    print("Training Start!!!", flush=True)

    #### optimizer
    optimizer = optimizer_type(model.parameters(), lr=lr)

    ###### REFERENCE CODE from https://morvanzhou.github.io/tutorials/machine-learning/torch/3-05-train-on-batch/

    torch_dataset = Data.TensorDataset(data_train, data_train)

    loader = Data.DataLoader(
        dataset=torch_dataset,  # torch TensorDataset format
        batch_size=batch_size,  # mini batch size
        shuffle=True,
        num_workers=data_loading_workers,
    )

    #### for early stopping
    min_testing_loss = float("inf")
    tolerance_cnt = 0

    ##### different folders
    save_dir_models = debugger.file_path('models')
    if not os.path.isdir(save_dir_models):
        os.mkdir(save_dir_models)

    save_dir_reconstruction_base = debugger.file_path('reconstruction')
    if not os.path.isdir(save_dir_reconstruction_base):
        os.mkdir(save_dir_reconstruction_base)

    save_dir_query_base = debugger.file_path('query')
    if not os.path.isdir(save_dir_query_base):
        os.mkdir(save_dir_query_base)

    for epoch in range(training_epoch):

        training_losses = []
        for step, (batch_x, batch_y) in enumerate(loader):

            #### training
            batch_x = batch_x.to(device)
            optimizer.zero_grad()
            batch_x_reconstructed = model(batch_x)
            loss = loss_fn(batch_x_reconstructed, batch_x)
            loss.backward()
            training_losses.append(loss.cpu().detach().numpy())
            optimizer.step()

            # print(f"loss for epoch {epoch} stpe {step} : {loss.item()}")

        print(f"training loss for epoch {epoch} : {np.mean(training_losses)}")

        ### testing losss
        testing_loss, CD_loss, EMD_loss = calculate_loss(
            data_test, model, loss_fn)
        print(
            f"testing loss for epoch {epoch} : {testing_loss} {CD_loss} {EMD_loss}"
        )

        if testing_loss < min_testing_loss:

            ### reset tolerance
            min_testing_loss = testing_loss
            tolerance_cnt = 0

            ### save reconstruction on testing data
            save_dir_reconstruction = os.path.join(
                save_dir_reconstruction_base, f'epoch_{epoch}')
            os.mkdir(save_dir_reconstruction)
            save_reconstructed_point(data_test, model, save_dir_reconstruction,
                                     label_test)

            torch.save(model,
                       os.path.join(save_dir_models, f'{epoch}_model.pth'))

            ### get query results
            save_dir_query = os.path.join(save_dir_query_base,
                                          f'epoch_{epoch}')
            os.mkdir(save_dir_query)
            query_acc = caculate_query_acc(input_shapes=data_query,
                                           data_matrix=data_test,
                                           top_k=config.top_k,
                                           model=model,
                                           input_shapes_labels=label_query,
                                           data_labels=label_test,
                                           save_base=save_dir_query)

            print(f"average query acc : {query_acc}")
        else:
            tolerance_cnt += 1

            #### end the training if no more improvement
            if tolerance_cnt == tolerance_epoch:
                break

    print("Training Done!!!")
Ejemplo n.º 12
0
    batch_num = int(math.ceil(X.size(0) / config.batch_size))
    for i in range(batch_num):
        X_temp = model(X[i * config.batch_size:(i + 1) *
                         config.batch_size]).detach().cpu().numpy()
        for j in range(X_temp.shape[0]):
            write_point_cloud(
                X_temp[0],
                os.path.join(
                    save_dir,
                    f'{i*config.batch_size+j}_{label_names[label[i*config.batch_size+j]]}.obj'
                ))


if __name__ == "__main__":

    debugger = MyDebugger('training_points_autoencoder',
                          save_print_to_file=config.save_print_to_file)

    ### training data
    f = h5py.File('./data/train_data.h5')
    data_train = f['data'][:]  ### [9840, 2048, 3]
    X = torch.from_numpy(data_train).float()

    #### testing data + query data
    f = h5py.File('./data/test_data.h5')
    data_test = f['data'][:]  ###

    data_test = torch.from_numpy(data_test).float().to(device)
    label_test = np.squeeze(f['label'])

    f = h5py.File('./data/query_data.h5')
    data_query = f['data'][:]  ###