Source code for torch_geometric.datasets.geometry

import os
import os.path as osp
import glob

import torch
from torch_geometric.data import InMemoryDataset, download_url, extract_zip
from torch_geometric.io import read_off


[docs]class GeometricShapes(InMemoryDataset): r"""Synthetic dataset of various geometric shapes like cubes, spheres or pyramids. .. note:: Data objects hold mesh faces instead of edge indices. To convert the mesh to a graph, use the :obj:`torch_geometric.transforms.FaceToEdge` as :obj:`pre_transform`. To convert the mesh to a point cloud, use the :obj:`torch_geometric.transforms.SamplePoints` as :obj:`transform` to sample a fixed number of points on the mesh faces according to their face area. Args: root (string): Root directory where the dataset should be saved. train (bool, optional): If :obj:`True`, loads the training dataset, otherwise the test dataset. (default: :obj:`True`) transform (callable, optional): A function/transform that takes in an :obj:`torch_geometric.data.Data` object and returns a transformed version. The data object will be transformed before every access. (default: :obj:`None`) pre_transform (callable, optional): A function/transform that takes in an :obj:`torch_geometric.data.Data` object and returns a transformed version. The data object will be transformed before being saved to disk. (default: :obj:`None`) pre_filter (callable, optional): A function that takes in an :obj:`torch_geometric.data.Data` object and returns a boolean value, indicating whether the data object should be included in the final dataset. (default: :obj:`None`) """ url = 'https://github.com/Yannick-S/geometric_shapes/raw/master/raw.zip' def __init__(self, root, train=True, transform=None, pre_transform=None, pre_filter=None): super(GeometricShapes, self).__init__(root, transform, pre_transform, pre_filter) path = self.processed_paths[0] if train else self.processed_paths[1] self.data, self.slices = torch.load(path) @property def raw_file_names(self): return ['2d_circle'] @property def processed_file_names(self): return ['training.pt', 'test.pt'] def download(self): path = download_url(self.url, self.root) extract_zip(path, self.root) os.unlink(path) def process(self): torch.save(self.process_set('train'), self.processed_paths[0]) torch.save(self.process_set('test'), self.processed_paths[1]) def process_set(self, dataset): categories = glob.glob(osp.join(self.raw_dir, '*', '')) categories = sorted([x.split(os.sep)[-2] for x in categories]) data_list = [] for target, category in enumerate(categories): folder = osp.join(self.raw_dir, category, dataset) paths = glob.glob('{}/*.off'.format(folder)) for path in paths: data = read_off(path) data.pos = data.pos - data.pos.mean(dim=0, keepdim=True) data.y = torch.tensor([target]) data_list.append(data) if self.pre_filter is not None: data_list = [d for d in data_list if self.pre_filter(d)] if self.pre_transform is not None: data_list = [self.pre_transform(d) for d in data_list] return self.collate(data_list)