Python将自己的图片数据集导入h5py,做识别的预处理
自己的图像导入可以参照上文。
在Keras框架下搭建一个卷积神经网络,结构如下:
###一、导入必要的包
import numpy as np
#import tensorflow as tf
from keras import layers
from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.models import Model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
import pydot
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
import keras.backend as K
K.set_image_data_format('channels_last')
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
import os
import numpy as np
from PIL import Image
import tensorflow as tf
import sklearn
from sklearn import preprocessing
import h5py
import scipy
#导入必要的包
%matplotlib inline
# 二、导入 hdf5 数据集
#我的图片大小为(64*64*3)
train_dataset = h5py.File('data.h5', 'r')
X_train = np.array(train_dataset['X_train'][:]) # your train set features
Y_train = np.array(train_dataset['y_train'][:]) # your train set labels
X_test = np.array(train_dataset['X_test'][:]) # your train set features
Y_test = np.array(train_dataset['y_test'][:]) # your train set labels
train_dataset.close()
print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))
(1820, 64, 64, 3)
(1820, 1)
1.0
0.0
(450, 64, 64, 3)
(450, 1)
#三、建立模型
# GRADED FUNCTION: TumbleModel
def TumbleModel(input_shape):
"""
Implementation of the HappyModel.
Arguments:
input_shape -- shape of the images of the dataset
Returns:
model -- a Model() instance in Keras
"""
### START CODE HERE ###
# Feel free to use the suggested outline in the text above to get started, and run through the whole
# exercise (including the later portions of this notebook) once. The come back also try out other
# network architectures as well.
X_input = Input(shape=input_shape)
X = ZeroPadding2D(padding=(1, 1))(X_input)
X = Conv2D(8, kernel_size=(3,3), strides=(1,1))(X)
X = BatchNormalization(axis=3)(X)
X = Activation('relu')(X)
X = MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid')(X) #32*32*8
X = ZeroPadding2D(padding=(1, 1))(X)
X = Conv2D(16, kernel_size=(3,3), strides=(1,1))(X) #32*32*16
X = BatchNormalization(axis=3)(X)
X = Activation('relu')(X)
X = MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid')(X) #16*16*16
X = ZeroPadding2D(padding=(1, 1))(X)
X = Conv2D(32, kernel_size=(3,3), strides=(1,1))(X) #16*16*32
X = BatchNormalization(axis=3)(X)
X = Activation('relu')(X)
X = MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid')(X) #8*8*32
# FC
X = Flatten()(X)
Y = Dense(1, activation='sigmoid')(X)
model = Model(inputs = X_input, outputs = Y, name='TumbleModel')
### END CODE HERE ###
return model
tumbleModel = TumbleModel((64, 64, 3))
import keras
tumbleModel.compile(optimizer=keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0), loss='binary_crossentropy', metrics=['accuracy'])
#训练模型
tumbleModel.fit(x=X_train, y=Y_train, batch_size=16, epochs=20)
#测试模型
preds = tumbleModel.evaluate(x=X_test, y=Y_test)
print()
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))
450/450 [==============================] - 2s 4ms/step
Loss = 0.010784328720615627
Test Accuracy = 0.9955555555555555
#测试自己的图片
img_path = 'images/img_angleview_p21_faint_a1_204_symmetry.png'
img = image.load_img(img_path, target_size=(64, 64))
imshow(img)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
print(tumbleModel.predict(x))
#四、保存自己的模型,并将其删除
#该文件将包含:
#模型的结构,以便重构该模型
#模型的权重
#训练配置(损失函数,优化器等)
#优化器的状态,以便于从上次训练中断的地方开始
from keras.models import load_model
tumbleModel.save('tumblemodel.h5') # creates a HDF5 file 'my_model.h5'
del tumbleModel # deletes the existing model
五、重新导入自己的模型
另外开一个ipynb(Jupyter Notebook), 即重新写一个程序
import numpy as np
from keras.models import load_model
from keras.models import Model
import h5py
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
%matplotlib inline
# returns a compiled model
# identical to the previous one
tumbleModel = load_model('tumblemodel.h5')
# 导入测试数据 hdf5 dataset
train_dataset = h5py.File('data.h5', 'r')
X_test = np.array(train_dataset['X_test'][:]) # your train set features
Y_test = np.array(train_dataset['y_test'][:]) # your train set labels
train_dataset.close()
preds = tumbleModel.evaluate(x=X_test, y=Y_test)
print()
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))
img_path = 'images/img_angleview_p21_faint_a1_204_symmetry.png'
img = image.load_img(img_path, target_size=(64, 64))
imshow(img)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
print(tumbleModel.predict(x))