Search code examples
tensorflowclassificationimage-classification

Can you combine two NNs in TensorFlow?


I have a collection of images, for example, cats and dogs. I also have a CSV accompanying this. The CSV has metadata for the images like the weight of the animal.

I have made a classifier for the cats VS dogs images. How can I use the CSV with the metadata to improve this classifier? Do I need to make a separate classifier for the metadata and combine these two classifiers?

Sorry if this is a stupid question but I can't find anything online and I don't even know the term for what I am looking for.

Thank you for taking the time to read this


Solution

  • Yes you can, in Keras you could use the functional API as explained in detail in this post.

    Your code should look like this:

    # define two sets of inputs
    inputA = Input(shape=(32,))
    inputB = Input(shape=(128,))
    
    # the first branch operates on the first input
    x = Dense(8, activation="relu")(inputA)
    x = Dense(4, activation="relu")(x)
    x = Model(inputs=inputA, outputs=x)
    
    # the second branch opreates on the second input
    y = Dense(64, activation="relu")(inputB)
    y = Dense(32, activation="relu")(y)
    y = Dense(4, activation="relu")(y)
    y = Model(inputs=inputB, outputs=y)
    
    # combine the output of the two branches
    combined = concatenate([x.output, y.output])
    
    # apply a FC layer and then a regression prediction on the
    # combined outputs
    z = Dense(2, activation="relu")(combined)
    z = Dense(1, activation="linear")(z)
    
    # our model will accept the inputs of the two branches and
    # then output a single value
    model = Model(inputs=[x.input, y.input], outputs=z)