def AlexNetFeatureExtractor(features): fc7 = AlexNet(features, feature_extract=True) shape = (fc7.get_shape().as_list()[-1], nb_classes) fc8W = tf.Variable(tf.truncated_normal(shape, mean, stddev)) fc8b = tf.Variable(tf.zeros(nb_classes)) logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b) probs = tf.nn.softmax(logits) return probs
def AlexNetFeatureExtractor(features, labels): fc7 = AlexNet(features, feature_extract=True) fc7 = tf.stop_gradient(fc7) shape = (fc7.get_shape().as_list()[-1], nb_classes) fc8W = tf.Variable(tf.truncated_normal(shape, mean, stddev)) fc8b = tf.Variable(tf.zeros(nb_classes)) logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b) # probs = tf.nn.softmax(logits) preds = tf.argmax(logits, axis=1) trues = tf.argmax(labels, axis=1) acc_op = tf.reduce_mean(tf.equal(preds, trues), tf.float32) loss_op = tf.softmax_cross_entropy_with_logits(logits, labels) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss, var_list=[fc8W, fc8b]) return train_op, acc_op, loss_op
def modified(features): resized = tf.image.resize_images(features, (227, 227)) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(resized, 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], nb_classes) fc8W = tf.Variable(tf.truncated_normal(shape, stddev=1e-2)) fc8b = tf.Variable(tf.zeros(nb_classes)) logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b) return logits
def _create_inference_op(self): resized = tf.image.resize_images(self.X, (227, 227)) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(resized, 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. nb_classes = 43 shape = (fc7.get_shape().as_list()[-1], nb_classes ) # use this shape for the weight matrix with tf.name_scope("last_layer"): weights = tf.Variable(tf.random_normal(shape, stddev=0.01), name="weights") bias = tf.Variable(tf.zeros(nb_classes), name="bias") logits = tf.add(tf.matmul(fc7, weights), bias) return logits
BATCH_SIZE = 150 y = tf.placeholder(tf.int32, (None)) one_hot_y = tf.one_hot(y, 43) rate = 0.005 features = tf.placeholder(tf.float32, (None, 32, 32, 3)) resized = tf.image.resize_images(features, (227, 227)) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(resized, 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. fc7shape = (fc7.get_shape().as_list()[-1], nb_classes) print(fc7.get_shape().as_list()) fc8W = tf.Variable(tf.random_normal(shape=fc7shape, stddev=1e-4)) fc8b = tf.Variable(tf.zeros(nb_classes)) logits = tf.matmul(fc7, fc8W) + fc8b #probabilities = tf.nn.softmax(fc8 # TODO: Define loss, training, accuracy operations. # HINT: Look back at your traffic signs project solution, you may # be able to reuse some the code. ## Trained model here. # Model training parameters cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=one_hot_y, logits=logits) loss_operation = tf.reduce_mean(cross_entropy)
resized = tf.image.resize_images(x, (227, 227)) Y = tf.placeholder(tf.int64, [None, n_class]) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(resized, 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. # fc7 size is 4096 # n_class is 43 mu = 0 sigma = 0.1 input_N = fc7.get_shape().as_list()[-1] fc8W = tf.Variable(tf.truncated_normal((input_N, n_class), mean = mu, stddev = sigma)) fc8b = tf.Variable(tf.zeros(n_class)) logits = tf.matmul(fc7, fc8W) + fc8b prediction = tf.nn.softmax(logits) # TODO: Define loss, training, accuracy operations. # HINT: Look back at your traffic signs project solution, you may # be able to reuse some the code. # Define loss and optimizer rate = 0.001 cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=logits) loss_operation = tf.reduce_mean(cross_entropy) optimizer = tf.train.AdamOptimizer(learning_rate = rate) training_operation = optimizer.minimize(loss_operation)
features = tf.placeholder(tf.float32, (None, 250, 376, 3)) labels = [] for i in range(nb_rotations): labels.append(tf.placeholder(tf.int64, [None])) resized = tf.image.resize_images(features, (227, 227)) # Returns the second final layer of the AlexNet model, # this allows us to redo the last layer maxpool5 = AlexNet(resized, feature_extract=True) maxpool5 = tf.stop_gradient(maxpool5) # average pooling size 3 stride 1 #pooled = tf.nn.pool(fc7, (3,3), "AVG", 1) shape = (maxpool5.get_shape().as_list()[-1], nb_classes) orientation_W = [] orientation_b = [] for i in range(nb_rotations): orientation_W.append(tf.Variable(tf.truncated_normal(shape, stddev=0.0001))) orientation_b.append(tf.Variable(tf.zeros(nb_classes))) logits = [] softmaxes = [] mse = [] for i in range(nb_rotations): logit = tf.nn.xw_plus_b(maxpool5, orientation_W[i], orientation_b[i]) logits.append(logit) softmaxes.append(tf.nn.softmax(logit))
def main(): # tensors features = tf.placeholder(tf.float32, (None, 32, 32, 3)) resized = tf.image.resize_images(features, (227, 227)) labels = tf.placeholder(tf.int32, (None)) labels_one_hot = tf.one_hot(labels, nb_classes) # TODO: make this function work above # train_op, acc_op, loss_op = AlexNetFeatureExtractor(resized, labels) # feature extraction from AlexNet fc7 = AlexNet(resized, feature_extract=True) fc7 = tf.stop_gradient(fc7) shape = (fc7.get_shape().as_list()[-1], nb_classes) fc8W = tf.Variable(tf.truncated_normal(shape, mean, stddev)) fc8b = tf.Variable(tf.zeros(nb_classes)) logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b) # probs = tf.nn.softmax(logits) preds = tf.argmax(logits, axis=1) trues = tf.argmax(labels_one_hot, axis=1) acc_op = tf.reduce_mean(tf.cast(tf.equal(preds, trues), tf.float32)) cross_entropy = tf.nn.softmax_cross_entropy_with_logits( logits, labels_one_hot) loss_op = tf.reduce_mean(cross_entropy) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss_op, var_list=[fc8W, fc8b]) init_op = tf.global_variables_initializer() # load the data X, y = load_data() # split the data into train and val X_train, X_val, y_train, y_val = \ train_test_split(X, y, test_size=0.25, random_state=0) def evaluate(sess, X_eval, y_eval): nb_samples = X_eval.shape[0] total_loss = 0 total_acc = 0 for start_i in range(0, nb_samples, batch_size): end_i = start_i + batch_size X_eval_batch = X_eval[start_i:end_i] y_eval_batch = y_eval[start_i:end_i] loss, accuracy = \ sess.run([loss_op, acc_op], feed_dict={features: X_eval_batch, labels: y_eval_batch}) total_loss += (loss * X_eval_batch.shape[0]) total_loss += (accuracy * X_eval_batch.shape[0]) return total_loss / nb_samples, total_acc / nb_samples with tf.Session() as sess: sess.run(init_op) nb_samples = len(X_train) # TODO: write more concisely if possible for epoch_i in range(epochs): print('Epoch {}'.format(epoch_i + 1)) X_train, y_train = shuffle(X_train, y_train) t0 = time.time() for start_i in range(0, nb_samples, batch_size): end_i = start_i + batch_size X_batch = X_train[start_i:end_i] y_batch = y_train[start_i:end_i] sess.run(train_op, feed_dict={ features: X_batch, labels: y_batch }) val_loss, val_acc = evaluate(sess, X_val, y_val) # show loss and accuracy for each epoch print('Time {:.3f} seconds'.format(epoch_i, time.time() - t0)) print('Validation Loss = ', val_loss) print('Validation Accuracy = ', val_acc)
# TODO: Define placeholders and resize operation. X = tf.placeholder(tf.float32, [None, 32,32,3]) y = tf.placeholder(tf.int32, [None]) one_hot_y = tf.one_hot(y,NB_CLASSES) resized = tf.image.resize_images(X, [227,227]) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(resized, 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], NB_CLASSES) fc8_w = tf.Variable(tf.truncated_normal(shape, stddev=1e-2)) fc8_b = tf.Variable(tf.zeros(NB_CLASSES)) logits = tf.matmul(fc7, fc8_w) + fc8_b # TODO: Define loss, training, accuracy operations. # HINT: Look back at your traffic signs project solution, you may # be able to reuse some the code. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits, labels = one_hot_y)) optimizer = tf.train.AdamOptimizer(learning_rate = .001).minimize(cost, var_list=[fc8_w, fc8_b]) predictions = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_y, 1)) accuracy_score = tf.reduce_mean(tf.cast(predictions, tf.float32)) init = tf.global_variables_initializer()
# TODO: Define placeholders and resize operation. gtsrb_nclasses = 43 x = tf.placeholder(tf.float32, (None, 32, 32, 3)) x_resized = tf.image.resize_images(x, (227, 227)) y = tf.placeholder(tf.int32, (None)) one_hot_y = tf.one_hot(y, gtsrb_nclasses) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(x_resized, 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], gtsrb_nclasses) fc8W = tf.Variable(tf.random_normal(shape, stddev=0.01)) fc8b = tf.Variable(tf.zeros(gtsrb_nclasses)) logits = tf.matmul(fc7, fc8W) + 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. LEARNING_RATE = 1e-3 cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, one_hot_y) loss = tf.reduce_mean(cross_entropy) optimizer = tf.train.GradientDescentOptimizer(learning_rate=LEARNING_RATE) training_op = optimizer.minimize(loss) # TODO: Train and evaluate the feature extraction model. BATCH_SIZE = 128
# TODO: Split data into training and validation sets. X_train, X_validation, y_train, y_validation = train_test_split(X_train, y_train, test_size=0.2) # TODO: Define placeholders and resize operation. x = tf.placeholder(tf.float32, [None, 32, 32, 3]) y = tf.placeholder(tf.int32, [None]) one_hot_y = tf.one_hot(y, 43) resized = tf.image.resize_images(x, [227, 227]) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(resized, feature_extract=True) shape = (fc7.get_shape().as_list()[-1], 43) fc8W = tf.Variable(tf.truncated_normal(shape, stddev=1e-2)) fc8b = tf.Variable(tf.zeros(43)) # 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. logits = tf.nn.xw_plus_b(fc7, fc8W, 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.softmax_cross_entropy_with_logits(logits, one_hot_y) loss = tf.reduce_mean(cross_entropy)
# TODO: Define placeholders and resize operation. x = tf.placeholder(tf.float32, (None, 32, 32, 3)) resized = tf.image.resize_images(x, 227, 227) y_true = tf.placeholder(tf.float32, shape=[None, n_classes]) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(resized, 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. fc_8_shape = (fc7.get_shape().as_list()[-1], n_classes) fc_8_weights = tf.Variable(tf.truncated_normal(shape=fc_8_shape, stddev=1e-2)) fc_8_biases = tf.Variable(tf.zeros(n_classes)) fc_8_logits = tf.nn.xw_plus_b(fc7, fc_8_weights, fc_8_biases) probs = tf.nn.softmax(fc_8_logits) # TODO: Define loss, training, accuracy operations. # HINT: Look back at your traffic signs project solution, you may # be able to reuse some the code. cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=fc_8_logits, labels=y_true)) optimizer = tf.train.GradientDescentOptimizer(1e-3).minimize(cost)
# TODO: Define placeholders and resize operation. x = tf.placeholder(tf.float32, (None, 32, 32, 3)) y = tf.placeholder(tf.int64, (None)) x_resized = tf.image.resize_images(x, (227, 227)) learning_rate = 0.001 # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(x_resized, 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. fc7_shape = fc7.get_shape().as_list()[-1] fc8_w = tf.Variable(tf.truncated_normal((fc7_shape, n_labels), stddev=1e-2)) fc8_b = tf.Variable(tf.zeros(n_labels)) logits = tf.matmul(fc7, fc8_w) + fc8_b # 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=logits, labels=y) loss = tf.reduce_mean(cross_entropy) preds = tf.arg_max(logits, 1) accuracy = tf.reduce_mean(tf.cast(tf.equal(preds, y), tf.float32)) optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss,
def train_model(X_train, y_train, X_valid, y_valid, X_test, y_test, resuming=False, learning_rate=0.001, max_epochs=1001, batch_size=128, early_stopping_enabled=True, early_stopping_patience=10, log_epoch=1, print_epoch=1, top_k=5, return_top_k=False, plot_featuremap=False): print('========= train_model() arguments: ==========') frame = inspect.currentframe() args, _, _, values = inspect.getargvalues(frame) for i in args[6:]: print("{} = {}".format(i, values[i])) print('=============================================') model_dir = os.path.join(os.getcwd(), 'models', '001') os.makedirs(model_dir, exist_ok=True) print('model dir: {}'.format(model_dir)) model_fname = os.path.join(model_dir, 'model_cpkt') model_fname_best_epoch = os.path.join(model_dir, 'best_epoch') model_train_history = os.path.join(model_dir, 'training_history.npz') start = time.time() graph = tf.Graph() with graph.as_default(): # Input data. For the training data, we use a placeholder that will be fed at run time with a training minibatch. x = tf.placeholder(tf.float32, (None, 32, 32, X_test.shape[-1])) y = tf.placeholder(tf.int32, (None)) one_hot_y = tf.one_hot(y, nb_classes) is_training = tf.placeholder(tf.bool) resized = tf.image.resize_images(x, (227, 227)) fc7 = AlexNet(resized, 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) shape = (fc7.get_shape().as_list()[-1], nb_classes ) # use this shape for the weight matrix fc8W = tf.get_variable( name='fc8W', shape=shape, initializer=tf.contrib.layers.xavier_initializer()) fc8b = tf.get_variable(name='fc8b', shape=nb_classes, initializer=tf.constant_initializer(0.0)) logits = tf.matmul(fc7, fc8W) + fc8b predictions = tf.nn.softmax(logits) top_k_predictions = tf.nn.top_k(predictions, top_k) cross_entropy = tf.nn.softmax_cross_entropy_with_logits( labels=one_hot_y, logits=logits) loss_operation = tf.reduce_mean(cross_entropy, name='loss') optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) training_operation = optimizer.minimize(loss_operation) pred_y = tf.argmax(logits, 1, name='prediction') actual_y = tf.argmax(one_hot_y, 1) correct_prediction = tf.equal(pred_y, actual_y) accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name='accuracy') with tf.Session(graph=graph) as sess: sess.run(tf.global_variables_initializer()) total_parameters = 0 for variable in tf.trainable_variables(): # shape is an array of tf.Dimension shape = variable.get_shape() print(variable) print(shape) # print(len(shape)) variable_parametes = 1 for dim in shape: # print(dim) variable_parametes *= dim.value # print(variable_parametes) total_parameters += variable_parametes print('total # of parameters: ', total_parameters) def output_top_k(X_data): top_k_preds = sess.run([top_k_predictions], feed_dict={ x: X_data, is_training: False }) return top_k_preds def evaluate(X_data, y_data, aux_output=False): n_data = len(X_data) correct_pred = np.array([]) y_pred = np.array([]) y_actual = np.array([]) loss_batch = np.array([]) acc_batch = np.array([]) batch_sizes = np.array([]) for offset in range(0, n_data, batch_size): batch_x, batch_y = X_data[offset:offset + batch_size], y_data[offset:offset + batch_size] batch_sizes = np.append(batch_sizes, batch_y.shape[0]) if aux_output: accuracy, loss, cp_, yp_, ya_ = \ sess.run([accuracy_operation, loss_operation, correct_prediction, pred_y, actual_y], feed_dict={x: batch_x, y: batch_y, is_training: False}) correct_pred = np.append(correct_pred, cp_) y_pred = np.append(y_pred, yp_) y_actual = np.append(y_actual, ya_) else: accuracy, loss = sess.run( [accuracy_operation, loss_operation], feed_dict={ x: batch_x, y: batch_y, is_training: False }) loss_batch = np.append(loss_batch, loss) acc_batch = np.append(acc_batch, accuracy) final_acc = np.average(acc_batch, weights=batch_sizes) final_loss = np.average(loss_batch, weights=batch_sizes) if aux_output: return final_acc, final_loss, correct_pred, y_pred, y_actual else: return final_acc, final_loss # If we chose to keep training previously trained model, restore session. if resuming: try: tf.train.Saver().restore(sess, model_fname) print('Restored session from {}'.format(model_fname)) except Exception as e: print( "Failed restoring previously trained model: file does not exist." ) print( "Trying to restore from best epoch from previously training session." ) try: tf.train.Saver().restore(sess, model_fname_best_epoch) print('Restored session from {}'.format( model_fname_best_epoch)) except Exception as e: print("Failed to restore, will train from scratch now.") # print([v.op.name for v in tf.all_variables()]) # print([n.name for n in tf.get_default_graph().as_graph_def().node]) saver = tf.train.Saver() early_stopping = EarlyStopping(tf.train.Saver(), sess, patience=early_stopping_patience, minimize=True, restore_path=model_fname_best_epoch) train_loss_history = np.empty([0], dtype=np.float32) train_accuracy_history = np.empty([0], dtype=np.float32) valid_loss_history = np.empty([0], dtype=np.float32) valid_accuracy_history = np.empty([0], dtype=np.float32) if max_epochs > 0: print("================= TRAINING ==================") else: print("================== TESTING ==================") print(" Timestamp: " + get_time_hhmmss()) for epoch in range(max_epochs): X_train, y_train = shuffle(X_train, y_train) for offset in tqdm(range(0, X_train.shape[0], batch_size)): end = offset + batch_size batch_x, batch_y = X_train[offset:end], y_train[offset:end] sess.run(training_operation, feed_dict={ x: batch_x, y: batch_y, is_training: True }) # If another significant epoch ended, we log our losses. if epoch % log_epoch == 0: train_accuracy, train_loss = evaluate(X_train, y_train) valid_accuracy, valid_loss = evaluate(X_valid, y_valid) if epoch % print_epoch == 0: print("-------------- EPOCH %4d/%d --------------" % (epoch, max_epochs)) print(" Train loss: %.8f, accuracy: %.2f%%" % (train_loss, 100 * train_accuracy)) print("Validation loss: %.8f, accuracy: %.2f%%" % (valid_loss, 100 * valid_accuracy)) print(" Best loss: %.8f at epoch %d" % (early_stopping.best_monitored_value, early_stopping.best_monitored_epoch)) print(" Elapsed time: " + get_time_hhmmss(start)) print(" Timestamp: " + get_time_hhmmss()) else: valid_loss = 0. valid_accuracy = 0. train_loss = 0. train_accuracy = 0. valid_loss_history = np.append(valid_loss_history, [valid_loss]) valid_accuracy_history = np.append(valid_accuracy_history, [valid_accuracy]) train_loss_history = np.append(train_loss_history, [train_loss]) train_accuracy_history = np.append(train_accuracy_history, [train_accuracy]) if early_stopping_enabled: # Get validation data predictions and log validation loss: if valid_loss == 0: _, valid_loss = evaluate(X_valid, y_valid) if early_stopping(valid_loss, epoch): print( "Early stopping.\nBest monitored loss was {:.8f} at epoch {}." .format(early_stopping.best_monitored_value, early_stopping.best_monitored_epoch)) break # Evaluate on test dataset. valid_accuracy, valid_loss, valid_cp, valid_yp, valid_ya = evaluate( X_valid, y_valid, aux_output=True) test_accuracy, test_loss, test_cp, test_yp, test_ya = evaluate( X_test, y_test, aux_output=True) print("=============================================") print(" Valid loss: %.8f, accuracy = %.2f%%)" % (valid_loss, 100 * valid_accuracy)) print(" Test loss: %.8f, accuracy = %.2f%%)" % (test_loss, 100 * test_accuracy)) print(" Total time: " + get_time_hhmmss(start)) print(" Timestamp: " + get_time_hhmmss()) # Save model weights for future use. saved_model_path = saver.save(sess, model_fname) print("Model file: " + saved_model_path) np.savez(model_train_history, train_loss_history=train_loss_history, train_accuracy_history=train_accuracy_history, valid_loss_history=valid_loss_history, valid_accuracy_history=valid_accuracy_history) print("Train history file: " + model_train_history) if return_top_k: top_k_preds = output_top_k(X_test) result_dict = dict(test_accuracy=test_accuracy, test_loss=test_loss, test_cp=test_cp, test_yp=test_yp, test_ya=test_ya, valid_accuracy=valid_accuracy, valid_loss=valid_loss, valid_cp=valid_cp, valid_yp=valid_yp, valid_ya=valid_ya) if return_top_k: return result_dict, top_k_preds else: return result_dict
from alexnet import AlexNet sign_names = pd.read_csv('signnames.csv') nb_classes = 43 x = tf.placeholder(tf.float32, (None, 32, 32, 3)) resized = tf.image.resize_images(x, (227, 227)) # NOTE: By setting `feature_extract` to `True` we return # the second to last layer. fc7 = AlexNet(resized, feature_extract=True) # TODO: Define a new fully connected layer followed by a softmax activation to classify # the traffic signs. Assign the result of the softmax activation to `probs` below. # HINT: Look at the final layer definition in alexnet.py to get an idea of what this # should look like. shape = [fc7.get_shape().as_list()[-1], nb_classes] # use this shape for the weight matrix probs = tf.nn.softmax( tf.matmul(fc7, tf.Variable(tf.truncated_normal(shape))) + tf.Variable(tf.zeros(nb_classes))) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) # Read Images im1 = imread("construction.jpg").astype(np.float32) im1 = im1 - np.mean(im1) im2 = imread("stop.jpg").astype(np.float32) im2 = im2 - np.mean(im2)
X_train, X_valida, y_train, y_valida = train_test_split(data['features'], data['labels'], test_size=0.33, random_state=0) # Define placeholders and resize operation. features = tf.placeholder(tf.float32, (None, 32, 32, 3)) labels = tf.placeholder(tf.int64, None) resized = tf.image.resize_images(features, (227, 227)) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(resized, 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) # Add the final layer for traffic sign classification. shape = (fc7.get_shape().as_list()[-1], nb_classes) # shape for the weight matrix(4096, 43) fc8W = tf.Variable(tf.random_normal(shape, stddev=1e-2), dtype=tf.float32) fc8b = tf.Variable(tf.zeros(nb_classes, dtype=tf.float32)) logits = tf.matmul(fc7, fc8W) + fc8b # Define loss, training, accuracy operations. cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels) loss_op = tf.reduce_mean(cross_entropy) opt = tf.train.AdamOptimizer() train_op = opt.minimize(loss_op, var_list=[fc8W, fc8b]) init_op = tf.global_variables_initializer() preds = tf.arg_max(logits, 1) accuracy_op = tf.reduce_mean(tf.cast(tf.equal(preds, labels), tf.float32)) # Train and evaluate the feature extraction model. def eval_on_data(X, y, sess):
num_test_samples = X_test.shape[0] # TODO: Define placeholders and resize operation. features = tf.placeholder(tf.float32, shape=(None, 32, 32, 3)) labels = tf.placeholder(tf.int64) resized = tf.image.resize_images(features, (227, 227)) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(resized, 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. print("###", fc7.get_shape()) shape = (fc7.get_shape().as_list()[-1], nb_classes) fc8W = tf.Variable(tf.truncated_normal(shape, stddev=1e-2)) fc8b = tf.Variable(tf.zeros(nb_classes)) logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b) probs = tf.nn.softmax(logits) # TODO: Define loss, training, accuracy operations. # HINT: Look back at your traffic signs project solution, you may # be able to reuse some the code. entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits) loss_op = tf.reduce_mean(entropy) optimizer_op = tf.train.AdamOptimizer().minimize(loss_op) #train_op = opt.minimize(loss_op, var_list=[fc8W, fc8b]) pred_op = tf.argmax(probs, axis=1)
import pandas as pd from scipy.misc import imread from alexnet import AlexNet sign_names = pd.read_csv('signnames.csv') nb_classes = 43 x = tf.placeholder(tf.float32, (None, 32, 32, 3)) resized = tf.image.resize_images(x, (227, 227)) # Returns the second final layer of the AlexNet model, # this allows us to redo the last layer for the specifically for # traffic signs model. fc7 = AlexNet(resized, feature_extract=True) ## print the shape of fc7 print('shape: ' + fc7.get_shape()) shape = (fc7.get_shape().as_list()[-1], nb_classes) fc8W = tf.Variable(tf.truncated_normal(shape, stddev=1e-2)) fc8b = tf.Variable(tf.zeros(nb_classes)) logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b) probs = tf.nn.softmax(logits) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) # Read Images im1 = imread("construction.jpg").astype(np.float32) im1 = im1 - np.mean(im1) im2 = imread("stop.jpg").astype(np.float32)
from alexnet import AlexNet sign_names = pd.read_csv('signnames.csv') nb_classes = 43 x = tf.placeholder(tf.float32, (None, 32, 32, 3)) resized = tf.image.resize_images(x, (227, 227)) # NOTE: By setting `feature_extract` to `True` we return # the second to last layer. fc7 = AlexNet(resized, feature_extract=True) # TODO: Define a new fully connected layer followed by a softmax activation to classify # the traffic signs. Assign the result of the softmax activation to `probs` below. # HINT: Look at the final layer definition in alexnet.py to get an idea of what this # should look like. shape = (fc7.get_shape().as_list()[-1], nb_classes) # use this shape for the weight matrix # fc8 # fc(43, relu=False, name='fc8') fc8W = tf.Variable(tf.truncated_normal(shape, stddev=1e-02)) fc8b = tf.Variable(tf.zeros(nb_classes)) # print("fc7", fc7.get_shape(), fc7.dtype) # print("fc8W", fc8W.get_shape(), fc8W.dtype) # print("fc8b", fc8b.get_shape(), fc8b.dtype) logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b) probs = tf.nn.softmax(logits) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init)
features = tf.placeholder(tf.float32, (None, 32, 32, 3)) # None for no shape labels = tf.placeholder(tf.int64, None) # Resize images using built in resize feature to expected 227x227 resized = tf.image.resize_images(features, (227, 227)) # Pass features as first argument to `AlexNet`. fc7 = AlexNet(resized, 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) # Add the final layer for traffic sign classification. shape = (fc7.get_shape().as_list()[-1], number_of_classes) # Create some random stuff fc8W = tf.Variable(tf.truncated_normal(shape, stddev=.01)) # Zeros for bias fc8b = tf.Variable(tf.zeros(number_of_classes)) # Score using X_train * weights + bias logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b) # pass scores and labels to softmax super duper version cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels) # Define loss, training, accuracy operations. # define goal of reducing average of score? loss_operation = tf.reduce_mean(cross_entropy) # assign AdamOptimizer to var for code readability
# TODO: Define placeholders and resize operation. x = tf.placeholder(tf.float32, (None, 32, 32, 3)) y = tf.placeholder(tf.int32, (None)) x_resized = tf.image.resize_images(x, (227, 227)) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(x_resized, 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) num_classes = 43 reg = 0.001 fc8_W_shape = (fc7.get_shape().as_list()[-1], num_classes) # TODO: Add the final layer for traffic sign classification. fc8_W = tf.get_variable( 'fc8_W', shape=fc8_W_shape, initializer=tf.contrib.layers.xavier_initializer(uniform=False), regularizer=tf.contrib.layers.l2_regularizer(reg)) fc8_b = tf.Variable(tf.constant(0.0, shape=[num_classes], dtype=tf.float32)) logits = tf.matmul(fc7, fc8_W) + fc8_b probs = tf.nn.softmax(logits) # TODO: Define loss, training, accuracy operations. # HINT: Look back at your traffic signs project solution, you may # be able to reuse some the code. one_hot_y = tf.one_hot(y, num_classes)
# TODO: Define placeholders and resize operation. X = tf.placeholder(tf.float32, [None, 32, 32, 3], 'X') y = tf.placeholder(tf.int32, [None], 'y') y_one_hot = tf.one_hot(y, n_classes) X_resized = tf.image.resize_images(X, [227, 227]) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(X_resized, 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], n_classes] W = tf.Variable(tf.truncated_normal(shape, stddev=1e-2)) b = tf.Variable(tf.zeros(n_classes)) fc8 = tf.nn.xw_plus_b(fc7, W, b) logits = fc8 # TODO: Define loss, training, accuracy operations. learning_rate = 0.0001 epoch = 10 batch_size = 128 cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, y_one_hot) loss = tf.reduce_mean(cross_entropy) optimizer = tf.train.AdamOptimizer() training_operation = optimizer.minimize(loss) accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits, 1), tf.argmax(y_one_hot, 1)), tf.float32))
labels = tf.placeholder(tf.int32, None) #AlexNet expects images to be 277x277x3 resized_features = tf.image.resize_images(features, (227, 227)) # pass features placeholder as first argument to `AlexNet`. #pass feature_extract=True means that we want last layer: fc7 #for feature extraction fc7 = AlexNet(resized_features, 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) # Add the final layer for traffic sign classification. fc7_features_count = fc7.get_shape().as_list()[1] fc8W = tf.Variable(tf.truncated_normal((fc7_features_count, nb_classes))) fc8b = tf.Variable(tf.zeros(nb_classes)) logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b) # Define loss, training, accuracy operations. cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels) loss_op = tf.reduce_mean(cross_entropy) optimizer = tf.train.AdamOptimizer() optimze_op = optimizer.minimize(loss_op, var_list=[fc8W, fc8W]) predictions_match = tf.equal(tf.arg_max(logits, 1), tf.arg_max(labels, 1)) accuracy_op = tf.reduce_mean(tf.cast(predictions_match, tf.float32)) # Train and evaluate the feature extraction model.
with open('./train.p', 'rb') as f: data = pickle.load(f) X_train, X_val, y_train, y_val = train_test_split(data['features'], data['labels'], test_size=0.33, random_state=0) features = tf.placeholder(tf.float32, (None, 32, 32, 3)) labels = tf.placeholder(tf.int64, None) resized = tf.image.resize_images(features, (227, 227)) # Returns the second final layer of the AlexNet model, # this allows us to redo the last layer for the traffic signs # model. fc7 = AlexNet(resized, feature_extract=True) fc7 = tf.stop_gradient(fc7) shape = (fc7.get_shape().as_list()[-1], nb_classes) fc8W = tf.Variable(tf.truncated_normal(shape, stddev=1e-2)) fc8b = tf.Variable(tf.zeros(nb_classes)) logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels) loss_op = tf.reduce_mean(cross_entropy) opt = tf.train.AdamOptimizer() train_op = opt.minimize(loss_op, var_list=[fc8W, fc8b]) init_op = tf.global_variables_initializer() preds = tf.arg_max(logits, 1) accuracy_op = tf.reduce_mean(tf.cast(tf.equal(preds, labels), tf.float32)) def eval_on_data(X, y, sess):
import pandas as pd from scipy.misc import imread from alexnet import AlexNet sign_names = pd.read_csv('signnames.csv') nb_classes = 43 x = tf.placeholder(tf.float32, (None, 32, 32, 3)) resized = tf.image.resize_images(x, (227, 227)) # NOTE: By setting `feature_extract` to `True` we return # the second to last layer. fc7 = AlexNet(resized, feature_extract=True) # TODO: Define a new fully connected layer followed by a softmax activation to classify # the traffic signs. Assign the result of the softmax activation to `probs` below. shape = (fc7.get_shape().as_list()[-1], nb_classes) # use this shape for the weight matrix mu = 0.0 sigma = 0.01 fc8W = tf.Variable(tf.truncated_normal(shape, mean=mu, stddev=sigma)) fc8b = tf.Variable(tf.zeros(shape[1])) logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b) probs = tf.nn.softmax(logits) init = tf.initialize_all_variables() sess = tf.Session() sess.run(init) # Read Images im1 = imread("construction.jpg").astype(np.float32) im1 = im1 - np.mean(im1)
from alexnet import AlexNet sign_names = pd.read_csv('signnames.csv') nb_classes = 43 x = tf.placeholder(tf.float32, (None, 32, 32, 3)) resized = tf.image.resize_images(x, (227, 227)) # NOTE: By setting `feature_extract` to `True` we return # the second to last layer. fc7 = AlexNet(resized, feature_extract=True) # TODO: Define a new fully connected layer followed by a softmax activation to classify # the traffic signs. Assign the result of the softmax activation to `probs` below. # HINT: Look at the final layer definition in alexnet.py to get an idea of what this # should look like. shape = (fc7.get_shape().as_list()[-1], nb_classes ) # use this shape for the weight matrix fc_W = tf.Variable(tf.truncated_normal(shape, mean=0, stddev=0.1)) fc_b = tf.Variable(tf.zeros(nb_classes)) logits = tf.matmul(fc7, fc_W) + fc_b probs = tf.nn.softmax(logits) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) # Read Images im1 = imread("construction.jpg").astype(np.float32) im1 = im1 - np.mean(im1) im2 = imread("stop.jpg").astype(np.float32)
test_size=0.33) # TODO: Define placeholders and resize operation. x = tf.placeholder(tf.float32, (None, 32, 32, 3)) y = tf.placeholder(tf.int64, None) resized = tf.image.resize_images(x, (227, 227)) # TODO: pass placeholder as first argument to `AlexNet`. fc7 = AlexNet(resized, 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. fx_W = tf.Variable(tf.truncated_normal((fc7.get_shape().as_list()[-1], 43))) fx_b = tf.Variable(tf.zeros(43)) logits = tf.matmul(fc7, fx_W) + fx_b # 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, y) loss_op = tf.reduce_mean(cross_entropy) opti = tf.train.AdamOptimizer(learning_rate=0.001) train_op = opti.minimize(loss_op) accuracy_op = tf.reduce_mean(tf.cast(tf.equal(preds, y), tf.float32)) predictions = tf.arg_max(logits, 1) def eval_on_data(X, y, sess):