|
- # VoxelCNN
- import os, logging
- os.environ['TL_BACKEND'] = 'tensorflow'
- import random as rn
- import numpy as np
- import tensorlayer as tl
- from utils.inout import input_fn_voxel_dnn, get_shape_data, get_files, load_points
- import sys
- import argparse
- import datetime
- from tensorlayer.layers import Module
-
- #import tensorflow as tf
- #from tensorflow import keras
- #from tensorflow import nn
- #from tensorflow.keras import initializers
- #from tensorflow.keras.utils import Progbar
- os.environ["CUDA_VISIBLE_DEVICES"] = '0'
-
- random_seed = 42
- #tf.random.set_seed(random_seed)
- np.random.seed(random_seed)
- rn.seed(random_seed)
-
- # Defining main block
- class MaskedConv3D(Module):
-
- def __init__(self,in_channels,
- mask_type,
- filters,
- kernel_size,
- strides=1,
- padding='same',
- kernel_initializer='glorot_uniform',
- bias_initializer='zeros'):
- super(MaskedConv3D, self).__init__()
-
- assert mask_type in {'A', 'B'}
- self.mask_type = mask_type
-
- self.filters = filters
- self.kernel_size = kernel_size
- self.strides = strides
- self.padding = padding.upper()
- self.in_channels = in_channels
- #self.kernel_initializer = initializers.get(kernel_initializer)
- #self.bias_initializer = initializers.get(bias_initializer)
- self.build() #需要显式调用
-
- def build(self):
- self.kernel = self._get_weights('kernel', #tl规定:[filter_depth, filter_height, filter_width, in_channels, out_channels]
- shape=(self.kernel_size, #D
- self.kernel_size, #H
- self.kernel_size, #W
- self.in_channels,
- self.filters),
- init=tl.initializers.XavierUniform(), #和'glorot_uniform'一样
- trainable=True)
-
- self.bias = self._get_weights('bias',
- shape=(self.filters,),
- init=tl.initializers.zeros(),
- trainable=True)
-
- center = self.kernel_size // 2
-
- mask = np.ones(self.kernel.shape, dtype=np.float32)
- mask[center, center, center + (self.mask_type == 'B'):, :, :] = 0. # centre depth layer, center row
- mask[center, center + 1:, :, :, :] = 0. # center depth layer, lower row
- mask[center + 1:, :, :, :, :] = 0. # behind layers,all row, columns
-
- #self.mask = tf.constant(mask, dtype=tf.float32, name='mask')
- self.mask = tl.ops.constant(mask, dtype=tl.float32) #tl的函数没有name
-
- #def call(self, input): #tl input:[batch, in_depth, in_height, in_width, in_channels]
- def forward(self, input):
- #masked_kernel = tf.math.multiply(self.mask, self.kernel)
- masked_kernel = tl.ops.multiply(self.mask, self.kernel)
- #x = nn.conv3d(input, masked_kernel, strides=[1, self.strides, self.strides, self.strides, 1], padding=self.padding)
- x = tl.ops.conv3d(input, masked_kernel, strides=[1, self.strides, self.strides, self.strides, 1], padding=self.padding)
- #x = nn.bias_add(x, self.bias)
- x = tl.ops.bias_add(x, self.bias, data_format='NDHWC')
- return x
-
-
- class ResidualBlock(Module):
-
- def __init__(self, in_channels, h):
- super(ResidualBlock, self).__init__(name='')
-
- #self.conv2a = keras.layers.Conv3D(filters=h, kernel_size=1, strides=1) #这是一个类
- self.conv2a = tl.layers.Conv3d(n_filter=h, filter_size=(1, 1, 1), strides=(1, 1, 1), padding="VALID", W_init=tl.initializers.XavierUniform(), b_init=tl.initializers.zeros())
- self.conv2b = MaskedConv3D(in_channels, mask_type='B', filters=h, kernel_size=5, strides=1)
- #self.conv2c = keras.layers.Conv3D(filters=2 * h, kernel_size=1, strides=1)
- self.conv2c = tl.layers.Conv3d(n_filter=2 * h, filter_size=(1, 1, 1), strides=(1, 1, 1), padding="VALID", W_init=tl.initializers.XavierUniform(), b_init=tl.initializers.zeros())
-
- #def call(self, input_tensor):
- def forward(self, input_tensor):
- #x = nn.relu(input_tensor)
- x = tl.layers.LeakyReLU(alpha=0.0)(input_tensor)
- x = self.conv2a(x)
-
- #x = nn.relu(x)
- x = tl.layers.LeakyReLU(alpha=0.0)(x)
- x = self.conv2b(x)
-
- #x = nn.relu(x)
- x = tl.layers.LeakyReLU(alpha=0.0)(x)
- x = self.conv2c(x)
-
- x += input_tensor
- return x
-
- def compute_acc(y_true, y_predict,loss,logger,step, record2log=True): #y_true和logits.shape: (8, 64, 64, 64, 2)
- y_true = tl.convert_to_numpy(y_true)
- y_true = np.argmax(y_true, axis=4)
- #y_true = tf.argmax(y_true, axis=4)
- y_predict = tl.convert_to_numpy(y_predict)
- y_predict = np.argmax(y_predict, axis=4)
- #y_predict = tf.argmax(y_predict, axis=4)
- #tp = tf.math.count_nonzero(y_predict * y_true, dtype=tf.float32) #结果是一个数
- tp = tl.ops.CountNonzero(dtype=tl.float32)(y_predict * y_true)
- #tn = tf.math.count_nonzero((y_predict - 1) * (y_true - 1), dtype=tf.float32)
- tn = tl.ops.CountNonzero(dtype=tl.float32)((y_predict - 1) * (y_true - 1))
- #fp = tf.math.count_nonzero(y_predict * (y_true - 1), dtype=tf.float32)
- fp = tl.ops.CountNonzero(dtype=tl.float32)(y_predict * (y_true - 1))
- #fn = tf.math.count_nonzero((y_predict - 1) * y_true, dtype=tf.float32)
- fn = tl.ops.CountNonzero(dtype=tl.float32)((y_predict - 1) * y_true)
- precision = tp / (tp + fp)
- recall = tp / (tp + fn)
- accuracy = (tp + tn) / (tp + tn + fp + fn)
- specificity = tn / (tn + fp)
- f1_score = (2 * precision * recall) / (precision + recall)
-
- if record2log:
- logger.info('bc/loss:' + str(loss) + ', ' + str(step))
- logger.info('bc/precision:' + str(precision) + ', ' + str(step))
- logger.info('bc/recall:' + str(recall) + ', ' + str(step))
- logger.info('bc/accuracy:' + str(accuracy) + ', ' + str(step))
- logger.info('bc/specificity:' + str(specificity) + ', ' + str(step))
- logger.info('bc/f1_score:' + str(f1_score) + ', ' + str(step))
-
- a = [tp, tn, fp, fn, precision, recall, accuracy, specificity, f1_score]
- return a
-
- class VoxelCNN(Module):
- def __init__(self, depth=64, height=64, width=64, n_channel=1, output_channel=2,residual_blocks=2,n_filters=64):
- super(VoxelCNN, self).__init__()
- self.depth = depth
- self.height = height
- self.width = width
- self.n_channel = n_channel
- self.output_channel = output_channel
- self.residual_blocks=residual_blocks
- self.n_filters=n_filters
- #self.init__ = super(VoxelCNN, self).__init__()
- self.masked_conv1 = MaskedConv3D(in_channels=1, mask_type='A', filters=self.n_filters, kernel_size=7, strides=1)
- self.residual_block = ResidualBlock(in_channels=32, h=int(self.n_filters/2))
- self.masked_conv2 = MaskedConv3D(in_channels=64, mask_type='B', filters=self.n_filters, kernel_size=1, strides=1)
- self.masked_conv3 = MaskedConv3D(in_channels=64, mask_type='B', filters=self.output_channel, kernel_size=1, strides=1)
-
- def forward(self, inputs):
- x = self.masked_conv1(inputs)
- for i in range(self.residual_blocks):
- x = self.residual_block(x)
- x = tl.layers.LeakyReLU(alpha=0.0)(x)
- x = self.masked_conv2(x)
- x = tl.layers.LeakyReLU(alpha=0.0)(x)
- x = self.masked_conv3(x)
- return x
-
- '''def build_voxelCNN_model(self):
- # Creating model
- inputs = keras.layers.Input(shape=(self.depth, self.height, self.width, self.n_channel)) #inputs.shape: (None, 64, 64, 64, 1)实际推理时,中间的维度不一定是64,还可能是32、16、8,但是最后的channel大小都是一样的
- x = MaskedConv3D(in_channels=1, mask_type='A', filters=self.n_filters, kernel_size=7, strides=1)(inputs)
- for i in range(self.residual_blocks):
- x = ResidualBlock(in_channels=32, h=int(self.n_filters/2))(x) #两个ResidualBlock都是x.shape: (None, 64, 64, 64, 32)
- x = keras.layers.Activation(activation='relu')(x)
- x = MaskedConv3D(in_channels=64, mask_type='B', filters=self.n_filters, kernel_size=1, strides=1)(x) #输入的x.shape: (None, 64, 64, 64, 64)
- x = keras.layers.Activation(activation='relu')(x)
- x = MaskedConv3D(in_channels=64, mask_type='B', filters=self.output_channel, kernel_size=1, strides=1)(x) #输入的x.shape: (None, 64, 64, 64, 64)
- #x = nn.softmax(x, axis=-1)#add or remove softmax here
- voxelCNN = keras.Model(inputs=inputs, outputs=x)
- #voxelCNN.summary()
- return voxelCNN
- '''
-
- class Trainer():
- def __init__(self, model, output_path, dhw=64, output_channel=2):
- self.model = model
- self.logger = self.getlogger(output_path)
- self.logger.info(model)
- self.depth = dhw
- self.output_channel = output_channel
- self.DISPLAY_STEP = 600 #600??
-
- def getlogger(self, logdir):
- logger = logging.getLogger(__name__) #提供了应用程序可以直接使用的接口
- logger.setLevel(level = logging.INFO) #NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL
- if not os.path.exists(logdir):
- os.makedirs(logdir)
- handler = logging.FileHandler(os.path.join(logdir, 'log.txt')) #将(logger创建的)日志记录发送到合适的目的输出
- handler.setLevel(logging.INFO)
- formatter = logging.Formatter('%(asctime)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') #决定日志记录的最终输出格式
- handler.setFormatter(formatter)
- console = logging.StreamHandler() #用于输出到控制台
- console.setLevel(logging.INFO)
- console.setFormatter(formatter)
- logger.addHandler(handler)
- logger.addHandler(console)
-
- return logger
-
- def points2voxel(self,points, bbox_max):
- no_blocks = len(points) #202
- blocks_oc = np.zeros((no_blocks, bbox_max, bbox_max, bbox_max, 1), dtype=np.float32) #[202,64,64,64,1]
- for i, block in enumerate(points):
- block = block[:, 0:3]
- block = block.astype(np.uint32)
- blocks_oc[i, block[:, 0], block[:, 1], block[:, 2], 0] = 1.0 #可能有损
- return blocks_oc #blocks_oc=voxel化后的各block的voxel集合,coor_min_max:每个block的xyz的最小最大值,lower_level_ocv:每个block的点坐标/2,去重后的点个数
-
- def calling_dataset(self,training_dirs, batch_size,portion_data):
- # loading data and # Creating input stream using tf.data API
- # training_dir="../../database/ModelNet40_200_pc512_oct3_4k/**/*.ply"
- # batch_size=2
- total_files=[]
- for training_dir in training_dirs:
- training_dir=training_dir+'**/*.ply'
- p_min, p_max, dense_tensor_shape = get_shape_data(self.depth, 'channels_last') #dense_tensor_shape=[64,64,64,1]
- files = get_files(training_dir)
- total_files_len = len(files) #65,和文件个数一致
-
- # sorting and selecting files
- sizes = [os.stat(x).st_size for x in files]
- files_with_sizes = list(zip(files, sizes))
- files_sorted_by_points = sorted(files_with_sizes, key=lambda x: -x[1]) #从大到小
- files_sorted_by_points=files_sorted_by_points[:int(total_files_len*0.9)]
- files=list(zip(*files_sorted_by_points))
- files=list(files[0])
- files=rn.sample(files,int(len(files)*portion_data))
- total_files = np.concatenate((total_files, files), axis=0)
- self.logger.info('Selected '+str(len(files))+' from '+str(total_files_len)+' in '+training_dir)
- #print('Selected ', len(files), ' from ', total_files_len, ' in ', training_dir)
-
- assert len(total_files) > 0
- rn.shuffle(total_files) # shuffle file
- self.logger.info('Total blocks for training: '+str(len(total_files)))
- #print('Total blocks for training: ', len(total_files))
- points = load_points(total_files) #只有xyz
-
- files_cat = np.array([os.path.split(os.path.split(x)[0])[1] for x in total_files])
- points_train = points[files_cat == 'train'] #points_train.shape: (58,)
- points_val = points[files_cat == 'test']
- number_training_data = len(points_train) #58
- train_datas = self.points2voxel(points_train, bbox_max=self.depth) #voxel化
- #train_dataset = tl.iterate.minibatches(train_datas, train_datas, batch_size, shuffle=True) #只能一个epoch
- #train_dataset = input_fn_voxel_dnn(points_train, batch_size, dense_tensor_shape, 'channels_last', repeat=False, shuffle=True) #过程中做了稀疏处理,就是voxel化
- # train_dataset=train_dataset.batch(batch_size)
- test_datas = self.points2voxel(points_val, bbox_max=self.depth)
- #test_dataset = tl.iterate.minibatches(test_datas, test_datas, batch_size, shuffle=True)
- #test_dataset = input_fn_voxel_dnn(points_val, batch_size, dense_tensor_shape, 'channels_last', repeat=False, shuffle=True)
- # train_dataset = input_fn_gaussain(points_train, batch_size, dense_tensor_shape, 'channels_last',gaussain_power=1, repeat=False,
- # shuffle=True)
- # # train_dataset=train_dataset.batch(batch_size)
- # test_dataset = input_fn_gaussain(points_val, batch_size, dense_tensor_shape, 'channels_last',gaussain_power=0, repeat=False, shuffle=True)
- return train_datas, test_datas,number_training_data
-
- def train_voxelCNN(self, batch, epochs, model_path, dataset, portion_data, saved_model=None): #model_path:将要保存的路径 saved_model:已有模型路径
- #log directory
- #current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
- #train_log_dir = model_path+'log' + current_time + '/train'
- #test_log_dir = model_path + 'log' + current_time + '/test'
- #train_summary_writer = tf.summary.create_file_writer(train_log_dir)
- #test_summary_writer = tf.summary.create_file_writer(test_log_dir)
- #initialize model and optimizer, loss
- #voxelCNN = self.build_voxelCNN_model()
- voxelCNN = self.model
- [train_datas, test_datas,number_training_data] = self.calling_dataset(training_dirs=dataset, batch_size=batch,portion_data=portion_data)
- learning_rate = 1e-3
- optimizer = tl.optimizers.Adam(lr=learning_rate)
- #optimizer = tf.optimizers.Adam(lr=learning_rate)
- #compute_loss = keras.losses.CategoricalCrossentropy(from_logits=True, ) #logits就是一个向量,下一步将被投给softmax的向量。from_logits=True,表明输入的y_hat还没有计算softmax
- n_epochs = epochs
- n_iter = int(number_training_data / batch)
- #early stopping setting
- best_val_loss, best_val_epoch = None, None
- best_train_loss, best_train_epoch = None, None
- max_patience=4 #5
- early_stop=False
- #Load lastest checkpoint
- '''vars_to_load = {"Weight_biases": voxelCNN.trainable_variables, "optimizer": optimizer}
- checkpoint = tf.train.Checkpoint(**vars_to_load)
- latest_ckpt = tf.train.latest_checkpoint(saved_model)
- '''
- #if latest_ckpt is not None:
- if saved_model is not None:
- #checkpoint.restore(latest_ckpt)
- tl.files.load_and_assign_npz(name=saved_model, network=voxelCNN) #name='model.npz'
- self.logger.info('Loaded last checkpoint')
- #print('Loaded last checkpoint')
- else:
- self.logger.info('Training from scratch')
- #print('Training from scratch')
- avg_train_loss=0
- avg_test_loss=0
- #ckpt_manager = tf.train.CheckpointManager(checkpoint, checkpoint_name='ckpt_', directory=model_path, max_to_keep=40)
- losses=[]
- #training
- loss_fn = tl.cost.softmax_cross_entropy_with_logits
- net_with_loss = tl.models.WithLoss(voxelCNN, loss_fn)
- train_weights = voxelCNN.trainable_weights
- train_one_step = tl.models.TrainOneStep(net_with_loss, optimizer, train_weights)
- self.logger.info('Training:')
- for epoch in range(n_epochs):
- #progbar = Progbar(n_iter)
- self.logger.info('Epoch {:}/{:}'.format(epoch + 1, n_epochs))
- #print('\n Epoch {:}/{:}'.format(epoch + 1, n_epochs))
- loss_per_epochs=[]
- voxelCNN.set_train() # enable dropout
- train_dataset = tl.iterate.minibatches(train_datas, train_datas, batch_size=batch, shuffle=True)
- for i_iter, (batch_x,_) in enumerate(train_dataset): #batch_x shape=(8, 64, 64, 64, 1),即voxel化后的东西,类型为float32
- #batch_y = tf.cast(batch_x, tf.int32)
- batch_y = batch_x
- #with tf.GradientTape() as ae_tape:
- #logits = voxelCNN(batch_x, training=True) #output_channel=2,logits.shape: (8, 64, 64, 64, 2)
- y_true = tl.ops.reshape(batch_y,(batch_y.shape[0], batch_y.shape[1], batch_y.shape[2], batch_y.shape[3]))
- y_true = tl.ops.cast(y_true, tl.int32) #算loss的时候,y_true数据类型必须是int
- #y_true = tf.one_hot(batch_y, self.output_channel)
- #y_true = tf.reshape(y_true,(y_true.shape[0], self.depth, self.height, self.width, self.output_channel))
-
- #loss = tl.cost.softmax_cross_entropy_with_logits(logits, y_true, 'my_loss') #logits还没有算softmax,在cross_entropy中计算softmax,y_true不是one_hot的,比logits少一维,loss是一个数(内部有tf.reduce_mean)
- #loss = compute_loss(y_true, logits) #loss: tf.Tensor(0.017612608, shape=(), dtype=float32)
- loss = train_one_step(batch_x, y_true) #计算loss,自动求梯度,并自动更新训练权重,返回loss
- #y_true = tl.ops.reshape(y_true,(batch_y.shape[0], batch_y.shape[1], batch_y.shape[2], batch_y.shape[3], 1))
- y_true = tl.ops.OneHot(depth=self.output_channel, on_value=None, off_value=None, axis=None, dtype=None)(y_true) #维度会变成(8, 64, 64, 64, 2)
- logits = voxelCNN(batch_x)
- if i_iter % self.DISPLAY_STEP == 5:
- metrics = compute_acc(y_true, logits,loss,self.logger,int(epoch*n_iter+i_iter)) #返回结果并记录到日志
- #gradients = ae_tape.gradient(loss, voxelCNN.trainable_variables)
- #gradients, _ = tf.clip_by_global_norm(gradients, 1.0) #tl暂时无法实现,先不控制看看训练情况 #控制梯度,但不是直接clamp
- #optimizer.apply_gradients(zip(gradients, voxelCNN.trainable_variables))
- #loss_per_epochs.append(loss/batch_x.shape[0])
- loss_per_epochs.append(loss) #loss已经是平均值,不需要/batch_x.shape[0]
- #progbar.add(1, values=[('loss', loss),('f1', metrics[8])])
- avg_train_loss=np.average(loss_per_epochs)
- losses.append(avg_train_loss)
- self.logger.info('Avg training loss: '+str(avg_train_loss))
- #print('Avg training loss: ', avg_train_loss)
-
- # Validation dataset
- test_losses=[]
- test_metrics=[]
- # i=0
- self.logger.info('Testing:')
- voxelCNN.set_eval()
- test_dataset = tl.iterate.minibatches(test_datas, test_datas, batch_size=batch, shuffle=True)
- for i_iter, (batch_x,_) in enumerate(test_dataset):
- batch_y = tl.ops.cast(batch_x, tl.int32)
- #batch_y = tf.cast(batch_x, tf.int32)
- logits = voxelCNN(batch_x)
- #logits = voxelCNN(batch_x, training=True)
- y_true = tl.ops.reshape(batch_y,(batch_y.shape[0], batch_y.shape[1], batch_y.shape[2], batch_y.shape[3]))
- loss = loss_fn(logits, y_true, name='test_loss')
- y_true = tl.ops.OneHot(depth=self.output_channel, on_value=None, off_value=None, axis=None, dtype=None)(y_true)
- #y_true = tf.one_hot(batch_y, self.output_channel)
- #y_true = tf.reshape(y_true,(y_true.shape[0], self.depth, self.height, self.width, self.output_channel))
- #loss = compute_loss(y_true, logits)
- record2log = False
- if i_iter % (self.DISPLAY_STEP) == 5:
- record2log=True
- metrics = compute_acc(y_true, logits,loss,self.logger,i_iter, record2log=record2log)
- #test_losses.append(loss/batch_x.shape[0])
- test_losses.append(loss)
- test_metrics.append(metrics)
- # i+=1
- # if(i>2000):
- # break
-
- test_metrics=np.asarray(test_metrics)
- avg_metrics=np.average(test_metrics,axis=0)
- avg_test_loss=np.average(test_losses)
-
- #print results
- self.logger.info("Testing result on epoch: %i, test loss: %f " % (epoch, avg_test_loss))
- #print("Testing result on epoch: %i, test loss: %f " % (epoch, avg_test_loss))
- self.logger.info(' tp: '+str(avg_metrics[0])+ ' tn: '+ str(avg_metrics[1])+ ' fp: '+str(avg_metrics[2])+ ' fn: '+ str(avg_metrics[3])+
- ' precision: '+str(avg_metrics[4])+ ' recall: '+ str(avg_metrics[5])+ ' accuracy: '+ str(avg_metrics[6])+
- ' specificity '+ str(avg_metrics[7])+ ' f1 '+ str(avg_metrics[8]))
- '''tf.print(' tp: ', avg_metrics[0], ' tn: ', avg_metrics[1], ' fp: ', avg_metrics[2], ' fn: ', avg_metrics[3],
- ' precision: ', avg_metrics[4], ' recall: ', avg_metrics[5], ' accuracy: ', avg_metrics[6],
- ' specificity ', avg_metrics[7], ' f1 ', avg_metrics[8], output_stream=sys.stdout)
- '''
- if best_val_loss is None or best_val_loss > avg_test_loss:
- best_val_loss, best_val_epoch = avg_test_loss, epoch
- tl.files.save_npz(voxelCNN.all_weights, name=model_path+'model_'+str(epoch)+'.npz')
- #ckpt_manager.save()
- self.logger.info('Saved model on epoch: %i, for better test.'% epoch)
- #print('Saved model')
- elif best_train_loss is None or best_train_loss > avg_train_loss:
- best_train_loss, best_train_epoch = avg_train_loss, epoch
- tl.files.save_npz(voxelCNN.all_weights, name=model_path+'model_'+str(epoch)+'.npz')
- self.logger.info('Saved model on epoch: %i, for better train.'% epoch)
- if best_val_epoch < epoch - max_patience and best_train_epoch < epoch - max_patience:
- self.logger.info('Early stopping on epoch: %i'% epoch)
- #print('Early stopping')
- break
-
- '''def restore_voxelCNN(self,model_path): #用于推理时加载模型
- voxelCNN = self.build_voxelCNN_model()
- #voxelCNN.summary()
- learning_rate = 1e-3
- optimizer = keras.optimizers.Adam(lr=learning_rate)
- vars_to_load = {"Weight_biases": voxelCNN.trainable_variables, "optimizer": optimizer}
- checkpoint = tf.train.Checkpoint(**vars_to_load)
- # Restore variables from latest checkpoint.
- latest_ckpt = tf.train.latest_checkpoint(model_path)
- if latest_ckpt is not None:
- checkpoint.restore(latest_ckpt)
- else:
- print('Can not load model: ', model_path)
- return voxelCNN
- '''
-
- if __name__ == "__main__":
- # Command line main application function.
- parser = argparse.ArgumentParser(description='Encoding octree')
- parser.add_argument("-blocksize", '--block_size', type=int,
- default=64,
- help='input size of block')
- parser.add_argument("-nfilters", '--n_filters', type=int,
- default=64,
- help='Number of filters')
- parser.add_argument("-batch", '--batch_size', type=int,
- default=8,
- help='batch size')
- parser.add_argument("-epochs", '--epochs', type=int,
- default=50,
- help='number of training epochs')
- parser.add_argument("-inputmodel", '--savedmodel', type=str, help='path to saved model file')
- #parser.add_argument("-loss", '--loss_img_name', type=str, help='name of loss image')
- parser.add_argument("-outputmodel", '--saving_model_path', type=str, default='Model/voxeldnn64_tl/', help='path to output model file')
- parser.add_argument("-dataset", '--dataset', action='append', type=str, help='path to dataset ')
- parser.add_argument("-portion_data", '--portion_data', type=float,
- default=1,
- help='portion of dataset to put in training, densier pc are selected first')
- args = parser.parse_args()
- block_size=args.block_size
- #batch, epochs, loss_img_path, model_path=[args.batch_size,args.epochs,args.loss_img_name,args.saving_model_path]
- voxelCNN=VoxelCNN(depth=block_size,height=block_size,width=block_size,n_channel=1,output_channel=2,residual_blocks=2,n_filters=args.n_filters)
- voxelCNN.init_build(tl.layers.Input(shape=(1, block_size, block_size, block_size, 1))) #这一步要有,走一遍前向推理,把前面没填的in_channels这些参数补上,再执行build
- trainer = Trainer(model=voxelCNN, output_path=args.saving_model_path, dhw=block_size, output_channel=2)
- trainer.train_voxelCNN(args.batch_size,args.epochs,args.saving_model_path, args.dataset,args.portion_data,args.savedmodel)
- #python3 -m training.voxel_dnn_training -blocksize 64 -nfilters 64 -inputmodel Model/voxelDNN_CAT1 -outputmodel Model/voxelDNN_CAT1 -dataset /datnguyen_dataset/database/Modelnet40/ModelNet40_200_pc512_oct3/ -dataset /datnguyen_dataset/database/CAT1/cat1_selected_vox10_oct4/ -batch 8 -epochs 30
- #python3 -m training.voxel_mixing_context -epoch 50 -blocksize 64 -outputmodel Model/voxelDnnSuperRes/ -inputmodel Model/voxelDnnSuperRes -dataset /datnguyen_dataset/database/Microsoft/10bitdepth_selected_oct4/ -dataset /datnguyen_dataset/database/MPEG/selected_8i_oct4/ -dataset /datnguyen_dataset/database/Modelnet40/ModelNet40_200_pc512_oct3/ -batch 8 -nfilters 64
|