[Tensorflow] organize and display data entry day1-

I really tensorflow a not open around the pit (heaven who spared .jpg)

In fact tensorflow1 and 2 big difference, from a temporary into the pit after 1, 2, then it's too simple.

tf2 change of function (for reference): https://docs.google.com/spreadsheets/d/1FLFJLzg7WNP6JHODX5q8BDgptKafq_slHpnHVbJIteQ/edit#gid=0

This article documents only stepped on my pit course.

References: https://www.datacamp.com/community/tutorials/tensorflow-tutorial

Source: https://btsd.ethz.ch/shareddata/


Also part of the basics of writing, recording here only actions and results.

import skimage
import tensorflow as tf
from skimage import io # [MUST] for skimage.io.imread
import os
import matplotlib.pyplot as plt # draw distribution graph
from skimage import transform
from skimage.color import rgb2gray # convert img to grayscale
import numpy as np

def first_try():
    # initialize constant
    x1 = tf.constant([1,2,3,4])
    x2 = tf.constant([5,6,7,8])
    # multiply
    result = tf.multiply(x1, x2)
    # only return a tensor, not real-value
    # that means: tf does not calculate. only deprive a graph
    print(result) # Tensor("Mul:0", shape=(4,), dtype=int32)
    # run result and print. 'with' will close automatically
    #sess = tf.Session()
    #print(sess.run(result))
    #sess.close()
    with tf.Session() as sess:
        output = sess.run(result)
        print(output)

def load_data(data_dir):
    dirs = [d for d in os.listdir(data_dir)
            if os.path.isdir(os.path.join(data_dir, d))]
    labels = []
    images = []
    # each type of sign
    for d in dirs:
        # .ppm 's file name
        label_dir = os.path.join(data_dir, d)
        # real path of .ppm
        file_names = [os.path.join(label_dir, f)
                      for f in os.listdir(label_dir)
                      if f.endswith(".ppm")]
        for f in file_names:
            # load image
            images.append(skimage.io.imread(f))
            labels.append(int(d))
    return images, labels

def random_show(images, name, cmap=None):
    for i in range(len(name)):
        plt.subplot(1, len(name), i+1)
        plt.axis('off')
        # add cmap for gray-scaled pic, which set cmap='gray'
        # or u'll get wrong color
        plt.imshow(images[name[i]], cmap)
        plt.subplots_adjust(wspace=0.5)
        print("shape: {0}, min: {1}, max: {2}".format(images[name[i]].shape,
                                                      images[name[i]].min(),
                                                      images[name[i]].max()))
    plt.show()


def show_each_label_pic(labels):
    uniq_labels = set(labels)
    # initialize the figure
    plt.figure(figsize=(15, 15))
    i = 1
    for label in uniq_labels:
        # pick the 1st image for each label
        image = images[labels.index(label)]
        # 8X8, ith
        plt.subplot(8, 8, i)
        plt.axis ( ' off ' )
        plt.title("Label {0} ({1})".format(label, labels.count(label)))
        i += 1
        plt.imshow(image) # plot single picture
    plt.show()

def transform_img(images, rows, cols):
    return [transform.resize(image, (rows, cols)) for image in images]

def to_gray(images):
    # need array
    return rgb2gray(np.array(images))

if __name__=="__main__":
    ROOT_PATH = r"G:/share/testTF"
    train_data_dir = ROOT_PATH + "/Training"
    images, labels = load_data(train_data_dir)
    #print(len(set(labels))) # 62. coz 62 type of traffic signs
    #print(len(images)) # 4575
    #plt.hist(labels, 63) # draw a bar-graph.
    #plt.show()
    #random_show(images, [300, 2250, 3650, 4000])
    #print(type(images[0])) # <class 'numpy.ndarray'>
    #show_each_label_pic(labels)
    images28 = transform_img(images, 28, 28)
    #random_show(images28, [300, 2250, 3650, 4000])
    gray_images28 = to_gray(images28)
    random_show(gray_images28, [300, 2250, 3650, 4000], cmap="gray")

image:

Bar graph:

View of four random drawing:

Statistics about how many of each label map:

 

 

And in fact, after this resize the data were normalized into (0,1) of the

 

Grayscale how to: convert to greyscale here because the authors say, the current problem, the color does not work in the classification. Which I will then re-validation.

 

Guess you like

Origin www.cnblogs.com/pxy7896/p/11912538.html