Skip to content

Instantly share code, notes, and snippets.

Avatar

e96031413

View GitHub Profile
@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):
View linked-list.py
# Linked list implementation in Python
# https://www.programiz.com/dsa/linked-list
# Linked list operations in Python
# Create a node
class Node:
def __init__(self, data):
self.data = data
self.next = None
View queue.py
# https://www.programiz.com/dsa/queue
# Queue implementation in Python
class Queue:
def __init__(self):
self.queue = []
# Add an element
View stack.py
# Stack implementation in python
# https://www.programiz.com/dsa/stack
# Creating a stack
def create_stack():
stack = []
return stack
# Creating an empty stack
View AdaIN_dataset.py
import os
import glob
import numpy as np
from tqdm import tqdm
import torch
from torch.utils.data import Dataset
from torchvision import transforms
from PIL import Image
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
View AdaIN_test.py
import os
import argparse
from PIL import Image
import torch
from torchvision import transforms
from torchvision.utils import save_image
from model import Model
output_folder = 'output_image'
if os.path.isfile(output_folder):
View modify_pytorch_output.py
def testing(UnNormalize, writer, val_loader, model, criterion, args, size_val_df, y_pred, y_true):
batch_time = AverageMeter('Time', ':6.3f')
losses = AverageMeter('Loss', ':.4e')
top1 = AverageMeter('Acc@1', ':6.2f')
top5 = AverageMeter('Acc@5', ':6.2f')
progress = ProgressMeter(
len(val_loader),
[batch_time, losses, top1, top5],
prefix='Test: ')
View tsne_mobilenetv2.py
for i, (img, target,_) in enumerate(tqdm(dataloader)):
feat_list = []
def hook(module, input, output):
# 由於MobileNetv2不像ResNet18有宣告self.avgpool(),因此我的作法是將模型卷積層的最後一層的輸出手動進行adaptive_avg_pool2d
# 接著將它加到feature_list中
feat_list.append(nn.functional.adaptive_avg_pool2d(output.clone(), (1, 1)).reshape(output.clone().shape[0], -1).detach())
@e96031413
e96031413 / check-image-path-in-dataloader.py
Created Jul 12, 2021
PyTorch如何列出分類錯誤之原始圖片路徑?
View check-image-path-in-dataloader.py
class CustomDataset(torch.utils.data.Dataset):
def __init__(self, dataframe, transform):
self.dataframe = dataframe
self.transform = transform
def __len__(self):
return len(self.dataframe)
def __getitem__(self, index):
row = self.dataframe.iloc[index]