def _maybe_download(self):
     # For files in dropbox or google drive, cannot directly use request to download
     # This can be changed directly use download_file method when the file is stored in server
     if not os.path.exists(self.checkpoint_path) or self.overwrite:
         checkpoint_google_id = Constant.PRE_TRAIN_VOICE_GENERATOR_MODEL_GOOGLE_DRIVE_ID
         download_file_from_google_drive(file_id=checkpoint_google_id, dest_path=self.checkpoint_path,
                                         overwrite=self.overwrite)
Beispiel #2
0
def cached_path(file_info, cache_dir=None):
    if cache_dir is None:
        cache_dir = PYTORCH_PRETRAINED_BERT_CACHE

    os.makedirs(cache_dir, exist_ok=True)
    file_path = os.path.join(cache_dir, file_info.local_name)

    if not os.path.exists(file_path):
        download_file_from_google_drive(file_id=file_info.google_drive_id,
                                        dest_path=file_path,
                                        verbose=True)
    return file_path
    def load(self):

        self.device = get_device()
        self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)

        output_model_file = os.path.join(tempfile.gettempdir(), 'text_sentiment_pytorch_model.bin')

        download_file_from_google_drive(TEXT_SENTIMENT_FILE_ID, output_model_file)

        model_state_dict = torch.load(output_model_file, map_location=lambda storage, loc: storage)
        self.model = BertForSequenceClassification.from_pretrained('bert-base-uncased', state_dict=model_state_dict)
        self.model.to(self.device)
Beispiel #4
0
 def load(self, model_path=None):
     temp_path = temp_path_generator()
     ensure_dir(temp_path)
     model_paths = [
         f'{temp_path}/{file_name}'
         for file_name in Constant.FACE_DETECTOR['MODEL_NAMES']
     ]
     for google_id, file_name in zip(
             Constant.FACE_DETECTOR['MODEL_GOOGLE_ID'],
             Constant.FACE_DETECTOR['MODEL_NAMES']):
         download_file_from_google_drive(
             file_id=google_id, dest_path=f'{temp_path}/{file_name}')
     return model_paths
    def load(self):
        self.device = get_device()
        self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased',
                                                       do_lower_case=True)

        output_model_file = os.path.join(tempfile.gettempdir(), self.model_dir)

        download_file_from_google_drive(self.file_id, output_model_file)

        model_state_dict = torch.load(
            output_model_file, map_location=lambda storage, loc: storage)
        self.model = BertForSequenceClassification.from_pretrained(
            'bert-base-uncased',
            state_dict=model_state_dict,
            num_labels=self.num_classes)
        self.model.to(self.device)
Beispiel #6
0
 def __init__(self, verbose=True, model_path=None):
     """Initialize the instance."""
     self.verbose = verbose
     self.model = None
     self.device = get_device()
     self.model_path = model_path if model_path is not None else temp_path_generator(
     )
     ensure_dir(self.model_path)
     self.local_paths = [
         os.path.join(self.model_path, x.local_name)
         for x in self._google_drive_files
     ]
     for path, x in zip(self.local_paths, self._google_drive_files):
         if not os.path.exists(path):
             download_file_from_google_drive(file_id=x.google_drive_id,
                                             dest_path=path,
                                             verbose=True)
    def load(self, model_path=None):

        if model_path is None:
            model_file_name = Constant.OBJECT_DETECTOR['MODEL_NAME']
            temp_path = temp_path_generator()
            ensure_dir(temp_path)
            model_path = f'{temp_path}/{model_file_name}'
            download_file_from_google_drive(file_id=Constant.OBJECT_DETECTOR['MODEL_GOOGLE_ID'], dest_path=model_path)
        # load net
        num_classes = len(VOC_CLASSES) + 1  # +1 for background
        self.model = self._build_ssd('test', 300, num_classes)  # initialize SSD
        if self.device.startswith("cuda"):
            self.model.load_state_dict(torch.load(model_path))
        else:
            self.model.load_state_dict(torch.load(model_path, map_location=lambda storage, loc: storage))
        self.model.eval()
        print('Finished loading model!')

        self.model = self.model.to(self.device)