from keras.utils import load_img, img_to_array
#加载数据
img = load_img("datas/gg.jpg", target_size=(224, 224))
img = img_to_array(img)
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np
#加载vgg模型,不要全连接层
model_vgg = VGG16(weights="imagenet", include_top=False)
#添加维度
x = np.expand_dims(img, axis=0)
x = preprocess_input(x)
x.shape #(1, 224, 224, 3)
#轮廓提取
features = model_vgg.predict(x)
#flatten
features = features.reshape(1, 7*7*512)
#批量预处理
def modelProcess(img_path, model):
img = load_img(img_path, target_size=(224, 224))
img = img_to_array(img)
x = np.expand_dims(img, axis=0)
x = preprocess_input(x)
x_vgg = model.predict(x)
x_vgg = x_vgg.reshape(1, 25088)
return x_vgg
import os
folder = "datas/dataset/vgg/cats"
dirs = os.listdir(folder)
img_path = []
for i in dirs:
if os.path.splitext(i)[1] == ".jpg":
img_path.append(i)
img_path = [folder + "/" + i for i in img_path]
features1 = np.zeros([len(img_path), 25088])
for i in range(len(img_path)):
feature_i = modelProcess(img_path[i], model_vgg)
print("preprocesses", img_path[i])
features1[i] = feature_i
folder = "datas/dataset/vgg/dogs"
dirs = os.listdir(folder)
img_path = []
for i in dirs:
if os.path.splitext(i)[1] == ".jpg":
img_path.append(i)
img_path = [folder + "/" + i for i in img_path]
features2 = np.zeros([len(img_path), 25088])
for i in range(len(img_path)):
feature_i = modelProcess(img_path[i], model_vgg)
print("preprocesses", img_path[i])
features2[i] = feature_i
y1 = np.zeros(300)
y2 = np.ones(300)
X = np.concatenate((features1, features2), axis=0)
y = np.concatenate((y1, y2), axis=0)
y = y.reshape(-1, 1)
#数据拆分
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=50)
from keras.models import Sequential
from keras.layers import Dense
#建立模型
model = Sequential()
model.add(Dense(units=10, activation="relu", input_dim=25088))
model.add(Dense(units=1, activation="sigmoid"))
model.summary()
#配置模型
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
#训练
model.fit(x_train, y_train, epochs=50)
from sklearn.metrics import accuracy_score
#训练集准确率
y_train_predict = model.predict(x_train)
a = np.ones(420)
b = a / 2
c = np.insert(y_train_predict, 0, b, axis=1)
y_train_predict = np.argmax(c, axis=1)
y_train_predict = y_train_predict.reshape(-1, 1)
accuracy_score(y_train, y_train_predict) # 1.0
#测试集准确率
y_test_predict = model.predict(x_test)
a = np.ones(180)
b = a / 2
c = np.insert(y_test_predict, 0, b, axis=1)
y_test_predict = np.argmax(c, axis=1)
y_test_predict = y_test_predict.reshape(-1, 1)
accuracy_score(y_test, y_test_predict) #0.9666666666666667
# 下载图片预测
img = load_img("datas/gg.jpg", target_size=(224, 224))
img = img_to_array(img)
x = np.expand_dims(img, axis=0)
x = preprocess_input(x)
features = model_vgg.predict(x)
features = features.reshape(1, 7*7*512)
result = model.predict(features)
c = np.insert(result, 0, 0.5, axis=1)
result = np.argmax(c, axis=1)
result = result.reshape(-1, 1)
result # 1
网友评论