Kaggle : Using a Convolutional Neural Network for classifying Cats vs Dogs


数据下载


https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data


Part 1 - Preprocessing


#Package Requirements
#!/usr/bin/python2
# -*- coding: UTF-8 -*-
import cv2                 # working with, mainly resizing, images
import numpy as np         # dealing with arrays
import os                  # dealing with directories
from random import shuffle # mixing up or currently ordered data that might lead our network astray in training.
from tqdm import tqdm      # a nice pretty percentage bar for tasks. TRAIN_DIR = '/home/bids/miscellaneous/catvsdog/train'
TEST_DIR = '/home/bids/miscellaneous/catvsdog/test'
IMG_SIZE = 50
LR = 1e-3MODEL_NAME = 'dogsvscats-{}-{}.model'.format(LR, '2conv-basic') # just so we remember which saved model is which, sizes must match#Convert the dog/cat to an array with one-hot,标签类别化def label_img(img):word_label = img.split('.')[-3]# conversion to one-hot array [cat,dog]#                            [much cat, no dog]if word_label == 'cat': return [1,0]#                             [no cat, very doggo]elif word_label == 'dog': return [0,1]#fully process the training images and their labels into arrays训练集预处理def create_train_data():training_data = []for img in tqdm(os.listdir(TRAIN_DIR)):label = label_img(img)path = os.path.join(TRAIN_DIR,img)img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))training_data.append([np.array(img),np.array(label)])shuffle(training_data) #将序列的所有元素随机排序np.save('train_data.npy', training_data)return training_data#无标签数据预处理
def process_test_data():testing_data = []for img in tqdm(os.listdir(TEST_DIR)):path = os.path.join(TEST_DIR,img)img_num = img.split('.')[0]img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))testing_data.append([np.array(img), img_num])shuffle(testing_data)np.save('test_data.npy', testing_data)return testing_data#run the training生成训练集合
train_data = create_train_data()
# If you have already created the dataset:
#train_data = np.load('train_data.npy')

Part 2 - Convolutional Neural Network


#define our neural network
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regressionconvnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')model = tflearn.DNN(convnet, tensorboard_dir='log')#saving our model after every session, and reloading itif os.path.exists('{}.meta'.format(MODEL_NAME)):model.load(MODEL_NAME)print('model loaded!')#split out training and testing data,有标签数据分为训练集和测试集
train = train_data[:-500]
test = train_data[-500:]#separate my features and labels 特征,类别分离
X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
Y = [i[1] for i in train]test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
test_y = [i[1] for i in test]#fit for 3 epochs
model.fit({'input': X}, {'targets': Y}, n_epoch=3, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

执行结果

Training Step: 1148  | total loss: 11.71334 | time: 4.061s
| Adam | epoch: 003 | loss: 11.71334 - acc: 0.4913 -- iter: 24448/24500
Training Step: 1149  | total loss: 11.72928 | time: 5.074s
| Adam | epoch: 003 | loss: 11.72928 - acc: 0.4906 | val_loss: 11.88134 - val_acc: 0.4840 -- iter: 24500/24500
--

#Part 3 - Size Matters


#探讨网络深度的影响

import tensorflow as tf
tf.reset_default_graph()convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')model = tflearn.DNN(convnet, tensorboard_dir='log')if os.path.exists('{}.meta'.format(MODEL_NAME)):model.load(MODEL_NAME)print('model loaded!')train = train_data[:-500]
test = train_data[-500:]X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
Y = [i[1] for i in train]test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
test_y = [i[1] for i in test]model.fit({'input': X}, {'targets': Y}, n_epoch=20, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

执行结果

Training Step: 7659  | total loss: 0.30021 | time: 7.384s
| Adam | epoch: 020 | loss: 0.30021 - acc: 0.8845 -- iter: 24448/24500
Training Step: 7660  | total loss: 0.29932 | time: 8.405s
| Adam | epoch: 020 | loss: 0.29932 - acc: 0.8820 | val_loss: 0.53824 - val_acc: 0.7900 -- iter: 24500/24500
--
# save the model保存模型
model.save(MODEL_NAME)
#reload the model and continue training载入模型继续训练import tensorflow as tf
tf.reset_default_graph()convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')model = tflearn.DNN(convnet, tensorboard_dir='log')if os.path.exists('/home/bids/miscellaneous/catvsdog/{}.meta'.format(MODEL_NAME)):model.load(MODEL_NAME)print('model loaded!')train = train_data[:-500]
test = train_data[-500:]X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
Y = [i[1] for i in train]test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
test_y = [i[1] for i in test]model.fit({'input': X}, {'targets': Y}, n_epoch=10, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id=MODEL_NAME)model.save(MODEL_NAME)

执行结果

Training Step: 11489  | total loss: 0.15848 | time: 6.454s
| Adam | epoch: 010 | loss: 0.15848 - acc: 0.9387 -- iter: 24448/24500
Training Step: 11490  | total loss: 0.15471 | time: 7.476s
| Adam | epoch: 010 | loss: 0.15471 - acc: 0.9385 | val_loss: 1.08745 - val_acc: 0.7840 -- iter: 24500/24500
--
INFO:tensorflow:/home/bids/miscellaneous/catvsdog/dogsvscats-0.001-2conv-basic.model is not in all_model_checkpoint_paths. Manually adding it.

Part 4 - Visually inspecting our network against unlabeled data


import matplotlib.pyplot as plt# if you need to create the data:
#test_data = process_test_data()
# if you already have some saved:
test_data = np.load('test_data.npy')fig=plt.figure()for num,data in enumerate(test_data[:12]):# cat: [1,0]# dog: [0,1]img_num = data[1]img_data = data[0]y = fig.add_subplot(3,4,num+1)orig = img_datadata = img_data.reshape(IMG_SIZE,IMG_SIZE,1)#model_out = model.predict([data])[0]model_out = model.predict([data])[0]if np.argmax(model_out) == 1: str_label='Dog'else: str_label='Cat'y.imshow(orig,cmap='gray')plt.title(str_label)y.axes.get_xaxis().set_visible(False)y.axes.get_yaxis().set_visible(False)
plt.show()

这里写图片描述


Part 5 - Submit


import timestart = time.clock()
with open('submission_file.csv','w') as f:f.write('id,prolabel0,prolabel1,labelname\n')with open('submission_file.csv','a') as f:for data in tqdm(test_data):img_num = data[1]img_data = data[0]orig = img_datadata = img_data.reshape(IMG_SIZE,IMG_SIZE,1)model_out = model.predict([data])[0]#对概率最大的值赋予标签名if np.argmax(model_out) == 1: str_label='Dog'else:str_label='Cat'f.write('{},{},{},{}\n'.format(img_num,model_out[0],model_out[1],str_label))end = time.clock()
print("The running time is %g s" %(end-start))

执行结果及显示

The running time is 50.5271 s

submission_file.csv

id,prolabel0,prolabel1,labelname
1212,0.921218276024,0.0787817165256,Cat
9302,1.18830442375e-07,0.999999880791,Dog
12411,1.42178024021e-07,0.999999880791,Dog
6441,0.083547629416,0.916452348232,Dog
7080,0.427787721157,0.572212278843,Dog
5382,0.00636592926458,0.993634104729,Dog
353,0.782497525215,0.217502549291,Cat
5446,6.87951469445e-05,0.99993121624,Dog
4448,0.653192222118,0.346807777882,Cat
338,0.999975681305,2.43726990448e-05,Cat
9638,0.989276170731,0.010723865591,Cat
3980,0.998381376266,0.00161859672517,Cat
7844,0.608684241772,0.391315758228,Cat
9771,4.93591596751e-06,0.999995112419,Dog
10791,0.669038891792,0.330961108208,Cat
3925,0.0286846794188,0.971315264702,Dog
7400,0.246031478047,0.753968536854,Dog
5971,0.0695387497544,0.930461287498,Dog
3286,0.999945521355,5.44977847312e-05,Cat
6044,0.971816003323,0.0281839352101,Cat
9298,0.0108342785388,0.989165723324,Dog
729,0.999630212784,0.000369725632481,Cat
2853,1.0,1.77208714369e-09,Cat
516,0.0357219539583,0.964278101921,Dog
5129,0.997949063778,0.00205094460398,Cat
10121,0.999883055687,0.000116942981549,Cat
3133,0.00119872530922,0.998801231384,Dog
4034,0.999985337257,1.46087259054e-05,Cat
3158,0.999208390713,0.000791579775978,Cat
4069,0.307045787573,0.692954182625,Dog
1478,1.0,2.06947931014e-11,Cat
7171,0.999999880791,9.28167693814e-08,Cat
10217,0.394322454929,0.605677485466,Dog
7977,0.178869321942,0.821130692959,Dog
9163,0.999999523163,4.64977688353e-07,Cat
11553,0.866536676884,0.133463352919,Cat
6130,0.997319757938,0.00268025626428,Cat
3611,1.0,1.71961218598e-08,Cat
1042,0.474040418863,0.525959551334,Dog
8793,0.352943599224,0.647056400776,Dog
9708,3.69108086318e-08,1.0,Dog
3499,1.0,4.09031670756e-08,Cat
2291,0.148211851716,0.85178810358,Dog
8138,0.391484260559,0.608515799046,Dog
9219,0.998136520386,0.00186341057997,Cat
11983,0.885705888271,0.114294171333,Cat
3192,0.18194848299,0.81805151701,Dog
3734,7.41316398489e-07,0.999999284744,Dog
4732,0.00249696546234,0.997503101826,Dog
10883,4.8458132369e-06,0.999995112419,Dog
11081,1.06594075078e-06,0.999998927116,Dog
11917,0.999524831772,0.000475237378851,Cat
2637,0.944479882717,0.0555201396346,Cat
7868,0.872499227524,0.127500742674,Cat
11800,0.999999642372,3.36826360581e-07,Cat
3444,0.990353822708,0.00964620243758,Cat
5415,0.990461647511,0.00953831989318,Cat
11853,0.958735942841,0.0412640571594,Cat
880,0.99071675539,0.0092832185328,Cat
8928,0.994742512703,0.00525750452653,Cat
12410,0.653257966042,0.346742123365,Cat
2755,0.443003803492,0.556996226311,Dog
9677,2.42542625983e-07,0.999999761581,Dog
9417,2.07065786739e-09,1.0,Dog
8962,0.969185173512,0.0308148153126,Cat
4797,1.27675070871e-06,0.999998688698,Dog
2027,0.341440081596,0.658559858799,Dog
11619,0.979589402676,0.0204106215388,Cat
8100,0.999998807907,1.18297475638e-06,Cat
10599,0.0924441888928,0.907555758953,Dog
10758,0.0891051217914,0.910894930363,Dog
1497,0.000189088241314,0.999810993671,Dog
11472,0.00835430901498,0.991645753384,Dog
6861,0.931414544582,0.0685854256153,Cat
12459,0.000314021250233,0.999686002731,Dog
4517,0.999940514565,5.95222300035e-05,Cat
4550,0.928105473518,0.0718945041299,Cat
12385,0.00242776027881,0.997572243214,Dog
4907,0.987588703632,0.0124113131315,Cat
10130,0.881314516068,0.118685476482,Cat
3146,0.999999523163,4.93570894378e-07,Cat
3786,0.997477352619,0.00252265273593,Cat
1203,0.0800434201956,0.919956505299,Dog
2496,0.415617018938,0.58438295126,Dog
7563,0.95674598217,0.043254006654,Cat
2239,0.970162272453,0.0298377349973,Cat
4682,0.999291062355,0.000708963314537,Cat
4378,0.0031416725833,0.996858358383,Dog
7665,0.909289181232,0.0907108262181,Cat
2823,0.99922311306,0.000776931643486,Cat
2185,0.000722112716176,0.999277889729,Dog
5316,0.583464622498,0.416535377502,Cat
9270,0.468584805727,0.531415224075,Dog
11067,0.953469336033,0.0465307161212,Cat
5680,0.125824138522,0.874175846577,Dog
9933,0.609232544899,0.390767514706,Cat
4388,0.00831792596728,0.991682112217,Dog
6527,0.316535592079,0.683464348316,Dog
8562,0.0318263992667,0.96817356348,Dog
3506,0.718664944172,0.281334996223,Cat
1401,0.00189117959235,0.998108863831,Dog
100,0.99985408783,0.000145845173392,Cat
3225,0.308728516102,0.691271424294,Dog
9353,0.941712677479,0.0582873113453,Cat
7416,0.25361225009,0.746387779713,Dog
841,0.836331427097,0.16366866231,Cat
3107,0.0021977883298,0.997802197933,Dog
10709,1.39225221574e-05,0.999986052513,Dog
6230,0.00928914453834,0.990710794926,Dog
10398,0.999177634716,0.000822360860184,Cat
2232,0.592651724815,0.407348304987,Cat
8878,0.0124056786299,0.987594246864,Dog
408,0.203185468912,0.796814501286,Dog
3679,0.952772557735,0.0472274385393,Cat
3445,8.16722513264e-06,0.999991774559,Dog
7682,5.48541611352e-05,0.999945163727,Dog
11807,0.982504308224,0.0174957159907,Cat
11197,0.97709685564,0.0229031760246,Cat
7556,0.113571561873,0.886428415775,Dog
5190,0.999565899372,0.000434125337051,Cat
10039,0.163930222392,0.836069762707,Dog
9409,3.1199199384e-06,0.999996900558,Dog
3684,0.0231008175761,0.976899206638,Dog
3436,0.0557881891727,0.944211781025,Dog
6475,0.000631944218185,0.999368131161,Dog
945,0.0242063160986,0.975793719292,Dog
12042,0.834796190262,0.165203869343,Cat
……
……
……


参考文献


Classifying Cats vs Dogs with a Convolutional Neural Network on Kaggle

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/246803.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

李宏毅机器学习课程1~~~Introduction Regression

机器学习介绍 机器学习就是要找一个函数。 机器学习的三大要素框架:训练集,函数集(模型集),损失函数集。 机器学习图谱 AI训练师的成长之路。 1. 梯度下降法的理解Gradient Descent 参数变化的方向就是损失函数减少的方…

李宏毅机器学习课程2~~~误差从哪里来?

Stanford机器学习—第六讲. 怎样选择机器学习方法、系统 误差来源 误差主要来自于偏差和方差。 数学上定义: 通过covariate X 预测 Y ,我们假设存在如下关系: Y f(X) ϵ 满足正态分布均值为0 方差σϵ 模型预测错误定义为: …

李宏毅机器学习课程3~~~梯度下降法

梯度下降法描述 梯度下降法是为了找到最优的目标函数,寻找的过程就是沿着损失函数下降的方向来确定参数变化的方向。参数更新的过程就是一个不断迭代的过程,每次更新参数学到的函数都会使得误差损失越来越小,也就是说学习到的参数函数越来越逼…

李宏毅机器学习课程4~~~分类:概率生成模型

分类问题用回归来解决? 当有右图所示的点时,这些点会大幅改变分类线的位置。这时候就会导致整体的回归结果变差。当把多分类当成回归问题,类别分别为1,2,3,4……,因为回归的问题是预测具体的值,这样定义类别…

李宏毅机器学习课程5~~~分类:逻辑回归

Function Set 不同的w,b来确定不同的函数,这样就组成了函数集合,不同的w,b可以来表达不同的分布函数。 Good of a Function 变换表达形式 两个Bernoulli distribution的交叉熵。所谓交叉熵,是用来刻画两个分布的相似性…

李宏毅机器学习课程6~~~深度学习入门

深度学习历史 深度学习经典步骤 神经网络的符合标记含义 Wij 代表的是从神经元j到神经元i,这样写的目的是便于表达,否则最后的表达式子就是Wij的转置,细节见下面。 每个神经元的偏执值组成一个向量b 单个神…

李宏毅机器学习课程7~~~反向传播

到底为什么基于反向传播的纯监督学习在过去表现不佳?Geoffrey Hinton总结了目前发现的四个方面问题: 带标签的数据集很小,只有现在的千分之一. 计算性能很慢,只有现在的百万分之一. 权重的初始化方式笨拙. 使用了错误的非线性模型…

李宏毅机器学习课程8~~~keras

keras keras示例 确定网络结构 确定损失函数 确定训练网络参数 batchsize与运算时间,平行运算,可以缩简运算时间。batchsize不能太大,这是由于内存的关系。此外,batchsize太大容易陷入局部极值点或者鞍点。batchsize=&…

李宏毅机器学习课程9~~~深度学习技巧

Recipe of Deep Learning Overfitting overfitting的判断是要训练误差与测试误差做比较。这个56-layer的网络在训练集上都没有训练好,说白了就是有点欠拟合。所以仅仅依靠测试集上的结果来判断56-layer比20-layer overfitting是不合理的。 更多理解见 Overfitting…

Liner(分段线性插值)

第一次写微博,记录自己的学习历程~~~~欢迎大家一起探讨~~~~ 分段线性插值故名思议就是说把给定样本点的区间分成多个不同区间,记为[xi,xi1],在每个区间上的一次线性方程为: 关于其证明: 分段线性插值在速度和误差取得…

在linux设置回收站 - 防止失误操作造成数据清空,并定期清理

安装trash sudo apt-get install trash-chi 原理 执行trash命令后,是将文件移动了用户的回收站,每个用户的回收站路径为$HOME/.local/share/Trash,比如用户asin的回收站位于/home/asin/.local/share/Trash,用户root的回收站位于…

Spline(三次样条插值)

关于三次样条插值,计算方法比较复杂,但是静下心来仔细研究也是可以理解的。 本文借鉴文章来源:http://www.cnki.com.cn/Article/CJFDTotal-BGZD200611035.htm 定义: 简单来说就是给定了一些在区间[a,b]的数据点{x1,x2,x3.....xn…

李宏毅机器学习课程10~~~卷积神经网络

卷积的意义 数字图像是一个二维的离散信号,对数字图像做卷积操作其实就是利用卷积核(卷积模板)在图像上滑动,将图像点上的像素灰度值与对应的卷积核上的数值相乘,然后将所有相乘后的值相加作为卷积核中间像素对应的图像…

matlab自带的插值函数interp1的四种插值方法

x0:2*pi; ysin(x); xx0:0.5:2*pi;%interp1对sin函数进行分段线性插值,调用interp1的时候,默认的是分段线性插值 y1interp1(x,y,xx); figure plot(x,y,o,xx,y1,r) title(分段线性插值)%临近插值 y2interp1(x,y,xx,nearest); figure plot(x,y,o,xx,y2,r); …

拉格朗日插值法(Lagrange)

拉格朗日插值法是基于基函数的插值方法,插值多项式可以表示为: 其中称为 i 次基函数 Matlab中拉格朗日插值法函数为:Language 功能:求已知点数据点的拉格朗日多项式 调用格式:fLagrange(x,y) 或者 f ’Lagrange(x,y,x0) 其中&a…

当你在应用机器学习时你应该想什么

如今, 机器学习变得十分诱人, 它已在网页搜索, 商品推荐, 垃圾邮件检测, 语音识别, 图像识别, 自然语言处理等诸多领域发挥重要作用. 和以往我们显式地通过编程告诉计算机如何进行计算不同, 机器学习是一种数据驱动方法(data-driven approach). 然而, 有时候机器学习像是一种”…

利用均差的牛顿插值法(Newton)

函数f的零阶均差定义为 ,一阶定义均差为: 一般地,函数f 的k阶均差定义为: 或者上面这个式子求的k1阶均差 利用均差的牛顿插值法多项式为: 简单计算的时候可以观看下面的差商(均差)表&#xff1a…

深度学习(Deep Learning)读书思考三:正则化

概述 正则化是机器学习中非常重要并且非常有效的减少泛华误差的技术,特别是在深度学习模型中,由于其模型参数非常多非常容易产生过拟合。因此研究者也提出很多有效的技术防止过拟合,比较常用的技术包括: 参数添加约束,…

利用差分的牛顿插值法(Newton)

差分牛顿插值法要求是等距的。 先来看三个概念 差分与均差的关系如下: 牛顿(Newton)插值的基本公式为: 由于差分插值是等距的,所以可以设xx0nh 对于上式 再由差分和均差的关系,可以将上面的黄色部分也就是牛顿插值基本公式转换…

埃尔米特(Hermite)插值

Hermite插值满足在节点上等于给定函数值,而且在节点上的导数值也等于给定的导数值。对于高阶导数的情况,Hermite插值多项式比较复杂,在实际情况中,常常遇到的是函数值与一阶导数给定的情况。在此情况下,n个节点x1,x2,……