from keras.utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
import sys
import cv2
from keras import regularizers
from keras.models import Sequential
from keras.layers.core import Dense,Dropout,Activation,Flatten
from keras.layers import Conv2D,MaxPooling2D,BatchNormalization
from keras.optimizers import SGD,Adam,RMSprop
from keras.callbacks import TensorBoard
IMG_CHANNELS=3
weight_decay = 0.0005
IMG_ROWS=224
IMG_COLS=224
BATCH_SIZE=14#输出
NB_EPOCH=10
NB_CLASSES=2
VERBOSE=1
VALIDATION_SPLIT=0.2
OPTIM=RMSprop()
x_test=np.empty((500,IMG_ROWS,IMG_COLS,3),np.float16)
x_train=np.empty((200,IMG_ROWS,IMG_COLS,3),np.float16)
train_data=np.zeros(200)
test_data=np.zeros(500)
#读入训练样本
for i in range(250):
if i<125:
test_data[i]=1
imagepath = 'E:/catdog/data/train/cat/cat.'+str(i)+'.jpg'
image1 = cv2.imread(imagepath)
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
image1 = cv2.resize(image1,(IMG_ROWS,IMG_COLS))
#plt.imshow(image1)
#plt.show()
x_test[i,:,:,:]=image1
else:
test_data[i]=0
imagepath='E:/catdog/data/train/dog/dog.'+str(i-125)+'.jpg'
image1 = cv2.imread(imagepath)
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
image1 = cv2.resize(image1, (IMG_ROWS, IMG_COLS))
#plt.imshow(image1)
#plt.show()
x_test[i, :, :, :] = image1
y_test=to_categorical(test_data)#标签
x_test=np.array(x_test)
#读入测试样本
for i in range(200):
if i<100:
train_data[i]=1
imagepath = 'E:/catdog/data/train/cat/cat.'+str(i+250)+'.jpg'
image1 = cv2.imread(imagepath)
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
image1 = cv2.resize(image1,(IMG_ROWS,IMG_COLS))
x_train[i,:,:,:]=image1
else:
train_data[i]=0
imagepath='E:/catdog/data/train/dog/dog.'+str(i+250-100)+'.jpg'
image1 = cv2.imread(imagepath)
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
image1 = cv2.resize(image1, (IMG_ROWS, IMG_COLS))
x_train[i, :, :, :] = image1
y_train=to_categorical(train_data)
print(sys.getsizeof(x_train),sys.getsizeof(y_train))
x_train=np.array(x_train)
x_train=x_train/255
x_test = x_test/255
print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)
model = Sequential()
model.add(Conv2D(64, (3, 3), padding='same',
input_shape=(IMG_ROWS, IMG_COLS, 3), kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.3))
# layer2 32*32*64
model.add(Conv2D(64, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
# layer3 16*16*64
model.add(Conv2D(128, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.4))
# layer4 16*16*128
model.add(Conv2D(128, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
# layer5 8*8*128
model.add(Conv2D(256, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.4))
# layer6 8*8*256
model.add(Conv2D(256, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.4))
# layer7 8*8*256
model.add(Conv2D(256, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
# layer8 4*4*256
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.4))
# layer9 4*4*512
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.4))
# layer10 4*4*512
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
# layer11 2*2*512
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.4))
# layer12 2*2*512
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.4))
# layer13 2*2*512
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
# layer14 1*1*512
model.add(Flatten())
model.add(Dense(512, kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
# layer15 512
model.add(Dense(512, kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('relu'))
model.add(BatchNormalization())
# layer16 512
model.add(Dropout(0.5))
model.add(Dense(NB_CLASSES))
model.add(Activation('sigmoid'))
# 10
model.summary()
print(x_train.shape)
model.compile(loss='categorical_crossentropy',optimizer=OPTIM,metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=BATCH_SIZE,epochs=NB_EPOCH,validation_split=VALIDATION_SPLIT,verbose=VERBOSE,
callbacks=[TensorBoard(log_dir='./tmp/cat_dog')])
score=model.evaluate(x_test,y_test,batch_size=BATCH_SIZE,verbose=VERBOSE)
print("Test score:",score[0])
print("Test accuracy:",score[1])