Skip to content

Instantly share code, notes, and snippets.

from itertools import product
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import autograd
from torch.autograd.function import once_differentiable
class my_func(autograd.Function):
@staticmethod
def forward(ctx, x, scale=2.0):
@knsong
knsong / resnet18.py
Created January 16, 2020 08:16
resnet18.py
'''ResNet in PyTorch.
For Pre-activation ResNet, see 'preact_resnet.py'.
Reference:
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Deep Residual Learning for Image Recognition. arXiv:1512.03385
'''
import torch
import torch.nn as nn
@knsong
knsong / resnet18.py
Created January 16, 2020 08:16
resnet18.py
'''ResNet in PyTorch.
For Pre-activation ResNet, see 'preact_resnet.py'.
Reference:
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Deep Residual Learning for Image Recognition. arXiv:1512.03385
'''
import torch
import torch.nn as nn
@knsong
knsong / dali_externalsource.py
Last active January 10, 2020 09:36
dali_externalsource.py
import math
import os
import nvidia.dali.ops as ops
import nvidia.dali.types as types
import nvidia.dali.tfrecord as tfrec
from nvidia.dali.backend import oss
from nvidia.dali.pipeline import Pipeline
from nvidia.dali.plugin.pytorch import DALIGenericIterator, DALIClassificationIterator
from nvidia.dali.types import DALIDataType
Sample::Sample(){
LOG(INFO) << "Initializing model";
struct timeval begint, endt;
gettimeofday(&begint, NULL);
TF_Buffer* graph_def = read_file("./models.pb");
tf_graph_ = TF_NewGraph();
// Import graph_def into graph
TF_Status* status = TF_NewStatus();
TF_ImportGraphDefOptions* opts = TF_NewImportGraphDefOptions();
TF_GraphImportGraphDef(tf_graph_, graph_def, opts, status);
name: "CaffeNet"
layer {
name: "input"
type: "Input"
top: "data"
input_param {
shape {
dim: 1
dim: 3
#include <algorithm>
#include <vector>
#include "caffe/layers/relu_layer.hpp"
namespace caffe {
template <typename Dtype>
__global__ void ReLUForward(const int n, const Dtype* in, Dtype* out,
Dtype negative_slope, Dtype threshold) {
@knsong
knsong / regression_accuracy_layer.cpp
Last active October 15, 2016 04:41
regression accuracy layer for caffe
#include "caffe/layers/regression_accuracy_layer.hpp"
namespace caffe{
template <typename Dtype>
void RegressionAccuracyLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top){
vector<int> top_shape(0); // Accuracy is a scalar; 0 axes.
top[0]->Reshape(top_shape);
}
template <typename Dtype>