2017-07-23 48 views
1

我如何矢量化,重塑和規範化我的圖像像mnist.train.images中的一個圖像的大小矢量相同?我已經試過以下至今沒有成功:矢量化,重塑和規範我的圖像像mnist.train.images

import os 
import re 
import numpy as np 
import tensorflow as tf 
from tensorflow.python.framework import ops 
import os,sys 
#import Image 
from PIL import Image 
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets 

# Load data and check the shape of the first mnist.train.images image 
data_dir = 'temp' 
mnist = read_data_sets(data_dir) 
print("tmnist.train.images[0].shape is") 
print(mnist.train.images[0].shape) # the result is (784,) 

def resize_image(image): 
    img = Image.open(image) 
    arr = np.array(img) 
    #my mind is drawing a blank... additional code to make this work... 
    return arr 


resize_image("test.png") 

回答

1

下面應該工作:

def resize_image(image): 
    img = Image.open(image) 
    img = img.resize((28, 28)) 
    arr = np.array(img) 

    #convert to gray scale 
    if len(arr.shape) > 2: 
    arr = np.mean(arr, 2) 

    #flatten 
    arr = arr.flatten() 
    return arr 
+1

感謝您的及時答覆。對於正常化,我補充說:從sklearn進口預處理; min_max_scaler = preprocessing.MinMaxScaler(); arr_train_minmax = min_max_scaler.fit_transform(arr);似乎工作。希望它符合mnist.train.images的標準化 – caramelslice