Skip to content

Instantly share code, notes, and snippets.

import torch
import torch.nn as nn
import numpy as np
from onnx_coreml import convert
from torch.autograd import Variable
import torch.onnx
import torchvision
import onnx
model_name = "dog_vs_cat_image.onnx"
class ImageScale(nn.Module):
def __init__(self):
super().__init__()
self.denorminator = torch.full((3, sz, sz), 255.0, device=torch.device("cuda"))
def forward(self, x): return torch.div(x, self.denorminator).unsqueeze(0)
# We need to:
# - Add ImageScale by 255.0 at the front
# - Replace LogSoftmax layer with Softmax at the end to get probability instead of loss/cost
final_model = [ImageScale()] + (list(learn.model.children())[:-1] + [nn.Softmax()])
import torch
from torch import nn
class MyAdaptiveMaxPool2d(nn.Module):
def __init__(self, sz=None):
super().__init__()
self.p = nn.MaxPool2d((10, 10), padding=0)
# why (10, 10)? Because input image size is 299, \
# if you use 224, this should be (7, 7)
# if you want to know which number for other image size,
import torch
import torch.nn as nn
import numpy as np
from onnx_coreml import convert
from torch.autograd import Variable
import torch.onnx
import torchvision
import onnx
def check_onnx_compatible(model, model_name, sz, input_names, output_names):
dummy_input = Variable(torch.randn(3, sz, sz)).cuda()
@jalola
jalola / d2s.py
Last active August 31, 2022 07:54
d2s pytorch
class DepthToSpace(nn.Module):
def __init__(self, block_size):
super(DepthToSpace, self).__init__()
self.block_size = block_size
self.block_size_sq = block_size*block_size
def forward(self, input):
output = input.permute(0, 2, 3, 1)
(batch_size, d_height, d_width, d_depth) = output.size()
s_depth = int(d_depth / self.block_size_sq)
//alpha = 1; beta = 3.14
private static long retrieval(double x) {
createPascal((int) x);
long sum = 0;
int level = (int) (x / beta) - 1; // first level
int k = level;
int remember = 0;
while (k >= 0) {
double delta = x - (level - k) * alpha - k * beta; // the last P will be processed first