第四周编程作业(二)-Deep Neural Network for Image Classification: Application(一)

简介: 第四周编程作业(二)-Deep Neural Network for Image Classification: Application(一)

Deep Neural Network for Image Classification: Application


When you finish this, you will have finished the last programming assignment of Week 4, and also the last programming assignment of this course!

You will use use the functions you'd implemented in the previous assignment to build a deep network, and apply it to cat vs non-cat classification. Hopefully, you will see an improvement in accuracy relative to your previous logistic regression implementation.


After this assignment you will be able to:

  • Build and apply a deep neural network to supervised learning.

Let's get started!


1 - Packages


Let's first import all the packages that you will need during this assignment.

  • numpy is the fundamental package for scientific computing with Python.
  • matplotlib is a library to plot graphs in Python.
  • h5py is a common package to interact with a dataset that is stored on an H5 file.
  • PIL and scipy are used here to test your model with your own picture at the end.
  • dnn_app_utils provides the functions implemented in the "Building your Deep Neural Network: Step by Step" assignment to this notebook.
  • np.random.seed(1) is used to keep all the random function calls consistent. It will help us grade your work.

import time
import numpy as np
import h5py
import matplotlib.pyplot as plt
import scipy
from PIL import Image
from scipy import ndimage
from dnn_app_utils_v2 import *
%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
%load_ext autoreload
%autoreload 2
np.random.seed(1)


2 - Dataset


You will use the same "Cat vs non-Cat" dataset as in "Logistic Regression as a Neural Network" (Assignment 2). The model you had built had 70% test accuracy on classifying cats vs non-cats images. Hopefully, your new model will perform a better!


Problem Statement: You are given a dataset ("data.h5") containing:

- a training set of m_train images labelled as cat (1) or non-cat (0)

- a test set of m_test images labelled as cat and non-cat

- each image is of shape (num_px, num_px, 3) where 3 is for the 3 channels (RGB).

Let's get more familiar with the dataset. Load the data by running the cell below.

train_x_orig, train_y, test_x_orig, test_y, classes = load_data()

The following code will show you an image in the dataset. Feel free to change the index and re-run the cell multiple times to see other images.

# Example of a picture
index = 10
plt.imshow(train_x_orig[index])
print ("y = " + str(train_y[0,index]) + ". It's a " + classes[train_y[0,index]].decode("utf-8") +  " picture.")

y = 0. It's a non-cat picture.


20.png

output_7_1.png

# Explore your dataset 
m_train = train_x_orig.shape[0]
num_px = train_x_orig.shape[1]
m_test = test_x_orig.shape[0]
print ("Number of training examples: " + str(m_train))
print ("Number of testing examples: " + str(m_test))
print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)")
print ("train_x_orig shape: " + str(train_x_orig.shape))
print ("train_y shape: " + str(train_y.shape))
print ("test_x_orig shape: " + str(test_x_orig.shape))
print ("test_y shape: " + str(test_y.shape))

Number of training examples: 209
Number of testing examples: 50
Each image is of size: (64, 64, 3)
train_x_orig shape: (209, 64, 64, 3)
train_y shape: (1, 209)
test_x_orig shape: (50, 64, 64, 3)
test_y shape: (1, 50)


As usual, you reshape and standardize the images before feeding them to the network. The code is given in the cell below.


<caption><center> <u>Figure 1</u>: Image to vector conversion.

</center></caption>

# Reshape the training and test examples 
train_x_flatten = train_x_orig.reshape(train_x_orig.shape[0], -1).T   # The "-1" makes reshape flatten the remaining dimensions
test_x_flatten = test_x_orig.reshape(test_x_orig.shape[0], -1).T
# Standardize data to have feature values between 0 and 1.
train_x = train_x_flatten/255.
test_x = test_x_flatten/255.
print ("train_x's shape: " + str(train_x.shape))
print ("test_x's shape: " + str(test_x.shape))

train_x's shape: (12288, 209)
test_x's shape: (12288, 50)


$12,288$ equals $64 \times 64 \times 3$ which is the size of one reshaped image vector.

相关文章
|
6月前
|
人工智能 搜索推荐 SEO
外贸机械企业AI搜索曝光提升3倍:AB客GEO介入全过程复盘
本文剖析外贸机械企业如何应对AI搜索变革:传统SEO已不足,需转向GEO(生成式搜索引擎优化)。通过构建企业数字人格、建立客户采购问题库、重构FAQ/产品/案例内容、增强结构化数据,使AI能理解、信任并引用企业。某企业实践后,AI搜索曝光提升约3倍,实现从“争排名”到“进答案”的跃迁。
462 0
|
8月前
|
人工智能 IDE 开发工具
Zed 编辑器小修小补:让 diff 颜色终于“说人话“了!
本文详解Zed IDE在diff颜色语义化上的精妙改进:新增行用绿色、删除行用红色,真正符合直觉;通过专属token解耦样式与语义,提升主题兼容性与可维护性,并为未来扩展预留接口。小改动,大体验!
403 1
|
11月前
|
存储 缓存 算法
如何实现本地缓存?
本地缓存是将数据存储在应用服务器内存中以提升访问速度的技术。设计时需关注数据结构(Key-Value)、线程安全、对象上限、清除策略(如LRU、LFU)和过期时间。推荐使用高性能的Caffeine框架,其采用Window TinyLFU算法,支持异步操作与精确失效机制,优于Guava Cache,是Spring 5默认缓存方案。
436 5
|
12月前
|
网络协议 安全 应用服务中间件
Debian 11.1 安装Nginx 并开启HTTP3 详细教程
本文介绍如何在Linux系统中从源码编译安装支持HTTP/3的Nginx 1.25.5。涵盖系统更新、依赖安装、源码下载、配置编译参数(含HTTP/3模块)、安装及验证全过程,并提供启用HTTP/2与HTTP/3共存的nginx.conf配置示例,确保兼容性。最后提醒开放云服务器安全组的TCP/UDP 443端口以支持QUIC协议。
568 10
|
12月前
|
缓存 API 开发者
爱回收批量价格查询 API 接口实战:批量估价上限、请求拆分与效率优化
爱回收批量价格查询 API(接口路径:/v1/device/batch/estimate)是为多设备集中估价场景设计的高效接口,支持一次性传入多个设备的配置信息,批量返回回收报价。其核心优势在于 “减少接口调用次数、提升批量处理效率”,适用于:
|
存储 编解码 缓存
《冬季游戏动态交互的底层逻辑优化全解析》
本文聚焦冬季游戏动态渲染优化,围绕动态积雪、实时气象、物理交互、地形形变及间接光影表现五大核心方向展开。针对初期全场景动态效果导致的帧率卡顿、服务器负载过高问题,提出“交互优先级矩阵”“地形-气象联动模型”“预计算+实时插值”等创新策略,通过核心场景高精度渲染、非核心场景逻辑简化与资源复用,实现动态元素自然呈现与性能稳定的平衡。优化后,主流设备帧率稳定在60帧以上,多人联机延迟可控,玩家沉浸感显著提升。文章结合实战案例拆解底层优化逻辑,为冬季游戏动态渲染提供兼具实用价值与独特视角的进阶方法论,彰显“取舍与协同”的渲染核心思维。
548 0
|
机器学习/深度学习 存储 PyTorch
PyTorch内存优化的10种策略总结:在有限资源环境下高效训练模型
在大规模深度学习模型训练中,GPU内存容量常成为瓶颈,特别是在训练大型语言模型和视觉Transformer时。本文系统介绍了多种内存优化策略,包括混合精度训练、低精度训练(如BF16)、梯度检查点、梯度累积、张量分片与分布式训练、
946 14
PyTorch内存优化的10种策略总结:在有限资源环境下高效训练模型
|
数据可视化 安全 持续交付
敏捷方法大比拼:Scrum 适合你,还是 Kanban 更合适?
在数字化时代,企业面临项目管理的诸多挑战,如信息不透明、沟通低效等。Scrum 和 Kanban 作为敏捷管理方法,通过迭代优化和流程可视化提升协作效率与交付速度。Scrum 适合周期性迭代交付,强调短周期冲刺;Kanban 则适用于持续交付,强调任务流动性和灵活性。两者结合可形成 ScrumBan 模式,进一步优化任务处理。 对于数据安全要求高的企业,私有化部署工具(如板栗看板)确保数据自主可控、高安全性及定制化需求,保障业务连续性。选择合适的敏捷方法并结合私有化部署,能有效提升团队协作效率,助力企业在竞争中保持领先。
|
安全 网络安全 网络架构
电脑提示“Waiting for Device”的解决方案
电脑提示“Waiting for Device”的解决方案
1898 4
|
存储 前端开发 JavaScript
微任务和宏任务有什么区别
微任务和宏任务是JavaScript异步编程中的两个概念。宏任务包括整体代码块、setTimeout等,微任务有Promise、MutationObserver等。主要区别在于执行时机:每次事件循环中,宏任务只执行一个,而微任务会在当前宏任务结束后、下一个宏任务开始前全部执行完毕。