Question: Python 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':
Python 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)
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']
# print(X.shape)
Y = datadict['labels']
X = X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float")
Y = np.array(Y)
return X, Y
# get a batch of images, and a batch of test images.
dataimages, datalables = load_batch('data_batch_1')
testimages, testlables = load_batch('test_batch')
print(dataimages.shape)
print(testimages.shape)
def classify(dataimages, datalables, testimages, testlables):
# Step 1: Extract one image randomly from test images, call it testimage
rn = np.random.randint(10000)
testimage = testimages[rn]
testlable = testlables[rn]
plt.imshow(testimage)
result = dataimages - testimage
result = np.abs(result)
res_sum = np.sum(result, axis = (1, 2, 3))
minindex = np.argmin(res_sum)
res_image = dataimages[minindex]
res_lable = datalables[minindex]
return res_lable == testlable
res = classify(dataimages, datalables, testimages, testlables)
print(res)
eff=0
for i in range(100):
res=classify(dataimages,datlabes, tetsimages,testlables)
if res:
eff+=1
print(eff)
# Question # Normalize the images by subtracting a mean value of each image from its pixels
# Calculate the efficiency again with this new method
# Print the resultant efficiency values of both the ways.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
