In [1]:
%matplotlib inline
In [2]:
import pandas as pd
import matplotlib.pyplot as plt
In [3]:
# Read in the meta info and target variables
labels = pd.read_csv('../TRAIN/track_parms.csv')
labels = labels.rename(columns={'phi': 'theta'})
labels[['filename', 'theta', 'z']].tail()
Out[3]:
In [7]:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import Sequential
from tensorflow.keras.layers import (
Conv2D, Activation, MaxPooling2D,
Flatten, Dense, Dropout
)
height = 100
width = 36
channels = 3
datagen = ImageDataGenerator(rescale=1./255.,
validation_split=0.25)
In [8]:
def double_regression_model():
model = Sequential()
# Convolution Layer
model.add(Conv2D(filters=32,
kernel_size=(3, 3),
input_shape=(height, width, channels)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Dense, Regression Layer
model.add(Flatten())
model.add(Dense(32))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(2))
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['mse'])
return model
In [10]:
n_subsample = 1000
batch_size = 32
train_gen = datagen.flow_from_dataframe(
dataframe=labels.head(n_subsample),
directory="../TRAIN",
x_col="filename",
y_col=["z", "theta"],
subset="training",
target_size=(height, width),
batch_size=batch_size,
seed=314,
shuffle=False,
class_mode="other",
)
val_gen = datagen.flow_from_dataframe(
dataframe=labels.head(n_subsample),
directory="../TRAIN",
x_col="filename",
y_col=["z", "theta"],
subset="validation",
target_size=(height, width),
batch_size=batch_size,
seed=314,
shuffle=False,
class_mode="other",
)
In [11]:
STEP_SIZE_TRAIN = train_gen.n//train_gen.batch_size
STEP_SIZE_VAL = val_gen.n//val_gen.batch_size
In [12]:
regressor = double_regression_model()
regressor.summary()
In [13]:
reg_history = regressor.fit_generator(
generator=train_gen,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=val_gen,
validation_steps=STEP_SIZE_VAL,
epochs=10
)
In [15]:
plt.plot(reg_history.history['mse'],
label="Train MSE")
plt.plot(reg_history.history['val_mse'],
label="Validation MSE")
#plt.ylim([0, 5.5])
plt.legend()
plt.show()
In [16]:
def deep_double_regression_model():
model = Sequential()
# Convolution Layer
model.add(Conv2D(filters=32,
kernel_size=(3, 3),
input_shape=(height, width, channels)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Dense, Classification Layer
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(2))
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['mse'])
return model
In [21]:
n_subsample = 5000
batch_size = 32
train_gen = datagen.flow_from_dataframe(
dataframe=labels.head(n_subsample),
directory="../TRAIN",
x_col="filename",
y_col=["z", "theta"],
subset="training",
target_size=(height, width),
batch_size=batch_size,
seed=314,
shuffle=False,
class_mode="other",
)
val_gen = datagen.flow_from_dataframe(
dataframe=labels.head(n_subsample),
directory="../TRAIN",
x_col="filename",
y_col=["z", "theta"],
subset="validation",
target_size=(height, width),
batch_size=batch_size,
seed=314,
shuffle=False,
class_mode="other",
)
In [22]:
STEP_SIZE_TRAIN = train_gen.n//train_gen.batch_size
STEP_SIZE_VAL = val_gen.n//val_gen.batch_size
In [23]:
regressor = deep_double_regression_model()
regressor.summary()
In [24]:
reg_history = regressor.fit_generator(
generator=train_gen,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=val_gen,
validation_steps=STEP_SIZE_VAL,
epochs=15
)
In [ ]:
plt.plot(reg_history.history['mse'],
label="Train MSE")
plt.plot(reg_history.history['val_mse'],
label="Validation MSE")
#plt.ylim([0, 5.5])
plt.legend()
plt.show()
In [ ]:
holdout_track_params = pd.read_csv('../VALIDATION/track_parms.csv')
holdout_track_params = holdout_track_params.rename(columns={'phi': 'theta'})
In [ ]:
holdout_gen = datagen.flow_from_dataframe(
dataframe=holdout_track_params,
directory="../VALIDATION",
x_col="filename",
y_col=["z", "phi"],
subset=None,
target_size=(height, width),
batch_size=32,
seed=314,
shuffle=False,
class_mode="raw",
)
In [ ]:
holdout_track_params['z_pred'] = 0.0
holdout_track_params['phi_pred'] = 0.0
In [ ]:
y_pred = deep_double_regression_model.predict(holdout_gen)
In [ ]:
from sklearn.metrics import r2_score, mean_squared_error
In [ ]:
holdout_track_params['z_pred'] = [y[0] for y in y_pred]
holdout_track_params['phi_pred'] = [y[1] for y in y_pred]
In [ ]:
holdout_track_params['delta_z'] = holdout_track_params.eval('z - z_pred')
holdout_track_params['delta_phi'] = holdout_track_params.eval('phi - phi_pred')
In [ ]:
print(r2_score(holdout_track_params['phi'],
holdout_track_params['phi_pred']))
print(mean_squared_error(holdout_track_params['phi'],
holdout_track_params['phi_pred']))
In [ ]:
print(r2_score(holdout_track_params['z'],
holdout_track_params['z_pred']))
print(mean_squared_error(holdout_track_params['z'],
holdout_track_params['z_pred']))
In [ ]:
fig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3, figsize=(14, 5))
holdout_track_params['delta_z'].hist(bins=10, alpha=0.5,
log=False, ax=ax0)
holdout_track_params['delta_z'].hist(bins=10, alpha=0.5,
log=True, ax=ax1)
holdout_track_params['delta_phi'].hist(bins=10, ax=ax2,
alpha=0.5)
ax0.set_title(r'Residual $\Delta z$')
ax1.set_title(r'Residual $\Delta z$ (log)')
ax2.set_title(r'Residual: $\Delta\theta$')
plt.show()
In [354]:
from matplotlib import cm
fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2,
figsize=(12, 6),
sharex=True,
sharey=True)
scatter = plt.scatter(x=holdout_track_params['z'],
y=holdout_track_params['z_pred'],
c=holdout_track_params['phi'],
cmap=cm.seismic,
vmin=-10., vmax=10.,
alpha=0.2)
plt.colorbar(scatter, label=r'$\theta$')
ax1.set_facecolor("#888888")
_ = plt.title(r'z (vertex), colored by $\theta$')
_ = plt.xlabel('z_true')
plt.sca(ax0)
scatter = plt.scatter(x=holdout_track_params['z'],
y=holdout_track_params['z_pred'],
alpha=0.2)
_ = plt.title('z (vertex)')
_ = plt.xlabel('z_true')
_ = plt.ylabel('z_pred')
plt.subplots_adjust(right=1.)
In [354]:
from matplotlib import cm
fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2,
figsize=(12, 6),
sharex=True,
sharey=True)
scatter = plt.scatter(x=holdout['z'],
y=holdout['z_pred'],
c=holdout['theta'],
cmap=cm.seismic,
vmin=-10., vmax=10.,
alpha=0.2)
plt.colorbar(scatter, label=r'$\theta$')
ax1.set_facecolor("#888888")
_ = plt.title(r'z (vertex), colored by $\theta$')
_ = plt.xlabel('z_true')
plt.sca(ax0)
scatter = plt.scatter(x=holdout['z'],
y=holdout['z_pred'],
alpha=0.2)
_ = plt.title('z (vertex)')
_ = plt.xlabel('z_true')
_ = plt.ylabel('z_pred')
plt.subplots_adjust(right=1.)
In [281]:
plt.scatter(x=holdout_track_params['phi'], y=holdout_track_params['phi_pred'],
alpha=0.2)
_ = plt.title(r'$\theta$')
_ = plt.xlabel(r'$\theta$_true')
_ = plt.ylabel(r'$\theta$_pred')
In [ ]: