def predict():
    ''' Main entry point to the train function. Sets up the arguments and runs the trainer '''
    parser = argparse.ArgumentParser(description='Predict the output.')
    parser.add_argument("imagePath", help="Specifies the image to load")
    parser.add_argument("checkpointPath",
                        help="Specifies the checkpoint file to load")
    parser.add_argument("--top_k",
                        help="Specifies the no of top matches to produce",
                        type=int,
                        default=1,
                        dest='K')
    parser.add_argument("--category_names",
                        help="Specifies the files for the category names",
                        dest='catNames',
                        default='cat_to_name.json')
    parser.add_argument("--gpu",
                        help="Flag to enable GPU",
                        action='store_true')

    args = parser.parse_args()

    try:
        # get the target device
        if args.gpu and not torch.cuda.is_available():
            raise Exception("GPU Not Supported")

        # load the device
        device = torch.device("cuda:0" if args.gpu else "cpu")

        # load checkpoint
        newmodel, classtoidx = loadCheckpoint(args.checkpointPath, device)

        # load the category names
        catNames = loadClassNames(args.catNames)

        # load the image
        testImg = processImage(args.imagePath)

        # compute the reverse map of index to class name
        idxtoclass = {v: k for k, v in classtoidx.items()}
        # run in eval mode
        newmodel.eval()

        # no gradients
        with torch.no_grad():
            # convert to a batch of tensors of size 1
            pyImg = torch.from_numpy(testImg).unsqueeze_(0).to(
                device, dtype=torch.float)
            output = newmodel.forward(pyImg)
            # since we used the Log Softmax, use the exp to get the probabilities
            torch.exp_(output)
            probPy, indicesPy = output.topk(args.K)
            probs = probPy.cpu().numpy()
            indices = indicesPy.cpu().numpy()
            classes = [catNames[idxtoclass[idx]] for idx in indices[0]]
            return probs[0], classes

    except Exception as e:
        print(e)
        raise e
Ejemplo n.º 2
0
def transform_parameters_backward(features, anchor_space):
    wa = anchor_space[:, :, :, 2] - anchor_space[:, :, :, 0]
    ha = anchor_space[:, :, :, 3] - anchor_space[:, :, :, 1]
    xa = anchor_space[:, :, :, 0] + wa / 2
    ya = anchor_space[:, :, :, 1] + ha / 2
    batch_size_and_anchor_len, h_feature, w_feature = features.size()
    anchor_len, _, _ = xa.size()
    xa = xa.repeat(batch_size_and_anchor_len // anchor_len, 1, 1)
    ya = ya.repeat(batch_size_and_anchor_len // anchor_len, 1, 1)
    wa = wa.repeat(batch_size_and_anchor_len // anchor_len, 1, 1)
    ha = ha.repeat(batch_size_and_anchor_len // anchor_len, 1, 1)
    #
    tx, ty, tw, th = torch.unstack(features, 3)
    torch.addcmul_(xa, 1, wa, tx)
    torch.addcmul_(ya, 1, ha, ty)
    torch.exp_(tw)
    torch.exp_(th)
    tw.mul_(wa)
    th.mul_(ha)
    #
    x = xa
    y = ya
    w = tw
    h = th
    #
    box = torch.Tensor(
        torch.Size([batch_size_and_anchor_len, 4, h_feature, w_feature]))
    box[:, 0, :, :] = x - w / 2
    box[:, 1, :, :] = y - h / 2
    box[:, 2, :, :] = w + box[:, 0, :, :]
    box[:, 3, :, :] = h + box[:, 1, :, :]
    return box
def validate(model, dataloader, device, loss_func):
    # turn on eval mode
    model.eval()
    model.to(device)
    # turn off gradient tracking
    with torch.no_grad():
        loss_sum = 0.
        accuracy = 0.
        step = 0
        for inputs, labels in dataloader:
            # update step
            step += 1
            # load data to device
            inputs, labels = inputs.to(device), labels.to(device)
            # forward pass
            preds = model.forward(inputs)
            # loss
            loss = loss_func(preds, labels)
            loss_sum += loss
            # accuracy
            torch.exp_(preds)
            accuracy += (torch.max(preds, 1)[1] == labels).type(
                torch.FloatTensor).mean()
        pass
    loss_sum /= step
    accuracy /= step
    return loss_sum, accuracy
Ejemplo n.º 4
0
def validateModel(model, data_loader, device, criterion):
    """ this function calculate accuracy and loss based on
    model and data loader
    """
    # turn on eval model
    model.eval()
    model.to(device)
    # calculate loss and accuracy
    loss_sum = 0.
    accuracy = 0.
    num_step = 0
    with torch.no_grad():
        for inputs, labels in data_loader:
            num_step += 1
            # prepare data
            inputs, labels = inputs.to(device), labels.to(device)
            # forward pass
            preds = model.forward(inputs)
            # loss
            loss = criterion(preds, labels)
            loss_sum += loss.item()
            # accuracy
            torch.exp_(preds)
            accuracy += (torch.max(preds, dim=1)[1] == labels).type(
                torch.FloatTensor).mean()
            pass
        accuracy /= num_step
        loss_sum /= num_step
    return accuracy, loss_sum
def predict(image_path, model, topk=5):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''

    # TODO: Implement the code to predict the class from an image file
    # load image
    image = Image.open(image_path)
    im = process_image(image)
    # to tensor
    im = torch.from_numpy(im)
    # forward pass
    model.eval()
    with torch.no_grad():
        im.unsqueeze_(0)
        im = im.type(torch.FloatTensor)
        im = im.to(device)
        pred = model.forward(im)
        torch.exp_(pred)
        pass
    # get tok k
    pred.squeeze_(0)
    top_k_prob, top_k_idx = torch.topk(pred, topk)
    # get idx_to_class
    idx_to_class = {v: k for k, v in model.class_to_idx.items()}
    # return
    return [x.item()
            for x in top_k_prob], [idx_to_class[x.item()] for x in top_k_idx]
Ejemplo n.º 6
0
def getAccuracyWithModel(model, data_loader, use_gpu=True):
    # turn on eval model
    model.eval()
    if torch.cuda.is_available() and use_gpu:
        device = torch.device('cuda')
        pass
    else:
        device = torch.device('cpu')
        pass
    model.to(device)
    # get accuracy
    accuracy = 0.
    num_step = 0
    with torch.no_grad():
        for inputs, labels in data_loader:
            num_step += 1
            # prepare data
            inputs, labels = inputs.to(device), labels.to(device)
            # forward pass
            preds = model.forward(inputs)
            # update accuracy
            torch.exp_(preds)
            accuracy += (torch.max(preds, dim=1)[1] == labels).type(
                torch.FloatTensor).mean()
            pass
        accuracy /= num_step
        pass
    return accuracy
Ejemplo n.º 7
0
def get_boxes(predictions: torch.Tensor, image_shape, p=0.5):
    image_w, image_h = image_shape
    grid_size = predictions.shape[-1]
    assert predictions.shape == (5, grid_size, grid_size)

    cx = torch.as_tensor(grid_centers_1d(image_w, grid_size),
                         dtype=torch.float32)
    cy = torch.as_tensor(grid_centers_1d(image_h, grid_size),
                         dtype=torch.float32)
    cx2d, cy2d = torch.meshgrid(cx, cy)
    cx2d, cy2d = cx2d.T, cy2d.T
    cx2d, cy2d = cx2d.to(predictions.device), cy2d.to(predictions.device)

    torch.exp_(predictions[3:])
    predictions[1] *= image_w / grid_size
    predictions[2] *= image_h / grid_size
    predictions[3] *= image_w / grid_size
    predictions[4] *= image_h / grid_size

    predictions[1] += cx2d - predictions[3] / 2
    predictions[2] += cy2d - predictions[4] / 2
    predictions[3:5] += predictions[1:3]

    predictions = predictions.view(-1, grid_size**2)
    # [[logit, x1, y1, x2, y2] for _ in boxes if sigmoid(logit) >= p]
    return predictions[:, predictions[0] >= np.log(p / (1 - p))]
Ejemplo n.º 8
0
def kernelmat(X, sigma=None):
    """ kernel matrix baker
        Args
            X             (tensor) shape (batchsize, dims)
            sigma         (float [None]) from config
    """
    m, dim = X.size()
    H = torch.eye(m, device=X.device).sub_(1 / m)
    Kx = distmat(X)

    if sigma:
        variance = 2. * sigma * sigma * dim
        torch.exp_(Kx.mul_(-1.0 / variance))
    else:
        try:
            sx = sigma_estimation(X, X)
            variance = 2. * sx * sx
            torch.exp_(Kx.mul_(-1.0 / variance))
        except RuntimeError as e:
            raise RuntimeError(
                "Unstable sigma {} with maximum/minimum input ({},{})".format(
                    sx, torch.max(X), torch.min(X)))

    Kxc = torch.mm(Kx, H)
    del H
    del Kx
    return Kxc
Ejemplo n.º 9
0
def safe_real_exp(values: torch.Tensor) -> torch.Tensor:
    assert values.dim() == 2 and values.size(1) == 2
    amplitude = values[:, 0]
    amplitude -= torch.max(amplitude)
    torch.exp_(amplitude)
    phase = values[:, 1]
    phase /= 3.141592653589793
    torch.round_(phase)
    torch.abs_(phase)
    phase = torch.fmod(phase, 2.0)
    return amplitude * (1.0 - 2.0 * phase)
Ejemplo n.º 10
0
    def validate(self) -> float:
        acc = 0
        for (images, labels) in self.val_data:
            images = images.view(-1, 28 * 28)
            with torch.no_grad():
                preds = self.model(images)

            torch.exp_(preds)
            acc = self.metric_func(preds, labels)

        acc = round(acc, 4) * 100
        print("Current Accuracy: {}%".format(acc))
        return acc
 def forward(self, mu, logvar):
     logstd = 0.5 * logvar
     std = torch.exp_(logstd)
     if self.training:
         z = torch.randn_like(std, dtype=torch.float32) * std + mu
     else: 
         z = mu
     return z
Ejemplo n.º 12
0
    def __init__(self, n_params, num_samples):
        super().__init__()
        self.N = n_params

        self.a = nn.Linear(1, self.N, bias=False)
        torch.exp_(self.a.weight.data)
        self.b = nn.Linear(1, self.N, bias=False)
        torch.abs_(self.b.weight.data)

        self.pz = MultivariateNormal(torch.zeros(self.N),
                                     torch.eye(self.N))  # latent distribution
        # self.px = Uniform(-1*torch.ones(self.N) + 3, 3*torch.ones(self.N)) # target distribution
        self.px = MultivariateNormal(
            torch.zeros(self.N) + 4,
            3 * torch.eye(self.N))  # target distribution

        self.num_samples = num_samples
 def reparam(self, mu, logvar):
     logstd = 0.5 * logvar
     std = torch.exp_(logstd)
     if self.training:
         z = torch.randn_like(std) * std + mu
     else: 
         z = mu
     return z
Ejemplo n.º 14
0
def mish(input, inplace = False):
    '''
    Applies the mish function element-wise:

    .. math::

        mish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + e^{x}))

    See additional documentation for :mod:`echoAIAI.Activation.Torch.mish`.
    '''
    if inplace:
        inp = input.clone()
        torch.exp_(input)
        input += 1
        torch.tanh_(torch.log_(input))
        input *= inp
        return input
    else:
        return input * torch.tanh(F.softplus(input))
Ejemplo n.º 15
0
def predictWithModel(image_path, model, topk=3, cat_to_name={}, use_gpu=True):
    """this function make predications on topk probs and cats/names given
    input image path

    :image_path: path to image for prediction
    :model: trained network model
    :cat_to_name: dict mapping category to class name
    :use_gpu: use cuda or not
    :returns: return list of topk probabilities and categories/classes

    """
    # turn on eval model
    model.eval()
    if torch.cuda.is_available() and use_gpu:
        device = torch.device('cuda')
        pass
    else:
        device = torch.device('cpu')
        pass
    model.to(device)
    # make prediction
    with torch.no_grad():
        # load image
        im = Image.open(image_path)
        im = test_transforms(im)
        im.unsqueeze_(0)
        # prepare data
        im = im.to(device)
        # forward pass
        pred = model.forward(im)
        torch.exp_(pred)
        pred.squeeze_(0)
        pass
    # top k
    probs, top_idx = torch.topk(pred, topk)
    probs = [x.item() for x in probs]
    classes = [model.idx_to_class[x.item()] for x in top_idx]
    if cat_to_name:
        classes = [cat_to_name[x] for x in classes]
        pass
    # return
    return probs, classes
Ejemplo n.º 16
0
    def _compute_gaussian_kernel(opts: Settings, device: th.device):
        """Compute a gaussian kernel with the given kernel size and standard
        deviation."""
        delta = th.arange(
            opts.kernel_size,
            device=device) - opts.kernel_size // 2

        gauss_1d = th.exp_(-th.square_(delta) / (2.0 * opts.sigma**2))
        # NOTE(ycho): Don't divide by sum...
        # gauss_1d /= gauss_1d.sum()
        out = gauss_1d[None, :] * gauss_1d[:, None]
        return out
Ejemplo n.º 17
0
def get_embed_mask(x_embed, k=3, r=1, hardness_lambda=0.1, isL2Dist=False):
    """
    args:
       x_embed: embedding feature, [N, C, H, W], e.g., C = F = 64
       args_dict : parameters 
    """

    #print ('Calculating mask:')
    dist = im2dist(x_embed, k=k, r=1, isL2Dist=isL2Dist)  #[N, k*k,H,W]
    mask = torch.exp_(-hardness_lambda * dist)  # [N,k*k,H,W]
    mask_sum = torch.sum(mask, dim=1, keepdim=True)  #[N,1,H,W]
    return mask_sum  # [N,1,H,W]
Ejemplo n.º 18
0
def predict_transform(prediction,
                      input_size,
                      anchors,
                      num_classes,
                      gpu_enabled=False):
    """
    This function consists of two steps
    1: It takes a [batch_size, 3*(5+num_classes), h, w] feature map
       and transforms it to a list of single bbox predictions
    2: It takes the output of (1) and maps the bbox prediction parameters t_x, t_y, t_w, t_h, p_o
       to actual bboxes. For more information see the paper (yolo9000 or yolo v3)
    NB gpu_enabled is currently not properly implemented!
    input
    pred:        feature map
    input_size:  input size of the net's input image
    anchors:     list of anchors related to the current feature map (anchor=[x_prior, y_prior])
    num_classes: number of classes, 80 for COCO
    """
    pred = prediction
    h, w = pred.shape[2], pred.shape[3]
    C = num_classes
    n_anchors = len(anchors)
    scale = input_size // h * 1.

    # step 1
    assert (pred.shape[1] == n_anchors*(5+C)), \
        'predict_transform() expected 255 feature channel predictions. Received {}.'.format(pred.shape[1])

    pred = pred.permute(
        0, 2, 3,
        1).contiguous()  # put the bboxes for single location in last dimension
    pred = pred.view(-1, h * w * n_anchors, 5 +
                     C)  # squash all dims except for a single bbox dim (5+C)

    # step 2
    # this part is a bit trickier
    # the first 5 slices of pred (pred[:,:,:5])
    # correspond (in order) to these predictions: t_x, t_y, t_w, t_h, p_o

    ########## calculating bbox centers from t_x, t_y ##########
    # first create a meshed and warped version of the cell offset parameters c_x and c_y
    # N.B. we first need x then y (opposite of standard image dimension ordering of h then w)
    c_x, c_y = np.meshgrid(np.arange(w),
                           np.arange(h))  # create mesh coordinates
    c_x, c_y = c_x.reshape(-1)[:, None], c_y.reshape(
        -1)[:, None]  # flatten mesh coordinates
    c_xy_offset = np.concatenate((c_x, c_y),
                                 axis=1)  # concatenate x and y coordinates
    c_xy_offset = np.tile(c_xy_offset, (1, n_anchors)).reshape(
        -1, 2)  # tile by the amount of anchors
    c_xy_offset = torch.tensor(
        c_xy_offset,
        dtype=torch.float)[None, :]  # cast to torch, create batch dimensions

    torch.sigmoid_(pred[:, :, :2]).add_(
        c_xy_offset)  # apply sigmoid, add cell offset
    pred[:, :, :2].mul_(scale)  # scale pred location to net's input size

    ########## calculating bbox dimensions from t_w, t_h ##########
    anchors = np.tile(anchors, (h * w, 1))  # tile by the amount for cells
    anchors = torch.tensor(
        anchors,
        dtype=torch.float)[None, :]  # cast to torch, create batch dimensions

    torch.exp_(pred[:, :, 2:4]).mul_(
        anchors)  # apply exp and multiply by anchor dims

    ########## calculating objectness and class probabilities from p_o ##########
    pred[:, :, 4:] = torch.sigmoid(
        pred[:, :, 4:])  # sigmoid the objectness[4] & each class's[5:] score

    return pred
Ejemplo n.º 19
0
            loss.backward()

            # update weights
            optimin.step()

        else:
            print2(f"Epoch {epo+1}/{epochs}..>..>.>  " +
                   f"Training loss:{iterm_loss/len(traindownloader):.4f}")


# train the neural network
traniner(2)

# use trained model to make prediction
# load next dataset
img, label = next(iter(testdownloader))

# selection image at number 5
img = img[5]

# turn off the gradients, we are just making predictions
with tch.no_grad():
    # predict the image
    logpreds = model(img)

# compute the probabilities, our results are log probabilities
prob = tch.exp_(logpreds)

# display the results
helper.view_classify(img, prob, version='Fashion')
plt.show()
Ejemplo n.º 20
0
 def forward(self, data):
     condensed = self.condense_conv(data)
     diffs = [data[:, i, None, ...] - condensed[:, i :: self.in_channels, ...] for i in range(self.in_channels)]
     return torch.stack([torch.exp_(-sum([diff ** 2 for diff in diffs]) / (2 * self.sq_var[i])) for i in range(len(self.sq_var))], 1)
Ejemplo n.º 21
0
    EPOCH = 1
    # p1, p2 = 1, 1
    totaltime,losslist,precslist=[],[],[]
    precs0, recs0,i = 0, 0,0
    for epoch in range(EPOCH):
        time_start = time.time()
        for step, (x, yOffs, yConf) in enumerate(train_loader):
            (outConf, outOffs) = net(x)
            del x
           #torch.Size([15360, 3]) torch.Size([15360, 2])
            yConfl = yConf.type(torch.cuda.LongTensor)

            # if step == 0:
            #     p1 = 1 / loss_softmax(outConf, yConfl)
            #     p2 = 1 / loss_offset(outOffs, yOffs)
            tgt_noise = (torch.exp_(torch.div(torch.randn(*yOffs.shape) ,20))).type(torch.cuda.FloatTensor)
            mask=((yConf!=0).view((-1,1))).type(torch.cuda.FloatTensor)  #  Tensor Type match!!!!
            del yConf
            n=torch.sum(mask)
            # yOffsb=yOffs.type(torch.ByteTensor).cuda(non_blocking=True)
            # outOffs=outOffs.type(torch.ByteTensor).cuda(non_blocking=True)
            # print("=====================",n, "  \t  ", yOffs, outOffs,mask)
            # outOffs=(mask.mul(outOffs)).type(torch.Tensor).cuda(non_blocking=True)
            # yOffs = (mask.mul(yOffs)).type(torch.Tensor).cuda(non_blocking=True)
            a=loss_class(outConf.log().mul((1-outConf).pow(2)), yConfl)  #Focal loss
            if n > 0:
                b=torch.sqrt(torch.div(loss_offset(outOffs, torch.mul(yOffs , tgt_noise)),n))  #RMSE loss
            else:
                b=0
            del tgt_noise,mask,n,yConfl,yOffs
            # print(outConf.shape, yConfl.shape,mask.shape) #torch.Size([15360, 3]) torch.Size([15360]) which is correct
Ejemplo n.º 22
0
 def forward(self, x):
     hmap = self.hmap(x)
     wh = torch.exp_(self.w_h_(x))
     reg = self.reg_(x)
     wh = torch.cat([wh, reg], dim=1)
     return {'hmap': hmap, 'wh': wh}
Ejemplo n.º 23
0
def apply_batch_bilateral_filter(
        embed_in,
        sg_filter,
        win_width,
        sigma_v,
        device,
        dilation,
        x_in=None,
        reg_constant=1e-20  # regularization constant
):
    """ using im2col to change the filtering to element-wise matrix multiplication

    Performs standard bilateral filtering of an input image. 
    Padding is taken care by the inside function im2col_pytorch()!!!

    Args:
        embed_in   (Tensor)   embeding for generatin mask, in size [N,F,H,W]
        sg_filter  (Tensor)   spatial gaussia filter, given sigma_s as spatial gaussian std. dev.
        win_width    (float)  spatical gaussin filter half window size; 
        sigma_v      (float)  value gaussian std. dev.
        reg_constant (float)  optional regularization constant for pathalogical cases
        x_in       (Tensor)   input array, in size [N,C,H,W],
                              if not None, then apply the bilateral filter to it;

    Returns:
        result (Tensor) output bilateral filter and/or bilateral-filtered x_in
    """

    k = 2 * win_width + 1
    N, F, H, W = embed_in.shape[:]
    #print ('embed_in shape = ', embed_in.shape)
    """ apply im2col for embedding masking """
    embed_im2col = im2col_pytorch(embed_in, k,
                                  dilation)  # in shape [N, (k*k*F), H, W]
    embed_im2col = embed_im2col.view(N, F, k * k, H, W)
    #print ("sigma_v ", sigma_v)
    """ change to in-place operation to save GPU memory """
    embed_tile = embed_in.view(N, F, 1, H, W)
    embed_im2col -= embed_tile  # broadcasting
    rg_filter = torch.exp_(
        -0.5 * torch.sum(embed_im2col.pow_(2), dim=1, keepdim=True) /
        (sigma_v**2))  # [N,1,k*k,H,W]
    #print ('rg_filter :', rg_filter[0,0,:,20,20])
    sg_filter = sg_filter.view(1, 1, k * k, 1, 1)
    # in shape (N, 1, k*k, H, W)
    rg_filter *= sg_filter  # broadcasting

    if x_in is None:
        result = None
    else:
        """
        emplement eq (4) in W. Harley' paper 
        "Segmentation-Aware Convolutional Networks Using Local Attention Masks (ICCV'17)"
        """
        filter_sum = torch.ones([N, 1, H, W], device=device) * reg_constant
        """ broadcasting """
        #unrolled for element-wise multiplication:
        N, C, H, W = x_in.shape[:]
        x_in_im2col = im2col_pytorch(x_in, k,
                                     dilation)  # in shape [N, C*k*k,H,W]
        x_in_im2col = x_in_im2col.view(N, C, k * k, H,
                                       W)  # in shape [N,C,k*k,H,W]

        #(N, C, k*k, H, W ) * (N, 1, k*k, H, W) => (N, C, k*k, H, W)
        result = x_in_im2col * rg_filter
        result = torch.sum(result, axis=2)  # N x C x H x W
        filter_sum += torch.sum(rg_filter, axis=2)  # N x 1 x H x W
        # normalize the result and return
        # N x C x H x W
        result = result / filter_sum

        if 0:  # show
            print('rg_filter shape = ', rg_filter.shape)
            for i in range(0, k * k):
                shft_x = i % (2 * win_width + 1) - win_width
                shft_y = i / (2 * win_width + 1) - win_width
                print("show shift [%d][%d] " % (shft_y, shft_x))
                pfm.show(rg_filter[0, 0, i, :, :].cpu().numpy())

    return rg_filter, result, filter_sum
Ejemplo n.º 24
0
# Now, use PyTorch tensors to complete the following computation:
# 
# Create a tensor of integers from the range 0 to 99, inclusive. Add 0.5 to each element in the tensor, and square each element of the result. Then, negate each element of the tensor, and apply the exponential to each element (i.e., change each element x into e^x). Now, sum all the elements of the tensor. Multiply this tensor by 2 and square each element and print your result.
# 
# If you're right, you should get something very close to $$\pi \approx 3.14 .$$

# In[5]:


val = torch.arange(100).float()
val+=0.5 #Adds 0.5 to each element
#val = val*val #Multiply element wise
val = torch.pow(val,2)
val *= -1 #Negate (times -1)
torch.exp_(val) #Inplace for exp^x
val=torch.sum(val)*2
val=torch.pow(val,2)

### YOUR CODE HERE

print(val)


# Now we'll try writing a computation that's prevalent throughout a lot of deep learning algorithms - calculating the softmax function:
# $$softmax(x_i) = \frac{e^{x_i}}{\sum_{j = 0}^{n - 1} e^{x_j}}$$
# Calculate the softmax function for the $val$ tensor below where $n$ is the number of elements in $val$, and $x_i$ is each element in $val$. DO NOT use the built-in softmax function. We should end up with a tensor that represents a probability distribution that sums to 1. (hint: you should calculate the sum of the exponents first)

# In[6]:

 def forward(self, input):
     self.input = input
     self.output = 1 / (1 + torch.exp_(-self.input))
     return self.output
Ejemplo n.º 26
0
 def forward(self, mu, logvar):
     logstd = 0.5 * logvar
     std = torch.exp_(logstd)
     z = torch.randn_like(std, dtype=torch.float32) * std + mu
     return z
Ejemplo n.º 27
0
def synmean(a, syntrain, iterations=1000000):
    with torch.no_grad():
        # note aimag defined with plus here, exp below modified consistently
        # [areal] = [aimag] = nbasis x nsignals
        areal, aimag = torch.t(a[:, 0::2]), torch.t(a[:, 1::2])

        # [anorm] = nsignals
        anorm = torch.sum(areal * areal + aimag * aimag, dim=0)

        cnt = 0
        mean, square, cov, norm = 0.0, 0.0, 0.0, 0.0
        adapt = None
        while cnt < iterations:
            # [x] = nbatch
            x, _, alpha = syntrain()
            cnt = cnt + alpha.shape[0]

            x = numpy2cuda(x, alpha.dtype == torch.float32)

            # [alphareal] = [alphaimag] = nbatch x nbasis
            alphareal, alphaimag = alpha[:, 0::2], alpha[:, 1::2]

            # [alphanorm] = nbatch
            alphanorm = torch.sum(alphareal * alphareal +
                                  alphaimag * alphaimag,
                                  dim=1)

            # [like] = nbatch x nsignals = [(nbatch x nbasis) @ (nbasis x nsignals) + (nbatch x 1) + nsignals]
            # like = alphareal @ areal + alphaimag @ aimag - 0.5*alphanorm.unsqueeze(1) - 0.5*anorm
            like = alphareal @ areal
            like += alphaimag @ aimag
            like -= 0.5 * alphanorm.unsqueeze(1)
            like -= 0.5 * anorm

            if adapt is None:
                adapt = torch.max(like, dim=0)[0]

            like -= adapt
            like = torch.exp_(like)  # in-place operation

            # [mean] = nsignals = nbatch @ (nbatch x nsignals)
            # in the 2D case, 2 x nsignals = 2 x nbatch @ (nbatch x nsignals)
            mean += torch.t(x) @ like
            square += (torch.t(x)**2) @ like

            if x.dim() == 2:
                cov += (x[:, 0] * x[:, 1]) @ like

            # [norm] = nsignals
            norm += torch.sum(like, dim=0)

            # note this tensor is very large (iter x len(a)); we should get rid of it asap
            del like

        # naive variance algorithm, hope for best, see also
        # https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance

        # [ret] = nsignals
        retmean = mean / norm
        reterr = torch.sqrt(square / norm - retmean * retmean)

        if x.dim() == 1:
            return cuda2numpy(retmean).T, cuda2numpy(reterr).T
        else:
            retcov = cov / norm - retmean[0, :] * retmean[1, :]
            return cuda2numpy(retmean).T, cuda2numpy(reterr).T, cuda2numpy(
                retcov)
Ejemplo n.º 28
0
b = torch.ones(1, 2, 4, 3)
print(a.matmul(b))
print(a.matmul(b).shape)

# 指数运算
a = torch.tensor([1, 2])
print(torch.pow(a, 3))
print(a.pow(3))
print(a**3)
print(a.pow_(3))

# exp
a = torch.tensor([1, 2], dtype=torch.float32)
print(a.type())
print(torch.exp(a))
print(torch.exp_(a))
print(a.exp())
print(a.exp_())

# 对数
a = torch.tensor([10, 2], dtype=torch.float32)
print(torch.log(a))
print(torch.log_(a))
print(a.log())
print(a.log_())

# sqart
a = torch.tensor([10, 2], dtype=torch.float32)
print(torch.sqrt(a))
print(torch.sqrt_(a))
print(a.sqrt())
Ejemplo n.º 29
0
def packernel2d(input,
                mask=None,
                kernel_size=0,
                stride=1,
                padding=0,
                output_padding=0,
                dilation=1,
                kernel_type='gaussian',
                smooth_kernel_type='none',
                smooth_kernel=None,
                inv_alpha=None,
                inv_lambda=None,
                channel_wise=False,
                normalize_kernel=False,
                transposed=False,
                native_impl=False):
    kernel_size = _pair(kernel_size)
    dilation = _pair(dilation)
    padding = _pair(padding)
    output_padding = _pair(output_padding)
    stride = _pair(stride)
    output_mask = False if mask is None else True
    norm = None

    if mask is not None and mask.dtype != input.dtype:
        mask = torch.tensor(mask, dtype=input.dtype, device=input.device)

    if transposed:
        in_sz = tuple(
            int((o - op - 1 - (k - 1) * d + 2 * p) // s) + 1
            for (o, k, s, p, op,
                 d) in zip(input.shape[-2:], kernel_size, stride, padding,
                           output_padding, dilation))
    else:
        in_sz = input.shape[-2:]

    if mask is not None or normalize_kernel:
        mask_pattern = input.new_ones(1, 1, *in_sz)
        mask_pattern = nd2col(mask_pattern,
                              kernel_size,
                              stride=stride,
                              padding=padding,
                              output_padding=output_padding,
                              dilation=dilation,
                              transposed=transposed)
        if mask is not None:
            mask = nd2col(mask,
                          kernel_size,
                          stride=stride,
                          padding=padding,
                          output_padding=output_padding,
                          dilation=dilation,
                          transposed=transposed)
            if not normalize_kernel:
                norm = mask.sum(dim=2, keepdim=True).sum(dim=3, keepdim=True) \
                       / mask_pattern.sum(dim=2, keepdim=True).sum(dim=3, keepdim=True)
        else:
            mask = mask_pattern

    if transposed:
        stride = _pair(1)
        padding = tuple(
            (k - 1) * d // 2 for (k, d) in zip(kernel_size, dilation))

    if native_impl:
        bs, k_ch, in_h, in_w = input.shape

        x = nd2col(input,
                   kernel_size,
                   stride=stride,
                   padding=padding,
                   dilation=dilation)
        x = x.view(bs, k_ch, -1, *x.shape[-2:]).contiguous()

        if smooth_kernel_type == 'none':
            self_idx = kernel_size[0] * kernel_size[1] // 2
            feat_0 = x[:, :, self_idx:self_idx + 1, :, :]
        else:
            smooth_kernel_size = smooth_kernel.shape[2:]
            smooth_padding = (
                int(padding[0] - (kernel_size[0] - smooth_kernel_size[0]) / 2),
                int(padding[1] - (kernel_size[1] - smooth_kernel_size[1]) / 2))
            crop = tuple(-1 * np.minimum(0, smooth_padding))
            input_for_kernel_crop = input.view(-1, 1, in_h,
                                               in_w)[:, :,
                                                     crop[0]:_neg_idx(crop[0]),
                                                     crop[1]:_neg_idx(crop[1])]
            smoothed = F.conv2d(input_for_kernel_crop,
                                smooth_kernel,
                                stride=stride,
                                padding=tuple(np.maximum(0, smooth_padding)))
            feat_0 = smoothed.view(bs, k_ch, 1, *x.shape[-2:])
        x = x - feat_0
        if kernel_type.find('_asym') >= 0:
            x = F.relu(x, inplace=True)
        # x.pow_(2)  # this causes an autograd issue in pytorch>0.4
        x = x * x
        if not channel_wise:
            x = torch.sum(x, dim=1, keepdim=True)
        if kernel_type == 'gaussian':
            x = torch.exp_(
                x.mul_(-0.5)
            )  # TODO profiling for identifying the culprit of 5x slow down
            # x = torch.exp(-0.5 * x)
        elif kernel_type.startswith('inv_'):
            epsilon = 1e-4
            x = inv_alpha.view(1, -1, 1, 1, 1) \
                + torch.pow(x + epsilon, 0.5 * inv_lambda.view(1, -1, 1, 1, 1))
        else:
            raise ValueError()
        output = x.view(*(x.shape[:2] + tuple(kernel_size) +
                          x.shape[-2:])).contiguous()
    else:
        assert (smooth_kernel_type == 'none' and kernel_type == 'gaussian')
        output = GaussKernel2dFn.apply(input, kernel_size, stride, padding,
                                       dilation, channel_wise)

    if mask is not None:
        output = output * mask  # avoid numerical issue on masked positions

    if normalize_kernel:
        norm = output.sum(dim=2, keepdim=True).sum(dim=3, keepdim=True)

    if norm is not None:
        empty_mask = (norm == 0)
        # output = output / (norm + torch.tensor(empty_mask, dtype=input.dtype, device=input.device))
        output = output / (norm + empty_mask.clone().detach())
        output_mask = (1 - empty_mask) if output_mask else None
    else:
        output_mask = None

    return output, output_mask
Ejemplo n.º 30
0
 def __call__(self, sample):
     sample.clamp_(0)
     torch.exp_(sample)
     sample -= 1
     sample.div_(self.gamma)
     return sample.clamp_(min=self.min_clip)