Skip to content

Instantly share code, notes, and snippets.

View xmfbit's full-sized avatar

xmfbit xmfbit

  • Bytedance
  • Beijing,China
View GitHub Profile
@xmfbit
xmfbit / fuse_conv_bn_in_torch_module.py
Created October 18, 2021 18:25
example of torch.fx, showing how to fuse the conv-bn module groups
""" Fuse conv-bn pattern in torch.Module, an example for torch.fx
see: https://pytorch.org/tutorials/intermediate/fx_conv_bn_fuser.html
"""
import copy
from typing import Tuple, Dict, Any
import torch
import torch.fx as fx
import torch.nn as nn
@xmfbit
xmfbit / data.py
Created September 27, 2017 02:38
Char level RNN generator
import os
class CharDataset(object):
def __init__(self, path):
if not os.path.exists(path):
raise RuntimeError('Cannot open the file: {}'.format(path))
self.raw_data = open(path, 'r').read()
self.chars = list(set(self.raw_data))
self.data_size = len(self.raw_data)
print('There are {} characters in the file'.format(self.data_size))
@xmfbit
xmfbit / model.py
Created September 27, 2017 02:25
A simple example of DCGAN on MNIST using PyTorch
import torch
import torch.nn as nn
import torch.nn.functional as F
def init_weight(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0., 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1., 0.02)
@xmfbit
xmfbit / model.py
Last active March 5, 2023 17:03
ResNet-164 training experiment on CIFAR10 using PyTorch, see the paper: Identity Mappings in Deep Residual Networks
import torch
import torch.nn as nn
import math
## the model definition
# see HeKaiming's implementation using torch:
# https://github.com/KaimingHe/resnet-1k-layers/blob/master/README.md
class Bottleneck(nn.Module):
expansion = 4 # # output cahnnels / # input channels
@xmfbit
xmfbit / FlowNetS-deploy.prototxt
Created June 21, 2017 10:37
Deploy prototxt file for FlowNetS, FlyingChairs dataset
# Enter your network definition here.
# Use Shift+Enter to update the visualization.
layer {
name: "CustomData1"
type: "CustomData"
top: "blob0"
top: "blob1"
top: "blob2"
top: "blob3"
include {
@xmfbit
xmfbit / logger.py
Last active July 19, 2018 15:20
a wrapper file for logging in Python
# -*- coding: UTF-8 -*-
# File: logger.py
# Author: Yuxin Wu <[email protected]>
from __future__ import print_function
import logging
import os
import errno
import shutil
import os.path
from datetime import datetime
@xmfbit
xmfbit / convert.py
Created March 15, 2017 09:19
convert yolo cfg file to caffe prototxt file. If there are different definitions of `reorg` and `region` layer params, change the code.
# convert yolo cfg file to caffe prototxt file
import sys
import argparse
TYPES = ["Conv", "ReLU", "Pool", "Route", "Reorg", "Region"]
ACTIVATION_TYPES = ['leaky', 'linear']
layer_names = [];
def HasConflictNameError(conflicted_name):
print 'Error! The layer name \"{}\" has been in the list.'.format(conflicted_name)
@xmfbit
xmfbit / pytorch_mnist.py
Last active March 4, 2023 19:45
an example of pytorch on mnist dataset
import os
import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torch.nn.functional as F
import torch.optim as optim
## load mnist dataset
use_cuda = torch.cuda.is_available()
@xmfbit
xmfbit / test_reorg_layer.cpp
Created January 16, 2017 08:48
test file for reorg_layer
#include <vector>
#include "gtest/gtest.h"
#include "caffe/common.hpp"
#include "caffe/blob.hpp"
#include "caffe/layers/reorg_layer.hpp"
#include "caffe/test/test_caffe_main.hpp"
namespace caffe {
template <typename TypeParam>
class ReorgLayerTest : public MultiDeviceTest<TypeParam> {
typedef typename TypeParam::Dtype Dtype;
@xmfbit
xmfbit / ocr_three_lstm.prototxt
Created September 28, 2016 02:00
3 lstm for ocr task
# has 3 lstm layer
name: "ocr"
layer {
name: "data"
type: "OCRData"
top: "data"
top: "label"
image_data_param {
is_color: false