3.Programming in TensorFlow and Keras
生活随笔
收集整理的這篇文章主要介紹了
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_paths2)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):pass3)Modeling
您可以查看說明頁面以提醒自己如何執行此操作。
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从零开始学视觉Transformer(6
- 下一篇: 东方甄选农业直播爆火 俞敏洪:教育才是主