text:
import util
import numpy as np
import cv2
from matplotlib import pyplot as plt

path = util.argv[1]
img = cv2.imread(path ,0)
# Initiate STAR detector
orb = cv2.ORB_create()
# find the keypoints with ORB
kp = orb.detect(img,None)
# compute the descriptors with ORBkp,
des = orb.compute(img, kp)
# draw only keypoints location,not size and orientation
img2 = cv2.drawKeypoints(img,kp,color=(0,255,0), outImage = img.copy(), flags=0)
plt.imshow(img2)
plt.show()

ext: py

reason: import numpy


text:
__all__ = ["shapeCylinder"]

import numpy as np
from .utils import check_target
from LevelSetPy.Utilities.matlab_utils import *
from .utils import check_target

def shapeCylinder(grid, axis_align=[], center=None, radius=1):
    """
     shapeCylinder: implicit surface function for a cylinder.

       data = shapeCylinder(grid, axis_align, center, radius)

     Creates an implicit surface function (actually signed distance) for a
       coordinate axis aligned cylinder whose axis runs parallel to the
       coordinate dimensions specified in axis_align.

     Can be used to create:
       Intervals, circles and spheres (if axis_align is empty).
       Slabs (if axis_align contains all dimensions except one).

     Parameters:
     ==========

       grid: Grid structure (see processGrid.py for details).

       axis_align: Vector specifying indices of coordinate axes with which the
       cylinder is aligned.  Defaults to the empty vector (eg: the cylinder is
       actually a sphere).

       center: Vector specifying a point at the center of the cylinder.
       Entries in the ignored dimensions are ignored.  May be a scalar, in
       which case the scalar is multiplied by a vector of ones of the
       appropriate length.  Defaults to 0 (eg centered at the origin).

       radius: Scalar specifying the radius of the cylinder.  Defaults to 1.

     Output:
     =======
       data: Output data array (of size grid.size) containing the implicit
       surface function.

     Translated August 2, 2021 | Lekan Molu
    ---------------------------------------------------------------------------
     Default parameter values.
    """

    if not np.any(center):
        center = zeros(grid.dim, 1)
    elif(numel(center) == 1):
        center = center * ones(grid.dim, 1, dtype=np.float64)

    #---------------------------------------------------------------------------
    # Signed distance function calculation.
    data = np.zeros((grid.shape))
    for i in range(grid.dim):
        if(i != axis_align):
            data += (grid.xs[i] - center[i])**2
    data = np.sqrt(data) - radius

    #---------------------------------------------------------------------------
    # Warn the user if there is no sign change on the grid
    #  (ie there will be no implicit surface to visualize).
    check_target(data)
    return data

max_stars_repo_name: robotsorcerer/LevelSetPy

max_issues_repo_name: robotsorcerer/LevelSetPy

max_forks_repo_name: robotsorcerer/LevelSetPy


text:
# Name: Chenying Wang
# Email: chenying.wang@usc.edu
# USC ID: ****-****-**
# Date: Friday, April 3, 2020

import numpy as np

def nhwc2nchw(tensor):
    return np.transpose(tensor, [0, 3, 1, 2])

def nchw2nhwc(tensor):
    return np.transpose(tensor, [0, 2, 3, 1])

max_stars_repo_name: chenying-wang/usc-ee-coursework-public

max_issues_repo_name: chenying-wang/usc-ee-coursework-public

max_forks_repo_name: chenying-wang/usc-ee-coursework-public


text:
import skimage
import numpy as np
from typing import Sequence, Union
from itertools import product

def unfold(x: np.array, filter_size: Union[int, np.array] = 3):
    feature_size, *spatial = x.shape
    if isinstance(filter_size, int):
        filter_size = np.array((filter_size,) * len(spatial))

    unfolded = np.zeros((feature_size, *filter_size, *spatial))

    def get_spans(shift):
        if shift > 0:
            source_span = slice(0, -shift)
            shifted_span = slice(shift, None)
        elif shift < 0:
            source_span = slice(-shift, None)
            shifted_span = slice(0, shift)
        else:
            shifted_span = source_span = slice(0, None)
        return source_span, shifted_span

    start_span_coord = filter_size // 2
    stop_span_coord = filter_size - start_span_coord - 1
    shift_boundaries = [
        np.arange(-start_coord, stop_coord + 1)
        for start_coord, stop_coord in zip(start_span_coord, stop_span_coord)
    ]

    for shifts in product(*shift_boundaries):
        cur_source_span, cur_shifted_span = zip(*map(get_spans, shifts))
        current_slice = (...,) + tuple(shifts + start_span_coord) + cur_source_span
        unfolded[current_slice] = x[(...,) + cur_shifted_span]

    return unfolded

def create_spatial_feats(shape: Sequence[int], filter_size: Union[int, np.array] = 3, feature_size: int = 2):
    if isinstance(filter_size, int):
        filter_size = np.array((filter_size,) * len(shape))

    start_span_coord = filter_size // 2
    stop_span_coord = filter_size - start_span_coord - 1
    shift_boundaries = [
        np.arange(-start_coord, stop_coord + 1)
        for start_coord, stop_coord in zip(start_span_coord, stop_span_coord)
    ]

    holder = np.zeros((feature_size,) + tuple(filter_size) + shape)
    for shift in product(*shift_boundaries):
        current_slice = shift + start_span_coord
        shift = np.reshape(shift, (feature_size,) + (1,) * 2 * len(filter_size))

        slices = (slice(None),) + tuple([slice(x, x + 1) for x in current_slice])
        holder[slices] = shift
        if shift.any():
            holder[slices] /= np.linalg.norm(shift)

    return holder

def preprocess_image(image: np.array):
    if len(image.shape) == 2:
        brightness = image
        image = np.expand_dims(image, 0)
    elif len(image.shape) == 3 and image.shape[2] == 3:
        brightness = skimage.color.rgb2hsv(image)[:, :, 2]
        image = np.transpose(image, (2, 0, 1))
    else:
        raise RuntimeError
    return image, brightness

def flatten_first_dims(x, n_dims=2):
    shape = x.shape
    return np.reshape(x, ((np.product(shape[:n_dims]),) + shape[n_dims:]))

def eval_path(image, reference_image):
    if image.shape != reference_image.shape:
        raise ValueError(f"Wrong image and reference image shapes! Image shape: {image.shape}, reference image shape: {reference_image.shape}")

    x_size, y_size, _ = image.shape
    hit = 0
    miss = 0
    for x in range(x_size):
        for y in range(y_size):
            if image[x, y, 0] != reference_image[x, y, 0]:
                miss += 1
            elif image[x, y, 1] != reference_image[x, y, 1]:
                miss += 1
            elif image[x, y, 2] != reference_image[x, y, 2]:
                miss += 1
            else:
                hit += 1
    score = miss/(hit + miss)
    return score, hit, miss

max_stars_repo_name: roaringdeer/intelligent-scissors

max_issues_repo_name: roaringdeer/intelligent-scissors

max_forks_repo_name: roaringdeer/intelligent-scissors


text:
import argparse
import csv

import matplotlib.pyplot as plt
import numpy as np

# parser
parser = argparse.ArgumentParser('Plot GPU use')
parser.add_argument('csv_path', type=str)
args = parser.parse_args()

with open(args.csv_path, 'r') as csvfile:
  plots = csv.reader(csvfile, delimiter=',')
  y = [[int(r) for r in row] for row in plots]
  x = range(len(y))  # number of iterations
  num_gpus = len(y[0])  # number of gpus

# plotting gpu use over time
plt.plot(x, y)
plt.legend(['gpu' + str(n) for n in range(num_gpus)])

plt.xlabel('time')
plt.ylabel('gpu use (%)')

plt.show()

# plotting average gpu use
avg_y = np.mean(np.asarray(y), axis=0)
y_pos = np.arange(num_gpus)
plt.bar(y_pos, height=avg_y)
plt.xticks(y_pos, ['gpu' + str(n) for n in range(num_gpus)])

plt.ylabel('Average gpu use (%)')

plt.show()


text:
"""
 Copyright (c) 2019 Intel Corporation

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import unittest

import numpy as np

from extensions.ops.quantize import QuantizeOp, broadcastable
from mo.graph.graph import Node
from mo.utils.unittest.graph import build_graph

class TestBroadcastable(unittest.TestCase):
    def test_matching(self):
        self.assertTrue(broadcastable([1, 2, 3], [1, 2, 3]))

    def test_incomplete(self):
        self.assertTrue(broadcastable([1, 1, 1], [1, 2, 3]))
        self.assertTrue(broadcastable([2, 3], [1, 2, 3]))
        self.assertTrue(broadcastable([1, 3], [1, 2, 3]))
        self.assertTrue(broadcastable([1, 1], [1, 2, 3]))
        self.assertTrue(broadcastable([], [1, 2, 3]))
        self.assertTrue(broadcastable([1], [1, 2, 3]))

    def test_reverse_incomplete(self):
        self.assertFalse(broadcastable([1, 2, 3], [1, 1, 1]))
        self.assertFalse(broadcastable([1, 2, 3], [2, 3]))
        self.assertFalse(broadcastable([1, 2, 3], [1, 3]))
        self.assertFalse(broadcastable([1, 2, 3], [1, 1]))
        self.assertFalse(broadcastable( [1, 2, 3], []))
        self.assertFalse(broadcastable([1, 2, 3], [1]))

    def test_invalid(self):
        self.assertFalse(broadcastable([3, 2, 1], [1, 2, 3]))
        self.assertFalse(broadcastable([5], [6]))
        self.assertFalse(broadcastable([5], [1]))

nodes_attributes = {'node_in_1': {'op': 'Identity', 'kind': 'op'},
                    'node_in_2': {'op': 'Identity', 'kind': 'op'},
                    'node_in_3': {'op': 'Identity', 'kind': 'op'},
                    'node_in_4': {'op': 'Identity', 'kind': 'op'},
                    'node_in_5': {'op': 'Identity', 'kind': 'op'},
                    'quantize': {'op': 'Quantize', 'kind': 'op', 'levels': 2},
                    'node_out_1': {'op': 'Identity', 'kind': 'op'},
                    'op_output': {'kind': 'op', 'op': 'OpOutput'}
                    }

class TestQuantizeOp(unittest.TestCase):
    def test_shape_only(self):
        graph = build_graph(nodes_attributes,
                            [('node_in_1', 'quantize'),
                             ('node_in_2', 'quantize'),
                             ('node_in_3', 'quantize'),
                             ('node_in_4', 'quantize'),
                             ('node_in_5', 'quantize'),
                             ('quantize', 'node_out_1'),
                             ('node_out_1', 'op_output')
                             ],
                            {'node_out_1': {'shape': None},
                             'node_in_1': {'shape': np.array([1, 3, 10, 20])},
                             'node_in_2': {'shape': np.array([1, 3, 10, 20])},
                             'node_in_3': {'shape': np.array([1, 3, 10, 20])},
                             'node_in_4': {'shape': np.array([1, 3, 10, 20])},
                             'node_in_5': {'shape': np.array([1, 3, 10, 20])},
                             })

        quantize_node = Node(graph, 'quantize')
        QuantizeOp.infer(quantize_node)
        quantize_shape = np.array([1, 3, 10, 20])
        res_shape = graph.node['node_out_1']['shape']
        for i in range(0, len(quantize_shape)):
            self.assertEqual(quantize_shape[i], res_shape[i])

    def test_shape_and_value(self):
        graph = build_graph(nodes_attributes,
                            [('node_in_1', 'quantize'),
                             ('node_in_2', 'quantize'),
                             ('node_in_3', 'quantize'),
                             ('node_in_4', 'quantize'),
                             ('node_in_5', 'quantize'),
                             ('quantize', 'node_out_1'),
                             ('node_out_1', 'op_output')
                             ],
                            {
                                'node_out_1': {
                                    'shape': None,
                                    'value': None,
                                },
                                'node_in_1': {
                                    'shape': np.array([4]),
                                    'value': np.array([5, 17, 0, 100], dtype=np.float32),
                                },
                                'node_in_2': {
                                    'shape': np.array([4]),
                                    'value': np.array([0, 12, 12, 12], dtype=np.float32),
                                },
                                'node_in_3': {
                                    'shape': np.array([4]),
                                    'value': np.array([10, 20, 20, 20], dtype=np.float32),
                                },
                                'node_in_4': {
                                    'shape': np.array([4]),
                                    'value': np.array([0, 0, 0, 0], dtype=np.float32),
                                },
                                'node_in_5': {
                                    'shape': np.array([4]),
                                    'value': np.array([1, 1, 1, 1], dtype=np.float32),
                                },
                            })

        exp_node = Node(graph, 'quantize')
        QuantizeOp.infer(exp_node)
        quantize_shape = np.array([4])
        quantize_value = np.array([0, 1, 0, 1], dtype=np.float32)
        res_shape = graph.node['node_out_1']['shape']
        res_value = graph.node['node_out_1']['value']
        for i in range(0, len(quantize_shape)):
            self.assertEqual(quantize_shape[i], res_shape[i])
        for i in range(0, len(quantize_value)):
            self.assertAlmostEqual(quantize_value[i], res_value[i], places=6)

max_stars_repo_name: shinh/dldt


text:
import copy
import logging
import sys
import time
import os
import six
import pickle
import json
import numpy as np
import pdb
from tqdm import tqdm
import torch
import torch.utils.data as Data
from torch.autograd import Variable

class Dataset(Data.Dataset):
    def __init__(self, data_info):
        self.dial_id = data_info['dial_id']
        self.turn_id = data_info['turn_id']
        self.history = data_info['history']
        self.in_txt = data_info['in_txt']
        self.prev_st = data_info['prev_st']
        self.curr_st = data_info['curr_st']
        self.out_st = data_info['out_st']
        self.out_ptr = data_info['out_ptr']
        self.out_txt_in = data_info['out_txt_in']
        self.out_txt_out = data_info['out_txt_out']
        if 'act' in data_info:
            self.out_act = data_info['act']
        else:
            self.out_act = None
        self.num_total_seqs = len(data_info['dial_id'])

    def __getitem__(self, index):
        item_info = {
            'dial_id':self.dial_id[index],
            'turn_id': self.turn_id[index],
            'history': self.history[index],
            'in_txt': self.in_txt[index],
            'prev_st': self.prev_st[index],
            'curr_st': self.curr_st[index],
            'out_st': self.out_st[index],
            'out_ptr': self.out_ptr[index],
            'out_txt_in': self.out_txt_in[index],
            'out_txt_out': self.out_txt_out[index],
            }
        if self.out_act is not None and len(self.out_act)>0:
            item_info['out_act'] = self.out_act[index]
        return item_info

    def __len__(self):
        return self.num_total_seqs

def subsequent_mask(size):
    "Mask out subsequent positions."
    attn_shape = (1, size, size)
    subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
    return torch.from_numpy(subsequent_mask) == 0

def make_std_mask(tgt, pad, sys_act=False):
    "Create a mask to hide padding and future words."
    tgt_mask = (tgt != pad).unsqueeze(-2)
    tgt_mask = tgt_mask & Variable(subsequent_mask(tgt.size(-1)).type_as(tgt_mask.data))
    if sys_act:
        pdb.set_trace()
    return tgt_mask

def prepare_domain_slot_mask(in_slots, in_domains, slots_idx):
    if len(in_domains)==0: return []
    batch_size = in_slots.shape[0]
    mask_len = in_slots.shape[1]*in_domains.shape[1]
    mask = torch.zeros(mask_len)
    count = 0
    for domain, idx in slots_idx.items():
        real_idx = [count*in_slots.shape[1]+i for i in idx]
        mask[real_idx] = 1
        count += 1
    return mask.unsqueeze(0).unsqueeze(0).expand(batch_size, 1, mask_len)

def add_sys_act_mask(out_mask):
    temp = torch.ones(out_mask.shape[0], out_mask.shape[1], 1)
    out = torch.cat([temp.cuda().int(), out_mask.int()], dim=-1)
    temp = torch.zeros(out.shape[0], 1, out.shape[-1])
    temp[:,:,0] = temp[:,:,0]+1
    out = torch.cat([temp.cuda().int(), out], dim=1)
    return out

class Batch:
    def __init__(self, in_domains, in_slots, h_batch, u_batch, in_state, in_curr_state,
                  out_state, out_ptr, res_in_batch, res_out_batch, ntokens_state, slots_idx,
                  req_idx, inf_idx, req_batch, inf_batch, dial_ids, out_act):
        self.dial_ids = dial_ids
        self.in_slots = in_slots
        self.in_domains = in_domains
        self.in_his = h_batch
        self.in_his_mask = (h_batch != 2).unsqueeze(-2)
        self.in_txt = u_batch
        self.in_txt_mask = (u_batch != 2).unsqueeze(-2)
        self.in_state = in_state
        self.in_state_mask = (in_state != 2).unsqueeze(-2)
        self.in_curr_state = in_curr_state
        self.in_curr_state_mask = (in_curr_state != 2).unsqueeze(-2)
        self.out_state = out_state
        self.out_ptr = out_ptr
        self.out_txt = res_in_batch
        self.out_mask = make_std_mask(res_in_batch, 2)
        self.out_txt_y = res_out_batch
        self.ntokens_res = (res_out_batch != 2).data.sum()
        self.ntokens_state = ntokens_state
        self.domain_slot_mask = prepare_domain_slot_mask(in_slots, in_domains, slots_idx)
        self.req_idx = req_idx
        self.inf_idx = inf_idx
        self.out_req_state = req_batch
        self.out_inf_state = inf_batch
        self.out_act = out_act

    def to_cuda(self):
        self.in_slots = self.in_slots.to('cuda', non_blocking=True)
        self.in_domains = self.in_domains.to('cuda', non_blocking=True)
        self.in_his = self.in_his.to('cuda', non_blocking=True)
        self.in_his_mask = self.in_his_mask.to('cuda', non_blocking=True)
        self.in_txt = self.in_txt.to('cuda', non_blocking=True)
        self.in_txt_mask = self.in_txt_mask.to('cuda', non_blocking=True)
        self.in_state = self.in_state.to('cuda', non_blocking=True)
        self.in_state_mask = self.in_state_mask.to('cuda', non_blocking=True)
        self.in_curr_state = self.in_curr_state.to('cuda', non_blocking=True)
        self.in_curr_state_mask = self.in_curr_state_mask.to('cuda', non_blocking=True)
        if self.out_state is not None:
            out_state = {}
            for domain, domain_states in self.out_state.items():
                out_state[domain] = []
                for state in domain_states:
                    out_state[domain].append(state.to('cuda', non_blocking=True))
            self.out_state = out_state
        if self.out_req_state is not None:
            self.out_req_state = self.out_req_state.contiguous().to('cuda', non_blocking=True)
            self.out_inf_state = self.out_inf_state.contiguous().to('cuda', non_blocking=True)
        self.out_ptr = self.out_ptr.to('cuda', non_blocking=True)
        self.out_txt = self.out_txt.to('cuda', non_blocking=True)
        self.out_txt_y = self.out_txt_y.to('cuda', non_blocking=True)
        self.out_mask = self.out_mask.to('cuda', non_blocking=True)
        if len(self.in_domains)>0:
            self.domain_slot_mask = self.domain_slot_mask.contiguous().to('cuda', non_blocking=True)
        if self.out_act is not None:
            self.out_act = self.out_act.contiguous().to('cuda', non_blocking=True)

def collate_fn(data, vocab, slots, args):
    def pad_seq(seqs, pad_token, max_length=-1):
        if max_length<0:
            max_length = max([len(s) for s in seqs])
        output = []
        for seq in seqs:
            result = np.ones(max_length, dtype=np.int)*pad_token
            result[:len(seq)] = seq
            output.append(result)
        return output

    def prepare_data(seqs):
        return torch.from_numpy(np.asarray(seqs)).long()

    def prepare_dst(dst_batch, slots, args):
        if args.share_dst_gen:
            domain_slot_names = slots['domain_slots']
            inf_idx = []
            req_idx = []
            nb_slots = len(slots['slots'])
            if args.domain_flow:
                count = 0
                slots_idx = slots['slots_idx']
                for domain, slot_indices in slots_idx.items():
                    for name_index, index in enumerate(slot_indices):
                        slot_name = domain_slot_names[domain][name_index]
                        if 'request' in slot_name or 'booked' in slot_name or 'is_active' in slot_name:
                            req_idx.append(count*nb_slots+index)
                        else:
                            inf_idx.append(count*nb_slots+index)
                    count += 1
            else:
                slots_idx = slots['merged_slots_idx']
                for domain, slot_indices in slots_idx.items():
                    for name_index, index in enumerate(slot_indices):
                        slot_name = domain_slot_names[domain][name_index]
                        if 'request' in slot_name or 'booked' in slot_name or 'is_active' in slot_name:
                            req_idx.append(index)
                        else:
                            inf_idx.append(index)
            inf_batch = []
            req_batch = []
            num_tokens = 0
            max_inf_len = 0
            for domain, domain_slots in dst_batch.items():
                for idx, slot_values in enumerate(domain_slots):
                    slot_name = domain_slot_names[domain][idx]
                    if 'request' in slot_name or 'booked' in slot_name or 'is_active' in slot_name:
                        req_batch.append(slot_values)
                    else:
                        inf_batch.append(slot_values)
                        lens = [len(i) for i in slot_values]
                        if max(lens)>max_inf_len: max_inf_len=max(lens)
                        num_tokens += sum(lens)
            req_batch = prepare_data(np.asarray(req_batch).transpose())
            for idx, inf in enumerate(inf_batch):
                inf_batch[idx] = prepare_data(pad_seq(inf, 2, max_length=max_inf_len))
            inf_batch = torch.cat([i.unsqueeze(0) for i in inf_batch], dim=0).permute(1, 0, 2)
            return req_idx, inf_idx, req_batch, inf_batch, num_tokens
        else:
            num_tokens = {}
            domain_slot_names = slots['domain_slots']
            for domain, domain_slots in dst_batch.items():
                for idx, slot_values in enumerate(domain_slots):
                    slot_name = domain_slot_names[domain][idx]
                    if 'request' in slot_name or 'booked' in slot_name or 'is_active' in slot_name:
                        dst_batch[domain][idx] = prepare_data(slot_values)
                    else:
                        lens = [len(i) for i in slot_values]
                        if domain not in num_tokens:
                            num_tokens[domain] = {}
                        num_tokens[domain][slot_name] = sum(lens)
                        dst_batch[domain][idx] = prepare_data(pad_seq(slot_values, 2))
            return dst_batch, num_tokens

    def prepare_slots(slots, n_seqs, vocab, domain_flow):
        if domain_flow:
            slots_ls = slots['slots']
            return [[vocab['in+domain+bs']['word2idx']['<{}>'.format(s)] for s in slots_ls] for i in range(n_seqs)]
        else:
            slots_ls = slots['merged_slots']
            return [[[vocab['in+domain+bs']['word2idx'][s[0]],vocab['in+domain+bs']['word2idx'][s[1]]]
                for s in slots_ls] for i in range(n_seqs)]

    def prepare_domains(slots, n_seqs, vocab, domain_flow):
        if domain_flow:
            domains = slots['domain_slots'].keys()
            return [[vocab['in+domain+bs']['word2idx']['<{}>'.format(d)] for d in domains] for i in range(n_seqs)]
        else:
            return []

    def prepare_act(acts, nb_labels):
        out = []
        for act in acts:
            labels = torch.tensor(act).unsqueeze(0)
            out.append(torch.zeros(labels.size(0), nb_labels).scatter_(1, labels, 1.))
        out = torch.cat(out, dim=0)
        return out

    item_info = {}
    for key in data[0].keys():
        item_info[key] = [d[key] for d in data]
    h_batch = prepare_data(pad_seq(item_info['history'], 2))
    u_batch = prepare_data(pad_seq(item_info['in_txt'], 2))
    prev_st_batch = prepare_data(pad_seq(item_info['prev_st'], 2))
    curr_st_batch = prepare_data(pad_seq(item_info['curr_st'], 2))
    res_in_batch = prepare_data(pad_seq(item_info['out_txt_in'], 2))
    res_out_batch = prepare_data(pad_seq(item_info['out_txt_out'], 2))
    out_ptr_batch = prepare_data(pad_seq(item_info['out_ptr'], 2))
    out_dst_batch = {}
    encoded_states = item_info['out_st']
    for encoded_state in encoded_states[:1]:
        for domain, domain_states in encoded_state.items():
            out_dst_batch[domain] = []
            for state in domain_states:
                out_dst_batch[domain].append([])
    for encoded_state in encoded_states:
        for domain, domain_states in encoded_state.items():
            for state_idx, domain_state in enumerate(domain_states):
                out_dst_batch[domain][state_idx].append(domain_state)
    dst_batch, req_idx, inf_idx, req_batch, inf_batch = None, None, None, None, None
    if args.share_dst_gen:
        req_idx, inf_idx, req_batch, inf_batch, dst_tokens = prepare_dst(out_dst_batch, slots, args)
    else:
        dst_batch, dst_tokens = prepare_dst(out_dst_batch, slots, args)
    n_seqs = len(res_in_batch)
    slots_batch = prepare_data(prepare_slots(slots, n_seqs, vocab, args.domain_flow))
    domains_batch = prepare_data(prepare_domains(slots, n_seqs, vocab, args.domain_flow))
    if hasattr(args, 'sys_act') and args.sys_act:
        act_batch = prepare_act(item_info['out_act'], len(vocab['act']['word2idx']))
    else:
        act_batch = None
    batch = Batch(domains_batch, slots_batch, h_batch, u_batch, prev_st_batch, curr_st_batch,
                  dst_batch, out_ptr_batch, res_in_batch, res_out_batch, dst_tokens, slots['slots_idx'],
                  req_idx, inf_idx, req_batch, inf_batch, item_info['dial_id'], act_batch)

    return batch

max_stars_repo_name: henryhungle/UniConv

max_issues_repo_name: henryhungle/UniConv

max_forks_repo_name: henryhungle/UniConv


text:
# -*- coding:utf-8 -*-
# -------------------------------------------------------------------------------
# Name:        Object bounding box label tool
# Purpose:     Label object bounding boxes for Detection
# Author:      HUST
# Created:     08/06/2018
#-------------------------------------------------------------------------------
from tkinter import messagebox, filedialog
from tkinter import *
from PIL import Image, ImageTk
import numpy as np
import xml.dom.minidom
import xml.dom
import glob
import os

# colors for the bboxes
COLORS = ['red', 'orange', 'cyan', 'green', 'blue', 'purple', 'pink', 'black', 'gray']
# classes name
CLASSES = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
           'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse',
           'motorbike', 'person', 'pottedplant', 'sheep', 'sofa',
           'train', 'tvmonitor']

# Some parameters for VOC label
_POSE = 'Unspecified'
_TRUNCATED = '0'
_DIFFICULT = '0'
_SEGMENTED = '0'

class LabelTool:
    def __init__(self, master):
        self.parent = master
        self.parent.title("LabelTool")
        self.w, self.h =  self.parent.maxsize()
        # self.parent.geometry('%dx%d' %(w, h))
        self.parent.state('zoomed')
        self.frame = Frame(self.parent)
        self.frame.pack(fill=BOTH, expand=1)

        # initialize mouse state
        self.STATE = {'click': 0, 'x': 0, 'y': 0}

        # initialize global state
        self.flag = 0
        self.imageList = []     # The list for images path.
        self.outDir = './Labels'    # The path of labelled annotations.
        self.cur = 0    # The index of images path.
        self.total = 0     # The total images.
        self.del_num = 0    # The number of deleted images.
        self.image_name = ''    # The name of precessing image.
        self.label_filename = ''    # The name of .xml.
        self.label_filename1 = ''   # The name of .txt, it's used to check annotations.
        self.tkimg = None   # Image displayed on canvas.
        self.img = None
        self.width = 0     # The size of origin image.
        self.height = 0    # The size of origin image.
        self.depth = 0     # The size of origin image.
        self.color_map = COLORS[0]   # The color of bounding boxes.
        self.class_name = None     # The class name of right-click.
        self.classes_name = []     # All classes of a image.
        self.bboxIdLine = []
        self.bboxIdList = []     # Save drawing handle.
        self.classIdList = []    # Save text handle.
        self.bboxId = None     # Drawing handle.
        self.bboxList = []     # Save all bounding boxes.
        self.hl = None      # Horizontal line.
        self.vl = None      # Vertical line.
        self.coordinate = []
        self.x1 = 0
        self.y1 = 0
        self.xx1 = 0
        self.x2 = 0
        self.yy1 = 0
        self.y2 = 0

        # dir entry & load
        radio_var = IntVar()
        self.label = Label(self.frame, text = 'Label Type:')
        self.label.place(x = 10, y = 10, width = 70, height = 16)
        self.rad = Radiobutton(self.frame, text = 'rectangle', variable = radio_var, value = 1, command = self.Rectangle)
        self.rad.place(x = 90, y = 10, width = 80, height = 16)
        self.rad1 = Radiobutton(self.frame, text = 'polygon', variable = radio_var, value = 2, command = self.Polygon)
        self.rad1.place(x = 180, y = 10, width = 80, height = 16)

        self.label1 = Label(self.frame, text = "Image Path:")
        self.label1.place(x = 10, y = 40, width = 71, height = 16)
        self.label2 = Entry(self.frame)
        self.label2.place(x = 90, y = 40, width = 255, height = 20)
        self.label3 = Label(self.frame, text = "Image Size:")
        self.label3.place(x = 10, y = 70, width = 71, height = 16)
        self.label4 = Label(self.frame, bg='white')
        self.label4.place(x = 90, y = 70, width = 171, height = 16)
        self.label5 = Label(self.frame, text = "Bounding Box:")
        self.label5.place(x = 3, y = 100, width = 101, height = 16)
        self.label6 = Label(self.frame, text =  "Doing:     /    ")
        self.label6.place(x = 120, y = 100 , width = 150, height = 16)
        self.label7 = Label(self.frame, bg = 'white', fg = 'red', anchor = W, text = "x: ")
        self.label7.place(x = 270, y = 120, width = 75, height = 20)
        self.label8 = Label(self.frame, bg = 'white', fg = 'red', anchor = W, text = "y: ")
        self.label8.place(x = 270, y = 145, width = 75, height = 20)

        self.button = Button(self.frame, text = "Load Image", state = 'disabled', command = self.Load_image)
        self.button.place(x = 270, y = 70, width = 75, height = 20)
        self.button1 = Button(self.frame, text = "Delete", state = 'disabled', command = self.delBBox)
        self.button1.place(x = 270, y = 190, width = 75, height = 20)
        self.button2 = Button(self.frame, text = "ClearAll", state = 'disabled', command = self.clearBBox)
        self.button2.place(x = 270, y = 220, width = 75, height = 20)
        self.button3 = Button(self.frame, text = "<< Prev", state = 'disabled', command = self.prevImage)
        self.button3.bind_all('w', self.prevImage)
        self.button3.bind_all('a', self.prevImage)
        self.button3.place(x = 10, y = 250, width = 65, height = 20)
        self.button4 = Button(self.frame, text = "Next >>", state = 'disabled', command = self.nextImage)
        self.button4.bind_all("s", self.nextImage)
        self.button4.bind_all("d", self.nextImage)
        self.button4.place(x = 90, y = 250, width = 65, height = 20)
        self.button5 = Button(self.frame, text ="Delete Image", state = 'disabled', command = self.Delete_image)
        self.button5.place(x = 170, y = 250, width = 90, height = 20)
        self.button6 = Button(self.frame, text = "Help", command = Help)
        self.button6.place(x = 275, y = 250, width = 65, height = 20)

        # main panel for labeling
        self.canvas_w = self.w - 370    # The size of canvas.
        self.canvas_h = self.h - 80
        self.mainPanel = Canvas(self.frame, bg = 'white')
        self.mainPanel.bind("<Button-1>", self.mouseClick)
        self.mainPanel.bind("<Button-3>", self.popupList)
        self.mainPanel.bind("<Button-2>", self.delete_line)
        self.mainPanel.bind("<Motion>", self.mouseMove)
        self.mainPanel.place(x = 360, y = 10, width = self.canvas_w, height = self.canvas_h)

        # showing bbox info & delete bbox
        self.listbox = Listbox(self.frame)
        self.listbox.place(x = 10, y = 120, width = 250, height = 120)

        # showing log
        self.label9 = Label(self.frame, text = "Print Log:")
        self.label9.place(x = 1, y = 280, width = 71, height = 20)
        self.messageList = Listbox(self.frame)
        self.messageList.place(x = 10, y = 300, width = 336, height = 300)

        # Right-click menu, display class name.
        self.contextMenu = Menu(self.frame)
        self.Classes = StringVar()
        for classes in CLASSES:
            self.contextMenu.add_radiobutton(label = classes, variable = self.Classes, command = self.clickMenu)

    def Rectangle(self):
        self.flag = 0
        self.button.config(state='active')

    def Polygon(self):
        self.flag = 1
        self.button.config(state='active')

    def Load_image(self):
        filename = filedialog.askdirectory()
        self.imageList = glob.glob(os.path.join(filename, '*.jpg'))
        if len(self.imageList) == 0:
            messagebox.showinfo('Information', 'No .JPEG images found in the specified dir!')
            return

        self.button1.config(state='active')
        self.button2.config(state='active')
        self.button3.config(state='active')
        self.button4.config(state='active')
        self.button5.config(state='active')
        self.cur = 1
        self.total = len(self.imageList)
        self.messageList.insert(END, "The number of images is %d " % self.total)

        if not os.path.exists(self.outDir):
            os.mkdir(self.outDir)
        if not os.path.exists('./label'):
            os.mkdir('./label')

        self.loadImage()

    def loadImage(self):
        # load image
        image_path = self.imageList[self.cur - 1]
        entry = StringVar()
        entry.set(image_path)
        self.label2.config(textvariable = entry)
        self.img = Image.open(image_path)
        size_img = np.shape(self.img)
        self.width = size_img[1]
        self.height = size_img[0]
        self.depth = size_img[2]
        self.img = self.img.resize((self.canvas_w, self.canvas_h), Image.ANTIALIAS)
        self.tkimg = ImageTk.PhotoImage(self.img)
        self.mainPanel.create_image(0, 0, image = self.tkimg, anchor=NW)
        self.label6.config(text = "Doing: %d/%d" %(self.cur, self.total))
        self.label4.config(text = '(%d, %d, %d)' % (self.width, self.height, self.depth))

        # load labels
        self.clearBBox()
        self.image_name = os.path.split(image_path)[-1].split('.')[0]
        label_name = self.image_name + '.xml'
        self.label_filename = os.path.join(self.outDir, label_name)
        label_name1 = self.image_name + '.txt'
        self.label_filename1 = os.path.join('./label', label_name1)
        if os.path.exists(self.label_filename1):
            with open(self.label_filename1) as f:
                for (ind, line) in enumerate(f):
                    if ind == 0:
                        continue
                    if self.flag == 0:
                        tmp = [t.strip() for t in line.split()]
                        self.bboxList.append(tuple([tmp[0], tmp[1], tmp[2], tmp[3]]))
                        self.classes_name.append(tmp[4])
                        x = float(tmp[0]) * self.canvas_w / self.width
                        y = float(tmp[1]) * self.canvas_h / self.height
                        xx = float(tmp[2]) * self.canvas_w / self.width
                        yy = float(tmp[3]) * self.canvas_h / self.height
                        color = COLORS[(len(self.bboxList) - 1) % len(COLORS)]
                        tmpId = self.mainPanel.create_rectangle(int(x), int(y), int(xx), int(yy), width = 2, outline = color)
                        classId = self.mainPanel.create_text(int(x) + 5, int(y) + 8, text = tmp[4], anchor = W, fill = color)
                        self.bboxIdList.append(tmpId)
                        self.classIdList.append(classId)
                        self.listbox.insert(END, '(%s, %s) -> (%s, %s) -> %s' % (tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]))
                        self.listbox.itemconfig(len(self.bboxIdList) - 1, fg = color)
                    if self.flag == 1:
                        tmp = [t.strip() for t in line.split()]
                        boxes = []
                        self.coordinate = []
                        self.bboxIdLine = []
                        for i in range(0, len(tmp)-1, 2):
                            self.coordinate.append((tmp[i], tmp[i+1]))
                            x = float(tmp[i]) * self.canvas_w / self.width
                            y = float(tmp[i+1]) * self.canvas_h / self.height
                            boxes.append((int(x), int(y)))
                        self.bboxList.append(self.coordinate)
                        self.classes_name.append(tmp[8])
                        color = COLORS[(len(self.bboxList) - 1) % len(COLORS)]
                        classId = self.mainPanel.create_text(boxes[0][0]+5, boxes[0][1]+8, text = tmp[8], anchor = W, fill = color)
                        for i in range(len(boxes)-1):
                            tmpId = self.mainPanel.create_line(boxes[i], boxes[i+1], width = 2, fill = color)
                            self.bboxIdLine.append(tmpId)
                        tmpId = self.mainPanel.create_line(boxes[3], boxes[0], width=2, fill=color)
                        self.bboxIdLine.append(tmpId)
                        self.bboxIdList.append(self.bboxIdLine)
                        self.classIdList.append(classId)
                        self.listbox.insert(END, '(%d,%d)->(%d,%d)->(%d,%d)->(%d,%d)->%s' % (boxes[0][0], boxes[0][1], boxes[1][0], boxes[1][1], boxes[2][0],
                                                                                boxes[2][1], boxes[3][0], boxes[3][1],self.class_name))
                        self.listbox.itemconfig(len(self.bboxIdList) - 1, fg = color)

    def saveImage(self):
        if self.classes_name:
            img_name = self.image_name + '.jpg'
            shape = [self.width, self.height, self.depth]
            doc = createXML(img_name, shape, self.classes_name, self.bboxList, self.flag)
            writeXMLFile(doc, self.label_filename)

            with open(self.label_filename1, 'w') as f:
                f.write('%d\n' % len(self.bboxList))
                if self.flag == 0:
                    for ind in range(len(self.classes_name)):
                        f.write(' '.join(map(str, self.bboxList[ind])) + ' ' + self.classes_name[ind] + '\n')

                if self.flag == 1:
                    for ind in range(len(self.classes_name)):
                        for box in self.bboxList[ind]:
                            f.write(str(box[0]) + ' ' + str(box[1]) + ' ')
                        f.write(self.classes_name[ind] + '\n')

            self.messageList.insert(END, "Image No. %d saved" % self.cur)
            if len(self.messageList.get(0,END)) > 16:
                self.messageList.delete(1)

    def mouseClick(self, event):
        if self.flag == 0:
            if self.STATE['click'] == 0:
                self.STATE['x'], self.STATE['y'] = event.x, event.y
            else:
                self.x1, x2 = min(self.STATE['x'], event.x), max(self.STATE['x'], event.x)
                self.y1, y2 = min(self.STATE['y'], event.y), max(self.STATE['y'], event.y)
                self.xx1 = int(float(self.x1) * self.width / self.canvas_w)
                self.x2 = int(float(x2) * self.width / self.canvas_w)
                self.yy1 = int(float(self.y1) * self.height / self.canvas_h)
                self.y2 = int(float(y2) * self.height / self.canvas_h)
                self.bboxList.append((self.xx1, self.yy1, self.x2, self.y2))
                self.bboxIdList.append(self.bboxId)
                self.bboxId = None
            self.STATE['click'] = 1 - self.STATE['click']

        if self.flag == 1:
            if self.STATE['click'] == 0:
                self.STATE['x'], self.STATE['y'] = event.x, event.y
                x1, x2 = event.x, event.y
                self.x1, self.y1 = event.x, event.y
            else:
                x1, x2 = event.x, event.y
            x1 = int(float(x1) * self.width / self.canvas_w)
            x2 = int(float(x2) * self.height / self.canvas_h)
            self.STATE['x'], self.STATE['y'] = event.x, event.y
            self.coordinate.append((x1, x2))
            if self.bboxId:
                self.bboxIdLine.append(self.bboxId)
            self.STATE['click'] += 1
            if self.STATE['click'] == 4:
                self.STATE['click'] = 0
                self.bboxList.append(self.coordinate)
                self.bboxId = self.mainPanel.create_line(self.STATE["x"], self.STATE['y'], self.x1, self.y1, width=2,
                                                         fill=self.color_map)
                self.bboxIdLine.append(self.bboxId)
                self.bboxIdList.append(self.bboxIdLine)
                self.coordinate = []
                self.bboxIdLine = []

            self.bboxId = None

    def mouseMove(self, event):
        self.color_map = COLORS[len(self.bboxIdList) % len(COLORS)]
        self.label7.config(text = 'x: %.2f' % event.x)
        self.label8.config(text = 'y: %.2f' % event.y)
        if self.tkimg:
            if self.hl:
                self.mainPanel.delete(self.hl)
            self.hl = self.mainPanel.create_line(0, event.y, self.tkimg.width(), event.y, width = 2)
            if self.vl:
                self.mainPanel.delete(self.vl)
            self.vl = self.mainPanel.create_line(event.x, 0, event.x, self.tkimg.height(), width = 2)
        if self.STATE['click']:
            if self.bboxId:
                self.mainPanel.delete(self.bboxId)
            if self.flag == 0:
                self.bboxId = self.mainPanel.create_rectangle(self.STATE['x'], self.STATE['y'], event.x, event.y, width = 2, outline = self.color_map)
            if self.flag == 1:
                self.bboxId = self.mainPanel.create_line(self.STATE["x"], self.STATE['y'], event.x, event.y, width = 2, fill = self.color_map)

    def popupList(self, event):
        self.contextMenu.post(event.x_root, event.y_root)

    def clickMenu(self):
        self.class_name = self.Classes.get()
        self.color_map = COLORS[(len(self.bboxIdList) - 1) % len(COLORS)]
        if self.flag == 0:
            self.listbox.insert(END, '(%d, %d) -> (%d, %d) -> %s' % (self.xx1, self.yy1, self.x2, self.y2, self.class_name))
        if self.flag == 1:
            num = len(self.bboxIdList)
            coor = self.bboxList[num - 1]
            self.listbox.insert(END, '(%d,%d)->(%d,%d)->(%d,%d)->(%d,%d)->%s' % (coor[0][0], coor[0][1], coor[1][0], coor[1][1],
                                coor[2][0], coor[2][1], coor[3][0], coor[3][1], self.class_name))
        self.listbox.itemconfig(len(self.bboxIdList) - 1, fg = self.color_map)
        classIdx = self.mainPanel.create_text(self.x1+5, self.y1+8, text = self.class_name, anchor = W, fill = self.color_map)
        self.classes_name.append(self.class_name)
        self.classIdList.append(classIdx)

    def delete_line(self, event):
        if self.bboxId:
            try:
                if self.flag == 0:
                    self.mainPanel.delete(self.bboxId)
                    self.bboxId = None
                    self.STATE['click'] = 0
            except:
                pass
        else:
            try:
                if self.flag == 0:
                    if len(self.bboxIdList) != len(self.classes_name):
                        boxId = self.bboxIdList.pop()
                        self.mainPanel.delete(boxId)
                        self.bboxList.pop()
                if self.flag == 1:
                    if len(self.bboxIdList) != len(self.classes_name):
                        boxId = self.bboxIdList.pop()
                        self.bboxList.pop()
                        for Id in boxId:
                            self.mainPanel.delete(Id)
            except:
                pass

    def delBBox(self):
        try:
            sel = self.listbox.curselection()
            if len(sel) != 1 :
                return
            idx = int(sel[0])
            if self.flag == 0:
                self.mainPanel.delete(self.bboxIdList[idx])
            if self.flag == 1:
                for Id in self.bboxIdList[idx]:
                    self.mainPanel.delete(Id)
            self.mainPanel.delete(self.classIdList[idx])
            self.bboxIdList.pop(idx)
            self.classIdList.pop(idx)
            self.bboxList.pop(idx)
            self.classes_name.pop(idx)
            self.listbox.delete(idx)
        except:
            pass

    def clearBBox(self):
        try:
            for idx in range(len(self.bboxIdList)):
                if self.flag == 0:
                    self.mainPanel.delete(self.bboxIdList[idx])
                if self.flag == 1:
                    for Id in self.bboxIdList[idx]:
                        self.mainPanel.delete(Id)
                self.mainPanel.delete(self.classIdList[idx])
            self.listbox.delete(0, len(self.classes_name))
            self.bboxIdList = []
            self.classIdList = []
            self.bboxList = []
            self.classes_name = []
            self.coordinate = []
            self.bboxIdLine = []
        except:
            pass

    def Delete_image(self, event):
        image_path = self.imageList[self.cur-1]
        if self.cur < self.total:
            self.cur += 1
            self.loadImage()
        else:
            self.messageList.insert(END, "Image NO. %d deleted" % self.cur)
            if len(self.messageList.get(0,END)) > 1:
                self.messageList.delete(1)
            messagebox.showinfo('Information','\tCompleted!\n\nAll images have been labelled!')
        os.remove(image_path)
        self.del_num += 1
        self.messageList.insert(END, "Image NO. %d deleted \t%d images have been deleted." %(self.cur-1, self.del_num))
        if len(self.messageList.get(0, END)) > 16:
            self.messageList.delete(1)

    def prevImage(self, event = None):
        try:
            self.saveImage()
            if self.cur > 1:
                self.cur -= 1
                self.loadImage()
            else:
                messagebox.showinfo('Information', "\t\n\nIt's already the first image.")
        except:
            pass

    def nextImage(self, event = None):
        try:
            self.saveImage()
            if self.cur < self.total:
                self.cur += 1
                self.loadImage()
            else:
                self.messageList.insert(END, "All images have been labelled!")
                if len(self.messageList.get(0,END)) > 16:
                    self.messageList.delete(1)
                messagebox.showinfo('Information','\tCompleted!\n\nAll images have been labelled!')
        except:
            pass

def createElementNode(doc, tag, attr):
    element_node = doc.createElement(tag)
    text_node = doc.createTextNode(attr)
    element_node.appendChild(text_node)
    return element_node

def createChildNode(doc, tag, attr, parent_node):
    child_node = createElementNode(doc, tag, attr)
    parent_node.appendChild(child_node)

def createObjectNode(doc, classes_name, bbox, flag):
    object_node = doc.createElement('object')
    for i in range(len(classes_name)):
        class_name = classes_name[i]
        boxes = bbox[i]
        createChildNode(doc, 'name', class_name, object_node)
        createChildNode(doc, 'pose', _POSE, object_node)
        createChildNode(doc, 'truncated', _TRUNCATED, object_node)
        createChildNode(doc, 'difficult', _DIFFICULT, object_node)

        bndbox_node = doc.createElement('bndbox')
        if flag == 0:
            createChildNode(doc, 'xmin', str(boxes[0]), bndbox_node)
            createChildNode(doc, 'ymin', str(boxes[1]), bndbox_node)
            createChildNode(doc, 'xmax', str(boxes[2]), bndbox_node)
            createChildNode(doc, 'ymax', str(boxes[3]), bndbox_node)
        if flag == 1:
            createChildNode(doc, 'xlu', str(boxes[0][0]), bndbox_node)
            createChildNode(doc, 'ylu', str(boxes[0][1]), bndbox_node)
            createChildNode(doc, 'xru', str(boxes[1][0]), bndbox_node)
            createChildNode(doc, 'yru', str(boxes[1][1]), bndbox_node)
            createChildNode(doc, 'xrd', str(boxes[2][0]), bndbox_node)
            createChildNode(doc, 'yrd', str(boxes[2][1]), bndbox_node)
            createChildNode(doc, 'xld', str(boxes[3][0]), bndbox_node)
            createChildNode(doc, 'yld', str(boxes[3][1]), bndbox_node)

        object_node.appendChild(bndbox_node)
    return object_node

def writeXMLFile(doc, filename):
    tmpfile = open('tmp.xml', 'w')
    doc.writexml(tmpfile, addindent = ' '*4, newl = '\n', encoding = 'utf-8')
    tmpfile.close()

    fin = open('tmp.xml')
    fout = open(filename, 'w')
    lines = fin.readlines()

    for line in lines[1:]:
        if line.split():
            fout.writelines(line)
    fin.close()
    fout.close()
    os.remove('tmp.xml')

def createXML(image_name, shape, classes_name, bbox, flag):
    my_dom = xml.dom.getDOMImplementation()
    doc = my_dom.createDocument(None, 'annotation', None)

    root_node = doc.documentElement
    createChildNode(doc, 'folder', 'HUST2018', root_node)
    createChildNode(doc, 'filename', image_name, root_node)

    source_node = doc.createElement('source')
    createChildNode(doc, 'database', 'Detection', source_node)
    createChildNode(doc, 'annotation', 'HUST2018', source_node)
    createChildNode(doc, 'image', 'flickr', source_node)
    createChildNode(doc, 'flickrid', 'NULL', source_node)
    root_node.appendChild(source_node)

    owner_node = doc.createElement('owner')
    createChildNode(doc, 'flickr_url', '0', owner_node)
    createChildNode(doc, 'name', '?', owner_node)
    root_node.appendChild(owner_node)

    size_node = doc.createElement('size')
    createChildNode(doc, 'width', str(shape[0]), size_node)
    createChildNode(doc, 'height', str(shape[1]), size_node)
    createChildNode(doc, 'depth', str(shape[2]), size_node)
    root_node.appendChild(size_node)

    createChildNode(doc, 'segmented', _SEGMENTED, root_node)

    object_node = createObjectNode(doc, classes_name, bbox, flag)
    root_node.appendChild(object_node)
    return doc

def Help():
    space = "     "
    p = "\n\n" + space + "This is a simple tool for labelling object in image.\n"
    p1 = space + "The Label-Tool can label rectangle (two coordinates) and polygon (four coordinates), you can select mode.\n\n"
    p2 = space + "Usage:\n"
    p3 = space + space + "1. Select mode (rectangle or polygon). This is a radio, you can only choose one, and can't change mode during labelling.\n"
    p4 = space + space + "2. Select path that has '.jpg' file. If not '.jpg' format, you can run 'image.py' to generate and number images.\n"
    p5 = space + space + "3. Label:\n"
    p6 = "\tIf you choose 'rectangle', click the left mouse button, select the first point, then move the mouse, click the left button " \
         "again, select the second point, and then you can see the rectangle. And then click the right mouse button to select class name. " \
         "Finally, a bounding box is labelled.\n"
    p7 = "\nNOTE: If you draw rectangle wrong and class name has not been selected, you can click middle mouse button to delete rectangle. " \
         "If class name has been selected, you can click the bounding box that you want to delete in 'Bounding Box', then click 'Delete' " \
         "button. If you want to delete all bounding boxes, you can click 'ClearAll' button.\n\n"
    p8 = "\tIf you choose 'polygon', click the left mouse button four times to select four points, then you can see the polygon. And " \
         "then click the right mouse button to select class name.\n" \
         "\nNOTE: When you label polygon, you must click left-up, right-up, right-down, left-down of the polygon in order.\n\n"
    p9 = space + space + "4. If you label a image, click 'Next >>' button to next image, or you can press shortcut 's' or 'd' to next image. " \
                         "If you want to check previous image, you can click '<< Prev' button, or press shortcut 'w' or 'a'.\n"
    p10 = space + space + "5. If you think the image is not good, you can click 'Delete Image' button to delete image.\n"
    p11 = space + space + "6. Image's label is saved to '.xml' format, this is VOC format, you can easily use. Image's " \
                          "label is also saved to '.txt' format, this is used to check annotation.\n"
    p12 = "\nNOTE: Image shows on the canvas, the image is forced to zoom to the canvas size, so the image may be distorted, but " \
          "this is no problem, the coordinates of the annotation are restored to the original image size.\n"
    p13 = "\nNOTE: If you label your own data set, you need to modify 'CLASSES'.\n"
    p14 = "\nNOTE: If you find a bug in using, you can submit it in the issue."
    log = p + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10 + p11 + p12 + p13 + p14
    messagebox.showinfo('Help', log)

if __name__ == '__main__':
    root = Tk()
    tool = LabelTool(root)
    root.mainloop()

max_stars_repo_name: leeyoshinari/Label-Tool

max_issues_repo_name: leeyoshinari/Label-Tool

max_forks_repo_name: leeyoshinari/Label-Tool


text:
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.gridspec as gs
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.stats.diagnostic import acorr_ljungbox as ljung
from statsmodels.tsa.stattools import adfuller

### define various error metrics for time series forecasting
def MAE(forecast, observation):
    """ This is a scaled dependent error measure.
        :var forecast: np.ndarray or pd.DataFrame object with the computed forecast
        :var observation: np.ndarray or pd.DataFrame object with the actual observations to compare the forecast to
        :return MAE: mean absolute error (float)
    """
    if isinstance(forecast, pd.DataFrame):
        forecast = np.array(forecast)
    if isinstance(observation, pd.DataFrame):
        observation = np.array(observation)
    err = forecast - observation
    return np.mean(np.abs(err))

def RMSE(forecast, observation):
    """ This is a scaled dependent error measure.
        :var forecast: np.ndarray or pd.DataFrame object with the computed forecast
        :var observation: np.ndarray or pd.DataFrame object with the actual observations to compare the forecast to
        :return RMSE: root mean squared error (float)
    """
    if isinstance(forecast, pd.DataFrame):
        forecast = np.array(forecast)
    if isinstance(observation, pd.DataFrame):
        observation = np.array(observation)
    err = forecast - observation
    return np.sqrt(np.mean(err ** 2))

def MAPE(forecast, observation):
    """ This error measure is a percentage error, thus is unit-free and not scale dependent.
        :var forecast: np.ndarray or pd.DataFrame object with the computed forecast
        :var observation: np.ndarray or pd.DataFrame object with the actual observations to compare the forecast to
        :return: MAPE: mean absolute percentage error (float)
    """
    if isinstance(forecast, pd.DataFrame):
        forecast = np.array(forecast)
    if isinstance(observation, pd.DataFrame):
        observation = np.array(observation)

    err = forecast - observation
    return np.mean(np.abs(err / observation))

def sMAPE(forecast, observation):
    """ This error measure is a percentage error, thus is unit-free and not scale dependent.
        Symmetric mean absolute percentage error: is symmetrizised along forecast and observations
        :var forecast: np.ndarray or pd.DataFrame object with the computed forecast
        :var observation: np.ndarray or pd.DataFrame object with the actual observations to compare the forecast to
        :return: sMAPE: symmetric mean absolute percentage error (float)
    """
    if isinstance(forecast, pd.DataFrame):
        forecast = np.array(forecast)
    if isinstance(observation, pd.DataFrame):
        observation = np.array(observation)
    err = forecast - observation
    return np.mean(2. * np.abs(err) / (forecast + observation))

def MASE(forecast, observation, seasonal=None):
    """ This error measure is a scaled error, thus is not scale dependent.
        The MASE is scaled by the length of the forecast as well as the difference between lags of data
        :var forecast: np.ndarray or pd.DataFrame object with the computed forecast
        :var observation: np.ndarray or pd.DataFrame object with the actual observations to compare the forecast to
        :return: MASE: mean absolute scaled error (float)
    """
    if isinstance(forecast, pd.DataFrame):
        forecast = np.array(forecast)
    if isinstance(observation, pd.DataFrame):
        observation = np.array(observation)
    err = forecast - observation
    T = len(forecast)
    if seasonal == None:
        nlagged = observation[1:]
        lagged = observation[:-1]
        q = err / (1. / (T - 1.) * np.sum(np.abs(nlagged - lagged)))
    else:
        m = seasonal
        nlagged = observation[m + 1:]
        lagged = observation[:-(m + 1)]
        q = err / (1. / (T - m) * np.sum(np.abs(nlagged - lagged)))

    return np.mean(np.abs(q))

### define measurements of strength for trend and seasonality in time series
def measure_trend(data):
    """ This is a measurement of the strength of the trend component in a time series. It was proposed in:
        Wang, X., Smith, K. A., & Hyndman, R. J. (2006). Characteristic-based clustering for time series data.
        Data Mining and Knowledge Discovery, 13(3), 335–364.
        :var data: time series as a pd.DataFrame object (1-dimensional)
        :return: tuple of trend-strength for additive and multiplicative trend components
    """
    assert isinstance(data, pd.DataFrame)

    F = []

    for model in ['additive', 'multiplicative']:
        decomposition = sm.tsa.seasonal_decompose(data[data.columns[:1]], model=model)
        trend = np.array(decomposition.trend.dropna())#.to_numpy()
        resid = np.array(decomposition.resid.dropna())#.to_numpy()
        F_T = max([0, 1. - np.var(np.array(resid)) / np.var(np.array(resid) + np.array(trend))])
        F.append(F_T)
    return (F[0], F[1])

def measure_seasonality(data):
    """ This is a measurement of the strength of the seasonal component in a time series. It was proposed in:
        Wang, X., Smith, K. A., & Hyndman, R. J. (2006). Characteristic-based clustering for time series data.
        Data Mining and Knowledge Discovery, 13(3), 335–364.
        :var data: time series as a pd.DataFrame object (1-dimensional)
        :return: tuple of seasonal-strength for additive and multiplicative seasonal components
    """

    assert isinstance(data, pd.DataFrame)

    F = []

    for model in ['additive', 'multiplicative']:
        decomposition = sm.tsa.seasonal_decompose(data[data.columns[:1]], model=model)
        season = decomposition.seasonal
        resid = decomposition.resid
        ### remove data from seasonal data, where resid has np.nan entries to ensure same lengths
        season = np.array(season[~resid.isna()].dropna())#.to_numpy()
        resid = np.array(resid.dropna())#.to_numpy()
        F_S = max([0, 1. - np.var(np.array(resid)) / np.var(np.array(resid) + np.array(season))])
        F.append(F_S)
    return (F[0], F[1])

def plot_ACF(x, residuals):
    """top figure: plots residuals as a function of the x-coordinate
        left bottom figure: plots histogram with kde
        right bottom figure: plots ACF of residuals
        :var x: is most likely the time variable (np.ndarray)
        :var residuals: already computed residuals of the time series
        """

    ### Minimum and maximum of residuals for y-axis limits
    Min = residuals.min() * 1.2
    Max = residuals.max() * 1.2

    plt.figure(figsize=(16, 8))
    gspec = gs.GridSpec(2, 4)

    top_fig = plt.subplot(gspec[0, :3])
    ### plots residuals as function of x
    top_fig.plot(x, residuals, c='k');
    plt.grid()
    top_fig.set_xlabel("time");
    top_fig.set_ylabel("residuals");
    top_fig.set_ylim(Min, Max);

    top_right = plt.subplot(gspec[0, 3])
    ### plots histogram of residuals beside the residuals plot
    ### shares y-axis with top_fig
    sns.distplot(residuals, rug=True, vertical=True);
    top_right.set_ylim(Min, Max);
    top_right.spines['left'].set_visible(False)
    top_right.yaxis.set_visible(False)
    ### adjust position of figure such that it looks pretty
    pos1 = top_right.get_position()  # get the original position
    pos2 = [pos1.x0 - 0.035, pos1.y0, pos1.width, pos1.height]
    top_right.set_position(pos2)  # set a new position
    ### delete first xtick
    labels = top_right.get_xticks().tolist()
    labels[0] = ''
    top_right.set_xticklabels(labels)

    lags = 20

    left_bottom = plt.subplot(gspec[1, :2])
    ### plots ACF
    left_bottom.set_xlabel("lags");
    # left_bottom.set_ylabel("ACF");
    ### adjust position of figure such that it looks pretty
    pos1 = left_bottom.get_position()  # get the original position
    pos2 = [pos1.x0, pos1.y0 - 0.1, pos1.width, pos1.height]
    left_bottom.set_position(pos2)  # set a new position
    left_bottom = plot_acf(residuals, ax=left_bottom, lags=lags, title='ACF', zero=True)

    right_bottom = plt.subplot(gspec[1, 2:])
    ### plots PACF
    right_bottom.set_xlabel("lags");
    # right_bottom.set_ylabel("PACF");
    ### adjust position of figure such that it looks pretty
    pos1 = right_bottom.get_position()  # get the original position
    pos2 = [pos1.x0, pos1.y0 - 0.1, pos1.width, pos1.height]
    right_bottom.set_position(pos2)  # set a new position
    right_bottom = plot_pacf(residuals, ax=right_bottom, lags=lags, title='PACF', zero=True)

def ljung_box_test(x, alpha=0.05, seasonal=None):
    """
        Performs ljung-box and box-pierce test on x-data in order to check for auto-correlation
        Remark: still an experimental feature in statsmodels.stats.diagnostic
        :var x: time series input: np.ndarray
        :param alpha: significance level
        :param seasonal: whether the time series has a seasonal component
    """
    if seasonal != None:
        h = min([2 * seasonal, int(len(x) / 5)])
    else:
        h = min([10, int(len(x) / 5)])
    ljung_stat, ljung_p, box_stat, box_p = ljung(x, lags=h, boxpierce=True)

    ### Ljung-Box test
    n = len(x)
    n_ljung = (np.array(ljung_p) < alpha).sum()
    #     EXPERIMENTAL COMPUTE Q VALUE FOR LJUNG BOX TEST
    #     corrs = np.correlate(x, x)[1:]**2
    #     num = np.array(range(len(corrs)))
    #     print(corrs, num)
    #     Q = np.sum(corrs/(n-num))*n*(n+2.)
    #     (chi2, p) = chisquare(x, ddof=0)
    #     print(Q, chi2)
    ### Box-Pierce test
    n_box = (np.array(box_p) < alpha).sum()
    print("Estimated required lag to be h=", h)
    print("Ljung-Box test\n Number of p-values below significance level of " + "{0:.2f}: {1}".format(alpha, n_ljung))
    #     print("Q={0:.2f}>{1:.2f}".format(Q, chi2)) if Q>chi2 else "Q={0:.2f}<{1:.2f}".format(Q, chi2)
    print("Box-Pierce test\n Number of p-values below significance level of " + "{0:.2f}: {1}".format(alpha, n_box))
    print("When there are p-values below the significance level, we can reject the null hypothesis," +
          " meaning that there are still correlations in the data. ")

def aug_dickey_fuller(timeseries, window):
    """
        Test, whether given timeseries is stationary by applying Dickey Fuller test and plot the series
        :param timeseries: pd.DataFrame
        :param window: (int) Size of the moving window. This is the number of observations used for calculating the statistic.
                        Each window will be a fixed size.
    """
    #Determing rolling statistics
    rolmean = timeseries.rolling(window=window).mean()
    rolstd = timeseries.rolling(window=window).std()
#Plot rolling statistics:
    plt.plot(timeseries, color='blue',label='Original')
    plt.plot(rolmean, color='red', label='Rolling Mean')
    plt.plot(rolstd, color='black', label = 'Rolling Std')
    plt.legend(loc='best')
    plt.title('Rolling Mean & Standard Deviation')
    plt.xlabel("time index");
    plt.ylabel("scaled value")
    plt.show()
    #Perform Dickey-Fuller test:
    print('Results of Dickey-Fuller Test:')
    dftest = adfuller(timeseries, autolag='AIC')
    dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
    for key,value in dftest[4].items():
        dfoutput['Critical Value (%s)'%key] = value
    print(dfoutput)

def plot_decomposition(data, model='additive'):
    """
        Performs a classical decomposition of a time series
        :var data: time series as a pd.DataFrame object (1-dimensional)
        :param model: decomposition type
    """

    assert isinstance(data, pd.DataFrame)
    col = data.columns[0]

    decomposition = sm.tsa.seasonal_decompose(data, model=model)
    season = decomposition.seasonal
    trend = decomposition.trend
    resid = decomposition.resid

    plt.figure(figsize=(16, 8))
    gspec = gs.GridSpec(4, 1)

    ax1 = plt.subplot(gspec[0, 0])
    plt.plot(data.index, data[col]);
    ax1.set_ylabel("data");

    ax2 = plt.subplot(gspec[1, 0])
    plt.plot(data.index, season);
    ax2.set_ylabel("seasonal");
    ### adjust position of figure such that it looks pretty
    pos1 = ax2.get_position()  # get the original position
    pos2 = [pos1.x0, pos1.y0 - 0.01, pos1.width, pos1.height]
    ax2.set_position(pos2)  # set a new position

    ax3 = plt.subplot(gspec[2, 0])
    plt.plot(data.index, trend);
    ax3.set_ylabel("trend");
    ### adjust position of figure such that it looks pretty
    pos1 = ax3.get_position()  # get the original position
    pos2 = [pos1.x0, pos1.y0 - 0.02, pos1.width, pos1.height]
    ax3.set_position(pos2)  # set a new position

    ax4 = plt.subplot(gspec[3, 0])
    plt.plot(data.index, resid);
    ax4.set_ylabel("remainder");
    ### adjust position of figure such that it looks pretty
    pos1 = ax4.get_position()  # get the original position
    pos2 = [pos1.x0, pos1.y0 - 0.03, pos1.width, pos1.height]
    ax4.set_position(pos2)  # set a new position
    ax4.set_xlabel("date");

def forecast_movingaverage(train, test, seasonal=None, my_windowtype = 'hamming'):
    """Computes a forecast for a moving average model
        It uses a naive forecast if there is a seasonal pattern
        and a forecast mean forecast using the last N data points for the remainder
        :var train: training set: pd.DataFrame
        :var test: test set: pd.DataFrame
        :param seasonal: window parameter for rolling mean of seasonal component
        :param my_windowtype: window type used for rolling mean
        :return (predict, observed, std): pd.DataFrames of prediction, observation and standard deviation of training
                                            data compared with moving average (float)
    """

    assert isinstance(train, pd.DataFrame)
    assert isinstance(test, pd.DataFrame)
    col = train.columns[0]

    ### moving averages of training data
    if seasonal == None:
        seasonal = 7
    mavg = train.rolling(window=seasonal, center=True, win_type=my_windowtype).mean()

    ### number of missing data points at end of mavg
    nans_end = int(seasonal / 2 - 1) if seasonal % 2 == 0 else int((seasonal - 1) / 2)
    nans_front = nans_end + 1  # if seasonal % 2 == 0 else nans_end+1
    for n in reversed(range(nans_end + 1)):
        mavg.iloc[-n] = mavg.iloc[-seasonal - n]

    train = train.iloc[nans_front:]
    mavg = mavg.iloc[nans_front:]
    remainder = train - mavg
    std = float(np.std(remainder))

    ### forecasting of test data
    N = 7
    observation, pred = [], []
    hist = list(train[col])
    date = test.index

    for m in range(len(test)):
        season = mavg.iloc[-seasonal + m]
        fluc = np.mean(np.array(remainder.iloc[-1]))
        remainder = remainder.append(pd.DataFrame([fluc], index=[date[m]], columns=[col]))
        forecast = season + fluc

        observation.append(test.iloc[m])
        pred.append(forecast)
        #         hist.append(test.iloc[m])
        hist.append(forecast)
    predict = pd.DataFrame(pred, index=date, columns=[col])
    observed = pd.DataFrame(observation, index=date, columns=[col])
    return (predict, observed, std)

def search_ARIMA_params(data, prange):
    """
    Grid search for optimal ARIMA parameters
    :param data: np.ndarray of input data
    :param prange: int of maximum parameter
    :return: triple of ints with best parameters
    """
    p = q = d = range(0, prange)
    pdq = list(itertools.product(p, d, q))
    params = {}
    for param in pdq:
        try:
            mod = ARIMA(data, order=param)
            results = mod.fit()
            params[param] = results.aic
            print("ARIMA {} - AIC:{}".format(param, results.aic))
        except:
            print("Could not fit model for param ", param)
            continue
    best_param = min(params.items(), key=itemgetter(1))
    print("Found best parameter to be ", best_param[0], "with AIC=", best_param[1])
    return best_param

def search_SARIMA_params(data, prange):
    """
    Grid search for optimal ARIMA parameters
    :param data: np.ndarray of input data
    :param prange: int of maximum parameter
    :return: triple of ints with best parameters
    """
    p = q = d = range(0, prange)
    pdq = list(itertools.product(p, d, q))
    # Generate all different combinations of seasonal p, q and q triplets
    seasonal_pdq = [(x[0], x[1], x[2], 12) for x in list(itertools.product(p, d, q))]
    params = {}
    for param in pdq:
        for param_seasonal in seasonal_pdq:
            try:
                mod = sm.tsa.statespace.SARIMAX(data,
                                                order=param,
                                                seasonal_order=param_seasonal,
                                                enforce_stationarity=False,
                                                enforce_invertibility=False)

                results = mod.fit()
                params[(param, param_seasonal)] = results.aic
                print('SARIMAX {}x{}12 - AIC:{}'.format(param, param_seasonal, results.aic))
            except:
                continue
    best_param = min(params.items(), key=itemgetter(1))
    print("Found best parameter to be ", best_param[0], "with AIC=", best_param[1])
    return best_param

reason: import numpy,import statsmodels,from statsmodels


text:
#!/usr/bin/env python3 -u
import logging
import math
import os
import random
import sys

import numpy as np
import torch

from fairseq import (
    checkpoint_utils, distributed_utils, metrics, options, progress_bar, tasks, utils
)
from fairseq.data import iterators
from fairseq.trainer import Trainer
from fairseq.meters import StopwatchMeter

import sacrebleu
import rouge_local as rouge
from bleu_local import compute_bleu

logging.basicConfig(
    format='%(asctime)s | %(levelname)s | %(name)s | %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    level=logging.INFO,
    stream=sys.stdout,
)
logger = logging.getLogger('fairseq_cli.train')

def main(args, init_distributed=False):
    utils.import_user_module(args)

    assert args.max_tokens is not None or args.max_sentences is not None, \
        'Must specify batch size either with --max-tokens or --max-sentences'

    # Initialize CUDA and distributed training
    if torch.cuda.is_available() and not args.cpu:
        torch.cuda.set_device(args.device_id)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    if init_distributed:
        args.distributed_rank = distributed_utils.distributed_init(args)

    if distributed_utils.is_master(args):
        checkpoint_utils.verify_checkpoint_directory(args.save_dir)

    # Print args
    logger.info(args)

    # Setup task, e.g., translation, language modeling, etc.
    task = tasks.setup_task(args)

    # Load valid dataset (we load training data below, based on the latest checkpoint)
    for valid_sub_split in args.valid_subset.split(','):
        task.load_dataset(valid_sub_split, combine=False, epoch=0)

    # Build model and criterion
    model = task.build_model(args)
    criterion = task.build_criterion(args)
    logger.info(model)
    logger.info('model {}, criterion {}'.format(args.arch, criterion.__class__.__name__))
    logger.info('num. model params: {} (num. trained: {})'.format(
        sum(p.numel() for p in model.parameters()),
        sum(p.numel() for p in model.parameters() if p.requires_grad),
    ))

    # Build trainer
    trainer = Trainer(args, task, model, criterion)
    logger.info('training on {} GPUs'.format(args.distributed_world_size))
    logger.info('max tokens per GPU = {} and max sentences per GPU = {}'.format(
        args.max_tokens,
        args.max_sentences,
    ))

    # Load the latest checkpoint if one is available and restore the
    # corresponding train iterator
    extra_state, epoch_itr = checkpoint_utils.load_checkpoint(args, trainer)

    # Train until the learning rate gets too small
    train_meter = StopwatchMeter()
    train_meter.start()
    valid_subsets = args.valid_subset.split(',')

    tokenize = sacrebleu.DEFAULT_TOKENIZER if not args.eval_tokenized_bleu else 'none'
    hyps, refs = validate(args, trainer, task, epoch_itr, valid_subsets)

    for h, r, split in zip(hyps, refs, args.valid_subset.split(',')):
        assert len(h) == len(r)

        sacrebleu_score, _, _ = sacrebleu.corpus_bleu(h, [r], tokenize=tokenize), hyps, refs
        bleu = compute_cvpr_bleu(h, r)
        rouge_score = rouge.rouge(h, r)

        print('{} set has {} samples,\n'
              'sacrebleu: {},\n'
              'CVPR BLEU scripts: {}\n'
              'CVPR ROUGE: {}'.format(split, len(h), sacrebleu_score, bleu, rouge_score))

        print('performance: {:.2f} {}'.format(rouge_score['rouge_l/f_score']*100 ,' '.join([str(b) for b in bleu])))

def validate(args, trainer, task, epoch_itr, subsets):
    """Evaluate the model on the validation set(s) and return the losses."""

    if args.fixed_validation_seed is not None:
        # set fixed seed for every validation
        utils.set_torch_seed(args.fixed_validation_seed)

    hypothes = []
    references = []

    for subset in subsets:
        # Initialize data iterator
        itr = task.get_batch_iterator(
            dataset=task.dataset(subset),
            max_tokens=args.max_tokens_valid,
            max_sentences=args.max_sentences_valid,
            max_positions=utils.resolve_max_positions(
                task.max_positions(),
                trainer.get_model().max_positions(),
            ),
            ignore_invalid_inputs=args.skip_invalid_size_inputs_valid_test,
            required_batch_size_multiple=args.required_batch_size_multiple,
            seed=args.seed,
            num_shards=args.distributed_world_size,
            shard_id=args.distributed_rank,
            num_workers=args.num_workers,
        ).next_epoch_itr(shuffle=False)
        progress = progress_bar.build_progress_bar(
            args, itr, epoch_itr.epoch,
            prefix='valid on \'{}\' subset'.format(subset),
            no_progress_bar='simple'
        )

        # create a new root metrics aggregator so validation metrics
        # don't pollute other aggregators (e.g., train meters)
        with metrics.aggregate(new_root=True) as agg:
            hyps, refs = [], []
            for sample in progress:
                logging_output, h, r = trainer.valid_step(sample, generate=True)

                hyps.extend(h)
                refs.extend(r)

        # log validation stats
        stats = get_valid_stats(args, trainer, agg.get_smoothed_values())
        progress.print(stats, tag=subset, step=trainer.get_num_updates())

        hypothes.append(hyps)
        references.append(refs)

    return hypothes, references

def get_valid_stats(args, trainer, stats):
    if 'nll_loss' in stats and 'ppl' not in stats:
        stats['ppl'] = utils.get_perplexity(stats['nll_loss'])
    stats['num_updates'] = trainer.get_num_updates()
    if hasattr(checkpoint_utils.save_checkpoint, 'best'):
        key = 'best_{0}'.format(args.best_checkpoint_metric)
        best_function = max if args.maximize_best_checkpoint_metric else min
        stats[key] = best_function(
            checkpoint_utils.save_checkpoint.best,
            stats[args.best_checkpoint_metric],
        )
    return stats

def distributed_main(i, args, start_rank=0):
    args.device_id = i
    if args.distributed_rank is None:  # torch.multiprocessing.spawn
        args.distributed_rank = start_rank + i
    main(args, init_distributed=True)

def compute_cvpr_bleu(hyps, refs, max_order=4):
    """Assume tokens in hypothesis and references are seperated with spaces.
    """
    tokenized_hyps = []
    tokenized_refs = []

    for h in hyps:
        tokenized_hyps.append(h.split())

    for r in refs:
        tokenized_refs.append([r.split()])

    bleu_all_orders = []

    for i in list(range(1, max_order+1)):
        bleu, precisions, bp, ratio, translation_length, reference_length = compute_bleu(tokenized_refs, tokenized_hyps, max_order=i)
        if i == 4:
            print ('precisions: {}'.format([str(round(p*100, 2)) for p in precisions]))
        bleu_all_orders.append(round(bleu * 100, 2))

    return bleu_all_orders

def cli_main(modify_parser=None):
    """
    Dongxu:
    1) Parse arguments; 2) choose distribution strategy; 3) call main() with args.
    :param modify_parser:
    :return:
    """
    parser = options.get_training_parser()
    args = options.parse_args_and_arch(parser, modify_parser=modify_parser)

    if args.distributed_init_method is None:
        distributed_utils.infer_init_method(args)

    if args.distributed_init_method is not None:
        # distributed training
        if torch.cuda.device_count() > 1 and not args.distributed_no_spawn:
            start_rank = args.distributed_rank
            args.distributed_rank = None  # assign automatically
            torch.multiprocessing.spawn(
                fn=distributed_main,
                args=(args, start_rank),
                nprocs=torch.cuda.device_count(),
            )
        else:
            distributed_main(args.device_id, args)
    elif args.distributed_world_size > 1:
        # fallback for single node with multiple GPUs
        assert args.distributed_world_size <= torch.cuda.device_count()
        port = random.randint(10000, 20000)
        args.distributed_init_method = 'tcp://localhost:{port}'.format(port=port)
        args.distributed_rank = None  # set based on device id
        if max(args.update_freq) > 1 and args.ddp_backend != 'no_c10d':
            logger.info('NOTE: you may get faster training with: --ddp-backend=no_c10d')
        torch.multiprocessing.spawn(
            fn=distributed_main,
            args=(args, ),
            nprocs=args.distributed_world_size,
        )
    else:
        # single GPU training
        main(args)

if __name__ == '__main__':
    cli_main()

max_stars_repo_name: verashira/TSPNet

max_issues_repo_name: verashira/TSPNet

max_forks_repo_name: verashira/TSPNet


text:
#### Code for converting videos to frames

import pylab
import cv2
import imageio
import scipy
from skimage.transform import rotate

filename = './task1.1.mp4'
vid = imageio.get_reader(filename,  'ffmpeg')
num= 0
for i in range(0,100000):
    image = vid.get_data(i)
    image_resize=cv2.resize(image, (112,112))
    #image_rot=rotate(image_resisze,0)
    cv2.imwrite("%d.png" % num,image_resize)
    num=num+1
    print(i)

reason: import scipy


text:
# def get_answer():
#     """Get an answer."""
#     return True

import numpy as np

def fn_inv_partitioned_a(invA,m_B,m_C,m_D):
    m_C_invA = m_C@invA
    m_E = m_D - m_C_invA@m_B
    invE = np.linalg.inv(m_E)
    m_invA_B_invE = invA@m_B@invE

    invH11 = invA + (m_invA_B_invE @ m_C_invA);
    invH12 = -m_invA_B_invE;
    invH21 = -invE @ m_C_invA;
    invH22 = invE;

    row1 = np.concatenate((invH11, invH12),axis = 1)
    row2 = np.concatenate((invH21, invH22),axis = 1)
    invH = np.concatenate((row1,row2),axis = 0)

    return invH

def fn_inv_partitioned_b(m_A,m_B,m_C,invD):
    m_B_invD = m_B @ invD;
    m_F = m_A - (m_B_invD @ m_C);
    invF = np.linalg.inv(m_F);
    m_invD_C_invF = invD @ m_C @ invF;

    invH11 = invF;
    invH12 = -invF @ m_B_invD;
    invH21 = -m_invD_C_invF;
    invH22 = invD + (m_invD_C_invF @ m_B_invD);

    row1 = np.concatenate((invH11, invH12),axis = 1)
    row2 = np.concatenate((invH21, invH22),axis = 1)
    invH = np.concatenate((row1,row2),axis = 0)

    return invH

max_stars_repo_name: ida-j/hetsar

max_issues_repo_name: ida-j/hetsar

max_forks_repo_name: ida-j/hetsar