Skip to content

Instantly share code, notes, and snippets.

Avatar

e96031413

View GitHub Profile
@e96031413
e96031413 / pytorch_tensor_to_img.py
Created Dec 10, 2021
從PyTorch的Tensor資料中,將資料保存成實體圖片檔案
View pytorch_tensor_to_img.py
from torchvision.utils import save_image
features, imgs, labels, image_path = get_features_trained_weight(model, transform_dataset)
img1 = imgs[1] #第1張圖片
img1 = torch.from_numpy(img1) #格式轉換
save_image(img1, 'img1.png') #保存成'img1.png'
View tsne-tensorboard.py
def get_features_trained_weight(model, transform_dataset):
if torch.cuda.is_available():
device = 'cuda'
else:
device = 'cpu'
if isinstance(model,torch.nn.DataParallel):
model = model.module
model.eval()
model.to(device)
View calculate_params_and_macs_pytorch.py
# thop(simple)
from thop import profile
model = mobilenetv3_large(width_mult=args.width_mult, num_classes=2)
input = torch.randn(1, 3, 224, 224)
macs, params = profile(model, inputs=(input, ), verbose=False)
print("%.2f | %.2f" % (params / (1000 ** 2), macs / (1000 ** 3)))
# torch info(detailed)
from torchinfo import summary
model = mobilenetv3_large(width_mult=args.width_mult, num_classes=2)
View custom_pytorch_data_augmentation.py
# https://www.jianshu.com/p/9a9000d226b6
# https://stackoverflow.com/questions/64420379/is-it-possible-to-add-own-function-in-transform-compose-in-pytorch
import cv2
import torchvision.transforms as transforms
class hisEqulColor(object):
def __call__(self, img):
img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) # PyTorch的Transform要求PIL格式的圖片,若要使用OpenCV則需先進行轉換
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
View overkill_leakage.py
# 針對每個元件計算其Overkill rate和Leakage rate
# 目前只適用於batch size = 1的情形
# 用來產生overkill和leakage數值的dataframe
import pandas as pd
import torch
test_df_mapping2_label = test_df.copy() #複製一份要mapping到2個label的testing資料
test_df_mapping2_label.loc[test_df_mapping2_label['class'] == 0, 'class'] = 0 #將大於1的label轉成1
test_df_mapping2_label.loc[test_df_mapping2_label['class'] == 1, 'class'] = 1
test_df_mapping2_label.loc[test_df_mapping2_label['class'] == 2, 'class'] = 1
View max-heap.py
# https://www.programiz.com/dsa/heap-data-structure
# Max-Heap data structure in Python
def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[i] < arr[l]:
largest = l
View BalancedSoftmaxLoss.py
"""Copyright (c) Facebook, Inc. and its affiliates.
All rights reserved.
This source code is licensed under the license found in the
LICENSE file in the root directory of this source tree.
Portions of the source code are from the OLTR project which
notice below and in LICENSE in the root directory of
this source tree.
Copyright (c) 2019, Zhongqi Miao
All rights reserved.
"""
View dataset-mean-std-calculate.py
# https://github.com/pytorch/examples/blob/master/imagenet/main.py
import argparse
import os
import sys
import random
import shutil
import time
import warnings
from tqdm import tqdm
import pdb
@e96031413
e96031413 / all-cnn-resnet.py
Created Aug 31, 2021
ResNet without Pooling layer, Inspired by "Striving for Simplicity: The All Convolutional Net"
View all-cnn-resnet.py
import torch
from torch import Tensor
import torch.nn as nn
from .._internally_replaced_utils import load_state_dict_from_url
from typing import Type, Any, Callable, Union, List, Optional
__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
'resnet152', 'resnext50_32x4d', 'resnext101_32x8d',
View hash-table.py
# https://www.programiz.com/dsa/hash-table
# Python program to demonstrate working of HashTable
hashTable = [[],] * 10
def checkPrime(n):
if n == 1 or n == 0:
return 0
for i in range(2, n//2):