Looking to Listen at the Cocktail Party 代码详解

2023-10-24 22:58

本文主要是介绍Looking to Listen at the Cocktail Party 代码详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这个是清华某位大佬对论文《Looking to Listen at the Cocktail Party 》的一个复现。代码链接

网络结构如下图:

在这里插入图片描述
由于AVSpeech这个数据集里是一些视频的片段,而输入网络的是视频中的人脸区域。所以先要做人脸识别,并把人脸截取。
这个代码中使用了Python的一个pretrained的mtcnn的包直接做的。

def face_detect(file,detector,frame_path,cat_train,output_dir):name = file.replace('.jpg', '').split('-')log = cat_train.iloc[int(name[0])]x = log[3]y = log[4]img = cv2.imread('%s%s'%(frame_path,file))x = img.shape[1] * xy = img.shape[0] * yfaces = detector.detect_faces(img)# check if detected facesif(len(faces)==0):print('no face detect: '+file)return #no facebounding_box = bounding_box_check(faces,x,y)if(bounding_box == None):print('face is not related to given coord: '+file)returnprint(file," ",bounding_box)print(file," ",x, y)crop_img = img[bounding_box[1]:bounding_box[1] + bounding_box[3],bounding_box[0]:bounding_box[0]+bounding_box[2]]crop_img = cv2.resize(crop_img,(160,160))cv2.imwrite('%s/frame_'%output_dir + name[0] + '_' + name[1] + '.jpg', crop_img)#crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2RGB)#plt.imshow(crop_img)#plt.show()

以下是AV的model代码:

from keras.models import Sequential
from keras.layers import Input, Dense, Convolution2D,Bidirectional, concatenate
from keras.layers import Flatten, BatchNormalization, ReLU, Reshape, Lambda, TimeDistributed
from keras.models import Model
from keras.layers.recurrent import LSTM
from keras.initializers import he_normal, glorot_uniform
import tensorflow as tfdef AV_model(people_num=2):def UpSampling2DBilinear(size):return Lambda(lambda x: tf.image.resize(x, size, method=tf.image.ResizeMethod.BILINEAR))def sliced(x, index):return x[:, :, :, index]# --------------------------- AS start ---------------------------audio_input = Input(shape=(298, 257, 2))print('as_0:', audio_input.shape)as_conv1 = Convolution2D(96, kernel_size=(1, 7), strides=(1, 1), padding='same', dilation_rate=(1, 1), name='as_conv1')(audio_input)as_conv1 = BatchNormalization()(as_conv1)as_conv1 = ReLU()(as_conv1)print('as_1:', as_conv1.shape)as_conv2 = Convolution2D(96, kernel_size=(7, 1), strides=(1, 1), padding='same', dilation_rate=(1, 1), name='as_conv2')(as_conv1)as_conv2 = BatchNormalization()(as_conv2)as_conv2 = ReLU()(as_conv2)print('as_2:', as_conv2.shape)as_conv3 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(1, 1), name='as_conv3')(as_conv2)as_conv3 = BatchNormalization()(as_conv3)as_conv3 = ReLU()(as_conv3)print('as_3:', as_conv3.shape)as_conv4 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(2, 1), name='as_conv4')(as_conv3)as_conv4 = BatchNormalization()(as_conv4)as_conv4 = ReLU()(as_conv4)print('as_4:', as_conv4.shape)as_conv5 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(4, 1), name='as_conv5')(as_conv4)as_conv5 = BatchNormalization()(as_conv5)as_conv5 = ReLU()(as_conv5)print('as_5:', as_conv5.shape)as_conv6 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(8, 1), name='as_conv6')(as_conv5)as_conv6 = BatchNormalization()(as_conv6)as_conv6 = ReLU()(as_conv6)print('as_6:', as_conv6.shape)as_conv7 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(16, 1), name='as_conv7')(as_conv6)as_conv7 = BatchNormalization()(as_conv7)as_conv7 = ReLU()(as_conv7)print('as_7:', as_conv7.shape)as_conv8 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(32, 1), name='as_conv8')(as_conv7)as_conv8 = BatchNormalization()(as_conv8)as_conv8 = ReLU()(as_conv8)print('as_8:', as_conv8.shape)as_conv9 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(1, 1), name='as_conv9')(as_conv8)as_conv9 = BatchNormalization()(as_conv9)as_conv9 = ReLU()(as_conv9)print('as_9:', as_conv9.shape)as_conv10 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(2, 2), name='as_conv10')(as_conv9)as_conv10 = BatchNormalization()(as_conv10)as_conv10 = ReLU()(as_conv10)print('as_10:', as_conv10.shape)as_conv11 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(4, 4), name='as_conv11')(as_conv10)as_conv11 = BatchNormalization()(as_conv11)as_conv11 = ReLU()(as_conv11)print('as_11:', as_conv11.shape)as_conv12 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(8, 8), name='as_conv12')(as_conv11)as_conv12 = BatchNormalization()(as_conv12)as_conv12 = ReLU()(as_conv12)print('as_12:', as_conv12.shape)as_conv13 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(16, 16), name='as_conv13')(as_conv12)as_conv13 = BatchNormalization()(as_conv13)as_conv13 = ReLU()(as_conv13)print('as_13:', as_conv13.shape)as_conv14 = Convolution2D(96, kernel_size=(5, 5), strides=(1, 1), padding='same', dilation_rate=(32, 32), name='as_conv14')(as_conv13)as_conv14 = BatchNormalization()(as_conv14)as_conv14 = ReLU()(as_conv14)print('as_14:', as_conv14.shape)as_conv15 = Convolution2D(8, kernel_size=(1, 1), strides=(1, 1), padding='same', dilation_rate=(1, 1), name='as_conv15')(as_conv14)as_conv15 = BatchNormalization()(as_conv15)as_conv15 = ReLU()(as_conv15)print('as_15:', as_conv15.shape)AS_out = Reshape((298, 8 * 257))(as_conv15)print('AS_out:', AS_out.shape)# --------------------------- AS end ---------------------------# --------------------------- VS_model start ---------------------------VS_model = Sequential()VS_model.add(Convolution2D(256, kernel_size=(7, 1), strides=(1, 1), padding='same', dilation_rate=(1, 1), name='vs_conv1'))VS_model.add(BatchNormalization())VS_model.add(ReLU())VS_model.add(Convolution2D(256, kernel_size=(5, 1), strides=(1, 1), padding='same', dilation_rate=(1, 1), name='vs_conv2'))VS_model.add(BatchNormalization())VS_model.add(ReLU())VS_model.add(Convolution2D(256, kernel_size=(5, 1), strides=(1, 1), padding='same', dilation_rate=(2, 1), name='vs_conv3'))VS_model.add(BatchNormalization())VS_model.add(ReLU())VS_model.add(Convolution2D(256, kernel_size=(5, 1), strides=(1, 1), padding='same', dilation_rate=(4, 1), name='vs_conv4'))VS_model.add(BatchNormalization())VS_model.add(ReLU())VS_model.add(Convolution2D(256, kernel_size=(5, 1), strides=(1, 1), padding='same', dilation_rate=(8, 1), name='vs_conv5'))VS_model.add(BatchNormalization())VS_model.add(ReLU())VS_model.add(Convolution2D(256, kernel_size=(5, 1), strides=(1, 1), padding='same', dilation_rate=(16, 1), name='vs_conv6'))VS_model.add(BatchNormalization())VS_model.add(ReLU())VS_model.add(Reshape((75, 256, 1)))VS_model.add(UpSampling2DBilinear((298, 256)))VS_model.add(Reshape((298, 256)))# --------------------------- VS_model end ---------------------------video_input = Input(shape=(75, 1, 1792, people_num))AVfusion_list = [AS_out]for i in range(people_num):single_input = Lambda(sliced, arguments={'index': i})(video_input)VS_out = VS_model(single_input)AVfusion_list.append(VS_out)AVfusion = concatenate(AVfusion_list, axis=2)AVfusion = TimeDistributed(Flatten())(AVfusion)print('AVfusion:', AVfusion.shape)lstm = Bidirectional(LSTM(400, input_shape=(298, 8 * 257), return_sequences=True), merge_mode='sum')(AVfusion)print('lstm:', lstm.shape)fc1 = Dense(600, name="fc1", activation='relu', kernel_initializer=he_normal(seed=27))(lstm)print('fc1:', fc1.shape)fc2 = Dense(600, name="fc2", activation='relu', kernel_initializer=he_normal(seed=42))(fc1)print('fc2:', fc2.shape)fc3 = Dense(600, name="fc3", activation='relu', kernel_initializer=he_normal(seed=65))(fc2)print('fc3:', fc3.shape)complex_mask = Dense(257 * 2 * people_num, name="complex_mask", kernel_initializer=glorot_uniform(seed=87))(fc3)print('complex_mask:', complex_mask.shape)complex_mask_out = Reshape((298, 257, 2, people_num))(complex_mask)print('complex_mask_out:', complex_mask_out.shape)AV_model = Model(inputs=[audio_input, video_input], outputs=complex_mask_out)# # compile AV_model# AV_model.compile(optimizer='adam', loss='mse')return AV_model

这个大佬太强了,自愧不如。

这篇关于Looking to Listen at the Cocktail Party 代码详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/278322

相关文章

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python

Mysql 中的多表连接和连接类型详解

《Mysql中的多表连接和连接类型详解》这篇文章详细介绍了MySQL中的多表连接及其各种类型,包括内连接、左连接、右连接、全外连接、自连接和交叉连接,通过这些连接方式,可以将分散在不同表中的相关数据... 目录什么是多表连接?1. 内连接(INNER JOIN)2. 左连接(LEFT JOIN 或 LEFT

Java中ArrayList的8种浅拷贝方式示例代码

《Java中ArrayList的8种浅拷贝方式示例代码》:本文主要介绍Java中ArrayList的8种浅拷贝方式的相关资料,讲解了Java中ArrayList的浅拷贝概念,并详细分享了八种实现浅... 目录引言什么是浅拷贝?ArrayList 浅拷贝的重要性方法一:使用构造函数方法二:使用 addAll(

Java中switch-case结构的使用方法举例详解

《Java中switch-case结构的使用方法举例详解》:本文主要介绍Java中switch-case结构使用的相关资料,switch-case结构是Java中处理多个分支条件的一种有效方式,它... 目录前言一、switch-case结构的基本语法二、使用示例三、注意事项四、总结前言对于Java初学者

Linux内核之内核裁剪详解

《Linux内核之内核裁剪详解》Linux内核裁剪是通过移除不必要的功能和模块,调整配置参数来优化内核,以满足特定需求,裁剪的方法包括使用配置选项、模块化设计和优化配置参数,图形裁剪工具如makeme... 目录简介一、 裁剪的原因二、裁剪的方法三、图形裁剪工具四、操作说明五、make menuconfig

JAVA利用顺序表实现“杨辉三角”的思路及代码示例

《JAVA利用顺序表实现“杨辉三角”的思路及代码示例》杨辉三角形是中国古代数学的杰出研究成果之一,是我国北宋数学家贾宪于1050年首先发现并使用的,:本文主要介绍JAVA利用顺序表实现杨辉三角的思... 目录一:“杨辉三角”题目链接二:题解代码:三:题解思路:总结一:“杨辉三角”题目链接题目链接:点击这里

SpringBoot使用注解集成Redis缓存的示例代码

《SpringBoot使用注解集成Redis缓存的示例代码》:本文主要介绍在SpringBoot中使用注解集成Redis缓存的步骤,包括添加依赖、创建相关配置类、需要缓存数据的类(Tes... 目录一、创建 Caching 配置类二、创建需要缓存数据的类三、测试方法Spring Boot 熟悉后,集成一个外

详解Java中的敏感信息处理

《详解Java中的敏感信息处理》平时开发中常常会遇到像用户的手机号、姓名、身份证等敏感信息需要处理,这篇文章主要为大家整理了一些常用的方法,希望对大家有所帮助... 目录前后端传输AES 对称加密RSA 非对称加密混合加密数据库加密MD5 + Salt/SHA + SaltAES 加密平时开发中遇到像用户的

Springboot使用RabbitMQ实现关闭超时订单(示例详解)

《Springboot使用RabbitMQ实现关闭超时订单(示例详解)》介绍了如何在SpringBoot项目中使用RabbitMQ实现订单的延时处理和超时关闭,通过配置RabbitMQ的交换机、队列和... 目录1.maven中引入rabbitmq的依赖:2.application.yml中进行rabbit