<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Convolutional Neural Networks with 17flowers
A simple deep learning example on how to start classifying images with your own data.
This notebook is expected to be executed after 17flowers_data.ipynb (for data creation).
Setup
After preparing necessary environment, please make sure to have these files where you execute this notebook.
https
Step1:  Use a pretrained VGG model with our Vgg16 class
Step2:  The original pre-trained Vgg16 class classifies images into one of the 1000 categories. This number of categories depends on the dataset which Vgg16 was trained with. (http
Step3:  Use Vgg16 for basic image recognition
Let's grab batches of data from our test folder
Step4:  With finetuning, the model vgg outputs the probability of 17 classes. As you can see below, they are generally relevant to the input images. 
Note that these images are not used during model training.
Step5:  Without finetune?
Step6:  Again get some images for prediction
Step7:  Without finetuning, the model vgg2 outputs the probability of 1000 classes. As you can see below, they are sometimes totally irrelevant.
<ASSISTANT_TASK:>
Python Code:
from __future__ import division, print_function
%matplotlib inline
path = "data/17flowers/"
import os, json
from glob import glob
import numpy as np
np.set_printoptions(precision=4, linewidth=100)
from matplotlib import pyplot as plt
# check that ~/.keras/keras.json is set for Theano and includes "image_data_format": "channels_first"
from importlib import reload  # Python 3
import utils; reload(utils)
from utils import plots
Explanation: Convolutional Neural Networks with 17flowers
A simple deep learning example on how to start classifying images with your own data.
This notebook is expected to be executed after 17flowers_data.ipynb (for data creation).
Setup
After preparing necessary environment, please make sure to have these files where you execute this notebook.
https://github.com/roebius/deeplearning1_keras2/tree/master/nbs
- utils.py
- vgg16.py
End of explanation
# As large as you can, but no larger than 64 is recommended.
batch_size = 8
#batch_size = 64
# Import our class, and instantiate
import vgg16; reload(vgg16)
from vgg16 import Vgg16
Explanation: Use a pretrained VGG model with our Vgg16 class
End of explanation
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)
# please note that the vgg model is compiled inside the finetune method.
vgg.finetune(batches)
vgg.fit(batches, val_batches, batch_size, nb_epoch=1)
Explanation: The original pre-trained Vgg16 class classifies images into one of the 1000 categories. This number of categories depends on the dataset which Vgg16 was trained with. (http://image-net.org/challenges/LSVRC/2014/browse-synsets)
In order to classify images into the categories which we prepare (17 categories of flowers, in this notebook), fine-tuning technology is useful. It:
- keeps the most weights from the pre-trained Vgg16 model, but modifies only a few parts of the weights
- changes the dimension of the output layer (from 1000 to 17, in this notebook)
End of explanation
batches = vgg.get_batches(path+'test', batch_size=5)
imgs,labels = next(batches)
print(labels)
#plots(imgs, titles=labels)
plots(imgs)
Explanation: Use Vgg16 for basic image recognition
Let's grab batches of data from our test folder:
End of explanation
vgg.predict(imgs, True)
vgg.classes[:20]
Explanation: With finetuning, the model vgg outputs the probability of 17 classes. As you can see below, they are generally relevant to the input images. 
Note that these images are not used during model training.
End of explanation
# make new Vgg16 instance
vgg2 = Vgg16()
# need to compile the model before using
vgg2.compile()
Explanation: Without finetune?
End of explanation
batches = vgg2.get_batches(path+'test', batch_size=5)
imgs,labels = next(batches)
print(labels)
plots(imgs)
Explanation: Again get some images for prediction
End of explanation
vgg2.predict(imgs, True)
vgg2.classes[:20]
Explanation: Without finetuning, the model vgg2 outputs the probability of 1000 classes. As you can see below, they are sometimes totally irrelevant.
End of explanation
<END_TASK>