当前位置:   article > 正文

【Tensorflow】tensorflow和keras+读取jpg图像数据格式的MNIST数据集_keras读取图片数据集格式

keras读取图片数据集格式

1.数据集介绍

jpg图像数据格式的MNIST数据集:(放在database1文件夹下面)

 

2.tensorflow读取jpg图像数据格式的mnist数据集:

tensorflow1.x的读取方式:

 

 

 

tensorflow1.12以上的读取方式:(最好是1.13.1或者2.x)

https://blog.csdn.net/Black_Friend/article/details/104529859

  1. import tensorflow as tf
  2. import random
  3. import pathlib
  4. data_path = pathlib.Path('./database1/')
  5. print(type(data_path))#<class 'pathlib.WindowsPath'>
  6. all_image_paths = list(data_path.glob('*/*'))
  7. print(type(data_path.glob('*/*')))#<class 'generator'>
  8. # print(all_image_paths)
  9. all_image_paths = [str(path) for path in all_image_paths] # 所有图片路径的列表
  10. random.shuffle(all_image_paths) # 打散
  11. # print(all_image_paths[0:3])
  12. image_count = len(all_image_paths)
  13. print('image_count: ',image_count)
  14. label_names = sorted(item.name for item in data_path.glob('*/') if item.is_dir())
  15. print('label_names: ',label_names)
  16. label_to_index = dict((name, index) for index, name in enumerate(label_names))
  17. print('label_to_index: ',label_to_index)
  18. all_image_labels = [label_to_index[pathlib.Path(path).parent.name] for path in all_image_paths]
  19. db_train = tf.data.Dataset.from_tensor_slices((all_image_paths, all_image_labels))
  20. def load_and_preprocess_from_path_label(path, label):
  21. image = tf.io.read_file(path) # 读取图片
  22. image = tf.image.decode_jpeg(image, channels=3)
  23. image = tf.cast(image, dtype=tf.float32) / 255.0
  24. # image = tf.image.resize(image, [28, 28]) # 原始图片大小为(100, 100, 3),重设为(192, 192)
  25. # image /= 255.0 # 归一化到[0,1]范围
  26. label = tf.cast(label, dtype=tf.int32)
  27. label = tf.one_hot(label, depth=10)
  28. return image, label
  29. db_train.shuffle(1000)
  30. db_train.map(load_and_preprocess_from_path_label)
  31. db_train.batch(64)
  32. db_train.repeat(2)
  33. print(type(db_train))#<class 'tensorflow.python.data.ops.dataset_ops.DatasetV1Adapter'>
  34. print(db_train.output_shapes)#(TensorShape([]), TensorShape([]))

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号