Tensorflow实现一个完整的CNN例子

使用了Tensorflow实现一个狗的类别的识别,模型中狗的类别一共为十种,数据集如下

链接: https://pan.baidu.com/s/1bpAjRkj 密码: jdc3

使用到的工具包是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')

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值