当前位置: 首页 > news >正文

wordpress迁移安装seo查询百科

wordpress迁移安装,seo查询百科,网站开发女生可以做吗,云南网络科技公司排名简介: 1.基于VGG16模型进行特征提取, 结合mlp实现猫狗二分类 2.训练数据--"dog_cat_class\training_set" 3.模型训练流程 1.对图像数据进行导入和预处理 2.搭建模型, 导入VGG16模型, 去除mlp层, 将经过VGG16训练后的数据作为输入, 输入到自建的mlp层中进行训练, 要…

简介: 

        1.基于VGG16模型进行特征提取, 结合mlp实现猫狗二分类

        2.训练数据--"dog_cat_class\training_set"

        3.模型训练流程

                        1.对图像数据进行导入和预处理

                        2.搭建模型, 导入VGG16模型, 去除mlp层,  将经过VGG16训练后的数据作为输入, 输入到自建的mlp层中进行训练,

                                要求:

                                           hidden layers=1, units=10, activation=relu

                                           out layer:units=1, activation=sigmoid

                        3.对模型进行评估和预测

                        4.随机下载百度的12张猫/狗的图片, 对模型进行实战测试

        4.代码实现(推荐直接看第二个, 比较规范)

4.1,   小垃圾写的(我写的)

# ============================================================
# 1.数据集导入, 单张图片导入, 可以通过load_image导入
from keras.preprocessing.image import load_img, img_to_array
img_path=r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\dogs\dog.1.jpg"
img_data=load_img(img_path, target_size=(224,224))
img_data=img_to_array(img_data)
# print(img_data.shape)
# type(img_data)# 2.模型搭建和模型训练
# 对图像增加一个维度, 然后通过图像预处理, 成为合格的图像输入格式, 
from keras.applications.vgg16 import preprocess_input
import numpy as np
x_train=np.expand_dims(img_data, axis=0)  # 这里最好换一个名字, 如果接受变量还是img_data的话, 当再一次执行这个代码单元, 会增加数组的维度
x_train=preprocess_input(x_train)
# print(x_train.shape)
#  将合格的图片数据输入到VGG16模型中
from keras.applications.vgg16 import VGG16
extract_model=VGG16(include_top=False, weights='imagenet')
img_features=extract_model.predict(x_train)
print(img_features.shape)# 实现对图片的批量读入
import numpy as npfrom keras.applications.vgg16 import VGG16
vgg=VGG16(include_top=False, weights='imagenet')from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import preprocess_input 
def model_prepro(model, img_path):img_data=load_img(img_path, target_size=(224,224))img_array=img_to_array(img_data)x_train=np.expand_dims(img_array, axis=0)x_train=preprocess_input(x_train)x_vgg=model.predict(x_train)x_vgg=x_vgg.reshape(1, 25088)return x_vggimport os
# file_path=r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\"+sub_file
file_path1=r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\cat"
img_name_list = os.listdir(file_path1)img_list=[]
for i in img_name_list:if os.path.splitext(i)[1]=='.jpg':  img_list.append(i)
img_path_list=[os.path.join(file_path1, i) for i in img_list]  
img_feature_array1=np.zeros([len(img_path_list), 25088])        
for i in range(len(img_path_list)):img_feature=model_prepro(vgg, img_path_list[i])img_feature_array1[i]=img_feature# 显示正在处理的图片print("preprecessing is"+img_list[i])#####################################################
file_path2=r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\dog"
img_name_list = os.listdir(file_path2)img_list=[]
for i in img_name_list:if os.path.splitext(i)[1]=='.jpg':  img_list.append(i)
img_path_list=[os.path.join(file_path2, i) for i in img_list]  
img_feature_array2=np.zeros([len(img_path_list), 25088])        
for i in range(len(img_path_list)):img_feature=model_prepro(vgg, img_path_list[i])img_feature_array2[i]=img_feature# 显示正在处理的图片print("preprecessing is"+img_list[i])print(img_feature_array1.shape, img_feature_array2.shape)
y1=np.zeros(600)
y2=np.ones(602)
x_all=np.concatenate((img_feature_array1, img_feature_array2), axis=0)
y_all=np.concatenate((y1, y2), axis=0)
y_all=y_all.reshape(-1,1)
print(x_all.shape, y_all.shape)
# 分割数据集
from sklearn.model_selection import train_test_split
x_train, x_test,y_train, y_test = train_test_split(x_all, y_all, test_size=0.3, random_state=10)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)# MLP模型搭建和训练
from keras.models import Sequential
vgg_model=Sequential()
from keras.layers import Dense
vgg_model.add(Dense(units=10, input_dim=25088, activation='relu'))
vgg_model.add(Dense(units=1, activation='sigmoid'))
vgg_model.compile(optimizer='adam', metrics=['accuracy'], loss='binary_crossentropy')
vgg_model.fit(x_train, y_train, epochs=50)
vgg_model.summary()# ==========================================================================
# 训练集预测
y_train_predict=vgg_model.predict(x_train)
y_train_predict=np.argmax(y_train_predict, axis=1)
print(y_train_predict.shape)
# 计算train准确率
from sklearn.metrics import accuracy_score
accuracy_score=accuracy_score(y_train, y_train_predict)
print("accuracy is ", accuracy_score)# 测试集预测
y_test_predict=vgg_model.predict(x_test)
y_test_predict=np.argmax(y_test_predict, axis=1)
print(y_test_predict.shape)
# 计算test准确率
from sklearn.metrics import accuracy_score
accuracy_score=accuracy_score(y_test, y_test_predict)
print("accuracy is ", accuracy_score)# ====================================================================
#  在网上下载图片, 进行随机测试
from keras.preprocessing.image import load_img, img_to_array
pic_animal=r"C:\Users\鹰\Desktop\Dog+Cat\11.jpg"
pic_animal=load_img(pic_animal, target_size=(224,224))
pic_animal=img_to_array(pic_animal)
x_train=np.expand_dims(pic_animal, axis=0)
x_train=preprocess_input(x_train)
# 特征提取
features=vgg.predict(x_train)
x=features.reshape(1, -1)
print(x.shape)
print(features.shape)
y_predict=vgg_model.predict(x)
import numpy as np
y_predict=np.argmax(y_predict, axis=1)
print("result is :", y_predict)
# 结果为0--猫, 结果为1--狗
结果是...

 

 4.2: 千问大模型修改后的

import numpy as np
from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import preprocess_input, VGG16
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from sklearn.metrics import accuracy_score
import os# 1. 数据集导入, 单张图片导入, 可以通过load_image导入
img_path = r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\dogs\dog.1.jpg"
img_data = load_img(img_path, target_size=(224, 224))
img_data = img_to_array(img_data)
print("Single image shape:", img_data.shape)# 2. 模型搭建和模型训练
def model_prepro(model, img_path):img_data = load_img(img_path, target_size=(224, 224))img_array = img_to_array(img_data)x_train = np.expand_dims(img_array, axis=0)x_train = preprocess_input(x_train)x_vgg = model.predict(x_train)x_vgg = x_vgg.reshape(1, -1)return x_vgg# 加载 VGG16 模型
vgg = VGG16(include_top=False, weights='imagenet')# 处理 cat 文件夹
file_path1 = r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\cat"
img_name_list = os.listdir(file_path1)
img_list = [i for i in img_name_list if os.path.splitext(i)[1].lower() == '.jpg']
img_path_list = [os.path.join(file_path1, i) for i in img_list]
img_feature_array1 = np.zeros([len(img_path_list), 25088])
for i in range(len(img_path_list)):img_feature = model_prepro(vgg, img_path_list[i])img_feature_array1[i] = img_featureprint(f"Processing: {img_list[i]} (Cat)")# 处理 dog 文件夹
file_path2 = r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\dog"
img_name_list = os.listdir(file_path2)
img_list = [i for i in img_name_list if os.path.splitext(i)[1].lower() == '.jpg']
img_path_list = [os.path.join(file_path2, i) for i in img_list]
img_feature_array2 = np.zeros([len(img_path_list), 25088])
for i in range(len(img_path_list)):img_feature = model_prepro(vgg, img_path_list[i])img_feature_array2[i] = img_featureprint(f"Processing: {img_list[i]} (Dog)")print("Feature array shapes:", img_feature_array1.shape, img_feature_array2.shape)# 创建标签
y1 = np.zeros(len(img_feature_array1))
y2 = np.ones(len(img_feature_array2))# 合并特征和标签
x_all = np.concatenate((img_feature_array1, img_feature_array2), axis=0)
y_all = np.concatenate((y1, y2), axis=0)
y_all = y_all.reshape(-1, 1)
print("Combined data shapes:", x_all.shape, y_all.shape)# 分割数据集
x_train, x_test, y_train, y_test = train_test_split(x_all, y_all, test_size=0.3, random_state=10)
print("Data split shapes:", x_train.shape, x_test.shape, y_train.shape, y_test.shape)# MLP模型搭建和训练
vgg_model = Sequential()
vgg_model.add(Dense(units=128, input_dim=25088, activation='relu'))
vgg_model.add(Dense(units=64, activation='relu'))
vgg_model.add(Dense(units=1, activation='sigmoid'))
vgg_model.compile(optimizer='adam', metrics=['accuracy'], loss='binary_crossentropy')
vgg_model.fit(x_train, y_train, epochs=100, batch_size=32, validation_data=(x_test, y_test))
vgg_model.summary()# 训练集预测
y_train_predict = vgg_model.predict(x_train)
y_train_predict = (y_train_predict > 0.5).astype(int)  # 使用阈值 0.5 进行二分类
print("Train prediction shape:", y_train_predict.shape)# 计算train准确率
train_accuracy = accuracy_score(y_train, y_train_predict)
print("Train accuracy is:", train_accuracy)# 测试集预测
y_test_predict = vgg_model.predict(x_test)
y_test_predict = (y_test_predict > 0.5).astype(int)  # 使用阈值 0.5 进行二分类
print("Test prediction shape:", y_test_predict.shape)# 计算test准确率
test_accuracy = accuracy_score(y_test, y_test_predict)
print("Test accuracy is:", test_accuracy)# 在网上下载图片, 进行随机测试
pic_animal = r"C:\Users\鹰\Desktop\Dog+Cat\11.jpg"
pic_animal = load_img(pic_animal, target_size=(224, 224))
pic_animal = img_to_array(pic_animal)
x_train = np.expand_dims(pic_animal, axis=0)
x_train = preprocess_input(x_train)# 特征提取
features = vgg.predict(x_train)
x = features.reshape(1, -1)
print("Feature shape:", x.shape)
print("Feature shape before reshape:", features.shape)# 预测
y_predict = vgg_model.predict(x)
y_predict = (y_predict > 0.5).astype(int)  # 使用阈值 0.5 进行二分类
print("Prediction result is:", "猫" if y_predict[0][0] == 0 else "狗")
 结果是...

这对我的心灵的伤害是百分百的暴击, 我的是反面教材........

想要看正版规范代码, 就看第二个,     当然, 

如果觉得50%的成功率还行的话,   那我的勉强也能看

兄弟们不嫌弃的话, 也可以看看,     吸取一下经验教训, 看个乐子

5.扩展

        扩展1:

                keras.models 模块中的主要组成部分:

                        1.Sequential 模型是一种线性堆叠的层结构,适用于大多数简单的神经网络

                        2.Functional API 是一种更灵活的模型构建方式,允许创建复杂的非线性拓扑结构

        扩展2:

                keras.applications 导入 VGG16 时,你可以得到以下主要部分:

                       VGG16 Model: 这是整个 VGG16 网络模型,可以直接用来进行预测或者作为迁移学习的基础。

                       Preprocess Input: 一个函数,用于对输入图像数据进行预处理,以便与 VGG16 模型兼容。实现:from keras.applications.vgg16 import preprocess_input。

                      Decode Predictions: 一个函数,用于将 VGG16 模型的输出转换为人类可读的标签。实现:from keras.applications.vgg16 import decode_predictions。

                        Weights: 预训练的权重文件。这些权重是在 ImageNet 数据集上训练得到的,可以帮助你在自己的任务上快速获得较好的性能。

 6.数据集链接:

官网:

Cat and Dog | KaggleCats and Dogs dataset to train a DL modelicon-default.png?t=O83Ahttps://www.kaggle.com/datasets/tongpython/cat-and-dog?resource=download

百度网盘分享:

链接:https://pan.baidu.com/s/1T1mymwIqOOF3MKfWxRtnpQ 
提取码:6axn

http://www.mmbaike.com/news/49472.html

相关文章:

  • 余姚 做网站全国疫情突然又严重了
  • 湛江建设网站如何开通网站
  • 公司网站百度排名没有了网站域名怎么注册
  • 武汉网站建设不推广宽带业务如何推广
  • 被墙的网站有哪些深圳seo优化公司哪家好
  • 顺德做网站的公司百度旅游官网
  • 无锡做公司网站的个人开发app最简单方法
  • 深圳seo网站优化google谷歌搜索主页
  • 企业互联网服务平台外贸网站seo推广教程
  • 化妆品行业网站开发国外搜索引擎优化
  • 安卓系统开发公司关键词优化是怎么做的
  • 网站升级维护需要多久想要网站导航推广页
  • 武昌做网站公司软件开发培训机构
  • 金坛建设局招标网站海外建站
  • 怎么在vps上做网站商务软文写作范文200字
  • 网站开发实例百度站长seo
  • 在线 网站建设国内搜索引擎有哪些
  • 宿迁做网站需要多少钱厦门seo排名
  • 外贸网站建站k资源搜索
  • 阿里云营销网站建设五八精准恶意点击软件
  • 江苏网站建设网站排名优化搜索引擎优化的含义和目标
  • 用axure做的网站成品好的建站网站
  • wordpress 弹幕泸州网站优化推广
  • 企业网站备案还是不用备案找片子有什么好的关键词推荐
  • 上海网站建设专业公司百度关键词排名批量查询工具
  • 大型网站开发教程商丘seo排名
  • 网站架设的结构南京百度推广
  • 创建网站运营费用手机网站怎么优化关键词
  • 广告品牌设计机构网站织梦模板百度seo查询系统
  • 珲春网站建设地推项目发布平台