Skip to content

Instantly share code, notes, and snippets.

View shuuchen's full-sized avatar
🎯
Focusing

Shuchen Du shuuchen

🎯
Focusing
View GitHub Profile
@shuuchen
shuuchen / TripletLoss.py
Last active June 8, 2020 13:09
Triplet Loss
import torch
from torch import nn
from torch.nn.modules.distance import PairwiseDistance
class TripletLoss(nn.Module):
def __init__(self, margin=5.0):
super(TripletLoss, self).__init__()
self.margin = margin
@shuuchen
shuuchen / TripletNetwork.py
Created March 4, 2020 10:43
Triplet Network
class TripletNetwork(SiameseNetwork):
def __init__(self):
super().__init__()
def forward(self, input1, input2, input3):
output1 = self.forward_once(input1)
output2 = self.forward_once(input2)
output3 = self.forward_once(input3)
return output1, output2, output3
@shuuchen
shuuchen / SiameseNetwork.py
Created March 4, 2020 10:40
Siamese Network
class SiameseNetwork(nn.Module):
def __init__(self):
super(SiameseNetwork, self).__init__()
self.cnn1 = nn.Sequential(
nn.ReflectionPad2d(1),
nn.Conv2d(1, 4, kernel_size=3),
nn.ReLU(inplace=True),
nn.BatchNorm2d(4),
from collections import defaultdict
from heapq import *
def dijkstra(edges, f, t):
g = defaultdict(list)
for l, r, c in edges:
g[l].append((c, r))
q, seen, mins = [(0, f, ())], set(), {f: 0}
@shuuchen
shuuchen / union_find_tree.py
Last active June 20, 2019 14:31
An implementation of union find tree
class UT:
def __init__(self, n):
self.ps = list(range(n))
self.rs = [0] * n
def find(self, x):
if self.ps[x] != x:
self.ps[x] = self.find(self.ps[x])
return self.ps[x]
@shuuchen
shuuchen / install_nvidia_driver.txt
Last active June 10, 2019 09:56
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.
This post shows how to deal with the following error:
(base) [dushu@ip-172-20-149-210 ~]$ nvidia-smi
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver.
Make sure that the latest NVIDIA driver is installed and running.
Solution
Just install the latest nvidia driver.
Process
@shuuchen
shuuchen / trie.py
Created May 27, 2019 08:34
Implementation of trie
from collections import defaultdict
class TrieNode:
def __init__(self):
self.isWord = False
self.children = defaultdict(TrieNode)
class Trie:
def __init__(self):
@shuuchen
shuuchen / img_augment.py
Last active May 24, 2019 01:55
Using PyTorch functional APIs for image data augmentation
import torchvision.transforms.functional as F
import numpy as np
from PIL import Image
import os
from matplotlib import pyplot as plt
# input images
d = '../data/homes/test_input/'
img0 = Image.open(os.path.join(d, '352.jpg'))
img1 = Image.open(os.path.join(d, '95.jpg'))
@shuuchen
shuuchen / file0.txt
Last active September 10, 2018 03:41
Pytorch でシーケンスデータを順番で読込 ref: https://qiita.com/shuuchen/items/466dc7977a146f7f38f2
class ImageFolderWithPaths(datasets.ImageFolder):
"""Custom dataset that includes image file paths. Extends
torchvision.datasets.ImageFolder
"""
# override the __getitem__ method. this is the method dataloader calls
def __getitem__(self, index):
# this is what ImageFolder normally returns
original_tuple = super(ImageFolderWithPaths, self).__getitem__(index)
# the image file path
@shuuchen
shuuchen / file0.txt
Created August 14, 2018 07:16
pycharm で conda 環境を構築 (windows 版) ref: https://qiita.com/shuuchen/items/0b8b73894bc43aff370c
C:\Users\to>python
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import sys
>>> sys.executable
'C:\\Users\\to\\Anaconda3\\python.exe'