卷积LSTM网络

简历
演示了卷积LSTM网络的使用。

这个脚本演示了卷积LSTM网络的使用。该网络用于预测人工生成的包含移动方块的电影的下一帧。

图书馆(keras)图书馆(abind)图书馆(光栅)

函数定义

generate_movies<-函数n_samples =1200n_frames =15) {<-80关口<-80noisy_movies<-数组0昏暗的=c(n_samples, n_frames, rows, cols))shifted_movies<-数组0昏暗的=c(n_samples, n_frames, rows, cols))n<-样本3.81(年代1n_samples) {(我1n) {#初始位置xstart<-样本20.601ystart<-样本20.601#运动方向directionx<-样本-111directiony<-样本-111#正方形的大小w<-样本23.1x_shift<-xstart+directionx0(n_frames))y_shift<-ystart+directiony0(n_frames))(t1n_frames) {square_x<-(x_shift [t]-w)(x_shift [t]+w)square_y<-(y_shift [t]-w)(y_shift [t]+w)Noisy_movies [s, t, square_x, square_y]<-Noisy_movies [s, t, square_x, square_y]+1#通过添加噪声使其更健壮。这个想法是如果#在推断时,像素的值不准确#;我们需要将网络训练得健壮而稳定#把它看作是属于一个正方形的像素。如果runif1>0.5) {noise_f<-样本c-11),1square_x_n<-(x_shift [t]-w-1(x_shift [t]+w+1square_y_n<-(y_shift [t]-w-1(y_shift [t]+w+1Noisy_movies [s, t, square_x_n, square_y_n]<-Noisy_movies [s, t, square_x_n, square_y_n]+noise_f0.1#将ground truth移1square_x_s<-(x_shift [t+1]-w)(x_shift [t+1]+w)square_y_s<-(y_shift [t+1]-w)(y_shift [t+1]+w)Shifted_movies [s, t, square_x_s, square_y_s]<-Shifted_movies [s, t, square_x_s, square_y_s]+1#剪切到40x40的窗口noisy_movies<-noisy_movies [,,21602160]shifted_moviesshifted_movies [,,21602160]noisy_movies [noisy_movies>1]<-1shifted_movies [shifted_movies>1]<-1#添加通道维度noisy_movies<-array_reshape(noisy_moviesc昏暗的(noisy_movies),1))shifted_movies<-array_reshape(shifted_moviesc昏暗的(shifted_movies),1))列表noisy_movies =noisy_movies,shifted_movies =shifted_movies)

数据准备

人工数据生成:

  • 生成3到7个移动方块的电影。
  • 正方形的形状为1x1或2x2像素,它们随时间线性移动。
  • 为了方便起见,我们首先创建宽高较大的电影(80x80)
  • 最后我们选择一个40x40的窗口。
电影<-generate_moviesn_samples =1000n_frames =15more_movies<-generate_moviesn_samples =200n_frames =15

模型定义

#初始化模型模型<-keras_model_sequential()模型%>%#从2D卷积LSTM层开始layer_conv_lstm_2dinput_shape =列表40401),过滤器=40kernel_size =c3.3.),填充=“相同”return_sequences =真正的%>%#正常化前一层的激活layer_batch_normalization()%>%#添加3x隐藏的2D卷积LSTM层,与#批处理归一化层之间layer_conv_lstm_2d过滤器=40kernel_size =c3.3.),填充=“相同”return_sequences =真正的%>%layer_batch_normalization()%>%layer_conv_lstm_2d过滤器=40kernel_size =c3.3.),填充=“相同”return_sequences =真正的%>%layer_batch_normalization()%>%layer_conv_lstm_2d过滤器=40kernel_size =c3.3.),填充=“相同”return_sequences =真正的%>%layer_batch_normalization()%>%添加最终的3D卷积输出层layer_conv_3d过滤器=1kernel_size =c3.3.3.),激活=“乙状结肠”填充=“相同”data_format =“channels_last”为训练准备模型模型%>%编译损失=“binary_crossentropy”优化器=“adadelta”模型

培训

模型%>%适合电影noisy_movies,电影shifted_movies,batch_size =10时代=30.validation_split =0.05

可视化

#在一部电影上测试网络#喂食前7个位置,然后#预测新职位#要可视化的示例哪一个<-One hundred.跟踪<-more_moviesnoisy_movies (,18,,,1]跟踪<-数组(跟踪,c1840401))(k115) {如果(k<8) {pngpaste0(k,“_animate.png”))票面价值mfrow =c12),bg =“白色”(more_moviesnoisy_movies (k……1])%>%光栅()%>%情节()%>%标题主要=paste0“Ground_”k))(more_moviesnoisy_movies (k……1])%>%光栅()%>%情节()%>%标题主要=paste0“Ground_”k))dev.off()其他的{#然后将预测与实际情况进行比较pngpaste0(k,“_animate.png”))票面价值mfrow =c12),bg =“白色”(more_moviesnoisy_movies (k……1])%>%光栅()%>%情节()%>%标题主要=paste0“Ground_”k))#做预测new_pos<-模型%>%预测(跟踪)#切片最后一行new_pos_loc<-new_pos [1、钾、1401401]new_pos_loc%>%光栅()%>%情节()%>%标题主要=paste0“Pred_”k))#重塑它new_pos<-数组(new_pos_locc1140401))#将其绑定到早期数据跟踪<-abind(跟踪、new_pos沿着=2dev.off()#也可以通过运行来创建gif系统"convert -delay 40 *.png animation.gif"

动画