In [1]:
from IPython.display import Image, display

使用Inception模型来做Transfer Learning。输入图片,并存储模型处理之后的Transfer Value。Transfer Value是尚未用于分类的输出,里面保存了一张图片的必要信息。当所有图片都处理之后,并存储在磁盘上。接下来使用transfer-value作为其他神经网络的输入,将transfer-value作为新数据样本,并训练新的神经网络分类结果。


In [2]:
Image('data/08_transfer_learning_flowchart.png')


Out[2]:

In [3]:
%matplotlib inline

In [33]:
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import time
import os
import inception
import prettytensor as pt
import matplotlib.cm as cm

In [5]:
import cifar10

In [6]:
from cifar10 import num_classes

In [7]:
cifar10.maybe_download_and_extract()


数据已经存在!

In [8]:
class_names = cifar10.load_class_names()
class_names


Loading data: data/CIFAR-10/cifar-10-batches-py/batches.meta
Out[8]:
[u'airplane',
 u'automobile',
 u'bird',
 u'cat',
 u'deer',
 u'dog',
 u'frog',
 u'horse',
 u'ship',
 u'truck']

加载训练数据:训练图片、整型标签、one-hot编码过的标签


In [9]:
images_train, cls_train, labels_train = cifar10.load_training_data()


Loading data: data/CIFAR-10/cifar-10-batches-py/data_batch_1
Loading data: data/CIFAR-10/cifar-10-batches-py/data_batch_2
Loading data: data/CIFAR-10/cifar-10-batches-py/data_batch_3
Loading data: data/CIFAR-10/cifar-10-batches-py/data_batch_4
Loading data: data/CIFAR-10/cifar-10-batches-py/data_batch_5

In [10]:
images_test, cls_test, labels_test = cifar10.load_test_data()


Loading data: data/CIFAR-10/cifar-10-batches-py/test_batch

In [11]:
print("Size of:")
print("- Training-set:\t\t{}".format(len(images_train)))
print("- Test-set:\t\t{}".format(len(images_test)))


Size of:
- Training-set:		50000
- Test-set:		10000

In [12]:
def plot_images(images, cls_true, cls_pred=None, smooth=True):
    assert len(images) == len(cls_true)
    
    fig, axes = plt.subplots(3, 3)
    hspace = 0.3 if cls_pred is None else 0.6
    fig.subplots_adjust(hspace=hspace, wspace=0.3)
    if smooth:
        interpolation = 'spline16'
    else:
        interpolation = 'nearest'
        
    for i, ax in enumerate(axes.flat):
        if i < len(images):
            ax.imshow(images[i], interpolation=interpolation)
            cls_true_name = class_names[cls_true[i]]
            if cls_pred is None:
                xlabel = "True: {0}".format(cls_true_name)
            else:
                cls_pred_name = class_names[cls_pred[i]]
                xlabel = "True: {0}\nPred: {1}".format(cls_true_name, cls_pred_name)
            ax.set_xlabel(xlabel)
        ax.set_xticks([])
        ax.set_yticks([])
    plt.show()

In [13]:
# Get the first images from the test-set.
images = images_test[0:9]

# Get the true classes for those images.
cls_true = cls_test[0:9]

# Plot the images and labels using our helper-function above.
plot_images(images=images, cls_true=cls_true, smooth=False)



In [14]:
inception.maybe_download()


Downloading Inception v3 model and data ...
数据已经存在!

In [15]:
model = inception.Inception()

In [16]:
from inception import transfer_values_cache

In [17]:
file_path_cache_train = os.path.join(cifar10.data_path, 'inception_cifar10_train.pkl')
file_path_cache_test = os.path.join(cifar10.data_path, 'inception_cifar10_test.pkl')

In [18]:
print (u"使用Inception模型,开始处理输入的验证图片,输出transfer-value并存储")

images_scaled = images_train * 255.0

transfer_values_train = transfer_values_cache(cache_path=file_path_cache_train, 
                                              model=model,
                                              images=images_train)


使用Inception模型,开始处理输入的验证图片,输出transfer-value并存储
- Processing image:  50000 / 50000()
- Data saved to cache-file: data/CIFAR-10/inception_cifar10_train.pkl

In [20]:
print("使用Inception模型,开始处理输入的测试图片,输出transfer-value并存储 ...")

# Scale images because Inception needs pixels to be between 0 and 255,
# while the CIFAR-10 functions return pixels between 0.0 and 1.0
images_scaled = images_test * 255.0

# If transfer-values have already been calculated then reload them,
# otherwise calculate them and save them to a cache-file.
transfer_values_test = transfer_values_cache(cache_path=file_path_cache_test,
                                             images=images_scaled,
                                             model=model)


使用Inception模型,开始处理输入的测试图片,输出transfer-value并存储 ...
- Processing image:  10000 / 10000()
- Data saved to cache-file: data/CIFAR-10/inception_cifar10_test.pkl

In [19]:
transfer_values_train.shape


Out[19]:
(50000, 2048)

In [21]:
transfer_values_test.shape


Out[21]:
(10000, 2048)

In [22]:
transfer_values_test[0].shape


Out[22]:
(2048,)

In [23]:
def plot_transfer_values(i):
    print ("input image: ")
    plt.imshow(images_test[i], interpolation='nearest')
    plt.show()
    
    print("Transfer-values for the image using Inception model:")
    
    img = transfer_values_test[i]
    img = img.reshape((32, 64))
    
    plt.imshow(img, interpolation='nearest', cmap='Reds')
    plt.show()

In [24]:
plot_transfer_values(16)


input image: 
Transfer-values for the image using Inception model:

In [25]:
plot_transfer_values(17)


input image: 
Transfer-values for the image using Inception model:

In [26]:
from sklearn.decomposition import PCA

In [36]:
pca = PCA(n_components=2)

In [43]:
transfer_values = transfer_values_train[0:5000]

In [44]:
cls = cls_train[0:5000]

In [45]:
transfer_values_reduced = pca.fit_transform(transfer_values)

In [46]:
transfer_values_reduced.shape


Out[46]:
(5000, 2)

In [47]:
def plot_scatter(values, cls):
    cmap = cm.rainbow(np.linspace(0.0, 1.0, num_classes))
    
    colors = cmap[cls]
    
    x = values[:, 0]
    y = values[:, 1]
    
    plt.scatter(x, y, color=colors)
    plt.show()

In [48]:
plot_scatter(transfer_values_reduced, cls)



In [49]:
from sklearn.manifold import TSNE

In [50]:
pca = PCA(n_components=50)
transfer_values_50d = pca.fit_transform(transfer_values)

In [51]:
tsne = TSNE(n_components=2)

In [ ]:
transfer_values_reduced = tsne.fit_transform(transfer_values_50d)