PointMLP/classification_ModelNet40/data.py

109 lines
3.6 KiB
Python
Raw Permalink Normal View History

2021-10-04 07:58:19 +00:00
import glob
2023-08-03 14:40:14 +00:00
import os
2021-10-04 07:58:19 +00:00
import h5py
import numpy as np
from torch.utils.data import Dataset
2023-08-03 14:40:14 +00:00
2021-10-04 07:58:19 +00:00
os.environ["HDF5_USE_FILE_LOCKING"] = "FALSE"
2023-08-03 14:40:14 +00:00
2021-10-04 07:58:19 +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")
2021-10-04 07:58:19 +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, "modelnet40_ply_hdf5_2048")):
www = "https://shapenet.cs.stanford.edu/media/modelnet40_ply_hdf5_2048.zip"
2021-10-04 07:58:19 +00:00
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))
2021-10-04 07:58:19 +00:00
def load_data(partition):
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")
2021-10-04 07:58:19 +00:00
all_data = []
all_label = []
2023-08-03 14:40:14 +00:00
for h5_name in glob.glob(os.path.join(DATA_DIR, "modelnet40_ply_hdf5_2048", "ply_data_%s*.h5" % partition)):
2021-10-04 07:58:19 +00:00
# print(f"h5_name: {h5_name}")
2023-08-03 14:40:14 +00:00
f = h5py.File(h5_name, "r")
data = f["data"][:].astype("float32")
label = f["label"][:].astype("int64")
2021-10-04 07:58:19 +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
2023-08-03 14:40:14 +00:00
2021-10-04 07:58:19 +00:00
def random_point_dropout(pc, max_dropout_ratio=0.875):
2023-08-03 14:40:14 +00:00
"""batch_pc: BxNx3."""
2021-10-04 07:58:19 +00:00
# for b in range(batch_pc.shape[0]):
2023-08-03 14:40:14 +00:00
dropout_ratio = np.random.random() * max_dropout_ratio # 0~0.875
drop_idx = np.where(np.random.random(pc.shape[0]) <= dropout_ratio)[0]
2021-10-04 07:58:19 +00:00
# print ('use random drop', len(drop_idx))
2023-08-03 14:40:14 +00:00
if len(drop_idx) > 0:
pc[drop_idx, :] = pc[0, :] # set to the first point
2021-10-04 07:58:19 +00:00
return pc
2023-08-03 14:40:14 +00:00
2021-10-04 07:58:19 +00:00
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:58:19 +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:58:19 +00:00
def jitter_pointcloud(pointcloud, sigma=0.01, clip=0.02):
N, C = pointcloud.shape
2023-08-03 14:40:14 +00:00
pointcloud += np.clip(sigma * np.random.randn(N, C), -1 * clip, clip)
2021-10-04 07:58:19 +00:00
return pointcloud
class ModelNet40(Dataset):
2023-08-03 14:40:14 +00:00
def __init__(self, num_points, partition="train"):
2021-10-04 07:58:19 +00:00
self.data, self.label = load_data(partition)
self.num_points = num_points
2023-08-03 14:40:14 +00:00
self.partition = partition
2021-10-04 07:58:19 +00:00
def __getitem__(self, item):
2023-08-03 14:40:14 +00:00
pointcloud = self.data[item][: self.num_points]
2021-10-04 07:58:19 +00:00
label = self.label[item]
2023-08-03 14:40:14 +00:00
if self.partition == "train":
2021-10-04 07:58:19 +00:00
# pointcloud = random_point_dropout(pointcloud) # open for dgcnn not for our idea for all
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:58:19 +00:00
train = ModelNet40(1024)
2023-08-03 14:40:14 +00:00
test = ModelNet40(1024, "test")
2021-10-04 07:58:19 +00:00
# for data, label in train:
# print(data.shape)
# print(label.shape)
from torch.utils.data import DataLoader
2023-08-03 14:40:14 +00:00
train_loader = DataLoader(
ModelNet40(partition="train", num_points=1024),
num_workers=4,
batch_size=32,
shuffle=True,
drop_last=True,
)
2021-10-04 07:58:19 +00:00
for batch_idx, (data, label) in enumerate(train_loader):
print(f"batch_idx: {batch_idx} | data shape: {data.shape} | ;lable shape: {label.shape}")
2023-08-03 14:40:14 +00:00
train_set = ModelNet40(partition="train", num_points=1024)
test_set = ModelNet40(partition="test", num_points=1024)
2021-10-04 07:58:19 +00:00
print(f"train_set size {train_set.__len__()}")
print(f"test_set size {test_set.__len__()}")