构建卷积自动编码器时的尺寸错误

时间:2016-10-12 11:50:24

标签: python python-2.7 deep-learning keras autoencoder

我正在采取我在Keras的第一步,并努力与我的图层的尺寸。我正在构建一个卷积自动编码器,我想用MNIST数据集进行训练。不幸的是,我似乎无法确定尺寸,而且我很难理解我的错误在哪里。

我的模型是通过以下方式构建的:

def build_model(nb_filters=32, nb_pool=2, nb_conv=3):
    input_img = Input(shape=(1, 28, 28))

    x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img)
    x = MaxPooling2D((2, 2), border_mode='same')(x)
    x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
    x = MaxPooling2D((2, 2), border_mode='same')(x)
    x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
    encoded = MaxPooling2D((2, 2), border_mode='same')(x)

    x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)
    x = UpSampling2D((2, 2))(x)
    x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
    x = UpSampling2D((2, 2))(x)
    x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x)
    x = UpSampling2D((2, 2))(x)
    decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)

return Model(input_img, decoded)

并使用以下方法检索数据:

def load_data():
    (x_train, _), (x_test, _) = mnist.load_data()

    x_train = x_train.astype('float32') / 255.
    x_test = x_test.astype('float32') / 255.
    x_train = np.reshape(x_train, (len(x_train), 1, 28, 28))
    x_test = np.reshape(x_test, (len(x_test), 1, 28, 28))
    return x_train, x_test

如您所见,我正在尝试将图像标准化以黑白显示,并简单地训练自动编码器以便能够恢复它们。

下面你可以看到我得到的错误:

  

Traceback(最近一次调用最后一次):文件   “C:/Users//Documents/GitHub/main/research/research_framework/experiment.py”   第46行,在       callbacks = [EarlyStopping(耐心= 3)])文件“C:\ Users \ AppData \ Local \ Continuum \ Anaconda2 \ lib \ site-packages \ keras \ engine \ training.py”,   第1047行,合适       batch_size = batch_size)文件“C:\ Users \ AppData \ Local \ Continuum \ Anaconda2 \ lib \ site-packages \ keras \ engine \ training.py”,   第978行,在_standardize_user_data中       exception_prefix ='model target')文件“C:\ Users \ AppData \ Local \ Continuum \ Anaconda2 \ lib \ site-packages \ keras \ engine \ training.py”,   第111行,在standardize_input_data中       str(array.shape))异常:检查模型目标时出错:预期卷积2d_7有形状(无,8,32,1)但得到数组   形状(60000L,1L,28L,28L)总参数:8273

     

使用退出代码1完成处理

你可以帮助我解决这个错误吗? Keras网站之外是否有关于建立模型和处理此类问题的材料?

干杯

2 个答案:

答案 0 :(得分:2)

看起来您的输入形状不正确。尝试将(1,28,28)更改为(28,28,1)并查看是否适合您。有关解决问题的更多详细信息和其他选项,请参阅the answer to another question

答案 1 :(得分:1)

原因是当我在keras.json中更改后端配置时,我没有更改图像dimanesion,所以它仍然设置为tensorflow。

将其更改为:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class ExampleFrame extends JFrame {

    private JPanel drawPanel = new DrawPanel();
    private Timer timer;
    private int alpha = 255;
    private final int TIMER_TICK = 50;
    private final int ALPHA_TICK_VALUE = 3;

    private class DrawPanel extends JPanel {

        final int PANEL_HEIGHT = 80;
        final int PANEL_WIDTH = 100;
        final int TEXT_MARGIN = 20;

        DrawPanel() {
            setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Color color = new Color(0, 0, 0, alpha);
            g.setColor(color);
            g.drawString("Hello World", TEXT_MARGIN,
                    PANEL_HEIGHT / 2 + g.getFontMetrics().getHeight() / 2);
        }

    }

    public void createAndShow() {
        getContentPane().add(drawPanel);
        timer = new Timer(TIMER_TICK, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                alpha -= ALPHA_TICK_VALUE;
                if (alpha >= 0) {
                    drawPanel.repaint();
                } else {
                    alpha = 0;
                    timer.stop();
                }
            }
        });
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        timer.start();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ExampleFrame ef = new ExampleFrame();
                ef.createAndShow();
            }
        });
    }
}

做了这个伎俩。