-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtensorPrep.m
More file actions
89 lines (74 loc) · 2.43 KB
/
tensorPrep.m
File metadata and controls
89 lines (74 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function tensorPrep(training, task)
% Generate image and label tensors for a ConvNet in Marvin
% The images are warped/cropped from the panoramas according to angles determined in panos_x.m
% INPUTS
% training - boolean indicating if generated tensors are for training or testing
% task - string indicating the task (e.g., 'bike_lane' - see panos_x.m)
addpath(genpath('dependencies'));
panoDir = 'panos/'; % directory containing GSV panoramas and xml metadata
imgsize = 227;
% Load variables
matVar = load('processed_panos.mat');
panos = matVar.panos;
files = matVar.files;
% Remove files
files([panos.remove]) = [];
panos([panos.remove]) = [];
% Train/test longitudinal thresholding
panoCoords = cell2mat({panos.coords}');
longitudes = sort(panoCoords(:,2));
long_thresh = longitudes(round(0.8*length(longitudes))); % arbitrary 4:1 split
fileRemove = false(length(panos));
if training
fileRemove(panoCoords(:,2) > long_thresh) = true;
data_name = 'train';
else
fileRemove(panoCoords(:,2) <= long_thresh) = true;
data_name = 'test';
end
files(fileRemove) = [];
panos(fileRemove) = [];
% Determine pano crops and labels for the specified task
[files, headings, returned_labels] = panos_x(panos, files, task);
% Preallocate tensors
data = zeros(imgsize, imgsize, 3, length(files), 'uint8');
labels = zeros(1,1,1,size(data,4));
% Generate tensors
parfor_progress(['preparing ' task], length(files));
parfor i = 1:length(files)
img = imread([panoDir files{i}(1:end-4) '.jpg']);
img = panoWarp(img, headings(i));
data(:,:,:,i) = img;
labels(1,1,1,i) = returned_labels(i);
parfor_progress(['preparing ' task]);
end
% Save tensors
data_num = size(data, 4);
saveDir = ['tensors/' task];
if ~exist(saveDir,'dir')
mkdir(saveDir)
end
% Image tensor
tensor.type = 'uint8';
tensor.sizeof = 1;
tensor.name = 'images';
tensor.value = data;
tensor.dim = 4;
writeTensors(sprintf('%s/%s_images_%d.tensor', saveDir, data_name, data_num), tensor);
% Mean image tensor
if strcmp(data_name, 'train')
tensor.type = 'half';
tensor.sizeof = 2;
tensor.name = 'mean';
tensor.value = single(mean(data,4));
tensor.dim= 3;
writeTensors(sprintf('%s/%s_mean_%d.tensor',saveDir, data_name, data_num), tensor);
end
% Label tensor
tensor.type = 'half';
tensor.sizeof = 2;
tensor.name = 'labels';
tensor.value = single(labels);
tensor.dim = 4;
writeTensors(sprintf('%s/%s_labels_%d.tensor',saveDir, data_name, data_num), tensor);
end