def __init__(self,
                 n_cells=4,
                 n_input=1024,
                 n_hidden_state=128,
                 initializer=None):
        with tf.name_scope("GRU_Grid"):
            if initializer is None:
                init = tf.contrib.layers.xavier_initializer()
            else:
                init = initializer

            # parameters for the hidden state and update & reset gates
            self.W = [
                Weight_Matrices(
                    n_cells, n_input, n_hidden_state, initializer=init)
            ] * 3
            self.U = [
                tf.Variable(init([3, 3, 3, n_hidden_state, n_hidden_state]),
                            name="U")
            ] * 3
            self.b = [
                tf.Variable(init([n_cells, n_cells, n_cells, n_hidden_state]),
                            name="b")
            ] * 3

            params = utils.read_params()
            if params["VIS"]["HISTOGRAMS"]:
                for i in range(3):
                    tf.summary.histogram("U[{}]".format(i), self.U[i])
                    tf.summary.histogram("b[{}]".format(i), self.b[i])
コード例 #2
0
    def __init__(self, n_cells, n_x, n_h, initializer=None):
        with tf.compat.v1.name_scope("Weight_Matrices"):
            params = utils.read_params()
            # class variables
            self.n_cells = n_cells

            if initializer is None:
                init = tf.compat.v1.keras.initializers.VarianceScaling(
                    scale=1.0, mode="fan_avg", distribution="uniform")
            else:
                init = initializer

            with tf.compat.v1.name_scope("x_list"):
                x_list = []
                for x in range(self.n_cells):
                    with tf.compat.v1.name_scope("y_list"):
                        y_list = []
                        for y in range(self.n_cells):
                            z_list = []
                            with tf.compat.v1.name_scope("z_list"):
                                for z in range(self.n_cells):
                                    name = "W_{}{}{}".format(x, y, z)
                                    W = tf.Variable(init([n_x, n_h]),
                                                    name=name)

                                    if params["VIS"]["HISTOGRAMS"]:
                                        tf.compat.v1.summary.histogram(name, W)
                                    z_list.append(W)
                            y_list.append(z_list)
                    x_list.append(y_list)

            self.weight_matrix_grid = x_list
コード例 #3
0
ファイル: encoder.py プロジェクト: zhushaoquan/3D-FHNet
def fully_connected_sequence(sequence, initializer=None):
    with tf.name_scope(
            "fully_connected_sequence"):  # 在fully_connected_sequence名字域内
        if initializer is None:  # 如果没有给定初始化器
            init = tf.contrib.layers.xavier_initializer()  # 就用Xavier初始化器
        else:  # 反之,如果给定了初始化器
            init = initializer  # 就使用给定的初始化器

        weights = tf.Variable(init([1024, 1024]),
                              name="weights")  # 定义weights变量
        bias = tf.Variable(init([1024]), name="bias")  # 定义bias变量

        def forward_pass(a):
            return tf.nn.bias_add(  # 定义前馈函数,将输入与weights相乘再加上bias
                tf.matmul(a, weights), bias)

        ret = tf.map_fn(forward_pass, sequence,
                        name='fully_connected_map')  # 将前馈操作批量应用于sequence

        params = utils.read_params()
        if params["VIS"]["HISTOGRAMS"]:
            tf.summary.histogram("weights", weights)
            tf.summary.histogram("bias", bias)

    return ret
コード例 #4
0
ファイル: preprocessor.py プロジェクト: nindoumon/3d-r2n2
    def __init__(self, X):
        with tf.compat.v1.name_scope("Preprocessor"):
            params = utils.read_params()
            if params["TRAIN"]["TIME_STEP_COUNT"] == "RANDOM":
                n_timesteps = tf.random.uniform([],
                                                minval=1,
                                                maxval=25,
                                                dtype=tf.int32)
                tf.compat.v1.summary.scalar("n_timesteps", n_timesteps)
            elif isinstance(params["TRAIN", "TIME_STEP_COUNT"],
                            int) and params["TRAIN"]["TIME_STEP_COUNT"] > 0:
                n_timesteps = params["TRAIN"]["TIME_STEP_COUNT"]
            else:
                n_timesteps = tf.shape(input=X)[1]

            n_batchsize = tf.shape(input=X)[0]
            X_dropped_alpha = X[:, :, :, :, 0:3]  # drop alpha channel
            X_cropped = tf.image.random_crop(
                X_dropped_alpha,
                [n_batchsize, n_timesteps, 127, 127, 3])  # randomly crop

            if params["TRAIN"]["SHUFFLE_IMAGE_SEQUENCE"]:
                X_shuffled = shuffle_sequence(X_cropped)
                self.out_tensor = X_shuffled
            else:
                self.out_tensor = X_cropped
コード例 #5
0
def create_path_csv(data_dir, label_dir):
    print("creating path csv for {} and {}".format(data_dir, label_dir))
    params = utils.read_params()

    common_paths = []
    for dir_top, subdir_cmps in dircmp(data_dir, label_dir).subdirs.items():
        for dir_bot in subdir_cmps.common_dirs:
            common_paths.append(os.path.join(dir_top, dir_bot))

    mapping = pd.DataFrame(common_paths, columns=["common_dirs"])
    mapping['data_dirs'] = mapping.apply(
        lambda data_row: os.path.join(data_dir, data_row.common_dirs), axis=1)

    mapping['label_dirs'] = mapping.apply(
        lambda data_row: os.path.join(label_dir, data_row.common_dirs), axis=1)

    table = []
    for n, d, l in zip(common_paths, mapping.data_dirs, mapping.label_dirs):
        data_row = [os.path.dirname(n) + "_" + os.path.basename(n)]
        data_row += construct_file_path_list_from_dir(d, [".png"])
        data_row += construct_file_path_list_from_dir(l, [".binvox"])
        table.append(data_row)

    paths = pd.DataFrame(table)
    paths.to_csv("{}/paths.csv".format(params["DIRS"]["OUTPUT"]))
    return paths
コード例 #6
0
def setup_dir():
    params = utils.read_params()
    DIR = params["DIRS"]
    for d in DIR.values():
        utils.make_dir(d)

    utils.check_params_json("params.json")
コード例 #7
0
    def __init__(self,  n_cells, n_x, n_h,  initializer=None):
        with tf.name_scope("Weight_Matrices"):
            params = utils.read_params() # 读取超参
            # class variables
            self.n_cells = n_cells # RNN_cell_num

            if initializer is None: # 如果没有设定初始化方式
                init = tf.contrib.layers.xavier_initializer() # 就用Xavier初始化
            else: # 反之如果设定了初始化方式
                init = initializer # 就用设定的初始化方式

            # 创建x_list的结构:[n_cells,n_cells,n_cells][n_x,n_h]
            with tf.name_scope("x_list"):
                x_list = []
                for x in range(self.n_cells):
                    with tf.name_scope("y_list"):
                        y_list = []
                        for y in range(self.n_cells):
                            z_list = []
                            with tf.name_scope("z_list"):
                                for z in range(self.n_cells):
                                    name = "W_{}{}{}".format(x, y, z)
                                    W = tf.Variable(init(
                                        [n_x, n_h]), name=name)

                                    if params["VIS"]["HISTOGRAMS"]:
                                        tf.summary.histogram(name, W)
                                    z_list.append(W)
                            y_list.append(z_list)
                    x_list.append(y_list)

            self.weight_matrix_grid = x_list
コード例 #8
0
def preprocess_dataset():
    params = utils.read_params()
    dataset_size = params["DATASET_SIZE"]
    output_dir = params["DIRS"]["OUTPUT"]
    data_preprocessed_dir = params["DIRS"]["DATA_PREPROCESSED"]
    data_dir = params["DIRS"]["DATA"]

    if not os.path.isfile("{}/paths.csv".format(output_dir)):
        dataset.create_path_csv("{}/ShapeNetRendering".format(data_dir),
                                "{}/ShapeNetVox32".format(data_dir))

    path_list = pd.read_csv("{}/paths.csv".format(output_dir),
                            index_col=0).values
    # randomly pick examples from dataset
    shuffle(path_list)

    if dataset_size <= 0 or dataset_size >= len(path_list):
        dataset_size = len(path_list)

    for i in range(dataset_size):
        model_name = path_list[i, 0]
        utils.to_npy('{}/{}_x'.format(data_preprocessed_dir, model_name),
                     load_data(path_list[i, 1:-1]))
        utils.to_npy('{}/{}_y'.format(data_preprocessed_dir, model_name),
                     load_label(path_list[i, -1]))
コード例 #9
0
    def __init__(self,
                 n_cells=4,
                 n_input=1024,
                 n_hidden_state=128,
                 initializer=None):
        with tf.compat.v1.name_scope("GRU_Grid"):
            if initializer is None:
                init = tf.compat.v1.keras.initializers.VarianceScaling(
                    scale=1.0, mode="fan_avg", distribution="uniform")
            else:
                init = initializer

            # parameters for the hidden state and update & reset gates
            self.W = [
                Weight_Matrices(
                    n_cells, n_input, n_hidden_state, initializer=init)
            ] * 3
            self.U = [
                tf.Variable(init([3, 3, 3, n_hidden_state, n_hidden_state]),
                            name="U")
            ] * 3
            self.b = [
                tf.Variable(init([n_cells, n_cells, n_cells, n_hidden_state]),
                            name="b")
            ] * 3

            params = utils.read_params()
            if params["VIS"]["HISTOGRAMS"]:
                for i in range(3):
                    tf.compat.v1.summary.histogram("U[{}]".format(i),
                                                   self.U[i])
                    tf.compat.v1.summary.histogram("b[{}]".format(i),
                                                   self.b[i])
コード例 #10
0
ファイル: dataset.py プロジェクト: zhushaoquan/3D-FHNet
def setup_dir():  # 用于创建一些需要的路径
    params = utils.read_params()  # 读取超参
    DIR = params["DIRS"]  # 从读取的超参中读取DIR字典
    for d in DIR.values():  # 遍历DIR字典
        utils.make_dir(d)  # 创建路径

    utils.check_params_json(
        "params.json")  # 用于检查params_json文件是否存在,若是不存在,就写一个空的
コード例 #11
0
def conv_sequence(sequence,
                  in_featuremap_count,
                  out_featuremap_count,
                  initializer=None,
                  K=3,
                  S=[1, 1, 1, 1],
                  D=[1, 1, 1, 1],
                  P="SAME"):
    with tf.name_scope("conv_sequence"):
        if initializer is None:
            init = tf.contrib.layers.xavier_initializer()
        else:
            init = initializer

        kernel = tf.Variable(init(
            [K, K, in_featuremap_count, out_featuremap_count]),
                             name="kernel")
        bias = tf.Variable(init([out_featuremap_count]), name="bias")

        def conv2d(x):
            return tf.nn.bias_add(
                tf.nn.conv2d(x,
                             kernel,
                             S,
                             padding=P,
                             dilations=D,
                             name="conv2d"), bias)

        ret = tf.map_fn(conv2d, sequence, name="conv2d_map")

        tf.add_to_collection("feature_maps", ret)

        # visualization code
        params = utils.read_params()
        image_count = params["VIS"]["IMAGE_COUNT"]
        if params["VIS"]["KERNELS"]:
            kern_1 = tf.concat(tf.unstack(kernel, axis=-1), axis=-1)
            kern_2 = tf.transpose(kern_1, [2, 0, 1])
            kern_3 = tf.expand_dims(kern_2, -1)
            tf.summary.image("2d kernel", kern_3, max_outputs=image_count)

        if params["VIS"]["FEATURE_MAPS"]:
            feature_map_1 = tf.concat(tf.unstack(ret, axis=4), axis=2)
            feature_map_2 = tf.concat(tf.unstack(feature_map_1, axis=1),
                                      axis=2)
            feature_map_3 = tf.expand_dims(feature_map_2, -1)
            tf.summary.image("feature_map",
                             feature_map_3,
                             max_outputs=image_count)

        if params["VIS"]["HISTOGRAMS"]:
            tf.summary.histogram("kernel", kernel)
            tf.summary.histogram("bias", bias)

        if params["VIS"]["SHAPES"]:
            print(ret.shape)
    return ret
コード例 #12
0
def load_preprocessed_dataset():
    data_preprocessed_dir = utils.read_params(
    )["DIRS"]["DATA_PREPROCESSED"]

    data_all = sorted(
        dataset.construct_file_path_list_from_dir(data_preprocessed_dir, ["_x.npy"]))
    label_all = sorted(
        dataset.construct_file_path_list_from_dir(data_preprocessed_dir, ["_y.npy"]))

    return np.array(data_all), np.array(label_all)
コード例 #13
0
def conv_vox(vox,
             in_featurevoxel_count,
             out_featurevoxel_count,
             K=3,
             S=[1, 1, 1, 1, 1],
             D=[1, 1, 1, 1, 1],
             initializer=None,
             P="SAME"):
    with tf.name_scope("conv_vox"):
        if initializer is None:
            init = tf.contrib.layers.xavier_initializer()
        else:
            init = initializer

        kernel = tf.Variable(init(
            [K, K, K, in_featurevoxel_count, out_featurevoxel_count]),
                             name="kernel")
        bias = tf.Variable(init([out_featurevoxel_count]), name="bias")
        ret = tf.nn.bias_add(
            tf.nn.conv3d(vox, kernel, S, padding=P, dilations=D,
                         name="conv3d"), bias)
        tf.add_to_collection("feature_voxels", ret)

        # visualization code
        params = utils.read_params()
        image_count = params["VIS"]["IMAGE_COUNT"]
        if params["VIS"]["KERNELS"]:
            kern_1 = tf.concat(tf.unstack(kernel, axis=-1), axis=-1)
            kern_2 = tf.transpose(kern_1, [3, 0, 1, 2])
            kern_3 = tf.expand_dims(kern_2, -1)
            kern_4 = tf.concat(tf.unstack(kern_3, axis=1), axis=1)
            tf.summary.image("3d kernel", kern_4, max_outputs=image_count)

        if params["VIS"]["VOXEL_SLICES"]:
            vox_slice_1 = tf.unstack(ret, axis=4)[1]
            vox_slice_2 = tf.split(vox_slice_1, 4, axis=3)
            vox_slice_3 = tf.concat(vox_slice_2, axis=1)
            vox_slice_4 = tf.concat(tf.unstack(vox_slice_3, axis=-1), axis=2)
            vox_slice_5 = tf.expand_dims(vox_slice_4, -1)
            tf.summary.image("vox_slices",
                             vox_slice_5,
                             max_outputs=image_count)

        if params["VIS"]["FEATURE_VOXELS"]:
            tf.summary.tensor_summary("feature_voxels", ret[0, :, :, :, 0])

        if params["VIS"]["HISTOGRAMS"]:
            tf.summary.histogram("kernel", kernel)
            tf.summary.histogram("bias", bias)

        if params["VIS"]["SHAPES"]:
            print(ret.shape)

    return ret
コード例 #14
0
ファイル: dataset.py プロジェクト: zhushaoquan/3D-FHNet
def create_path_csv(data_dir, label_dir):
    print("creating path csv for {} and {}".format(data_dir,
                                                   label_dir))  # 在终端打印创建csv的信息
    params = utils.read_params()  # 读取超参

    common_paths = []  # 创建一个list,用于存储data和label中相同的对应的路径
    # dircmp用于比较两个目录中的文件是否相同,得到filecmp.dircmp object,.common_dirs可以得到两个目录中的相同目录
    # dircmp().subdirs返回相同名称的对应字典,键是相同文件夹的名字,值是filecmp.dircmp object,.common_dirs可以得到那个相同目录下的相同目录名,也就是两层都相同的目录的子目录名
    # dircmp().subdirs.items()得到两个目录中的相同名称的键值对
    for dir_top, subdir_cmps in dircmp(
            data_dir,
            label_dir).subdirs.items():  # 遍历data_dir和label_dir中的相同目录的键值对
        for dir_bot in subdir_cmps.common_dirs:  # 遍历相同目录中的相同目录
            common_paths.append(
                os.path.join(dir_top, dir_bot)
            )  # 将data_dir和label_dir中的相同目录名和相同目录中的相同目录名连起来,加到common_paths中,也就是得到两层目录都相同的list

    mapping = pd.DataFrame(common_paths,
                           columns=["common_dirs"
                                    ])  # 将common_paths列表转化成dataframe
    mapping[
        'data_dirs'] = mapping.apply(  # DataFrame.apply()用于将第一个参数的函数应用于一批数据,axis=0是按列操作,axis=1是按行操作
            lambda data_row: os.path.join(data_dir, data_row.common_dirs),
            axis=1)  # 将data_dirs连上每个common_dirs,得到所有的对应的data的路径
    for dirs in mapping.data_dirs:
        dirs = os.path.join(data_dir, 'rendering')  # 原版需要在最后增加一个rendering文件夹

    mapping['label_dirs'] = mapping.apply(  # 同上
        lambda data_row: os.path.join(label_dir, data_row.common_dirs),
        axis=1)  # 将label_dirs连上每个common_dirs,得到所有的的对应的label的路径

    table = []
    # zip()用于将可迭代的对象打包成元组
    for n, d, l in zip(
            common_paths, mapping.data_dirs, mapping.label_dirs
    ):  # 将common_paths,mapping.data_dirs,mapping.label_dirs打包用于迭代
        # TODO: common_paths是由os.path.join()得到的,似乎最后没有\,所以是将路径的最后两个文件夹中间的\换成了_???
        data_row = [
            os.path.dirname(n) + "_" + os.path.basename(n)
        ]  # os.path.dirname()返回文件所在目录;os.path.basename()返回路径最后的文件名。这里相当于将最后一个\变成了_
        data_row += construct_file_path_list_from_dir(
            d, [".png"])  # data_row中增加data_dirs中所有.png文件的路径
        data_row += construct_file_path_list_from_dir(
            l, [".binvox"])  # data_row中增加label_dirs中所有.binvox文件的路径
        table.append(
            data_row
        )  # 将这一个common_paths,data_dirs,label_dirs中得到的路径作为一行,加入到table中去

    paths = pd.DataFrame(table)  # 将table转化成pandas.DataFrame,存在paths中
    paths.to_csv("{}\\paths.csv".format(
        params["DIRS"]["OUTPUT"]))  # 将paths写成csv,存在output路径中
    return paths
コード例 #15
0
ファイル: dataset.py プロジェクト: zhushaoquan/3D-FHNet
def load_preprocessed_dataset():  # 用于取得所有打包的数据的路径
    data_preprocessed_dir = utils.read_params(  # 读取预处理过的数据的路径
    )["DIRS"]["DATA_PREPROCESSED"]

    data_all = sorted(  # sorted()函数对所有可迭代的对象进行排序操作
        dataset.construct_file_path_list_from_dir(
            data_preprocessed_dir,
            ["_x.npy"]))  # 寻找data_preprocessed_dir目录下的所有文件名包括'_x.npy'的文件名
    label_all = sorted(  # sorted()函数对所有可迭代的对象进行排序操作
        dataset.construct_file_path_list_from_dir(
            data_preprocessed_dir,
            ["_y.npy"]))  # 寻找data_preprocessed_dir目录下的所有文件名包括'_y.npy'的文件名

    return np.array(data_all), np.array(label_all)  # 将list转换成np.ndarray并返回
コード例 #16
0
    def __init__(self, n_cells=4, n_input=1024, n_hidden_state=128, initializer=None):
        with tf.name_scope("LSTM_Grid"):
            if initializer is None: # 如果没有给定初始化器的参数
                init = tf.contrib.layers.xavier_initializer() # 使用xavier初始化器
            else: # 反之,如果给定了初始化器的参数
                init = initializer # 使用给定的初始化器

            # 定义forget gate,input gate,output gate,cell state的参数
            self.W = [Weight_Matrices(
                n_cells, n_input, n_hidden_state, initializer=init)]*4
            self.U = [tf.Variable(init(
                [3, 3, 3, n_hidden_state, n_hidden_state]), name="U")]*4
            self.b = [tf.Variable(init(
                [n_cells, n_cells, n_cells, n_hidden_state]), name="b")]*4

            params = utils.read_params() # 读取超参
            if params["VIS"]["HISTOGRAMS"]: # 如果需要可视化直方图
                for i in range(4):
                    tf.summary.histogram("U[{}]".format(i), self.U[i])
                    tf.summary.histogram("b[{}]".format(i), self.b[i])
コード例 #17
0
def fully_connected_sequence(sequence, initializer=None):
    with tf.name_scope("fully_connected_sequence"):
        if initializer is None:
            init = tf.contrib.layers.xavier_initializer()
        else:
            init = initializer

        weights = tf.Variable(init([1024, 1024]), name="weights")
        bias = tf.Variable(init([1024]), name="bias")

        def forward_pass(a):
            return tf.nn.bias_add(tf.matmul(a, weights), bias)

        ret = tf.map_fn(forward_pass, sequence, name='fully_connected_map')

        params = utils.read_params()
        if params["VIS"]["HISTOGRAMS"]:
            tf.summary.histogram("weights", weights)
            tf.summary.histogram("bias", bias)

    return ret
コード例 #18
0
ファイル: network.py プロジェクト: zhushaoquan/3D-FHNet
    def read_params(self, params=None):  # 用于读取超参
        if params is None:  # 如果没有给定超参
            self.params = utils.read_params()  # 就读取默认超参
        else:  # 如果给定了超参
            self.params = params  # 就用给定的超参

        if self.params["TRAIN"][
                "INITIALIZER"] == "XAVIER":  # 如果超参中的初始化设定为XAVIER
            self.init = tf.contrib.layers.xavier_initializer(
            )  # 用Xavier初始化(使每一层输出的方差应该尽量相等)
        else:  # 如果超参中的初始化不是设定为XAVIER
            self.init = tf.random_normal_initializer()  # 生成标准正态分布的随机数来初始化

        self.CREATE_TIME = datetime.now().strftime(
            "%Y-%m-%d_%H.%M.%S")  # 获取当前的时间,并将其转换为字符串格式存储下来,作为创建时间
        self.MODEL_DIR = "{}\\model_{}".format(  # string.format()的功能是,将后面的参数填到前面的{}中去
            self.params["DIRS"]["MODELS_LOCAL"], self.CREATE_TIME)  # 得到待创建的路径名
        utils.make_dir(self.MODEL_DIR)  # 新建路径

        with open(self.MODEL_DIR + '\\params.json', 'w') as f:
            json.dump(self.params, f)  # 将超参写成json文件
コード例 #19
0
ファイル: preprocessor.py プロジェクト: zhushaoquan/3D-FHNet
    def __init__(self, X):
        with tf.name_scope("Preprocessor"): # 使用Preprocessor名字域
            params = utils.read_params() # 读取超参
            if params["TRAIN"]["TIME_STEP_COUNT"] == "RANDOM": # 如果超参中训练时的time_step_count是RANDOM
                n_timesteps = tf.random_uniform( # tf.random_uniform()用于产生随机数,并且产生的值是均匀分布在minval和maxval之间
                    [], minval=1, maxval=13, dtype=tf.int32) # 如果要随机次数训练,那么在minval和maxval之间随机取 TODO: shape属性为[]会出现什么情况?
                tf.summary.scalar("n_timesteps", n_timesteps) # 可视化
            elif isinstance(params["TRAIN"]["TIME_STEP_COUNT"], int) and params["TRAIN"]["TIME_STEP_COUNT"] > 0: # 如果超参中的time_step_count不是RANDOM而是一个int数
                n_timesteps = params["TRAIN"]["TIME_STEP_COUNT"] # 使用设置好的那个数来作为n_timesteps
            else: # 又不是随机又不是特定整数
                n_timesteps = tf.shape(X)[1] # n_timesteps取X的第一维 TODO: 估计是取所有图片

            n_batchsize = tf.shape(X)[0] # n_batchsize取X的第0维
            X_dropped_alpha = X[:, :, :, :, 0:3]  # 取RGB数据,丢弃透明度
            X_cropped = tf.random_crop( # 裁剪X
                X_dropped_alpha, [n_batchsize, n_timesteps, 128, 128, 3])   # randomly crop

            if params["TRAIN"]["SHUFFLE_IMAGE_SEQUENCE"]: # 如果超参里设置了打乱图片的顺序
                X_shuffled = shuffle_sequence(X_cropped) # 打乱
                self.out_tensor = X_shuffled # 将待返回的tensor设置成打乱的X
            else: # 如果超参里设置的是不打乱图片的顺序
                self.out_tensor = X_cropped # 将待返回的tensor设置成裁剪后的X
コード例 #20
0
def fully_connected_sequence(sequence, initializer=None):
    with tf.compat.v1.name_scope("fully_connected_sequence"):
        if initializer is None:
            init = tf.compat.v1.keras.initializers.VarianceScaling(scale=1.0, mode="fan_avg", distribution="uniform")
        else:
            init = initializer

        weights = tf.Variable(
            init([1024, 1024]), name="weights")
        bias = tf.Variable(init([1024]), name="bias")

        def forward_pass(a): return tf.nn.bias_add(
            tf.matmul(a, weights), bias)

        ret = tf.map_fn(forward_pass, sequence, name='fully_connected_map')

        params = utils.read_params()
        if params["VIS"]["HISTOGRAMS"]:
            tf.compat.v1.summary.histogram("weights", weights)
            tf.compat.v1.summary.histogram("bias", bias)

    return ret
コード例 #21
0
ファイル: dataset.py プロジェクト: zhushaoquan/3D-FHNet
def preprocess_dataset():
    params = utils.read_params()  # 读取超参
    dataset_size = params[
        "DATASET_SIZE"]  # 从超参中读取数据集尺寸(使用多少个模型,设置为小于等于0就是使用所有模型)
    output_dir = params["DIRS"]["OUTPUT"]  # 从超参中读取输出路径
    data_preprocessed_dir = params["DIRS"][
        "DATA_PREPROCESSED"]  # 从超参中读取处理过后的数据存放路径
    data_dir = params["DIRS"]["DATA"]  # 从超参中读取数据路径

    if not os.path.isfile(
            "{}\\paths.csv".format(output_dir)):  # 判断输出路径中是否存在paths.csv
        dataset.create_path_csv(  # 如果不存在,就创建一个
            "{}\\{}".format(data_dir,
                            image_dir), "{}\\ShapeNetVox32".format(data_dir)
        )  # paths.csv中每一行是一个物体,第一列是索引,2-倒数第2列是所有图片的路径,最后一列是体素的路径

    path_list = pd.read_csv(  # 读取paths.csv
        "{}\\paths.csv".format(output_dir),
        index_col=0).as_matrix()  # dataframe.as_matrix()用于将dataframe转换成矩阵
    # randomly pick examples from dataset
    shuffle(path_list)  # numpy.random.shuffle()用于打乱list中元素的顺序

    if dataset_size <= 0 or dataset_size >= len(
            path_list):  # 如果设定的Dataset_size小于等于零或者大于所有数据的个数
        dataset_size = len(path_list)  # 就将dataset_size设定为所有数据的个数

    for i in range(dataset_size):  # 循环dataset_size次
        model_name = path_list[i, 0]  # 取出需要使用的路径
        utils.to_npy(
            '{}\\{}_x'.format(data_preprocessed_dir,
                              model_name),  # 将图片数据存储成.npy
            load_data(path_list[i, 1:-1]))  # 加载图片数据
        utils.to_npy(
            '{}\\{}_y'.format(data_preprocessed_dir,
                              model_name),  # 将体素数据存储成.npy
            load_label(path_list[i, -1]))  # 加载体素数据
コード例 #22
0
    def save_loss(loss_arr, loss_type):
        loss_ndarr = np.array(loss_arr)
        save_dir = net.get_cur_epoch_dir()
        np.save("{}/{}_loss.npy".format(save_dir, loss_type), loss_ndarr)

    def plot_loss(loss_arr, loss_type):
        loss_ndarr = np.array(loss_arr)
        save_dir = net.get_cur_epoch_dir()
        plt.plot(loss_ndarr.flatten())
        plt.savefig("{}/{}_loss.png".format(save_dir, loss_type),
                    bbox_inches="tight")
        plt.close()

    # params on disk
    print(utils.read_params()["TRAIN"])

    # get preprocessed data
    data, label = dataset.load_preprocessed_dataset()

    # init network
    net = network.Network()
    # net.init_parameters()

    params = net.get_params()
    train_params = params["TRAIN"]

    # split dataset
    X_train, y_train, X_val, y_val, X_test, y_test = dataset.train_val_test_split(
        data, label)
    save_dataset_split()
コード例 #23
0
ファイル: encoder.py プロジェクト: zhushaoquan/3D-FHNet
def conv_sequence(sequence,
                  in_featuremap_count,
                  out_featuremap_count,
                  initializer=None,
                  K=3,
                  S=[1, 1, 1, 1],
                  D=[1, 1, 1, 1],
                  P="SAME"):
    with tf.name_scope("conv_sequence"):  # 在conv_sequence名字域中
        if initializer is None:  # 如果没有给定初始化器
            init = tf.contrib.layers.xavier_initializer()  # 就使用Xavier初始化器
        else:  # 反之,如果给定了初始化器
            init = initializer  # 使用给定的初始化器

        kernel = tf.Variable(init(
            [K, K, in_featuremap_count, out_featuremap_count]),
                             name="kernel")  # 创建卷积核的变量
        bias = tf.Variable(init([out_featuremap_count]),
                           name="bias")  # 创建偏差的变量

        # tf.nn.conv2d(input,filter,strides,padding)中,
        # input是一个4D输入[batch_size,in_height,in_width,n_channels],
        # filter是一个4D输入[filter_height,filter_width,in_channels,out_channels]
        # padding有两种选项,same是做填充,valid是不做填充
        # tf.nn.bias_add()的功能是将bias加到每一个value上
        def conv2d(x):
            return tf.nn.bias_add(
                tf.nn.conv2d(x,
                             kernel,
                             S,
                             padding=P,
                             dilations=D,
                             name="conv2d"), bias)

        # tf.map_fn(fn,elems)用于将elems从第一维展开,批量进行fn运算
        ret = tf.map_fn(conv2d, sequence, name="conv2d_map")
        # tf.add_to_collection('list_name',element)用于将element添加到list_name中
        # tf.get_collection('list_name')返回名称为list_name的列表
        tf.add_to_collection("feature_maps", ret)

        # visualization code
        params = utils.read_params()  # 读取超参
        image_count = params["VIS"]["IMAGE_COUNT"]
        if params["VIS"]["KERNELS"]:
            kern_1 = tf.concat(tf.unstack(kernel, axis=-1), axis=-1)
            kern_2 = tf.transpose(kern_1, [2, 0, 1])
            kern_3 = tf.expand_dims(kern_2, -1)
            tf.summary.image("2d kernel", kern_3, max_outputs=image_count)

        if params["VIS"]["FEATURE_MAPS"]:
            feature_map_1 = tf.concat(tf.unstack(ret, axis=4), axis=2)
            feature_map_2 = tf.concat(tf.unstack(feature_map_1, axis=1),
                                      axis=2)
            feature_map_3 = tf.expand_dims(feature_map_2, -1)
            tf.summary.image("feature_map",
                             feature_map_3,
                             max_outputs=image_count)

        if params["VIS"]["HISTOGRAMS"]:
            tf.summary.histogram("kernel", kernel)
            tf.summary.histogram("bias", bias)

        if params["VIS"]["SHAPES"]:
            print(ret.shape)
    return ret
コード例 #24
0
ファイル: run.py プロジェクト: zhushaoquan/3D-FHNet
    def save_loss(loss_arr, loss_type):
        loss_ndarr = np.array(loss_arr)
        save_dir = net.get_cur_epoch_dir()
        np.save("{}\\{}_loss.npy".format(save_dir, loss_type), loss_ndarr)

    def plot_loss(loss_arr, loss_type):
        loss_ndarr = np.array(loss_arr)
        save_dir = net.get_cur_epoch_dir()
        plt.plot(loss_ndarr.flatten())
        plt.savefig("{}\\{}_loss.png".format(save_dir, loss_type),
                    bbox_inches="tight")
        plt.close()

    # params on disk
    print(utils.read_params()["TRAIN"])  # 读取超参,并打印超参中的训练时使用的超参

    # get preprocessed data
    data, label = dataset.load_preprocessed_dataset()  # 得到打包的数据和标签的路径
    print(data.shape[0])

    # init network
    net = network.Network()  # 建图
    # net.init_parameters()

    params = net.get_params()
    train_params = params["TRAIN"]

    # split dataset
    X_train, y_train, X_val, y_val, X_test, y_test = dataset.train_val_test_split(
        data, label)
コード例 #25
0
    def __init__(self, params=None):
        # read params
        if params is None:
            self.params = utils.read_params()
        else:
            self.params = params

        if self.params["TRAIN"]["INITIALIZER"] == "XAVIER":
            init = tf.contrib.layers.xavier_initializer()
        else:
            init = tf.random_normal_initializer()

        self.CREATE_TIME = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
        self.MODEL_DIR = "{}/model_{}".format(
            self.params["DIRS"]["MODELS_LOCAL"], self.CREATE_TIME)
        utils.make_dir(self.MODEL_DIR)

        with open(self.MODEL_DIR + '/params.json', 'w') as f:
            json.dump(self.params, f)

        # place holders
        with tf.name_scope("Data"):
            self.X = tf.placeholder(tf.float32, [None, None, None, None, None])
        with tf.name_scope("Labels"):
            self.Y_onehot = tf.placeholder(tf.float32, [None, 32, 32, 32, 2])

        pp = preprocessor.Preprocessor(self.X)
        X_preprocessed = pp.out_tensor
        n_batchsize = tf.shape(X_preprocessed)[0]

        # encoder
        print("encoder")
        if self.params["TRAIN"]["ENCODER_MODE"] == "DILATED":
            en = encoder.Dilated_Encoder(X_preprocessed)
        elif self.params["TRAIN"]["ENCODER_MODE"] == "RESIDUAL":
            en = encoder.Residual_Encoder(X_preprocessed)
        else:
            en = encoder.Simple_Encoder(X_preprocessed)
        encoded_input = en.out_tensor

        # visualize transformation of input state to voxel
        if self.params["VIS"]["ENCODER_PROCESS"]:
            with tf.name_scope("misc"):
                feature_maps = tf.get_collection("feature_maps")
                fm_list = []
                for fm in feature_maps:
                    fm_slice = fm[0, 0, :, :, 0]
                    fm_shape = fm_slice.get_shape().as_list()
                    fm_slice = tf.pad(fm_slice,
                                      [[0, 0], [127 - fm_shape[0], 0]])
                    fm_list.append(fm_slice)
                fm_img = tf.concat(fm_list, axis=0)
                tf.summary.image("feature_map_list",
                                 tf.expand_dims(tf.expand_dims(fm_img, -1), 0))

        # recurrent_module
        print("recurrent_module")
        with tf.name_scope("Recurrent_module"):
            rnn_mode = self.params["TRAIN"]["RNN_MODE"]
            n_cell = self.params["TRAIN"]["RNN_CELL_NUM"]
            n_hidden = self.params["TRAIN"]["RNN_HIDDEN_SIZE"]

            if rnn_mode == "LSTM":
                rnn = recurrent_module.LSTM_Grid(initializer=init)
                hidden_state = (
                    tf.zeros([n_batchsize, n_cell, n_cell, n_cell, n_hidden],
                             name="zero_hidden_state"),
                    tf.zeros([n_batchsize, n_cell, n_cell, n_cell, n_hidden],
                             name="zero_cell_state"))
            else:
                rnn = recurrent_module.GRU_Grid(initializer=init)
                hidden_state = tf.zeros(
                    [n_batchsize, n_cell, n_cell, n_cell, n_hidden],
                    name="zero_hidden_state")

            n_timesteps = self.params["TRAIN"]["TIME_STEP_COUNT"]
            # feed a limited seqeuence of images
            if isinstance(n_timesteps, int) and n_timesteps > 0:
                for t in range(n_timesteps):
                    hidden_state = rnn.call(encoded_input[:, t, :],
                                            hidden_state)
            else:  # feed an arbitray seqeuence of images
                n_timesteps = tf.shape(X_preprocessed)[1]

                t = tf.constant(0)

                def condition(h, t):
                    return tf.less(t, n_timesteps)

                def body(h, t):
                    h = rnn.call(encoded_input[:, t, :], h)
                    t = tf.add(t, 1)
                    return h, t

                hidden_state, t = tf.while_loop(condition, body,
                                                (hidden_state, t))

        # decoder
        print("decoder")
        if isinstance(hidden_state, tuple):
            hidden_state = hidden_state[0]
        if self.params["TRAIN"]["DECODER_MODE"] == "DILATED":
            de = decoder.Dilated_Decoder(hidden_state)
        elif self.params["TRAIN"]["DECODER_MODE"] == "RESIDUAL":
            de = decoder.Residual_Decoder(hidden_state)
        else:
            de = decoder.Simple_Decoder(hidden_state)
        self.logits = de.out_tensor

        # visualize transformation of hidden state to voxel
        if self.params["VIS"]["DECODER_PROCESS"]:
            with tf.name_scope("misc"):
                feature_voxels = tf.get_collection("feature_voxels")
                fv_list = []
                for fv in feature_voxels:
                    fv_slice = fv[0, :, :, 0, 0]
                    fv_shape = fv_slice.get_shape().as_list()
                    fv_slice = tf.pad(fv_slice,
                                      [[0, 0], [32 - fv_shape[0], 0]])
                    fv_list.append(fv_slice)
                fv_img = tf.concat(fv_list, axis=0)
                tf.summary.image("feature_voxel_list",
                                 tf.expand_dims(tf.expand_dims(fv_img, -1), 0))

        # loss
        print("loss")
        voxel_loss = loss.Voxel_Softmax(self.Y_onehot, self.logits)
        self.loss = voxel_loss.loss
        self.softmax = voxel_loss.softmax
        tf.summary.scalar("loss", self.loss)

        # misc
        print("misc")
        with tf.name_scope("misc"):
            self.step_count = tf.Variable(0,
                                          trainable=False,
                                          name="step_count")
            self.print = tf.Print(self.loss, [self.step_count, self.loss, t])

        # optimizer
        print("optimizer")
        if self.params["TRAIN"]["OPTIMIZER"] == "ADAM":
            optimizer = tf.train.AdamOptimizer(
                learning_rate=self.params["TRAIN"]["ADAM_LEARN_RATE"],
                epsilon=self.params["TRAIN"]["ADAM_EPSILON"])
            tf.summary.scalar("adam_learning_rate", optimizer._lr)
        else:
            optimizer = tf.train.GradientDescentOptimizer(
                learning_rate=self.params["TRAIN"]["GD_LEARN_RATE"])
            tf.summary.scalar("learning_rate", optimizer._learning_rate)

        grads_and_vars = optimizer.compute_gradients(self.loss)
        self.apply_grad = optimizer.apply_gradients(
            grads_and_vars, global_step=self.step_count)

        # metric
        print("metrics")
        with tf.name_scope("metrics"):
            Y = tf.argmax(self.Y_onehot, -1)
            predictions = tf.argmax(self.softmax, -1)
            acc, acc_op = tf.metrics.accuracy(Y, predictions)
            rms, rms_op = tf.metrics.root_mean_squared_error(
                self.Y_onehot, self.softmax)
            iou, iou_op = tf.metrics.mean_iou(Y, predictions, 2)
            self.metrics_op = tf.group(acc_op, rms_op, iou_op)

        tf.summary.scalar("accuracy", acc)
        tf.summary.scalar("rmse", rms)
        tf.summary.scalar("iou", iou)

        # initalize
        # config=tf.ConfigProto(log_device_placement=True)
        print("setup")
        self.summary_op = tf.summary.merge_all()
        self.sess = tf.InteractiveSession()
        if self.params["MODE"] == "DEBUG":
            self.sess = tf_debug.TensorBoardDebugWrapperSession(
                self.sess,
                "nat-oitwireless-inside-vapornet100-c-15126.Princeton.EDU:6064"
            )

        # summaries
        print("summaries")
        if self.params["MODE"] == "TEST":
            self.test_writer = tf.summary.FileWriter(
                "{}/test".format(self.MODEL_DIR), self.sess.graph)
        else:
            self.train_writer = tf.summary.FileWriter(
                "{}/train".format(self.MODEL_DIR), self.sess.graph)
            self.val_writer = tf.summary.FileWriter(
                "{}/val".format(self.MODEL_DIR), self.sess.graph)

        # initialize
        print("initialize")
        tf.global_variables_initializer().run()
        tf.local_variables_initializer().run()
        print("...done!")
コード例 #26
0
    def __init__(self, params=None):
        # read params
        if params is None:
            params = utils.read_params()['TRAIN_PARAMS']

        self.LEARN_RATE = params['LEARN_RATE']
        self.BATCH_SIZE = params['BATCH_SIZE']
        self.EPOCH_COUNT = params['EPOCH_COUNT']
        self.CREATE_TIME = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
        self.MODEL_DIR = "out/model_{}_L:{}_E:{}_B:{}".format(
            self.CREATE_TIME, self.LEARN_RATE, self.EPOCH_COUNT, self.BATCH_SIZE)

        # place holders
        self.X = tf.placeholder(tf.float32, [None, 24, 137, 137, 4])

        # encoder
        print("encoder")
        encoder = encoder_module.Conv_Encoder(self.X)
        encoded_input = encoder.out_tensor

        print("recurrent_module")
        # recurrent_module
        with tf.name_scope("recurrent_module"):
            GRU_Grid = recurrent_module.GRU_Grid()
            hidden_state = None
            for t in range(24):
                hidden_state = GRU_Grid.call(
                    encoded_input[:, t, :], hidden_state)

        # decoder
        print("decoder")
        decoder = decoder_module.Conv_Decoder(hidden_state)
        logits = decoder.out_tensor

        # loss
        print("loss")
        self.Y = tf.placeholder(tf.uint8, [None, 32, 32, 32])
        voxel_softmax = loss_module.Voxel_Softmax(self.Y, logits)
        self.prediction = voxel_softmax.prediction
        self.loss = voxel_softmax.batch_loss
        tf.summary.scalar('loss', self.loss)

        # optimizer
        print("optimizer")
        self.step_count = tf.Variable(
            0, trainable=False, name="step_count")
        optimizer = tf.train.GradientDescentOptimizer(
            learning_rate=self.LEARN_RATE)
        grads_and_vars = optimizer.compute_gradients(self.loss)
        self.apply_grad = optimizer.apply_gradients(
            grads_and_vars, global_step=self.step_count)

        # misc op
        print("misc op")
        self.print = tf.Print(
            self.loss, [self.step_count, self.LEARN_RATE, self.loss])
        self.summary_op = tf.summary.merge_all()
        self.sess = tf.InteractiveSession()

        print("initalize variables")
        tf.global_variables_initializer().run()

        # pointers to training objects
        self.train_writer = None
        self.val_writer = None
        self.test_writer = None