学习自中国大学MOOC TensorFlow学习课程
import csv
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
#from google.colab import files
一个2828像素(784维)的向量,由一个矩形编码的灰度图2828像素水平方向首尾连接拉伸维一个长向量转化而来,所以有784列
所以是784里
#uploaded = files.upload()
def get_data(filename):
# You will need to write code that will read the file passed
# into this function. The first line contains the column headers
# so you should ignore it
# Each successive line contians 785 comma separated values between 0 and 255
# The first value is the label
# The rest are the pixel values for that picture
# The function will return 2 np.array types. One with all the labels
# One with all the images
#
# Tips:
# If you read a full line (as 'row') then row[0] has the label
# and row[1:785] has the 784 pixel values
# Take a look at np.array_split to turn the 784 pixels into 28x28
# You are reading in strings, but need the values to be floats
# Check out np.array().astype for a conversion
with open(filename) as training_file:
csv_reader = csv.reader(training_file, delimiter=',')
first_line = True
temp_images = []
temp_labels = []
for row in csv_reader:
if first_line:
print("Ignoring first line")
first_line = False
else:
temp_labels.append(row[0])
image_data = row[1:785]
image_data_as_array = np.array_split(image_data, 28) #turn the 784 pixels into 28x28
temp_images.append(image_data_as_array)
images = np.array(temp_images).astype('float')
labels = np.array(temp_labels).astype('float')
return images, labels
training_images, training_labels = get_data('sign_mnist_train.csv')
testing_images, testing_labels = get_data('sign_mnist_test.csv')
# Keep these
print(training_images.shape)
print(training_labels.shape)
print(testing_images.shape)
print(testing_labels.shape)
Ignoring first line
Ignoring first line
(27455, 28, 28)
(27455,)
(7172, 28, 28)
(7172,)
# In this section you will have to add another dimension to the data
# So, for example, if your array is (10000, 28, 28)
# You will need to make it (10000, 28, 28, 1)
# Hint: np.expand_dims
training_images = np.expand_dims(training_images, axis=3)
testing_images = np.expand_dims(testing_images, axis=3)
# Create an ImageDataGenerator and do Image Augmentation
# 数据增强
train_datagen = ImageDataGenerator(
rescale=1. / 255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest') #最近邻算法填补移动后图像的空白
validation_datagen = ImageDataGenerator(
rescale=1. / 255)
# Keep These
print(training_images.shape)
print(testing_images.shape)
(27455, 28, 28, 1)
(7172, 28, 28, 1)
# Define the model
# Use no more than 2 Conv2D and 2 MaxPooling2D
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64, (3, 3), activation='relu', input_shape=(28, 28, 1)), #28*28像素灰度图(1通道)
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'), #28*28像素灰度图(1通道)
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(26, activation=tf.nn.softmax)]) #分26类 ,softmax概率化,让所有输出加起来等于1
# Compile Model.
# Before modification
# model.compile(optimizer = tf.train.AdamOptimizer(),
# loss = 'sparse_categorical_crossentropy',
# metrics=['accuracy'])
#
# After modification
model.compile(optimizer=tf.optimizers.Adam(),
loss = 'sparse_categorical_crossentropy', #多分类问题,使用稀疏交叉熵
metrics = 'accuracy')
# Train the Model
history = model.fit_generator(train_datagen.flow(training_images, training_labels, batch_size = 32), #小批量处理,每批32个样本
#每个epoch处理的批次数(步长)为样本总数除以每批的批次数
steps_per_epoch = len(training_images) / 32,
epochs = 15,
validation_data = validation_datagen.flow(testing_images, testing_labels, batch_size = 32),
validation_steps = len(testing_images) / 32)
#测试样本来评估准确度
print("测试样本来评估准确度")
model.evaluate(testing_images, testing_labels) #诡异。。。
D:\anaconda\envs\TF2_4\lib\site-packages\tensorflow\python\keras\engine\training.py:1844: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
warnings.warn('`Model.fit_generator` is deprecated and '
Epoch 1/15
857/857 [==============================] - 13s 14ms/step - loss: 3.0379 - accuracy: 0.0941 - val_loss: 1.8248 - val_accuracy: 0.4314
Epoch 2/15
857/857 [==============================] - 14s 16ms/step - loss: 2.1553 - accuracy: 0.3154 - val_loss: 1.5972 - val_accuracy: 0.4877
Epoch 3/15
857/857 [==============================] - 12s 14ms/step - loss: 1.7471 - accuracy: 0.4380 - val_loss: 1.0424 - val_accuracy: 0.6439
Epoch 4/15
857/857 [==============================] - 12s 14ms/step - loss: 1.4753 - accuracy: 0.5178 - val_loss: 0.8661 - val_accuracy: 0.7040
Epoch 5/15
857/857 [==============================] - 13s 15ms/step - loss: 1.3026 - accuracy: 0.5751 - val_loss: 0.8586 - val_accuracy: 0.6887
Epoch 6/15
857/857 [==============================] - 13s 15ms/step - loss: 1.1988 - accuracy: 0.6107 - val_loss: 0.8323 - val_accuracy: 0.6912
Epoch 7/15
857/857 [==============================] - 13s 15ms/step - loss: 1.1008 - accuracy: 0.6358 - val_loss: 0.6382 - val_accuracy: 0.7863
Epoch 8/15
857/857 [==============================] - 13s 15ms/step - loss: 1.0156 - accuracy: 0.6607 - val_loss: 0.6549 - val_accuracy: 0.7731
Epoch 9/15
857/857 [==============================] - 13s 16ms/step - loss: 0.9297 - accuracy: 0.6916 - val_loss: 0.5476 - val_accuracy: 0.7968
Epoch 10/15
857/857 [==============================] - 14s 16ms/step - loss: 0.8900 - accuracy: 0.7066 - val_loss: 0.7116 - val_accuracy: 0.7524
Epoch 11/15
857/857 [==============================] - 13s 15ms/step - loss: 0.8351 - accuracy: 0.7218 - val_loss: 0.4836 - val_accuracy: 0.8165
Epoch 12/15
857/857 [==============================] - 14s 17ms/step - loss: 0.7879 - accuracy: 0.7369 - val_loss: 0.4462 - val_accuracy: 0.8489
Epoch 13/15
857/857 [==============================] - 14s 17ms/step - loss: 0.7512 - accuracy: 0.7510 - val_loss: 0.4098 - val_accuracy: 0.8450
Epoch 14/15
857/857 [==============================] - 17s 19ms/step - loss: 0.7164 - accuracy: 0.7583 - val_loss: 0.3492 - val_accuracy: 0.8765
Epoch 15/15
857/857 [==============================] - 14s 16ms/step - loss: 0.6825 - accuracy: 0.7697 - val_loss: 0.4251 - val_accuracy: 0.8487
测试样本来评估准确度
225/225 [==============================] - 2s 6ms/step - loss: 456.2860 - accuracy: 0.4756
[456.2859802246094, 0.4755995571613312]
# Plot the chart for accuracy and loss on both training and validation
import matplotlib.pyplot as plt
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'r', label='Training Loss')
plt.plot(epochs, val_loss, 'b', label='Validation Loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
因为训练数据少,所以会出现这种测试集性能更好的情况:
output_6_0.png
output_6_1.png
#释放资源
import os, signal
#在Windows中,signal()只能叫SIGABRT, SIGFPE,SIGILL,SIGINT,SIGSEGV,或 SIGTERM。ValueError在其他情况下,将引发A。
#os.kill(os.getpid(), signal.SIGKILL)
os.kill(os.getpid(), signal.SIGINT)














网友评论