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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/main_AttSets.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/main_AttSets.py deleted file mode 100644 index 672986fab5909e2cdaaeec46ec12b5c114979640..0000000000000000000000000000000000000000 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/main_AttSets.py +++ /dev/null @@ -1,719 +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") -parser.add_argument("--epoch", type=int, default=20) -parser.add_argument("--batchsize", type=int, default=2) -parser.add_argument("--train_view_num", type=int, default=24) -parser.add_argument("--total_mv", type=int, default=24) - -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['batch_size'] = obs_config.batchsize -config['total_mv'] = obs_config.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, 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 65135e0c4529572e794f8cbf6b8f57716f836182..0000000000000000000000000000000000000000 --- 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/npu_train.sh b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/npu_train.sh deleted file mode 100644 index a91aac6989b37feec9b61c414d2245e8e4cb5760..0000000000000000000000000000000000000000 --- 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 6d5d0ba20ae1082572c565387e6fae9eb1ff5364..0000000000000000000000000000000000000000 --- 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/Model/.keep b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Model/.keep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md index 11c24c143465b34defb5144a40e9718a2d740863..e6025a0570265a60b513e70e4605b5e854bb9c84 100644 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/README.md @@ -1,84 +1,178 @@ -# 3D_Object_Reconstruction +- [基本信息](#基本信息.md) +- [概述](#概述.md) +- [训练环境准备](#训练环境准备.md) +- [快速上手](#快速上手.md) +- [训练结果](#训练结果.md) +- [高级参考](#高级参考.md) +

基本信息

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

概述

-#### 概述 -A Novel Hybrid Ensemble Approach For 3D Object Reconstruction from Multi-View Monocular RGB images for Robotic Simulations. + 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 + https://arxiv.org/pdf/1901.11153.pdf -参考实现: +- 参考实现: -适配昇腾 AI 处理器的实现:https://toscode.gitee.com/ascend/modelzoo/pulls/5278 + https://github.com/Ajithbalakrishnan/3D-Object-Reconstruction-from-Multi-View-Monocular-RGB-images -#### 算法架构 -![Image text](https://gitee.com/zhangwx21/Object_Reconstruction/blob/master/structure_updated.png) +- 适配昇腾 AI 处理器的实现: + + + https://toscode.gitee.com/ascend/modelzoo/pulls/5278 + +## 默认配置 -#### 默认配置 +- 训练数据集预处理(以ShapeNet训练集为例,仅作为用户参考示例): -数据集 + - 图像的输入尺寸为137*137 + - 图像输入格式:png -ShapeNet rendered images http://cvgl.stanford.edu/data2/ShapeNetRendering.tgz +- 测试数据集预处理(以ShapeNet验证集为例,仅作为用户参考示例) -ShapeNet voxelized models http://cvgl.stanford.edu/data2/ShapeNetVox32.tg + - 图像的输入尺寸为137*137 + - 图像输入格式:png -运行环境 +- 数据集获取: -python 3.5 + http://cvgl.stanford.edu/data2/ShapeNetRendering.tgz + http://cvgl.stanford.edu/data2/ShapeNetVox32.tgz -tensorflow 1.13.0 +- 训练超参 -numpy 1.13.3 + - Batch size: 2 + - Train epoch: 50 + - Train step: 80900 -scipy 0.19.0 -matplotlib +## 支持特性 -skimage +| 特性列表 | 是否支持 | +|-------|------| +| 分布式训练 | 否 | +| 混合精度 | 是 | +| 并行数据 | 是 | -PyMCubes +## 混合精度训练 -超参数设置 +昇腾910 AI处理器提供自动混合精度功能,可以针对全网中float32数据类型的算子,按照内置的优化策略,自动将部分float32的算子降低精度到float16,从而在精度损失很小的情况下提升系统性能并减少内存使用。 -batchsize:2 +## 开启混合精度 -Learning rate(LR): 0.001/0.0005/0.0001 +脚本已默认开启混合精度。 -Train epoch: 50 +

训练环境准备

-#### 训练模型 +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。 -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 +- 数据集准备 +1. 模型训练使用ShapeNet数据集,数据集请自行获取。 -2. The training log on Modelarts has been saved in the log folder. +## 模型训练 + +- 单击“立即下载”,并选择合适的下载方式下载源码包。 + +- 启动训练之前,首先要配置程序运行相关环境变量。 + + 环境变量配置信息参见: + + [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。 -3. Due to the limitation of running time, the program only trained on the airplane data set(02691156). diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/binvox_rw.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/binvox_rw.py similarity index 100% rename from TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/binvox_rw.py rename to TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/binvox_rw.py diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/demo_AttSets.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/demo_AttSets.py similarity index 100% rename from TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/demo_AttSets.py rename to TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/demo_AttSets.py diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/export_obj.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/export_obj.py similarity index 100% rename from TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/export_obj.py rename to TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/export_obj.py 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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 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 0000000000000000000000000000000000000000..69155def89eda5cbe6cc72031f1606f97644c3ec --- /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) diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/modelarts_entry.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelarts_entry_acc.py similarity index 61% rename from TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/modelarts_entry.py rename to TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelarts_entry_acc.py index 4fb8aca7705b06a06a466aa17538b909cb3738c5..ae7b7985a8c8807490679f1adcd3aacbae4e1494 100644 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/modelarts_entry.py +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelarts_entry_acc.py @@ -27,29 +27,38 @@ # 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") +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())) -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") +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 index 7b4ecc7492257f8bd5265de1b95b0f286709d427..cef7e6440db36b8f831ca6280ef5cdb6d6b6b67f 100644 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelzoo_level.txt +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/modelzoo_level.txt @@ -1,6 +1,6 @@ -GPUStatus:OK -NPUMigrationStatus:OK -FuncStatus:OK -PrecisionStatus:POK -AutoTune:NOK +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 index 75fd48ace4cd97196a51f379abc1b235f1920863..8b716f006c68a350dba859e22094cadfb1c04d74 100644 --- a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/requirements.txt +++ b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/requirements.txt @@ -1,11 +1,11 @@ -tensorflow==1.13.0 - -numpy==1.13.3 - -scipy==0.19.0 - -matplotlib - -skimage - +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/test/ train_full_1p.sh b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/test/ train_full_1p.sh deleted file mode 100644 index 8ae2d297aa55758c0558838315304650db7cebc9..0000000000000000000000000000000000000000 --- 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 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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 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 0000000000000000000000000000000000000000..e092e968b91221da128afea31d0f0c5be0773ea2 --- /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 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 b1b6c36e15941722307c99eea7f42584466ce402..ea30cc72df6ef4b5aab002db63d4e1c52f1af722 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`/../ #集合通信参数,不需要修改 diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/tools.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/tools.py similarity index 100% rename from TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/tools.py rename to TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/tools.py diff --git a/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/voxel.py b/TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/voxel.py similarity index 100% rename from TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/Code/voxel.py rename to TensorFlow/contrib/cv/Pix2Vox_ID1284_for_TensorFlow/voxel.py