Tensorflow实现简单图像探索

数据采用斯坦福SVHN数据集(The Street View HouseNumbers Dataset)。

下载地址:http://ufldl.stanford.edu/housenumbers/

数据集名 train_32x32.mattest_32x32.mat 

格式为MATLAB文件,可从scipy.io中import loadmat读取,这里Python2.6版本。下面为具体代码


#! usr/bin/python

#coding=utf-8

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

from scipy.io import loadmat as load

train= load('/Users/zhiren1111/Desktop/train_32x32.mat')
test= load('/Users/zhiren1111/Desktop/test_32x32.mat')


def reformat(samples,labels):
    new=np.transpose(samples,(3,0,1,2)).astype(np.float64)
    labels=np.array(list(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.float64)
        return new ,labels
def renormarize(samples):
    a=np.add.reduce(samples,keepdims=True,axis=3)
    a=a/3.0
    return a/128.0-1.0



def inspect(dataset,labels,i):
    if dataset.shape[3]==1:
        shape=dataset.shape
        dataset=dataset.reshape(shape[0],shape[1],shape[2])
    plt.imshow(dataset[i])
    plt.show()



train_samples=train["X"]

train_labels=train["y"]


_train_samples,_train_labels=reformat(train_samples,train_labels)




_train_samples=renormarize(_train_samples)

inspect(_train_samples,_train_labels,1)
 
  
 
  
 
  
 
  
本篇文章完全参考t天空下的斌的文章,只是把测试环境从python3.x换成python2.x
详细链接如下
 http://blog.csdn.net/a595130080/article/details/64440464 
  
 
  
 
  
 
  
 
  
 
  
 
  

你可能感兴趣的:(Tensor,flow,深度学习,python)