Skip to content

Instantly share code, notes, and snippets.

# Initialize z
for i in range(n):
z = f(z, a(i), i)
assert(f(x1) == y1)
assert(f(x2) == y2)
# ...
assert(f(xn) == yn)
def multi_head_cnn_model_fn(features, labels, mode):
# Extract the features
dense = extract_features(features)
# Predictions for each task
predictions_nose = tf.layers.dense(inputs=dense, units=2)
predictions_pose = tf.layers.dense(inputs=dense, units=5)
logits = {'head_nose': predictions_nose, 'head_pose': predictions_pose}
def single_head_cnn_model_fn(features, labels, mode):
# Extract the features
dense = extract_features(features)
# Predictions
predictions = tf.layers.dense(inputs=dense, units=2)
# Optimizer
optimizer = tf.train.AdamOptimizer()
# Adapted from here: https://www.tensorflow.org/tutorials/layers
def single_task_cnn_model_fn(features, labels, mode):
# Get features
dense = extract_features(features)
# Make predictions
predictions = tf.layers.dense(inputs=dense, units=2)
outputs = {
# Reimplement the feature extraction from the original paper
def extract_features(features):
# Input layer
input_layer = tf.reshape(features["x"], [-1, 40, 40, 3])
# First convolutive layer
conv1 = tf.layers.conv2d(inputs=input_layer, filters=16, kernel_size=[5, 5], padding="same", activation=tf.nn.relu)
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
# Second convolutive layer
# This snippet is adapted from here: https://www.tensorflow.org/guide/datasets
def input_fn(dataframe, is_eval=False):
# Load the list of files
filenames = tf.constant(dataframe.iloc[:, 0].tolist())
# Load the labels
labels = tf.constant(dataframe.iloc[:, 1:].values.astype(np.float32))
# Build the dataset with image processing on top of it
# Reads an image from a file, decodes it into a dense tensor, and resizes it
# to a fixed shape.
def _parse_function(filename, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_string, channels=3) # Channels needed because some test images are b/w
image_resized = tf.image.resize_images(image_decoded, [40, 40])
image_shape = tf.cast(tf.shape(image_decoded), tf.float32)
label = tf.concat([label[0:5] / image_shape[0], label[5:10] / image_shape[1], label[10:]], axis=0)
return {"x": image_resized}, label
# We can debug using eager execution
for img, labels in dataset.batch(4).take(1):
print(img)
print(labels)
# tf.Tensor(
# [b'lfw_5590/Aaron_Eckhart_0001.jpg' b'lfw_5590/Aaron_Guiel_0001.jpg' ...
# 2. 3. ]], shape=(4, 14), dtype=float64)
# Load filenames and labels
filenames = tf.constant(train_data.iloc[:, 0].tolist())
labels = tf.constant(train_data.iloc[:, 1:].values)
# Add to a dataset object
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))