第五讲 卷积神经网络 LeNet5 cifar10

 1 import tensorflow as tf
 2 import os
 3 import numpy as np
 4 from matplotlib import pyplot as plt
 5 from tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, MaxPool2D, Dropout, Flatten, Dense
 6 from tensorflow.keras import Model
 7 
 8 
 9 np.set_printoptions(threshold=np.inf)
10 
11 cifar10 = tf.keras.datasets.cifar10
12 (x_train, y_train), (x_test, y_test) = cifar10.load_data()
13 x_train, x_test = x_train/25.0, x_test/255.0
14 
15 
16 class LeNet5(Model):
17   def __init__(self):
18     super(LeNet5, self).__init__()
19     self.c1 = Conv2D(filters=6, kernel_size=(5, 5), activation='sigmoid')
20     self.p1 = MaxPool2D(pool_size=(2, 2), strides=2)
21     self.c2 = Conv2D(filters=16, kernel_size=(5, 5), activation='sigmoid')
22     self.p2 = MaxPool2D(pool_size=(2, 2), strides=2)
23 
24     self.flatten = Flatten()
25     self.f1 = Dense(120, activation='sigmoid')
26     self.f2=Dense(84, activation='sigmoid')
27     self.f3=Dense(10, activation='softmax')
28 
29   def call(self, x):
30     x = self.c1(x)
31     x = self.p1(x)
32     x = self.c2(x)
33     x = self.p2(x)
34 
35     x = self.flatten(x)
36     x = self.f1(x)
37     x = self.f2(x)
38     y = self.f3(x)
39     return y
40 
41 
42 model = LeNet5()
43 
44 model.compile(optimizer='adam', 
45               loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
46               metrics = ['sparse_categorical_accuracy'])
47 
48 checkpoint_save_path = "./checkpoint/Baseline.ckpt"
49 if os.path.exists(checkpoint_save_path + ".index"):
50   print("--------------------load the model-----------------")
51   model.load_weights(checkpoint_save_path)
52 
53 cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path, save_weights_only=True, save_best_only=True)
54 
55 history = model.fit(x_train, y_train, batch_size=32, epochs=20, validation_data=(x_test, y_test), validation_freq=1, callbacks=[cp_callback])
56 
57 model.summary()
58 
59 
60 
61 with open('./weights.txt', 'w') as file:
62   for v in model.trainable_variables:
63     file.write(str(v.name) + '\n')
64     file.write(str(v.shape) + '\n')
65     file.write(str(v.numpy()) + '\n')
66 
67 
68 
69 def plot_acc_loss_curve(history):
70     # 显示训练集和验证集的acc和loss曲线
71     from matplotlib import pyplot as plt
72     acc = history.history['sparse_categorical_accuracy']
73     val_acc = history.history['val_sparse_categorical_accuracy']
74     loss = history.history['loss']
75     val_loss = history.history['val_loss']
76     
77     plt.figure(figsize=(15, 5))
78     plt.subplot(1, 2, 1)
79     plt.plot(acc, label='Training Accuracy')
80     plt.plot(val_acc, label='Validation Accuracy')
81     plt.title('Training and Validation Accuracy')
82     plt.legend()
83    #plt.grid()
84     
85     plt.subplot(1, 2, 2)
86     plt.plot(loss, label='Training Loss')
87     plt.plot(val_loss, label='Validation Loss')
88     plt.title('Training and Validation Loss')
89     plt.legend()
90     #plt.grid()
91     plt.show()
92 
93 plot_acc_loss_curve(history)
  
#以下代码来自 dive into DL TF2.0

1
import tensorflow as tf 2 print(tf.__version__) 3 4 5 net = tf.keras.models.Sequential([ 6 tf.keras.layers.Conv2D(filters=6, kernel_size=5, activation='sigmoid', input_shape=(28, 28, 1)), 7 tf.keras.layers.MaxPool2D(pool_size=2, strides=2), 8 tf.keras.layers.Conv2D(filters=16, kernel_size=5, activation='sigmoid'), 9 tf.keras.layers.MaxPool2D(pool_size=2, strides=2), 10 tf.keras.layers.Flatten(), 11 tf.keras.layers.Dense(120, activation='sigmoid'), 12 tf.keras.layers.Dense(84, activation='sigmoid'), 13 tf.keras.layers.Dense(10, activation='softmax') 14 ]) 15 16 17 X = tf.random.uniform((1, 28, 28, 1)) 18 for layer in net.layers: 19 X = layer(X) 20 print(layer.name, 'output shape\t', X.shape) 21 22 23 net.summary() 24 25 fashion_mnist = tf.keras.datasets.fashion_mnist 26 (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() 27 28 train_images = tf.reshape(train_images, (train_images.shape[0], train_images.shape[1], train_images.shape[2], 1)) 29 print(train_images.shape) 30 31 test_images = tf.reshape(test_images, (test_images.shape[0], test_images.shape[1], test_images.shape[2], 1)) 32 print(test_images.shape) 33 34 35 optimizer = tf.keras.optimizers.SGD(learning_rate=0.9, momentum=0.0, nesterov=False) 36 37 net.compile(optimizer = optimizer, 38 loss = 'sparse_categorical_crossentropy', 39 metrics = ['accuracy']) 40 41 42 net.fit(train_images, train_labels, epochs=5, validation_split=0.1) 43 44 net.evaluate(test_images, test_labels, verbose=2)

 

posted @ 2020-05-08 22:33  WWBlog  阅读(781)  评论(0编辑  收藏  举报