Skip to content

Instantly share code, notes, and snippets.

View yunjey's full-sized avatar

Yunjey Choi yunjey

View GitHub Profile
@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)
@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)
"""
# 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
{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',
{
"tench": 0,
"goldfish": 1,
"great white shark": 2,
"tiger shark": 3,
"hammerhead": 4,
"electric ray": 5,
"stingray": 6,
"cock": 7,
"hen": 8,
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()
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)
find . -type d -print0 | while read -d '' -r dir; do
files=("$dir"/*)
printf "%5d files in directory %s\n" "${#files[@]}" "$dir"
done
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__()
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)