import keras import matplotlib.pyplot as plt # for plotting import os # provides a way of using operating system dependent functionality import cv2 #Image handling library import numpy as np import keras.backend as K from keras.callbacks import Callback,ModelCheckpoint from keras.models import Sequential,load_model from keras.layers import Dense, Dropout from keras.wrappers.scikit_learn import KerasClassifier import keras.backend as K from sklearn.datasets import make_multilabel_classification # Import of keras model and hidden layers for our convolutional network from keras.layers import Conv2D, Activation, MaxPool2D, Dense, Flatten, Dropout
Using TensorFlow backend.
读取目录,遍历文件夹
1 2 3 4 5 6 7
# paths for dataset data_path = "./hand_gesture_dataset" IMG_SIZE = 40 CATEGORIES = [] for i in os.listdir(data_path): CATEGORIES.append(i) CATEGORIES
data = {} for category in CATEGORIES: path = os.path.join(data_path, category) # print(os.listdir(path)) for img in os.listdir(path): img_arr = cv2.imread(os.path.join(path,img)) # ,cv2.IMREAD_GRAYSCALE image_data.append([img_arr,revice(category)]) # data[category] =image_data # print(data.get(categorys,img_arr))
plt.imshow(img_arr)
<matplotlib.image.AxesImage at 0x19a7e675e80>
打乱数据
1 2 3
# shuffle the input data import random random.shuffle(image_data)
将数据和label分离
1 2 3 4 5 6
input_data = [] label = [] for X, y in image_data: input_data.append(X) label.append(y) # input_data[:5]
1
# label
1 2 3 4 5 6 7 8
plt.figure(1, figsize=(10,10)) for i inrange(1,17): plt.subplot(4,4,i) plt.imshow(image_data[i][0], cmap='hot') plt.xticks([]) plt.yticks([]) # plt.title(CATEGORIES[label[i]][3:]) #plt.show()
归一化数值
1 2 3 4 5
# Normalizing the data input_data = np.array(input_data) label = np.array(label) input_data = input_data/255.0 input_data.shape
(96252, 40, 40, 3)
数据分割
1 2 3
from sklearn.datasets import make_multilabel_classification X,y=make_multilabel_classification(n_samples=500,n_features=4,n_classes=2,n_labels=3,random_state=1) X