iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 13
0
Big Data

tensorflow 學習筆記系列 第 13

Tensorflow Day13 使用 Google 的 Inception Model

今日目標

  • 載入 Google 預訓練好的 Inception ResNet V2 模型
  • 使用一張圖片給模型辨識

簡介

以下是一個 Google 所創造出來的一個大型模型,並且花了很多的計算能力還有許多的時間計算出了模型的權重,這個過程可是要非常花錢的,不太是我們自己可以訓練得出來的(除非有很多的 $$).不過 google 很佛心的開源了這個模型,今天就讓我們來試用一下這一個 google 的模型.

照著 google blog 執行範例

Google blog 說明

import numpy as np
import os
import tensorflow as tf
import urllib2

from datasets import imagenet
from nets import inception
from preprocessing import inception_preprocessing

slim = tf.contrib.slim

batch_size = 3
image_size = inception.inception_v3.default_image_size

checkpoints_dir = '/root/code/model'
checkpoints_filename = 'inception_resnet_v2_2016_08_30.ckpt'
model_name = 'InceptionResnetV2'
sess = tf.InteractiveSession()
graph = tf.Graph()
graph.as_default()

def classify_from_url(url):
    image_string = urllib2.urlopen(url).read()
    image = tf.image.decode_jpeg(image_string, channels=3)
    processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
    processed_images  = tf.expand_dims(processed_image, 0)
    
    # Create the model, use the default arg scope to configure the batch norm parameters.
    with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
        logits, _ = inception.inception_resnet_v2(processed_images, num_classes=1001, is_training=False)
    probabilities = tf.nn.softmax(logits)
    
    init_fn = slim.assign_from_checkpoint_fn(
        os.path.join(checkpoints_dir, checkpoints_filename),
        slim.get_model_variables(model_name))
    
    init_fn(sess)
    np_image, probabilities = sess.run([image, probabilities])
    probabilities = probabilities[0, 0:]
    sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]
        
    plt.figure()
    plt.imshow(np_image.astype(np.uint8))
    plt.axis('off')
    plt.show()

    names = imagenet.create_readable_names_for_imagenet_labels()
    for i in range(5):
        index = sorted_inds[i]
        print('Probability %0.2f%% => [%s]' % (probabilities[index], names[index]))

Probability 0.92% => [cocker spaniel, English cocker spaniel, cocker]
Probability 0.02% => [Sussex spaniel]
Probability 0.01% => [clumber, clumber spaniel]
Probability 0.00% => [golden retriever]
Probability 0.00% => [Irish setter, red setter]

小結

今天實際了使用了 google 開源的辨識模型來執行一個簡單的例子.

學習資源連結


上一篇
Tensorflow Day12 儲存以及載入模型參數
下一篇
Tensorflow Day14 Fine Tuning
系列文
tensorflow 學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言