Skip to content

Instantly share code, notes, and snippets.

View yunjey's full-sized avatar

Yunjey Choi yunjey

View GitHub Profile
@yunjey
yunjey / make_dataset.py
Created June 29, 2017 08:36
make black/blonde dataset for cyclegan
import os
from PIL import Image
# Attribute
f = open('./list_attr_celeba.txt', 'r')
text = f.readlines()
# Number of images
text[0]
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)
@yunjey
yunjey / download_flickr_image.py
Last active December 26, 2023 15:22
downloading images from flickr using python-flickr
# 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)
# 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 / contextual_loss.py
Created September 20, 2018 08:54
pytorch implementation of contextual loss: https://arxiv.org/abs/1803.02077
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)
"""
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__()
find . -type d -print0 | while read -d '' -r dir; do
files=("$dir"/*)
printf "%5d files in directory %s\n" "${#files[@]}" "$dir"
done
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)
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()
{
"tench": 0,
"goldfish": 1,
"great white shark": 2,
"tiger shark": 3,
"hammerhead": 4,
"electric ray": 5,
"stingray": 6,
"cock": 7,
"hen": 8,