<jupyter_start><jupyter_text>Thinking in tensors in PyTorchHands-on training  by [<NAME>](https://p.migdal.pl) (2019). Version 0.4 for Uniwersytet Śląski. Extra: Transfer learning[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/stared/thinking-in-tensors-writing-in-pytorch/blob/master/extra/Transfer%20learning.ipynb)As of now (PyTorch 1.1.0) [TORCHVISION.MODELS](https://pytorch.org/docs/stable/torchvision/models.html) offers: AlexNet, VGG, ResNet, SqueezeNet, DenseNet, Inception v3, GoogLeNet, ShuffleNet v2, MobileNet v2, ResNeXt.Link to: [ImageNet Hierarchy](https://observablehq.com/@mbostock/imagenet-hierarchy) by <NAME> At some point I created [a bit racy version of that](https://observablehq.com/@stared/tree-of-reddit-sex-life).<jupyter_code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

import torch
from torchvision import models, transforms
# we need to transform data for these models
transform = transforms.Compose([
    transforms.Resize((224,224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225])
])
model = models.squeezenet1_1(pretrained=True).eval()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
device

model = model.to(device)
img_paths = ["../imgs/dog.jpeg"]
imgs = [Image.open(img_path) for img_path in img_paths]
img_tensors = torch.stack([transform(img) for img in imgs]).to(device)
pred_logits_tensor = model(img_tensors)
pred_probs = pred_logits_tensor.softmax(dim=1).cpu().data.numpy()
pred_probs<jupyter_output><empty_output><jupyter_text>See https://github.com/stared/frankenstein-networks/blob/master/sampling_imagenet/download_cifar_sample.ipynb(**TODO**: transform it to a nicer format)<jupyter_code>import json
import pandas as pd
imagenet_classes = json.load(open("../data/imagenet_class_index.json"))
pd.DataFrame([imagenet_classes)

with open("../data/imagenet_class_index.json") as f:
    categories_raw = json.load(f)
    
categories = [(int(k), v) for k, v in categories_raw.items()]
categories = [tuple([k] + v) for k, v in sorted(categories)]
categories[:5]
df = pd.DataFrame(categories)
df.columns = ["id", "imagenet_code", "name"]
df = df.set_index("id")
df.head()
df["prediction"] = pred_probs[0]
df.sort_values(by="prediction", ascending=False).head(10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14715447154471545, 'fraction_numerical': 0.02113821138211382, 'mean_word_length': 5.724043715846994, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '342842', 'license': 'MIT', 'n_tokens_mistral': 923, 'n_tokens_neox': 882, 'n_words': 173, 'path': 'extra/Using an ImageNet-pretrained model.ipynb', 'repo_name': 'werkaaa/deep-learning-intro-pytorch', 'size': '2472'}