<jupyter_start><jupyter_text>Using pre-trained modelToday we're going to build and fine-tune CNN based on weights pre-trained on ImageNet: the largest image classification dataset as of now.More about imagenet: http://image-net.org/Setup: classify from a set of 1000 classes.<jupyter_code>import requests

# class labels
LABELS_URL = 'https://s3.amazonaws.com/outcome-blog/imagenet/labels.json'
labels = {int(key):value for (key, value) in requests.get(LABELS_URL).json().items()}
print(list(labels.items())[:5])<jupyter_output>[(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')]<jupyter_text>TorchVisionPyTorch has several companion libraries, one of them being [torchvision](https://github.com/pytorch/vision/tree/master/) - it contains a number of popular vision datasets, preprocessing tools and most importantly, [pre-trained models](https://github.com/pytorch/vision/tree/master/torchvision/models).For now, we're going to use torch Inception-v3 module. We're gonna use the inception-v3 network:![img](https://hackathonprojects.files.wordpress.com/2016/09/googlenet_diagram.png?w=650&h=192)Let's first look at the code here: [url](https://github.com/pytorch/vision/blob/master/torchvision/models/inception.py)<jupyter_code>from torchvision.models.inception import inception_v3

model = inception_v3(pretrained=True,      # load existing weights
                     transform_input=True, # preprocess input image the same way as in training
                    )

model.aux_logits = False # don't predict intermediate logits (yellow layers at the bottom)
model.train(False)
import torch
from torch.autograd import Variable

dummy_x = Variable(torch.randn(5, 3, 299, 299))
model(dummy_x)<jupyter_output><empty_output><jupyter_text>Predict class probabilities<jupyter_code>import matplotlib.pyplot as plt
from scipy.misc import imresize
%matplotlib inline

img = imresize(plt.imread('sample_images/albatross.jpg'), (299,299)) / 255.
plt.imshow(img)
plt.show()

img = Variable(torch.FloatTensor(img.reshape([1, 299, 299, 3]).transpose([0,3,1,2])))

probs = torch.nn.functional.softmax(model(img), dim=-1)

probs = probs.data.numpy()

top_ix = probs.ravel().argsort()[-1:-10:-1]
print ('top-10 classes are: \n [prob : class label]')
for l in top_ix:
    print ('%.4f :\t%s' % (probs.ravel()[l], labels[l].split(',')[0]))<jupyter_output>/home/jheuristic/anaconda3/lib/python3.6/site-packages/scipy/misc/pilutil.py:482: FutureWarning: Conversion of the second argument of issubdtype from `int` to `np.signedinteger` is deprecated. In future, it will be treated as `np.int64 == np.dtype(int).type`.
  if issubdtype(ts, int):
/home/jheuristic/anaconda3/lib/python3.6/site-packages/scipy/misc/pilutil.py:485: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  elif issubdtype(type(size), float):<jupyter_text>Having fun with pre-trained nets<jupyter_code>!wget http://cdn.com.do/wp-content/uploads/2017/02/Donal-Trum-Derogar.jpeg -O img.jpg
img = imresize(plt.imread('img.jpg'), (299,299)) / 255.
plt.imshow(img)
plt.show()

img = Variable(torch.FloatTensor(img.reshape([1, 299, 299, 3]).transpose([0,3,1,2])))

probs = torch.nn.functional.softmax(model(img), dim=-1)

probs = probs.data.numpy()

top_ix = probs.ravel().argsort()[-1:-10:-1]
print ('top-10 classes are: \n [prob : class label]')
for l in top_ix:
    print ('%.4f :\t%s' % (probs.ravel()[l], labels[l].split(',')[0]))<jupyter_output>/home/jheuristic/anaconda3/lib/python3.6/site-packages/scipy/misc/pilutil.py:482: FutureWarning: Conversion of the second argument of issubdtype from `int` to `np.signedinteger` is deprecated. In future, it will be treated as `np.int64 == np.dtype(int).type`.
  if issubdtype(ts, int):
/home/jheuristic/anaconda3/lib/python3.6/site-packages/scipy/misc/pilutil.py:485: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  elif issubdtype(type(size), float):<jupyter_text>Grand-quest: Dogs Vs Cats* original competition* https://www.kaggle.com/c/dogs-vs-cats* 25k JPEG images of various size, 2 classes (guess what) Your main objective* In this seminar your goal is to fine-tune a pre-trained model to distinguish between the two rivaling animals* The first step is to just reuse some network layer as features<jupyter_code>!wget https://www.dropbox.com/s/d61lupw909hc785/dogs_vs_cats.train.zip?dl=1 -O data.zip
!unzip data.zip<jupyter_output><empty_output><jupyter_text>for starters* Train sklearn model, evaluate validation accuracy (should be >80%<jupyter_code>#extract features from images
from tqdm import tqdm
from scipy.misc import imresize
import os
X = []
Y = []

#this may be a tedious process. If so, store the results in some pickle and re-use them.
for fname in tqdm(os.listdir('train/')):
    y = fname.startswith("cat")
    img = imread("train/"+fname)
    img = imresize(img,(IMAGE_W,IMAGE_W))
    img = zoo.inception_v3.preprocess_input(img[None].astype('float32'))
    
    features = <use network to process the image into features>
    # Hint: use model.features(x)
    
    Y.append(y)
    X.append(features)

X = np.concatenate(X) #stack all [1xfeatures] matrices into one. 
assert X.ndim==2
#WARNING! the concatenate works for [1xN] matrices. If you have other format, stack them yourself.

#crop if we ended prematurely
Y = Y[:len(X)]
<split data here or use cross-validation><jupyter_output><empty_output><jupyter_text>__load our dakka__![img](https://s-media-cache-ak0.pinimg.com/564x/80/a1/81/80a1817a928744a934a7d32e7c03b242.jpg)<jupyter_code>from sklearn.ensemble import RandomForestClassifier,ExtraTreesClassifier,GradientBoostingClassifier,AdaBoostClassifier
from sklearn.linear_model import LogisticRegression, RidgeClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier<jupyter_output><empty_output><jupyter_text>Main quest* Get the score improved!* You have to reach __at least 95%__ on the test set. More = better.No methods are illegal: ensembling, data augmentation, NN hacks. Just don't let test data slip into training. Split the raw image data  * please do train/validation/test instead of just train/test  * reasonable but not optimal split is 20k/2.5k/2.5k or 15k/5k/5k Choose which vgg layers are you going to use  * Anything but for prob is okay  * Do not forget that vgg16 uses dropout Build a few layers on top of chosen "neck" layers.  * a good idea is to just stack more layers inside the same network  * alternative: stack on top of get_output Train the newly added layers for some iterations  * you can selectively train some weights by sending the correct parameters in the optimizer      * `opt = torch.optim.Adam([head_only.parameters()])``  * it's cruicial to monitor the network performance at this and following steps Fine-tune the network body  * probably a good idea to SAVE your new network weights now 'cuz it's easy to mess things up.  * Moreover, saving weights periodically is a no-nonsense idea  * even more cruicial to monitor validation performance  * main network body may need a separate, much lower learning rate  Grading* 95% accuracy on test yields 10 points* -1 point per 5% less accuracy Some ways to get bonus points* explore other networks from the model zoo* play with architecture* 96%/97%/98%/99%/99.5% test score (screen pls).* data augmentation, prediction-time data augmentation* use any more advanced fine-tuning technique you know/read anywhere* ml hacks that benefit the final score<jupyter_code>#<A whole lot of your code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11618782432603468, 'fraction_numerical': 0.027464877863561575, 'mean_word_length': 5.135093167701863, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '141925', 'license': 'MIT', 'n_tokens_mistral': 2763, 'n_tokens_neox': 2586, 'n_words': 785, 'path': '_under_construction/week3/seminar_pytorch.ipynb', 'repo_name': 'RalucaFe/Practical_DL', 'size': '7901'}