keras搬砖系列-vgg16进行分类
表头:
import time
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
from keras.layers import Dropout, Flatten, Dense
from keras.layers.convolutional import Convolution2D,MaxPooling2D
from keras.models import Sequential
from keras.preprocessing import image
from keras import backend as K
K.set_image_data_format('channels_first')
import numpy as np
导入数据:
t0 = time.time()
height,width = 224,224
img_path = 'cat.jpg'
img = image.load_img(img_path,target_size=(224,224))
x = image.img_to_array(img)
x = np.expand_dims(x,axis=0)
x = preprocess_input(x)
将导入的图片变为224,224大小,然后将三维的图片变为四维的图片。
print("开始建模CNN ...")
model = Sequential()
# Block 1, 2层
model.add(Convolution2D(64, 3, 3, activation='relu',