tensorflow1.x代码转换到tensorflow2.x

这里我考虑使用PASCAL VOC 2007和PASCAL VOC 2012数据集来示意,这两个数据集的一些介绍可以参见如下博客的介绍

计算机视觉标准数据集整理—PASCAL VOC数据集_xingwei_09的博客-CSDN博客_pascal数据集下载

这里我给出两个数据集的链接路径

VOC 2007

链接:https://pan.baidu.com/s/1xK3gmKMpK1CzgxeyEuVYiw 
提取码:21ww 

VOC 2012

链接:https://pan.baidu.com/s/1GK7o5Xu3X6JihJzKqA9FCw 
提取码:0obj 

这两个数据集在这篇博客中暂且用不到,下面要记录的是Anaconda3中tensorflow 2.3的配置过程(之前一直用的是tensorflow 1.x系列搭配keras,考虑到跟上节奏,还是配置下tensorflow 2.x的环境吧)

一. Anaconda3下配置tensorflow 2.3.1

1. 我安装的是Anaconda3-5.2.0-Windows-x86_64.exe,对应python版本是3.6.5版本

2. 在Anaconda Prompt环境里直接pip安装

pip install tensorflow

如果下载速度很慢,可以更改下载源,参见我的博客

等待一段后出现报错Found existing installation: wrapt 1.10.11. ERROR: Cannot uninstall 'wrapt'. It is a distutils installed project and thus we cannot accurately determine which files belong...

此时可以手动将Anaconda3下的site-packages中含有tensorflow或者tensorboard前缀的文件夹手动删除,我的路径如下:

删除完毕后可以执行如下命令:

pip install -U --ignore-installed wrapt enum34 simplejson netaddr

完毕后再执行pip install tensorflow

3 安装成功后,可以import下tensorflow

import tensorflow as tf
tf.__version__

执行可以看到我安装的tensorflow版本

注: 在实验过程中,我有出现过could not load dynamic library 'cudart64_101.dll': dlerror: cudart64_101.dll not found报错

这里其实是cuda版本和cudnn版本和tensorflow2.3.1不搭配,当前我的cuda版本是我的博客里的配置,得更改为cuda10.1,cudnn7.6配置,这里上传下我的cuda和cudnn文件

cudnn-10.1-windows10-x64-v7.6.5.32.zip

链接:https://pan.baidu.com/s/1_VFpx8Idl0ghXdmIShg8lg 
提取码:87vc 

cuda_10.1.105_418.96_win10.exe

链接:https://pan.baidu.com/s/1NKwE0yMmfNIzXoaZHf6QLw 
提取码:66gp 
 

二. tensorflow 2.3.1更新tensorflow 1.10版本的代码

1. 如下是当时用tensorflow1.10创建的代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 在sess之前,计算图需要构建完成,才能对变量进行正确的初始化。详细的知识感觉自己也不是很懂。
# 。。。反正就记住了,在使用Sess启动计算图之前,一定要构建完整的计算图,不能在会话里面
# 再补充计算图。
import tensorflow as tf
import numpy as np
def f1(xx):
    w1=tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1),name="w1")
    return tf.matmul(xx, w1)

def f2(aa):
    w2=tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1),name="w2")
    return tf.matmul(aa, w2)


x=tf.placeholder(tf.float32,shape=[None,2],name="x")
#y=tf.placeholder(tf.float32,shape=[None,1],name="y")
a=f1(x)
y=f2(a)
init=tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    tmp=np.array([[0.7,0.9]])
    print(sess.run(y,feed_dict={x:tmp}))
    tf.identity(y,"y")
    writer = tf.summary.FileWriter('D://tensorflow-log//test_tensorboard3', tf.get_default_graph())
    writer.close()

出现报错AttributeError: module 'tensorflow' has no attribute 'placeholder'

可以将import tensorflow as tf更改为

import tensorflow.compat.v1 as tf
tf.disable_eager_execution()

即如下代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 在sess之前,计算图需要构建完成,才能对变量进行正确的初始化。详细的知识感觉自己也不是很懂。
# 。。。反正就记住了,在使用Sess启动计算图之前,一定要构建完整的计算图,不能在会话里面
# 再补充计算图。
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
#import tensorflow as tf
import numpy as np
def f1(xx):
    w1=tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1),name="w1")
    return tf.matmul(xx, w1)

def f2(aa):
    w2=tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1),name="w2")
    return tf.matmul(aa, w2)


x=tf.placeholder(tf.float32,shape=[None,2],name="x")
#y=tf.placeholder(tf.float32,shape=[None,1],name="y")
a=f1(x)
y=f2(a)
init=tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    tmp=np.array([[0.7,0.9]])
    print(sess.run(y,feed_dict={x:tmp}))
    tf.identity(y,"y")
    writer = tf.summary.FileWriter('D://tensorflow-log//test_tensorboard3', tf.get_default_graph())
    writer.close()

运行成功。

2. 如下代码是当时用tensorflow1.10搭配keras2.1.2所写的代码,通过加载一个已经训练好的模型权重参数来预测mnist数据集

所用权重参数h5文件见如下链接:

链接:https://pan.baidu.com/s/1eBu5aISDnH_-BS6AOnJR_A 
提取码:2mk1 
 

所用mnist文件夹链接如下:

链接:https://pan.baidu.com/s/191IQx9OT1ddLLxMV5riVcA 
提取码:cl3e 

# -*- coding: UTF-8 -*-

# mnist神经网络训练,采用LeNet-5模型

import os
import cv2
import numpy as np
import pydot
import graphviz
import struct

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.advanced_activations import PReLU
from keras.optimizers import SGD, Adadelta, Adagrad

from keras.utils import np_utils
from keras.utils import plot_model

import h5py
from keras.models import model_from_json
from keras.models import load_model
import matplotlib.pyplot as plt
import pickle as p
import matplotlib.image as plimg
from PIL import Image
import tensorflow as tf
import keras.backend as K
def load_mnist(path, kind='train'):
    """Load MNIST data from `path`"""
    labels_path = os.path.join(path,
                               '%s-labels.idx1-ubyte'
                               % kind)
    images_path = os.path.join(path,
                               '%s-images.idx3-ubyte'
                               % kind)

    print("labels_path: ",labels_path)
    print("images_path: ", images_path)
    with open(labels_path, 'rb') as lbpath:
        magic, n = struct.unpack('>II',
                                 lbpath.read(8))
        labels = np.fromfile(lbpath,
                             dtype=np.uint8)

    with open(images_path, 'rb') as imgpath:
        magic, num, rows, cols = struct.unpack('>IIII',
                                               imgpath.read(16))
        images = np.fromfile(imgpath,
                             dtype=np.uint8).reshape(len(labels), 784)

    return images, labels
    
# 建立一个Sequential模型
model = Sequential()

# model.add(Conv2D(4, 5, 5, border_mode='valid',input_shape=(28,28,1)))
# 第一个卷积层,4个卷积核,每个卷积核5*5,卷积后24*24,第一个卷积核要申明input_shape(通道,大小) ,激活函数采用“tanh”
model.add(Conv2D(filters=4, kernel_size=(5, 5), padding='valid', input_shape=(28, 28, 1), activation='tanh'))

# model.add(Conv2D(8, 3, 3, subsample=(2,2), border_mode='valid'))
# 第二个卷积层,8个卷积核,不需要申明上一个卷积留下来的特征map,会自动识别,下采样层为2*2,卷完且采样后是11*11
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(filters=8, kernel_size=(3, 3), padding='valid', activation='tanh'))
# model.add(Activation('tanh'))

# model.add(Conv2D(16, 3, 3, subsample=(2,2), border_mode='valid'))
# 第三个卷积层,16个卷积核,下采样层为2*2,卷完采样后是4*4
model.add(Conv2D(filters=16, kernel_size=(3, 3), padding='valid', activation='tanh'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# model.add(Activation('tanh'))

model.add(Flatten())
# 把多维的模型压平为一维的,用在卷积层到全连接层的过度

# model.add(Dense(128, input_dim=(16*4*4), init='normal'))
# 全连接层,首层的需要指定输入维度16*4*4,128是输出维度,默认放第一位
model.add(Dense(128, activation='tanh'))

# model.add(Activation('tanh'))

# model.add(Dense(10, input_dim= 128, init='normal'))
# 第二层全连接层,其实不需要指定输入维度,输出为10维,因为是10类
model.add(Dense(10, activation='softmax'))
# model.add(Activation('softmax'))
# 激活函数“softmax”,用于分类

# 训练CNN模型

sgd = SGD(lr=0.05, momentum=0.9, decay=1e-6, nesterov=True)
# 采用随机梯度下降法,学习率初始值0.05,动量参数为0.9,学习率衰减值为1e-6,确定使用Nesterov动量
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
# 配置模型学习过程,目标函数为categorical_crossentropy:亦称作多类的对数损失,注意使用该目标函数时,需要将标签转化为形如(nb_samples, nb_classes)的二值序列,第18行已转化,优化器为sgd
model.load_weights("CNN.h5")

path = ".\\mnist"
X_train, y_train = load_mnist(path, kind='train')

path = ".\\mnist"
X_test, y_test = load_mnist(path, kind='t10k')

print("X_train: ",X_train.T.shape, X_train.dtype)
print('y_train: ',y_train.T.shape, y_train.dtype)
print("X_test: ",X_test.shape, X_test.dtype)
print("y_test: ",y_test.shape, y_test.dtype)

number_index = 69
x_test1 = X_test[number_index,:]
print(x_test1.shape)

x_test1 = x_test1.reshape(28,28)
print(x_test1.shape)

plt.subplot(1,1,1)
plt.imshow(x_test1, cmap='gray', interpolation='none')

x_test1 = x_test1[np.newaxis, ..., np.newaxis]
x_test1_pred = model.predict(x_test1, batch_size=1, verbose=1)
print("x_test1_pred: ",np.argmax(x_test1_pred))

x_test1_actual = y_test[number_index]
print("x_test1_actual: ", x_test1_actual)

writer = tf.summary.FileWriter('logs/', tf.get_default_graph())
writer.close()
K.clear_session()

会报ModuleNotFoundError: No module named 'keras'错误,需要将from keras.model import方式改为from tensorflow.keras.models import方式,更改之后见如下代码:

# -*- coding: UTF-8 -*-

# mnist神经网络训练,采用LeNet-5模型

import os
import cv2
import numpy as np
import pydot
import graphviz
import struct
import h5py

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, Activation, PReLU
from tensorflow.keras.optimizers import SGD, Adadelta, Adagrad
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import model_from_json, load_model
from tensorflow.keras import backend as K

import matplotlib.pyplot as plt
import pickle as p
import matplotlib.image as plimg
from PIL import Image
import tensorflow as tf

def load_mnist(path, kind='train'):
    """Load MNIST data from `path`"""
    labels_path = os.path.join(path,
                               '%s-labels.idx1-ubyte'
                               % kind)
    images_path = os.path.join(path,
                               '%s-images.idx3-ubyte'
                               % kind)

    print("labels_path: ",labels_path)
    print("images_path: ", images_path)
    with open(labels_path, 'rb') as lbpath:
        magic, n = struct.unpack('>II',
                                 lbpath.read(8))
        labels = np.fromfile(lbpath,
                             dtype=np.uint8)

    with open(images_path, 'rb') as imgpath:
        magic, num, rows, cols = struct.unpack('>IIII',
                                               imgpath.read(16))
        images = np.fromfile(imgpath,
                             dtype=np.uint8).reshape(len(labels), 784)

    return images, labels


# 创建日志
logdir = 'D://tensorflow-v2-log//test_tensorboard'
writer = tf.summary.create_file_writer(logdir)

#开启autograph跟踪
tf.summary.trace_on(graph=True, profiler=True)

# 建立一个Sequential模型
model = Sequential()

# model.add(Conv2D(4, 5, 5, border_mode='valid',input_shape=(28,28,1)))
# 第一个卷积层,4个卷积核,每个卷积核5*5,卷积后24*24,第一个卷积核要申明input_shape(通道,大小) ,激活函数采用“tanh”
model.add(Conv2D(filters=4, kernel_size=(5, 5), padding='valid', input_shape=(28, 28, 1), activation='tanh'))

# model.add(Conv2D(8, 3, 3, subsample=(2,2), border_mode='valid'))
# 第二个卷积层,8个卷积核,不需要申明上一个卷积留下来的特征map,会自动识别,下采样层为2*2,卷完且采样后是11*11
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(filters=8, kernel_size=(3, 3), padding='valid', activation='tanh'))
# model.add(Activation('tanh'))

# model.add(Conv2D(16, 3, 3, subsample=(2,2), border_mode='valid'))
# 第三个卷积层,16个卷积核,下采样层为2*2,卷完采样后是4*4
model.add(Conv2D(filters=16, kernel_size=(3, 3), padding='valid', activation='tanh'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# model.add(Activation('tanh'))

model.add(Flatten())
# 把多维的模型压平为一维的,用在卷积层到全连接层的过度

# model.add(Dense(128, input_dim=(16*4*4), init='normal'))
# 全连接层,首层的需要指定输入维度16*4*4,128是输出维度,默认放第一位
model.add(Dense(128, activation='tanh'))

# model.add(Activation('tanh'))

# model.add(Dense(10, input_dim= 128, init='normal'))
# 第二层全连接层,其实不需要指定输入维度,输出为10维,因为是10类
model.add(Dense(10, activation='softmax'))
# model.add(Activation('softmax'))
# 激活函数“softmax”,用于分类

# 训练CNN模型

sgd = SGD(lr=0.05, momentum=0.9, decay=1e-6, nesterov=True)
# 采用随机梯度下降法,学习率初始值0.05,动量参数为0.9,学习率衰减值为1e-6,确定使用Nesterov动量
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
# 配置模型学习过程,目标函数为categorical_crossentropy:亦称作多类的对数损失,注意使用该目标函数时,需要将标签转化为形如(nb_samples, nb_classes)的二值序列,第18行已转化,优化器为sgd
model.load_weights("CNN.h5")

path = ".\\mnist"
X_train, y_train = load_mnist(path, kind='train')

path = ".\\mnist"
X_test, y_test = load_mnist(path, kind='t10k')

print("X_train: ",X_train.T.shape, X_train.dtype)
print('y_train: ',y_train.T.shape, y_train.dtype)
print("X_test: ",X_test.shape, X_test.dtype)
print("y_test: ",y_test.shape, y_test.dtype)

number_index = 69
x_test1 = X_test[number_index,:]
print(x_test1.shape)

x_test1 = x_test1.reshape(28,28)
print(x_test1.shape)

plt.subplot(1,1,1)
plt.imshow(x_test1, cmap='gray', interpolation='none')
plt.show()
cv2.imshow("img1",x_test1)
cv2.waitKey()

x_test1 = x_test1[np.newaxis, ..., np.newaxis]
x_test1_pred = model.predict(x_test1, batch_size=1, verbose=1)
print("x_test1_pred: ",np.argmax(x_test1_pred))

x_test1_actual = y_test[number_index]
print("x_test1_actual: ", x_test1_actual)

#将计算图信息写入日志
with writer.as_default():
    tf.summary.trace_export(
        name="autograph",
        step=0,
        profiler_outdir=logdir)

可以看到保存计算图这块语句也做了修改,如果不更改还是使用tf.summary.FileWriter()函数,则会出现如下报错

AttributeError: module 'tensorboard.summary._tf.summary' has no attribute 'FileWriter'

如上代码执行完毕后,在D:\tensorflow-v2-log\test_tensorboard路径下便可以看到tensorboard日志

在D盘下可以新建一个cmd文件,文件里内容如下:

tensorboard --logdir=D://tensorflow-v2-log --host=127.0.0.1

运行该cmd文件,

可在网页中输入如下地址:http://localhost:6006,可看到tensorboard可视化的一些内容

接下来的博客内容应该更贴近实战性,理论性的知识想待实战性的写完后再深入探讨,比较浅显的整理见我前面的博客系列,其实只能算归纳吧

既然要做平台,就要有一个界面来提供标注,训练模型,然后考虑如何把一些传统算法的预处理和后处理功能集成到里面去,同时也要考虑怎么去更好的把平台和项目应用相结合。下面一篇博客只是一个小插曲,只是展示下可以用c#来做界面,重点说明如何在c#中结合python来做开发:底层使用python来搭建神经网络训练和预测,上层界面使用c#界面来做开发。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1.项目代码功能经验证ok,确保稳定可靠运行。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通。 2.主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈! 【资源说明】 基于TensorFlow深度学习框架实现遥感影像语义分割python源码+项目说明(毕设项目) 数据 * 训练数据集包括803张卫星图片,RGB格式,尺寸2448 * 2448 * 图像分辨率为50cm,由 DigitalGlobe's 卫星提供 * 可通过下载页点击"Starting Kit"下载数据。 标注 * 每张卫星图片有一张与之对应的标注图片。这张标注图片也是RGB格式,一共分为7类,每类对应的图像(R,G,B)编码对应关系如下: * 城市土地: 0,255,255,浅蓝色,人造建筑(可以忽略道路) * 农业用地: 255,255,0,黄色,农田,任何计划中(定期)的种植、农田、果园、葡萄园、苗圃、观赏性园艺以及养殖区 * 牧场: 255,0,255,紫色,除了森林,农田之外的绿色土地,草地 * 森林:0,255,0,绿色,任何土地上有x%的树冠密度。 * 水系:0,0,255,深蓝色,江河湖海湿地 * 荒地:255,255,255, 白色,山地,沙漠,戈壁,沙滩,没有植被的地方 * 未知土地: 0,0,0,黑色,云层遮盖或其他因素 * 卫星图片和与其对应的标注图片的命名格式为 id_sat.jpg和id_mask.png,id 是一个随机的整数。 * 需要注意: * 由于压缩,标注图像的值可能不是准确的目标颜色值。当转换到标签时,请将每个R/G/B通道按128阈值二值化。 * 高分辨率卫星图像的土地覆被分割仍然是一个探索性的任务,由于标注多类分割的代价很大,标签还远远不够完善。 评价指标 * 我们将采用像素级别的平均IoU分数作为评价指标。 * IoU的定义是:交集/并集,公式: 预测准确的面积/(预测准确面积 + 没有预测出来的面积 + 预测错误的面积) * 平均IoU由各个类别的IoU取均值得到 * 需要注意的是"未知土地"(0,0,0)并不是一个真实的类别,在这个计算中也没有起到作用。所以有效的mIoU是前6个类别IoU的均值。 文件结构组织 * deepglobe_land * dataset * land_train (存放下载下来的原始数据 ) * onechannel_label (内部数据通过运行 rgb2label.py 生成) * voc_train_all.record (通过运行 create_tf_record_all.py 生成 ) * ini_checkpoints * resnet_v2_101 (resnet_v2_101.ckpt and train.graph) * utils (工具包) * rgb2label.py * create_tf_record_all.py * deeplab_model.py ### 代码执行顺序与注意事项

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

竹叶青lvye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值