import pickle
import tensorflow as tf
import time
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from alexnet import AlexNet

number_of_classes = 43
BATCH_SIZE = 128
N_EPOCHS = 3

# TODO: Load traffic signs data.

with open('./train.p', 'rb') as f:
    data = pickle.load(f)
# note: data is a dictionary with keys: dict_keys(['coords', 'features', 'sizes', 'labels'])

# TODO: Split data into training and validation sets.
x_train, x_test, y_train, y_test = train_test_split(data['features'], data['labels'])

# TODO: Define placeholders and resize operation.
original_features_placeholder = tf.placeholder(tf.float32, (None, 32, 32, 3))
resized_features_placeholder = \
    tf.image.resize_images(original_features_placeholder, (227, 227))

labels_placeholder = tf.placeholder(tf.int64, None)

# TODO: pass placeholder as first argument to `AlexNet`.
fc7 = AlexNet(features=resized_features_placeholder, feature_extract=True)
# NOTE: `tf.stop_gradient` prevents the gradient from flowing backwards
# past this point, keeping the weights before and up to `fc7` frozen.
# This also makes training faster, less work to do!
fc7 = tf.stop_gradient(fc7)

# TODO: Add the final layer for traffic sign classification.
shape = (fc7.get_shape().as_list()[-1], number_of_classes)
fc8W = tf.Variable(tf.truncated_normal(shape=shape))
fc8b = tf.Variable(tf.zeros(shape=number_of_classes))
fc8_logits = tf.nn.xw_plus_b(x=fc7, weights=fc8W, biases=fc8b)

# TODO: Define loss, training, accuracy operations.
# HINT: Look back at your traffic signs project solution, you may
# be able to reuse some the code.
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=fc8_logits, labels=labels_placeholder)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate=.001)
training_operation = optimizer.minimize(loss_operation)

preds = tf.arg_max(fc8_logits, 1)

accuracy_operation = tf.reduce_mean(tf.cast(tf.equal(preds, labels_placeholder), tf.float32))

init = tf.global_variables_initializer()

# TODO: Train and evaluate the feature extraction model.


def evaluate(x_data, y_data, the_session):
    num_examples = x_data.shape[0]
    total_accuracy = 0
    total_loss = 0
    for the_offset in range(0, num_examples, BATCH_SIZE):
        the_end = the_offset+BATCH_SIZE
        batch_x = x_data[the_offset:the_end]
        batch_y = y_data[the_offset:the_end]
        loss, accuracy = the_session.run([loss_operation, accuracy_operation],
                                         feed_dict={
                                                 original_features_placeholder: batch_x,
                                                 labels_placeholder: batch_y
                                             }
                                         )
        total_accuracy += (accuracy * batch_x.shape[0])
        total_loss += (loss * batch_x.shape[0])
    return total_accuracy / num_examples, total_loss / num_examples

with tf.Session() as sess:
    sess.run(init)

    train_length = x_train.shape[0]

    for i in range(N_EPOCHS):
        x_train, y_train = shuffle(x_train, y_train)
        le_time = time.time()
        for offset in range(0, train_length, BATCH_SIZE):
            end = offset + BATCH_SIZE
            sess.run(fetches=training_operation,
                     feed_dict={
                         original_features_placeholder: x_train[offset:end],
                         labels_placeholder: y_train[offset:end]
                     })
            print("Epoch: ", i, ", ", offset / train_length, "%")
        validation_accuracy, validation_loss = evaluate(x_data=x_test, y_data=y_test, the_session=sess)
        print("Epoch", i + 1)
        print("Time: %.3f seconds" % (time.time() - le_time))
        print("Validation Loss =", validation_accuracy)
        print("Validation Accuracy =", validation_loss)
        print("")
4677d43513403da77d484abc03425efeb9cbdf33
<mask token>


def evaluate(x_data, y_data, the_session):
    num_examples = x_data.shape[0]
    total_accuracy = 0
    total_loss = 0
    for the_offset in range(0, num_examples, BATCH_SIZE):
        the_end = the_offset + BATCH_SIZE
        batch_x = x_data[the_offset:the_end]
        batch_y = y_data[the_offset:the_end]
        loss, accuracy = the_session.run([loss_operation,
            accuracy_operation], feed_dict={original_features_placeholder:
            batch_x, labels_placeholder: batch_y})
        total_accuracy += accuracy * batch_x.shape[0]
        total_loss += loss * batch_x.shape[0]
    return total_accuracy / num_examples, total_loss / num_examples


<mask token>

<mask token>
with open('./train.p', 'rb') as f:
    data = pickle.load(f)
<mask token>


def evaluate(x_data, y_data, the_session):
    num_examples = x_data.shape[0]
    total_accuracy = 0
    total_loss = 0
    for the_offset in range(0, num_examples, BATCH_SIZE):
        the_end = the_offset + BATCH_SIZE
        batch_x = x_data[the_offset:the_end]
        batch_y = y_data[the_offset:the_end]
        loss, accuracy = the_session.run([loss_operation,
            accuracy_operation], feed_dict={original_features_placeholder:
            batch_x, labels_placeholder: batch_y})
        total_accuracy += accuracy * batch_x.shape[0]
        total_loss += loss * batch_x.shape[0]
    return total_accuracy / num_examples, total_loss / num_examples


with tf.Session() as sess:
    sess.run(init)
    train_length = x_train.shape[0]
    for i in range(N_EPOCHS):
        x_train, y_train = shuffle(x_train, y_train)
        le_time = time.time()
        for offset in range(0, train_length, BATCH_SIZE):
            end = offset + BATCH_SIZE
            sess.run(fetches=training_operation, feed_dict={
                original_features_placeholder: x_train[offset:end],
                labels_placeholder: y_train[offset:end]})
            print('Epoch: ', i, ', ', offset / train_length, '%')
        validation_accuracy, validation_loss = evaluate(x_data=x_test,
            y_data=y_test, the_session=sess)
        print('Epoch', i + 1)
        print('Time: %.3f seconds' % (time.time() - le_time))
        print('Validation Loss =', validation_accuracy)
        print('Validation Accuracy =', validation_loss)
        print('')

<mask token>
number_of_classes = 43
BATCH_SIZE = 128
N_EPOCHS = 3
with open('./train.p', 'rb') as f:
    data = pickle.load(f)
x_train, x_test, y_train, y_test = train_test_split(data['features'], data[
    'labels'])
original_features_placeholder = tf.placeholder(tf.float32, (None, 32, 32, 3))
resized_features_placeholder = tf.image.resize_images(
    original_features_placeholder, (227, 227))
labels_placeholder = tf.placeholder(tf.int64, None)
fc7 = AlexNet(features=resized_features_placeholder, feature_extract=True)
fc7 = tf.stop_gradient(fc7)
shape = fc7.get_shape().as_list()[-1], number_of_classes
fc8W = tf.Variable(tf.truncated_normal(shape=shape))
fc8b = tf.Variable(tf.zeros(shape=number_of_classes))
fc8_logits = tf.nn.xw_plus_b(x=fc7, weights=fc8W, biases=fc8b)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=
    fc8_logits, labels=labels_placeholder)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
training_operation = optimizer.minimize(loss_operation)
preds = tf.arg_max(fc8_logits, 1)
accuracy_operation = tf.reduce_mean(tf.cast(tf.equal(preds,
    labels_placeholder), tf.float32))
init = tf.global_variables_initializer()


def evaluate(x_data, y_data, the_session):
    num_examples = x_data.shape[0]
    total_accuracy = 0
    total_loss = 0
    for the_offset in range(0, num_examples, BATCH_SIZE):
        the_end = the_offset + BATCH_SIZE
        batch_x = x_data[the_offset:the_end]
        batch_y = y_data[the_offset:the_end]
        loss, accuracy = the_session.run([loss_operation,
            accuracy_operation], feed_dict={original_features_placeholder:
            batch_x, labels_placeholder: batch_y})
        total_accuracy += accuracy * batch_x.shape[0]
        total_loss += loss * batch_x.shape[0]
    return total_accuracy / num_examples, total_loss / num_examples


with tf.Session() as sess:
    sess.run(init)
    train_length = x_train.shape[0]
    for i in range(N_EPOCHS):
        x_train, y_train = shuffle(x_train, y_train)
        le_time = time.time()
        for offset in range(0, train_length, BATCH_SIZE):
            end = offset + BATCH_SIZE
            sess.run(fetches=training_operation, feed_dict={
                original_features_placeholder: x_train[offset:end],
                labels_placeholder: y_train[offset:end]})
            print('Epoch: ', i, ', ', offset / train_length, '%')
        validation_accuracy, validation_loss = evaluate(x_data=x_test,
            y_data=y_test, the_session=sess)
        print('Epoch', i + 1)
        print('Time: %.3f seconds' % (time.time() - le_time))
        print('Validation Loss =', validation_accuracy)
        print('Validation Accuracy =', validation_loss)
        print('')

import pickle
import tensorflow as tf
import time
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from alexnet import AlexNet
number_of_classes = 43
BATCH_SIZE = 128
N_EPOCHS = 3
with open('./train.p', 'rb') as f:
    data = pickle.load(f)
x_train, x_test, y_train, y_test = train_test_split(data['features'], data[
    'labels'])
original_features_placeholder = tf.placeholder(tf.float32, (None, 32, 32, 3))
resized_features_placeholder = tf.image.resize_images(
    original_features_placeholder, (227, 227))
labels_placeholder = tf.placeholder(tf.int64, None)
fc7 = AlexNet(features=resized_features_placeholder, feature_extract=True)
fc7 = tf.stop_gradient(fc7)
shape = fc7.get_shape().as_list()[-1], number_of_classes
fc8W = tf.Variable(tf.truncated_normal(shape=shape))
fc8b = tf.Variable(tf.zeros(shape=number_of_classes))
fc8_logits = tf.nn.xw_plus_b(x=fc7, weights=fc8W, biases=fc8b)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=
    fc8_logits, labels=labels_placeholder)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
training_operation = optimizer.minimize(loss_operation)
preds = tf.arg_max(fc8_logits, 1)
accuracy_operation = tf.reduce_mean(tf.cast(tf.equal(preds,
    labels_placeholder), tf.float32))
init = tf.global_variables_initializer()


def evaluate(x_data, y_data, the_session):
    num_examples = x_data.shape[0]
    total_accuracy = 0
    total_loss = 0
    for the_offset in range(0, num_examples, BATCH_SIZE):
        the_end = the_offset + BATCH_SIZE
        batch_x = x_data[the_offset:the_end]
        batch_y = y_data[the_offset:the_end]
        loss, accuracy = the_session.run([loss_operation,
            accuracy_operation], feed_dict={original_features_placeholder:
            batch_x, labels_placeholder: batch_y})
        total_accuracy += accuracy * batch_x.shape[0]
        total_loss += loss * batch_x.shape[0]
    return total_accuracy / num_examples, total_loss / num_examples


with tf.Session() as sess:
    sess.run(init)
    train_length = x_train.shape[0]
    for i in range(N_EPOCHS):
        x_train, y_train = shuffle(x_train, y_train)
        le_time = time.time()
        for offset in range(0, train_length, BATCH_SIZE):
            end = offset + BATCH_SIZE
            sess.run(fetches=training_operation, feed_dict={
                original_features_placeholder: x_train[offset:end],
                labels_placeholder: y_train[offset:end]})
            print('Epoch: ', i, ', ', offset / train_length, '%')
        validation_accuracy, validation_loss = evaluate(x_data=x_test,
            y_data=y_test, the_session=sess)
        print('Epoch', i + 1)
        print('Time: %.3f seconds' % (time.time() - le_time))
        print('Validation Loss =', validation_accuracy)
        print('Validation Accuracy =', validation_loss)
        print('')

import pickle
import tensorflow as tf
import time
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from alexnet import AlexNet

number_of_classes = 43
BATCH_SIZE = 128
N_EPOCHS = 3

# TODO: Load traffic signs data.

with open('./train.p', 'rb') as f:
    data = pickle.load(f)
# note: data is a dictionary with keys: dict_keys(['coords', 'features', 'sizes', 'labels'])

# TODO: Split data into training and validation sets.
x_train, x_test, y_train, y_test = train_test_split(data['features'], data['labels'])

# TODO: Define placeholders and resize operation.
original_features_placeholder = tf.placeholder(tf.float32, (None, 32, 32, 3))
resized_features_placeholder = \
    tf.image.resize_images(original_features_placeholder, (227, 227))

labels_placeholder = tf.placeholder(tf.int64, None)

# TODO: pass placeholder as first argument to `AlexNet`.
fc7 = AlexNet(features=resized_features_placeholder, feature_extract=True)
# NOTE: `tf.stop_gradient` prevents the gradient from flowing backwards
# past this point, keeping the weights before and up to `fc7` frozen.
# This also makes training faster, less work to do!
fc7 = tf.stop_gradient(fc7)

# TODO: Add the final layer for traffic sign classification.
shape = (fc7.get_shape().as_list()[-1], number_of_classes)
fc8W = tf.Variable(tf.truncated_normal(shape=shape))
fc8b = tf.Variable(tf.zeros(shape=number_of_classes))
fc8_logits = tf.nn.xw_plus_b(x=fc7, weights=fc8W, biases=fc8b)

# TODO: Define loss, training, accuracy operations.
# HINT: Look back at your traffic signs project solution, you may
# be able to reuse some the code.
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=fc8_logits, labels=labels_placeholder)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate=.001)
training_operation = optimizer.minimize(loss_operation)

preds = tf.arg_max(fc8_logits, 1)

accuracy_operation = tf.reduce_mean(tf.cast(tf.equal(preds, labels_placeholder), tf.float32))

init = tf.global_variables_initializer()

# TODO: Train and evaluate the feature extraction model.


def evaluate(x_data, y_data, the_session):
    num_examples = x_data.shape[0]
    total_accuracy = 0
    total_loss = 0
    for the_offset in range(0, num_examples, BATCH_SIZE):
        the_end = the_offset+BATCH_SIZE
        batch_x = x_data[the_offset:the_end]
        batch_y = y_data[the_offset:the_end]
        loss, accuracy = the_session.run([loss_operation, accuracy_operation],
                                         feed_dict={
                                                 original_features_placeholder: batch_x,
                                                 labels_placeholder: batch_y
                                             }
                                         )
        total_accuracy += (accuracy * batch_x.shape[0])
        total_loss += (loss * batch_x.shape[0])
    return total_accuracy / num_examples, total_loss / num_examples

with tf.Session() as sess:
    sess.run(init)

    train_length = x_train.shape[0]

    for i in range(N_EPOCHS):
        x_train, y_train = shuffle(x_train, y_train)
        le_time = time.time()
        for offset in range(0, train_length, BATCH_SIZE):
            end = offset + BATCH_SIZE
            sess.run(fetches=training_operation,
                     feed_dict={
                         original_features_placeholder: x_train[offset:end],
                         labels_placeholder: y_train[offset:end]
                     })
            print("Epoch: ", i, ", ", offset / train_length, "%")
        validation_accuracy, validation_loss = evaluate(x_data=x_test, y_data=y_test, the_session=sess)
        print("Epoch", i + 1)
        print("Time: %.3f seconds" % (time.time() - le_time))
        print("Validation Loss =", validation_accuracy)
        print("Validation Accuracy =", validation_loss)
        print("")