def __init__(self, embeddings, w2i, c2i, model_params): self._w2i = w2i self._c2i = c2i model = NERRNNModel(embeddings=embeddings, **model_params) if torch.cuda.is_available(): self._system = pw.System(model, last_activation=nn.Softmax(dim=-1), device=torch.device('cuda')) else: self._system = pw.System(model, last_activation=nn.Softmax(dim=-1), device=torch.device('cpu'))
def __init__(self, pretrained_bert_name, model_params): self._pretrained_bert_name = pretrained_bert_name bert_model = AutoModel.from_pretrained(pretrained_bert_name) model = XNLIBERTModel(bert_model, **model_params) if torch.cuda.is_available(): self._system = pw.System(model, last_activation=nn.Softmax(dim=-1), device=torch.device('cuda')) else: self._system = pw.System(model, last_activation=nn.Softmax(dim=-1), device=torch.device('cpu'))
def load_anomaly_detector(ad_model_path, device): assert path.exists(ad_model_path) logging.info(f"Loading anomaly detector from {ad_model_path}") anomaly_detector = pw.System(model=AnomalyDetector(), device=device) anomaly_detector.load_model_state(ad_model_path) return anomaly_detector.model.eval()
def load_models(feature_extractor_path, ad_model_path, features_method='c3d', device='cuda'): """ Loads both feature extractor and anomaly detector from the given paths :param feature_extractor_path: path of the features extractor weights to load :param ad_model_path: path of the anomaly detector weights to load :param features_method: name of the model to use for features extraction :param device: device to use for the models :return: anomaly_detector, feature_extractor """ assert path.exists(feature_extractor_path) assert path.exists(ad_model_path) feature_extractor, anomaly_detector = None, None if features_method == 'c3d': logging.info(f"Loading feature extractor from {feature_extractor_path}") feature_extractor = C3D(pretrained=feature_extractor_path) else: raise NotImplementedError(f"Features extraction method {features_method} not implemented") logging.info(f"Loading anomaly detector from {ad_model_path}") feature_extractor = feature_extractor.to(device).eval() anomaly_detector = pw.System(model=AnomalyDetector(), device=device) anomaly_detector.load_model_state(ad_model_path) anomaly_detector = anomaly_detector.model.eval() return anomaly_detector, feature_extractor
def __init__(self, pretrained_bert_name, preprocessing_function, bert_like_special_tokens, model_params): self._pretrained_bert_name = pretrained_bert_name bert_model = AutoModel.from_pretrained(pretrained_bert_name) model = NERBERTModel(bert_model, **model_params) self._preprocessing_function = preprocessing_function self._bert_like_special_tokens = bert_like_special_tokens if torch.cuda.is_available(): self._system = pw.System(model, last_activation=nn.Softmax(dim=-1), device=torch.device('cuda')) else: self._system = pw.System(model, last_activation=nn.Softmax(dim=-1), device=torch.device('cpu'))
if __name__ == "__main__": args = get_args() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") data_loader = FeaturesLoaderVal(features_path=args.features_path, annotation_path=args.annotation_path) data_iter = torch.utils.data.DataLoader( data_loader, batch_size=1, shuffle=False, num_workers=0, # 4, # change this part accordingly pin_memory=True) network = AnomalyDetector() system = pw.System(model=network, device=device) system.load_model_state(args.model_path) # enable cudnn tune cudnn.benchmark = True y_trues = torch.tensor([]) y_preds = torch.tensor([]) with torch.no_grad(): for features, start_end_couples, lengths in tqdm(data_iter): # features is a batch where each item is a tensor of 32 4096D features features = features.to(device) outputs = system.model(features).squeeze(-1) # (batch_size, 32) for vid_len, couples, output in zip(lengths, start_end_couples, outputs.cpu().numpy()):