# 我在训练自己的人脸分类模型的时候发现图片的维度不能太高,经过很多次测试过后觉得一般人脸图片分为28*28大小训练的效果比较好。
建议在使用其训练自己的物体识别模型的时候,尽量把图片压缩到28*28
# coding:utf-8
import time import matplotlib.pyplot as plt
from autokeras import ImageClassifier
from keras.engine.saving import load_model
from keras.utils import plot_model
from scipy.misc import imresize
import numpy as np
import pandas as pd
import random
import os from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score os.environ['TF_CPP_MIN_LOG_LEVEL'] = ''
# 导入图片的函数 def read_img(path):
nameList = os.listdir(path)
n = len(nameList)
# indexImg,columnImg = plt.imread(path+'/'+nameList[0]).shape
x_train = np.zeros([n,28,28,1]);y_train=[]
for i in range(n):
x_train[i,:,:,0] = imresize(plt.imread(path+'/'+nameList[i]),[28,28])
y_train.append(np.int(nameList[i].split('.')[1]))
return x_train,y_train x_train,y_train = read_img('./dataset')
y_train = pd.DataFrame(y_train)
n = len(y_train[y_train.iloc[:,0]==2]) x_train = np.array(x_train) x_wzp = np.random.choice(y_train[y_train.iloc[:,0]==1].index.tolist(),n,replace=False) x_train_w = x_train[x_wzp,:].copy()
x_train_l = x_train[y_train[y_train.iloc[:,0]==2].index.tolist()].copy()
x_train = np.concatenate([x_train_w,x_train_l],axis=0) print(x_train.shape) y_train = y_train.iloc[-208:,:].copy() # 对两组数据进行洗牌
index = random.sample(range(len(y_train)),len(y_train))
index = np.array(index)
y_train = y_train.iloc[index,:]
# y_train.plot()
# plt.show()
x_train = x_train[index,:,:,:] # x_train,x_test,y_train,y_test = train_test_split(x_train,y_train,test_size=0.2)
# print(x_train.shape,y_train.shape,x_test.shape,y_test.shape)
# y_test = y_test.values.reshape(-1)
y_train = y_train.values.reshape(-1) # 数据测试 print(y_train)
for i in range(5):
n = i*20
img = x_train[n,:,:,:].reshape((28,28))
print(y_train[n])
plt.figure()
plt.imshow(img,cmap='gray')
plt.xticks([])
plt.yticks([])
plt.show() if __name__=='__main__':
start = time.time()
# 模型构建
model = ImageClassifier(verbose=True)
# 搜索网络模型
model.fit(x_train,y_train,time_limit=1*60)
# 验证最优模型
model.final_fit(x_train,y_train,x_train,y_train,retrain=True)
# 给出评估结果
score = model.evaluate(x_train,y_train)
# 识别结果
y_predict = model.predict(x_train)
# y_pred = np.argmax(y_predict,axis=1)
# 精确度
accuracy = accuracy_score(y_train,y_predict)
# 打印出score与accuracy
print('score:',score,' accuracy:',accuracy)
print(y_predict,y_train)
model_dir = r'./trainer/auto_learn_Model.h5'
model_img = r'./trainer/imgModel_ST.png' # 保存可视化模型
# model.load_searcher().load_best_model().produce_keras_model().save(model_dir)
# 加载模型
# automodel = load_model(model_dir)
# 输出模型 structure 图
# plot_model(automodel, to_file=model_img) end = time.time()
print('time:',end-start)

关于auto-keras训练cnn模型的更多相关文章

  1. keras训练cnn模型时loss为nan

    keras训练cnn模型时loss为nan 1.首先记下来如何解决这个问题的:由于我代码中 model.compile(loss='categorical_crossentropy', optimiz ...

  2. 入门项目数字手写体识别:使用Keras完成CNN模型搭建(重要)

    摘要: 本文是通过Keras实现深度学习入门项目——数字手写体识别,整个流程介绍比较详细,适合初学者上手实践. 对于图像分类任务而言,卷积神经网络(CNN)是目前最优的网络结构,没有之一.在面部识别. ...

  3. Keras入门(四)之利用CNN模型轻松破解网站验证码

    项目简介   在之前的文章keras入门(三)搭建CNN模型破解网站验证码中,笔者介绍介绍了如何用Keras来搭建CNN模型来破解网站的验证码,其中验证码含有字母和数字.   让我们一起回顾一下那篇文 ...

  4. 使用docker安装部署Spark集群来训练CNN(含Python实例)

    使用docker安装部署Spark集群来训练CNN(含Python实例) http://blog.csdn.net/cyh_24/article/details/49683221 实验室有4台神服务器 ...

  5. 1.keras实现-->自己训练卷积模型实现猫狗二分类(CNN)

    原数据集:包含 25000张猫狗图像,两个类别各有12500 新数据集:猫.狗 (照片大小不一样) 训练集:各1000个样本 验证集:各500个样本 测试集:各500个样本 1= 狗,0= 猫 # 将 ...

  6. keras入门(三)搭建CNN模型破解网站验证码

    项目介绍   在文章CNN大战验证码中,我们利用TensorFlow搭建了简单的CNN模型来破解某个网站的验证码.验证码如下: 在本文中,我们将会用Keras来搭建一个稍微复杂的CNN模型来破解以上的 ...

  7. Keras 训练一个单层全连接网络的线性回归模型

    1.准备环境,探索数据 import numpy as np from keras.models import Sequential from keras.layers import Dense im ...

  8. Python机器学习笔记:深入学习Keras中Sequential模型及方法

    Sequential 序贯模型 序贯模型是函数式模型的简略版,为最简单的线性.从头到尾的结构顺序,不分叉,是多个网络层的线性堆叠. Keras实现了很多层,包括core核心层,Convolution卷 ...

  9. keras训练和保存

    https://cloud.tencent.com/developer/article/1010815 8.更科学地模型训练与模型保存 filepath = 'model-ep{epoch:03d}- ...

随机推荐

  1. iOS- UIPickerView餐厅点餐系统

    在餐厅里的点餐系统的核心控件就是UIPickerView 今天晚上在整理以前的项目笔记时,特意把UIPickerView单独拿出来,做了一个简陋的点餐道具. 因为没有素材图片,所有大家将就看看吧 0. ...

  2. phpcms 本地环境调试缓慢 解决办法

    用记事本打开host文件,(文件位置,windows下一般在路径C:\Windows\System32\drivers\etc下)找到#127.0.0.1      localhost 这一句  去掉 ...

  3. js 控制

    js 制动控制 代码 是 :setInterval(function(){$(".egg").click();},1000); 使用方法:调出浏览器放控制台(console),一般 ...

  4. Ehcache概念篇

    前言 缓存用于提供性能和减轻数据库负荷,本文在缓存概念的基础上对Ehcache进行叙述,经实践发现3.x版本高可用性不好实现,所以我采用2.x版本. Ehcache是开源.基于缓存标准.基于java的 ...

  5. 使用WebClient类对网页下载源码,对文件下载保存及异步下载并报告下载进度

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAx4AAAI7CAIAAADtTtpYAAAgAElEQVR4nO3dX6xlV33Y8f3UJFUqHq

  6. matlab edge

    edge在matlab中用来进行边缘检测BW = edge(I) 采用灰度或一个二值化图像I作为它的输入,并返回一个与I相同大小的二值化图像BW,在函数检测到边缘的地方为1,其他地方为0. BW = ...

  7. iOS进阶--将项目的编译速度提高5倍

    前言 作为开发团队的负责人,最近因为在快速迭代开发新功能,项目规模急速增长,单个端业务代码约23万行,私有库约6万行,第三方库代码约15万行,单个客户端的代码行数约60万.现在打包一次耗时需要11~1 ...

  8. BZOJ 1022 小约翰的游戏(anti-sg)

    这是个anti-sg问题,套用sj定理即可解. SJ定理 对于任意一个Anti-SG游戏,如果定义所有子游戏的SG值为0时游戏结束,先手必胜的条件: 1.游戏的SG值为0且所有子游戏SG值均不超过1. ...

  9. 用select模拟一个socket server

    1, 必须在非阻塞模式下,才能实现IO的多路复用,否则一个卡住就都卡住了.(单线程下的多路复用) 先检测自己,现在没有客户端连进来,所以会卡住. # 用select去模拟socket,实现单线程下的多 ...

  10. LOJ 模拟赛

    1.LOJ 507 接竹竿 link dp[i]表示前i个的最大分数,所以dp[i]=max(dp[i-1],dp[j-1]+sum[i]-sum[j-1])   (color i ==color j ...