import os
import glob
import os.path
import numpy as np
import tensorflow as tf
from tensorflow.python.platform import gfile # 原始输入数据的目录,这个目录下有5个子目录,每个子目录底下保存这属于该
# 类别的所有图片。
INPUT_DATA = 'F:\\TensorFlowGoogle\\201806-github\\datasets\\flower_photos'
# 输出文件地址。我们将整理后的图片数据通过numpy的格式保存。
OUTPUT_FILE = 'F:\\flower_processed_data.npy' # 测试数据和验证数据比例。
VALIDATION_PERCENTAGE = 10
TEST_PERCENTAGE = 10 # 读取数据并将数据分割成训练数据、验证数据和测试数据。
def create_image_lists(sess, testing_percentage, validation_percentage):
sub_dirs = [x[0] for x in os.walk(INPUT_DATA)]
is_root_dir = True # 初始化各个数据集。
training_images = []
training_labels = []
testing_images = []
testing_labels = []
validation_images = []
validation_labels = []
current_label = 0 # 读取所有的子目录。
for sub_dir in sub_dirs:
if is_root_dir:
is_root_dir = False
continue
# 获取一个子目录中所有的图片文件。
extensions = ['jpg', 'jpeg', 'JPG', 'JPEG']
file_list = []
dir_name = os.path.basename(sub_dir)
for extension in extensions:
file_glob = os.path.join(INPUT_DATA, dir_name, '*.' + extension)
file_list.extend(glob.glob(file_glob))
if not file_list: continue
print("processing:", dir_name) i = 0
# 处理图片数据。
for file_name in file_list:
i += 1
# 读取并解析图片,将图片转化为299*299以方便inception-v3模型来处理。
image_raw_data = gfile.FastGFile(file_name, 'rb').read()
image = tf.image.decode_jpeg(image_raw_data)
if image.dtype != tf.float32:
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
image = tf.image.resize_images(image, [299, 299])
image_value = sess.run(image) # 随机划分数据聚。
chance = np.random.randint(100)
if chance < validation_percentage:
validation_images.append(image_value)
validation_labels.append(current_label)
elif chance < (testing_percentage + validation_percentage):
testing_images.append(image_value)
testing_labels.append(current_label)
else:
training_images.append(image_value)
training_labels.append(current_label)
if i % 200 == 0:
print(i, "images processed.")
current_label += 1 # 将训练数据随机打乱以获得更好的训练效果。
state = np.random.get_state()
np.random.shuffle(training_images)
np.random.set_state(state)
np.random.shuffle(training_labels)
return np.asarray([training_images, training_labels,validation_images, validation_labels,testing_images, testing_labels]) with tf.Session() as sess:
processed_data = create_image_lists(sess, TEST_PERCENTAGE, VALIDATION_PERCENTAGE)
# 通过numpy格式保存处理后的数据。
np.save(OUTPUT_FILE, processed_data)

最新文章

  1. iOS开发-UI 从入门到精通(二)
  2. Unable to find messages file &#39;cscui.dll&#39; 问题解决
  3. CentOS安装squid代理服务器
  4. Ptex源码学习笔记-1
  5. oracle日期操作
  6. 【基础知识】.Net基础加强11天
  7. hdu 1361 Parencodings 简单模拟
  8. Careercup - Google面试题 - 5765091433644032
  9. R语言基础(二) 可视化基础
  10. javascript笔记——jqGrid再次封装
  11. android设置组件所占的比例
  12. 九度OJ 1035:找出直系亲属(二叉树)
  13. Overlapping rectangles判断两个矩形是否重叠的问题 C++
  14. NiftyNet 数据预处理
  15. MySQL STR_TO_DATE函数
  16. Modbus库开发笔记:Modbus ASCII Slave开发
  17. SpringMVC 常用applicationContext.xml、web.xml、servlet-mvc.xml简单配置
  18. window10+linux双系统安装
  19. spring boot 系列之五:spring boot 通过devtools进行热部署
  20. Web前端js下载流文件

热门文章

  1. 题解【CJOJ1371】[IOI2002]任务安排
  2. LED Decorative Light Supplier Introduction - LED Track Light Products
  3. Java代码如何关联Hadoop源码
  4. 剑指offer 面试题38 字符串的排列
  5. Vue - 表单修饰符
  6. angular2项目打包部署的坑
  7. 吴裕雄 python 机器学习——集成学习随机森林RandomForestClassifier分类模型
  8. 【转】Chrome——F12 谷歌开发者工具详解
  9. RPA_播放语音
  10. php 下载word 含图片