使用TensorFlow双流卷积神经网络对CK+表情数据库进行分类

更新时间:2023-06-23 22:45:22 阅读: 评论:0

使⽤TensorFlow双流卷积神经⽹络对CK+表情数据库进⾏分类双流卷积神经⽹络我最初是在做⾏为识别的时候接触到的,双流指的是时间流和空间流,再具体⼀点就是,时间流指的是对光流图⽚进⾏卷积处理,然后空间流指的是对RGB图⽚进⾏卷积处理,然后进⾏融合操作。这样往往⽐单纯对RGB图⽚进⾏卷积效果好,特别是在视频⾏为识别等⽅⾯,因为引⼊了时间信息。话不多说,放上代码及讲解。
import tensorflow as tf
#%%
def inference(s_images,T_images, batch_size, n_class):
koopa
'''Build the model
Args:
images: image batch, 4D tensor, tf.float32, [batch_size, width, height, channels]
Returns:
output tensor with the computed logits, float, [batch_size, n_class]
'''
#conv1, shape = [kernel size, kernel size, channels, kernel numbers]
# one stream space
with tf.variable_scope('s_conv1') as scope:
weights = tf.get_variable('weights',
shape = [3,3,3, 16],
dtype = tf.float32,
uncated_normal_initializer(stddev=0.1,dtype=tf.float32))
bias = tf.get_variable('bias',
shape=[16],
dtype=tf.float32,
stant_initializer(0.1))
conv = v2d(s_images, weights, strides=[1,1,1,1], padding='SAME')
pre_activation = tf.nn.bias_add(conv, bias)
s_conv1 = lu(pre_activation, name= scope.name)
#pool1 and norm1
with tf.variable_scope('s_pooling1_lrn') as scope:
pool1 = tf.nn.max_pool(s_conv1, ksize=[1,3,3,1],strides=[1,2,2,1],
padding='SAME', name='s_pooling1')
norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0,
beta=0.75,name='s_norm1')
#conv2
with tf.variable_scope('s_conv2') as scope:
weights = tf.get_variable('weights',
shape=[3,3,16,16],
dtype=tf.float32,
uncated_normal_initializer(stddev=0.1,dtype=tf.float32))
bias = tf.get_variable('bias',
dinnertimeshape=[16],
dtype=tf.float32,
stant_initializer(0.1))
conv = v2d(norm1, weights, strides=[1,1,1,1],padding='SAME')
pre_activation = tf.nn.bias_add(conv, bias)
s_conv2 = lu(pre_activation, name='s_conv2')
#pool2 and norm2
with tf.variable_scope('s_pooling2_lrn') as scope:
norm2 = tf.nn.lrn(s_conv2, depth_radius=4, bias=1.0, alpha=0.001/9.0,
beta=0.75,name='s_norm2')
pool2 = tf.nn.max_pool(norm2, ksize=[1,3,3,1], strides=[1,1,1,1],
padding='SAME',name='s_pooling2')
#local3
with tf.variable_scope('s_local3') as scope:
with tf.variable_scope('s_local3') as scope:
reshape = tf.reshape(pool2, shape=[batch_size, -1])
dim = _shape()[1].value
weights = tf.get_variable('weights',
shape=[dim,128],
dtype=tf.float32,
uncated_normal_initializer(stddev=0.005,dtype=tf.float32))        bias = tf.get_variable('bias',
shape=[128],
dtype=tf.float32,
stant_initializer(0.1))
s_local3 = lu(tf.matmul(reshape, weights) + bias, name=scope.name)
#local4
#with tf.variable_scope('s_local4') as scope:
#    weights = tf.get_variable('weights',
七年级下册英语复习#                              shape=[128,128],
#                              dtype=tf.float32,
#                              uncated_normal_initializer(stddev=0.005,dtype=tf.float32))    #    bias = tf.get_variable('bias',
#                            shape=[128],
#                            dtype=tf.float32,
#                            stant_initializer(0.1))
#    local4 = lu(tf.matmul(local3, weights) + bias, name='s_local4')
# another stream temporal
with tf.variable_scope('T_conv1') as scope:
weights = tf.get_variable('weights',
shape = [3,3,3, 16],
dtype = tf.float32,
最新大片推荐
uncated_normal_initializer(stddev=0.1,dtype=tf.float32))
bias = tf.get_variable('bias',
shape=[16],
dtype=tf.float32,
stant_initializer(0.1))
conv = v2d(T_images, weights, strides=[1,1,1,1], padding='SAME')
pre_activation = tf.nn.bias_add(conv, bias)
T_conv1 = lu(pre_activation, name= scope.name)
#pool1 and norm1
with tf.variable_scope('T_pooling1_lrn') as scope:
pool1 = tf.nn.max_pool(T_conv1, ksize=[1,3,3,1],strides=[1,2,2,1],
padding='SAME', name='T_pooling1')
norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0,
beta=0.75,name='T_norm1')
#conv2
with tf.variable_scope('T_conv2') as scope:
weights = tf.get_variable('weights',
shape=[3,3,16,16],
dtype=tf.float32,
uncated_normal_initializer(stddev=0.1,dtype=tf.float32))
bias = tf.get_variable('bias',
shape=[16],
dtype=tf.float32,
stant_initializer(0.1))
conv = v2d(norm1, weights, strides=[1,1,1,1],padding='SAME')
pre_activation = tf.nn.bias_add(conv, bias)
T_conv2 = lu(pre_activation, name='T_conv2')
#pool2 and norm2
with tf.variable_scope('T_pooling2_lrn') as scope:
norm2 = tf.nn.lrn(T_conv2, depth_radius=4, bias=1.0, alpha=0.001/9.0,
beta=0.75,name='T_norm2')
beta=0.75,name='T_norm2')
pool2 = tf.nn.max_pool(norm2, ksize=[1,3,3,1], strides=[1,1,1,1],
padding='SAME',name='T_pooling2')
#local3
with tf.variable_scope('T_local3') as scope:
reshape = tf.reshape(pool2, shape=[batch_size, -1])
dim = _shape()[1].value
weights = tf.get_variable('weights',
shape=[dim,128],
dtype=tf.float32,
uncated_normal_initializer(stddev=0.005,dtype=tf.float32))        bias = tf.get_variable('bias',
shape=[128],
dtype=tf.float32,
stant_initializer(0.1))
T_local3 = lu(tf.matmul(reshape, weights) + bias, name=scope.name)
local3 = s_local3 + T_local3
#local4
with tf.variable_scope('local4') as scope:
weights = tf.get_variable('weights',
shape=[128,128],
dtype=tf.float32,
uncated_normal_initializer(stddev=0.005,dtype=tf.float32))        bias = tf.get_variable('bias',
shape=[128],
dtype=tf.float32,
stant_initializer(0.1))
local4 = lu(tf.matmul(local3, weights) + bias, name='local4')
# softmax
with tf.variable_scope('softmax_linear') as scope:
weights = tf.get_variable('softmax_linear',
shape=[128, n_class],
dtype=tf.float32,
uncated_normal_initializer(stddev=0.005,dtype=tf.float32))        bias = tf.get_variable('bias',
shape=[n_class],
dtype=tf.float32,
stant_initializer(0.1))
softmax_linear = tf.add(tf.matmul(local4, weights), bias, name='softmax_linear')
return softmax_linear
#%%
def loss(logits, labels):
'''Compute loss from logits and labels
Args:
logits: logits tensor, float, [batch_size, n_class]
labels: label tensor, tf.int32, [batch_size]
Returns:
loss tensor of float type
'''
with tf.variable_scope('loss') as scope:
cross_entropy = tf.nn.spar_softmax_cross_entropy_with_logits\
(logits=logits, labels=labels, name='xentropy_per_example')
loss = tf.reduce_mean(cross_entropy, name='loss')
tf.summary.scalar(scope.name+'/loss', loss)
return loss
#%%
def trainning(loss, learning_rate):
'''Training ops, the Op returned by this function is what must be pasd to
'ss.run()' call to cau the model to train.
Args:
loss: loss tensor, from loss()
Returns:
train_op: The op for trainning
'''
with tf.name_scope('optimizer'):
optimizer = tf.train.AdamOptimizer(learning_rate= learning_rate)
global_step = tf.Variable(0, name='global_step', trainable=Fal)
train_op = optimizer.minimize(loss, global_step= global_step)
return train_op
#%%
def evaluation(logits, labels):
"""Evaluate the quality of the logits at predicting the label.
Args:
logits: Logits tensor, float - [batch_size, NUM_CLASSES].
labels: Labels tensor, int32 - [batch_size], with values in the
range [0, NUM_CLASSES).
Returns:
A scalar int32 tensor with the number of examples (out of batch_size)
that were predicted correctly.
"""
with tf.variable_scope('accuracy') as scope:
correct = tf.nn.in_top_k(logits, labels, 1)
correct = tf.cast(correct, tf.float16)
accuracy = tf.reduce_mean(correct)
#correct_prediction = tf.equal(tf.argmax(logits,1),tf.argmax(labels,1))
#accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
tf.summary.scalar(scope.name+'/accuracy', accuracy)
return accuracy
代码中写的S_conv等以S开头的是空间流,也就是对RGB进⾏训练,T_conv等以T开头的是时间流,对光流图⽚进⾏训练。并将结果在全连接层进⾏融合。以上代码是双流卷积模型的代码,其中的层数都可以修改。
import tensorflow as tf
import numpy as np
import os
#train_dir = '/home/hrz/projects/tensorflow/My-TensorFlow-tutorials/cats_vs_dogs/data/train/'
def get_files(file_dir):
angry = []
label_angry = []
happy = []
label_happy = []
surprid = []
label_surprid = []
disgusted = []
label_disgusted = []
fearful = []
label_fearful = []
sadness = []
label_sadness = []
for sub_file_dir in os.listdir(file_dir):
if sub_file_dir == 'angry':
for name in os.listdir(file_dir+'/'+sub_file_dir):
angry.append(file_dir+'/'+sub_file_dir+'/'+name)正品英文
label_angry.append(0)
elif sub_file_dir == 'disgusted':国际音标教学视频
for name in os.listdir(file_dir+'/'+sub_file_dir):
disgusted.append(file_dir+'/'+sub_file_dir+'/'+name)
label_disgusted.append(1)
elif sub_file_dir == 'fearful':
for name in os.listdir(file_dir+'/'+sub_file_dir):
fearful.append(file_dir+'/'+sub_file_dir+'/'+name)
label_fearful.append(2)
elif sub_file_dir == 'happy':
for name in os.listdir(file_dir+'/'+sub_file_dir):
happy.append(file_dir+'/'+sub_file_dir+'/'+name)
label_happy.append(3)
elif sub_file_dir == 'sadness':
for name in os.listdir(file_dir+'/'+sub_file_dir):
sadness.append(file_dir+'/'+sub_file_dir+'/'+name)
label_sadness.append(4)
elif sub_file_dir == 'surprid':
for name in os.listdir(file_dir+'/'+sub_file_dir):上海电视大学地址
surprid.append(file_dir+'/'+sub_file_dir+'/'+name)
label_surprid.append(5)
print('Already!!',len(label_angry))
image_list = np.hstack((angry,disgusted,fearful,happy,sadness,surprid))
label_list = np.hstack((label_angry,label_disgusted,label_fearful,label_happy,label_sadness,label_su
rprid)) temp = np.array([image_list,label_list])
temp = anspo()
np.random.shuffle(temp)
image_list = list(temp[:, 0])
label_list = list(temp[:, 1])
label_list = [int(i) for i in label_list]
return image_list, label_list
the pirate baydef get_batch(image,label,image_W,image_H,batch_size,capacity):
'''
Args:
image: list type
label: list type剪什么发型好
image_W: image width
image_H: image height
batch_size: batch size
capacity: the maximum elements in queue
Returns:
image_batch: 4D tensor [batch_size, width, height, 3], dtype=tf.float32
label_batch: 1D tensor [batch_size], dtype=tf.int32
'''
image = tf.cast(image,tf.string)
label = tf.cast(label,tf.int32)
圣诞节歌曲下载input_queue = tf.train.slice_input_producer([image,label])
label = input_queue[1]
image_contents = tf.read_file(input_queue[0])
image = tf.image.decode_jpeg(image_contents,channels=3)
image = size_image_with_crop_or_pad(image,image_W,image_H)
image = tf.image.per_image_standardization(image)

本文发布于:2023-06-23 22:45:22,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/155354.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:卷积   双流   代码   时间   流图   视频   分类
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图