Python将自己的图片数据集导入h5py,做识别的预处理
2018-03-21 15:45
781 查看
很多情况下,在训练卷积神经网络时,需要将自己的图片作为卷积神经网络的输入。
将自己的图片数据集导入h5py,所占空间小,使用方便
条件:自己的图片,eg:cats VS dogs,并将两类图片分别放置于两个文件夹(我这里是yes_tumble与not_tumble)import os
import numpy as np
from PIL import Image
import tensorflow as tf
import matplotlib.pyplot as plt
import sklearn
from sklearn import preprocessing
import h5py
import scipy
#导入必要的包
将自己的图片数据集导入h5py,所占空间小,使用方便
条件:自己的图片,eg:cats VS dogs,并将两类图片分别放置于两个文件夹(我这里是yes_tumble与not_tumble)import os
import numpy as np
from PIL import Image
import tensorflow as tf
import matplotlib.pyplot as plt
import sklearn
from sklearn import preprocessing
import h5py
import scipy
#导入必要的包
def get_files(file_dir): cats = [] label_cats = [] dogs = [] label_dogs = [] for file in os.listdir(file_dir+'/not_tumble'): cats.append(file_dir +'/not_tumble'+'/'+ file) label_cats.append(0) #添加标签,该类标签为0,此为2分类例子,多类别识别问题自行添加 for file in os.listdir(file_dir+'/yes_tumble'): dogs.append(file_dir +'/yes_tumble'+'/'+file) label_dogs.append(1) #把cat和dog合起来组成一个list(img和lab) image_list = np.hstack((cats, dogs)) label_list = np.hstack((label_cats, label_dogs)) #利用shuffle打乱顺序 temp = np.array([image_list, label_list]) temp = temp.transpose() np.random.shuffle(temp) #从打乱的temp中再取出list(img和lab) image_list = list(temp[:, 0]) label_list = list(temp[:, 1]) label_list = [int(i) for i in label_list] return image_list,label_list #返回两个list 分别为图片文件名及其标签 顺序已被打乱
train_dir = 'F:/CSISA_Picture' image_list,label_list = get_files(train_dir) print(len(image_list)) print(len(label_list))
#450为数据长度的20% Train_image = np.random.rand(len(image_list)-450, 64, 64, 3).astype('float32') Train_label = np.random.rand(len(image_list)-450, 1).astype('float32') Test_image = np.random.rand(450, 64, 64, 3).astype('float32') Test_label = np.random.rand(450, 1).astype('float32')
for i in range(len(image_list)-450): Train_image[i] = np.array(plt.imread(image_list[i])) Train_label[i] = np.array(label_list[i]) for i in range(len(image_list)-450, len(image_list)): Test_image[i+450-len(image_list)] = np.array(plt.imread(image_list[i])) Test_label[i+450-len(image_list)] = np.array(label_list[i])
# Create a new file f = h5py.File('data.h5', 'w') f.create_dataset('X_train', data=Train_image) f.create_dataset('y_train', data=Train_label) f.create_dataset('X_test', data=Test_image) f.create_dataset('y_test', data=Test_label) f.close()
# Load hdf5 dataset train_dataset = h5py.File('data.h5', 'r') train_set_x_orig = np.array(train_dataset['X_train'][:]) # your train set features train_set_y_orig = np.array(train_dataset['y_train'][:]) # your train set labels test_set_x_orig = np.array(train_dataset['X_test'][:]) # your train set features test_set_y_orig = np.array(train_dataset['y_test'][:]) # your train set labels f.close()
print(train_set_x_orig.shape) print(train_set_y_orig.shape) print(train_set_x_orig.max()) print(train_set_x_orig.min()) print(test_set_x_orig.shape) print(test_set_y_orig.shape)
#测试 plt.imshow(train_set_x_orig[222]) print(train_set_y_orig[222])
相关文章推荐
- python爬取人脸识别图片数据集/python爬去图片/python爬虫
- 制作自己的数据集之1 python3 实现cifar10数据集的可视化 --- 减少数据集6万张图片为100张图片
- 【MNIST/Python】手写体数字训练/测试数据集(图片格式)下载及分割预处理
- caffe 从mnsit示例到自己创建数据集再到识别手写图片
- python爬取人脸识别图片数据集/python爬去图片/python爬虫
- python爬取人脸识别图片数据集/python爬去图片/python爬虫
- 使用python将自己的图片数据集准换成为cnn的数据集
- python爬取人脸识别图片数据集/python爬去图片/python爬虫
- 京东猪脸识别比赛数据预处理:用Python将视频每一帧提取存储为图片
- python+caffe训练自己的图片数据流程
- 自己用python捣鼓的煎蛋网图片爬虫
- python模块之imghdr(识别不同格式的图片文件)
- python爬虫点触验证码的识别思路(图片版)
- mnsit 手写数据集 python3.x的读入 以及利用softmax回归进行数字识别
- Python人工智能之图片识别
- Python利用Face++实现身份证件图片识别
- PK/NN/*/SVM:实现手写数字识别(数据集50000张图片)比较3种算法神经网络、灰度平均值、SVM各自的准确率—Jason niu
- Python 3 色情图片识别
- Python numpy包 np.random.shuffle(x) 数据集 batch 预处理
- Python使用OCR技术识别图片中的文字(入门篇)