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学习指南>第二版.<数学分 ...
随机推荐
- 【西北师大-2108Java】第九次作业成绩汇总
[西北师大-2108Java]第九次作业成绩汇总 作业题目 面向对象程序设计(JAVA) 第11周学习指导及要求 实验目的与要求 (1)理解泛型概念: (2)掌握泛型类的定义与使用: (3)掌握泛型方 ...
- AcWing 33. 链表中倒数第k个节点
习题地址 https://www.acwing.com/solution/acwing/content/2997/ 题目描述输入一个链表,输出该链表中倒数第k个结点. 注意: k >= 0;如果 ...
- rsync的简介及使用
1.rsync的基础概述 1.什么是备份 相当于给源文件增加一个副本,但是备份只会备份当前状态的数据,当你在写数据是,不会备份新写入的数据,除非自己手动在备份一次. 2.为什么要做备份 1.需要备份一 ...
- POJ3974Palindrome(Manacher)
传送门 题目大意:求最长回文串 题解:Manacher 代码: #include<cstdio> #include<cstring> #include<iostream& ...
- echarts 中 参数的详讲
xAxis 属性 xAxis : [ { type : 'category',//坐标轴类型 // show:'',//是否显示 x 轴 //id:'',组件 ID.默认不指定. //gridInde ...
- Educational Codeforces Round 76 (Rated for Div. 2) D. Yet Another Monster Killing Problem 贪心
D. Yet Another Monster Killing Problem You play a computer game. In this game, you lead a party of
- php精确计算
php BC高精确度函数库 结果: php一般的取余 只是除以整数 bc精度取余 精确到了小数
- ubuntu 18.04 安装mysql 遇到语言格式不兼容性问题解决
安装mysql的时候,遇到了这样一个错误:perl: warning: Setting locale failed. perl: warning: Please check that your loc ...
- 【STM32H7教程】第31章 STM32H7的USART应用之RS485
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第31章 STM32H7的USART应用之RS48 ...
- PHPer的项目RESTful API设计规范是怎样的?
RESTful 是目前最流行的 API 设计规范,用于 Web 数据接口的设计. 什么是RESTful RESTful是一种软件设计风格, 主要用于客户端与服务端交互的软件. 一般来说RESTful ...