G.Jep
G.Jep

Reputation: 11

Generated circle image preprocessing for Tensorflow

I am writing a machine learning program to identify the centers of circles in black and white images. The image generator script is here:

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")

This script outputs an image like this a black and white circle, and the label giving the center is output to a text file. I am using the script found at https://github.com/tensorflow/models/blob/master/inception/inception/data/build_imagenet_data.py as a black box to process my image data for use with Tensorflow. However, when I run this command:

 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

I get the following error message:

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)

Is this the correct method? If so, what am I doing wrong? If not, what is the correct method for formatting my data for use with Tensorflow? If it helps, I plan to use a convolutional neural network to identify the centers. This is likely overkill, but it's more for practice before I complete a similar, more complicated task.

Thanks, and any advice is appreciated.

Upvotes: 1

Views: 733

Answers (1)

iga
iga

Reputation: 3633

The https://github.com/tensorflow/models/blob/master/inception/inception/data/build_imagenet_data.py script expects a very specific directory structure with training and validation images in different directories. The error you are seeing seems to be that you don't have the "validate" directory.

You should take a look here https://www.tensorflow.org/api_guides/python/image for examples of how to load jpeg images

Upvotes: 1

Related Questions