首页 文章

如何在Keras处理大图像?

提问于
浏览
0

我正在研究Keras的机器学习问题 . 我的目标是训练一个模型,其输入是 (4214, 22311) 矩阵,输出是 (22311,) vector .

这样的二维数据可以被视为图像输入,但是大多数图像分类器仅适用于具有形状 (200, 200) 的小图像 .

我尝试了几种方法在Keras上构建模型,例如CNN,双向LSTM或简单的神经网络(扁平和密集) . 但它们都不起作用 .

我没有完成一个时代就收到了 killed . 所以我想知道什么样的结构可以处理如此大的输入 .

enter image description here

1 回答

  • 0

    很有可能它可以在完全卷积模型中工作,你必须在任何类型的压扁/密集之前消除空间尺寸 . 但是因为有太多的卷积只有相同的数据,所以它似乎不是最好的想法....

    而且由于你想要一个22311结果的输出,可能LSTM方法(考虑一个不同的变量是第一个)将是最有用的....

    An LSTM model:

    在第一次尝试时,您可以尝试非状态LSTM模型 .

    但是不要像上一个维度是功能一样工作(20000个功能将创建许多参数的方式)!好像他们是时间步骤一样工作 .

    逐步转换功能可能会使模型更难以理解 . 另一方面,如果你不这样做,你的记忆总会爆发 . 但是,如果一个不同的数据是第一步,那么所有以下(和重复的)数据很有可能获得一些额外的含义 .

    所以,首先,我们只重塑一个功能:

    inp = Input((4214,1))
    out = Lambda(yourFunction, output_shape=(4214,22312))(inp)
    out = Reshape((4214,22312,1))(out)
    

    现在,让我们尝试一个技巧,使其好像是一个序列序列,添加TimeDistributed包装器(这允许增加维度):

    out = TimeDistributed(LSTM(cells,return_sequences=True))(out)
    
    #you may add more
    

    让我们保持 return_sequences = True ,因为您希望模型末尾的步数非常相似 .

    在某些时候,让's collapse the 4214 dimension, placing it at the end, as if it were the time steps (or even as if it were features - risky, but a lot less risky than 23312 features). Here, since they'独立, Bidirectional 包装器可能会派上用场:

    out = Permute((2,1,3))(out)
    

    1 - 如果您选择4214步骤:

    #you can use one or more layers with return_sequences=True before this one
    out = TimeDistributed(Bidirectional(LSTM(cells,return_sequences=False)))(out)
    

    2 - 如果您选择4214功能:

    #remove the last dimension which was 1
    out = Reshape((22312,4214))  
    
    #no need to care about they being a sequence, no TimeDistributed, no Bidirectional. 
    out = LSTM(cells,return_sequences=True)(out)
        #the return_sequences=True here relates to the 22312 steps   
    
    #but you need to go back to 1 feature per step:
    out = Reshape((22312,1))(out)
    

    现在4214尺寸已经崩溃,现在一切都像 (batchSize, 22312, 1) 那样形成了一个序列 . 由于我们不再是4D,而是3D,我们可以放弃TimeDistributed包装器:

    out = LSTM(cells,return_sequences=True)(out)
    

    最后,使用lambda图层丢弃此序列的第一个元素(只是为了匹配您想要的输出):

    out = Lambda(lambda x: x[:,1:,:], output_shape=(22311,1))(out)
    out = Reshape((22311,))(out)
    
    model = Model(inp,out)
    

相关问题