关于auto-keras训练cnn模型
# 我在训练自己的人脸分类模型的时候发现图片的维度不能太高,经过很多次测试过后觉得一般人脸图片分为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模型的更多相关文章
- keras训练cnn模型时loss为nan
keras训练cnn模型时loss为nan 1.首先记下来如何解决这个问题的:由于我代码中 model.compile(loss='categorical_crossentropy', optimiz ...
- 入门项目数字手写体识别:使用Keras完成CNN模型搭建(重要)
摘要: 本文是通过Keras实现深度学习入门项目——数字手写体识别,整个流程介绍比较详细,适合初学者上手实践. 对于图像分类任务而言,卷积神经网络(CNN)是目前最优的网络结构,没有之一.在面部识别. ...
- Keras入门(四)之利用CNN模型轻松破解网站验证码
项目简介 在之前的文章keras入门(三)搭建CNN模型破解网站验证码中,笔者介绍介绍了如何用Keras来搭建CNN模型来破解网站的验证码,其中验证码含有字母和数字. 让我们一起回顾一下那篇文 ...
- 使用docker安装部署Spark集群来训练CNN(含Python实例)
使用docker安装部署Spark集群来训练CNN(含Python实例) http://blog.csdn.net/cyh_24/article/details/49683221 实验室有4台神服务器 ...
- 1.keras实现-->自己训练卷积模型实现猫狗二分类(CNN)
原数据集:包含 25000张猫狗图像,两个类别各有12500 新数据集:猫.狗 (照片大小不一样) 训练集:各1000个样本 验证集:各500个样本 测试集:各500个样本 1= 狗,0= 猫 # 将 ...
- keras入门(三)搭建CNN模型破解网站验证码
项目介绍 在文章CNN大战验证码中,我们利用TensorFlow搭建了简单的CNN模型来破解某个网站的验证码.验证码如下: 在本文中,我们将会用Keras来搭建一个稍微复杂的CNN模型来破解以上的 ...
- Keras 训练一个单层全连接网络的线性回归模型
1.准备环境,探索数据 import numpy as np from keras.models import Sequential from keras.layers import Dense im ...
- Python机器学习笔记:深入学习Keras中Sequential模型及方法
Sequential 序贯模型 序贯模型是函数式模型的简略版,为最简单的线性.从头到尾的结构顺序,不分叉,是多个网络层的线性堆叠. Keras实现了很多层,包括core核心层,Convolution卷 ...
- keras训练和保存
https://cloud.tencent.com/developer/article/1010815 8.更科学地模型训练与模型保存 filepath = 'model-ep{epoch:03d}- ...
随机推荐
- Swift-assert使用时机
什么时候使用断言呢? 包含下面的情况时使用断言: 1.整型下标索引作为值传给自定义索引实现的参数时,但下标索引值不能太低也不能太高时,使用断言 2.传值给函数但如果这个传过来的值无效时,函数就不能完成 ...
- 将sublime添加到右键菜单
sublime text 添加到鼠标右键功能: 把以下内容复制并保存到文件,重命名为:sublime_addright.reg,然后双击就可以了. (注意:需要把下面代码中的Sublime的安装目录( ...
- C#添加本地打印机
class Program { static void Main(string[] args) { const string printerName = "Print to file&quo ...
- BZOJ 1025 游戏(分组背包)
题目所谓的序列长度实际上就是各循环节的lcm+1. 所以题目等价于求出 一串数之和等于n,这串数的lcm种数. 由唯一分解定理可以联想到只要把每个素数的幂次放在一个分组里,然后对整体做一遍分组背包就行 ...
- 【bzoj4736/uoj#274】[清华集训2016]温暖会指引我们前行 语文题+LCT
题目描述 http://uoj.ac/problem/274 题解 语文题+LCT 对于这种语文题建议还是自己读题好一些... 读懂题后发现:由于温度互不相同,最大生成树上的路径必须走(不走的话温度大 ...
- NOIP2002 提高组
[NOIP2002] 提高组 T1.均分纸牌 算法:贪心(模拟) [分析]: 1.简化 2.过滤 3.辩证法 详见课件的例7 还有一种类似的思路是:求出平均值后,i←1 to n-1扫描,若a[i] ...
- BZOJ4103 [Thu Summer Camp 2015]异或运算 【可持久化trie树】
题目链接 BZOJ4103 题解 一眼看过去是二维结构,实则未然需要树套树之类的数据结构 区域异或和,就一定是可持久化\(trie\)树 观察数据,\(m\)非常大,而\(n\)和\(p\)比较小,甚 ...
- BZOJ2761 不重复的数字 【treap】
2761: [JLOI2011]不重复数字 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 5517 Solved: 2087 [Submit][S ...
- react设置默认state和默认props
1.默认状态设置 1.constructor (ES6) constructor(props) { this.state = { n: ... } } 2.getInitialState (ES5) ...
- Ext之延时加载
大家在多线程下使用extjs时应该遇到过以下情况: 同时渲染几个组件时,如果组件的内容是动态读取的时候,有时会出现后组件内容不是正确的渲染顺序出现的内容.比如同时渲染两个form,form的字段是动态 ...