keras框架下搭建卷积神经网络,并导入自己的图片做分类。保存自己的keras模型,并重新载入

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

自己的图像导入可以参照上文。

在Keras框架下搭建一个卷积神经网络,结构如下:

                                keras框架下搭建卷积神经网络,并导入自己的图片做分类。保存自己的keras模型,并重新载入_第1张图片

###一、导入必要的包
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)
Epoch 1/20
1820/1820 [==============================] - 19s 10ms/step - loss: 0.0763 - acc: 0.9643
Epoch 2/20
1820/1820 [==============================] - 15s 8ms/step - loss: 0.0061 - acc: 0.9989
Epoch 3/20
1820/1820 [==============================] - 16s 9ms/step - loss: 0.0028 - acc: 1.0000
Epoch 4/20
1820/1820 [==============================] - 15s 8ms/step - loss: 0.0014 - acc: 1.0000
Epoch 5/20
1820/1820 [==============================] - 16s 9ms/step - loss: 8.4464e-04 - acc: 1.0000
Epoch 6/20
1820/1820 [==============================] - 16s 9ms/step - loss: 6.3100e-04 - acc: 1.0000
Epoch 7/20
1820/1820 [==============================] - 16s 9ms/step - loss: 4.4503e-04 - acc: 1.0000
Epoch 8/20
1820/1820 [==============================] - 16s 9ms/step - loss: 3.7848e-04 - acc: 1.0000
Epoch 9/20
1820/1820 [==============================] - 16s 9ms/step - loss: 3.4882e-04 - acc: 1.0000
Epoch 10/20
1820/1820 [==============================] - 17s 9ms/step - loss: 2.7042e-04 - acc: 1.0000
Epoch 11/20
1820/1820 [==============================] - 16s 9ms/step - loss: 2.5073e-04 - acc: 1.0000
Epoch 12/20
1820/1820 [==============================] - 16s 9ms/step - loss: 1.5670e-04 - acc: 1.0000
Epoch 13/20
1820/1820 [==============================] - 16s 9ms/step - loss: 1.8972e-04 - acc: 1.0000
Epoch 14/20
1820/1820 [==============================] - 16s 9ms/step - loss: 1.2710e-04 - acc: 1.0000
Epoch 15/20
1820/1820 [==============================] - 16s 9ms/step - loss: 1.1341e-04 - acc: 1.0000
Epoch 16/20
1820/1820 [==============================] - 16s 9ms/step - loss: 1.4600e-04 - acc: 1.0000
Epoch 17/20
1820/1820 [==============================] - 16s 9ms/step - loss: 9.3298e-05 - acc: 1.0000
Epoch 18/20
1820/1820 [==============================] - 16s 9ms/step - loss: 7.5278e-05 - acc: 1.0000
Epoch 19/20
1820/1820 [==============================] - 16s 9ms/step - loss: 6.0495e-05 - acc: 1.0000
Epoch 20/20
1820/1820 [==============================] - 16s 9ms/step - loss: 5.7501e-05 - acc: 1.0000
Out[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))



 
 

你可能感兴趣的:(keras框架下搭建卷积神经网络,并导入自己的图片做分类。保存自己的keras模型,并重新载入)