Skip to main content

Merge mutiple models or embedding into a single one very easily
in Pytorch.





## Usage

https://colab.research.google.com/drive/1vOFxEcLQdgCxCCJCkp-mxouTQ1f8F5FX?usp=sharing



### Example Short:

import os, random, numpy as np, pandas as pd ;from box import Box
from copy import deepcopy
import copy, collections
import torch.nn as nn
import torchvision

##### only this
from torchmerge import merge as me


#############################################################################################
def test3d():    
   from box import Box ; from copy import deepcopy
   from torch.utils.data import DataLoader, TensorDataset
   

   ARG = Box({
       'MODE'   : 'mode1',
       'DATASET': {},
       'MODEL_INFO' : {},
   })
   PARAMS = {}


   ##################################################################
   if ARG.MODE == 'mode1':
       ARG.MODEL_INFO.TYPE = 'dataonly' 
       train_config                           = Box({})
       train_config.LR                        = 0.001
       train_config.SEED                      = 42
       train_config.DEVICE                    = 'cpu'
       train_config.BATCH_SIZE                = 64
       train_config.EPOCHS                    = 1
       train_config.EARLY_STOPPING_THLD       = 10
       train_config.VALID_FREQ                = 1
       train_config.SAVE_FILENAME             = './model.pt'
       train_config.TRAIN_RATIO               = 0.7
       train_config.VAL_RATIO                 = 0.2
       train_config.TEST_RATIO                = 0.1


   ####################################################################
   def load_DataFrame():
       return None


   def test_dataset_f_mnist(samples=100):
       from sklearn.model_selection import train_test_split
       from torchvision import transforms, datasets
       # Generate the transformations
       train_list_transforms = [transforms.ToTensor(),transforms.Lambda(lambda x: x.repeat(3, 1, 1))]

       dataset1 = datasets.FashionMNIST(root="data",train=True,
                                        transform=transforms.Compose(train_list_transforms),download=True,)
       
       #sampling the requred no. of samples from dataset 
       dataset1 = torch.utils.data.Subset(dataset1, np.arange(samples))
       X,Y    = [],  []
       for data, targets in dataset1:
           X.append(data)
           Y.append(targets)

       #Converting list to tensor format
       X,y = torch.stack(X),torch.Tensor(Y)

       train_r, test_r, val_r  = train_config.TRAIN_RATIO, train_config.TEST_RATIO,train_config.VAL_RATIO
       train_X, test_X, train_y, test_y = train_test_split(X,  y,  test_size=1 - train_r)
       valid_X, test_X, valid_y, test_y = train_test_split(test_X, test_y, test_size= test_r / (test_r + val_r))
       return (train_X, train_y, valid_X, valid_y, test_X , test_y)


   def prepro_dataset(self,df:pd.DataFrame=None):
       train_X ,train_y,valid_X ,valid_y,test_X, test_y = test_dataset_f_mnist(samples=100)
       return train_X ,train_y,valid_X ,valid_y,test_X,test_y


   
   ### modelA  ########################################################
   from torchvision import  models
   model_ft = models.resnet18(pretrained=True)
   embA_dim = model_ft.fc.in_features  ###

   ARG.modelA               = {}   
   ARG.modelA.name          = 'resnet18'
   ARG.modelA.nn_model      = model_ft
   ARG.modelA.layer_emb_id  = 'fc'
   ARG.modelA.architect     = [ embA_dim]  ### head s
   modelA = me.model_create(ARG.modelA)
   


   ### modelB  ########################################################
   from torchvision import  models
   model_ft = models.resnet50(pretrained=True)
   embB_dim = int(model_ft.fc.in_features)

   ARG.modelB               = {}   
   ARG.modelB.name          = 'resnet50'
   ARG.modelB.nn_model      = model_ft
   ARG.modelB.layer_emb_id  = 'fc'
   ARG.modelB.architect     = [embB_dim ]   ### head size
   modelB = me.model_create(ARG.modelB )




   ### merge_model  ###################################################
   ### EXPLICIT DEPENDENCY  
   ARG.merge_model           = {}
   ARG.merge_model.name      = 'modelmerge1'

   ARG.merge_model.architect                  = {}
   ARG.merge_model.architect.input_dim        =  embA_dim + embB_dim 

   ARG.merge_model.architect.merge_type       = 'cat'
   ARG.merge_model.architect.merge_layers_dim = [1024, 768]  ### Common embedding is 768
   ARG.merge_model.architect.merge_custom     = None


   ### Custom head
   ARG.merge_model.architect.head_layers_dim  = [ 128, 1]    ### Specific task    
   ARG.merge_model.architect.head_custom      = None
 
 
   ARG.merge_model.dataset       = {}
   ARG.merge_model.dataset.dirin = "/"
   ARG.merge_model.dataset.coly = 'ytarget'
   ARG.merge_model.train_config  = train_config


   model = me.MergeModel_create(ARG, model_create_list= [modelA, modelB ] )
   model.build()



   #### Run Model   ###################################################
   model.training(load_DataFrame, prepro_dataset) 

   model.save_weight('ztmp/model_x5.pt')
   model.load_weights('ztmp/model_x5.pt')
   inputs = torch.randn((train_config.BATCH_SIZE,3,28,28)).to(model.device)
   outputs = model.predict(inputs)
   print(outputs)













Metadata

Release files for torchmerge 0.1.16540980

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchmerge 0.1.16540980
File Size Uploaded
torchmerge-0.1.16540980.tar.gz 9.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torchmerge 0.1.16540980
File Interpreter ABI Platform
torchmerge-0.1.16540980-py3-none-any.whl Python 3 none any Details

Total release size: 17.5 kB

Release files / torchmerge-0.1.16540980.tar.gz

Download URL torchmerge-0.1.16540980.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
66463a58bc357b9eaeec25e7c6e7938b6ec74db29fe05ea25ef2fad254096106
BLAKE2b-256 checksum
How to use checksums
165e8f04c7fb9138f320285fac18b31191b43a7350e473f13e639bc9d95198c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / torchmerge-0.1.16540980-py3-none-any.whl

Download URL torchmerge-0.1.16540980-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7027dbd52966554095ed1dfc910ef9720c77186915ab50c8f174364ab91a49da
BLAKE2b-256 checksum
How to use checksums
aa2dc190fbec1e8eccc016a0fa361ac35acfc838c3a4e4877e084cf04ef623eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release history Release notifications | RSS feed

This release

0.1.16540980 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page