3.Programming in TensorFlow and Keras
Intro
这是深度学习第3课。
在本课程结束时,您将能够编写TensorFlow和Keras代码,以使用计算机视觉中最好的模型之一。
Lesson
[1]
from IPython.display import YouTubeVideo
YouTubeVideo('Epn3ryqr-F8', width=800, height=450)
Sample Code
选择要使用的图像
【2】
from os.path import joinimage_dir = '../input/dog-breed-identification/train/'
img_paths = [join(image_dir, filename) for filename in ['0246f44bb123ce3f91c939861eb97fb7.jpg','84728e78632c0910a69d33f82e62638c.jpg','8825e914555803f4c67b26593c9d5aff.jpg','91a5e8db15bccfb6cfa2df5e8b95ec03.jpg']]
读取和准备用于建模的图像的函数
【3】
import numpy as np
from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.preprocessing.image import load_img, img_to_arrayimage_size = 224def read_and_prep_images(img_paths, img_height=image_size, img_width=image_size):imgs = [load_img(img_path, target_size=(img_height, img_width)) for img_path in img_paths]img_array = np.array([img_to_array(img) for img in imgs])return preprocess_input(img_array)
/opt/conda/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.from ._conv import register_converters as _register_converters
使用预训练权重文件创建模型,作出预测:
【4】
from tensorflow.python.keras.applications import ResNet50my_model = ResNet50(weights='../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
test_data = read_and_prep_images(img_paths)
preds = my_model.predict(test_data)
视觉预测
【5】
import sys
# Add directory holding utility functions to path to allow importing
sys.path.append('/kaggle/input/python-utility-code-for-deep-learning-exercises/utils')
from decode_predictions import decode_predictionsfrom IPython.display import Image, displaymost_likely_labels = decode_predictions(preds, top=3, class_list_path='../input/resnet50/imagenet_class_index.json')for i, img_path in enumerate(img_paths):display(Image(img_path))print(most_likely_labels[i])
[('n02097209', 'standard_schnauzer', 0.54968953), ('n02097047', 'miniature_schnauzer', 0.42013007), ('n02097130', 'giant_schnauzer', 0.019662209)]
[('n02092339', 'Weimaraner', 0.9932476), ('n02099849', 'Chesapeake_Bay_retriever', 0.0026773105), ('n02109047', 'Great_Dane', 0.0013211624)]
[('n02105855', 'Shetland_sheepdog', 0.95110327), ('n02106030', 'collie', 0.043800134), ('n02096294', 'Australian_terrier', 0.0012826553)]
[('n02110627', 'affenpinscher', 0.90041274), ('n02112706', 'Brabancon_griffon', 0.059599612), ('n02086079', 'Pekinese', 0.008652819)]
Exercise
现在您已准备好自己使用强大的TensorFlow模型。
Continue
练习结束后,继续学习Transfer Learning。将让您利用预先训练的模型,远远超出最初的目的。 当他们第一次体验Transfer学习的力量时,大多数人都感到惊讶。
Exercise:Coding in TensorFlow and Keras
Exercise Introduction
电视节目硅谷有一个名为“See Food”的应用程序,它承诺在图片中识别食物(在这个紧张的场景中演示应用程序)。
在本练习中,您将使用预先训练的模型和TensorFlow为此应用程序构建引擎。
复制这篇笔记,并按照以下步骤。
1)Create Image Paths
我们已经提供了测试的图片文件,运行下面的代码存储文件路径:
【1】
from os.path import joinhot_dog_image_dir = '../input/hot-dog-not-hot-dog/seefood/train/hot_dog'hot_dog_paths = [join(hot_dog_image_dir,filename) for filename in ['1000288.jpg','127117.jpg']]not_hot_dog_image_dir = '../input/hot-dog-not-hot-dog/seefood/train/not_hot_dog'
not_hot_dog_paths = [join(not_hot_dog_image_dir, filename) for filename in['823536.jpg','99890.jpg']]img_paths = hot_dog_paths + not_hot_dog_paths
2)Set Up Preprocessing
将read_and_prep_images函数从指令页面复制到下面的单元格中(替换当前存在的函数)。
【2】
import numpy as np
from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.preprocessing.image import load_img, img_to_arrayimage_size = 224def read_and_prep_images(img_paths, img_height=image_size, img_width=image_size):pass
3)Modeling
- 创建一个Resnet50模型并将其另存为my_model。
- 将read_and_prep_images函数应用于img_paths并将结果保存为image_data。
- 使用my_model预测image_data的内容。 将结果存储在my_preds中。
您可以查看说明页面以提醒自己如何执行此操作。
4) Visualize Your Results
导入我们用来获取顶部标签的decode_predictions函数。TensorFlow包含此函数的替代版本,但我们将使用针对在Kaggle Kernel上运行而优化的版本。
【4】
import sys
from learntools.deep_learning.decode_predictions import decode_predictions
取消注释下面的行以查看示例图像和预测
【5】
from IPython.display import Image, display# most_likely_labels = decode_predictions(my_preds, top=3, class_list_path='../input/resnet50/imagenet_class_index.json')
# for i, img_path in enumerate(img_paths):
# display(Image(img_path))
# print(most_likely_labels[i])
Keep Going
您已准备好进行Transfer Learning,这将允许您为自定义目的应用相同级别的效率。
3.Programming in TensorFlow and Keras相关推荐
- 深度学习环境配置指南:Pytorch、TensorFlow、Keras
点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 作者丨Yukyin@知乎 来源丨https://zhuanlan.z ...
- tensorflow tf.keras.utils.plot_model 画深度学习神经网络拓扑图
tensorflow tf.keras.utils.plot_model 画网络拓扑图 # pip install graphviz # pip install pydot # 下载 graphviz ...
- keras和tensorflow使用 keras.callbacks.EarlyStopping 提前结束训练
此文首发于我的个人博客:keras和tensorflow使用 keras.callbacks.EarlyStopping 提前结束训练 - zhang0peter的个人博客 一般来说机器学习的训练次数 ...
- keras和tensorflow使用 keras.callbacks.TensorBoard 可视化数据
此文首发于我的个人博客:keras和tensorflow使用 keras.callbacks.TensorBoard 可视化数据 - zhang0peter的个人博客 TensorBoard 是一个非 ...
- tensorflow与keras关系
tensorflow简介以及与Keras的关系 - eyesfree - 博客园 TensorFlow 和keras有什么区别? - 知乎
- 加快Tensorflow和Keras图像数据集的训练速度
这几天在训练一个CNN网络,使用到了两百多万个图片,虽然使用到了GPU NVIDIA GeForce GTX 1080Ti,但是还是很慢.故查阅了一些训练提速的文章,跟大家分享. Tensorflow ...
- windows安装TensorFlow和Keras遇到的问题及其解决方法
windows安装TensorFlow和Keras遇到的问题及其解决方法 参考文章: (1)windows安装TensorFlow和Keras遇到的问题及其解决方法 (2)https://www.cn ...
- TensorFlow框架--Keras
目录 Keras介绍 Keras和tensorflow关系 Keras介绍 Keras 是一个高级的Python 神经网络框架,其文档详.Keras 已经被添加到 TensorFlow 中,成为其默认 ...
- 2_初学者快速掌握主流深度学习框架Tensorflow、Keras、Pytorch学习代码(20181211)
初学者快速掌握主流深度学习框架Tensorflow.Keras.Pytorch学习代码 一.TensorFlow 1.资源地址: 2.资源介绍: 3.配置环境: 4.资源目录: 二.Keras 1.资 ...
最新文章
- 倍福TwinCAT(贝福Beckhoff)基础教程5.1 TwinCAT-2 运行可执行文件
- 【讲师团招募令】14场线上万人社区大会 邀你来分享
- istringstream ostringstream stringstream
- linux 文件与进程
- 数字图像处理:第七章 邻域运算
- 关于JAVA中子类和父类的构造方法
- Android官方开发文档Training系列课程中文版:分享文件之获取文件信息
- 2.2.1.处理机调度的概念,层次
- oracle12 快照保存时间,【AWR】调整AWR数据采样时间间隔及历史快照保留时间
- 打印十字图-蓝桥杯历届试题
- 计算机cmd复制粘贴指令,win7系统使用CMD命令复制和删除文件夹的方法
- java关闭窗口的6种方法
- 盈通785G显卡超频/开核教程
- Finite State Transducer(FST)in NLP
- 获取当天日期的前一天或前几天,查询一天内的数据
- 计算机知识动画小学,电脑动画
- dht11 python mysql_自己动手实现智能家居之温湿度数据采集存储(DHT11,MySql)
- PCB实用设计3 | 二极管大全·尖刺·滤波·有意义的降压
- ABB机械臂和RobotStudio编程简介
- mysql holdlock_mysql事务隔离界别与锁机制
热门文章
- html5 应用框架,基于HTML5移动应用框架的研究及应用
- 华为做raid5步骤_华为验厂验厂流程如何?主要内容是什么呢?
- python list存储方式_Python 之 将 list 存储为 .mat 文件
- 初学echart的简单使用
- threejs中坐标系转换和实现物体跟随鼠标移动
- idea中java文件怎么运行_Java入门基础篇-如何在Java中创建只读文件
- SIP协议学习2-pjsip
- Windows Embedded CE 6.0开发初体验(五)构建CE平台
- java连接imserver_java后端IM消息推送服务开发——协议
- docker访问宿主机mysql_docker容器内访问宿主机127.0.0.1服务