text:
import numpy
import gc
import matplotlib
matplotlib.use("Agg")
import sys
from PIL import Image
# import matplotlib.pyplot as plt

import matplotlib.pyplot as plt
# from net.scalenet import ScaleNetParams, ScaleNet
# from trainer.scalenettrainer import ScaleNetTrainerParams, ScaleNetTrainer
from data_importer.util.handdetector import HandDetector
import os
import numpy as np

from data_importer.data.importers import NYUImporter
from data_importer.data.dataset import NYUDataset

from data_importer.util.helpers import shuffle_many_inplace
from data_importer.util.handpose_evaluation import ICVLHandposeEvaluation, NYUHandposeEvaluation, MSRAHandposeEvaluation

from pathlib import Path
CURRENT_DIR = Path(__file__).parent

class Anyone(object):
    def __init__(self,ix=None):

        eval_prefix = 'MSRA15_COM_AUGMENT'
        if not os.path.exists('./eval/'+eval_prefix+'/'):
                        os.makedirs('./eval/'+eval_prefix+'/')

        rng = numpy.random.RandomState(23455)

        print("create data")
        aug_modes = ['com', 'rot', 'none']  # 'sc',

        comref=None
        docom=False

        dataset_path = os.path.join(str(CURRENT_DIR.parent),"data/NYU")
        cache_path = os.path.join(str(CURRENT_DIR),"cache")
        self.di = NYUImporter(dataset_path,cacheDir=cache_path)
        # Seq1_1 = di.loadSequence('train', shuffle=True, rng=rng, docom=False)
        # Seq1_1 = Seq1_1._replace(name='train_gt')
        Seq1_2 = self.di.loadSequence('train', shuffle=True, rng=rng, docom=True)
        Seq1_2 = Seq1_2._replace(name='train_com')
        # trainSeqs = [Seq1_1, Seq1_2]
        seqs= [Seq1_2]

        # self.hpe = NYUHandposeEvaluation(gt3D, joints)

        if ix:
            sample_ix= ix
        else:
            sample_ix= np.random.randint(1000)
            print(sample_ix)

        self.data=seqs[0].data[sample_ix]
        # self.get_origin_img(self.data.fileName)
        print("you now choosing seq 0 {}".format(sample_ix))
        plt.imshow(self.data.dpt)

    def draw_point(self,step=100):

        import cv2
        bg_img = np.ones(300*300).reshape(300,300)
        annoscale = 1
        joint = self.data.gtorig
        ax = bg_img
        hpe = self.hpe

        for i in range(len(hpe.jointConnections)):
        # for i in range(step):
            lc = tuple((hpe.jointConnectionColors[i]*255.).astype(int))
        #     lc = tuple((rgb_to_gray(self.jointConnectionColors[i])*255.).astype(int))
        #     lc = color
            cv2.line(ax, (int(numpy.rint(joint[hpe.jointConnections[i][0], 0])),
                          int(numpy.rint(joint[hpe.jointConnections[i][0], 1]))),
                     (int(numpy.rint(joint[hpe.jointConnections[i][1], 0])),
                      int(numpy.rint(joint[hpe.jointConnections[i][1], 1]))),
                     (102,0,0), thickness=3*annoscale, lineType=cv2.LINE_AA)
            plt.imshow(ax)

    def get_gtorig_shape(self):

        return self.data.gtcrop.shape

    def get_origin_img(self):

        from skimage.io import imshow

        self.orig_dept=self.di.loadDepthMap(self.data.fileName)
        imshow(self.orig_dept)

def get_dept(filename):

    """
    Read a depth-map
    :param filename: file name to load
    :return: image data of depth image
    """

    from skimage.io import imshow
    import cv2
    img = Image.open(filename)
    # top 8 bits of depth are packed into green channel and lower 8 bits into blue
    assert len(img.getbands()) == 3
    # r, g, b = img.split()
    # r = np.asarray(r, np.int32)
    # g = np.asarray(g, np.int32)
    # b = np.asarray(b, np.int32)
    # dpt = np.bitwise_or(np.left_shift(g, 8), b)
    # imgdata = np.asarray(img, np.float32)
    # im = Image.fromarray(imgdata)
    # im.save("./test.png")
    # img.show()
    # fig = plt.figure()
    # plt.show(imgdata)
    # fig.savefig("./test.png")
    # imshow(imgdata)
    img  = cv2.imread(filename, -1)
    b_channel, g_channel, r_channel = cv2.split(img)
    imgdata = cv2.merge((g_channel, b_channel,r_channel))
    cv2.imshow("img",imgdata)
    cv2.waitKey(2)
    # cv2.destroyAllWindows()
    return imgdata

def get_dept_cv2(filename):
    import cv2

    """
    Read a depth-map
    :param filename: file name to load
    :return: image data of depth image
    """
    img  = cv2.imread(filename, -1)
    depth=cv2.split(img)[0]
    depth[depth>800]=0
    depth=depth/1000.0000
    cv2.imshow('img',depth)
    cv2.waitKey(1)
    return depth
def plot_line():
    a=[1,3,4]
    b=[1,3,4]
    fig = plt.figure()
    plt.plot(a,b)
    plt.show()
    fig.savefig("test.png")


text:
from __future__ import absolute_import, division, print_function

from builtins import (bytes, str, open, super, range,
                      zip, round, input, int, pow, object, map, zip)

__author__ = "Andrea Tramacere"

# Standard library
# eg copy
# absolute import rg:from copy import deepcopy

# Dependencies
# eg numpy
# absolute import eg: import numpy as np

import  os

# Project
# relative import eg: from .mod import f
from astropy.io import  fits as pf

from cdci_data_analysis.analysis.queries import ImageQuery
from cdci_data_analysis.analysis.products import QueryProductList,CatalogProduct,ImageProduct,QueryOutput
from cdci_data_analysis.analysis.catalog import BasicCatalog
from cdci_data_analysis.analysis.io_helper import  FitsFile

from .osa_catalog import  OsaIsgriCatalog,OsaJemxCatalog
from .osa_dataserve_dispatcher import    OsaDispatcher

class OsaImageProduct(ImageProduct):

    def __init__(self,name,file_name,skyima,out_dir=None,prod_prefix=None):
        header = skyima.header
        data = skyima.data
        super(OsaImageProduct, self).__init__(name,data=data,header=header,name_prefix=prod_prefix,file_dir=out_dir,file_name=file_name)
        #check if you need to copy!

    @classmethod
    def build_from_ddosa_skyima(cls,name,file_name,skyima,out_dir=None,prod_prefix=None):
        skyima=FitsFile(skyima).open()
        return  cls(name,skyima=skyima[4],out_dir=out_dir,prod_prefix=prod_prefix,file_name=file_name)

class OsaMosaicQuery(ImageQuery):

    def __init__(self,name):

        super(OsaMosaicQuery, self).__init__(name)

    def get_data_server_query(self,instrument,
                              config=None):

        scwlist_assumption, cat, extramodules, inject=OsaDispatcher.get_osa_query_base(instrument)
        E1=instrument.get_par_by_name('E1_keV').value
        E2=instrument.get_par_by_name('E2_keV').value
        target, modules, assume=self.set_instr_dictionaries(extramodules,scwlist_assumption,E1,E2)

        q=OsaDispatcher(config=config, target=target, modules=modules, assume=assume, inject=inject)

        return q

    def process_product(self, instrument, prod_list):

        query_image = prod_list.get_prod_by_name('mosaic_image')
        query_catalog = prod_list.get_prod_by_name('mosaic_catalog')
        detection_significance = instrument.get_par_by_name('detection_threshold').value

        if detection_significance is not None:
            query_catalog.catalog.selected = query_catalog.catalog._table['significance'] > float(
                detection_significance)

        print('--> query was ok')
        # file_path = Path(scratch_dir, 'query_mosaic.fits')
        query_image.write(overwrite=True)
        # file_path = Path(scratch_dir, 'query_catalog.fits')
        query_catalog.write(overwrite=True)

        html_fig = query_image.get_html_draw(catalog=query_catalog.catalog,
                                             vmin=instrument.get_par_by_name('image_scale_min').value,
                                             vmax=instrument.get_par_by_name('image_scale_max').value)

        query_out = QueryOutput()

        query_out.prod_dictionary['image'] = html_fig
        query_out.prod_dictionary['catalog'] = query_catalog.catalog.get_dictionary()
        query_out.prod_dictionary['file_name'] = str(query_image.file_path.name)
        query_out.prod_dictionary['download_file_name'] = 'image.gz'
        query_out.prod_dictionary['prod_process_message'] = ''

        return query_out

    def set_instr_dictionaries(self,extramodules,scwlist_assumption,E1,E2):
        raise RuntimeError('Must be specified for each instrument')

class JemxMosaicQuery(OsaMosaicQuery):
    def __init__(self,name ):
        super(JemxMosaicQuery, self).__init__(name)

    def get_dummy_products(self, instrument, config=None, **kwargs):
        pass

    def set_instr_dictionaries(self,extramodules,scwlist_assumption,E1,E2):
        target = "mosaic_jemx"
        modules = ["git://ddjemx","git://ddosa_delegate_ddjemx"] + extramodules

        assume = ['ddjemx.JMXScWImageList(input_scwlist=%s)' % scwlist_assumption[0],
                   scwlist_assumption[1],
                  'ddjemx.JEnergyBins(use_bins=[(%(E1)s,%(E2)s)])' % dict(E1=E1, E2=E2),
                  'ddjemx.JEMX(use_num=2)']

        return target, modules, assume

    def build_product_list(self, instrument,res, out_dir, prod_prefix=None):

        image = OsaImageProduct.build_from_ddosa_skyima('mosaic_image', 'jemx_query_mosaic.fits', res.skyima,
                                                        out_dir=out_dir, prod_prefix=prod_prefix)
        osa_catalog = CatalogProduct('mosaic_catalog', catalog=OsaJemxCatalog.build_from_ddosa_srclres(res.srclres),
                                     file_name='query_catalog.fits', name_prefix=prod_prefix, file_dir=out_dir)

        prod_list = [image, osa_catalog]

        return prod_list

    def get_dummy_products(self, instrument, config, out_dir='./'):

        dummy_cache = config.dummy_cache

        failed = False
        image = None
        catalog = None

        user_catalog = instrument.get_par_by_name('user_catalog').value

        image = ImageProduct.from_fits_file(in_file='%s/jemx_query_mosaic.fits' % dummy_cache,
                                            out_file_name='jemx_query_mosaic.fits',
                                            prod_name='mosaic_image',
                                            ext=0,
                                            file_dir=out_dir)

        catalog = CatalogProduct(name='mosaic_catalog',
                                 catalog=BasicCatalog.from_fits_file('%s/query_catalog.fits' % dummy_cache),
                                 file_name='query_catalog.fits',
                                 file_dir=out_dir)

        if user_catalog is not None:
            print('setting from user catalog', user_catalog, catalog)
            catalog.catalog = user_catalog

        prod_list = QueryProductList(prod_list=[image, catalog])
        return prod_list

class IsgriMosaicQuery(OsaMosaicQuery):
    def __init__(self,name ):
        super(IsgriMosaicQuery, self).__init__(name)

    def build_product_list(self,instrument,res,out_dir,prod_prefix=None):
        #print('ciccio', prod_prefix)

        image = OsaImageProduct.build_from_ddosa_skyima('mosaic_image', 'isgri_query_mosaic.fits', res.skyima,
                                                            out_dir=out_dir, prod_prefix=prod_prefix)
        osa_catalog = CatalogProduct('mosaic_catalog',
                                         catalog=OsaIsgriCatalog.build_from_ddosa_srclres(res.srclres),
                                         file_name='query_catalog.fits', name_prefix=prod_prefix, file_dir=out_dir)

        prod_list =  [image, osa_catalog]

        return prod_list

    def set_instr_dictionaries(self,extramodules,scwlist_assumption,E1,E2):
        print ('E1,E2',E1,E2)
        target = "mosaic_ii_skyimage"
        modules = ["git://ddosa", 'git://ddosa_delegate'] + extramodules
        assume = ['ddosa.ImageGroups(input_scwlist=%s)' % scwlist_assumption[0],
                   scwlist_assumption[1],
                  'ddosa.ImageBins(use_ebins=[(%(E1)s,%(E2)s)],use_version="onebin_%(E1)s_%(E2)s")'%dict(E1=E1,E2=E2),
                  'ddosa.ImagingConfig(use_SouFit=0,use_version="soufit0")', ]

        return target,modules,assume

    def get_dummy_products(self, instrument, config, out_dir='./'):

        dummy_cache = config.dummy_cache

        failed = False
        image = None
        catalog = None

        user_catalog = instrument.get_par_by_name('user_catalog').value

        image = ImageProduct.from_fits_file(in_file='%s/isgri_query_mosaic.fits' % dummy_cache,
                                            out_file_name='isgri_query_mosaic.fits',
                                            prod_name='mosaic_image',
                                            ext=0,
                                            file_dir=out_dir)

        catalog = CatalogProduct(name='mosaic_catalog',
                                 catalog=BasicCatalog.from_fits_file('%s/query_catalog.fits' % dummy_cache),
                                 file_name='query_catalog.fits',
                                 file_dir=out_dir)

        if user_catalog is not None:
            print('setting from user catalog', user_catalog, catalog)
            catalog.catalog = user_catalog

        prod_list = QueryProductList(prod_list=[image, catalog])
        return prod_list

ext: py

reason: import numpy,from astropy


text:
'''
Edit form train_ocr
'''
import os, sys
import numpy as np
import cv2
import net_utils
import math
import torch.nn.functional as F
import collections
import glob
import csv
import editdistance
import torch
from ocr_utils import ocr_image, ocr_batch, crnn_batch    # next(iter(data_loader))
from data_gen import draw_box_points
from ocr_utils import print_seq_ext
from demo import resize_image
from nms import get_boxes
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
sys.path.append('./build')

f = open('codec.txt', 'r')
codec = f.readlines()[0]
codec_rev = {}
index = 4
for i in range(0, len(codec)):
  codec_rev[codec[i]] = index
  index += 1
f.close()
device = 'cuda'

def draw_text_points(img, det_text, box, color=(255, 255, 255)):
  # "draw text to img"
  font = ImageFont.truetype("Arial-Unicode-Regular.ttf", 25)
  img = Image.fromarray(img)
  center = (box[0, :] + box[1, :] + box[2, :] + box[3, :]) / 4
  pil_draw = ImageDraw.Draw(img)
  pil_draw.text(center, det_text, fill=color, font=font)
  return np.array(img)

def load_gt(p, is_icdar=False):
  '''
  load annotation from the text file,
  :param p:
  :return:
  '''
  text_polys = []
  text_gts = []
  if not os.path.exists(p):
    return np.array(text_polys, dtype=np.float32), text_gts
  with open(p, 'r') as f:
    reader = csv.reader(f, delimiter=',', quotechar='"')
    for line in reader:
      # strip BOM. \ufeff for python3,	\xef\xbb\bf for python2
      line = [i.strip('\ufeff').strip('\xef\xbb\xbf') for i in line]

      x1, y1, x2, y2, x3, y3, x4, y4 = list(map(float, line[:8]))
      # cls = 0
      gt_txt = ''
      delim = ''
      start_idx = 8
      if is_icdar:
        start_idx = 8

      for idx in range(start_idx, len(line)):
        gt_txt += delim + line[idx]
        delim = ','

      text_polys.append([x4, y4, x1, y1, x2, y2, x3, y3])
      text_line = gt_txt.strip()

      text_gts.append(text_line)

    return np.array(text_polys, dtype=np.float), text_gts

def intersect(a, b):
  '''Determine the intersection of two rectangles'''
  rect = (0, 0, 0, 0)
  r0 = max(a[0], b[0])
  c0 = max(a[1], b[1])
  r1 = min(a[2], b[2])
  c1 = min(a[3], b[3])
  # Do we have a valid intersection?
  if r1 > r0 and c1 > c0:
    rect = (r0, c0, r1, c1)
  return rect

def union(a, b):
  r0 = min(a[0], b[0])
  c0 = min(a[1], b[1])
  r1 = max(a[2], b[2])
  c1 = max(a[3], b[3])
  return (r0, c0, r1, c1)

def area(a):
  '''Computes rectangle area'''
  width = a[2] - a[0]
  height = a[3] - a[1]
  return abs(width * height)

def evaluate_image(img, detections, gt_rect, gt_txts, iou_th=0.5, iou_th_vis=0.5, iou_th_eval=0.5, eval_text_length=3):
  '''
  Summary : Returns end-to-end true-positives, detection true-positives, number of GT to be considered for eval (len > 2).
  Description : For each predicted bounding-box, comparision is made with each GT entry. Values of number of end-to-end true
                              positives, number of detection true positives, number of GT entries to be considered for evaluation are computed.

  Parameters
  ----------
  iou_th_eval : float
          Threshold value of intersection-over-union used for evaluation of predicted bounding-boxes
  iou_th_vis : float
          Threshold value of intersection-over-union used for visualization when transciption is true but IoU is lesser.
  iou_th : float
          Threshold value of intersection-over-union between GT and prediction.
  word_gto : list of lists
          List of ground-truth bounding boxes along with transcription.
  batch : list of lists
          List containing data (input image, image file name, ground truth).
  detections : tuple of tuples
          Tuple of predicted bounding boxes along with transcriptions and text/no-text score.

  Returns
  -------
  tp : int
          Number of predicted bounding-boxes having IoU with GT greater than iou_th_eval.
  tp_e2e : int
          Number of predicted bounding-boxes having same transciption as GT and len > 2.
  gt_e2e : int
          Number of GT entries for which transcription len > 2.
  '''

  gt_to_detection = {}
  detection_to_gt = {}
  tp = 0
  tp_e2e = 0
  tp_e2e_ed1 = 0
  gt_e2e = 0

  gt_matches = np.zeros(gt_rect.shape[0])
  gt_matches_ed1 = np.zeros(gt_rect.shape[0])

  # print('\n')
  for i in range(0, len(detections)):

    det = detections[i]
    box = det[0]  # Predicted bounding-box parameters
    box = np.array(box, dtype="int")  # Convert predicted bounding-box to numpy array
    box = box[0:8].reshape(4, 2)
    bbox = cv2.boundingRect(box)

    bbox = [bbox[0], bbox[1], bbox[2], bbox[3]]
    bbox[2] += bbox[0]  # Convert width to right-coordinate
    bbox[3] += bbox[1]  # Convert height to bottom-coordinate

    det_text = det[1]  # Predicted transcription for bounding-box

    for gt_no in range(len(gt_rect)):

      gtbox = gt_rect[gt_no]
      txt = gt_txts[gt_no]  # GT transcription for given GT bounding-box
      gtbox = np.array(gtbox, dtype="int")
      gtbox = gtbox[0:8].reshape(4, 2)
      rect_gt = cv2.boundingRect(gtbox)

      rect_gt = [rect_gt[0], rect_gt[1], rect_gt[2], rect_gt[3]]
      rect_gt[2] += rect_gt[0]  # Convert GT width to right-coordinate
      rect_gt[3] += rect_gt[1]  # Convert GT height to bottom-coordinate

      inter = intersect(bbox, rect_gt)  # Intersection of predicted and GT bounding-boxes
      uni = union(bbox, rect_gt)  # Union of predicted and GT bounding-boxes
      ratio = area(inter) / float(area(uni))  # IoU measure between predicted and GT bounding-boxes

      # 1). Visualize the predicted-bounding box if IoU with GT is higher than IoU threshold (iou_th) (Always required)
      # 2). Visualize the predicted-bounding box if transcription matches the GT and condition 1. holds
      # 3). Visualize the predicted-bounding box if transcription matches and IoU with GT is less than iou_th_vis and 1. and 2. hold
      if ratio > iou_th:
        ###
        img = draw_text_points(img, det_text, box, color=(0, 255, 255))
        ###
        if not gt_no in gt_to_detection:
          gt_to_detection[gt_no] = [0, 0]

        edit_dist = editdistance.eval(det_text.lower(), txt.lower())
        #############
        # print('{0}___ratio:{1}___editdist:{2}'.format(det_text, ratio, edit_dist))
        ##
        # edit_dist =0 - draw GREEN
        # edit_dist > 0 - draw BLUE
        # not match IOU - draw RED

        if edit_dist <= 1:
          gt_matches_ed1[gt_no] = 1

        if edit_dist == 0:  # det_text.lower().find(txt.lower()) != -1:
          draw_box_points(img, box, color=(0, 255, 0), thickness=2)  # GREEN - edit_distant = 0
          gt_matches[gt_no] = 1  # Change this parameter to 1 when predicted transcription is correct.
          if ratio < iou_th_vis:
            # draw_box_points(draw, box, color = (255, 255, 255), thickness=2)
            # cv2.imshow('draw', draw)
            # cv2.waitKey(0)
            pass

        '''
        gt_to_dectection {gt_no :[ratio,idx_predict] }  - ground_true thứ gt_no trùng với predict thứ idx_predict
        detection_to_gt {id_predict :[gt_no,ratio,edit_dist]}  - predict thứ idx_predict trùng với ground_true thứ gt_no
            #gt_no : ứng với dòng tứ gt_no trong file gt_txt
            #idx_predict : ứng với predict thứ idx_predict trong output list của model
            #radio : IOU
            #edit_dist : Character error rate (CER)

        '''

        tupl = gt_to_detection[gt_no]
        if tupl[0] < ratio:
          tupl[0] = ratio
          tupl[1] = i
          detection_to_gt[i] = [gt_no, ratio, edit_dist]

  # Count the number of end-to-end and detection true-positives
  ##
  # cv2.imshow('draw', img)
  # cv2.waitKey(0)

  # cv2.imwrite('preview/{0}'.format(base_nam), img)

  ##
  for gt_no in range(gt_matches.shape[0]):
    gt = gt_matches[gt_no]
    gt_ed1 = gt_matches_ed1[gt_no]
    txt = gt_txts[gt_no]

    gtbox = gt_rect[gt_no]
    gtbox = np.array(gtbox, dtype="int")
    gtbox = gtbox[0:8].reshape(4, 2)

    if len(txt) >= eval_text_length and not txt.startswith('##'):
      gt_e2e += 1
      if gt == 1:
        tp_e2e += 1
      if gt_ed1 == 1:
        tp_e2e_ed1 += 1

    if gt_no in gt_to_detection:
      tupl = gt_to_detection[gt_no]
      if tupl[0] > iou_th_eval:  # Increment detection true-positive, if IoU is greater than iou_th_eval
        if len(txt) >= eval_text_length and not txt.startswith('##'):
          tp += 1
      else:
        pass
      # draw_box_points(img, gtbox, color = (255, 255, 255), thickness=2)

  for i in range(0, len(detections)):
    det = detections[i]
    box = det[0]  # Predicted bounding-box parameters
    box = np.array(box, dtype="int")  # Convert predicted bounding-box to numpy array
    box = box[0:8].reshape(4, 2)

    if not i in detection_to_gt:
      draw_box_points(img, box, color=(0, 0, 255), thickness=2)  # RED - not match IOU > 0.5
      img = draw_text_points(img, det[1], box, color=(0, 0, 255))
    else:
      [gt_no, ratio, edit_dist] = detection_to_gt[i]
      if edit_dist > 0:
        draw_box_points(img, box, color=(255, 0, 0), thickness=2)  # BLUE - edit_distant > 0

  # cv2.imshow('draw', draw)
  # print('Missing:', len(detections) - len(detection_to_gt))
  return tp, tp_e2e, gt_e2e, tp_e2e_ed1, detection_to_gt, img

def cer(predict, gt):
  distance = editdistance.eval(predict, gt)
  return distance,len(gt)

class strLabelConverter(object):
    """Convert between str and label.
    NOTE:
        Insert `blank` to the alphabet for CTC.
    Args:
        alphabet (str): set of the possible characters.
        ignore_case (bool, default=True): whether or not to ignore all of the case.
    """

    def __init__(self, alphabet, ignore_case=False):
      self._ignore_case = ignore_case
      if self._ignore_case:
        alphabet = alphabet.lower()
      self.alphabet = alphabet + '-'  # for `-1` index

      self.dict = {}
      index = 4
      for char in (alphabet):
        # NOTE: 0 is reserved for 'blank' required by wrap_ctc
        self.dict[char] = index
        index += 1

    def encode(self, text):
      """Support batch or single str.
      Args:
          text (str or list of str): texts to convert.
      Returns:
          torch.IntTensor [length_0 + length_1 + ... length_{n - 1}]: encoded texts.
          torch.IntTensor [n]: length of each text.
      """
      if isinstance(text, str):
        texts = []
        for char in text:
          if char in self.dict:
            texts.append(self.dict[char.lower() if self._ignore_case else char])
          else:
            texts.append(3)

        length = [len(text)]
      elif isinstance(text, collections.Iterable):
        length = [len(s) for s in text]
        text = ''.join(text)
        texts, _ = self.encode(text)

      return (torch.IntTensor(texts), torch.IntTensor(length))

    def decode(self, t, length, raw=False):
      """Decode encoded texts back into strs.
      Args:
          torch.IntTensor [length_0 + length_1 + ... length_{n - 1}]: encoded texts.
          torch.IntTensor [n]: length of each text.
      Raises:
          AssertionError: when the texts and its length does not match.
      Returns:
          text (str or list of str): texts to convert.
      """
      if length.numel() == 1:
        length = length[0]
        assert t.numel() == length, "text with length: {} does not match declared length: {}".format(t.numel(),
                                                                                length)
        if raw:
          return ''.join([self.alphabet[i - 4] for i in t])
        else:
          char_list = []
          for i in range(length):
            if t[i] > 3 and t[i] < (len(self.alphabet) + 4) and (not (i > 0 and t[i - 1] == t[i])):
              char_list.append(self.alphabet[t[i] - 4])
            elif t[i] == 3 and (not (i > 0 and t[i - 1] == t[i])):
              char_list.append(' ')
          return ''.join(char_list)
      else:
        # batch mode
        assert t.numel() == length.sum(), "texts with length: {} does not match declared length: {}".format(
          t.numel(), length.sum())
        texts = []
        index = 0
        for i in range(length.numel()):
          l = length[i]
          texts.append(
            self.decode(
              t[index:index + l], torch.IntTensor([l]), raw=raw))
          index += l
        return texts

def dice_loss(segm_preds, score_maps,training_masks,multi_scale = False):

  score_maps = np.asarray(score_maps, dtype=np.uint8)
  training_masks = np.asarray(training_masks, dtype=np.uint8)

  smaps_var = net_utils.np_to_variable(score_maps, is_cuda=False)
  training_mask_var = net_utils.np_to_variable(training_masks, is_cuda=False)
  segm_pred = segm_preds[0].squeeze(1)
  segm_pred1 = segm_preds[1].squeeze(1)
  inp = segm_pred * training_mask_var
  target = smaps_var * training_mask_var

  smooth = 1.
  iflat = inp.view(-1)
  tflat = target.view(-1)
  intersection = (iflat * tflat).sum()
  result = - ((2. * intersection + smooth) /
            (iflat.sum() + tflat.sum() + smooth))
  if multi_scale:
    iou_gts = F.interpolate(smaps_var.unsqueeze(1), size=(segm_pred1.size(1), segm_pred1.size(2)), mode='bilinear',
                            align_corners=True).squeeze(1)
    iou_masks = F.interpolate(training_mask_var.unsqueeze(1), size=(segm_pred1.size(1), segm_pred1.size(2)), mode='bilinear',
                              align_corners=True).squeeze(1)
    inp2 = segm_pred1 * iou_masks
    target2 = iou_gts * iou_masks

    # smooth = 1.
    iflat2 = inp2.view(-1)
    tflat2 = target2.view(-1)
    intersection2 = (iflat2 * tflat2).sum()
    result += - ((2. * intersection2 + smooth) /
            (iflat2.sum() + tflat2.sum() + smooth))

  return result

def evaluate(e2edataloader,net):
  '''
  Description : Eval E2E. Batch will be got --> Cut rec (x1y1,x2y2,x3y3,x4y4) --> predict
  :param e2edataloader:
  :param net:
  :return:
  '''
  net.eval()
  num_count = 0
  norm_height = 44
  distance_sum = 0
  len_cer = 0
  loss_seg = 0
  len_wer = 0
  with torch.no_grad():
    for index, date in enumerate(e2edataloader):
      im_data, gtso, lbso, score_maps, training_masks = date
      im_data = im_data.to(device)
        #get_loss _ cer _ wer
      res = ocr_batch(net, codec, im_data, gtso, lbso, norm_height)
      pred, target = res
      if not isinstance(pred, list):
        pred = [pred]
      assert (len(pred) == len(target))

      target_cer = ''.join(target).replace(" ", "").lower()
      pred_cer = ''.join(pred).replace(" ", "").lower()
      distance,len_sum= cer(pred_cer,target_cer)
      distance_sum += distance
      len_cer += len_sum
      len_wer += len(pred)
      for idx in range (len(pred)):
        if pred[idx].replace(" ", "").lower() == target[idx].replace(" ", "").lower():
          num_count += 1
    WER = 1 - (num_count / len_wer)
    CER = distance_sum / len_cer
    return CER, WER

def evaluate_crnn(e2edataloader,net):
  #load image form datagen _ eval end2end
  net.eval()
  norm_height = 48
  num_count = 0
  distance_sum = 0
  len_cer = 0
  len_wer = 0
  with torch.no_grad():
    for index, date in enumerate(e2edataloader):
      im_data, gtso, lbso, score_maps, training_masks = date
      im_data = im_data.to(device)
        #get_loss _ cer _ wer
      # res = ocr_batch(net, codec, im_data, ctc_loss, gtso, lbso)
      res = crnn_batch(net, codec, im_data, gtso, lbso, norm_height)

      pred, target = res
      if not isinstance(pred, list):
        pred = [pred]
      assert (len(pred) == len(target))

      target_cer = ''.join(target).replace(" ", "").lower()
      pred_cer = ''.join(pred).replace(" ", "").lower()
      distance,len_sum= cer(pred_cer,target_cer)
      distance_sum += distance
      len_cer += len_sum
      len_wer += len(pred)
      for idx in range (len(pred)):
        if pred[idx].replace(" ", "").lower() == target[idx].replace(" ", "").lower():
          num_count += 1
    WER = 1 - (num_count / len_wer)
    CER = distance_sum / len_cer
    return CER, WER

def eval_ocr(ocrdataloader,net):
  '''
  Description: load valid_ocr to predict
  :param ocrdataloader:
  :param net: crnn
  :return: CER WER
  '''
  # norm_height = 44
  net.eval()
  converter = strLabelConverter(codec)
  num_count = 0
  distance_sum = 0
  len_cer = 0
  len_wer = 0
  with torch.no_grad():
    for index, date in enumerate(ocrdataloader):
      im_data, lbso = date
      im_data = im_data.to(device)
      features = net.forward_features(im_data)
      labels_pred = net.forward_ocr(features)

      ctc_f = labels_pred.data.cpu().numpy()
      ctc_f = ctc_f.swapaxes(1, 2)
      labelss = ctc_f.argmax(2)
      leng=[labelss.shape[1] for i in range(labelss.shape[0])]

      labelss.resize((1, labelss.shape[0] * labelss.shape[1]))
      pred = converter.decode(torch.IntTensor(labelss[0,:]),torch.IntTensor(leng))
      target = lbso

      if not isinstance(pred, list):
        pred = [pred]
      assert (len(pred) == len(target))

      target_cer = ''.join(target).replace(" ", "").lower()
      pred_cer = ''.join(pred).replace(" ", "").lower()
      distance,len_sum= cer(pred_cer,target_cer)
      distance_sum += distance
      len_cer += len_sum
      len_wer += len(target)
      for idx in range (len(target)):
        if pred[idx].replace(" ", "").lower() == target[idx].replace(" ", "").lower():
          num_count += 1
    WER = 1 - (num_count / len_wer)
    CER = distance_sum / len_cer
    return CER, WER

def eval_ocr_crnn(ocrdataloader,net):
  # Decription : evaluate OCR model
  # norm_height = 48
  net.eval()
  converter = strLabelConverter(codec)
  num_count = 0
  distance_sum = 0
  len_cer = 0
  len_wer = 0
  with torch.no_grad():
    for index, date in enumerate(ocrdataloader):
      im_data, lbso = date
      im_data = im_data.to(device)
      labels_pred = net.forward_ocr(im_data)
      labels_pred = labels_pred.permute(1, 2, 0)
      ctc_f = labels_pred.data.cpu().numpy()
      ctc_f = ctc_f.swapaxes(1, 2)
      labelss = ctc_f.argmax(2)
      leng=[labelss.shape[1] for i in range(labelss.shape[0])]

      labelss.resize((1, labelss.shape[0] * labelss.shape[1]))
      pred = converter.decode(torch.IntTensor(labelss[0,:]),torch.IntTensor(leng))
      target = lbso

      if not isinstance(pred, list):
        pred = [pred]
      assert (len(pred)== len(target))

      target_cer = ''.join(target).replace(" ", "").lower()
      pred_cer = ''.join(pred).replace(" ", "").lower()
      distance,len_sum= cer(pred_cer,target_cer)
      distance_sum += distance
      len_cer += len_sum
      len_wer += len(target)
      for idx in range (len(target)):
        if pred[idx].replace(" ", "").lower() == target[idx].replace(" ", "").lower():
          num_count += 1
    WER = 1 - (num_count / len_wer)
    CER = distance_sum / len_cer
    return CER, WER

def evaluate_e2e(root, net, norm_height = 40,name_model='E2E', normalize= False ,save = False, cuda= True,save_dir = 'eval'):
  #Decription : evaluate model E2E
  net = net.eval()
  # if cuda:
  #   print('Using cuda ...')
  #   net = net.to(device)

  images = glob.glob(os.path.join(root, '*.jpg'))
  png = glob.glob(os.path.join(root, '*.png'))
  images.extend(png)
  png = glob.glob(os.path.join(root, '*.JPG'))
  images.extend(png)

  imagess = np.asarray(images)

  tp_all = 0
  gt_all = 0
  tp_e2e_all = 0
  gt_e2e_all = 0
  tp_e2e_ed1_all = 0
  detecitons_all = 0
  eval_text_length = 2
  segm_thresh = 0.5
  min_height = 8
  idx =0

  if not os.path.exists(save_dir):
    os.mkdir(save_dir)

  note_path = os.path.join(save_dir, 'note_eval.txt')
  note_file = open(note_path, 'a')

  with torch.no_grad():

    index = np.arange(0, imagess.shape[0])
    # np.random.shuffle(index)
    for i in index:
      img_name = imagess[i]
      base_nam = os.path.basename(img_name)
      #
      # if args.evaluate == 1:
      res_gt = base_nam.replace(".jpg", '.txt').replace(".png", '.txt')
      res_gt = '{0}/gt_{1}'.format(root, res_gt)
      if not os.path.exists(res_gt):
        res_gt = base_nam.replace(".jpg", '.txt').replace("_", "")
        res_gt = '{0}/gt_{1}'.format(root, res_gt)
        if not os.path.exists(res_gt):
          print('missing! {0}'.format(res_gt))
          gt_rect, gt_txts = [], []
      # continue
      gt_rect, gt_txts = load_gt(res_gt)

      # print(img_name)
      img = cv2.imread(img_name)

      im_resized,_ = resize_image(img, max_size=1848 * 1024, scale_up=False)  # 1348*1024 #1848*1024
      images = np.asarray([im_resized], dtype=np.float)

      if normalize:
        images /= 128
        images -= 1
      im_data = net_utils.np_to_variable(images, is_cuda=cuda).permute(0, 3, 1, 2)

      [iou_pred, iou_pred1], rboxs, angle_pred, features = net(im_data)
      iou = iou_pred.data.cpu()[0].numpy()
      iou = iou.squeeze(0)

      rbox = rboxs[0].data.cpu()[0].numpy()
      rbox = rbox.swapaxes(0, 1)
      rbox = rbox.swapaxes(1, 2)

      detections = get_boxes(iou, rbox, angle_pred[0].data.cpu()[0].numpy(), segm_thresh)

      im_scalex = im_resized.shape[1] / img.shape[1]
      im_scaley = im_resized.shape[0] / img.shape[0]

      detetcions_out = []
      detectionso = np.copy(detections)
      if len(detections) > 0:
        detections[:, 0] /= im_scalex
        detections[:, 2] /= im_scalex
        detections[:, 4] /= im_scalex
        detections[:, 6] /= im_scalex

        detections[:, 1] /= im_scaley
        detections[:, 3] /= im_scaley
        detections[:, 5] /= im_scaley
        detections[:, 7] /= im_scaley

      for bid, box in enumerate(detections):

        boxo = detectionso[bid]
        # score = boxo[8]
        boxr = boxo[0:8].reshape(-1, 2)
        # box_area = area(boxr.reshape(8))

        # conf_factor = score / box_area

        center = (boxr[0, :] + boxr[1, :] + boxr[2, :] + boxr[3, :]) / 4

        dw = boxr[2, :] - boxr[1, :]
        dw2 = boxr[0, :] - boxr[3, :]
        dh = boxr[1, :] - boxr[0, :]
        dh2 = boxr[3, :] - boxr[2, :]

        h = math.sqrt(dh[0] * dh[0] + dh[1] * dh[1]) + 1
        h2 = math.sqrt(dh2[0] * dh2[0] + dh2[1] * dh2[1]) + 1
        h = (h + h2) / 2
        w = math.sqrt(dw[0] * dw[0] + dw[1] * dw[1])
        w2 = math.sqrt(dw2[0] * dw2[0] + dw2[1] * dw2[1])
        w = (w + w2) / 2

        if ((h - 1) / im_scaley) < min_height:
          continue

        input_W = im_data.size(3)
        input_H = im_data.size(2)
        target_h = norm_height

        scale = target_h / h
        target_gw = int(w * scale + target_h / 4)
        target_gw = max(8, int(round(target_gw / 8)) * 8)
        xc = center[0]
        yc = center[1]
        w2 = w
        h2 = h

        angle = math.atan2((boxr[2][1] - boxr[1][1]), boxr[2][0] - boxr[1][0])
        angle2 = math.atan2((boxr[3][1] - boxr[0][1]), boxr[3][0] - boxr[0][0])
        angle = (angle + angle2) / 2

        # show pooled image in image layer
        scalex = (w2 + h2 / 4) / input_W
        scaley = h2 / input_H

        th11 = scalex * math.cos(angle)
        th12 = -math.sin(angle) * scaley * input_H / input_W
        th13 = (2 * xc - input_W - 1) / (input_W - 1)

        th21 = math.sin(angle) * scalex * input_W / input_H
        th22 = scaley * math.cos(angle)
        th23 = (2 * yc - input_H - 1) / (input_H - 1)

        t = np.asarray([th11, th12, th13, th21, th22, th23], dtype=np.float)
        t = torch.from_numpy(t).type(torch.FloatTensor)
        t = t.to(device)
        theta = t.view(-1, 2, 3)

        grid = F.affine_grid(theta, torch.Size((1, 3, int(target_h), int(target_gw))))
        x = F.grid_sample(im_data, grid)

        features = net.forward_features(x)
        labels_pred = net.forward_ocr(features)

        ctc_f = labels_pred.data.cpu().numpy()
        ctc_f = ctc_f.swapaxes(1, 2)

        labels = ctc_f.argmax(2)

        conf = np.mean(np.exp(ctc_f.max(2)[labels > 3]))

        det_text, conf2, dec_s, word_splits = print_seq_ext(labels[0, :], codec)
        det_text = det_text.strip()

        if conf < 0.01 and len(det_text) == 3:
          continue

        if len(det_text) > 0:
          dtxt = det_text.strip()
          if len(dtxt) >= eval_text_length:
            # print('{0} - {1}'.format(dtxt, conf_factor))
            boxw = np.copy(boxr)
            boxw[:, 1] /= im_scaley
            boxw[:, 0] /= im_scalex
            boxw = boxw.reshape(8)

            detetcions_out.append([boxw, dtxt])

      pix = img

      # if args.evaluate == 1:
      tp, tp_e2e, gt_e2e, tp_e2e_ed1, detection_to_gt, pixx = evaluate_image(pix, detetcions_out, gt_rect, gt_txts,
                                                                             eval_text_length=eval_text_length)
      tp_all += tp
      gt_all += len(gt_txts)
      tp_e2e_all += tp_e2e
      gt_e2e_all += gt_e2e
      tp_e2e_ed1_all += tp_e2e_ed1
      detecitons_all += len(detetcions_out)

      if save:
        cv2.imwrite('{0}/{1}'.format(save_dir,base_nam), pixx)

      # print("	E2E recall tp_e2e:{0:.3f} / tp:{1:.3f} / e1:{2:.3f}, precision: {3:.3f}".format(
      #   tp_e2e_all / float(max(1, gt_e2e_all)),
      #   tp_all / float(max(1, gt_e2e_all)),
      #   tp_e2e_ed1_all / float(max(1, gt_e2e_all)),
      #   tp_all / float(max(1, detecitons_all))))

    note_file.write('Model{4}---E2E recall tp_e2e:{0:.3f} / tp:{1:.3f} / e1:{2:.3f}, precision: {3:.3f} \n'.format(
      tp_e2e_all / float(max(1, gt_e2e_all)),
      tp_all / float(max(1, gt_e2e_all)),
      tp_e2e_ed1_all / float(max(1, gt_e2e_all)),
      tp_all / float(max(1, detecitons_all)),name_model))

    note_file.close()
  return (
      tp_e2e_all / float(max(1, gt_e2e_all)),
      tp_all / float(max(1, gt_e2e_all)),
      tp_e2e_ed1_all / float(max(1, gt_e2e_all)),
      tp_all / float(max(1, detecitons_all)))
    # res_file.close()

def evaluate_e2e_crnn(root, net, norm_height = 48,name_model='E2E', normalize= False ,save = False, cuda= True,save_dir = 'eval'):
  #Decription : evaluate model E2E
  net = net.eval()
  # if cuda:
  #   print('Using cuda ...')
  #   net = net.to(device)

  images = glob.glob(os.path.join(root, '*.jpg'))
  png = glob.glob(os.path.join(root, '*.png'))
  images.extend(png)
  png = glob.glob(os.path.join(root, '*.JPG'))
  images.extend(png)

  imagess = np.asarray(images)

  tp_all = 0
  gt_all = 0
  tp_e2e_all = 0
  gt_e2e_all = 0
  tp_e2e_ed1_all = 0
  detecitons_all = 0
  eval_text_length = 2
  segm_thresh = 0.5
  min_height = 8
  idx =0

  if not os.path.exists(save_dir):
    os.mkdir(save_dir)

  note_path = os.path.join(save_dir, 'note_eval.txt')
  note_file = open(note_path, 'a')

  with torch.no_grad():

    index = np.arange(0, imagess.shape[0])
    # np.random.shuffle(index)
    for i in index:
      img_name = imagess[i]
      base_nam = os.path.basename(img_name)
      #
      # if args.evaluate == 1:
      res_gt = base_nam.replace(".jpg", '.txt').replace(".png", '.txt')
      res_gt = '{0}/gt_{1}'.format(root, res_gt)
      if not os.path.exists(res_gt):
        res_gt = base_nam.replace(".jpg", '.txt').replace("_", "")
        res_gt = '{0}/gt_{1}'.format(root, res_gt)
        if not os.path.exists(res_gt):
          print('missing! {0}'.format(res_gt))
          gt_rect, gt_txts = [], []
      # continue
      gt_rect, gt_txts = load_gt(res_gt)

      # print(img_name)
      img = cv2.imread(img_name)

      im_resized,_ = resize_image(img, max_size=1848 * 1024, scale_up=False)  # 1348*1024 #1848*1024
      images = np.asarray([im_resized], dtype=np.float)

      if normalize:
        images /= 128
        images -= 1
      im_data = net_utils.np_to_variable(images, is_cuda=cuda).permute(0, 3, 1, 2)

      [iou_pred, iou_pred1], rboxs, angle_pred, features = net(im_data)
      iou = iou_pred.data.cpu()[0].numpy()
      iou = iou.squeeze(0)

      rbox = rboxs[0].data.cpu()[0].numpy()
      rbox = rbox.swapaxes(0, 1)
      rbox = rbox.swapaxes(1, 2)

      detections = get_boxes(iou, rbox, angle_pred[0].data.cpu()[0].numpy(), segm_thresh)

      im_scalex = im_resized.shape[1] / img.shape[1]
      im_scaley = im_resized.shape[0] / img.shape[0]

      detetcions_out = []
      detectionso = np.copy(detections)
      if len(detections) > 0:
        detections[:, 0] /= im_scalex
        detections[:, 2] /= im_scalex
        detections[:, 4] /= im_scalex
        detections[:, 6] /= im_scalex

        detections[:, 1] /= im_scaley
        detections[:, 3] /= im_scaley
        detections[:, 5] /= im_scaley
        detections[:, 7] /= im_scaley

      for bid, box in enumerate(detections):

        boxo = detectionso[bid]
        # score = boxo[8]
        boxr = boxo[0:8].reshape(-1, 2)
        # box_area = area(boxr.reshape(8))

        # conf_factor = score / box_area

        center = (boxr[0, :] + boxr[1, :] + boxr[2, :] + boxr[3, :]) / 4

        dw = boxr[2, :] - boxr[1, :]
        dw2 = boxr[0, :] - boxr[3, :]
        dh = boxr[1, :] - boxr[0, :]
        dh2 = boxr[3, :] - boxr[2, :]

        h = math.sqrt(dh[0] * dh[0] + dh[1] * dh[1]) + 1
        h2 = math.sqrt(dh2[0] * dh2[0] + dh2[1] * dh2[1]) + 1
        h = (h + h2) / 2
        w = math.sqrt(dw[0] * dw[0] + dw[1] * dw[1])
        w2 = math.sqrt(dw2[0] * dw2[0] + dw2[1] * dw2[1])
        w = (w + w2) / 2

        if ((h - 1) / im_scaley) < min_height:
          continue

        input_W = im_data.size(3)
        input_H = im_data.size(2)
        target_h = norm_height

        scale = target_h / h
        target_gw = int(w * scale + target_h / 4)
        target_gw = max(8, int(round(target_gw / 8)) * 8)
        xc = center[0]
        yc = center[1]
        w2 = w
        h2 = h

        angle = math.atan2((boxr[2][1] - boxr[1][1]), boxr[2][0] - boxr[1][0])
        angle2 = math.atan2((boxr[3][1] - boxr[0][1]), boxr[3][0] - boxr[0][0])
        angle = (angle + angle2) / 2

        # show pooled image in image layer
        scalex = (w2 + h2 / 4) / input_W
        scaley = h2 / input_H

        th11 = scalex * math.cos(angle)
        th12 = -math.sin(angle) * scaley * input_H / input_W
        th13 = (2 * xc - input_W - 1) / (input_W - 1)

        th21 = math.sin(angle) * scalex * input_W / input_H
        th22 = scaley * math.cos(angle)
        th23 = (2 * yc - input_H - 1) / (input_H - 1)

        t = np.asarray([th11, th12, th13, th21, th22, th23], dtype=np.float)
        t = torch.from_numpy(t).type(torch.FloatTensor)
        t = t.to(device)
        theta = t.view(-1, 2, 3)

        grid = F.affine_grid(theta, torch.Size((1, 3, int(target_h), int(target_gw))))
        x = F.grid_sample(im_data, grid)

        # features = net.forward_features(x)
        # labels_pred = net.forward_ocr(features)
        labels_pred = net.forward_ocr(x)
        labels_pred = labels_pred.permute(1, 2, 0)

        ctc_f = labels_pred.data.cpu().numpy()
        ctc_f = ctc_f.swapaxes(1, 2)

        labels = ctc_f.argmax(2)

        conf = np.mean(np.exp(ctc_f.max(2)[labels > 3]))
        if conf < 0.02:
        	continue

        det_text, conf2, dec_s, word_splits = print_seq_ext(labels[0, :], codec)
        det_text = det_text.strip()

        if conf < 0.01 and len(det_text) == 3:
          continue

        if len(det_text) > 0:
          dtxt = det_text.strip()
          if len(dtxt) >= eval_text_length:
            # print('{0} - {1}'.format(dtxt, conf_factor))
            boxw = np.copy(boxr)
            boxw[:, 1] /= im_scaley
            boxw[:, 0] /= im_scalex
            boxw = boxw.reshape(8)

            detetcions_out.append([boxw, dtxt])

      pix = img

      # if args.evaluate == 1:
      tp, tp_e2e, gt_e2e, tp_e2e_ed1, detection_to_gt, pixx = evaluate_image(pix, detetcions_out, gt_rect, gt_txts,
                                                                             eval_text_length=eval_text_length)
      tp_all += tp
      gt_all += len(gt_txts)
      tp_e2e_all += tp_e2e
      gt_e2e_all += gt_e2e
      tp_e2e_ed1_all += tp_e2e_ed1
      detecitons_all += len(detetcions_out)
      # print(gt_all)
      if save:
        cv2.imwrite('{0}/{1}'.format(save_dir,base_nam), pixx)

      # print("	E2E recall tp_e2e:{0:.3f} / tp:{1:.3f} / e1:{2:.3f}, precision: {3:.3f}".format(
      #   tp_e2e_all / float(max(1, gt_e2e_all)),
      #   tp_all / float(max(1, gt_e2e_all)),
      #   tp_e2e_ed1_all / float(max(1, gt_e2e_all)),
      #   tp_all / float(max(1, detecitons_all))))

    note_file.write('Model{4}---E2E recall tp_e2e:{0:.3f} / tp:{1:.3f} / e1:{2:.3f}, precision: {3:.3f} \n'.format(
      tp_e2e_all / float(max(1, gt_e2e_all)),
      tp_all / float(max(1, gt_e2e_all)),
      tp_e2e_ed1_all / float(max(1, gt_e2e_all)),
      tp_all / float(max(1, detecitons_all)),name_model))

    note_file.close()
  return (
      tp_e2e_all / float(max(1, gt_e2e_all)),
      tp_all / float(max(1, gt_e2e_all)),
      tp_e2e_ed1_all / float(max(1, gt_e2e_all)),
      tp_all / float(max(1, detecitons_all)))
    # res_file.close()

reason: import numpy


text:
import numpy as np
from cv2 import cv2
import random
import huffman
import collections

def zigzag(input):
    horizontal = 0
    vertical = 0
    vertical_max = input.shape[0]
    horizontal_max = input.shape[1]
    i = 0
    output = np.zeros((vertical_max * horizontal_max))

    while ((vertical < vertical_max) and (horizontal < horizontal_max)):
        if ((horizontal + vertical) % 2) == 0:
            if (vertical == 0):
                output[i] = input[vertical, horizontal]
                if (horizontal == horizontal_max):
                    vertical = vertical + 1
                else:
                    horizontal = horizontal + 1
                i = i + 1

            elif ((horizontal == horizontal_max - 1) and (vertical < vertical_max)):
            	output[i] = input[vertical, horizontal]
            	vertical = vertical + 1
            	i = i + 1

            elif ((vertical > 0) and (horizontal < horizontal_max - 1)):
            	output[i] = input[vertical, horizontal]
            	vertical = vertical - 1
            	horizontal = horizontal + 1
            	i = i + 1

        else:
        	if ((vertical == vertical_max - 1) and (horizontal <= horizontal_max - 1)):
        		output[i] = input[vertical, horizontal]
        		horizontal = horizontal + 1
        		i = i + 1

        	elif (horizontal == 0):
        		output[i] = input[vertical, horizontal]
        		if (vertical == vertical_max - 1):
        			horizontal = horizontal + 1
        		else:
        			vertical = vertical + 1
        		i = i + 1

        	elif ((vertical < vertical_max - 1) and (horizontal > 0)):
        		output[i] = input[vertical, horizontal]
        		vertical = vertical + 1
        		horizontal = horizontal - 1
        		i = i + 1

        if ((vertical == vertical_max - 1) and (horizontal == horizontal_max - 1)):
        	output[i] = input[vertical, horizontal]
        	break
    return output

def huffman_encoding(macroblock,output):
    zigzaged = zigzag(macroblock)
    codes = huffman.codebook(collections.Counter(zigzaged).items())
    for i in zigzaged:
        output.append(codes.get(i))

A = []
count = 0
while count <= 3:
    if count == 0:
        print("\nGive ID of students in a team (min:1 and max:3 students per team)")
        count += 1
    if count == 1:
        student1 = input("\nID of first student: ")
        if len(student1) != 5 and not student1.isnumeric():
            print("ID must contain 5 digits")
            continue
        else:
            selection = input("\nDo you want to stop? (y/n): ").lower()
            if selection in ["y", "n"]:
                A.append(student1)
                if(selection == 'y'):
                    break
            else:
                print("\nInvalid input.")
                continue
            count += 1
    if count == 2:
        student2 = input("\nID of second student: ")
        if len(student2) != 5 and not student2.isnumeric():
            print("ID must contain 5 digits")
            continue
        else:
            selection = input("\nDo you want to stop? (y/n): ").lower()
            if selection in ["y", "n"]:
                A.append(student2)
                if(selection == 'y'):
                    break
            else:
                print("\nInvalid input.")
                continue
            count += 1
    if count == 3:
        student3 = input("\nID of third student: ")
        if len(student3) != 5 and not student2.isnumeric():
            print("ID must contain 5 digits")
            continue
        else:
            A.append(student3)
            count += 1
print("\nStudents' ID for this group: " + str(A))

for i in range(len(A)):
    change_index = random.randint(0,4)
    A[i] = A[i][:change_index] + "5" + A[i][change_index+1:]
print("\nStudents' ID for this group after the change of one digit with the number 5: " + str(A))

logos_simpiesis = []
for f in range(100):
    imageA = np.zeros([104, 200])
    for i in range(104):
        for j in range(40):
            rnd = random.randint(0,len(A)-1)
            for a in range(5):
                imageA[i, j*5 + a] = A[rnd][a]

    imageB = np.zeros([104, 200], dtype=np.float32)

    quantization_table = np.array([[16, 11, 10, 16, 24, 40, 51, 61],
                                [12, 12, 14, 19, 26, 58, 60, 55],
                                [14, 13, 16, 24, 40, 57, 69, 56],
                                [14, 17, 22, 29, 51, 87, 80, 62],
                                [18, 22, 37, 56, 68, 109, 103, 77],
                                [24, 35, 55, 64, 81, 104, 113, 92],
                                [49, 64, 78, 87, 103, 121, 120, 101],
                                [72, 92, 95, 98, 112, 100, 103, 99]])

    imageA_after_huffman = []
    for i in range(0,104,8):
        for j in range(0,200,8):
            huffman_encoding(imageA[i:(i+8),j:(j+8)],imageA_after_huffman)

    imageB_after_huffman = []
    for i in range(0,104,8):
        for j in range(0,200,8):
            imageB[i:(i+8),j:(j+8)] = cv2.dct(imageA[i:(i+8),j:(j+8)])
            imageB[i:(i+8),j:(j+8)] = np.ceil(np.divide(imageB[i:(i+8),j:(j+8)], quantization_table))
            #imageC[i:(i+8),j:(j+8)] = np.multiply(quantization_table, imageB[i:(i+8),j:(j+8)])
            #imageC[i:(i+8),j:(j+8)] = np.round(cv2.idct(imageC[i:(i+8),j:(j+8)]))
            huffman_encoding(imageB[i:(i+8),j:(j+8)],imageB_after_huffman)

    imageA_bits = len(''.join(imageA_after_huffman))
    imageB_bits = len(''.join(imageB_after_huffman))
    logos_simpiesis.append(imageA_bits/imageB_bits)

print("\nAverage compression ratio: " + str(sum(logos_simpiesis)/len(logos_simpiesis)))
input("\nPress any button to exit the program...")

max_stars_repo_path: Part B/part_b.py

max_issues_repo_path: Part B/part_b.py

max_forks_repo_path: Part B/part_b.py


text:
#!/usr/bin/env python
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

import unittest

import numpy as np
import onnx
from onnx import TensorProto, helper
from op_test_utils import TestDataFeeds, check_model_correctness, check_op_type_count

from onnxruntime.quantization import quantize_dynamic

class TestOpAttention(unittest.TestCase):
    def input_feeds(self, n, name2shape):
        input_data_list = []
        for i in range(n):
            inputs = {}
            for name, shape in name2shape.items():
                inputs.update({name: np.random.randint(-1, 2, shape).astype(np.float32)})
            input_data_list.extend([inputs])
        dr = TestDataFeeds(input_data_list)
        return dr

    def construct_model_attention_and_matmul(self, output_model_path):
        #      (input)
        #         |
        #     Attention
        #         |
        #       MatMul
        #         |
        #      (output)
        input_name = "input"
        output_name = "output"
        initializers = []

        def make_attention_node(input_name, weight_shape, weight_name, bias_shape, bias_name, output_name):
            weight_data = np.random.normal(0, 0.1, weight_shape).astype(np.float32)
            initializers.append(onnx.numpy_helper.from_array(weight_data, name=weight_name))

            bias_data = np.random.normal(0, 0.1, bias_shape).astype(np.float32)
            initializers.append(onnx.numpy_helper.from_array(bias_data, name=bias_name))

            return onnx.helper.make_node("Attention", [input_name, weight_name, bias_name], [output_name])

        def make_matmul_node(input_name, weight_shape, weight_name, output_name):
            weight_data = np.random.normal(0, 0.1, weight_shape).astype(np.float32)
            initializers.append(onnx.numpy_helper.from_array(weight_data, name=weight_name))

            return onnx.helper.make_node("MatMul", [input_name, weight_name], [output_name])

        # make attention node
        attention_output_name = "attention_output"
        attention_node = make_attention_node(
            input_name, [10, 30], "qkv.weight", [30], "qkv.bias", attention_output_name
        )
        attention_node.domain = "com.microsoft"
        attention_node.attribute.extend([helper.make_attribute("num_heads", 5)])

        # make matmul node
        matmul_node = make_matmul_node(attention_output_name, [10, 10], "matmul.weight", output_name)

        # make graph
        input_tensor = helper.make_tensor_value_info(input_name, TensorProto.FLOAT, [1, -1, 10])
        output_tensor = helper.make_tensor_value_info(output_name, TensorProto.FLOAT, [1, -1, 10])
        graph_name = "attention_test"
        graph = helper.make_graph(
            [attention_node, matmul_node],
            graph_name,
            [input_tensor],
            [output_tensor],
            initializer=initializers,
        )
        model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 13)])
        model.ir_version = onnx.IR_VERSION

        onnx.save(model, output_model_path)

    def dynamic_attention_quant_test(self, model_fp32_path, model_int8_path, per_channel, reduce_range):
        quantize_dynamic(
            model_fp32_path,
            model_int8_path,
            per_channel=per_channel,
            reduce_range=reduce_range,
        )
        quant_nodes = {"QAttention": 1, "MatMulInteger": 1}
        check_op_type_count(self, model_int8_path, **quant_nodes)
        check_model_correctness(
            self,
            model_fp32_path,
            model_int8_path,
            {"input": np.random.rand(1, 5, 10).astype(np.float32)},
        )

    def test_quantize_attention(self):
        np.random.seed(1)
        model_fp32_path = "attention_fp32.onnx"
        model_int8_path = "attention_fp32.quant.onnx"
        self.construct_model_attention_and_matmul(model_fp32_path)

        self.dynamic_attention_quant_test(model_fp32_path, model_int8_path, True, True)
        self.dynamic_attention_quant_test(model_fp32_path, model_int8_path, True, False)
        self.dynamic_attention_quant_test(model_fp32_path, model_int8_path, False, True)
        self.dynamic_attention_quant_test(model_fp32_path, model_int8_path, False, False)

if __name__ == "__main__":
    unittest.main()

max_stars_repo_name: jamill/onnxruntime

max_issues_repo_name: jamill/onnxruntime

max_forks_repo_name: jamill/onnxruntime


text:
# -*- coding: utf-8 -*-
# Part of the PsychoPy library
# Copyright (C) 2012-2020 iSolver Software Solutions (C) 2021 Open Science Tools Ltd.
# Distributed under the terms of the GNU General Public License (GPL).
from __future__ import division
from builtins import object
"""
Pixel to Visual Angle Calculation.

Uses "symmetric angles" formula.

Assumptions:
   1) unit origin == position 0.0, 0.0 == screen center
   2) Eye is orthogonal to origin of 2D plane
"""

import numpy as np
arctan = np.arctan2
rad2deg = np.rad2deg
hypot = np.hypot

class VisualAngleCalc(object):
    def __init__(self, display_size_mm, display_res_pix, eye_distance_mm=None):
        """Used to store calibrated surface information and eye to screen
        distance so that pixel positions can be converted to visual degree
        positions.

        Note: The information for display_size_mm,display_res_pix, and default
        eye_distance_mm could all be read automatically when opening a ioDataStore
        file. This automation should be implemented in a future release.

        """
        self._display_width = display_size_mm[0]
        self._display_height = display_size_mm[1]
        self._display_x_resolution = display_res_pix[0]
        self._display_y_resolution = display_res_pix[1]
        self._eye_distance_mm = eye_distance_mm
        self.mmpp_x = self._display_width / self._display_x_resolution
        self.mmpp_y = self._display_height / self._display_y_resolution

    def pix2deg(self, pixel_x, pixel_y=None, eye_distance_mm=None):
        """
        Stimulus positions (pixel_x,pixel_y) are defined in x and y pixel units,
        with the origin (0,0) being at the **center** of the display, as to match
        the PsychoPy pix unit coord type.

        The pix2deg method is vectorized, meaning that is will perform the
        pixel to angle calculations on all elements of the provided pixel
        position numpy arrays in one numpy call.

        The conversion process can use either a fixed eye to calibration
        plane distance, or a numpy array of eye distances passed as
        eye_distance_mm. In this case the eye distance array must be the same
        length as pixel_x, pixel_y arrays.
        """
        eye_dist_mm = self._eye_distance_mm
        if eye_distance_mm is not None:
            eye_dist_mm = eye_distance_mm

        x_mm = self.mmpp_x * pixel_x
        y_mm = self.mmpp_y * pixel_y

        Ah = arctan(x_mm, hypot(eye_dist_mm, y_mm))
        Av = arctan(y_mm, hypot(eye_dist_mm, x_mm))

        return rad2deg(Ah), rad2deg(Av)

###############################################################################

def generatedPointGrid(pixel_width, pixel_height, width_scalar=1.0,
                       height_scalar=1.0, horiz_points=5, vert_points=5):
    """Generate a set of points in a NxM grid.

    Useful for creating calibration target positions, etc.

    """
    swidth = pixel_width * width_scalar
    sheight = pixel_height * height_scalar
    # center 0 on screen center
    x, y = np.meshgrid(np.linspace(-swidth / 2.0, swidth / 2.0, horiz_points),
                       np.linspace(-sheight / 2.0, sheight / 2.0, vert_points))
    points = np.column_stack((x.flatten(), y.flatten()))
    return points

# Test it

if __name__ == '__main__':

    # physical size (w,h) of monitor in mm
    dsize = (600.0, 330.0)
    # pixel resolution of monitor
    dres = (1920.0, 1080.0)
    # eye distance to monitor in mm:
    edist = 550.0

    # create converter class. Can be reused event if eye distance changes
    # from call to call. Also should support calc with vectorized inputs.
    vacalc = VisualAngleCalc(dsize, dres, edist)

    pix_20x20 = generatedPointGrid(pixel_width=1900, pixel_height=1060,
                                   horiz_points=15, vert_points=15)

    x_pos = pix_20x20[:, 0]
    y_pos = pix_20x20[:, 1]
    deg_20x20 = vacalc.pix2deg(x_pos, y_pos)

    # Plot the 2D target array in pixel and in deg corord units.
    from matplotlib import pyplot

    fig = pyplot.figure()
    fig.suptitle(
        'Pixel to Visual Angle (Eye Dist: %.1f cm, %0.3fx%0.3f mm/pixel' %
        (edist / 10, vacalc.mmpp_x, vacalc.mmpp_y))
    ax1 = fig.add_subplot(211)
    ax1.plot(x_pos, y_pos, '+g')
    ax1.grid(True)
    ax1.set_xlabel('Horizontal Pixel Pos')
    ax1.set_ylabel('Vertical Pixel Pos')

    ax2 = fig.add_subplot(212)
    ax2.plot(deg_20x20[0], deg_20x20[1], '+b')
    ax2.grid(True)
    ax2.set_xlabel('Horizontal Visual Angle')
    ax2.set_ylabel('Vertical Visual Angle')

    pyplot.show()

max_stars_repo_name: mintzer/pupillometry-rf-back

max_issues_repo_name: mintzer/pupillometry-rf-back

max_forks_repo_name: mintzer/pupillometry-rf-back