Reconfigured folder structure
Added Evaluation, Preprocessing, Training, Transformation folders. Preprocessing is just a rework of the folder for the new structure of the old preprocessing folder. Training and Transformation are the old project file broken up into two parts and restructured. Evaluation is for evaluating the predictive power of the model.
This commit is contained in:
240
Project Final/Evaluation/Evalutation.ipynb
Normal file
240
Project Final/Evaluation/Evalutation.ipynb
Normal file
@@ -0,0 +1,240 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Using TensorFlow backend.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import numpy as np\n",
|
||||
"from six.moves import cPickle\n",
|
||||
"import matplotlib\n",
|
||||
"matplotlib.use('Agg')\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import matplotlib.gridspec as gridspec\n",
|
||||
"%matplotlib inline\n",
|
||||
"from keras import backend as K\n",
|
||||
"from keras.models import Model, model_from_json\n",
|
||||
"from keras.layers import Input, Dense, Flatten\n",
|
||||
"\n",
|
||||
"from prednet import PredNet\n",
|
||||
"from data_utils import SequenceGenerator\n",
|
||||
"\n",
|
||||
"from tqdm import tqdm"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"n_plot = 40\n",
|
||||
"batch_size = 10\n",
|
||||
"nt = 24"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"WEIGHTS_DIR = '../Training/weights/'\n",
|
||||
"DATA_DIR = '../data/'\n",
|
||||
"RESULTS_SAVE_DIR = './weather_results/'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"weights_file = os.path.join(WEIGHTS_DIR, 'prednet_weather_weights.hdf5')\n",
|
||||
"json_file = os.path.join(WEIGHTS_DIR, 'prednet_weather_model.json')\n",
|
||||
"test_file = os.path.join(DATA_DIR, 'x_test.hkl')\n",
|
||||
"test_sources = os.path.join(DATA_DIR, 'sources_test.hkl')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Load trained model\n",
|
||||
"f = open(json_file, 'r')\n",
|
||||
"json_string = f.read()\n",
|
||||
"f.close()\n",
|
||||
"train_model = model_from_json(json_string, custom_objects = {'PredNet': PredNet})\n",
|
||||
"train_model.load_weights(weights_file)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Create testing model (to output predictions)\n",
|
||||
"layer_config = train_model.layers[1].get_config()\n",
|
||||
"layer_config['output_mode'] = 'prediction'\n",
|
||||
"data_format = layer_config['data_format'] if 'data_format' in layer_config else layer_config['dim_ordering']\n",
|
||||
"test_prednet = PredNet(weights=train_model.layers[1].get_weights(), **layer_config)\n",
|
||||
"input_shape = list(train_model.layers[0].batch_input_shape[1:])\n",
|
||||
"input_shape[0] = nt\n",
|
||||
"inputs = Input(shape=tuple(input_shape))\n",
|
||||
"predictions = test_prednet(inputs)\n",
|
||||
"test_model = Model(inputs=inputs, outputs=predictions)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"test_generator = SequenceGenerator(test_file, test_sources, nt, sequence_start_mode='unique', data_format=data_format)\n",
|
||||
"X_test = test_generator.create_all()\n",
|
||||
"X_hat = test_model.predict(X_test, batch_size)\n",
|
||||
"if data_format == 'channels_first':\n",
|
||||
" X_test = np.transpose(X_test, (0, 1, 3, 4, 2))\n",
|
||||
" X_hat = np.transpose(X_hat, (0, 1, 3, 4, 2))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Compare MSE of PredNet predictions vs. using last frame. Write results to prediction_scores.txt\n",
|
||||
"mse_model = np.nanmean( (X_test[:, 1:] - X_hat[:, 1:])**2 ) # look at all timesteps except the first\n",
|
||||
"mse_prev = np.nanmean( (X_test[:, :-1] - X_test[:, 1:])**2 )\n",
|
||||
"if not os.path.exists(RESULTS_SAVE_DIR): os.mkdir(RESULTS_SAVE_DIR)\n",
|
||||
"f = open(RESULTS_SAVE_DIR + 'prediction_scores.txt', 'w')\n",
|
||||
"f.write(\"Model MSE: %f\\n\" % mse_model)\n",
|
||||
"f.write(\"Previous Frame MSE: %f\" % mse_prev)\n",
|
||||
"f.close()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Model MSE:\t 14.119876861572266\n",
|
||||
"Prev Frame MSE:\t 0.02834348939359188\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(\"Model MSE:\\t {}\\nPrev Frame MSE:\\t {}\".format(mse_model,mse_prev))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 63%|███████████████████████████████████████████████████▉ | 19/30 [09:38<05:35, 30.47s/it]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Plot some predictions\n",
|
||||
"aspect_ratio = float(X_hat.shape[3]) / X_hat.shape[2]\n",
|
||||
"plt.figure(figsize = (nt, 7*2*aspect_ratio))\n",
|
||||
"gs = gridspec.GridSpec(2*7, nt)\n",
|
||||
"gs.update(wspace=0., hspace=0.2)\n",
|
||||
"plot_save_dir = os.path.join(RESULTS_SAVE_DIR, 'prediction_plots/')\n",
|
||||
"if not os.path.exists(plot_save_dir): os.mkdir(plot_save_dir)\n",
|
||||
"plot_idx = np.random.permutation(X_test.shape[0])[:n_plot]\n",
|
||||
"for i in tqdm(plot_idx):\n",
|
||||
" for t in range(nt):\n",
|
||||
" for c in range(7):\n",
|
||||
" plt.subplot(gs[t + c*2*nt])\n",
|
||||
" plt.imshow(X_test[i,t,:,:,c], interpolation='none')\n",
|
||||
" plt.tick_params(axis='both', which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off')\n",
|
||||
" if t==0: plt.ylabel('Actual', fontsize=10)\n",
|
||||
"\n",
|
||||
" plt.subplot(gs[t + (c*2+1)*nt])\n",
|
||||
" plt.imshow(X_hat[i,t,:,:,c], interpolation='none')\n",
|
||||
" plt.tick_params(axis='both', which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off')\n",
|
||||
" if t==0: plt.ylabel('Predicted', fontsize=10)\n",
|
||||
"\n",
|
||||
" plt.savefig(plot_save_dir + 'plot_' + str(i) + '.png')\n",
|
||||
" plt.clf()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fig=plt.figure(figsize=(15,10))\n",
|
||||
"columns = 3\n",
|
||||
"rows = 4\n",
|
||||
"for i in range(1,columns+rows +1):\n",
|
||||
" fig.add_subplot(rows,columns,i)\n",
|
||||
" plt.imshow(X_test[0,0,:,:,i-1],X_hat[0,0,:,:,i-1])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"X_hat[0][0][0][0][2]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
68
Project Final/Evaluation/data_utils.py
Normal file
68
Project Final/Evaluation/data_utils.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import hickle as hkl
|
||||
import numpy as np
|
||||
from keras import backend as K
|
||||
from keras.preprocessing.image import Iterator
|
||||
|
||||
# Data generator that creates sequences for input into PredNet.
|
||||
class SequenceGenerator(Iterator):
|
||||
def __init__(self, data_file, source_file, nt,
|
||||
batch_size=8, shuffle=False, seed=None,
|
||||
output_mode='error', sequence_start_mode='all', N_seq=None,
|
||||
data_format=K.image_data_format()):
|
||||
self.X = hkl.load(data_file) # X will be like (n_images, nb_cols, nb_rows, nb_channels)
|
||||
self.sources = hkl.load(source_file) # source for each image so when creating sequences can assure that consecutive frames are from same video
|
||||
self.nt = nt
|
||||
self.batch_size = batch_size
|
||||
self.data_format = data_format
|
||||
assert sequence_start_mode in {'all', 'unique'}, 'sequence_start_mode must be in {all, unique}'
|
||||
self.sequence_start_mode = sequence_start_mode
|
||||
assert output_mode in {'error', 'prediction'}, 'output_mode must be in {error, prediction}'
|
||||
self.output_mode = output_mode
|
||||
|
||||
if self.data_format == 'channels_first':
|
||||
self.X = np.transpose(self.X, (0, 3, 1, 2))
|
||||
self.im_shape = self.X[0].shape
|
||||
|
||||
if self.sequence_start_mode == 'all': # allow for any possible sequence, starting from any frame
|
||||
self.possible_starts = np.array([i for i in range(self.X.shape[0] - self.nt) if self.sources[i] == self.sources[i + self.nt - 1]])
|
||||
elif self.sequence_start_mode == 'unique': #create sequences where each unique frame is in at most one sequence
|
||||
curr_location = 0
|
||||
possible_starts = []
|
||||
while curr_location < self.X.shape[0] - self.nt + 1:
|
||||
if self.sources[curr_location] == self.sources[curr_location + self.nt - 1]:
|
||||
possible_starts.append(curr_location)
|
||||
curr_location += self.nt
|
||||
else:
|
||||
curr_location += 1
|
||||
self.possible_starts = possible_starts
|
||||
|
||||
if shuffle:
|
||||
self.possible_starts = np.random.permutation(self.possible_starts)
|
||||
if N_seq is not None and len(self.possible_starts) > N_seq: # select a subset of sequences if want to
|
||||
self.possible_starts = self.possible_starts[:N_seq]
|
||||
self.N_sequences = len(self.possible_starts)
|
||||
super(SequenceGenerator, self).__init__(len(self.possible_starts), batch_size, shuffle, seed)
|
||||
|
||||
def next(self):
|
||||
with self.lock:
|
||||
index_array = next(self.index_generator)
|
||||
current_index = index_array[0]
|
||||
current_batch_size = len(index_array)
|
||||
batch_x = np.zeros((current_batch_size, self.nt) + self.im_shape, np.float32)
|
||||
for i, idx in enumerate(index_array):
|
||||
idx = self.possible_starts[idx]
|
||||
batch_x[i] = self.preprocess(self.X[idx:idx+self.nt])
|
||||
if self.output_mode == 'error': # model outputs errors, so y should be zeros
|
||||
batch_y = np.zeros(current_batch_size, np.float32)
|
||||
elif self.output_mode == 'prediction': # output actual pixels
|
||||
batch_y = batch_x
|
||||
return batch_x, batch_y
|
||||
|
||||
def preprocess(self, X):
|
||||
return X.astype(np.float32) / 255
|
||||
|
||||
def create_all(self):
|
||||
X_all = np.zeros((self.N_sequences, self.nt) + self.im_shape, np.float32)
|
||||
for i, idx in enumerate(self.possible_starts):
|
||||
X_all[i] = self.preprocess(self.X[idx:idx+self.nt])
|
||||
return X_all
|
||||
58
Project Final/Evaluation/keras_utils.py
Normal file
58
Project Final/Evaluation/keras_utils.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import os
|
||||
import numpy as np
|
||||
|
||||
from keras import backend as K
|
||||
from keras.legacy.interfaces import generate_legacy_interface, recurrent_args_preprocessor
|
||||
from keras.models import model_from_json
|
||||
|
||||
legacy_prednet_support = generate_legacy_interface(
|
||||
allowed_positional_args=['stack_sizes', 'R_stack_sizes',
|
||||
'A_filt_sizes', 'Ahat_filt_sizes', 'R_filt_sizes'],
|
||||
conversions=[('dim_ordering', 'data_format'),
|
||||
('consume_less', 'implementation')],
|
||||
value_conversions={'dim_ordering': {'tf': 'channels_last',
|
||||
'th': 'channels_first',
|
||||
'default': None},
|
||||
'consume_less': {'cpu': 0,
|
||||
'mem': 1,
|
||||
'gpu': 2}},
|
||||
preprocessor=recurrent_args_preprocessor)
|
||||
|
||||
# Convert old Keras (1.2) json models and weights to Keras 2.0
|
||||
def convert_model_to_keras2(old_json_file, old_weights_file, new_json_file, new_weights_file):
|
||||
from prednet import PredNet
|
||||
# If using tensorflow, it doesn't allow you to load the old weights.
|
||||
if K.backend() != 'theano':
|
||||
os.environ['KERAS_BACKEND'] = backend
|
||||
reload(K)
|
||||
|
||||
f = open(old_json_file, 'r')
|
||||
json_string = f.read()
|
||||
f.close()
|
||||
model = model_from_json(json_string, custom_objects = {'PredNet': PredNet})
|
||||
model.load_weights(old_weights_file)
|
||||
|
||||
weights = model.layers[1].get_weights()
|
||||
if weights[0].shape[0] == model.layers[1].stack_sizes[1]:
|
||||
for i, w in enumerate(weights):
|
||||
if w.ndim == 4:
|
||||
weights[i] = np.transpose(w, (2, 3, 1, 0))
|
||||
model.set_weights(weights)
|
||||
|
||||
model.save_weights(new_weights_file)
|
||||
json_string = model.to_json()
|
||||
with open(new_json_file, "w") as f:
|
||||
f.write(json_string)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
old_dir = './model_data/'
|
||||
new_dir = './model_data_keras2/'
|
||||
if not os.path.exists(new_dir):
|
||||
os.mkdir(new_dir)
|
||||
for w_tag in ['', '-Lall', '-extrapfinetuned']:
|
||||
m_tag = '' if w_tag == '-Lall' else w_tag
|
||||
convert_model_to_keras2(old_dir + 'prednet_kitti_model' + m_tag + '.json',
|
||||
old_dir + 'prednet_kitti_weights' + w_tag + '.hdf5',
|
||||
new_dir + 'prednet_kitti_model' + m_tag + '.json',
|
||||
new_dir + 'prednet_kitti_weights' + w_tag + '.hdf5')
|
||||
311
Project Final/Evaluation/prednet.py
Normal file
311
Project Final/Evaluation/prednet.py
Normal file
@@ -0,0 +1,311 @@
|
||||
import numpy as np
|
||||
|
||||
from keras import backend as K
|
||||
from keras import activations
|
||||
from keras.layers import Recurrent
|
||||
from keras.layers import Conv2D, UpSampling2D, MaxPooling2D
|
||||
from keras.engine import InputSpec
|
||||
from keras_utils import legacy_prednet_support
|
||||
|
||||
class PredNet(Recurrent):
|
||||
'''PredNet architecture - Lotter 2016.
|
||||
Stacked convolutional LSTM inspired by predictive coding principles.
|
||||
|
||||
# Arguments
|
||||
stack_sizes: number of channels in targets (A) and predictions (Ahat) in each layer of the architecture.
|
||||
Length is the number of layers in the architecture.
|
||||
First element is the number of channels in the input.
|
||||
Ex. (3, 16, 32) would correspond to a 3 layer architecture that takes in RGB images and has 16 and 32
|
||||
channels in the second and third layers, respectively.
|
||||
R_stack_sizes: number of channels in the representation (R) modules.
|
||||
Length must equal length of stack_sizes, but the number of channels per layer can be different.
|
||||
A_filt_sizes: filter sizes for the target (A) modules.
|
||||
Has length of 1 - len(stack_sizes).
|
||||
Ex. (3, 3) would mean that targets for layers 2 and 3 are computed by a 3x3 convolution of the errors (E)
|
||||
from the layer below (followed by max-pooling)
|
||||
Ahat_filt_sizes: filter sizes for the prediction (Ahat) modules.
|
||||
Has length equal to length of stack_sizes.
|
||||
Ex. (3, 3, 3) would mean that the predictions for each layer are computed by a 3x3 convolution of the
|
||||
representation (R) modules at each layer.
|
||||
R_filt_sizes: filter sizes for the representation (R) modules.
|
||||
Has length equal to length of stack_sizes.
|
||||
Corresponds to the filter sizes for all convolutions in the LSTM.
|
||||
pixel_max: the maximum pixel value.
|
||||
Used to clip the pixel-layer prediction.
|
||||
error_activation: activation function for the error (E) units.
|
||||
A_activation: activation function for the target (A) and prediction (A_hat) units.
|
||||
LSTM_activation: activation function for the cell and hidden states of the LSTM.
|
||||
LSTM_inner_activation: activation function for the gates in the LSTM.
|
||||
output_mode: either 'error', 'prediction', 'all' or layer specification (ex. R2, see below).
|
||||
Controls what is outputted by the PredNet.
|
||||
If 'error', the mean response of the error (E) units of each layer will be outputted.
|
||||
That is, the output shape will be (batch_size, nb_layers).
|
||||
If 'prediction', the frame prediction will be outputted.
|
||||
If 'all', the output will be the frame prediction concatenated with the mean layer errors.
|
||||
The frame prediction is flattened before concatenation.
|
||||
Nomenclature of 'all' is kept for backwards compatibility, but should not be confused with returning all of the layers of the model
|
||||
For returning the features of a particular layer, output_mode should be of the form unit_type + layer_number.
|
||||
For instance, to return the features of the LSTM "representational" units in the lowest layer, output_mode should be specificied as 'R0'.
|
||||
The possible unit types are 'R', 'Ahat', 'A', and 'E' corresponding to the 'representation', 'prediction', 'target', and 'error' units respectively.
|
||||
extrap_start_time: time step for which model will start extrapolating.
|
||||
Starting at this time step, the prediction from the previous time step will be treated as the "actual"
|
||||
data_format: 'channels_first' or 'channels_last'.
|
||||
It defaults to the `image_data_format` value found in your
|
||||
Keras config file at `~/.keras/keras.json`.
|
||||
|
||||
# References
|
||||
- [Deep predictive coding networks for video prediction and unsupervised learning](https://arxiv.org/abs/1605.08104)
|
||||
- [Long short-term memory](http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf)
|
||||
- [Convolutional LSTM network: a machine learning approach for precipitation nowcasting](http://arxiv.org/abs/1506.04214)
|
||||
- [Predictive coding in the visual cortex: a functional interpretation of some extra-classical receptive-field effects](http://www.nature.com/neuro/journal/v2/n1/pdf/nn0199_79.pdf)
|
||||
'''
|
||||
@legacy_prednet_support
|
||||
def __init__(self, stack_sizes, R_stack_sizes,
|
||||
A_filt_sizes, Ahat_filt_sizes, R_filt_sizes,
|
||||
pixel_max=1., error_activation='relu', A_activation='relu',
|
||||
LSTM_activation='tanh', LSTM_inner_activation='hard_sigmoid',
|
||||
output_mode='error', extrap_start_time=None,
|
||||
data_format=K.image_data_format(), **kwargs):
|
||||
self.stack_sizes = stack_sizes
|
||||
self.nb_layers = len(stack_sizes)
|
||||
assert len(R_stack_sizes) == self.nb_layers, 'len(R_stack_sizes) must equal len(stack_sizes)'
|
||||
self.R_stack_sizes = R_stack_sizes
|
||||
assert len(A_filt_sizes) == (self.nb_layers - 1), 'len(A_filt_sizes) must equal len(stack_sizes) - 1'
|
||||
self.A_filt_sizes = A_filt_sizes
|
||||
assert len(Ahat_filt_sizes) == self.nb_layers, 'len(Ahat_filt_sizes) must equal len(stack_sizes)'
|
||||
self.Ahat_filt_sizes = Ahat_filt_sizes
|
||||
assert len(R_filt_sizes) == (self.nb_layers), 'len(R_filt_sizes) must equal len(stack_sizes)'
|
||||
self.R_filt_sizes = R_filt_sizes
|
||||
|
||||
self.pixel_max = pixel_max
|
||||
self.error_activation = activations.get(error_activation)
|
||||
self.A_activation = activations.get(A_activation)
|
||||
self.LSTM_activation = activations.get(LSTM_activation)
|
||||
self.LSTM_inner_activation = activations.get(LSTM_inner_activation)
|
||||
|
||||
default_output_modes = ['prediction', 'error', 'all']
|
||||
layer_output_modes = [layer + str(n) for n in range(self.nb_layers) for layer in ['R', 'E', 'A', 'Ahat']]
|
||||
assert output_mode in default_output_modes + layer_output_modes, 'Invalid output_mode: ' + str(output_mode)
|
||||
self.output_mode = output_mode
|
||||
if self.output_mode in layer_output_modes:
|
||||
self.output_layer_type = self.output_mode[:-1]
|
||||
self.output_layer_num = int(self.output_mode[-1])
|
||||
else:
|
||||
self.output_layer_type = None
|
||||
self.output_layer_num = None
|
||||
self.extrap_start_time = extrap_start_time
|
||||
|
||||
assert data_format in {'channels_last', 'channels_first'}, 'data_format must be in {channels_last, channels_first}'
|
||||
self.data_format = data_format
|
||||
self.channel_axis = -3 if data_format == 'channels_first' else -1
|
||||
self.row_axis = -2 if data_format == 'channels_first' else -3
|
||||
self.column_axis = -1 if data_format == 'channels_first' else -2
|
||||
super(PredNet, self).__init__(**kwargs)
|
||||
self.input_spec = [InputSpec(ndim=5)]
|
||||
|
||||
def compute_output_shape(self, input_shape):
|
||||
if self.output_mode == 'prediction':
|
||||
out_shape = input_shape[2:]
|
||||
elif self.output_mode == 'error':
|
||||
out_shape = (self.nb_layers,)
|
||||
elif self.output_mode == 'all':
|
||||
out_shape = (np.prod(input_shape[2:]) + self.nb_layers,)
|
||||
else:
|
||||
stack_str = 'R_stack_sizes' if self.output_layer_type == 'R' else 'stack_sizes'
|
||||
stack_mult = 2 if self.output_layer_type == 'E' else 1
|
||||
out_stack_size = stack_mult * getattr(self, stack_str)[self.output_layer_num]
|
||||
out_nb_row = input_shape[self.row_axis] / 2**self.output_layer_num
|
||||
out_nb_col = input_shape[self.column_axis] / 2**self.output_layer_num
|
||||
if self.data_format == 'channels_first':
|
||||
out_shape = (out_stack_size, out_nb_row, out_nb_col)
|
||||
else:
|
||||
out_shape = (out_nb_row, out_nb_col, out_stack_size)
|
||||
|
||||
if self.return_sequences:
|
||||
return (input_shape[0], input_shape[1]) + out_shape
|
||||
else:
|
||||
return (input_shape[0],) + out_shape
|
||||
|
||||
def get_initial_state(self, x):
|
||||
input_shape = self.input_spec[0].shape
|
||||
init_nb_row = input_shape[self.row_axis]
|
||||
init_nb_col = input_shape[self.column_axis]
|
||||
|
||||
base_initial_state = K.zeros_like(x) # (samples, timesteps) + image_shape
|
||||
non_channel_axis = -1 if self.data_format == 'channels_first' else -2
|
||||
for _ in range(2):
|
||||
base_initial_state = K.sum(base_initial_state, axis=non_channel_axis)
|
||||
base_initial_state = K.sum(base_initial_state, axis=1) # (samples, nb_channels)
|
||||
|
||||
initial_states = []
|
||||
states_to_pass = ['r', 'c', 'e']
|
||||
nlayers_to_pass = {u: self.nb_layers for u in states_to_pass}
|
||||
if self.extrap_start_time is not None:
|
||||
states_to_pass.append('ahat') # pass prediction in states so can use as actual for t+1 when extrapolating
|
||||
nlayers_to_pass['ahat'] = 1
|
||||
for u in states_to_pass:
|
||||
for l in range(nlayers_to_pass[u]):
|
||||
ds_factor = 2 ** l
|
||||
nb_row = init_nb_row // ds_factor
|
||||
nb_col = init_nb_col // ds_factor
|
||||
if u in ['r', 'c']:
|
||||
stack_size = self.R_stack_sizes[l]
|
||||
elif u == 'e':
|
||||
stack_size = 2 * self.stack_sizes[l]
|
||||
elif u == 'ahat':
|
||||
stack_size = self.stack_sizes[l]
|
||||
output_size = stack_size * nb_row * nb_col # flattened size
|
||||
|
||||
reducer = K.zeros((input_shape[self.channel_axis], output_size)) # (nb_channels, output_size)
|
||||
initial_state = K.dot(base_initial_state, reducer) # (samples, output_size)
|
||||
if self.data_format == 'channels_first':
|
||||
output_shp = (-1, stack_size, nb_row, nb_col)
|
||||
else:
|
||||
output_shp = (-1, nb_row, nb_col, stack_size)
|
||||
initial_state = K.reshape(initial_state, output_shp)
|
||||
initial_states += [initial_state]
|
||||
|
||||
if K._BACKEND == 'theano':
|
||||
from theano import tensor as T
|
||||
# There is a known issue in the Theano scan op when dealing with inputs whose shape is 1 along a dimension.
|
||||
# In our case, this is a problem when training on grayscale images, and the below line fixes it.
|
||||
initial_states = [T.unbroadcast(init_state, 0, 1) for init_state in initial_states]
|
||||
|
||||
if self.extrap_start_time is not None:
|
||||
initial_states += [K.variable(0, int if K.backend() != 'tensorflow' else 'int32')] # the last state will correspond to the current timestep
|
||||
return initial_states
|
||||
|
||||
def build(self, input_shape):
|
||||
self.input_spec = [InputSpec(shape=input_shape)]
|
||||
self.conv_layers = {c: [] for c in ['i', 'f', 'c', 'o', 'a', 'ahat']}
|
||||
|
||||
for l in range(self.nb_layers):
|
||||
for c in ['i', 'f', 'c', 'o']:
|
||||
act = self.LSTM_activation if c == 'c' else self.LSTM_inner_activation
|
||||
self.conv_layers[c].append(Conv2D(self.R_stack_sizes[l], self.R_filt_sizes[l], padding='same', activation=act, data_format=self.data_format))
|
||||
|
||||
act = 'relu' if l == 0 else self.A_activation
|
||||
self.conv_layers['ahat'].append(Conv2D(self.stack_sizes[l], self.Ahat_filt_sizes[l], padding='same', activation=act, data_format=self.data_format))
|
||||
|
||||
if l < self.nb_layers - 1:
|
||||
self.conv_layers['a'].append(Conv2D(self.stack_sizes[l+1], self.A_filt_sizes[l], padding='same', activation=self.A_activation, data_format=self.data_format))
|
||||
|
||||
self.upsample = UpSampling2D(data_format=self.data_format)
|
||||
self.pool = MaxPooling2D(data_format=self.data_format)
|
||||
|
||||
self.trainable_weights = []
|
||||
nb_row, nb_col = (input_shape[-2], input_shape[-1]) if self.data_format == 'channels_first' else (input_shape[-3], input_shape[-2])
|
||||
for c in sorted(self.conv_layers.keys()):
|
||||
for l in range(len(self.conv_layers[c])):
|
||||
ds_factor = 2 ** l
|
||||
if c == 'ahat':
|
||||
nb_channels = self.R_stack_sizes[l]
|
||||
elif c == 'a':
|
||||
nb_channels = 2 * self.R_stack_sizes[l]
|
||||
else:
|
||||
nb_channels = self.stack_sizes[l] * 2 + self.R_stack_sizes[l]
|
||||
if l < self.nb_layers - 1:
|
||||
nb_channels += self.R_stack_sizes[l+1]
|
||||
in_shape = (input_shape[0], nb_channels, nb_row // ds_factor, nb_col // ds_factor)
|
||||
if self.data_format == 'channels_last': in_shape = (in_shape[0], in_shape[2], in_shape[3], in_shape[1])
|
||||
with K.name_scope('layer_' + c + '_' + str(l)):
|
||||
self.conv_layers[c][l].build(in_shape)
|
||||
self.trainable_weights += self.conv_layers[c][l].trainable_weights
|
||||
|
||||
self.states = [None] * self.nb_layers*3
|
||||
|
||||
if self.extrap_start_time is not None:
|
||||
self.t_extrap = K.variable(self.extrap_start_time, int if K.backend() != 'tensorflow' else 'int32')
|
||||
self.states += [None] * 2 # [previous frame prediction, timestep]
|
||||
|
||||
def step(self, a, states):
|
||||
r_tm1 = states[:self.nb_layers]
|
||||
c_tm1 = states[self.nb_layers:2*self.nb_layers]
|
||||
e_tm1 = states[2*self.nb_layers:3*self.nb_layers]
|
||||
|
||||
if self.extrap_start_time is not None:
|
||||
t = states[-1]
|
||||
a = K.switch(t >= self.t_extrap, states[-2], a) # if past self.extrap_start_time, the previous prediction will be treated as the actual
|
||||
|
||||
c = []
|
||||
r = []
|
||||
e = []
|
||||
|
||||
# Update R units starting from the top
|
||||
for l in reversed(range(self.nb_layers)):
|
||||
inputs = [r_tm1[l], e_tm1[l]]
|
||||
if l < self.nb_layers - 1:
|
||||
inputs.append(r_up)
|
||||
|
||||
inputs = K.concatenate(inputs, axis=self.channel_axis)
|
||||
i = self.conv_layers['i'][l].call(inputs)
|
||||
f = self.conv_layers['f'][l].call(inputs)
|
||||
o = self.conv_layers['o'][l].call(inputs)
|
||||
_c = f * c_tm1[l] + i * self.conv_layers['c'][l].call(inputs)
|
||||
_r = o * self.LSTM_activation(_c)
|
||||
c.insert(0, _c)
|
||||
r.insert(0, _r)
|
||||
|
||||
if l > 0:
|
||||
r_up = self.upsample.call(_r)
|
||||
|
||||
# Update feedforward path starting from the bottom
|
||||
for l in range(self.nb_layers):
|
||||
ahat = self.conv_layers['ahat'][l].call(r[l])
|
||||
if l == 0:
|
||||
ahat = K.minimum(ahat, self.pixel_max)
|
||||
frame_prediction = ahat
|
||||
|
||||
# compute errors
|
||||
e_up = self.error_activation(ahat - a)
|
||||
e_down = self.error_activation(a - ahat)
|
||||
|
||||
e.append(K.concatenate((e_up, e_down), axis=self.channel_axis))
|
||||
|
||||
if self.output_layer_num == l:
|
||||
if self.output_layer_type == 'A':
|
||||
output = a
|
||||
elif self.output_layer_type == 'Ahat':
|
||||
output = ahat
|
||||
elif self.output_layer_type == 'R':
|
||||
output = r[l]
|
||||
elif self.output_layer_type == 'E':
|
||||
output = e[l]
|
||||
|
||||
if l < self.nb_layers - 1:
|
||||
a = self.conv_layers['a'][l].call(e[l])
|
||||
a = self.pool.call(a) # target for next layer
|
||||
|
||||
if self.output_layer_type is None:
|
||||
if self.output_mode == 'prediction':
|
||||
output = frame_prediction
|
||||
else:
|
||||
for l in range(self.nb_layers):
|
||||
layer_error = K.mean(K.batch_flatten(e[l]), axis=-1, keepdims=True)
|
||||
all_error = layer_error if l == 0 else K.concatenate((all_error, layer_error), axis=-1)
|
||||
if self.output_mode == 'error':
|
||||
output = all_error
|
||||
else:
|
||||
output = K.concatenate((K.batch_flatten(frame_prediction), all_error), axis=-1)
|
||||
|
||||
states = r + c + e
|
||||
if self.extrap_start_time is not None:
|
||||
states += [frame_prediction, t + 1]
|
||||
return output, states
|
||||
|
||||
def get_config(self):
|
||||
config = {'stack_sizes': self.stack_sizes,
|
||||
'R_stack_sizes': self.R_stack_sizes,
|
||||
'A_filt_sizes': self.A_filt_sizes,
|
||||
'Ahat_filt_sizes': self.Ahat_filt_sizes,
|
||||
'R_filt_sizes': self.R_filt_sizes,
|
||||
'pixel_max': self.pixel_max,
|
||||
'error_activation': self.error_activation.__name__,
|
||||
'A_activation': self.A_activation.__name__,
|
||||
'LSTM_activation': self.LSTM_activation.__name__,
|
||||
'LSTM_inner_activation': self.LSTM_inner_activation.__name__,
|
||||
'data_format': self.data_format,
|
||||
'extrap_start_time': self.extrap_start_time,
|
||||
'output_mode': self.output_mode}
|
||||
base_config = super(PredNet, self).get_config()
|
||||
return dict(list(base_config.items()) + list(config.items()))
|
||||
Reference in New Issue
Block a user