<jupyter_start><jupyter_text>Load the datasets<jupyter_code>FILE_PATH = "C:\\Users\\quanh\\Desktop\\datasets\\06\\driving_log.csv"
df = pd.read_csv(FILE_PATH,
                names=['center','left','right','steering','throttle','break','speed'], 
                header=None)
df.count()<jupyter_output><empty_output><jupyter_text>Preprocess the dataset  Visualize the histogram of steering angle<jupyter_code>df.hist(column='steering', bins=20)<jupyter_output><empty_output><jupyter_text>Drop 95% of the 0 steering<jupyter_code>df = df.drop(df.query('steering == 0').sample(frac=.95).index) # Drop 95% of 0 steering<jupyter_output><empty_output><jupyter_text>Visualize the final dataset<jupyter_code>df.hist(column='steering', bins=20)
df.count()<jupyter_output><empty_output><jupyter_text>Prepare the dataset<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(df['center'], df['steering'], test_size=0.2)
train_data = X_train.tolist()
train_labels = y_train.tolist()
test_data = X_test.tolist()
test_labels = y_test.tolist()
print("Num of train data", len(train_data))
print("Num of test data", len(test_data))
print(train_data[10])
print(train_labels[10])
IMAGE_SIZE=64<jupyter_output><empty_output><jupyter_text>Create the input functions<jupyter_code>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

def _preprocessing(image,label, is_training):
    with tf.device('/cpu:0'):
        image = tf.image.crop_to_bounding_box(image, 60, 0, 100, 320)
        if is_training:
#             image = tf.image.random_brightness(image, max_delta=0.35)
#             image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
#             image = tf.image.random_hue(image, max_delta=0.2)
#             image = tf.image.random_contrast(image, lower=0.5, upper=1.5)

            flip = tf.random_uniform([1], -1, 1)[0] > 0
            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
def get_input_fn(list_data, list_labels, mode=tf.estimator.ModeKeys.EVAL, 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])
        features = {"x": images}
        return features, labels
        
    return _input_fn
train_input_fn = get_input_fn(train_data, train_labels, tf.estimator.ModeKeys.TRAIN)
test_input_fn = get_input_fn(test_data, test_labels, tf.estimator.ModeKeys.EVAL)
images, labels = train_input_fn()
with tf.Session() as sess:   
    for i in range(10):
        images_batch, labels_batch = sess.run([images, labels])
        print("images_batch size", images_batch['x'].shape)
        print("labels_batch size", labels_batch.shape)
        cur_image = images_batch['x'][0]
        print("labels_batch[0]", labels_batch[0])
        # Scale to 0, 1 for visualization
        cur_image = (cur_image - np.max(cur_image))/np.ptp(cur_image) + 1.0
        plt.imshow(cur_image.astype(np.float32))
        # Show the images_batch[0]
        # plt.imshow(images_batch[0])
        plt.show()<jupyter_output>images_batch size (64, 64, 64, 3)
labels_batch size (64,)
labels_batch[0] -0.25<jupyter_text>Create model function<jupyter_code>def my_model_fn(features, labels, mode):
    """Model function for our CNN"""
    
    net = tf.reshape(features['x'], [-1, IMAGE_SIZE, IMAGE_SIZE, 3])
    
    filters_size= [32, 64, 64, 128, 256]
    #filters_size= [32, 64, 128, 256]
    for i in range(len(filters_size)):
        net = tf.layers.conv2d(
            inputs=net,
            filters=filters_size[i],
            kernel_size=[3, 3],
            padding="same",
            activation=tf.nn.relu
        )
        net = tf.layers.batch_normalization(net, training=mode == tf.estimator.ModeKeys.TRAIN)
        net = tf.layers.max_pooling2d(
            inputs=net,
            pool_size=[2, 2],
            strides=[2,2]
        )        
    net = tf.layers.flatten(net)
    net = tf.layers.batch_normalization(net, training=mode == tf.estimator.ModeKeys.TRAIN)
    net = tf.layers.dropout(inputs=net, training=mode == tf.estimator.ModeKeys.TRAIN)
    net = tf.layers.dense(inputs=net, units=256)
#    net = tf.layers.batch_normalization(net, training=mode == tf.estimator.ModeKeys.TRAIN)
    net = tf.layers.dense(inputs=net, units=32)
#   net = tf.layers.batch_normalization(net, training=mode == tf.estimator.ModeKeys.TRAIN)
    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 )
    
    rmse = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(labels, output))), name="rmse_tensor")
    tf.summary.scalar('rmse', rmse)
    
    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)
my_estimator = tf.estimator.Estimator(
    model_fn=my_model_fn,
    model_dir="E:\\temp\\chapter_04\\v3"
)
tensors_to_log = {"rmse": "rmse_tensor"}
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=100)

train_spec = tf.estimator.TrainSpec(
    input_fn=train_input_fn,
    max_steps=35000,
    hooks=[logging_hook]
)

test_spec = tf.estimator.EvalSpec(
    input_fn=test_input_fn,
    steps=50, throttle_secs=60
)
tf.estimator.train_and_evaluate(my_estimator, train_spec, test_spec)<jupyter_output>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:Restoring parameters from E:\temp\chapter_04\v3\model.ckpt-25000
INFO:tensorflow:Saving checkpoints for 25001 into E:\temp\chapter_04\v3\model.ckpt.
INFO:tensorflow:rmse = 0.0327149
INFO:tensorflow:loss = 0.00107027, step = 25001
INFO:tensorflow:global_step/sec: 19.0224
INFO:tensorflow:rmse = 0.0352703 (5.260 sec)
INFO:tensorflow:loss = 0.00124399, step = 25101 (5.260 sec)
INFO:tensorflow:global_step/sec: 19.2203
INFO:tensorflow:rmse = 0.0636799 (5.202 sec)
INFO:tensorflow:loss = 0.00405513, step = 25201 (5.202 sec)
INFO:tensorflow:global_step/sec: 18.2764
INFO:tensorflow:rmse = 0.0417126 (5.473 sec)
INFO:tensorflow:loss = 0.00173994, step = 25301 (5.473 sec)
INFO:tensorflow:global_step/sec: 18.3402
INFO:tensorflow:rms[...]<jupyter_text>Export model<jupyter_code>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\\chapter_04\\v3\\export", 
                               serving_input_receiver_fn=serving_input_fn,
                               checkpoint_path="E:\\temp\\chapter_04\\v3\\model.ckpt-35000"
                              )<jupyter_output>INFO:tensorflow:Restoring parameters from E:\temp\chapter_04\v3\model.ckpt-35000
INFO:tensorflow:Assets added to graph.
INFO:tensorflow:No assets to write.
INFO:tensorflow:SavedModel written to: b"E:\\temp\\chapter_04\\v3\\export\\temp-b'1516540917'\\saved_model.pb"<jupyter_text>Predict new sample<jupyter_code>import os
export_dir = "E:\\temp\\chapter_04\\v1\\export"
saved_model_dir = os.path.join(export_dir, os.listdir(export_dir)[-1]) 
print(saved_model_dir)
predictor_fn = tf.contrib.predictor.from_saved_model(
  export_dir = saved_model_dir,
  signature_def_key='predictions')
import numpy as np
import cv2

predict_image = cv2.imread(test_data[0]).astype(np.float32)
predict_image = np.expand_dims(predict_image, axis=0)
print("predict_image", predict_image.shape)
output = predictor_fn(
  {
    'images': predict_image,
  }
)
print(output)<jupyter_output>{'output': -0.30280831}
{'fraction_non_alphanumeric': 0.12777777777777777, 'fraction_numerical': 0.03734567901234568, 'mean_word_length': 3.850798403193613, 'pattern_counts': {'":': 4, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '293004', 'license': 'MIT', 'n_tokens_mistral': 3643, 'n_tokens_neox': 3371, 'n_words': 556, 'path': 'Chapter04/Self Driving Car - Behavior Cloning.ipynb', 'repo_name': 'amirziai/Hands-on-Artificial-Intelligence-with-TensorFlow-Book', 'size': '9720'}