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 join

image_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_array

image_size = 224

def 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 ResNet50

my_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_predictions

from IPython.display import Image, display

most_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 join

hot_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_array

image_size = 224

def read_and_prep_images(img_paths, img_height=image_size, img_width=image_size):
    pass

3)Modeling

  1. 创建一个Resnet50模型并将其另存为my_model。
  2. 将read_and_prep_images函数应用于img_paths并将结果保存为image_data。
  3. 使用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,这将允许您为自定义目的应用相同级别的效率。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值