DeepLearning.ai-Week2-Keras tutorial-the Happy House
1 - Import Packages
import numpy as np
from keras import layers
from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.models import Model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
import pydot
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
from kt_utils import * import keras.backend as K
K.set_image_data_format('channels_last')
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow %matplotlib inline
2 - The Happy House
2.1 - Dataset Description

2.2 - Normalize the dataset and learn about its shape
图像大小为(64, 64, 3),训练集有600张图像,测试集有150张图像。
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset() # Normalize image vectors
X_train = X_train_orig/255.
X_test = X_test_orig/255. # Reshape
Y_train = Y_train_orig.T
Y_test = Y_test_orig.T print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))
Result:
number of training examples = 600
number of test examples = 150
X_train shape: (600, 64, 64, 3)
Y_train shape: (600, 1)
X_test shape: (150, 64, 64, 3)
Y_test shape: (150, 1)
3 - Building a model in Keras
这个作业比较开放,模型架构完全有自己决定。但建议使用作业提供的初始架构,然后再进行调整和改进。总之,我们可以修改模型的架构以及超参数。
使用Keras训练和测试模型,有如下步骤:
* 创建模型
* 编译模型:$model.compile(optimizer = "...", loss = "...", metrics = ["accuracy"])$
* 训练模型:$model.fit(x = ..., y = ..., epochs = ..., batch_size = ...)$
* 测试模型:$model.evaluate(x = ..., y = ...)$
综上,即是Create->Compile->Fit/Train->Evaluate/Test。
# GRADED FUNCTION: HappyModel def HappyModel(input_shape):
"""
Implementation of the HappyModel. Arguments:
input_shape -- shape of the images of the dataset Returns:
model -- a Model() instance in Keras
""" ### START CODE HERE ###
# Feel free to use the suggested outline in the text above to get started, and run through the whole
# exercise (including the later portions of this notebook) once. The come back also try out other
# network architectures as well.
X_input = Input(input_shape) # Zero-Padding: pads the border of X_input with zeroes
X = ZeroPadding2D((3, 3))(X_input) # CONV -> BN -> RELU Block applied to X X = Conv2D(32, (7, 7), strides=(1, 1), name="conv0")(X)
X = BatchNormalization(axis=3, name="bn0")(X)
X = Activation("relu")(X) # MAXPOOL
X = MaxPooling2D((2, 2), name="max_pool")(X) # FLATTEN X (means convert it to a vector) + FULLYCONNECTED
X = Flatten()(X)
X = Dense(1, activation="sigmoid", name="fc")(X) # Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
model = Model(inputs=X_input, outputs=X, name="HappyModel")
### END CODE HERE ### return model
3.1 - 创建模型
### START CODE HERE ### (1 line)
happyModel = HappyModel(X_train[0, :, :, :].shape)
### END CODE HERE ###
3.2 - 编译模型
### START CODE HERE ### (1 line)
happyModel.compile(optimizer="adam", loss="mse", metrics=["accuracy"])
### END CODE HERE ###
3.3 - 训练模型
我选择迭代10次,每一个批次有16个样本。
### START CODE HERE ### (1 line)
happyModel.fit(x=X_train, y=Y_train, epochs=40, batch_size=16)
### END CODE HERE ###
Result:
Epoch 1/10
600/600 [==============================] - 15s 25ms/step - loss: 1.5877 - acc: 0.6433
Epoch 2/10
600/600 [==============================] - 15s 25ms/step - loss: 0.3024 - acc: 0.8617
Epoch 3/10
600/600 [==============================] - 15s 25ms/step - loss: 0.1550 - acc: 0.9317
Epoch 4/10
600/600 [==============================] - 15s 25ms/step - loss: 0.1032 - acc: 0.9683
Epoch 5/10
600/600 [==============================] - 15s 26ms/step - loss: 0.1603 - acc: 0.9367
Epoch 6/10
600/600 [==============================] - 16s 26ms/step - loss: 0.0952 - acc: 0.9733
Epoch 7/10
600/600 [==============================] - 15s 26ms/step - loss: 0.0820 - acc: 0.9767
Epoch 8/10
600/600 [==============================] - 16s 26ms/step - loss: 0.0670 - acc: 0.9833
Epoch 9/10
600/600 [==============================] - 15s 26ms/step - loss: 0.0699 - acc: 0.9750
Epoch 10/10
600/600 [==============================] - 16s 27ms/step - loss: 0.1436 - acc: 0.9467
3.4 - 测试模型
### START CODE HERE ### (1 line)
preds = happyModel.evaluate(x=X_test, y=Y_test)
### END CODE HERE ###
print()
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))
Result:
150/150 [==============================] - 2s 11ms/step Loss = 4.14517145475
Test Accuracy = 0.559999998411
4 - Summary
happyModel.summary()
Result:
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) (None, 64, 64, 3) 0
_________________________________________________________________
zero_padding2d_1 (ZeroPaddin (None, 70, 70, 3) 0
_________________________________________________________________
conv0 (Conv2D) (None, 64, 64, 32) 4736
_________________________________________________________________
bn0 (BatchNormalization) (None, 64, 64, 32) 128
_________________________________________________________________
activation_1 (Activation) (None, 64, 64, 32) 0
_________________________________________________________________
max_pool (MaxPooling2D) (None, 32, 32, 32) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 32768) 0
_________________________________________________________________
fc (Dense) (None, 1) 32769
=================================================================
Total params: 37,633
Trainable params: 37,569
Non-trainable params: 64
_________________________________________________________________
plot_model(happyModel, to_file='HappyModel.png')
SVG(model_to_dot(happyModel).create(prog='dot', format='svg'))
Result:
5 - References
https://web.stanford.edu/class/cs230/
DeepLearning.ai-Week2-Keras tutorial-the Happy House的更多相关文章
- DeepLearning.ai学习笔记(三)结构化机器学习项目--week2机器学习策略(2)
一.进行误差分析 很多时候我们发现训练出来的模型有误差后,就会一股脑的想着法子去减少误差.想法固然好,但是有点headlong~ 这节视频中吴大大介绍了一个比较科学的方法,具体的看下面的例子 还是以猫 ...
- DeepLearning.ai学习笔记汇总
第一章 神经网络与深度学习(Neural Network & Deeplearning) DeepLearning.ai学习笔记(一)神经网络和深度学习--Week3浅层神经网络 DeepLe ...
- Coursera深度学习(DeepLearning.ai)编程题&笔记
因为是Jupyter Notebook的形式,所以不方便在博客中展示,具体可在我的github上查看. 第一章 Neural Network & DeepLearning week2 Logi ...
- Coursera机器学习+deeplearning.ai+斯坦福CS231n
日志 20170410 Coursera机器学习 2017.11.28 update deeplearning 台大的机器学习课程:台湾大学林轩田和李宏毅机器学习课程 Coursera机器学习 Wee ...
- 吴恩达deepLearning.ai循环神经网络RNN学习笔记_看图就懂了!!!(理论篇)
前言 目录: RNN提出的背景 - 一个问题 - 为什么不用标准神经网络 - RNN模型怎么解决这个问题 - RNN模型适用的数据特征 - RNN几种类型 RNN模型结构 - RNN block - ...
- 吴恩达deepLearning.ai循环神经网络RNN学习笔记_没有复杂数学公式,看图就懂了!!!(理论篇)
本篇文章被Google中国社区组织人转发,评价: 条理清晰,写的很详细! 被阿里算法工程师点在看! 所以很值得一看! 前言 目录: RNN提出的背景 - 一个问题 - 为什么不用标准神经网络 - RN ...
- 【Deeplearning.ai 】吴恩达深度学习笔记及课后作业目录
吴恩达深度学习课程的课堂笔记以及课后作业 代码下载:https://github.com/douzujun/Deep-Learning-Coursera 吴恩达推荐笔记:https://mp.weix ...
- Coursera DeepLearning.ai Logistic Regression逻辑回归总结
既<Machine Learning>课程后,Andrew Ng又推出了新一系列的课程<DeepLearning.ai>,注册了一下可以试听7天.之后每个月要$49,想想还是有 ...
- Deeplearning.ai课程笔记--汇总
从接触机器学习就了解到Andrew Ng的机器学习课程,后来发现又出来深度学习课程,就开始在网易云课堂上学习deeplearning.ai的课程,Andrew 的课真是的把深入浅出.当然学习这些课程还 ...
- 课程四(Convolutional Neural Networks),第二 周(Deep convolutional models: case studies) —— 2.Programming assignments : Keras Tutorial - The Happy House (not graded)
Keras tutorial - the Happy House Welcome to the first assignment of week 2. In this assignment, you ...
随机推荐
- css的简单学习笔记
1.CSS的简介 *css :层叠样式表 **层叠: 一层一层. **样式表: 具有大量的属性和属性值 *使得页面的显示效果更加好. *css将网页内容和显示样式进行分离,提高了显示功能. *css不 ...
- vue-cli+webpack在生成的项目中使用bootstrap的方法
在一个html页面中加入bootstrap是很方便,就是一般的将css和js文件通过Link和Script标签就行.那么在一个用vue-vli生成的前端项目中如何加入?因为框架不一样了,略微要适应一下 ...
- iis8.0 https配置教程
打开iis>选择左侧根>点击右侧服务器证书 打开界面后 空白处点击右键选择导入 成功导入证书 选择需要绑定证书的网站点击选择>编辑绑定>ssl证书请选择您导入的证书 点击SSL ...
- spring的设计模式
spring中用到哪些设计模式 1.工厂模式,这个很明显,在各种BeanFactory以及ApplicationContext创建中都用到了: 2.模版模式,这个也很明显,在各种BeanFacto ...
- beeswarm-蜜蜂图
一.beeswarm作为一维散点图包R包,可以生成点不重复的图,与stripchart的区别就是等值点不会重叠到一起,下图展示了stripchart与beeswarm图的区别: stripchart( ...
- 本地服务器上挂载A目录到B目录
原因: 由于某个分区满了,切磁盘无法扩大分区空间,但是项目依赖该分区,需要继续像该分区存储文件,此时其他分区还有很大的空间,使用挂载的方式,在有空间的分区创建新目录,将新目录挂载到源目录下即可. 执行 ...
- ruby--Hash方法汇总
一.给Hash添加默认值 :h = {1,2,3,4} #=> {1 => 2, 3 => 4} h.default = 7 h[1] ...
- 解决 Ubuntu 经常 卡死
ubuntu 的卡死可能与显卡驱动不兼容有关. 这里提供2种方式, 1.禁用原来自带的nouveau显卡驱动sudo gedit /etc/modprobe.d/blacklist.conf在最后一行 ...
- JQ和Js获取span标签的内容
JQ和Js获取span标签的内容 html: 1 <span id="content">‘我是span标签的内容’</span> javascript获取: ...
- 关于Ant脚本
在开发中,一个项目要经历单元测试l,集成测试,系统测试,测试过程中可能要不断修改代码,Ant脚本,通过一个xml文件,封装一系列繁琐又常用的操作,通过Ant指令执行xml脚本来批处理创建删除任务,编译 ...