|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | + |
| 4 | +@description: |
| 5 | +""" |
| 6 | +from __future__ import absolute_import, division, print_function |
| 7 | + |
| 8 | +import matplotlib.pyplot as plt |
| 9 | +import numpy as np |
| 10 | +import pandas as pd |
| 11 | +import tensorflow as tf |
| 12 | +from tensorflow import keras |
| 13 | +from tensorflow.keras import layers |
| 14 | + |
| 15 | +print(tf.__version__) |
| 16 | + |
| 17 | +(train_images, train_labels), (test_images, test_labels) = keras.datasets.fashion_mnist.load_data() |
| 18 | +a = pd.DataFrame(train_labels) |
| 19 | +print(a) |
| 20 | +b = a[0].value_counts() |
| 21 | +print(b) |
| 22 | + |
| 23 | +class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', |
| 24 | + 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] |
| 25 | +# 2.探索数据 |
| 26 | +print(train_images.shape) |
| 27 | +print(train_labels.shape) |
| 28 | +print(test_images.shape) |
| 29 | +print(test_labels.shape) |
| 30 | +# 3.处理数据 |
| 31 | +plt.figure() |
| 32 | +plt.imshow(train_images[0]) |
| 33 | +plt.colorbar() |
| 34 | +plt.grid(False) |
| 35 | +plt.show() |
| 36 | +plt.savefig("0.png") |
| 37 | + |
| 38 | +train_images = train_images / 255.0 |
| 39 | +test_images = test_images / 255.0 |
| 40 | +plt.figure(figsize=(10, 10)) |
| 41 | +for i in range(25): |
| 42 | + plt.subplot(5, 5, i + 1) |
| 43 | + plt.xticks([]) |
| 44 | + plt.yticks([]) |
| 45 | + plt.grid(False) |
| 46 | + plt.imshow(train_images[i], cmap=plt.cm.binary) |
| 47 | + plt.xlabel(class_names[train_labels[i]]) |
| 48 | +plt.show() |
| 49 | + |
| 50 | +# 4.构造网络 |
| 51 | +model = keras.Sequential( |
| 52 | + [ |
| 53 | + layers.Flatten(input_shape=[28, 28]), |
| 54 | + layers.Dense(128, activation='relu'), |
| 55 | + layers.Dense(10, activation='softmax') |
| 56 | + ]) |
| 57 | +model.compile(optimizer='adam', |
| 58 | + loss='sparse_categorical_crossentropy', |
| 59 | + metrics=['accuracy']) |
| 60 | +# 5.train |
| 61 | +model.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels)) |
| 62 | + |
| 63 | +# 6.save |
| 64 | +model_path = "image_model.h5" |
| 65 | +model.save(model_path) |
| 66 | +del model |
| 67 | + |
| 68 | +# 7.load model |
| 69 | +model = keras.models.load_model(model_path) |
| 70 | +predictions = model.predict(test_images) |
| 71 | +print(predictions[0]) |
| 72 | +print(np.argmax(predictions[0])) |
| 73 | +print(test_labels[0]) |
| 74 | + |
| 75 | + |
| 76 | +def plot_image(i, predictions_array, true_label, img): |
| 77 | + predictions_array, true_label, img = predictions_array[i], true_label[i], img[i] |
| 78 | + plt.grid(False) |
| 79 | + plt.xticks([]) |
| 80 | + plt.yticks([]) |
| 81 | + |
| 82 | + plt.imshow(img, cmap=plt.cm.binary) |
| 83 | + |
| 84 | + predicted_label = np.argmax(predictions_array) |
| 85 | + if predicted_label == true_label: |
| 86 | + color = 'blue' |
| 87 | + else: |
| 88 | + color = 'red' |
| 89 | + |
| 90 | + plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label], |
| 91 | + 100 * np.max(predictions_array), |
| 92 | + class_names[true_label]), |
| 93 | + color=color) |
| 94 | + |
| 95 | + |
| 96 | +def plot_value_array(i, predictions_array, true_label): |
| 97 | + predictions_array, true_label = predictions_array[i], true_label[i] |
| 98 | + plt.grid(False) |
| 99 | + plt.xticks([]) |
| 100 | + plt.yticks([]) |
| 101 | + thisplot = plt.bar(range(10), predictions_array, color="#777777") |
| 102 | + plt.ylim([0, 1]) |
| 103 | + predicted_label = np.argmax(predictions_array) |
| 104 | + |
| 105 | + thisplot[predicted_label].set_color('red') |
| 106 | + thisplot[true_label].set_color('blue') |
| 107 | + |
| 108 | + |
| 109 | +i = 0 |
| 110 | +plt.figure(figsize=(6, 3)) |
| 111 | +plt.subplot(1, 2, 1) |
| 112 | +plot_image(i, predictions, test_labels, test_images) |
| 113 | +plt.subplot(1, 2, 2) |
| 114 | +plot_value_array(i, predictions, test_labels) |
| 115 | +plt.show() |
| 116 | + |
| 117 | +# Plot the first X test images, their predicted label, and the true label |
| 118 | +# Color correct predictions in blue, incorrect predictions in red |
| 119 | +num_rows = 5 |
| 120 | +num_cols = 3 |
| 121 | +num_images = num_rows * num_cols |
| 122 | +plt.figure(figsize=(2 * 2 * num_cols, 2 * num_rows)) |
| 123 | +for i in range(num_images): |
| 124 | + plt.subplot(num_rows, 2 * num_cols, 2 * i + 1) |
| 125 | + plot_image(i, predictions, test_labels, test_images) |
| 126 | + plt.subplot(num_rows, 2 * num_cols, 2 * i + 2) |
| 127 | + plot_value_array(i, predictions, test_labels) |
| 128 | +plt.show() |
| 129 | + |
| 130 | +img = test_images[0] |
| 131 | + |
| 132 | +img = (np.expand_dims(img, 0)) |
| 133 | + |
| 134 | +print(img.shape) |
| 135 | +predictions_single = model.predict(img) |
| 136 | + |
| 137 | +print(predictions_single) |
| 138 | +plot_value_array(0, predictions_single, test_labels) |
| 139 | +_ = plt.xticks(range(10), class_names, rotation=45) |
0 commit comments