Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions experiments/imagenet_tt_vgg16/cnn_pass_imagenet_mat.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
function [net, info] = cnn_pass_imagenet_mat(net, imdb, getBatch, outputDir, varargin)
%CNN_PASS_IMAGENET_MAT pass data through the network

opts.train = [] ;
opts.val = [] ;
opts.numEpochs = 300 ;
opts.batchSize = 256 ;
opts.useGpu = false ;
opts.learningRate = 0.001 ;
opts.continue = false ;
opts.expDir = fullfile('data','exp') ;
opts.figuresPath = fullfile('data','figures','fig') ;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's obsolete.

opts.conserveMemory = true ;
opts.sync = true ;
opts.prefetch = false ;
opts.weightDecay = 0.0005 ;
opts.useGpu = false;
opts.momentum = 0.9 ;
opts.errorType = 'multiclass' ;
opts.plotDiagnostics = false ;
opts = vl_argparse(opts, varargin) ;

if ~exist(opts.expDir, 'dir'), mkdir(opts.expDir) ; end
if isempty(opts.train), opts.train = find(imdb.images.set==1) ; end
if isempty(opts.val), opts.val = find(imdb.images.set==2) ; end
if isnan(opts.train), opts.train = [] ; end

% -------------------------------------------------------------------------
% Network initialization
% -------------------------------------------------------------------------



% -------------------------------------------------------------------------
% Validate
% -------------------------------------------------------------------------

rng(0) ;

if opts.useGpu
one = gpuArray(single(1)) ;
else
one = single(1) ;
end

startTime = tic;
info.train.objective = [] ;
info.train.error = [] ;
info.train.topFiveError = [] ;
info.train.speed = [] ;
info.val.objective = [] ;
info.val.error = [] ;
info.val.topFiveError = [] ;
info.val.speed = [] ;
info.time = [] ;
opts.expDir
res = [] ;
%---------------data saving data
dataDirMimic = outputDir;
if ~exist(dataDirMimic, 'dir')
mkdir(dataDirMimic);
end

if opts.useGpu
net = vl_simplenn_move(net, 'gpu') ;
end

for epoch=1:opts.numEpochs
val = opts.val ;

info.train.objective(end+1) = 0 ;
info.train.error(end+1) = 0 ;
info.train.topFiveError(end+1) = 0 ;
info.train.speed(end+1) = 0 ;
info.val.objective(end+1) = 0 ;
info.val.error(end+1) = 0 ;
info.val.topFiveError(end+1) = 0 ;
info.val.speed(end+1) = 0 ;
info.time(end+1) = 0;

% evaluation on validation set
lastProcessBatch = 1;
curBatchnumber = 0;
for t=lastProcessBatch:opts.batchSize:numel(val)
curBatchnumber = curBatchnumber + 1;
batch_time = tic ;
batch = val(t:min(t+opts.batchSize-1, numel(val))) ;
fprintf('validation: epoch %02d: processing batch %3d of %3d ...', epoch, ...
fix(t/opts.batchSize)+1, ceil(numel(val)/opts.batchSize)) ;
[im, labels] = getBatch(imdb, batch) ;
if opts.prefetch
nextBatch = val(t+opts.batchSize:min(t+2*opts.batchSize-1, numel(val))) ;
getBatch(imdb, nextBatch) ;
end
if opts.useGpu
im = gpuArray(im) ;
end

net.layers{end}.class = labels ;
opts.useGpu
[res, data_img] = vl_simplenn_imagenet_mat(net, im, [], res, ...
'disableDropout', true, ...
'conserveMemory', opts.conserveMemory, ...
'sync', opts.sync) ; %#ok
lastProcessBatch = t; %#ok
save(fullfile(outputDir,'curBatch.mat'),'lastProcessBatch')
save(fullfile(outputDir, strcat('data_img', num2str(curBatchnumber))), 'data_img');
% print information
batch_time = toc(batch_time) ;
speed = numel(batch)/batch_time;
info.val = updateError(opts, info.val, net, res, batch_time) ;
fprintf(' %.2f s (%.1f images/s)', batch_time, speed) ;
n = t + numel(batch) - 1 ;
if strcmp(opts.errorType, 'mse')
fprintf(' err %.5f\n', info.val.error(end)/n) ;
else
fprintf(' err %.1f err5 %.1f', ...
info.val.error(end)/n*100, info.val.topFiveError(end)/n*100) ;
fprintf('\n') ;
end
end

end

% -------------------------------------------------------------------------
function info = updateError(opts, info, net, res, speed)
% -------------------------------------------------------------------------
predictions = gather(res(end-1).x) ;
sz = size(predictions) ;
n = prod(sz(1:2)) ;

labels = net.layers{end}.class ;
info.objective(end) = info.objective(end) + sum(double(gather(res(end).x))) ;
info.speed(end) = info.speed(end) + speed ;
switch opts.errorType
case 'multiclass'
[~,predictions] = sort(predictions, 3, 'descend') ;
error = ~bsxfun(@eq, predictions, reshape(labels, 1, 1, 1, [])) ;
info.error(end) = info.error(end) +....
sum(sum(sum(error(:,:,1,:))))/n ;
info.topFiveError(end) = info.topFiveError(end) + ...
sum(sum(sum(min(error(:,:,1:5,:),[],3))))/n ;
case 'binary'
error = bsxfun(@times, predictions, labels) < 0 ;
info.error(end) = info.error(end) + sum(error(:))/n ;
case 'mse'
error = predictions - labels;
info.error(end) = info.error(end) + sum(error(:).^2);

end

% -------------------------------------------------------------------------

81 changes: 81 additions & 0 deletions experiments/imagenet_tt_vgg16/evaluate_imagenet_tt.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
function info = evaluate_imagenet_tt(varargin)
% evaluate_imagenet_tt evauate vgg16-tt models on imagenet
opts.data_path = fullfile('data', 'imagenet12','data_path') ;
opts.dataDir = fullfile('data', 'imagenet12') ;
opts.expDir = fullfile('data', 'imagenet12-eval-vgg-f') ;
opts.imdbPath = fullfile(opts.expDir, 'imdb.mat');
opts.modelPath = fullfile('data', 'models', 'imagenet-vgg-f.mat') ;
opts.lite = false ;
opts.numFetchThreads = 8 ;
opts.train.batchSize = 64 ;
opts.train.numEpochs = 1 ;
opts.train.gpus = [1];
opts.train.prefetch = false ;
opts.train.expDir = opts.expDir ;

opts = vl_argparse(opts, varargin) ;
display(opts);

% -------------------------------------------------------------------------
% Database initialization
% -------------------------------------------------------------------------

if exist(opts.imdbPath)
imdb = load(opts.imdbPath) ;
else
imdb = cnn_imagenet_setup_data('dataDir', opts.dataDir, 'lite', opts.lite) ;
mkdir(opts.expDir) ;
save(opts.imdbPath, '-struct', 'imdb') ;
end

% -------------------------------------------------------------------------
% Network initialization
% -------------------------------------------------------------------------

net = load(opts.modelPath) ;
net.layers{end}.type = 'softmaxloss' ; % softmax -> softmaxloss

% Synchronize label indexes between the model and the image database
imdb = cnn_imagenet_sync_labels(imdb, net);

% -------------------------------------------------------------------------
% Stochastic gradient descent
% -------------------------------------------------------------------------

data_path = fullfile(opts.data_path, 'data_img');
getBatchWrapper = @(imdb,batch) getBatch(imdb, batch, data_path);
[net,info] = cnn_train(net, imdb, getBatchWrapper, opts.train, ...
'conserveMemory', true, ...
'train', NaN, ...
'val', find(imdb.images.set==2)) ;

% -------------------------------------------------------------------------
function [im,labels] = getBatch(imdb, batch, data_path)
% -------------------------------------------------------------------------
%data_path = '../../data_permanent/data_imagenet_mimic_deep/data_img';
sizeBatch = 64;
%batch(1)
for i = 1 : numel(batch)
%for val without train
nFile = ceil((batch(i) - 1281167) / 64);
%nEl = batch(i) - 64 * floor((batch(i) - 1281167) / 64);

% nFile = ceil(batch(i) / sizeBatch);
% fprintf('nFile %d\n', nFile);
batchData = load(strcat(data_path, num2str(nFile), '.mat'));
%batchData
batchData = batchData.data_img;
if i == 1
im_size = size(batchData);
im_size(4) = numel(batch);
im =single(zeros(im_size));
end
% batch(i) - (nFile + 20019-1 - 1) * sizeBatch
% batch(i)
%size(batchData)
%for val without train
im(:,:,:,i) = batchData(:,:,:, batch(i) - 15 - (nFile + 20019-1 - 1) * sizeBatch);
end
labels = imdb.images.label(batch) ;
%size(labels)
%size(im)
64 changes: 64 additions & 0 deletions experiments/imagenet_tt_vgg16/make_data_mat.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function info = make_data_mat(varargin)
% MAKE_DATA_MAT pass data though the network and save it
opts.outputDir = fullfile('data', 'imagenet_TT','outputDir') ;
opts.dataDir = fullfile('data', 'imagenet_TT') ;
opts.expDir = fullfile('data', 'imagenet_TT') ;
opts.imdbPath = fullfile(opts.expDir, 'imdb.mat');
opts.modelPath = fullfile('data', 'models', 'imagenet-vgg-deep-16.mat') ;
opts.lite = false ;
opts.numFetchThreads = 8 ;
opts.train.batchSize = 64 ;
opts.train.numEpochs = 1 ;
opts.train.useGpu = true;
opts.train.prefetch = false ;
opts.train.expDir = opts.expDir ;

opts = vl_argparse(opts, varargin) ;
display(opts);

% -------------------------------------------------------------------------
% Database initialization
% -------------------------------------------------------------------------

if exist(opts.imdbPath)
imdb = load(opts.imdbPath) ;
else
imdb = cnn_imagenet_setup_data('dataDir', opts.dataDir, 'lite', opts.lite) ;
mkdir(opts.expDir) ;
save(opts.imdbPath, '-struct', 'imdb') ;
end

% -------------------------------------------------------------------------
% Network initialization
% -------------------------------------------------------------------------

net = load(opts.modelPath) ;
net.layers{end}.type = 'softmaxloss' ; % softmax -> softmaxloss

% Synchronize label indexes between the model and the image database
imdb = cnn_imagenet_sync_labels(imdb, net);

% -------------------------------------------------------------------------
% Stochastic gradient descent
% -------------------------------------------------------------------------

fn = getBatchWrapper(net.normalization, opts.numFetchThreads) ;

[~,info] = cnn_pass_imagenet_mat(net, imdb, fn, opts.outputDir, opts.train, ...
'conserveMemory', true, ...
'train', NaN, ...
'val', find(imdb.images.set == 2)) ;

% -------------------------------------------------------------------------
function fn = getBatchWrapper(opts, numThreads)
% -------------------------------------------------------------------------
fn = @(imdb,batch) getBatch(imdb,batch,opts,numThreads) ;

% -------------------------------------------------------------------------
function [im,labels] = getBatch(imdb, batch, opts, numThreads)
% -------------------------------------------------------------------------
images = strcat([imdb.imageDir filesep], imdb.images.name(batch)) ;
im = cnn_imagenet_get_batch(images, opts, ...
'numThreads', numThreads, ...
'prefetch', nargout == 0) ;
labels = imdb.images.label(batch) ;
63 changes: 63 additions & 0 deletions experiments/imagenet_tt_vgg16/resize_from_tar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash
# sudo apt-get update
# sudo apt-get install imagemagick
# Start in folder with original .tar files
#Before launch set PACKEDTRAINDIR and DATA_OUT
# Variables
PACKEDTRAINDIR=/home/ubuntu/tmp/train_packed
DATA_OUT=/home/ubuntu/imagenet_mimic
TRAINDIR=$DATA_OUT/images/train
VALDIR=$DATA_OUT/images/val

mkdir $DATA_OUT
mkdir $DATA_OUT/images
# Unpack main train archive
mkdir $PACKEDTRAINDIR
tar -xvf ILSVRC2012_img_train.tar -C $PACKEDTRAINDIR

# Unpack & resize nested train archives
mkdir $TRAINDIR

for NAME in $PACKEDTRAINDIR/*.tar; do
INDEX=$(basename $NAME .tar)
echo $INDEX
if test -d $TRAINDIR/$INDEX; then
echo "Folder "$TRAINDIR/$INDEX" exists"
else
mkdir $TRAINDIR/$INDEX
tar -xf $PACKEDTRAINDIR/$INDEX.tar -C $TRAINDIR/$INDEX
# Resize to height to 256, preserving the aspect ratio
mogrify -resize 256x256^ "$TRAINDIR/$INDEX/*.JPEG"
fi
done

# Validation
mkdir $VALDIR
tar -xf ILSVRC2012_img_val.tar -C $VALDIR
mogrify -resize 256x256^ "$VALDIR/*.JPEG"


# CMYK -> RGB. Important
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a short comment explaining why this is needed?

mogrify -colorspace rgb $TRAINDIR/n03062245/n03062245_4620.JPEG
mogrify -colorspace rgb $TRAINDIR/n04264628/n04264628_27969.JPEG
mogrify -colorspace rgb $TRAINDIR/n03961711/n03961711_5286.JPEG
mogrify -colorspace rgb $TRAINDIR/n01739381/n01739381_1309.JPEG
mogrify -colorspace rgb $TRAINDIR/n04258138/n04258138_17003.JPEG
mogrify -colorspace rgb $TRAINDIR/n03018349/n03018349_4028.JPEG
mogrify -colorspace rgb $TRAINDIR/n04336792/n04336792_7448.JPEG
mogrify -colorspace rgb $TRAINDIR/n02492035/n02492035_15739.JPEG
mogrify -colorspace rgb $TRAINDIR/n03544143/n03544143_17228.JPEG
mogrify -colorspace rgb $TRAINDIR/n03467068/n03467068_12171.JPEG
mogrify -colorspace rgb $TRAINDIR/n03633091/n03633091_5218.JPEG
mogrify -colorspace rgb $TRAINDIR/n02447366/n02447366_23489.JPEG
mogrify -colorspace rgb $TRAINDIR/n03347037/n03347037_9675.JPEG
mogrify -colorspace rgb $TRAINDIR/n02077923/n02077923_14822.JPEG
mogrify -colorspace rgb $TRAINDIR/n02747177/n02747177_10752.JPEG
mogrify -colorspace rgb $TRAINDIR/n04371774/n04371774_5854.JPEG
mogrify -colorspace rgb $TRAINDIR/n07583066/n07583066_647.JPEG
mogrify -colorspace rgb $TRAINDIR/n04596742/n04596742_4225.JPEG
mogrify -colorspace rgb $TRAINDIR/n13037406/n13037406_4650.JPEG
mogrify -colorspace rgb $TRAINDIR/n03529860/n03529860_11437.JPEG
mogrify -colorspace rgb $TRAINDIR/n03710637/n03710637_5125.JPEG
mogrify -colorspace rgb $TRAINDIR/n04033995/n04033995_2932.JPEG
mogrify -colorspace rgb $VALDIR/ILSVRC2012_val_00019877.JPEG
Loading