示例#1
0
def saveEncodings(SHAPENET_PATH=os.path.join("G:\\", "Documenti",
                                             "ShapeNetCore.v2"),
                  cat_filter=["table", "chair", "sofa"],
                  SUBDIVISIONS=2,
                  NAME="learning_pred_and_gen_vector_autoencoder_adam_lr_5",
                  BATCH_SIZE=8,
                  TMP="C:\\GL\\3DShapeGen\\tmp\\pathslist.pk"):
    #
    # Instantiate model
    #
    model = Model(name=NAME, lr=10**-5)
    model.initialize(BATCH_SIZE)
    model.load()
    #
    # Load in advance the paths of all the elements
    # we need to get the embeddings
    #
    if os.path.exists(TMP):
        with open(TMP, "rb") as f:
            paths = pickle.load(f)
    else:
        paths = [
            x for x in dataGenerator(
                SHAPENET_PATH, cat_filter=cat_filter, generate_path=True)
        ]
        with open(TMP, "wb") as f:
            pickle.dump(paths, f)

    #
    # batch the elements and for each batch produce a tensor of binvoxes
    #
    batches = []
    for i in range(0, len(paths), BATCH_SIZE):
        batches.append(paths[i:i + BATCH_SIZE])
    for batch in tqdm(batches):
        models_paths = [
            os.path.join(x, "models", "model_normalized.solid.binvox")
            for x in batch
        ]
        binvoxs = []
        for x in models_paths:
            if os.path.exists(x):
                binvoxs.append(binvoxToNumpy(x))
            else:
                binvoxs.append(np.zeros(shape=(128, 128, 128)))
        binvoxs = tf.convert_to_tensor(binvoxs, dtype=tf.float32)
        binvoxs = tf.reshape(binvoxs,
                             shape=[
                                 binvoxs.shape[0], binvoxs.shape[1],
                                 binvoxs.shape[2], binvoxs.shape[3], 1
                             ])
        binvoxs = tf.nn.max_pool3d(binvoxs,
                                   2 * SUBDIVISIONS,
                                   2 * SUBDIVISIONS,
                                   'VALID',
                                   data_format='NDHWC',
                                   name=None)
        #
        # Produce the embeddings, the embeddings has shape: (bs,embedding_dim)
        #
        embeddings = model.encode(binvoxs)
        #
        # parallel write of the elements to disk
        #
        args = [(path, emb, NAME) for path, emb in zip(batch, embeddings)]
        p = Pool(BATCH_SIZE)
        p.map(__writeDiskMapFn, args)
        epochs = 1000
        for i in range(epochs):
            optimizer.zero_grad()

            output = AE(data_torch)
            loss = loss_fn(output, data_torch)

            loss.backward()
            optimizer.step()

            if i % 100 == 0:
                print('Epoch {}: {:.4f}'.format(i, loss))

        AE.eval()
        with torch.no_grad():
            data_transformed = AE.encode(data_torch).detach().numpy()

    else:
        raise Exception('Please specify a valid solver!')

    edges2d = []
    for e in edge_list:
        edges2d.append(data_transformed[[e[0], e[1]]])

    # check crosses
    crosses = cross(edges2d, edge_list, findall=True)
    print('{}: {} crosses'.format(method, len(crosses)))

    plt.scatter(data_transformed[:, 0],
                data_transformed[:, 1],
                c=t,