In [1]:
import pandas as pd
import numpy
import tensorflow as tf
import matplotlib.pyplot as plt

In [2]:
FILE_PATH = "C:\\Users\\quanh\\Desktop\\datasets\\06\\driving_log.csv"

In [3]:
df = pd.read_csv(FILE_PATH,
                names=['center','left','right','steering','throttle','break','speed'], header=None)

In [4]:
df.columns


Out[4]:
Index(['center', 'left', 'right', 'steering', 'throttle', 'break', 'speed'], dtype='object')

In [5]:
df.count()


Out[5]:
center      8012
left        8012
right       8012
steering    8012
throttle    8012
break       8012
speed       8012
dtype: int64

In [6]:
df.head(10)


Out[6]:
center left right steering throttle break speed
0 C:\Users\quanh\Desktop\datasets\06\IMG\center_... C:\Users\quanh\Desktop\datasets\06\IMG\left_20... C:\Users\quanh\Desktop\datasets\06\IMG\right_2... 0.0 0.0 0 0.000008
1 C:\Users\quanh\Desktop\datasets\06\IMG\center_... C:\Users\quanh\Desktop\datasets\06\IMG\left_20... C:\Users\quanh\Desktop\datasets\06\IMG\right_2... 0.0 0.0 0 0.000009
2 C:\Users\quanh\Desktop\datasets\06\IMG\center_... C:\Users\quanh\Desktop\datasets\06\IMG\left_20... C:\Users\quanh\Desktop\datasets\06\IMG\right_2... 0.0 0.0 0 0.000010
3 C:\Users\quanh\Desktop\datasets\06\IMG\center_... C:\Users\quanh\Desktop\datasets\06\IMG\left_20... C:\Users\quanh\Desktop\datasets\06\IMG\right_2... 0.0 0.0 0 0.000008
4 C:\Users\quanh\Desktop\datasets\06\IMG\center_... C:\Users\quanh\Desktop\datasets\06\IMG\left_20... C:\Users\quanh\Desktop\datasets\06\IMG\right_2... 0.0 0.0 0 0.000008
5 C:\Users\quanh\Desktop\datasets\06\IMG\center_... C:\Users\quanh\Desktop\datasets\06\IMG\left_20... C:\Users\quanh\Desktop\datasets\06\IMG\right_2... 0.0 0.0 0 0.000009
6 C:\Users\quanh\Desktop\datasets\06\IMG\center_... C:\Users\quanh\Desktop\datasets\06\IMG\left_20... C:\Users\quanh\Desktop\datasets\06\IMG\right_2... 0.0 0.0 0 0.000009
7 C:\Users\quanh\Desktop\datasets\06\IMG\center_... C:\Users\quanh\Desktop\datasets\06\IMG\left_20... C:\Users\quanh\Desktop\datasets\06\IMG\right_2... 0.0 0.0 0 0.000013
8 C:\Users\quanh\Desktop\datasets\06\IMG\center_... C:\Users\quanh\Desktop\datasets\06\IMG\left_20... C:\Users\quanh\Desktop\datasets\06\IMG\right_2... 0.0 0.0 0 0.000002
9 C:\Users\quanh\Desktop\datasets\06\IMG\center_... C:\Users\quanh\Desktop\datasets\06\IMG\left_20... C:\Users\quanh\Desktop\datasets\06\IMG\right_2... 0.0 0.0 0 0.000006

In [7]:
df.hist(column='steering', bins=20)


Out[7]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x00000228046F6160>]], dtype=object)

In [8]:
df = df.drop(df.query('steering == 0').sample(frac=.95).index) # Drop 95% of 0 steering

In [9]:
df.hist(column='steering', bins=20)


Out[9]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0000022804A0B208>]], dtype=object)

In [10]:
df.count()


Out[10]:
center      1916
left        1916
right       1916
steering    1916
throttle    1916
break       1916
speed       1916
dtype: int64

In [11]:
train_data = df['center'].tolist()
train_labels = df['steering'].tolist()
print(len(train_data))
print(len(train_labels))
print(train_data[10])
print(train_labels[10])


1916
1916
C:\Users\quanh\Desktop\datasets\06\IMG\center_2018_01_15_03_03_04_339.jpg
0.009389671

In [12]:
IMAGE_SIZE=64

In [13]:
def _parse_function(filename, label):
    with tf.device('/cpu:0'):
        image_string = tf.read_file(filename)
        image_decoded = tf.image.decode_jpeg(image_string)
    return image_decoded, label

In [14]:
def _preprocessing(image,label, is_training):
    with tf.device('/cpu:0'):
        flip = tf.random_uniform([1], -1, 1)[0] > 0
        image = tf.image.crop_to_bounding_box(image, 60, 0, 100, 320)
        image = tf.cond(flip, lambda: tf.image.flip_left_right(image), lambda: image)
        label = tf.cond(flip, lambda: -label, lambda: label)
        image = tf.image.per_image_standardization(image)
    return image, label

In [15]:
def get_input_fn(list_data, list_labels, mode=tf.estimator.ModeKeys.TRAIN, batch_size=64):
    def _input_fn():
        
        is_training = (mode == tf.estimator.ModeKeys.TRAIN)
        dataset = tf.data.Dataset.from_tensor_slices((list_data, list_labels)) 

        dataset = dataset.shuffle(buffer_size= 2 * batch_size + 1)
        
        dataset = dataset.map(_parse_function, num_parallel_calls=4)
        
        dataset = dataset.map(lambda image, label: (_preprocessing(image, label, is_training)), num_parallel_calls=8)
        
        dataset = dataset.prefetch(batch_size * 4 + 1)
        
        dataset = dataset.repeat()
        
        dataset = dataset.batch(batch_size)
        
        iterator = dataset.make_one_shot_iterator()
        
        images, labels = iterator.get_next()
        images = tf.image.resize_images(images, [IMAGE_SIZE, IMAGE_SIZE])
        return images, labels
        
    return _input_fn

In [16]:
train_input_fn = get_input_fn(train_data, train_labels)
test_input_fn = get_input_fn(train_data, train_labels, tf.estimator.ModeKeys.EVAL)

In [17]:
images, labels = train_input_fn()
with tf.Session() as sess:
    for i in range(1):
        images_batch, labels_batch = sess.run([images, labels])
        print("images_batch size", images_batch.shape)
        print("labels_batch size", labels_batch.shape)
        print("labels_batch[0]", labels_batch[0])
        plt.imshow(images_batch[0])
        plt.show()


images_batch size (64, 64, 64, 3)
labels_batch size (64,)
labels_batch[0] -0.028169

In [129]:
def my_model_fn(features, labels, mode):
    """Model function for our CNN"""
    
    net = tf.reshape(features['x'], [-1, IMAGE_SIZE, IMAGE_SIZE, 3])
    
    for _ in range(3):
        net = tf.layers.conv2d(
            inputs=net,
            filters=32,
            kernel_size=[5, 5],
            padding="same",
            activation=tf.nn.relu
        )
        net = tf.layers.max_pooling2d(
            inputs=net,
            pool_size=[2, 2],
            strides=[2,2]
        )
    for _ in range(2):
        net = tf.layers.conv2d(
            inputs=net,
            filters=64,
            kernel_size=[3, 3],
            padding="same",
            activation=tf.nn.relu
        )
        net = tf.layers.max_pooling2d(
            inputs=net,
            pool_size=[2, 2],
            strides=[2,2]
        )        
    net = tf.layers.flatten(net)
    net = tf.layers.dense(inputs=net, units=512)
    net = tf.layers.dense(inputs=net, units=128)
    net = tf.layers.dense(inputs=net, units=10)
    logits = tf.layers.dense(inputs=net, units=1) # only steering angle
    output = tf.squeeze(logits)
    
    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {
            "output": output,
        }
        export_outputs = {
            'predictions': tf.estimator.export.PredictOutput(predictions)
        }
        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, export_outputs=export_outputs )
    
    loss = tf.losses.mean_squared_error(labels, output)
    
    optimizer = tf.train.AdamOptimizer()
    train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
    
    eval_metric_ops = {
        "rmse": tf.metrics.root_mean_squared_error(labels, output)
    }
    return tf.estimator.EstimatorSpec(
        mode=mode, 
        loss=loss, 
        train_op=train_op,
        eval_metric_ops=eval_metric_ops)

In [130]:
my_estimator = tf.estimator.Estimator(
    model_fn=my_model_fn,
    model_dir="E:\\temp\\my_estimator"
)


INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_num_ps_replicas': 0, '_tf_random_seed': None, '_is_chief': True, '_task_id': 0, '_task_type': 'worker', '_keep_checkpoint_max': 5, '_num_worker_replicas': 1, '_keep_checkpoint_every_n_hours': 10000, '_session_config': None, '_service': None, '_master': '', '_save_checkpoints_secs': 600, '_model_dir': 'E:\\temp\\my_estimator', '_save_summary_steps': 100, '_log_step_count_steps': 100, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x0000022A0FD785F8>, '_save_checkpoints_steps': None}

In [20]:
train_spec = tf.estimator.TrainSpec(
    input_fn=train_input_fn,
    max_steps=5000
)

test_spec = tf.estimator.EvalSpec(
    input_fn=test_input_fn,
    steps=50, throttle_secs=60
)

In [21]:
tf.estimator.train_and_evaluate(my_estimator, train_spec, test_spec)


INFO:tensorflow:Running training and evaluation locally (non-distributed).
INFO:tensorflow:Start train and evaluate loop. The evaluate will happen after 60 secs (eval_spec.throttle_secs) or training is finished.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into E:\temp\my_estimator\model.ckpt.
INFO:tensorflow:step = 1, loss = 0.584787
INFO:tensorflow:global_step/sec: 17.0749
INFO:tensorflow:step = 101, loss = 0.0395434 (5.861 sec)
INFO:tensorflow:global_step/sec: 19.6134
INFO:tensorflow:step = 201, loss = 0.0494057 (5.098 sec)
INFO:tensorflow:global_step/sec: 19.5213
INFO:tensorflow:step = 301, loss = 0.0267567 (5.126 sec)
INFO:tensorflow:global_step/sec: 19.3772
INFO:tensorflow:step = 401, loss = 0.0373968 (5.157 sec)
INFO:tensorflow:global_step/sec: 19.4679
INFO:tensorflow:step = 501, loss = 0.0319365 (5.140 sec)
INFO:tensorflow:global_step/sec: 19.5251
INFO:tensorflow:step = 601, loss = 0.0291568 (5.121 sec)
INFO:tensorflow:global_step/sec: 19.7181
INFO:tensorflow:step = 701, loss = 0.0268002 (5.069 sec)
INFO:tensorflow:global_step/sec: 19.6559
INFO:tensorflow:step = 801, loss = 0.0221703 (5.092 sec)
INFO:tensorflow:global_step/sec: 19.187
INFO:tensorflow:step = 901, loss = 0.0187486 (5.208 sec)
INFO:tensorflow:global_step/sec: 19.4793
INFO:tensorflow:step = 1001, loss = 0.0136613 (5.134 sec)
INFO:tensorflow:Saving checkpoints for 1045 into E:\temp\my_estimator\model.ckpt.
INFO:tensorflow:Loss for final step: 0.0157893.
INFO:tensorflow:Starting evaluation at 2018-01-15-14:15:50
INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-1045
INFO:tensorflow:Evaluation [1/50]
INFO:tensorflow:Evaluation [2/50]
INFO:tensorflow:Evaluation [3/50]
INFO:tensorflow:Evaluation [4/50]
INFO:tensorflow:Evaluation [5/50]
INFO:tensorflow:Evaluation [6/50]
INFO:tensorflow:Evaluation [7/50]
INFO:tensorflow:Evaluation [8/50]
INFO:tensorflow:Evaluation [9/50]
INFO:tensorflow:Evaluation [10/50]
INFO:tensorflow:Evaluation [11/50]
INFO:tensorflow:Evaluation [12/50]
INFO:tensorflow:Evaluation [13/50]
INFO:tensorflow:Evaluation [14/50]
INFO:tensorflow:Evaluation [15/50]
INFO:tensorflow:Evaluation [16/50]
INFO:tensorflow:Evaluation [17/50]
INFO:tensorflow:Evaluation [18/50]
INFO:tensorflow:Evaluation [19/50]
INFO:tensorflow:Evaluation [20/50]
INFO:tensorflow:Evaluation [21/50]
INFO:tensorflow:Evaluation [22/50]
INFO:tensorflow:Evaluation [23/50]
INFO:tensorflow:Evaluation [24/50]
INFO:tensorflow:Evaluation [25/50]
INFO:tensorflow:Evaluation [26/50]
INFO:tensorflow:Evaluation [27/50]
INFO:tensorflow:Evaluation [28/50]
INFO:tensorflow:Evaluation [29/50]
INFO:tensorflow:Evaluation [30/50]
INFO:tensorflow:Evaluation [31/50]
INFO:tensorflow:Evaluation [32/50]
INFO:tensorflow:Evaluation [33/50]
INFO:tensorflow:Evaluation [34/50]
INFO:tensorflow:Evaluation [35/50]
INFO:tensorflow:Evaluation [36/50]
INFO:tensorflow:Evaluation [37/50]
INFO:tensorflow:Evaluation [38/50]
INFO:tensorflow:Evaluation [39/50]
INFO:tensorflow:Evaluation [40/50]
INFO:tensorflow:Evaluation [41/50]
INFO:tensorflow:Evaluation [42/50]
INFO:tensorflow:Evaluation [43/50]
INFO:tensorflow:Evaluation [44/50]
INFO:tensorflow:Evaluation [45/50]
INFO:tensorflow:Evaluation [46/50]
INFO:tensorflow:Evaluation [47/50]
INFO:tensorflow:Evaluation [48/50]
INFO:tensorflow:Evaluation [49/50]
INFO:tensorflow:Evaluation [50/50]
INFO:tensorflow:Finished evaluation at 2018-01-15-14:15:54
INFO:tensorflow:Saving dict for global step 1045: global_step = 1045, loss = 0.0175874, rmse = 0.132618
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-1045
INFO:tensorflow:Saving checkpoints for 1046 into E:\temp\my_estimator\model.ckpt.
INFO:tensorflow:step = 1046, loss = 0.0112453
INFO:tensorflow:global_step/sec: 19.1032
INFO:tensorflow:step = 1146, loss = 0.0160088 (5.238 sec)
INFO:tensorflow:global_step/sec: 19.3922
INFO:tensorflow:step = 1246, loss = 0.0128025 (5.157 sec)
INFO:tensorflow:global_step/sec: 19.6521
INFO:tensorflow:step = 1346, loss = 0.0138781 (5.091 sec)
INFO:tensorflow:global_step/sec: 19.3809
INFO:tensorflow:step = 1446, loss = 0.00933546 (5.159 sec)
INFO:tensorflow:global_step/sec: 19.5175
INFO:tensorflow:step = 1546, loss = 0.00723384 (5.122 sec)
INFO:tensorflow:global_step/sec: 19.1907
INFO:tensorflow:step = 1646, loss = 0.0138703 (5.209 sec)
INFO:tensorflow:global_step/sec: 19.291
INFO:tensorflow:step = 1746, loss = 0.00541171 (5.187 sec)
INFO:tensorflow:global_step/sec: 19.3246
INFO:tensorflow:step = 1846, loss = 0.00600682 (5.172 sec)
INFO:tensorflow:global_step/sec: 19.026
INFO:tensorflow:step = 1946, loss = 0.00684906 (5.286 sec)
INFO:tensorflow:global_step/sec: 18.3706
INFO:tensorflow:step = 2046, loss = 0.00660866 (5.415 sec)
INFO:tensorflow:Saving checkpoints for 2113 into E:\temp\my_estimator\model.ckpt.
INFO:tensorflow:Loss for final step: 0.0054053.
INFO:tensorflow:Starting evaluation at 2018-01-15-14:16:58
INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-2113
INFO:tensorflow:Evaluation [1/50]
INFO:tensorflow:Evaluation [2/50]
INFO:tensorflow:Evaluation [3/50]
INFO:tensorflow:Evaluation [4/50]
INFO:tensorflow:Evaluation [5/50]
INFO:tensorflow:Evaluation [6/50]
INFO:tensorflow:Evaluation [7/50]
INFO:tensorflow:Evaluation [8/50]
INFO:tensorflow:Evaluation [9/50]
INFO:tensorflow:Evaluation [10/50]
INFO:tensorflow:Evaluation [11/50]
INFO:tensorflow:Evaluation [12/50]
INFO:tensorflow:Evaluation [13/50]
INFO:tensorflow:Evaluation [14/50]
INFO:tensorflow:Evaluation [15/50]
INFO:tensorflow:Evaluation [16/50]
INFO:tensorflow:Evaluation [17/50]
INFO:tensorflow:Evaluation [18/50]
INFO:tensorflow:Evaluation [19/50]
INFO:tensorflow:Evaluation [20/50]
INFO:tensorflow:Evaluation [21/50]
INFO:tensorflow:Evaluation [22/50]
INFO:tensorflow:Evaluation [23/50]
INFO:tensorflow:Evaluation [24/50]
INFO:tensorflow:Evaluation [25/50]
INFO:tensorflow:Evaluation [26/50]
INFO:tensorflow:Evaluation [27/50]
INFO:tensorflow:Evaluation [28/50]
INFO:tensorflow:Evaluation [29/50]
INFO:tensorflow:Evaluation [30/50]
INFO:tensorflow:Evaluation [31/50]
INFO:tensorflow:Evaluation [32/50]
INFO:tensorflow:Evaluation [33/50]
INFO:tensorflow:Evaluation [34/50]
INFO:tensorflow:Evaluation [35/50]
INFO:tensorflow:Evaluation [36/50]
INFO:tensorflow:Evaluation [37/50]
INFO:tensorflow:Evaluation [38/50]
INFO:tensorflow:Evaluation [39/50]
INFO:tensorflow:Evaluation [40/50]
INFO:tensorflow:Evaluation [41/50]
INFO:tensorflow:Evaluation [42/50]
INFO:tensorflow:Evaluation [43/50]
INFO:tensorflow:Evaluation [44/50]
INFO:tensorflow:Evaluation [45/50]
INFO:tensorflow:Evaluation [46/50]
INFO:tensorflow:Evaluation [47/50]
INFO:tensorflow:Evaluation [48/50]
INFO:tensorflow:Evaluation [49/50]
INFO:tensorflow:Evaluation [50/50]
INFO:tensorflow:Finished evaluation at 2018-01-15-14:17:02
INFO:tensorflow:Saving dict for global step 2113: global_step = 2113, loss = 0.00487257, rmse = 0.0698038
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-2113
INFO:tensorflow:Saving checkpoints for 2114 into E:\temp\my_estimator\model.ckpt.
INFO:tensorflow:step = 2114, loss = 0.00457899
INFO:tensorflow:global_step/sec: 17.5681
INFO:tensorflow:step = 2214, loss = 0.00491493 (5.697 sec)
INFO:tensorflow:global_step/sec: 18.6943
INFO:tensorflow:step = 2314, loss = 0.00328933 (5.346 sec)
INFO:tensorflow:global_step/sec: 18.6594
INFO:tensorflow:step = 2414, loss = 0.0022422 (5.363 sec)
INFO:tensorflow:global_step/sec: 17.3391
INFO:tensorflow:step = 2514, loss = 0.00266153 (5.772 sec)
INFO:tensorflow:global_step/sec: 17.0108
INFO:tensorflow:step = 2614, loss = 0.0025551 (5.872 sec)
INFO:tensorflow:global_step/sec: 18.1072
INFO:tensorflow:step = 2714, loss = 0.00915247 (5.522 sec)
INFO:tensorflow:global_step/sec: 19.2798
INFO:tensorflow:step = 2814, loss = 0.0054006 (5.189 sec)
INFO:tensorflow:global_step/sec: 19.5557
INFO:tensorflow:step = 2914, loss = 0.00265075 (5.117 sec)
INFO:tensorflow:global_step/sec: 19.1686
INFO:tensorflow:step = 3014, loss = 0.00326275 (5.214 sec)
INFO:tensorflow:global_step/sec: 18.9861
INFO:tensorflow:step = 3114, loss = 0.0018738 (5.268 sec)
INFO:tensorflow:Saving checkpoints for 3135 into E:\temp\my_estimator\model.ckpt.
INFO:tensorflow:Loss for final step: 0.00276522.
INFO:tensorflow:Starting evaluation at 2018-01-15-14:18:05
INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-3135
INFO:tensorflow:Evaluation [1/50]
INFO:tensorflow:Evaluation [2/50]
INFO:tensorflow:Evaluation [3/50]
INFO:tensorflow:Evaluation [4/50]
INFO:tensorflow:Evaluation [5/50]
INFO:tensorflow:Evaluation [6/50]
INFO:tensorflow:Evaluation [7/50]
INFO:tensorflow:Evaluation [8/50]
INFO:tensorflow:Evaluation [9/50]
INFO:tensorflow:Evaluation [10/50]
INFO:tensorflow:Evaluation [11/50]
INFO:tensorflow:Evaluation [12/50]
INFO:tensorflow:Evaluation [13/50]
INFO:tensorflow:Evaluation [14/50]
INFO:tensorflow:Evaluation [15/50]
INFO:tensorflow:Evaluation [16/50]
INFO:tensorflow:Evaluation [17/50]
INFO:tensorflow:Evaluation [18/50]
INFO:tensorflow:Evaluation [19/50]
INFO:tensorflow:Evaluation [20/50]
INFO:tensorflow:Evaluation [21/50]
INFO:tensorflow:Evaluation [22/50]
INFO:tensorflow:Evaluation [23/50]
INFO:tensorflow:Evaluation [24/50]
INFO:tensorflow:Evaluation [25/50]
INFO:tensorflow:Evaluation [26/50]
INFO:tensorflow:Evaluation [27/50]
INFO:tensorflow:Evaluation [28/50]
INFO:tensorflow:Evaluation [29/50]
INFO:tensorflow:Evaluation [30/50]
INFO:tensorflow:Evaluation [31/50]
INFO:tensorflow:Evaluation [32/50]
INFO:tensorflow:Evaluation [33/50]
INFO:tensorflow:Evaluation [34/50]
INFO:tensorflow:Evaluation [35/50]
INFO:tensorflow:Evaluation [36/50]
INFO:tensorflow:Evaluation [37/50]
INFO:tensorflow:Evaluation [38/50]
INFO:tensorflow:Evaluation [39/50]
INFO:tensorflow:Evaluation [40/50]
INFO:tensorflow:Evaluation [41/50]
INFO:tensorflow:Evaluation [42/50]
INFO:tensorflow:Evaluation [43/50]
INFO:tensorflow:Evaluation [44/50]
INFO:tensorflow:Evaluation [45/50]
INFO:tensorflow:Evaluation [46/50]
INFO:tensorflow:Evaluation [47/50]
INFO:tensorflow:Evaluation [48/50]
INFO:tensorflow:Evaluation [49/50]
INFO:tensorflow:Evaluation [50/50]
INFO:tensorflow:Finished evaluation at 2018-01-15-14:18:09
INFO:tensorflow:Saving dict for global step 3135: global_step = 3135, loss = 0.0025126, rmse = 0.0501258
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-3135
INFO:tensorflow:Saving checkpoints for 3136 into E:\temp\my_estimator\model.ckpt.
INFO:tensorflow:step = 3136, loss = 0.00117879
INFO:tensorflow:global_step/sec: 19.1649
INFO:tensorflow:step = 3236, loss = 0.00254358 (5.223 sec)
INFO:tensorflow:global_step/sec: 18.6315
INFO:tensorflow:step = 3336, loss = 0.00233618 (5.368 sec)
INFO:tensorflow:global_step/sec: 16.5589
INFO:tensorflow:step = 3436, loss = 0.00112982 (6.035 sec)
INFO:tensorflow:global_step/sec: 19.4755
INFO:tensorflow:step = 3536, loss = 0.00300638 (5.138 sec)
INFO:tensorflow:global_step/sec: 19.276
INFO:tensorflow:step = 3636, loss = 0.00297431 (5.185 sec)
INFO:tensorflow:global_step/sec: 18.9501
INFO:tensorflow:step = 3736, loss = 0.00427117 (5.282 sec)
INFO:tensorflow:global_step/sec: 19.6211
INFO:tensorflow:step = 3836, loss = 0.00234275 (5.112 sec)
INFO:tensorflow:global_step/sec: 19.2909
INFO:tensorflow:step = 3936, loss = 0.00155328 (5.166 sec)
INFO:tensorflow:global_step/sec: 19.8753
INFO:tensorflow:step = 4036, loss = 0.002166 (5.029 sec)
INFO:tensorflow:global_step/sec: 19.2426
INFO:tensorflow:step = 4136, loss = 0.00163108 (5.197 sec)
INFO:tensorflow:Saving checkpoints for 4190 into E:\temp\my_estimator\model.ckpt.
INFO:tensorflow:Loss for final step: 0.00254868.
INFO:tensorflow:Starting evaluation at 2018-01-15-14:19:12
INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-4190
INFO:tensorflow:Evaluation [1/50]
INFO:tensorflow:Evaluation [2/50]
INFO:tensorflow:Evaluation [3/50]
INFO:tensorflow:Evaluation [4/50]
INFO:tensorflow:Evaluation [5/50]
INFO:tensorflow:Evaluation [6/50]
INFO:tensorflow:Evaluation [7/50]
INFO:tensorflow:Evaluation [8/50]
INFO:tensorflow:Evaluation [9/50]
INFO:tensorflow:Evaluation [10/50]
INFO:tensorflow:Evaluation [11/50]
INFO:tensorflow:Evaluation [12/50]
INFO:tensorflow:Evaluation [13/50]
INFO:tensorflow:Evaluation [14/50]
INFO:tensorflow:Evaluation [15/50]
INFO:tensorflow:Evaluation [16/50]
INFO:tensorflow:Evaluation [17/50]
INFO:tensorflow:Evaluation [18/50]
INFO:tensorflow:Evaluation [19/50]
INFO:tensorflow:Evaluation [20/50]
INFO:tensorflow:Evaluation [21/50]
INFO:tensorflow:Evaluation [22/50]
INFO:tensorflow:Evaluation [23/50]
INFO:tensorflow:Evaluation [24/50]
INFO:tensorflow:Evaluation [25/50]
INFO:tensorflow:Evaluation [26/50]
INFO:tensorflow:Evaluation [27/50]
INFO:tensorflow:Evaluation [28/50]
INFO:tensorflow:Evaluation [29/50]
INFO:tensorflow:Evaluation [30/50]
INFO:tensorflow:Evaluation [31/50]
INFO:tensorflow:Evaluation [32/50]
INFO:tensorflow:Evaluation [33/50]
INFO:tensorflow:Evaluation [34/50]
INFO:tensorflow:Evaluation [35/50]
INFO:tensorflow:Evaluation [36/50]
INFO:tensorflow:Evaluation [37/50]
INFO:tensorflow:Evaluation [38/50]
INFO:tensorflow:Evaluation [39/50]
INFO:tensorflow:Evaluation [40/50]
INFO:tensorflow:Evaluation [41/50]
INFO:tensorflow:Evaluation [42/50]
INFO:tensorflow:Evaluation [43/50]
INFO:tensorflow:Evaluation [44/50]
INFO:tensorflow:Evaluation [45/50]
INFO:tensorflow:Evaluation [46/50]
INFO:tensorflow:Evaluation [47/50]
INFO:tensorflow:Evaluation [48/50]
INFO:tensorflow:Evaluation [49/50]
INFO:tensorflow:Evaluation [50/50]
INFO:tensorflow:Finished evaluation at 2018-01-15-14:19:16
INFO:tensorflow:Saving dict for global step 4190: global_step = 4190, loss = 0.00242326, rmse = 0.0492266
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-4190
INFO:tensorflow:Saving checkpoints for 4191 into E:\temp\my_estimator\model.ckpt.
INFO:tensorflow:step = 4191, loss = 0.00164028
INFO:tensorflow:global_step/sec: 19.2055
INFO:tensorflow:step = 4291, loss = 0.00198752 (5.208 sec)
INFO:tensorflow:global_step/sec: 19.5328
INFO:tensorflow:step = 4391, loss = 0.00235546 (5.122 sec)
INFO:tensorflow:global_step/sec: 19.4641
INFO:tensorflow:step = 4491, loss = 0.00386377 (5.141 sec)
INFO:tensorflow:global_step/sec: 19.6019
INFO:tensorflow:step = 4591, loss = 0.0019706 (5.097 sec)
INFO:tensorflow:global_step/sec: 19.5251
INFO:tensorflow:step = 4691, loss = 0.00135884 (5.122 sec)
INFO:tensorflow:global_step/sec: 19.4641
INFO:tensorflow:step = 4791, loss = 0.00226441 (5.138 sec)
INFO:tensorflow:global_step/sec: 19.6559
INFO:tensorflow:step = 4891, loss = 0.00227162 (5.089 sec)
INFO:tensorflow:global_step/sec: 19.8793
INFO:tensorflow:step = 4991, loss = 0.00201185 (5.028 sec)
INFO:tensorflow:Saving checkpoints for 5000 into E:\temp\my_estimator\model.ckpt.
INFO:tensorflow:Loss for final step: 0.00196182.
INFO:tensorflow:Starting evaluation at 2018-01-15-14:20:05
INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-5000
INFO:tensorflow:Evaluation [1/50]
INFO:tensorflow:Evaluation [2/50]
INFO:tensorflow:Evaluation [3/50]
INFO:tensorflow:Evaluation [4/50]
INFO:tensorflow:Evaluation [5/50]
INFO:tensorflow:Evaluation [6/50]
INFO:tensorflow:Evaluation [7/50]
INFO:tensorflow:Evaluation [8/50]
INFO:tensorflow:Evaluation [9/50]
INFO:tensorflow:Evaluation [10/50]
INFO:tensorflow:Evaluation [11/50]
INFO:tensorflow:Evaluation [12/50]
INFO:tensorflow:Evaluation [13/50]
INFO:tensorflow:Evaluation [14/50]
INFO:tensorflow:Evaluation [15/50]
INFO:tensorflow:Evaluation [16/50]
INFO:tensorflow:Evaluation [17/50]
INFO:tensorflow:Evaluation [18/50]
INFO:tensorflow:Evaluation [19/50]
INFO:tensorflow:Evaluation [20/50]
INFO:tensorflow:Evaluation [21/50]
INFO:tensorflow:Evaluation [22/50]
INFO:tensorflow:Evaluation [23/50]
INFO:tensorflow:Evaluation [24/50]
INFO:tensorflow:Evaluation [25/50]
INFO:tensorflow:Evaluation [26/50]
INFO:tensorflow:Evaluation [27/50]
INFO:tensorflow:Evaluation [28/50]
INFO:tensorflow:Evaluation [29/50]
INFO:tensorflow:Evaluation [30/50]
INFO:tensorflow:Evaluation [31/50]
INFO:tensorflow:Evaluation [32/50]
INFO:tensorflow:Evaluation [33/50]
INFO:tensorflow:Evaluation [34/50]
INFO:tensorflow:Evaluation [35/50]
INFO:tensorflow:Evaluation [36/50]
INFO:tensorflow:Evaluation [37/50]
INFO:tensorflow:Evaluation [38/50]
INFO:tensorflow:Evaluation [39/50]
INFO:tensorflow:Evaluation [40/50]
INFO:tensorflow:Evaluation [41/50]
INFO:tensorflow:Evaluation [42/50]
INFO:tensorflow:Evaluation [43/50]
INFO:tensorflow:Evaluation [44/50]
INFO:tensorflow:Evaluation [45/50]
INFO:tensorflow:Evaluation [46/50]
INFO:tensorflow:Evaluation [47/50]
INFO:tensorflow:Evaluation [48/50]
INFO:tensorflow:Evaluation [49/50]
INFO:tensorflow:Evaluation [50/50]
INFO:tensorflow:Finished evaluation at 2018-01-15-14:20:09
INFO:tensorflow:Saving dict for global step 5000: global_step = 5000, loss = 0.00357471, rmse = 0.0597889

In [156]:
def preprocess_image(image):
    """Preprocess a single image of layout [height, width, depth]."""
    image = tf.image.crop_to_bounding_box(image, 60, 0, 100, 320)
    image = tf.image.per_image_standardization(image)
    images = tf.image.resize_images(tf.expand_dims(image, 0), [IMAGE_SIZE, IMAGE_SIZE])
    image = tf.squeeze(images)
    return image

def serving_input_fn():
    receiver_tensor = {'images': tf.placeholder(shape=[None, 160, 320, 3], dtype=tf.float32)}
    features = {'x': tf.map_fn(preprocess_image, receiver_tensor['images'])}

    return tf.estimator.export.ServingInputReceiver(features, receiver_tensor)
my_estimator.export_savedmodel("E:\\temp\\my_estimator\\export", serving_input_receiver_fn=serving_input_fn)


INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-5000
INFO:tensorflow:Assets added to graph.
INFO:tensorflow:No assets to write.
INFO:tensorflow:SavedModel written to: b"E:\\temp\\my_estimator\\export\\temp-b'1516032179'\\saved_model.pb"
Out[156]:
b'E:\\temp\\my_estimator\\export\\1516032179'

In [157]:
import os
export_dir = "E:\\temp\\my_estimator\\export"
saved_model_dir = os.path.join(export_dir, os.listdir(export_dir)[-1]) 
print(saved_model_dir)


E:\temp\my_estimator\export\1516032179

In [158]:
predictor_fn = tf.contrib.predictor.from_saved_model(
  export_dir = saved_model_dir,
  signature_def_key='predictions')


INFO:tensorflow:Restoring parameters from b'E:\\temp\\my_estimator\\export\\1516032179\\variables\\variables'

In [159]:
import numpy as np
import cv2

predict_image = cv2.imread(train_data[0]).astype(np.float32)
#predict_image = cv2.resize(predict_image, (64, 64)).astype(np.float32)
predict_image = np.expand_dims(predict_image, axis=0)
print("predict_image", predict_image.shape)


predict_image (1, 160, 320, 3)

In [160]:
output = predictor_fn(
  {
    'images': predict_image,
  }
)

In [161]:
print(output)


{'output': -0.13725133}

In [ ]:


In [38]:
predict_input_fn = get_input_fn(train_data[0:10], train_labels[0:10], tf.estimator.ModeKeys.PREDICT)

In [39]:
predictions = my_estimator.predict(input_fn=predict_input_fn)

In [35]:
predictions


Out[35]:
<generator object Estimator.predict at 0x00000229D6053C50>

In [40]:
outputs = []
N = 10

for i, output in enumerate(predictions):
    if i == N:
        break
    outputs.append(output['output'])
print(outputs)


WARNING:tensorflow:Input graph does not contain a QueueRunner. That means predict yields forever. This is probably a mistake.
INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-5000
[0.078390844, 0.053177118, 0.02075159, 0.040205192, 0.014565398, 0.06641838, 0.053918242, 0.037990037, -0.017086534, 0.073967986]

In [41]:
import itertools

In [47]:
list(itertools.islice(predictions, 1))[-1]['output']


Out[47]:
0.039633013

In [120]:
import numpy as np
import cv2

predict_image = cv2.imread(train_data[0])
predict_image = cv2.resize(predict_image, (64, 64)).astype(np.float32)
predict_image = np.expand_dims(predict_image, axis=0)
print("predict_image", predict_image.shape)


predict_image (1, 64, 64, 3)

In [121]:
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={'x':np.array(predict_image)},
    batch_size=1,
    num_epochs=1,
    shuffle=False)

In [122]:
predictions = my_estimator.predict(input_fn=predict_input_fn)

In [124]:
list(itertools.islice(predictions, 1))


INFO:tensorflow:Restoring parameters from E:\temp\my_estimator\model.ckpt-5000
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-124-6ff34445a568> in <module>()
----> 1 list(itertools.islice(predictions, 1))

E:\Miniconda3\envs\env3-gpu\lib\site-packages\tensorflow\python\estimator\estimator.py in predict(self, input_fn, predict_keys, hooks, checkpoint_path)
    423               yield pred
    424           else:
--> 425             for i in range(self._extract_batch_length(preds_evaluated)):
    426               yield {
    427                   key: value[i]

E:\Miniconda3\envs\env3-gpu\lib\site-packages\tensorflow\python\estimator\estimator.py in _extract_batch_length(self, preds_evaluated)
    587     batch_length = None
    588     for key, value in six.iteritems(preds_evaluated):
--> 589       batch_length = batch_length or value.shape[0]
    590       if value.shape[0] != batch_length:
    591         raise ValueError('Batch length of predictions should be same. %s has '

IndexError: tuple index out of range

In [ ]: