Image dataset creation and reading of tensorflow

  TensorFlow is Google's open source deep learning framework. The identification of similar things can be generated through a feed of a large amount of labeled data. Of course, if the amount of data is small, it can only be used as a classifier.

   TensorFlow was also introduced to me by a bear brother. At that time, we agreed to do this together, but in the end, I was the only one left to explore (people with wives, um~ It’s still a pit)

   

# -*- coding: utf-8 -*-
import them
import tensorflow as tf
from PIL import Image
import numpy as np

class_path="/home/images/"
writer = tf.python_io.TFRecordWriter("anm_pic_train.tfrecords")

count = 0
Dec={}

def sort(key):
    global count
    if key in dic:
        return dic[key]
    else:
        dic[key]=count
        count = count +1
        return count-1

def text2vec(text):
    result = np.zeros(3, dtype=np.int)
    if text < 10 :
        result[0]=1
    if (text >= 10) and (text < 30):
        result[1]=1
    if text >= 30:
        result[2]=1
    return result

for file in os.listdir(class_path):
    if file.endswith(".jpg"):
        file_path = class_path + file
#       print(file_path)
        img = Image.open(file_path)
        # wide
        if img.mode == 'RGB':
            # handle the size of the image
            img = img.resize((240, 320))
            img_raw = img.tobytes()
            example = tf.train.Example(features=tf.train.Features(feature={
                #image corresponds to a single result
                "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[sort(file[:file.rindex("_")])])),
                # Images correspond to multiple results
                #"label": tf.train.Feature(int64_list=tf.train.Int64List(value=text2vec(xx))),
                'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
            }))
            writer.write(example.SerializeToString())
writer.close()
print('dic:',dic,'  length:', len(dic))


# read tf
def read_and_decode(filename):
    filename_queue = tf.train.string_input_producer([filename])
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(serialized_example,
       features={
           # The label return of a single result is int
           'label': tf.FixedLenFeature([], tf.int64),
           # Array return, [3] The length of the input array is the same
           # 'label': tf.FixedLenFeature([3], tf.int64),
           'img_raw' : tf.FixedLenFeature([], tf.string),
       })
    img = tf.decode_raw(features['img_raw'], tf.uint8)
    img = tf.reshape(img, [240, 320, 3])
    # normalize
    img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
    label = features['label']
    return img, label

img, label = read_and_decode("anm_pic_train.tfrecords")
img_batch, label_batch = tf.train.shuffle_batch([img, label],batch_size=10, capacity=2000, min_after_dequeue=1000)
init = tf.global_variables_initializer()

with tf.Session() as sess:

    sess.run(init)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)
    for i in range(10):
        val, l = sess.run([img_batch, label_batch])
        print(l)
    print ("complete ...")
    coord.request_stop()
    coord.join(threads)
    sess.close()

 

   The above is the code for tensorflow local image data set and queue reading. Among them, the RGB picture is one of the deep pits (the codes of many articles on the Internet are the same, but I get an error when I use it) , when a large number of pictures are downloaded, the bit depth of some pictures is different, such as 24, 32, etc., among which 24 rgb.

   In tf.reshape, a 3-channel image is specified, that is, a 24-bit deep image. If it is a 32-bit deep image, an error will be reported here, but the pitfall is that it will not report an error when making a dataset wink .

 

 

Result:

 Image storage directory:



 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326596836&siteId=291194637