import os import random import skimage.data import skimage.transform import matplotlib import matplotlib.pyplot as plt import numpy as np import tensorflow as tf
# Allow image embeding in notebook %matplotlib inline
defload_data(data_dir): """Loads a data set and returns two lists: images: a list of Numpy arrays, each representing an image. labels: a list of numbers that represent the images labels. """ # Get all subdirectories of data_dir. Each represents a label. directories = [d for d in os.listdir(data_dir) if os.path.isdir(os.path.join(data_dir, d))] # Loop through the label directories and collect the data in # two lists, labels and images. labels = [] images = [] for d in directories: label_dir = os.path.join(data_dir, d) file_names = [os.path.join(label_dir, f) for f in os.listdir(label_dir) if f.endswith(".ppm")] # For each label, load it's images and add them to the images list. # And add the label number (i.e. directory name) to the labels list. for f in file_names: images.append(skimage.data.imread(f)) labels.append(int(d)) return images, labels
# Load training and testing datasets. ROOT_PATH = "" train_data_dir = os.path.join(ROOT_PATH, "datasets/BelgiumTS/Training") test_data_dir = os.path.join(ROOT_PATH, "datasets/BelgiumTS/Testing")
defdisplay_images_and_labels(images, labels): """Display the first image of each label.""" unique_labels = set(labels) # set:不重复出现 plt.figure(figsize=(15, 15)) # figure size i = 1 for label in unique_labels: # Pick the first image for each label. image = images[labels.index(label)] #每个label在整个labels表中的位置 # str1.index(str2, beg=0, end=len(string)) str2在str1中的索引值 # print(labels.index(label)) plt.subplot(8, 8, i) # A grid of 8 rows x 8 columns plt.axis('off') plt.title("Label {0} ({1})".format(label, labels.count(label)))# label,totalnum i += 1 _ = plt.imshow(image) plt.show()
defdisplay_label_images(images, label): """Display images of a specific label.""" limit = 24# show a max of 24 images plt.figure(figsize=(15, 5)) i = 1
start = labels.index(label) end = start + labels.count(label)# count:统计字符串里某个字符出现的次数 for image in images[start:end][:limit]: plt.subplot(3, 8, i) # 3 rows, 8 per row plt.axis('off') i += 1 plt.imshow(image) plt.show()
# Create a graph to hold the model. graph = tf.Graph()
# Create model in the graph. with graph.as_default(): # Placeholders for inputs and labels. images_ph = tf.placeholder(tf.float32, [None, 32, 32, 3]) labels_ph = tf.placeholder(tf.int32, [None])
# Convert logits to label indexes (int). # Shape [None], which is a 1D vector of length == batch_size. predicted_labels = tf.argmax(logits, 1)
# Define the loss function. 【损失函数】 # Cross-entropy is a good choice for classification. 交叉熵 loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels_ph))
# Create training op. 【梯度下降算法】 train = tf.train.GradientDescentOptimizer(learning_rate=0.08).minimize(loss)
# And, finally, an initialization op to execute before training. # TODO: rename to tf.global_variables_initializer() on TF 0.12. init = tf.global_variables_initializer()
# 随机从训练集中选取10张图片 sample_indexes = random.sample(range(len(images32)), 10) sample_images = [images32[i] for i in sample_indexes] sample_labels = [labels[i] for i in sample_indexes]
# Run the "predicted_labels" op. predicted = session.run([predicted_labels], feed_dict={images_ph: sample_images})[0] print(sample_labels)#样本标签 print(predicted)#预测值
# Display the predictions and the ground truth visually. fig = plt.figure(figsize=(10, 10)) for i inrange(len(sample_images)): truth = sample_labels[i] prediction = predicted[i] plt.subplot(5, 2,1+i) plt.axis('off') color='green'if truth == prediction else'red' plt.text(40, 10, "Truth: {0}\nPrediction: {1}".format(truth, prediction), fontsize=12, color=color) plt.imshow(sample_images[i])
# 转换图片尺寸 test_images32 = [skimage.transform.resize(image, (32, 32)) for image in test_images] #显示转换后的图片 display_images_and_labels(test_images32, test_labels)
1 2 3 4 5 6 7 8
# Run predictions against the full test set. predicted = session.run([predicted_labels], feed_dict={images_ph: test_images32})[0] # 计算准确度 match_count = sum([int(y == y_) for y, y_ inzip(test_labels, predicted)]) accuracy = match_count / len(test_labels) # 输出测试集上的准确度 print("Accuracy: {:.3f}".format(accuracy))
1
Accuracy: 0.534
1 2
# Close the session. This will destroy the trained model. session.close()