tensorflow进行SVHN数据实验

   SVHN is a real-world image dataset for developing machine learning and object recognition algorithms with minimal requirement on data preprocessing and formatting.数据库下载地址

   我所下载的数据集是32×32的形式的。然后用scipy将数据导入到代码中,先进行数据的分析,mat后缀的格式是matplotlib的数据形式,scipy能够很好的读取,但是数据形式何不相同。其中代码如下:

# coding: UTF-8
from scipy.io import loadmat as load

traindata = load('data/train_32x32.mat')
testdata = load('data/test_32x32.mat')

print ("Train Data Shape:", traindata['X'].shape)
print ("Test Data Shape:", traindata['y'].shape)

实验的数据显示数据的形式是matlib的,和普通的形式很不相同。然后进行数据的预处理和观察数据:

# coding: UTF-8
from scipy.io import loadmat as load
import matplotlib.pyplot as plt
import numpy as np


def reformat(samples, labels):
    # 改变原始数据的形状
    # (图片高,图片宽,通道数,图片数)->(图片数,图片高,图片宽,通道数)
    # labels 变成one-hot encoding
    samples = np.transpose(samples, (3, 0, 1, 2))
    labels = np.array([x[0] for x in labels])
    one_hot_labels = []
    for num in labels:
        one_hot = [0.0] * 10
        if num == 10:
            one_hot[0] = 1.0
        else:
            one_hot[num] = 1.0
        one_hot_labels.append(one_hot)
    labels = np.array(one_hot_labels).astype(np.float32)
    return samples, labels


def normalize(samples):
    # 将图片从0~255 线性映射到 -1.0~+1.0
    # 并且灰度化
    pass

def distribution(labels, name):
    # 查看一下每个label的分布。就是比例
    pass

def inspect(dataset, labels, i):
    # 显示图片看看
    print(labels[i])
    plt.imshow(dataset[i])
    plt.show()

train = load('data/train_32x32.mat')
test = load('data/test_32x32.mat')

print ("Train Data Shape:", train['X'].shape)
print ("Train Label Shape:", train['y'].shape)

train_samples = train['X']
train_labels = train['y']
test_samples = test['X']
test_labels = test['y']

_train_sample, _train_labels = reformat(train_samples, train_labels)
_test_sample, _test_labels = reformat(test_samples, test_labels)

num_labels = 10
image_size = 32


if __name__ == '__main__':
    # 探索数据
    inspect(_train_sample, _train_labels, 12322)

其中代码中reformat时进行数据的预处理,将改变原始数据的格式,并将labels变成one-hot形式。然后就是inspect函数,进行数据的显示,能够将图像进行显示。其结果如下所示。














  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值