How do I build a Fully Convolutional Neural Network in Keras?
<p>I am working with Keras and when attempting to implement a FCNN with a similar architecture to VGG, I get the error:</p>

<blockquote>
  <p>TypeError: img must be 4D tensor</p>
</blockquote>

<p>Here is my code for the FCNN:</p>

<pre><code>def buildmodel():
  model = Sequential()
  model.add(Convolution2D(4,3,3,border_mode='same',input_shape=(3,64,64)))
  model.add(LeakyReLU(alpha=0.3))
  model.add(MaxPooling2D((2,2),strides=(2,2),border_mode='valid', dim_ordering='th'))
  model.add(Convolution2D(8,3,3,border_mode='same'))
  model.add(LeakyReLU(alpha=0.3))
  model.add(MaxPooling2D((2,2),strides=(2,2),border_mode='valid', dim_ordering='th'))

  model.add(UpSampling2D((2,2)))
  model.add(Convolution2D(16,3,3,border_mode='same',input_shape=x[0].shape))
  model.add(LeakyReLU(alpha=0.3))
  model.add(MaxPooling2D((2,2),strides=(2,2),border_mode='valid', dim_ordering='th'))
  model.add(Convolution2D(32,3,3,border_mode='same'))
  model.add(LeakyReLU(alpha=0.3))
  model.add(MaxPooling2D((2,2),strides=(2,2),border_mode='valid', dim_ordering='th'))

  model.add(UpSampling2D((2,2)))
  model.add(Convolution2D(64,3,3,border_mode='same',input_shape=x[0].shape))
  model.add(LeakyReLU(alpha=0.3))
  model.add(Convolution2D(64,3,3,border_mode='same'))
  model.add(LeakyReLU(alpha=0.3))
  model.add(MaxPooling2D((2,2),strides=(2,2),border_mode='valid', dim_ordering='th'))

  model.add(UpSampling2D((2,2)))
  model.add(Convolution2D(128,3,3,border_mode='same',input_shape=x[0].shape))
  model.add(LeakyReLU(alpha=0.3))
  model.add(Convolution2D(128,3,3,border_mode='same'))
  model.add(LeakyReLU(alpha=0.3))
  model.add(MaxPooling2D((2,2),strides=(2,2),border_mode='valid', dim_ordering='th'))

  model.add(UpSampling2D((2,2)))
  model.add(Convolution2D(4,3,3,border_mode='same',input_shape=x[0].shape))
  model.add(LeakyReLU(alpha=0.3))
  model.add(Convolution2D(1,3,3,border_mode='same',activation='sigmoid'))

  return model

inputs = Input(shape=(len(x),3,64,64))
fcnn = buildmodel()
outputs = fcnn(inputs)
print(fcnn.inbound_nodes)
opt = RMSprop(lr=0.001, rho=0.9, epsilon=1e-6) 
model = Model(input=inputs, output=outputs)
model.compile(loss='hinge', optimizer=opt)
model.fit(x)
</code></pre>

<p>x has a shape (nsamples,3,64,64). When using the VGG model verbatim in Keras with fully connected layers, there seemed to be no problem, so I'm confused as to how the new architecture is causing problems with the image shape. </p>