PointNet大雜燴

2021-01-14 阿chai帶你學AI

人工智慧技術乾貨請關注AIZOO、Jack Cui


機器人與自動駕駛課程請關注深藍學院


我是來自山區、樸實、不偷電瓶的AI算法工程師阿chai,給大家分享人工智慧、自動駕駛、機器人、3D感知相關的知識

阿chai之前介紹了很多有關3D點雲的知識,包括PointNet,今天給大家整理一下不同框架下PointNet網絡部分的Demo。

PointNet相關介紹請前往Pointnet詳解+百度飛槳復現算法。

1.TensorFlow1.x

import tensorflow as tf
import numpy as np
import math
import sys
import os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
sys.path.append(os.path.join(BASE_DIR, '../utils'))

import tf_util

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

def get_model(point_cloud, is_training, bn_decay=None):
    
    batch_size = point_cloud.get_shape()[0].value
    num_point = point_cloud.get_shape()[1].value
    end_points = {}
    input_image = tf.expand_dims(point_cloud, -1)
    
    net = tf_util.conv2d(input_image, 64, [1,3],
                         padding='VALID', stride=[1,1],
                         bn=True, is_training=is_training,
                         scope='conv1', bn_decay=bn_decay)
    net = tf_util.conv2d(net, 64, [1,1],
                         padding='VALID', stride=[1,1],
                         bn=True, is_training=is_training,
                         scope='conv2', bn_decay=bn_decay)
    net = tf_util.conv2d(net, 64, [1,1],
                         padding='VALID', stride=[1,1],
                         bn=True, is_training=is_training,
                         scope='conv3', bn_decay=bn_decay)
    net = tf_util.conv2d(net, 128, [1,1],
                         padding='VALID', stride=[1,1],
                         bn=True, is_training=is_training,
                         scope='conv4', bn_decay=bn_decay)
    net = tf_util.conv2d(net, 1024, [1,1],
                         padding='VALID', stride=[1,1],
                         bn=True, is_training=is_training,
                         scope='conv5', bn_decay=bn_decay)

    net = tf_util.max_pool2d(net, [num_point,1],
                             padding='VALID', scope='maxpool')
    
    net = tf.reshape(net, [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.fully_connected(net, 256, bn=True, is_training=is_training,
                                  scope='fc2', bn_decay=bn_decay)
    net = tf_util.dropout(net, keep_prob=0.7, is_training=is_training,
                          scope='dp1')
    net = tf_util.fully_connected(net, 40, activation_fn=None, scope='fc3')

    return net, end_points

def get_loss(pred, label, end_points):
    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)
    return classify_loss

if __name__=='__main__':
    with tf.Graph().as_default():
        inputs = tf.zeros((32,1024,3))
        outputs = get_model(inputs, tf.constant(True))
        print(outputs)

2.Pytorch

from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.utils.data
from torch.autograd import Variable
import numpy as np
import torch.nn.functional as F

class STN3d(nn.Module):
    def __init__(self):
        super(STN3d, self).__init__()
        self.conv1 = torch.nn.Conv1d(3, 64, 1)
        self.conv2 = torch.nn.Conv1d(64, 128, 1)
        self.conv3 = torch.nn.Conv1d(128, 1024, 1)
        self.fc1 = nn.Linear(1024, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc3 = nn.Linear(256, 9)
        self.relu = nn.ReLU()

        self.bn1 = nn.BatchNorm1d(64)
        self.bn2 = nn.BatchNorm1d(128)
        self.bn3 = nn.BatchNorm1d(1024)
        self.bn4 = nn.BatchNorm1d(512)
        self.bn5 = nn.BatchNorm1d(256)

    def forward(self, x):
        batchsize = x.size()[0]
        x = F.relu(self.bn1(self.conv1(x)))
        x = F.relu(self.bn2(self.conv2(x)))
        x = F.relu(self.bn3(self.conv3(x)))
        x = torch.max(x, 2, keepdim=True)[0]
        x = x.view(-1, 1024)

        x = F.relu(self.bn4(self.fc1(x)))
        x = F.relu(self.bn5(self.fc2(x)))
        x = self.fc3(x)

        iden = Variable(torch.from_numpy(np.array([1,0,0,0,1,0,0,0,1]).astype(np.float32))).view(1,9).repeat(batchsize,1)
        if x.is_cuda:
            iden = iden.cuda()
        x = x + iden
        x = x.view(-1, 3, 3)
        return x

class STNkd(nn.Module):
    def __init__(self, k=64):
        super(STNkd, self).__init__()
        self.conv1 = torch.nn.Conv1d(k, 64, 1)
        self.conv2 = torch.nn.Conv1d(64, 128, 1)
        self.conv3 = torch.nn.Conv1d(128, 1024, 1)
        self.fc1 = nn.Linear(1024, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc3 = nn.Linear(256, k*k)
        self.relu = nn.ReLU()

        self.bn1 = nn.BatchNorm1d(64)
        self.bn2 = nn.BatchNorm1d(128)
        self.bn3 = nn.BatchNorm1d(1024)
        self.bn4 = nn.BatchNorm1d(512)
        self.bn5 = nn.BatchNorm1d(256)

        self.k = k

    def forward(self, x):
        batchsize = x.size()[0]
        x = F.relu(self.bn1(self.conv1(x)))
        x = F.relu(self.bn2(self.conv2(x)))
        x = F.relu(self.bn3(self.conv3(x)))
        x = torch.max(x, 2, keepdim=True)[0]
        x = x.view(-1, 1024)

        x = F.relu(self.bn4(self.fc1(x)))
        x = F.relu(self.bn5(self.fc2(x)))
        x = self.fc3(x)

        iden = Variable(torch.from_numpy(np.eye(self.k).flatten().astype(np.float32))).view(1,self.k*self.k).repeat(batchsize,1)
        if x.is_cuda:
            iden = iden.cuda()
        x = x + iden
        x = x.view(-1, self.k, self.k)
        return x

class PointNetfeat(nn.Module):
    def __init__(self, global_feat = True, feature_transform = False):
        super(PointNetfeat, self).__init__()
        self.stn = STN3d()
        self.conv1 = torch.nn.Conv1d(3, 64, 1)
        self.conv2 = torch.nn.Conv1d(64, 128, 1)
        self.conv3 = torch.nn.Conv1d(128, 1024, 1)
        self.bn1 = nn.BatchNorm1d(64)
        self.bn2 = nn.BatchNorm1d(128)
        self.bn3 = nn.BatchNorm1d(1024)
        self.global_feat = global_feat
        self.feature_transform = feature_transform
        if self.feature_transform:
            self.fstn = STNkd(k=64)

    def forward(self, x):
        n_pts = x.size()[2]
        trans = self.stn(x)
        x = x.transpose(2, 1)
        x = torch.bmm(x, trans)
        x = x.transpose(2, 1)
        x = F.relu(self.bn1(self.conv1(x)))

        if self.feature_transform:
            trans_feat = self.fstn(x)
            x = x.transpose(2,1)
            x = torch.bmm(x, trans_feat)
            x = x.transpose(2,1)
        else:
            trans_feat = None

        pointfeat = x
        x = F.relu(self.bn2(self.conv2(x)))
        x = self.bn3(self.conv3(x))
        x = torch.max(x, 2, keepdim=True)[0]
        x = x.view(-1, 1024)
        if self.global_feat:
            return x, trans, trans_feat
        else:
            x = x.view(-1, 1024, 1).repeat(1, 1, n_pts)
            return torch.cat([x, pointfeat], 1), trans, trans_feat

class PointNetCls(nn.Module):
    def __init__(self, k=2, feature_transform=False):
        super(PointNetCls, self).__init__()
        self.feature_transform = feature_transform
        self.feat = PointNetfeat(global_feat=True, feature_transform=feature_transform)
        self.fc1 = nn.Linear(1024, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc3 = nn.Linear(256, k)
        self.dropout = nn.Dropout(p=0.3)
        self.bn1 = nn.BatchNorm1d(512)
        self.bn2 = nn.BatchNorm1d(256)
        self.relu = nn.ReLU()

    def forward(self, x):
        x, trans, trans_feat = self.feat(x)
        x = F.relu(self.bn1(self.fc1(x)))
        x = F.relu(self.bn2(self.dropout(self.fc2(x))))
        x = self.fc3(x)
        return F.log_softmax(x, dim=1), trans, trans_feat


class PointNetDenseCls(nn.Module):
    def __init__(self, k = 2, feature_transform=False):
        super(PointNetDenseCls, self).__init__()
        self.k = k
        self.feature_transform=feature_transform
        self.feat = PointNetfeat(global_feat=False, feature_transform=feature_transform)
        self.conv1 = torch.nn.Conv1d(1088, 512, 1)
        self.conv2 = torch.nn.Conv1d(512, 256, 1)
        self.conv3 = torch.nn.Conv1d(256, 128, 1)
        self.conv4 = torch.nn.Conv1d(128, self.k, 1)
        self.bn1 = nn.BatchNorm1d(512)
        self.bn2 = nn.BatchNorm1d(256)
        self.bn3 = nn.BatchNorm1d(128)

    def forward(self, x):
        batchsize = x.size()[0]
        n_pts = x.size()[2]
        x, trans, trans_feat = self.feat(x)
        x = F.relu(self.bn1(self.conv1(x)))
        x = F.relu(self.bn2(self.conv2(x)))
        x = F.relu(self.bn3(self.conv3(x)))
        x = self.conv4(x)
        x = x.transpose(2,1).contiguous()
        x = F.log_softmax(x.view(-1,self.k), dim=-1)
        x = x.view(batchsize, n_pts, self.k)
        return x, trans, trans_feat

def feature_transform_regularizer(trans):
    d = trans.size()[1]
    batchsize = trans.size()[0]
    I = torch.eye(d)[None, :, :]
    if trans.is_cuda:
        I = I.cuda()
    loss = torch.mean(torch.norm(torch.bmm(trans, trans.transpose(2,1)) - I, dim=(1,2)))
    return loss

if __name__ == '__main__':
    sim_data = Variable(torch.rand(32,3,2500))
    trans = STN3d()
    out = trans(sim_data)
    print('stn', out.size())
    print('loss', feature_transform_regularizer(out))

    sim_data_64d = Variable(torch.rand(32, 64, 2500))
    trans = STNkd(k=64)
    out = trans(sim_data_64d)
    print('stn64d', out.size())
    print('loss', feature_transform_regularizer(out))

    pointfeat = PointNetfeat(global_feat=True)
    out, _, _ = pointfeat(sim_data)
    print('global feat', out.size())

    pointfeat = PointNetfeat(global_feat=False)
    out, _, _ = pointfeat(sim_data)
    print('point feat', out.size())

    cls = PointNetCls(k = 5)
    out, _, _ = cls(sim_data)
    print('class', out.size())

    seg = PointNetDenseCls(k = 3)
    out, _, _ = seg(sim_data)
    print('seg', out.size())

3. Keras

from keras.layers import Conv1D, MaxPooling1D, Flatten, Dropout, Input, BatchNormalization, Dense
from keras.layers import Reshape, Lambda, concatenate
from keras.models import Model
from keras.engine.topology import Layer
import numpy as np
import tensorflow as tf

class MatMul(Layer):

    def __init__(self, **kwargs):
        super(MatMul, self).__init__(**kwargs)

    def build(self, input_shape):
        
        if not isinstance(input_shape, list):
            raise ValueError('`MatMul` layer should be called '
                             'on a list of inputs')
        if len(input_shape) != 2:
            raise ValueError('The input of `MatMul` layer should be a list containing 2 elements')

        if len(input_shape[0]) != 3 or len(input_shape[1]) != 3:
            raise ValueError('The dimensions of each element of inputs should be 3')

        if input_shape[0][-1] != input_shape[1][1]:
            raise ValueError('The last dimension of inputs[0] should match the dimension 1 of inputs[1]')

    def call(self, inputs):
        if not isinstance(inputs, list):
            raise ValueError('A `MatMul` layer should be called '
                             'on a list of inputs.')
        return tf.matmul(inputs[0], inputs[1])

    def compute_output_shape(self, input_shape):
        output_shape = [input_shape[0][0], input_shape[0][1], input_shape[1][-1]]
        return tuple(output_shape)


def PointNet(nb_classes):
    input_points = Input(shape=(2048, 3))

    x = Conv1D(64, 1, activation='relu')(input_points)
    x = BatchNormalization()(x)
    x = Conv1D(128, 1, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Conv1D(1024, 1, activation='relu')(x)
    x = BatchNormalization()(x)
    x = MaxPooling1D(pool_size=2048)(x)

    x = Dense(512, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dense(256, activation='relu')(x)
    x = BatchNormalization()(x)

    x = Dense(9, weights=[np.zeros([256, 9]), np.array([1, 0, 0, 0, 1, 0, 0, 0, 1]).astype(np.float32)])(x)
    input_T = Reshape((3, 3))(x)

    g = MatMul()([input_points, input_T])
    g = Conv1D(64, 1, activation='relu')(g)
    g = BatchNormalization()(g)
    g = Conv1D(64, 1, activation='relu')(g)
    g = BatchNormalization()(g)

    f = Conv1D(64, 1, activation='relu')(g)
    f = BatchNormalization()(f)
    f = Conv1D(128, 1, activation='relu')(f)
    f = BatchNormalization()(f)
    f = Conv1D(1024, 1, activation='relu')(f)
    f = BatchNormalization()(f)
    f = MaxPooling1D(pool_size=2048)(f)
    f = Dense(512, activation='relu')(f)
    f = BatchNormalization()(f)
    f = Dense(256, activation='relu')(f)
    f = BatchNormalization()(f)
    f = Dense(64 * 64, weights=[np.zeros([256, 64 * 64]), np.eye(64).flatten().astype(np.float32)])(f)
    feature_T = Reshape((64, 64))(f)

    g = MatMul()([g, feature_T])
    g = Conv1D(64, 1, activation='relu')(g)
    g = BatchNormalization()(g)
    g = Conv1D(128, 1, activation='relu')(g)
    g = BatchNormalization()(g)
    g = Conv1D(1024, 1, activation='relu')(g)
    g = BatchNormalization()(g)

    global_feature = MaxPooling1D(pool_size=2048)(g)

    c = Dense(512, activation='relu')(global_feature)
    c = BatchNormalization()(c)
    c = Dropout(0.5)(c)
    c = Dense(256, activation='relu')(c)
    c = BatchNormalization()(c)
    c = Dropout(0.5)(c)
    c = Dense(nb_classes, activation='softmax')(c)
    prediction = Flatten()(c)

    model = Model(inputs=input_points, outputs=prediction)

    return model

4.TensorFlow2.x

import os
import sys

sys.path.insert(0, './')

import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense, Dropout

from pnet2_layers.layers import Pointnet_SA, Pointnet_FP


class SEM_SEG_Model(Model):

 def __init__(self, batch_size, num_classes, bn=False, activation=tf.nn.relu):
  super(SEM_SEG_Model, self).__init__()

  self.activation = activation
  self.batch_size = batch_size
  self.keep_prob = 0.5
  self.num_classes = num_classes
  self.bn = bn

  self.kernel_initializer = 'glorot_normal'
  self.kernel_regularizer = None

  self.init_network()


 def init_network(self):

  self.sa_1 = Pointnet_SA(
   npoint=1024,
   radius=0.1,
   nsample=32,
   mlp=[32, 32, 64],
   group_all=False,
   activation=self.activation,
   bn = self.bn
  )

  self.sa_2 = Pointnet_SA(
   npoint=256,
   radius=0.2,
   nsample=32,
   mlp=[64, 64, 128],
   group_all=False,
   activation=self.activation,
   bn = self.bn
  )

  self.sa_3 = Pointnet_SA(
   npoint=64,
   radius=0.4,
   nsample=32,
   mlp=[128, 128, 256],
   group_all=False,
   activation=self.activation,
   bn = self.bn
  )

  self.sa_4 = Pointnet_SA(
   npoint=16,
   radius=0.8,
   nsample=32,
   mlp=[256, 256, 512],
   group_all=False,
   activation=self.activation,
   bn = self.bn
  )

  self.fp_1 = Pointnet_FP(
   mlp = [256, 256],
   activation = self.activation,
   bn = self.bn
  )

  self.fp_2 = Pointnet_FP(
   mlp = [256, 256],
   activation = self.activation,
   bn = self.bn
  )

  self.fp_3 = Pointnet_FP(
   mlp = [256, 128],
   activation = self.activation,
   bn = self.bn
  )

  self.fp_4 = Pointnet_FP(
   mlp = [128, 128, 128],
   activation = self.activation,
   bn = self.bn
  )


  self.dense1 = Dense(128, activation=self.activation)

  self.dropout1 = Dropout(self.keep_prob)

  self.dense2 = Dense(self.num_classes, activation=tf.nn.softmax)


 def forward_pass(self, input, training):

  l0_xyz = input
  l0_points = None

  l1_xyz, l1_points = self.sa_1(l0_xyz, l0_points, training=training)
  l2_xyz, l2_points = self.sa_2(l1_xyz, l1_points, training=training)
  l3_xyz, l3_points = self.sa_3(l2_xyz, l2_points, training=training)
  l4_xyz, l4_points = self.sa_4(l3_xyz, l3_points, training=training)

  l3_points = self.fp_1(l3_xyz, l4_xyz, l3_points, l4_points, training=training)
  l2_points = self.fp_2(l2_xyz, l3_xyz, l2_points, l3_points, training=training)
  l1_points = self.fp_3(l1_xyz, l2_xyz, l1_points, l2_points, training=training)
  l0_points = self.fp_4(l0_xyz, l1_xyz, l0_points, l1_points, training=training)

  net = self.dense1(l0_points)
  net = self.dropout1(net)
  pred = self.dense2(net)

  return pred


 def train_step(self, input):

  with tf.GradientTape() as tape:

   pred = self.forward_pass(input[0], True)
   loss = self.compiled_loss(input[1], pred)
  
  gradients = tape.gradient(loss, self.trainable_variables)
  self.optimizer.apply_gradients(zip(gradients, self.trainable_variables))

  self.compiled_metrics.update_state(input[1], pred)

  return {m.name: m.result() for m in self.metrics}


 def test_step(self, input):

  pred = self.forward_pass(input[0], False)
  loss = self.compiled_loss(input[1], pred)

  self.compiled_metrics.update_state(input[1], pred)

  return {m.name: m.result() for m in self.metrics}


 def call(self, input, training=False):

  return self.forward_pass(input, training)

5.Caffe

name: "pointnet_cls_basic"

# -- data layer 
layer {
  name: "data"
  type: "HDF5Data"
  top: "data"
  top: "label"
  hdf5_data_param {
    source: "data/modelnet40_ply_hdf5_2048/train_files.txt"
    batch_size: 32
  }
  include: { phase: TRAIN }
}
layer {
  name: "data"
  type: "HDF5Data"
  top: "data"
  top: "label"
  hdf5_data_param {
    source: "data/modelnet40_ply_hdf5_2048/test_files.txt"
    batch_size: 32
  }
  include: { phase: TEST }
}

layer {
    name: "reshape"
    type: "Reshape"
    bottom: "data"
    top: "data_reshape"
    reshape_param {
      shape {
        dim: 0  # copy the dimension from below
        dim: 1
        dim: -1
        dim: 3 # infer it from the other dimensions
      }
    }
}

#  mlp -
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data_reshape"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 64
    pad: 0
    kernel_w: 3
    kernel_h: 1
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "bn1"
  type: "BatchNorm"
  bottom: "conv1"
  top: "conv1"
}
layer {
    bottom: "conv1"
    top: "conv1"
    name: "scale1"
    type: "Scale"
    scale_param {
        bias_term: true
    }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "conv1"
  top: "conv1"
}

layer {
  name: "conv2"
  type: "Convolution"
  bottom: "conv1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 64
    pad: 0
    kernel_w: 1
    kernel_h: 1
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "bn2"
  type: "BatchNorm"
  bottom: "conv2"
  top: "conv2"
}
layer {
    bottom: "conv2"
    top: "conv2"
    name: "scale2"
    type: "Scale"
    scale_param {
        bias_term: true
    }
}
layer {
  name: "relu2"
  type: "ReLU"
  bottom: "conv2"
  top: "conv2"
}


layer {
  name: "conv3"
  type: "Convolution"
  bottom: "conv2"
  top: "conv3"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 64
    pad: 0
    kernel_w: 1
    kernel_h: 1
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "bn3"
  type: "BatchNorm"
  bottom: "conv3"
  top: "conv3"
}
layer {
    bottom: "conv3"
    top: "conv3"
    name: "scale3"
    type: "Scale"
    scale_param {
        bias_term: true
    }
}
layer {
  name: "relu3"
  type: "ReLU"
  bottom: "conv3"
  top: "conv3"
}

layer {
  name: "conv4"
  type: "Convolution"
  bottom: "conv3"
  top: "conv4"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 128
    pad: 0
    kernel_w: 1
    kernel_h: 1
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "bn4"
  type: "BatchNorm"
  bottom: "conv4"
  top: "conv4"
}
layer {
    bottom: "conv4"
    top: "conv4"
    name: "scale4"
    type: "Scale"
    scale_param {
        bias_term: true
    }
}
layer {
  name: "relu4"
  type: "ReLU"
  bottom: "conv4"
  top: "conv4"
}

layer {
  name: "conv5"
  type: "Convolution"
  bottom: "conv4"
  top: "conv5"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 1024
    pad: 0
    kernel_w: 1
    kernel_h: 1
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "bn5"
  type: "BatchNorm"
  bottom: "conv5"
  top: "conv5"
}
layer {
    bottom: "conv5"
    top: "conv5"
    name: "scale5"
    type: "Scale"
    scale_param {
        bias_term: true
    }
}
layer {
  name: "relu5"
  type: "ReLU"
  bottom: "conv5"
  top: "conv5"
}


#  max pool -
layer {
  name: "pool"
  type: "Pooling"
  bottom: "conv5"
  top: "global_feat"
  pooling_param {
    pool: MAX
    pad: 0
    kernel_h: 2048
    kernel_w: 1
    stride: 1
  }
}

#  fc -
layer {
  name: "fc1"
  type: "InnerProduct"
  bottom: "global_feat"
  top: "fc1"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 1
    decay_mult: 1
  }
  inner_product_param {
    num_output: 512
    weight_filler {
      type: "gaussian"
      std: 0.001
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "bn6"
  type: "BatchNorm"
  bottom: "fc1"
  top: "fc1"
}
layer {
    bottom: "fc1"
    top: "fc1"
    name: "scale6"
    type: "Scale"
    scale_param {
        bias_term: true
    }
}
layer {
  name: "relu6"
  type: "ReLU"
  bottom: "fc1"
  top: "fc1"
}

layer {
  name: "fc2"
  type: "InnerProduct"
  bottom: "fc1"
  top: "fc2"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 1
    decay_mult: 1
  }
  inner_product_param {
    num_output: 256
    weight_filler {
      type: "gaussian"
      std: 0.001
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "bn7"
  type: "BatchNorm"
  bottom: "fc2"
  top: "fc2"
}
layer {
    bottom: "fc2"
    top: "fc2"
    name: "scale7"
    type: "Scale"
    scale_param {
        bias_term: true
    }
}
layer {
  name: "relu7"
  type: "ReLU"
  bottom: "fc2"
  top: "fc2"
}

layer {
  name: "drop1"
  type: "Dropout"
  bottom: "fc2"
  top: "drop1"
  dropout_param {
    dropout_ratio: 0.3
  }
}
layer {
  name: "fc3"
  type: "InnerProduct"
  bottom: "drop1"
  top: "fc3"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 1
    decay_mult: 1
  }
  inner_product_param {
    num_output: 40
    weight_filler {
      type: "gaussian"
      std: 0.001
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "fc3"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "fc3"
  bottom: "label"
  top: "loss"
}

6.PaddlePaddle

import paddle.fluid as fluid

def get_model(img, momentum):
    # 第一個卷積-池化層
    img = fluid.layers.conv2d(input=img, 
        num_filters=64, 
        filter_size=[1,3])
    img = fluid.layers.batch_norm(input=img,
        act='relu',
        momentum = momentum,
        param_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=1.0)),
        bias_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=0.0))
    )
    
    img = fluid.layers.conv2d(input=img, 
        num_filters=64, 
        filter_size=1)
    img = fluid.layers.batch_norm(input=img,
        act='relu',
        momentum = momentum,
        param_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=1.0)),
        bias_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=0.0))
    )
        
    img = fluid.layers.conv2d(input=img, 
        num_filters=128, 
        filter_size=1)
    img = fluid.layers.batch_norm(input=img,
        act='relu',
        momentum = momentum,
        param_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=1.0)),
        bias_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=0.0))
    )
    
    img = fluid.layers.conv2d(input=img, 
        num_filters=1024, 
        filter_size=1)
    momentum = momentum
    img = fluid.layers.batch_norm(input=img,
        act='relu',
        momentum = momentum,
        param_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=1.0)),
        bias_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=0.0))
    )
    
    img = fluid.layers.pool2d(img,[1024,1],'max')
    img = fluid.layers.squeeze(img,[])
    img = fluid.layers.fc(input=img, size=512)
    img = fluid.layers.batch_norm(input=img,
        act='relu',
        momentum = momentum,
        param_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=1.0)),
        bias_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=0.0))
    )
    
    img = fluid.layers.fc(input=img, size=256)
    img = fluid.layers.batch_norm(input=img,
        act='relu',
        momentum = momentum,
        param_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=1.0)),
        bias_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(value=0.0))
    )
    
    img = fluid.layers.dropout(x=img, dropout_prob=0.7)
    img = fluid.layers.fc(input=img, size=40, act='softmax')
    return img

相關焦點

  • SBUs—the turning point in the development of MOFs
    A strategy to develop such materials is in development in reticular chemistry, derived from the Latin translation "reticulum" as "having the form of a net."
  • 英語語法學習point at point to point out用法
    point to、point out、point at的意思:point at指較近的事物(可以接觸),point to指較遠的事物(不接觸),point out表示的是給某人指示方向,要點或錯誤等。例:He pointed at the child with his pen.
  • 大雜燴:女人內褲進化史 德國招妓院服務體驗員
    查看往期大雜燴 歡迎愛發圖的你加入大雜燴玩家群158924815。
  • 沙盒+恐龍+打架,Steam上一些有著「大雜燴」玩法的遊戲盤點
    近些年來,在Steam上出了不少有著"大雜燴"玩法的遊戲,這些遊戲本身並沒有什麼特殊之處,只不過它們將不同的遊戲元素結合在了一起,不曾想這種做法直接使得這些遊戲擦出了巨大的火花,備受玩家們的歡迎。接下來小編就來給大家盤點一些"大雜燴"玩法的遊戲,說不定在座的各位玩家都有體驗過這些作品。
  • 靈魂大雜燴是什麼樣的?
    現在的情況是除了何經理和榮門童的其他人都是靈魂大雜燴,還有搜查到的線索。靈魂大雜燴已經有人知道自己是兇手?郝妹妹的靈魂用白門童的身體殺死了甄霄雲,而真正的白門童已經在死在了郝妹妹的身體裡。經過何經理的縝密推理真相已經大白:最終的兇手就是蒲動物的身體郝妹妹的靈魂。至於最終他們身體裡的誰,大家可以自己試著去解答一下,反正我暈了。
  • D Set weight Point 和D Set point Transition
    來分享一個非常重要的調參數據是的,D set point weight 和D setpoint transition
  • 「Turning Point」是什麼意思?
    turning point,轉折點This is the turning point in my life.這是我人生中的轉折點。It has not yet to reach the turning point in fighting with coronavirus.與冠狀病毒鬥爭還沒有到達轉折點。It is a turning point in my career.這是我事業上的轉折點。
  • 英語語法:at which point用法
    新東方網>英語>英語學習>語法詞彙>語法指導>正文英語語法:at which point用法 2012-12-27 10:41 來源:英美者 作者:
  • 職場英語:beside the point 離題,不中肯
    詞組拓展2:和point有關的英語短語彙總   the weakest [best, strongest] point 最大缺[優]點   a point of contact 【機械工程】接觸點;【數學】切點   the eye point 出射點   a full point 句號   at this point
  • 星導演「電影大雜燴」票房撲街?外星人大戰胖娃,劇情撲朔迷離
    星導演「電影大雜燴」票房撲街?外星人大戰胖娃,劇情撲朔迷離星太奇終於完成了自己的夢想,可成為導演後卻遇到了很多苦惱,手忙腳亂的寫完了所有劇本,可卻總被許多人篡改劇情,至此大家才發現星太奇的電影成為了「電影大雜燴」,所以票房也自然而然撲街,甚至劇情中還有外星人和胖娃的對戰,所以星太奇當導演的過程也給我們提了個醒,夢想和現實總有差距,但一定要堅持自己的夢想,更不要受到別人的影響哦。
  • 這個四字母不簡單:「大雜燴」域名misc.com昨日27萬元結拍!
    據查詢,misc其實是英語單詞「miscellaneous」的前四個字母,有「雜項、混合體、大雜燴」的意思。另外,這四個字母還常用於NIKE等品牌服飾上表示其尺碼為均碼,同時也是「移動信息服務中心、馬來西亞國際航運有限公司、宏指令結構技術體系」等含義的縮寫。因此,這個域名的應用範圍還是比較廣,IT、服裝、航運和各類綜合性信息網站都可以。
  • 太空垃圾「臨界點」 tipping point
    請看相關報導:The amount of debris orbiting the Earth has reached "a tipping point" for collisions, which would in turn generate more of the debris that threatens astronauts
  • Websense、Stonesoft與Raytheon組成Forcepoint
    Forcepoint通過一個統一的雲中心平臺保護用戶、網絡和數據並極大地提高了所有安全產品的整體效率。Forcepoint平臺將抵禦內部威脅和外部威脅,並迅速發現漏洞,減少「駐留時間」(妥協和修復的時間)、阻斷盜竊行為。「使用Forcepoint,各機構可以保護雲中、路上或者辦公室裡等任何時點的用戶、網絡和數據安全。
  • 每日英語:You've got a point there
    新東方網>英語>英語學習>口語>每日一句英語>正文每日英語:You've got a point there 2013-02-18 15:52 來源:恆星英語 作者:
  • Crestpoint收購薩裡工業與漢密爾頓零售地產
    進行了這些收購之後,Crestpoint的管理資產總額已增至約53億美元。Crestpoint總裁兼創始人凱文·萊昂(Kevin Leon)表示:「在困難的環境中,尤其是在工業領域中,用如此優質的資產來增長投資組合,應該為我們的投資組合提供長期的切實利益。」「儘管充滿挑戰的一年,Crestpoint到2020年仍設法在加拿大各地收購了超過7億加元的優質物業。」
  • net的意思是網,net result是什麼意思呢?
    說到net這個單詞,很多人會想到網、網狀物這類意思。除了網、網狀物,net還有什麼意思呢?今天,我們就一起來看一下net都有哪些用法。首先,我們看一下net做名詞的用法。1、His aunt wants to buy net curtains.他阿姨想買網眼窗簾。
  • 皇后樂隊大雜燴單曲《波西米亞狂想曲》背後的創作故事
    皇后樂隊大雜燴單曲《波西米亞狂想曲》背後的創作故事 2020-07-03 20:01 來源:澎湃新聞·澎湃號·湃客
  • 1分鐘英語挑戰:「You have a point.」 是什麼意思?
    You have a point (there).said when someone's point of view or opinion makes sense.B: Yeah, you have a point there. 是的,你說得有道理。I see your point.
  • 每日一句英語:Let's get to the point
    新東方網>英語>英語學習>口語>每日一句英語>正文每日一句英語:Let's get to the point 2013-02-18 15:53 來源:恆星英語 作者:
  • 安東帕SAXSpoint 2.0多孔膜表徵
    使用Tosca 400和SAXSpoint 2.0系統來表徵垂直基底的多孔結構聚合物薄膜。左邊的圖是用SAXSpoint 2.0系統研究結構的 GISAXS圖樣。從反射圖上計算得到孔-孔間距大約50 nm。從相應的反射位置(見插圖)看,孔的六方排布可見。右邊的圖顯示的是相應的AFM形貌圖,使用Anton Paar新的AFM Tosca 400獲得。