打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python将自己的图片数据集导入h5py,做识别的预处理

  1. 转载自:https://blog.csdn.net/chenkz123/article/details/79640658  

很多情况下,在训练卷积神经网络时,需要将自己的图片作为卷积神经网络的输入。

将自己的图片数据集导入h5py,所占空间小,使用方便

条件:自己的图片,eg:cats VS dogs,并将两类图片分别放置于两个文件夹(我这里是yes_tumble与not_tumble)

  1. import os
  2. import numpy as np
  3. from PIL import Image
  4. import tensorflow as tf
  5. import matplotlib.pyplot as plt
  6. import sklearn
  7. from sklearn import preprocessing
  8. import h5py
  9. import scipy
  10. #导入必要的包
  1. def get_files(file_dir):
  2.     cats = []
  3.     label_cats = []
  4.     dogs = []
  5.     label_dogs = []
  6.     for file in os.listdir(file_dir+'/not_tumble'):
  7.             cats.append(file_dir +'/not_tumble'+'/'+ file) 
  8.             label_cats.append(0) #添加标签,该类标签为0,此为2分类例子,多类别识别问题自行添加
  9.     for file in os.listdir(file_dir+'/yes_tumble'):
  10.             dogs.append(file_dir +'/yes_tumble'+'/'+file)
  11.             label_dogs.append(1)
  12.     #把cat和dog合起来组成一个list(img和lab)
  13.     image_list = np.hstack((cats, dogs))
  14.     label_list = np.hstack((label_cats, label_dogs))
  15.     #利用shuffle打乱顺序
  16.     temp = np.array([image_list, label_list])
  17.     temp = temp.transpose()
  18.     np.random.shuffle(temp)
  19.     #从打乱的temp中再取出list(img和lab)
  20.     image_list = list(temp[:, 0])
  21.     label_list = list(temp[:, 1])
  22.     label_list = [int(i) for i in label_list] 
  23.     return  image_list,label_list
  24.     #返回两个list 分别为图片文件名及其标签  顺序已被打乱
  1. train_dir = 'F:/CSISA_Picture'
  2. image_list,label_list = get_files(train_dir)
  3. print(len(image_list))
  4. print(len(label_list))
  1. #450为数据长度的20%
  2. Train_image = np.random.rand(len(image_list)-450, 64, 64, 3).astype('float32')
  3. Train_label = np.random.rand(len(image_list)-450, 1).astype('float32')
  4. Test_image = np.random.rand(450, 64, 64, 3).astype('float32')
  5. Test_label = np.random.rand(450, 1).astype('float32')
  1. for i in range(len(image_list)-450):
  2. Train_image[i] = np.array(plt.imread(image_list[i]))
  3. Train_label[i] = np.array(label_list[i])
  4. for i in range(len(image_list)-450, len(image_list)):
  5. Test_image[i+450-len(image_list)] = np.array(plt.imread(image_list[i]))
  6. Test_label[i+450-len(image_list)] = np.array(label_list[i])
  1. # Create a new file
  2. f = h5py.File('data.h5', 'w')
  3. f.create_dataset('X_train', data=Train_image)
  4. f.create_dataset('y_train', data=Train_label)
  5. f.create_dataset('X_test', data=Test_image)
  6. f.create_dataset('y_test', data=Test_label)
  7. f.close()
  1. # Load hdf5 dataset
  2. train_dataset = h5py.File('data.h5', 'r')
  3. train_set_x_orig = np.array(train_dataset['X_train'][:]) # your train set features
  4. train_set_y_orig = np.array(train_dataset['y_train'][:]) # your train set labels
  5. test_set_x_orig = np.array(train_dataset['X_test'][:]) # your train set features
  6. test_set_y_orig = np.array(train_dataset['y_test'][:]) # your train set labels
  7. f.close()
  1. print(train_set_x_orig.shape)
  2. print(train_set_y_orig.shape)
  3. print(train_set_x_orig.max())
  4. print(train_set_x_orig.min())
  5. print(test_set_x_orig.shape)
  6. print(test_set_y_orig.shape)
  1. #测试
  2. plt.imshow(train_set_x_orig[222])
  3. print(train_set_y_orig[222])






本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
[action]tensorflow 深度学习实战(1) deep learning 清洗数据
基于深度学习的图像分类:使用卷积神经网络实现猫狗分类器
Implementing a CNN for Human Activity Recognition in Tensorflow
怎么调用pytorch中mnist数据集
Dataset之Fashion-MNIST:Fashion-MNIST数据集简介、下载、使用方法之详细攻略
目标检测的常用数据处理方法!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服