[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom op error #57742

Open
ZHUANG-JLU opened this issue Sep 19, 2022 · 0 comments
Open

custom op error #57742

ZHUANG-JLU opened this issue Sep 19, 2022 · 0 comments
Assignees
Labels
comp:runtime c++ runtime, performance issues (cpu) stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.9 Issues found in the TF 2.9 release (or RCs) type:support Support issues

Comments

@ZHUANG-JLU
Copy link
ZHUANG-JLU commented Sep 19, 2022

To replace the tf.py_func, I wrote custom op by c++ and use tf.load_op_library to load the op in python.And get Segmentation fault (core dumped).And when I comment SetShapeFn, there is no error. But the output shape of the custom op is unknow.Could someone can help me ? Another info is using pb model to inference by c++ custom op is correct. Below is the error and code
######################################################################################################
This is the error.
######################################################################################################
WARNING:tensorflow:From` /home/zhj/Py/frustum-pointnets-master/models/tf_util.py:384: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

WARNING:tensorflow:From /home/zhj/Py/frustum-pointnets-master/models/tf_util.py:613: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use rate instead of keep_prob. Rate should be set to rate = 1 - keep_prob.
WARNING:tensorflow:From /home/zhj/Py/frustum-pointnets-master/models/model_util.py:235: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
WARNING:tensorflow:From /home/zhj/Py/frustum-pointnets-master/models/model_util.py:236: calling reduce_sum_v1 (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead
mask.shape---------------------------- (1, 1024)
Segmentation fault (core dumped)

######################################################################################################
This is the custom op

#include "tensorflow/core/framework/common_shape_fns.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/shape_inference.h"

namespace tensorflow {

using shape_inference::DimensionHandle;
using shape_inference::InferenceContext;
using shape_inference::ShapeHandle;
using shape_inference::UnchangedShape;

REGISTER_OP("Zhj")
    .Input("input: float")
    .Output("output: int32")
    .SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) {
        DimensionHandle batch = c->Dim(c->input(0), 0);
        ShapeHandle output_shape = c->MakeShape({batch,c->MakeDim(512),c->MakeDim(2)});
        c->set_output(0, output_shape);
        return Status::OK();
    });

class PyFuncZhj : public tensorflow::OpKernel {
 public:
  explicit PyFuncZhj(OpKernelConstruction* context) : OpKernel(context) {}

  void Compute(OpKernelContext* context) override {
    // Grab the input tensor
    const Tensor& tensor_in = context->input(0);
    const TensorShape& input_shape = tensor_in.shape();
    auto inputEigen = tensor_in.shaped<float, 2>({input_shape.dim_size(0), input_shape.dim_size(1)});
    // Create an output tensor
    Tensor* output_tensor;
    OP_REQUIRES_OK(context, context->allocate_output(
                                0, TensorShape({input_shape.dim_size(0), 512,2}),
                                &output_tensor));
    auto outputEigen = output_tensor->shaped<int32, 3>({input_shape.dim_size(0), 512, 2});
    std::vector<std::vector<int>> indicesVec;
    for (int i = 0; i < input_shape.dim_size(0); i++)
    {
      std::vector<int> tmpIndices;
      for (int j = 0; j < input_shape.dim_size(1); j++)//output_shape.dim_size(1)
      {
        if(inputEigen(i,j) > 0.1)
        {
          tmpIndices.push_back(j);
        }
      }
      int lengthIndices = tmpIndices.size();
      if(lengthIndices > 0)
      {
        while(tmpIndices.size() < 512)
        {
          int rand_num = rand() % lengthIndices;
          tmpIndices.push_back(tmpIndices[rand_num]);
        }
        while (tmpIndices.size() > 512)
        {
          int rand_num = rand() % tmpIndices.size();
          tmpIndices.erase(tmpIndices.begin() + rand_num);
        }
      }
      indicesVec.push_back(tmpIndices);
    }
    for(int i = 0; i < indicesVec.size(); i++)
    {
      if(0 == indicesVec[i].size())
      {
        for(int j = 0; j < 512; j++)
        {
          outputEigen(i, j, 0) = i;  // batchsize
          outputEigen(i, j, 1) = 0;
        }
      }
      else
      {
        for(int j = 0; j < indicesVec[i].size(); j++)
        {
          outputEigen(i, j, 0) = i;  // batchsize
          outputEigen(i, j, 1) = indicesVec[i][j];
        }
      }
    }
  }
};
REGISTER_KERNEL_BUILDER(Name("Zhj").Device(DEVICE_CPU), PyFuncZhj);
}

######################################################################################################
This is the python code

Zhj = tf.load_op_library("/home/zhj/Py/frustum-pointnets-master/train/log_v1/zhj.so")
def tf_gather_object_pc(point_cloud, mask, npoints=512):
    ''' Gather object point clouds according to predicted masks.
    Input:
        point_cloud: TF tensor in shape (B,N,C)
        mask: TF tensor in shape (B,N) of 0 (not pick) or 1 (pick)
        npoints: int scalar, maximum number of points to keep (default: 512)
    Output:
        object_pc: TF tensor in shape (B,npoint,C)
        indices: TF int tensor in shape (B,npoint,2)
    '''
    def mask_to_indices(mask):
        indices = np.zeros((mask.shape[0], npoints, 2), dtype=np.int32)
        for i in range(mask.shape[0]):
            pos_indices = np.where(mask[i,:]>0.5)[0]
            # skip cases when pos_indices is empty
            if len(pos_indices) > 0: 
                if len(pos_indices) > npoints:
                    choice = np.random.choice(len(pos_indices),
                        npoints, replace=False)
                else:
                    choice = np.random.choice(len(pos_indices),
                        npoints-len(pos_indices), replace=True)
                    choice = np.concatenate((np.arange(len(pos_indices)), choice))
                np.random.shuffle(choice)
                indices[i,:,1] = pos_indices[choice]
            indices[i,:,0] = i
        return indices

    print("mask.shape----------------------------", mask.shape)  # mask.shape: (1,1024)
    # indices = tf.py_func(mask_to_indices, [mask], tf.int32)
    indices = Zhj.zhj([mask])
    object_pc = tf.gather_nd(point_cloud, indices)
    return object_pc, indices
@mohantym mohantym added comp:lite TF Lite related issues type:support Support issues TF 2.9 Issues found in the TF 2.9 release (or RCs) comp:runtime c++ runtime, performance issues (cpu) and removed comp:lite TF Lite related issues labels Sep 19, 2022
@mohantym mohantym assigned sachinprasadhs and unassigned mohantym Sep 19, 2022
@sachinprasadhs sachinprasadhs added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:runtime c++ runtime, performance issues (cpu) stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.9 Issues found in the TF 2.9 release (or RCs) type:support Support issues
Projects
None yet
Development

No branches or pull requests

4 participants