text:
from torchvision import datasets, models, transforms
import torch.nn.functional as F
import torch
import torch.nn as nn
import torch.nn.init as init
from torch.autograd import Function
import pdb
import math
import numpy as np

model_urls = {
    'resnet18': 'https://s3.amazonaws.com/pytorch/models/resnet18-5c106cde.pth',
    'resnet34': 'https://s3.amazonaws.com/pytorch/models/resnet34-333f7ec4.pth',
    'resnet50': 'https://s3.amazonaws.com/pytorch/models/resnet50-19c8e357.pth',
    'resnet101': 'https://s3.amazonaws.com/pytorch/models/resnet101-5d3b4d8f.pth',
    'resnet152': 'https://s3.amazonaws.com/pytorch/models/resnet152-b121ed2d.pth',
}

def calc_coeff(iter_num, high=1.0, low=0.0, alpha=10.0, max_iter=10000.0):
    return np.float(2.0 * (high - low) / (1.0 + np.exp(-alpha*iter_num / max_iter)) - (high - low) + low)

def grl_hook(coeff):
    def fun1(grad):
        return -coeff*grad.clone()
    return fun1

def init_weights(m):
    classname = m.__class__.__name__
    if classname.find('Conv2d') != -1 or classname.find('ConvTranspose2d') != -1:
        nn.init.kaiming_uniform_(m.weight)
        #nn.init.zeros_(m.bias)
    elif classname.find('BatchNorm') != -1:
        nn.init.normal_(m.weight, 1.0, 0.02)
        #nn.init.zeros_(m.bias)
    elif classname.find('Linear') != -1:
        nn.init.xavier_normal_(m.weight)
        #nn.init.zeros_(m.bias)

class RandomLayer(nn.Module):
    def __init__(self, input_dim_list=[], output_dim=1024):
        super(RandomLayer, self).__init__()
        self.input_num = len(input_dim_list)
        self.output_dim = output_dim
        self.random_matrix = [torch.randn(input_dim_list[i], output_dim) for i in range(self.input_num)]

    def forward(self, input_list):
        return_list = [torch.mm(input_list[i], self.random_matrix[i]) for i in range(self.input_num)]
        return_tensor = return_list[0] / math.pow(float(self.output_dim), 1.0/len(return_list))
        for single in return_list[1:]:
            return_tensor = torch.mul(return_tensor, single)
        return return_tensor

    def cuda(self):
        super(RandomLayer, self).cuda()
        self.random_matrix = [val.cuda() for val in self.random_matrix]

class AdversarialNetwork(nn.Module):
  def __init__(self, in_feature, hidden_size):
    super(AdversarialNetwork, self).__init__()
    self.ad_layer1 = nn.Linear(in_feature, hidden_size)
    self.ad_layer2 = nn.Linear(hidden_size, hidden_size)
    self.ad_layer3 = nn.Linear(hidden_size, 1)
    self.relu1 = nn.ReLU()
    self.relu2 = nn.ReLU()
    self.dropout1 = nn.Dropout(0.5)
    self.dropout2 = nn.Dropout(0.5)
    self.sigmoid = nn.Sigmoid()
    self.apply(init_weights)
    self.iter_num = 0
    self.alpha = 10
    self.low = 0.0
    self.high = 1.0
    self.max_iter = 10000.0

  def forward(self, x):
    if self.training:
        self.iter_num += 1
    coeff = calc_coeff(self.iter_num, self.high, self.low, self.alpha, self.max_iter)
    x = x * 1.0
    x.register_hook(grl_hook(coeff))
    x = self.ad_layer1(x)
    x = self.relu1(x)
    x = self.dropout1(x)
    x = self.ad_layer2(x)
    x = self.relu2(x)
    x = self.dropout2(x)
    y = self.ad_layer3(x)
    y = self.sigmoid(y)
    return y

  def output_num(self):
    return 1
  def get_parameters(self):
    return [{"params":self.parameters(), "lr_mult":10, 'decay_mult':2}]

def loss_Entropy(input_):
    bs = input_.size(0)
    epsilon = 1e-5
    entropy = -input_ * torch.log(input_ + epsilon)
    entropy = torch.sum(entropy, dim=1)
    return entropy

def loss_CDAN(input_list, ad_net, entropy=None, coeff=None, random_layer=None):
    softmax_output = input_list[1].detach()
    feature = input_list[0]
    if random_layer is None:
        op_out = torch.bmm(softmax_output.unsqueeze(2), feature.unsqueeze(1))
        ad_out = ad_net(op_out.view(-1, softmax_output.size(1) * feature.size(1)))
    else:
        random_out = random_layer.forward([feature, softmax_output])
        ad_out = ad_net(random_out.view(-1, random_out.size(1)))
    batch_size = softmax_output.size(0) // 2
    dc_target = torch.from_numpy(np.array([[1]] * batch_size + [[0]] * batch_size)).float().cuda()
    if entropy is not None:
        entropy.register_hook(grl_hook(coeff))
        entropy = 1.0+torch.exp(-entropy)
        source_mask = torch.ones_like(entropy)
        source_mask[feature.size(0)//2:] = 0
        source_weight = entropy*source_mask
        target_mask = torch.ones_like(entropy)
        target_mask[0:feature.size(0)//2] = 0
        target_weight = entropy*target_mask
        weight = source_weight / torch.sum(source_weight).detach().item() + \
                 target_weight / torch.sum(target_weight).detach().item()
        return torch.sum(weight.view(-1, 1) * nn.BCELoss(reduce=False)(ad_out, dc_target)) / torch.sum(weight).detach().item()
    else:
        return nn.BCELoss()(ad_out, dc_target)

def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        m.weight.data.normal_(0.0, 0.1)
    elif classname.find('Linear') != -1:
        nn.init.xavier_normal_(m.weight)
        nn.init.zeros_(m.bias)
    elif classname.find('BatchNorm') != -1:
        m.weight.data.normal_(1.0, 0.1)
        m.bias.data.fill_(0)

class GradReverse(Function):
    @staticmethod
    def forward(ctx, x, lambd):
        ctx.lambd = lambd
        return x.view_as(x)

    @staticmethod
    def backward(ctx, grad_output):
        return (grad_output * -ctx.lambd), None

def grad_reverse(x, lambd=1.0):
    return GradReverse.apply(x, lambd)

class GradMulti(Function):
    def __init__(self, lambd):
        self.lambd = lambd

    def forward(self, x):
        return x.view_as(x)

    def backward(self, grad_output):
        return (grad_output * self.lambd)

def grad_multi(x, lambd=1.0):
    return GradMulti(lambd)(x)
def l2_norm(input):
    input_size = input.size()
    buffer = torch.pow(input, 2)

    normp = torch.sum(buffer, 1).add_(1e-10)
    norm = torch.sqrt(normp)

    _output = torch.div(input, norm.view(-1, 1).expand_as(input))

    output = _output.view(input_size)

    return output

def conv3x3(in_planes, out_planes, stride=1, sn=False):
    "3x3 convolution with padding"
    if not sn:
        return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                         padding=1, bias=True)
    else:
        return SNConv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                        padding=1, bias=True)

def conv1x1(in_planes, out_planes, stride=1):
    "1x1 convolution no padding"
    return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride,
                     padding=0, bias=False)

class L2Norm(nn.Module):
    def __init__(self, n_channels, scale):
        super(L2Norm, self).__init__()
        self.n_channels = n_channels
        self.gamma = scale or None
        self.eps = 1e-10
        self.weight = nn.Parameter(torch.Tensor(self.n_channels))
        self.reset_parameters()

    def reset_parameters(self):
        init.constant(self.weight, self.gamma)

    def forward(self, x):
        norm = x.pow(2).sum(1).sqrt() + self.eps
        x /= norm.expand_as(x)
        out = self.weight.unsqueeze(0).expand_as(x) * x
        return out

class AlexNetBase(nn.Module):
    def __init__(self,pret=True):
        super(AlexNetBase, self).__init__()
        model_alexnet = models.alexnet(pretrained=pret)
        #print(model_alexnet.features)
        self.conv1 = model_alexnet.features[0]
        self.relu1 = model_alexnet.features[1]
        self.pool1 = model_alexnet.features[2]
        self.conv2 = model_alexnet.features[3]
        self.relu2 = model_alexnet.features[4]
        self.pool2 = model_alexnet.features[5]
        self.conv3 = model_alexnet.features[6]
        self.relu3 = model_alexnet.features[7]
        self.conv4 = model_alexnet.features[8]
        self.relu4 = model_alexnet.features[9]
        self.conv5 = model_alexnet.features[10]
        self.relu5 = model_alexnet.features[11]
        self.pool3 = model_alexnet.features[12]

        self.feature1 = nn.Sequential(*list(model_alexnet.features._modules.values())[:6])
        self.feature2 = nn.Sequential(*list(model_alexnet.features._modules.values())[6:])

        #self.features = model_alexnet.features
        self.classifier = nn.Sequential()
        for i in range(6):
            self.classifier.add_module("classifier" + str(i), model_alexnet.classifier[i])
        self.__in_features = model_alexnet.classifier[6].in_features

    def forward(self, x, target=False, lamda=0.1):
        x = self.conv1(x)
        x = self.relu1(x)
        x = self.pool1(x)
        if target:
            x = grad_multi(x,lamda)
        x = self.conv2(x)
        x = self.relu2(x)
        x = self.pool2(x)
        if target:
            x = grad_multi(x, lamda)
        x = self.conv3(x)
        x = self.relu3(x)
        if target:
            x = grad_multi(x, lamda)
        x = self.conv4(x)
        x = self.relu4(x)
        if target:
            x = grad_multi(x, lamda)
        x = self.conv5(x)
        x = self.relu5(x)
        x = self.pool3(x)
        #x = self.feature1(x)
        #x = self.feature2(x)
        feature = x
        x = x.view(x.size(0), 256 * 6 * 6)
        #x = F.normalize(x)
        x = self.classifier(x)
        # x = F.relu(self.fc1(x))
        # x = F.relu(self.fc2(x))
        return x

    def output_num(self):
        return self.__in_features

class AlexNetBase_selfsup(nn.Module):
    def __init__(self,path=False):
        super(AlexNetBase_selfsup, self).__init__()

        if path:
            checkpoint = torch.load(path)
            pretrained_dict = checkpoint['net']
            # lemniscate = checkpoint['lemniscate']
            # best_acc = checkpoint['acc']
            # start_epoch = checkpoint['epoch']
            from collections import OrderedDict
            new_state_dict = OrderedDict()
            for k, v in pretrained_dict.items():
                name = k[7:]  # remove `module.`
                new_state_dict[name] = v

            model_alexnet = models.alexnet(pretrained=True)

            model_dict = model_alexnet.state_dict()
            # # 1. filter out unnecessary keys
            new_state_dict = {k: v for k, v in new_state_dict.items() if k in model_dict}
            # # 2. overwrite entries in the existing state dict
            model_dict.update(new_state_dict)
            # # 3. load the new state dict
            model_alexnet.load_state_dict(model_dict)

        else:
            model_alexnet = models.alexnet(pretrained=True)

        #print(model_alexnet.features)
        self.conv1 = model_alexnet.features[0]
        self.relu1 = model_alexnet.features[1]
        self.pool1 = model_alexnet.features[2]
        self.conv2 = model_alexnet.features[3]
        self.relu2 = model_alexnet.features[4]
        self.pool2 = model_alexnet.features[5]
        self.conv3 = model_alexnet.features[6]
        self.relu3 = model_alexnet.features[7]
        self.conv4 = model_alexnet.features[8]
        self.relu4 = model_alexnet.features[9]
        self.conv5 = model_alexnet.features[10]
        self.relu5 = model_alexnet.features[11]
        self.pool3 = model_alexnet.features[12]

        self.feature1 = nn.Sequential(*list(model_alexnet.features._modules.values())[:6])
        self.feature2 = nn.Sequential(*list(model_alexnet.features._modules.values())[6:])

        #self.features = model_alexnet.features
        self.classifier = nn.Sequential()
        for i in range(6):
            self.classifier.add_module("classifier" + str(i), model_alexnet.classifier[i])
        self.__in_features = model_alexnet.classifier[6].in_features

    def forward(self, x, target=False, lamda=0.1):
        x = self.conv1(x)
        x = self.relu1(x)
        x = self.pool1(x)
        if target:
            x = grad_multi(x,lamda)
        x = self.conv2(x)
        x = self.relu2(x)
        x = self.pool2(x)
        if target:
            x = grad_multi(x, lamda)
        x = self.conv3(x)
        x = self.relu3(x)
        if target:
            x = grad_multi(x, lamda)
        x = self.conv4(x)
        x = self.relu4(x)
        if target:
            x = grad_multi(x, lamda)
        x = self.conv5(x)
        x = self.relu5(x)
        x = self.pool3(x)
        #x = self.feature1(x)
        #x = self.feature2(x)
        feature = x
        x = x.view(x.size(0), 256 * 6 * 6)
        #x = F.normalize(x)
        x = self.classifier(x)
        # x = F.relu(self.fc1(x))
        # x = F.relu(self.fc2(x))
        return x

    def output_num(self):
        return self.__in_features

class VGGBase(nn.Module):
    def __init__(self, option='vgg', pret=True, no_pool=False):
        super(VGGBase, self).__init__()
        self.dim = 2048
        self.no_pool = no_pool
        if option =='vgg_bn':
            vgg16=models.vgg11_bn(pretrained=pret)
        else:
            vgg16 = models.vgg16(pretrained=pret)
        self.classifier = nn.Sequential(*list(vgg16.classifier._modules.values())[:-1])
        self.features = nn.Sequential(*list(vgg16.features._modules.values())[:])
        self.s = nn.Parameter(torch.FloatTensor([10]))

    def forward(self, x, source=True,target=False):
        x = self.features(x)
        x = x.view(x.size(0), 7 * 7 * 512)
        x = self.classifier(x)

        return x

class Predictor(nn.Module):
    def __init__(self, num_class=64, inc=4096, temp=0.1,
                 cosine=False,dropout=False):
        super(Predictor, self).__init__()
        if cosine:
            self.fc = nn.Linear(inc, num_class, bias=False)
        else:
            self.fc = nn.Linear(inc, num_class,bias=True)
        self.num_class = num_class
        self.temp = temp
        self.cosine = cosine
        self.dropout = dropout
    def forward(self, x, reverse=False,eta=0.1):
        if reverse:
            x = grad_reverse(x,eta)
        if self.dropout:
            x = F.dropout(x,training=self.training,p=0.1)
        if not self.cosine:
            x_out = self.fc(x)
            return x_out
        else:
            x = F.normalize(x)
            x_out = self.fc(x) / self.temp
            return x_out

    def weight_norm(self):
        w = self.fc.weight.data
        norm = w.norm(p=2, dim=1, keepdim=True)
        self.fc.weight.data = w.div(norm.expand_as(w))

class Predictor_deep(nn.Module):
    def __init__(self, num_class=64, inc=4096, temp=0.1,dropout=False):
        super(Predictor_deep, self).__init__()
        self.fc1 = nn.Linear(inc, 512)
        self.fc2 = nn.Linear(512, num_class)
        self.num_class = num_class
        self.temp = temp
        self.dropout = dropout
    def forward(self, x, reverse=False,eta=0.1):

        x = self.fc1(x)
        if reverse:
            x = grad_reverse(x, eta)
        x = F.normalize(x)
        if self.dropout:
            x = F.dropout(x,training=self.training,p=0.1)
        x_out = self.fc2(x)/0.05
        return x_out

class Predictor_single(nn.Module):
    def __init__(self, num_class=64, low_dim=4096, temp=0.1,dropout=False):
        super(Predictor_single, self).__init__()

        self.fc2 = nn.Linear(low_dim, num_class)
        self.num_class = num_class
        self.temp = temp
        self.dropout = dropout

    def forward(self, x, reverse=False,eta=0.1):

        if reverse:
            x = grad_reverse(x, eta)
        # x = F.normalize(x)
        if self.dropout:
            x = F.dropout(x,training=self.training, p=0.1)
        x_out = self.fc2(x)/0.05
        return x_out

class Predictor_deep_2(nn.Module):
    def __init__(self, num_class=64, inc=4096, low_dim=512,temp=0.1,dropout=False, path=False):
        super(Predictor_deep_2, self).__init__()
        self.fc = nn.Linear(inc, low_dim)
        self.fc2 = nn.Linear(low_dim, num_class)
        self.num_class = num_class
        self.temp = temp
        self.dropout = dropout

        weights_init(self)
        if path:
            import torch
            checkpoint = torch.load(path)
            pretrained_dict = checkpoint['net']
            # lemniscate = checkpoint['lemniscate']
            # best_acc = checkpoint['acc']
            # start_epoch = checkpoint['epoch']
            from collections import OrderedDict
            new_state_dict = OrderedDict()
            for k, v in pretrained_dict.items():
                name = k[7:]  # remove `module.`
                new_state_dict[name] = v

            model_dict = self.state_dict()
            # 1. filter out unnecessary keys
            new_state_dict = {k: v for k, v in new_state_dict.items() if k in model_dict}
            # 2. overwrite entries in the existing state dict
            model_dict.update(new_state_dict)
            # 3. load the new state dict
            self.load_state_dict(model_dict)

    def forward(self, x, reverse=False,eta=0.1):

        x = self.fc(x)
        if reverse:
            x = grad_reverse(x, eta)
        x = F.normalize(x)
        if self.dropout:
            x = F.dropout(x,training=self.training,p=0.1)
        x_out = self.fc2(x)/0.05
        return x_out

    def forward_2(self, x, reverse=False,eta=0.1):

        x_feat = self.fc(x)
        if reverse:
            x = grad_reverse(x, eta)
        x_out = F.normalize(x_feat)

        if self.dropout:
            x = F.dropout(x,training=self.training,p=0.1)
        x_out = self.fc2(x_out)/0.05
        return x_out, x_feat

class Predictor_deep_id(nn.Module):
    def __init__(self, num_class=64, inc=4096, low_dim=512,temp=0.1,dropout=False, path=False, normalize=False):
        super(Predictor_deep_id, self).__init__()
        self.fc2 = nn.Linear(low_dim, num_class)
        self.num_class = num_class
        self.temp = temp
        self.dropout = dropout
        self.normalize =  normalize
        weights_init(self)

    def forward(self, x, reverse=False,eta=0.1):
        if reverse:
            x = grad_reverse(x, eta)
        # if self.normalize:
        #     x = F.normalize(x)
        if self.dropout:
            x = F.dropout(x,training=self.training,p=0.1)
        x_out = self.fc2(x)/0.05
        return x_out

    def forward_2(self, x, reverse=False,eta=0.1):

        x_feat = self.fc(x)
        if reverse:
            x = grad_reverse(x, eta)
        x_out = F.normalize(x_feat)

        if self.dropout:
            x = F.dropout(x,training=self.training,p=0.1)
        x_out = self.fc2(x_out)/0.05
        return x_out, x_feat

class Predictor_deep_id_2(nn.Module):
    def __init__(self, num_class=64, inc=4096, low_dim=512,temp=0.1,dropout=False, path=False, normalize=False):
        super(Predictor_deep_id_2, self).__init__()
        self.fc2 = nn.Linear(low_dim, num_class)
        self.num_class = num_class
        self.temp = temp
        self.dropout = dropout
        self.normalize =  normalize
        weights_init(self)

    def forward(self, x, reverse=False,eta=0.1):
        if reverse:
            x = grad_reverse(x, eta)
        # if self.normalize:
        #     x = F.normalize(x)
        if self.dropout:
            x = F.dropout(x,training=self.training,p=0.1)
        x_out = self.fc2(x)
        return x_out

class Discriminator(nn.Module):
    def __init__(self, inc=4096):
        super(Discriminator, self).__init__()
        self.fc1_1 = nn.Linear(inc, 512)
        self.fc2_1 = nn.Linear(512, 512)
        self.fc3_1 = nn.Linear(512, 1)
    def forward(self, x, reverse=True, eta=1.0):
        if reverse:
            x = grad_reverse(x,eta)
        x = F.relu(self.fc1_1(x))
        x = F.relu(self.fc2_1(x))
        x_out = F.sigmoid(self.fc3_1(x))
        return x_out

class Discriminator_2(nn.Module):
    def __init__(self, inc=4096):
        super(Discriminator_2, self).__init__()
        self.fc1_1 = nn.Linear(inc, 256)
        self.fc2_1 = nn.Linear(256, 128)
        self.fc3_1 = nn.Linear(128, 1)
    def forward(self, x, reverse=True, eta=1.0):
        if reverse:
            x = grad_reverse(x,eta)
        x = F.relu(self.fc1_1(x))
        x = F.relu(self.fc2_1(x))
        x_out = F.sigmoid(self.fc3_1(x))
        return x_out

max_stars_repo_name: VisionLearningGroup/CDS

max_issues_repo_name: VisionLearningGroup/CDS

max_forks_repo_name: VisionLearningGroup/CDS


text:
from setuptools import setup
from Cython.Build import cythonize
import numpy as np

setup(
    name='c_nms',
    ext_modules=cythonize("c_nms.pyx"),
    zip_safe=False,
    include_dirs=[np.get_include()]
)

ext: py

reason: import numpy


text:
"""
Demonstration of Earnshaw's theorem

Ahmed Al-kharusi
Please check the simulation yourself. You may find mistakes!
See the references

"""

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#The moving charge
position_velocity_xi = np.array([0, 0])
position_velocity_yi = np.array([-0.01, 0])
position_velocity_zi = np.array([0.01,0])

#The fixed charges
CHARGE1_POSITION = np.array([1, 1, 1])
CHARGE2_POSITION = np.array([1, 1, -1])
CHARGE3_POSITION = np.array([1, -1, 1])
CHARGE4_POSITION = np.array([-1, 1, 1])
CHARGE5_POSITION = np.array([1, -1, -1])
CHARGE6_POSITION = np.array([-1, -1, 1])
CHARGE7_POSITION = np.array([-1, 1, -1])
CHARGE8_POSITION = np.array([-1, -1, -1])

#Coulomb constant arb. units
k = 1
# set all charges =+1 and masses =1

#consider removing place holders for more efficient codes
def acceleration(position_velocity, axis , position_velocity_x, position_velocity_y,position_velocity_z):
    d1 = (position_velocity[0]-CHARGE1_POSITION[axis])
    d2 = (position_velocity[0]-CHARGE2_POSITION[axis])
    d3 = (position_velocity[0]-CHARGE3_POSITION[axis])
    d4 = (position_velocity[0]-CHARGE4_POSITION[axis])
    d5 = (position_velocity[0]-CHARGE5_POSITION[axis])
    d6 = (position_velocity[0]-CHARGE6_POSITION[axis])
    d7 = (position_velocity[0]-CHARGE7_POSITION[axis])
    d8 = (position_velocity[0]-CHARGE8_POSITION[axis])

    r1 = np.linalg.norm(np.array([CHARGE1_POSITION[0]-position_velocity_x[0], CHARGE1_POSITION[1]-position_velocity_y[0], CHARGE1_POSITION[2]-position_velocity_z[0]]))
    r2 = np.linalg.norm(np.array([CHARGE2_POSITION[0]-position_velocity_x[0], CHARGE2_POSITION[1]-position_velocity_y[0], CHARGE2_POSITION[2]-position_velocity_z[0]]))
    r3 = np.linalg.norm(np.array([CHARGE3_POSITION[0]-position_velocity_x[0], CHARGE3_POSITION[1]-position_velocity_y[0], CHARGE3_POSITION[2]-position_velocity_z[0]]))
    r4 = np.linalg.norm(np.array([CHARGE4_POSITION[0]-position_velocity_x[0], CHARGE4_POSITION[1]-position_velocity_y[0], CHARGE4_POSITION[2]-position_velocity_z[0]]))
    r5 = np.linalg.norm(np.array([CHARGE5_POSITION[0]-position_velocity_x[0], CHARGE5_POSITION[1]-position_velocity_y[0], CHARGE5_POSITION[2]-position_velocity_z[0]]))
    r6 = np.linalg.norm(np.array([CHARGE6_POSITION[0]-position_velocity_x[0], CHARGE6_POSITION[1]-position_velocity_y[0], CHARGE6_POSITION[2]-position_velocity_z[0]]))
    r7 = np.linalg.norm(np.array([CHARGE7_POSITION[0]-position_velocity_x[0], CHARGE7_POSITION[1]-position_velocity_y[0], CHARGE7_POSITION[2]-position_velocity_z[0]]))
    r8 = np.linalg.norm(np.array([CHARGE8_POSITION[0]-position_velocity_x[0], CHARGE8_POSITION[1]-position_velocity_y[0], CHARGE8_POSITION[2]-position_velocity_z[0]]))

    return k*(d1/r1**3+d2/r2**3+d3/r3**3+d4/r4**3+d5/r5**3+d6/r6**3+d7/r7**3+d8/r8**3)

def derivatives_x(position_velocity_x, t, x,y,z):
    return np.array([position_velocity_x[1], acceleration(position_velocity_x, 0, x,y,z) ])

def derivatives_y(position_velocity_y, t, x,y,z):
    return np.array([position_velocity_y[1], acceleration(position_velocity_y, 1, x,y,z)])
                                    #place_holder so that it can be used with rk4
def derivatives_z(position_velocity_z, t, x,y,z):
    return np.array([position_velocity_z[1], acceleration(position_velocity_z, 2, x,y,z)])

#taken from... (see references)
def rk4(y,dy,t,h, x,yy,z):
    k1=dy(y,t, x,yy,z)
    k2=dy(y+h/2*k1,t+h/2, x,yy,z)
    k3=dy(y+h/2*k2,t+h/2, x,yy,z)
    k4=dy(y+h*k3,t+h, x,yy,z)
    y=y+h*(k1+2*k2+2*k3+k4)/6
    #t=t+h
    return y

def implement_rk4(position_velocity_x, position_velocity_y, position_velocity_z, t, h, steps_no):
    global time_arr
    time_arr = np.array([t])
    data_x = np.array([position_velocity_x])
    data_y = np.array([position_velocity_y])
    data_z = np.array([position_velocity_z])

    for i in range(steps_no):
        tempx = position_velocity_x
        tempy = position_velocity_y

        position_velocity_x = rk4( position_velocity_x, derivatives_x, t, h ,position_velocity_x, position_velocity_y, position_velocity_z)
        position_velocity_y = rk4( position_velocity_y, derivatives_y,  t,h, tempx, position_velocity_y, position_velocity_z)
        position_velocity_z = rk4( position_velocity_z, derivatives_z,  t,h, tempx, tempy, position_velocity_z)
        t +=h

        #time_arr = np.append(time_arr, t)
        data_x = np.vstack((data_x, position_velocity_x))
        data_y = np.vstack((data_y, position_velocity_y))
        data_z = np.vstack((data_z, position_velocity_z))

        time_arr = np.append(time_arr, t)
        #print (star2_arr_x," ", star1_arr_x)
    [x, vx] = data_x.transpose()
    [y, vy] = data_y.transpose()
    [z, zy] = data_z.transpose()

    return [x ,y, z] # can also return vx, vy, vz for energies and othe info

t = 0 # starting time
h = 1/(30) # step size for the RK4 method
steps_no = 40000 # number of steps of the RK4 method

[x, y, z] = implement_rk4(position_velocity_xi, position_velocity_yi, position_velocity_zi, t, h, steps_no)

save_every_n_frames = 30

(ymin, ymax) = (-1.1, 1.1)
(xmin, xmax) = (-1.1, 1.1)
(zmin, zmax) = (-1.1, 1.1)

for j in range(int(len(x)/save_every_n_frames)-1):
    i = j*save_every_n_frames
    fig = plt.figure(figsize=(10,10))

    ax = fig.gca(projection='3d', autoscale_on=False,xlim=(xmin,xmax), ylim=(ymin,ymax), zlim=(zmin,zmax))

    ax.scatter(x[0:i], y[0:i], z[0:i], s=2, c='black', alpha=0.9)

    ax.scatter(x[i], y[i], z[i], s=200, c='c', label='Free charge')

    ax.set_title("Free charge position:("+str(round(x[i],2))+", "+str(round(y[i],2))+", "+str(round(z[i],2))+")\n Add a small offset"#'Add a small offset\n'
                 , c='blue',fontsize=14,loc='left' )

    ax.scatter(CHARGE1_POSITION[0], CHARGE1_POSITION[1], CHARGE1_POSITION[2] , s=200, c='r', label="Fixed charges\nat cube vertices")
    ax.scatter(CHARGE2_POSITION[0], CHARGE2_POSITION[1], CHARGE2_POSITION[2] , s=200, c='r')
    ax.scatter(CHARGE3_POSITION[0], CHARGE3_POSITION[1], CHARGE3_POSITION[2] , s=200, c='r')
    ax.scatter(CHARGE4_POSITION[0], CHARGE4_POSITION[1], CHARGE4_POSITION[2] , s=200, c='r')
    ax.scatter(CHARGE5_POSITION[0], CHARGE5_POSITION[1], CHARGE5_POSITION[2] , s=200, c='r')
    ax.scatter(CHARGE6_POSITION[0], CHARGE6_POSITION[1], CHARGE6_POSITION[2] , s=200, c='r')
    ax.scatter(CHARGE7_POSITION[0], CHARGE7_POSITION[1], CHARGE7_POSITION[2] , s=200, c='r')
    ax.scatter(CHARGE8_POSITION[0], CHARGE8_POSITION[1], CHARGE8_POSITION[2] , s=200, c='r')

    ax.set_xlabel('$x$', fontsize=30)
    ax.set_ylabel('$y$', fontsize=30)
    ax.set_zlabel('$z$', fontsize=30)
    ax.legend(fontsize=18)
    plt.tight_layout()
    fig.savefig(str(j)+'.png',dpi=110)
    plt.show()
    plt.close()

"""
References:
#The rk4 function is taken from
https://youtu.be/HPreOWKJOiY
"""

max_stars_repo_name: Ahmed-alkharusi/Ion-trap-simulation

max_issues_repo_name: Ahmed-alkharusi/Ion-trap-simulation

max_forks_repo_name: Ahmed-alkharusi/Ion-trap-simulation


text:
import numpy as np
from ch04.negative_sampling_layer import UnigramSampler,SigmoidWithLoss

class Embedding:
    def __init__(self,W):
        self.params = [W]
        self.grads = [np.zeros_like(W)]
        self.idx = None

    def forward(self,idx):
        W, = self.params
        self.idx = idx
        out = W[idx]
        return out

    def backward(self,dout):
        dW, = self.grads
        dW[...] = 0
        for i, word_id in enumerate(self.idx):
            dW[word_id] = dout[i]
        return None

class EmbeddingDot:
    def __init__(self,W):
        self.embed = Embedding(W)
        self.params = self.embed.params
        self.grads = self.embed.grads
        self.cache = None

    def forward(self, h, idx):
        target_W = self.embed.forward(idx)
        out = np.sum(target_W * h, axis=1)

        self.cache = (h,target_W)
        return out

    def backward(self,dout):
        h,target_W = self.cache
        dout = dout.reshape(dout.shape[0],1)

        dtarget_W = dout * h
        self.embed.backward(dtarget_W)
        dh = dout * target_W
        return dh

class NegativeSamplingLoss:
    def __init__(self,W,corpus,power=0.75,sample_size=5):
        self.sample_size = sample_size
        self.sampler = UnigramSampler(corpus,power,sample_size)
        self.loss_layers = [SigmoidWithLoss() for _ in range(sample_size + 1)]
        self.embed_dot_layers = [EmbeddingDot(W) for _ in range(sample_size+1)]
        self.params,self.grads = [],[]
        for layer in self.embed_dot_layers:
            self.params += layer.params
            self.grads += layer.grads

    def forward(self,h,target):
        batch_size = target.shape[0]
        negative_sample = self.sampler.get_negative_sample(target)

        #正例のフォーワード
        score = self.embed_dot_layers[0].forward(h,target)
        correct_label = np.ones(batch_size, dtype=np.int32)
        loss = self.loss_layers[0].forward(score,correct_label)

        #負例のフォーワード
        negative_label = np.zeros(batch_size,dtype=np.int32)
        for i in range(self.sample_size):
            negative_target = negative_sample[:,i]
            score = self.embed_dot_layers[1+i].forward(h,negative_target)
            loss += self.loss_layers[1+i].forward(score,negative_label)

        return loss

    def backward(self,dout=1):
        dh = 0
        for l0,l1 in zip(self.loss_layers,self.embed_dot_layers):
            dscore = l0.backward(dout)
            dh += l1.backward(dscore)
        return dh

class CBOW:
    def __init__(self,vocab_size,hidden_size,window_size,corpus):
        V,H = vocab_size,hidden_size

        #重みの初期化
        W_in = 0.01 * np.random.randn(V,H).astype('f')
        W_out = 0.01 * np.random.randn(V,H).astype('f')

        #レイヤの生成
        self.in_layers = []
        for i in range(2*window_size):
            layer = Embedding(W_in)
            self.in_layers.append(layer)
        self.ns_loss = NegativeSamplingLoss(W_out,corpus,power=0.75,sample_size=5)

        #全ての重みと勾配を配列にまとめる
        layers = self.in_layers + [self.ns_loss]
        self.params ,self.grads = [],[]
        for layer in layers:
            self.params += layer.params
            self.grads += layer.grads

        #メンバ変数に単語の分散表現を設定
        self.word_vecs = W_in

    def forward(self,contexts,target):
        h = 0
        for i, layer in enumerate(self.in_layers):
            h += layer.forward(contexts[:,i])
        h += 1 / len(self.in_layers)
        loss = self.ns_loss.forward(h,target)
        return loss

    def backward(self,dout=1):
        dout = self.ns_loss.backward(dout)
        dout *= 1 / len(self.in_layers)
        for layer in self.in_layers:
            layer.backward(dout)
        return None

# corpus = np.array([0,1,2,3,4,1,2,3])
# power = 0.75
# sample_size = 2
#
# sampler = UnigramSampler(corpus,power,sample_size)
# target = np.array([1,3,0])
# negative_sample = sampler.get_negative_sample(target)
# print(negative_sample)
import pickle

with open('cbow_params.pkl', 'rb') as f:
    data = pickle.load(f)
    print(data)


text:
# -*- coding: utf-8 -*-
# ---
# jupyter:
#   jupytext:
#     formats: ipynb,py:light
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.3.3
#   kernelspec:
#     display_name: py3 venv
#     language: python
#     name: py3
# ---

# +
# list two-theta angle values
# for a given materials and wavelength

# note: do not work yet for non-cubic material
# note: export to  .py file using jupytext light-script pairing
# -

import numpy as np
from scipy import constants
import tabulate as tab

# +
# Latttice parameter in Angström
# and crystal structure

materials = {
    'Cu':   (3.615, 'fcc'),
    'Nb':   (3.3063, 'bcc'),
    'Si':   (5.431194, 'fcc'),
    'LaB6': (4.15682, 'cubic'), # https://www-s.nist.gov/srmors/certificates/660c.pdf
    'Al':   (4.046, 'fcc'),
    'Ag':   (4.079, 'fcc')
}
# source:
# https://en.wikipedia.org/wiki/Lattice_constant
# -

def keV_to_Angstrom(E_keV):
    """
    # E = hc/lambda
    # lambda = hc/E
    """
    E_J = 1e3 * E_keV * constants.e
    lambda_m = constants.c * constants.h / E_J
    return lambda_m * 1e10

# +
def twoTheta_Bragg_law(distance, wavelength):
    u = wavelength/2/distance
    if np.abs(u) <= 1:
        return 2* np.arcsin(u) * 180/np.pi
    else:
        return np.NaN

def d_hkl_cubic(a, h, k, l):
    return a/np.sqrt( h**2 + k**2 + l**2 )

# +
def all_hkl(n_max=7):
    """Generate all possible hlk triplet"""
    all_hkl = []
    for l in range(1, n_max):  #  <-- indice max.
        for k in range(l+1):
            for h in range(k+1):
                all_hkl.append(sorted([h, k, l], reverse=True))

    all_hkl = sorted(all_hkl, key=lambda x: sum(i**2 for i in x))
    return all_hkl

# Condition d'existences:
def existence_FCC(h, k, l):
    # même parité
    return h % 2 == k % 2 and k % 2 == l % 2

def existence_BCC(h, k, l):
    # somme paire
    return (h+k+l) % 2 == 0

def existence_ZincBlende(h, k, l):
    # même parité
    meme_parite = (h % 2 == k % 2 and k % 2 == l % 2)
    motif = ((h+k+l-2) % 4 != 0)
    return meme_parite and motif

# -

hkl_list_per_struct = {
    'fcc':[ hkl for hkl in all_hkl(n_max=6) if existence_FCC(*hkl) ],
    'bcc':[ hkl for hkl in all_hkl(n_max=6) if existence_BCC(*hkl) ],
    'cubic':[ hkl for hkl in all_hkl(n_max=6) ]
}

def list_peak(mat_symbol, wavelength, twoth_max=360):
    mat = materials[mat_symbol]
    data = []
    for hkl in hkl_list_per_struct[mat[1]]:
        d = d_hkl_cubic(mat[0], *hkl)
        two_theta = twoTheta_Bragg_law(d, wavelength)

        if np.isnan( two_theta ):
            break
        if two_theta > twoth_max:
            continue

        peak_info = {'hkl':f"({''.join(str(u) for u in hkl)})" + mat_symbol,
                    '2𝜃 (deg)':two_theta,
                    'd (A)':d,
                    'hkl_tuple':hkl}
        data.append(peak_info)
    return data

def peak_table(mat_symbol, wavelength):
    material = materials[mat_symbol]
    hkl_list = hkl_list_per_struct[material[-1].lower()]
    a = material[0] # if cubic...
    tab_data = list_peak(mat_symbol, wavelength)

    text = f'{mat_symbol}, {material[-1].lower()}, a={material[0]:.3f}A, w={wavelength:.3f}A \n'
    return text + tab.tabulate(tab_data, headers="keys")

w_K_alpha1_Cu = 1.540598  # Angström
w_K_alpha2_Cu = 1.544426  # Angström

E_keV = 20# 18.777 # keV
w = keV_to_Angstrom(E_keV)
#print('wavelength:', w, 'Angstöm')

table = peak_table('Nb', w_K_alpha1_Cu)
#print(table)

reason: import numpy,from scipy


text:
#! -*- coding: utf-8 -*-
# 用seq2seq的方式阅读理解任务
# 数据集和评测同 https://github.com/bojone/dgcnn_for_reading_comprehension

import json, os, re
import numpy as np
from bert4keras.backend import keras, K
from bert4keras.models import build_transformer_model
from bert4keras.tokenizers import Tokenizer, load_vocab
from bert4keras.optimizers import Adam
from bert4keras.snippets import sequence_padding, open
from bert4keras.snippets import DataGenerator, AutoRegressiveDecoder
from tqdm import tqdm

max_p_len = 256
max_q_len = 64
max_a_len = 32
max_qa_len = max_q_len + max_a_len
batch_size = 32
epochs = 20

# RoBERTa small
config_path = '/root/kg/bert/chinese_roberta_L-6_H-384_A-12/bert_config.json'
checkpoint_path = '/root/kg/bert/chinese_roberta_L-6_H-384_A-12/bert_model.ckpt'
dict_path = '/root/kg/bert/chinese_roberta_L-6_H-384_A-12/vocab.txt'
model_type = 'bert'

"""
# albert small
config_path = '/root/kg/bert/albert_small_zh_google/albert_config.json'
checkpoint_path = '/root/kg/bert/albert_small_zh_google/albert_model.ckpt'
dict_path = '/root/kg/bert/albert_small_zh_google/vocab.txt'
model_type = 'albert'

# RoBERTa tiny
config_path = '/root/kg/bert/chinese_roberta_L-4_H-312_A-12/bert_config.json'
checkpoint_path = '/root/kg/bert/chinese_roberta_L-4_H-312_A-12/bert_model.ckpt'
dict_path = '/root/kg/bert/chinese_roberta_L-4_H-312_A-12/vocab.txt'
model_type = 'bert'

# albert tiny
config_path = '/root/kg/bert/albert_tiny_zh_google/albert_config.json'
checkpoint_path = '/root/kg/bert/albert_tiny_zh_google/albert_model.ckpt'
dict_path = '/root/kg/bert/albert_tiny_zh_google/vocab.txt'
model_type = 'albert'
"""

# 标注数据
webqa_data = json.load(open('/root/qa_datasets/WebQA.json'))
sogou_data = json.load(open('/root/qa_datasets/SogouQA.json'))

# 保存一个随机序（供划分valid用）
if not os.path.exists('../random_order.json'):
    random_order = list(range(len(sogou_data)))
    np.random.shuffle(random_order)
    json.dump(random_order, open('../random_order.json', 'w'), indent=4)
else:
    random_order = json.load(open('../random_order.json'))

# 划分valid
train_data = [sogou_data[j] for i, j in enumerate(random_order) if i % 3 != 0]
valid_data = [sogou_data[j] for i, j in enumerate(random_order) if i % 3 == 0]
train_data.extend(train_data)
train_data.extend(webqa_data)  # 将SogouQA和WebQA按2:1的比例混合

# 加载并精简词表，建立分词器
token_dict, keep_tokens = load_vocab(
    dict_path=dict_path,
    simplified=True,
    startwith=['[PAD]', '[UNK]', '[CLS]', '[SEP]'],
)
tokenizer = Tokenizer(token_dict, do_lower_case=True)

class data_generator(DataGenerator):
    """数据生成器
    """
    def __iter__(self, random=False):
        """单条样本格式：[CLS]篇章[SEP]问题[SEP]答案[SEP]
        """
        batch_token_ids, batch_segment_ids = [], []
        for is_end, D in self.sample(random):
            question = D['question']
            answers = [p['answer'] for p in D['passages'] if p['answer']]
            passage = np.random.choice(D['passages'])['passage']
            passage = re.sub(u' |、|；|，', ',', passage)
            final_answer = ''
            for answer in answers:
                if all([a in passage[:max_p_len - 2] for a in answer.split(' ')]):
                    final_answer = answer.replace(' ', ',')
                    break
            qa_token_ids, qa_segment_ids = tokenizer.encode(
                question, final_answer, max_length=max_qa_len + 1)
            p_token_ids, p_segment_ids = tokenizer.encode(passage,
                                                          max_length=max_p_len)
            token_ids = p_token_ids + qa_token_ids[1:]
            segment_ids = p_segment_ids + qa_segment_ids[1:]
            batch_token_ids.append(token_ids)
            batch_segment_ids.append(segment_ids)
            if len(batch_token_ids) == self.batch_size or is_end:
                batch_token_ids = sequence_padding(batch_token_ids)
                batch_segment_ids = sequence_padding(batch_segment_ids)
                yield [batch_token_ids, batch_segment_ids], None
                batch_token_ids, batch_segment_ids = [], []

model = build_transformer_model(
    config_path,
    checkpoint_path,
    model=model_type,
    application='unilm',
    keep_tokens=keep_tokens,  # 只保留keep_tokens中的字，精简原字表
)
model.summary()

# 交叉熵作为loss，并mask掉输入部分的预测
y_true = model.input[0][:, 1:]  # 目标tokens
y_mask = model.input[1][:, 1:]
y_pred = model.output[:, :-1]  # 预测tokens，预测与目标错开一位
cross_entropy = K.sparse_categorical_crossentropy(y_true, y_pred)
cross_entropy = K.sum(cross_entropy * y_mask) / K.sum(y_mask)

model.add_loss(cross_entropy)
model.compile(optimizer=Adam(1e-5))

class ReadingComprehension(AutoRegressiveDecoder):
    """beam search解码来生成答案
    passages为多篇章组成的list，从多篇文章中自动决策出最优的答案，
    如果没答案，则返回空字符串。
    mode是extractive时，按照抽取式执行，即答案必须是原篇章的一个片段。
    """
    def __init__(self, mode='extractive', **kwargs):
        super(ReadingComprehension, self).__init__(**kwargs)
        self.mode = mode

    def get_ngram_set(self, x, n):
        """生成ngram合集，返回结果格式是:
        {(n-1)-gram: set([n-gram的第n个字集合])}
        """
        result = {}
        for i in range(len(x) - n + 1):
            k = tuple(x[i:i + n])
            if k[:-1] not in result:
                result[k[:-1]] = set()
            result[k[:-1]].add(k[-1])
        return result

    @AutoRegressiveDecoder.set_rtype('probas')
    def predict(self, inputs, output_ids, step):
        inputs = [i for i in inputs if i[0, 0] > -1]  # 过滤掉无答案篇章
        topk = len(inputs[0])
        all_token_ids, all_segment_ids = [], []
        for token_ids in inputs:  # inputs里每个元素都代表一个篇章
            token_ids = np.concatenate([token_ids, output_ids], 1)
            segment_ids = np.zeros_like(token_ids)
            if step > 0:
                segment_ids[:, -output_ids.shape[1]:] = 1
            all_token_ids.extend(token_ids)
            all_segment_ids.extend(segment_ids)
        padded_all_token_ids = sequence_padding(all_token_ids)
        padded_all_segment_ids = sequence_padding(all_segment_ids)
        probas = model.predict([padded_all_token_ids, padded_all_segment_ids])
        probas = [
            probas[i, len(ids) - 1] for i, ids in enumerate(all_token_ids)
        ]
        probas = np.array(probas).reshape((len(inputs), topk, -1))
        if step == 0:
            # 这一步主要是排除没有答案的篇章
            # 如果一开始最大值就为end_id，那说明该篇章没有答案
            argmax = probas[:, 0].argmax(axis=1)
            available_idxs = np.where(argmax != self.end_id)[0]
            if len(available_idxs) == 0:
                scores = np.zeros_like(probas[0])
                scores[:, self.end_id] = 1
                return scores
            else:
                for i in np.where(argmax == self.end_id)[0]:
                    inputs[i][:, 0] = -1  # 无答案篇章首位标记为-1
                probas = probas[available_idxs]
                inputs = [i for i in inputs if i[0, 0] > -1]  # 过滤掉无答案篇章
        if self.mode == 'extractive':
            # 如果是抽取式，那么答案必须是篇章的一个片段
            # 那么将非篇章片段的概率值全部置0
            new_probas = np.zeros_like(probas)
            ngrams = {}
            for token_ids in inputs:
                token_ids = token_ids[0]
                sep_idx = np.where(token_ids == tokenizer._token_end_id)[0][0]
                p_token_ids = token_ids[1:sep_idx]
                for k, v in self.get_ngram_set(p_token_ids, step + 1).items():
                    ngrams[k] = ngrams.get(k, set()) | v
            for i, ids in enumerate(output_ids):
                available_idxs = ngrams.get(tuple(ids), set())
                available_idxs.add(tokenizer._token_end_id)
                available_idxs = list(available_idxs)
                new_probas[:, i, available_idxs] = probas[:, i, available_idxs]
            probas = new_probas
        return (probas**2).sum(0) / (probas.sum(0) + 1)  # 某种平均投票方式

    def answer(self, question, passages, topk=1):
        token_ids = []
        for passage in passages:
            passage = re.sub(u' |、|；|，', ',', passage)
            p_token_ids = tokenizer.encode(passage, max_length=max_p_len)[0]
            q_token_ids = tokenizer.encode(question, max_length=max_q_len + 1)[0]
            token_ids.append(p_token_ids + q_token_ids[1:])
        output_ids = self.beam_search(token_ids, topk)  # 基于beam search
        return tokenizer.decode(output_ids)

reader = ReadingComprehension(start_id=None,
                              end_id=tokenizer._token_end_id,
                              maxlen=max_a_len,
                              mode='extractive')

def predict_to_file(data, filename, topk=1):
    """将预测结果输出到文件，方便评估
    """
    with open(filename, 'w', encoding='utf-8') as f:
        for d in tqdm(iter(data), desc=u'正在预测(共%s条样本)' % len(data)):
            q_text = d['question']
            p_texts = [p['passage'] for p in d['passages']]
            a = reader.answer(q_text, p_texts, topk)
            if a:
                s = u'%s\t%s\n' % (d['id'], a)
            else:
                s = u'%s\t\n' % (d['id'])
            f.write(s)
            f.flush()

class Evaluate(keras.callbacks.Callback):
    def __init__(self):
        self.best = 0.

    def on_epoch_end(self, epoch, logs=None):
        acc, f1, final = self.evaluate()
        if final > self.best:
            self.best = final
            model.save_weights('best_model.weights')
        print('acc: %.5f, f1: %.5f, final: %.5f, best final: %.5f\n' % (acc, f1, final, self.best))

    def evaluate(self):
        predict_to_file(valid_data, 'tmp_result.txt')
        acc, f1, final = json.loads(
            os.popen(
                'python /root/reading/evaluate_tool/evaluate.py tmp_result.txt tmp_output.txt'
            ).read().strip()
        )
        return acc, f1, final

if __name__ == '__main__':

    evaluator = Evaluate()
    train_generator = data_generator(train_data, batch_size)

    model.fit_generator(train_generator.forfit(),
                        steps_per_epoch=len(train_generator),
                        epochs=epochs,
                        callbacks=[evaluator])

else:

    model.load_weights('./best_model.weights')

max_stars_repo_name: dumpmemory/pretrained-models

max_issues_repo_name: mathCrazyy/pretrained-models

max_forks_repo_name: mathCrazyy/pretrained-models


text:
# coding=UTF-8
import numpy as np
from glob import *
import os
from torchvision import transforms
import matplotlib.pyplot as plt
from torchvision.datasets import ImageFolder
import torch
from torchvision import models
import torch.nn as nn
import torch.nn.modules
from torch import optim
from torch.optim import lr_scheduler
import time
from torch.autograd import Variable
from PIL import Image
import sys

def createDataset():
    ##创建数据集
    path = 'myCatAndDotSet/'

    files = glob(os.path.join(path, '*.jpg'))
    print(f'total num of imags :{len(files)}')
    numImages = len(files)

    shuffle = np.random.permutation(numImages)
    os.mkdir(os.path.join(path, 'train'))
    os.mkdir(os.path.join(path, 'valid'))

    #创建目录
    for t in ['valid', 'train']:
        for folder in ['dog', 'cat']:
            os.mkdir(os.path.join(path, t, folder))

    for i in shuffle[:500]:
        folder = files[i].split('/')[-1].split('.')[0]
        image = files[i].split('/')[-1]
        os.rename(files[i], os.path.join(path, 'train', folder, image))

    for i in shuffle[500:1000]:
        folder = files[i].split('/')[-1].split('.')[0]
        image = files[i].split('/')[-1]
        os.rename(files[i], os.path.join(path, 'valid', folder, image))

def loadDataSet():
    simpleTransform = transforms.Compose([transforms.Resize([224, 224]), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
    train = ImageFolder('myCatAndDotSet/train', simpleTransform)
    valid = ImageFolder('myCatAndDotSet/valid', simpleTransform)

    # imshow(train[30][0])

    #训练集，一定要用shuffle=True打乱, 否则训练没有效果，原因未知
    trainDataGen = torch.utils.data.DataLoader(train, shuffle=True, batch_size=10, num_workers=1)
    validDataGen = torch.utils.data.DataLoader(valid, batch_size=10, num_workers=1)
    print("dataSize:", len(trainDataGen.dataset))

    dataset_size = {'train': len(trainDataGen.dataset), 'valid': len(validDataGen.dataset)}
    dataloaders = {'train': trainDataGen, 'valid': validDataGen}
    return dataloaders, dataset_size

def imshow(inp):
    inp = inp.numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    inp = std * inp + mean
    inp = np.clip(inp, 0, 1)
    plt.imshow(inp)

def defineResnet():
    #认识resnet
    resnet = models.resnet18(pretrained=True)
    print(resnet.fc)
    num_ftrs = resnet.fc.in_features
    resnet.fc = nn.Linear(num_ftrs, 2)

    is_cuda = True
    if is_cuda:
        model_ft = resnet.cuda()
    # print(resnet)
    print(resnet.fc)

    return model_ft

is_cuda = True

#模型训练
def trainingModel():
    DataLoader, dataSize = loadDataSet()

    resnetModel = defineResnet()

    learning_rate = 0.001
    totoalEpoch = 5

    #交叉熵损失
    criterion_loss = nn.CrossEntropyLoss()

    #优化器
    optimizer_ft = optim.SGD(resnetModel.parameters(), lr=learning_rate, momentum=0.9)

    #使用StopLr函数来动态修改学习率
    scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)

    since = time.time()

    #获取预训练权重数据
    best_model_wts = resnetModel.state_dict()
    bast_acc = 0.0

    #周期为500次
    for epoch in range(totoalEpoch):
        print('周期 {}/{}'.format(epoch, totoalEpoch))
        print('-' * 10)

        for phase in ['train', 'valid']:
            if phase == 'train':
                #设置为训练模式
                scheduler.step()
                resnetModel.train(True)
                print("-------train-start----------")
            else:
                resnetModel.train(False)
                print('----------valid- start--------')

            running_loss = 0.0
            running_corrents = 0

            for data in DataLoader[phase]:
                inputs, labels = data

                if is_cuda:
                    #数据导入到GPU
                    inputs = Variable(inputs.cuda())
                    labels = Variable(labels.cuda())
                else:
                    inputs = Variable(inputs)
                    labels = Variable(labels)

                #梯度清0
                optimizer_ft.zero_grad()

                #数据输入模型得到输出
                outputs = resnetModel(inputs)

                #计算预测值 pred是预测值
                _, preds = torch.max(outputs.data, 1)
                # print("pred :", preds)

                #求损失值
                loss = criterion_loss(outputs, labels)
                # print("loss:", loss)

                if phase == 'train':
                    #求梯度
                    loss.backward()
                    # print("backward loss:", loss)
                    #优化权重，更新权重参数， 反向传播
                    optimizer_ft.step()

                # running_loss += loss.data[0]              #新版本不是这样用，用法如下
                running_loss += loss.item()
                running_corrents += torch.sum(preds == labels.data)
                # print('preds:', preds)
                # print('labels.data:', labels.data)
                # print('preds == labels.data:', preds == labels.data)
                # print('runnnig loss:', loss)
                # print('running_corrents:', running_corrents)
                # print('datasize:', dataSize[phase])

            #原来的tensoer 与整数之间的/ 被废弃 改用如下方式
            epoch_loss = torch.true_divide(running_loss, dataSize[phase])
            epoch_acc = torch.true_divide(running_corrents, dataSize[phase])

            # print('{} Loss:{:.4f} Acc:{:.4f}'.format(phase, epoch_loss, epoch_acc))
            print('epoch_loss:', epoch_loss)
            # print('epoch_acc:', epoch_acc)

            if phase == 'valid' and epoch_acc > bast_acc:
                #acc 是准确率
                bast_acc = epoch_acc
                best_model_wts = resnetModel.state_dict()
            print()
        time_elapsed = time.time() - since
        print("trainning complete in {:.0f}m {:.0f}s".format(time_elapsed / 60, time_elapsed % 60))
        print('Best val Acc:{:.4f}'.format(bast_acc))

        time.sleep(1)
    # 加载训练好的模型参数
    resnetModel.load_state_dict(best_model_wts)

    # img = Image.open('dog.2.jpg').convert('RGB')
    # img = simpleTransform(img).unsqueeze(0)
    #
    # img = img.to(device, torch.float)
    #
    # outputs = resnetModel(img)
    # print('output:', outputs)
    # _,preds =torch.max(outputs.data, 1)
    #
    # print('preds:',preds)
    # print("图像检测结果为:",class_label[preds[0]])

    #最终保存模型
    torch.save(resnetModel, "1.pth")  #保存整个网络和参数
    # torch.save(resnetModel.state_dict(), "1.pth")       #只保存参数（未成功）

class_label = ["cat", "dog"]
model_path = '1.pth'
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
simpleTransform = transforms.Compose([transforms.Resize([224, 224]), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

#测试模型
def useTrainedModelDetect(imgName):
    # 只加载保存的参数（未成功）
    # resnetModel = defineResnet()
    # params = torch.load(model_path, device)
    # resnetModel.load_state_dict(params)

    print(sys.argv[0], sys.argv[1])

    # 加载保存的整个网络和参数
    resnetModel = torch.load(model_path)

    img = Image.open(imgName).convert('RGB')
    img = simpleTransform(img).unsqueeze(0)

    img = img.to(device, torch.float)

    outputs = resnetModel(img)
    print('output:', outputs)
    _, preds = torch.max(outputs.data, 1)

    print('preds:', preds)
    print("图像检测结果为:", class_label[preds[0]])

if __name__ == "__main__":
    # createDataset()
    # imageTransforms()
    # defineResnet()
    # trainingModel()

    useTrainedModelDetect(sys.argv[1])


text:
from collections import deque

import numpy as np

from dvmvs.utils import is_pose_available, pose_distance

class KeyframeBuffer:
    def __init__(self, buffer_size, keyframe_pose_distance, optimal_t_score, optimal_R_score, store_return_indices):
        self.buffer = deque([], maxlen=buffer_size)
        self.keyframe_pose_distance = keyframe_pose_distance
        self.optimal_t_score = optimal_t_score
        self.optimal_R_score = optimal_R_score
        self.__tracking_lost_counter = 0
        self.__store_return_indices = store_return_indices  # mostly required for simulation of the frame selection

    def calculate_penalty(self, t_score, R_score):
        degree = 2.0
        R_penalty = np.abs(R_score - self.optimal_R_score) ** degree
        t_diff = t_score - self.optimal_t_score
        if t_diff < 0.0:
            t_penalty = 5.0 * (np.abs(t_diff) ** degree)
        else:
            t_penalty = np.abs(t_diff) ** degree
        return R_penalty + t_penalty

    def try_new_keyframe(self, pose, image, index=None):
        if self.__store_return_indices and index is None:
            raise ValueError("Storing and returning the frame indices is requested in the constructor, but index=None is passed to the function")

        if is_pose_available(pose):
            self.__tracking_lost_counter = 0
            if len(self.buffer) == 0:
                if self.__store_return_indices:
                    self.buffer.append((pose, image, index))
                else:
                    self.buffer.append((pose, image))
                return 0  # pose is available, new frame added but buffer was empty, this is the first frame, no depth map prediction will be done
            else:
                if self.__store_return_indices:
                    last_pose, last_image, last_index = self.buffer[-1]
                else:
                    last_pose, last_image = self.buffer[-1]

                combined_measure, R_measure, t_measure = pose_distance(pose, last_pose)

                if combined_measure >= self.keyframe_pose_distance:
                    if self.__store_return_indices:
                        self.buffer.append((pose, image, index))
                    else:
                        self.buffer.append((pose, image))
                    return 1  # pose is available, new frame added, everything is perfect, and we will predict a depth map later
                else:
                    return 2  # pose is available but not enough change has happened since the last keyframe
        else:
            self.__tracking_lost_counter += 1

            if self.__tracking_lost_counter > 30:
                if len(self.buffer) > 0:
                    self.buffer.clear()
                    return 3  # a pose reading has not arrived for over a second, tracking is now lost
                else:
                    return 4  # we are still very lost
            else:
                return 5  # pose is not available right now, but not enough time has passed to consider lost, there is still hope :)

    def get_best_measurement_frames(self, n_requested_measurement_frames):
        buffer_array = list(self.buffer)

        if self.__store_return_indices:
            reference_pose, reference_image, reference_index = buffer_array[-1]
        else:
            reference_pose, reference_image = buffer_array[-1]

        n_requested_measurement_frames = min(n_requested_measurement_frames, len(buffer_array) - 1)

        penalties = []
        for i in range(len(buffer_array) - 1):
            measurement_pose = buffer_array[i][0]
            combined_measure, R_measure, t_measure = pose_distance(reference_pose, measurement_pose)
            penalty = self.calculate_penalty(t_measure, R_measure)
            penalties.append(penalty)
        indices = np.argpartition(penalties, n_requested_measurement_frames - 1)[:n_requested_measurement_frames]

        measurement_frames = []
        for index in indices:
            measurement_frames.append(buffer_array[index])
        return measurement_frames

class SimpleBuffer:
    def __init__(self, buffer_size, store_return_indices):
        self.buffer = deque([], maxlen=buffer_size + 1)
        self.__tracking_lost_counter = 0
        self.__store_return_indices = store_return_indices  # mostly required for simulation of the frame selection

    def try_new_keyframe(self, pose, image, index=None):
        if self.__store_return_indices and index is None:
            raise ValueError("Storing and returning the frame indices is requested in the constructor, but index=None is passed to the function")

        if is_pose_available(pose):
            self.__tracking_lost_counter = 0
            if len(self.buffer) == 0:
                if self.__store_return_indices:
                    self.buffer.append((pose, image, index))
                else:
                    self.buffer.append((pose, image))
                return 0  # pose is available, new frame added but buffer was empty, this is the first frame, no depth map prediction will be done
            else:
                if self.__store_return_indices:
                    self.buffer.append((pose, image, index))
                else:
                    self.buffer.append((pose, image))
                return 1  # pose is available, new frame added, everything is perfect, and we will predict a depth map later
        else:
            self.__tracking_lost_counter += 1

            if self.__tracking_lost_counter > 30:
                if len(self.buffer) > 0:
                    self.buffer.clear()
                    return 2  # a pose reading has not arrived for over a second, tracking is now lost
                else:
                    return 3  # we are still very lost
            else:
                return 4  # pose is not available right now, but not enough time has passed to consider lost, there is still hope :)

    def get_measurement_frames(self):
        measurement_frames = list(self.buffer)[:-1]
        return measurement_frames

max_issues_repo_name: MorchelPeng/deep-video-mvs

max_forks_repo_name: MorchelPeng/deep-video-mvs


text:
import pyaudio
import math, random
import numpy as np
import find_peaks as fp
from search_tree import SearchTree

from pydub import AudioSegment

import time

END = 10000 #Sample for 10 seconds
THRESH = 0.6

def match(audio_name, mv_name):
    audio_file = AudioSegment.from_file(audio_name)[5000:(END + 5000)]
    mv_file = AudioSegment.from_file(mv_name)

    data = np.frombuffer(audio_file._data, np.int16)

    audio_channels = []
    audio_sparse_maps = [] #freq/time mappings of amp peaks in spectogram

    for i in range(audio_file.channels):
        audio_channels.append(data[i::audio_file.channels])

    for channel_data in audio_channels:
        smap = fp.get_sparse_map(channel_data, audio_file.frame_rate)
        audio_sparse_maps.append(list(smap))

    l = 0
    length = 120000 #mv_file.duration_seconds * 1000
    best = None
    mn = -1

    while((l + END) <= length):
        if(l%5000 == 0):
            print('At time', l/1000, '(s)')
        sample = mv_file[l:(l + END)]

        sample_channels = []
        sample_sparse_maps = []

        data = np.frombuffer(sample._data, np.int16)
        for i in range(sample.channels):
            sample_channels.append(data[i::sample.channels])

        #print(sample_channels[0][])

        for channel_data in sample_channels:
            smap = fp.get_sparse_map(channel_data, mv_file.frame_rate)
            sample_sparse_maps.append(list(smap))

        try:
            curr = difference(audio_sparse_maps, sample_sparse_maps)
        except():
            return (0, 0)

        if((mn == -1) or ((curr > 0) and (mn - curr >= THRESH))):
            mn = curr
            best = l
        #print(curr)
        l += 500
    return (best - 5000, mn)

def difference(audio_maps, sample_maps):
    mean_offset = 0
    map_cnt = min(len(audio_maps), len(sample_maps))
    for i in range(map_cnt):
        mean_offset += offset(audio_maps[i], sample_maps[i])

    return (mean_offset / map_cnt)

def offset(mapA, mapB):
    #print('query start')
    init = time.time() * 1000
    mean_dist = 0
    st = SearchTree()
    #print('sizes:',len(mapA), len(mapB))
    random.shuffle(mapA) #Reduce tree height
    for i in mapA:
        #print(i[0], i[1])
        st.push(i[0], i[1])
    #print('building done. Time was', (time.time() * 1000 - init))
    init = time.time() * 1000
    for j in mapB:
        #print(j, st.query(j[0], j[1], False))
        res = st.query(j[0], j[1], False)
        mean_dist += res[0]

    #print('query end. Time was', (time.time() * 1000 - init), 'Visited count:', res[1])

    return (mean_dist / len(mapB))
'''
def offset(mapA, mapB):
    mean_dist = 0
    for i in mapA:
        min_dist = -1
        res = None
        for j in mapB:
            if(res == None):
                res = j
                min_dist = dist(i[0], i[1], j[0], j[1])
            else:
                if(dist(i[0], i[1], j[0], j[1]) < min_dist):
                    min_dist = dist(i[0], i[1], j[0], j[1])
                    res = j
        mean_dist += min_dist
    return (mean_dist / len(mapA))
'''

def dist(x1, y1, x2, y2):
    return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)

max_stars_repo_name: VidithM/goto-music

max_issues_repo_name: VidithM/goto-music

max_forks_repo_name: VidithM/goto-music


text:
import gzip
import logging
import time

from model_syntax import load_compressed_file

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s',
    datefmt="%Y-%m-%d %H:%M:%S")
logger = logging.getLogger(__name__)

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
import tensorflow.compat.v1 as tf
import argparse
from model_configs import ModelConfigType
from utils import pc_io
from utils.octree_coding import departition_octree

np.random.seed(42)
tf.set_random_seed(42)

def read_pcs(length, folder):
    return [pc_io.load_pc(os.path.join(folder, f'{j}.ply')) for j in range(length)]

def decompress():
    model = ModelConfigType[args.model_config].build()
    logger.info('Files loading')
    compressed_data = []
    for file in args.input_files:
        with gzip.open(file, "rb") as f:
            compressed_data.append(load_compressed_file(f))
    logger.info('Files loaded')
    model.decompress()

    # Checkpoints
    saver = tf.train.Saver(keep_checkpoint_every_n_hours=1)
    # Init
    init = tf.global_variables_initializer()

    len_files = len(args.input_files)
    i = 0
    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True
    with tf.Session(config=tf_config) as sess:
        logger.info('Init session')
        sess.run(init)

        checkpoint = tf.train.latest_checkpoint(args.checkpoint_dir)
        assert checkpoint is not None, f'Checkpoint {args.checkpoint_dir} was not found'
        saver.restore(sess, checkpoint)

        for (resolution, level, binstr, blocks), ori_file, output_file in zip(compressed_data, args.input_files, args.output_files):
            logger.info(f'{i}/{len_files} - Writing {ori_file} to {output_file} with {len(blocks)} blocks')
            output_dir, _ = os.path.split(output_file)
            os.makedirs(output_dir, exist_ok=True)

            x_shape = np.array([resolution, resolution, resolution], dtype=np.uint32) // (2 ** level)

            if args.debug:
                dec_blocks_enc = read_pcs(len(blocks), ori_file + '.enc.blocks')
                debug_data = np.load(ori_file + '.enc.data.npz', allow_pickle=True)
                data_list_enc, debug_t_list_enc = debug_data['data'], debug_data['debug_t_list']
                dec_blocks, debug_t_list = model.decompress_blocks(sess, blocks, x_shape, debug=args.debug)
                max_retries = 100
                # Sometimes, GPU computations are wrong... debug mode detects computation errors and resolves them
                for j, (db, dbe) in enumerate(zip(dec_blocks, dec_blocks_enc)):
                    logger.info(f'{i}/{len_files} - Verifying block {j}/{len(blocks)} {db.shape} {dbe.shape}')
                    error = True
                    retries = 0
                    new_block = db
                    new_debug = debug_t_list[j]
                    # Checking inputs consistency
                    np.testing.assert_equal(blocks[j], data_list_enc[j])
                    while error and retries < max_retries:
                        try:
                            intermediate_error = False
                            # Checking intermediate results consistency
                            for key in new_debug:
                                try:
                                    v1 = new_debug[key]
                                    v_len = np.prod(v1.shape)
                                    v2 = debug_t_list_enc[j][key]
                                    if v1.dtype == object:
                                        np.testing.assert_equal(v1, v2, err_msg=f'Values did not match for key {key}')
                                        err_n = np.sum(v1 != v2)
                                        err_percent = 100 * (err_n / v_len)
                                        logger.info(f'{key} {err_n}/{v_len} ({err_percent:.2f}%)')
                                    elif np.issubdtype(v1.dtype, np.number):
                                        atol = 0.001
                                        rtol = 1e-7
                                        aerr = abs(v1 - v2)
                                        aerr_n = np.sum(aerr > rtol * abs(v2))
                                        err_percent = 100 * (aerr_n / v_len)
                                        logger.info(f'{key} {aerr_n}/{v_len} ({err_percent:.2f}%) min {np.min(aerr)} mean {np.mean(aerr)} max {np.max(aerr)}')
                                        np.testing.assert_allclose(v1, v2, rtol=rtol, atol=atol,
                                                                   err_msg=f'Values did not match for key {key}')
                                    else:
                                        raise RuntimeError(f'Unsupported type {v1.dtype} for key {key} and value {v1}')
                                except AssertionError as e:
                                    if np.issubdtype(v1.dtype, np.number):
                                        idxs = np.argwhere(abs(v1 - v2) > atol + rtol * abs(v1))
                                        for idx in idxs[:500]:
                                            idx = tuple(idx)
                                            logger.error(f'{key} mismatch {idx} expected {v2[idx]} but got {v1[idx]}')
                                        if len(idxs) > 500:
                                            logger.error(f'Remaining entries truncated...')
                                    intermediate_error = True
                                    print(e)
                            if intermediate_error:
                                np.savez_compressed(output_file + '.dec.dump.npz', dec=new_debug, enc=debug_t_list_enc[j])
                                raise AssertionError('Intermediate results are wrong')

                            # Checking final result consistency
                            np.testing.assert_equal(new_block, dbe)
                            dec_blocks[j] = new_block
                            error = False
                            if retries > 0:
                                logger.info(f'{i}/{len_files} - Verifying block {j}/{len(blocks)} error resolved {retries}/{max_retries} retries {new_block.shape}')
                        except AssertionError as e:
                            logger.warning(f'{i}/{len_files} - Verifying block {j}/{len(blocks)} {retries}/{max_retries} retries\n{str(e)}')
                            error = True
                            new_block, new_debug = model.decompress_blocks(sess, [blocks[j]], x_shape, debug=args.debug)
                            new_block, new_debug = (new_block[0], new_debug[0])
                        retries += 1
                    if retries == max_retries:
                        raise RuntimeError(f'{i}/{len_files} - Verifying {ori_file} failed after {retries} retries')

            else:
                dec_blocks, _ = model.decompress_blocks(sess, blocks, x_shape, debug=args.debug)

            # Hardcode bbox_min
            bbox_min = [0, 0, 0]
            # Assume blocks of equal size
            bbox_max = x_shape * (2 ** level)
            dec_blocks = departition_octree(dec_blocks, binstr, bbox_min, bbox_max, level)
            pa = np.vstack(dec_blocks)

            pc_io.write_df(output_file, pc_io.pa_to_df(pa))
            i += 1
    logger.info('Finished')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        prog='decompress_octree.py',
        description='Decompress a file.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument(
        '--input_files', nargs='+',
        help='Input files.', required=True)
    parser.add_argument(
        '--output_files', nargs='+',
        help='Input files.', required=True)
    parser.add_argument(
        '--checkpoint_dir',
        help='Directory where to save/load model checkpoints.', required=True)
    parser.add_argument(
        '--model_config',
        help=f'Model used: {ModelConfigType.keys()}.', required=True)
    parser.add_argument(
        '--num_filters', type=int, default=32,
        help='Number of filters per layer.')
    parser.add_argument(
        '--data_format', default='channels_first',
        help='Data format used: channels_first or channels_last')
    parser.add_argument(
        '--debug', default=False, action='store_true',
        help='Use debug data to check results.')

    args = parser.parse_args()

    assert args.data_format in ['channels_first', 'channels_last']
    assert len(args.input_files) == len(args.output_files)
    assert args.model_config in ModelConfigType.keys()

    decompress()