Keras学习笔记一:修改数据读入方式为本地图片读入
第一种方法:
Keras官方给的图片去噪示例要自动下载mnist数据集并处理,不能修改和加入自己的数据集。
from keras.datasets import mnist
(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
以上代码实现了把mnist数据集读到x_train 和x_test 中并且丢弃标签,全过程是封闭的
现需要将本地的mnist数据集,解压成图片格式,然后通过文件操作把图片一个一个读进去同样存在x_train 和x_test 中,并且能和原来的程序完美衔接。
修改如下:
mnist数据集放到和py文件同一个目录,名为MNIST_data,将下载的二进制文件转为图片见 https://www.cnblogs.com/dzzy/p/10824072.html
目录树如图
import os
base_dir = 'MNIST_data' #基准目录
train_dir = os.path.join(base_dir,'mnist_train') #train目录
#file1 = os.listdir(train_dir) #读目录下的图
#image1 = [os.path.join(train_dir,i) for i in file1] #合成每一个图的路径名称
validation_dir="".join(train_dir)
test_datagen = ImageDataGenerator(rescale= 1./255)
validation_generator = test_datagen.flow_from_directory(validation_dir,
target_size = (28,28),
color_mode = "grayscale",
batch_size = 60000,
class_mode = "categorical")
#利用test_datagen.flow_from_directory(图像地址,单通道,目标size,批量数目,标签分类情况)
for x_train,batch_labels in validation_generator:
break test_dir = os.path.join(base_dir,'mnist_test') #test目录
#file2 = os.listdir(test_dir) #读目录下的图
#image2 = [os.path.join(test_dir,i) for i in file2] #合成每一个图的路径名称
validation_dir="".join(test_dir)
test_datagen = ImageDataGenerator(rescale= 1./255)
validation_generator = test_datagen.flow_from_directory(validation_dir,
target_size = (28,28),
color_mode = "grayscale",
batch_size = 10000,
class_mode = "categorical")
#利用test_datagen.flow_from_directory(图像地址,单通道,目标size,批量数目,标签分类情况) for x_test,batch_labels in validation_generator:
break #创造有噪声的图像
noise_factor = 0.5
x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape)
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
x_train_noisy = x_train_noisy.astype(np.float)
x_test_noisy = x_test_noisy.astype(np.float)
可以达到同样的效果,只是将图片逐个读到内存需要多花一些时间
第二种方法:
import glob
from PIL import Image
Datapath = "MNIST_data/mnist_train/*.png"
x_train = np.zeros(x_train.shape)
i = 0
for imageFile in glob.glob(Datapath ):
# 打开图像并转化为数字矩阵
img = np.array(Image.open(imageFile))
img = np.reshape(img, (1, 28, 28, 1))
img = img.astype('float32') / 255.
x_train[i] = img
i += 1
要求图片都在mnist_train目录下,同样可以达到目的
Keras学习笔记一:修改数据读入方式为本地图片读入的更多相关文章
- .NET Remoting学习笔记(二)激活方式
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科 ♂风车车.Net 激活方式概念 在 ...
- 【转载】.NET Remoting学习笔记(二)激活方式
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科 ♂风车车.Net 激活方式概念 在访 ...
- 官网实例详解-目录和实例简介-keras学习笔记四
官网实例详解-目录和实例简介-keras学习笔记四 2018-06-11 10:36:18 wyx100 阅读数 4193更多 分类专栏: 人工智能 python 深度学习 keras 版权声明: ...
- go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用)
目录 go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用) warden direct demo-server gr ...
- WPF-学习笔记 动态修改控件Margin的值
原文:WPF-学习笔记 动态修改控件Margin的值 举例说明:动态添加一个TextBox到Grid中,并设置它的Margin: TextBox text = new TextBox(); t_gri ...
- springmvc学习笔记(18)-json数据交互
springmvc学习笔记(18)-json数据交互 标签: springmvc springmvc学习笔记18-json数据交互 springmvc进行json交互 环境准备 加入json转换的依赖 ...
- 学习笔记:利用GDI+生成简单的验证码图片
学习笔记:利用GDI+生成简单的验证码图片 /// <summary> /// 单击图片时切换图片 /// </summary> /// <param name=&quo ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
随机推荐
- python爬取妹子图全站全部图片-可自行添加-线程-进程爬取,图片去重
from bs4 import BeautifulSoupimport sys,os,requests,pymongo,timefrom lxml import etreedef get_fenlei ...
- docker基础知识
兴起于2010年,2013年docker开源. 什么是docker? built ship run 官方定位: Docker is a world's leading software contain ...
- 1-10000以内的完数(js)
//1-10000以内的完数 //完数:因子之和相加等于这个数 //例如:6的因子为1,2,3:1+2+3=6 // 6 // 28 // 496 // 8128 let sum = 0, i, j; ...
- day09 并发编程
一. 目录 1.进程的概念和两种创建方式 2.多进程爬虫 3.守护进程 4.进程队列 5.进程队列简单应用(实现数据共享) 6.线程的两种创建方式 7.线程和进程的效率对比 8.线程共享统一进程的数据 ...
- jsp引入文件时候经常遇到的${ctx}
jsp引入文件时候经常遇到的${ctx} 在jsp页面中经常见到这样的代码: <script type="text/JavaScript" src="${ctx}/ ...
- IIS出现Server Error in '/' Application 错误的解决办法
C:\Windows\Temp"文件夹加上 IIS_IUSRS的权限
- SQL 语句外键 a foreign key constraint fails
queryRunner.update("SET FOREIGN_KEY_CHECKS = 0;"); queryRunner.update(sql, pid); queryRunn ...
- Nginx中虚拟主机配置
一.Nginx中虚拟主机配置 1.基于域名的虚拟主机配置 1.修改宿主机的hosts文件(系统盘/windows/system32/driver/etc/HOSTS) linux : vim /etc ...
- Scala(一)——基本类型
Scala语言快速入门(基本类型) (参考视频:av39126512,韩顺平281集scala精讲) 一.Linux和Windows环境安装 这部分跳过,直接使用IDEA进行搭建,和其他编程语言配置差 ...
- springboot中使用filter用配置类方式
在03-springboot-web的Filter包下,创建HeFilter类 代码示例: package com.bjpowernode.springboot.filter; import java ...