Skip to content

Instantly share code, notes, and snippets.

Avatar

Yunjey Choi yunjey

View GitHub Profile
View imagenet_idx2class.txt
{0: 'tench, Tinca tinca',
1: 'goldfish, Carassius auratus',
2: 'great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias',
3: 'tiger shark, Galeocerdo cuvieri',
4: 'hammerhead, hammerhead shark',
5: 'electric ray, crampfish, numbfish, torpedo',
6: 'stingray',
7: 'cock',
8: 'hen',
9: 'ostrich, Struthio camelus',
View imagenet_class2idx.json
{
"tench": 0,
"goldfish": 1,
"great white shark": 2,
"tiger shark": 3,
"hammerhead": 4,
"electric ray": 5,
"stingray": 6,
"cock": 7,
"hen": 8,
View test_requires_grad.py
import torch
x1 = torch.tensor(1.).requires_grad_(True)
x2 = torch.tensor(2.).requires_grad_(True)
w = torch.tensor(3.).requires_grad_(False)
y1 = w * x1
loss1 = torch.mean((y1-1)**2)
loss1.backward()
View resize.py
import os
from PIL import Image
import argparse
import time
import datetime
def main(config):
src_dir = config.src_dir
filenames = os.listdir(src_dir)
View count_files_for_each_dir.sh
find . -type d -print0 | while read -d '' -r dir; do
files=("$dir"/*)
printf "%5d files in directory %s\n" "${#files[@]}" "$dir"
done
View non_local.py
from torch.nn.utils import spectral_norm
import torch.nn.functional as F
import torch.nn as nn
import torch
class NonLocalBlock(nn.Module):
"""Non-local block."""
def __init__(self, conv_dim):
super(NonLocalBlock, self).__init__()
@yunjey
yunjey / contextual_loss.py
Created Sep 20, 2018
pytorch implementation of contextual loss: https://arxiv.org/abs/1803.02077
View contextual_loss.py
def contextual_loss(x, y, h=0.5):
"""Computes contextual loss between x and y.
Args:
x: features of shape (N, C, H, W).
y: features of shape (N, C, H, W).
Returns:
cx_loss = contextual loss between x and y (Eq (1) in the paper)
"""
View google_crawler.py
# pip install icrawler
from icrawler.builtin import GoogleImageCrawler
from datetime import date
google_crawler = GoogleImageCrawler(parser_threads=2, downloader_threads=3,
storage={'root_dir': './husky'}) # directory where images are downloaded
google_crawler.crawl(keyword='Siberian husky', max_num=1000, # max_num should be equal or less than 1000
date_min=date(2012, 3, 1), date_max=date(2012, 6, 1), # you can change date to get more than 1000 images
@yunjey
yunjey / download_flickr_image.py
Last active Sep 7, 2021
downloading images from flickr using python-flickr
View download_flickr_image.py
# First, you should install flickrapi
# pip install flickrapi
import flickrapi
import urllib
from PIL import Image
# Flickr api access key
flickr=flickrapi.FlickrAPI('c6a2c45591d4973ff525042472446ca2', '202ffe6f387ce29b', cache=True)
View attn_prob_summation.py
import torch
import numpy as np
# Hyper-parameters
vocab_size = 10
batch_size = 3
seq_length = 4
# Generate random indices of range [0, vocab_size)
word_indices = torch.from_numpy(np.random.randint(low=0, high=vocab_size, size=(batch_size, seq_length))).view(-1)