import os
import tensorflow as tf
from PIL import Image
# 源数据地址
cwd = 'C:\\Users\\xiaodeng\\Desktop\\UCMerced_LandUse\\Images'
# 生成record路径及文件名
train_record_path = r"C:\\Users\州谈\xiaodeng\\Desktop\\train.tfrecords"
test_record_path = r"C:\\Users\\xiaodeng\\Desktop\\test.tfrecords"
# 分类
classes = {'agricultural','airplane','baseballdiamond',
'beach','buildings','chaparral','denseresidential',
'forest','freeway','golfcourse','harbor',
'intersection','mediumresidential','mobilehomepark','overpass',
'parkinglot','river','runway','sparseresidential','storagetanks','tenniscourt'}
def _byteslist(value):
"""二进制属性"""
return tf.train.Feature(bytes_list = tf.train.BytesList(value = [value]))
def _int64list(value):
"""整数属性"""
return tf.train.Feature(int64_list = tf.train.Int64List(value = [value]))
#def create_train_record(cwd,classes):
"""创建训练集tfrecord"""册灶碰
writer = tf.python_io.TFRecordWriter(train_record_path) # 创建一个writer
NUM = 1 # 显示创建过程(计数)
for index, name in enumerate(classes):
class_path = cwd + "/" + name + '/'
l = int(len(os.listdir(class_path)) * 0.7) # 取前70%创建训练集
for img_name in os.listdir(class_path)[:l]:
img_path = class_path + img_name
img = Image.open(img_path)
img = img.resize((256, 256))# resize图片大小
img_raw = img.tobytes() # 将图片转化为原生bytes
example = tf.train.Example( # 封装到Example中
features=tf.train.Features(feature={
"label":_int64list(index), # label必须为整数类型属性
'img_raw':_byteslist(img_raw) # 图辩旅片必须为二进制属性
}))
writer.write(example.SerializeToString())
print('Creating train record in ',NUM)
NUM += 1
writer.close() # 关闭writer
print("Create train_record successful!")
#def create_test_record(cwd,classes):
"""创建测试tfrecord"""
writer = tf.python_io.TFRecordWriter(test_record_path)
NUM = 1
for index, name in enumerate(classes):
class_path = cwd + '/' + name + '/'
l = int(len(os.listdir(class_path)) * 0.7)
for img_name in os.listdir(class_path)[l:]: # 剩余30%作为测试集
img_path = class_path + img_name
img = Image.open(img_path)
img = img.resize((256, 256))
img_raw = img.tobytes() # 将图片转化为原生bytes
# print(index,img_raw)
example = tf.train.Example(
features=tf.train.Features(feature={
"label":_int64list(index),
'img_raw':_byteslist(img_raw)
}))
writer.write(example.SerializeToString())
print('Creating test record in ',NUM)
NUM += 1
writer.close()
基本胡肢禅使用使用 TensorFlow, 你必须明白 TensorFlow:
使用图 (graph) 来表示计算任务。
在被称之为 会话 (Session) 的上下文 (context) 中执行图。
使用 tensor 表示数据。
通过 变量 (Variable) 维护状态。
使用 feed 和 fetch 可以为任意的 *** 作(arbitrary operation) 赋值或者裤尘从其中获取数据。
综述
TensorFlow 是一个编程系统, 使用图来表示计算任务。 图中的节点被称之为 op
(operation 的缩写)。 一个 op 获得 0 个或多个 Tensor, 执行计算,
产生 0 个或多个 Tensor. 每个 Tensor 是一个类型化的多维数组。
例如, 你可以将一小组图像集表示为一个四维浮点数数组,
这四个维度分别是 [batch, height, width, channels].
一个 TensorFlow 图描述了计算的过程。 为了进行计算, 图必须在 会话 里被启动。
会话 将图的 op 分发到诸如 CPU 或 GPU 之类的 设备 上, 同时提供执行 op 的方法。
这些方法执行后, 将产生的 tensor 返回。 在 Python 语言中, 返回的 tensor 是
numpy ndarray 对象; 在 C 和 C++ 语言中, 返回的 tensor 是
tensorflow::Tensor 实例。
计算图
TensorFlow 程序通常被组织成一个构建阶段和一个执行阶段。 在构建阶段, op 的执行步骤
被描述成一个图。 在执行阶段, 使用会话执行执行图中的 op.
例如, 通常在构建阶段创建一个图来表示和训练神经网络, 然后在执行阶段反复执行图中的训练 op.
TensorFlow 支持 C, C++, Python 编程语言。 目前, TensorFlow 的 Python 库更加易用,
它提供了大量的辅助函数来简化构建图的工作, 这些函数尚未被 C 和 C++ 库支持。
三种语言的会话库 (session libraries) 是一致的。
构建图
构建图的第一步, 是创建源 op (source op)。 源 op 不需要任何输入, 例如 常量 (Constant)。 源 op 的输出被传递给其它 op 做运饥睁算。
Python 库中, op 构造器的返回值代表被构造出的 op 的输出, 这些返回值可以传递给其它
op 构造器作为输入。
TensorFlow Python 库有一个默认图 (default graph), op 构造器可以为其增加节点。 这个默认图对
许多程序来说已经足够用了。 阅读 Graph 类 文档
陶涛
学号:19131213373
【嵌牛导读】tensor flow是用来实现mnist手写数字识别的一个库,但是运行时也会存在各种问题,在这里,为大家分享一下自己在学习过程中遇到孝散族的问题以及解决办法。
【巧弊嵌牛鼻子】python,tensor flow, warning
【嵌牛正文】
tensor flow是用来实现mnist手写数字识别的一个库,但是运行时也会存在各种问题,在这里,为大掘庆家分享一下自己在学习过程中遇到的问题以及解决办法。
跑tensorflow时报下面的warning:
2019-11-22 11:00:31.504004: W tensorflow/core/platform/cpu_feature_guard.cc:45]The TensorFlow library wasn't compiled to use SSE4.2 instructions,but these are available on your machine and could speed up CPU computations.
2019-11-22 11:00:31.504312: W tensorflow/core/platform/cpu_feature_guard.cc:45]The TensorFlow library wasn't compiled to use AVX instructions,but these are available on your machine and could speed up CPU computations.
2019-11-22 11:00:31.504325: W tensorflow/core/platform/cpu_feature_guard.cc:45]The TensorFlow library wasn't compiled to use AVX2 instructions,but these are available on your machine and could speed up CPU computations.
2019-11-22 11:00:31.504335: W tensorflow/core/platform/cpu_feature_guard.cc:45]The TensorFlow library wasn't compiled to use FMA instructions,but these are available on your machine and could speed up CPU computations.
解决办法:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)