在之前我们接触了MNIST的相关知识,并且建立了简单的线性逻辑回归模型,接下来,学习了udacity上的tensorflow教程,并完成了任务一
notMNIST与MNIST相似,图片大小为28x28,灰度,与MNIST类似。但是较比MNIST,notMNIST含有 A-J 10个类别的艺术印刷体字符,字符的形状各异,噪声更多,更加难以处理
数据下载
首先将测试集和训练集下载到本机,
每个每个数据集下有A-J的文件夹,这里下载的是.tar.gz格式的,下载后,右键解压,生成一个.tar文件,然后继续解压,才会解压完成。
打开Jupyter Notebook
由于这里我是采用的anaconda安装的tensorflow,所以在开始菜单,所有程序找到anaconda3,然后找到jupyter notebook打开即可;
如果不是anaconda安装的,可参考之前的教程:点击查看教程
deep learning
这里,我们参考tensorflow下udacity的教程步骤进行的,
引包检查
首先检查,接下来要使用的各种包是否都已经安好,代码如下:
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
import tarfile
from IPython.display import display, Image
from scipy import ndimage
from sklearn.linear_model import LogisticRegression
from six.moves.urllib.request import urlretrieve
from six.moves import cPickle as pickle
点击jupyter notebook的run cell,(就是一个向右的箭头),点击后不报错即说明各种包已经安好,如果有错,按照错误提示,安装缺失的包即可
problem1:生成pickle
我们没必要将所有数据载入内存,可以利用pickle分别存储每个类型的数据,然后再分别处理。 之后,汇总到一个可具操作性的数据集中。
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, :,