Tensorflow實現一個完整的CNN例子
阿新 • • 發佈:2019-02-04
使用了Tensorflow實現一個狗的類別的識別,模型中狗的類別一共為十種,資料集如下
使用到的工具包是tfleran,可以直接使用pip安裝 pip install tflearn
由於很多例子都是在mnist之類現有的資料集中跑的,當用到自己的資料集的時候,往往會比較麻煩
tflearn有現成的工具包可以將資料集轉為hdf5格式供我們使用,只需要一個小小的準備
dog_10_images/n02085620-Chihuahua/n02085620_10074.jpg 0
dog_10_images/n02085620-Chihuahua/n02085620_10131.jpg 0
dog_10_images/n02085620-Chihuahua/n02085620_10621.jpg 0
dog_10_images/n02085620-Chihuahua/n02085620_1073.jpg 0
dog_10_images/n02085620-Chihuahua/n02085620_10976.jpg 0
生成類似這樣的txt檔案,後面0表示類別 ok 其程式碼如下
import os
import re
def createFileList(path,txt_path):
fw = open(txt_path,'r+')
image_files = os.listdir(path)
for i in range(len(image_files)):
dog_categories = os.listdir(path+'/'+image_files[i])
for each_image in dog_categories:
fw.write(path+'/' +image_files[i]+'/'+ each_image + ' %d\n'%i)
print('生成txt檔案成功\n')
fw.close()
path = 'dog_10_images'
txt_path = 'train.txt'
createFileList(path,txt_path)
這樣之後再這樣
dataset_file = 'train.txt'
# Build a HDF5 dataset (only required once)
from tflearn.data_utils import build_hdf5_image_dataset
build_hdf5_image_dataset(dataset_file, image_shape=(128 , 128), mode='file', output_path='dataset.h5', categorical_labels=True, normalize=True)
# Load HDF5 dataset
import h5py
h5f = h5py.File('dataset.h5', 'r')
X = h5f['X']
Y = h5f['Y']
import tflearn
from tflearn.data_utils import shuffle, to_categorical
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
X, Y = shuffle(X, Y)
from sklearn.model_selection import train_test_split
train_X, test_X, train_y, test_y = train_test_split(X,Y,test_size = 0.2,random_state = 0)
# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()
# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
# Convolutional network building
network = input_data(shape=[None, 128, 128, 3],
data_preprocessing=img_prep,
data_augmentation=img_aug,name="InputData")
network = conv_2d(network, 32, 3, activation='relu',name="Conv2D1")
network = max_pool_2d(network, 2,name="MaxPool2D1")
network = conv_2d(network, 64, 3, activation='relu',name="Conv2D2")
network = conv_2d(network, 64, 3, activation='relu',name="Conv2D3")
network = max_pool_2d(network, 2,name="MaxPool2D2")
network = fully_connected(network, 512, activation='relu',name="FullyConnected1")
network = dropout(network, 0.5)
network = fully_connected(network, 10, activation='softmax',name="FullyConnected2")
network = regression(network, optimizer='adam',
loss='categorical_crossentropy',
learning_rate=0.001)
# Train using classifier
model = tflearn.DNN(network, tensorboard_verbose=3)
model.fit(train_X, train_y, n_epoch=50, shuffle=True, validation_set=(test_X, test_y),
show_metric=True, batch_size=96, run_id='dog_cnn')