From 37a7eea82b5cd20053d34a7570eb0a80ecd60580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:41:22 +0000 Subject: [PATCH 01/16] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/Pix2Vox=5FID1284=5Ffor=5FTensorFlow/Code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Pix2Vox_ID1284_for_TensorFlow/Code/.keep | 0 .../Code/binvox_rw.py | 284 ------- .../Code/demo_AttSets.py | 186 ----- .../Code/export_obj.py | 45 -- .../Code/main_AttSets.py | 719 ----------------- .../Code/main_AttSets_bak.py | 722 ------------------ .../Code/modelarts_entry.py | 55 -- .../Code/npu_train.sh | 59 -- .../Code/ops_info.json | 10 - .../Code/tools.py | 482 ------------ .../Code/voxel.py | 89 --- 11 files changed, 2651 deletions(-) delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/.keep delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/binvox_rw.py delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/demo_AttSets.py delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/export_obj.py delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/main_AttSets.py delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/main_AttSets_bak.py delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/modelarts_entry.py delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/npu_train.sh delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/ops_info.json delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/tools.py delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/voxel.py diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/.keep b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/binvox_rw.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/binvox_rw.py deleted file mode 100644 index 40005d24a..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/binvox_rw.py +++ /dev/null @@ -1,284 +0,0 @@ -# Copyright (C) 2012 Daniel Maturana -# This file is part of binvox-rw-py. -# -# binvox-rw-py is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# binvox-rw-py is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# -# ============================================================================ -# Copyright 2021 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# along with binvox-rw-py. If not, see . -# - -""" -Binvox to Numpy and back. ->>> import numpy as np ->>> import binvox_rw ->>> with open('chair.binvox', 'rb') as f: -... m1 = binvox_rw.read_as_3d_array(f) -... ->>> m1.dims -[32, 32, 32] ->>> m1.scale -41.133000000000003 ->>> m1.translate -[0.0, 0.0, 0.0] ->>> with open('chair_out.binvox', 'wb') as f: -... m1.write(f) -... ->>> with open('chair_out.binvox', 'rb') as f: -... m2 = binvox_rw.read_as_3d_array(f) -... ->>> m1.dims==m2.dims -True ->>> m1.scale==m2.scale -True ->>> m1.translate==m2.translate -True ->>> np.all(m1.data==m2.data) -True ->>> with open('chair.binvox', 'rb') as f: -... md = binvox_rw.read_as_3d_array(f) -... ->>> with open('chair.binvox', 'rb') as f: -... ms = binvox_rw.read_as_coord_array(f) -... ->>> data_ds = binvox_rw.dense_to_sparse(md.data) ->>> data_sd = binvox_rw.sparse_to_dense(ms.data, 32) ->>> np.all(data_sd==md.data) -True ->>> # the ordering of elements returned by numpy.nonzero changes with axis ->>> # ordering, so to compare for equality we first lexically sort the voxels. ->>> np.all(ms.data[:, np.lexsort(ms.data)] == data_ds[:, np.lexsort(data_ds)]) -True -""" -from npu_bridge.npu_init import * - -import numpy as np - -class Voxels(object): - """ Holds a binvox model. - data is either a three-dimensional numpy boolean array (dense representation) - or a two-dimensional numpy float array (coordinate representation). - dims, translate and scale are the model metadata. - dims are the voxel dimensions, e.g. [32, 32, 32] for a 32x32x32 model. - scale and translate relate the voxels to the original model coordinates. - To translate voxel coordinates i, j, k to original coordinates x, y, z: - x_n = (i+.5)/dims[0] - y_n = (j+.5)/dims[1] - z_n = (k+.5)/dims[2] - x = scale*x_n + translate[0] - y = scale*y_n + translate[1] - z = scale*z_n + translate[2] - """ - - def __init__(self, data, dims, translate, scale, axis_order): - self.data = data - self.dims = dims - self.translate = translate - self.scale = scale - assert (axis_order in ('xzy', 'xyz')) - self.axis_order = axis_order - - def clone(self): - data = self.data.copy() - dims = self.dims[:] - translate = self.translate[:] - return Voxels(data, dims, translate, self.scale, self.axis_order) - - def write(self, fp): - write(self, fp) - -def read_header(fp): - """ Read binvox header. Mostly meant for internal use. - """ - line = fp.readline().strip() - if not line.startswith(b'#binvox'): - raise IOError('Not a binvox file') - dims = list(map(int, fp.readline().strip().split(b' ')[1:])) - translate = list(map(float, fp.readline().strip().split(b' ')[1:])) - scale = list(map(float, fp.readline().strip().split(b' ')[1:]))[0] - line = fp.readline() - return dims, translate, scale - -def read_as_3d_array(fp, fix_coords=True): - """ Read binary binvox format as array. - Returns the model with accompanying metadata. - Voxels are stored in a three-dimensional numpy array, which is simple and - direct, but may use a lot of memory for large models. (Storage requirements - are 8*(d^3) bytes, where d is the dimensions of the binvox model. Numpy - boolean arrays use a byte per element). - Doesn't do any checks on input except for the '#binvox' line. - """ - dims, translate, scale = read_header(fp) - raw_data = np.frombuffer(fp.read(), dtype=np.uint8) - # if just using reshape() on the raw data: - # indexing the array as array[i,j,k], the indices map into the - # coords as: - # i -> x - # j -> z - # k -> y - # if fix_coords is true, then data is rearranged so that - # mapping is - # i -> x - # j -> y - # k -> z - values, counts = raw_data[::2], raw_data[1::2] - data = np.repeat(values, counts).astype(np.bool) - data = data.reshape(dims) - if fix_coords: - # xzy to xyz TODO the right thing - data = np.transpose(data, (0, 2, 1)) - axis_order = 'xyz' - else: - axis_order = 'xzy' - return Voxels(data, dims, translate, scale, axis_order) - -def read_as_coord_array(fp, fix_coords=True): - """ Read binary binvox format as coordinates. - Returns binvox model with voxels in a "coordinate" representation, i.e. an - 3 x N array where N is the number of nonzero voxels. Each column - corresponds to a nonzero voxel and the 3 rows are the (x, z, y) coordinates - of the voxel. (The odd ordering is due to the way binvox format lays out - data). Note that coordinates refer to the binvox voxels, without any - scaling or translation. - Use this to save memory if your model is very sparse (mostly empty). - Doesn't do any checks on input except for the '#binvox' line. - """ - dims, translate, scale = read_header(fp) - raw_data = np.frombuffer(fp.read(), dtype=np.uint8) - - values, counts = raw_data[::2], raw_data[1::2] - - sz = np.prod(dims) - index, end_index = 0, 0 - end_indices = np.cumsum(counts) - indices = np.concatenate(([0], end_indices[:-1])).astype(end_indices.dtype) - - values = values.astype(np.bool) - indices = indices[values] - end_indices = end_indices[values] - - nz_voxels = [] - for index, end_index in zip(indices, end_indices): - nz_voxels.extend(range(index, end_index)) - nz_voxels = np.array(nz_voxels) - # TODO are these dims correct? - # according to docs, - # index = x * wxh + z * width + y; // wxh = width * height = d * d - - x = nz_voxels / (dims[0]*dims[1]) - zwpy = nz_voxels % (dims[0]*dims[1]) # z*w + y - z = zwpy / dims[0] - y = zwpy % dims[0] - if fix_coords: - data = np.vstack((x, y, z)) - axis_order = 'xyz' - else: - data = np.vstack((x, z, y)) - axis_order = 'xzy' - - #return Voxels(data, dims, translate, scale, axis_order) - return Voxels(np.ascontiguousarray(data), dims, translate, scale, axis_order) - -def dense_to_sparse(voxel_data, dtype=np.int): - """ From dense representation to sparse (coordinate) representation. - No coordinate reordering. - """ - if voxel_data.ndim!=3: - raise ValueError('voxel_data is wrong shape; should be 3D array.') - return np.asarray(np.nonzero(voxel_data), dtype) - -def sparse_to_dense(voxel_data, dims, dtype=np.bool): - if voxel_data.ndim!=2 or voxel_data.shape[0]!=3: - raise ValueError('voxel_data is wrong shape; should be 3xN array.') - if np.isscalar(dims): - dims = [dims]*3 - dims = np.atleast_2d(dims).T - # truncate to integers - xyz = voxel_data.astype(np.int) - # discard voxels that fall outside dims - valid_ix = ~np.any((xyz < 0) | (xyz >= dims), 0) - xyz = xyz[:,valid_ix] - out = np.zeros(dims.flatten(), dtype=dtype) - out[tuple(xyz)] = True - return out - -#def get_linear_index(x, y, z, dims): - #""" Assuming xzy order. (y increasing fastest. - #TODO ensure this is right when dims are not all same - #""" - #return x*(dims[1]*dims[2]) + z*dims[1] + y - -def write(voxel_model, fp): - """ Write binary binvox format. - Note that when saving a model in sparse (coordinate) format, it is first - converted to dense format. - Doesn't check if the model is 'sane'. - """ - if voxel_model.data.ndim==2: - # TODO avoid conversion to dense - dense_voxel_data = sparse_to_dense(voxel_model.data, voxel_model.dims) - else: - dense_voxel_data = voxel_model.data - - fp.write('#binvox 1\n') - fp.write('dim '+' '.join(map(str, voxel_model.dims))+'\n') - fp.write('translate '+' '.join(map(str, voxel_model.translate))+'\n') - fp.write('scale '+str(voxel_model.scale)+'\n') - fp.write('data\n') - if not voxel_model.axis_order in ('xzy', 'xyz'): - raise ValueError('Unsupported voxel model axis order') - - if voxel_model.axis_order=='xzy': - voxels_flat = dense_voxel_data.flatten() - elif voxel_model.axis_order=='xyz': - voxels_flat = np.transpose(dense_voxel_data, (0, 2, 1)).flatten() - - # keep a sort of state machine for writing run length encoding - state = voxels_flat[0] - ctr = 0 - for c in voxels_flat: - if c==state: - ctr += 1 - # if ctr hits max, dump - if ctr==255: - fp.write(chr(state)) - fp.write(chr(ctr)) - ctr = 0 - else: - # if switch state, dump - fp.write(chr(state)) - fp.write(chr(ctr)) - state = c - ctr = 1 - # flush out remainders - if ctr > 0: - fp.write(chr(state)) - fp.write(chr(ctr)) - -if __name__ == '__main__': - import doctest - doctest.testmod() - diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/demo_AttSets.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/demo_AttSets.py deleted file mode 100644 index aad6f0277..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/demo_AttSets.py +++ /dev/null @@ -1,186 +0,0 @@ -# -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -# Copyright 2021 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from npu_bridge.npu_init import * -import tensorflow as tf -import os -import sys -sys.path.append('..') -import tools as tools -import numpy as np -import matplotlib.pyplot as plt - -GPU='0' - - -def load_real_rgbs(test_mv=5): - obj_rgbs_folder ='/home/ma-user/work/3DOR/Data/amazon_real_rgbs/airfilter/' - rgbs = [] - rgbs_views = sorted(os.listdir(obj_rgbs_folder)) - for v in rgbs_views: - if not v.endswith('png'): continue - - rgbs.append(tools.Data.load_single_X_rgb_r2n2(obj_rgbs_folder + v, train=False)) - - rgbs = np.asarray(rgbs) - x_sample = rgbs[0:test_mv, :, :, :].reshape(1, test_mv, 127, 127, 3) - return x_sample, None - -def load_shapenet_rgbs(test_mv=8): - obj_rgbs_folder = './Data_sample/ShapeNetRendering/03001627/1a6f615e8b1b5ae4dbbc9440457e303e/rendering/' - obj_gt_vox_path ='./Data_sample/ShapeNetVox32/03001627/1a6f615e8b1b5ae4dbbc9440457e303e/model.binvox' - rgbs=[] - rgbs_views = sorted(os.listdir(obj_rgbs_folder)) - for v in rgbs_views: - if not v.endswith('png'): continue - rgbs.append(tools.Data.load_single_X_rgb_r2n2(obj_rgbs_folder + v, train=False)) - rgbs = np.asarray(rgbs) - x_sample = rgbs[0:test_mv, :, :, :].reshape(1, test_mv, 127, 127, 3) - y_true = tools.Data.load_single_Y_vox(obj_gt_vox_path) - return x_sample, y_true - -def ttest_demo(): -# model_path = './Model_released/' - model_path='/home/ma-user/work/3DOR/Model/train_mod/' - if not os.path.isfile(model_path + 'model.cptk.data-00000-of-00001'): - print ('please download our released model first!') - return - - config = tf.ConfigProto(allow_soft_placement=True) - config.gpu_options.visible_device_list = GPU - - - - with tf.Session(config=npu_config_proto(config_proto=config)) as sess: - saver = tf.train.import_meta_graph(model_path + 'model.cptk.meta', clear_devices=True) - saver.restore(sess, model_path + 'model.cptk') - print ('model restored!') - - # graph = tf.get_default_graph() - # print(graph.get_operations()) - - X = tf.get_default_graph().get_tensor_by_name("Placeholder:0") - Y_pred = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_9:0") - - plot_data_8 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_8:0") - plot_data_7 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_7:0") #############(1,1024) - plot_data_6 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_6:0") #############(1,1024) - plot_data_5 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_5:0") - plot_data_4 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_4:0") - plot_data_3 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_3:0") - plot_data_2 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_2:0") - plot_data_1 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_1:0") - - -# print("X: ", X.shape) #Tensor("Placeholder:0", shape=(?, ?, 127, 127, 3), dtype=float32) -# print(Y_pred) #Tensor("r2n/Reshape_9:0", shape=(?, 32, 32, 32), dtype=float32) - -# print("x_sample: ", x_sample.shape) -# print("x_sample_data: ", type(x_sample[:,:,:,:,1])) -# print(y_pred.shape) ###############################(1, 32, 32, 32) ############################## - - -# x_sample, gt_vox = load_shapenet_rgbs() - x_sample, gt_vox = load_real_rgbs() - - - plot_buf_1= tf.reshape(plot_data_1, [-1, 32, 32, 1]) - plot_buf_2= tf.reshape(plot_data_2, [-1, 32, 32, 1]) - plot_buf_3= tf.reshape(plot_data_3, [-1, 32, 32, 1]) - plot_buf_4= tf.reshape(plot_data_4, [-1, 32, 32, 1]) - plot_buf_5= tf.reshape(plot_data_5, [-1, 32, 32, 1]) - plot_buf_6= tf.reshape(plot_data_6, [-1, 32, 32, 1]) - plot_buf_7= tf.reshape(plot_data_7, [-1, 32, 32, 1]) - plot_buf_8= tf.reshape(plot_data_8, [-1, 32, 32, 1]) - - -# tf.summary.image("RESHAPE_1", plot_buf_1) -# tf.summary.image("RESHAPE_2", plot_buf_2) -# tf.summary.image("RESHAPE_3", plot_buf_3) -# tf.summary.image("RESHAPE_4", plot_buf_4) -# tf.summary.image("RESHAPE_5", plot_buf_5) -# tf.summary.image("RESHAPE_6", plot_buf_6) -# tf.summary.image("RESHAPE_7", plot_buf_7) -# tf.summary.image("RESHAPE_8", plot_buf_8) - - - summary_8 = tf.summary.image("RESHAPE_8", plot_buf_8) - summary_7 = tf.summary.image("RESHAPE_7", plot_buf_7) - summary_6 = tf.summary.image("RESHAPE_6", plot_buf_6) - summary_5 = tf.summary.image("RESHAPE_5", plot_buf_5) - summary_4 = tf.summary.image("RESHAPE_4", plot_buf_4) - summary_3 = tf.summary.image("RESHAPE_3", plot_buf_3) - summary_2 = tf.summary.image("RESHAPE_2", plot_buf_2) - summary_1 = tf.summary.image("RESHAPE_1", plot_buf_1) - - -# summary_op = tf.summary.image("RESHAPE_4", plot_buf_4) -# with tf.Session() as sess: -# y_pred,1_summary,2_summary = sess.run([Y_pred,summary_op_1,summary_op_2], feed_dict={X: x_sample}) - - y_pred,summary_pred_1,summary_pred_2,summary_pred_3,summary_pred_4,summary_pred_5,summary_pred_6,summary_pred_7,summary_pred_8 = sess.run([Y_pred,summary_1,summary_2,summary_3,summary_4,summary_5,summary_6,summary_7,summary_8], feed_dict={X: x_sample}) - -# Write summary tf.summary.FileWriter - writer = tf.summary.FileWriter('./logs') - - - writer.add_summary(summary_pred_1) - writer.add_summary(summary_pred_2) - writer.add_summary(summary_pred_3) - writer.add_summary(summary_pred_4) - writer.add_summary(summary_pred_5) - writer.add_summary(summary_pred_6) - writer.add_summary(summary_pred_7) - writer.add_summary(summary_pred_8) - - writer.close() - -# sys.exit(). sys.exit() - ###### to visualize - th = 0.25 - y_pred[y_pred>=th]=1 - y_pred[y_pred= 0.5] = 1 - prediction[prediction < 0.5] = 0 - intersection = np.sum(np.logical_and(prediction,gt)) - union = np.sum(np.logical_or(prediction,gt)) - IoU = float(intersection) / float(union) - return IoU -##################################### -def refiner_network(volumes_in): - with tf.device('/cpu:0'): - with tf.variable_scope('ref_enc'): - - input_volumes_32 = tf.reshape(volumes_in, [-1, vox_res32, vox_res32, vox_res32, 1],name="ref_net_in") - - print("input_volumes_32_shape" , input_volumes_32.shape) #input_volumes_32_shape (?,32,32,32,1) - - rn1=Conv3D(filters=32, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_c1')(input_volumes_32) - rn2=BatchNormalization()(rn1) - rn3=LeakyReLU(alpha=.2)(rn2) - print("rn3.shape",rn3.shape) # rn3.shape (?, 32, 32, 32, 32) - volumes_16_l =MaxPooling3D(pool_size=(2, 2, 2),name='ref_m1')(rn3) - - print("volumes_16_l_shape" , volumes_16_l.shape) #volumes_16_l_shape (?,16,16,16,32) - - rn5=Conv3D(filters=64, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_c2')(volumes_16_l) - rn6=BatchNormalization()(rn5) - rn7=LeakyReLU(alpha=.2)(rn6) - print("rn7.shape",rn7.shape) #rn7.shape (?, 16, 16, 16, 64) - volumes_8_l =MaxPooling3D(pool_size=(2, 2, 2),name='ref_m2')(rn7) - - print("volumes_8_l_shape" ,volumes_8_l.shape) #volumes_8_l_shape (?,8,8,8,64) - - rn9=Conv3D(filters=128, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_c3')(volumes_8_l) - rn10=BatchNormalization()(rn9) - rn11=LeakyReLU(alpha=.2)(rn10) - print("rn11.shape",rn11.shape) #rn11.shape (?, 8, 8, 8, 128) - volumes_4_l =MaxPooling3D(pool_size=(2, 2, 2),name='ref_m3')(rn11) - - print("volumes_4_l_shape" , volumes_4_l.shape) #volumes_4_l_shape (?,4,4,4,128) - - flatten_features=tf.reshape(volumes_4_l , [-1,8192],name="ref_fc1_in") - with tf.variable_scope('ref_fc'): - - fc1=Dense(units=2048, activation='relu',name='ref_fc1')(flatten_features) -# fc1=tanh(fc1) - - fc1=relu(fc1, alpha=0.0, max_value=None, threshold=0.0) - - print("fc1_shape",fc1.shape) #fc1_shape (?,4,4,4,2048) - - fc2=Dense(units=8192, activation='relu',name='ref_fc2')(fc1) -# fc2=tanh(fc2) - fc2=relu(fc2, alpha=0.0, max_value=None, threshold=0.0) - - print("fc2_shape",fc2.shape) #fc2_shape (?,4,4,4,8192) - - fc2=tf.reshape(fc2, [-1, 4,4,4,128],name="ref_fc2_out") - - with tf.variable_scope('ref_Dec'): - - reshaped_1=Add()([fc2,volumes_4_l]) - - print("reshaped_1.shape",reshaped_1.shape) #reshaped_1.shape (?,4,4,4,128) - - rn13=Conv3DTranspose(filters=64, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_d1',strides=(2, 2, 2))(reshaped_1) - - rn14=BatchNormalization()(rn13) - volumes_4_r=relu(rn14, alpha=0.0, max_value=None, threshold=0.0) - - print("volumes_4_r_shape",volumes_4_r.shape) #volumes_4_r_shape (?,8,8,8,64) - - reshaped_2=Add() ([volumes_4_r,volumes_8_l]) - - print("reshaped_2_shape",reshaped_2.shape) #volumes_2_shape (?,8,8,8,64) - - - rn16=Conv3DTranspose(filters=32, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_d2',strides=(2, 2, 2))(reshaped_2) - rn17=BatchNormalization()(rn16) - volumes_8_r =relu(rn17, alpha=0.0, max_value=None, threshold=0.0) - - reshaped_3=Add()([volumes_8_r,volumes_16_l]) - - print("reshaped_3_shape",reshaped_3.shape) #reshaped_3_shape (?,16,16,16,32) - - - rn19=Conv3DTranspose(filters=1, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_d3',strides=(2, 2, 2))(volumes_8_r) - print("rn19_shape",rn19.shape) #rn19_shape (?, ?, ?, ?, 1) -# volumes_16_r= tf.nn.sigmoid(rn19,name='ref_sigmoid1') -# reshape_4=volumes_16_r #################### - - reshape_4=Add()([rn19,input_volumes_32]) - reshape_4=(reshape_4*0.5) - print("reshape_4_5",reshape_4.shape) #reshape_4_5 (?,32,32,32,1) - - reshape_4= tf.nn.sigmoid(reshape_4,name='ref_sigmoid1') - - - print("reshape_4_sig_shape",reshape_4.shape) #reshape_4_sig_shape (?,32,32,32,1) - - reshape_5=tf.reshape(reshape_4, [-1, vox_res32, vox_res32, vox_res32],name="ref_out") - - return reshape_5 - -def attsets_fc(x, out_ele_num): - with tf.variable_scope('att_fc'): - in_ele_num = tf.shape(x)[1] - in_ele_len = int(x.get_shape()[2]) - out_ele_len = in_ele_len - print("out_ele_len ", out_ele_len) - #################### - x_1st = x - x_1st_tp = tf.reshape(x_1st, [-1, in_ele_len],name="att_in") - weights_1st = tools.Ops.fc(x_1st_tp, out_d=out_ele_num*out_ele_len, name="att") - - ########## option 1 - weights_1st = weights_1st - ########## option 2 -# weights_1st = tf.nn.tanh(weights_1st) - - #################### - weights_1st = tf.reshape(weights_1st, [-1, in_ele_num, out_ele_num, out_ele_len],name="att_fc_out") - weights_1st = tf.nn.softmax(weights_1st, 1) - x_1st = tf.tile(x_1st[:,:,None,:], [1,1,out_ele_num,1]) - x_1st = x_1st*weights_1st - x_1st = tf.reduce_sum(x_1st, axis=1) - x_1st = tf.reshape(x_1st, [-1, out_ele_num*out_ele_len],name="att_out") - return x_1st, weights_1st - -##################################### -class Network: - def __init__(self): - self.train_mod_dir = './Model/train_mod/' - self.train_sum_dir = './Model/train_sum/' - self.test_res_dir = './Model/test_res/' - self.test_sum_dir = './Model/test_sum/' - - print ('re_train : ', re_train) - if os.path.exists(self.test_res_dir): - if re_train: - print ('test_res_dir and files kept!') - else: - shutil.rmtree(self.test_res_dir) - os.makedirs(self.test_res_dir) - print ('test_res_dir: deleted and then created!') - else: - os.makedirs(self.test_res_dir) - print ('test_res_dir: created!') - - if os.path.exists(self.train_mod_dir): - if re_train: - if os.path.exists(self.train_mod_dir + 'model.cptk.data-00000-of-00001'): - print ('model found! will be reused!') - else: - print ('model not found! error!') - #exit() - else: - shutil.rmtree(self.train_mod_dir) - os.makedirs(self.train_mod_dir) - print ('train_mod_dir: deleted and then created!') - else: - os.makedirs(self.train_mod_dir) - print ('train_mod_dir: created!') - - if os.path.exists(self.train_sum_dir): - if re_train: - print ('train_sum_dir and files kept!') - else: - shutil.rmtree(self.train_sum_dir) - os.makedirs(self.train_sum_dir) - print ('train_sum_dir: deleted and then created!') - else: - os.makedirs(self.train_sum_dir) - print ('train_sum_dir: created!') - - if os.path.exists(self.test_sum_dir): - if re_train: - print ('test_sum_dir and files kept!') - else: - shutil.rmtree(self.test_sum_dir) - os.makedirs(self.test_sum_dir) - print ('test_sum_dir: deleted and then created!') - else: - os.makedirs(self.test_sum_dir) - print ('test_sum_dir: created!') - - def base_r2n2(self, X_rgb): - with tf.variable_scope('Encoder'): - im_num = tf.shape(X_rgb)[1] - - [_, _, d1, d2, cc] = X_rgb.get_shape() - X_rgb = tf.reshape(X_rgb, [-1, int(d1), int(d2), int(cc)],name="en_in") - print("Network Structure") - print("base_r2n2",X_rgb.shape) #base_r2n2 (?, 127, 127, 3) - -# plot_buf_1= tf.reshape(X_rgb, [-1, 127, 127, 3]) ################### -# tf.summary.image("Input", plot_buf_1)############################### - - en_c = [96, 128, 256, 256, 256, 256] - l1 = tools.Ops.xxlu(tools.Ops.conv2d(X_rgb, k=7, out_c=en_c[0], str=1, name='en_c1'), label='lrelu') - print("l1_r2n",l1.shape) #l1_r2n (?, 127, 127, 96) - - - - l2 = tools.Ops.xxlu(tools.Ops.conv2d(l1, k=3, out_c=en_c[0], str=1, name='en_c2'), label='lrelu') - l2 = tools.Ops.maxpool2d(l2, k=2, s=2, name='en_mp1') - print("l2_r2n",l2.shape) #l2_r2n (?, 64, 64, 96) - -# plot_buf_1= tf.reshape(l2, [-1, 32, 32, 3]) ######################### -# tf.summary.image("L2_MP_en", plot_buf_1)############################# - - l3 = tools.Ops.xxlu(tools.Ops.conv2d(l2, k=3, out_c=en_c[1], str=1, name='en_c3'), label='lrelu') - print("l3_r2n",l3.shape) #l3_r2n (?, 64, 64, 128) - l4 = tools.Ops.xxlu(tools.Ops.conv2d(l3, k=3, out_c=en_c[1], str=1, name='en_c4'), label='lrelu') - print("l4_r2n",l4.shape) #l4_r2n (?, 64, 64, 128) - l22 = tools.Ops.conv2d(l2, k=1, out_c=en_c[1], str=1, name='en_c22') - print("l22_r2n",l22.shape) #l22_r2n (?, 64, 64, 128) - l4 = l4 + l22 - l4 = tools.Ops.maxpool2d(l4, k=2, s=2, name='en_mp2') - print("l4+l22_r2n",l4.shape) #l4+l22_r2n (?, 32, 32, 128) - -# plot_buf_1= tf.reshape(l4, [-1, 32, 32, 3]) ########################## -# tf.summary.image("l4+l22_en", plot_buf_1)############################# - - l5 = tools.Ops.xxlu(tools.Ops.conv2d(l4, k=3, out_c=en_c[2], str=1, name='en_c5'), label='lrelu') - print("l5_r2n",l5.shape) #l5_r2n (?, 32, 32, 256) - l6 = tools.Ops.xxlu(tools.Ops.conv2d(l5, k=3, out_c=en_c[2], str=1, name='en_c6'), label='lrelu') - print("l6_r2n",l6.shape) #l6_r2n (?, 32, 32, 256) - l44 = tools.Ops.conv2d(l4, k=1, out_c=en_c[2], str=1, name='en_c44') - print("l44_r2n",l44.shape) #l44_r2n (?, 32, 32, 256) - l6 = l6 + l44 - l6 = tools.Ops.maxpool2d(l6, k=2, s=2, name='en_mp3') - print("l6+l44_r2n",l6.shape) #l6+l44_r2n (?, 16, 16, 256) - -# plot_buf_1= tf.reshape(l6, [-1, 16, 16, 3]) ########################## -# tf.summary.image("l6+l44_en", plot_buf_1)############################# - - l7 = tools.Ops.xxlu(tools.Ops.conv2d(l6, k=3, out_c=en_c[3], str=1, name='en_c7'), label='lrelu') - print("l7_r2n",l7.shape) #l7_r2n (?, 16, 16, 256) - l8 = tools.Ops.xxlu(tools.Ops.conv2d(l7, k=3, out_c=en_c[3], str=1, name='en_c8'), label='lrelu') - print("l8_r2n",l8.shape)#l8_r2n (?, 16, 16, 256) -# l66=tools.Ops.conv2d(l6, k=1, out_c=en_c[3], str=1, name='en_c66') -# print("l66_r2n",l66.shape) -# l8=l8+l66 - l8 = tools.Ops.maxpool2d(l8, k=2, s=2, name='en_mp4') - print("l8_r2n",l8.shape) #l8_r2n (?, 8, 8, 256) - -# plot_buf_1= tf.reshape(l8, [-1, 8, 8, 3]) ######################## -# tf.summary.image("l8_en", plot_buf_1)############################# - - l9 = tools.Ops.xxlu(tools.Ops.conv2d(l8, k=3, out_c=en_c[4], str=1, name='en_c9'), label='lrelu') - print("l9_r2n",l9.shape) #l9_r2n (?, 8, 8, 256) - l10 = tools.Ops.xxlu(tools.Ops.conv2d(l9, k=3, out_c=en_c[4], str=1, name='en_c10'), label='lrelu') - print("l10_r2n",l10.shape)#l10_r2n (?, 8, 8, 256) - l88 = tools.Ops.conv2d(l8, k=1, out_c=en_c[4], str=1, name='en_c88') - print("l88_r2n",l88.shape) #l88_r2n (?, 8, 8, 256) - l10 = l10 + l88 - l10 = tools.Ops.maxpool2d(l10, k=2, s=2, name='en_mp5') - print("l10_r2n",l10.shape) #l10_r2n (?, 4, 4, 256) - -# plot_buf_1= tf.reshape(l10, [-1, 4, 4, 3]) ######################## -# tf.summary.image("l10_en", plot_buf_1)############################# - - l11 = tools.Ops.xxlu(tools.Ops.conv2d(l10, k=3, out_c=en_c[5], str=1, name='en_c11'), label='lrelu') - print("l11_r2n",l11.shape) #l11_r2n (?, 4, 4, 256) - l12 = tools.Ops.xxlu(tools.Ops.conv2d(l11, k=3, out_c=en_c[5], str=1, name='en_c12'), label='lrelu') - print("l12_r2n",l12.shape) #l12_r2n (?, 4, 4, 256) - l1010 = tools.Ops.conv2d(l10, k=1, out_c=en_c[5], str=1, name='en_c1010') - print("l1010_r2n",l1010.shape) #l1010_r2n (?, 4, 4, 256) - l12 = l12 + l1010 - l12 = tools.Ops.maxpool2d(l12, k=2, s=2, name='en_mp6') - print("l12_r2n",l12.shape) #l12_r2n (?, 2, 2, 256) - -# plot_buf_1= tf.reshape(l12, [-1, 2, 2, 3]) ######################## -# tf.summary.image("l12_en", plot_buf_1)############################# - - [_, d1, d2, cc] = l12.get_shape() - l12 = tf.reshape(l12, [-1, int(d1) * int(d2) * int(cc)],name="en_fc1_in") - print("fc1_input_r2n",l12.shape) #fc1_input_r2n (?, 1024) - fc = tools.Ops.xxlu(tools.Ops.fc(l12, out_d=1024, name='en_fc1'), label='lrelu') - print("fc1_output_r2n",fc.shape)#fc1_output_r2n (?, 1024) - - with tf.variable_scope('Att_Net'): - #### use fc attention - input = tf.reshape(fc, [-1, im_num, 1024],name="Att_fc_in") - print("att_fc_in_r2n",input.shape) #att_fc_in_r2n (?, ?, 1024) - latent_3d, weights = attsets_fc(input, out_ele_num=1) - print("att_fc_out_r2n",latent_3d.shape) #att_fc_out_r2n (?, 1024) - - with tf.variable_scope('Decoder'): - #### - latent_3d = tools.Ops.xxlu(tools.Ops.fc(latent_3d, out_d=4*4*4*128, name='de_fc2'), label='lrelu') - print("fc3_out_r2n",latent_3d.shape) #fc3_out_r2n (?, 8192) - latent_3d = tf.reshape(latent_3d, [-1, 4, 4, 4, 128],name="de_fc2_out") - - #### - - de_c = [128, 128, 128, 64, 32, 1] - - print("d1_in_r2n",latent_3d.shape) #d1_in_r2n (?, 4, 4, 4, 128) - d1 = tools.Ops.xxlu(tools.Ops.deconv3d(latent_3d, k=3, out_c=de_c[1], str=2, name='de_c1'), label='lrelu') - print("d1_out_r2n",d1.shape) #d1_out_r2n (?, 8, 8, 8, 128) - d2 = tools.Ops.xxlu(tools.Ops.deconv3d(d1, k=3, out_c=de_c[1], str=1, name='de_c2'), label='lrelu') - print("d2_out_r2n",d2.shape) #d2_out_r2n (?, 8, 8, 8, 128) - d00 = tools.Ops.deconv3d(latent_3d, k=1, out_c=de_c[1], str=2, name='de_c00') - print("d00_out_r2n",d00.shape)#d00_out_r2n (?, 8, 8, 8, 128) - d2 = d2 + d00 - print("d2+d00_out_r2n",d2.shape)#d2+d00_out_r2n (?, 8, 8, 8, 128) - -# plot_buf_1= tf.reshape(d2, [-1, 8, 8, 4]) ################################ -# tf.summary.image("d2+d00_out_de", plot_buf_1)############################# - - d3 = tools.Ops.xxlu(tools.Ops.deconv3d(d2, k=3, out_c=de_c[2], str=2, name='de_c3'), label='lrelu') - print("d3_out_r2n",d3.shape)#d3_out_r2n (?, 16, 16, 16, 128) - d4 = tools.Ops.xxlu(tools.Ops.deconv3d(d3, k=3, out_c=de_c[2], str=1, name='de_c4'), label='lrelu') - print("d4_out_r2n",d4.shape)#d4_out_r2n (?, 16, 16, 16, 128) - d22 = tools.Ops.deconv3d(d2, k=1, out_c=de_c[2], str=2, name='de_c22') - print("d22_out_r2n",d22.shape)#d22_out_r2n (?, 16, 16, 16, 128) - d4 = d4 + d22 - print("d4+d22_out_r2n",d4.shape)#d4+d22_out_r2n (?, 16, 16, 16, 128) - -# plot_buf_1= tf.reshape(d4, [-1, 16, 16, 4]) ############################## -# tf.summary.image("d4+d22_out_de", plot_buf_1)############################# - - d5 = tools.Ops.xxlu(tools.Ops.deconv3d(d4, k=3, out_c=de_c[3], str=2, name='de_c5'), label='lrelu') - print("d5_out_r2n",d5.shape)#d5_out_r2n (?, 32, 32, 32, 64) - d6 = tools.Ops.xxlu(tools.Ops.deconv3d(d5, k=3, out_c=de_c[3], str=1, name='de_c6'), label='lrelu') - print("d6_out_r2n",d6.shape)#d6_out_r2n (?, 32, 32, 32, 64) - d44 = tools.Ops.deconv3d(d4, k=1, out_c=de_c[3], str=2, name='de_c44') - print("d44_out_r2n",d44.shape)#d44_out_r2n (?, 32, 32, 32, 64) - d6 = d6 + d44 - print("d6+d44_out_r2n",d6.shape) #d6+d44_out_r2n (?, 32, 32, 32, 64) - -# plot_buf_1= tf.reshape(d6, [-1, 32, 32, 4]) ############################## -# tf.summary.image("d6+d44_out_de", plot_buf_1)############################# - - d7 = tools.Ops.xxlu(tools.Ops.deconv3d(d6, k=3, out_c=de_c[4], str=1, name='de_c7'), label='lrelu') - print("d7_out_r2n",d7.shape) #d7_out_r2n (?, 32, 32, 32, 32) - d8 = tools.Ops.xxlu(tools.Ops.deconv3d(d7, k=3, out_c=de_c[4], str=1, name='de_c8'), label='lrelu') - print("d8_out_r2n",d8.shape)#d8_out_r2n (?, 32, 32, 32, 32) - d77 = tools.Ops.xxlu(tools.Ops.deconv3d(d7, k=3, out_c=de_c[4], str=1, name='de_c77'), label='lrelu') - print("d77_out_r2n",d77.shape)#d77_out_r2n (?, 32, 32, 32, 32) - d8 = d8 + d77 - print("d8+d77_out_r2n",d8.shape) #d8+d77_out_r2n (?, 32, 32, 32, 32) - -# plot_buf_1= tf.reshape(d8, [-1, 32, 32, 4]) ############################## -# tf.summary.image("d8+d77_out_de", plot_buf_1)############################# - - d11 = tools.Ops.deconv3d(d8, k=3, out_c=de_c[5], str=1, name='de_c9') - print("d11_out_r2n",d11.shape) #d11_out_r2n (?, 32, 32, 32, 1) - -# plot_buf_1= tf.reshape(d11, [-1, 32, 32,4]) ########################## -# tf.summary.image("Ref_input", plot_buf_1)############################# - - ref_in = tf.reshape(d11, [-1, vox_res32, vox_res32, vox_res32],name="ref_in") ### - - y = tf.nn.sigmoid(d11,name='de_sigmoid') - - att_o = tf.reshape(y, [-1, vox_res32, vox_res32, vox_res32],name="de_out") - - print("att_out_shape",att_o.shape) #att_out_shape (?, 32, 32, 32) - - with tf.variable_scope('ref_net'): - - ref_o=refiner_network(ref_in) - -# plot_buf_1= tf.reshape(ref_o, [-1, 32, 32,4]) ###################### -# tf.summary.image("Ref_Out", plot_buf_1)############################# - - return ref_o,att_o, weights - - def build_graph(self): - img_res = 127 - vox_res = 32 - self.X_rgb = tf.placeholder(shape=[None, None, img_res, img_res, 3], dtype=tf.float32) - self.Y_vox = tf.placeholder(shape=[None, vox_res, vox_res, vox_res], dtype=tf.float32) - self.lr = tf.placeholder(tf.float32) - self.refine_lr = tf.placeholder(tf.float32) - with tf.device('/cpu:0'): - self.Y_pred,self.vae_o, self.weights = self.base_r2n2(self.X_rgb) - tf.summary.histogram('Attsets_Weights', self.weights) - with tf.device('/cpu:0'): - ### rec loss - print ("reached") - with tf.variable_scope('Loss_Fun'): - Y_vox_ = tf.reshape(self.Y_vox, shape=[-1, vox_res ** 3]) - Y_pred_ = tf.reshape(self.Y_pred, shape=[-1, vox_res ** 3]) - vae_o_=tf.reshape(self.vae_o, shape=[-1, vox_res ** 3]) - - self.vae_loss = tf.reduce_mean(-tf.reduce_mean(Y_vox_ * tf.log(vae_o_ + 1e-8), reduction_indices=[1]) - - tf.reduce_mean((1 - Y_vox_) * tf.log(1 - vae_o_ + 1e-8),reduction_indices=[1])) - self.rec_loss = tf.reduce_mean(-tf.reduce_mean(Y_vox_ * tf.log(Y_pred_ + 1e-8), reduction_indices=[1]) - - tf.reduce_mean((1 - Y_vox_) * tf.log(1 - Y_pred_ + 1e-8),reduction_indices=[1])) - sum_rec_loss = tf.summary.scalar('rec_loss', self.rec_loss) - self.sum_merged = sum_rec_loss - tf.summary.histogram('rec_loss', self.rec_loss) - tf.summary.scalar("vae-loss",self.vae_loss) - tf.summary.histogram("vae_loss",self.vae_loss) - self.mean_loss = tf.div(x=tf.math.add(x=self.vae_loss,y=self.rec_loss,name='add_loss'),y=2,name='mean_loss') - tf.summary.histogram("mean_vae_loss",self.mean_loss) - tf.summary.scalar("mean_vae_loss",self.mean_loss) - - with tf.variable_scope('Evaluation_Metric'): - gt_vox=Y_vox_ - self.iou_ref = metric_iou(Y_pred_,gt_vox) - tf.summary.scalar('iou_refiner', self.iou_ref) - tf.summary.histogram('iou_refiner', self.iou_ref) - self.iou_vae = metric_iou(vae_o_,gt_vox) - tf.summary.scalar('iou_vae', self.iou_vae) - tf.summary.histogram("iou_vae",self.iou_vae) - - with tf.variable_scope('Optimization'): - - base_en_var = [var for var in tf.trainable_variables() if var.name.startswith('Encoder/en')] - base_dec_var = [var for var in tf.trainable_variables() if var.name.startswith('Decoder/de')] - att_var = [var for var in tf.trainable_variables() if var.name.startswith('Att_Net/att')] - refine_var = [var for var in tf.trainable_variables() if var.name.startswith('ref_net/ref')] - self.refine_optim = tf.train.AdamOptimizer(learning_rate=self.refine_lr).minimize(self.rec_loss, var_list=refine_var) - self.base_en_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.vae_loss, var_list=base_en_var) - self.base_de_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.vae_loss, var_list=base_dec_var) - self.att_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.vae_loss, var_list=att_var) - - - print ("total weights:",tools.Ops.variable_count()) - self.saver = tf.train.Saver(max_to_keep=1) - - config = tf.ConfigProto(allow_soft_placement=True) - #config.gpu_options.allow_growth = True - #config.gpu_options.visible_device_list = '0,1' - - self.sess = tf.Session(config=npu_config_proto(config_proto=config)) - self.merged = tf.summary.merge_all() - self.sum_writer_train = tf.summary.FileWriter(self.train_sum_dir, self.sess.graph) - self.sum_writer_test = tf.summary.FileWriter(self.test_sum_dir, self.sess.graph) - - ####################### - path = self.train_mod_dir - #path = './Model_released/' # retrain the released model - - if os.path.isfile(path + 'model.cptk.data-00000-of-00001'): - print ("restoring saved model!") - self.saver.restore(self.sess, path + 'model.cptk') - else: - self.sess.run(tf.global_variables_initializer()) - - - return 0 - - def train(self, data): - for epoch in range(0, obs_config.epoch, 1): - train_view_num = obs_config.train_view_num - data.shuffle_train_files(epoch, train_mv=train_view_num) - total_train_batch_num = data.total_train_batch_num #int(len(self.X_rgb_train_files)/(self.batch_size*train_mv)) - print ('total_train_batch_num:', total_train_batch_num) - for i in range(total_train_batch_num): - #### training - X_rgb_bat, Y_vox_bat = data.load_X_Y_train_next_batch(train_mv=train_view_num) - print("multi_view_train_X_rgb_bat : ",X_rgb_bat.shape)#np.asarray(X.append(X_rgb[b*train_mv:(b+1)*train_mv,:])) - - - print(time.ctime()) - - ##### option 1: seperate train, seperate optimize - #if epoch<=30: - # single_view_train=True - # multi_view_train=False - #else: - # single_view_train=False - # multi_view_train=True - - ##### optiion 2: joint train, seperate optimize - single_view_train = True - multi_view_train = False - - if epoch <= 5: - att_lr=.0002 - ref_lr=.0002 - if epoch > 5 and epoch <= 40: - att_lr=.0001 - ref_lr=.0001 - if epoch > 40: - att_lr=.00005 - ref_lr=.00005 - ########### single view train - if single_view_train: - - rgb = np.reshape(X_rgb_bat,[batch_size*train_view_num, 1, 127,127,3]) - print("single_view_train_rgb_input_shape ",rgb.shape) - vox = np.tile(Y_vox_bat[:,None,:,:,:],[1,train_view_num,1,1,1]) - vox = np.reshape(vox, [batch_size*train_view_num, 32,32,32]) - start_time=time.time() - vae_loss_c,eee,ddd, rec_loss_c, sum_train,rrr,mean_vae,iou_ref_,iou_vae_ = self.sess.run([self.vae_loss,self.base_en_optim2,self.base_de_optim2,self.rec_loss,self.merged,self.refine_optim,self.mean_loss,self.iou_ref,self.iou_vae],feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr,self.refine_lr: ref_lr}) - print('TimeHistory = %.4f' % (time.time()-start_time)) - print ('ep:', epoch, 'i:', i, 'train single rec loss:', rec_loss_c) - print ('ep:', epoch, 'i:', i, 'train single vae loss:', vae_loss_c) - print ('ep:', epoch, 'i:', i, 'train single mean_vae loss:',mean_vae) - print ('ep:', epoch, 'i:', i, 'train single ref_iou:',iou_ref_) - print ('ep:', epoch, 'i:', i, 'train single vae_iou:',iou_vae_) - - ########## multi view train - if multi_view_train: - - vae_loss_c,rec_loss_c, _, sum_train,xxx,mean_vae,iou_ref_,iou_vae_ = self.sess.run([self.vae_loss,self.rec_loss, self.att_optim2, self.merged,self.refine_optim,self.mean_loss,self.iou_ref,self.iou_vae],feed_dict={self.X_rgb: X_rgb_bat, self.Y_vox: Y_vox_bat,self.lr: att_lr,self.refine_lr: ref_lr}) - print ('ep:', epoch, 'i:', i, 'train multi rec loss:', rec_loss_c) - print ('ep:', epoch, 'i:', i, 'train multi vae loss:', vae_loss_c) - print('ep:',epoch,'i',i,'train multi mean_vae loss:',mean_vae) - print ('ep:', epoch, 'i:', i, 'train multi ref_iou:',iou_ref_) - print ('ep:', epoch, 'i:', i, 'train multi vae_iou:',iou_vae_) - - ############ - if i % 5 == 0: - self.sum_writer_train.add_summary(sum_train, epoch * total_train_batch_num + i) - - - #### testing - if i % 10 == 0 : - X_rgb_batch, Y_vox_batch = data.load_X_Y_test_next_batch(test_mv=3) - -# vae_pred = tf.get_default_graph().get_tensor_by_name("Decoder/de_out:0") -# ref_pred = tf.get_default_graph().get_tensor_by_name("ref_net/ref_Dec/ref_out:0") -# gt_vox=Y_vox_batch.astype(np.float32) - -# iou_pred = metric_iou(ref_pred,gt_vox) -# tf.summary.scalar("iou",iou_pred) - - rrrr,aaaa,rec_loss_te, qwerty, Y_vox_test_pred, att_pred, sum_test,mean_vae,iou_ref_,iou_vae_ = \ - self.sess.run([self.refine_optim,self.att_optim2,self.rec_loss,self.vae_loss, self.Y_pred,self.weights, self.merged,self.mean_loss,self.iou_ref,self.iou_vae],feed_dict={self.X_rgb: X_rgb_batch, self.Y_vox: Y_vox_batch,self.lr: 0,self.refine_lr: 0}) - - X_rgb_batch = X_rgb_batch.astype(np.float16) - - Y_vox_batch = Y_vox_batch.astype(np.float16) - Y_vox_test_pred = Y_vox_test_pred.astype(np.float16) - att_pred = att_pred.astype(np.float16) - to_save = {'X_test':X_rgb_batch,'Y_test_pred':Y_vox_test_pred,'att_pred':att_pred,'Y_test_true':Y_vox_batch} - scipy.io.savemat(self.test_res_dir+'X_Y_pred_'+str(epoch).zfill(2)+'_'+str(i).zfill(5)+'.mat',to_save,do_compression=True) - - - self.sum_writer_test.add_summary(sum_test, epoch * total_train_batch_num + i) - -# iou_ref=evaluate_voxel_prediction(ref_pred_,gt_vox) -# iou_vae=evaluate_voxel_prediction(vae_pred_,gt_vox) - -# print("Ref_iou:",iou_ref) -# print("Vae_iou:",iou_vae) - -# plot_list_iou.append(iou_ref) -# plot_list_i.append((i/50)) -# graph_plot(plot_list_iou,plot_list_i) - print ('ep:', epoch, 'i:', i, 'test rec loss:', rec_loss_te) - print ('ep:', epoch, 'i:', i, 'test vae loss:', qwerty ) - print ('ep:', epoch, 'i:', i, 'test mean_vae loss:', mean_vae) - print ('ep:', epoch, 'i:', i, 'test ref_iou:',iou_ref_) - print ('ep:', epoch, 'i:', i, 'test vae_iou:',iou_vae_) - - #### model saving - #if i % 100 == 0 : - #self.saver.save( self.sess, save_path=self.train_mod_dir + 'model.cptk' ) - #print ( 'epoch:', epoch, 'i:', i, 'model saved!' ) - - -# plt.show() - - -########## -if __name__ =='__main__': - npu_keras_sess = set_keras_session_npu_config() - - net = Network() #net=object to create instance - - print("network compleated") ### - - net.build_graph() - print("graph compleated") - - - data = tools.Data(config) - print("tools.data compleated") - - - print('training data') - - net.train(data) - - close_session(npu_keras_sess) - - diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/main_AttSets_bak.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/main_AttSets_bak.py deleted file mode 100644 index 65135e0c4..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/main_AttSets_bak.py +++ /dev/null @@ -1,722 +0,0 @@ -# -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -# Copyright 2021 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from npu_bridge.npu_init import * -import tensorflow as tf -import os - -#os.system('pip install scipy==0.19.0') -#os.system('pip install matplotlib==2.0.2') -#os.system('pip install skimage==0.13.0') -#os.system('pip install PyMCubes') - -import shutil -import sys -import scipy.io -sys.path.append('..') -import tools as tools -import numpy as np -import time -from keras.layers import BatchNormalization,Conv3D,MaxPooling3D,Dense,Reshape,Add,LeakyReLU,Conv3DTranspose -from keras.activations import relu,sigmoid,tanh -from keras import models -import copy -import matplotlib.pyplot as plt - -import argparse -#import moxing as mox - - -parser = argparse.ArgumentParser() -parser.add_argument('--train_url', type=str, help='the path model saved') -parser.add_argument("--data_url", type=str, default="./dataset") -obs_config = parser.parse_args() - -################### -#import tflearn -################### - -batch_size = 2 -img_res = 127 -vox_res32 = 32 -total_mv = 24 -GPU0 = '0' -GPU1 = '1' - -re_train = True -#re_train=True -single_view_train = True -multi_view_train = True - -########################### - -plot_list_iou = [] -plot_list_i = [] -iii=0 -config={} # python dictionary -config['batch_size'] = batch_size -config['total_mv'] = total_mv -#config['cat_names'] = ['02691156','02828884','04530566','03636649','03001627'] -#config['cat_names'] = ['02691156','02828884','02933112','02958343','03001627','03211117', -# '03636649','03691459','04090263','04256520','04379243','04401088','04530566'] -config['cat_names'] = ['02691156'] -for name in config['cat_names']: - config['X_rgb_'+name] = obs_config.data_url+'/ShapeNetRendering/'+name+'/' - - config['Y_vox_'+name] = obs_config.data_url+'/ShapeNetVox32/'+name+'/' - - # config['Y_vox_'+name] = '/home/wiproec4/3d reconstruction/attsets/Data_sample/shapenet dataset/ShapeNetVox32/train_1_dataset/'+name+'/' - - -# output : {'batch_size': 1, 'total_mv': 24, 'cat_names': ['03001627'], 'Y_vox_03001627': '/home/wiproec4/3d reconstruction/attsets/Data_sample/#ShapeNetVox32/03001627/', 'X_rgb_03001627': '/home/wiproec4/3d reconstruction/attsets/Data_sample/ShapeNetRendering/03001627/'} - -def metric_iou(prediction, gt): -# labels = tf.greater_equal(gt[gt], 0.5) -# prediction = tf.cast(prediction,tf.int32) - predictions = tf.greater_equal(prediction, 0.5) - gt_=tf.greater_equal(gt, 0.5) - intersection = tf.reduce_sum(tf.cast(tf.logical_and(predictions,gt_),tf.float32)) - union = tf.reduce_sum(tf.cast(tf.math.logical_or(predictions,gt_),tf.float32)) - iou = tf.cast(x = intersection,dtype=tf.float32)/ tf.cast(x = union,dtype=tf.float32) - return iou - - -def graph_plot(iou_value,i_value): - x = i_value - y = iou_value - plt.plot(x, y, color='green', linestyle='dashed', linewidth = 3, marker='o', markerfacecolor='blue', markersize=12) - - plt.ylim(0,2) - plt.xlim(0,500) - - plt.xlabel('Iterations') - - plt.ylabel('IOU') - - plt.title('Refiner Accuracy') - - plt.show() - - -def evaluate_voxel_prediction(prediction, gt): - #""" The prediction and gt are 3 dim voxels. Each voxel has values 1 or 0""" - prediction[prediction >= 0.5] = 1 - prediction[prediction < 0.5] = 0 - intersection = np.sum(np.logical_and(prediction,gt)) - union = np.sum(np.logical_or(prediction,gt)) - IoU = float(intersection) / float(union) - return IoU -##################################### -def refiner_network(volumes_in): - with tf.device('/cpu:0'): - with tf.variable_scope('ref_enc'): - - input_volumes_32 = tf.reshape(volumes_in, [-1, vox_res32, vox_res32, vox_res32, 1],name="ref_net_in") - - print("input_volumes_32_shape" , input_volumes_32.shape) #input_volumes_32_shape (?,32,32,32,1) - - rn1=Conv3D(filters=32, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_c1')(input_volumes_32) - rn2=BatchNormalization()(rn1) - rn3=LeakyReLU(alpha=.2)(rn2) - print("rn3.shape",rn3.shape) # rn3.shape (?, 32, 32, 32, 32) - volumes_16_l =MaxPooling3D(pool_size=(2, 2, 2),name='ref_m1')(rn3) - - print("volumes_16_l_shape" , volumes_16_l.shape) #volumes_16_l_shape (?,16,16,16,32) - - rn5=Conv3D(filters=64, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_c2')(volumes_16_l) - rn6=BatchNormalization()(rn5) - rn7=LeakyReLU(alpha=.2)(rn6) - print("rn7.shape",rn7.shape) #rn7.shape (?, 16, 16, 16, 64) - volumes_8_l =MaxPooling3D(pool_size=(2, 2, 2),name='ref_m2')(rn7) - - print("volumes_8_l_shape" ,volumes_8_l.shape) #volumes_8_l_shape (?,8,8,8,64) - - rn9=Conv3D(filters=128, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_c3')(volumes_8_l) - rn10=BatchNormalization()(rn9) - rn11=LeakyReLU(alpha=.2)(rn10) - print("rn11.shape",rn11.shape) #rn11.shape (?, 8, 8, 8, 128) - volumes_4_l =MaxPooling3D(pool_size=(2, 2, 2),name='ref_m3')(rn11) - - print("volumes_4_l_shape" , volumes_4_l.shape) #volumes_4_l_shape (?,4,4,4,128) - - flatten_features=tf.reshape(volumes_4_l , [-1,8192],name="ref_fc1_in") - with tf.variable_scope('ref_fc'): - - fc1=Dense(units=2048, activation='relu',name='ref_fc1')(flatten_features) -# fc1=tanh(fc1) - - fc1=relu(fc1, alpha=0.0, max_value=None, threshold=0.0) - - print("fc1_shape",fc1.shape) #fc1_shape (?,4,4,4,2048) - - fc2=Dense(units=8192, activation='relu',name='ref_fc2')(fc1) -# fc2=tanh(fc2) - fc2=relu(fc2, alpha=0.0, max_value=None, threshold=0.0) - - print("fc2_shape",fc2.shape) #fc2_shape (?,4,4,4,8192) - - fc2=tf.reshape(fc2, [-1, 4,4,4,128],name="ref_fc2_out") - - with tf.variable_scope('ref_Dec'): - - reshaped_1=Add()([fc2,volumes_4_l]) - - print("reshaped_1.shape",reshaped_1.shape) #reshaped_1.shape (?,4,4,4,128) - - rn13=Conv3DTranspose(filters=64, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_d1',strides=(2, 2, 2))(reshaped_1) - - rn14=BatchNormalization()(rn13) - volumes_4_r=relu(rn14, alpha=0.0, max_value=None, threshold=0.0) - - print("volumes_4_r_shape",volumes_4_r.shape) #volumes_4_r_shape (?,8,8,8,64) - - reshaped_2=Add() ([volumes_4_r,volumes_8_l]) - - print("reshaped_2_shape",reshaped_2.shape) #volumes_2_shape (?,8,8,8,64) - - - rn16=Conv3DTranspose(filters=32, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_d2',strides=(2, 2, 2))(reshaped_2) - rn17=BatchNormalization()(rn16) - volumes_8_r =relu(rn17, alpha=0.0, max_value=None, threshold=0.0) - - reshaped_3=Add()([volumes_8_r,volumes_16_l]) - - print("reshaped_3_shape",reshaped_3.shape) #reshaped_3_shape (?,16,16,16,32) - - - rn19=Conv3DTranspose(filters=1, kernel_size=(4, 4, 4), padding='same',data_format="channels_last",name='ref_d3',strides=(2, 2, 2))(volumes_8_r) - print("rn19_shape",rn19.shape) #rn19_shape (?, ?, ?, ?, 1) -# volumes_16_r= tf.nn.sigmoid(rn19,name='ref_sigmoid1') -# reshape_4=volumes_16_r #################### - - reshape_4=Add()([rn19,input_volumes_32]) - reshape_4=(reshape_4*0.5) - print("reshape_4_5",reshape_4.shape) #reshape_4_5 (?,32,32,32,1) - - reshape_4= tf.nn.sigmoid(reshape_4,name='ref_sigmoid1') - - - print("reshape_4_sig_shape",reshape_4.shape) #reshape_4_sig_shape (?,32,32,32,1) - - reshape_5=tf.reshape(reshape_4, [-1, vox_res32, vox_res32, vox_res32],name="ref_out") - - return reshape_5 - -def attsets_fc(x, out_ele_num): - with tf.variable_scope('att_fc'): - in_ele_num = tf.shape(x)[1] - in_ele_len = int(x.get_shape()[2]) - out_ele_len = in_ele_len - print("out_ele_len ", out_ele_len) - #################### - x_1st = x - x_1st_tp = tf.reshape(x_1st, [-1, in_ele_len],name="att_in") - weights_1st = tools.Ops.fc(x_1st_tp, out_d=out_ele_num*out_ele_len, name="att") - - ########## option 1 - weights_1st = weights_1st - ########## option 2 -# weights_1st = tf.nn.tanh(weights_1st) - - #################### - weights_1st = tf.reshape(weights_1st, [-1, in_ele_num, out_ele_num, out_ele_len],name="att_fc_out") - weights_1st = tf.nn.softmax(weights_1st, 1) - x_1st = tf.tile(x_1st[:,:,None,:], [1,1,out_ele_num,1]) - x_1st = x_1st*weights_1st - x_1st = tf.reduce_sum(x_1st, axis=1) - x_1st = tf.reshape(x_1st, [-1, out_ele_num*out_ele_len],name="att_out") - return x_1st, weights_1st - -##################################### -class Network: - def __init__(self): - self.train_mod_dir = './Model/train_mod/' - self.train_sum_dir = './Model/train_sum/' - self.test_res_dir = './Model/test_res/' - self.test_sum_dir = './Model/test_sum/' - - print ('re_train : ', re_train) - if os.path.exists(self.test_res_dir): - if re_train: - print ('test_res_dir and files kept!') - else: - shutil.rmtree(self.test_res_dir) - os.makedirs(self.test_res_dir) - print ('test_res_dir: deleted and then created!') - else: - os.makedirs(self.test_res_dir) - print ('test_res_dir: created!') - - if os.path.exists(self.train_mod_dir): - if re_train: - if os.path.exists(self.train_mod_dir + 'model.cptk.data-00000-of-00001'): - print ('model found! will be reused!') - else: - print ('model not found! error!') - #exit() - else: - shutil.rmtree(self.train_mod_dir) - os.makedirs(self.train_mod_dir) - print ('train_mod_dir: deleted and then created!') - else: - os.makedirs(self.train_mod_dir) - print ('train_mod_dir: created!') - - if os.path.exists(self.train_sum_dir): - if re_train: - print ('train_sum_dir and files kept!') - else: - shutil.rmtree(self.train_sum_dir) - os.makedirs(self.train_sum_dir) - print ('train_sum_dir: deleted and then created!') - else: - os.makedirs(self.train_sum_dir) - print ('train_sum_dir: created!') - - if os.path.exists(self.test_sum_dir): - if re_train: - print ('test_sum_dir and files kept!') - else: - shutil.rmtree(self.test_sum_dir) - os.makedirs(self.test_sum_dir) - print ('test_sum_dir: deleted and then created!') - else: - os.makedirs(self.test_sum_dir) - print ('test_sum_dir: created!') - - def base_r2n2(self, X_rgb): - with tf.variable_scope('Encoder'): - im_num = tf.shape(X_rgb)[1] - - [_, _, d1, d2, cc] = X_rgb.get_shape() - X_rgb = tf.reshape(X_rgb, [-1, int(d1), int(d2), int(cc)],name="en_in") - print("Network Structure") - print("base_r2n2",X_rgb.shape) #base_r2n2 (?, 127, 127, 3) - -# plot_buf_1= tf.reshape(X_rgb, [-1, 127, 127, 3]) ################### -# tf.summary.image("Input", plot_buf_1)############################### - - en_c = [96, 128, 256, 256, 256, 256] - l1 = tools.Ops.xxlu(tools.Ops.conv2d(X_rgb, k=7, out_c=en_c[0], str=1, name='en_c1'), label='lrelu') - print("l1_r2n",l1.shape) #l1_r2n (?, 127, 127, 96) - - - - l2 = tools.Ops.xxlu(tools.Ops.conv2d(l1, k=3, out_c=en_c[0], str=1, name='en_c2'), label='lrelu') - l2 = tools.Ops.maxpool2d(l2, k=2, s=2, name='en_mp1') - print("l2_r2n",l2.shape) #l2_r2n (?, 64, 64, 96) - -# plot_buf_1= tf.reshape(l2, [-1, 32, 32, 3]) ######################### -# tf.summary.image("L2_MP_en", plot_buf_1)############################# - - l3 = tools.Ops.xxlu(tools.Ops.conv2d(l2, k=3, out_c=en_c[1], str=1, name='en_c3'), label='lrelu') - print("l3_r2n",l3.shape) #l3_r2n (?, 64, 64, 128) - l4 = tools.Ops.xxlu(tools.Ops.conv2d(l3, k=3, out_c=en_c[1], str=1, name='en_c4'), label='lrelu') - print("l4_r2n",l4.shape) #l4_r2n (?, 64, 64, 128) - l22 = tools.Ops.conv2d(l2, k=1, out_c=en_c[1], str=1, name='en_c22') - print("l22_r2n",l22.shape) #l22_r2n (?, 64, 64, 128) - l4 = l4 + l22 - l4 = tools.Ops.maxpool2d(l4, k=2, s=2, name='en_mp2') - print("l4+l22_r2n",l4.shape) #l4+l22_r2n (?, 32, 32, 128) - -# plot_buf_1= tf.reshape(l4, [-1, 32, 32, 3]) ########################## -# tf.summary.image("l4+l22_en", plot_buf_1)############################# - - l5 = tools.Ops.xxlu(tools.Ops.conv2d(l4, k=3, out_c=en_c[2], str=1, name='en_c5'), label='lrelu') - print("l5_r2n",l5.shape) #l5_r2n (?, 32, 32, 256) - l6 = tools.Ops.xxlu(tools.Ops.conv2d(l5, k=3, out_c=en_c[2], str=1, name='en_c6'), label='lrelu') - print("l6_r2n",l6.shape) #l6_r2n (?, 32, 32, 256) - l44 = tools.Ops.conv2d(l4, k=1, out_c=en_c[2], str=1, name='en_c44') - print("l44_r2n",l44.shape) #l44_r2n (?, 32, 32, 256) - l6 = l6 + l44 - l6 = tools.Ops.maxpool2d(l6, k=2, s=2, name='en_mp3') - print("l6+l44_r2n",l6.shape) #l6+l44_r2n (?, 16, 16, 256) - -# plot_buf_1= tf.reshape(l6, [-1, 16, 16, 3]) ########################## -# tf.summary.image("l6+l44_en", plot_buf_1)############################# - - l7 = tools.Ops.xxlu(tools.Ops.conv2d(l6, k=3, out_c=en_c[3], str=1, name='en_c7'), label='lrelu') - print("l7_r2n",l7.shape) #l7_r2n (?, 16, 16, 256) - l8 = tools.Ops.xxlu(tools.Ops.conv2d(l7, k=3, out_c=en_c[3], str=1, name='en_c8'), label='lrelu') - print("l8_r2n",l8.shape)#l8_r2n (?, 16, 16, 256) -# l66=tools.Ops.conv2d(l6, k=1, out_c=en_c[3], str=1, name='en_c66') -# print("l66_r2n",l66.shape) -# l8=l8+l66 - l8 = tools.Ops.maxpool2d(l8, k=2, s=2, name='en_mp4') - print("l8_r2n",l8.shape) #l8_r2n (?, 8, 8, 256) - -# plot_buf_1= tf.reshape(l8, [-1, 8, 8, 3]) ######################## -# tf.summary.image("l8_en", plot_buf_1)############################# - - l9 = tools.Ops.xxlu(tools.Ops.conv2d(l8, k=3, out_c=en_c[4], str=1, name='en_c9'), label='lrelu') - print("l9_r2n",l9.shape) #l9_r2n (?, 8, 8, 256) - l10 = tools.Ops.xxlu(tools.Ops.conv2d(l9, k=3, out_c=en_c[4], str=1, name='en_c10'), label='lrelu') - print("l10_r2n",l10.shape)#l10_r2n (?, 8, 8, 256) - l88 = tools.Ops.conv2d(l8, k=1, out_c=en_c[4], str=1, name='en_c88') - print("l88_r2n",l88.shape) #l88_r2n (?, 8, 8, 256) - l10 = l10 + l88 - l10 = tools.Ops.maxpool2d(l10, k=2, s=2, name='en_mp5') - print("l10_r2n",l10.shape) #l10_r2n (?, 4, 4, 256) - -# plot_buf_1= tf.reshape(l10, [-1, 4, 4, 3]) ######################## -# tf.summary.image("l10_en", plot_buf_1)############################# - - l11 = tools.Ops.xxlu(tools.Ops.conv2d(l10, k=3, out_c=en_c[5], str=1, name='en_c11'), label='lrelu') - print("l11_r2n",l11.shape) #l11_r2n (?, 4, 4, 256) - l12 = tools.Ops.xxlu(tools.Ops.conv2d(l11, k=3, out_c=en_c[5], str=1, name='en_c12'), label='lrelu') - print("l12_r2n",l12.shape) #l12_r2n (?, 4, 4, 256) - l1010 = tools.Ops.conv2d(l10, k=1, out_c=en_c[5], str=1, name='en_c1010') - print("l1010_r2n",l1010.shape) #l1010_r2n (?, 4, 4, 256) - l12 = l12 + l1010 - l12 = tools.Ops.maxpool2d(l12, k=2, s=2, name='en_mp6') - print("l12_r2n",l12.shape) #l12_r2n (?, 2, 2, 256) - -# plot_buf_1= tf.reshape(l12, [-1, 2, 2, 3]) ######################## -# tf.summary.image("l12_en", plot_buf_1)############################# - - [_, d1, d2, cc] = l12.get_shape() - l12 = tf.reshape(l12, [-1, int(d1) * int(d2) * int(cc)],name="en_fc1_in") - print("fc1_input_r2n",l12.shape) #fc1_input_r2n (?, 1024) - fc = tools.Ops.xxlu(tools.Ops.fc(l12, out_d=1024, name='en_fc1'), label='lrelu') - print("fc1_output_r2n",fc.shape)#fc1_output_r2n (?, 1024) - - with tf.variable_scope('Att_Net'): - #### use fc attention - input = tf.reshape(fc, [-1, im_num, 1024],name="Att_fc_in") - print("att_fc_in_r2n",input.shape) #att_fc_in_r2n (?, ?, 1024) - latent_3d, weights = attsets_fc(input, out_ele_num=1) - print("att_fc_out_r2n",latent_3d.shape) #att_fc_out_r2n (?, 1024) - - with tf.variable_scope('Decoder'): - #### - latent_3d = tools.Ops.xxlu(tools.Ops.fc(latent_3d, out_d=4*4*4*128, name='de_fc2'), label='lrelu') - print("fc3_out_r2n",latent_3d.shape) #fc3_out_r2n (?, 8192) - latent_3d = tf.reshape(latent_3d, [-1, 4, 4, 4, 128],name="de_fc2_out") - - #### - - de_c = [128, 128, 128, 64, 32, 1] - - print("d1_in_r2n",latent_3d.shape) #d1_in_r2n (?, 4, 4, 4, 128) - d1 = tools.Ops.xxlu(tools.Ops.deconv3d(latent_3d, k=3, out_c=de_c[1], str=2, name='de_c1'), label='lrelu') - print("d1_out_r2n",d1.shape) #d1_out_r2n (?, 8, 8, 8, 128) - d2 = tools.Ops.xxlu(tools.Ops.deconv3d(d1, k=3, out_c=de_c[1], str=1, name='de_c2'), label='lrelu') - print("d2_out_r2n",d2.shape) #d2_out_r2n (?, 8, 8, 8, 128) - d00 = tools.Ops.deconv3d(latent_3d, k=1, out_c=de_c[1], str=2, name='de_c00') - print("d00_out_r2n",d00.shape)#d00_out_r2n (?, 8, 8, 8, 128) - d2 = d2 + d00 - print("d2+d00_out_r2n",d2.shape)#d2+d00_out_r2n (?, 8, 8, 8, 128) - -# plot_buf_1= tf.reshape(d2, [-1, 8, 8, 4]) ################################ -# tf.summary.image("d2+d00_out_de", plot_buf_1)############################# - - d3 = tools.Ops.xxlu(tools.Ops.deconv3d(d2, k=3, out_c=de_c[2], str=2, name='de_c3'), label='lrelu') - print("d3_out_r2n",d3.shape)#d3_out_r2n (?, 16, 16, 16, 128) - d4 = tools.Ops.xxlu(tools.Ops.deconv3d(d3, k=3, out_c=de_c[2], str=1, name='de_c4'), label='lrelu') - print("d4_out_r2n",d4.shape)#d4_out_r2n (?, 16, 16, 16, 128) - d22 = tools.Ops.deconv3d(d2, k=1, out_c=de_c[2], str=2, name='de_c22') - print("d22_out_r2n",d22.shape)#d22_out_r2n (?, 16, 16, 16, 128) - d4 = d4 + d22 - print("d4+d22_out_r2n",d4.shape)#d4+d22_out_r2n (?, 16, 16, 16, 128) - -# plot_buf_1= tf.reshape(d4, [-1, 16, 16, 4]) ############################## -# tf.summary.image("d4+d22_out_de", plot_buf_1)############################# - - d5 = tools.Ops.xxlu(tools.Ops.deconv3d(d4, k=3, out_c=de_c[3], str=2, name='de_c5'), label='lrelu') - print("d5_out_r2n",d5.shape)#d5_out_r2n (?, 32, 32, 32, 64) - d6 = tools.Ops.xxlu(tools.Ops.deconv3d(d5, k=3, out_c=de_c[3], str=1, name='de_c6'), label='lrelu') - print("d6_out_r2n",d6.shape)#d6_out_r2n (?, 32, 32, 32, 64) - d44 = tools.Ops.deconv3d(d4, k=1, out_c=de_c[3], str=2, name='de_c44') - print("d44_out_r2n",d44.shape)#d44_out_r2n (?, 32, 32, 32, 64) - d6 = d6 + d44 - print("d6+d44_out_r2n",d6.shape) #d6+d44_out_r2n (?, 32, 32, 32, 64) - -# plot_buf_1= tf.reshape(d6, [-1, 32, 32, 4]) ############################## -# tf.summary.image("d6+d44_out_de", plot_buf_1)############################# - - d7 = tools.Ops.xxlu(tools.Ops.deconv3d(d6, k=3, out_c=de_c[4], str=1, name='de_c7'), label='lrelu') - print("d7_out_r2n",d7.shape) #d7_out_r2n (?, 32, 32, 32, 32) - d8 = tools.Ops.xxlu(tools.Ops.deconv3d(d7, k=3, out_c=de_c[4], str=1, name='de_c8'), label='lrelu') - print("d8_out_r2n",d8.shape)#d8_out_r2n (?, 32, 32, 32, 32) - d77 = tools.Ops.xxlu(tools.Ops.deconv3d(d7, k=3, out_c=de_c[4], str=1, name='de_c77'), label='lrelu') - print("d77_out_r2n",d77.shape)#d77_out_r2n (?, 32, 32, 32, 32) - d8 = d8 + d77 - print("d8+d77_out_r2n",d8.shape) #d8+d77_out_r2n (?, 32, 32, 32, 32) - -# plot_buf_1= tf.reshape(d8, [-1, 32, 32, 4]) ############################## -# tf.summary.image("d8+d77_out_de", plot_buf_1)############################# - - d11 = tools.Ops.deconv3d(d8, k=3, out_c=de_c[5], str=1, name='de_c9') - print("d11_out_r2n",d11.shape) #d11_out_r2n (?, 32, 32, 32, 1) - -# plot_buf_1= tf.reshape(d11, [-1, 32, 32,4]) ########################## -# tf.summary.image("Ref_input", plot_buf_1)############################# - - ref_in = tf.reshape(d11, [-1, vox_res32, vox_res32, vox_res32],name="ref_in") ### - - y = tf.nn.sigmoid(d11,name='de_sigmoid') - - att_o = tf.reshape(y, [-1, vox_res32, vox_res32, vox_res32],name="de_out") - - print("att_out_shape",att_o.shape) #att_out_shape (?, 32, 32, 32) - - with tf.variable_scope('ref_net'): - - ref_o=refiner_network(ref_in) - -# plot_buf_1= tf.reshape(ref_o, [-1, 32, 32,4]) ###################### -# tf.summary.image("Ref_Out", plot_buf_1)############################# - - return ref_o,att_o, weights - - def build_graph(self): - img_res = 127 - vox_res = 32 - self.X_rgb = tf.placeholder(shape=[None, None, img_res, img_res, 3], dtype=tf.float32) - self.Y_vox = tf.placeholder(shape=[None, vox_res, vox_res, vox_res], dtype=tf.float32) - self.lr = tf.placeholder(tf.float32) - self.refine_lr = tf.placeholder(tf.float32) - with tf.device('/cpu:0'): - self.Y_pred,self.vae_o, self.weights = self.base_r2n2(self.X_rgb) - tf.summary.histogram('Attsets_Weights', self.weights) - with tf.device('/cpu:0'): - ### rec loss - print ("reached") - with tf.variable_scope('Loss_Fun'): - Y_vox_ = tf.reshape(self.Y_vox, shape=[-1, vox_res ** 3]) - Y_pred_ = tf.reshape(self.Y_pred, shape=[-1, vox_res ** 3]) - vae_o_=tf.reshape(self.vae_o, shape=[-1, vox_res ** 3]) - - self.vae_loss = tf.reduce_mean(-tf.reduce_mean(Y_vox_ * tf.log(vae_o_ + 1e-8), reduction_indices=[1]) - - tf.reduce_mean((1 - Y_vox_) * tf.log(1 - vae_o_ + 1e-8),reduction_indices=[1])) - self.rec_loss = tf.reduce_mean(-tf.reduce_mean(Y_vox_ * tf.log(Y_pred_ + 1e-8), reduction_indices=[1]) - - tf.reduce_mean((1 - Y_vox_) * tf.log(1 - Y_pred_ + 1e-8),reduction_indices=[1])) - sum_rec_loss = tf.summary.scalar('rec_loss', self.rec_loss) - self.sum_merged = sum_rec_loss - tf.summary.histogram('rec_loss', self.rec_loss) - tf.summary.scalar("vae-loss",self.vae_loss) - tf.summary.histogram("vae_loss",self.vae_loss) - self.mean_loss = tf.div(x=tf.math.add(x=self.vae_loss,y=self.rec_loss,name='add_loss'),y=2,name='mean_loss') - tf.summary.histogram("mean_vae_loss",self.mean_loss) - tf.summary.scalar("mean_vae_loss",self.mean_loss) - - with tf.variable_scope('Evaluation_Metric'): - gt_vox=Y_vox_ - self.iou_ref = metric_iou(Y_pred_,gt_vox) - tf.summary.scalar('iou_refiner', self.iou_ref) - tf.summary.histogram('iou_refiner', self.iou_ref) - self.iou_vae = metric_iou(vae_o_,gt_vox) - tf.summary.scalar('iou_vae', self.iou_vae) - tf.summary.histogram("iou_vae",self.iou_vae) - - with tf.variable_scope('Optimization'): - - base_en_var = [var for var in tf.trainable_variables() if var.name.startswith('Encoder/en')] - base_dec_var = [var for var in tf.trainable_variables() if var.name.startswith('Decoder/de')] - att_var = [var for var in tf.trainable_variables() if var.name.startswith('Att_Net/att')] - refine_var = [var for var in tf.trainable_variables() if var.name.startswith('ref_net/ref')] - self.refine_optim = tf.train.AdamOptimizer(learning_rate=self.refine_lr).minimize(self.rec_loss, var_list=refine_var) - self.base_en_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.vae_loss, var_list=base_en_var) - self.base_de_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.vae_loss, var_list=base_dec_var) - self.att_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.vae_loss, var_list=att_var) - - - print ("total weights:",tools.Ops.variable_count()) - self.saver = tf.train.Saver(max_to_keep=1) - - config = tf.ConfigProto(allow_soft_placement=True) - #config.gpu_options.allow_growth = True - #config.gpu_options.visible_device_list = '0,1' - - self.sess = tf.Session(config=npu_config_proto(config_proto=config)) - self.merged = tf.summary.merge_all() - self.sum_writer_train = tf.summary.FileWriter(self.train_sum_dir, self.sess.graph) - self.sum_writer_test = tf.summary.FileWriter(self.test_sum_dir, self.sess.graph) - - ####################### - path = self.train_mod_dir - #path = './Model_released/' # retrain the released model - - if os.path.isfile(path + 'model.cptk.data-00000-of-00001'): - print ("restoring saved model!") - self.saver.restore(self.sess, path + 'model.cptk') - else: - self.sess.run(tf.global_variables_initializer()) - - - return 0 - - def train(self, data): - for epoch in range(0, 20, 1): - train_view_num = 24 ##!!!!!!!!!!! - data.shuffle_train_files(epoch, train_mv=train_view_num) - total_train_batch_num = data.total_train_batch_num #int(len(self.X_rgb_train_files)/(self.batch_size*train_mv)) - print ('total_train_batch_num:', total_train_batch_num) - for i in range(total_train_batch_num): - #### training - X_rgb_bat, Y_vox_bat = data.load_X_Y_train_next_batch(train_mv=train_view_num) - print("multi_view_train_X_rgb_bat : ",X_rgb_bat.shape)#np.asarray(X.append(X_rgb[b*train_mv:(b+1)*train_mv,:])) - - - print(time.ctime()) - - ##### option 1: seperate train, seperate optimize - #if epoch<=30: - # single_view_train=True - # multi_view_train=False - #else: - # single_view_train=False - # multi_view_train=True - - ##### optiion 2: joint train, seperate optimize - single_view_train = True - multi_view_train = False - - if epoch <= 5: - att_lr=.0002 - ref_lr=.0002 - if epoch > 5 and epoch <= 40: - att_lr=.0001 - ref_lr=.0001 - if epoch > 40: - att_lr=.00005 - ref_lr=.00005 - ########### single view train - if single_view_train: - - rgb = np.reshape(X_rgb_bat,[batch_size*train_view_num, 1, 127,127,3]) - print("single_view_train_rgb_input_shape ",rgb.shape) - vox = np.tile(Y_vox_bat[:,None,:,:,:],[1,train_view_num,1,1,1]) - vox = np.reshape(vox, [batch_size*train_view_num, 32,32,32]) - start_time=time.time() - vae_loss_c,eee,ddd, rec_loss_c, sum_train,rrr,mean_vae,iou_ref_,iou_vae_ = self.sess.run([self.vae_loss,self.base_en_optim2,self.base_de_optim2,self.rec_loss,self.merged,self.refine_optim,self.mean_loss,self.iou_ref,self.iou_vae],feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr,self.refine_lr: ref_lr}) - print('TimeHistory = %.4f' % (time.time()-start_time)) - print ('ep:', epoch, 'i:', i, 'train single rec loss:', rec_loss_c) - print ('ep:', epoch, 'i:', i, 'train single vae loss:', vae_loss_c) - print ('ep:', epoch, 'i:', i, 'train single mean_vae loss:',mean_vae) - print ('ep:', epoch, 'i:', i, 'train single ref_iou:',iou_ref_) - print ('ep:', epoch, 'i:', i, 'train single vae_iou:',iou_vae_) - - ########## multi view train - if multi_view_train: - - vae_loss_c,rec_loss_c, _, sum_train,xxx,mean_vae,iou_ref_,iou_vae_ = self.sess.run([self.vae_loss,self.rec_loss, self.att_optim2, self.merged,self.refine_optim,self.mean_loss,self.iou_ref,self.iou_vae],feed_dict={self.X_rgb: X_rgb_bat, self.Y_vox: Y_vox_bat,self.lr: att_lr,self.refine_lr: ref_lr}) - print ('ep:', epoch, 'i:', i, 'train multi rec loss:', rec_loss_c) - print ('ep:', epoch, 'i:', i, 'train multi vae loss:', vae_loss_c) - print('ep:',epoch,'i',i,'train multi mean_vae loss:',mean_vae) - print ('ep:', epoch, 'i:', i, 'train multi ref_iou:',iou_ref_) - print ('ep:', epoch, 'i:', i, 'train multi vae_iou:',iou_vae_) - - ############ - if i % 5 == 0: - self.sum_writer_train.add_summary(sum_train, epoch * total_train_batch_num + i) - - - #### testing - if i % 10 == 0 : - X_rgb_batch, Y_vox_batch = data.load_X_Y_test_next_batch(test_mv=3) - -# vae_pred = tf.get_default_graph().get_tensor_by_name("Decoder/de_out:0") -# ref_pred = tf.get_default_graph().get_tensor_by_name("ref_net/ref_Dec/ref_out:0") -# gt_vox=Y_vox_batch.astype(np.float32) - -# iou_pred = metric_iou(ref_pred,gt_vox) -# tf.summary.scalar("iou",iou_pred) - - rrrr,aaaa,rec_loss_te, qwerty, Y_vox_test_pred, att_pred, sum_test,mean_vae,iou_ref_,iou_vae_ = \ - self.sess.run([self.refine_optim,self.att_optim2,self.rec_loss,self.vae_loss, self.Y_pred,self.weights, self.merged,self.mean_loss,self.iou_ref,self.iou_vae],feed_dict={self.X_rgb: X_rgb_batch, self.Y_vox: Y_vox_batch,self.lr: 0,self.refine_lr: 0}) - - X_rgb_batch = X_rgb_batch.astype(np.float16) - - Y_vox_batch = Y_vox_batch.astype(np.float16) - Y_vox_test_pred = Y_vox_test_pred.astype(np.float16) - att_pred = att_pred.astype(np.float16) - to_save = {'X_test':X_rgb_batch,'Y_test_pred':Y_vox_test_pred,'att_pred':att_pred,'Y_test_true':Y_vox_batch} - scipy.io.savemat(self.test_res_dir+'X_Y_pred_'+str(epoch).zfill(2)+'_'+str(i).zfill(5)+'.mat',to_save,do_compression=True) - - - self.sum_writer_test.add_summary(sum_test, epoch * total_train_batch_num + i) - -# iou_ref=evaluate_voxel_prediction(ref_pred_,gt_vox) -# iou_vae=evaluate_voxel_prediction(vae_pred_,gt_vox) - -# print("Ref_iou:",iou_ref) -# print("Vae_iou:",iou_vae) - -# plot_list_iou.append(iou_ref) -# plot_list_i.append((i/50)) -# graph_plot(plot_list_iou,plot_list_i) - print ('ep:', epoch, 'i:', i, 'test rec loss:', rec_loss_te) - print ('ep:', epoch, 'i:', i, 'test vae loss:', qwerty ) - print ('ep:', epoch, 'i:', i, 'test mean_vae loss:', mean_vae) - print ('ep:', epoch, 'i:', i, 'test ref_iou:',iou_ref_) - print ('ep:', epoch, 'i:', i, 'test vae_iou:',iou_vae_) - - #### model saving - #if i % 100 == 0 : - #self.saver.save( self.sess, save_path=self.train_mod_dir + 'model.cptk' ) - #print ( 'epoch:', epoch, 'i:', i, 'model saved!' ) - - -# plt.show() - - -########## -if __name__ =='__main__': - npu_keras_sess = set_keras_session_npu_config() - - net = Network() #net=object to create instance - - print("network compleated") ### - - net.build_graph() - print("graph compleated") - - - data = tools.Data(config) - print("tools.data compleated") - - - print('training data') - - net.train(data) - - close_session(npu_keras_sess) - - - - - - - - - - - - diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/modelarts_entry.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/modelarts_entry.py deleted file mode 100644 index 4fb8aca77..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/modelarts_entry.py +++ /dev/null @@ -1,55 +0,0 @@ -# -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -# Copyright 2021 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -import os -import argparse -import sys - -# 解析输入参数data_url -parser = argparse.ArgumentParser() -parser.add_argument("--data_url", type=str, default="/cache/dataset") -parser.add_argument("--train_url", type=str, default="/cache/output") -config = parser.parse_args() - -print("[CANN-Modelzoo] code_dir path is [%s]" % (sys.path[0])) -code_dir = sys.path[0] - -print("[CANN-Modelzoo] work_dir path is [%s]" % (os.getcwd())) -work_dir = os.getcwd() - -print("[CANN-Modelzoo] start run train shell") -# 执行训练脚本 -shell_cmd = ("bash %s/npu_train.sh %s %s %s %s " % (code_dir, code_dir, work_dir, config.data_url, config.train_url)) -os.system(shell_cmd) -print("[CANN-Modelzoo] finish run train shell") - - - - diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/npu_train.sh b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/npu_train.sh deleted file mode 100644 index a91aac698..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/npu_train.sh +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/bash - -code_dir=$1 -work_dir=$2 -dataset_path=$3 -output_path=$4 - -#############训练前输入目录文件确认######################### -echo "[CANN-Modelzoo] before train - list my run files[/usr/local/Ascend/ascend-toolkit]:" -ls -al /usr/local/Ascend/ascend-toolkit -echo "" - -echo "[CANN-Modelzoo] before train - list my code files[${code_dir}]:" -ls -al ${code_dir} -echo "" - -echo "[CANN-Modelzoo] before train - list my work files[${work_dir}]:" -ls -al ${work_dir} -echo "" - -echo "[CANN-Modelzoo] before train - list my dataset files[${dataset_path}]:" -ls -al ${dataset_path} -echo "" - -echo "[CANN-Modelzoo] before train - list my output files[${output_path}]:" -ls -al ${output_path} -echo "" - -######环境变量修改###### -###如果需要修改环境变量的,在此处修改 -#设置日志级别为info -#export ASCEND_GLOBAL_LOG_LEVEL=1 -#设置日志打屏到屏幕 -#export ASCEND_SLOG_PRINT_TO_STDOUT=1 -#export TF_CPP_MIN_LOG_LEVEL=0 -env > ${output_path}/my_env.log - -######训练执行###### -###此处每个网络执行命令不同,需要修改 -python3.7 ${code_dir}/main_AttSets.py --data_url=${dataset_path} --train_url=${output_path} -if [ $? -eq 0 ]; -then - echo "[CANN-Modelzoo] train return success" -else - echo "[CANN-Modelzoo] train return failed" -fi - -######训练后把需要备份的内容保存到output_path###### -###此处每个网络不同,视情况添加cp -cp -r ${work_dir} ${output_path} - -######训练后输出目录文件确认###### -echo "[CANN-Modelzoo] after train - list my work files[${work_dir}]:" -ls -al ${work_dir} -echo "" - -echo "[CANN-Modelzoo] after train - list my output files[${output_path}]:" -ls -al ${output_path} -echo "" diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/ops_info.json b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/ops_info.json deleted file mode 100644 index 6d5d0ba20..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/ops_info.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "black-list": { - "to-add": [ - "Mul", - "ReduceSumD", - "SquaredDifference" - ] - } -} - diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/tools.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/tools.py deleted file mode 100644 index 6054ceb1e..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/tools.py +++ /dev/null @@ -1,482 +0,0 @@ -# -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -# Copyright 2021 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from npu_bridge.npu_init import * -import tensorflow as tf -from PIL import Image -import numpy as np -import os -import sys -import random -import matplotlib.pyplot as plt -import matplotlib.gridspec as gridspec -from mpl_toolkits import mplot3d -import re -import binvox_rw as binvox_rw -################################################################# -from voxel import voxel2obj -#from PyMCubes import mcubes -from export_obj import export_obj -################################################################# -class Data: - def __init__(self, config): - self.config = config - self.batch_size = config['batch_size'] - self.train_batch_index = 0 - self.test_batch_index_sq = 0 - self.cat_names = config['cat_names'] - self.total_mv = config['total_mv'] - self.cat_test_1st_index = None - - self.X_rgb_train_files_ori, self.Y_vox_train_files_ori, self.X_rgb_test_files_ori, self.Y_vox_test_files_ori \ - =self.load_X_Y_files_paths_all(self.cat_names) - - print ('X_rgb_train_files_ori:', len(self.X_rgb_train_files_ori)) - print ('X_rgb_test_files_ori:', len(self.X_rgb_test_files_ori)) - - @staticmethod - def crop_center(im, new_height, new_width): -# print('crop_center') - height = im.shape[0] # Get dimensions - width = im.shape[1] - left = (width - new_width) // 2 - top = (height - new_height) // 2 - right = (width + new_width) // 2 - bottom = (height + new_height) // 2 - return im[top:bottom, left:right] - - @staticmethod - def add_random_color_background(im, color_range): -# print('add_random_color_background') - r, g, b = [np.random.randint(color_range[i][0], color_range[i][1] + 1) for i in range(3)] -# print (r) -# print (g) -# print (b) - if isinstance(im, Image.Image): - im = np.array(im) -# print (im.shape) -# b = np.array([3]) ### -# np.concatenate((im, '3')) ## -# im=np.append(im,3) ### -# print (im.shape) ### - if im.shape[2] > 3: - - # If the image has the alpha channel, add the background - alpha = (np.expand_dims(im[:, :, 3], axis=2) == 0).astype(np.float) - - im = im[:, :, :3] - bg_color = np.array([[[r, g, b]]]) - im = alpha * bg_color + (1 - alpha) * im - return im - - @staticmethod - def image_transform(img, crop_x, crop_y, crop_loc=None, color_tint=None): -# print('image_transform') - RANDOM_CROP = True - # Slight translation - if RANDOM_CROP and not crop_loc: - crop_loc = [np.random.randint(0, crop_y), np.random.randint(0, crop_x)] - - if crop_loc: - cr, cc = crop_loc - height, width, _ = img.shape - img_h = height - crop_y - img_w = width - crop_x - img = img[cr:cr + img_h, cc:cc + img_w] - # depth = depth[cr:cr+img_h, cc:cc+img_w] - - FLIP = True - if FLIP and np.random.rand() > 0.5: - img = img[:, ::-1, ...] - return img - - @staticmethod - def preprocess_img(im, train): -# print('processing_img') - # add random background - TRAIN_NO_BG_COLOR_RANGE = [[225, 255], [225, 255], [225, 255]] - TEST_NO_BG_COLOR_RANGE = [[240, 240], [240, 240], [240, 240]] - im = Data.add_random_color_background(im, TRAIN_NO_BG_COLOR_RANGE if train else TEST_NO_BG_COLOR_RANGE) - - # If the image has alpha channel, remove it. - CONST_IMG_W = 127 - CONST_IMG_H = 127 - im_rgb = np.array(im)[:, :, :3].astype(np.float32) - if train: - # Data augmentation - PAD_X = 10 - PAD_Y = 10 - t_im = Data.image_transform(im_rgb, PAD_X, PAD_Y) - else: - t_im = Data.crop_center(im_rgb, CONST_IMG_H, CONST_IMG_W) - - # Scale image - t_im = t_im / 255. - return t_im - - @staticmethod - def load_single_X_rgb_r2n2(img_path, train): -# print('load_single_X_rgb_r2n2') - im = Image.open(img_path) - - t_im = Data.preprocess_img(im, train=train) - # plt.figure() - # plt.imshow(t_im) - return t_im - - @staticmethod - def load_single_Y_vox(vox_path): -# print('load_single_Y_vox') - with open(vox_path, 'rb') as ff: - vox = binvox_rw.read_as_3d_array(ff) - vox_grid = vox.data.astype(int) - - #Data.plotFromVoxels(vox_grid) - return vox_grid - - @staticmethod - def load_X_Y_files_paths(X_cat_folder, Y_cat_folder): -# print('load_X_Y_files_paths') - X_obj_folders=[X_f for X_f in sorted(os.listdir(X_cat_folder))] - Y_obj_folders=[Y_f for Y_f in sorted(os.listdir(Y_cat_folder))] - if len(X_obj_folders) != len(Y_obj_folders): - print ('Files are inconsistent in:', X_cat_folder, 'and', Y_cat_folder) - - #### split train/test, according to 3D-R2N2 paper - train_num = int(0.8*len(X_obj_folders)) - idx = list(range(train_num)) - - test_objs = [] - X_train_obj_folders =[]; Y_train_obj_folders=[] - X_test_obj_folders =[]; Y_test_obj_folders=[] - for i in range(len(X_obj_folders)): - obj_na = X_obj_folders[i] - if obj_na not in Y_obj_folders: - print ('inconsistent single obj ignored') - continue - if i in idx: - X_train_obj_folders.append(X_cat_folder+obj_na+'/') - Y_train_obj_folders.append(Y_cat_folder+obj_na+'/') - else: - X_test_obj_folders.append(X_cat_folder+obj_na+'/') - Y_test_obj_folders.append(Y_cat_folder+obj_na+'/') - test_objs.append(obj_na) - print ('train objs:', len(X_train_obj_folders)) - print ('test objs:', len(X_test_obj_folders)) - - ######### - def load_x_y_files(X_obj_fo, Y_obj_fo): -# print('load_x_y_files') - X_files_paths = [] - Y_files_paths = [] - for j in range(len(X_obj_fo)): - if X_obj_fo[j][-5:] not in Y_obj_fo[j]: - print ('inconsistent single obj exit') - exit() - for xf in sorted(os.listdir(X_obj_fo[j]+'rendering/')): - if '.png' in xf: - X_files_paths.append(X_obj_fo[j]+'rendering/'+xf) - Y_files_paths.append(Y_obj_fo[j]+'model.binvox') - return X_files_paths, Y_files_paths - ######### - - X_train_files, Y_train_files = load_x_y_files(X_train_obj_folders, Y_train_obj_folders) - X_test_files, Y_test_files = load_x_y_files(X_test_obj_folders, Y_test_obj_folders) - - return X_train_files, Y_train_files,X_test_files,Y_test_files - - @staticmethod - def load_X_Y_rgb_vox(X_files_full_path, Y_files_full_path, train): -# print('load_X_Y_rgb_vox') - if len(X_files_full_path) != len(Y_files_full_path): - print ('load_X_Y_rgb_vox error!') - exit() - X_rgb = [] - Y_vox = [] - for X_f, Y_f in zip(X_files_full_path, Y_files_full_path): - na = re.split('/', X_f)[-3] - if na not in Y_f: - print ('X Y rgb vox file not consistent!') - exit() - rgb = Data.load_single_X_rgb_r2n2(X_f, train=train) - X_rgb.append(rgb) - - vox = Data.load_single_Y_vox(Y_f) - Y_vox.append(vox) - - X_rgb = np.asarray(X_rgb) - Y_vox = np.asarray(Y_vox) - return X_rgb, Y_vox - - def test_listdir(self, path): - print('当前执行路径', os.getcwd()) - for file in os.listdir(path): - file_path = os.path.join(path, file) - print(file_path) - - def load_X_Y_files_paths_all(self,cat_names): -# print('load_X_Y_files_paths_all') - x_rgb_str='X_rgb_' - y_vox_str='Y_vox_' - - X_train_files_paths_all=[] - Y_trian_files_paths_all=[] - X_test_files_paths_all=[] - Y_test_files_paths_all=[] - self.cat_test_1st_index = [0] - for name in cat_names: - self.test_listdir(os.getcwd()) - print ('loading files:', name) - X_rgb_folder = self.config[x_rgb_str+name] - Y_vox_folder = self.config[y_vox_str+name] - - X_train_files, Y_train_files, X_test_files, Y_test_files = self.load_X_Y_files_paths(X_rgb_folder, Y_vox_folder) - self.cat_test_1st_index.append(len(X_test_files)) - - for X_rgb_f, Y_vox_f in zip(X_train_files, Y_train_files): - X_train_files_paths_all.append(X_rgb_f) - Y_trian_files_paths_all.append(Y_vox_f) - for X_rgb_f, Y_vox_f in zip(X_test_files, Y_test_files): - X_test_files_paths_all.append(X_rgb_f) - Y_test_files_paths_all.append(Y_vox_f) - - return X_train_files_paths_all, Y_trian_files_paths_all,X_test_files_paths_all,Y_test_files_paths_all - - ################################ - def load_X_Y_train_next_batch(self, train_mv): -# print('load_X_Y_train_next_batch') - X_rgb_files = self.X_rgb_train_files[self.batch_size*self.train_batch_index*train_mv:self.batch_size*(self.train_batch_index+1)*train_mv] - Y_vox_files = self.Y_vox_train_files[self.batch_size*self.train_batch_index*train_mv:self.batch_size*(self.train_batch_index+1)*train_mv] - self.train_batch_index +=1 - - X_rgb, Y_vox = self.load_X_Y_rgb_vox(X_rgb_files, Y_vox_files, train=True) - X = [] - Y = [] - for b in range(self.batch_size): - X.append(X_rgb[b * train_mv:(b + 1) * train_mv, :]) - Y.append(Y_vox[b * train_mv, :, :, :]) - X = np.asarray(X) - Y = np.asarray(Y) - return X, Y - - def load_X_Y_test_next_batch(self, test_mv): -# print('load_X_Y_test_next_batch') - num = self.total_mv - idx = random.sample(range(len(self.X_rgb_test_files_ori)//num), self.batch_size) ##############added extra '/' - X_rgb_files = [] - Y_vox_files =[] - - for i in idx: - tp1 = self.X_rgb_test_files_ori[i*num:(i+1)*num] - tp2 = self.Y_vox_test_files_ori[i*num:(i+1)*num] - for x, y in zip(tp1, tp2): - X_rgb_files.append(x) - Y_vox_files.append(y) - X_rgb_batch, Y_vox_batch = self.load_X_Y_rgb_vox(X_rgb_files, Y_vox_files, train=False) - - X =[] - Y =[] - for b in range(self.batch_size): - tp1 = X_rgb_batch[b*num:(b+1)*num,:,:,:] - tp2 = Y_vox_batch[b*num:(b+1)*num,:,:,:] - idx2 = random.sample(range(num), test_mv) - X.append(tp1[idx2]) - Y.append(tp2[idx2[0]]) - X = np.asarray(X) - Y = np.asarray(Y) - return X, Y - - def shuffle_train_files(self, ep, train_mv): -# print('shuffle_train_files') - num = self.total_mv - X_rgb_new=[]; Y_vox_new=[] - self.train_batch_index = 0 - X_rgb = self.X_rgb_train_files_ori - Y_vox = self.Y_vox_train_files_ori - index = list(range(int(len(X_rgb)/num))) - random.Random(ep).shuffle(index) - - for i in index: - tp1 = X_rgb[i*num:(i+1)*num] - tp2 = Y_vox[i*num:(i+1)*num] - - view_ind = list(range(num)) - random.Random(i+100).shuffle(view_ind) - valid_view_num = int(num / train_mv) * train_mv - view_ind = view_ind[0:valid_view_num] - - for j in view_ind: - x = tp1[j] - y = tp2[j] - na = re.split('/', x)[-3] - if na not in y: - print ('X Y rgb vox file not consistent!') - exit() - X_rgb_new.append(x) - Y_vox_new.append(y) - self.X_rgb_train_files = X_rgb_new - self.Y_vox_train_files = Y_vox_new - self.total_train_batch_num = int(len(self.X_rgb_train_files)/(self.batch_size*train_mv)) - - def shuffle_test_files(self, test_mv, seed): -# print('shuffle_test_files') - X_rgb_new=[]; Y_vox_new=[] - X_rgb = self.X_rgb_test_files_ori - Y_vox = self.Y_vox_test_files_ori - - num = self.total_mv - ### - cat_test_1st_index_new =[] - for k in self.cat_test_1st_index: - cat_test_1st_index_new.append( int(k/num)* int(num/test_mv)*test_mv) - self.cat_test_1st_index=cat_test_1st_index_new - ### - total_obj = int(len(X_rgb)/num) - for i in range(total_obj): - view_ind = list(range(num)) - random.Random(i+seed).shuffle(view_ind) - - valid_view_num = int(num/test_mv)*test_mv - view_ind = view_ind[0:valid_view_num] - for id in view_ind: - X_rgb_new.append(X_rgb[i*num+id]) - Y_vox_new.append(Y_vox[i*num+id]) - - self.X_rgb_test_files = X_rgb_new - self.Y_vox_test_files = Y_vox_new - -class Ops: - - @staticmethod - def lrelu(x, leak=0.2): - return tf.nn.leaky_relu(x,alpha=0.2,name=None) - - @staticmethod - def relu(x): - return tf.nn.relu(x) - - @staticmethod - def xxlu(x, label='relu'): - if label == 'relu': - return Ops.relu(x) - if label == 'lrelu': - return Ops.lrelu(x, leak=0.2) - - @staticmethod - def variable_sum(var, name): - with tf.name_scope(name): - mean = tf.reduce_mean(var) - tf.summary.scalar('mean', mean) - stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean))) - tf.summary.scalar('stddev', stddev) - tf.summary.scalar('max', tf.reduce_max(var)) - tf.summary.scalar('min', tf.reduce_min(var)) - tf.summary.histogram('histogram', var) - - @staticmethod - def variable_count(): - total_para = 0 - for variable in tf.trainable_variables(): - shape = variable.get_shape() - variable_para = 1 - for dim in shape: - variable_para *= dim.value - total_para += variable_para - return total_para - - @staticmethod - def fc(x, out_d, name): - xavier_init = tf.contrib.layers.xavier_initializer() - zero_init = tf.zeros_initializer() - in_d = x.get_shape()[1] - w = tf.get_variable(name + '_w', [in_d, out_d], initializer=xavier_init,dtype=tf.float32) - b = tf.get_variable(name + '_b', [out_d], initializer=zero_init,dtype=tf.float32) - y = tf.nn.bias_add(tf.matmul(x, w), b) - Ops.variable_sum(w, name) - return y - - @staticmethod - def conv2d(x, k, out_c, str, name, pad='SAME'): - xavier_init = tf.contrib.layers.xavier_initializer() - zero_init = tf.zeros_initializer() - in_c = x.get_shape()[3] - w = tf.get_variable(name + '_w', [k, k, in_c, out_c], initializer=xavier_init, dtype=tf.float32) - b = tf.get_variable(name + '_b', [out_c], initializer=zero_init,dtype=tf.float32) - - stride = [1, str, str, 1] - y = tf.nn.bias_add(tf.nn.conv2d(x, w, stride, pad), b) - Ops.variable_sum(w, name) - return y - - @staticmethod - def maxpool2d(x, k, s,name, pad='SAME'): - ker = [1,k,k,1] - str = [1,s,s,1] - y = tf.nn.max_pool(x,ksize=ker, strides=str, padding=pad, name=name) - return y - - @staticmethod - def conv3d(x, k, out_c, str, name, pad='SAME'): - xavier_init = tf.contrib.layers.xavier_initializer() - zero_init = tf.zeros_initializer() - in_c = x.get_shape()[4] - w = tf.get_variable(name + '_w', [k, k, k, in_c, out_c], initializer=xavier_init, dtype=tf.float32) - b = tf.get_variable(name + '_b', [out_c], initializer=zero_init, dtype=tf.float32) - stride = [1, str, str, str, 1] - y = tf.nn.bias_add(tf.nn.conv3d(x, w, stride, pad), b) - Ops.variable_sum(w, name) - return y - - @staticmethod - def deconv3d(x, k, out_c, str, name, pad='SAME'): - xavier_init = tf.contrib.layers.xavier_initializer() - zero_init = tf.zeros_initializer() - bat = tf.shape(x)[0] - [_, in_d1, in_d2, in_d3, in_c] = x.get_shape() - in_d1 = int(in_d1); in_d2 = int(in_d2); in_d3 = int(in_d3); in_c = int(in_c) - w = tf.get_variable(name + '_w', [k, k, k, out_c, in_c], initializer=xavier_init, dtype=tf.float32) - b = tf.get_variable(name + '_b', [out_c], initializer=zero_init, dtype=tf.float32) - - out_shape = [bat, in_d1 * str, in_d2 * str, in_d3 * str, out_c] - stride = [1, str, str, str, 1] - y = tf.nn.conv3d_transpose(x, w, output_shape=out_shape, strides=stride, padding=pad) - y = tf.nn.bias_add(y, b) - Ops.variable_sum(w, name) - return y - - @staticmethod - def maxpool3d(x,k,s,name,pad='SAME'): - ker = [1,k,k,k,1] - str = [1,s,s,s,1] - y = tf.nn.max_pool3d(x,ksize=ker,strides=str,padding=pad, name=name) - return y - - - - diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/voxel.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/voxel.py deleted file mode 100644 index 8bce75ecf..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/voxel.py +++ /dev/null @@ -1,89 +0,0 @@ -# -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -# Copyright 2021 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from npu_bridge.npu_init import * -import numpy as np - - -def evaluate_voxel_prediction(preds, gt, thresh): - preds_occupy = preds[:, 1, :, :] >= thresh - diff = np.sum(np.logical_xor(preds_occupy, gt[:, 1, :, :])) - intersection = np.sum(np.logical_and(preds_occupy, gt[:, 1, :, :])) - union = np.sum(np.logical_or(preds_occupy, gt[:, 1, :, :])) - num_fp = np.sum(np.logical_and(preds_occupy, gt[:, 0, :, :])) # false positive - num_fn = np.sum(np.logical_and(np.logical_not(preds_occupy), gt[:, 1, :, :])) # false negative - return np.array([diff, intersection, union, num_fp, num_fn]) - - -def voxel2mesh(voxels, surface_view): - cube_verts = [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], - [1, 1, 1]] # 8 points - - cube_faces = [[0, 1, 2], [1, 3, 2], [2, 3, 6], [3, 7, 6], [0, 2, 6], [0, 6, 4], [0, 5, 1], - [0, 4, 5], [6, 7, 5], [6, 5, 4], [1, 7, 3], [1, 5, 7]] # 12 face - - cube_verts = np.array(cube_verts) - cube_faces = np.array(cube_faces) + 1 - - scale = 0.01 - cube_dist_scale = 1.1 - verts = [] - faces = [] - curr_vert = 0 - - positions = np.where(voxels > 0.3) - voxels[positions] = 1 - for i, j, k in zip(*positions): - # identifies if current voxel has an exposed face - if not surface_view or np.sum(voxels[i-1:i+2, j-1:j+2, k-1:k+2]) < 27: - verts.extend(scale * (cube_verts + cube_dist_scale * np.array([[i, j, k]]))) - faces.extend(cube_faces + curr_vert) - curr_vert += len(cube_verts) - - return np.array(verts), np.array(faces) - - -def write_obj(filename, verts, faces): - """ write the verts and faces on file.""" - with open(filename, 'w') as f: - # write vertices - f.write('g\n# %d vertex\n' % len(verts)) - for vert in verts: - f.write('v %f %f %f\n' % tuple(vert)) - - # write faces - f.write('# %d faces\n' % len(faces)) - for face in faces: - f.write('f %d %d %d\n' % tuple(face)) - - -def voxel2obj(filename, pred, surface_view = True): - verts, faces = voxel2mesh(pred, surface_view) - write_obj(filename, verts, faces) - -- Gitee From 9aa28751ec8079a6f567fc616bb8b09f6f028936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:41:27 +0000 Subject: [PATCH 02/16] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/Pix2Vox=5FID1284=5Ffor=5FTensorFlow/Model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/.keep | 0 .../contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/test_sum/.keep | 0 .../cv/Pix2Vox_ID1284_for_TensorFlow/Model/train_mod/.keep | 0 .../cv/Pix2Vox_ID1284_for_TensorFlow/Model/train_sum/.keep | 0 4 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/.keep delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/test_sum/.keep delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/train_mod/.keep delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/train_sum/.keep diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/.keep b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/test_sum/.keep b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/test_sum/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/train_mod/.keep b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/train_mod/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/train_sum/.keep b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/train_sum/.keep deleted file mode 100644 index e69de29bb..000000000 -- Gitee From 3c739cc741fcd585484c940cb59bb34a449bb73c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:41:33 +0000 Subject: [PATCH 03/16] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/Pix2Vox=5FID1284=5Ffor=5FTensorFlow/log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/log/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/log/.keep diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/log/.keep b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/log/.keep deleted file mode 100644 index e69de29bb..000000000 -- Gitee From f323ee0f5282945feaa4bb771dddf1ad1696c39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:41:43 +0000 Subject: [PATCH 04/16] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20TensorFlow/?= =?UTF-8?q?contrib/cv/Pix2Vox=5FID1284=5Ffor=5FTensorFlow/test/=20train=5F?= =?UTF-8?q?full=5F1p.sh=20=E4=B8=BA=20TensorFlow/contrib/cv/Pix2Vox=5FID12?= =?UTF-8?q?84=5Ffor=5FTensorFlow/test/train=5Ffull=5F1p.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/{ train_full_1p.sh => train_full_1p.sh} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/{ train_full_1p.sh => train_full_1p.sh} (100%) diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/ train_full_1p.sh b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_full_1p.sh similarity index 100% rename from TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/ train_full_1p.sh rename to TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_full_1p.sh -- Gitee From 2447e38f0e503b3530c4a15b24efad2bc947aa34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:41:49 +0000 Subject: [PATCH 05/16] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/Pix2Vox=5FID1284=5Ffor=5FTensorFlow/test/.k?= =?UTF-8?q?eep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/.keep diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/.keep b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/.keep deleted file mode 100644 index e69de29bb..000000000 -- Gitee From 0aca247b6f3d2817c6b1ec7446d1b8e2545db621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:42:05 +0000 Subject: [PATCH 06/16] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/Pix2Vox=5FID1284=5Ffor=5FTensorFlow/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/.keep diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/.keep b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/.keep deleted file mode 100644 index e69de29bb..000000000 -- Gitee From b9d7198aed6bff338257a509fb03b4c2efcd2e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:42:17 +0000 Subject: [PATCH 07/16] update TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_performance_1p.sh. --- .../Pix2Vox_ID1284_for_TensorFlow/test/train_performance_1p.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_performance_1p.sh b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_performance_1p.sh index b1b6c36e1..ea30cc72d 100644 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_performance_1p.sh +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_performance_1p.sh @@ -1,7 +1,7 @@ #!/bin/bash #当前路径,不需要修改 -cur_path=`pwd`/../Code/ +cur_path=`pwd`/../ #集合通信参数,不需要修改 -- Gitee From 281889d7370d53ed575b5f98be72cb9209df98cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:46:04 +0000 Subject: [PATCH 08/16] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=AE=AD=E7=BB=83?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=96=87=E4=BB=B6=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main_AttSets.py | 765 ++++++++++++++++++ 1 file changed, 765 insertions(+) create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/main_AttSets.py diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/main_AttSets.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/main_AttSets.py new file mode 100644 index 000000000..69155def8 --- /dev/null +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/main_AttSets.py @@ -0,0 +1,765 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from npu_bridge.npu_init import * +import tensorflow as tf +import os + +# os.system('pip install scipy==0.19.0') +# os.system('pip install matplotlib==2.0.2') +# os.system('pip install skimage==0.13.0') +# os.system('pip install PyMCubes') + +import shutil +import sys +import scipy.io + +sys.path.append('..') +import tools as tools +import numpy as np +import time +from keras.layers import BatchNormalization, Conv3D, MaxPooling3D, Dense, Reshape, Add, LeakyReLU, Conv3DTranspose +from keras.activations import relu, sigmoid, tanh +from keras import models +import copy +import matplotlib.pyplot as plt + +import argparse + +# import moxing as mox + + +parser = argparse.ArgumentParser() +parser.add_argument('--train_url', type=str, help='the path model saved') +parser.add_argument("--data_url", type=str, default="./dataset") +obs_config = parser.parse_args() + +################### +# import tflearn +################### + +batch_size = 2 +img_res = 127 +vox_res32 = 32 +total_mv = 24 +GPU0 = '0' +GPU1 = '1' + +re_train = True +# re_train=True +single_view_train = True +multi_view_train = True + +########################### + +plot_list_iou = [] +plot_list_i = [] +iii = 0 +config = {} # python dictionary +config['batch_size'] = batch_size +config['total_mv'] = total_mv +# config['cat_names'] = ['02691156','02828884','04530566','03636649','03001627'] +# config['cat_names'] = ['02691156','02828884','02933112','02958343','03001627','03211117', +# '03636649','03691459','04090263','04256520','04379243','04401088','04530566'] +config['cat_names'] = ['02691156'] +for name in config['cat_names']: + config['X_rgb_' + name] = obs_config.data_url + '/ShapeNetRendering/' + name + '/' + + config['Y_vox_' + name] = obs_config.data_url + '/ShapeNetVox32/' + name + '/' + + +# config['Y_vox_'+name] = '/home/wiproec4/3d reconstruction/attsets/Data_sample/shapenet dataset/ShapeNetVox32/train_1_dataset/'+name+'/' + + +# output : {'batch_size': 1, 'total_mv': 24, 'cat_names': ['03001627'], 'Y_vox_03001627': '/home/wiproec4/3d reconstruction/attsets/Data_sample/#ShapeNetVox32/03001627/', 'X_rgb_03001627': '/home/wiproec4/3d reconstruction/attsets/Data_sample/ShapeNetRendering/03001627/'} + +def metric_iou(prediction, gt): + # labels = tf.greater_equal(gt[gt], 0.5) + # prediction = tf.cast(prediction,tf.int32) + predictions = tf.greater_equal(prediction, 0.5) + gt_ = tf.greater_equal(gt, 0.5) + intersection = tf.reduce_sum(tf.cast(tf.logical_and(predictions, gt_), tf.float32)) + union = tf.reduce_sum(tf.cast(tf.math.logical_or(predictions, gt_), tf.float32)) + iou = tf.cast(x=intersection, dtype=tf.float32) / tf.cast(x=union, dtype=tf.float32) + return iou + + +def graph_plot(iou_value, i_value): + x = i_value + y = iou_value + plt.plot(x, y, color='green', linestyle='dashed', linewidth=3, marker='o', markerfacecolor='blue', markersize=12) + + plt.ylim(0, 2) + plt.xlim(0, 500) + + plt.xlabel('Iterations') + + plt.ylabel('IOU') + + plt.title('Refiner Accuracy') + + plt.show() + + +def evaluate_voxel_prediction(prediction, gt): + # """ The prediction and gt are 3 dim voxels. Each voxel has values 1 or 0""" + prediction[prediction >= 0.5] = 1 + prediction[prediction < 0.5] = 0 + intersection = np.sum(np.logical_and(prediction, gt)) + union = np.sum(np.logical_or(prediction, gt)) + IoU = float(intersection) / float(union) + return IoU + + +##################################### +def refiner_network(volumes_in): + with tf.device('/cpu:0'): + with tf.variable_scope('ref_enc'): + input_volumes_32 = tf.reshape(volumes_in, [-1, vox_res32, vox_res32, vox_res32, 1], name="ref_net_in") + + print("input_volumes_32_shape", input_volumes_32.shape) # input_volumes_32_shape (?,32,32,32,1) + + rn1 = Conv3D(filters=32, kernel_size=(4, 4, 4), padding='same', data_format="channels_last", name='ref_c1')( + input_volumes_32) + rn2 = BatchNormalization()(rn1) + rn3 = LeakyReLU(alpha=.2)(rn2) + print("rn3.shape", rn3.shape) # rn3.shape (?, 32, 32, 32, 32) + volumes_16_l = MaxPooling3D(pool_size=(2, 2, 2), name='ref_m1')(rn3) + + print("volumes_16_l_shape", volumes_16_l.shape) # volumes_16_l_shape (?,16,16,16,32) + + rn5 = Conv3D(filters=64, kernel_size=(4, 4, 4), padding='same', data_format="channels_last", name='ref_c2')( + volumes_16_l) + rn6 = BatchNormalization()(rn5) + rn7 = LeakyReLU(alpha=.2)(rn6) + print("rn7.shape", rn7.shape) # rn7.shape (?, 16, 16, 16, 64) + volumes_8_l = MaxPooling3D(pool_size=(2, 2, 2), name='ref_m2')(rn7) + + print("volumes_8_l_shape", volumes_8_l.shape) # volumes_8_l_shape (?,8,8,8,64) + + rn9 = Conv3D(filters=128, kernel_size=(4, 4, 4), padding='same', data_format="channels_last", + name='ref_c3')(volumes_8_l) + rn10 = BatchNormalization()(rn9) + rn11 = LeakyReLU(alpha=.2)(rn10) + print("rn11.shape", rn11.shape) # rn11.shape (?, 8, 8, 8, 128) + volumes_4_l = MaxPooling3D(pool_size=(2, 2, 2), name='ref_m3')(rn11) + + print("volumes_4_l_shape", volumes_4_l.shape) # volumes_4_l_shape (?,4,4,4,128) + + flatten_features = tf.reshape(volumes_4_l, [-1, 8192], name="ref_fc1_in") + with tf.variable_scope('ref_fc'): + fc1 = Dense(units=2048, activation='relu', name='ref_fc1')(flatten_features) + # fc1=tanh(fc1) + + fc1 = relu(fc1, alpha=0.0, max_value=None, threshold=0.0) + + print("fc1_shape", fc1.shape) # fc1_shape (?,4,4,4,2048) + + fc2 = Dense(units=8192, activation='relu', name='ref_fc2')(fc1) + # fc2=tanh(fc2) + fc2 = relu(fc2, alpha=0.0, max_value=None, threshold=0.0) + + print("fc2_shape", fc2.shape) # fc2_shape (?,4,4,4,8192) + + fc2 = tf.reshape(fc2, [-1, 4, 4, 4, 128], name="ref_fc2_out") + + with tf.variable_scope('ref_Dec'): + reshaped_1 = Add()([fc2, volumes_4_l]) + + print("reshaped_1.shape", reshaped_1.shape) # reshaped_1.shape (?,4,4,4,128) + + rn13 = Conv3DTranspose(filters=64, kernel_size=(4, 4, 4), padding='same', data_format="channels_last", + name='ref_d1', strides=(2, 2, 2))(reshaped_1) + + rn14 = BatchNormalization()(rn13) + volumes_4_r = relu(rn14, alpha=0.0, max_value=None, threshold=0.0) + + print("volumes_4_r_shape", volumes_4_r.shape) # volumes_4_r_shape (?,8,8,8,64) + + reshaped_2 = Add()([volumes_4_r, volumes_8_l]) + + print("reshaped_2_shape", reshaped_2.shape) # volumes_2_shape (?,8,8,8,64) + + rn16 = Conv3DTranspose(filters=32, kernel_size=(4, 4, 4), padding='same', data_format="channels_last", + name='ref_d2', strides=(2, 2, 2))(reshaped_2) + rn17 = BatchNormalization()(rn16) + volumes_8_r = relu(rn17, alpha=0.0, max_value=None, threshold=0.0) + + reshaped_3 = Add()([volumes_8_r, volumes_16_l]) + + print("reshaped_3_shape", reshaped_3.shape) # reshaped_3_shape (?,16,16,16,32) + + rn19 = Conv3DTranspose(filters=1, kernel_size=(4, 4, 4), padding='same', data_format="channels_last", + name='ref_d3', strides=(2, 2, 2))(volumes_8_r) + print("rn19_shape", rn19.shape) # rn19_shape (?, ?, ?, ?, 1) + # volumes_16_r= tf.nn.sigmoid(rn19,name='ref_sigmoid1') + # reshape_4=volumes_16_r #################### + + reshape_4 = Add()([rn19, input_volumes_32]) + reshape_4 = (reshape_4 * 0.5) + print("reshape_4_5", reshape_4.shape) # reshape_4_5 (?,32,32,32,1) + + reshape_4 = tf.nn.sigmoid(reshape_4, name='ref_sigmoid1') + + print("reshape_4_sig_shape", reshape_4.shape) # reshape_4_sig_shape (?,32,32,32,1) + + reshape_5 = tf.reshape(reshape_4, [-1, vox_res32, vox_res32, vox_res32], name="ref_out") + + return reshape_5 + + +def attsets_fc(x, out_ele_num): + with tf.variable_scope('att_fc'): + in_ele_num = tf.shape(x)[1] + in_ele_len = int(x.get_shape()[2]) + out_ele_len = in_ele_len + print("out_ele_len ", out_ele_len) + #################### + x_1st = x + x_1st_tp = tf.reshape(x_1st, [-1, in_ele_len], name="att_in") + weights_1st = tools.Ops.fc(x_1st_tp, out_d=out_ele_num * out_ele_len, name="att") + + ########## option 1 + weights_1st = weights_1st + ########## option 2 + # weights_1st = tf.nn.tanh(weights_1st) + + #################### + weights_1st = tf.reshape(weights_1st, [-1, in_ele_num, out_ele_num, out_ele_len], name="att_fc_out") + weights_1st = tf.nn.softmax(weights_1st, 1) + x_1st = tf.tile(x_1st[:, :, None, :], [1, 1, out_ele_num, 1]) + x_1st = x_1st * weights_1st + x_1st = tf.reduce_sum(x_1st, axis=1) + x_1st = tf.reshape(x_1st, [-1, out_ele_num * out_ele_len], name="att_out") + return x_1st, weights_1st + + +##################################### +class Network: + def __init__(self): + self.train_mod_dir = './Model/train_mod/' + self.train_sum_dir = './Model/train_sum/' + self.test_res_dir = './Model/test_res/' + self.test_sum_dir = './Model/test_sum/' + + print('re_train : ', re_train) + if os.path.exists(self.test_res_dir): + if re_train: + print('test_res_dir and files kept!') + else: + shutil.rmtree(self.test_res_dir) + os.makedirs(self.test_res_dir) + print('test_res_dir: deleted and then created!') + else: + os.makedirs(self.test_res_dir) + print('test_res_dir: created!') + + if os.path.exists(self.train_mod_dir): + if re_train: + if os.path.exists(self.train_mod_dir + 'model.cptk.data-00000-of-00001'): + print('model found! will be reused!') + else: + print('model not found! error!') + # exit() + else: + shutil.rmtree(self.train_mod_dir) + os.makedirs(self.train_mod_dir) + print('train_mod_dir: deleted and then created!') + else: + os.makedirs(self.train_mod_dir) + print('train_mod_dir: created!') + + if os.path.exists(self.train_sum_dir): + if re_train: + print('train_sum_dir and files kept!') + else: + shutil.rmtree(self.train_sum_dir) + os.makedirs(self.train_sum_dir) + print('train_sum_dir: deleted and then created!') + else: + os.makedirs(self.train_sum_dir) + print('train_sum_dir: created!') + + if os.path.exists(self.test_sum_dir): + if re_train: + print('test_sum_dir and files kept!') + else: + shutil.rmtree(self.test_sum_dir) + os.makedirs(self.test_sum_dir) + print('test_sum_dir: deleted and then created!') + else: + os.makedirs(self.test_sum_dir) + print('test_sum_dir: created!') + + def base_r2n2(self, X_rgb): + with tf.variable_scope('Encoder'): + im_num = tf.shape(X_rgb)[1] + + [_, _, d1, d2, cc] = X_rgb.get_shape() + X_rgb = tf.reshape(X_rgb, [-1, int(d1), int(d2), int(cc)], name="en_in") + print("Network Structure") + print("base_r2n2", X_rgb.shape) # base_r2n2 (?, 127, 127, 3) + + # plot_buf_1= tf.reshape(X_rgb, [-1, 127, 127, 3]) ################### + # tf.summary.image("Input", plot_buf_1)############################### + + en_c = [96, 128, 256, 256, 256, 256] + l1 = tools.Ops.xxlu(tools.Ops.conv2d(X_rgb, k=7, out_c=en_c[0], str=1, name='en_c1'), label='lrelu') + print("l1_r2n", l1.shape) # l1_r2n (?, 127, 127, 96) + + l2 = tools.Ops.xxlu(tools.Ops.conv2d(l1, k=3, out_c=en_c[0], str=1, name='en_c2'), label='lrelu') + l2 = tools.Ops.maxpool2d(l2, k=2, s=2, name='en_mp1') + print("l2_r2n", l2.shape) # l2_r2n (?, 64, 64, 96) + + # plot_buf_1= tf.reshape(l2, [-1, 32, 32, 3]) ######################### + # tf.summary.image("L2_MP_en", plot_buf_1)############################# + + l3 = tools.Ops.xxlu(tools.Ops.conv2d(l2, k=3, out_c=en_c[1], str=1, name='en_c3'), label='lrelu') + print("l3_r2n", l3.shape) # l3_r2n (?, 64, 64, 128) + l4 = tools.Ops.xxlu(tools.Ops.conv2d(l3, k=3, out_c=en_c[1], str=1, name='en_c4'), label='lrelu') + print("l4_r2n", l4.shape) # l4_r2n (?, 64, 64, 128) + l22 = tools.Ops.conv2d(l2, k=1, out_c=en_c[1], str=1, name='en_c22') + print("l22_r2n", l22.shape) # l22_r2n (?, 64, 64, 128) + l4 = l4 + l22 + l4 = tools.Ops.maxpool2d(l4, k=2, s=2, name='en_mp2') + print("l4+l22_r2n", l4.shape) # l4+l22_r2n (?, 32, 32, 128) + + # plot_buf_1= tf.reshape(l4, [-1, 32, 32, 3]) ########################## + # tf.summary.image("l4+l22_en", plot_buf_1)############################# + + l5 = tools.Ops.xxlu(tools.Ops.conv2d(l4, k=3, out_c=en_c[2], str=1, name='en_c5'), label='lrelu') + print("l5_r2n", l5.shape) # l5_r2n (?, 32, 32, 256) + l6 = tools.Ops.xxlu(tools.Ops.conv2d(l5, k=3, out_c=en_c[2], str=1, name='en_c6'), label='lrelu') + print("l6_r2n", l6.shape) # l6_r2n (?, 32, 32, 256) + l44 = tools.Ops.conv2d(l4, k=1, out_c=en_c[2], str=1, name='en_c44') + print("l44_r2n", l44.shape) # l44_r2n (?, 32, 32, 256) + l6 = l6 + l44 + l6 = tools.Ops.maxpool2d(l6, k=2, s=2, name='en_mp3') + print("l6+l44_r2n", l6.shape) # l6+l44_r2n (?, 16, 16, 256) + + # plot_buf_1= tf.reshape(l6, [-1, 16, 16, 3]) ########################## + # tf.summary.image("l6+l44_en", plot_buf_1)############################# + + l7 = tools.Ops.xxlu(tools.Ops.conv2d(l6, k=3, out_c=en_c[3], str=1, name='en_c7'), label='lrelu') + print("l7_r2n", l7.shape) # l7_r2n (?, 16, 16, 256) + l8 = tools.Ops.xxlu(tools.Ops.conv2d(l7, k=3, out_c=en_c[3], str=1, name='en_c8'), label='lrelu') + print("l8_r2n", l8.shape) # l8_r2n (?, 16, 16, 256) + # l66=tools.Ops.conv2d(l6, k=1, out_c=en_c[3], str=1, name='en_c66') + # print("l66_r2n",l66.shape) + # l8=l8+l66 + l8 = tools.Ops.maxpool2d(l8, k=2, s=2, name='en_mp4') + print("l8_r2n", l8.shape) # l8_r2n (?, 8, 8, 256) + + # plot_buf_1= tf.reshape(l8, [-1, 8, 8, 3]) ######################## + # tf.summary.image("l8_en", plot_buf_1)############################# + + l9 = tools.Ops.xxlu(tools.Ops.conv2d(l8, k=3, out_c=en_c[4], str=1, name='en_c9'), label='lrelu') + print("l9_r2n", l9.shape) # l9_r2n (?, 8, 8, 256) + l10 = tools.Ops.xxlu(tools.Ops.conv2d(l9, k=3, out_c=en_c[4], str=1, name='en_c10'), label='lrelu') + print("l10_r2n", l10.shape) # l10_r2n (?, 8, 8, 256) + l88 = tools.Ops.conv2d(l8, k=1, out_c=en_c[4], str=1, name='en_c88') + print("l88_r2n", l88.shape) # l88_r2n (?, 8, 8, 256) + l10 = l10 + l88 + l10 = tools.Ops.maxpool2d(l10, k=2, s=2, name='en_mp5') + print("l10_r2n", l10.shape) # l10_r2n (?, 4, 4, 256) + + # plot_buf_1= tf.reshape(l10, [-1, 4, 4, 3]) ######################## + # tf.summary.image("l10_en", plot_buf_1)############################# + + l11 = tools.Ops.xxlu(tools.Ops.conv2d(l10, k=3, out_c=en_c[5], str=1, name='en_c11'), label='lrelu') + print("l11_r2n", l11.shape) # l11_r2n (?, 4, 4, 256) + l12 = tools.Ops.xxlu(tools.Ops.conv2d(l11, k=3, out_c=en_c[5], str=1, name='en_c12'), label='lrelu') + print("l12_r2n", l12.shape) # l12_r2n (?, 4, 4, 256) + l1010 = tools.Ops.conv2d(l10, k=1, out_c=en_c[5], str=1, name='en_c1010') + print("l1010_r2n", l1010.shape) # l1010_r2n (?, 4, 4, 256) + l12 = l12 + l1010 + l12 = tools.Ops.maxpool2d(l12, k=2, s=2, name='en_mp6') + print("l12_r2n", l12.shape) # l12_r2n (?, 2, 2, 256) + + # plot_buf_1= tf.reshape(l12, [-1, 2, 2, 3]) ######################## + # tf.summary.image("l12_en", plot_buf_1)############################# + + [_, d1, d2, cc] = l12.get_shape() + l12 = tf.reshape(l12, [-1, int(d1) * int(d2) * int(cc)], name="en_fc1_in") + print("fc1_input_r2n", l12.shape) # fc1_input_r2n (?, 1024) + fc = tools.Ops.xxlu(tools.Ops.fc(l12, out_d=1024, name='en_fc1'), label='lrelu') + print("fc1_output_r2n", fc.shape) # fc1_output_r2n (?, 1024) + + with tf.variable_scope('Att_Net'): + #### use fc attention + input = tf.reshape(fc, [-1, im_num, 1024], name="Att_fc_in") + print("att_fc_in_r2n", input.shape) # att_fc_in_r2n (?, ?, 1024) + latent_3d, weights = attsets_fc(input, out_ele_num=1) + print("att_fc_out_r2n", latent_3d.shape) # att_fc_out_r2n (?, 1024) + + with tf.variable_scope('Decoder'): + #### + latent_3d = tools.Ops.xxlu(tools.Ops.fc(latent_3d, out_d=4 * 4 * 4 * 128, name='de_fc2'), label='lrelu') + print("fc3_out_r2n", latent_3d.shape) # fc3_out_r2n (?, 8192) + latent_3d = tf.reshape(latent_3d, [-1, 4, 4, 4, 128], name="de_fc2_out") + + #### + + de_c = [128, 128, 128, 64, 32, 1] + + print("d1_in_r2n", latent_3d.shape) # d1_in_r2n (?, 4, 4, 4, 128) + d1 = tools.Ops.xxlu(tools.Ops.deconv3d(latent_3d, k=3, out_c=de_c[1], str=2, name='de_c1'), label='lrelu') + print("d1_out_r2n", d1.shape) # d1_out_r2n (?, 8, 8, 8, 128) + d2 = tools.Ops.xxlu(tools.Ops.deconv3d(d1, k=3, out_c=de_c[1], str=1, name='de_c2'), label='lrelu') + print("d2_out_r2n", d2.shape) # d2_out_r2n (?, 8, 8, 8, 128) + d00 = tools.Ops.deconv3d(latent_3d, k=1, out_c=de_c[1], str=2, name='de_c00') + print("d00_out_r2n", d00.shape) # d00_out_r2n (?, 8, 8, 8, 128) + d2 = d2 + d00 + print("d2+d00_out_r2n", d2.shape) # d2+d00_out_r2n (?, 8, 8, 8, 128) + + # plot_buf_1= tf.reshape(d2, [-1, 8, 8, 4]) ################################ + # tf.summary.image("d2+d00_out_de", plot_buf_1)############################# + + d3 = tools.Ops.xxlu(tools.Ops.deconv3d(d2, k=3, out_c=de_c[2], str=2, name='de_c3'), label='lrelu') + print("d3_out_r2n", d3.shape) # d3_out_r2n (?, 16, 16, 16, 128) + d4 = tools.Ops.xxlu(tools.Ops.deconv3d(d3, k=3, out_c=de_c[2], str=1, name='de_c4'), label='lrelu') + print("d4_out_r2n", d4.shape) # d4_out_r2n (?, 16, 16, 16, 128) + d22 = tools.Ops.deconv3d(d2, k=1, out_c=de_c[2], str=2, name='de_c22') + print("d22_out_r2n", d22.shape) # d22_out_r2n (?, 16, 16, 16, 128) + d4 = d4 + d22 + print("d4+d22_out_r2n", d4.shape) # d4+d22_out_r2n (?, 16, 16, 16, 128) + + # plot_buf_1= tf.reshape(d4, [-1, 16, 16, 4]) ############################## + # tf.summary.image("d4+d22_out_de", plot_buf_1)############################# + + d5 = tools.Ops.xxlu(tools.Ops.deconv3d(d4, k=3, out_c=de_c[3], str=2, name='de_c5'), label='lrelu') + print("d5_out_r2n", d5.shape) # d5_out_r2n (?, 32, 32, 32, 64) + d6 = tools.Ops.xxlu(tools.Ops.deconv3d(d5, k=3, out_c=de_c[3], str=1, name='de_c6'), label='lrelu') + print("d6_out_r2n", d6.shape) # d6_out_r2n (?, 32, 32, 32, 64) + d44 = tools.Ops.deconv3d(d4, k=1, out_c=de_c[3], str=2, name='de_c44') + print("d44_out_r2n", d44.shape) # d44_out_r2n (?, 32, 32, 32, 64) + d6 = d6 + d44 + print("d6+d44_out_r2n", d6.shape) # d6+d44_out_r2n (?, 32, 32, 32, 64) + + # plot_buf_1= tf.reshape(d6, [-1, 32, 32, 4]) ############################## + # tf.summary.image("d6+d44_out_de", plot_buf_1)############################# + + d7 = tools.Ops.xxlu(tools.Ops.deconv3d(d6, k=3, out_c=de_c[4], str=1, name='de_c7'), label='lrelu') + print("d7_out_r2n", d7.shape) # d7_out_r2n (?, 32, 32, 32, 32) + d8 = tools.Ops.xxlu(tools.Ops.deconv3d(d7, k=3, out_c=de_c[4], str=1, name='de_c8'), label='lrelu') + print("d8_out_r2n", d8.shape) # d8_out_r2n (?, 32, 32, 32, 32) + d77 = tools.Ops.xxlu(tools.Ops.deconv3d(d7, k=3, out_c=de_c[4], str=1, name='de_c77'), label='lrelu') + print("d77_out_r2n", d77.shape) # d77_out_r2n (?, 32, 32, 32, 32) + d8 = d8 + d77 + print("d8+d77_out_r2n", d8.shape) # d8+d77_out_r2n (?, 32, 32, 32, 32) + + # plot_buf_1= tf.reshape(d8, [-1, 32, 32, 4]) ############################## + # tf.summary.image("d8+d77_out_de", plot_buf_1)############################# + + d11 = tools.Ops.deconv3d(d8, k=3, out_c=de_c[5], str=1, name='de_c9') + print("d11_out_r2n", d11.shape) # d11_out_r2n (?, 32, 32, 32, 1) + + # plot_buf_1= tf.reshape(d11, [-1, 32, 32,4]) ########################## + # tf.summary.image("Ref_input", plot_buf_1)############################# + + ref_in = tf.reshape(d11, [-1, vox_res32, vox_res32, vox_res32], name="ref_in") ### + + y = tf.nn.sigmoid(d11, name='de_sigmoid') + + att_o = tf.reshape(y, [-1, vox_res32, vox_res32, vox_res32], name="de_out") + + print("att_out_shape", att_o.shape) # att_out_shape (?, 32, 32, 32) + + with tf.variable_scope('ref_net'): + ref_o = refiner_network(ref_in) + + # plot_buf_1= tf.reshape(ref_o, [-1, 32, 32,4]) ###################### + # tf.summary.image("Ref_Out", plot_buf_1)############################# + + return ref_o, att_o, weights + + def build_graph(self): + img_res = 127 + vox_res = 32 + self.X_rgb = tf.placeholder(shape=[None, None, img_res, img_res, 3], dtype=tf.float32) + self.Y_vox = tf.placeholder(shape=[None, vox_res, vox_res, vox_res], dtype=tf.float32) + self.lr = tf.placeholder(tf.float32) + self.refine_lr = tf.placeholder(tf.float32) + with tf.device('/cpu:0'): + self.Y_pred, self.vae_o, self.weights = self.base_r2n2(self.X_rgb) + tf.summary.histogram('Attsets_Weights', self.weights) + with tf.device('/cpu:0'): + ### rec loss + print("reached") + with tf.variable_scope('Loss_Fun'): + Y_vox_ = tf.reshape(self.Y_vox, shape=[-1, vox_res ** 3]) + Y_pred_ = tf.reshape(self.Y_pred, shape=[-1, vox_res ** 3]) + vae_o_ = tf.reshape(self.vae_o, shape=[-1, vox_res ** 3]) + + self.vae_loss = tf.reduce_mean(-tf.reduce_mean(Y_vox_ * tf.log(vae_o_ + 1e-8), reduction_indices=[1]) - + tf.reduce_mean((1 - Y_vox_) * tf.log(1 - vae_o_ + 1e-8), + reduction_indices=[1])) + self.rec_loss = tf.reduce_mean(-tf.reduce_mean(Y_vox_ * tf.log(Y_pred_ + 1e-8), reduction_indices=[1]) - + tf.reduce_mean((1 - Y_vox_) * tf.log(1 - Y_pred_ + 1e-8), + reduction_indices=[1])) + sum_rec_loss = tf.summary.scalar('rec_loss', self.rec_loss) + self.sum_merged = sum_rec_loss + tf.summary.histogram('rec_loss', self.rec_loss) + tf.summary.scalar("vae-loss", self.vae_loss) + tf.summary.histogram("vae_loss", self.vae_loss) + self.mean_loss = tf.div(x=tf.math.add(x=self.vae_loss, y=self.rec_loss, name='add_loss'), y=2, + name='mean_loss') + tf.summary.histogram("mean_vae_loss", self.mean_loss) + tf.summary.scalar("mean_vae_loss", self.mean_loss) + + with tf.variable_scope('Evaluation_Metric'): + gt_vox = Y_vox_ + self.iou_ref = metric_iou(Y_pred_, gt_vox) + tf.summary.scalar('iou_refiner', self.iou_ref) + tf.summary.histogram('iou_refiner', self.iou_ref) + self.iou_vae = metric_iou(vae_o_, gt_vox) + tf.summary.scalar('iou_vae', self.iou_vae) + tf.summary.histogram("iou_vae", self.iou_vae) + + with tf.variable_scope('Optimization'): + base_en_var = [var for var in tf.trainable_variables() if var.name.startswith('Encoder/en')] + base_dec_var = [var for var in tf.trainable_variables() if var.name.startswith('Decoder/de')] + att_var = [var for var in tf.trainable_variables() if var.name.startswith('Att_Net/att')] + refine_var = [var for var in tf.trainable_variables() if var.name.startswith('ref_net/ref')] + # self.refine_optim = tf.train.AdamOptimizer(learning_rate=self.refine_lr).minimize(self.rec_loss, var_list=refine_var) + # self.base_en_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.vae_loss, var_list=base_en_var) + # self.base_de_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.vae_loss, var_list=base_dec_var) + # self.att_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.vae_loss, var_list=att_var) + self.refine_optim = tf.train.AdamOptimizer(learning_rate=self.refine_lr) + self.base_en_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr) + self.base_de_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr) + self.att_optim2 = tf.train.AdamOptimizer(learning_rate=self.lr) + + ''' + + loss_scale_manager_1 = ExponentialUpdateLossScaleManager(init_loss_scale=2 ** 32, incr_every_n_steps=1000, + decr_every_n_nan_or_inf=2, decr_ratio=0.5) + loss_scale_manager_2 = ExponentialUpdateLossScaleManager(init_loss_scale=2 ** 32, incr_every_n_steps=1000, + decr_every_n_nan_or_inf=2, decr_ratio=0.5) + loss_scale_manager_3 = ExponentialUpdateLossScaleManager(init_loss_scale=2 ** 32, incr_every_n_steps=1000, + decr_every_n_nan_or_inf=2, decr_ratio=0.5) + loss_scale_manager_4 = ExponentialUpdateLossScaleManager(init_loss_scale=2 ** 32, incr_every_n_steps=1000, + decr_every_n_nan_or_inf=2, decr_ratio=0.5) + ''' + loss_scale_manager_1 = FixedLossScaleManager(loss_scale=8589935000.0, enable_overflow_check=False) + loss_scale_manager_2 = FixedLossScaleManager(loss_scale=2147483600.0, enable_overflow_check=False) + loss_scale_manager_3 = FixedLossScaleManager(loss_scale=2147483600.0, enable_overflow_check=False) + loss_scale_manager_4 = FixedLossScaleManager(loss_scale=4294967300.0, enable_overflow_check=False) + + self.refine_optim = NPULossScaleOptimizer(self.refine_optim, loss_scale_manager_1).minimize(self.rec_loss, + var_list=refine_var) + self.base_en_optim2 = NPULossScaleOptimizer(self.base_en_optim2, loss_scale_manager_2).minimize( + self.vae_loss, var_list=base_en_var) + self.base_de_optim2 = NPULossScaleOptimizer(self.base_de_optim2, loss_scale_manager_3).minimize( + self.vae_loss, var_list=base_dec_var) + self.att_optim2 = NPULossScaleOptimizer(self.att_optim2, loss_scale_manager_4).minimize(self.vae_loss, + var_list=att_var) + + print("total weights:", tools.Ops.variable_count()) + self.saver = tf.train.Saver(max_to_keep=1) + + config = tf.ConfigProto(allow_soft_placement=True) + config.gpu_options.allow_growth = True + config.gpu_options.visible_device_list = '0,1' + + self.sess = tf.Session(config=npu_config_proto(config_proto=config)) + #config = npu_config_proto(config_proto=config) + self.merged = tf.summary.merge_all() + self.sum_writer_train = tf.summary.FileWriter(self.train_sum_dir, self.sess.graph) + self.sum_writer_test = tf.summary.FileWriter(self.test_sum_dir, self.sess.graph) + + ####################### + path = self.train_mod_dir + # path = './Model_released/' # retrain the released model + + if os.path.isfile(path + 'model.cptk.data-00000-of-00001'): + print("restoring saved model!") + self.saver.restore(self.sess, path + 'model.cptk') + else: + self.sess.run(tf.global_variables_initializer()) + + return 0 + + def train(self, data): + for epoch in range(0, 50, 1): + train_view_num = 24 ##!!!!!!!!!!! + data.shuffle_train_files(epoch, train_mv=train_view_num) + total_train_batch_num = data.total_train_batch_num # int(len(self.X_rgb_train_files)/(self.batch_size*train_mv)) + print('total_train_batch_num:', total_train_batch_num) + for i in range(total_train_batch_num): + #### training + X_rgb_bat, Y_vox_bat = data.load_X_Y_train_next_batch(train_mv=train_view_num) + print("multi_view_train_X_rgb_bat : ", + X_rgb_bat.shape) # np.asarray(X.append(X_rgb[b*train_mv:(b+1)*train_mv,:])) + + print(time.ctime()) + + ##### option 1: seperate train, seperate optimize + # if epoch<=30: + # single_view_train=True + # multi_view_train=False + # else: + # single_view_train=False + # multi_view_train=True + + ##### optiion 2: joint train, seperate optimize + single_view_train = True + multi_view_train = False + + if epoch <= 5: + att_lr = .0002 + ref_lr = .0002 + if epoch > 5 and epoch <= 40: + att_lr = .0001 + ref_lr = .0001 + if epoch > 40: + att_lr = .00005 + ref_lr = .00005 + ########### single view train + if single_view_train: + rgb = np.reshape(X_rgb_bat, [batch_size * train_view_num, 1, 127, 127, 3]) + print("single_view_train_rgb_input_shape ", rgb.shape) + vox = np.tile(Y_vox_bat[:, None, :, :, :], [1, train_view_num, 1, 1, 1]) + vox = np.reshape(vox, [batch_size * train_view_num, 32, 32, 32]) + # vae_loss_c,eee,ddd, rec_loss_c, sum_train,rrr,mean_vae,iou_ref_,iou_vae_ = self.sess.run([self.vae_loss,self.base_en_optim2,self.base_de_optim2,self.rec_loss,self.merged,self.refine_optim,self.mean_loss,self.iou_ref,self.iou_vae],feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr,self.refine_lr: ref_lr}) + + vae_loss_c = self.sess.run([self.vae_loss], feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr, + self.refine_lr: ref_lr}) + eee = self.sess.run([self.base_en_optim2], + feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr, self.refine_lr: ref_lr}) + ddd = self.sess.run([self.base_de_optim2], + feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr, self.refine_lr: ref_lr}) + rec_loss_c = self.sess.run([self.rec_loss], feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr, + self.refine_lr: ref_lr}) + sum_train = self.sess.run(self.merged, + feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr, self.refine_lr: ref_lr}) + rrr = self.sess.run([self.refine_optim], + feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr, self.refine_lr: ref_lr}) + mean_vae = self.sess.run([self.mean_loss], + feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr, self.refine_lr: ref_lr}) + iou_ref_ = self.sess.run([self.iou_ref], + feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr, self.refine_lr: ref_lr}) + iou_vae_ = self.sess.run([self.iou_vae], feed_dict={self.X_rgb: rgb, self.Y_vox: vox, self.lr: att_lr, + self.refine_lr: ref_lr}) + + print('ep:', epoch, 'i:', i, 'train single rec loss:', rec_loss_c) + print('ep:', epoch, 'i:', i, 'train single vae loss:', vae_loss_c) + print('ep:', epoch, 'i:', i, 'train single mean_vae loss:', mean_vae) + print('ep:', epoch, 'i:', i, 'train single ref_iou:', iou_ref_) + print('ep:', epoch, 'i:', i, 'train single vae_iou:', iou_vae_) + + ########## multi view train + + if multi_view_train: + vae_loss_c, rec_loss_c, _, sum_train, xxx, mean_vae, iou_ref_, iou_vae_ = self.sess.run( + [self.vae_loss, self.rec_loss, self.att_optim2, self.merged, self.refine_optim, self.mean_loss, + self.iou_ref, self.iou_vae], + feed_dict={self.X_rgb: X_rgb_bat, self.Y_vox: Y_vox_bat, self.lr: att_lr, self.refine_lr: ref_lr}) + print('ep:', epoch, 'i:', i, 'train multi rec loss:', rec_loss_c) + print('ep:', epoch, 'i:', i, 'train multi vae loss:', vae_loss_c) + print('ep:', epoch, 'i', i, 'train multi mean_vae loss:', mean_vae) + print('ep:', epoch, 'i:', i, 'train multi ref_iou:', iou_ref_) + print('ep:', epoch, 'i:', i, 'train multi vae_iou:', iou_vae_) + + ############ + if i % 10 == 0: + self.sum_writer_train.add_summary(sum_train, epoch * total_train_batch_num + i) + + + #### testing + if i % 20 == 0: + X_rgb_batch, Y_vox_batch = data.load_X_Y_test_next_batch(test_mv=3) + + # vae_pred = tf.get_default_graph().get_tensor_by_name("Decoder/de_out:0") + # ref_pred = tf.get_default_graph().get_tensor_by_name("ref_net/ref_Dec/ref_out:0") + # gt_vox=Y_vox_batch.astype(np.float32) + + # iou_pred = metric_iou(ref_pred,gt_vox) + # tf.summary.scalar("iou",iou_pred) + + rrrr, aaaa, rec_loss_te, qwerty, Y_vox_test_pred, att_pred, sum_test, mean_vae, iou_ref_, iou_vae_ = \ + self.sess.run([self.refine_optim, self.att_optim2, self.rec_loss, self.vae_loss, self.Y_pred, self.weights, + self.merged, self.mean_loss, self.iou_ref, self.iou_vae], + feed_dict={self.X_rgb: X_rgb_batch, self.Y_vox: Y_vox_batch, self.lr: 0, self.refine_lr: 0}) + + X_rgb_batch = X_rgb_batch.astype(np.float16) + + Y_vox_batch = Y_vox_batch.astype(np.float16) + Y_vox_test_pred = Y_vox_test_pred.astype(np.float16) + att_pred = att_pred.astype(np.float16) + to_save = {'X_test': X_rgb_batch, 'Y_test_pred': Y_vox_test_pred, 'att_pred': att_pred, + 'Y_test_true': Y_vox_batch} + scipy.io.savemat(self.test_res_dir + 'X_Y_pred_' + str(epoch).zfill(2) + '_' + str(i).zfill(5) + '.mat', + to_save, do_compression=True) + + self.sum_writer_test.add_summary(sum_test, epoch * total_train_batch_num + i) + + # iou_ref=evaluate_voxel_prediction(ref_pred_,gt_vox) + # iou_vae=evaluate_voxel_prediction(vae_pred_,gt_vox) + + # print("Ref_iou:",iou_ref) + # print("Vae_iou:",iou_vae) + + # plot_list_iou.append(iou_ref) + # plot_list_i.append((i/50)) + # graph_plot(plot_list_iou,plot_list_i) + print('ep:', epoch, 'i:', i, 'test rec loss:', rec_loss_te) + print('ep:', epoch, 'i:', i, 'test vae loss:', qwerty) + print('ep:', epoch, 'i:', i, 'test mean_vae loss:', mean_vae) + print('ep:', epoch, 'i:', i, 'test ref_iou:', iou_ref_) + print('ep:', epoch, 'i:', i, 'test vae_iou:', iou_vae_) + + #### model saving + if i % 100 == 0: + self.saver.save(self.sess, save_path=self.train_mod_dir + '/' + str(epoch) + str(i) + '/' + 'model.cptk') + print('epoch:', epoch, 'i:', i, 'model saved!') + + +# plt.show() + + +########## +if __name__ == '__main__': + npu_keras_sess = set_keras_session_npu_config() + + net = Network() # net=object to create instance + + print("network compleated") ### + + net.build_graph() + print("graph compleated") + + data = tools.Data(config) + print("tools.data compleated") + + print('training data') + + net.train(data) + + close_session(npu_keras_sess) -- Gitee From 218fc5953e1b38102262f45cdb8c41fce154302c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:46:25 +0000 Subject: [PATCH 09/16] =?UTF-8?q?=E5=8E=BB=E6=8E=89code=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E5=B1=82=E7=BA=A7=20=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../binvox_rw.py | 284 ++++++++++++++++++ .../demo_AttSets.py | 186 ++++++++++++ .../export_obj.py | 45 +++ 3 files changed, 515 insertions(+) create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/binvox_rw.py create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/demo_AttSets.py create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/export_obj.py diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/binvox_rw.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/binvox_rw.py new file mode 100644 index 000000000..40005d24a --- /dev/null +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/binvox_rw.py @@ -0,0 +1,284 @@ +# Copyright (C) 2012 Daniel Maturana +# This file is part of binvox-rw-py. +# +# binvox-rw-py is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# binvox-rw-py is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# along with binvox-rw-py. If not, see . +# + +""" +Binvox to Numpy and back. +>>> import numpy as np +>>> import binvox_rw +>>> with open('chair.binvox', 'rb') as f: +... m1 = binvox_rw.read_as_3d_array(f) +... +>>> m1.dims +[32, 32, 32] +>>> m1.scale +41.133000000000003 +>>> m1.translate +[0.0, 0.0, 0.0] +>>> with open('chair_out.binvox', 'wb') as f: +... m1.write(f) +... +>>> with open('chair_out.binvox', 'rb') as f: +... m2 = binvox_rw.read_as_3d_array(f) +... +>>> m1.dims==m2.dims +True +>>> m1.scale==m2.scale +True +>>> m1.translate==m2.translate +True +>>> np.all(m1.data==m2.data) +True +>>> with open('chair.binvox', 'rb') as f: +... md = binvox_rw.read_as_3d_array(f) +... +>>> with open('chair.binvox', 'rb') as f: +... ms = binvox_rw.read_as_coord_array(f) +... +>>> data_ds = binvox_rw.dense_to_sparse(md.data) +>>> data_sd = binvox_rw.sparse_to_dense(ms.data, 32) +>>> np.all(data_sd==md.data) +True +>>> # the ordering of elements returned by numpy.nonzero changes with axis +>>> # ordering, so to compare for equality we first lexically sort the voxels. +>>> np.all(ms.data[:, np.lexsort(ms.data)] == data_ds[:, np.lexsort(data_ds)]) +True +""" +from npu_bridge.npu_init import * + +import numpy as np + +class Voxels(object): + """ Holds a binvox model. + data is either a three-dimensional numpy boolean array (dense representation) + or a two-dimensional numpy float array (coordinate representation). + dims, translate and scale are the model metadata. + dims are the voxel dimensions, e.g. [32, 32, 32] for a 32x32x32 model. + scale and translate relate the voxels to the original model coordinates. + To translate voxel coordinates i, j, k to original coordinates x, y, z: + x_n = (i+.5)/dims[0] + y_n = (j+.5)/dims[1] + z_n = (k+.5)/dims[2] + x = scale*x_n + translate[0] + y = scale*y_n + translate[1] + z = scale*z_n + translate[2] + """ + + def __init__(self, data, dims, translate, scale, axis_order): + self.data = data + self.dims = dims + self.translate = translate + self.scale = scale + assert (axis_order in ('xzy', 'xyz')) + self.axis_order = axis_order + + def clone(self): + data = self.data.copy() + dims = self.dims[:] + translate = self.translate[:] + return Voxels(data, dims, translate, self.scale, self.axis_order) + + def write(self, fp): + write(self, fp) + +def read_header(fp): + """ Read binvox header. Mostly meant for internal use. + """ + line = fp.readline().strip() + if not line.startswith(b'#binvox'): + raise IOError('Not a binvox file') + dims = list(map(int, fp.readline().strip().split(b' ')[1:])) + translate = list(map(float, fp.readline().strip().split(b' ')[1:])) + scale = list(map(float, fp.readline().strip().split(b' ')[1:]))[0] + line = fp.readline() + return dims, translate, scale + +def read_as_3d_array(fp, fix_coords=True): + """ Read binary binvox format as array. + Returns the model with accompanying metadata. + Voxels are stored in a three-dimensional numpy array, which is simple and + direct, but may use a lot of memory for large models. (Storage requirements + are 8*(d^3) bytes, where d is the dimensions of the binvox model. Numpy + boolean arrays use a byte per element). + Doesn't do any checks on input except for the '#binvox' line. + """ + dims, translate, scale = read_header(fp) + raw_data = np.frombuffer(fp.read(), dtype=np.uint8) + # if just using reshape() on the raw data: + # indexing the array as array[i,j,k], the indices map into the + # coords as: + # i -> x + # j -> z + # k -> y + # if fix_coords is true, then data is rearranged so that + # mapping is + # i -> x + # j -> y + # k -> z + values, counts = raw_data[::2], raw_data[1::2] + data = np.repeat(values, counts).astype(np.bool) + data = data.reshape(dims) + if fix_coords: + # xzy to xyz TODO the right thing + data = np.transpose(data, (0, 2, 1)) + axis_order = 'xyz' + else: + axis_order = 'xzy' + return Voxels(data, dims, translate, scale, axis_order) + +def read_as_coord_array(fp, fix_coords=True): + """ Read binary binvox format as coordinates. + Returns binvox model with voxels in a "coordinate" representation, i.e. an + 3 x N array where N is the number of nonzero voxels. Each column + corresponds to a nonzero voxel and the 3 rows are the (x, z, y) coordinates + of the voxel. (The odd ordering is due to the way binvox format lays out + data). Note that coordinates refer to the binvox voxels, without any + scaling or translation. + Use this to save memory if your model is very sparse (mostly empty). + Doesn't do any checks on input except for the '#binvox' line. + """ + dims, translate, scale = read_header(fp) + raw_data = np.frombuffer(fp.read(), dtype=np.uint8) + + values, counts = raw_data[::2], raw_data[1::2] + + sz = np.prod(dims) + index, end_index = 0, 0 + end_indices = np.cumsum(counts) + indices = np.concatenate(([0], end_indices[:-1])).astype(end_indices.dtype) + + values = values.astype(np.bool) + indices = indices[values] + end_indices = end_indices[values] + + nz_voxels = [] + for index, end_index in zip(indices, end_indices): + nz_voxels.extend(range(index, end_index)) + nz_voxels = np.array(nz_voxels) + # TODO are these dims correct? + # according to docs, + # index = x * wxh + z * width + y; // wxh = width * height = d * d + + x = nz_voxels / (dims[0]*dims[1]) + zwpy = nz_voxels % (dims[0]*dims[1]) # z*w + y + z = zwpy / dims[0] + y = zwpy % dims[0] + if fix_coords: + data = np.vstack((x, y, z)) + axis_order = 'xyz' + else: + data = np.vstack((x, z, y)) + axis_order = 'xzy' + + #return Voxels(data, dims, translate, scale, axis_order) + return Voxels(np.ascontiguousarray(data), dims, translate, scale, axis_order) + +def dense_to_sparse(voxel_data, dtype=np.int): + """ From dense representation to sparse (coordinate) representation. + No coordinate reordering. + """ + if voxel_data.ndim!=3: + raise ValueError('voxel_data is wrong shape; should be 3D array.') + return np.asarray(np.nonzero(voxel_data), dtype) + +def sparse_to_dense(voxel_data, dims, dtype=np.bool): + if voxel_data.ndim!=2 or voxel_data.shape[0]!=3: + raise ValueError('voxel_data is wrong shape; should be 3xN array.') + if np.isscalar(dims): + dims = [dims]*3 + dims = np.atleast_2d(dims).T + # truncate to integers + xyz = voxel_data.astype(np.int) + # discard voxels that fall outside dims + valid_ix = ~np.any((xyz < 0) | (xyz >= dims), 0) + xyz = xyz[:,valid_ix] + out = np.zeros(dims.flatten(), dtype=dtype) + out[tuple(xyz)] = True + return out + +#def get_linear_index(x, y, z, dims): + #""" Assuming xzy order. (y increasing fastest. + #TODO ensure this is right when dims are not all same + #""" + #return x*(dims[1]*dims[2]) + z*dims[1] + y + +def write(voxel_model, fp): + """ Write binary binvox format. + Note that when saving a model in sparse (coordinate) format, it is first + converted to dense format. + Doesn't check if the model is 'sane'. + """ + if voxel_model.data.ndim==2: + # TODO avoid conversion to dense + dense_voxel_data = sparse_to_dense(voxel_model.data, voxel_model.dims) + else: + dense_voxel_data = voxel_model.data + + fp.write('#binvox 1\n') + fp.write('dim '+' '.join(map(str, voxel_model.dims))+'\n') + fp.write('translate '+' '.join(map(str, voxel_model.translate))+'\n') + fp.write('scale '+str(voxel_model.scale)+'\n') + fp.write('data\n') + if not voxel_model.axis_order in ('xzy', 'xyz'): + raise ValueError('Unsupported voxel model axis order') + + if voxel_model.axis_order=='xzy': + voxels_flat = dense_voxel_data.flatten() + elif voxel_model.axis_order=='xyz': + voxels_flat = np.transpose(dense_voxel_data, (0, 2, 1)).flatten() + + # keep a sort of state machine for writing run length encoding + state = voxels_flat[0] + ctr = 0 + for c in voxels_flat: + if c==state: + ctr += 1 + # if ctr hits max, dump + if ctr==255: + fp.write(chr(state)) + fp.write(chr(ctr)) + ctr = 0 + else: + # if switch state, dump + fp.write(chr(state)) + fp.write(chr(ctr)) + state = c + ctr = 1 + # flush out remainders + if ctr > 0: + fp.write(chr(state)) + fp.write(chr(ctr)) + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/demo_AttSets.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/demo_AttSets.py new file mode 100644 index 000000000..aad6f0277 --- /dev/null +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/demo_AttSets.py @@ -0,0 +1,186 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from npu_bridge.npu_init import * +import tensorflow as tf +import os +import sys +sys.path.append('..') +import tools as tools +import numpy as np +import matplotlib.pyplot as plt + +GPU='0' + + +def load_real_rgbs(test_mv=5): + obj_rgbs_folder ='/home/ma-user/work/3DOR/Data/amazon_real_rgbs/airfilter/' + rgbs = [] + rgbs_views = sorted(os.listdir(obj_rgbs_folder)) + for v in rgbs_views: + if not v.endswith('png'): continue + + rgbs.append(tools.Data.load_single_X_rgb_r2n2(obj_rgbs_folder + v, train=False)) + + rgbs = np.asarray(rgbs) + x_sample = rgbs[0:test_mv, :, :, :].reshape(1, test_mv, 127, 127, 3) + return x_sample, None + +def load_shapenet_rgbs(test_mv=8): + obj_rgbs_folder = './Data_sample/ShapeNetRendering/03001627/1a6f615e8b1b5ae4dbbc9440457e303e/rendering/' + obj_gt_vox_path ='./Data_sample/ShapeNetVox32/03001627/1a6f615e8b1b5ae4dbbc9440457e303e/model.binvox' + rgbs=[] + rgbs_views = sorted(os.listdir(obj_rgbs_folder)) + for v in rgbs_views: + if not v.endswith('png'): continue + rgbs.append(tools.Data.load_single_X_rgb_r2n2(obj_rgbs_folder + v, train=False)) + rgbs = np.asarray(rgbs) + x_sample = rgbs[0:test_mv, :, :, :].reshape(1, test_mv, 127, 127, 3) + y_true = tools.Data.load_single_Y_vox(obj_gt_vox_path) + return x_sample, y_true + +def ttest_demo(): +# model_path = './Model_released/' + model_path='/home/ma-user/work/3DOR/Model/train_mod/' + if not os.path.isfile(model_path + 'model.cptk.data-00000-of-00001'): + print ('please download our released model first!') + return + + config = tf.ConfigProto(allow_soft_placement=True) + config.gpu_options.visible_device_list = GPU + + + + with tf.Session(config=npu_config_proto(config_proto=config)) as sess: + saver = tf.train.import_meta_graph(model_path + 'model.cptk.meta', clear_devices=True) + saver.restore(sess, model_path + 'model.cptk') + print ('model restored!') + + # graph = tf.get_default_graph() + # print(graph.get_operations()) + + X = tf.get_default_graph().get_tensor_by_name("Placeholder:0") + Y_pred = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_9:0") + + plot_data_8 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_8:0") + plot_data_7 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_7:0") #############(1,1024) + plot_data_6 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_6:0") #############(1,1024) + plot_data_5 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_5:0") + plot_data_4 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_4:0") + plot_data_3 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_3:0") + plot_data_2 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_2:0") + plot_data_1 = tf.get_default_graph().get_tensor_by_name("r2n/Reshape_1:0") + + +# print("X: ", X.shape) #Tensor("Placeholder:0", shape=(?, ?, 127, 127, 3), dtype=float32) +# print(Y_pred) #Tensor("r2n/Reshape_9:0", shape=(?, 32, 32, 32), dtype=float32) + +# print("x_sample: ", x_sample.shape) +# print("x_sample_data: ", type(x_sample[:,:,:,:,1])) +# print(y_pred.shape) ###############################(1, 32, 32, 32) ############################## + + +# x_sample, gt_vox = load_shapenet_rgbs() + x_sample, gt_vox = load_real_rgbs() + + + plot_buf_1= tf.reshape(plot_data_1, [-1, 32, 32, 1]) + plot_buf_2= tf.reshape(plot_data_2, [-1, 32, 32, 1]) + plot_buf_3= tf.reshape(plot_data_3, [-1, 32, 32, 1]) + plot_buf_4= tf.reshape(plot_data_4, [-1, 32, 32, 1]) + plot_buf_5= tf.reshape(plot_data_5, [-1, 32, 32, 1]) + plot_buf_6= tf.reshape(plot_data_6, [-1, 32, 32, 1]) + plot_buf_7= tf.reshape(plot_data_7, [-1, 32, 32, 1]) + plot_buf_8= tf.reshape(plot_data_8, [-1, 32, 32, 1]) + + +# tf.summary.image("RESHAPE_1", plot_buf_1) +# tf.summary.image("RESHAPE_2", plot_buf_2) +# tf.summary.image("RESHAPE_3", plot_buf_3) +# tf.summary.image("RESHAPE_4", plot_buf_4) +# tf.summary.image("RESHAPE_5", plot_buf_5) +# tf.summary.image("RESHAPE_6", plot_buf_6) +# tf.summary.image("RESHAPE_7", plot_buf_7) +# tf.summary.image("RESHAPE_8", plot_buf_8) + + + summary_8 = tf.summary.image("RESHAPE_8", plot_buf_8) + summary_7 = tf.summary.image("RESHAPE_7", plot_buf_7) + summary_6 = tf.summary.image("RESHAPE_6", plot_buf_6) + summary_5 = tf.summary.image("RESHAPE_5", plot_buf_5) + summary_4 = tf.summary.image("RESHAPE_4", plot_buf_4) + summary_3 = tf.summary.image("RESHAPE_3", plot_buf_3) + summary_2 = tf.summary.image("RESHAPE_2", plot_buf_2) + summary_1 = tf.summary.image("RESHAPE_1", plot_buf_1) + + +# summary_op = tf.summary.image("RESHAPE_4", plot_buf_4) +# with tf.Session() as sess: +# y_pred,1_summary,2_summary = sess.run([Y_pred,summary_op_1,summary_op_2], feed_dict={X: x_sample}) + + y_pred,summary_pred_1,summary_pred_2,summary_pred_3,summary_pred_4,summary_pred_5,summary_pred_6,summary_pred_7,summary_pred_8 = sess.run([Y_pred,summary_1,summary_2,summary_3,summary_4,summary_5,summary_6,summary_7,summary_8], feed_dict={X: x_sample}) + +# Write summary tf.summary.FileWriter + writer = tf.summary.FileWriter('./logs') + + + writer.add_summary(summary_pred_1) + writer.add_summary(summary_pred_2) + writer.add_summary(summary_pred_3) + writer.add_summary(summary_pred_4) + writer.add_summary(summary_pred_5) + writer.add_summary(summary_pred_6) + writer.add_summary(summary_pred_7) + writer.add_summary(summary_pred_8) + + writer.close() + +# sys.exit(). sys.exit() + ###### to visualize + th = 0.25 + y_pred[y_pred>=th]=1 + y_pred[y_pred Date: Wed, 6 Apr 2022 08:48:18 +0000 Subject: [PATCH 10/16] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/Pix2Vox=5FID1284=5Ffor=5FTensorFlow/LICENSE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cv/Pix2Vox_ID1284_for_TensorFlow/LICENSE | 308 ------------------ 1 file changed, 308 deletions(-) delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/LICENSE diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/LICENSE b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/LICENSE deleted file mode 100644 index 6478627e0..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/LICENSE +++ /dev/null @@ -1,308 +0,0 @@ -MIT License - -Copyright (c) 2020 Ajith Balakrishnan - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - - -#============================================================================ -Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - ------------------- -Files: third_party/compute_library/... - -MIT License - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------------- -Files: ACKNOWLEDGEMENTS -LICENSE - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ------------------- -Files: third_party/hexagon - -Copyright (c) 2016-2019, The Linux Foundation. All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted (subject to the limitations in the -disclaimer below) provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - - * Neither the name of The Linux Foundation nor the names of its - contributors may be used to endorse or promote products derived - from this software without specific prior written permission. - -NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE -GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT -HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED -WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE -GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER -IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN -IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file -- Gitee From fc2003bcd3cd290f0e2c3663bceb445d50713bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:48:23 +0000 Subject: [PATCH 11/16] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/Pix2Vox=5FID1284=5Ffor=5FTensorFlow/README.?= =?UTF-8?q?md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Pix2Vox_ID1284_for_TensorFlow/README.md | 84 ------------------- 1 file changed, 84 deletions(-) delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md deleted file mode 100644 index 11c24c143..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md +++ /dev/null @@ -1,84 +0,0 @@ -# 3D_Object_Reconstruction - -#### 基本信息 -发布者(Publisher):Huawei - -应用领域(Application Domain): 3D Object Reconstruction - -版本(Version):1.2 - -修改时间(Modified) :2021.11.06 - -框架(Framework):TensorFlow 1.15.0 - -模型格式(Model Format):ckpt - -精度(Precision):Mixed - -处理器(Processor):昇腾910 - -应用级别(Categories):Official - -描述(Description):基于TensorFlow框架的物体三维重建网络训练代码 - - - -#### 概述 -A Novel Hybrid Ensemble Approach For 3D Object Reconstruction from Multi-View Monocular RGB images for Robotic Simulations. - -Code comes from: https://github.com/Ajithbalakrishnan/3D-Object-Reconstruction-from-Multi-View-Monocular-RGB-images - -参考论文:https://arxiv.org/pdf/1901.11153.pdf - -参考实现: - -适配昇腾 AI 处理器的实现:https://toscode.gitee.com/ascend/modelzoo/pulls/5278 - -#### 算法架构 -![Image text](https://gitee.com/zhangwx21/Object_Reconstruction/blob/master/structure_updated.png) - - -#### 默认配置 - -数据集 - -ShapeNet rendered images http://cvgl.stanford.edu/data2/ShapeNetRendering.tgz - -ShapeNet voxelized models http://cvgl.stanford.edu/data2/ShapeNetVox32.tg - -运行环境 - -python 3.5 - -tensorflow 1.13.0 - -numpy 1.13.3 - -scipy 0.19.0 - -matplotlib - -skimage - -PyMCubes - -超参数设置 - -batchsize:2 - -Learning rate(LR): 0.001/0.0005/0.0001 - -Train epoch: 50 - -#### 训练模型 - -The code runs on HUAWEI's Modelarts. The main program is ./Code/modelarts_entry.py - - -#### 其他说明 - -1. Unzip the data set to ./Data/ShapeNetRendering and ./Data/ShapeNetVox32 - -2. The training log on Modelarts has been saved in the log folder. - -3. Due to the limitation of running time, the program only trained on the airplane data set(02691156). -- Gitee From d5b77145439f4e0d1414bb62c1f0a5ecd576cef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:48:30 +0000 Subject: [PATCH 12/16] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/Pix2Vox=5FID1284=5Ffor=5FTensorFlow/modelzo?= =?UTF-8?q?o=5Flevel.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cv/Pix2Vox_ID1284_for_TensorFlow/modelzoo_level.txt | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelzoo_level.txt diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelzoo_level.txt b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelzoo_level.txt deleted file mode 100644 index 7b4ecc749..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelzoo_level.txt +++ /dev/null @@ -1,6 +0,0 @@ -GPUStatus:OK -NPUMigrationStatus:OK -FuncStatus:OK -PrecisionStatus:POK -AutoTune:NOK -PerfStatus:NOK \ No newline at end of file -- Gitee From f5a3e1b64b61780f76cf6465ea2f2704ce490066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:48:37 +0000 Subject: [PATCH 13/16] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/Pix2Vox=5FID1284=5Ffor=5FTensorFlow/require?= =?UTF-8?q?ments.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cv/Pix2Vox_ID1284_for_TensorFlow/requirements.txt | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/requirements.txt diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/requirements.txt b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/requirements.txt deleted file mode 100644 index 75fd48ace..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/requirements.txt +++ /dev/null @@ -1,11 +0,0 @@ -tensorflow==1.13.0 - -numpy==1.13.3 - -scipy==0.19.0 - -matplotlib - -skimage - -PyMCubes \ No newline at end of file -- Gitee From 0b3d5875a339112be8224348827ed2e03dc89349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:49:27 +0000 Subject: [PATCH 14/16] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=AE=AD=E7=BB=83?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cv/Pix2Vox_ID1284_for_TensorFlow/LICENSE | 308 +++++++++++ .../Pix2Vox_ID1284_for_TensorFlow/README.md | 178 +++++++ .../modelarts_entry_acc.py | 64 +++ .../modelzoo_level.txt | 6 + .../requirements.txt | 11 + .../cv/Pix2Vox_ID1284_for_TensorFlow/tools.py | 482 ++++++++++++++++++ .../cv/Pix2Vox_ID1284_for_TensorFlow/voxel.py | 89 ++++ 7 files changed, 1138 insertions(+) create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/LICENSE create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelarts_entry_acc.py create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelzoo_level.txt create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/requirements.txt create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/tools.py create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/voxel.py diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/LICENSE b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/LICENSE new file mode 100644 index 000000000..6478627e0 --- /dev/null +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/LICENSE @@ -0,0 +1,308 @@ +MIT License + +Copyright (c) 2020 Ajith Balakrishnan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + +#============================================================================ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +------------------ +Files: third_party/compute_library/... + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +------------------ +Files: ACKNOWLEDGEMENTS +LICENSE + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +------------------ +Files: third_party/hexagon + +Copyright (c) 2016-2019, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted (subject to the limitations in the +disclaimer below) provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE +GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT +HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md new file mode 100644 index 000000000..e6025a057 --- /dev/null +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md @@ -0,0 +1,178 @@ +- [基本信息](#基本信息.md) +- [概述](#概述.md) +- [训练环境准备](#训练环境准备.md) +- [快速上手](#快速上手.md) +- [训练结果](#训练结果.md) +- [高级参考](#高级参考.md) +

基本信息

+ +**发布者(Publisher):Huawei** + +**应用领域(Application Domain):3D Object Reconstruction** + +**版本(Version):1.2** + +**修改时间(Modified) :2022.2.11** + +**大小(Size):110KB** + +**框架(Framework):TensorFlow 1.15.0** + +**模型格式(Model Format):ckpt** + +**精度(Precision):Mixed** + +**处理器(Processor):昇腾910** + +**应用级别(Categories):Official** + +**描述(Description):基于TensorFlow框架的物体三维重建网络训练代码** + +

概述

+ + A Novel Hybrid Ensemble Approach For 3D Object Reconstruction from Multi-View Monocular RGB images for Robotic Simulations. + +- 参考论文: + + https://arxiv.org/pdf/1901.11153.pdf + +- 参考实现: + + https://github.com/Ajithbalakrishnan/3D-Object-Reconstruction-from-Multi-View-Monocular-RGB-images + +- 适配昇腾 AI 处理器的实现: + + + https://toscode.gitee.com/ascend/modelzoo/pulls/5278 + + +## 默认配置 + +- 训练数据集预处理(以ShapeNet训练集为例,仅作为用户参考示例): + + - 图像的输入尺寸为137*137 + - 图像输入格式:png + +- 测试数据集预处理(以ShapeNet验证集为例,仅作为用户参考示例) + + - 图像的输入尺寸为137*137 + - 图像输入格式:png + +- 数据集获取: + + http://cvgl.stanford.edu/data2/ShapeNetRendering.tgz + http://cvgl.stanford.edu/data2/ShapeNetVox32.tgz + +- 训练超参 + + - Batch size: 2 + - Train epoch: 50 + - Train step: 80900 + + +## 支持特性 + +| 特性列表 | 是否支持 | +|-------|------| +| 分布式训练 | 否 | +| 混合精度 | 是 | +| 并行数据 | 是 | + +## 混合精度训练 + +昇腾910 AI处理器提供自动混合精度功能,可以针对全网中float32数据类型的算子,按照内置的优化策略,自动将部分float32的算子降低精度到float16,从而在精度损失很小的情况下提升系统性能并减少内存使用。 + +## 开启混合精度 + +脚本已默认开启混合精度。 + +

训练环境准备

+ +1. 硬件环境准备请参见各硬件产品文档"[驱动和固件安装升级指南]( https://support.huawei.com/enterprise/zh/category/ai-computing-platform-pid-1557196528909)"。需要在硬件设备上安装与CANN版本配套的固件与驱动。 +2. 代码使用的华为镜像为ascend-share/5.0.4.alpha005_tensorflow-ascend910-cp37-euleros2.8-aarch64-training:1.15.0-21.0.2_1224。 + + + +

快速上手

+ +- 数据集准备 +1. 模型训练使用ShapeNet数据集,数据集请自行获取。 + +## 模型训练 + +- 单击“立即下载”,并选择合适的下载方式下载源码包。 + +- 启动训练之前,首先要配置程序运行相关环境变量。 + + 环境变量配置信息参见: + + [Ascend 910训练平台环境变量设置](https://gitee.com/ascend/modelzoo/wikis/Ascend%20910%E8%AE%AD%E7%BB%83%E5%B9%B3%E5%8F%B0%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E8%AE%BE%E7%BD%AE?sort_id=3148819) + +- 单卡训练 + + 1. 配置训练参数。 + + 首先在脚本main_AttSets.py中,配置batch_size、epochs、total_mv等参数,请用户根据实际路径配置data_path,或者在启动训练的命令行中以参数形式下发。 + + ``` + batch_size=2 + total_mv=24 + epochs=50 + ``` + + 2. 启动训练。 + + 启动单卡训练 (脚本为Pix2Vox_ID1284_for_TensorFlow/test/train_full_1p.sh) + + ``` + bash train_full_1p.sh + ``` + +

训练结果

+ +- 精度结果比对 + +|精度指标项|论文发布|GPU实测|NPU实测| +|---|---|---|---| +|ACC|NA|0.8184|0.8289| + +- 性能结果比对 + +|性能指标项|论文发布|GPU实测|NPU实测| +|---|---|---|---| +|FPS|NA|34.12|14.15| + + +

高级参考

+ +## 脚本和示例代码 + +``` +├── main_AttSets.py //网络训练代码 +├── demo_AttSets.py //网络测试代码 +├── binvox_rw.py //体素模型预处理代码 +├── export_obj.py //模型加载代码 +├── tools.py //自定义工具包 +├── voxel.py //体素模型预处理代码 +├── npu_train.sh //npu训练脚本 +├── README.md //代码说明文档 +├── requirements.tx //训练python依赖列表 +├── test +│ ├──train_full_1p.sh //单卡全量训练启动脚本 +``` + +## 脚本参数 + +``` +--data_path 数据集路径 +--batch_size 每个NPU的batch size,默认:2 +--att_lr、ref_lr 学习率,默认:0.0002/0.0001/0.00005 +--epochs 训练epcoh数量,默认:50 +``` + +## 训练过程 + +1. 通过“模型训练”中的训练指令启动单卡卡训练。 + +2. 参考脚本的模型存储路径为./Model/train_mod。 + diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelarts_entry_acc.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelarts_entry_acc.py new file mode 100644 index 000000000..ae7b7985a --- /dev/null +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelarts_entry_acc.py @@ -0,0 +1,64 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +import os +import argparse +import sys + +# 解析输入参数data_url +parser = argparse.ArgumentParser() +parser.add_argument("--data_url", type=str, default="/home/ma-user/modelarts/inputs/data_url_0") +parser.add_argument("--train_url", type=str, default="/home/ma-user/modelarts/outputs/train_url_0/") +config = parser.parse_args() + +print("[CANN-Modelzoo] code_dir path is [%s]" % (sys.path[0])) +code_dir = sys.path[0] +os.chdir(code_dir) +print("[CANN-Modelzoo] work_dir path is [%s]" % (os.getcwd())) + +print("[CANN-Modelzoo] before train - list my run files:") +os.system("ls -al /usr/local/Ascend/ascend-toolkit/") + +print("[CANN-Modelzoo] before train - list my dataset files:") +os.system("ls -al %s" % config.data_url) + +print("[CANN-Modelzoo] start run train shell") +# 设置sh文件格式为linux可执行 +os.system("dos2unix ./test/*") + +# 执行train_full_1p.sh或者train_performance_1p.sh,需要用户自己指定 +# full和performance的差异,performance只需要执行很少的step,控制在15分钟以内,主要关注性能FPS +os.system("bash ./test/train_full_1p.sh --data_path=%s --output_path=%s " % (config.data_url, config.train_url)) + +print("[CANN-Modelzoo] finish run train shell") + +# 将当前执行目录所有文件拷贝到obs的output进行备份 +print("[CANN-Modelzoo] after train - list my output files:") +os.system("cp -r %s %s " % (code_dir, config.train_url)) +os.system("ls -al %s" % config.train_url) diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelzoo_level.txt b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelzoo_level.txt new file mode 100644 index 000000000..cef7e6440 --- /dev/null +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelzoo_level.txt @@ -0,0 +1,6 @@ +GPUStatus:OK +NPUMigrationStatus:OK +FuncStatus:OK +PrecisionStatus:OK +AutoTune:NOK +PerfStatus:NOK \ No newline at end of file diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/requirements.txt b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/requirements.txt new file mode 100644 index 000000000..8b716f006 --- /dev/null +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/requirements.txt @@ -0,0 +1,11 @@ +tensorflow==1.13.0 + +numpy==1.13.3 + +scipy==0.19.0 + +matplotlib + +skimage + +PyMCubes \ No newline at end of file diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/tools.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/tools.py new file mode 100644 index 000000000..6054ceb1e --- /dev/null +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/tools.py @@ -0,0 +1,482 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from npu_bridge.npu_init import * +import tensorflow as tf +from PIL import Image +import numpy as np +import os +import sys +import random +import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec +from mpl_toolkits import mplot3d +import re +import binvox_rw as binvox_rw +################################################################# +from voxel import voxel2obj +#from PyMCubes import mcubes +from export_obj import export_obj +################################################################# +class Data: + def __init__(self, config): + self.config = config + self.batch_size = config['batch_size'] + self.train_batch_index = 0 + self.test_batch_index_sq = 0 + self.cat_names = config['cat_names'] + self.total_mv = config['total_mv'] + self.cat_test_1st_index = None + + self.X_rgb_train_files_ori, self.Y_vox_train_files_ori, self.X_rgb_test_files_ori, self.Y_vox_test_files_ori \ + =self.load_X_Y_files_paths_all(self.cat_names) + + print ('X_rgb_train_files_ori:', len(self.X_rgb_train_files_ori)) + print ('X_rgb_test_files_ori:', len(self.X_rgb_test_files_ori)) + + @staticmethod + def crop_center(im, new_height, new_width): +# print('crop_center') + height = im.shape[0] # Get dimensions + width = im.shape[1] + left = (width - new_width) // 2 + top = (height - new_height) // 2 + right = (width + new_width) // 2 + bottom = (height + new_height) // 2 + return im[top:bottom, left:right] + + @staticmethod + def add_random_color_background(im, color_range): +# print('add_random_color_background') + r, g, b = [np.random.randint(color_range[i][0], color_range[i][1] + 1) for i in range(3)] +# print (r) +# print (g) +# print (b) + if isinstance(im, Image.Image): + im = np.array(im) +# print (im.shape) +# b = np.array([3]) ### +# np.concatenate((im, '3')) ## +# im=np.append(im,3) ### +# print (im.shape) ### + if im.shape[2] > 3: + + # If the image has the alpha channel, add the background + alpha = (np.expand_dims(im[:, :, 3], axis=2) == 0).astype(np.float) + + im = im[:, :, :3] + bg_color = np.array([[[r, g, b]]]) + im = alpha * bg_color + (1 - alpha) * im + return im + + @staticmethod + def image_transform(img, crop_x, crop_y, crop_loc=None, color_tint=None): +# print('image_transform') + RANDOM_CROP = True + # Slight translation + if RANDOM_CROP and not crop_loc: + crop_loc = [np.random.randint(0, crop_y), np.random.randint(0, crop_x)] + + if crop_loc: + cr, cc = crop_loc + height, width, _ = img.shape + img_h = height - crop_y + img_w = width - crop_x + img = img[cr:cr + img_h, cc:cc + img_w] + # depth = depth[cr:cr+img_h, cc:cc+img_w] + + FLIP = True + if FLIP and np.random.rand() > 0.5: + img = img[:, ::-1, ...] + return img + + @staticmethod + def preprocess_img(im, train): +# print('processing_img') + # add random background + TRAIN_NO_BG_COLOR_RANGE = [[225, 255], [225, 255], [225, 255]] + TEST_NO_BG_COLOR_RANGE = [[240, 240], [240, 240], [240, 240]] + im = Data.add_random_color_background(im, TRAIN_NO_BG_COLOR_RANGE if train else TEST_NO_BG_COLOR_RANGE) + + # If the image has alpha channel, remove it. + CONST_IMG_W = 127 + CONST_IMG_H = 127 + im_rgb = np.array(im)[:, :, :3].astype(np.float32) + if train: + # Data augmentation + PAD_X = 10 + PAD_Y = 10 + t_im = Data.image_transform(im_rgb, PAD_X, PAD_Y) + else: + t_im = Data.crop_center(im_rgb, CONST_IMG_H, CONST_IMG_W) + + # Scale image + t_im = t_im / 255. + return t_im + + @staticmethod + def load_single_X_rgb_r2n2(img_path, train): +# print('load_single_X_rgb_r2n2') + im = Image.open(img_path) + + t_im = Data.preprocess_img(im, train=train) + # plt.figure() + # plt.imshow(t_im) + return t_im + + @staticmethod + def load_single_Y_vox(vox_path): +# print('load_single_Y_vox') + with open(vox_path, 'rb') as ff: + vox = binvox_rw.read_as_3d_array(ff) + vox_grid = vox.data.astype(int) + + #Data.plotFromVoxels(vox_grid) + return vox_grid + + @staticmethod + def load_X_Y_files_paths(X_cat_folder, Y_cat_folder): +# print('load_X_Y_files_paths') + X_obj_folders=[X_f for X_f in sorted(os.listdir(X_cat_folder))] + Y_obj_folders=[Y_f for Y_f in sorted(os.listdir(Y_cat_folder))] + if len(X_obj_folders) != len(Y_obj_folders): + print ('Files are inconsistent in:', X_cat_folder, 'and', Y_cat_folder) + + #### split train/test, according to 3D-R2N2 paper + train_num = int(0.8*len(X_obj_folders)) + idx = list(range(train_num)) + + test_objs = [] + X_train_obj_folders =[]; Y_train_obj_folders=[] + X_test_obj_folders =[]; Y_test_obj_folders=[] + for i in range(len(X_obj_folders)): + obj_na = X_obj_folders[i] + if obj_na not in Y_obj_folders: + print ('inconsistent single obj ignored') + continue + if i in idx: + X_train_obj_folders.append(X_cat_folder+obj_na+'/') + Y_train_obj_folders.append(Y_cat_folder+obj_na+'/') + else: + X_test_obj_folders.append(X_cat_folder+obj_na+'/') + Y_test_obj_folders.append(Y_cat_folder+obj_na+'/') + test_objs.append(obj_na) + print ('train objs:', len(X_train_obj_folders)) + print ('test objs:', len(X_test_obj_folders)) + + ######### + def load_x_y_files(X_obj_fo, Y_obj_fo): +# print('load_x_y_files') + X_files_paths = [] + Y_files_paths = [] + for j in range(len(X_obj_fo)): + if X_obj_fo[j][-5:] not in Y_obj_fo[j]: + print ('inconsistent single obj exit') + exit() + for xf in sorted(os.listdir(X_obj_fo[j]+'rendering/')): + if '.png' in xf: + X_files_paths.append(X_obj_fo[j]+'rendering/'+xf) + Y_files_paths.append(Y_obj_fo[j]+'model.binvox') + return X_files_paths, Y_files_paths + ######### + + X_train_files, Y_train_files = load_x_y_files(X_train_obj_folders, Y_train_obj_folders) + X_test_files, Y_test_files = load_x_y_files(X_test_obj_folders, Y_test_obj_folders) + + return X_train_files, Y_train_files,X_test_files,Y_test_files + + @staticmethod + def load_X_Y_rgb_vox(X_files_full_path, Y_files_full_path, train): +# print('load_X_Y_rgb_vox') + if len(X_files_full_path) != len(Y_files_full_path): + print ('load_X_Y_rgb_vox error!') + exit() + X_rgb = [] + Y_vox = [] + for X_f, Y_f in zip(X_files_full_path, Y_files_full_path): + na = re.split('/', X_f)[-3] + if na not in Y_f: + print ('X Y rgb vox file not consistent!') + exit() + rgb = Data.load_single_X_rgb_r2n2(X_f, train=train) + X_rgb.append(rgb) + + vox = Data.load_single_Y_vox(Y_f) + Y_vox.append(vox) + + X_rgb = np.asarray(X_rgb) + Y_vox = np.asarray(Y_vox) + return X_rgb, Y_vox + + def test_listdir(self, path): + print('当前执行路径', os.getcwd()) + for file in os.listdir(path): + file_path = os.path.join(path, file) + print(file_path) + + def load_X_Y_files_paths_all(self,cat_names): +# print('load_X_Y_files_paths_all') + x_rgb_str='X_rgb_' + y_vox_str='Y_vox_' + + X_train_files_paths_all=[] + Y_trian_files_paths_all=[] + X_test_files_paths_all=[] + Y_test_files_paths_all=[] + self.cat_test_1st_index = [0] + for name in cat_names: + self.test_listdir(os.getcwd()) + print ('loading files:', name) + X_rgb_folder = self.config[x_rgb_str+name] + Y_vox_folder = self.config[y_vox_str+name] + + X_train_files, Y_train_files, X_test_files, Y_test_files = self.load_X_Y_files_paths(X_rgb_folder, Y_vox_folder) + self.cat_test_1st_index.append(len(X_test_files)) + + for X_rgb_f, Y_vox_f in zip(X_train_files, Y_train_files): + X_train_files_paths_all.append(X_rgb_f) + Y_trian_files_paths_all.append(Y_vox_f) + for X_rgb_f, Y_vox_f in zip(X_test_files, Y_test_files): + X_test_files_paths_all.append(X_rgb_f) + Y_test_files_paths_all.append(Y_vox_f) + + return X_train_files_paths_all, Y_trian_files_paths_all,X_test_files_paths_all,Y_test_files_paths_all + + ################################ + def load_X_Y_train_next_batch(self, train_mv): +# print('load_X_Y_train_next_batch') + X_rgb_files = self.X_rgb_train_files[self.batch_size*self.train_batch_index*train_mv:self.batch_size*(self.train_batch_index+1)*train_mv] + Y_vox_files = self.Y_vox_train_files[self.batch_size*self.train_batch_index*train_mv:self.batch_size*(self.train_batch_index+1)*train_mv] + self.train_batch_index +=1 + + X_rgb, Y_vox = self.load_X_Y_rgb_vox(X_rgb_files, Y_vox_files, train=True) + X = [] + Y = [] + for b in range(self.batch_size): + X.append(X_rgb[b * train_mv:(b + 1) * train_mv, :]) + Y.append(Y_vox[b * train_mv, :, :, :]) + X = np.asarray(X) + Y = np.asarray(Y) + return X, Y + + def load_X_Y_test_next_batch(self, test_mv): +# print('load_X_Y_test_next_batch') + num = self.total_mv + idx = random.sample(range(len(self.X_rgb_test_files_ori)//num), self.batch_size) ##############added extra '/' + X_rgb_files = [] + Y_vox_files =[] + + for i in idx: + tp1 = self.X_rgb_test_files_ori[i*num:(i+1)*num] + tp2 = self.Y_vox_test_files_ori[i*num:(i+1)*num] + for x, y in zip(tp1, tp2): + X_rgb_files.append(x) + Y_vox_files.append(y) + X_rgb_batch, Y_vox_batch = self.load_X_Y_rgb_vox(X_rgb_files, Y_vox_files, train=False) + + X =[] + Y =[] + for b in range(self.batch_size): + tp1 = X_rgb_batch[b*num:(b+1)*num,:,:,:] + tp2 = Y_vox_batch[b*num:(b+1)*num,:,:,:] + idx2 = random.sample(range(num), test_mv) + X.append(tp1[idx2]) + Y.append(tp2[idx2[0]]) + X = np.asarray(X) + Y = np.asarray(Y) + return X, Y + + def shuffle_train_files(self, ep, train_mv): +# print('shuffle_train_files') + num = self.total_mv + X_rgb_new=[]; Y_vox_new=[] + self.train_batch_index = 0 + X_rgb = self.X_rgb_train_files_ori + Y_vox = self.Y_vox_train_files_ori + index = list(range(int(len(X_rgb)/num))) + random.Random(ep).shuffle(index) + + for i in index: + tp1 = X_rgb[i*num:(i+1)*num] + tp2 = Y_vox[i*num:(i+1)*num] + + view_ind = list(range(num)) + random.Random(i+100).shuffle(view_ind) + valid_view_num = int(num / train_mv) * train_mv + view_ind = view_ind[0:valid_view_num] + + for j in view_ind: + x = tp1[j] + y = tp2[j] + na = re.split('/', x)[-3] + if na not in y: + print ('X Y rgb vox file not consistent!') + exit() + X_rgb_new.append(x) + Y_vox_new.append(y) + self.X_rgb_train_files = X_rgb_new + self.Y_vox_train_files = Y_vox_new + self.total_train_batch_num = int(len(self.X_rgb_train_files)/(self.batch_size*train_mv)) + + def shuffle_test_files(self, test_mv, seed): +# print('shuffle_test_files') + X_rgb_new=[]; Y_vox_new=[] + X_rgb = self.X_rgb_test_files_ori + Y_vox = self.Y_vox_test_files_ori + + num = self.total_mv + ### + cat_test_1st_index_new =[] + for k in self.cat_test_1st_index: + cat_test_1st_index_new.append( int(k/num)* int(num/test_mv)*test_mv) + self.cat_test_1st_index=cat_test_1st_index_new + ### + total_obj = int(len(X_rgb)/num) + for i in range(total_obj): + view_ind = list(range(num)) + random.Random(i+seed).shuffle(view_ind) + + valid_view_num = int(num/test_mv)*test_mv + view_ind = view_ind[0:valid_view_num] + for id in view_ind: + X_rgb_new.append(X_rgb[i*num+id]) + Y_vox_new.append(Y_vox[i*num+id]) + + self.X_rgb_test_files = X_rgb_new + self.Y_vox_test_files = Y_vox_new + +class Ops: + + @staticmethod + def lrelu(x, leak=0.2): + return tf.nn.leaky_relu(x,alpha=0.2,name=None) + + @staticmethod + def relu(x): + return tf.nn.relu(x) + + @staticmethod + def xxlu(x, label='relu'): + if label == 'relu': + return Ops.relu(x) + if label == 'lrelu': + return Ops.lrelu(x, leak=0.2) + + @staticmethod + def variable_sum(var, name): + with tf.name_scope(name): + mean = tf.reduce_mean(var) + tf.summary.scalar('mean', mean) + stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean))) + tf.summary.scalar('stddev', stddev) + tf.summary.scalar('max', tf.reduce_max(var)) + tf.summary.scalar('min', tf.reduce_min(var)) + tf.summary.histogram('histogram', var) + + @staticmethod + def variable_count(): + total_para = 0 + for variable in tf.trainable_variables(): + shape = variable.get_shape() + variable_para = 1 + for dim in shape: + variable_para *= dim.value + total_para += variable_para + return total_para + + @staticmethod + def fc(x, out_d, name): + xavier_init = tf.contrib.layers.xavier_initializer() + zero_init = tf.zeros_initializer() + in_d = x.get_shape()[1] + w = tf.get_variable(name + '_w', [in_d, out_d], initializer=xavier_init,dtype=tf.float32) + b = tf.get_variable(name + '_b', [out_d], initializer=zero_init,dtype=tf.float32) + y = tf.nn.bias_add(tf.matmul(x, w), b) + Ops.variable_sum(w, name) + return y + + @staticmethod + def conv2d(x, k, out_c, str, name, pad='SAME'): + xavier_init = tf.contrib.layers.xavier_initializer() + zero_init = tf.zeros_initializer() + in_c = x.get_shape()[3] + w = tf.get_variable(name + '_w', [k, k, in_c, out_c], initializer=xavier_init, dtype=tf.float32) + b = tf.get_variable(name + '_b', [out_c], initializer=zero_init,dtype=tf.float32) + + stride = [1, str, str, 1] + y = tf.nn.bias_add(tf.nn.conv2d(x, w, stride, pad), b) + Ops.variable_sum(w, name) + return y + + @staticmethod + def maxpool2d(x, k, s,name, pad='SAME'): + ker = [1,k,k,1] + str = [1,s,s,1] + y = tf.nn.max_pool(x,ksize=ker, strides=str, padding=pad, name=name) + return y + + @staticmethod + def conv3d(x, k, out_c, str, name, pad='SAME'): + xavier_init = tf.contrib.layers.xavier_initializer() + zero_init = tf.zeros_initializer() + in_c = x.get_shape()[4] + w = tf.get_variable(name + '_w', [k, k, k, in_c, out_c], initializer=xavier_init, dtype=tf.float32) + b = tf.get_variable(name + '_b', [out_c], initializer=zero_init, dtype=tf.float32) + stride = [1, str, str, str, 1] + y = tf.nn.bias_add(tf.nn.conv3d(x, w, stride, pad), b) + Ops.variable_sum(w, name) + return y + + @staticmethod + def deconv3d(x, k, out_c, str, name, pad='SAME'): + xavier_init = tf.contrib.layers.xavier_initializer() + zero_init = tf.zeros_initializer() + bat = tf.shape(x)[0] + [_, in_d1, in_d2, in_d3, in_c] = x.get_shape() + in_d1 = int(in_d1); in_d2 = int(in_d2); in_d3 = int(in_d3); in_c = int(in_c) + w = tf.get_variable(name + '_w', [k, k, k, out_c, in_c], initializer=xavier_init, dtype=tf.float32) + b = tf.get_variable(name + '_b', [out_c], initializer=zero_init, dtype=tf.float32) + + out_shape = [bat, in_d1 * str, in_d2 * str, in_d3 * str, out_c] + stride = [1, str, str, str, 1] + y = tf.nn.conv3d_transpose(x, w, output_shape=out_shape, strides=stride, padding=pad) + y = tf.nn.bias_add(y, b) + Ops.variable_sum(w, name) + return y + + @staticmethod + def maxpool3d(x,k,s,name,pad='SAME'): + ker = [1,k,k,k,1] + str = [1,s,s,s,1] + y = tf.nn.max_pool3d(x,ksize=ker,strides=str,padding=pad, name=name) + return y + + + + diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/voxel.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/voxel.py new file mode 100644 index 000000000..8bce75ecf --- /dev/null +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/voxel.py @@ -0,0 +1,89 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from npu_bridge.npu_init import * +import numpy as np + + +def evaluate_voxel_prediction(preds, gt, thresh): + preds_occupy = preds[:, 1, :, :] >= thresh + diff = np.sum(np.logical_xor(preds_occupy, gt[:, 1, :, :])) + intersection = np.sum(np.logical_and(preds_occupy, gt[:, 1, :, :])) + union = np.sum(np.logical_or(preds_occupy, gt[:, 1, :, :])) + num_fp = np.sum(np.logical_and(preds_occupy, gt[:, 0, :, :])) # false positive + num_fn = np.sum(np.logical_and(np.logical_not(preds_occupy), gt[:, 1, :, :])) # false negative + return np.array([diff, intersection, union, num_fp, num_fn]) + + +def voxel2mesh(voxels, surface_view): + cube_verts = [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], + [1, 1, 1]] # 8 points + + cube_faces = [[0, 1, 2], [1, 3, 2], [2, 3, 6], [3, 7, 6], [0, 2, 6], [0, 6, 4], [0, 5, 1], + [0, 4, 5], [6, 7, 5], [6, 5, 4], [1, 7, 3], [1, 5, 7]] # 12 face + + cube_verts = np.array(cube_verts) + cube_faces = np.array(cube_faces) + 1 + + scale = 0.01 + cube_dist_scale = 1.1 + verts = [] + faces = [] + curr_vert = 0 + + positions = np.where(voxels > 0.3) + voxels[positions] = 1 + for i, j, k in zip(*positions): + # identifies if current voxel has an exposed face + if not surface_view or np.sum(voxels[i-1:i+2, j-1:j+2, k-1:k+2]) < 27: + verts.extend(scale * (cube_verts + cube_dist_scale * np.array([[i, j, k]]))) + faces.extend(cube_faces + curr_vert) + curr_vert += len(cube_verts) + + return np.array(verts), np.array(faces) + + +def write_obj(filename, verts, faces): + """ write the verts and faces on file.""" + with open(filename, 'w') as f: + # write vertices + f.write('g\n# %d vertex\n' % len(verts)) + for vert in verts: + f.write('v %f %f %f\n' % tuple(vert)) + + # write faces + f.write('# %d faces\n' % len(faces)) + for face in faces: + f.write('f %d %d %d\n' % tuple(face)) + + +def voxel2obj(filename, pred, surface_view = True): + verts, faces = voxel2mesh(pred, surface_view) + write_obj(filename, verts, faces) + -- Gitee From 840cda80023b6de94d4af1b6c3d8abae59d511db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:50:01 +0000 Subject: [PATCH 15/16] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/Pix2Vox=5FID1284=5Ffor=5FTensorFlow/test/tr?= =?UTF-8?q?ain=5Ffull=5F1p.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/train_full_1p.sh | 27 ------------------- 1 file changed, 27 deletions(-) delete mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_full_1p.sh diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_full_1p.sh deleted file mode 100644 index 8ae2d297a..000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_full_1p.sh +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -# Copyright 2021 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. \ No newline at end of file -- Gitee From 1195397db6c8892ee831fa4a40cdfe0f8a879d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BA=9A=E6=96=87?= <18013846960@189.cn> Date: Wed, 6 Apr 2022 08:50:12 +0000 Subject: [PATCH 16/16] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=AE=AD=E7=BB=83?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/train_full_1p.sh | 181 ++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_full_1p.sh diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_full_1p.sh new file mode 100644 index 000000000..e092e968b --- /dev/null +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/train_full_1p.sh @@ -0,0 +1,181 @@ +#!/bin/bash + +########################################################## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +########################################################## +# shell脚本所在路径 +cur_path=`echo $(cd $(dirname $0);pwd)` + +# 判断当前shell是否是performance +perf_flag=`echo $0 | grep performance | wc -l` + +# 当前执行网络的名称 +Network=`echo $(cd $(dirname $0);pwd) | awk -F"/" '{print $(NF-1)}'` + +export RANK_SIZE=1 +export RANK_ID=0 +export JOB_ID=10087 + +# 路径参数初始化 +data_path="" +output_path="" + +# 帮助信息,不需要修改 +if [[ $1 == --help || $1 == -h ]];then + echo"usage:./train_performance_1P.sh " + echo " " + echo "parameter explain: + --data_path # dataset of training + --output_path # output of training + --train_steps # max_step for training + --train_epochs # max_epoch for training + --batch_size # batch size + -h/--help show help message + " + exit 1 +fi + +# 参数校验,不需要修改 +for para in $* +do + if [[ $para == --data_path* ]];then + data_path=`echo ${para#*=}` + elif [[ $para == --output_path* ]];then + output_path=`echo ${para#*=}` + elif [[ $para == --train_steps* ]];then + train_steps=`echo ${para#*=}` + elif [[ $para == --train_epochs* ]];then + train_epochs=`echo ${para#*=}` + elif [[ $para == --batch_size* ]];then + batch_size=`echo ${para#*=}` + fi +done + +# 校验是否传入data_path,不需要修改 +if [[ $data_path == "" ]];then + echo "[Error] para \"data_path\" must be config" + exit 1 +fi + +# 校验是否传入output_path,不需要修改 +if [[ $output_path == "" ]];then + output_path="./test/output/${ASCEND_DEVICE_ID}" +fi + +# 设置打屏日志文件名,请保留,文件名为${print_log} +print_log="./test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log" +etp_flag=${etp_running_flag} +if [ x"${etp_flag}" != xtrue ]; +then + echo "running without etp..." + print_log_name=`ls /home/ma-user/modelarts/log/ | grep proc-rank` + print_log="/home/ma-user/modelarts/log/${print_log_name}" +fi +echo ${print_log} + +CaseName="" +function get_casename() +{ + if [ x"${perf_flag}" = x1 ]; + then + CaseName=${Network}_bs${batch_size}_${RANK_SIZE}'p'_'perf' + else + CaseName=${Network}_bs${batch_size}_${RANK_SIZE}'p'_'acc' + fi +} + +# 跳转到code目录 +cd ${cur_path}/../ +rm -rf ./test/output/${ASCEND_DEVICE_ID} +mkdir -p ./test/output/${ASCEND_DEVICE_ID} + +# 训练开始时间记录,不需要修改 +start_time=$(date +%s) +########################################################## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +########################################################## + +#========================================================= +#========================================================= +#========训练执行命令,需要根据您的网络进行修改============== +#========================================================= +#========================================================= +# 您的训练数据集在${data_path}路径下,请直接使用这个变量获取 +# 您的训练输出目录在${output_path}路径下,请直接使用这个变量获取 +# 您的其他基础参数,可以自定义增加,但是batch_size请保留,并且设置正确的值 +batch_size=64 + +if [ x"${etp_flag}" != xtrue ]; +then + python3.7 ./main_AttSets.py --data_url=${data_path} --train_url=${output_path} +else + python3.7 ./main_AttSets.py --data_url=${data_path} --train_url=${output_path} > ${print_log} +fi + +# 性能相关数据计算 +# StepTime=`grep "sec/step :" ${print_log} | tail -n 10 | awk '{print $NF}' | awk '{sum+=$1} END {print sum/NR}'` +# FPS=`awk 'BEGIN{printf "%.2f\n", '${batch_size}'/'${StepTime}'}'` + +# 精度相关数据计算 +train_accuracy=`cat ${print_log} | grep "train single ref_iou" | tail -n 200 | awk -F"[" '{print $NF}' | awk -F"]" '{print $1}' | awk '{sum+=$1} END {print sum/NR}'` +# 提取所有loss打印信息 +grep "train single ref_iou: " ${print_log} | awk -F"[" '{print $NF}' | awk -F"]" '{print $1}' > ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt + +########################################################### +#########后面的所有内容请不要修改########################### +#########后面的所有内容请不要修改########################### +#########后面的所有内容请不要修改########################### +########################################################### + +# 判断本次执行是否正确使用Ascend NPU +use_npu_flag=`grep "The model has been compiled on the Ascend AI processor" ${print_log} | wc -l` +if [ x"${use_npu_flag}" == x0 ]; +then + echo "------------------ ERROR NOTICE START ------------------" + echo "ERROR, your task haven't used Ascend NPU, please check your npu Migration." + echo "------------------ ERROR NOTICE END------------------" +else + echo "------------------ INFO NOTICE START------------------" + echo "INFO, your task have used Ascend NPU, please check your result." + echo "------------------ INFO NOTICE END------------------" +fi + +# 获取最终的casename,请保留,case文件名为${CaseName} +get_casename + +# 重命名loss文件 +if [ -f ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt ]; +then + mv ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt ./test/output/${ASCEND_DEVICE_ID}/${CaseName}_loss.txt +fi + +# 训练端到端耗时 +end_time=$(date +%s) +e2e_time=$(( $end_time - $start_time )) + +echo "------------------ Final result ------------------" +# 输出性能FPS/单step耗时/端到端耗时 +echo "Final Performance images/sec : $FPS" +echo "Final Performance sec/step : $StepTime" +echo "E2E Training Duration sec : $e2e_time" + +# 输出训练精度 +echo "Final Train Accuracy : ${train_accuracy}" + +# 最后一个迭代loss值,不需要修改 +ActualLoss=(`awk 'END {print $NF}' $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}_loss.txt`) + +#关键信息打印到${CaseName}.log中,不需要修改 +echo "Network = ${Network}" > $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "RankSize = ${RANK_SIZE}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "BatchSize = ${batch_size}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "DeviceType = `uname -m`" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "CaseName = ${CaseName}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualFPS = ${FPS}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "TrainingTime = ${StepTime}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualLoss = ${ActualLoss}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "E2ETrainingTime = ${e2e_time}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log \ No newline at end of file -- Gitee