Question: import matplotlib.pyplot as plt import numpy as np import _pickle as pickle import platform def load_pickle(f): version = platform.python_version_tuple() if version[0] == '2': return pickle.load(f)
import matplotlib.pyplot as plt import numpy as np import _pickle as pickle import platform
def load_pickle(f): version = platform.python_version_tuple() if version[0] == '2': return pickle.load(f) elif version[0] == '3': return pickle.load(f, encoding='latin1') raise ValueError("invalid python version: {}".format(version))
def load_batch(filename): """ load single batch of cifar """ with open(filename, 'rb') as f: datadict = load_pickle(f) X = datadict['data'] Y = datadict['labels'] X = X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float") Y = np.array(Y) return X
# get a batch of images, and a batch of test images. dataimages = load_batch('data_batch_1') testimages = load_batch('test_batch')
# In this exercise, you'll be implementing a basic image classification algorithm # you have to simply subtract the test image form all the data images # and declare the image classification with the one, which returns you least difference # It's summarized in following steps. You have to implement each step using numpy powerful # vectorized operations. You can't use any loop in this task. # Since this is a very basic classifier, you won't see any good results # However, you can see some of the testcases are good fit and approximate the classification
# Step 1: Extract one image randomly from test images, call it testimage
# Step 2: Subtract the testimage from all the dataimages
# step 3: Find the absolute value of the result at step 2, and sum up every image result
# Step 4: Now find the index of minimum value of the result at step 3
# Step 5: Display the the image at the index found in step 4 from dataimages
# Step 6: Display the image the you chose randomly at Step 1.
# Sample code to plot images as subplot. plt.subplot(2, 1, 1) plt.imshow(dataimages[10].astype('uint8'))
plt.subplot(2, 1, 2) plt.imshow(dataimages[20].astype('uint8'))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
