forked from posjoker/CNN-based-Indoor-Localization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_data.py
61 lines (52 loc) · 2.24 KB
/
input_data.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#coding=utf-8
import tensorflow as tf
import numpy as np
import os
# file_dir = '/home/hjxu/PycharmProjects/tf_examples/dog_cat/data/train/'
# 获取文件路径和标签
def get_files(filename):
class_train = []
label_train = []
for train_class in os.listdir(filename):
for pic in os.listdir(filename+train_class):
class_train.append(filename+train_class+'/'+pic)
label_train.append(train_class)
temp = np.array([class_train,label_train])
temp = temp.transpose()
#shuffle the samples
np.random.shuffle(temp)
#after transpose, images is in dimension 0 and label in dimension 1
image_list = list(temp[:,0])
label_list = list(temp[:,1])
label_list = [int(i) for i in label_list]
#print(label_list)
return image_list,label_list
# 生成相同大小的批次
def get_batch(image, label, image_W, image_H, batch_size, capacity):
# image, label: 要生成batch的图像和标签list
# image_W, image_H: 图片的宽高
# batch_size: 每个batch有多少张图片
# capacity: 队列容量
# return: 图像和标签的batch
# 将python.list类型转换成tf能够识别的格式
image = tf.cast(image, tf.string)
label = tf.cast(label, tf.int32)
# 生成队列
input_queue = tf.train.slice_input_producer([image, label])
image_contents = tf.read_file(input_queue[0])
label = input_queue[1]
image = tf.image.decode_jpeg(image_contents, channels=3)
# 统一图片大小
# 视频方法
# image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H)
# 我的方法
image = tf.image.resize_images(image, [image_H, image_W], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
image = tf.cast(image, tf.float32)
# image = tf.image.per_image_standardization(image) # 标准化数据
image_batch, label_batch = tf.train.batch([image, label],
batch_size=batch_size,
num_threads=64, # 线程
capacity=capacity)
# 这行多余?
# label_batch = tf.reshape(label_batch, [batch_size])
return image_batch, label_batch