text: '''
  Basic Point2Sequence classification model
  '''
  import os
  import sys
  BASE_DIR = os.path.dirname(__file__)
  sys.path.append(BASE_DIR)
  sys.path.append(os.path.join(BASE_DIR, 'utils'))
  import tensorflow as tf
  import numpy as np
  import models.Point2Sequence.utils.tf_util as tf_util
  from pointnet_util import pointnet_sa_module, point2sequence_module, pointnet_sa_module_density
  from models.perturbation import perturbation_point_xyz, perturbation_point_uniform
  
  def placeholder_inputs(batch_size, num_point):
      pointclouds_pl = tf.placeholder(tf.float32, shape=(batch_size, num_point, 3))
      labels_pl = tf.placeholder(tf.int32, shape=(batch_size))
      return pointclouds_pl, labels_pl
  
  BANDW = 0.05
  def get_model(point_cloud, is_training, num_class, bn_decay=None):
      """ Classification Point2Sequence, input is BxNx3, output BxCLASSES """
      batch_size = point_cloud.get_shape()[0].value
      end_points = {}
  
      pert_point_cloud, perturbation = perturbation_point_xyz(point_cloud)
  
      l0_xyz = pert_point_cloud
      l0_points = None
  
      # Set abstraction layers
      l1_xyz, l1_points = point2sequence_module(l0_xyz, l0_points, 384, [16,32,64,128], [[32,64,128], [64,64,128], [64,64,128], [128,128,128]], 128, 128, is_training, bn_decay, scope='layer1')
      l2_xyz, l2_points, _ = pointnet_sa_module_density(l1_xyz, l1_points, npoint=None, radius=1.6, sigma=16*BANDW ,nsample=None, mlp=[256,512,1024], mlp2=None, group_all=True, is_training=is_training, bn_decay=bn_decay, scope='layer2')
  
      # Fully connected layers
      net = tf.reshape(l2_points, [batch_size, -1])
      net = tf_util.fully_connected(net, 512, bn=True, is_training=is_training, scope='fc1', bn_decay=bn_decay)
      net = tf_util.dropout(net, keep_prob=0.4, is_training=is_training, scope='dp1')
      net = tf_util.fully_connected(net, 256, bn=True, is_training=is_training, scope='fc2', bn_decay=bn_decay)
      net = tf_util.dropout(net, keep_prob=0.4, is_training=is_training, scope='dp2')
      net = tf_util.fully_connected(net, num_class, activation_fn=None, scope='fc3')
  
      return net, perturbation, end_points
  
  
  def get_loss(pred, label, end_points):
      """ pred: B*NUM_CLASSES,
          label: B, """
      loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=pred, labels=label)
      classify_loss = tf.reduce_mean(loss)
      tf.summary.scalar('classify loss', classify_loss)
      tf.add_to_collection('losses', classify_loss)
      return classify_loss
  
  def get_adversarial_loss(pred, targert_label, pert, lam =1.0):
      loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = pred, labels = targert_label)
      classify_loss = tf.reduce_mean(loss)
      #pert_loss = tf.norm(pert)
      pert_loss =tf.square(tf.norm(pert,ord = 'euclidean'))
  
      return classify_loss + lam * pert_loss, classify_loss,  pert_loss
  
  
  if __name__=='__main__':
      with tf.Graph().as_default():
          inputs = tf.zeros((32,1024,3))
          net, _ = get_model(inputs, tf.constant(True))
          print(net)
  