玩命加载中...
# 构造卷积网络进行形状识别
3个小节,预计用时**45分钟**。
卷积神经网络是解决图像相关的机器学习问题的常用模型。本教程基于SofaSofa数据练习赛形状识别2:方圆之外的数据集,讲解和展示了建立卷积神经网恋的步骤。
本教程基于**Python 3.7**与**TensorFlow**。
原创者:**[Zealing](http://sofasofa.io/user_profile.php?id=1001997)** | 修改校对:SofaSofa TeamM |
### 1. 加载数据
```python
# --- 参数设置 ---
# 手动查看测试集中的图像,并且增加了五个异形,放入训练集
num_anno=5
epochs=100
SEED=0
# 考虑到异性并不多,所以设置如下权重,来解决非平衡下的分类
class_weight={0:1.,1:1.,2:10.}
```
```python
# 加载训练集
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
train_data_raw = np.genfromtxt('train.csv', delimiter=',')
train_data=train_data_raw[1:,1:]
[n_train,d]=train_data.shape
y_train=train_data[:,d-1]
x_train=train_data[:,:-1]
[n_train,d]=x_train.shape
sz=40
x_train=x_train.reshape([n_train,sz,sz])
# 加载测试集
test_data_raw = np.genfromtxt('test.csv', delimiter=',')
test_data=test_data_raw[1:,:]
x_test=test_data[:,1:]
n_test=x_test.shape[0]
x_test=x_test.reshape([n_test,sz,sz])
```
## 2. 数据处理
数据处理中我们先对图像进行预处理,步骤包括
- 利用中值滤波(median filter)进行降噪
- 利用阈值分割法(threshold segmentation)生成掩膜(binary mask)
- 利用形态闭合(morphology closing)来填充图中的小洞
```python
from skimage.restoration import (denoise_tv_chambolle, denoise_bilateral,
denoise_wavelet, estimate_sigma)
from scipy import ndimage, misc
import statistics
import scipy
def my_preprocessing(I,show_fig=False):
I_median=ndimage.median_filter(I, size=5)
mask=(I_median 0.5).astype(int)
pred_prob_test = model.predict(x_test, batch_size=batch_size, verbose=1)
pred_test = np.array(pred_prob_test > 0.5).astype(int)
y_test_hat=pred_test[:,1]+pred_test[:,2]*2;
y_train_hat=pred_train[:,1]+pred_train[:,2]*2;
return y_train_hat,y_test_hat,history
```
Using TensorFlow backend.
```python
y_train_hat,y_test_hat,history=train_model(x_train_prc_anno, y_train_mlb,x_test_prc,
epochs=epochs,batch_size=64,class_weight=class_weight)
```
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 36, 36, 32) 832
_________________________________________________________________
conv2d_2 (Conv2D) (None, 32, 32, 32) 25632
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 16, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 12, 12, 32) 25632
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 6, 6, 32) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 4, 4, 32) 9248
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 2, 2, 32) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 2, 2, 32) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 128) 0
_________________________________________________________________
dense_1 (Dense) (None, 128) 16512
_________________________________________________________________
dense_2 (Dense) (None, 3) 387
=================================================================
Total params: 78,243
Trainable params: 78,243
Non-trainable params: 0
_________________________________________________________________
Epoch 1/100
156/156 [==============================] - 4s 26ms/step - loss: 2.2816 - acc: 0.3979 - val_loss: 1.1561 - val_acc: 0.4688
Epoch 2/100
156/156 [==============================] - 2s 15ms/step - loss: 1.4215 - acc: 0.5093 - val_loss: 0.7161 - val_acc: 0.6562
Epoch 3/100
156/156 [==============================] - 2s 14ms/step - loss: 0.7764 - acc: 0.7068 - val_loss: 0.3253 - val_acc: 0.8594
Epoch 4/100
156/156 [==============================] - 2s 13ms/step - loss: 0.5159 - acc: 0.8224 - val_loss: 0.4067 - val_acc: 0.8281
Epoch 5/100
156/156 [==============================] - 2s 13ms/step - loss: 0.3667 - acc: 0.8801 - val_loss: 0.0952 - val_acc: 0.9688
... ...
Epoch 96/100
156/156 [==============================] - 2s 13ms/step - loss: 0.0201 - acc: 0.9949 - val_loss: 9.9051e-05 - val_acc: 1.0000
Epoch 97/100
156/156 [==============================] - 2s 13ms/step - loss: 0.0207 - acc: 0.9941 - val_loss: 0.0071 - val_acc: 1.0000
Epoch 98/100
156/156 [==============================] - 2s 13ms/step - loss: 0.0263 - acc: 0.9945 - val_loss: 6.1150e-04 - val_acc: 1.0000
Epoch 99/100
156/156 [==============================] - 2s 15ms/step - loss: 0.0200 - acc: 0.9945 - val_loss: 0.0016 - val_acc: 1.0000
Epoch 100/100
156/156 [==============================] - 3s 17ms/step - loss: 0.0300 - acc: 0.9934 - val_loss: 0.0015 - val_acc: 1.0000
Loss on training and testing.
![png](output_10_1.png)
10000/10000 [==============================] - 0s 46us/step
5191/5191 [==============================] - 0s 47us/step
### 5. 提交结果
最后的结果可以提交到这里,来看看精度如何哦!
```python
import pandas as pd
submit = pd.read_csv('sample_submit.csv')
submit['y'] = y_test_hat
submit.to_csv('my_CNN_prediction.csv', index=False)
```