Spaces:
Build error
Build error
File size: 13,018 Bytes
4d588ce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
import torch.nn as nn
import torch
import numpy as np
import os
from torch.utils.data import Dataset, DataLoader
from tqdm import tqdm
from utils import TrainClock, cycle, ensure_dirs, ensure_dir
import argparse
import h5py
import shutil
import json
import random
from plyfile import PlyData, PlyElement
import sys
sys.path.append("..")
from agent import BaseAgent
from pointnet2_ops.pointnet2_modules import PointnetFPModule, PointnetSAModule
from plyfile import PlyData, PlyElement
def write_ply(points, filename, text=False):
""" input: Nx3, write points to filename as PLY format. """
points = [(points[i,0], points[i,1], points[i,2]) for i in range(points.shape[0])]
vertex = np.array(points, dtype=[('x', 'f4'), ('y', 'f4'),('z', 'f4')])
el = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
with open(filename, mode='wb') as f:
PlyData([el], text=text).write(f)
class Config(object):
n_points = 2048
batch_size = 128
num_workers = 8
nr_epochs = 200
lr = 1e-4
lr_step_size = 50
# beta1 = 0.5
grad_clip = None
noise = 0.02
save_frequency = 100
val_frequency = 10
def __init__(self, args):
self.data_root = os.path.join(args.proj_dir, args.exp_name, "results/all_zs_ckpt{}.h5".format(args.ae_ckpt))
self.exp_dir = os.path.join(args.proj_dir, args.exp_name, "pc2cad_tune_noise{}_{}_new".format(self.n_points, self.noise))
print(self.exp_dir)
self.log_dir = os.path.join(self.exp_dir, 'log')
self.model_dir = os.path.join(self.exp_dir, 'model')
self.gpu_ids = args.gpu_ids
if (not args.test) and args.cont is not True and os.path.exists(self.exp_dir):
response = input('Experiment log/model already exists, overwrite? (y/n) ')
if response != 'y':
exit()
shutil.rmtree(self.exp_dir)
ensure_dirs([self.log_dir, self.model_dir])
if not args.test:
os.system("cp pc2cad.py {}".format(self.exp_dir))
with open('{}/config.txt'.format(self.exp_dir), 'w') as f:
json.dump(self.__dict__, f, indent=2)
class PointNet2(nn.Module):
def __init__(self):
super(PointNet2, self).__init__()
self.use_xyz = True
self._build_model()
def _build_model(self):
self.SA_modules = nn.ModuleList()
self.SA_modules.append(
PointnetSAModule(
npoint=512,
radius=0.1,
nsample=64,
mlp=[0, 32, 32, 64],
# bn=False,
use_xyz=self.use_xyz,
)
)
self.SA_modules.append(
PointnetSAModule(
npoint=256,
radius=0.2,
nsample=64,
mlp=[64, 64, 64, 128],
# bn=False,
use_xyz=self.use_xyz,
)
)
self.SA_modules.append(
PointnetSAModule(
npoint=128,
radius=0.4,
nsample=64,
mlp=[128, 128, 128, 256],
# bn=False,
use_xyz=self.use_xyz,
)
)
self.SA_modules.append(
PointnetSAModule(
mlp=[256, 256, 512, 1024],
# bn=False,
use_xyz=self.use_xyz
)
)
self.fc_layer = nn.Sequential(
nn.Linear(1024, 512),
nn.LeakyReLU(True),
nn.Linear(512, 256),
nn.LeakyReLU(True),
nn.Linear(256, 256),
nn.Tanh()
)
def _break_up_pc(self, pc):
xyz = pc[..., 0:3].contiguous()
features = pc[..., 3:].transpose(1, 2).contiguous() if pc.size(-1) > 3 else None
return xyz, features
def forward(self, pointcloud):
r"""
Forward pass of the network
Parameters
----------
pointcloud: Variable(torch.cuda.FloatTensor)
(B, N, 3 + input_channels) tensor
Point cloud to run predicts on
Each point in the point-cloud MUST
be formated as (x, y, z, features...)
"""
xyz, features = self._break_up_pc(pointcloud)
for module in self.SA_modules:
xyz, features = module(xyz, features)
return self.fc_layer(features.squeeze(-1))
class EncoderPointNet(nn.Module):
def __init__(self, n_filters=(128, 256, 512, 1024), bn=True):
super(EncoderPointNet, self).__init__()
self.n_filters = list(n_filters) # + [latent_dim]
# self.latent_dim = latent_dim
model = []
prev_nf = 3
for idx, nf in enumerate(self.n_filters):
conv_layer = nn.Conv1d(prev_nf, nf, kernel_size=1, stride=1)
model.append(conv_layer)
if bn:
bn_layer = nn.BatchNorm1d(nf)
model.append(bn_layer)
act_layer = nn.LeakyReLU(inplace=True)
model.append(act_layer)
prev_nf = nf
self.model = nn.Sequential(*model)
self.fc_layer = nn.Sequential(
nn.Linear(1024, 512),
nn.LeakyReLU(True),
nn.Linear(512, 256),
nn.Tanh()
)
def forward(self, x):
x = x.permute(0, 2, 1)
x = self.model(x)
x = torch.mean(x, dim=2)
x = self.fc_layer(x)
return x
class TrainAgent(BaseAgent):
def build_net(self, config):
net = PointNet2()
if len(config.gpu_ids) > 1:
net = nn.DataParallel(net)
# net = EncoderPointNet()
return net
def set_loss_function(self):
self.criterion = nn.MSELoss().cuda()
def set_optimizer(self, config):
"""set optimizer and lr scheduler used in training"""
self.optimizer = torch.optim.Adam(self.net.parameters(), config.lr) # , betas=(config.beta1, 0.9))
self.scheduler = torch.optim.lr_scheduler.StepLR(self.optimizer, config.lr_step_size)
def forward(self, data):
points = data["points"].cuda()
code = data["code"].cuda()
pred_code = self.net(points)
loss = self.criterion(pred_code, code)
return pred_code, {"mse": loss}
def read_ply(path, with_normal=False):
with open(path, 'rb') as f:
plydata = PlyData.read(f)
x = np.array(plydata['vertex']['x'])
y = np.array(plydata['vertex']['y'])
z = np.array(plydata['vertex']['z'])
vertex = np.stack([x, y, z], axis=1)
if with_normal:
nx = np.array(plydata['vertex']['nx'])
ny = np.array(plydata['vertex']['ny'])
nz = np.array(plydata['vertex']['nz'])
normals = np.stack([nx, ny, nz], axis=1)
if with_normal:
return np.concatenate([vertex, normals], axis=1)
else:
return vertex
class ShapeCodesDataset(Dataset):
def __init__(self, phase, config):
super(ShapeCodesDataset, self).__init__()
self.n_points = config.n_points
self.data_root = config.data_root
# self.abc_root = "/mnt/disk6/wurundi/abc"
self.abc_root = "/home/rundi/data/abc"
self.pc_root = self.abc_root + "/pc_v5a_processed_merge"
self.path = os.path.join(self.abc_root, "cad_e10_l6_c15_len60_min0_t100.json")
with open(self.path, "r") as fp:
self.all_data = json.load(fp)[phase]
with h5py.File(self.data_root, 'r') as fp:
self.zs = fp["{}_zs".format(phase)][:]
self.noise = config.noise
def __getitem__(self, index):
data_id = self.all_data[index]
pc_path = os.path.join(self.pc_root, data_id + '.ply')
if not os.path.exists(pc_path):
return self.__getitem__(index + 1)
pc_n = read_ply(pc_path, with_normal=True)
pc = pc_n[:, :3]
normal = pc_n[:, -3:]
sample_idx = random.sample(list(range(pc.shape[0])), self.n_points)
pc = pc[sample_idx]
normal = normal[sample_idx]
normal = normal / (np.linalg.norm(normal, axis=1, keepdims=True) + 1e-6)
pc = pc + np.random.uniform(-self.noise, self.noise, (pc.shape[0], 1)) * normal
pc = torch.tensor(pc, dtype=torch.float32)
shape_code = torch.tensor(self.zs[index], dtype=torch.float32)
return {"points": pc, "code": shape_code, "id": data_id}
def __len__(self):
return len(self.zs)
def get_dataloader(phase, config, shuffle=None):
is_shuffle = phase == 'train' if shuffle is None else shuffle
dataset = ShapeCodesDataset(phase, config)
dataloader = DataLoader(dataset, batch_size=config.batch_size, shuffle=is_shuffle, num_workers=config.num_workers)
return dataloader
parser = argparse.ArgumentParser()
# parser.add_argument('--proj_dir', type=str, default="/mnt/disk6/wurundi/cad_gen",
# help="path to project folder where models and logs will be saved")
parser.add_argument('--proj_dir', type=str, default="/home/rundi/project_log/cad_gen",
help="path to project folder where models and logs will be saved")
parser.add_argument('--exp_name', type=str, required=True, help="name of this experiment")
parser.add_argument('--ae_ckpt', type=str, required=True, help="desired checkpoint to restore")
parser.add_argument('--continue', dest='cont', action='store_true', help="continue training from checkpoint")
parser.add_argument('--ckpt', type=str, default='latest', required=False, help="desired checkpoint to restore")
parser.add_argument('--test',action='store_true', help="test mode")
parser.add_argument('--n_samples', type=int, default=100, help="number of samples to generate when testing")
parser.add_argument('-g', '--gpu_ids', type=str, default="0",
help="gpu to use, e.g. 0 0,1,2. CPU not supported.")
args = parser.parse_args()
if args.gpu_ids is not None:
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu_ids)
cfg = Config(args)
print("data path:", cfg.data_root)
agent = TrainAgent(cfg)
if not args.test:
# load from checkpoint if provided
if args.cont:
agent.load_ckpt(args.ckpt)
# for g in agent.optimizer.param_groups:
# g['lr'] = 1e-5
# create dataloader
train_loader = get_dataloader('train', cfg)
val_loader = get_dataloader('validation', cfg)
val_loader = cycle(val_loader)
# start training
clock = agent.clock
for e in range(clock.epoch, cfg.nr_epochs):
# begin iteration
pbar = tqdm(train_loader)
for b, data in enumerate(pbar):
# train step
outputs, losses = agent.train_func(data)
pbar.set_description("EPOCH[{}][{}]".format(e, b))
pbar.set_postfix({k: v.item() for k, v in losses.items()})
# validation step
if clock.step % cfg.val_frequency == 0:
data = next(val_loader)
outputs, losses = agent.val_func(data)
clock.tick()
clock.tock()
if clock.epoch % cfg.save_frequency == 0:
agent.save_ckpt()
# if clock.epoch % 10 == 0:
agent.save_ckpt('latest')
else:
# load trained weights
agent.load_ckpt(args.ckpt)
test_loader = get_dataloader('test', cfg)
# save_dir = os.path.join(cfg.exp_dir, "results/fake_z_ckpt{}_num{}_pc".format(args.ckpt, args.n_samples))
save_dir = os.path.join(cfg.exp_dir, "results/pc2cad_ckpt{}_num{}".format(args.ckpt, args.n_samples))
if not os.path.exists(save_dir):
os.makedirs(save_dir)
all_zs = []
all_ids = []
pbar = tqdm(test_loader)
cnt = 0
for i, data in enumerate(pbar):
with torch.no_grad():
pred_z, _ = agent.forward(data)
pred_z = pred_z.detach().cpu().numpy()
# print(pred_z.shape)
all_zs.append(pred_z)
all_ids.extend(data['id'])
pts = data['points'].detach().cpu().numpy()
# for j in range(pred_z.shape[0]):
# save_path = os.path.join(save_dir, "{}.ply".format(data['id'][j]))
# write_ply(pts[j], save_path)
# for j in range(pred_z.shape[0]):
# save_path = os.path.join(save_dir, "{}.h5".format(data['id'][j]))
# with h5py.File(save_path, 'w') as fp:
# fp.create_dataset("zs", data=pred_z[j])
cnt += pred_z.shape[0]
if cnt > args.n_samples:
break
all_zs = np.concatenate(all_zs, axis=0)
# save generated z
save_path = os.path.join(cfg.exp_dir, "results/pc2cad_z_ckpt{}_num{}.h5".format(args.ckpt, args.n_samples))
ensure_dir(os.path.dirname(save_path))
with h5py.File(save_path, 'w') as fp:
fp.create_dataset("zs", shape=all_zs.shape, data=all_zs)
save_path = os.path.join(cfg.exp_dir, "results/pc2cad_z_ckpt{}_num{}_ids.json".format(args.ckpt, args.n_samples))
with open(save_path, 'w') as fp:
json.dump(all_ids, fp)
|