title: How to modify layers of pretrained models in Keras like Inception-v3?
body: <p>I want to use Inception-v3 with pretrained weights on ImageNet to take inputs that are not just 3 channel RGB images but have more channels, such that the dimension is <em>(224, 224, x!=3)</em>, and then assigning a self-defined set of weights to the following Conv2D layer. I was trying to change the input layer and the subsequent Conv2D layer such that it suits my needs, but I could not find a structured way of doing so.</p>
  
  <p>I tried building a custom Conv2d tensor with <strong>Conv2D(...)(input)</strong> and assigning that to the corresponding layer of Inception, but this fails because it requires actual layers, while the above instruction yields a tensor. For all it matters, <strong>Conv2D(...)(Input)</strong> and <strong>Inception.layers[1].output</strong> yields the correct same output (which it should be since I just want to change the input dimensions and weights), the question is how to wrap the new Conv2D input-output mapping as a layer and replace it in Inception?</p>
  
  <p>I could try hacking my way through this, but generally I wondered if there is a swift and elegant way of reassigning certain layers in those pretrained models with custom specifications.</p>
  
  <p>Thank you!</p>
  
  <p>Edit:
  What works is inserting these lines at line 394 of the inception_v3.py from Keras, disabling the exception for more than 3 channel inputs and then simply calling the constructor with the desired input. (Note that <strong>Original calls the original InceptionV3 constructor</strong>)</p>
  
  <p>Code:</p>
  
  <pre><code>original_model = Original(weights='imagenet', include_top=False, input_shape=(299, 299, 3))
  weights = model.get_weights()
  original_weights = original_model.get_weights()
  
  for i in range(1, len(original_weights)):
         weights[i] = original_weights[i]
  averaged_weights = np.mean(weights[0], axis=2)[:, :, None, :]
  replicated_weights = np.repeat(averaged_weights, 20, axis=2)
  weights[0] = replicated_weights
  </code></pre>
  
  <p>Then I can call</p>
  
  <pre><code>InceptionV3(weights='imagenet', include_top=False, input_shape=(299, 299, 20))
  </code></pre>
  
  <p>This work and gives the desired result, but seems very hacky.</p>