keras 学习笔记(二) ——— data_generator
data_generator
每次输出一个batch,基于keras.utils.Sequence
Base object for fitting to a sequence of data, such as a dataset.
Every
Sequencemust implement the__getitem__and the__len__methods. If you want to modify your dataset between epochs you may implementon_epoch_end. The method__getitem__should return a complete batch.Notes
Sequenceare a safer way to do multiprocessing. This structure guarantees that the network will only train once on each sample per epoch which is not the case with generators.
Sequence example: https://keras.io/utils/#sequence
#!/usr/bin/env python
# coding: utf-8 from keras.utils import Sequence
import numpy as np
from keras.preprocessing import image
from skimage.io import imread class My_Custom_Generator(Sequence) :
def __init__(self, image_filenames, labels, batch_size) :
self.image_filenames = image_filenames
self.labels = labels
self.batch_size = batch_size
def __len__(self) :
return (np.ceil(len(self.image_filenames) / float(self.batch_size))).astype(np.int) def __getitem__(self, idx) :
batch_y = self.labels[idx * self.batch_size : (idx+1) * self.batch_size]
batch_x = self.image_filenames[idx * self.batch_size : (idx+1) * self.batch_size]
batch_seq = [] #batch_seq
for x in batch_x: #len(x) =16
seq_img = []
for img in x: #len(item) =25
seq_img.append(image.img_to_array(imread(img)))
seq_x = np.array([seq_img])
batch_seq.append(seq_img)
batch_seq_list = np.array(batch_seq)
return batch_seq_list, np.array(batch_y)
两种将数据输出为numpy.array的方法
通过list转为numpy.array
速度快,list转array过程需要注意数据维度变化
''' list
batch_x =X_train_filenames[idx * batch_size : (idx+1) * batch_size]
batch_seq = [] #batch_seq
for x in batch_x: #len(x) =16
seq_img = []
for img in x: #len(item) =25
seq_img.append(image.img_to_array(imread(img)))
seq_x = np.array([seq_img])
batch_seq.append(seq_img)
batch_seq_list = np.array(batch_seq)
'''
利用np.empty
速度慢,开始前确定batch维度即可
'''numpy
batch_x =X_train_filenames[idx * batch_size : (idx+1) * batch_size]
batch_seq = np.empty((0,25,224,224,3),float)
for x in batch_x: #len(x) =16
seq_batch = np.empty((0,224,224,3),float)
for item in x: #len(item) =25
seq_batch = np.append(seq_batch, np.expand_dims(image.img_to_array(imread(item)), axis=0), axis = 0)
batch_seq2 = np.append(batch_seq, np.expand_dims((seq_batch), axis=0), axis = 0)
'''
keras 学习笔记(二) ——— data_generator的更多相关文章
- Keras学习笔记二:保存本地模型和调用本地模型
使用深度学习模型时当然希望可以保存下训练好的模型,需要的时候直接调用,不再重新训练 一.保存模型到本地 以mnist数据集下的AutoEncoder 去噪为例.添加: file_path=" ...
- Keras学习笔记——Hello Keras
最近几年,随着AlphaGo的崛起,深度学习开始出现在各个领域,比如无人车.图像识别.物体检测.推荐系统.语音识别.聊天问答等等.因此具备深度学习的知识并能应用实践,已经成为很多开发者包括博主本人的下 ...
- WPF的Binding学习笔记(二)
原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...
- AJax 学习笔记二(onreadystatechange的作用)
AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...
- [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计
源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...
- JMX学习笔记(二)-Notification
Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...
- java之jvm学习笔记二(类装载器的体系结构)
java的class只在需要的时候才内转载入内存,并由java虚拟机的执行引擎来执行,而执行引擎从总的来说主要的执行方式分为四种, 第一种,一次性解释代码,也就是当字节码转载到内存后,每次需要都会重新 ...
- Java IO学习笔记二
Java IO学习笔记二 流的概念 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入输 ...
- 《SQL必知必会》学习笔记二)
<SQL必知必会>学习笔记(二) 咱们接着上一篇的内容继续.这一篇主要回顾子查询,联合查询,复制表这三类内容. 上一部分基本上都是简单的Select查询,即从单个数据库表中检索数据的单条语 ...
- NumPy学习笔记 二
NumPy学习笔记 二 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...
随机推荐
- Codeforces Global Round 5
传送门 A. Balanced Rating Changes 签到,分正负搞一下就行. B. Balanced Tunnel 题意: 给出\(n\)辆车的进洞顺序和出洞顺序,问有多少量车实现了洞中超车 ...
- 如何创建Azure Face API和计算机视觉Computer Vision API
在人工智能技术飞速发展的当前,利用技术手段实现人脸识别.图片识别已经不是什么难事.目前,百度.微软等云计算厂商均推出了人脸识别和计算机视觉的API,其优势在于不需要搭建本地环境,只需要通过网络交互,就 ...
- Python连载47-json文件、正则表达式初步
一.在线工具 1.https://www.sojson.com/ 2.http://www.w3cshool.com.cn/json/ 3.http://www.runoob.com/json/jso ...
- php逻辑运算符 异或
- Groovy元编程应用之自动生成订单搜索接口测试用例集
背景 在 "Groovy元编程简明教程" 一文中,简明地介绍了 Groovy 元编程的特性. 那么,元编程可以应用哪些场合呢?元编程通常可以用来自动生成一些相似的模板代码. 在 & ...
- Swiper实现轮播图效果
为了实现轮播图(carousel)效果或左右滑动显示不同的内容,我们采用Swiper来实现. 需要引入swiper.min.css和swiper.min.js,文件可从https://github.c ...
- Alpine Linux 安装 lxml Pillow 失败
lixm 需要编译安装,因此需要先安装gcc g++ RUN apk add --update --no-cache g++ gcc libxslt-dev python3-dev openssl-d ...
- SSM定时任务(spring3.0)
SSM定时任务主要分为两部分 1.applicationContext.xml配置文件设置 设置如下: 在xmlns中添加:xmlns:task="http://www.springfram ...
- MySQL学习——操作数据库
MySQL学习——操作数据库 摘要:本文主要学习了使用DDL语句操作数据库的方法. 创建数据库 语法 create database [if not exists] 数据库名 [default] ch ...
- LinuxShell脚本——选择结构
LinuxShell脚本——选择结构 摘要:本文主要学习了Shell脚本中的选择结构. if-else语句 基本语法 最简单的用法就是只使用if语句,它的语法格式为: if 条件 then 命令 fi ...