title: How to feed tensor to pre-trained model in the computational graph with keras?
body: <p>I want to train a specific conditional <code>GAN</code> with some deterministic constraints at the end of my generator with <code>Keras</code> and to do so I need first to compute the embeddings of my Generator outputs with <code>VGG-16 pre-trained model</code>. </p>
  
  <p>I'm using <code>python 3.6</code>.</p>
  
  <p>In my computational Graph, I want to feed my Generator outputs <code>img</code> to a pre-trained VGG-16 model in order to get the embeddings.</p>
  
  <p>My <code>img</code> is then a tensor of shape (None,224,224,3) since I am in the computational Graph. Thing is if i compile the following i get the error </p>
  
  <blockquote>
    <p>When feeding symbolic tensors to a model, we expect the tensors to
    have a static batch size. Got tensor with shape: (None, 224, 224, 3)</p>
  </blockquote>
  
  <pre><code>self.vgg = self.build_vgg()
  
  def build_vgg(self):
      vgg16_model = keras.applications.vgg16.VGG16()
      return Model(inputs=vgg16_model.input,outputs=vgg16_model.get_layer('fc2').output)
  
          #-------------------------------
      # Construct Computational Graph
      #         for Generator
      #-------------------------------
  
      # For the generator we freeze the critic's layers
      self.critic.trainable = False
      self.generator.trainable = True
      self.vgg.trainable = False
  
  
      # Sampled noise for input to generator
      noise = Input(shape=(self.latent_dim,))
  
      # Input Embedding:
      embedding = Input(shape=(self.embedding,))
  
  
      # Generate images based of noise
  
      img = self.generator([noise,embedding])
  
      # Discriminator determines validity
  
      valid = self.critic(img)
  
      # Get the embeddings from vgg-16:
      X = self.vgg.predict(img)
  </code></pre>
  
  <p>Obviously, I can't loop along the first axis since it's None index. I tried to apply a function to this 'img' tensor using the tensorflow function <strong>'tf.map_fn'</strong> like the following :</p>
  
  <pre><code>    def Embedding(self,img):
      fn = lambda x: self.vgg.predict(preprocess_input(np.expand_dims(x, axis=0))).flatten()
      embedding = tf.map_fn(fn,img,dtype=tf.float32)
      return embedding
  
          #-------------------------------
      # Construct Computational Graph
      #         for Generator
      #-------------------------------
  
      # For the generator we freeze the critic's layers
      self.critic.trainable = False
      self.generator.trainable = True
      self.vgg.trainable = False
  
      # Sampled noise for input to generator
      noise = Input(shape=(self.latent_dim,))
  
      # Input Embedding:
      embedding = Input(shape=(self.embedding,))
  
  
      # Generate images based of noise
  
      img = self.generator([noise,embedding])
  
      # Discriminator determines validity
  
      valid = self.critic(img)
  
      # Get the embeddings from VGG16
      X = self.Embedding(img)
  </code></pre>
  
  <p>But i get the following error:</p>
  
  <blockquote>
    <p>ValueError: setting an array element with a sequence.</p>
  </blockquote>
  
  <p>To recap, I want to apply a <code>pre-trained VGG-16 model</code> on a <code>tensor</code> with shape (None,224,224,3) along the Batch_Size Axis (0) in the computational graph in <code>Keras</code>. What i explained to you before is what I already tried...</p>
  
  <p>Does anyone have any suggestion to this ?</p>