<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Pretrained CNN
Step1:  Imagenet pretrained models
Documentation from
Step2:  Imagenet 1000 classes
Step3:  Using pretrained CNN as feature extractors
<ASSISTANT_TASK:>
Python Code:
import dogs_vs_cats as dvc
all_files = dvc.image_files()
Explanation: Pretrained CNN: transfer learning
Nature article: Dermatologist-level classification of skin cancer with deep neural networks
End of explanation
from keras.applications.nasnet import NASNetMobile
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np
# https://keras.io/applications/#vgg16
model = NASNetMobile(weights='imagenet')
input_image_shape = (224,224,3)
img_path = all_files[10]
img = image.load_img(img_path, target_size=input_image_shape[1:])
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])
# decode the results into a list of tuples (class, description, probability)
model.summary()
Explanation: Imagenet pretrained models
Documentation from: https://keras.io/applications/. In keras.applications namespace we have the latest top accuracy solutions of imagenet 2012 classification contest.
End of explanation
# (one such list for each sample in the batch)
print('Predicted:', decode_predictions(preds, top=5))
from IPython.display import Image
Image(img_path)
print(preds.shape)
from keras.applications.imagenet_utils import CLASS_INDEX
# Imagenet 1000 classes
CLASS_INDEX
# predict a set of images
n_images = 10
x = np.ndarray((n_images,3,224,224))
for i,img_path in enumerate(all_files[0:n_images]):
    img = image.load_img(img_path, target_size=(224, 224))
    x[i] = image.img_to_array(img)
# preprocess and predict all together
x_preprocessed = preprocess_input(x)
preds = model.predict(x_preprocessed,verbose=1)
print("")
print(preds.shape)
dec_preds = decode_predictions(preds,top=5)
dec_preds
from IPython.display import Image,display
for img_path,dec_pred in zip(all_files[0:n_images],dec_preds):
    display(Image(img_path,width="120px",height="120px"))
    print(" ".join(["%s (prob: %.3f)"%(elm[1],elm[2]) for elm in dec_pred]))
Explanation: Imagenet 1000 classes:
* http://image-net.org/explore
End of explanation
# load model without top layer
n_images_train=500
n_images_test=500
input_image_shape = (3,224,224)
train_features, train_labels,train_files, \
test_features, test_labels, test_files = dvc.training_test_datasets(all_files,
                                                                    n_images_train,n_images_test,
                                                                    input_image_shape)
# load_img from keras.preprocessing loads the images in [0,255] scale
train_features = preprocess_input(train_features)
test_features = preprocess_input(test_features)
from keras.models import Model
base_model = VGG16(weights='imagenet')
model = Model(input=base_model.input, output=base_model.get_layer('fc2').output)
print("Predicting train images")
train_features_cnn = model.predict(train_features,verbose=1)
print("Predicting test images")
test_features_cnn = model.predict(test_features,verbose=1)
train_features_cnn.shape
from sklearn import svm
from sklearn.model_selection import GridSearchCV
tuned_parameters = {'kernel': ['linear'],
                     'C': [1, 10, 100, 1000]}
clf = GridSearchCV(svm.SVC(C=1), tuned_parameters, cv=5,n_jobs=7)
clf.fit(train_features_cnn, train_labels)
clf.best_estimator_
print("Train score: {}".format(clf.score(train_features_cnn,train_labels)))
print("Test score: {}".format(clf.score(test_features_cnn,test_labels)))
Explanation: Using pretrained CNN as feature extractors
End of explanation
<END_TASK>