Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dbatra1 authored and abhshkdz committed Sep 22, 2015
0 parents commit a081cc6
Show file tree
Hide file tree
Showing 11 changed files with 812 additions and 0 deletions.
86 changes: 86 additions & 0 deletions 2_finetuning-alexnet-wikiart-style/download_wikiart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env python
"""
Download WikiArt images and write
Caffe ImageData layer files
This script has been forked from
https://github.com/BVLC/caffe/blob/master/examples/finetune_flickr_style/assemble_data.py
"""
import os
import urllib
import argparse
import numpy as np
import pandas as pd
from skimage import io
import multiprocessing

root_dirname = os.path.abspath(os.path.dirname(__file__))
training_dirname = os.path.join(root_dirname, 'data/wikiart')

def download_image(args_tuple):
"For use with multiprocessing map. Returns filename on fail."
try:
url, filename = args_tuple
if not os.path.exists(filename):
urllib.urlretrieve(url, filename)
test_read_image = io.imread(filename)
return True
except KeyboardInterrupt:
raise Exception() # multiprocessing doesn't catch keyboard exceptions
except:
return False

if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Download a subset of the WikiArt style dataset to a directory.')
parser.add_argument(
'-s', '--seed', type=int, default=0,
help="random seed")
parser.add_argument(
'-i', '--images', type=int, default=-1,
help="number of images to use (-1 for all [default])",
)
parser.add_argument(
'-w', '--workers', type=int, default=-1,
help="num workers used to download images. -x uses (all - x) cores [-1 default]."
)

args = parser.parse_args()
np.random.seed(args.seed)

# Read data, shuffle order, and subsample.
csv_filename = os.path.join(root_dirname, 'wikiart.csv.gz')
df = pd.read_csv(csv_filename, index_col=0, compression='gzip')
df = df.iloc[np.random.permutation(df.shape[0])]
if args.images > 0 and args.images < df.shape[0]:
df = df.iloc[:args.images]

# Make directory for images and get local filenames.
if training_dirname is None:
training_dirname = os.path.join(root_dirname, 'data/wikiart')
images_dirname = os.path.join(training_dirname, 'images')
if not os.path.exists(images_dirname):
os.makedirs(images_dirname)
df['image_filename'] = [
os.path.join(images_dirname, _ + '.jpg') for _ in df.index.values
]

# Download images.
num_workers = args.workers
if num_workers <= 0:
num_workers = multiprocessing.cpu_count() + num_workers
print('Downloading {} images with {} workers...'.format(
df.shape[0], num_workers))
pool = multiprocessing.Pool(processes=num_workers)
map_args = zip(df['image_url'], df['image_filename'])
results = pool.map(download_image, map_args)

# Only keep rows with valid images, and write out training file lists.
df = df[results]
for split in ['train', 'test']:
split_df = df[df['_split'] == split]
filename = os.path.join(training_dirname, '{}.txt'.format(split))
split_df[['image_filename', 'label']].to_csv(
filename, sep=' ', header=None, index=None)
print('Writing train/val for {} successfully downloaded images.'.format(
df.shape[0]))
11 changes: 11 additions & 0 deletions 2_finetuning-alexnet-wikiart-style/styles.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
label,style
0,Art Nouveau (Modern)
1,Baroque
2,Expressionism
3,Impressionism
4,Neoclassicism
5,Post-Impressionism
6,Realism
7,Romanticism
8,Surrealism
9,Symbolism
Binary file not shown.
163 changes: 163 additions & 0 deletions 3_visualizing-breaking-convnets/breaking-convnets.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Setup\n",
"import os\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.cm as cm\n",
"%matplotlib inline\n",
"\n",
"# Make sure that caffe is on the python path\n",
"caffe_root = ''\n",
"import sys\n",
"sys.path.insert(0, caffe_root + 'python')\n",
"\n",
"import caffe\n",
"\n",
"plt.rcParams['figure.figsize'] = (10, 10)\n",
"plt.rcParams['image.interpolation'] = 'nearest'\n",
"plt.rcParams['image.cmap'] = 'gray'\n",
"\n",
"model_prototxt = 'deploy.prototxt'\n",
"pretrained_model = os.path.join(caffe_root,'models/bvlc_alexnet/bvlc_alexnet.caffemodel')\n",
"\n",
"caffe.set_mode_cpu()\n",
"net = caffe.Classifier(model_prototxt, pretrained_model,\n",
" mean=np.load(os.path.join(caffe_root,'python/caffe/imagenet/ilsvrc_2012_mean.npy')).mean(1).mean(1),\n",
" channel_swap=(2,1,0),\n",
" raw_scale=255,\n",
" image_dims=(256, 256))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We start with the image of a cat and desired label as goose. We update input image for `n_iterations` iterations till it maximises class score for goose."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"image_file = 'cat_2.jpg'\n",
"input_image = caffe.io.load_image(image_file)\n",
"\n",
"n_iterations = 40\n",
"desired_label = 99 # goose\n",
"label = np.zeros((1,1,1,1000))\n",
"label[0,0,0,desired_label] = 1;\n",
"step_size = 1000\n",
"reg = 0.002\n",
"\n",
"input_image = net.transformer.preprocess('data',input_image)\n",
"fooling_image = input_image[:]\n",
"zero_image = np.zeros(fooling_image.shape)\n",
"\n",
"for i in range(n_iterations):\n",
" net.blobs['data'].data[...] = fooling_image\n",
" \n",
" # Perform forward pass\n",
" # TODO\n",
" # \n",
" # END OF YOUR CODE\n",
" \n",
" # Perform backward pass for the desired class\n",
" # TODO\n",
" # \n",
" # END OF YOUR CODE\n",
" \n",
" # Compute gradient and incremental update\n",
" # Store update value in di\n",
" # TODO\n",
" # \n",
" # di = \n",
" # END OF YOUR CODE\n",
" \n",
" fooling_image += di\n",
" zero_image += di\n",
" \n",
"plt.subplot(1,2,1)\n",
"plt.imshow(net.transformer.deprocess('data', zero_image))\n",
"plt.subplot(1,2,2)\n",
"plt.imshow(net.transformer.deprocess('data', fooling_image))\n",
"\n",
"# Save the image\n",
"plt.imsave('cat_fooled.jpg',net.transformer.deprocess('data', fooling_image))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we check our prediction on the modified image to make sure it is able to 'fool' the ConvNet."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['n01855672 goose' 'n01806567 quail' 'n01847000 drake'\n",
" 'n02018207 American coot, marsh hen, mud hen, water hen, Fulica americana'\n",
" 'n01592084 chickadee']\n"
]
}
],
"source": [
"image_file = 'cat_fooled.jpg'\n",
"net.blobs['data'].data[...] = net.transformer.preprocess('data',caffe.io.load_image(image_file))\n",
"prediction = net.forward()\n",
"# load labels\n",
"imagenet_labels_filename = os.path.join(caffe_root,'data/ilsvrc12/synset_words.txt')\n",
"try:\n",
" labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\\t')\n",
"except:\n",
" !../data/ilsvrc12/get_ilsvrc_aux.sh\n",
" labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\\t')\n",
"# sort top k predictions from softmax output\n",
"top_k = prediction['fc8'][0].flatten().argsort()[-1:-6:-1]\n",
"print labels[top_k]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Binary file added 3_visualizing-breaking-convnets/cat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 3_visualizing-breaking-convnets/cat_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit a081cc6

Please sign in to comment.