Submitted models will be loaded as-is from a single submitted HDF5 compatible with keras.models.load model(). The loaded model will then be fed a final set of data (TEST) that will conform to the formats outlined above. No post processing on the model’s output will be performed, meaning the models are expected to provide two real number outputs such that the first output has been trained to give θ and the second to give z.
keras.models.load_model()
In [1]:
%matplotlib inline
In [2]:
import os
import sys
import gzip
import pandas as pd
import numpy as np
import math
width = 36
height = 100
batch_size = 32
train_df = pd.read_csv('../TRAIN/track_parms.csv')
train_df = train_df.rename(columns={'phi': 'theta'})
valid_df = pd.read_csv('../VALIDATION/track_parms.csv')
valid_df = valid_df.rename(columns={'phi': 'theta'})
STEP_SIZE_TRAIN = len(train_df)/batch_size
STEP_SIZE_VALID = len(valid_df)/batch_size
In [3]:
def generate_arrays_from_file(path, labels_df):
images_path = os.path.join(path, 'images.raw.gz')
print('Generator created for: {}'.format(images_path))
batch_input = []
batch_labels_theta = []
batch_labels_z = []
idx = 0
ibatch = 0
while True:
with gzip.open(images_path) as f:
while True:
# Read in one image
bytes = f.read(width*height)
if len(bytes) != (width*height): break # break into outer loop so we can re-open file
data = np.frombuffer(bytes, dtype='B', count=width*height)
pixels = np.reshape(data, [width, height, 1], order='F')
pixels_norm = np.transpose(pixels.astype(np.float) / 255., axes=(1, 0, 2) )
# Labels
theta = labels_df.theta[idx]
z = labels_df.z[idx]
idx += 1
# Add to batch and check if it is time to yield
batch_input.append( pixels_norm )
batch_labels_theta.append(theta)
batch_labels_z.append( z )
if len(batch_input) == batch_size:
ibatch += 1
# Since we are training multiple loss functions we must
# pass the labels back as a dictionary whose keys match
# the layer their corresponding values are being applied
# to.
labels_dict = {
'theta_output' : np.array(batch_labels_theta),
'z_output' : np.array(batch_labels_z),
}
yield ( np.array(batch_input), labels_dict )
batch_input = []
batch_labels_theta = []
batch_labels_z = []
idx = 0
f.close()
In [4]:
train_generator = generate_arrays_from_file('../TRAIN', train_df)
valid_generator = generate_arrays_from_file('../VALIDATION', valid_df)
In [5]:
import tensorflow as tf
In [6]:
def double_regression_model():
image_input = tf.keras.Input(shape=(height, width, 1),
name='image_input')
x = tf.keras.layers.Conv2D(8, (3, 3))(image_input)
x = tf.keras.layers.Activation(tf.nn.relu)(x)
x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(x)
x = tf.keras.layers.Conv2D(12, (3, 3))(x)
x = tf.keras.layers.Activation(tf.nn.relu)(x)
x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(x)
#### Theta branch
theta_branch = tf.keras.layers.Conv2D(16, (2, 2))(x)
theta_branch = tf.keras.layers.Activation(tf.nn.relu)(theta_branch)
theta_branch = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(theta_branch)
theta_branch = tf.keras.layers.Conv2D(16, (3, 3))(theta_branch)
theta_branch = tf.keras.layers.Activation(tf.nn.relu)(theta_branch)
theta_branch = tf.keras.layers.Flatten()(theta_branch)
theta_branch = tf.keras.layers.Dense(16)(theta_branch)
theta_branch = tf.keras.layers.Activation(tf.nn.relu)(theta_branch)
theta_branch = tf.keras.layers.Dropout(0.5)(theta_branch)
output_theta = tf.keras.layers.Dense(1, activation='linear',
name='theta_output')(theta_branch)
####
#### Vertex branch
z_branch = tf.keras.layers.Conv2D(16, (2, 2))(x)
z_branch = tf.keras.layers.Activation(tf.nn.relu)(z_branch)
z_branch = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(z_branch)
z_branch = tf.keras.layers.Conv2D(16, (3, 3))(z_branch)
z_branch = tf.keras.layers.Activation(tf.nn.relu)(z_branch)
z_branch = tf.keras.layers.Flatten()(z_branch)
z_branch = tf.keras.layers.Dense(16)(z_branch)
z_branch = tf.keras.layers.Activation(tf.nn.relu)(z_branch)
z_branch = tf.keras.layers.Dropout(0.5)(z_branch)
output_z = tf.keras.layers.Dense(1, activation='linear',
name='z_output')(z_branch)
####
model = tf.keras.Model(inputs=image_input, outputs=[output_theta, output_z])
model.compile(
optimizer='adam',
loss={
'theta_output': 'mean_squared_error',
'z_output': 'mean_squared_error'
},
metrics=['mse']
)
return model
In [7]:
model = double_regression_model()
In [8]:
model.summary()
In [41]:
history = model.fit_generator(
generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=15,
initial_epoch=6,
)
In [45]:
model.save('jlab_submission.h5')
In [46]:
model.save_weights('jlab_submission_weights.h5')
In [47]:
valid_generator = generate_arrays_from_file('../VALIDATION', valid_df)
y_pred = model.predict_generator(valid_generator, steps=STEP_SIZE_VALID)
In [48]:
valid_df['theta_pred'] = y_pred[0][:50000]
In [49]:
import matplotlib.pyplot as plt
In [50]:
plt.hist(y_pred[0])
Out[50]:
In [51]:
valid_df = valid_df.eval('d_theta = theta - theta_pred')
In [52]:
valid_df.d_theta.hist()
Out[52]:
In [63]:
def original_double_regression_model():
image_input = tf.keras.Input(shape=(height, width, 1),
name='image_input')
x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(image_input)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(x)
def branch(input_layer, output_name):
y = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(input_layer)
y = tf.keras.layers.BatchNormalization()(y)
y = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(y)
y = tf.keras.layers.Conv2D(64, (3, 3), activation='relu')(y)
y = tf.keras.layers.BatchNormalization()(y)
y = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(y)
y = tf.keras.layers.Flatten()(y)
y = tf.keras.layers.Dense(64, activation='relu')(y)
y = tf.keras.layers.BatchNormalization()(y)
y = tf.keras.layers.Dropout(0.5)(y)
output = tf.keras.layers.Dense(
1, activation='linear',
name=output_name
)(y)
return output
theta_output = branch(x, 'theta_output')
z_output = branch(x, 'z_output')
model = tf.keras.Model(inputs=image_input, outputs=[theta_output, z_output])
model.compile(
optimizer='adam',
loss={
'theta_output': 'mean_squared_error',
'z_output': 'mean_squared_error'
}
)
return model
In [64]:
original_model = original_double_regression_model()
original_model.summary()
In [65]:
train_generator = generate_arrays_from_file('../TRAIN', train_df)
valid_generator = generate_arrays_from_file('../VALIDATION', valid_df)
In [69]:
original_history = original_model.fit_generator(
generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=7,
initial_epoch=5
)
In [75]:
original_history.history.keys()
Out[75]:
In [76]:
plt.plot(original_history.history['z_output_loss'], label="Train z-vertex MSE")
plt.plot(original_history.history['val_z_output_loss'], label="Validation z-vertex MSE")
plt.legend()
plt.show()
In [78]:
original_model.save('../models/jlab_submission.h5')
In [79]:
import keras
In [81]:
reloaded_model = tf.keras.models.load_model('../models/jlab_submission.h5')
In [ ]: