title: Keras beginner: what is supposed to be the output shape of the last layer?
body: <p>I'm having a hard time wrapping my head around the math behind CNN's and how exactly I should modify the output shape in between layers of my neural network.</p>
  
  <p>I am trying to do the carvana image masking challenge on kaggle <a href="https://www.kaggle.com/c/carvana-image-masking-challenge" rel="nofollow noreferrer">https://www.kaggle.com/c/carvana-image-masking-challenge</a> . So in other words, I'm trying to create a neural network that, given a picture of a car, can identify the boundaries of the car within that image and crop it out from the rest of the background.</p>
  
  <p>So my inputs are all images, width=959px and height=640px. The shape of my input array is (159, 640, 959, 3) where 159 represents the fact that the input array holds 159 images in total. 
  The targets I created are matrices with 640 rows and 959 columns (an entry for each pixel), using booleans to represent whether or not the corresponding pixel is a car/within the boundaries of a car. The shape of the target data is (159, 640, 959) where 159 likely represents the fact that the target holds 159 images</p>
  
  <p>I created a prematurely structured convolutional network (by that I just mean, there's very few filters used). The code for the architecture is here.</p>
  
  <pre><code>  nn = Sequential()
  
    nn.add(Conv2D(8,(3,3), input_shape = (IMG_HEIGHT, IMG_WIDTH, 3),    activation = 'relu', padding = 'same'))
    nn.add(Conv2D(8, (3,3), activation='relu', padding='same'))
    nn.add(Dense(1, activation='softmax'))
  </code></pre>
  
  <p>Summary() shows the following:</p>
  
  <pre><code># Summary:
  Layer (type)                 Output Shape              Param #   
  =================================================================
  conv2d_1 (Conv2D)            (None, 640, 959, 8)       224       
  _________________________________________________________________
  conv2d_2 (Conv2D)            (None, 640, 959, 8)       584       
  _________________________________________________________________
  dense_1 (Dense)              (None, 640, 959, 6)       54        
  =================================================================
  Total params: 862
  Trainable params: 862
  </code></pre>
  
  <p>And the error I've been stuck with is just...</p>
  
  <p>ValueError: Error when checking target: expected dense_1 to have 4 dimensions, but got array with shape (159, 640, 959)</p>
  
  <p>at the moment I'm actually not sure how I would modify this code to make it work and get past this error. I'm confused about how the last layer is supposed to have 4 dimensions. It seems according to Keras's summary this output actually does have 4 dimensions, but one of those dimensions is marked as None. If the output isn't supposed to have a shape of (640, 959), just like each target image... I don't really know what the shape of the output is supposed to be.
  I'm just having a hard time putting what I have previously learned about regarding convolutional networks into actual code. I can't get past this error and I am currently struggling to figure out how. There's something fundamental that I'm not doing correctly...</p>
  
  <p>edit: originally said the images had a shape of 440px X 959px. This is incorrect, it's actually 640px X 959px. Really inconvenient typo on my part.</p>