Merge the trained models

Hi there, i am very new to the ML field and i’m excited to learn as much as possible from this forum’s members.

Recently I’ve tried the image classification at, it’s so easy to follow and i am really amazed by its training result. So, I trained the machine to recognized a couple of things and export the result into a model. Today i trained the machine again to recognize a new class / category of objects, but I do not know how to merge the model has been exported previously with the new one. I’ve read the articles about transfer learning, but i am not sure whether this technique can be / how to applied in this case .

Hi @hangsien ,

Yes, you can apply transfer learning in this case as well.

Please follow the below steps:

Load the previously exported model into your machine learning framework.
Freeze the layers of the previously trained model so that they will not be updated during the new training process.
Add new layers to the model to accommodate the new category of objects that you want to recognize.
Train the model on your new dataset, which includes the old dataset as well as the new category of objects. During training, only the newly added layers will be updated, while the old layers will remain frozen.
Test the model on a new set of images to evaluate its accuracy and performance.
Export the updated model that includes the old and new layers and use it for image classification.

I hope this helps you.


Hi @Laxma_Reddy_Patlolla ,

Thanks for your willingness to lend a helping hand and spending time to explaining the steps. I tried merge them together but it throw me an error:

const path = require("path");
const fs   = require("fs");
const tf   = require("@tensorflow/tfjs-node");

const model1Path     = path.join(__dirname, "../assets/models/trained/dogsCats/model.json");
const model2Path     = path.join(__dirname, "../assets/models/trained/rabbitsBirds/model.json");
const predictionPath = path.join(__dirname, `../assets/images/rabbits`);

const inputHeight = 224;
const inputWidth  = 224;

const mergeModels = async () => {
  /** ===== TO MERGE THE MODELS ===== **/

  // loading dogs & cats model
  let model1 = await tf.loadLayersModel(`file://${model1Path}`);

  // loading rabbits & birds model
  let model2 = await tf.loadLayersModel(`file://${model2Path}`);

  // freeze the model layers
  for (let layer of model1.layers) {
    layer.trainable = false;

  // freeze the model layers
  for (let layer of model2.layers) {
    layer.trainable = false;

  const combinedModel = tf.sequential();


  /** ===== TO MAKE PREDICTION ===== **/

  const filename    = "1-rabbit.jpg";
  const imagePath   = `${predictionPath}/${filename}`;

  const imageBuffer = fs.readFileSync(imagePath);
  const imageTensor = tf.node.decodeImage(imageBuffer, 3);

  // resize the image to 224 x 224
  const smallTensor = tf.image.resizeBilinear(imageTensor, [inputHeight, inputWidth], true);

  let result = combinedModel.predict(smallTensor.div(255).expandDims()).squeeze();
  const { values, indices } = tf.topk(result, 3);

module.exports = { mergeModels };