Skip to content

Instantly share code, notes, and snippets.

View EckoTan0804's full-sized avatar
🔥

EckoTan EckoTan0804

🔥
View GitHub Profile
@EckoTan0804
EckoTan0804 / unnormalize_tensor.py
Created March 12, 2023 10:25
Unnormalize normalized PyTorch tensor
class UnNormalize(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
"""
Args:
tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
Returns:
# Place this file to the root of the project
# Adjust based on your needs
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-added-large-files # Prevent giant files from being committed.
- id: check-ast # Simply check whether files parse as valid python.
- id: fix-byte-order-marker # Removes UTF-8 byte order marker.
@EckoTan0804
EckoTan0804 / img_concat.py
Created July 5, 2021 09:24
Concat image horizontally
from PIL import Image
images = [Image.open(x) for x in ['Test1.jpg', 'Test2.jpg', 'Test3.jpg']]
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
@EckoTan0804
EckoTan0804 / model_params_summary.py
Created June 1, 2021 19:41
Summary of model's parameters
from prettytable import PrettyTable
def count_parameters(model):
table = PrettyTable(["Module", "Parameters"])
# Assume the model consists of three main components: backbone, transformer, head
module_param_dict = {
"backbone": 0,
"transformer": 0,
"head": 0
@EckoTan0804
EckoTan0804 / measure_performance.py
Last active February 10, 2023 22:33
Measure runtime performance of PyTorch model
import math
import torch
import numpy as np
from torchvision.models import resnet18, ResNet18_Weights # TODO: Import your model
from tqdm import tqdm
from rich import print
# ========== TODO: Adjust based on your needs ==========
MODEL = resnet18(weights=ResNet18_Weights.IMAGENET1K_V1)
import argparse
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
import torchvision
import torchvision.transforms as T
from torchvision.datasets import ImageFolder
"""
This is a batched LSTM forward and backward pass
"""
import numpy as np
import code
class LSTM:
@staticmethod
def init(input_size, hidden_size, fancy_forget_bias_init = 3):
@EckoTan0804
EckoTan0804 / save_figure.py
Last active March 2, 2021 20:22
Save figure
import os
import matplotlib.pyplot as plt
def save_fig(
fig, fig_name, fig_dir, tight_layout=True, padding=False, fig_extension="png", resolution=300, transparent=True
):
"""
Save figure
Parameters
----------