1 of 24

Getting Started with

TensorFlow 2.0

bit.ly/getting-started-tf2

박해선 - ML GDE

haesun.park@tensorflow.blog

2 of 24

What is TensorFlow

  • https://tensorflow.org
  • Machine Learning Framework based on dataflow graph, AutoDiff especially Neural Networks
  • TensorFlow 0.5 Release (2015. 11)
  • C++ core, Python API
  • Several High-level API including Keras
  • Define and Run (kitchen sink)

3 of 24

What is TensorFlow 2.0

  • TensorFlow 2.0 beta1 (2019. 06 ~ )
  • Keras Python API
  • Eager execution (Define by Run)
  • Functions, not session
  • AutoGraph
  • API Cleanup
  • No more Globals

4 of 24

Install TensorFlow

Windows, macOS, Linux + Python (2.7), 3.x

$ pip install tensorflow

$ pip install tensorflow-gpu

$ pip install tensorflow==2.0.0-beta1

$ pip install tensorflow-gpu==2.0.0-beta1

5 of 24

TF 2.0 Concept Diagram

6 of 24

Eager Execution

  • 1.x : tf.enable_eager_execution()
  • 2.x: default
  • Define by Run support (like PyTorch, Chainer)
  • Rapid Development
  • Easy Debugging (use Python toolchain) → easy to check bottlenecks
  • Native Control Flow (if, for etc) → easy to make complex model
  • Boost performance by AutoGraph

7 of 24

TensorFlow 1.x

TensorFlow 2.x

>>> import tensorflow as tf

>>>

>>> t = tf.nn.sigmoid([0.])

>>>

>>> print(t)

Tensor("Sigmoid_1:0", shape=(1,), dtype=float32)

>>> import tensorflow as tf

>>>

>>> t = tf.nn.sigmoid([0.])

>>>

>>> print(t)

tf.Tensor([0.5], shape=(1,), dtype=float32)

>>>

>>> print(t.numpy())

[0.5]

8 of 24

TensorFlow 1.x

TensorFlow 2.x

import tensorflow as tf

## 그래프를 정의합니다

g = tf.Graph()

with g.as_default():

x = tf.placeholder(dtype=tf.float32,

shape=(None), name='x')

w = tf.Variable(2.0, name='weight')

b = tf.Variable(0.7, name='bias')

z = w * x + b

init = tf.global_variables_initializer()

## 세션을 만들고 그래프 g를 전달합니다

with tf.Session(graph=g) as sess:

## w와 b를 초기화합니다

sess.run(init)

## z를 평가합니다

for t in [1.0, 0.6, -1.8]:

print('x=%4.1f --> z=%4.1f'%(

t, sess.run(z, feed_dict={x:t})))

import tensorflow as tf

w = tf.Variable(2.0, name='weight')

b = tf.Variable(0.7, name='bias')

# z를 평가합니다

for x in [1.0, 0.6, -1.8]:

z = w * x + b

print('x=%4.1f --> z=%4.1f'%(x, z))

9 of 24

AutoGraph

tf.Graph() + tf.session() → @tf.function

# TensorFlow 1.x

outputs = session.run(ops, feed_dict={placeholder: input})

@tf.function

def simple_func():

# complex computation with pure python

...

return z

# TensorFlow 2.0

outputs = simple_func(input)

  • for/while → tf.while_loop
  • if → tf.cond
  • for _ in dataset → dataset.reduce

10 of 24

Keras

  • High-Level Neural Networks Specification (https://keras.io) (2015. 03)
  • Add to tf.contrib.keras at TensorFlow 1.2
  • Promote to tf.keras at TensorFlow 1.4 (tf.layerstf.keras)
  • 1st Class Python API for TensorFlow 2.0
  • Deprecated tf.layer, tf.contrib.layers(Slim)

11 of 24

Why Keras

Handson-Machine Leanring with Scikit-Learn and TensorFlow 2/E

Swift !

12 of 24

Sequential API

from tensorflow import tf

model = tf.keras.Sequential()

# 64개의 유닛을 가진 완전 연결 층을 모델에 추가합니다:

model.add(tf.keras.layers.Dense(64, activation='relu'))

# 또 하나를 추가합니다:

model.add(tf.keras.layers.Dense(64, activation='relu'))

# 10개의 출력 유닛을 가진 소프트맥스 층을 추가합니다:

model.add(tf.keras.layers.Dense(10, activation='softmax'))

# 컴파일

model.compile(optimizer=tf.keras.optimizers.Adam(0.001),

loss='categorical_crossentropy',

metrics=['accuracy'])

# 모델 훈련

model.fit(train_data, labels, epochs=10, batch_size=32)

# 모델 평가

model.evaluate(test_data, labels)

# 샘플 예측

model.predict(new_sample)

model = tf.keras.Sequential([

tf.keras.layers.Dense(64),

tf.keras.layers.Dense(64),

tf.keras.layers.Dense(10),

])

13 of 24

Functional API

from tensorflow import tf

# 입력과 출력을 연결

inputs = tf.keras.Input(shape=(784,), name='img')

x = tf.keras.layers.Dense(64, activation='relu')(inputs)

x = tf.keras.layers.Dense(64, activation='relu')(x)

outputs = tf.keras.layers.Dense(10, activation='softmax')(x)

# 모델 생성

model = tf.keras.keras.Model(inputs=inputs, outputs=outputs, name='mnist_model')

# 컴파일

...

# 훈련

...

14 of 24

API Cleanup

  • tf.contrib → TF Addons (https://github.com/tensorflow/addons)
  • tf.app, tf.flags, tf.logging is removed
  • No more Globals: tf.global_variables_initializer(), tf.variable_scope(), etc
  • Duplicated API cleanup
  • Parameter name and order compatible with numpy (e.g dim → axis)
  • Support lecay API at tf.compat.v1

15 of 24

Upgrade to 2.0

  • tf_upgrade_v2 --infile tensorfoo.py --outfile tensorfoo-upgraded.py�tf_upgrade_v2.py --intree ~/code/old --outtree ~/code/new
  • tf.layers → tf.keras.layers
  • tf.contrib.layers → tf.layers → tf.keras.layers
  • https://www.tensorflow.org/beta/guide/migration_guide

import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()

16 of 24

TensorFlow 2.0 Recommendation

  • Use Keras layers, models
  • Use small python function
  • @tf.function (AutoGraph)
  • tf.data.Datasets

17 of 24

Google I/O ’19 highlights

18 of 24

Keras Tuner

  • Automatic Hyperparameter Searching (AutoML)
  • github.com/keras-team/keras-tuner

19 of 24

Keras Tuner

  • Automatic Hyperparameter Searching (AutoML)
  • github.com/keras-team/keras-tuner

20 of 24

On-Device ML

21 of 24

Google Coral

  • https://coral.withgoogle.com/

USB Accelerator

Dev Board

Edge TPU

22 of 24

Coral Demo

23 of 24

USB Accelerator + Raspberry Pi

  • Face Detection with MobileNet SSD v2

+

Live Demo !

24 of 24

감사합니다