Skip to content

Instantly share code, notes, and snippets.

# https://stackoverflow.com/a/45925049/13369757
import matplotlib.pyplot as plt
import numpy as np
fig, host = plt.subplots(figsize=(12,10)) # (width, height) in inches
par1 = host.twinx()
par2 = host.twinx()
host.set_xlabel("Threshold")
import numpy as np
import torch
import pickle
from torch.utils.data import Dataset, DataLoader
# 提取特徵
def extract(loader):
featureExtractor.eval()
features = None
with torch.no_grad():
@e96031413
e96031413 / CopyFileFromPathInTXT.py
Created December 27, 2021 08:14
Copy file to a specific folder with paths in a txt file
import os
import shutil
fileListingFile = "missclassified_file_path.txt"
outputDir = "./"
# sample path in txt: /root/notebooks/XXXXX/datasets/XXXXXXXX.jpg
with open(fileListingFile, "r") as file:
fileNames = [file.strip() for file in file.readlines()]
@e96031413
e96031413 / PyTorchDataloader_wo_StopIteration.py
Last active December 20, 2021 08:06
Iterating over PyTorch dataloader without StopIteration using try/except
batch_iterator = iter(dataloader)
images, targets = next(batch_iterator)
try:
images, targets = next(batch_iterator)
except StopIteration:
batch_iterator = iter(data_loader)
images, targets = next(batch_iterator)
# https://github.com/rajatkoner08/oodformer/blob/master/networks/resnet.py
'''ResNet in PyTorch.
BasicBlock and Bottleneck module is from the original ResNet paper:
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Deep Residual Learning for Image Recognition. arXiv:1512.03385
PreActBlock and PreActBottleneck module is from the later paper:
[2] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Identity Mappings in Deep Residual Networks. arXiv:1603.05027
Original code is from https://github.com/kuangliu/pytorch-cifar/blob/master/models/resnet.py
'''
@e96031413
e96031413 / pytorch_tensor_to_img.py
Created December 10, 2021 02:20
從PyTorch的Tensor資料中,將資料保存成實體圖片檔案
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'
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)
# 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)
# 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)
# 針對每個元件計算其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