【发布时间】:2023-03-27 02:15:02
【问题描述】:
读完this并参加课程后,我正在努力解决作业1中的第二个问题(notMnist):
让我们验证数据是否仍然看起来不错。显示来自 ndarray 的标签和图像的样本。提示:你可以使用 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()
但这就是我得到的:
这没有多大意义。老实说,我的代码也可能,因为我不确定如何解决这个问题!
泡菜是这样制作的:
image_size = 28 # Pixel width and height.
pixel_depth = 255.0 # Number of levels per pixel.
def load_letter(folder, min_num_images):
"""Load the data for a single letter label."""
image_files = os.listdir(folder)
dataset = np.ndarray(shape=(len(image_files), image_size, image_size),
dtype=np.float32)
print(folder)
num_images = 0
for image in image_files:
image_file = os.path.join(folder, image)
try:
image_data = (ndimage.imread(image_file).astype(float) -
pixel_depth / 2) / pixel_depth
if image_data.shape != (image_size, image_size):
raise Exception('Unexpected image shape: %s' % str(image_data.shape))
dataset[num_images, :, :] = image_data
num_images = num_images + 1
except IOError as e:
print('Could not read:', image_file, ':', e, '- it\'s ok, skipping.')
dataset = dataset[0:num_images, :, :]
if num_images < min_num_images:
raise Exception('Many fewer images than expected: %d < %d' %
(num_images, min_num_images))
print('Full dataset tensor:', dataset.shape)
print('Mean:', np.mean(dataset))
print('Standard deviation:', np.std(dataset))
return dataset
该函数的调用方式如下:
dataset = load_letter(folder, min_num_images_per_class)
try:
with open(set_filename, 'wb') as f:
pickle.dump(dataset, f, pickle.HIGHEST_PROTOCOL)
这里的想法是:
现在让我们以更易于管理的格式加载数据。因为,根据您的计算机设置,您可能无法将它们全部放入内存中,因此我们会将每个类加载到单独的数据集中,将它们存储在磁盘上并独立管理它们。稍后我们会将它们合并成一个大小可控的数据集。
我们会将整个数据集转换为浮点值的 3D 数组(图像索引,x,y),标准化为具有大约为零的均值和约 0.5 的标准差,以使训练更容易。
【问题讨论】:
-
除非我们已注册课程,否则我们无法查看您的链接。请在您的问题中粘贴相关讨论。
-
@erip 感谢您的评论。 link 可以访问吗?嗯,你是对的。
-
是的,评论的链接是可以访问的。
-
好的,@erip!我还将代码编辑为最少。额外的代码是我解决的第一个问题的剩余代码(如果有帮助,我可以发布)。
-
我认为你的大问题是你所做的一切都被声明为
dataset(而不是初始化它)。它加载了垃圾值(在本例中为 0)。你没有在策划任何事情。如果没有更多数据或上下文,我不确定我们能做些什么。
标签: python matplotlib machine-learning computer-vision deep-learning