深度学习课程:问题2作业1(notMNIST)

2024-06-28 19:05:23 发布

您现在位置:Python中文网/ 问答频道 /正文

在阅读了dataset-into-a-3d-array-image-index-x-y-of-floating-point-values-normalized-to-have-approximately-zero-mean-and-standard-deviation-0-5-to-make-training-easier-down-the-road/45902" rel="noreferrer">this并参加了课程之后,我正在努力解决作业1(notMnist)中的第二个问题:

Let's verify that the data still looks good. Displaying a sample of the labels and images from the ndarray. Hint: you can use matplotlib.pyplot.

以下是我尝试的:

import random
rand_smpl = [ train_datasets[i] for i in sorted(random.sample(xrange(len(train_datasets)), 1)) ]
print(rand_smpl)
filename = rand_smpl[0]
import pickle
loaded_pickle = pickle.load( open( filename, "r" ) )
image_size = 28  # Pixel width and height.
import numpy as np
dataset = np.ndarray(shape=(len(loaded_pickle), image_size, image_size),
                         dtype=np.float32)
import matplotlib.pyplot as plt

plt.plot(dataset[2])
plt.ylabel('some numbers')
plt.show()

但我得到的是:

enter image description here

这没什么意义。老实说,我的代码也可能是这样,因为我不太确定如何解决这个问题!在


泡菜是这样制作的:

^{pr2}$

函数的调用方式如下:

  dataset = load_letter(folder, min_num_images_per_class)
  try:
    with open(set_filename, 'wb') as f:
      pickle.dump(dataset, f, pickle.HIGHEST_PROTOCOL)

这里的想法是:

Now let's load the data in a more manageable format. Since, depending on your computer setup you might not be able to fit it all in memory, we'll load each class into a separate dataset, store them on disk and curate them independently. Later we'll merge them into a single dataset of manageable size.

We'll convert the entire dataset into a 3D array (image index, x, y) of floating point values, normalized to have approximately zero mean and standard deviation ~0.5 to make training easier down the road.


Tags: andofthetoinimageimportsize
3条回答

按如下方式进行:

#define a function to conver label to letter
def letter(i):
    return 'abcdefghij'[i]


# you need a matplotlib inline to be able to show images in python notebook
%matplotlib inline
#some random number in range 0 - length of dataset
sample_idx = np.random.randint(0, len(train_dataset))
#now we show it
plt.imshow(train_dataset[sample_idx])
plt.title("Char " + letter(train_labels[sample_idx]))

你的代码改变了数据集的类型实际上,它不是大小的标准(220000,28,28)

一般来说,pickle是一个包含一些对象的文件,而不是数组本身。您应该直接使用pickle中的对象来获取火车数据集(使用代码片段中的符号):

^{pr2}$

更新时间:

根据@gsarmas的请求,指向我的整个Assignment1解决方案的链接位于here。在

代码是注释性的,大部分都是自解释的,但是如果有任何问题,可以通过您喜欢的任何方式在github上联系

使用此代码:

#random select a letter
i = np.random.randint( len(train_datasets) )
plt.title( "abcdefghij"[i] )

#read the file of selected letter
f = open( train_datasets[i], "rb" )
f = pickle.load(f)

#random select an image in the file
j = np.random.randint( len(f) )

#show image
plt.axis('off')
img = plt.imshow( f[ j, :, : ] )

enter image description here

请核对一下这个代码

pickle_file = train_datasets[0]
with open(pickle_file, 'rb') as f:

# unpickle
letter_set = pickle.load(f)  

# pick a random image index
sample_idx = np.random.randint(len(letter_set))

# extract a 2D slice
sample_image = letter_set[sample_idx, :, :]  
plt.figure()

# display it
plt.imshow(sample_image) 

Output

相关问题 更多 >