for Robot Artificial Inteligence

2. TF tutorial

|

TF_Example.py

#!/usr/bin/env python  
import rospy

import numpy

import tf
import tf2_ros
import geometry_msgs.msg

def publish_transforms():
    t1 = geometry_msgs.msg.TransformStamped()
    t1.header.stamp = rospy.Time.now()
    t1.header.frame_id = "world"
    t1.child_frame_id = "F1"
    t1.transform.translation.x = 1.0
    t1.transform.translation.y = 1.0
    t1.transform.translation.z = 0.0
    q1 = tf.transformations.quaternion_from_euler(1.0, 1.0, 1.0)
    t1.transform.rotation.x = q1[0]
    t1.transform.rotation.y = q1[1]
    t1.transform.rotation.z = q1[2]
    t1.transform.rotation.w = q1[3]
    br.sendTransform(t1)

    t2 = geometry_msgs.msg.TransformStamped()
    t2.header.stamp = rospy.Time.now()
    t2.header.frame_id = "F1"
    t2.child_frame_id = "F2"
    t2.transform.translation.x = 1.0
    t2.transform.translation.y = 0.0
    t2.transform.translation.z = 0.0
    q2 = tf.transformations.quaternion_about_axis(1.57, (1,0,0))
    t2.transform.rotation.x = q2[0]
    t2.transform.rotation.y = q2[1]
    t2.transform.rotation.z = q2[2]
    t2.transform.rotation.w = q2[3]
    br.sendTransform(t2)

    T1 = numpy.dot(tf.transformations.translation_matrix((1.0, 1.0, 0.0)),
                   tf.transformations.quaternion_matrix(q1))
    T1_inverse = tf.transformations.inverse_matrix(T1)

    t3 = geometry_msgs.msg.TransformStamped()
    t3.header.stamp = rospy.Time.now()
    t3.header.frame_id = "F3"
    t3.child_frame_id = "F4"
    tr3 = tf.transformations.translation_from_matrix(T1_inverse)
    t3.transform.translation.x = tr3[0]
    t3.transform.translation.y = tr3[1]
    t3.transform.translation.z = tr3[2]
    q3 = tf.transformations.quaternion_from_matrix(T1_inverse)
    t3.transform.rotation.x = q3[0]
    t3.transform.rotation.y = q3[1]
    t3.transform.rotation.z = q3[2]
    t3.transform.rotation.w = q3[3]
    br.sendTransform(t3)

    T2 = numpy.dot(tf.transformations.translation_matrix((1.0, 0.0, 0.0)),
                   tf.transformations.quaternion_matrix(q2))
    T2_inverse = tf.transformations.inverse_matrix(T2)

    t4 = geometry_msgs.msg.TransformStamped()
    t4.header.stamp = rospy.Time.now()
    t4.header.frame_id = "F2"
    t4.child_frame_id = "F3"
    tr4 = tf.transformations.translation_from_matrix(T2_inverse)
    t4.transform.translation.x = tr4[0]
    t4.transform.translation.y = tr4[1]
    t4.transform.translation.z = tr4[2]
    q4 = tf.transformations.quaternion_from_matrix(T2_inverse)
    t4.transform.rotation.x = q4[0]
    t4.transform.rotation.y = q4[1]
    t4.transform.rotation.z = q4[2]
    t4.transform.rotation.w = q4[3]
    br.sendTransform(t4)

if __name__ == '__main__':
    rospy.init_node('tf2_examples')

    br = tf2_ros.TransformBroadcaster()
    rospy.sleep(0.5)

    while not rospy.is_shutdown():
        publish_transforms()
        rospy.sleep(0.05)

second Tutorial

second Tutorial.py

#!/usr/bin/env python  
import rospy

import numpy

import tf
import tf2_ros
import geometry_msgs.msg

def message_from_transform(T):
	msg = geometry_msgs.msg.Transform()
	q = tf.transformations.quaternion_from_matrix(T)
	p = tf.transformations.translation_from_matrix(T)
	msg.translation.x = p[0]
	msg.translation.y = p[1]
	msg.translation.z = p[2]
	msg.rotation.x = q[0]
	msg.rotation.y = q[1]
	msg.rotation.z = q[2]
	msg.rotation.w = q[3]
	return msg

def publish_transforms():
    object_transform = geometry_msgs.msg.TransformStamped()
    object_transform.header.stamp = rospy.Time.now()
    object_transform.header.frame_id = "base_frame"
    object_transform.child_frame_id = "object_frame"
    T_1 = tf.transformations.concatenate_matrices(tf.transformations.quaternion_matrix(tf.transformations.quaternion_from_euler(0.79,0.0,0.79)),
    tf.transformations.translation_matrix([0.0,1.0,1.0]))
    object_transform.transform = message_from_transform(T_1)
    br.sendTransform(object_transform)

    robot_transform = geometry_msgs.msg.TransformStamped()
    robot_transform.header.stamp = rospy.Time.now()
    robot_transform.header.frame_id = "base_frame"
    robot_transform.child_frame_id = "robot_frame"
    T_2 = tf.transformations.concatenate_matrices(tf.transformations.quaternion_matrix(tf.transformations.quaternion_from_euler(0.0,0.0,1.5)),
    tf.transformations.translation_matrix([0.0,-1.0,0.0]))
    robot_transform.transform = message_from_transform(T_2)
    br.sendTransform(robot_transform)

    camera_transform = geometry_msgs.msg.TransformStamped()
    camera_transform.header.stamp = rospy.Time.now()
    camera_transform.header.frame_id = "robot_frame"
    camera_transform.child_frame_id = "camera_frame"
    T_3 = tf.transformations.concatenate_matrices(tf.transformations.translation_matrix([0.0,0.1,0.1]),
    tf.transformations.quaternion_matrix(tf.transformations.quaternion_from_euler(0.,0.,0.)))

    Tr = tf.transformations.concatenate_matrices(tf.transformations.inverse_matrix(T_3),tf.transformations.inverse_matrix(T_2),T_1)
    p = tf.transformations.translation_from_matrix(Tr)
    dir = numpy.cross(numpy.array([1,0,0]),numpy.array([p[0],p[1],p[2]]))
    theta = numpy.arccos(p[0]/numpy.linalg.norm(numpy.array([p[0],p[1],p[2]])))
    T_3 = tf.transformations.concatenate_matrices(T_3,tf.transformations.rotation_matrix(theta,dir))
    #p = tf.transformations.translation_from_matrix(T_3)
    #theta = numpy.arccos(p[0]/numpy.linalg.norm(numpy.array([p[0],p[1],p[2]])))
    #T_3 = tf.transformations.concatenate_matrices(T_3,tf.transformations.quaternion_matrix(tf.transformations.quaternion_from_euler(theta,0.0,0.0)))

    camera_transform.transform = message_from_transform(T_3)
    br.sendTransform(camera_transform)

    # co = geometry_msgs.msg.TransformStamped()
    # co.header.stamp = rospy.Time.now()
    # co.header.frame_id = "camera_frame"
    # co.child_frame_id = "asd"
    # Tr = tf.transformations.concatenate_matrices(tf.transformations.inverse_matrix(T_3),tf.transformations.inverse_matrix(T_2),T_1)
    # #p = tf.transformations.translation_from_matrix(Tr)
    # #theta = numpy.arccos(p[0]/numpy.linalg.norm(numpy.array([p[0],p[1],p[2]])))
    # co.transform = message_from_transform(Tr)
    # #Tr = tf.transformations.concatenate_matrices(Tr,tf.transformations.quaternion_matrix(tf.transformations.quaternion_from_euler(theta,0.0,0.0)))
    # br.sendTransform(co)



if __name__ == '__main__':
    rospy.init_node('project2_solution')

    br = tf2_ros.TransformBroadcaster()
    rospy.sleep(0.5)

    while not rospy.is_shutdown():
        publish_transforms()
        rospy.sleep(0.05)

Third Tutorial

Thrid Tutorial.py

#!/usr/bin/env python  
import rospy

import numpy

import tf
import tf2_ros
import geometry_msgs.msg

def message_from_transform(T):

    msg = geometry_msgs.msg.Transform()
    q = tf.transformations.quaternion_from_matrix(T)
    translation = tf.transformations.translation_from_matrix(T)
    msg.translation.x = translation[0]
    msg.translation.y = translation[1]
    msg.translation.z = translation[2]
    msg.rotation.x = q[0]
    msg.rotation.y = q[1]
    msg.rotation.z = q[2]
    msg.rotation.w = q[3]

    return msg

def publish_transforms():
    object_transform = geometry_msgs.msg.TransformStamped()
    object_transform.header.stamp = rospy.Time.now()
    object_transform.header.frame_id = "base_frame"
    object_transform.child_frame_id = "object_frame"
    q1 = tf.transformations.quaternion_from_euler(0.79,0.0,0.79)
    T1 = numpy.dot(tf.transformations.quaternion_matrix(q1),tf.transformations.translation_matrix((0.0,1.0,1.0)))
    object_transform.transform = message_from_transform(T1)
    br.sendTransform(object_transform)

    robot_transform = geometry_msgs.msg.TransformStamped()
    robot_transform.header.stamp = rospy.Time.now()
    robot_transform.header.frame_id = "base_frame"
    robot_transform.child_frame_id = "robot_frame"
    q2= tf.transformations.quaternion_from_euler(0,0,1.5)
    T2 = numpy.dot(tf.transformations.quaternion_matrix(q2), tf.transformations.translation_matrix((0.0,-1.0,0.0)))
    robot_transform.transform = message_from_transform(T2)
    br.sendTransform(robot_transform)

    camera_transform = geometry_msgs.msg.TransformStamped()
    camera_transform.header.stamp = rospy.Time.now()
    camera_transform.header.frame_id = "robot_frame"
    camera_transform.child_frame_id = "camera_frame"

    #T1 is TBO
    #T2 is TBR

    TRC = tf.transformations.translation_matrix((0.0,0.1,0.1))
    TBC = numpy.dot(T2,TRC)
    TCO = numpy.dot(tf.transformations.inverse_matrix(TBC),T1)
    v1_n = tf.transformations.translation_from_matrix(TCO)
    v1 = v1_n/numpy.linalg.norm(v1_n) # vector or matrix norm
    x = [1,0,0]
    w = numpy.cross(x,v1) #cross product
    rt = numpy.arccos(numpy.dot(x,v1_n))
    q3 = tf.transformations.quaternion_about_axis(rt,w)
    T3 = numpy.dot(TRC,tf.transformations.quaternion_matrix(q3))
    camera_transform.transform = message_from_transform(T3)
    br.sendTransform(camera_transform)

if __name__ == '__main__':
    rospy.init_node('project2_solution')

    br = tf2_ros.TransformBroadcaster()
    rospy.sleep(0.5)

    while not rospy.is_shutdown():
        publish_transforms()
        rospy.sleep(0.05)

Comment  Read more

2. Optimization application

|

Optimization in Signal and Image processing

Model arising in compressive sensing and imaging sciences

  • Separable(可分开的) Structures of problems

  • Convexity and easy subproblems(子问题).
  • Often involving large, nonsmooth convex functions with separable structure.
  • First-order method can be very slow for producing high accuracy solutions, but also share many advantages:
    • Non-differentiability
    • Use minimal information, e.g.,(f;∇f)
    • Often lead to very simple and “cheap” iterative schemes
    • Complexity/iteration mildly dependent (e.g., linear) in problem’s dimension
    • Suitable when high accuracy is not crucial [in many large scale applications, the data is anyway corrupted or known only roughly

Example: compressive sensing(Kernel 같은 개념)

  • Goal: Recover sparse signal from very few linear measurements.
  • Basis pursuit model:

Compressive Sensing

  • Find the sparest solution
    • Given n=256, m=128.
      • n is original signal
      • m is output value
    • A = randn(m,n); u = sprandn(n, 1, 0.1); b = A*u;

Sparse((흔히 넓은 지역에 분포된 정도가) 드문, (밀도가) 희박한) recovery models

  • Basis pursuit model : nonunique solution
  • Ax = b = {$x^*$+N(A)} => ∀ v ∈ N(A), Av = 0
    • N(A) is non-space
  • μ is given parameter(constant variable)

Sparsity((흔히 넓은 지역에 분포된 정도가) 드문, (밀도가) 희박한) under a basis

  • Given a reprenting basis or dictionary, we want to recover a signal which is sparse under this basis.
  • Two types of models:

  • Commonly used dictionaries include both analytic and trained ones.
    • analytic bases: Id, DCT, wavelets, curvelets, gabor, etc., also their combinations; they have analytic properties, often easy to compute (for example, multiplying a vector takes O(n log n) instead of O($n^2$))
    • data driven: can also be numerically learned from training data or partial signal, patches (offline or online learning).
    • they can be orthogonal, frame, normalized or general.
    • If Φ is orthogonal (or just invertible), the analyse based model is equivalent to synthesis based one by changing the variable. When it is not, the model is harder to solve.

Parallel MRI reconstruction

Nonconvex sparsity models

  • min f (x) + h(x)
  • where either f or h is nonconvex or both are nonconvex. Some popular models:
    • Nonlinear least square for nonlinear inverse problems f (x) = ‖F(x) − y‖^2.
    • h(x) = ‖x‖_p, where 0 <= p < 1 or rank constraint.
    • Alternating minimization methods in many applications, such as blind deconvolution, matrix factorization, or dictionary learning etc.

Optimization of Matrices

The Netflix problem

Matrix Rank Minimization

Video separation

Sparse and low-rank matrix separation

  • Given a matrix M, we want to find a low rank matrix W and a sparse matrix E, so that W + E = M.
  • Convex approximation:

  • Robust PCA (reduce dimension)

Extension

Correlation Matrices

  • A correlation(相互关系) matrix satisfies:
    • X = X^T , X_ii = 1, i = 1, . . . , n, X >= 0.
  • Example: (low-rank) nearest correlation matrix estimation

Optimization in Machine learning

Why Optimization in Machine Learning?

Loss functions in neural network

convolution operator

Optimization algorithms in Deep learning: stochastic gradient methods

  • pytorch/caffe2: adadelta, adagrad, adam, nesterov, rmsprop, YellowFin https://github.com/pytorch/pytorch/tree/master/caffe2/sgd
  • pytorch/torch: sgd, asgd, adagrad, rmsprop, adadelta, adam, adamax https://github.com/pytorch/pytorch/tree/master/torch/optim
  • tensorflow: Adadelta, AdagradDA, Adagrad, ProximalAdagrad, Ftrl, Momentum, adam, Momentum, CenteredRMSProp https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/kernels/training_ops.cc

Generative adversarial network (GAN)

Reinforcement Learning

Optimization in Finance

Portfolio optimization

Review of Risk Measures

Reference

Optimization method - Standford University

Comment  Read more

2. Frame Operation

|

Frame Operation

  • How to find the position of that point from the persepective of Frame 2 or 1 ?
    • Translation, Rotation, Roto(Rotation and Translation)

1. Frame Translation

2. Frame Rotation

3. Composition of Rotation

4. Properties of Rotations

Comment  Read more

16. DeepLearning Overview

|

Deep Learning

  • Deep Learning is a name for Neural network
  • in its simplest form, it’s just a bunch of logistic regressions stacked together
  • layers in between input and output are called hidden leyers
  • we call this network a “feedforward neural network”
  • Nonlinear activation functions (f) make it a nonlinear function approximator

Training

  • Despite the complexity, training hasn’t changed since logistic regression, we still just do gradient descent
  • problem: not as robust with deep networks. sensitive to hyperparameters:
    • Learning rate, # hidden units, # hidden layers, activation fcn, optimizer(AdaGrad, RMSprop, Adam, etc) which have their own hyperparameters
    • we won’t know what works until we try

Feature Engineering

  • As with all ML models, input is a feature vector x
  • Neural networks are nice because they save us from having to do lots of manual feature engineering
  • Nonlinear characteristics of NNs have been shown to learn features automatically and hierarchically between layers
  • Ex
    • layer 1 : edges
    • layer 2 : groups of edges
    • layer 3 : eye, nose, lips, ears
    • layer 4 : entire faces

Working with images

  • as a human, one of our main sensory(感觉的) input
  • as a robot navigating the real-world, images are also one of our main sensory input
  • images also make up states in video games
  • thus we’ll need to know how to work with images to play Atary environments in Open Gym
  • Luckily we have tools for this: Convolutional neural network
  • Does 2-D Convolutions before the fully-connected layers
  • (All layers in a feedforward network are fully-connected)
  • Convolutional Layers have filter which are mush smaller than the image
  • Also, instead of working with 1-D feature vectors, we work with the 2-D image directly(3-D if color)
  • idea: slide kernal/filter across the image and multiply by a patch to get output
  • Aside from the sliding, it works exactly like a fully-connected layer
  • Multiplication and nonlinear activation
  • Concept of shared weights
  • smaller # of parameters, takes up less space and trains faster

Working with sequences

  • In RL, not only are we interested in images, but sequences too
  • Main tool: recurrent neural networks
  • Episode is made of sequence of states, actions and rewards
  • Any Network where a node loops back to an earlier node is recurrent
  • Typically don’t think of individual recurrent connections, but recurrent layers or units, e.g LSTM or GRU
  • we think of them as black boxs: input -> box -> outpu
  • since output depends on previous inputs, it means the black box has “memory”
  • useful because we can make decisions based on previous frames/ states

Classification vs Regression

  • we mostly focused on multi-class classification
  • softmax on multiple output nodes + cross entropy
  • in RL we want to predict a real-value scalar(the value function)
  • one output node + squared error

Reference:

Artificial Intelligence Reinforcement Learning

Advance AI : Deep-Reinforcement Learning

Cutting-Edge Deep-Reinforcement Learning

Comment  Read more

1. essence of e

|

Comment  Read more