text:
from os import listdir, makedirs, getcwd, remove
from os.path import isfile, join, abspath, exists, isdir, expanduser
from PIL import Image
import pickle
import nltk
import cv2 , os, time
import numpy as np

import torch
from torch.optim import lr_scheduler
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
import torch.utils.data as data
import torch.nn as nn
import torch.autograd as autograd
from torch.nn import ReLU

import torchvision
from torchvision import transforms, datasets, models

import scipy.misc
import matplotlib.pyplot as plt
import json

class cnn_layer_visualization():

    def __init__(self,model,imagenet_class_index_file_path="",output_path=""):
        self.model = model
        self.output_path = output_path
        self.modulelist = list(self.model.features.modules())
        self.labels = json.load(open(imagenet_class_index_file_path))

    def grayscale(self,image):
        image = torch.sum(image, dim=0)
        image = torch.div(image, image.shape[0])
        return image

    def normalize(self,image):
        normalize = transforms.Normalize(
            mean=[0.485,0.456,0.406],
            std=[0.229,0.224,0.225]
        )
        preprocess = transforms.Compose([
            transforms.Resize((224,224)),
            transforms.ToTensor(),
            normalize
        ])

        image = Variable(preprocess(image).unsqueeze(0))

        return image

    def predict(self,image):
        _, index = self.model(image).data[0].max(0)
        return str(index[0]), self.labels[str(index)][1]

    def deprocess(self,image):
        return image * torch.Tensor([0.229,0.224,0.225]) + torch.Tensor([0.485,0.456,0.406])

    def layer_outputs(self,image):
        outputs = []
        names = []
        for layer in self.modulelist[1:]:
            image = layer(image)
            outputs.append(image)
            names.append(str(layer))

        output_im = []
        for i in outputs:
            i = i.squeeze(0)
            temp = self.grayscale(i)
            output_im.append(temp.data.cpu().numpy())

        fig = plt.figure()
        plt.rcParams["figure.figsize"] = (100, 100)

        for i in range(len(output_im)):
            a = fig.add_subplot(8, 4, i + 1)
            imgplot = plt.imshow(output_im[i])
            plt.axis('off')
            a.set_title(names[i].partition('(')[0], fontsize=15)

        plt.savefig(join(self.output_path,'layer_outputs.jpg'), bbox_inches='tight')
        print("File saved at {} with file name {}.".format(self.output_path,"layer_outputs.jpg"))

    def get_all_layers(self,image):
        image = self.normalize(image)

        self.layer_outputs(image)

    def one_layer_output(self,image,layer_num = 0):
        outputs = []
        names = []
        for en,layer in enumerate(self.modulelist[1:]):
            image = layer(image)
            outputs.append(image)
            names.append(str(layer))
            if en == layer_num:
                break

        output_im = []
        out = outputs[-1].squeeze(0)
        temp = self.grayscale(out)
        output_im.append(temp.data.cpu().numpy())

        fig = plt.figure()
        plt.rcParams["figure.figsize"] = (100, 100)

        for i in range(len(output_im)):
            a = fig.add_subplot(8, 4, i + 1)
            imgplot = plt.imshow(output_im[i])
            plt.axis('off')
            a.set_title(names[i].partition('(')[0], fontsize=15)

        plt.savefig(join(self.output_path,'one_layer_output.jpg'), bbox_inches='tight')
        print("File saved at {} with file name {}.".format(self.output_path,"one_layer_output.jpg"))

    def get_one_layer(self,image,layer_num=0):
        image = self.normalize(image)

        self.one_layer_output(image, layer_num)

    def get_one_layer_live(self,layer_num=0):
        cv2.namedWindow("Input")
        cv2.namedWindow("Layer number {}".format(layer_num))

        cap = cv2.VideoCapture(0)
        count = 0
        start_time = time.time()

        while cap.isOpened():
            rval, frame = cap.read()
            frame_PIL = Image.fromarray(np.uint8(frame*255))
            prep_img = self.normalize(frame_PIL)

            image = prep_img

            outputs = []
            names = []
            for en, layer in enumerate(self.modulelist[1:]):
                image = layer(image)
                outputs.append(image)
                names.append(str(layer))
                if en == layer_num:
                    break

            output_im = []
            out = outputs[-1].squeeze(0)
            temp = self.grayscale(out)
            output_im.append(temp.data.cpu().numpy())

            output = output_im[0]

            cv2.imshow("Input", frame)
            cv2.imshow("Colored Guided Backpropagation", output)

            count += 1

            if count == 10:
                print("Frame per sec:{}".format(10 / (time.time() - start_time)))
                count = 0
                start_time = time.time()

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cv2.destroyAllWindows()

    def get_all_layers_live(self):
        cv2.namedWindow("Input")
        for i in range(len(self.modulelist[1:])):
            cv2.namedWindow("Layer number {}".format(i))

        cap = cv2.VideoCapture(0)
        count = 0
        start_time = time.time()

        while cap.isOpened():
            rval, frame = cap.read()
            frame_PIL = Image.fromarray(np.uint8(frame*255))
            prep_img = self.normalize(frame_PIL)

            image = prep_img

            outputs = []
            names = []
            for layer in self.modulelist[1:]:
                image = layer(image)
                outputs.append(image)
                names.append(str(layer))

            output_im = []
            for i in outputs:
                i = i.squeeze(0)
                temp = self.grayscale(i)
                output_im.append(temp.data.cpu().numpy())

            cv2.imshow("Input", frame)
            for i in range(len(output_im)):
                cv2.imshow("Layer number {}".format(i), output_im[i])

            count += 1

            if count == 10:
                print("Frame per sec:{}".format(10 / (time.time() - start_time)))
                count = 0
                start_time = time.time()

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cv2.destroyAllWindows()

    def filter_outputs(self,image, layer_to_visualize):
        if layer_to_visualize < 0:
            layer_to_visualize += 31
        output = None
        name = None
        for count, layer in enumerate(self.modulelist[1:]):
            image = layer(image)
            if count == layer_to_visualize:
                output = image
                name = str(layer)

        filters = []
        output = output.data.squeeze()
        for i in range(output.shape[0]):
            filters.append(output[i, :, :])

        fig = plt.figure()
        plt.rcParams["figure.figsize"] = (10, 10)

        for i in range(int(np.sqrt(len(filters))) * int(np.sqrt(len(filters)))):
            fig.add_subplot(np.sqrt(len(filters)), np.sqrt(len(filters)), i + 1)
            imgplot = plt.imshow(filters[i])
            plt.axis('off')

        plt.savefig(join(self.output_path, 'filter_outputs.jpg'), bbox_inches='tight')
        print("File saved at {} with file name {}.".format(self.output_path, "filter_outputs.jpg"))

    def get_all_filters_of_one_layer(self,image,layer_num):
        image = self.normalize(image)

        self.filter_outputs(image, layer_num)

    def get_all_filters_live(self,layer_to_visualize):
        cv2.namedWindow("Input")
        for i in range(len(self.modulelist[1:])):
            cv2.namedWindow("filter number {}".format(i))

        cap = cv2.VideoCapture(0)
        count = 0
        start_time = time.time()

        while cap.isOpened():
            rval, frame = cap.read()
            frame_PIL = Image.fromarray(np.uint8(frame*255))
            prep_img = self.normalize(frame_PIL)

            image = prep_img

            if layer_to_visualize < 0:
                layer_to_visualize += 31
            output = None
            name = None
            for count, layer in enumerate(self.modulelist[1:]):
                image = layer(image)
                if count == layer_to_visualize:
                    output = image
                    name = str(layer)

            filters = []
            output = output.data.squeeze().numpy()
            for i in range(output.shape[0]):
                filters.append(output[i, :, :])

            cv2.imshow("Input", frame)
            for i in range(len(filters)):
                cv2.imshow("filter number {}".format(i), filters[i])

            count += 1

            if count == 10:
                print("Frame per sec:{}".format(10 / (time.time() - start_time)))
                count = 0
                start_time = time.time()

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cv2.destroyAllWindows()

    def get_one_filter_live(self,layer_to_visualize,filter_num):
        cv2.namedWindow("Input")
        cv2.namedWindow("filter number {}".format(filter_num))

        cap = cv2.VideoCapture(0)
        count = 0
        start_time = time.time()

        while cap.isOpened():
            rval, frame = cap.read()
            frame_PIL = Image.fromarray(np.uint8(frame*255))
            prep_img = self.normalize(frame_PIL)

            image = prep_img

            if layer_to_visualize < 0:
                layer_to_visualize += 31
            output = None
            name = None
            for count, layer in enumerate(self.modulelist[1:]):
                image = layer(image)
                if count == layer_to_visualize:
                    output = image
                    name = str(layer)

            filters = []
            output = output.data.squeeze().numpy()

            filters.append(output[filter_num, :, :])

            cv2.imshow("Input", frame)
            cv2.imshow("filter number {}".format(filter_num), filters[0])

            count += 1

            if count == 10:
                print("Frame per sec:{}".format(10 / (time.time() - start_time)))
                count = 0
                start_time = time.time()

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cv2.destroyAllWindows()

    def visualize_weights(self, layer):
        weight_used = []
        for w in self.model.features.children():
            if isinstance(w, torch.nn.modules.conv.Conv2d):
                weight_used.append(w.weight.data)

        filters = []
        for i in range(weight_used[layer].shape[0]):
            filters.append(weight_used[layer][i, :, :, :].sum(dim=0))
            filters[i].div(weight_used[layer].shape[1])

        fig = plt.figure()
        plt.rcParams["figure.figsize"] = (10, 10)
        for i in range(int(np.sqrt(weight_used[layer].shape[0])) * int(np.sqrt(weight_used[layer].shape[0]))):
            a = fig.add_subplot(np.sqrt(weight_used[layer].shape[0]), np.sqrt(weight_used[layer].shape[0]), i + 1)
            imgplot = plt.imshow(filters[i])
            plt.axis('off')

        plt.savefig(join(self.output_path, 'weights.jpg'), bbox_inches='tight')
        print("File saved at {} with file name {}.".format(self.output_path, "weights.jpg"))

def load_image( path):
    image = Image.open(path)
    image = image.convert('RGB')
    return image

kitten_1 = load_image("../images/index.png")

model = models.vgg16(pretrained=True)

CLV = cnn_layer_visualization(model,output_path="",imagenet_class_index_file_path="../labels/imagenet_class_index.json")
CLV.visualize_weights(0)

max_stars_repo_name: Maitreyapatel/Deep-Visualization

max_issues_repo_name: Maitreyapatel/Deep-Visualization

max_forks_repo_name: Maitreyapatel/Deep-Visualization

reason: import numpy,import scipy


text:
import numpy as np

def get_reflector_emission(reflector_data, reflected_js, start_time, end_time):
    # If end time is not none then returns current ->  return = [C/s]
    # If end time == None then returns total charge emitted from start_time until sim end  -> return = [C]
    reflector_lookup = {
        'time': 0,
        'dt': 1,
        'jsid': 2,
        'charge_specular': 3,
        'charge_diffuse': 4
    }

    if not hasattr(reflected_js, '__iter__'):
        reflected_js = [reflected_js, ]

    diffuse_reflected_current, specular_reflected_current = 0., 0.
    for js in reflected_js:
        indices = np.where((reflector_data[:, reflector_lookup['jsid']] == js))[0]

        if not end_time:
            indices = np.where((reflector_data[:, reflector_lookup['time']] > start_time) &
                               (reflector_data[:, reflector_lookup['jsid']] == js))[0]
            # end_time = np.max(reflector_data[:, reflector_lookup['time']])
        else:
            indices = np.where((reflector_data[:, reflector_lookup['time']] > start_time) &
                               (reflector_data[:, reflector_lookup['time']] < end_time) &
                               (reflector_data[:, reflector_lookup['jsid']] == js))[0]

        specular_reflected_current += np.sum(reflector_data[indices, reflector_lookup['charge_specular']])
        diffuse_reflected_current += np.sum(reflector_data[indices, reflector_lookup['charge_diffuse']])

    if end_time:
        return (specular_reflected_current + diffuse_reflected_current) / (end_time - start_time)
    else:
        return (specular_reflected_current + diffuse_reflected_current)

def get_species_current(scraper_data, species_jsid, start_time, end_time):
    # If end time is not none then returns current ->  return = [C/s]
    # If end time == None then returns total charge emitted from start_time until sim end  -> return = [C]

    # Takes scraper_data from rswarp.particlecollector.particlereflector.analyze_collected_charge
    #   (assumes list ordered by species.js)
    # scraper_data is a list of `lostparticles_data` for all species
    # locally define lostparticles_data indices for a helpful reminder
    scraper_lookup = {
        'time': 0,
        'charge': 1,  # Warp records macroparticle_weight * number_macroparticles * macrop_charge == physical charge
        'dt': 2,
        'jsid': 3
    }

    total_current = 0.
    for species_data in scraper_data:
        if species_data.size == 0 or np.any(species_data[:, scraper_lookup['jsid']].astype(int) != species_jsid):
            # This species isn't here or is not the designated species
            continue
        if not end_time:
            indices = np.where((species_data[:, scraper_lookup['time']] > start_time))[0]
            # end_time = np.max(species_data[:, scraper_lookup['time']])
        else:
            indices = np.where((species_data[:, scraper_lookup['time']] > start_time) &
                               (species_data[:, scraper_lookup['time']] < end_time))[0]

        total_species_charge_collected = np.sum(species_data[indices, scraper_lookup['charge']])

        total_current += total_species_charge_collected

    if end_time:
        return total_current / (end_time - start_time)
    else:
        return total_current

def get_total_current(scraper_data, start_time, end_time, area):
    # scraper_data is a list of `lostparticles_data` for each species
    # locally define lostparticles_data indices for a helpful reminder
    scraper_lookup = {
        'time': 0,
        'charge': 1,  # Warp records macroparticle_weight * number_macroparticles == physical charge
        'dt': 2,
        'jsid': 3
    }

    total_current = 0.
    for species_id in range(len(scraper_data)):
        total_current += get_species_current(scraper_data, species_id, start_time, end_time)

    print(total_current, total_current / area)
    return total_current

ext: py

max_stars_repo_name: radiasoft/rswarp

max_issues_repo_name: radiasoft/rswarp

max_forks_repo_name: radiasoft/rswarp

reason: import numpy


text:
'''
------------------------------------------------
The following code was created by me, with small functions
taken from Fajtl et al for specific utility. Wherever I write
"from VASnet", I am referring to code taken/amended from the following
repository and paper:

https://github.com/ok1zjf/VASNet

@article{fajtl2018summarizing,
    title={Summarizing Videos with Attention},
    author={Jiri Fajtl and Hajar Sadeghi Sokeh and Vasileios Argyriou and Dorothy Monekosso and Paolo Remagnino},
    journal={arXiv:1812.01969},
    year={2018}
}

------------------------------------------------
'''

import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter

import random
import numpy as np
import os
import h5py
import pandas as pd
from tqdm import tqdm

from vasnet_tools import *
from vsum_tools import *
from model import *
from train import *
from vst import *

class VST_keyframe(VST):
    def __init__(self, parameters):
        print("——— Initialising VST keyframe")
        rnd_seed = 16259

        # Set random seed for all packages that use randomness
        random.seed(rnd_seed)
        np.random.seed(rnd_seed)
        torch.manual_seed(rnd_seed)

        self.parameters = parameters
        self.summary_writer = None

        # 0 or 1 so vocab size is 2, 3 for start symbol
        self.model = make_model(tgt_vocab=3, d_model=1024, d_ff=1024, h=8, dropout=self.parameters.dropout)

        if parameters.model_summary:
            string, _, _ = torch_summarize(self.model)
            print(string)

        self.optimizer = None
        self.criterion = LabelSmoothing(size=3, padding_idx=2, smoothing=0.15)
        self.dataset_name = None
        self.dataset_type = None
        self.datasets = {}
        self.splits = None
        self.split_num = None
        self.split_file = None
        self.train_keys = None
        self.test_keys = None
        self.model.eval()

        if self.parameters.cuda:
            torch.cuda.set_device(0)
            torch.cuda.manual_seed(rnd_seed)
            self.model.cuda()

    def run_epoch(self, epoch, keys, loss_compute):
        if self.parameters.verbose:
            iterable = tqdm(keys)
        else:
            iterable = keys

        average_loss = 0

        for i, key in enumerate(iterable):
            # key is of the form dataset/video_id
            dataset, video = key.split('/')
            data = self.datasets[dataset][video]

            input_sequence = data['features'][...]
            target_scores = data['gtsummary'][...]

            # Add <BOS> character which in our case is 2
            target_scores = np.insert(target_scores, 0, 2)

            # Make them tensors, making the 'batch' of size 1
            input_sequence = torch.from_numpy(input_sequence).unsqueeze(0)
            target_scores = torch.from_numpy(target_scores).unsqueeze(0).long()

            # target_scores_y = torch.from_numpy(target_scores_y).unsqueeze(0)
            target_scores_y = target_scores[:, 1:] # Attends to all but the first
            target_scores = target_scores[:, :-1] # shifted right by 1

            enc_mask, dec_mask = make_std_mask(input_sequence, target_scores)

            if self.parameters.cuda:
                input_sequence = input_sequence.cuda()
                target_scores = target_scores.cuda()
                target_scores_y = target_scores_y.cuda()
                enc_mask = enc_mask.cuda()
                dec_mask = dec_mask.cuda()

            # FORWARD MODEL AND OPTIMISER #

            y = self.model.forward(input_sequence, target_scores, enc_mask, dec_mask)

            loss = loss_compute(y, target_scores_y, target_scores_y.size(-1))
            average_loss += loss

        average_loss /= len(keys)

        return average_loss

    def eval(self, epoch, keys):

        print(f"— Evaluating Test Samples for epoch: {epoch+1}")

        machine_summary = {}

        self.model.eval()

        if self.parameters.verbose:
            iterable = tqdm(keys)
        else:
            iterable = keys

        with torch.no_grad():
            for i, key in enumerate(iterable):
                # key is of the form dataset/video_id
                dataset, video = key.split('/')
                data = self.datasets[dataset][video]

                input_sequence = data['features'][...]

                # Make them tensors, making the 'batch' of size 1
                input_sequence = torch.from_numpy(input_sequence).unsqueeze(0)

                enc_mask, _ = make_std_mask(input_sequence)

                if self.parameters.cuda:
                    input_sequence = input_sequence.float().cuda()
                    enc_mask = enc_mask.float().cuda()

                if self.parameters.beam_width > 0:
                    machine_summary[key] = beam_search(self, self.parameters.beam_width, input_sequence, enc_mask, max_len=input_sequence.size(1), start_symbol=2)[0][1:]
                else:
                    machine_summary[key] = self.greedy_decode(input_sequence, enc_mask, max_len=input_sequence.size(1), start_symbol=2)[0][1:]

        mean_f_score, video_scores = self.eval_summary(machine_summary, keys, metric=self.dataset_name)
        print(f"Mean F-score: {mean_f_score}")

        # average_loss = 0

        # for _, key in enumerate(self.test_keys):
        #     dataset, video = key.split('/')
        #     # Get data from dataset at the video we care about
        #     data = self.datasets[dataset][video]

        #     # Extract the input sequence from the h5 data for testing
        #     input_sequence = data['features'][...]
        #     target_scores = data['gtscore'][...]
        #     summary = data['gtsummary'][...]

        #     original_scores = torch.from_numpy(target_scores).unsqueeze(0)

        #     target_scores = np.tile(target_scores, (1024, 1)).transpose()

        #     # Make them tensors, making the 'batch' of size 1
        #     input_sequence = torch.from_numpy(input_sequence).unsqueeze(0)
        #     target_scores = torch.from_numpy(target_scores).unsqueeze(0)

        #     # (?) Normalise frame scores
        #     target_scores -= target_scores.min()
        #     target_scores /= target_scores.max()

        #     original_scores -= original_scores.min()
        #     original_scores /= original_scores.max()

        #     # Create masks for the encoder and decoder
        #     # enc_mask = torch.ones(size=(input_sequence.size(1), input_sequence.size(1))).unsqueeze(0)
        #     # dec_mask = subsequent_mask(input_sequence.size(1))
        #     enc_mask, dec_mask = make_std_mask(input_sequence, target_scores)

        #     # (?) Cuda
        #     if self.parameters.cuda:
        #             input_sequence = input_sequence.float().cuda()
        #             target_scores = target_scores.float().cuda()
        #             enc_mask = enc_mask.float().cuda()
        #             dec_mask = dec_mask.float().cuda()

        #     # Forward pass of the model
        #     y = self.model.forward(input_sequence, target_scores[:, :-1, :], enc_mask, dec_mask[:, :-1, :-1])

        #     # Calculate loss to log using summary writer
        #     loss, generated = loss_backprop(self.model.generator, self.criterion, y, original_scores[:, 1:], 1, bp=False)
        #     average_loss += loss

        #     machine_summary[key] = generated

        # mean_f_score, video_scores = self.eval_summary(machine_summary, self.test_keys, metric=self.dataset_name)
        # # Log results for this eval
        # self.summary_writer.add_scalar('Loss/test', average_loss/len(self.test_keys), epoch)
        # self.summary_writer.add_scalar('Accuracy/test', mean_f_score, epoch)

        # Print results for this eval
        # print(f"Mean Loss: {average_loss/len(self.test_keys)}")
        # print(f"Mean F-score: {mean_f_score}")

        return mean_f_score, video_scores

    def eval_single(self, epoch, key):

        print(f"— Evaluating Test Samples: {key}")

        machine_summary = {}

        self.model.eval()

        with torch.no_grad():
            # key is of the form dataset/video_id
            dataset, video = key.split('/')
            data = self.datasets[dataset][video]

            input_sequence = data['features'][...]

            input_sequence = torch.from_numpy(input_sequence).unsqueeze(0)

            enc_mask, _ = make_std_mask(input_sequence)

            if self.parameters.cuda:
                input_sequence = input_sequence.float().cuda()
                enc_mask = enc_mask.float().cuda()

            if self.parameters.beam_width > 0:
                machine_summary[key] = beam_search(self, self.parameters.beam_width, input_sequence, enc_mask, max_len=input_sequence.size(1), start_symbol=2, norm=1)[0][1:]
            else:
                machine_summary[key] = self.greedy_decode(input_sequence, enc_mask, max_len=input_sequence.size(1), start_symbol=2)[0][1:]

        f_score, summary, data = self.eval_single_summary(machine_summary, key, metric=self.dataset_name)

        return machine_summary[key], summary, f_score, data

    def greedy_decode(self, src, src_mask, max_len, start_symbol=0):
        # This requires a start symbol, this is represented by the value -1 in our target scores
        memory = self.model.encode(src, src_mask)

        ys = torch.ones(1, 1).long().fill_(start_symbol)

        for i in range(max_len):
            out = self.model.decode(memory, src_mask,
                            Variable(ys).cuda() if self.parameters.cuda else Variable(ys),
                            Variable(subsequent_mask(ys.size(1))
                                        .type_as(src.data)))
            prob = self.model.generator(out[:, -1])
            _, next_word = torch.max(prob, dim = 1)
            next_word = next_word.data[0]
            ys = torch.cat([ys,
                            torch.ones(1, 1).long().fill_(next_word)], dim=1)

        final_y = ys.view(1,-1).data.cpu().numpy()[:, 1:]
        return final_y

    def eval_summary(self, machine_summary_activations, test_keys, results_filename=None, metric='tvsum', att_vecs=None):
        """
        Amended from VASnet
        """
        eval_metric = 'avg' if metric == 'tvsum' else 'max'

        fms = []
        video_scores = []
        for key_idx, key in enumerate(test_keys):
            dataset, video = key.split('/')
            data = self.datasets[dataset][video]

            probs = machine_summary_activations[key]

            if 'change_points' not in data:
                print("ERROR: No change points in dataset/video ",key)

            cps = data['change_points'][...]
            num_frames = data['n_frames'][()]
            nfps = data['n_frame_per_seg'][...].tolist()
            positions = data['picks'][...]
            user_summary = data['user_summary'][...]

            machine_summary = self.generate_summary(probs, cps, num_frames, nfps, positions)
            fm, _, _ = evaluate_summary(machine_summary, user_summary, eval_metric)
            fms.append(fm)

            video_scores.append([key_idx + 1, key, "{:.1%}".format(fm)])

        mean_fm = np.mean(fms)

        return mean_fm, video_scores

    def generate_summary(self, ypred, cps, n_frames, nfps, positions):
        """
        Amended from VASnet
        Generate keyshot-based video summary i.e. a binary vector.
        Args:
        ---------------------------------------------
        - ypred: predicted importance scores.
        - cps: change points, 2D matrix, each row contains a segment.
        - n_frames: original number of frames.
        - nfps: number of frames per segment.
        - positions: positions of subsampled frames in the original video.
        """
        n_segs = cps.shape[0]
        frame_scores = np.zeros((n_frames), dtype=np.float32)
        if positions.dtype != int:
            positions = positions.astype(np.int32)
        if positions[-1] != n_frames:
            positions = np.concatenate([positions, [n_frames]])

        for i in range(len(positions) - 1):
            pos_left, pos_right = positions[i], positions[i+1]
            if i == len(ypred):
                frame_scores[pos_left:pos_right] = 0
            else:
                frame_scores[pos_left:pos_right] = ypred[i]

        return frame_scores

    def save_model(self, epoch):
        os.makedirs(self.parameters.model_dir + 'temp', exist_ok=True)
        path = os.path.join(self.parameters.model_dir + 'temp', 'epoch-'+str(epoch)+'.pth')
        print(f"Saving model to {path}")
        torch.save({
            'epoch': self.parameters.epochs,
            'arguments': self.parameters,
            'model_state_dict': self.model.state_dict(),
            'encoder_attention': self.model.encoder.layers,
            'decoder_attention': self.model.decoder.layers
        }, path)

    def get_summary_writer_log_dir(self):
        tb_log_dir_prefix = (
        f"VST_keyframe_"
        f"dropout={self.parameters.dropout}_"
        f"epochs={self.parameters.epochs}_"
        f"lr_factor={self.parameters.lr_factor}_"
        f"split_file={self.split_file}_{self.split_num}"
        f"run_"
        )

        i = 0
        while i < 1000:
            tb_log_dir = "logs/" + tb_log_dir_prefix + str(i)
            if not os.path.exists(tb_log_dir):
                return str(tb_log_dir)
            i += 1
        return str(tb_log_dir)

max_stars_repo_name: tim-roderick/VST

max_issues_repo_name: tim-roderick/VST

max_forks_repo_name: tim-roderick/VST


text:
#!/usr/bin/env pyturbubox

import pickle
import argparse
from pathlib import Path
from collections import namedtuple
from decayturb import *
import box
import re
import uncertainties as uc

import numpy as np
from matplotlib import pylab as plt

## ------------------------------------------------------------------------- #
pp = argparse.ArgumentParser(description = 'Plotting PDF')

pp.add_argument(
    '--setup',
    help='task identifier',
    type=str.lower, #required=True,
    default='default',
)

pp.add_argument(
    '--pickle',
    help='path of pickle file',
    type=Path, required=True,
)

pp.add_argument(
    '--output',
    help='path of png file',
    type=str, required=True,
)

def parseSlice(argstr):
    mm = re.match('(\d+):(\d+)', argstr)
    if mm:
        a,b = mm.groups()
        return slice(int(a),int(b))
    else:
        a = int(argstr)
        return slice(a,a+1)

pp.add_argument(
    '--index',
    help='set index',
    type=parseSlice, #required=True,
)

pp.add_argument(
    '--key',
    help='keyword method: pdf_vw pdf_mw',
    type=str, #required=True,
)

pp.add_argument(
    '--subkey',
    help='keyword: dens rmsv ekin',
    type=str, #required=True,
)

pp.add_argument(
    '--xrange',
    type=lambda arg: tuple(float(x) for x in arg.split(':')),
    #default=(-4,4),
)

pp.add_argument(
    '--yrange',
    type=lambda arg: tuple(float(x) for x in arg.split(':')),
    #default=(-7,2),
)

pp.add_argument(
    '--fit', action='store_true',
)

ARGV = pp.parse_args()

with open(ARGV.pickle, 'rb') as fh:
    runs = box.Box(pickle.load(fh))

runs.order = [
    runs.bouc3, runs.bouc5,
    runs.flppm, runs.eu_fv,
    runs.mp_fv, runs.mp_hy,
    runs.rk_fv, runs.rk_hy,
]

runs.bouc3.color = 'orange'
runs.bouc5.color = 'orange'

runs.flppm.color = 'purple'
runs.eu_fv.color = 'cyan'

runs.mp_fv.color = 'blue'
runs.mp_hy.color = 'blue'

runs.rk_fv.color = 'green'
runs.rk_hy.color = 'green'

runs.bouc3.id = 'bouc3'
runs.bouc5.id = 'bouc5'

runs.flppm.id = 'flppm'
runs.eu_fv.id = 'eu_fv'

runs.mp_fv.id = 'mp_fv'
runs.mp_hy.id = 'mp_hy'

runs.rk_fv.id = 'rk_fv'
runs.rk_hy.id = 'rk_hy'

## ------------------------------------------------------------------------- #
## define fitting functions

def pdf(x):
    return np.exp(-x**2/2)

def cdf(x):
    return 0.5*(1+scipy.special.erf(x/np.sqrt(2)))

def skew_pdf(x,*p):
    A, mu, sigma, alpha = p
    x = (x-mu)/sigma
    return 2*A/sigma*pdf(x)*cdf(alpha*x)

def gauss(x,*p):
    A, mu, sigma = p
    x = (x-mu)/sigma
    return A*pdf(x)

def gauss_density(x,*p):
    mu, sigma = p
    x = (x-mu)/sigma
    return np.exp(-x**2/2)/np.sqrt(2*np.pi*sigma**2)

def sigma_mach_relation(sigma):
    zeta = 0.5 # ratio compressible to solenoidal forcing
    b = 1-2/3*zeta
    return np.sqrt(np.exp(sigma**2)-1)/b

def sigma_mach_deriv(sigma):
    zeta = 0.5 # ratio compressible to solenoidal forcing
    b = 1-2/3*zeta
    return sigma * np.exp(sigma**2) / np.sqrt(np.exp(sigma**2)-1) / b

## ------------------------------------------------------------------------- #
## Set custom configurations

index  = ARGV.index
key    = ARGV.key
subkey = ARGV.subkey
xrange = ARGV.xrange
yrange = ARGV.yrange
title  = '%s/%s PDF at Dynamic Time: t_d = %.1f' % (
        key, subkey,
        runs.order[0].anal['scalars']['dyntime'][index][0])
xlabel = 'log. scale %s' % ARGV.subkey
ylabel = 'log. scale PDF'
fitfun = gauss
fitini = [1,0,1]
dyntime = runs.order[0].anal['scalars']['dyntime'][index][0]

if ARGV.setup == 'density vw':
    index  = ARGV.index
    key    = 'pdf_vw'
    subkey = 'dens'
    xrange = ARGV.xrange
    yrange = ARGV.yrange
    title  = 'Volume-weighted Density PDF at Dynamic Time %.1f' % dyntime
    xlabel = r'log. scale density log$_{10}(\rho)$'
    ylabel = 'log. scale PDF'
    fitfun = gauss_density
    fitini = [0,1]

elif ARGV.setup == 'density vw range':
    index  = ARGV.index or  slice(20,40)
    key    = 'pdf_vw'
    subkey = 'dens'
    xrange = ARGV.xrange
    yrange = ARGV.yrange
    title  = 'Volume-weighted Density PDF between 2 and 4 Crossing Times'
    xlabel = r'log. scale density log$_{10}(\rho)$'
    ylabel = 'log. scale PDF'
    fitfun = gauss_density
    fitini = [0,1]

elif ARGV.setup == 'density mw':
    index  = ARGV.index or slice(20,40)
    key    = 'pdf_mw'
    subkey = 'dens'
    xrange = ARGV.xrange
    yrange = ARGV.yrange
    title  = 'Mass-weighted Density PDF between 2 and 4 Crossing Times'
    xlabel = 'log. scale density'
    ylabel = 'log. scale PDF'
    fitfun = gauss_density
    fitini = [0,1]

else:
    raise NotImplementedError("Setup '%s' is unknown." % ARGV.setup)

## ------------------------------------------------------------------------- #
fig = plt.figure(figsize=(12,6))

## ------------------------------------------------------------------------- #
if xrange is not None:
    plt.xlim(*xrange)
if yrange is not None:
    plt.ylim(*yrange)

## ------------------------------------------------------------------------- #
## Plot average of all curves

Xs,Ys = list(),list()

for run in runs.order:
    ii = index
    df = run.anal

    # take average
    nbins,npdf = 0.,0.
    cbins,cpdf = 0.,0.
    for pdf,bins in df[key][subkey][ii]:
        cbins += bins
        nbins += 1.
        cpdf  += pdf
        npdf  += 1.

    bins = cbins / nbins
    pdf  = cpdf  / npdf

    # take average
    nbins,npdf = 0.,0.
    cbins,cpdf = 0.,0.
    for _pdf,_bins in df[key][subkey][ii]:
        cpdf  += (pdf - _pdf)**2
        npdf  += 1.

    dpdf = np.sqrt(cpdf/npdf)

    xs  = ulz.bins2xs(bins)
    ys  = pdf
    dys = dpdf

    #n  = 12
    #ys,xs = ulz.moving_avg_1d(ys,xs,n)
    #dys = ulz.moving_avg_1d(dys,n=n)

    Xs.append(xs)
    Ys.append(ys)

    dys = dys[ys>0]
    xs  = xs[ys>0]
    ys  = ys[ys>0]

    plt.fill_between(xs, np.log10(np.where(ys-dys > 0, ys-dys, np.nan)), np.log10(ys+dys),
        facecolor='grey', alpha=0.5)

    plt.plot(xs,np.log10(ys),lw=1.5,label=run.label,ls=run.line,color=run.color)
    #plt.plot(xs,ys,lw=1.5,label=run.label,ls=run.line,color=run.color)

    if ARGV.fit is not None:
        try:
            coeff, var_matrix = scipy.optimize.curve_fit(fitfun, xs, ys, p0=fitini)
            _ys = fitfun(xs, *coeff)

            coeffL, var_matrixL = scipy.optimize.curve_fit(fitfun, xs, ys-dys, p0=fitini)
            coeffU, var_matrixU = scipy.optimize.curve_fit(fitfun, xs, ys+dys, p0=fitini)

            mu     = coeff[0]
            dmu    = np.abs(coeffL[0] - coeffU[0]) + np.sqrt(var_matrix[0,0])

            sigma  = coeff[1]
            dsigma = np.abs(coeffL[1] - coeffU[1]) + np.sqrt(var_matrix[1,1])

            mach   = sigma_mach_relation(2*sigma)
            dmach  = np.abs(sigma_mach_deriv(2*sigma)*2*dsigma)

            print(run.id, dyntime, mu, dmu, sigma, dsigma, mach, dmach, sep="\t")
            plt.plot(xs,np.log10(_ys),':',label='_nolegend_', color='gray')
        except:
            pass

if False:
    Xs = np.mean(np.array(Xs),axis=0)
    Ys = np.mean(np.array(Ys),axis=0)

    Xs = Xs[Ys>0]
    Ys = Ys[Ys>0]

    #plt.plot(Xs,np.log10(Ys),':',label='average', color='black')

    f = gauss
    coeff, var_matrix = scipy.optimize.curve_fit(f, Xs, Ys, p0=[1., 0., 1.])

    xlim = plt.gca().get_xlim()
    ylim = plt.gca().get_ylim()

    _xs = np.linspace(*xlim,100)
    _ys = f(_xs, *coeff)
    #label = 'gaussian fit: A=%6.3f, mu=%6.3f, sigma=%6.3f' % tuple(coeff)
    label = 'gaussian fit'
    plt.plot(_xs,np.log10(_ys),'--',label=label, color='black',lw=1)

    print('gaussian fit: ', *coeff)

if False and ARGV.fit is not None:
    xlim = plt.gca().get_xlim()
    ylim = plt.gca().get_ylim()

    f = gauss
    coeff, var_matrix = scipy.optimize.curve_fit(f, Xs, Ys, p0=[1., 0., 1.])
    xs = np.linspace(*xlim,100)
    ys = f(xs, *coeff)
    #label = 'gaussian fit: A=%6.3f, mu=%6.3f, sigma=%6.3f' % tuple(coeff)
    label = 'gaussian fit'
    plt.plot(xs,np.log10(ys),'--',label=label, color='black')

    f = skew_pdf
    #coeff = [2,1,0.7,-20]
    coeff = [*coeff,1.0]
    coeff, var_matrix = scipy.optimize.curve_fit(f, Xs, Ys, p0=coeff)
    xs = np.linspace(*xlim,100)
    ys = f(xs, *coeff)
    #label = 'pdf-skew fit: A=%6.3f, mu=%6.3f, sigma=%6.3f, alpha=%6.3f' % tuple(coeff)
    label = 'pdf-skew'
    plt.plot(xs,np.log10(ys),'-',label=label, color='black')

## ------------------------------------------------------------------------- #

plt.grid()
plt.legend(
    #loc='upper right'
)

plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)

plt.tight_layout()

if ARGV.output in 'show':
    plt.show()
elif ARGV.output in 'none':
    pass
else:
    plt.savefig(str(ARGV.output), format='png')

max_stars_repo_name: jmark/turbubox

max_issues_repo_name: jmark/turbubox

max_forks_repo_name: jmark/turbubox


text:
# udi dataset process module
# modiflied from nuscenes_dataset.py

import json
import pickle
import time
import random
from copy import deepcopy
from functools import partial
from pathlib import Path
import subprocess

import fire
import numpy as np
import os

from second.core import box_np_ops
from second.core import preprocess as prep
from second.data import kitti_common as kitti
from second.data.dataset import Dataset, register_dataset
from second.utils.eval import get_coco_eval_result, get_official_eval_result
from second.utils.progress_bar import progress_bar_iter as prog_bar
from second.utils.timer import simple_timer

@register_dataset
class UDIDataset(Dataset):
    NumPointFeatures = 4
    NameMapping = {
        'car': 'car',
        'pedestrian': 'pedestrian',
        'cyclist': 'cyclist',
        'truck': 'truck',
        'forklift': 'forklift',
        'golf car': 'golf car',
        'motorcyclist': 'motorcyclist',
        'bicycle': 'bicycle',
        'motorbike': 'motorbike'
    }
    DefaultAttribute = {
        "car": "object_action_parked",
        "pedestrain": "object_action_walking",
        "bicycle": "object_action_driving_straight_forward",
        "motorcycle": "object_action_parked",
        "other_vehicle": "object_action_driving_straight_forward",
        "emergency_vehicle": "object_action_driving_straight_forward",
        "truck": "object_action_parked",
        "animal": "",
        "bus": "object_action_driving_straight_forward",
    }

    def __init__(self,
                 root_path,
                 info_path,
                 class_names=None,
                 prep_func=None,
                 num_point_features=None):
        self._root_path = Path(root_path)
        self._info_path = Path(info_path)
        with open(info_path, 'rb') as f:
            data = pickle.load(f)
        self._udi_infos = data["infos"]
        self._metadata = data["metadata"]
        self._class_names = class_names
        self._prep_func = prep_func

        self.version = self._metadata["version"]
        self._with_velocity = False

    def __len__(self):
        return len(self._udi_infos)

    def __getitem__(self, idx):
        input_dict = self.get_sensor_data(idx)
        example = self._prep_func(input_dict=input_dict)
        example["metadata"] = input_dict["metadata"]
        if "anchors_mask" in example:
            example["anchors_mask"] = example["anchors_mask"].astype(np.uint8)
        return example

    def get_sensor_data(self, query):
        idx = query
        if isinstance(query, dict):
            assert "lidar" in query
            idx = query["lidar"]["idx"]
        info = self._udi_infos[idx]
        res = {
            "lidar": {
                "type": "lidar",
                "points": None,
            },
            "metadata": {
                "token": info["token"]
            },
        }
        lidar_path = Path(info['lidar_path'])

        points = np.fromfile(str(lidar_path), dtype=np.float32).reshape((-1,4))

        points[:, 3] /= 255

        res["lidar"]["points"] = points

        if 'gt_boxes' in info:
            res["lidar"]["annotations"] = {
                'boxes': info["gt_boxes"],
                'names': info["gt_names"]
            }
        return res

    def evaluation_udi(self, detections, output_dir):
        version = self.version
        eval_set_map = {
            # "v1.0-mini": "mini_train",
            "v1.0-trainval": "val",
        }
        # gt_annos = self.ground_truth_annotations
        # if gt_annos is None:
            # return None
        udi_annos = {}
        mapped_class_names = self._class_names
        token2info = {}
        for info in self._udi_infos:
            token2info[info["token"]] = info
        for det in detections:
            annos = []
            boxes = _second_det_to_udi_box(det)
            for i, box in enumerate(boxes):
                name = mapped_class_names[box.label]
                velocity = box.velocity[:2].tolist()
                box.velocity = np.array([*velocity, 0.0])
            for i, box in enumerate(boxes):
                name = mapped_class_names[box.label]
                velocity = box.velocity[:2].tolist()
                nusc_anno = {
                    "sample_token": det["metadata"]["token"],
                    "translation": box.center.tolist(),
                    "size": box.wlh.tolist(),
                    "rotation": box.orientation.elements.tolist(),
                    "velocity": velocity,
                    "detection_name": name,
                    "detection_score": box.score,
                    "attribute_name": "",
                }
                annos.append(nusc_anno)
            udi_annos[det["metadata"]["token"]] = annos
        nusc_submissions = {
            "meta": {
                "use_camera": False,
                "use_lidar": False,
                "use_radar": False,
                "use_map": False,
                "use_external": False,
            },
            "results": udi_annos,
        }
        res_path = Path(output_dir) / "results_udi.json"
        with open(res_path, "w") as f:
            json.dump(nusc_submissions, f)
        eval_main_file = Path(__file__).resolve().parent / "udi_eval.py"
        # why add \"{}\"? to support path with spaces.
        cmd = f"python3 {str(eval_main_file)} --root_path=\"{str(self._root_path)}\""
        cmd += f" --info_path=\"{str(self._info_path)}\""
        cmd += f" --version={self.version}"
        cmd += f" --res_path=\"{str(res_path)}\" --eval_set={eval_set_map[self.version]}"
        cmd += f" --output_dir=\"{output_dir}\""
        # use subprocess can release all nusc memory after evaluation
        subprocess.check_output(cmd, shell=True)
        with open(Path(output_dir) / "metrics_summary.json", "r") as f:
            metrics = json.load(f)
        detail = {}
        res_path.unlink()  # delete results_nusc.json since it's very large
        result = f"Nusc {version} Evaluation\n"
        for name in mapped_class_names:
            detail[name] = {}
            for k, v in metrics["label_aps"][name].items():
                detail[name][f"dist@{k}"] = v
            tp_errs = []
            tp_names = []
            for k, v in metrics["label_tp_errors"][name].items():
                detail[name][k] = v
                tp_errs.append(f"{v:.4f}")
                tp_names.append(k)
            threshs = ', '.join(list(metrics["label_aps"][name].keys()))
            scores = list(metrics["label_aps"][name].values())
            scores = ', '.join([f"{s * 100:.2f}" for s in scores])
            result += f"{name} Nusc dist AP@{threshs} and TP errors\n"
            result += scores
            result += "\n"
            result += ', '.join(tp_names) + ": " + ', '.join(tp_errs)
            result += "\n"
        return {
            "results": {
                "nusc": result
            },
            "detail": {
                "nusc": detail
            },
        }

    def evaluation(self, detections, output_dir):
        res_udi = self.evaluation_udi(detections, output_dir)
        res = {
            "results": {
                "nusc": res_udi["result"]["nusc"],
            },
            "detail": {
                "eval.nusc": res_udi["detail"]["nusc"],
            },
        }
        return res

def _second_det_to_udi_box(detection):
    from udi_eval import Box
    import pyquaternion
    box3d = detection["box3d_lidar"].detach().cpu().numpy()
    scores = detection["scores"].detach().cpu().numpy()
    labels = detection["label_preds"].detach().cpu().numpy()
    box3d[:, 6] = -box3d[:, 6] - np.pi/2
    box_list = []
    for i in range(box3d.shape[0]):
        quat = pyquaternion.Quaternion(axis=[0, 0, 1], radians=box3d[i,6])
        velocity = (np.nan, np.nan, np.nan)
        # if box3d.shape[1] == 9:
        #     velocity = (*box3d[i, 7:9], 0.0)
        box = Box(
            box3d[i, :3],
            box3d[i, 3:6],
            quat,
            label=labels[i],
            score=scores[i],
            velocity=velocity)
        box_list.append(box)
    return box_list

# def _lidar_nusc_box_to_global(info, boxes, classes, eval_version="ICLR 2019"):
#     import pyquaternion
#     box_list = []
#     for box in boxes:
#         box.rotate(pyquaternion.Quaternion(info['lidar2ego_rotation']))
#         box.translate(np.array(info['lidar2ego_translation']))
#         box.rotate(pyquaternion.Quaternion(info['ego2global_rotation']))
#         box.translate(np.array(info['ego2global_translation']))
#         box_list.append(box)
#     return box_list

# def _get_available_scenes(lyft):
#     available_scenes = []
#     print("total scene num:", len(lyft.scene))
#     for scene in lyft.scene:
#         scene_token = scene["token"]
#         scene_rec = lyft.get('scene', scene_token)
#         sample_rec = lyft.get('sample', scene_rec['first_sample_token'])
#         sd_rec = lyft.get('sample_data', sample_rec['data']["LIDAR_TOP"])
#         has_more_frames = True
#         scene_not_exist = False
#         while has_more_frames:
#             lidar_path, boxes, _ = lyft.get_sample_data(sd_rec['token'])
#             if not Path(lidar_path).exists():
#                 scenes_not_exist = True
#                 break
#             else:
#                 break
#             if not sd_rec['next'] == "":
#                 sd_rec = lyft.get('sample_data', sd_rec['next'])
#             else:
#                 has_more_frames = False
#         if scene_not_exist:
#             continue
#         available_scenes.append(scene)
#     print("exist scene num:", len(available_scenes))
#     return available_scenes

def _fill_train_infos(root_path):
    train_udi_infos = []
    lidar_root_path = root_path+ "/lidar"
    label_root_path = root_path + "/label"
    img_root_path = root_path + "/image"

    filenames = os.listdir(lidar_root_path)

    for filename in prog_bar(filenames):

        index = filename.split(".")[0]
        lidar_path = lidar_root_path + "/" + index + ".bin"
        cam_path = img_root_path + "/" + index + ".jpg"
        label_path = label_root_path + "/" + index + "_bin.json"
        assert Path(lidar_path).exists()
        assert Path(cam_path).exists()
        assert Path(label_path).exists()

        with open(label_path, encoding='utf-8') as f:
            res = f.read()
        result = json.loads(res)

        boxes = result["elem"]

        info = {
            "lidar_path": lidar_path,
            "cam_front_path": cam_path,
            "filename": filename,
            "token": int(index),
        }
        gt_locs_list = []
        gt_dims_list = []
        print("label file path:", label_path)
        for box in boxes:
            box_loc = box["position"]
            box_size = box["size"]
            box_loc_ = np.array([box_loc["x"],box_loc["y"], box_loc["z"]], dtype=np.float)
            box_size_ = np.array([box_size["width"],box_size["depth"],box_size["height"]], dtype=np.float)
            box_loc_ = box_loc_.reshape(-1, 3)
            box_size_ = box_size_.reshape(-1, 3)
            gt_locs_list.append(box_loc_)
            gt_dims_list.append(box_size_)

        locs = np.concatenate(gt_locs_list, axis=0)
        dims = np.concatenate(gt_dims_list, axis=0)
        rots = np.array([b["yaw"] for b in boxes]).reshape(-1, 1)
        names = [b["class"] for b in boxes]

        for i in range(len(names)):
            if names[i] in UDIDataset.NameMapping:
                names[i] = UDIDataset.NameMapping[names[i]]
        names = np.array(names)
        # we need to convert rot to SECOND format.
        # change the rot format will break all checkpoint.
        gt_boxes = np.concatenate([locs, dims, -rots - np.pi / 2], axis=1)

        info["gt_boxes"] = gt_boxes
        info["gt_names"] = names
        train_udi_infos.append(info)

    return train_udi_infos

def create_udi_infos(root_path):
    # root_path = Path(root_path)
    root_path = str(root_path)

    train_udi_infos = _fill_train_infos(root_path)
    metadata = {
        "version": "v0.1-train",
    }
    print(
        f"train sample: {len(train_udi_infos)}"
    )
    data = {
        "infos": train_udi_infos,
        "metadata": metadata,
    }
    with open(root_path + "/infos_udi_train.pkl", 'wb') as f:
        pickle.dump(data, f)

def get_box_mean(info_path, class_name="car"):
    with open(info_path, 'rb') as f:
        lyft_infos = pickle.load(f)["infos"]
    gt_boxes_list = []
    for info in lyft_infos:
        gt_boxes = info["gt_boxes"]
        gt_names = info["gt_names"]
        mask = np.array([s == class_name for s in info["gt_names"]], dtype=np.bool_)
        gt_names = gt_names[mask]
        gt_boxes = gt_boxes[mask]
        gt_boxes_list.append(gt_boxes.reshape(-1, 7))
    gt_boxes_list = np.concatenate(gt_boxes_list, axis=0)
    return {
        "box3d": gt_boxes_list.mean(0).tolist(),
        "detail": gt_boxes_list
    }

def get_all_box_mean(info_path):
    det_names = set()
    for k, v in UDIDataset.NameMapping.items():
        if v not in det_names:
            det_names.add(v)
    det_names = sorted(list(det_names))
    res = {}
    details = {}
    for k in det_names:
        result = get_box_mean(info_path, k)
        details[k] = result["detail"]
        res[k] = result["box3d"]
    print(json.dumps(res, indent=2))
    return details

if __name__ == "__main__":
    fire.Fire()


text:
import numpy as np

from typing import Optional
from sklearn.discriminant_analysis import (LinearDiscriminantAnalysis,
                                           QuadraticDiscriminantAnalysis)
from fedot.core.operations.evaluation.\
    operation_implementations.implementation_interfaces import ModelImplementation

class DiscriminantAnalysisImplementation(ModelImplementation):

    def __init__(self, **params: Optional[dict]):
        super().__init__()
        self.params = params
        self.model = None

    def fit(self, train_data):
        """ Method fit model on a dataset

        :param train_data: data to train the model
        """
        self.model.fit(train_data.features, train_data.target)

        return self.model

    def predict(self, input_data, is_fit_chain_stage: Optional[bool] = None):
        """ Method make prediction with labels of classes

        :param input_data: data with features to process
        :param is_fit_chain_stage: is this fit or predict stage for chain
        """
        prediction = self.model.predict(input_data.features)

        prediction = nan_to_num(prediction)

        return prediction

    def predict_proba(self, input_data):
        """ Method make prediction with probabilities of classes

        :param input_data: data with features to process
        """
        prediction = self.model.predict_proba(input_data.features)

        prediction = nan_to_num(prediction)

        return prediction

    def get_params(self):
        """ Method return parameters, which can be optimized for particular
        operation
        """
        return self.model.get_params()

    @property
    def classes_(self):
        return self.model.classes_

class LDAImplementation(DiscriminantAnalysisImplementation):

    def __init__(self, **params: Optional[dict]):
        super().__init__()
        if not params:
            self.model = LinearDiscriminantAnalysis()
        else:
            self.model = LinearDiscriminantAnalysis(**params)
        self.params = params

class QDAImplementation(DiscriminantAnalysisImplementation):

    def __init__(self, **params: Optional[dict]):
        super().__init__()
        if not params:
            self.model = QuadraticDiscriminantAnalysis()
        else:
            self.model = QuadraticDiscriminantAnalysis(**params)
        self.params = params

def nan_to_num(prediction):
    """ Function converts nan values to numerical

    :return prediction: prediction without nans
    """
    if np.array([np.isnan(_) for _ in prediction]).any():
        prediction = np.nan_to_num(prediction)

    return prediction

max_stars_repo_name: Kwentar/FEDOT

max_issues_repo_name: Kwentar/FEDOT

max_forks_repo_name: Kwentar/FEDOT


text:
import numpy as np

def test_get_cell(case_data):
    """
    Test :meth:`.Vertex.get_cell`.

    Parameters
    ----------
    case_data : :class:`.CaseData`
        A test case. Holds the vertex to test and the correct cell.

    Returns
    -------
    None : :class:`NoneType`

    """

    _test_get_cell(case_data.vertex, case_data.cell)

def _test_get_cell(vertex, cell):
    """
    Test :meth:`.Vertex.get_cell`.

    Parameters
    ----------
    vertex : :class:`.Vertex`
        The vertex to test.

    cell : :class:`numpy.ndarray`
        The correct cell.

    Returns
    -------
    None : :class:`NoneType`

    """

    assert np.all(np.equal(vertex.get_cell(), cell))


text:
# -*- coding: utf-8 -*-
from __future__ import absolute_import

import numpy as np
from hypothesis import given, assume
from hypothesis.strategies import characters, text

from eli5.lime.textutils import SplitResult, TokenizedText

def test_split_result():
    s = SplitResult.fromtext("")
    assert list(s.tokens) == []

    s = SplitResult.fromtext("hello")
    assert list(s.tokens) == ["hello"]

    s = SplitResult.fromtext("hello  world!")
    assert list(s.tokens) == ["hello", "world"]
    assert list(s.separators) == ["", "  ", "!"]

@given(text())
def test_split_result_combine(text):
    assume("\x00" not in text)  # fixme

    s = SplitResult.fromtext(text)
    assert s.text == text

    s_copy = s.copy()
    assert (s_copy.parts == s.parts).all()
    assert s_copy.parts is not s.parts

def test_split_result_masked():
    s = SplitResult.fromtext("Hello, world!")
    assert s.masked(np.array([False, False], dtype=bool)).text == s.text
    assert s.masked(np.array([True, False], dtype=bool)).text == ", world!"
    assert s.masked(np.array([False, True], dtype=bool)).text == "Hello, !"
    assert s.masked(np.array([True, True], dtype=bool)).text == ", !"

def test_token_spans():
    s = SplitResult.fromtext("Hello, world!")
    assert s.token_spans == [(0, 5), (7, 12)]


text:
import time
import numpy as np
import torch
import torch.nn as nn
import os
import pickle
import copy

from torch.utils.data import DataLoader
from utils.data_process import transform_trajectory, get_scene_data
from utils.utility import get_lr_scheduler, get_modality, display_performance, get_memory_data
from utils.metrics import final_displacement_error, displacement_error, identity_loss, exp_l2_loss
from utils.checkings import check_classifier_consistency
from models.auxiliary import ObsEncoderTrainer, PredEncoderTrainer
from models.components import PCCSNet

def train_encoder(net, train_loader, val_loader, perf_dict, save_dir_base, prefix, loss_fn, args):
    batch_num = len(train_loader)
    optimizer = torch.optim.Adam(net.parameters(), lr=args.base_lr)
    scheduler = get_lr_scheduler(args.policy, optimizer, max_iter=batch_num * args.epochs)
    dict_key = prefix + "_Encoder"

    start_time = time.time()
    for epoch in range(args.epochs):
        train_loss, val_loss = 0, 0
        val_res = np.zeros(2).astype(float)
        net.train()
        for data in train_loader:
            batch_data, batch_data_gt = data["input"].cuda(), data["future"].cuda()
            optimizer.zero_grad()
            net_output = net(batch_data)
            net_output = transform_trajectory(net_output)
            loss = loss_fn(net_output, batch_data_gt)
            loss.backward()
            optimizer.step()
            scheduler.step()
            train_loss += loss.item()
        train_loss /= batch_num

        net.eval()
        with torch.no_grad():
            for data in val_loader:
                batch_data, batch_data_gt = data["input"].cuda(), data["future"].cuda()
                net_output = net(batch_data)
                net_output = transform_trajectory(net_output)
                loss = loss_fn(net_output, batch_data_gt)
                val_loss += loss.item()
                ADE = displacement_error(net_output, batch_data_gt, mode="sum").item() / args.pred_len
                FDE = final_displacement_error(net_output[:, -1], batch_data_gt[:, -1], mode="sum").item()
                val_res += [ADE, FDE]

        val_loss /= len(val_loader)
        val_res /= len(val_loader.dataset)

        if epoch % args.print_every == 0:
            end_time = time.time()
            print("==> Epoch: %d Train Loss: %.4f Val Loss: %.4f Val ADE: %.4f Val FDE: %.4f Time: %.4f"
                  % (epoch, train_loss, val_loss, val_res[0], val_res[1], end_time - start_time))
            start_time = end_time

        if val_res[int(args.FDE_prioritize)] < perf_dict[dict_key][int(args.FDE_prioritize)]:
            perf_dict[dict_key][0], perf_dict[dict_key][1] = val_res[0], val_res[1]
            torch.save(net.encoder.state_dict(), os.path.join(save_dir_base, dict_key + ".pth"))
            with open(os.path.join(save_dir_base, "Performances.pkl"), "wb") as f:
                pickle.dump(perf_dict, f, 4)
            print("==> Saved")

def train_decoder(net, train_loader, val_loader, perf_dict, save_dir_base, loss_fn, args):
    if not (perf_dict["Obs_Encoder"] == perf_dict["Decoder"][0]
            and perf_dict["Pred_Encoder"] == perf_dict["Decoder"][1]) or args.retrain:
        print("==> Encoders and Decoder Do NOT Match, Retrain Decoder...")
        perf_dict["Decoder"][0] = copy.deepcopy(perf_dict["Obs_Encoder"])
        perf_dict["Decoder"][1] = copy.deepcopy(perf_dict["Pred_Encoder"])
        perf_dict["Decoder"][2] = [1e3, 1e3]

    batch_num = len(train_loader)
    optimizer = torch.optim.Adam(net.decoder.parameters(), lr=args.base_lr)
    scheduler = get_lr_scheduler(args.policy, optimizer, max_iter=batch_num * args.epochs)

    start_time = time.time()
    for epoch in range(args.epochs):
        train_loss, val_loss = 0, 0
        val_res = np.zeros(2).astype(float)

        net.train()
        for data in train_loader:
            batch_obs_encoding, batch_pred_encoding, batch_trj_gt \
                = data["obs_enc"].cuda(), data["pred_enc"].cuda(), data["future"].cuda()
            optimizer.zero_grad()
            net_output = net.decoder([batch_obs_encoding, batch_pred_encoding])  # bs * 12 * 2
            net_output = transform_trajectory(net_output)
            loss = loss_fn(net_output, batch_trj_gt)
            loss.backward()
            optimizer.step()
            scheduler.step()
            train_loss += loss.item()
        train_loss /= batch_num

        net.eval()
        with torch.no_grad():
            for data in val_loader:
                batch_obs_encoding, batch_pred_encoding, batch_trj_gt \
                    = data["obs_enc"].cuda(), data["pred_enc"].cuda(), data["future"].cuda()
                net_output = net.decoder([batch_obs_encoding, batch_pred_encoding])
                net_output = transform_trajectory(net_output)
                loss = loss_fn(net_output, batch_trj_gt)
                val_loss += loss.item()
                ADE = displacement_error(net_output, batch_trj_gt, mode="sum").item() / args.pred_len
                FDE = final_displacement_error(net_output[:, -1], batch_trj_gt[:, -1], mode="sum").item()
                val_res += [ADE, FDE]

        val_loss /= len(val_loader.dataset)
        val_res /= len(val_loader.dataset)

        if epoch % args.print_every == 0:
            end_time = time.time()
            print("==> Epoch: %d Train Loss: %.4f Val Loss: %.4f ADE: %.4f FDE: %.4f Time: %.4f"
                  % (epoch, train_loss, val_loss, val_res[0], val_res[1], end_time - start_time))
            start_time = end_time

        if val_res[int(args.FDE_prioritize)] < perf_dict["Decoder"][2][int(args.FDE_prioritize)]:
            perf_dict["Decoder"][2][0], perf_dict["Decoder"][2][1] = val_res[0], val_res[1]
            torch.save(net.decoder.decoder.state_dict(), os.path.join(save_dir_base, "decoder.pth"))
            torch.save(net.decoder.fc.state_dict(), os.path.join(save_dir_base, "fc.pth"))
            with open(os.path.join(save_dir_base, "Performances.pkl"), "wb") as f:
                pickle.dump(perf_dict, f, 4)
            print("==> Saved")

def train_classifier(net, train_loader, save_dir, args):
    batch_num = len(train_loader)
    optimizer = torch.optim.Adam(net.classifier.parameters(), lr=args.base_lr)
    scheduler = get_lr_scheduler(args.policy, optimizer, max_iter=batch_num * args.epochs)

    for epoch in range(args.epochs):
        t1 = time.time()
        train_loss = 0
        net.train()
        for _, batch_data in enumerate(train_loader):
            batch_features, batch_prob = batch_data["obs_enc"].cuda(), batch_data["prob"].cuda()
            optimizer.zero_grad()
            net_output = net.classifier(batch_features)
            loss = -torch.mean(torch.sum(batch_prob * torch.log(net_output + 1e-8), dim=1))
            loss.backward()
            optimizer.step()
            scheduler.step()
            train_loss += loss.item()
        train_loss /= batch_num

        if epoch % args.print_every == 0:
            t2 = time.time()
            print("==> Epoch: %d Train Loss: %.4f Time: %.4f" % (epoch, train_loss, t2 - t1))

    torch.save(net.classifier.state_dict(), os.path.join(save_dir, "classifier.pth"))

def train_synthesizer(net, train_loader, all_modalities, dataset_name, perf_dict,
                      save_dir_base, args, val_loader):
    if not perf_dict["Decoder"] == perf_dict["Synthesizer"][:3] or args.retrain:
        print("==> Synthesizer Outdated, Retrain...")
        perf_dict["Synthesizer"][:3] = copy.deepcopy(perf_dict["Decoder"])
        perf_dict["Synthesizer"][3] = [1e3, 1e3]

    batch_num = len(train_loader)
    optimizer = torch.optim.Adam(net.synthesizer.parameters(), lr=args.base_lr)
    scheduler = get_lr_scheduler(args.policy, optimizer, max_iter=batch_num * args.epochs)
    for epoch in range(args.epochs):
        train_loss = 0
        t1 = time.time()
        net.train()
        for data in train_loader:
            batch_data, batch_memory_gt, batch_labels = \
                data["obs_enc"].cuda(), data["pred_enc"].cuda(), data["cluster_idx"]
            optimizer.zero_grad()
            pred_encoding = net.synthesizer(batch_data, batch_labels, all_modalities)
            loss = identity_loss(pred_encoding, batch_memory_gt.cuda())
            loss.backward()
            optimizer.step()
            scheduler.step()
            train_loss += loss.item()
        train_loss /= batch_num

        if epoch >= int(0.1 * args.epochs):
            net.eval()
            val_res = evaluate(net, val_loader, all_modalities, dataset_name, args)
            if val_res[int(args.FDE_prioritize)] < perf_dict["Synthesizer"][3][int(args.FDE_prioritize)]:
                perf_dict["Synthesizer"][3][0] = val_res[0]
                perf_dict["Synthesizer"][3][1] = val_res[1]
                torch.save(net.synthesizer.fc1.state_dict(), os.path.join(save_dir_base, "synthesizer_fc1.pth"))
                torch.save(net.synthesizer.fc2.state_dict(), os.path.join(save_dir_base, "synthesizer_fc2.pth"))
                with open(os.path.join(save_dir_base, "Performances.pkl"), "wb") as f:
                    pickle.dump(perf_dict, f, 4)
                print("==> Saved")

        t2 = time.time()
        if epoch % args.print_every == 0:
            print("==> Epoch: %d Train Loss: %.4f Time: %.4f" % (epoch, train_loss, t2 - t1))

def evaluate(net, val_loader, all_modalities, dataset_name, args):
    k_list = args.eval_topK
    assert len(k_list) > 0
    val_res = np.zeros(2).astype(float)
    mode_dict = {
        0: "ADE-prioritized",
        1: "FDE-prioritized",
        2: "Equal-Focus",
    }
    for k in k_list:
        start = time.time()
        val_res = np.zeros(2).astype(float)
        for data in val_loader:
            batch_obs_encoding, batch_pred_gt = data["obs_enc"].cuda(), data["pred"].cuda()
            net_output = net(batch_obs_encoding, k=k, all_modalities=all_modalities)
            net_output = transform_trajectory(net_output)
            assert len(net_output) == k
            ADE = displacement_error(net_output, batch_pred_gt) / args.pred_len
            FDE = final_displacement_error(net_output[:, -1], batch_pred_gt[:, -1])

            if args.eval_mode == 0:
                idx = torch.argmin(ADE)
                min_ADE, min_FDE = ADE[idx].item(), FDE[idx].item()
            elif args.eval_mode == 1:
                idx = torch.argmin(FDE)
                min_ADE, min_FDE = ADE[idx].item(), FDE[idx].item()
            else:
                min_ADE = torch.min(ADE).item()
                min_FDE = torch.min(FDE).item()

            val_res += [min_ADE, min_FDE]
        val_res /= len(val_loader)

        end = time.time()
        print("==> Dataset: %s TopK: %d Mode: %s ADE: %.4f FDE: %.4f Time: %.4f"
              % (dataset_name, k, mode_dict[args.eval_mode], val_res[0], val_res[1], end - start))

    return val_res

def gen_prediction(net, loader, all_modalities, dataset_name, args):
    k_list = args.eval_topK
    assert len(k_list) > 0
    with torch.no_grad():
        for k in k_list:
            start = time.time()
            full_output = []
            for data in loader:
                batch_obs_encoding, mat, last_obs_pos = \
                    data["obs_enc"].cuda(), data["rotate_mat"].cuda(), data["last_obs"].cuda()
                net_output = net(batch_obs_encoding, k=k, all_modalities=all_modalities)
                net_output = transform_trajectory(net_output)
                output = torch.bmm(mat.repeat(k, 1, 1), net_output.transpose(1, 2)).transpose(1, 2)
                output += last_obs_pos.unsqueeze(0)
                full_output.append(output.cpu().numpy())
            full_output = np.stack(full_output)
            end = time.time()
            file_name = "all_output_%s_top%d.npy" % (dataset_name, k)
            print("==> Dataset: " + dataset_name + " TopK: %d Time: %.4f FileName: %s"
                  % (k, end - start, file_name), full_output.shape)
            np.save(file_name, full_output)

def train(train_set, val_set, op_code, dataset_name, args):
    save_dir = os.path.join(args.save_dir, args.data_from, dataset_name)

    if os.path.exists(os.path.join(save_dir, "Performances.pkl")):
        with open(os.path.join(save_dir, "Performances.pkl"), "rb") as f:
            perf_dict = pickle.load(f)
        display_performance(perf_dict)
    else:
        perf_dict = {
            "Obs_Encoder": [1e3, 1e3],
            "Pred_Encoder": [1e3, 1e3],
            "Decoder": [[1e3, 1e3]] * 3,
            "Synthesizer": [[1e3, 1e3]] * 4
        }

    if op_code == 0:
        if args.retrain:
            print("Retrain Obs Encoder")
            perf_dict["Obs_Encoder"] = [1e3, 1e3]

        print("==> Training Obs_Encoder...")
        start_time = time.time()
        net = ObsEncoderTrainer(obs_len=args.obs_len, pre_len=args.pred_len,
                                hidden_size=args.obs_feat_size, num_layer=args.encoder_layer).cuda()
        train_set.set_mode(0)
        val_set.set_mode(0)
        train_loader = DataLoader(train_set, batch_size=args.bs)
        val_loader = DataLoader(val_set, batch_size=args.bs, shuffle=False)

        train_encoder(net, train_loader, val_loader, perf_dict, save_dir, "Obs", loss_fn=nn.MSELoss(), args=args)
        end_time = time.time()
        print("==> Time: %.2f" % (end_time - start_time))

    elif op_code == 1:
        print("==> Training Pred_Encoder...")
        start_time = time.time()
        net = PredEncoderTrainer(obs_len=args.obs_len, pre_len=args.pred_len,
                                 hidden_size=args.pred_feat_size, num_layer=args.encoder_layer).cuda()
        train_set.set_mode(1)
        val_set.set_mode(1)
        train_loader = DataLoader(train_set, batch_size=args.bs)
        val_loader = DataLoader(val_set, batch_size=args.bs, shuffle=False)

        train_encoder(net, train_loader, val_loader,
                      perf_dict, save_dir, "Pred", loss_fn=nn.MSELoss(), args=args)
        end_time = time.time()
        print("==> Time: %.2f" % (end_time - start_time))

    elif op_code in [2, 3, 4, 5]:
        net = PCCSNet(obs_len=args.obs_len, pre_len=args.pred_len, n_cluster=args.n_cluster,
                      obs_hidden_size=args.obs_feat_size, pred_hidden_size=args.pred_feat_size,
                      num_layer=args.encoder_layer).cuda()

        net.load_encoders(save_dir)

        train_set.set_mode(0)
        val_set.set_mode(0)
        train_loader = DataLoader(train_set, batch_size=args.bs, shuffle=False)
        val_loader = DataLoader(val_set, batch_size=args.bs, shuffle=False)

        train_obs_encoding, train_pred_encoding, val_obs_encoding, val_pred_encoding = [], [], [], []
        for data in train_loader:
            batch_data_obs, batch_data_pred = data["input"].cuda(), data["future"].cuda()
            obs_encoding, pred_encoding = net.gen_encoding(batch_data_obs, batch_data_pred)
            train_obs_encoding.append(obs_encoding)
            train_pred_encoding.append(pred_encoding)
        for data in val_loader:
            batch_data_obs, batch_data_pred = data["input"].cuda(), data["future"].cuda()
            obs_encoding, pred_encoding = net.gen_encoding(batch_data_obs, batch_data_pred)
            val_obs_encoding.append(obs_encoding)
            val_pred_encoding.append(pred_encoding)

        train_set.obs_enc = torch.cat(train_obs_encoding, dim=0)
        train_set.pred_enc = torch.cat(train_pred_encoding, dim=0)
        val_set.obs_enc = torch.cat(val_obs_encoding, dim=0)
        val_set.pred_enc = torch.cat(val_pred_encoding, dim=0)

        if op_code == 2:  # Train Decoder
            print("==> Training Decoder...")
            start_time = time.time()
            train_set.set_mode(2)
            val_set.set_mode(2)
            train_loader = DataLoader(train_set, batch_size=args.bs)
            val_loader = DataLoader(val_set, batch_size=args.bs, shuffle=False)

            train_decoder(net, train_loader, val_loader, perf_dict, save_dir, loss_fn=exp_l2_loss, args=args)
            end_time = time.time()
            print("==> Time: %.2f" % (end_time - start_time))

        else:
            memory_data, cluster_result, size_array = get_memory_data(train_set, save_dir, args)
            memory_data = torch.from_numpy(memory_data)
            train_set.cluster_results = torch.from_numpy(cluster_result).long()
            net.load_decoder(save_dir)

            if op_code == 3:  # Train Classifier
                print("==> Training Classifier...")
                start_time = time.time()
                data_size = len(train_set)
                if args.disable_modality_loss:
                    gt_prob = torch.zeros(data_size, args.n_cluster)
                    gt_prob[list(range(data_size)), cluster_result] = 1
                else:
                    gt_prob = get_scene_data(dataset_name, train_set, cluster_result, save_dir, args)
                    gt_prob = torch.from_numpy(gt_prob)

                train_set.classifier_gt = gt_prob
                train_set.set_mode(4)
                train_loader = DataLoader(train_set, batch_size=args.bs)
                train_classifier(net, train_loader, save_dir, args)
                end_time = time.time()
                print("==> Saved. Time: %.2f" % (end_time - start_time))

            else:
                net.load_classifier(save_dir)
                check_classifier_consistency(save_dir, args)

                val_set.set_mode(5)
                val_loader = DataLoader(val_set, batch_size=1, shuffle=False)
                all_modalities = get_modality(memory_data, cluster_result, args.n_cluster).cuda()

                if op_code == 4:  # Train Modality Synthesizer
                    print("==> Training Modality Synthesizer...")
                    start_time = time.time()
                    train_set.set_mode(3)
                    train_loader = DataLoader(train_set, batch_size=args.bs)
                    train_synthesizer(net, train_loader, all_modalities, dataset_name, perf_dict,
                                      save_dir, args, val_loader)
                    end_time = time.time()
                    print("==> Time: %.2f" % (end_time - start_time))

                elif op_code == 5:  # Eval
                    print("==> Evaluation")
                    net.load_synthesizer(save_dir)
                    net.eval()
                    evaluate(net, val_loader, all_modalities, dataset_name, args)

    elif op_code == 6:
        net = PCCSNet(obs_len=args.obs_len, pre_len=args.pred_len, n_cluster=args.n_cluster,
                      obs_hidden_size=args.obs_feat_size, pred_hidden_size=args.pred_feat_size,
                      num_layer=args.encoder_layer).cuda()

        net.load_models(save_dir)
        check_classifier_consistency(save_dir, args)
        val_set.set_mode(0)
        loader = DataLoader(val_set, batch_size=args.bs, shuffle=False)

        obs_encodings = []
        for data in loader:
            batch_data_obs = data["input"].cuda()
            obs_encoding, _ = net.gen_encoding(batch_data_obs, None, for_training=False)
            obs_encodings.append(obs_encoding)
        val_set.obs_enc = torch.cat(obs_encodings, dim=0)

        memory_data, cluster_result, size_array = get_memory_data(None, save_dir, args)
        memory_data = torch.from_numpy(memory_data)
        all_modalities = get_modality(memory_data, cluster_result, args.n_cluster).cuda()

        val_set.set_mode(6)
        loader = DataLoader(val_set, batch_size=1, shuffle=False)
        gen_prediction(net, loader, all_modalities, dataset_name, args)

    else:
        return

max_stars_repo_name: ApeironY/PCCSNet

max_issues_repo_name: ApeironY/PCCSNet

max_forks_repo_name: ApeironY/PCCSNet


text:
from pathlib import Path
import re

import numpy as np

inp = Path('input.txt').read_text()

grid = np.zeros((1000, 1000), np.bool)

for line in inp.splitlines():
    x1, y1, x2, y2 = map(int, re.findall(r'[0-9]+', line))
    if 'on' in line:
        repl = True
    elif 'off' in line:
        repl = False
    else:
        repl = np.invert(grid[x1:x2+1, y1:y2+1])
    grid[x1:x2+1, y1:y2+1] = repl

print(grid.sum())

max_stars_repo_name: BlaXpirit/advent-of-code

max_issues_repo_name: BlaXpirit/advent-of-code

max_forks_repo_name: BlaXpirit/advent-of-code


text:
import sys
sys.path.append('.')

import os
import cv2
import tqdm
import torch
import random
import tempfile
import argparse
import numpy as np
import multiprocessing

from torch.utils.data import DataLoader

from data.aug import ops
from data.dataset import UCAS_AOD
from data.dataset.ucas_aod import NAMES

from model.net import Net
from model.backbone import resnet

from utils.utils import hyp_parse
from utils.box.bbox_np import xy42xywha, xywha2xy4
from utils.box.metric import get_det_aps
from utils.parallel import CustomDetDataParallel

from torch import distributed as dist
from torch.nn import SyncBatchNorm

def set_seed(seed):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)

@torch.no_grad()

def main(args, rank, world_size, res, gts):
    torch.cuda.set_device(rank)
    dist.init_process_group("nccl", init_method='env://', rank=rank, world_size=world_size)

    set_seed(0)
    torch.backends.cudnn.benchmark = True

    backbone = resnet.resnet50

    image_size = 768
    batch_size = 4
    num_workers = 4
    data_dir = 'data/UCAS_AOD'
    dir_save = 'weights/ucas_weight'

    checkpoint = os.path.join(dir_save, args.ckpt)

    aug = ops.Resize(image_size)
    dataset = UCAS_AOD(data_dir, 'test', aug)
    test_sampler = torch.utils.data.distributed.DistributedSampler(dataset, world_size,
     rank)
    batch_sampler = torch.utils.data.BatchSampler(test_sampler, batch_size, drop_last=True)
    loader = DataLoader(dataset, batch_sampler=batch_sampler,\
             num_workers=num_workers, pin_memory=True, collate_fn=dataset.collate)
    num_classes = len(dataset.names)

    prior_box = {
        'strides': [8, 16, 32, 64, 128],
        'sizes': [3] * 5,
        'aspects': [[1, 2]] * 5,
        'scales': [[2 ** 0, 2 ** (1 / 3), 2 ** (2 / 3)]] * 5,
    }
    conf_thresh = 0.01
    nms_thresh = 0.45
    cfg = {
        'prior_box': prior_box,
        'num_classes': num_classes,
        'extra': 2,
        'conf_thresh': conf_thresh,
        'nms_thresh': nms_thresh,
    }

    device = torch.device(f'cuda:{rank}')
    model = Net(backbone(fetch_feature=True), cfg)
    model.build_pipe(shape=[2, 3, image_size, image_size])
    model = SyncBatchNorm.convert_sync_batchnorm(model)
    model.to(device)
    model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[rank])
    model.module.load_state_dict(torch.load(checkpoint, map_location=device))

    model.eval()

    count = 0
    gt_list, det_list = [], []
    tqdm_loader = tqdm.tqdm(loader) if rank == 0 else loader
    for images, targets, infos in tqdm_loader:
        images = images.cuda() / 255
        dets = model(images)
        for target, det, info in zip(targets, dets, infos):
            if target:
                bboxes = np.stack([xy42xywha(bbox) for bbox in info['objs']['bboxes']])
                labels = info['objs']['labels']
                gt_list.extend([count, bbox, 1, label] for bbox, label in zip(bboxes, labels))
            if det:
                ih, iw = info['shape'][:2]
                bboxes, scores, labels = list(map(lambda x: x.cpu().numpy(), det))
                bboxes = np.stack([xywha2xy4(bbox) for bbox in bboxes])
                bboxes_ = bboxes * [iw / image_size, ih / image_size]
                # bboxes = np.stack([xy42xywha(bbox) for bbox in bboxes_])
                bboxes = []
                for bbox in bboxes_.astype(np.float32):
                    (x, y), (w, h), a = cv2.minAreaRect(bbox)
                    bboxes.append([x, y, w, h, a])
                bboxes = np.array(bboxes)
                det_list.extend([count, bbox, score, label] for bbox, score, label in zip(bboxes, scores, labels))
            count += 1

    res.extend(det_list)
    gts.extend(gt_list)

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Distributed evaluation for UCAS_AOD dataset...')
    parser.add_argument('--gpus', help='num of gpus')
    parser.add_argument('--ckpt', help='checkpoint')
    parser.add_argument('--use_voc07', help='voc07 or voc10 metric')
    parser.add_argument('--local_rank', default=-1, type=int, help='node rank for distributed training')
    args = parser.parse_args()

    multiprocessing.set_start_method('spawn')

    if ',' in args.gpus:
        device_ids = [eval(x) for x in args.gpus.split(',') if len(x)!=0]
        os.environ["CUDA_VISIBLE_DEVICES"] = ','.join([str(id) for id in device_ids])
        device_ids = list(range(len(device_ids)))
    else:
        device_ids = [x for x in range(int(eval(args.gpus)))]

    res = multiprocessing.Manager().list()
    gts = multiprocessing.Manager().list()

    processes = []
    for device_id in device_ids:
        p = multiprocessing.Process(target=main, args=(args, device_id, len(device_ids), res, gts))
        p.start()
        processes.append(p)
    for p in processes:
        p.join()

    num_classes = len(NAMES)
    APs = get_det_aps(res, gts, num_classes, use_07_metric=args.use_voc07)
    mAP = sum(APs) / len(APs)
    print('AP')
    for name, ap in zip(NAMES, APs):
        print(f'{name}: {ap}')
    print(f'mAP: {mAP}')