Ejemplo n.º 1
0
def print_board(board: typ.List[typ.List[int]]) -> None:
    board = [[board_element(cell) for cell in row] for row in board]
    table = AsciiTable(board)
    table.inner_row_border = True
    print(table.table)
Ejemplo n.º 2
0
def coco_eval(result_files,
              result_types,
              coco,
              max_dets=(100, 300, 1000),
              classwise=False):
    for res_type in result_types:
        assert res_type in [
            'proposal', 'proposal_fast', 'bbox', 'segm', 'keypoints'
        ]

    if mmcv.is_str(coco):
        coco = COCO(coco)
    assert isinstance(coco, COCO)

    if result_types == ['proposal_fast']:
        ar = fast_eval_recall(result_files, coco, np.array(max_dets))
        for i, num in enumerate(max_dets):
            print('AR@{}\t= {:.4f}'.format(num, ar[i]))
        return

    for res_type in result_types:
        if isinstance(result_files, str):
            result_file = result_files
        elif isinstance(result_files, dict):
            result_file = result_files[res_type]
        else:
            assert TypeError('result_files must be a str or dict')
        assert result_file.endswith('.json')

        coco_dets = coco.loadRes(result_file)
        img_ids = coco.getImgIds()
        iou_type = 'bbox' if res_type == 'proposal' else res_type
        cocoEval = COCOeval(coco, coco_dets, iou_type)
        cocoEval.params.imgIds = img_ids
        if res_type == 'proposal':
            cocoEval.params.useCats = 0
            cocoEval.params.maxDets = list(max_dets)
        cocoEval.evaluate()
        cocoEval.accumulate()
        cocoEval.summarize()

        if classwise:
            # Compute per-category AP
            # from https://github.com/facebookresearch/detectron2/blob/03064eb5bafe4a3e5750cc7a16672daf5afe8435/detectron2/evaluation/coco_evaluation.py#L259-L283 # noqa
            precisions = cocoEval.eval['precision']
            catIds = coco.getCatIds()
            # precision has dims (iou, recall, cls, area range, max dets)
            assert len(catIds) == precisions.shape[2]

            results_per_category = []
            for idx, catId in enumerate(catIds):
                # area range index 0: all area ranges
                # max dets index -1: typically 100 per image
                nm = coco.loadCats(catId)[0]
                precision = precisions[:, :, idx, 0, -1]
                precision = precision[precision > -1]
                ap = np.mean(precision) if precision.size else float('nan')
                results_per_category.append(
                    ('{}'.format(nm['name']),
                     '{:0.3f}'.format(float(ap * 100))))

            N_COLS = min(6, len(results_per_category) * 2)
            results_flatten = list(itertools.chain(*results_per_category))
            headers = ['category', 'AP'] * (N_COLS // 2)
            results_2d = itertools.zip_longest(
                *[results_flatten[i::N_COLS] for i in range(N_COLS)])
            table_data = [headers]
            table_data += [result for result in results_2d]
            table = AsciiTable(table_data)
            print(table.table)
Ejemplo n.º 3
0
        compact_model_metric = eval_model(compact_model)

    metric_table = [["Metric", "Before", "After"],
                    [
                        "mAP", f'{origin_model_metric[0][2]:.6f}',
                        f'{compact_model_metric[0][2]:.6f}'
                    ],
                    [
                        "Parameters", f"{origin_nparameters}",
                        f"{compact_nparameters}"
                    ],
                    [
                        "Inference", f'{pruned_forward_time:.4f}',
                        f'{compact_forward_time:.4f}'
                    ]]
    print(AsciiTable(metric_table).table)

    # pruned_cfg_name = opt.cfg.replace('/', f'/prune_{opt.global_percent}_keep_{opt.layer_keep}_')
    import os

    pruned_cfg_name = '/home/ai/DeepGit/yolov3-channel-and-layer-pruning/v4.3' + f'/prune_{opt.global_percent}_keep_{opt.layer_keep}_' + '.cfg'
    # pruned_cfg_name = os.path.join('./cfg', f'/prune_{opt.global_percent}_keep_{opt.layer_keep}_' + '.cfg')
    pruned_cfg_file = write_cfg(pruned_cfg_name, [model.hyperparams.copy()] +
                                compact_module_defs)
    print(f'Config file has been saved: {pruned_cfg_file}')

    # compact_model_name = opt.weights.replace('/', f'/prune_{opt.global_percent}_keep_{opt.layer_keep}_')
    compact_model_name = '/home/ai/DeepGit/yolov3-channel-and-layer-pruning/v4.3' + f'/prune_{opt.global_percent}_keep_{opt.layer_keep}_' + '.weights'
    if compact_model_name.endswith('.pt'):
        compact_model_name = compact_model_name.replace('.pt', '.weights')
    save_weights(compact_model, path=compact_model_name)
Ejemplo n.º 4
0
def run():
    print_environment_info()
    parser = argparse.ArgumentParser(description="Trains the YOLO model.")
    parser.add_argument("-m",
                        "--model",
                        type=str,
                        default="config/yolov3.cfg",
                        help="Path to model definition file (.cfg)")
    parser.add_argument("-d",
                        "--data",
                        type=str,
                        default="config/coco.data",
                        help="Path to data config file (.data)")
    parser.add_argument("-e",
                        "--epochs",
                        type=int,
                        default=300,
                        help="Number of epochs")
    parser.add_argument(
        "-c",
        "--continue_from",
        type=int,
        default=0,
        help="Continue from epoch # (checkpoint file required)")
    parser.add_argument("-v",
                        "--verbose",
                        action='store_true',
                        help="Makes the training more verbose")
    parser.add_argument(
        "--n_cpu",
        type=int,
        default=8,
        help="Number of cpu threads to use during batch generation")
    parser.add_argument(
        "--pretrained_weights",
        type=str,
        help=
        "Path to checkpoint file (.weights or .pth). Starts training from checkpoint model"
    )
    parser.add_argument("--checkpoint_interval",
                        type=int,
                        default=1,
                        help="Interval of epochs between saving model weights")
    parser.add_argument(
        "--evaluation_interval",
        type=int,
        default=1,
        help="Interval of epochs between evaluations on validation set")
    parser.add_argument("--multiscale_training",
                        action="store_false",
                        help="Allow for multi-scale training")
    parser.add_argument(
        "--iou_thres",
        type=float,
        default=0.5,
        help="Evaluation: IOU threshold required to qualify as detected")
    parser.add_argument("--conf_thres",
                        type=float,
                        default=0.1,
                        help="Evaluation: Object confidence threshold")
    parser.add_argument(
        "--nms_thres",
        type=float,
        default=0.5,
        help="Evaluation: IOU threshold for non-maximum suppression")
    parser.add_argument(
        "--logdir",
        type=str,
        default="logs",
        help="Directory for training log files (e.g. for TensorBoard)")
    parser.add_argument("--seed",
                        type=int,
                        default=-1,
                        help="Makes results reproducable. Set -1 to disable.")
    args = parser.parse_args()
    print(f"Command line arguments: {args}")

    if args.seed != -1:
        provide_determinism(args.seed)

    logger = Logger(args.logdir)  # Tensorboard logger

    # Create output directories if missing
    os.makedirs("output", exist_ok=True)
    os.makedirs("checkpoints", exist_ok=True)

    # Get data configuration
    data_config = parse_data_config(args.data)
    train_path = data_config["train"]
    valid_path = data_config["valid"]
    class_names = load_classes(data_config["names"])
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    # ############
    # Create model
    # ############

    pretrained_weights = args.pretrained_weights
    if not pretrained_weights and args.continue_from:
        if os.path.exists(f"checkpoints/yolov3_ckpt_{args.continue_from}.pth"):
            pretrained_weights = f"checkpoints/yolov3_ckpt_{args.continue_from}.pth"
            print(
                f"located the checkpoint file: checkpoints/yolov3_ckpt_{args.continue_from}.pth, will continue."
            )
        else:
            print(
                f"can not find the checkpoint file of epoch {args.continue_from}, will start over."
            )

    print(f"now continue from the pretrained_weights: {pretrained_weights}")
    model = load_model(args.model, pretrained_weights)

    # Print model
    if args.verbose:
        summary(model,
                input_size=(3, model.hyperparams['height'],
                            model.hyperparams['height']))

    mini_batch_size = model.hyperparams['batch'] // model.hyperparams[
        'subdivisions']

    # #################
    # Create Dataloader
    # #################

    # Load training dataloader
    dataloader = _create_data_loader(train_path, mini_batch_size,
                                     model.hyperparams['height'], args.n_cpu,
                                     args.multiscale_training)

    # Load validation dataloader
    validation_dataloader = _create_validation_data_loader(
        valid_path, mini_batch_size, model.hyperparams['height'], args.n_cpu)

    # ################
    # Create optimizer
    # ################

    params = [p for p in model.parameters() if p.requires_grad]

    if (model.hyperparams['optimizer'] in [None, "adam"]):
        optimizer = optim.Adam(
            params,
            lr=model.hyperparams['learning_rate'],
            weight_decay=model.hyperparams['decay'],
        )
    elif (model.hyperparams['optimizer'] == "sgd"):
        optimizer = optim.SGD(params,
                              lr=model.hyperparams['learning_rate'],
                              weight_decay=model.hyperparams['decay'],
                              momentum=model.hyperparams['momentum'])
    else:
        print("Unknown optimizer. Please choose between (adam, sgd).")

    for epoch in range(args.epochs):

        if epoch <= args.continue_from:
            continue

        print("\n---- Training Model ----")

        model.train()  # Set model to training mode

        for batch_i, (_, imgs, targets) in enumerate(
                tqdm.tqdm(dataloader, desc=f"Training Epoch {epoch}")):
            batches_done = len(dataloader) * epoch + batch_i

            imgs = imgs.to(device, non_blocking=True)
            targets = targets.to(device)

            outputs = model(imgs)

            loss, loss_components = compute_loss(outputs, targets, model)

            loss.backward()

            ###############
            # Run optimizer
            ###############

            if batches_done % model.hyperparams['subdivisions'] == 0:
                # Adapt learning rate
                # Get learning rate defined in cfg
                lr = model.hyperparams['learning_rate']
                if batches_done < model.hyperparams['burn_in']:
                    # Burn in
                    lr *= (batches_done / model.hyperparams['burn_in'])
                else:
                    # Set and parse the learning rate to the steps defined in the cfg
                    for threshold, value in model.hyperparams['lr_steps']:
                        if batches_done > threshold:
                            lr *= value
                # Log the learning rate
                logger.scalar_summary("train/learning_rate", lr, batches_done)
                # Set learning rate
                for g in optimizer.param_groups:
                    g['lr'] = lr

                # Run optimizer
                optimizer.step()
                # Reset gradients
                optimizer.zero_grad()

            # ############
            # Log progress
            # ############
            if args.verbose:
                print(
                    AsciiTable([
                        ["Type", "Value"],
                        ["IoU loss", float(loss_components[0])],
                        ["Object loss",
                         float(loss_components[1])],
                        ["Class loss", float(loss_components[2])],
                        ["Loss", float(loss_components[3])],
                        ["Batch loss", to_cpu(loss).item()],
                    ]).table)

            # Tensorboard logging
            tensorboard_log = [("train/iou_loss", float(loss_components[0])),
                               ("train/obj_loss", float(loss_components[1])),
                               ("train/class_loss", float(loss_components[2])),
                               ("train/loss", to_cpu(loss).item())]
            logger.list_of_scalars_summary(tensorboard_log, batches_done)

            model.seen += imgs.size(0)

        # #############
        # Save progress
        # #############

        # Save model to checkpoint file
        if epoch % args.checkpoint_interval == 0:
            checkpoint_path = f"checkpoints/yolov3_ckpt_{epoch}.pth"
            print(f"---- Saving checkpoint to: '{checkpoint_path}' ----")
            torch.save(model.state_dict(), checkpoint_path)

        # ########
        # Evaluate
        # ########

        if epoch % args.evaluation_interval == 0:
            print("\n---- Evaluating Model ----")
            # Evaluate the model on the validation set
            metrics_output = _evaluate(model,
                                       validation_dataloader,
                                       class_names,
                                       img_size=model.hyperparams['height'],
                                       iou_thres=args.iou_thres,
                                       conf_thres=args.conf_thres,
                                       nms_thres=args.nms_thres,
                                       verbose=args.verbose)

            if metrics_output is not None:
                precision, recall, AP, f1, ap_class = metrics_output
                evaluation_metrics = [("validation/precision",
                                       precision.mean()),
                                      ("validation/recall", recall.mean()),
                                      ("validation/mAP", AP.mean()),
                                      ("validation/f1", f1.mean())]
                logger.list_of_scalars_summary(evaluation_metrics, epoch)
Ejemplo n.º 5
0
def Strategy_PingPong():
  OrderStatus = ''
  OrderID = ''
  OrderSide = ''
  comSell = ''
  comBuy = ''
  symbol = Settings.symbolPP
  base_price = Settings.base_pricePP
  #budget_BTC = Settings.budget_BTCPP
  start_operation = Settings.start_operationPP
  up_profit = Settings.up_profitPP
  down_profit = Settings.down_profitPP
  k = 0
  a=1

  while k <1:
    if symbol == Symbols.SymbolsMatrix[a][0]:
      le = len(str(Symbols.SymbolsMatrix[a][2]))
      k = 1
    else:
      a = int(a) + 1
  
  #print(str(le))

  while True:
    try:
      time.sleep(1)
      balanceALT = client.get_asset_balance(asset=str(symbol), recvWindow=1000000)
      balanceALTJSON = json.dumps(balanceALT)
      balanceALTRESP = json.loads(balanceALTJSON)
      balanceALTFREE = balanceALTRESP['free']
      budget_ALT = balanceALTFREE
      balanceBTC = client.get_asset_balance(asset='BTC', recvWindow=1000000)
      balanceBTCJSON = json.dumps(balanceBTC)
      balanceBTCRESP = json.loads(balanceBTCJSON)
      balanceBTCFREE = balanceBTCRESP['free']
      budget_BTC = float(balanceBTCFREE) * float(Settings.use_budget_BTCPP_procent)
      budget_BTC = round(budget_BTC,8)
      budget_BTC = decimal.Decimal(budget_BTC)
      budget_BTC = str(budget_BTC)[0:10]

      price = client.get_symbol_ticker(symbol=str(symbol)+"BTC")
      priceJSON = json.dumps(price)
      priceRESP = json.loads(priceJSON)
      price = priceRESP['price']
      aprofit = float(price) / float(base_price) - 1
      aprofit = float(aprofit) * 100
      aprofit = round(aprofit,2)
      up_profit = float(Settings.up_profitPP)
      down_profit = float(Settings.down_profitPP)
      up_price = float(up_profit) * float(base_price)
      up_price = decimal.Decimal(up_price)
      up_price = str(up_price)[0:10]
      down_price = float(down_profit) * float(base_price)
      down_price = decimal.Decimal(down_price)
      down_price = str(down_price)[0:10]
      up_profit = ((float(Settings.up_profitPP) / 1)-1) * 100
      up_profit = round(up_profit,2)
      down_profit = ((float(Settings.down_profitPP) / 1)-1) * 100
      down_profit = round(down_profit,2)

      title = str("Market :" + str(symbol + "BTC ") + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
      table_price= [
            ['Prices', 'Value', 'Profits'],
            ['Up Price', str(up_price), str(up_profit)+'%'],
            ['Base Price', str(base_price), ''],
            ['Down Price', str(down_price), str(down_profit)+'%'],
            ['\nActual Price', '\n' + str(price), '\n' + str(aprofit)+'%']
        ]

      table_balance= [
            ['Balance', 'Value'],
            [str(symbol), str(balanceALTFREE)],
            ['BTC', str(balanceBTCFREE)],
						['Budget BTC', str(budget_BTC)],
					  ['Budget ALT', str(budget_ALT)],
					  ['Next operation', str(start_operation)]
        ]

      table_order= [
            ['OrderID', 'Status', 'Side'],
            [str(OrderStatus),str(OrderID),str(OrderSide)]
        ]

      o = AsciiTable(table_order)
      y = AsciiTable(table_price)
      y.justify_columns[2] = 'right'
      x = AsciiTable(table_balance)
      x.justify_columns[1] = 'right'
      print(title)
      print(y.table)
      print(x.table)
      print(o.table)
      print("\n********************************************************************\n")

      if str(OrderID) != "":
        check = client.get_order(symbol=str(symbol+"BTC"), orderId=OrderID, recvWindow=1000000)
        Jorder = json.loads(json.dumps(check))
        OrderStatus = Jorder['status']
        if OrderStatus == "FILLED" and OrderSide == "SELL":
          start_operation = "BUY"
          bot.send_message(chat_id=key.chat_id, text=str(comSell))  #<--send msg Telegram
          time.sleep(5)
          budget_BTC = float(balanceBTCFREE) * float(Settings.use_budget_BTCPP_procent)
          budget_BTC = round(budget_BTC,8)
          budget_BTC = decimal.Decimal(budget_BTC)
          budget_BTC = str(budget_BTC)[0:10]
          budget_ALT = float(balanceALTFREE)
          OrderID = ""
        elif  OrderStatus == "FILLED" and OrderSide == "BUY":
          start_operation = "SELL"
          bot.send_message(chat_id=key.chat_id, text=str(comBuy))  #<--send msg Telegram
          time.sleep(5)
          budget_BTC = float(balanceBTCFREE) * float(Settings.use_budget_BTCPP_procent)
          budget_BTC = round(budget_BTC,8)
          budget_BTC = decimal.Decimal(budget_BTC)
          budget_BTC = str(budget_BTC)[0:10]
          budget_ALT = float(balanceALTFREE)
          OrderID = ""

      if start_operation == "SELL" and float(up_price) < float(price) and float(budget_ALT) > 0 and str(OrderID) == "":
        qua = float(budget_ALT)
        start_operation == "BUY"
        if le==1:
          qua = math.floor(qua)
        else: 
          qua = str(qua)[0:le]
        OrderSell = client.create_order(symbol=str(symbol+"BTC"), side=client.SIDE_SELL, type=client.ORDER_TYPE_LIMIT, timeInForce=client.TIME_IN_FORCE_GTC, quantity=str(qua), price=str(price), recvWindow=1000000)
        comSell = "\t Create Sell Order. Balance: " + str(qua) + "\tPrice: " + str(price)
        print(str(comSell))
        Jorder = json.loads(json.dumps(OrderSell))
        OrderStatus = Jorder['status']
        OrderID = Jorder['orderId']
        OrderSide = Jorder['side']

      if start_operation == "BUY" and float(down_price) > float(price) and float(budget_BTC) > 0 and str(OrderID) == "":
        qua = float(budget_BTC) / float(price)
        start_operation == "SELL"
        if le==1:
          qua = math.floor(qua)
        else: 
          qua = str(qua)[0:le]
        OrderBuy = client.create_order(symbol=str(symbol+"BTC"), side=client.SIDE_BUY, type=client.ORDER_TYPE_LIMIT, timeInForce=client.TIME_IN_FORCE_GTC, quantity=str(qua), price=str(price), recvWindow=1000000)
        comBuy = "\t Create Buy Order. Balance: " + str(qua) + "\tPrice: " + str(price)
        print(str(comBuy))
        Jorder = json.loads(json.dumps(OrderBuy))
        OrderStatus = Jorder['status']
        OrderID = Jorder['orderId']
        OrderSide = Jorder['side'] 
    except:
      print("EOFError")
      print("balanceAltResp " + balanceALTRESP['code'] + " " + balanceALTRESP['msg'])
      print("balanceBTCResp " + balanceBTCRESP['code'] + " " + balanceBTCRESP['msg'])
      print("priceRESP  " + priceRESP['code'] + " " + priceRESP['msg'])
      print("Jorder  " + Jorder['code'] + " " + Jorder['msg'])
Ejemplo n.º 6
0
 def __str__(self):
     data = [['key', 'value']] + [[k, v] for k, v in vars(self).items()]
     return AsciiTable(data, title='Config').table
Ejemplo n.º 7
0
#How many predictions to have average on motion
average_window_size = int((history_duration - motion_duration) *
                          predictions_per_second + 1)

try:
    from terminaltables import AsciiTable
    table_data = [['Parameter', 'Value'],
                  ['Predictions/s (target)', predictions_per_second],
                  ['Frames recorded/s (estimate)', recording_fps],
                  ['Time to teplace all history (s)', history_duration],
                  [
                      'Motion lifespan in history (s)',
                      (history_duration - motion_duration)
                  ], ['Prediction needed for average', average_window_size]]
    print AsciiTable(table_data).table
except ImportError:
    print("======== Classification parameters ========")
    print(predictions_per_second, "predictions/s (classifier target)")
    print(recording_fps, "frames/s (recording target)")
    print(history_duration, "s to replace all history")
    print((history_duration - motion_duration),
          "s (motion lifespan in history)")
    print(average_window_size, "predictions needed to average")
    print("===========================================\n")


def threaded(fn):
    def wrapper(*args, **kwargs):
        thread = threading.Thread(target=fn, args=args, kwargs=kwargs)
        thread.start()
Ejemplo n.º 8
0
    def generate_country_breakdown(self):
        '''Generates a breakdown of the traffic'''

        # We'll sort on transmitted data
        table_data = [
            ['Country', 'Subdivision', '% Transmitted', '% Received', 'TX Packets', 'RX Packets']
        ]

        tx_entry_dict = {}

        for country, values in self.traffic_report.statistics_dicts.items():
            table_entry = []

            # First add the country
            country_line = [
                country,
                "",
                values['transmit_percentage'],
                values['receive_percentage'],
                values['txpackets'],
                values['rxpackets']
            ]
            table_entry.append(country_line)

            # Now the subdivisions of each country
            for subdivision, subvalues in values['subdivisions'].items():
                sub_line = [
                    "",
                    subdivision,
                    subvalues['transmit_percentage'],
                    subvalues['receive_percentage'],
                    subvalues['txpackets'],
                    subvalues['rxpackets']
                ]
                table_entry.append(sub_line)

            # Add it to the TX dict now

            # This can happen if we get two stat dicts with the same percentage
            if values['transmit_percentage'] in tx_entry_dict:
                tx_entry_dict[values['transmit_percentage']] += table_entry
            else:
                tx_entry_dict[values['transmit_percentage']] = table_entry

        for key in sorted(tx_entry_dict.keys(), reverse=True):
            table_data += tx_entry_dict[key]

        # Add a final line with the total
        total = [
            "Total",
            "",
            float(100),
            float(100),
            self.traffic_report.total_txpackets,
            self.traffic_report.total_rxpackets
        ]
        table_data.append([])
        table_data.append(total)

        # Now generate a pretty table and return it
        table = AsciiTable(table_data)
        return table.table
Ejemplo n.º 9
0
validation_fit=np.insert(validation_fit,0,1,1)

#Printing Predicted and Target Values side by side
print("Predicted sound pressures levels for validation data---->")
targets_validation=validation_data[:,-1]
predicted_validation=validation_fit*W_jk
final_all=np.asmatrix(np.ones((targets_validation.shape[0],2)))
final_all[:,0]=targets_validation
final_all[:,1]=predicted_validation
final_all=np.asarray(final_all).tolist()

data_values=[['Target values in dB (Validation)','Predicted values in dB (Validation)']]
for i in range(1,len(final_all)):
    data_values.insert(i,final_all[i-1])
# print(data_values)
table=AsciiTable(data_values)
print(table.table)




###Testing
Test=np.asmatrix([6300,15.6,0.1016,39.6,0.0528487])
# print(validation_data[:,:])
Test=(Test-means)/stdivs
Test=np.insert(Test,0,1,1)
print('Predicted sound pressure level--->{} dB'.format(np.asscalar(Test*W_jk)))


#Plotting cost history for gradient descent
plt.title("Gradient Descent")
Ejemplo n.º 10
0
    def evaluate(self,
                 results,
                 metric='bbox',
                 logger=None,
                 jsonfile_prefix=None,
                 classwise=False,
                 proposal_nums=(100, 300, 1000),
                 iou_thrs=np.arange(0.5, 0.96, 0.05)):
        """Evaluation in COCO protocol.

        Args:
            results (list[list | tuple]): Testing results of the dataset.
            metric (str | list[str]): Metrics to be evaluated. Options are
                'bbox', 'segm', 'proposal', 'proposal_fast'.
            logger (logging.Logger | str | None): Logger used for printing
                related information during evaluation. Default: None.
            jsonfile_prefix (str | None): The prefix of json files. It includes
                the file path and the prefix of filename, e.g., "a/b/prefix".
                If not specified, a temp file will be created. Default: None.
            classwise (bool): Whether to evaluating the AP for each class.
            proposal_nums (Sequence[int]): Proposal number used for evaluating
                recalls, such as recall@100, recall@1000.
                Default: (100, 300, 1000).
            iou_thrs (Sequence[float]): IoU threshold used for evaluating
                recalls. If set to a list, the average recall of all IoUs will
                also be computed. Default: 0.5.

        Returns:
            dict[str, float]: COCO style evaluation metric.
        """

        metrics = metric if isinstance(metric, list) else [metric]
        allowed_metrics = ['bbox', 'segm', 'proposal', 'proposal_fast']
        for metric in metrics:
            if metric not in allowed_metrics:
                raise KeyError(f'metric {metric} is not supported')

        result_files, tmp_dir = self.format_results(results, jsonfile_prefix)

        eval_results = {}
        cocoGt = self.coco
        for metric in metrics:
            msg = f'Evaluating {metric}...'
            if logger is None:
                msg = '\n' + msg
            print_log(msg, logger=logger)

            if metric == 'proposal_fast':
                ar = self.fast_eval_recall(results,
                                           proposal_nums,
                                           iou_thrs,
                                           logger='silent')
                log_msg = []
                for i, num in enumerate(proposal_nums):
                    eval_results[f'AR@{num}'] = ar[i]
                    log_msg.append(f'\nAR@{num}\t{ar[i]:.4f}')
                log_msg = ''.join(log_msg)
                print_log(log_msg, logger=logger)
                continue

            if metric not in result_files:
                raise KeyError(f'{metric} is not in results')
            try:
                cocoDt = cocoGt.loadRes(result_files[metric])
            except IndexError:
                print_log('The testing results of the whole dataset is empty.',
                          logger=logger,
                          level=logging.ERROR)
                break

            iou_type = 'bbox' if metric == 'proposal' else metric
            cocoEval = COCOeval(cocoGt, cocoDt, iou_type)
            cocoEval.params.catIds = self.cat_ids
            cocoEval.params.imgIds = self.img_ids
            if metric == 'proposal':
                cocoEval.params.useCats = 0
                cocoEval.params.maxDets = list(proposal_nums)
                cocoEval.evaluate()
                cocoEval.accumulate()
                cocoEval.summarize()
                metric_items = [
                    'AR@100', 'AR@300', 'AR@1000', 'AR_s@1000', 'AR_m@1000',
                    'AR_l@1000'
                ]
                for i, item in enumerate(metric_items):
                    val = float(f'{cocoEval.stats[i + 6]:.3f}')
                    eval_results[item] = val
            else:
                cocoEval.evaluate()
                cocoEval.accumulate()
                cocoEval.summarize()
                if classwise:  # Compute per-category AP
                    # Compute per-category AP
                    # from https://github.com/facebookresearch/detectron2/
                    precisions = cocoEval.eval['precision']
                    # precision: (iou, recall, cls, area range, max dets)
                    assert len(self.cat_ids) == precisions.shape[2]

                    results_per_category = []
                    for idx, catId in enumerate(self.cat_ids):
                        # area range index 0: all area ranges
                        # max dets index -1: typically 100 per image
                        nm = self.coco.loadCats(catId)[0]
                        precision = precisions[:, :, idx, 0, -1]
                        precision = precision[precision > -1]
                        if precision.size:
                            ap = np.mean(precision)
                        else:
                            ap = float('nan')
                        results_per_category.append(
                            (f'{nm["name"]}', f'{float(ap):0.3f}'))

                    num_columns = min(6, len(results_per_category) * 2)
                    results_flatten = list(
                        itertools.chain(*results_per_category))
                    headers = ['category', 'AP'] * (num_columns // 2)
                    results_2d = itertools.zip_longest(*[
                        results_flatten[i::num_columns]
                        for i in range(num_columns)
                    ])
                    table_data = [headers]
                    table_data += [result for result in results_2d]
                    table = AsciiTable(table_data)
                    print_log('\n' + table.table, logger=logger)

                metric_items = [
                    'mAP', 'mAP_50', 'mAP_75', 'mAP_s', 'mAP_m', 'mAP_l'
                ]
                for i in range(len(metric_items)):
                    key = f'{metric}_{metric_items[i]}'
                    val = float(f'{cocoEval.stats[i]:.3f}')
                    eval_results[key] = val
                ap = cocoEval.stats[:6]
                eval_results[f'{metric}_mAP_copypaste'] = (
                    f'{ap[0]:.3f} {ap[1]:.3f} {ap[2]:.3f} {ap[3]:.3f} '
                    f'{ap[4]:.3f} {ap[5]:.3f}')
        if tmp_dir is not None:
            tmp_dir.cleanup()
        return eval_results
Ejemplo n.º 11
0
def make_table_rows(title, table_data):
    table = AsciiTable(table_data, title)
    dimensions = max_dimensions(table.table_data, table.padding_left, table.padding_right)[:3]
    output = table.gen_table(*dimensions)
    return map(lambda i: ''.join(i), list(output))
Ejemplo n.º 12
0
    print('Код ответа сервера: ' + str(req_.status_code))
    net_ = parser_sysifaces_interfaces.parsing(req_.text)
else:
    net_ = 'error'

print('Результат парсинга страницы ' + protocol_ + '://' + host_ + ':' +
      port_ + urn_ + ' :')
table_data_ = [[
    'Имя', 'Цвет', 'Номер', 'IP-адресс', 'Маска сети', 'MAC-адрес', 'MTU'
]]
for i_ in net_:
    table_data_.append([
        i_['name'], i_['color'], i_['number'], i_['address'], i_['netmask'],
        i_['mac'], i_['mtu']
    ])
print(AsciiTable(table_data_).table)
print('')

# Парсер для netstatus
description_ = str(parameters_['get']['netstatus']['description'])
urn_ = str(parameters_['get']['netstatus']['urn'])
print('Описание: ' + description_)

req_ = librequests.get(protocol_, host_, port_, urn_, login_, password_,
                       timeout_)

if type(req_) == requests.models.Response:
    print('Код ответа сервера: ' + str(req_.status_code))
    net_ = parser_netstatus_interfaces.parsing(req_.text)
else:
    net_ = 'error'
Ejemplo n.º 13
0
 def printDynamicMatrix(self, matrix):
     table = [['i\j'] + list(map(Knapsack.color, list(range(self.b + 1))))]
     for i, row in enumerate(matrix):
         table.append([Knapsack.color(i)] + row)
         # table.append([Knapsack.color(i)] + row)
     return '\nDynamic programming array\n' + AsciiTable(table).table
Ejemplo n.º 14
0
def Trading_with_SD(symbol):
    OrderStatus = ''
    OrderID = ''
    OrderSide = ''
    k = 0
    a = getA(symbol=symbol)
    #budgetBTCSD = Settings.budgetBTCSD
    startoperation = Settings.start_operationSD
    print("Start operation: " + startoperation)
    #print("Your budget: " + str(budgetBTCSD))
    print("Let's start ...")
    while k < 1:
        try:
            balanceALT = client.get_asset_balance(asset=str(symbol),
                                                  recvWindow=1000000)
            balanceALTJSON = json.dumps(balanceALT)
            balanceALTRESP = json.loads(balanceALTJSON)
            balanceALTFREE = balanceALTRESP['free']
            budget_ALT = balanceALTFREE
            balanceBTC = client.get_asset_balance(asset='BTC',
                                                  recvWindow=1000000)
            balanceBTCJSON = json.dumps(balanceBTC)
            balanceBTCRESP = json.loads(balanceBTCJSON)
            balanceBTCFREE = balanceBTCRESP['free']
            budgetBTCSD = float(balanceBTCFREE) * float(
                Settings.use_budget_BTCSD_procent)
            budgetBTCSD = round(budgetBTCSD, 8)
            budgetBTCSD = decimal.Decimal(budgetBTCSD)
            budgetBTCSD = str(budgetBTCSD)[0:10]
            time.sleep(2)
            le = len(str(Symbols.SymbolsMatrix[a][2]))
            lep = len(str(Symbols.SymbolsMatrix[a][3]))
            depth = client.get_order_book(symbol=symbol + 'BTC', limit=5)
            depthJSON = json.loads(json.dumps(depth))
            depthAskPrice = depthJSON['asks'][0][0]
            depthAskValue = depthJSON['asks'][0][1]
            depthBidPrice = depthJSON['bids'][0][0]
            depthBidValue = depthJSON['bids'][0][1]
            NewdepthAskPrice = float(depthAskPrice) - float(
                Symbols.SymbolsMatrix[a][3])
            NewdepthBidPrice = float(depthBidPrice) + float(
                Symbols.SymbolsMatrix[a][3])
            NewdepthAskPrice = decimal.Decimal(NewdepthAskPrice)
            NewdepthAskPrice = str(NewdepthAskPrice)[0:lep]
            NewdepthBidPrice = decimal.Decimal(NewdepthBidPrice)
            NewdepthBidPrice = str(NewdepthBidPrice)[0:lep]
            depth = (float(depthAskPrice) / float(depthBidPrice)) - 1
            depth = round((float(depth) * 100), 2)
            Newdepth = (float(NewdepthAskPrice) / float(NewdepthBidPrice)) - 1
            Newdepth = round((float(Newdepth) * 100), 2)
            depthAskPrice = str(depthAskPrice)[0:lep]
            depthBidPrice = str(depthBidPrice)[0:lep]
            qua = float(budgetBTCSD) / float(NewdepthAskPrice)
            if le == 1:
                qua = math.floor(qua)
            else:
                qua = str(qua)[0:le]
            table_depth = [[
                'Symbol', 'Ask Price (SELL)', 'Ask Volumen', 'Bid Price (BUY)',
                'Bid Volumen', 'Depth'
            ],
                           [
                               str(symbol),
                               str(depthAskPrice),
                               str(depthAskValue),
                               str(depthBidPrice),
                               str(depthBidValue),
                               str(depth) + '%'
                           ],
                           [
                               str(symbol),
                               str(NewdepthAskPrice),
                               str(qua),
                               str(NewdepthBidPrice),
                               str(qua),
                               str(Newdepth) + '%'
                           ],
                           [
                               "Status Order",
                               str(OrderStatus), "Id Order",
                               str(OrderID), "Side Order",
                               str(OrderSide)
                           ],
                           [
                               "Next operation",
                               str(startoperation), "Budget BTC",
                               str(budgetBTCSD), "Budget ALT",
                               str(budget_ALT)
                           ]]
            d = AsciiTable(table_depth)

            print(d.table)
            if float(Newdepth) > float(Settings.minDepth):
                #startoperation = Settings.start_operationSD
                if startoperation == "SELL" and float(budget_ALT) > 0:
                    qua = float(budget_ALT)
                    if le == 1:
                        qua = math.floor(qua)
                    else:
                        qua = str(qua)[0:le]
                    print("Try create Sell order. Price: " +
                          str(NewdepthAskPrice) + "Quantity: " + str(qua) +
                          "Change budget BTC in this strategy: " +
                          str(Settings.budgetBTCSD) + " ==>> " +
                          str(budgetBTCSD))
                    OrderSell = client.create_order(
                        symbol=str(symbol + "BTC"),
                        side=client.SIDE_SELL,
                        type=client.ORDER_TYPE_LIMIT,
                        timeInForce=client.TIME_IN_FORCE_GTC,
                        quantity=str(qua),
                        price=str(NewdepthAskPrice))
                    Jorder = json.loads(json.dumps(OrderSell))
                    OrderStatus = Jorder['status']
                    OrderID = Jorder['orderId']
                    OrderSide = Jorder['side']
                    OrderPrice = Jorder['price']
                    startoperation = ""  #BUY
                    print("Create SELL order: " + "\n\tPrice: " +
                          str(NewdepthAskPrice) + "\n\tQuantity: " + str(qua) +
                          "\n\tStatus: " + str(OrderStatus) + "\n\tOrderID: " +
                          str(OrderID) + "\n\tSide: " + str(OrderSide))
                if startoperation == "BUY" and float(budgetBTCSD) > 0:
                    qua = float(budgetBTCSD) / float(NewdepthBidPrice)
                    if le == 1:
                        qua = math.floor(qua)
                    else:
                        qua = str(qua)[0:le]
                    print("Try create Buy order. Price: " +
                          str(NewdepthBidPrice) + " Quantity: " + str(qua) +
                          " Change budget BTC in this strategy: " +
                          str(Settings.budgetBTCSD) + " ==>> " +
                          str(budgetBTCSD))
                    OrderBuy = client.create_order(
                        symbol=str(symbol + "BTC"),
                        side=client.SIDE_BUY,
                        type=client.ORDER_TYPE_LIMIT,
                        timeInForce=client.TIME_IN_FORCE_GTC,
                        quantity=str(qua),
                        price=str(NewdepthBidPrice))
                    Jorder = json.loads(json.dumps(OrderBuy))
                    OrderStatus = Jorder['status']
                    OrderID = Jorder['orderId']
                    OrderSide = Jorder['side']
                    OrderPrice = Jorder['price']
                    startoperation = ""  #SELL
                    print("Create BUY order: " + "\n\tPrice: " +
                          str(NewdepthBidPrice) + "\n\tQuantity: " + str(qua) +
                          "\n\tStatus: " + str(OrderStatus) + "\n\tOrderID: " +
                          str(OrderID) + "\n\tSide: " + str(OrderSide))
                while True:
                    balanceALT = client.get_asset_balance(asset=str(symbol),
                                                          recvWindow=1000000)
                    balanceALTJSON = json.dumps(balanceALT)
                    balanceALTRESP = json.loads(balanceALTJSON)
                    balanceALTFREE = balanceALTRESP['free']
                    budget_ALT = balanceALTFREE
                    balanceBTC = client.get_asset_balance(asset='BTC',
                                                          recvWindow=1000000)
                    balanceBTCJSON = json.dumps(balanceBTC)
                    balanceBTCRESP = json.loads(balanceBTCJSON)
                    balanceBTCFREE = balanceBTCRESP['free']
                    budgetBTCSD = float(balanceBTCFREE) * float(
                        Settings.use_budget_BTCSD_procent)
                    budgetBTCSD = round(budgetBTCSD, 8)
                    budgetBTCSD = decimal.Decimal(budgetBTCSD)
                    budgetBTCSD = str(budgetBTCSD)[0:10]
                    price = client.get_symbol_ticker(symbol=str(symbol) +
                                                     "BTC")
                    priceJSON = json.dumps(price)
                    priceRESP = json.loads(priceJSON)
                    price = priceRESP['price']
                    depth = client.get_order_book(symbol=symbol + 'BTC',
                                                  limit=5)
                    depthJSON = json.loads(json.dumps(depth))
                    depthAskPrice = depthJSON['asks'][0][0]
                    depthBidPrice = depthJSON['bids'][0][0]
                    depth = client.get_order_book(symbol=symbol + 'BTC',
                                                  limit=5)
                    depthJSON = json.loads(json.dumps(depth))
                    depthAskPrice = depthJSON['asks'][0][0]
                    depthBidPrice = depthJSON['bids'][0][0]
                    NewdepthAskPrice = float(depthAskPrice) - float(
                        Symbols.SymbolsMatrix[a][3])
                    NewdepthBidPrice = float(depthBidPrice) + float(
                        Symbols.SymbolsMatrix[a][3])
                    NewdepthAskPrice = decimal.Decimal(NewdepthAskPrice)
                    NewdepthAskPrice = str(NewdepthAskPrice)[0:lep]
                    NewdepthBidPrice = decimal.Decimal(NewdepthBidPrice)
                    NewdepthBidPrice = str(NewdepthBidPrice)[0:lep]
                    if str(OrderID) != "":
                        check = client.get_order(symbol=str(symbol + "BTC"),
                                                 orderId=OrderID,
                                                 recvWindow=1000000)
                        Jorder = json.loads(json.dumps(check))
                        OrderStatus = Jorder['status']
                        print("Check status Order: " + str(OrderStatus))
                        if OrderStatus == "FILLED" and OrderSide == "SELL":
                            startoperation = "BUY"
                            time.sleep(1)
                            balanceBTC = client.get_asset_balance(
                                asset='BTC', recvWindow=1000000)
                            balanceBTCJSON = json.dumps(balanceBTC)
                            balanceBTCRESP = json.loads(balanceBTCJSON)
                            balanceBTCFREE = balanceBTCRESP['free']
                            budget_BTC = float(balanceBTCFREE) * float(
                                Settings.use_budget_BTCBB_procent)
                            budget_BTC = round(budget_BTC, 8)
                            budget_BTC = decimal.Decimal(budget_BTC)
                            budget_BTC = str(budget_BTC)[0:10]
                            balanceALT = client.get_asset_balance(
                                asset=str(symbol), recvWindow=1000000)
                            balanceALTJSON = json.dumps(balanceALT)
                            balanceALTRESP = json.loads(balanceALTJSON)
                            balanceALTFREE = balanceALTRESP['free']
                            budget_ALT = float(balanceALTFREE)
                            OrderID = ""
                            print("Bot sold your coins. Market: " +
                                  str(symbol + "BTC") + " Quantity: " +
                                  str(qua) + " Budget BTC: " +
                                  str(budgetBTCSD) + " Budget ALT: " +
                                  str(budget_ALT) +
                                  " Change StartOperation on: " +
                                  str(startoperation))
                            break
                        elif OrderStatus == "FILLED" and OrderSide == "BUY":
                            startoperation = "SELL"
                            time.sleep(1)
                            balanceBTC = client.get_asset_balance(
                                asset='BTC', recvWindow=1000000)
                            balanceBTCJSON = json.dumps(balanceBTC)
                            balanceBTCRESP = json.loads(balanceBTCJSON)
                            balanceBTCFREE = balanceBTCRESP['free']
                            budget_BTC = float(balanceBTCFREE) * float(
                                Settings.use_budget_BTCBB_procent)
                            budget_BTC = round(budget_BTC, 8)
                            budget_BTC = decimal.Decimal(budget_BTC)
                            budget_BTC = str(budget_BTC)[0:10]
                            balanceALT = client.get_asset_balance(
                                asset=str(symbol), recvWindow=1000000)
                            balanceALTJSON = json.dumps(balanceALT)
                            balanceALTRESP = json.loads(balanceALTJSON)
                            balanceALTFREE = balanceALTRESP['free']
                            budget_ALT = float(balanceALTFREE)
                            OrderID = ""
                            print("Bot bought your coins. Market: " +
                                  str(symbol + "BTC") + " Quantity: " +
                                  str(qua) + " Set new budget BTC: " +
                                  str(budgetBTCSD) + " Budget ALT: " +
                                  str(budget_ALT) +
                                  " Change StartOperation on: " +
                                  str(startoperation))
                            break
                        elif OrderSide == "SELL" and float(
                                depthAskPrice) < float(
                                    OrderPrice):  #NewdepthAskPrice
                            result = client.cancel_order(symbol=str(symbol +
                                                                    "BTC"),
                                                         orderId=str(OrderID))
                            startoperation = "SELL"
                            time.sleep(1)
                            balanceBTC = client.get_asset_balance(
                                asset='BTC', recvWindow=1000000)
                            balanceBTCJSON = json.dumps(balanceBTC)
                            balanceBTCRESP = json.loads(balanceBTCJSON)
                            balanceBTCFREE = balanceBTCRESP['free']
                            budget_BTC = float(balanceBTCFREE) * float(
                                Settings.use_budget_BTCBB_procent)
                            budget_BTC = round(budget_BTC, 8)
                            budget_BTC = decimal.Decimal(budget_BTC)
                            budget_BTC = str(budget_BTC)[0:10]
                            balanceALT = client.get_asset_balance(
                                asset=str(symbol), recvWindow=1000000)
                            balanceALTJSON = json.dumps(balanceALT)
                            balanceALTRESP = json.loads(balanceALTJSON)
                            balanceALTFREE = balanceALTRESP['free']
                            budget_ALT = float(balanceALTFREE)
                            OrderID = ""
                            OrderStatus = ""
                            OrderSide = ""
                            print(
                                "Bot CANCEL last order, because price goes down..."
                            )
                            print("\t\tBudget BTC: " + str(budgetBTCSD))
                            print("\t\tBudget ALT: " + str(budget_ALT))
                            break
                        elif OrderSide == "BUY" and float(
                                depthBidPrice) > float(
                                    OrderPrice):  #NewdepthBidPrice
                            result = client.cancel_order(symbol=str(symbol +
                                                                    "BTC"),
                                                         orderId=str(OrderID))
                            startoperation = "BUY"
                            time.sleep(1)
                            balanceBTC = client.get_asset_balance(
                                asset='BTC', recvWindow=1000000)
                            balanceBTCJSON = json.dumps(balanceBTC)
                            balanceBTCRESP = json.loads(balanceBTCJSON)
                            balanceBTCFREE = balanceBTCRESP['free']
                            budget_BTC = float(balanceBTCFREE) * float(
                                Settings.use_budget_BTCBB_procent)
                            budget_BTC = round(budget_BTC, 8)
                            budget_BTC = decimal.Decimal(budget_BTC)
                            budget_BTC = str(budget_BTC)[0:10]
                            balanceALT = client.get_asset_balance(
                                asset=str(symbol), recvWindow=1000000)
                            balanceALTJSON = json.dumps(balanceALT)
                            balanceALTRESP = json.loads(balanceALTJSON)
                            balanceALTFREE = balanceALTRESP['free']
                            budget_ALT = float(balanceALTFREE)
                            OrderID = ""
                            OrderStatus = ""
                            OrderSide = ""
                            print(
                                "Bot CANCEL last order, because price goes up..."
                            )
                            print("\t\tBudget BTC: " + str(budgetBTCSD))
                            print("\t\tBudget ALT: " + str(budget_ALT))
                            break
                        else:
                            print(
                                '##################################################################'
                            )
                            print(
                                "Check status order and control prices. Please wait..."
                            )
                            print("Status order: " + str(OrderStatus) +
                                  " Side: " + str(OrderSide) + " Order ID" +
                                  str(OrderID))
                            print("\t\tNewdepthBidPrice: " +
                                  str(NewdepthBidPrice))
                            print("\t\tNewdepthAskPrice: " +
                                  str(NewdepthAskPrice))
                            print("\t\tBudget BTC: " + str(budgetBTCSD))
                            print("\t\tBudget ALT: " + str(budget_ALT))
                            print("\t\tPrice: " + str(price))
                            print(
                                '##################################################################'
                            )
        except:
            print("ERROR")
            print(check)
            print(OrderStatus + OrderID + OrderSide)
            print(result)
Ejemplo n.º 15
0
    maxW = db['maxW']
    maxP = db['maxP']
    maxQ = db['maxQ']
    maxR = db['maxR']
    max_T1 = db['max_T1']
    max_T2 = db['max_T2']
    max_T3 = db['max_T3']
    max_T4 = db['max_T4']
    dronename = db['drone_name']

    for key, value in db.items():
        data.append([str(key), str(value)])

db.close()

table = AsciiTable(data)
table.inner_row_border = True
print(table.table)

data = []
print('----------------------------------------------------------------')
print('Fetching dataset info from: {}'.format(dataset_readme))
print('----------------------------------------------------------------')
with shelve.open(dataset_readme) as db:
    for key, value in db.items():
        data.append([str(key), str(value)])
db.close()
table = AsciiTable(data)
table.inner_row_border = True
print(table.table)
Ejemplo n.º 16
0
    def evaluate(self,
                 results,
                 metric='bbox',
                 logger=None,
                 jsonfile_prefix=None,
                 classwise=False,
                 proposal_nums=(100, 300, 1000),
                 iou_thrs=np.arange(0.5, 0.96, 0.05)):
        """Evaluation in LVIS protocol.
        Args:
            results (list): Testing results of the dataset.
            metric (str | list[str]): Metrics to be evaluated.
            logger (logging.Logger | str | None): Logger used for printing
                related information during evaluation. Default: None.
            jsonfile_prefix (str | None):
            classwise (bool): Whether to evaluating the AP for each class.
            proposal_nums (Sequence[int]): Proposal number used for evaluating
                recalls, such as recall@100, recall@1000.
                Default: (100, 300, 1000).
            iou_thrs (Sequence[float]): IoU threshold used for evaluating
                recalls. If set to a list, the average recall of all IoUs will
                also be computed. Default: 0.5.
        Returns:
            dict[str: float]
        """
        try:
            from lvis import LVISResults, LVISEval
        except ImportError:
            raise ImportError('Please follow config/lvis/README.md to '
                              'install open-mmlab forked lvis first.')
        assert isinstance(results, list), 'results must be a list'
        assert len(results) == len(self), (
            'The length of results is not equal to the dataset len: {} != {}'.
            format(len(results), len(self)))

        metrics = metric if isinstance(metric, list) else [metric]
        allowed_metrics = ['bbox', 'segm', 'proposal', 'proposal_fast']
        for metric in metrics:
            if metric not in allowed_metrics:
                raise KeyError('metric {} is not supported'.format(metric))

        if jsonfile_prefix is None:
            tmp_dir = tempfile.TemporaryDirectory()
            jsonfile_prefix = osp.join(tmp_dir.name, 'results')
        else:
            tmp_dir = None
        result_files = self.results2json(results, jsonfile_prefix)

        eval_results = {}
        # get original api
        lvis_gt = self.coco
        for metric in metrics:
            msg = 'Evaluating {}...'.format(metric)
            if logger is None:
                msg = '\n' + msg
            print_log(msg, logger=logger)

            if metric == 'proposal_fast':
                ar = self.fast_eval_recall(
                    results, proposal_nums, iou_thrs, logger='silent')
                log_msg = []
                for i, num in enumerate(proposal_nums):
                    eval_results['AR@{}'.format(num)] = ar[i]
                    log_msg.append('\nAR@{}\t{:.4f}'.format(num, ar[i]))
                log_msg = ''.join(log_msg)
                print_log(log_msg, logger=logger)
                continue

            if metric not in result_files:
                raise KeyError('{} is not in results'.format(metric))
            try:
                lvis_dt = LVISResults(lvis_gt, result_files[metric])
            except IndexError:
                print_log(
                    'The testing results of the whole dataset is empty.',
                    logger=logger,
                    level=logging.ERROR)
                break

            iou_type = 'bbox' if metric == 'proposal' else metric
            lvis_eval = LVISEval(lvis_gt, lvis_dt, iou_type)
            lvis_eval.params.imgIds = self.img_ids
            if metric == 'proposal':
                lvis_eval.params.useCats = 0
                lvis_eval.params.maxDets = list(proposal_nums)
                lvis_eval.evaluate()
                lvis_eval.accumulate()
                lvis_eval.summarize()
                for k, v in lvis_eval.get_results().items():
                    if k.startswith('AR'):
                        val = float('{:.3f}'.format(float(v)))
                        eval_results[k] = val
            else:
                lvis_eval.evaluate()
                lvis_eval.accumulate()
                lvis_eval.summarize()
                lvis_results = lvis_eval.get_results()
                if classwise:  # Compute per-category AP
                    # Compute per-category AP
                    # from https://github.com/facebookresearch/detectron2/
                    precisions = lvis_eval.eval['precision']
                    # precision: (iou, recall, cls, area range, max dets)
                    assert len(self.cat_ids) == precisions.shape[2]

                    results_per_category = []
                    for idx, catId in enumerate(self.cat_ids):
                        # area range index 0: all area ranges
                        # max dets index -1: typically 100 per image
                        nm = self.coco.load_cats(catId)[0]
                        precision = precisions[:, :, idx, 0, -1]
                        precision = precision[precision > -1]
                        if precision.size:
                            ap = np.mean(precision)
                        else:
                            ap = float('nan')
                        results_per_category.append(
                            (f'{nm["name"]}', f'{float(ap):0.3f}'))

                    num_columns = min(6, len(results_per_category) * 2)
                    results_flatten = list(
                        itertools.chain(*results_per_category))
                    headers = ['category', 'AP'] * (num_columns // 2)
                    results_2d = itertools.zip_longest(*[
                        results_flatten[i::num_columns]
                        for i in range(num_columns)
                    ])
                    table_data = [headers]
                    table_data += [result for result in results_2d]
                    table = AsciiTable(table_data)
                    print_log('\n' + table.table, logger=logger)

                for k, v in lvis_results.items():
                    if k.startswith('AP'):
                        key = '{}_{}'.format(metric, k)
                        val = float('{:.3f}'.format(float(v)))
                        eval_results[key] = val
                ap_summary = ' '.join([
                    '{}:{:.3f}'.format(k, float(v))
                    for k, v in lvis_results.items() if k.startswith('AP')
                ])
                eval_results['{}_mAP_copypaste'.format(metric)] = ap_summary
            lvis_eval.print_results()
        if tmp_dir is not None:
            tmp_dir.cleanup()
        return eval_results
Ejemplo n.º 17
0
 def show_summary(summary_table):
     sum_table = AsciiTable(summary_table, "Summary")
     print sum_table.table
Ejemplo n.º 18
0
            precision, recall, AP, f1, ap_class = evaluate(
                model,
                path=valid_path,
                iou_thres=0.5,
                conf_thres=0.5,
                nms_thres=0.5,
                img_size=opt.img_size,
                batch_size=1,
            )
            # evaluation_metrics = [ ("val_precision", precision.mean()), ("val_recall", recall.mean()), ("val_mAP", AP.mean()), ("val_f1", f1.mean()), ]
            # logger.list_of_scalars_summary(evaluation_metrics, epoch)
            # Print class APs and mAP
            ap_table = [["Index", "Class name", "AP"]]
            for i, c in enumerate(ap_class):
                ap_table += [[c, class_names[c], "%.5f" % AP[i]]]
            print(AsciiTable(ap_table).table)
            print(f"---- mAP {AP.mean()}")

            accuracy_all.append(AP.mean())

        if epoch % opt.checkpoint_interval == 0:
            torch.save(model.state_dict(),
                       f"checkpoints/yolov3_ckpt_%d.pth" % epoch)

    fig = plt.figure()
    ax = fig.add_subplot(211)
    ax.plot(loss_all)
    ax2 = fig.add_subplot(212)
    ax2.plot(accuracy_all)
    plt.show(0)
Ejemplo n.º 19
0
def main():
    best_error = -1

    for epoch in range(args.epochs):

        start_time = time.time()

        losses, loss_name = train(model, optimizer)

        precision, recall, AP, f1, ap_class = valid(model,
                                                    iou_thres=args.iou_thres,
                                                    conf_thres=args.conf_thres,
                                                    nms_thres=args.nms_thres,
                                                    img_size=args.img_size)

        # store weights file
        decisive_error = AP.mean()
        if best_error < 0:
            best_error = decisive_error
        is_best = best_error < decisive_error
        best_error = max(best_error, decisive_error)
        torch.save(model.state_dict(),
                   args.save_path / 'yolov3_pineapple_{}.pth'.format(epoch))
        # #
        print(is_best)
        if is_best:
            torch.save(model.state_dict(),
                       args.save_path / 'yolov3_pineapple.pth')

        # write results to csv file
        with open(args.save_path / args.log, 'a') as csvfile:
            csv_writer = csv.writer(csvfile, delimiter='\t')
            csv_writer.writerow(
                [precision.mean(),
                 recall.mean(),
                 AP.mean(),
                 f1.mean()])

        # write results to tensorboard
        writer.add_scalar('Train/loss', losses[0], epoch)
        writer.flush()

        writer.add_scalar("Valid/precision", precision.mean(), epoch)
        writer.add_scalar("Valid/recall", recall.mean(), epoch)
        writer.add_scalar("Valid/mAP", AP.mean(), epoch)
        writer.add_scalar("Valid/f1", f1.mean(), epoch)
        writer.flush()

        # print to Terminal
        print("\n---- [Epoch {}/{}] ----".format(epoch, args.epochs))
        print("Total loss: {}".format(losses[0]))

        ap_table = [["Index", "Class name", "AP"]]
        for i, c in enumerate(ap_class):
            ap_table += [[c, class_names[c], "%.5f" % AP[i]]]
        print(AsciiTable(ap_table).table)
        print(f"---- mAP {AP.mean()}")

        epoch_left = args.epochs - (epoch + 1)
        time_left = datetime.timedelta(seconds=epoch_left *
                                       (time.time() - start_time))
        print("---- ETA {}".format(time_left))
Ejemplo n.º 20
0
                row_metrics = [
                    formats[metric] % yolo.metrics[metric]
                    for yolo in model.yolo_layers
                ]
                metric_table += [[metric, *row_metrics]]

                # Tensorboard logging
                tensorboard_log = []
                for j, yolo in enumerate(model.yolo_layers):
                    for name, metric in yolo.metrics.items():
                        if name != "grid_size":
                            tensorboard_log += [("{}_{}".format(name, j + 1),
                                                 metric)]
                logger.list_of_scalars_summary(tensorboard_log, batches_done)

            log_str += AsciiTable(metric_table).table

            global_metrics = [("Total Loss", loss.item())]

            # Compute mAP every tenth iteration
            if opt.compute_map and batches_done % 10 == 0:

                # ---------------
                #   Compute mAP
                # ---------------

                # Get NMS output
                predictions = non_max_suppression(outputs, 0.5, 0.5)
                # Convert target coordinates to x1y1x2y2
                targets[:, :, 1:] = xywh2xyxy(targets[:, :, 1:])
                # Rescale to image dimension
Ejemplo n.º 21
0
    def get_performance_measures(self):
        # Confusion Matrix
        conf_matrix_all_row_data = []
        conf_matrix_header1 = ['']
        conf_matrix_header1.append("Predictions")
        col_titles = ['']

        # # Organize Confusion Matrix
        conf_matrix_all_row_data.append(conf_matrix_header1)

        class_labels_list = self.tp_counter_dict.keys()

        for lbl in class_labels_list:
            col_titles.append(lbl)
            self.logger.debug("Current lbl COLUMN NAME: %s" % lbl)
        self.logger.debug("Length of Column Titles list: %i" % len(col_titles))

        conf_matrix_all_row_data.append(col_titles)

        for lbl in class_labels_list:
            #col_titles.append(lbl)
            single_row_data = []
            self.logger.debug("Current Class LABEL: %s" % lbl)
            single_row_data.append(lbl)
            # Check index of current Column-Title
            #for col_idx, col_lbl in enumerate(col_titles):
            for col_idx, col_lbl in enumerate(class_labels_list):
                error_lbls = str(single_row_data[0]) + "-as-" + col_lbl
                self.logger.debug("Current Error Label: %s" % error_lbls)
                if single_row_data[0] == col_lbl:
                    single_row_data.append(self.tp_counter_dict[col_lbl])
                # elif error_counts_dict.keys() contains single_row_data[0]  and col_lbl ==
                elif error_lbls in self.error_counts_dict.keys():
                    single_row_data.append(self.error_counts_dict[error_lbls])
                else:
                    single_row_data.append(0)

            conf_matrix_all_row_data.append(single_row_data)

            #single_row_data.insert()

        # Output the confusion matrix
        conf_matrix_table = AsciiTable(conf_matrix_all_row_data)
        # print(conf_matrix_table.table)
        self.logger.info("Confusion Matrix: \n%s" % conf_matrix_table.table)

        all_labels_total = sum(Counter(self.all_actual_labels).values())
        self.logger.info("All labels sum: %i" % all_labels_total)

        # Accuracy:
        all_true_pos = sum(self.tp_counter_dict.values())
        self.logger.info("All true positive sum: %i" % all_true_pos)
        accuracy_val = all_true_pos/all_labels_total
        self.logger.info("--> ACCURACY: %.5f = %.2f%%" % (accuracy_val, accuracy_val*100))

        # Misclassification Rate:
        all_fpos_and_all_fneg = sum(self.error_counts_dict.values())
        self.logger.info("All False Pos + All False Neg: %i" % all_fpos_and_all_fneg)
        misclassification_rate = all_fpos_and_all_fneg/all_labels_total
        self.logger.info("--> MISCLASSIFICATION RATE: %.5f" % misclassification_rate)
        self.logger.debug("--> Also equal to (1- Accuracy): %.5f" % (1-accuracy_val))

        # True-Positives Rate (Recall, Sensitivity) per Class
        #self.logger.debug("Len list_of_pred_labels: %i" % len(list_of_pred_labels))
        #self.logger.debug("Len unique_labels: %i" % len(unique_labels))
        for label_key, label_pred_value in self.tp_counter_dict.items():
            #self.logger.debug(item)
            #self.logger.info("%s : %s " % (item, tp_counter_dict[item]))
            self.logger.info("%s : %s " % (label_key, label_pred_value))
            actual_specific_lbl_count = Counter(self.all_actual_labels)[label_key]
            self.logger.info("--> True Pos+ Rate/ RECALL/ Sensitivity [%s]: %.5f = %.2f%% " %
                              (label_key, (label_pred_value / actual_specific_lbl_count),
                                           (label_pred_value / actual_specific_lbl_count*100)))
            # For False Negatives
            fpos_count = 0
            spec_label_pred_counts = 0 # For calculating the Precision later
            for error_lbl in self.error_counts_dict.keys():
                # self.logger.debug("Current Label: %s" % label_key)
                # self.logger.debug("Current 1st part of Error-Label: %s" % error_lbl.split("-as-")[0])
                if label_key == error_lbl.split("-as-")[0]:
                    fpos_count += int(self.error_counts_dict[error_lbl])
                    #self.logger.debug("Current False Pos sum: %i" % fpos_count)
                # For Counting the Precision Later
                if label_key == error_lbl.split("-as-")[1]:
                    spec_label_pred_counts += int(self.error_counts_dict[error_lbl])
                    self.logger.debug("Current False Pos sum: %i" % fpos_count)
            self.logger.debug("False Positive Sum: %s : %i" % (label_key, fpos_count))
            all_actual_negatives = all_labels_total - actual_specific_lbl_count
            false_neg_rate = fpos_count / all_actual_negatives
            self.logger.info("--> FALSE NEGATIVE RATE [%s]: %.5f = %.2f%%" % (label_key, false_neg_rate, false_neg_rate * 100))

        # Specificity (True Negative Rate)
            correctly_predicted_no = all_true_pos - self.tp_counter_dict[label_key]      # True Negatives
            self.logger.debug("Number predicted no per class [%s]: %i" % (label_key, correctly_predicted_no))
            true_neg_rate = correctly_predicted_no/all_actual_negatives
            self.logger.info("--> TRUE NEGATIVE RATE/ Specificity [%s]: %.5f = %.2f%%" % (label_key, true_neg_rate, true_neg_rate * 100))

        # Precision (Positive predictive value)
            #Get from error counts how many times another label was predicted as label in consideration
            total_predictions_per_label = label_pred_value + spec_label_pred_counts
            self.logger.debug("Total Predictions of a Certain Label [%s]: %i" % (label_key, total_predictions_per_label))
            precision = label_pred_value/ total_predictions_per_label
            self.logger.info("--> PRECISION [%s] : %.5f = %.2f%%" % (label_key, precision, precision * 100))
Ejemplo n.º 22
0
import sys

from json_read import json_read, json_check
from tsv_read import tsv_read, tsv_check
from encoding_define import encoding_define
from terminaltables import AsciiTable


if __name__ == '__main__':
    filename = sys.argv[1]
    try:
        f = open(filename, "r")
        f.close()
    except:
        print('Файл не валиден')
        sys.exit()
    enc = encoding_define(filename)
    if enc not in ['utf-8', 'utf-16', 'windows-1251']:
        print('Формат не валиден')
        sys.exit()
    if json_check(filename, enc):
        table = AsciiTable(json_read(filename, enc))
    elif tsv_check(filename, enc):
        table = AsciiTable(tsv_read(filename, enc))
    print(table.table)
Ejemplo n.º 23
0
                for g in optimizer.param_groups:
                    g['lr'] = lr

                # Run optimizer
                optimizer.step()
                # Reset gradients
                optimizer.zero_grad()

            # ----------------
            #   Log progress
            # ----------------
            log_str = ""
            log_str += AsciiTable([
                ["Type", "Value"],
                ["IoU loss", float(loss_components[0])],
                ["Object loss", float(loss_components[1])],
                ["Class loss", float(loss_components[2])],
                ["Loss", float(loss_components[3])],
                ["Batch loss", to_cpu(loss).item()],
            ]).table

            if opt.verbose: print(log_str)

            # Tensorboard logging
            tensorboard_log = [("train/iou_loss", float(loss_components[0])),
                               ("train/obj_loss", float(loss_components[1])),
                               ("train/class_loss", float(loss_components[2])),
                               ("train/loss", to_cpu(loss).item())]
            logger.list_of_scalars_summary(tensorboard_log, batches_done)

            model.seen += imgs.size(0)
def evaluate_two_class_unambiguous_model(
    model_fn, data_iter, attack_list,
    model_name=None,
    eval_results_dir='/tmp/unrestricted_advex_evals',
):
  """
  Evaluates a model_fn on a set of attacks and creates plots
  :param model_fn: A function mapping images to logits
  :param dataset_iter: An iterable that returns (batched_images, batched_labels, image_ids)
  :param attack_list: A list of callable Attacks
  :param model_name: An optional model_fn name

  :return a map from attack_name to accuracy at 80% and 100%
  """
  if model_name is None:
    model_name = 'unnamed_defense'
  adversarial_images_dir = os.path.join(eval_results_dir, model_name)

  # Load the whole data_iter into memory because we will iterate through the iterator multiple times
  data_iter = list(data_iter)

  table_data = [
    ['Attack name', 'Acc @ 80% cov', 'Acc @ 100% cov']
  ]
  results = {}
  for attack in attack_list:
    print("Executing attack: %s" % attack.name)

    if attack._stop_after_n_datapoints is not None:
      batch_size = len(data_iter[0][0])
      n_batches = int(math.ceil(float(attack._stop_after_n_datapoints) / batch_size))
      attack_data_iter = data_iter[:n_batches]
    else:
      attack_data_iter = data_iter

    logits, labels, correct, x_adv, image_ids = run_attack(model_fn, attack_data_iter, attack)

    results_dir = os.path.join(adversarial_images_dir, attack.name)
    plotting.save_correct_and_incorrect_adv_images(
      x_adv=x_adv,
      correct=correct,
      labels=labels,
      image_ids=image_ids,
      results_dir=results_dir,
    )

    # Confidence is the value of the larger of the two logits
    confidences = np.max(logits, axis=1)

    # We will plot accuracy at various coverages
    desired_coverages = np.linspace(0.01, 1.00, 100)

    preds = logits_to_preds(logits)
    num_covered, confident_error_idxs = _get_num_covered_and_confident_error_idxs(
      desired_coverages, preds, confidences, labels)

    plotting.plot_confident_error_rate(
      desired_coverages, confident_error_idxs, len(labels), attack.name, results_dir,
      legend=model_name)

    # Add accuracy at 80% and 100% to table and results
    num_errors_at_80 = len(confident_error_idxs[80 - 1])
    num_covered_at_80 = num_covered[80 - 1]

    num_errors_at_100 = len(confident_error_idxs[-1])
    num_covered_at_100 = num_covered[-1]

    assert len(logits) == num_covered_at_100
    assert len(logits) == (np.sum(correct, axis=0) + num_errors_at_100)

    acc_at_80 = 1.0 - (float(num_errors_at_80) / num_covered_at_80)
    acc_at_100 = 1.0 - (float(num_errors_at_100) / num_covered_at_100)

    table_data.append([attack.name, acc_at_80, acc_at_100])
    results[attack.name] = {
      'accuracy@80': acc_at_80,
      'accuracy@100': acc_at_100,
      'confident_error_idxs@80': confident_error_idxs[80 - 1],
      'confident_error_idxs@100': confident_error_idxs[-1],
    }
    print("[%s] acc@80%% = %.3f" % (attack.name, acc_at_80))

  # Print results
  print(AsciiTable(table_data).table)
  print("To visually inspect the adversarial examples "
        "that your model correctly and incorrectly classified, "
        "view them in: %s " % adversarial_images_dir)
  return results
Ejemplo n.º 25
0
                "Metrics",
                *[f"YOLO Layer {i}" for i in range(len(model.yolo_layers))]
            ]]

            # Log metrics at each YOLO layer
            formats = {m: "%.6f" for m in metrics}
            formats["grid_size"] = "%2d"
            formats["cls_acc"] = "%.2f%%"
            for metric in metrics:
                row_metrics = [
                    formats[metric] % yolo.metrics.get(metric, 0)
                    for yolo in model.yolo_layers
                ]
                metric_table += [[metric, *row_metrics]]

            log_str += AsciiTable(metric_table).table
            log_str += f"\nTotal loss {loss.item()}"

            # Determine approximate time left for epoch
            epoch_batches_left = len(dataloader) - (batch_i + 1)
            time_left = datetime.timedelta(seconds=epoch_batches_left *
                                           (time.time() - start_time) /
                                           (batch_i + 1))
            log_str += f"\n---- ETA {time_left}"

            print(log_str)

            # Tensorboard logging
            tensorboard_log = []
            for i, yolo in enumerate(model.yolo_layers):
                for name, metric in yolo.metrics.items():
Ejemplo n.º 26
0
def get_crypto_table(clear_console=False, colored=True):
    """
    Output: Returns an ascii table for all cryptocurrencies and their data
    Parameters:
        - clearConsole: Do we want to clear the console before
            returning this data (we do want to do this when running in
            monitor mode)
        - Colored: Do we want the table to be colored
    Logic:
        - Create header
        - Get metrics on each legal currency and insert into their own
            array
        - Get the total fiat by adding the last index of each metrics
            array together
        - Insert cointypes into the respective array
        - Combine the header and the crypto metrics into one big
            metrics array
        - Create the ascii table from this data and return it
    """
    metrics = []
    bitcoin_metrics = get_crypto_info("bitcoin", colored)
    ethereum_metrics = get_crypto_info("ethereum", colored)
    litecoin_metrics = get_crypto_info("litecoin", colored)
    bitcoin_cash_metrics = get_crypto_info("bitcoin-cash", colored)
    dash_metrics = get_crypto_info("dash", colored)
    ripple_metrics = get_crypto_info("ripple", colored)
    digibyte_metrics = get_crypto_info("digibyte", colored)
    total_fiat =   bitcoin_metrics[-1] \
                  + ethereum_metrics[-1] \
                  + litecoin_metrics[-1] \
                  + bitcoin_cash_metrics[-1] \
                  + dash_metrics[-1] \
                  + ripple_metrics[-1] \
                  + digibyte_metrics[-1]
    if colored:
        header = [
            Color("{automagenta}Coin Type{/automagenta}"),
            Color("{automagenta}Price " + config.fiatCurrency +
                  "{/automagenta}"),
            Color("{automagenta}24h Volume{/automagenta}"),
            Color("{automagenta}7d % Change{/automagenta}"),
            Color("{automagenta}24h % Change{/automagenta}"),
            Color("{automagenta}1h % Change{/automagenta}"),
            Color("{automagenta}Crypto Balance{/automagenta}"),
            Color("{automagenta}" + config.fiatCurrency.upper() + " Balance" +
                  "{/automagenta}")
        ]
        bitcoin_metrics.insert(
            0, Color("{autocyan}Bitcoin      (BTC){/autocyan}"))
        ethereum_metrics.insert(
            0, Color("{autocyan}Ethereum     (ETH){/autocyan}"))
        litecoin_metrics.insert(
            0, Color("{autocyan}Litecoin     (LTC){/autocyan}"))
        bitcoin_cash_metrics.insert(
            0, Color("{autocyan}Bitcoin Cash (BCH){/autocyan}"))
        dash_metrics.insert(0,
                            Color("{autocyan}Dash         (DSH){/autocyan}"))
        ripple_metrics.insert(0,
                              Color("{autocyan}Ripple       (XRP){/autocyan}"))
        digibyte_metrics.insert(
            0, Color("{autocyan}Digibyte     (DGB){/autocyan}"))
        footer = Color(
            "{automagenta}Last Updated: %s{/automagenta}\t\t\t\t\t\t\t      "
            "{autogreen}Total %s: %.2f{/autogreen}" %
            (str(datetime.now()), config.fiatCurrency, total_fiat))
    else:
        header = [
            "Coin Type", "Price " + config.fiatCurrency, "24h Volume",
            "7d % Change", "24h % Change", "1h % Change", "Crypto Balance",
            config.fiatCurrency.upper() + " Balance"
        ]
        bitcoin_metrics.insert(0, "Bitcoin      (BTC)")
        ethereum_metrics.insert(0, "Ethereum     (ETH)")
        litecoin_metrics.insert(0, "Litecoin     (LTC)")
        bitcoin_cash_metrics.insert(0, "Bitcoin Cash (BCH)")
        dash_metrics.insert(0, "Dash         (DSH)")
        ripple_metrics.insert(0, "Ripple       (XRP)")
        digibyte_metrics.insert(0, "Digibyte     (DGB)")
        footer = ("Last Updated: %s \t\t\t\t\t\t\t      Total %s: %.2f" %
                  (str(datetime.now()), config.fiatCurrency, total_fiat))
    metrics.append(header)
    metrics.append(bitcoin_metrics)
    metrics.append(ethereum_metrics)
    metrics.append(litecoin_metrics)
    metrics.append(bitcoin_cash_metrics)
    metrics.append(dash_metrics)
    metrics.append(ripple_metrics)
    metrics.append(digibyte_metrics)
    table = AsciiTable(metrics)
    if clear_console:
        clear()
    return table.table + "\n" + footer
Ejemplo n.º 27
0
    def evaluate(self,
                 results,
                 metric='bbox',
                 logger=None,
                 jsonfile_prefix=None,
                 classwise=False,
                 proposal_nums=(100, 300, 1000),
                 iou_thrs=None,
                 metric_items=None):
        """Evaluation in COCO protocol.

        Args:
            results (list[list | tuple]): Testing results of the dataset.
            metric (str | list[str]): Metrics to be evaluated. Options are
                'bbox', 'segm', 'proposal', 'proposal_fast'.
            logger (logging.Logger | str | None): Logger used for printing
                related information during evaluation. Default: None.
            jsonfile_prefix (str | None): The prefix of json files. It includes
                the file path and the prefix of filename, e.g., "a/b/prefix".
                If not specified, a temp file will be created. Default: None.
            classwise (bool): Whether to evaluating the AP for each class.
            proposal_nums (Sequence[int]): Proposal number used for evaluating
                recalls, such as recall@100, recall@1000.
                Default: (100, 300, 1000).
            iou_thrs (Sequence[float], optional): IoU threshold used for
                evaluating recalls/mAPs. If set to a list, the average of all
                IoUs will also be computed. If not specified, [0.50, 0.55,
                0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95] will be used.
                Default: None.
            metric_items (list[str] | str, optional): Metric items that will
                be returned. If not specified, ``['AR@100', 'AR@300',
                'AR@1000', 'AR_s@1000', 'AR_m@1000', 'AR_l@1000' ]`` will be
                used when ``metric=='proposal'``, ``['mAP', 'mAP_50', 'mAP_75',
                'mAP_s', 'mAP_m', 'mAP_l']`` will be used when
                ``metric=='bbox' or metric=='segm'``.

        Returns:
            dict[str, float]: COCO style evaluation metric.
        """

        metrics = metric if isinstance(metric, list) else [metric]
        allowed_metrics = [
            'bbox', 'segm', 'proposal', 'proposal_fast', 'pixel'
        ]
        for metric in metrics:
            if metric not in allowed_metrics:
                raise KeyError(f'metric {metric} is not supported')
        if iou_thrs is None:
            iou_thrs = np.linspace(.5,
                                   0.95,
                                   int(np.round((0.95 - .5) / .05)) + 1,
                                   endpoint=True)
        if metric_items is not None:
            if not isinstance(metric_items, list):
                metric_items = [metric_items]

        result_files, tmp_dir = self.format_results(results, jsonfile_prefix)

        eval_results = OrderedDict()
        cocoGt = self.coco
        for metric in metrics:
            msg = f'Evaluating {metric}...'
            if logger is None:
                msg = '\n' + msg
            print_log(msg, logger=logger)

            if metric == 'proposal_fast':
                ar = self.fast_eval_recall(results,
                                           proposal_nums,
                                           iou_thrs,
                                           logger='silent')
                log_msg = []
                for i, num in enumerate(proposal_nums):
                    eval_results[f'AR@{num}'] = ar[i]
                    log_msg.append(f'\nAR@{num}\t{ar[i]:.4f}')
                log_msg = ''.join(log_msg)
                print_log(log_msg, logger=logger)
                continue

            if metric not in result_files:
                raise KeyError(f'{metric} is not in results')
            try:
                cocoDt = cocoGt.loadRes(result_files[metric])
            except IndexError:
                print_log('The testing results of the whole dataset is empty.',
                          logger=logger,
                          level=logging.ERROR)
                break

            if metric == 'pixel':
                iou_type = 'segm'
            else:
                iou_type = 'bbox' if metric == 'proposal' else metric
            cocoEval = COCOeval(cocoGt, cocoDt, iou_type)
            cocoEval.params.catIds = self.cat_ids
            cocoEval.params.imgIds = self.img_ids
            cocoEval.params.maxDets = list(proposal_nums)
            cocoEval.params.iouThrs = iou_thrs
            # mapping of cocoEval.stats
            coco_metric_names = {
                'mAP': 0,
                'mAP_50': 1,
                'mAP_75': 2,
                'mAP_s': 3,
                'mAP_m': 4,
                'mAP_l': 5,
                'AR@100': 6,
                'AR@300': 7,
                'AR@1000': 8,
                'AR_s@1000': 9,
                'AR_m@1000': 10,
                'AR_l@1000': 11
            }
            if metric_items is not None:
                for metric_item in metric_items:
                    if metric_item not in coco_metric_names:
                        raise KeyError(
                            f'metric item {metric_item} is not supported')

            if metric == 'proposal':
                cocoEval.params.useCats = 0
                cocoEval.evaluate()
                cocoEval.accumulate()
                cocoEval.summarize()
                if metric_items is None:
                    metric_items = [
                        'AR@100', 'AR@300', 'AR@1000', 'AR_s@1000',
                        'AR_m@1000', 'AR_l@1000'
                    ]

                for item in metric_items:
                    val = float(
                        f'{cocoEval.stats[coco_metric_names[item]]:.3f}')
                    eval_results[item] = val
            elif metric == 'pixel':
                cocoEval._prepare()
                full_gt_mask = []
                full_dt_mask = []
                for img_index in cocoGt.imgToAnns.keys():
                    gt_img_anns = cocoGt.imgToAnns[img_index]
                    gt_img_info = cocoGt.imgs[img_index]
                    dt_img_anns = cocoDt.imgToAnns[img_index]
                    dt_img_info = cocoDt.imgs[img_index]

                    gt_mask = anns_to_mask(gt_img_anns, gt_img_info['height'],
                                           gt_img_info['width'])
                    dt_mask = anns_to_mask(dt_img_anns, dt_img_info['height'],
                                           gt_img_info['width'])

                    full_gt_mask.append(gt_mask)
                    full_dt_mask.append(dt_mask)

                max_size = np.max([mask.shape[:2] for mask in full_gt_mask],
                                  axis=0)

                for i, _ in enumerate(full_gt_mask):
                    full_gt_mask[i] = pad_img_to_size(full_gt_mask[i],
                                                      max_size)
                    full_dt_mask[i] = pad_img_to_size(full_dt_mask[i],
                                                      max_size)

                full_gt_mask = np.stack(full_gt_mask)
                full_dt_mask = np.stack(full_dt_mask)

                metrics = calculate_metrics(full_dt_mask, full_gt_mask)

                table_data = [['Pixel Metric', 'Value']]
                # add the pixel-wise metrics to the output
                for name, value in metrics._asdict().items():
                    eval_results[f'{metric}_{name}'] = value
                    table_data.append([f'{metric}_{name}', '%.4f' % value])

                table = AsciiTable(table_data)
                print_log('\n' + table.table, logger=logger)
            else:
                cocoEval.evaluate()
                cocoEval.accumulate()
                cocoEval.summarize()
                if classwise:  # Compute per-category AP
                    # Compute per-category AP
                    # from https://github.com/facebookresearch/detectron2/
                    precisions = cocoEval.eval['precision']
                    # precision: (iou, recall, cls, area range, max dets)
                    assert len(self.cat_ids) == precisions.shape[2]

                    results_per_category = []
                    for idx, catId in enumerate(self.cat_ids):
                        # area range index 0: all area ranges
                        # max dets index -1: typically 100 per image
                        nm = self.coco.loadCats(catId)[0]
                        precision = precisions[:, :, idx, 0, -1]
                        precision = precision[precision > -1]
                        if precision.size:
                            ap = np.mean(precision)
                        else:
                            ap = float('nan')
                        results_per_category.append(
                            (f'{nm["name"]}', f'{float(ap):0.3f}'))

                    num_columns = min(6, len(results_per_category) * 2)
                    results_flatten = list(
                        itertools.chain(*results_per_category))
                    headers = ['category', 'AP'] * (num_columns // 2)
                    results_2d = itertools.zip_longest(*[
                        results_flatten[i::num_columns]
                        for i in range(num_columns)
                    ])
                    table_data = [headers]
                    table_data += [result for result in results_2d]
                    table = AsciiTable(table_data)
                    print_log('\n' + table.table, logger=logger)

                if metric_items is None:
                    metric_items = [
                        'mAP', 'mAP_50', 'mAP_75', 'mAP_s', 'mAP_m', 'mAP_l'
                    ]

                for metric_item in metric_items:
                    key = f'{metric}_{metric_item}'
                    val = float(
                        f'{cocoEval.stats[coco_metric_names[metric_item]]:.3f}'
                    )
                    eval_results[key] = val
                ap = cocoEval.stats[:6]
                eval_results[f'{metric}_mAP_copypaste'] = (
                    f'{ap[0]:.3f} {ap[1]:.3f} {ap[2]:.3f} {ap[3]:.3f} '
                    f'{ap[4]:.3f} {ap[5]:.3f}')
        if tmp_dir is not None:
            tmp_dir.cleanup()
        return eval_results
Ejemplo n.º 28
0
Archivo: dataset.py Proyecto: dmxj/icv
    def statistic(self,
                  print_log=True,
                  is_plot_show=False,
                  plot_save_path=None):
        RATIO_TOP_K = 15
        samples = self.get_samples()

        sta = dict(overview=dict(), detail=dict())
        sta["overview"] = {
            "image_num":
            self.length,
            "class_num":
            len(self.categories),
            "bbox_num":
            sum([sample.count for sample in samples]),
            "min_width":
            min([sample.width for sample in samples]),
            "max_width":
            max([sample.width for sample in samples]),
            "min_height":
            min([sample.height for sample in samples]),
            "max_height":
            max([sample.height for sample in samples]),
            "small_bbox_num":
            0,
            "middle_bbox_num":
            0,
            "large_bbox_num":
            0,
            "same_size":
            len(set([(sample.height, sample.width)
                     for sample in samples])) == 1,
            "ratio_distribution":
            OrderedDict(),
        }

        sta["detail"] = {
            cat: {
                "num": 0,
                "ratio_distribution": OrderedDict(),
            }
            for cat in self.categories
        }

        # get bbox ratio distribution
        for sample in samples:
            s = sample.statistics()
            for label in s["cats"]:
                sta["detail"][label]["num"] += s["cats"][label]

            for label in s["ratios"]:
                for r in s["ratios"][label]:
                    ratio = str(r)
                    if ratio not in sta["overview"]["ratio_distribution"]:
                        sta["overview"]["ratio_distribution"][ratio] = 0
                    sta["overview"]["ratio_distribution"][ratio] += s[
                        "ratios"][label][r]

                    if ratio not in sta["detail"][label]["ratio_distribution"]:
                        sta["detail"][label]["ratio_distribution"][ratio] = 0
                    sta["detail"][label]["ratio_distribution"][ratio] += s[
                        "ratios"][label][r]

        # truncate for top k ratio num
        sta["overview"]["ratio_distribution"] = dict(
            OrderedDict(
                sorted(sta["overview"]["ratio_distribution"].items(),
                       key=lambda t: -t[1])[:RATIO_TOP_K]))

        ratio_kvs_totals = [
            (r, c)
            for r, c in sta["detail"][label]["ratio_distribution"].items()
            for label in sta["detail"]
        ]
        ratio_kvs = []
        forget_rks = []
        for (r, c) in ratio_kvs_totals:
            if r not in forget_rks:
                ratio_kvs.append((r, c))
            if len(ratio_kvs) >= RATIO_TOP_K:
                break
            forget_rks.append(r)

        ratio_keys = list(zip(*ratio_kvs))[0]
        for label in sta["detail"]:
            sta["detail"][label]["ratio_distribution"] = {
                k: sta["detail"][label]["ratio_distribution"][k]
                for k in ratio_keys
                if k in sta["detail"][label]["ratio_distribution"]
            }
        '''
        ratio_kvs = []
        for i, label in enumerate(sta["detail"]):
            tmp_rd = dict(
                OrderedDict(
                    sorted(sta["detail"][label]["ratio_distribution"].items(), key=lambda t: -t[1])[:RATIO_TOP_K * 10]))

            tmp_rd_k = list(tmp_rd.keys())
            tmp_rd_v = list(tmp_rd.values())
            if i == 0:
                ratio_kvs = list(zip(tmp_rd_k, tmp_rd_v))
            else:
                ratio_kvs = [x for x in ratio_kvs if x[0] in tmp_rd_k]

        ratio_kvs = sorted(ratio_kvs, key=lambda t: -t[1])[:RATIO_TOP_K]
        ratio_keys = list(zip(*ratio_kvs))[0]

        for label in sta["detail"]:
            sta["detail"][label]["ratio_distribution"] = {k: sta["detail"][label]["ratio_distribution"][k] for k in
                                                          ratio_keys}
        '''

        # get target(bbox/seg) size distribution
        bbox_size_dict = {}
        for sample in samples:
            for anno in sample.annos:
                if anno.label not in bbox_size_dict:
                    bbox_size_dict[anno.label] = []

                if str(round(anno.bbox.ratio, 1)) in ratio_keys:
                    bbox_size_dict[anno.label].append(
                        (anno.bbox.width, anno.bbox.height))

                if anno.bbox.is_small:
                    sta["overview"]["small_bbox_num"] += 1
                elif anno.bbox.is_middle:
                    sta["overview"]["middle_bbox_num"] += 1
                elif anno.bbox.is_large:
                    sta["overview"]["large_bbox_num"] += 1

                if self.is_seg_mode:
                    if "small_segarea_num" not in sta["overview"]:
                        sta["overview"]["small_segarea_num"] = 0
                    if "middle_segarea_num" not in sta["overview"]:
                        sta["overview"]["middle_segarea_num"] = 0
                    if "large_segarea_num" not in sta["overview"]:
                        sta["overview"]["large_segarea_num"] = 0

                    if (anno.seg_mode_mask and anno.mask.is_small) or (
                            anno.seg_mode_polys and anno.polys.is_small):
                        sta["overview"]["small_segarea_num"] += 1
                    if (anno.seg_mode_mask and anno.mask.is_middle) or (
                            anno.seg_mode_polys and anno.polys.is_middle):
                        sta["overview"]["middle_segarea_num"] += 1
                    if (anno.seg_mode_mask and anno.mask.is_large) or (
                            anno.seg_mode_polys and anno.polys.is_large):
                        sta["overview"]["large_segarea_num"] += 1

        if print_log or is_plot_show or plot_save_path is not None:
            # for overview
            overview_header = [
                "image_num", "class_num", "bbox_num", "same_image_size",
                "min_image_width", "max_image_width", "min_image_height",
                "max_image_height"
            ]
            overview_header_cn = [
                "图片总数", "类别总数", "目标框总数", "图片是否同尺寸", "最小图片宽度", "最大图片宽度",
                "最小图片高度", "最大图片高度"
            ]
            overview_table_data = [
                sta["overview"]["image_num"],
                sta["overview"]["class_num"],
                sta["overview"]["bbox_num"],
                sta["overview"]["same_size"],
                sta["overview"]["min_width"],
                sta["overview"]["max_width"],
                sta["overview"]["min_height"],
                sta["overview"]["max_height"],
            ]
            overview_table = AsciiTable(
                [overview_header, overview_header_cn, overview_table_data])
            overview_table.inner_footing_row_border = True

            # for target statistic
            target_statistic_header = [
                "bbox_num", "small_bbox_num", "middle_bbox_num",
                "large_bbox_num"
            ]
            target_statistic_header_cn = [
                "目标框总数", "小目标框总数", "中目标框总数", "大目标框总数"
            ]
            target_statistic_data = [
                sta["overview"]["bbox_num"],
                sta["overview"]["small_bbox_num"],
                sta["overview"]["middle_bbox_num"],
                sta["overview"]["large_bbox_num"],
            ]

            if self.is_seg_mode:
                target_statistic_header.extend([
                    "small_segarea_num", "middle_segarea_num",
                    "large_segarea_num"
                ])
                target_statistic_header_cn.extend(
                    ["小目标分割区域总数", "中目标分割区域总数", "大目标分割区域总数"])
                target_statistic_data.extend([
                    sta["overview"]["small_segarea_num"],
                    sta["overview"]["middle_segarea_num"],
                    sta["overview"]["large_segarea_num"],
                ])
            target_statistic_table = AsciiTable([
                target_statistic_header, target_statistic_header_cn,
                target_statistic_data
            ])
            target_statistic_table.inner_footing_row_border = True

            # for target rartio
            ratios = sorted(list(sta["overview"]["ratio_distribution"].keys()))

            ratio_distri_header = ["bbox ratio (h/w)"] + ratios
            ratio_distri_data = [
                sta["overview"]["ratio_distribution"][r] for r in ratios
            ]
            ratio_distri_table_data = [
                ratio_distri_header, ["-"] + ratio_distri_data
            ]

            ratio_distri_table = AsciiTable(ratio_distri_table_data)
            ratio_distri_table.inner_footing_row_border = True

            # for categories statistic
            cats_detail_header = ["bbox ratio (h/w) \ class"] + self.categories
            cats_detail_table_data = [cats_detail_header]
            cats_num_data = [
                sta["detail"][cat]["num"] for cat in self.categories
            ]
            cats_detail_table_data.append(["total num"] + cats_num_data)
            cat_ratios = sorted(
                list(sta["detail"][self.categories[0]]
                     ["ratio_distribution"].keys()))
            for r in cat_ratios:
                cats_detail_table_data.append([r] + [
                    sta["detail"][cat]["ratio_distribution"].get(r, 0)
                    for cat in self.categories
                ])

            cats_detail_table = AsciiTable(cats_detail_table_data)
            cats_detail_table.inner_footing_row_border = False

            print("overview[概览]:")
            print(overview_table.table)

            print("\nclass detail[类别数量分布]:")
            print(cats_detail_table.table)

            print("\ntarget statistic[目标框/分割区域统计]:")
            print(target_statistic_table.table)

            print("\nbbox ratio (h/w) distribution[目标框比例(高/宽)分布]:")
            print(ratio_distri_table.table)

            if is_plot_show or plot_save_path is not None:
                import matplotlib.pyplot as plt
                fig = plt.figure(num=5, figsize=(15, 8), dpi=100)

                ax1 = fig.add_subplot(3, 2, 1)
                overview_header_post = overview_header[:3] + overview_header[4:]
                overview_table_data_post = overview_table_data[:
                                                               3] + overview_table_data[
                                                                   4:]
                b1 = ax1.barh(list(range(len(overview_header_post))),
                              overview_table_data_post,
                              color='#6699CC',
                              tick_label=overview_header_post)
                for i, rect in enumerate(b1):
                    w = rect.get_width()
                    ax1.text(w + 1,
                             rect.get_y() + rect.get_height() / 2,
                             '%d' % int(overview_table_data_post[i]),
                             ha='left',
                             va='center')

                ax1.set_title("overview")

                ax2 = fig.add_subplot(3, 2, 2)
                b2 = ax2.bar(list(range(len(self.categories))),
                             cats_num_data,
                             color="red",
                             tick_label=self.categories)
                for xi, rect in enumerate(b2):
                    ax2.text(rect.get_x() + rect.get_width() / 2,
                             rect.get_height(),
                             "%d" % int(cats_num_data[xi]),
                             ha="center",
                             va="bottom")

                ax2.set_title("class detail")

                ax3 = fig.add_subplot(3, 2, 3)
                b3 = ax3.barh(list(range(len(target_statistic_header))),
                              target_statistic_data,
                              color="green",
                              tick_label=target_statistic_header)
                for i, rect in enumerate(b3):
                    w = rect.get_width()
                    ax3.text(w,
                             rect.get_y() + rect.get_height() / 2,
                             '%d' % int(target_statistic_data[i]),
                             ha='left',
                             va='center')

                ax3.set_title("target statistic")

                ax4 = fig.add_subplot(3, 2, 4)
                b4 = ax4.bar(list(range(len(ratios))),
                             ratio_distri_data,
                             color="blue",
                             tick_label=ratios)
                for xi, rect in enumerate(b4):
                    ax4.text(rect.get_x() + rect.get_width() / 2,
                             rect.get_height(),
                             "%d" % int(ratio_distri_data[xi]),
                             ha="center",
                             va="bottom")

                ax4.set_title("bbox ratio (h/w) distribution")

                ax5 = fig.add_subplot(3, 2, 5)
                for cat in bbox_size_dict:
                    w, h = zip(*bbox_size_dict[cat])
                    ax5.scatter(w, h, label=cat)

                ax5.set_xlabel("bbox width")
                ax5.set_ylabel("bbox height")
                ax5.set_title("bbox size distribution")
                ax5.legend()

                if is_plot_show:
                    fig.show()

                if plot_save_path is not None:
                    fig.savefig(plot_save_path)

        return sta
Ejemplo n.º 29
0
array_y = {}
headers = []
rows = []
i = 1

# Setando valores no dicionario array_y
for x in array_k:
    if x != ',':
        array_y[i] = int(x)
        headers.append(str(i) + '° dia')
        rows.append('R$ ' + str(x))
        i += 1

# Montando table com valores de cada dia
table_data = [headers, rows]
table = AsciiTable(table_data)
print(table.table)

# Input dos dias de compra e venda das acoes
dia_compra = int(input('Informe dia da compra da ação(apenas numero): '))
dia_venda = int(input('Informe dia da venda da ação(apenas numero): '))

# Determinando  maior lucro
if dia_venda < dia_compra:
    print('Está ação não está mais dispónivel para venda :(')
elif array_y.get(dia_compra) > array_y.get(dia_venda):
    print(0)
else:
    lucro = array_y.get(dia_venda) - array_y.get(dia_compra)
    print(f'Lucro: {lucro}')
Ejemplo n.º 30
0
    def evaluate2(self,
                  result_files,
                  metric='bbox',
                  logger=None,
                  jsonfile_prefix=None,
                  classwise=False,
                  proposal_nums=(100, 300, 1000),
                  iou_thrs=np.arange(0.5, 0.96, 0.05)):

        metrics = metric if isinstance(metric, list) else [metric]
        allowed_metrics = ['bbox', 'segm', 'proposal', 'proposal_fast']
        for metric in metrics:
            if metric not in allowed_metrics:
                raise KeyError(f'metric {metric} is not supported')

        eval_results = {}
        cocoGt = self.coco
        for metric in metrics:
            msg = f'Evaluating {metric}...'
            if logger is None:
                msg = '\n' + msg
            print_log(msg, logger=logger)

            if metric not in result_files:
                raise KeyError(f'{metric} is not in results')
            try:
                # cocoDt = cocoGt.loadRes(result_files[metric])
                from mmdet.helper.helpers import loadRes
                cocoDt = loadRes(cocoGt, result_files[metric])
            except IndexError:
                print_log('The testing results of the whole dataset is empty.',
                          logger=logger,
                          level=logging.ERROR)
                break
            iou_type = 'bbox' if metric == 'proposal' else metric
            cocoEval = COCOeval(cocoGt, cocoDt, iou_type)
            cocoEval.params.catIds = self.cat_ids
            cocoEval.params.imgIds = self.img_ids
            if metric == 'proposal':
                cocoEval.params.useCats = 0
                cocoEval.params.maxDets = list(proposal_nums)
                cocoEval.evaluate()
                cocoEval.accumulate()
                cocoEval.summarize()
                metric_items = [
                    'AR@100', 'AR@300', 'AR@1000', 'AR_s@1000', 'AR_m@1000',
                    'AR_l@1000'
                ]
                for i, item in enumerate(metric_items):
                    val = float(f'{cocoEval.stats[i + 6]:.3f}')
                    eval_results[item] = val
            else:

                # cocoEval.evaluate()
                cocoevalhelper.evaluate(cocoEval)

                ############calculate centroids ######################
                cocoevalhelper.computeCentroids(cocoEval)

                # cocoEval.accumulate()
                self.accumulate(cocoEval)
                self.summarize(cocoEval)
                if classwise:  # Compute per-category AP
                    # Compute per-category AP
                    # from https://github.com/facebookresearch/detectron2/
                    precisions = cocoEval.eval['precision']
                    # precision: (iou, recall, cls, area range, max dets)
                    assert len(self.cat_ids) == precisions.shape[2]

                    results_per_category = []
                    for idx, catId in enumerate(self.cat_ids):
                        # area range index 0: all area ranges
                        # max dets index -1: typically 100 per image
                        nm = self.coco.loadCats(catId)[0]
                        precision = precisions[:, :, idx, 0, -1]
                        precision = precision[precision > -1]
                        if precision.size:
                            ap = np.mean(precision)
                        else:
                            ap = float('nan')
                        results_per_category.append(
                            (f'{nm["name"]}', f'{float(ap):0.3f}'))

                    num_columns = min(6, len(results_per_category) * 2)
                    results_flatten = list(
                        itertools.chain(*results_per_category))
                    headers = ['category', 'AP'] * (num_columns // 2)
                    results_2d = itertools.zip_longest(*[
                        results_flatten[i::num_columns]
                        for i in range(num_columns)
                    ])
                    table_data = [headers]
                    table_data += [result for result in results_2d]
                    table = AsciiTable(table_data)
                    print_log('\n' + table.table, logger=logger)

                metric_items = [
                    'mAP', 'mAP_50', 'mAP_75', 'mAP_s', 'mAP_m', 'mAP_l'
                ]
                for i in range(len(metric_items)):
                    key = f'{metric}_{metric_items[i]}'
                    val = float(f'{cocoEval.stats[i]:.3f}')
                    eval_results[key] = val
                ap = cocoEval.stats[:6]
                eval_results[f'{metric}_mAP_copypaste'] = (
                    f'{ap[0]:.3f} {ap[1]:.3f} {ap[2]:.3f} {ap[3]:.3f} '
                    f'{ap[4]:.3f} {ap[5]:.3f}')

        return eval_results