Commit 8c0681ff authored by Svetlana Pavlitskaya's avatar Svetlana Pavlitskaya

Data preparations scripts and updated EMADL model

parent 2c4d88aa
import caffe
import csv
import numpy as np
from PIL import Image
import sys
blob = caffe.proto.caffe_pb2.BlobProto()
data = open("./driving_mean_1F.binaryproto" , 'rb' ).read()
blob.ParseFromString(data)
data = np.array(blob.data)
arr = np.array( caffe.io.blobproto_to_array(blob) )
arr = arr[0]
# np.save("./mean_image.npy", arr[0]) # shape is (210, 280, 3)
np.savetxt("./mean_image_R.txt", arr[0])
np.savetxt("./mean_image_G.txt", arr[1])
np.savetxt("./mean_image_B.txt", arr[2])
\ No newline at end of file
import mxnet as mx
def load_data_rec(self, batch_size):
train_iter = mx.image.ImageIter(
path_imgrec=self._data_dir_ + "torcs_train.rec",
data_shape=(3, 210, 280), # (channels, height, width)
batch_size=batch_size,
label_width=14,
data_name='image',
label_name='predictions_label'
)
test_iter = mx.image.ImageIter(
path_imgrec=self._data_dir_ + "torcs_test.rec",
data_shape=(3, 210, 280), # (channels, height, width)
batch_size=batch_size,
label_width=14,
data_name='image',
label_name='predictions_label'
)
data_mean = None
data_std = None
return train_iter, test_iter, data_mean, data_std
...@@ -2,101 +2,62 @@ import matplotlib.pyplot as plt ...@@ -2,101 +2,62 @@ import matplotlib.pyplot as plt
import numpy as np import numpy as np
import re import re
NETWORK= "Dpnet" EPOCH_WISE = False
DATASET = "DeepDriving Dataset" NETWORK= "DPNet"
LOGFILE = "../../dpnet_weights/normalized/train.log"
# define the paths to the training logs STEP = 50
logs = [
(0, "train_1.log") # lr=1e-2 rows = open(LOGFILE).read().strip()
# (85, "training_65.log"), # lr=1e-3 train_mse = list()
# (100, "training_85.log"), # lr=1e-4 validation_mse = list()
] train_iterations = list()
validation_iterations = list()
# initialize the list of train rank-1 and rank-5 accuracies, along speeds = list()
# with the training loss
(trainRank1, trainRank5, trainLoss) = ([], [], []) # grab the set of training epochs
epochs = set(re.findall(r'Epoch\[(\d+)\]', rows))
# initialize the list of validation rank-1 and rank-5 accuracies, epochs = sorted([int(e) for e in epochs])
# along with the validation loss for e in epochs:
(valRank1, valRank5, valLoss) = ([], [], []) train_mse_regexp = r'Epoch\[' + str(e) + '\].*(\s)mse=(.*)'
mse = re.findall(train_mse_regexp, rows)
# loop over the training logs mse = [float(a[1]) for a in mse]
for (i, (endEpoch, p)) in enumerate(logs): if EPOCH_WISE:
# load the contents of the log file, then initialize the batch train_mse.append(mse[-1])
# lists for the training and validation data else:
rows = open(p).read().strip() train_mse += mse
(bTrainMSE, bTrainRank5, bTrainLoss) = ([], [], [])
(bValRank1, bValRank5, bValLoss) = ([], [], [])
# grab the set of training epochs
epochs = set(re.findall(r'Epoch\[(\d+)\]', rows))
epochs = sorted([int(e) for e in epochs])
# loop over the epochs
for e in epochs:
# find all rank-1 accuracies, rank-5 accuracies, and loss
# values, then take the final entry in the list for each
s = r'Epoch\[' + str(e) + '\].*Train-mse=(.*)'
mse = re.findall(s, rows)[-1]
# update the batch training lists
bTrainMSE.append(float(mse))
# extract the validation rank-1 and rank-5 accuracies for each
# epoch, followed by the loss
bValRank1 = re.findall(r'Validation-mse=(.*)', rows)
# convert the validation rank-1, rank-5, and loss lists to floats speed_regexp = r'Epoch\[' + str(e) + '\].*(\s)Speed: (.*) samples'
bValRank1 = [float(x) for x in bValRank1] speed = re.findall(speed_regexp, rows)
speed = [float(a[1]) for a in speed]
speeds += speed
# check to see if we are examining a log file other than the validation_mse_regexp = r'Epoch\[' + str(e) + '\].*Validation-mse=(.*)'
# first one, and if so, use the number of the final epoch in current_validation_mse = re.findall(validation_mse_regexp, rows)
# the log file as our slice index validation_mse.append(float(current_validation_mse[0]))
if i > 0 and endEpoch is not None:
trainEnd = endEpoch - logs[i - 1][0]
valEnd = endEpoch - logs[i - 1][0]
# otherwise, this is the first epoch so no subtraction needs last_iteration = train_iterations[-1] if len(train_iterations) > 0 else 0
# to be done if EPOCH_WISE:
train_iterations.append(e)
validation_iterations.append(e)
else: else:
trainEnd = endEpoch current_iterations = range(last_iteration+STEP, last_iteration+STEP * len(mse) + STEP, STEP)
valEnd = endEpoch train_iterations += current_iterations
validation_iterations.append(last_iteration+STEP * len(mse) + STEP)
# update the training lists
trainRank1.extend(bTrainMSE[0:trainEnd])
trainRank5.extend(bTrainRank5[0:trainEnd])
trainLoss.extend(bTrainLoss[0:trainEnd])
# update the validation lists print("Mean speed is " + str(np.mean(speeds)))
valRank1.extend(bValRank1[0:valEnd])
valRank5.extend(bValRank5[0:valEnd])
valLoss.extend(bValLoss[0:valEnd])
# plot the accuracies # plot the accuracies
plt.style.use("ggplot") # plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, len(trainRank1)), trainRank1,
label="train_rank1")
plt.plot(np.arange(0, len(trainRank5)), trainRank5,
label="train_rank5")
plt.plot(np.arange(0, len(valRank1)), valRank1,
label="val_rank1")
plt.plot(np.arange(0, len(valRank5)), valRank5,
label="val_rank5")
plt.title("{}: rank-1 and rank-5 accuracy on {}".format(
NETWORK, DATASET))
plt.xlabel("Epoch #")
plt.ylabel("Accuracy")
plt.legend(loc="lower right")
# plot the losses
plt.style.use("ggplot")
plt.figure() plt.figure()
plt.plot(np.arange(0, len(trainLoss)), trainLoss, plt.plot(train_iterations, train_mse,
label="train_loss") label="train")
plt.plot(np.arange(0, len(valLoss)), valLoss, plt.plot(validation_iterations, validation_mse,
label="val_loss") label="validation")
plt.title("{}: cross-entropy loss on {}".format(NETWORK, DATASET)) if EPOCH_WISE:
plt.xlabel("Epoch #") plt.xlabel("Epochs #")
plt.ylabel("Loss") else:
plt.xlabel("Iterations")
plt.ylabel("MSE")
plt.legend(loc="upper right") plt.legend(loc="upper right")
plt.grid()
plt.show() plt.show()
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -7,7 +7,7 @@ import mxnet as mx ...@@ -7,7 +7,7 @@ import mxnet as mx
import os import os
EXAMPLES_PATH = "/media/sveta/4991e634-dd81-4cb9-bf46-2fa9c7159263/TORCS_examples/" EXAMPLES_PATH = "/media/sveta/4991e634-dd81-4cb9-bf46-2fa9c7159263/TORCS_examples/"
MODEL_PATH = "model/dpnet1/dpnet_newest" MODEL_PATH = "../../dpnet_weights/normalized/dpnet_newest"
RAW_PATH = "/media/sveta/4991e634-dd81-4cb9-bf46-2fa9c7159263/TORCS_raw/" RAW_PATH = "/media/sveta/4991e634-dd81-4cb9-bf46-2fa9c7159263/TORCS_raw/"
...@@ -16,10 +16,10 @@ def main(): ...@@ -16,10 +16,10 @@ def main():
sym, arg_params, aux_params = mx.model.load_checkpoint(MODEL_PATH, 0) sym, arg_params, aux_params = mx.model.load_checkpoint(MODEL_PATH, 0)
mod = mx.mod.Module(symbol=sym, mod = mx.mod.Module(symbol=sym,
context=mx.cpu(), context=mx.cpu(),
data_names=['image'], data_names=['data'],
label_names=['predictions_label']) label_names=['predictions_label'])
mod.bind(for_training=False, mod.bind(for_training=False,
data_shapes=[('image', (1, 3, 210, 280))], data_shapes=[('data', (1, 3, 210, 280))],
label_shapes=mod._label_shapes) label_shapes=mod._label_shapes)
mod.set_params(arg_params, aux_params, allow_missing=True) mod.set_params(arg_params, aux_params, allow_missing=True)
...@@ -39,9 +39,10 @@ def main(): ...@@ -39,9 +39,10 @@ def main():
prob = prob[0].tolist() prob = prob[0].tolist()
# Plot ground truth against predicted # Plot ground truth against predicted
plt.plot(range(len(labels)), labels, label='Ground truth') plt.scatter(range(len(labels)), labels, marker='x', label='Ground truth')
plt.plot(range(len(prob)), prob, label='Predicted') plt.scatter(range(len(prob)), prob, marker = 'x', label='Predicted')
plt.legend() plt.legend()
plt.grid()
ax = plt.gca() ax = plt.gca()
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f')) ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
plt.show() plt.show()
......
...@@ -4,6 +4,7 @@ import csv ...@@ -4,6 +4,7 @@ import csv
import cv2 import cv2
import datetime import datetime
import h5py import h5py
import math
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import mxnet as mx import mxnet as mx
import numpy as np import numpy as np
...@@ -13,6 +14,8 @@ from sklearn.cross_validation import train_test_split ...@@ -13,6 +14,8 @@ from sklearn.cross_validation import train_test_split
import tarfile import tarfile
import os import os
TRAIN_LENGTH = 387851
TEST_REC = "torcs_test.rec" TEST_REC = "torcs_test.rec"
TRAIN_REC = "torcs_train.rec" TRAIN_REC = "torcs_train.rec"
...@@ -32,14 +35,16 @@ def main(): ...@@ -32,14 +35,16 @@ def main():
# leveldb_to_rec(start_date) # leveldb_to_rec(start_date)
# read_from_recordio() # read_from_recordio()
# compute_train_mean() # compute_train_mean()
test_normalization() # test_normalization()
# check_saved_labels()
check_saved_images()
def compute_train_mean(): def compute_train_mean():
record = mx.recordio.MXRecordIO(RAW_PATH + TRAIN_REC, "r") record = mx.recordio.MXRecordIO(RAW_PATH + TRAIN_REC, "r")
all_means = list() all_means = list()
all_std = list() all_std = list()
for i in range(387851): for i in range(TRAIN_LENGTH):
if i % 1000 == 0: if i % 1000 == 0:
print(i) print(i)
item = record.read() item = record.read()
...@@ -91,6 +96,28 @@ def read_from_recordio(): ...@@ -91,6 +96,28 @@ def read_from_recordio():
quotechar='|', quoting=csv.QUOTE_MINIMAL) quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(header[1].tolist()) spamwriter.writerow(header[1].tolist())
def check_saved_labels():
record = mx.recordio.MXRecordIO(RAW_PATH + TRAIN_REC, "r")
for i in range(TRAIN_LENGTH):
item = record.read()
header, img = mx.recordio.unpack_img(item)
affordance = header[1].tolist()
# if not any([True if a>=0.1 and a<=0.9 else False for a in affordance ]):
if any(math.isnan(item) for item in affordance):
print(affordance)
def check_saved_images():
record = mx.recordio.MXRecordIO(RAW_PATH + TRAIN_LENGTH, "r")
for i in range(TRAIN_LENGTH):
item = record.read()
header, img = mx.recordio.unpack_img(item)
try:
img = Image.fromarray(img)
img.verify()
except (IOError, SyntaxError) as e:
print('Bad file:', i)
def leveldb_to_rec(start_date): def leveldb_to_rec(start_date):
train_record = mx.recordio.MXRecordIO(RAW_PATH + TRAIN_REC, "w") train_record = mx.recordio.MXRecordIO(RAW_PATH + TRAIN_REC, "w")
......
#!/usr/bin/env bash
echo "Generating files.."
java -jar embedded-montiarc-emadl-generator-0.2.1-SNAPSHOT-jar-with-dependencies.jar -m src/models -r Safetynet -o generated
configuration Dpnet{
num_epoch : 100
batch_size : 64
context:cpu
normalize: true
optimizer : sgd{
learning_rate: 0.01
// reduce the learning rate starting from 0.01 every 8000 iterations by a factor of 0.9 (decrease by 10%)
learning_rate_decay: 0.9
step_size: 8000
}
}
component Dpnet{ component Dpnet{
ports in Z(0:255)^{3, 210, 280} image, ports in Z(0:255)^{3, 210, 280} data,
out Q(-oo:oo)^{14,1,1} predictions; out Q(-oo:oo)^{14,1,1} predictions;
implementation CNN { implementation CNN {
...@@ -15,7 +15,7 @@ component Dpnet{ ...@@ -15,7 +15,7 @@ component Dpnet{
Dropout() Dropout()
} }
image -> data ->
conv(kernel=(11,11), channels=96, convStride=(4,4)) -> conv(kernel=(11,11), channels=96, convStride=(4,4)) ->
conv(kernel=(5,5), channels=256, convStride=(4,4)) -> conv(kernel=(5,5), channels=256, convStride=(4,4)) ->
conv(kernel=(3,3), channels=384, hasPool=false) -> conv(kernel=(3,3), channels=384, hasPool=false) ->
...@@ -23,7 +23,7 @@ component Dpnet{ ...@@ -23,7 +23,7 @@ component Dpnet{
conv(kernel=(3,3), channels=256) -> conv(kernel=(3,3), channels=256) ->
fc() -> fc() ->
fc() -> fc() ->
FullyConnected(units=14, no_bias=true) -> FullyConnected(units=2, no_bias=true) ->
predictions predictions
} }
......
configuration Dpnet{ configuration Safetynet{
num_epoch : 100 num_epoch : 100
batch_size : 64 batch_size : 64
context:cpu context:cpu
normalize: true normalize: true
optimizer : sgd{ optimizer : sgd{
weight_decay : 0.0005 learning_rate: 0.01
// reduce the learning rate starting from 0.01 every 8000 iterations by a factor of 0.9 (decrease by 10%) // reduce the learning rate starting from 0.01 every 8000 iterations by a factor of 0.9 (decrease by 10%)
learning_rate_decay: 0.9 learning_rate_decay: 0.9
step_size: 8000 step_size: 8000
learning_rate_minimum: 0.01 weight_decay : 0.0005
} }
} }
component Safetynet{
ports in Z(0:255)^{3, 210, 280} data,
out Q(0:1)^{2,1,1} predictions;
implementation CNN {
def conv(kernel, channels, hasPool=true, convStride=(1,1)){
Convolution(kernel=kernel, channels=channels, stride=convStride) ->
Relu() ->
Pooling(pool_type="max", kernel=(3,3), stride=(2,2), ?=hasPool)
}
def fc(){
FullyConnected(units=4096) ->
Relu() ->
Dropout()
}
image ->
conv(kernel=(11,11), channels=96, convStride=(4,4)) ->
conv(kernel=(5,5), channels=256, convStride=(4,4)) ->
conv(kernel=(3,3), channels=384, hasPool=false) ->
conv(kernel=(3,3), channels=384, hasPool=false) ->
conv(kernel=(3,3), channels=256) ->
fc() ->
fc() ->
FullyConnected(units=2) ->
Softmax() ->
predictions
}
}
...@@ -23,7 +23,7 @@ class CNNCreator_dpnet: ...@@ -23,7 +23,7 @@ class CNNCreator_dpnet:
_data_dir_ = "/media/sveta/4991e634-dd81-4cb9-bf46-2fa9c7159263/TORCS_raw/" _data_dir_ = "/media/sveta/4991e634-dd81-4cb9-bf46-2fa9c7159263/TORCS_raw/"
_model_dir_ = "model/dpnet/" _model_dir_ = "model/dpnet/"
_model_prefix_ = "dpnet" _model_prefix_ = "dpnet"
_input_names_ = ['image'] _input_names_ = ['data']
_input_shapes_ = [(3,210,280)] _input_shapes_ = [(3,210,280)]
_output_names_ = ['predictions_label'] _output_names_ = ['predictions_label']
...@@ -139,7 +139,7 @@ class CNNCreator_dpnet: ...@@ -139,7 +139,7 @@ class CNNCreator_dpnet:
# train_iter, test_iter, data_mean, data_std = self.load_data(batch_size) # train_iter, test_iter, data_mean, data_std = self.load_data(batch_size)
train_iter, test_iter, data_mean, data_std = custom_functions.load_data_rec(self, batch_size) train_iter, test_iter, data_mean, data_std = custom_functions.load_data_rec(self._data_dir_, batch_size)
if self.module == None: if self.module == None:
if normalize: if normalize:
self.construct(mx_context, data_mean, data_std) self.construct(mx_context, data_mean, data_std)
...@@ -174,18 +174,18 @@ class CNNCreator_dpnet: ...@@ -174,18 +174,18 @@ class CNNCreator_dpnet:
def construct(self, context, data_mean=None, data_std=None): def construct(self, context, data_mean=None, data_std=None):
image = mx.sym.var("image", image = mx.sym.var("data",
shape=(0,3,210,280)) shape=(0,3,210,280))
# image, output shape: {[3,210,280]} # image, output shape: {[3,210,280]}
if not data_mean is None: if not data_mean is None:
assert(not data_std is None) # assert(not data_std is None)
_data_mean_ = mx.sym.Variable("_data_mean_", shape=(3,210,280), init=MyConstant(value=data_mean.tolist())) _data_mean_ = mx.sym.Variable("_data_mean_", shape=(3,210,280), init=MyConstant(value=data_mean.tolist()))
_data_mean_ = mx.sym.BlockGrad(_data_mean_) _data_mean_ = mx.sym.BlockGrad(_data_mean_)
_data_std_ = mx.sym.Variable("_data_std_", shape=(3,210,280), init=MyConstant(value=data_mean.tolist())) #_data_std_ = mx.sym.Variable("_data_std_", shape=(3,210,280), init=MyConstant(value=data_mean.tolist()))
_data_std_ = mx.sym.BlockGrad(_data_std_) #_data_std_ = mx.sym.BlockGrad(_data_std_)
image = mx.symbol.broadcast_sub(image, _data_mean_) image = mx.symbol.broadcast_sub(image, _data_mean_)
image = mx.symbol.broadcast_div(image, _data_std_) # image = mx.symbol.broadcast_div(image, _data_std_)
conv1_ = mx.symbol.pad(data=image, conv1_ = mx.symbol.pad(data=image,
mode='constant', mode='constant',
pad_width=(0,0,0,0,5,4,4,3), pad_width=(0,0,0,0,5,4,4,3),
...@@ -323,13 +323,25 @@ class CNNCreator_dpnet: ...@@ -323,13 +323,25 @@ class CNNCreator_dpnet:
p=0.5, p=0.5,
name="dropout7_") name="dropout7_")
fc7_ = mx.symbol.FullyConnected(data=dropout7_, fc7_ = mx.symbol.FullyConnected(data=dropout7_,
num_hidden=256,
no_bias=False,
name="fc7_")
relu8_ = mx.symbol.Activation(data=fc7_,
act_type='relu',
name="relu8_")
dropout8_ = mx.symbol.Dropout(data=relu8_,
p=0.5,
name="dropout8_")
fc8_ = mx.symbol.FullyConnected(data=dropout8_,
num_hidden=14, num_hidden=14,
no_bias=True, no_bias=True,
name="fc7_") name="fc8_")
predictions = mx.symbol.LinearRegressionOutput(data=fc7_, predictions = mx.symbol.LinearRegressionOutput(data=fc8_,
name="predictions") name="predictions")
self.module = mx.mod.Module(symbol=mx.symbol.Group([predictions]), self.module = mx.mod.Module(symbol=mx.symbol.Group([predictions]),
data_names=self._input_names_, data_names=self._input_names_,
label_names=self._output_names_, label_names=self._output_names_,
......
...@@ -5,7 +5,7 @@ import CNNCreator_dpnet ...@@ -5,7 +5,7 @@ import CNNCreator_dpnet
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger() logger = logging.getLogger()
handler = logging.FileHandler("train.log","w", encoding=None, delay="true") handler = logging.FileHandler("train.log","a", encoding=None, delay="true")
logger.addHandler(handler) logger.addHandler(handler)
dpnet = CNNCreator_dpnet.CNNCreator_dpnet() dpnet = CNNCreator_dpnet.CNNCreator_dpnet()
...@@ -16,12 +16,12 @@ if __name__ == "__main__": ...@@ -16,12 +16,12 @@ if __name__ == "__main__":
normalize = True, normalize = True,
optimizer = 'sgd', optimizer = 'sgd',
optimizer_params = { optimizer_params = {
'weight_decay': 5.0E-4, 'learning_rate': 0.01,
'learning_rate_minimum': 0.01,
'learning_rate_decay': 0.9, 'learning_rate_decay': 0.9,
'step_size': 8000} 'step_size': 8000}