Tensorflow专题7:图片&数据集预处理

图片处理

1 更改图片尺寸

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os
import os.path
from PIL import Image
'''
filein: 输入图片
fileout: 输出图片
width: 输出图片宽度
height:输出图片高度
type:输出图片类型(png, gif, jpeg...)
'''
def ResizeImage(filein, fileout, width, height, type):
img = Image.open(filein)
out = img.resize((width, height),Image.ANTIALIAS) #resize image with high-quality
out.save(fileout, type)
if __name__ == "__main__":
filein = r'image\test.png'
fileout = r'image\testout.png'
width = 60
height = 85
type = 'png'
ResizeImage(filein, fileout, width, height, type)

2 把图片转为3维数组(两种方法)

用np + Image

1
2
3
4
5
6
7
8
9
10
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

image = Image.open(r'C:\Users\Administrator\Desktop\data\train\forest_001.jpg') #读取图片文件
plt.imshow(image)
plt.show() #将图片输出到屏幕

image_arr = np.array(image) #将图片以数组的形式读入变量
print (image_arr)

用Tensorflow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

image_contents = tf.read_file('D:/Tensorflow/slim/test images/000.jpg') #读取文件

image = tf.image.decode_jpeg(image_contents, channels=3) #解码jpeg

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

img=sess.run((image)) #img为三维数组
print (img.shape) #输出数组形状
print (img) #打印数组

plt.imshow(img) #显示数组
plt.show()

3 整合1,2实现把图片转为指定大小的三维数组★

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

def ImageToArray(imagePath, width, height):
image = Image.open(imagePath, "r") #读取图片文件
plt.imshow(image)
plt.show() #将图片输出到屏幕
out = image.resize((width, height),Image.ANTIALIAS)
return np.array(out) #将图片以数组的形式读入变量

image_arr = ImageToArray('D:/Tensorflow/slim/test images/000.jpg', 299, 299)
print (image_arr.shape)

数据处理

1 cifar-10 数据读取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def load_CIFAR_batch(filename):
""" load single batch of cifar """
with open(filename, 'rb') as f:
datadict = pickle.load(f,encoding='latin1')
X = datadict['data']
Y = datadict['labels']
X = X.reshape(10000, 3, 32,32).transpose(0,2,3,1).astype("float") # convert to channel_last
Y = np.array(Y)
return X, Y

#入口函数
def load_CIFAR10(ROOT):
""" load all of cifar """
xs = []
ys = []
for b in range(1,6):
f = os.path.join(ROOT, 'data_batch_%d' % (b, ))
X, Y = load_CIFAR_batch(f)
xs.append(X)
ys.append(Y)
X_train = np.concatenate(xs)#使变成行向量
Y_train = np.concatenate(ys)
del X, Y
X_test, Y_test = load_CIFAR_batch(os.path.join(ROOT, 'test_batch'))
f.close()
return X_train[:5000], Y_train[:5000], X_test[:1000], Y_test[:1000]

2 标签数据 one-hot 编码

1、python方法

1
2
3
4
5
def GetOneHotLabel(label, numClass):
m = np.zeros([len(label), numClass])
for i in range(len(label)):
m[i][label[i]] = 1
return np.float64(m)

2、Tensorflow方法

1
2
#例子
one_hot_labels = tf.one_hot(indices=tf.cast(y, tf.int32), depth=_num_classes)

3 归一化

把图片像素压缩到[-1,1]之间

1
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5

4 标准化

1
2
3
4
5
6
def per_image_standardization(image):
image = image.astype('float32')
image[:, :, 0] = (image[:, :, 0] - np.mean(image[:, :, 0])) / np.std(image[:, :, 0])
image[:, :, 1] = (image[:, :, 1] - np.mean(image[:, :, 1])) / np.std(image[:, :, 1])
image[:, :, 2] = (image[:, :, 2] - np.mean(image[:, :, 2])) / np.std(image[:, :, 2])
return image
꧁༺The༒End༻꧂