class_docstr: Train the SVM for the low-shot image classification tasks. Currently,
  datasets like VOC07 and Places205 are supported.
  
  The trained inherits from the SVMTrainer class and takes care of
  training SVM, evaluating, and aggregate the metrics.
class_docstr_tok: ["Train","the","SVM","for","the","low-shot","image","classification","tasks",".","Currently",",","datasets","like","VOC07","and","Places205","are","supported",".","The","trained","inherits","from","the","SVMTrainer","class","and","takes","care","of","training","SVM",",","evaluating",",","and","aggregate","the","metrics","."]
docstr: Train SVM on the input features and targets for a given low-shot
  k-value and the independent low-shot sample number.
  
  We save the trained SVM model for each combination:
      cost value, class number, sample number, k-value
docstr_tok: ["Train","SVM","on","the","input","features","and","targets","for","a","given","low-shot","k-value","and","the","independent","low-shot","sample","number",".","We","save","the","trained","SVM","model","for","each","combination",":","cost","value",",","class","number",",","sample","number",",","k-value"]
code: def train(self, features, targets, sample_num, low_shot_kvalue):
      """
      Train SVM on the input features and targets for a given low-shot
      k-value and the independent low-shot sample number.
  
      We save the trained SVM model for each combination:
          cost value, class number, sample number, k-value
      """
      logging.info("Training Low-shot SVM")
      if self.normalize:
          # normalize the features: N x 9216 (example shape)
          features = self._normalize_features(features)
  
      # get the class lists to train low-shot SVM classifier on
      self.cls_list = self._get_cls_list(targets)
      for cls_idx in range(len(self.cls_list)):
          cls_num = self.cls_list[cls_idx]
          for cost_idx in range(len(self.costs_list)):
              cost = self.costs_list[cost_idx]
              suffix = f"sample{sample_num}_k{low_shot_kvalue}"
              out_file = self._get_svm_low_shot_model_filename(
                  cls_num, cost, suffix
              )
              if (
                  g_pathmgr.exists(out_file)
                  and not self.config.force_retrain
              ):
                  logging.info(f"SVM model exists: {out_file}")
                  continue
              logging.info(f"Training model with the cost: {cost}")
              clf = LinearSVC(
                  C=cost,
                  class_weight={1: 2, -1: 1},
                  intercept_scaling=1.0,
                  verbose=1,
                  penalty=self.config["penalty"],
                  loss=self.config["loss"],
                  tol=0.0001,
                  dual=self.config["dual"],
                  max_iter=self.config["max_iter"],
              )
              (
                  train_feats,
                  train_cls_labels,
              ) = self._get_cls_feats_labels(cls_num, features, targets)
              num_positives = len(np.where(train_cls_labels == 1)[0])
              num_negatives = len(np.where(train_cls_labels == -1)[0])
              logging.info(
                  f"cls: {cls_num} has +ve: {num_positives} -ve: {num_negatives} "
                  f"ratio: {float(num_positives) / num_negatives} "
                  f"features: {train_feats.shape} "
                  f"cls_labels: {train_cls_labels.shape}"
              )
              clf.fit(train_feats, train_cls_labels)
              logging.info(f"Saving SVM model to: {out_file}")
              with g_pathmgr.open(out_file, "wb") as fwrite:
                  pickle.dump(clf, fwrite)
      logging.info(
          f"Done training: sample: {sample_num} k-value: {low_shot_kvalue}"
      )
  
file_path: vissl/vissl/utils/svm_utils/svm_low_shot_trainer.py
imports: import logging
  import pickle
  import numpy
  from iopath.common.file_io import g_pathmgr
  from sklearn.svm import LinearSVC
  from vissl.utils.io import load_file, save_file
  from vissl.utils.svm_utils.evaluate import get_precision_recall
  from vissl.utils.svm_utils.svm_trainer import SVMTrainer
label_desc: Use image node_id 5 for calling the SVMLowShotTrainer obj's underlying member method code with example usage: obj.train(features, targets, sample_num, low_shot_kvalue) without return types