图书馆(keras)图书馆(abind)图书馆(光栅)
卷积LSTM网络
简历
演示了卷积LSTM网络的使用。
这个脚本演示了卷积LSTM网络的使用。该网络用于预测人工生成的包含移动方块的电影的下一帧。
函数定义
<-函数(n_samples =1200,n_frames =15) { generate_movies<-80 行<-80 关口<-数组(0,昏暗的=c(n_samples, n_frames, rows, cols)) noisy_movies<- shifted_movies数组(0,昏暗的=c(n_samples, n_frames, rows, cols))<-样本(3.:8,1) n为(年代在1:n_samples) {为(我在1:n) {#初始位置<-样本(20.:60,1) xstart<-样本(20.:60,1) ystart#运动方向<-样本(-1:1,1) directionx<-样本(-1:1,1) directiony#正方形的大小<-样本(2:3.,1) w<-xstart+directionx*(0:(n_frames)) x_shift<-ystart+directiony*(0:(n_frames)) y_shift为(t在1:n_frames) {<-(x_shift [t]-w):(x_shift [t]+w) square_x<-(y_shift [t]-w):(y_shift [t]+w) square_y<- Noisy_movies [s, t, square_x, square_y]+1 Noisy_movies [s, t, square_x, square_y]#通过添加噪声使其更健壮。这个想法是如果#在推断时,像素的值不准确#;我们需要将网络训练得健壮而稳定#把它看作是属于一个正方形的像素。如果(runif(1)>0.5) {<-样本(c(-1,1),1) noise_f<-(x_shift [t]-w-1):(x_shift [t]+w+1) square_x_n<-(y_shift [t]-w-1):(y_shift [t]+w+1) square_y_n<- Noisy_movies [s, t, square_x_n, square_y_n]+noise_f*0.1 Noisy_movies [s, t, square_x_n, square_y_n] }#将ground truth移1<-(x_shift [t+1]-w):(x_shift [t+1]+w) square_x_s<-(y_shift [t+1]-w):(y_shift [t+1]+w) square_y_s<- Shifted_movies [s, t, square_x_s, square_y_s]+1 Shifted_movies [s, t, square_x_s, square_y_s] } } }#剪切到40x40的窗口<-noisy_movies [,,21:60,21:60] noisy_movies=shifted_movies [,,21:60,21:60] shifted_movies>1]<-1 noisy_movies [noisy_movies>1]<-1 shifted_movies [shifted_movies#添加通道维度<- noisy_moviesarray_reshape(noisy_moviesc(昏暗的(noisy_movies),1))<- shifted_moviesarray_reshape(shifted_moviesc(昏暗的(shifted_movies),1))列表(noisy_movies =noisy_movies,shifted_movies =shifted_movies) }
数据准备
人工数据生成:
- 生成3到7个移动方块的电影。
- 正方形的形状为1x1或2x2像素,它们随时间线性移动。
- 为了方便起见,我们首先创建宽高较大的电影(80x80)
- 最后我们选择一个40x40的窗口。
<-generate_movies(n_samples =1000,n_frames =15) 电影<-generate_movies(n_samples =200,n_frames =15) more_movies
模型定义
#初始化模型<-keras_model_sequential() 模型%>% 模型#从2D卷积LSTM层开始layer_conv_lstm_2d(input_shape =列表(零,40,40,1),过滤器=40,kernel_size =c(3.,3.),填充=“相同”,return_sequences =真正的%>% )#正常化前一层的激活layer_batch_normalization()%>%#添加3x隐藏的2D卷积LSTM层,与#批处理归一化层之间layer_conv_lstm_2d(过滤器=40,kernel_size =c(3.,3.),填充=“相同”,return_sequences =真正的%>% )layer_batch_normalization()%>%layer_conv_lstm_2d(过滤器=40,kernel_size =c(3.,3.),填充=“相同”,return_sequences =真正的%>% )layer_batch_normalization()%>%layer_conv_lstm_2d(过滤器=40,kernel_size =c(3.,3.),填充=“相同”,return_sequences =真正的%>% )layer_batch_normalization()%>%添加最终的3D卷积输出层layer_conv_3d(过滤器=1,kernel_size =c(3.,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_movies$noisy_movies (,1:8,,,1] 跟踪<-数组(跟踪,c(1,8,40,40,1)) 跟踪为(k在1:15) {如果(k<8) {png(paste0(k,“_animate.png”))票面价值(mfrow =c(1,2),bg =“白色”)$noisy_movies (k……1])%>%光栅()%>%情节()%>%标题(主要=paste0(“Ground_”k)) (more_movies$noisy_movies (k……1])%>%光栅()%>%情节()%>%标题(主要=paste0(“Ground_”k)) (more_moviesdev.off()其他的{ }#然后将预测与实际情况进行比较png(paste0(k,“_animate.png”))票面价值(mfrow =c(1,2),bg =“白色”)$noisy_movies (k……1])%>%光栅()%>%情节()%>%标题(主要=paste0(“Ground_”k)) (more_movies#做预测<-模型%>%预测(跟踪) new_pos#切片最后一行<-new_pos [1、钾、1:40,1:40,1] new_pos_loc%>%光栅()%>%情节()%>%标题(主要=paste0(“Pred_”k)) new_pos_loc#重塑它<-数组(new_pos_locc(1,1,40,40,1)) new_pos#将其绑定到早期数据<-abind(跟踪、new_pos沿着=2) 跟踪dev.off() } }#也可以通过运行来创建gif系统("convert -delay 40 *.png animation.gif")