Skip to content

Commit

Permalink
calculate class var and std
Browse files Browse the repository at this point in the history
  • Loading branch information
cmhungsteve committed Feb 4, 2018
1 parent c2ecd49 commit fa728fb
Showing 1 changed file with 51 additions and 9 deletions.
60 changes: 51 additions & 9 deletions CNN-Pred-Feat/visualizeFeatures.lua
Expand Up @@ -2,7 +2,7 @@

-- contact:
-- Min-Hung (Steve) Chen at <cmhungsteve@gatech.edu>
-- Last updated: 02/01/2018
-- Last updated: 02/03/2018

require 'xlua'
require 'torch'
Expand All @@ -23,8 +23,12 @@ op:option{'-idS', '--idSplit', action='store', dest='idSplit',
help='which split used for testing', default=1}
op:option{'-idV', '--idVideo', action='store', dest='idVideo',
help='video id of the extracted video', default=0}
op:option{'-s', '--save', action='store', dest='save',
op:option{'-sP', '--savePath', action='store', dest='savePath',
help='full path to the saved images', default='feature_images/'}
op:option{'-s', '--save', action='store', dest='save',
help='save the images or not', default='No'}
op:option{'-n', '--normalize', action='store', dest='normalize',
help='normalize the features or not', default='Yes'}

opt,args = op:parse()

Expand All @@ -39,20 +43,58 @@ print('featurePath: '..opt.featurePath)
print('Database: '..opt.nameDatabase)
print('Modality: '..opt.dataModality)
print('split #: '..idSplit)
if opt.save == 'Yes' then
print('will save images')
end
if opt.normalize == 'Yes' then
print('will normalize the features')
end

-- load the label
ucf101Label = require './ucf-101'
table.sort(ucf101Label)

----------------------------------------------
-- Load Data --
----------------------------------------------
feat = torch.load(opt.featurePath..opt.nameDatabase..'/data_feat_test_'..opt.dataModality..'_centerCrop_25f_sp'..idSplit..'.t7')
-- normalize the features
if opt.normalize == 'Yes' then
featMats = (feat.featMats-torch.mean(feat.featMats))/torch.std(feat.featMats)
else
featMats = feat.featMats
end

if idVideo > 0 then
print(feat.name[idVideo])
test = feat.featMats[idVideo]
image.save(opt.save..'test_'..feat.name[idVideo]..'.png', test)
else
for i=1,feat.featMats:size(1) do
print(feat.name[i])
test = feat.featMats[i]
image.save(opt.save..'test_'..feat.name[i]..'.png', test)
test = featMats[idVideo]

var_test = torch.mean(torch.var(test,2))
std_test = torch.mean(torch.std(test,2))
print(var_test, std_test)
if opt.save == 'Yes' then
image.save(opt.savePath..'test_'..feat.name[idVideo]..'.png', test)
end
else
var_std_allClass = torch.zeros(#ucf101Label,2)
for j=1, #ucf101Label do
print(ucf101Label[j])
var_allVideo = torch.Tensor()
std_allVideo = torch.Tensor()
for i=1,featMats:size(1) do
if string.match(feat.name[i], ucf101Label[j]) then
-- print(feat.name[i])
test = featMats[i]
var_test = torch.mean(torch.var(test,2))
std_test = torch.mean(torch.std(test,2))
var_allVideo = torch.cat(var_allVideo, torch.Tensor({var_test}))
std_allVideo = torch.cat(std_allVideo, torch.Tensor({std_test}))
end
end
var_std_allClass[j][1] = torch.mean(var_allVideo)
var_std_allClass[j][2] = torch.mean(std_allVideo)
end
print('variance & standard deviation of each class for '..opt.dataModality)
print(var_std_allClass)
torch.save('var_std_'..opt.dataModality..'.txt', var_std_allClass, 'ascii')
end

0 comments on commit fa728fb

Please sign in to comment.