Skip to content

Instantly share code, notes, and snippets.

View richardhahahaha's full-sized avatar

Richard Chen richardhahahaha

View GitHub Profile
@richardhahahaha
richardhahahaha / fusing_bn.py
Created December 23, 2019 05:14
fusing batch normalization and convolution
import torch
import torchvision
def fuse(conv, bn):
fused = torch.nn.Conv2d(
conv.in_channels,
conv.out_channels,
kernel_size=conv.kernel_size,
stride=conv.stride,
@richardhahahaha
richardhahahaha / binary_conv.py
Created May 22, 2019 06:22 — forked from daquexian/binary_conv.py
A correct and dabnn-compatible binary convolution PyTorch implementation
# A correct and dabnn-compatible PyTorch implementation of binary convolutions.
# It consists of a implementation of the binary convolution itself, and the way
# to make the implementation both ONNX- and dabnn-compatible
# 1. The input of binary convolutions should only be +1/-1, so we pad -1 instead
# of 0 by a explicit pad operation.
# 2. Since PyTorch doesn't support exporting Sign ONNX operator (until
# https://github.com/pytorch/pytorch/pull/20470 gets merged), we perform sign
# operation on input and weight by directly accessing the `data`
import torch
-- Xception model
-- a Torch7 implementation of: https://arxiv.org/abs/1610.02357
-- E. Culurciello, October 2016
require 'nn'
local nClasses = 1000
function nn.SpatialSeparableConvolution(nInputPlane, nOutputPlane, kW, kH)
local block = nn.Sequential()
block:add(nn.SpatialConvolutionMap(nn.tables.oneToOne(nInputPlane), kW,kH, 1,1, 1,1))
# pylint: disable=C0111,too-many-arguments,too-many-instance-attributes,too-many-locals,redefined-outer-name,fixme
# pylint: disable=superfluous-parens, no-member, invalid-name
import sys
sys.path.insert(0, "../../python")
import mxnet as mx
import numpy as np
import cv2, random
from io import BytesIO
from captcha.image import ImageCaptcha
local cjson = require('cjson')
local util = require('login_util')
if ngx.var.request_method ~= 'POST' then
ngx.log(ngx.WARN, 'request method is not post')
ngx.exit(400)
end
ngx.req.read_body()
local args = ngx.req.get_post_args()
-- Two dashes start a one-line comment.
--[[
Adding two ['s and ]'s makes it a
multi-line comment.
--]]
----------------------------------------------------
-- 1. Variables and flow control.
----------------------------------------------------
local ffi = require("ffi")
ffi.cdef[[
typedef long time_t;
typedef struct timeval {
time_t tv_sec;
time_t tv_usec;
} timeval;
int gettimeofday(struct timeval* t, void* tzp);
(function() {
try {
var a = new Uint8Array(1);
return; //no need
} catch(e) { }
function subarray(start, end) {
return this.slice(start, end);
}