PointMLP/classification_ScanObjectNN/ScanObjectNN.py

77 lines
2.5 KiB
Python
Raw Normal View History

2023-08-03 14:40:14 +00:00
"""ScanObjectNN download: http://103.24.77.34/scanobjectnn/h5_files.zip."""
2021-10-04 07:22:15 +00:00
import os
2023-08-03 14:40:14 +00:00
2021-10-04 07:22:15 +00:00
import h5py
import numpy as np
from torch.utils.data import Dataset
os.environ["HDF5_USE_FILE_LOCKING"] = "FALSE"
2022-02-16 02:32:53 +00:00
def download():
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
2023-08-03 14:40:14 +00:00
DATA_DIR = os.path.join(BASE_DIR, "data")
2022-02-16 02:32:53 +00:00
if not os.path.exists(DATA_DIR):
os.mkdir(DATA_DIR)
2023-08-03 14:40:14 +00:00
if not os.path.exists(os.path.join(DATA_DIR, "h5_files")):
2022-02-16 02:32:53 +00:00
# note that this link only contains the hardest perturbed variant (PB_T50_RS).
# for full versions, consider the following link.
2023-08-03 14:40:14 +00:00
www = "https://web.northeastern.edu/smilelab/xuma/datasets/h5_files.zip"
2022-02-16 02:32:53 +00:00
# www = 'http://103.24.77.34/scanobjectnn/h5_files.zip'
zipfile = os.path.basename(www)
2023-08-03 14:40:14 +00:00
os.system(f"wget {www} --no-check-certificate; unzip {zipfile}")
os.system(f"mv {zipfile[:-4]} {DATA_DIR}")
os.system("rm %s" % (zipfile))
2022-02-16 02:32:53 +00:00
2021-10-04 07:22:15 +00:00
def load_scanobjectnn_data(partition):
2022-02-16 02:32:53 +00:00
download()
2021-10-04 07:22:15 +00:00
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
all_data = []
all_label = []
2023-08-03 14:40:14 +00:00
h5_name = BASE_DIR + "/data/h5_files/main_split/" + partition + "_objectdataset_augmentedrot_scale75.h5"
2021-10-04 07:22:15 +00:00
f = h5py.File(h5_name, mode="r")
2023-08-03 14:40:14 +00:00
data = f["data"][:].astype("float32")
label = f["label"][:].astype("int64")
2021-10-04 07:22:15 +00:00
f.close()
all_data.append(data)
all_label.append(label)
all_data = np.concatenate(all_data, axis=0)
all_label = np.concatenate(all_label, axis=0)
return all_data, all_label
def translate_pointcloud(pointcloud):
2023-08-03 14:40:14 +00:00
xyz1 = np.random.uniform(low=2.0 / 3.0, high=3.0 / 2.0, size=[3])
2021-10-04 07:22:15 +00:00
xyz2 = np.random.uniform(low=-0.2, high=0.2, size=[3])
2023-08-03 14:40:14 +00:00
return np.add(np.multiply(pointcloud, xyz1), xyz2).astype("float32")
2021-10-04 07:22:15 +00:00
class ScanObjectNN(Dataset):
2023-08-03 14:40:14 +00:00
def __init__(self, num_points, partition="training"):
2021-10-04 07:22:15 +00:00
self.data, self.label = load_scanobjectnn_data(partition)
self.num_points = num_points
self.partition = partition
def __getitem__(self, item):
2023-08-03 14:40:14 +00:00
pointcloud = self.data[item][: self.num_points]
2021-10-04 07:22:15 +00:00
label = self.label[item]
2023-08-03 14:40:14 +00:00
if self.partition == "training":
2021-10-04 07:22:15 +00:00
pointcloud = translate_pointcloud(pointcloud)
np.random.shuffle(pointcloud)
return pointcloud, label
def __len__(self):
return self.data.shape[0]
2023-08-03 14:40:14 +00:00
if __name__ == "__main__":
2021-10-04 07:22:15 +00:00
train = ScanObjectNN(1024)
2023-08-03 14:40:14 +00:00
test = ScanObjectNN(1024, "test")
2021-10-04 07:22:15 +00:00
for data, label in train:
print(data.shape)
print(label)