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
VGG建立mlp模型
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 动物模型/心血管疾病模型/啮齿动物高血压模型-自发性高血压大鼠(SHR) 啮齿动物高血压模型 高血压是指在静息状态...
- 动物模型 心血管疾病模型 糖尿病性视网膜病变模型 糖尿病性视网膜病(DR)是糖尿病最常见的微血管并发症之一。 为了...
- 动物模型/神经系统疾病模型/β-淀粉样蛋白诱导阿尔茨海默病模型 研究表明,与AD相关的主要病理诱因是Aβ蛋白异常代...
- 动物模型/炎症与免疫疾病模型/LPS诱导的败血症模型 内毒素(LPS)是革兰氏阴性细菌的外膜成分,可引起动物强烈的...
- 动物模型 / 泌尿及生殖系统疾病模型 / 宫腔粘连模型 宫腔粘连模型 宫腔粘连(IUA)又称Asherman综合征...