pyanomaly/core/engine/abstract/abstract_engine.py
def _load_file(self, model_keys, model_file):
        """Method to load the data into pytorch structure.
        This function satifies many situations
        Args:
            model_keys: The keys of the trainer's model
            model_file: The data of the model
        """
        def load_state(item, saved_model_file, parallel=False):
            # import ipdb; ipdb.set_trace()
            temp_list = list(saved_model_file.keys())
            temp = temp_list[0]
            logger.info(f'=> The first key in model file is {temp}')
            # import ipdb; ipdb.set_trace()
            if not parallel:
                # if the trainer NOT uses the data parallel to train
                if temp.startswith('module.'):
                    # if the saved model file is in the Dataparallel 
                    new_state_dict = OrderedDict()
                    for k, v in saved_model_file.items():
                        name = k[7:]
                        new_state_dict[name] = v
                    logger.info('(|+_+|) => Change the DataParallel Model File to Normal')
                    getattr(self, item).load_state_dict(new_state_dict)
                else:
                    getattr(self, item).load_state_dict(saved_model_file)
            else:
                # if the trainer uses the data parallel to train
                if not temp.startswith('module.'):
                    # if the saved model file is in the Dataparallel
                    new_state_dict = OrderedDict()
                    for k, v in saved_model_file.items():
                        # name = k[7:]
                        name = 'module.' + k
                        new_state_dict[name] = v
                    logger.info('(|+_+|) => Change the Normal Model File to DataParallel')
                    getattr(self, item).load_state_dict(new_state_dict)
                else:
                    getattr(self, item).load_state_dict(saved_model_file)
        
        # import ipdb; ipdb.set_trace()
        parallel_flag = self.kwargs['parallel']
        for item in model_keys:
            item = str(item)
            if 'state_dict' in model_file.keys():
                logger.info('\033[5;31m!! Directly use the file, the state_dict is in file\033[0m')
                # getattr(self, item).load_state_dict(model_file['state_dict'])
                load_state(item, model_file['state_dict'], parallel_flag)
                continue

            if item not in model_file.keys():
                # The name of the layer is the key in model file. For example:
                # {'conv1.weight':....., 'conv1.bias':.....}
                logger.info('\033[5;31m!! Directly use the file\033[0m')
                # getattr(self, item).load_state_dict(model_file)
                load_state(item, model_file, parallel_flag)
            elif item in model_file.keys():
                if 'state_dict' in model_file[item].keys():
                    # The name of the 
                    # getattr(self, item).load_state_dict(model_file[item]['state_dict'])
                    load_state(item, model_file[item]['state_dict'], parallel_flag)
                else:
                    logger.info('\033[5;31m!! Not have the state_dict \033[0m')
                    # getattr(self, item).load_state_dict(model_file[item])
                    load_state(item, model_file[item], parallel_flag)
           
        logger.info('Finish load the weights into the networks!')
Method to load the data into pytorch structure.
        This function satifies many situations
        Args:
            model_keys: The keys of the trainer's model
            model_file: The data of the model
        
Method to load the data into pytorch structure.
This function satifies many situations
Method to load the data into pytorch structure . This function satifies many situations
def _load_file(self, model_keys, model_file):
        def load_state(item, saved_model_file, parallel=False):
            temp_list = list(saved_model_file.keys())
            temp = temp_list[0]
            logger.info(f'=> The first key in model file is {temp}')
            if not parallel:
                if temp.startswith('module.'):
                    new_state_dict = OrderedDict()
                    for k, v in saved_model_file.items():
                        name = k[7:]
                        new_state_dict[name] = v
                    logger.info('(|+_+|) => Change the DataParallel Model File to Normal')
                    getattr(self, item).load_state_dict(new_state_dict)
                else:
                    getattr(self, item).load_state_dict(saved_model_file)
            else:
                if not temp.startswith('module.'):
                    new_state_dict = OrderedDict()
                    for k, v in saved_model_file.items():
                        name = 'module.' + k
                        new_state_dict[name] = v
                    logger.info('(|+_+|) => Change the Normal Model File to DataParallel')
                    getattr(self, item).load_state_dict(new_state_dict)
                else:
                    getattr(self, item).load_state_dict(saved_model_file)
        parallel_flag = self.kwargs['parallel']
        for item in model_keys:
            item = str(item)
            if 'state_dict' in model_file.keys():
                logger.info('\033[5;31m!! Directly use the file, the state_dict is in file\033[0m')
                load_state(item, model_file['state_dict'], parallel_flag)
                continue
            if item not in model_file.keys():
                logger.info('\033[5;31m!! Directly use the file\033[0m')
                load_state(item, model_file, parallel_flag)
            elif item in model_file.keys():
                if 'state_dict' in model_file[item].keys():
                    load_state(item, model_file[item]['state_dict'], parallel_flag)
                else:
                    logger.info('\033[5;31m!! Not have the state_dict \033[0m')
                    load_state(item, model_file[item], parallel_flag)
        logger.info('Finish load the weights into the networks!')
def _load_file ( self , model_keys , model_file ) : def load_state ( item , saved_model_file , parallel = False ) : temp_list = list ( saved_model_file . keys ( ) ) temp = temp_list [ 0 ] logger . info ( f'=> The first key in model file is {temp}' ) if not parallel : if temp . startswith ( 'module.' ) : new_state_dict = OrderedDict ( ) for k , v in saved_model_file . items ( ) : name = k [ 7 : ] new_state_dict [ name ] = v logger . info ( '(|+_+|) => Change the DataParallel Model File to Normal' ) getattr ( self , item ) . load_state_dict ( new_state_dict ) else : getattr ( self , item ) . load_state_dict ( saved_model_file ) else : if not temp . startswith ( 'module.' ) : new_state_dict = OrderedDict ( ) for k , v in saved_model_file . items ( ) : name = 'module.' + k new_state_dict [ name ] = v logger . info ( '(|+_+|) => Change the Normal Model File to DataParallel' ) getattr ( self , item ) . load_state_dict ( new_state_dict ) else : getattr ( self , item ) . load_state_dict ( saved_model_file ) parallel_flag = self . kwargs [ 'parallel' ] for item in model_keys : item = str ( item ) if 'state_dict' in model_file . keys ( ) : logger . info ( '\033[5;31m!! Directly use the file, the state_dict is in file\033[0m' ) load_state ( item , model_file [ 'state_dict' ] , parallel_flag ) continue if item not in model_file . keys ( ) : logger . info ( '\033[5;31m!! Directly use the file\033[0m' ) load_state ( item , model_file , parallel_flag ) elif item in model_file . keys ( ) : if 'state_dict' in model_file [ item ] . keys ( ) : load_state ( item , model_file [ item ] [ 'state_dict' ] , parallel_flag ) else : logger . info ( '\033[5;31m!! Not have the state_dict \033[0m' ) load_state ( item , model_file [ item ] , parallel_flag ) logger . info ( 'Finish load the weights into the networks!' )
Method to load the data into pytorch structure.
Method to load the data into pytorch structure .
"""Method to load the data into pytorch structure.
        This function satifies many situations
        Args:
            model_keys: The keys of the trainer's model
            model_file: The data of the model
        """ # import ipdb; ipdb.set_trace()
 # import ipdb; ipdb.set_trace()
 # if the trainer NOT uses the data parallel to train
 # if the saved model file is in the Dataparallel 
 # if the trainer uses the data parallel to train
 # if the saved model file is in the Dataparallel
 # name = k[7:]
 # import ipdb; ipdb.set_trace()
 # getattr(self, item).load_state_dict(model_file['state_dict'])
 # The name of the layer is the key in model file. For example:
 # {'conv1.weight':....., 'conv1.bias':.....}
 # getattr(self, item).load_state_dict(model_file)
 # The name of the 
 # getattr(self, item).load_state_dict(model_file[item]['state_dict'])
 # getattr(self, item).load_state_dict(model_file[item])

self
model_keys
The keys of the trainer's model
The keys of the trainer ' s model
model_file
The data of the model
The data of the model