结果:
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 254, 254, 64) 1792 _________________________________________________________________ conv2d_1 (Conv2D) (None, 252, 252, 64) 36928 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 126, 126, 64) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 124, 124, 128) 73856 _________________________________________________________________ conv2d_3 (Conv2D) (None, 122, 122, 128) 147584 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 61, 61, 128) 0 _________________________________________________________________ conv2d_4 (Conv2D) (None, 59, 59, 256) 295168 _________________________________________________________________ conv2d_5 (Conv2D) (None, 57, 57, 256) 590080 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 28, 28, 256) 0 _________________________________________________________________ conv2d_6 (Conv2D) (None, 26, 26, 512) 1180160 _________________________________________________________________ max_pooling2d_3 (MaxPooling2 (None, 13, 13, 512) 0 _________________________________________________________________ conv2d_7 (Conv2D) (None, 11, 11, 512) 2359808 _________________________________________________________________ max_pooling2d_4 (MaxPooling2 (None, 5, 5, 512) 0 _________________________________________________________________ conv2d_8 (Conv2D) (None, 3, 3, 1024) 4719616 _________________________________________________________________ global_average_pooling2d (Gl (None, 1024) 0 _________________________________________________________________ dense (Dense) (None, 1024) 1049600 _________________________________________________________________ dense_1 (Dense) (None, 256) 262400 _________________________________________________________________ dense_2 (Dense) (None, 1) 257 ================================================================= Total params: 10,717,249 Trainable params: 10,717,249 Non-trainable params: 0 _________________________________________________________________
4.2 训练
steps_per_eooch = train_count//batch_size validation_steps = test_count//batch_size history = model.fit(train_ds,epochs=30,steps_per_epoch=steps_per_eooch,validation_data=test_ds,validation_steps=validation_steps)
Epoch 1/30 35/35 [==============================] - 20s 565ms/step - loss: 0.8902 - acc: 0.5688 - val_loss: 0.4821 - val_acc: 0.8672 Epoch 2/30 35/35 [==============================] - 19s 556ms/step - loss: 0.7571 - acc: 0.6170 - val_loss: 0.6877 - val_acc: 0.5078 Epoch 3/30 35/35 [==============================] - 19s 556ms/step - loss: 0.6371 - acc: 0.6232 - val_loss: 0.4861 - val_acc: 0.8008 Epoch 4/30 35/35 [==============================] - 19s 555ms/step - loss: 0.4127 - acc: 0.8554 - val_loss: 0.2898 - val_acc: 0.9062 Epoch 5/30 35/35 [==============================] - 19s 557ms/step - loss: 0.4168 - acc: 0.7688 - val_loss: 0.4776 - val_acc: 0.5000 Epoch 6/30 35/35 [==============================] - 19s 555ms/step - loss: 0.4127 - acc: 0.7080 - val_loss: 0.2026 - val_acc: 0.9297 Epoch 7/30 35/35 [==============================] - 19s 556ms/step - loss: 0.2303 - acc: 0.9384 - val_loss: 0.1515 - val_acc: 0.9453 Epoch 8/30 35/35 [==============================] - 19s 556ms/step - loss: 0.1769 - acc: 0.9491 - val_loss: 0.1918 - val_acc: 0.9531 Epoch 9/30 35/35 [==============================] - 19s 556ms/step - loss: 0.1526 - acc: 0.9518 - val_loss: 0.0907 - val_acc: 0.9727 Epoch 10/30 35/35 [==============================] - 19s 556ms/step - loss: 0.1172 - acc: 0.9625 - val_loss: 0.0790 - val_acc: 0.9766 Epoch 11/30 35/35 [==============================] - 19s 556ms/step - loss: 0.1337 - acc: 0.9482 - val_loss: 0.0888 - val_acc: 0.9805 Epoch 12/30 35/35 [==============================] - 19s 556ms/step - loss: 0.1312 - acc: 0.9536 - val_loss: 0.1095 - val_acc: 0.9805 Epoch 13/30 35/35 [==============================] - 19s 555ms/step - loss: 0.4718 - acc: 0.9027 - val_loss: 0.2007 - val_acc: 0.9141 Epoch 14/30 35/35 [==============================] - 19s 554ms/step - loss: 0.1906 - acc: 0.9321 - val_loss: 0.1523 - val_acc: 0.9609 Epoch 15/30 35/35 [==============================] - 19s 554ms/step - loss: 0.1567 - acc: 0.9554 - val_loss: 0.0998 - val_acc: 0.9727 Epoch 16/30 35/35 [==============================] - 19s 555ms/step - loss: 0.1333 - acc: 0.9589 - val_loss: 0.1101 - val_acc: 0.9805 Epoch 17/30 35/35 [==============================] - 19s 554ms/step - loss: 0.1245 - acc: 0.9679 - val_loss: 0.0773 - val_acc: 0.9844 Epoch 18/30 35/35 [==============================] - 19s 554ms/step - loss: 0.1157 - acc: 0.9652 - val_loss: 0.0978 - val_acc: 0.9805 Epoch 19/30 35/35 [==============================] - 19s 553ms/step - loss: 0.1237 - acc: 0.9688 - val_loss: 0.0766 - val_acc: 0.9766 Epoch 20/30 35/35 [==============================] - 19s 554ms/step - loss: 0.1069 - acc: 0.9670 - val_loss: 0.0850 - val_acc: 0.9805 Epoch 21/30 35/35 [==============================] - 19s 554ms/step - loss: 0.1234 - acc: 0.9696 - val_loss: 0.0670 - val_acc: 0.9805 Epoch 22/30 35/35 [==============================] - 19s 553ms/step - loss: 0.0945 - acc: 0.9741 - val_loss: 0.0665 - val_acc: 0.9805 Epoch 23/30 35/35 [==============================] - 19s 553ms/step - loss: 0.1293 - acc: 0.9679 - val_loss: 0.0733 - val_acc: 0.9805 Epoch 24/30 35/35 [==============================] - 19s 553ms/step - loss: 0.1314 - acc: 0.9607 - val_loss: 0.0785 - val_acc: 0.9805 Epoch 25/30 35/35 [==============================] - 19s 554ms/step - loss: 0.1082 - acc: 0.9661 - val_loss: 0.0637 - val_acc: 0.9844 Epoch 26/30 35/35 [==============================] - 19s 554ms/step - loss: 0.1139 - acc: 0.9714 - val_loss: 0.0671 - val_acc: 0.9805 Epoch 27/30 35/35 [==============================] - 19s 553ms/step - loss: 0.1266 - acc: 0.9652 - val_loss: 0.0688 - val_acc: 0.9766 Epoch 28/30 35/35 [==============================] - 19s 553ms/step - loss: 0.0986 - acc: 0.9696 - val_loss: 0.0668 - val_acc: 0.9844 Epoch 29/30 35/35 [==============================] - 19s 553ms/step - loss: 0.0882 - acc: 0.9723 - val_loss: 0.0513 - val_acc: 0.9805 Epoch 30/30 35/35 [==============================] - 19s 554ms/step - loss: 0.0832 - acc: 0.9777 - val_loss: 0.0423 - val_acc: 0.9883
5. 结果
这一部分我们将展示测试集合验证集的准确度和损失的变化趋势’
history.history.keys() plt.plot(history.epoch, history.history.get('acc'), label='acc') plt.plot(history.epoch, history.history.get('val_acc'), label='val_acc') plt.legend() plt.show()
plt.plot(history.epoch, history.history.get('loss'), label='loss') plt.plot(history.epoch, history.history.get('val_loss'), label='val_loss') plt.legend()