from keras. models import Sequential
from keras. layers import Conv2D, MaxPool2D, Activation, Dropout, Flatten, Dense
from keras. optimizers import Adam
from keras. preprocessing. image import ImageDataGenerator, img_to_array, load_img
model = Sequential( )
model. add( Conv2D( input_shape= ( 150 , 150 , 3 ) , filters= 32 , kernel_size= 3 , padding= 'same' , activation= 'relu' ) )
model. add( Conv2D( filters= 32 , kernel_size= 3 , padding= 'same' , activation= 'relu' ) )
model. add( MaxPool2D( pool_size= 2 , strides= 2 ) )
model. add( Conv2D( filters= 64 , kernel_size= 3 , padding= 'same' , activation= 'relu' ) )
model. add( Conv2D( filters= 64 , kernel_size= 3 , padding= 'same' , activation= 'relu' ) )
model. add( MaxPool2D( pool_size= 2 , strides= 2 ) )
model. add( Conv2D( filters= 128 , kernel_size= 3 , padding= 'same' , activation= 'relu' ) )
model. add( Conv2D( filters= 128 , kernel_size= 3 , padding= 'same' , activation= 'relu' ) )
model. add( MaxPool2D( pool_size= 2 , strides= 2 ) )
model. add( Flatten( ) )
model. add( Dense( 64 , activation= 'relu' ) )
model. add( Dropout( 0.5 ) )
model. add( Dense( 2 , activation= 'softmax' ) )
adam = Adam( lr= 1e - 4 )
model. compile ( optimizer= adam, loss= 'categorical_crossentropy' , metrics= [ 'accuracy' ] )
train_datagen = ImageDataGenerator(
rotation_range = 40 ,
width_shift_range = 0.2 ,
height_shift_range = 0.2 ,
rescale = 1 / 255 ,
shear_range = 20 ,
zoom_range = 0.2 ,
horizontal_flip = True ,
fill_mode = 'nearest' ,
)
test_datagen = ImageDataGenerator(
rescale = 1 / 255 ,
)
batch_size = 32
train_generator = train_datagen. flow_from_directory(
'image/train' ,
target_size= ( 150 , 150 ) ,
batch_size= batch_size,
)
test_generator = test_datagen. flow_from_directory(
'image/test' ,
target_size= ( 150 , 150 ) ,
batch_size= batch_size,
)
print ( train_generator[ 0 ] [ 0 ] . shape)
train_generator. class_indices
传入数据训练,参数step_per_epoch指的是每代训练包含多少个批次
model. fit_generator( train_generator, steps_per_epoch= len ( train_generator) ,
epochs= 30 , validation_data= test_generator, validation_steps= len ( test_generator) )
model. save( 'model_cnn.h5' )
from keras. models import load_model
import numpy as np
label = np. array( [ 'cat' , 'dog' ] )
model = load_model( 'model_cnn.h5' )
image = load_img( 'image/test/cat/cat.1002.jpg' )
image
image = image. resize( ( 150 , 150 ) )
image = img_to_array( image)
image = image/ 255
image = np. expand_dims( image, 0 )
image. shape
print ( label[ model. predict_classes( image) ] )
print ( model. predict( image) )