You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整数组结构向ConvLSTM2D输入3张图像?

解决方案

1. 输入/输出数组的正确形状转换

ConvLSTM2D的输入格式要求为 (batch_size, time_steps, height, width, channels),各维度含义:

  • batch_size:样本总数(即你准备的「3帧输入+1帧输出」的组数)
  • time_steps:输入序列的图像数量(这里固定为3)
  • height/width:单张图像的尺寸(你的案例中是64x64)
  • channels:图像通道数(灰度图为1)

按照你的数据准备逻辑,转换数组的代码如下:

import numpy as np

# 处理输入数据:转换为numpy数组并添加通道维度
input_d = np.array(input_d, dtype=np.float32)
# 从 (num_samples, 3, 64, 64) 变为 (num_samples, 3, 64, 64, 1)
input_d = input_d[..., np.newaxis]

# 处理输出数据:转换为numpy数组并添加通道维度
output_d = np.array(output_d, dtype=np.float32)
# 从 (num_samples, 1, 64, 64) 变为 (num_samples, 1, 64, 64, 1)
output_d = output_d[..., np.newaxis]

2. 模型输入形状的调整

你现有模型的input_shape=(None, 64, 64, 1)完全可用:其中None表示time_steps(序列长度)支持任意值,传入3完全适配。如果需要固定序列长度,也可以改为input_shape=(3, 64, 64, 1),两种写法均能正常运行。

3. 额外注意事项

  • 统一图像尺寸:在nrmlz_i函数中建议添加resize步骤,避免因原始图像尺寸不一致导致形状错误:
    def nrmlz_i(pths):
        for li in range(len(pths)):
            img = cv2.imread("timages/"+pths[li], cv2.IMREAD_GRAYSCALE)
            img = cv2.resize(img, (64, 64))  # 强制统一为64x64
            img_n = np.array(img)/255
            pths[li] = img_n
        return pths
    
  • 输出形状匹配:你的模型最后一层是Conv3D,由于前面的ConvLSTM2D都设置了return_sequences=True,模型输出形状为(batch_size, time_steps, 64, 64, 1),与转换后的output_d形状完全匹配,无需修改模型结构。

内容的提问来源于stack exchange,提问作者Павел Биза

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 10:36:50