[tensorflow] 入门day1-数据整理与展示
tensorflow真是一个我绕不开的坑(苍天饶过谁.jpg)
其实tensorflow1和2的差别挺大的,暂时从1入坑,2的话之后简单过一下。
tf2中更改的函数(供参考):https://docs.google.com/spreadsheets/d/1FLFJLzg7WNP6JHODX5q8BDgptKafq_slHpnHVbJIteQ/edit#gid=0
本文仅记录我的踩坑历程。
参考文献:https://www.datacamp.com/community/tutorials/tensorflow-tutorial
数据来源:https://btsd.ethz.ch/shareddata/
基础知识部分另外编写,这里只记录操作和结果。
import skimage
import tensorflow as tf
from skimage import io # [MUST] for skimage.io.imread
import os
import matplotlib.pyplot as plt # draw distribution graph
from skimage import transform
from skimage.color import rgb2gray # convert img to grayscale
import numpy as np def first_try():
# initialize constant
x1 = tf.constant([1,2,3,4])
x2 = tf.constant([5,6,7,8])
# multiply
result = tf.multiply(x1, x2)
# only return a tensor, not real-value
# that means: tf does not calculate. only deprive a graph
print(result) # Tensor("Mul:0", shape=(4,), dtype=int32)
# run result and print. 'with' will close automatically
#sess = tf.Session()
#print(sess.run(result))
#sess.close()
with tf.Session() as sess:
output = sess.run(result)
print(output) def load_data(data_dir):
dirs = [d for d in os.listdir(data_dir)
if os.path.isdir(os.path.join(data_dir, d))]
labels = []
images = []
# each type of sign
for d in dirs:
# .ppm 's file name
label_dir = os.path.join(data_dir, d)
# real path of .ppm
file_names = [os.path.join(label_dir, f)
for f in os.listdir(label_dir)
if f.endswith(".ppm")]
for f in file_names:
# load image
images.append(skimage.io.imread(f))
labels.append(int(d))
return images, labels def random_show(images, name, cmap=None):
for i in range(len(name)):
plt.subplot(1, len(name), i+1)
plt.axis('off')
# add cmap for gray-scaled pic, which set cmap='gray'
# or u'll get wrong color
plt.imshow(images[name[i]], cmap)
plt.subplots_adjust(wspace=0.5)
print("shape: {0}, min: {1}, max: {2}".format(images[name[i]].shape,
images[name[i]].min(),
images[name[i]].max()))
plt.show() def show_each_label_pic(labels):
uniq_labels = set(labels)
# initialize the figure
plt.figure(figsize=(15, 15))
i = 1
for label in uniq_labels:
# pick the 1st image for each label
image = images[labels.index(label)]
# 8X8, ith
plt.subplot(8, 8, i)
plt.axis('off')
plt.title("Label {0} ({1})".format(label, labels.count(label)))
i += 1
plt.imshow(image) # plot single picture
plt.show() def transform_img(images, rows, cols):
return [transform.resize(image, (rows, cols)) for image in images] def to_gray(images):
# need array
return rgb2gray(np.array(images)) if __name__=="__main__":
ROOT_PATH = r"G:/share/testTF"
train_data_dir = ROOT_PATH + "/Training"
images, labels = load_data(train_data_dir)
#print(len(set(labels))) # 62. coz 62 type of traffic signs
#print(len(images)) # 4575
#plt.hist(labels, 63) # draw a bar-graph.
#plt.show()
#random_show(images, [300, 2250, 3650, 4000])
#print(type(images[0])) # <class 'numpy.ndarray'>
#show_each_label_pic(labels)
images28 = transform_img(images, 28, 28)
#random_show(images28, [300, 2250, 3650, 4000])
gray_images28 = to_gray(images28)
random_show(gray_images28, [300, 2250, 3650, 4000], cmap="gray")
图像:
条形图:

随机查看的四个图:

统计一下每个label有多少个图:



而且这个resize之后数据其实进行了归一化,进到(0,1)了

灰度图怎么样:这里转化成灰度图是因为作者说,当前问题中,颜色在分类时不起作用。这一点我随后会再验证。

[tensorflow] 入门day1-数据整理与展示的更多相关文章
- 转:TensorFlow入门(六) 双端 LSTM 实现序列标注(分词)
http://blog.csdn.net/Jerr__y/article/details/70471066 欢迎转载,但请务必注明原文出处及作者信息. @author: huangyongye @cr ...
- [译]TensorFlow入门
TensorFlow入门 张量(tensor) Tensorflow中的主要数据单元是张量(tensor), 一个张量包含了一组基本数据,可以是列多维数据.一个张量的"等级"(ra ...
- TensorFlow高效读取数据的方法——TFRecord的学习
关于TensorFlow读取数据,官网给出了三种方法: 供给数据(Feeding):在TensorFlow程序运行的每一步,让python代码来供给数据. 从文件读取数据:在TensorFlow图的起 ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)
续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...
- 一些用于数据整理的excel函数
我们经常要从外部数据源(如数据库.文本文件或网页等)将数据导入excel中,但是此类数据往往比较混乱,无法满足我们的要求,因此在进行数据分析之前,需要将这些数据进行整理清洗,excel由于将数据的管理 ...
- (转)TensorFlow 入门
TensorFlow 入门 本文转自:http://www.jianshu.com/p/6766fbcd43b9 字数3303 阅读904 评论3 喜欢5 CS224d-Day 2: 在 Da ...
- #tensorflow入门(1)
tensorflow入门(1) 关于 TensorFlow TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操 ...
- TensorFlow入门(五)多层 LSTM 通俗易懂版
欢迎转载,但请务必注明原文出处及作者信息. @author: huangyongye @creat_date: 2017-03-09 前言: 根据我本人学习 TensorFlow 实现 LSTM 的经 ...
- TensorFlow入门之MNIST最佳实践
在上一篇<TensorFlow入门之MNIST样例代码分析>中,我们讲解了如果来用一个三层全连接网络实现手写数字识别.但是在实际运用中我们需要更有效率,更加灵活的代码.在TensorFlo ...
随机推荐
- nodejs+supertest+mocha 接口测试环境搭建
系统接口自动化测试 该框架用于对系统的接口自动化测试(nodejs+supertest+mocha)Homebrew 安装: ruby -e "$(curl -fsSL {+}https:/ ...
- 2018-2019-2 20165234 《网络对抗技术》 Exp6 信息搜集与漏洞扫描
Exp6 信息搜集与漏洞扫描 实验内容 1. 各种搜索技巧的应用 2. DNS IP注册信息的查询 3. 基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务的查点(以自己主机为目标) 4 ...
- IDEA 同时打开多个项目
打开IDEA Settings-->System Settings-->Open project in new wodow
- 服务端 CORS 解决跨域
当协议.域名.端口中任一个不同时产生跨域 CORS 跨域资源共享(Cross-origin resource sharing) 参考资料https://developer.mozilla.org/zh ...
- android: View, SurfaceView, GLSurfaceView, TextureView 区别与联系
区别与联系 View: 显示视图,内置画布,提供了图形绘制函数.触屏事件.按键事件函数等,必须在UI主线程内更新画面,速度较慢: SurfaceView: 基于view视图进行拓展的视图类,更适合2D ...
- Android 定义和使用样式
如图,在stryle.xml中定义样式 然后可以在布局文件中使用样式
- [Java复习] 多线程 并发 JUC 补充
线程安全问题? 当多个线程共享同一个全局变量,做写的操作时,可能会受到其他线程的干扰.读不会发生线程安全问题. -- Java内存模型. 非静态同步方法使用什么锁? this锁 静态同步方法使用什么 ...
- ISO/IEC 9899:2011 条款6.2.8——对象的对齐
6.2.8 对象的对齐 1.完整的对象类型具有对齐要求,对齐要求是对该类型对象可以在哪个地址进行分配的放置限制.一个对齐是一个实现定义的整数值,表示一个给定对象可以分配在相继两个地址之间跨多少字节的位 ...
- PostgreSQL学习笔记——窗口函数
在学习窗口函数之前,我们新建一个Product表并往其中插入一些数据: drop table if exists Product; create table Product ( product_id ...
- 在node.js中使用Set
var set = new Set(); set.add(1); console.log("test1 : " + set.has(1) + " ; " + s ...