生成Tensorflow的圆形图像预处理

时间:2017-08-10 03:35:24

标签: python tensorflow data-processing

我正在编写一个机器学习程序来识别黑白图像中的圆心。图像生成器脚本在这里:

from __future__ import division
from __future__ import print_function
import numpy as np
from PIL import Image
from random import randint


for n in range(0,400):
#import time
#date_string = time.strftime("%Y-%m-%d-%H:%M:%S")
#Initialize the matrix- Size (100,100)
size = 100
arr = np.zeros((size,size))

#Initialize the Gaussian Properties

x0 = randint(1,100); y0 = randint(1,100); sigmax = randint(1,10); 
sigmay = randint(1,10)

center = (x0,y0)
print (center)
#Create the Gaussian Function

def Gaussian(x,y):
    result = int(round( 255*np.exp(-(x - x0)**2 / (2 * sigmax**2)) * 
    np.exp( -(y - y0)**2 / (2 *sigmay**2))))
    return result

for i in range(size):
    for j in range(size):
        arr[i][j] = Gaussian(i,j)

im = Image.fromarray(arr)
if im.mode !='RGB':
    im = im.convert('RGB')
    #im.show()
    im.save("/home/garrett/train/"+str(n)+".jpeg", "JPEG")

此脚本输出这样的图像为黑白圆圈,给出中心的标签输出到文本文件。我正在使用https://github.com/tensorflow/models/blob/master/inception/inception/data/build_imagenet_data.py处找到的脚本 作为黑盒子来处理我的图像数据以供Tensorflow使用。但是,当我运行此命令时:

 python build_image_data.py --train_directory=./train --
output_directory=./  --validation_directory=./validate --
labels_file=mylabels.txt   --train_shards=1 --validation_shards=1 --
num_threads=1

我收到以下错误消息:

Saving results to ./
Determining list of input files and labels from ./validate.
Traceback (most recent call last):
  File "build_image_data.py", line 435, in <module>
    tf.app.run()
  File "/home/garrett/anaconda3/lib/python3.6/site-
packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "build_image_data.py", line 429, in main
    FLAGS.validation_shards, FLAGS.labels_file)
  File "build_image_data.py", line 415, in _process_dataset
    filenames, texts, labels = _find_image_files(directory, 
labels_file)
  File "build_image_data.py", line 379, in _find_image_files
    matching_files = tf.gfile.Glob(jpeg_file_path)
  File "/home/garrett/anaconda3/lib/python3.6/site-
packages/tensorflow/python/lib/io/file_io.py", line 332, in 
get_matching_files
    for single_filename in filename
  File "/home/garrett/anaconda3/lib/python3.6/contextlib.py", line 89, 
in __exit__
    next(self.gen)
  File "/home/garrett/anaconda3/lib/python3.6/site-
packages/tensorflow/python/framework/errors_impl.py", line 466, in 
raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.NotFoundError: ./validate/(33, 
23)

这是正确的方法吗?如果是这样,我做错了什么?如果没有,格式化我的数据以与Tensorflow一起使用的正确方法是什么?如果它有帮助,我计划使用卷积神经网络来识别中心。这可能有点过头了,但在我完成类似的,更复杂的任务之前,它更适合练习。

谢谢,任何建议都表示赞赏。

1 个答案:

答案 0 :(得分:0)

https://github.com/tensorflow/models/blob/master/inception/inception/data/build_imagenet_data.py脚本需要一个非常特定的目录结构,其中包含不同目录中的训练和验证图像。您看到的错误似乎是您没有&#34;验证&#34; 。目录

你应该看看https://www.tensorflow.org/api_guides/python/image,了解如何加载jpeg图像的例子