import numpy as np import skimage.io import matplotlib.pyplot as plt from cs231n.classifiers.pretrained_cnn import PretrainedCNN from cs231n.data_utils import load_tiny_imagenet from cs231n.image_utils import blur_image, deprocess_image plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots plt.rcParams['image.interpolation'] = 'nearest' plt.rcParams['image.cmap'] = 'gray' # for auto-reloading external modules # see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython data = load_tiny_imagenet('cs231n/datasets/tiny-imagenet-100-A', subtract_mean=True) for i, names in enumerate(data['class_names']): print i, ' '.join('"%s"' % name for name in names) # Visualize some examples of the training data classes_to_show = 7 examples_per_class = 5 class_idxs = np.random.choice(len(data['class_names']), size=classes_to_show, replace=False) for i, class_idx in enumerate(class_idxs): train_idxs, = np.nonzero(data['y_train'] == class_idx) train_idxs = np.random.choice(train_idxs, size=examples_per_class,
try: mode = sys.argv[2] if mode == 'normal' or mode == 'fast' or mode == 'normal_clip' or mode == 'fast_clip': print 'Mode:', mode else:assert False, "Need mode to be 'normal', 'fast', 'normal_clip', or 'fast_clip'" except: mode = 'normal' # Load the TinyImageNet-100-A dataset and a pretrained model from cs231n.data_utils import load_tiny_imagenet, load_models tiny_imagenet_a = 'cs231n/datasets/tiny-imagenet-100-A' class_names, X_train, y_train, X_val, y_val, X_test, y_test = load_tiny_imagenet(tiny_imagenet_a) # Zero-mean the data mean_img = np.mean(X_train, axis=0) X_train -= mean_img X_val -= mean_img X_test -= mean_img # Load a pretrained model; it is a five layer convnet. models_dir = 'cs231n/datasets/tiny-100-A-pretrained' model = load_models(models_dir)['model1'] from cs231n.classifiers.convnet import five_layer_convnet # Array of shape (X_val.shape[0],) storing predictions on the validation set. # y_val_pred[i] = c indicates that the model predicts that X_val[i] has label c.
get_ipython().magic(u'autoreload 2') # # Introducing TinyImageNet # # The TinyImageNet dataset is a subset of the ILSVRC-2012 classification dataset. It consists of 200 object classes, and for each object class it provides 500 training images, 50 validation images, and 50 test images. All images have been downsampled to 64x64 pixels. We have provided the labels for all training and validation images, but have withheld the labels for the test images. # # We have further split the full TinyImageNet dataset into two equal pieces, each with 100 object classes. We refer to these datasets as TinyImageNet-100-A and TinyImageNet-100-B; for this exercise you will work with TinyImageNet-100-A. # # To download the data, go into the `cs231n/datasets` directory and run the script `get_tiny_imagenet_a.sh`. Then run the following code to load the TinyImageNet-100-A dataset into memory. # # NOTE: The full TinyImageNet-100-A dataset will take up about 250MB of disk space, and loading the full TinyImageNet-100-A dataset into memory will use about 2.8GB of memory. # In[ ]: data = load_tiny_imagenet('cs231n/datasets/tiny-imagenet-100-A', subtract_mean=True) # # TinyImageNet-100-A classes # Since ImageNet is based on the WordNet ontology, each class in ImageNet (and TinyImageNet) actually has several different names. For example "pop bottle" and "soda bottle" are both valid names for the same class. Run the following to see a list of all classes in TinyImageNet-100-A: # In[ ]: for i, names in enumerate(data['class_names']): print i, ' '.join('"%s"' % name for name in names) # # Visualize Examples # Run the following to visualize some example images from random classses in TinyImageNet-100-A. It selects classes and images randomly, so you can run it several times to see different images. # In[ ]:
# A bit of setup import numpy as np import matplotlib.pyplot as plt from time import time # %matplotlib inline plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots plt.rcParams['image.interpolation'] = 'nearest' plt.rcParams['image.cmap'] = 'gray' # NOTE: The full TinyImageNet dataset will take up about 490MB of disk space, # and loading the full TinyImageNet-100-A dataset into memory will use about 2.8GB of memory. from cs231n.data_utils import load_tiny_imagenet tiny_imagenet_a = 'cs231n/datasets/tiny-imagenet-100-A' class_names, X_train, y_train, X_val, y_val, X_test, y_test = load_tiny_imagenet(tiny_imagenet_a) # Zero-mean the data mean_img = np.mean(X_train, axis=0) X_train -= mean_img X_val -= mean_img X_test -= mean_img
import sys sys.path.append("E:\\PythonProject\\assignment3") import time, os, json import numpy as np from scipy.misc import imread, imresize import matplotlib.pyplot as plt from cs231n.classifiers.pretrained_cnn import PretrainedCNN from cs231n.data_utils import load_tiny_imagenet from cs231n.image_utils import blur_image, deprocess_image, preprocess_image data = load_tiny_imagenet( 'E:/PythonProject/assignment3/cs231n/datasets/tiny-imagenet-100-A', subtract_mean=True) model = PretrainedCNN( h5_file='E:/PythonProject/assignment3/cs231n/datasets/pretrained_model.h5') ''' def create_class_visualization(target_y, model, **kwargs): """ Perform optimization over the image to generate class visualizations. Inputs: - target_y: Integer in the range [0, 100) giving the target class - model: A PretrainedCNN that will be used for generation Keyword arguments: - learning_rate: Floating point number giving the learning rate - blur_every: An integer; how often to blur the image as a regularizer - l2_reg: Floating point number giving L2 regularization strength on the image; this is lambda in the equation above.