Artificial Intelligence (AI) Library TensorFlow Trample Diary II

Last time One of the Trench Logs The legacy was finally resolved, so the author (that is, I) finally had the face to write a second article.

First, attach the sample code address of the convolution algorithm: https://github.com/tensorflow/models

This library contains code for common models implemented with tensorflow.Among them I'm using

The models/tree/master/tutorials/image/cifar10 example was outlined in the previous article.

On the last problem encountered:

Although I trained many times, every time I actually used it, the result was the same.The main reason for this problem is

In the core code file cifar10.py

tf.app.flags.DEFINE_integer('batch_size', 128,
                            """Number of images to process in a batch.""")

  

Changed by me to batch_size =1

At first I mistakenly thought this batch would correspond to the number of pictures in the.bin file of the training file, but it was not.This batch_size is meant to be used

cifar10_input.py
images, label_batch = tf.train.batch(
            [image, label],
            batch_size=batch_size,
            num_threads=num_preprocess_threads,
            capacity=min_queue_examples + 3 * batch_size)

 

Create a queue of 128 elements for pictures and labels to facilitate distributed processing.

Because the change to 1 may affect the training results.As a result, the overall loss is high, so the recognition rate is poor.Further verification is required.

 

Another reason is probably the deadliest

The way you used label in the last article is

# Create a queue that produces the filenames to read.
    filename_queue = tf.train.string_input_producer(filenames)

 

label also makes another string queue with string_input_producer

Because label is the classification name and the name of the folder in which the pictures are located, I drop the pictures folder names outside into a string queue of label and make a queue depuqeue inside.

This is a mistake because the two queues need to be perfectly consistent and cannot be added

This parameter, shuffle parameter, can randomly get picture files to train the model for more generalization ability.
# Create a queue that produces the filenames to read.
    filename_queue = tf.train.string_input_producer(filenames,name="filename_queue_hcq",shuffle=True)

shuffle=true or add.

You have to find another way to get label.

The cifar10_input.py method read_cifar10 is modified as follows:

def read_cifar10(filename_queue):
   class CIFAR10Record(object):
        pass

  
  reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)

    image0 = tf.image.decode_jpeg(value,3)

    esized_image = tf.image.resize_images(image0, [32, 32],
                                          method=tf.image.ResizeMethod.AREA)
    result = ImageNetRecord()
    re2=splitfilenames(tf.constant( filenames),len(filenames))
    key=splitfilenames(tf.reshape(key,[1],name="key_debug"),1)
    label=diff(re2,key)

 

The splitilenames method was added by me, mainly to cut the path out of the directory where the files are located

def splitfilenames(inputs,allstringlen):
    a = tf.string_split(inputs, "/\\")
    bigin = tf.cast(tf.size(a.values) / allstringlen -2, tf.int32)
    slitsinglelen = tf.cast(tf.size(a.values) / allstringlen, tf.int32)
    val = tf.reshape(a.values, [allstringlen, slitsinglelen])
    re2 = tf.cast(tf.slice(val, [0, bigin], [allstringlen, 1]),tf.string)
    re2 =tf.reshape(re2,[allstringlen])
    re2 =tf.unique(re2).y
    return re2

For example, "H:imagenetfortestn01440764" cut out "n01330764".This method supports batch processing.

Why writing is so troublesome.Because the input is tensor, all operations must be written according to the tensorflow api.

The diff method is used to determine the position of the key's classification name within all categories.Use this location as a label.

Readers may have a question here

"Why not use the category name'n01330764'as a label to train directly?"

There are also two limitations to the subsequent code, 1, the label must be of type int, and the 2label maximum cannot be greater than the total number of classifications.So you can't simply delete "n" and convert it to the number 1330764.

Although not very elegant, you officials tap.

def diff(re2,key):

    keys = tf.fill([tf.size(re2)],key[0])

    numpoi= tf.cast(tf.equal(re2, keys),tf.int32)
    numpoi=tf.argmax(numpoi)
    return numpoi

Okay, so far, the code for train is finished, so train for a while first.

This side of cifar10_eval.py needs to be changed.
if len(sys.argv)==1:
  SinglePicPath = "/tmp/8.jpg"
else:
  SinglePicPath =sys.argv[1]

Pass in the address of the single picture by a parameter to execute the recognizer.

Run 8.jpg first

The result is that 0 is so much because batchsize=128 is the same.

Then I wrote a page using C# MVC.Used to upload pictures and output Chinese results.

 

The main core code is:

 Thumbnails.Of(file).ZoomMethod(Thumbnails.ThumbnailZoomMethod.CUT).Resize(32, 32).ToFiles(imagepath);
            var p = new Process();
            //C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe
            p.StartInfo.FileName = @"C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe";
            p.StartInfo.Arguments = @"H:\tensorflow_cnn\tensorflow_cnn\tutorials\image\cifar10\cifar10_runsingle.py " + imagepath;
            p.StartInfo.WorkingDirectory = @"H:\";
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.UseShellExecute = false;
            p.Start();
            string output = p.StandardOutput.ReadToEnd();
            string errmsg = p.StandardError.ReadToEnd();
            p.WaitForExit();
            p.Close();
            var num = Regex.Replace(output, @".*\[array\(\[(\d+),.*\].*", "$1", RegexOptions.Singleline);
            string    result;

Mainly change the picture to 32*32 and use Process to pull up python to perform cifar10_runsingle.py (this is a modification of cifar10_eval.py).

Then cut the number of results out with the regular.

The rest is to replace locations like 0 with "n01330764"

"n01330764" replaced with Chinese, Previous There is a link to download Chinese.

Test it

------------------------------------------

--------------------------------------

 

-------------------

Because Eval was changed to execute code, the eval test set was not applied.Can't judge the recognition rate.This is the next place to be transformed.

However, imagenet is too large and is being trained once, which is estimated to take a long time.

It is not difficult to switch to GPU mode.There are tutorials.

Looking forward to the third of the Trench Logs, burn the year.

And a little bit about tensorboard, it's really awesome.Easy to install.Direct command line pip tensorboard

Then?

 

logdir pointing

 wt = tf.summary.FileWriter("/tmp/broaddata3")

The address of the summary set in the code.

Then open your browser to view it.

There's a pit here. Note that the tensorboard's runtime must match the log's runtime, otherwise it will always be prompted to not get the file.

Here you can see that I've added a new label_sum graph, and you can see that you do get a different label, starting at 0 at least.

This salute.

 

 

 

 

 



Keywords: Python github REST pip

Added by CraigRoberts on Sat, 08 Jun 2019 20:13:09 +0300