<jupyter_start><jupyter_text>Using Convolutional Neural Networks<jupyter_code>%matplotlib inline<jupyter_output><empty_output><jupyter_text>Define path to data: (It's a good idea to put it in a subdirectory of your notebooks folder, and then exclude that directory from git control by adding it to .gitignore.)<jupyter_code>#path = "data/dogscats/"
#path = "data/dogscats/sample/"
path = "data/kaggle/"<jupyter_output><empty_output><jupyter_text>A few basic libraries that we'll need for the initial exercises:<jupyter_code>from __future__ import division,print_function

import os, json
from glob import glob
import numpy as np
np.set_printoptions(precision=4, linewidth=100)
from matplotlib import pyplot as plt<jupyter_output><empty_output><jupyter_text>We have created a file most imaginatively called 'utils.py' to store any little convenience functions we'll want to use. We will discuss these as we use them.<jupyter_code>import utils; reload(utils)
from utils import plots<jupyter_output>WARNING (theano.sandbox.cuda): The cuda backend is deprecated and will be removed in the next release (v0.10).  Please switch to the gpuarray backend. You can get more information about how to switch at this URL:
 https://github.com/Theano/Theano/wiki/Converting-to-the-new-gpu-back-end%28gpuarray%29

Using gpu device 0: Tesla K80 (CNMeM is disabled, cuDNN not available)
Using Theano backend.<jupyter_text>Use a pretrained VGG model with our **Vgg16** class Our first step is simply to use a model that has been fully created for us, which can recognise a wide variety (1,000 categories) of images. We will use 'VGG', which won the 2014 Imagenet competition, and is a very simple model to create and understand. The VGG Imagenet team created both a larger, slower, slightly more accurate model (*VGG  19*) and a smaller, faster model (*VGG 16*). We will be using VGG 16 since the much slower performance of VGG19 is generally not worth the very minor improvement in accuracy.We have created a python class, *Vgg16*, which makes using the VGG 16 model very straightforward.   The punchline: state of the art custom model in 7 lines of codeHere's everything you need to do to get >97% accuracy on the Dogs vs Cats dataset - we won't analyze how it works behind the scenes yet, since at this stage we're just going to focus on the minimum necessary to actually do useful work.<jupyter_code># As large as you can, but no larger than 64 is recommended. 
# If you have an older or cheaper GPU, you'll run out of memory, so will have to decrease this.
batch_size=64
# Import our class, and instantiate
import vgg16; reload(vgg16)
from vgg16 import Vgg16
vgg = Vgg16()
# Grab a few images at a time for training and validation.
# NB: They must be in subdirectories named based on their category
batches = vgg.get_batches(path+'train', batch_size=batch_size)
val_batches = vgg.get_batches(path+'valid', batch_size=batch_size*2)
vgg.finetune(batches)
vgg.fit(batches, val_batches, nb_epoch=1)<jupyter_output>Found 16848 images belonging to 2 classes.
Found 8152 images belonging to 2 classes.
Epoch 1/1
16848/16848 [==============================] - 819s - loss: 0.1236 - acc: 0.9661 - val_loss: 0.0539 - val_acc: 0.9855[...]<jupyter_text>PredictionsPredict categories and save results for submission<jupyter_code># load test batch, make sure you put the test batch in a class subfolder
batches, preds = vgg.test(path+"test", batch_size = batch_size*2)
import csv

# get filenames
filenames = batches.filenames

with open("data/kaggle/my_submission.csv", "wb") as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=',')
    csvwriter.writerow(('id','label'))
    for i, pred in enumerate(preds):
        id = filenames[i].replace('unknown/','').replace('.jpg','') 
        #label = np.argmax(pred)
        label = pred[1]
        print(id,label,pred)                
        csvwriter.writerow((id,label))<jupyter_output>8317 0.20278 [ 0.7972  0.2028]
3804 2.03079e-08 [  1.0000e+00   2.0308e-08]
12358 1.32422e-05 [  9.9999e-01   1.3242e-05]
7186 0.942164 [ 0.0578  0.9422]
20 3.41625e-06 [  1.0000e+00   3.4162e-06]
5959 0.84136 [ 0.1586  0.8414]
10466 1.0 [  9.0699e-09   1.0000e+00]
12093 1.4398e-06 [  1.0000e+00   1.4398e-06]
330 9.34766e-05 [  9.9991e-01   9.3477e-05]
9411 8.95309e-08 [  1.0000e+00   8.9531e-08]
4883 0.999989 [  1.0616e-05   9.9999e-01]
10060 0.999999 [  1.1612e-06   1.0000e+00]
11193 0.999991 [  9.0689e-06   9.9999e-01]
7448 1.0 [  3.3594e-12   1.0000e+00]
8004 2.14534e-11 [  1.0000e+00   2.1453e-11]
10840 1.08517e-10 [  1.0000e+00   1.0852e-10]
9624 1.0 [  4.4534e-09   1.0000e+00]
4260 1.0 [  1.4939e-07   1.0000e+00]
9793 3.91881e-09 [  1.0000e+00   3.9188e-09]
8527 1.0 [  4.1880e-08   1.0000e+00]
5922 0.999983 [  1.7211e-05   9.9998e-01]
2344 4.13218e-11 [  1.0000e+00   4.1322e-11]
162 3.72848e-06 [  1.0000e+00   3.7285e-06]
12192 1.0 [  1.1966e-10   1.0000e+00]
871 1.0 [  2.0646e-[...]
{'fraction_non_alphanumeric': 0.23203645285664212, 'fraction_numerical': 0.11952330879775674, 'mean_word_length': 4.926272066458982, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '136368', 'license': 'Apache-2.0', 'n_tokens_mistral': 2972, 'n_tokens_neox': 2568, 'n_words': 577, 'path': 'deeplearning1/nbs/lesson1-homework.ipynb', 'repo_name': 'attibalazs/fastai-courses', 'size': '5706'}