Почему Tensorflow не может решить эту проблему? - PullRequest
0 голосов
/ 14 сентября 2018

data.py используется для генерации выборочных данных.test.py - основной код для решения параметров.

Движение объектов в трехмерном пространстве можно описать с помощью матрицы 4х4, а матрицу этого типа можно представить с помощью 6 параметров.У меня есть группа данных о положении объектов и еще одна группа окончательных данных о положении после преобразования объектов.Я хочу использовать тензор потока для численного расчета этих 6 параметров.При запуске следующего кода (test.py) выдается сообщение об ошибке «model trainning: None».Кто-нибудь может помочь мне отладить это?Является ли ограничение тензорного потока для этой задачи вычисления матрицы вида?

data.py

# coding: utf-8

# In[1]:


import numpy as np
import tensorflow
import pandas as pd


# In[2]:


def sample_points(low=0,high=100,N=10000):
    '''how to stack 1darray to 2darray'''
    x = np.random.randint(low,high,N)
    y = x*2
    z = np.random.randint(low,high,N)
    L = np.full((1,N),1,dtype=np.int)
    return np.array([x,y,z,[1 for i in range(N)]])


# In[3]:


xyzL_h=sample_points()
print(xyzL_h)


# In[4]:


def save_points(xyzL,filename='xyz.txt',homogenous=False):
    '''
    save 
    xyz  to ?.csv file, or xyz1 to ?.csv file
    '''
    xyzL_v = np.transpose(xyzL,(1,0))
    #print(xyzL_v.shape)
    df = pd.DataFrame(xyzL_v)
    df.columns=['x','y','z','1']
    if not homogenous:
        df.iloc[:,0:3].to_csv(filename,index=False,header=False)
    else:
        df.to_csv(filename,index=False,header=False)


# In[5]:


save_points(xyzL_h,filename='raw_xyz.txt')


# In[6]:


import matplotlib.pyplot as plt
get_ipython().magic('matplotlib inline')


# In[7]:


def show_data(xyzL_h):
    plt.plot(xyzL_h[0],xyzL_h[1],'r-')
    plt.plot(xyzL_h[0],xyzL_h[2],'b+')


# In[8]:


show_data(xyzL_h)


# In[9]:


class Screw():
    '''
    based on screw thoery of screw motion and transformation matrix 
    '''
    def __init__(self,lib):
        self._lib = lib
        self._p = None
        self._v = None
        self._w = None
        self._seta = 0
        self._m = None

    def dot(self,x,y):
        if self._lib.__name__ is 'numpy':
            return self._lib.dot(x,y)
        if self._lib.__name__ is 'tensorflow':
            #print("rank of y: ",y.shape,len(y.shape))
            if len(y.shape)==1:
                y = tensorflow.reshape(y,(-1,1))
            return self._lib.matmul(x,y)

    def cross(self,x,y):
        '''
        cross product of two vertors
        '''
        x_n = self._lib.reshape(x,(1,3))
        y_n = self._lib.reshape(y,(1,3))
        return self._lib.cross(x_n,y_n)

    def transform(self,cloud):
        '''
        cloud with shape of [4,None]
        '''
        return self.dot(self._m,cloud)

    def I(self,n):
        '''
        unit matrix
        '''
        if self._lib.__name__ == 'numpy':
            return self._lib.identity(n)
        elif self._lib.__name__ == 'tensorflow':
            return self._lib.matrix_diag([1.0 for i in range(n)])

    def w_from_rad(self,angle):
        '''
        define vector by sphere coordinate
        first of angle(alpha): is radian angle between direction vector and Z axis
        second of angle(beta): is radian angle between X-O-Y projection of vector and X axis
        '''
        lib = self._lib
        alpha,beta = angle[0],angle[1]
        k = lib.cos(alpha)
        i = lib.sin(alpha)*lib.cos(beta)
        j = lib.sin(alpha)*lib.sin(beta)
        if lib.__name__ == 'numpy':
            ijk = lib.array([i,j,k])
        elif lib.__name__ == 'tensorflow':
            ijk = lib.stack([i,j,k])
        self._w = ijk
        return ijk

    def wx(self,w):
        '''                
        #skew symmetric matrix of twist component[0:3] w 
        '''
        if self._lib.__name__ is 'numpy':
            m = self._lib.zeros((3,3))
            m[0,1]=-w[2]
            m[0,2]=w[1]
            m[1,0]=w[2]
            m[1,2]=-w[0]
            m[2,0]=-w[1]
            m[2,1]=w[0]
        elif self._lib.__name__ is 'tensorflow':
            m = self._lib.stack([[0,-w[2],w[1]],[w[2],0,-w[0]],[-w[1],w[0],0]])
           # m = self._lib.reshape(m,(3,3))
        return m

    def v(self,w,p):
        '''
        # twist component[3:6] v
        '''
        _v = self.dot(self.wx(w),p)
        self._v = -_v
        return -_v


    def tm(self,w,p,seta):
        '''
        transformation matrix out of screw theory
        this matrix is used for right multiplication operation of point cloud
        '''
        lib = self._lib
        if lib.__name__ is 'numpy':
            w = lib.array(w)
            p = lib.array(p)
        if lib.__name__ is 'tensorflow':
            w = lib.to_float(lib.Variable(w))
            p = lib.to_float(lib.Variable(p))            
        _v = self.v(w,p)
        _wx = self.wx(w)
        self._w = w
        self._v = _v
        self._p = p
        m33 = self.I(3)+_wx*lib.sin(seta)+self.dot(_wx,_wx)*(1-lib.cos(seta))
        mm = self.I(3)-m33
        #+w*self.dot(w,_v)*seta #'w*np.dot(w,_v)*seta' should be 0, when no translation movement 
        vv = self._lib.reshape(self.cross(w,_v),(-1,1))

        t = self.dot(mm,vv)[:,0]
        if lib.__name__ is 'numpy':
            m44 = self.I(4)
            m44[0:3,0:3]=m33
            m44[0:3,3]=t
            self._m = m44#lib.transpose(m44,(1,0))
        elif lib.__name__ is 'tensorflow':
            #print('t:',t)
            m34 = self._lib.stack([m33[:,0],m33[:,1],m33[:,2],t],axis=1)
            m44 = self._lib.stack([m34[0],m34[1],m34[2],[0.0,0.0,0.0,1.0]])
            self._m = m44#lib.transpose(m44,perm=[1,0])
        return self._m


# In[10]:


screw = Screw(np)
q,seta=[0.0,100.0,0.0],np.pi/2
w =screw.w_from_rad([np.pi/4,np.pi/4])
res = screw.tm(w,q,seta)


# In[11]:


print(res)


# In[12]:


class CloudProcess():
    def __init__(self,cloud,screw):
        self._cloud = cloud
        self._matrix = screw
    def transform(self):
        new_cloud = self._matrix.transform(self._cloud)
        return new_cloud


# In[13]:


op = CloudProcess(xyzL_h,screw)
xyzL_h_new = op.transform()
save_points(xyzL_h_new,filename='new_xyz.txt')


# In[14]:


screw = Screw(tensorflow)
clouds = tensorflow.placeholder(dtype=tensorflow.float32,shape=[4,None],name='XYZL')
p = tensorflow.Variable([0,100,0],name='PointOfAxis',dtype=tensorflow.float32)
angle = tensorflow.Variable([np.pi/4,np.pi/4],name='DirectionAngle',dtype=tensorflow.float32)
seta = tensorflow.constant(np.pi/2,dtype=tensorflow.float32)
w = screw.w_from_rad(angle)
m = screw.tm(w,p,seta)
op = CloudProcess(clouds,screw)
xyzL_new = op.transform()


# In[15]:


init = tensorflow.global_variables_initializer()
with tensorflow.Session() as sess:
    sess.run(init)
    res=sess.run(xyzL_new,feed_dict={clouds:xyzL_h})
    save_points(res,filename='new_xyz_tf.txt')


# In[ ]:

test.py

# coding: utf-8

# In[1]:


import tensorflow
import pandas as pd
import numpy as np


# In[2]:


def load_pointXYZ(filename):
    df = pd.read_csv(filename,header=None)
    df['1']=1
    df.columns = ['X','Y','Z','1']
    return df


# In[3]:


df1 = load_pointXYZ('raw_xyz.txt')
df2 = load_pointXYZ('new_xyz_tf.txt')


# In[4]:


X = df1.as_matrix().T
Y = df2.as_matrix().T
print(X)
print(Y)


# In[5]:


class Screw():
    '''
    based on screw thoery of screw motion and transformation matrix 
    '''
    def __init__(self,lib):
        self._lib = lib
        self._p = None
        self._v = None
        self._w = None
        self._seta = 0
        self._m = None

    def dot(self,x,y):
        if self._lib.__name__ is 'numpy':
            return self._lib.dot(x,y)
        if self._lib.__name__ is 'tensorflow':
            #print("rank of y: ",y.shape,len(y.shape))
            if len(y.shape)==1:
                y = tensorflow.reshape(y,(-1,1))
            return self._lib.matmul(x,y)

    def cross(self,x,y):
        '''
        cross product of two vertors
        '''
        x_n = self._lib.reshape(x,(1,3))
        y_n = self._lib.reshape(y,(1,3))
        return self._lib.cross(x_n,y_n)

    def transform(self,cloud):
        '''
        cloud with shape of [4,None]
        '''
        return self.dot(self._m,cloud)

    def I(self,n):
        '''
        unit matrix
        '''
        if self._lib.__name__ == 'numpy':
            return self._lib.identity(n)
        elif self._lib.__name__ == 'tensorflow':
            return self._lib.matrix_diag([1.0 for i in range(n)])

    def w_from_rad(self,angle):
        '''
        define vector by sphere coordinate
        first of angle(alpha): is radian angle between direction vector and Z axis
        second of angle(beta): is radian angle between X-O-Y projection of vector and X axis
        '''
        lib = self._lib
        alpha,beta = angle[0],angle[1]
        k = lib.cos(alpha)
        i = lib.sin(alpha)*lib.cos(beta)
        j = lib.sin(alpha)*lib.sin(beta)
        if lib.__name__ == 'numpy':
            ijk = lib.array([i,j,k])
        elif lib.__name__ == 'tensorflow':
            ijk = lib.stack([i,j,k])
        self._w = ijk
        return ijk

    def wx(self,w):
        '''                
        #skew symmetric matrix of twist component[0:3] w 
        '''
        if self._lib.__name__ is 'numpy':
            m = self._lib.zeros((3,3))
            m[0,1]=-w[2]
            m[0,2]=w[1]
            m[1,0]=w[2]
            m[1,2]=-w[0]
            m[2,0]=-w[1]
            m[2,1]=w[0]
        elif self._lib.__name__ is 'tensorflow':
            m = self._lib.stack([[0,-w[2],w[1]],[w[2],0,-w[0]],[-w[1],w[0],0]])
           # m = self._lib.reshape(m,(3,3))
        return m

    def v(self,w,p):
        '''
        # twist component[3:6] v
        '''
        _v = self.dot(self.wx(w),p)
        self._v = -_v
        return -_v


    def tm(self,w,p,seta):
        '''
        transformation matrix out of screw theory
        this matrix is used for right multiplication operation of point cloud
        '''
        lib = self._lib
        if lib.__name__ is 'numpy':
            w = lib.array(w)
            p = lib.array(p)
        if lib.__name__ is 'tensorflow':
            w = lib.to_float(lib.Variable(w))
            p = lib.to_float(lib.Variable(p))            
        _v = self.v(w,p)
        _wx = self.wx(w)
        self._w = w
        self._v = _v
        self._p = p
        m33 = self.I(3)+_wx*lib.sin(seta)+self.dot(_wx,_wx)*(1-lib.cos(seta))
        mm = self.I(3)-m33
        #+w*self.dot(w,_v)*seta #'w*np.dot(w,_v)*seta' should be 0, when no translation movement 
        vv = self._lib.reshape(self.cross(w,_v),(-1,1))

        t = self.dot(mm,vv)[:,0]
        if lib.__name__ is 'numpy':
            m44 = self.I(4)
            m44[0:3,0:3]=m33
            m44[0:3,3]=t
            self._m = m44#lib.transpose(m44,(1,0))
        elif lib.__name__ is 'tensorflow':
            m34 = self._lib.stack([m33[:,0],m33[:,1],m33[:,2],t],axis=1)
            m44 = self._lib.stack([m34[0],m34[1],m34[2],[0.0,0.0,0.0,1.0]])
            self._m = m44#lib.transpose(m44,perm=[1,0])
        return self._m


# In[6]:


class CloudProcess():
    def __init__(self,cloud,screw):
        self._cloud = cloud
        self._matrix = screw
    def transform(self):
        new_cloud = self._matrix.transform(self._cloud)
        return new_cloud


# In[7]:


screw = Screw(tensorflow)
cld_1 = tensorflow.placeholder(dtype=tensorflow.float32,shape=[4,None],name='XYZL')
cld_2 = tensorflow.placeholder(dtype=tensorflow.float32,shape=[4,None],name='XYZL')
p = tensorflow.Variable([0,50,2],name='PointOfAxis',dtype=tensorflow.float32)
angle = tensorflow.Variable([0.3,0.2],name='DirectionAngle',dtype=tensorflow.float32)
seta = tensorflow.constant(np.pi/2,dtype=tensorflow.float32)
w = screw.w_from_rad(angle)
m = screw.tm(w,p,seta)
op = CloudProcess(cld_1,screw)
xyzL_new = op.transform()
loss = tensorflow.reduce_sum(tensorflow.abs(xyzL_new-cld_2))
optimizer = tensorflow.train.GradientDescentOptimizer(learning_rate=0.1)
model = optimizer.minimize(loss)

init = tensorflow.global_variables_initializer()
with tensorflow.Session() as sess:
    sess.run(init)
    for i in range(10):
        res = sess.run(model,feed_dict={cld_1:X,cld_2:Y})
        print('model tranning:',res)
    res = sess.run(loss,feed_dict={cld_1:X,cld_2:Y})
    print('loss:',res)
    print(angle.eval())
    print(p.eval() )
...