代码取自于 https://github.com/jakeret/tf_unet
TensorFlow Unet文档 https://tf-unet.readthedocs.io/en/latest/installation.html
TensorFlow Unet安装
确保Tensorflow已安装,如果没有,请参考Tensorflow安装说明 link
- 克隆github工程
git clone https://github.com/jakeret/tf_unet.git
- 安装package
$ cd tf_unet
$ pip install -r requirements.txt
-
$ python setup.py install --user
Package使用
在其他工程中使用Tensorflow Unet的一个例子
from tf_unet import unet, util, image_util
#preparing data loading
data_provider = image_util.ImageDataProvider("fishes/train/*.tif")
#setup & training
net = unet.Unet(layers=3, features_root=64, channels=1, n_class=2)
trainer = unet.Trainer(net)
path = trainer.train(data_provider, output_path, training_iters=32, epochs=100)
#verification
...
prediction = net.predict(path, data)
unet.error_rate(prediction, util.crop_to_shape(label, prediction.shape))
img = util.combine_img_prediction(data, label, prediction)
util.save_image(img, "prediction.jpg")
可以利用Tensorboard跟踪学习的进度。tf_unet输出相关指标信息。
tf_unet Package的几个模块(链接有api和source,不再赘述)
工程自带示例程序(都已经亲测)
都是Jupyter notebooks,方便学习
建议使用1.5.0以上版本tensorflow,本人在测试这个代码的时候遇到报错AttributeError: 'module' object has no attribute 'softmax_cross_entropy_with_logits_v2'
,查了之后,发现1.4版本以下没有这个function,如果没有条件安装1.5.0以上,可git checkout 0.1.0切换至0.1.0版本
from __future__ import division, print_function
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
plt