网络结构:
测试代码(tensorflow-2.3):
from tensorflow.keras import layers, models, Model import tensorflow as tf # 定义网络输入 input_tensor = layers.Input(shape=(227,227,3)) # print(x) # => Tensor("input_1:0", shape=(None, 227, 227, 3), dtype=float32) # conv1 -> f=96, k=11*11, s=4, p="same" ->55,55,96 x = layers.Conv2D(96, 11, 4, padding="valid", activation="relu")(input_tensor) # print(x) # => Tensor("conv2d/Relu:0", shape=(None, 55, 55, 96), dtype=float32) # maxpooling -> k=3*3,s=2 -> 27,27,96 x = layers.MaxPool2D(3, 2, padding="valid")(x) # print(x) # => Tensor("max_pooling2d/MaxPool:0", shape=(None, 27, 27, 96), dtype=float32) # conv2 -> f=256, k=5*5, s=1, p="valid" -> 27,27,256 x = layers.Conv2D(256, 5, 1, "same", activation="relu")(x) # print(x) # => Tensor("conv2d_1/Relu:0", shape=(None, 27, 27, 256), dtype=float32) # maxpooling -> k=3, s=2, -> 13,13,256 x = layers.MaxPool2D(3, 2)(x) # print(x) # => Tensor("max_pooling2d_1/MaxPool:0", shape=(None, 13, 13, 256), dtype=float32) # conv3 -> f=384,k=3,s=1,same, -> 13,13,384 x = layers.Conv2D(384, 3, 1, "same", activation="relu")(x) # print(x) # => Tensor("conv2d_2/Relu:0", shape=(None, 13, 13, 384), dtype=float32) # conv4 -> f=384,k=3,s=1,same,->13,13,384 x = layers.Conv2D(384, 3, 1, "same", activation="relu")(x) # print(x) # => Tensor("conv2d_3/Relu:0", shape=(None, 13, 13, 384), dtype=float32) # conv5 -> f=256,k=3,s=1,"same", -> 13,13,256 x = layers.Conv2D(256, 3, 1, "same", activation="relu")(x) # print(x) # => Tensor("conv2d_4/Relu:0", shape=(None, 13, 13, 256), dtype=float32) # maxpooling -> k=3,s=2,-> 6,6,256 x = layers.MaxPool2D(3, 2)(x) # print(x) # => Tensor("max_pooling2d_2/MaxPool:0", shape=(None, 6, 6, 256), dtype=float32) # Flatten -> 9216 x = layers.Flatten()(x) # print(x) # => Tensor("flatten/Reshape:0", shape=(None, 9216), dtype=float32) # dence -> 4096 x = layers.Dense(4096, activation="relu")(x) # print(x) # => Tensor("dense/Relu:0", shape=(None, 4096), dtype=float32) # dense -> 4096 x = layers.Dense(4096, activation="relu")(x) # print(x) # => Tensor("dense_1/Relu:0", shape=(None, 4096), dtype=float32) # dense -> 10 output_tensor = layers.Dense(10, activation="softmax")(x) # print(output) # => Tensor("dense_2/Softmax:0", shape=(None, 10), dtype=float32) net = models.Model(inputs = input_tensor, outputs = output_tensor) print(net.summary()) """ Model: "functional_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) [(None, 227, 227, 3)] 0 _________________________________________________________________ conv2d (Conv2D) (None, 55, 55, 96) 34944 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 27, 27, 96) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 27, 27, 256) 614656 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 13, 13, 256) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 13, 13, 384) 885120 _________________________________________________________________ conv2d_3 (Conv2D) (None, 13, 13, 384) 1327488 _________________________________________________________________ conv2d_4 (Conv2D) (None, 13, 13, 256) 884992 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 6, 6, 256) 0 _________________________________________________________________ flatten (Flatten) (None, 9216) 0 _________________________________________________________________ dense (Dense) (None, 4096) 37752832 _________________________________________________________________ dense_1 (Dense) (None, 4096) 16781312 _________________________________________________________________ dense_2 (Dense) (None, 10) 40970 ================================================================= Total params: 58,322,314 Trainable params: 58,322,314 Non-trainable params: 0 _________________________________________________________________ None """ # 模拟生成4维张量 train_tensor = tf.random.normal(shape=(4,227,227,3)) train_out = net(train_tensor) print(train_out) """ tf.Tensor( [[0.09585018 0.10201856 0.10650603 0.08999295 0.0997481 0.10598496 0.09056135 0.09493181 0.10683279 0.1075733 ] [0.09847332 0.10025442 0.1024049 0.08964493 0.09974851 0.10610957 0.09039888 0.09572605 0.1074772 0.10976227] [0.09842361 0.10047951 0.10344481 0.08977058 0.09837217 0.10560185 0.09168874 0.09464937 0.10835848 0.10921095] [0.09636429 0.09776594 0.10355475 0.08937449 0.09913716 0.10745402 0.09196407 0.09745194 0.10796721 0.10896615]], shape=(4, 10), dtype=float32) """ # 模拟生成标签 label_tensor = tf.constant([1,2,3,4]) # 设置模型的优化器,Loss计算方式, 计算方式 net.compile(optimizer="adam", loss="SparseCategoricalCrossentropy", metrics='sparse_categorical_accuracy') net.fit(train_tensor, label_tensor, epochs=10, batch_size=1) """ Epoch 1/10 4/4 [==============================] - 1s 169ms/step - loss: 73.0120 - sparse_categorical_accuracy: 0.0000e+00 Epoch 2/10 4/4 [==============================] - 1s 162ms/step - loss: 3.2802 - sparse_categorical_accuracy: 0.0000e+00 Epoch 3/10 4/4 [==============================] - 1s 159ms/step - loss: 2.0551 - sparse_categorical_accuracy: 0.0000e+00 Epoch 4/10 4/4 [==============================] - 1s 162ms/step - loss: 2.0035 - sparse_categorical_accuracy: 0.0000e+00 Epoch 5/10 4/4 [==============================] - 1s 164ms/step - loss: 1.6734 - sparse_categorical_accuracy: 0.2500 Epoch 6/10 4/4 [==============================] - 1s 160ms/step - loss: 1.6491 - sparse_categorical_accuracy: 0.2500 Epoch 7/10 4/4 [==============================] - 1s 160ms/step - loss: 1.5298 - sparse_categorical_accuracy: 0.2500 Epoch 8/10 4/4 [==============================] - 1s 165ms/step - loss: 1.5773 - sparse_categorical_accuracy: 0.0000e+00 Epoch 9/10 4/4 [==============================] - 1s 164ms/step - loss: 1.4837 - sparse_categorical_accuracy: 0.2500 Epoch 10/10 4/4 [==============================] - 1s 159ms/step - loss: 1.4955 - sparse_categorical_accuracy: 0.0000e+00 """ # 模拟预测数据 test_tensor = tf.random.normal(shape=(1,227,227,3)) # 得到预测结果 predict = net.predict(test_tensor) # 打印结果 print(predict) """ [[0.00692442 0.25775012 0.25910458 0.16272165 0.27780315 0.00778223 0.00717733 0.00689636 0.00719397 0.00664626]] """ # 打印对应的预测最大概率的Index print(tf.math.argmax(predict, axis=1)) """ tf.Tensor([4], shape=(1,), dtype=int64) """ # 保存模型或者保存weights net.save("./net.h5")