人工智慧技術乾貨請關注AIZOO、Jack Cui
機器人與自動駕駛課程請關注深藍學院
我是來自山區、樸實、不偷電瓶的AI算法工程師阿chai,給大家分享人工智慧、自動駕駛、機器人、3D感知相關的知識
阿chai之前介紹了很多有關3D點雲的知識,包括PointNet,今天給大家整理一下不同框架下PointNet網絡部分的Demo。
PointNet相關介紹請前往Pointnet詳解+百度飛槳復現算法。
1.TensorFlow1.ximport 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)
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())
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
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)
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"
}
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