├── Results ├── README.md ├── Training_mnist_advCnn.png └── Reconstructions_mnist_cnn.png ├── Cluster Visualisation ├── visualiseCluster.m ├── convert_dict_to_features.lua ├── README.md ├── calc_imp_bits.lua └── advAE_cnn_mnist.lua ├── std_mean_ret.lua ├── BinarizedNeurons.lua ├── prepare_dict.lua ├── mnist_deploy.lua ├── retrieval_from_dict.lua ├── FGVC └── dataPrep.lua ├── Auto-encoder-cuda ├── deploy_adversarialAE_alex.lua ├── DenoisingAE.lua ├── VariationalAE.lua ├── AdversarialAE.lua └── AdversarialAE_alex.lua ├── advAE_cnn_mnist.lua ├── Pretrainedencoder.py └── resnet.py /Results/README.md: -------------------------------------------------------------------------------- 1 | # Result Readme 2 | This folder contains the training curves and reconstruction images. 3 | -------------------------------------------------------------------------------- /Results/Training_mnist_advCnn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arnaghosh/Auto-Encoder/HEAD/Results/Training_mnist_advCnn.png -------------------------------------------------------------------------------- /Results/Reconstructions_mnist_cnn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arnaghosh/Auto-Encoder/HEAD/Results/Reconstructions_mnist_cnn.png -------------------------------------------------------------------------------- /Cluster Visualisation/visualiseCluster.m: -------------------------------------------------------------------------------- 1 | clear; 2 | load('AdvAE/mnist_dict_with_mappingMat.mat'); 3 | dict_modif = max(dict,0); 4 | new_dict = dict_modif*mappingMat; 5 | colors = reshape(randperm(30),10,3); colors = colors/max(colors(:)); 6 | x = linspace(1,10,10); 7 | y = linspace(1,10,10); 8 | figure(2);scatter(x,y,100,colors,'filled'); 9 | color_points(:,:) = colors(label(:),:); 10 | figure(1);scatter(new_dict(:,1),new_dict(:,2),30,color_points,'filled') -------------------------------------------------------------------------------- /std_mean_ret.lua: -------------------------------------------------------------------------------- 1 | require 'nn'; 2 | require 'image'; 3 | mnist = require 'mnist'; 4 | require 'optim'; 5 | require 'gnuplot'; 6 | require 'cutorch'; 7 | require 'cunn'; 8 | require 'cudnn'; 9 | require './BinarizedNeurons' 10 | local fashion_mnist = require 'fashion-mnist'; 11 | ret_vec=torch.load("/home/siplab/AE/AE/fashion_mnist_retrieval_cvpr.t7") 12 | mytable={} 13 | mytable2={} 14 | for j=1,10 do 15 | x=0 16 | mytable3={} 17 | count=1 18 | y=torch.zeros(1000) 19 | for i=1,10000 do 20 | 21 | if (ret_vec[i][1]==j) 22 | then temp=ret_vec[i][2] x=x+temp y[count] = temp count=count+1 23 | --print(ret_vec[i][1],ret_vec[i][2],ret_vec[i][3],y) 24 | end 25 | 26 | end 27 | 28 | table.insert(mytable,j,torch.std(y)) 29 | table.insert(mytable2,j,x/count) 30 | end 31 | print(mytable,mytable2) 32 | -------------------------------------------------------------------------------- /BinarizedNeurons.lua: -------------------------------------------------------------------------------- 1 | local BinarizedNeurons,parent = torch.class('BinarizedNeurons', 'nn.Module') 2 | 3 | 4 | function BinarizedNeurons:__init(stcFlag) 5 | parent.__init(self) 6 | self.stcFlag = stcFlag 7 | self.randmat=torch.Tensor(); 8 | self.outputR=torch.Tensor(); 9 | end 10 | function BinarizedNeurons:updateOutput(input) 11 | self.randmat:resizeAs(input); 12 | self.outputR:resizeAs(input); 13 | self.output:resizeAs(input); 14 | self.outputR:copy(input):add(1):div(2) 15 | if self.train and self.stcFlag then 16 | local mask=self.outputR-self.randmat:rand(self.randmat:size()) 17 | self.output=mask:sign() 18 | else 19 | self.output:copy(self.outputR):add(-0.5):sign() 20 | end 21 | return self.output 22 | end 23 | 24 | function BinarizedNeurons:updateGradInput(input, gradOutput) 25 | self.gradInput:resizeAs(gradOutput) 26 | self.gradInput:copy(gradOutput) --:mul(0.5) 27 | return self.gradInput 28 | end 29 | -------------------------------------------------------------------------------- /Cluster Visualisation/convert_dict_to_features.lua: -------------------------------------------------------------------------------- 1 | require 'nn'; 2 | require 'image'; 3 | mnist = require 'mnist'; 4 | matio = require 'matio' 5 | require 'optim'; 6 | require 'gnuplot'; 7 | require 'cutorch'; 8 | require 'cunn'; 9 | --require 'cudnn'; 10 | 11 | Data = torch.load('AdvAE/mnist_dict.t7') 12 | Labels = (mnist.traindataset().label+1) 13 | --print(dict_data:size(), trainlabels:size()) 14 | zSize = Data:size(2) 15 | 16 | bit_imp = torch.load('AdvAE/bits_imp.t7') 17 | bit_imp = bit_imp - bit_imp[zSize+1] 18 | 19 | _,index = torch.sort(bit_imp:narrow(1,1,zSize),true); --true for descending order 20 | 21 | feature_mapper = torch.zeros(zSize,2); 22 | 23 | for b=1,zSize do 24 | if b%2==1 then --1st feature vector changed 25 | feature_mapper[index[b]][1] = 2^torch.floor((zSize-b)/2); 26 | feature_mapper[index[b]][2] = 0; 27 | else 28 | feature_mapper[index[b]][2] = 2^torch.floor((zSize-b)/2); 29 | feature_mapper[index[b]][1] = 0; 30 | end 31 | end 32 | print(index) 33 | print(feature_mapper) 34 | 35 | matio.save('AdvAE/mnist_dict_with_mappingMat.mat',{dict=Data,label=Labels,mappingMat=feature_mapper}); -------------------------------------------------------------------------------- /Cluster Visualisation/README.md: -------------------------------------------------------------------------------- 1 | # How to run :- 2 | + [advAE_cnn_mnist.lua](https://github.com/arnaghosh/Auto-Encoder/blob/master/Cluster%20Visualisation/advAE_cnn_mnist.lua) - Run this if adversary not saved. This is adhoc. Preferable to use the adversary. This saves another network with same structure as adversary trained on discriminating the classes from dictionary codes. 3 | + [calc_imp_bits.lua](https://github.com/arnaghosh/Auto-Encoder/blob/master/Cluster%20Visualisation/calc_imp_bits.lua) - This calculates the importance of each bit by forwarding the inputs with a bit masked and seeing hwo classification error changes. Masking the more important bit will significantly increase error. 4 | + [convert_dict_to_features.lua](https://github.com/arnaghosh/Auto-Encoder/blob/master/Cluster%20Visualisation/convert_dict_to_features.lua) - The bits are split into 2 halves - alternate ones form the basis for each feature (in 2D space). So, the top 2 most important bits form the MSB of the 2 features. This creates the transformation matrix for dictionary. 5 | + [visualiseCluster.m](https://github.com/arnaghosh/Auto-Encoder/blob/master/Cluster%20Visualisation/visualiseCluster.m) - Converts the dictionary to 2D feature space and generates a scatter plot with each class with a unique color. Also generates a figure showing the color label of each class. 6 | 7 | Hope this helps visualise clusters in 2D space for any dataset. 8 | -------------------------------------------------------------------------------- /prepare_dict.lua: -------------------------------------------------------------------------------- 1 | require 'nn'; 2 | require 'image'; 3 | mnist = require 'mnist'; 4 | require 'optim'; 5 | require 'gnuplot'; 6 | --require 'cutorch'; 7 | --require 'cunn'; 8 | --require 'cudnn'; 9 | require './BinarizedNeurons' 10 | 11 | zSize = 14 12 | --encoder 13 | encoder = torch.load('/media/arna/340fd3c9-2648-4333-9ec9-239babc34bb7/arna_data/AdvAE_data/encoder1.t7'); 14 | 15 | binariser = nn.Sequential(); 16 | binariser:add(BinarizedNeurons()) 17 | 18 | autoencoder = nn.Sequential() 19 | autoencoder:add(encoder) 20 | autoencoder:add(binariser) 21 | 22 | autoencoder = autoencoder--:cuda() 23 | print(autoencoder) 24 | 25 | --load MNIST data 26 | trainData = mnist.traindataset().data:double():div(255):reshape(60000,1,28,28)--:cuda() 27 | trainlabels = (mnist.traindataset().label+1)--:cuda() 28 | N = mnist.traindataset().size 29 | 30 | testData = mnist.testdataset().data:double():div(255):reshape(10000,1,28,28)--:cuda() 31 | testlabels = (mnist.testdataset().label+1)--:cuda() 32 | teSize = mnist.testdataset().size 33 | print(N,teSize) 34 | 35 | local dict = torch.Tensor(N,zSize); 36 | 37 | local x,y 38 | 39 | batchSize = 3000 40 | iterations = 50 41 | 42 | 43 | --Train 44 | print('Dictionary preparing') 45 | 46 | for n=1,N,batchSize do 47 | collectgarbage() 48 | x = trainData:narrow(1,n,batchSize)--:cuda() 49 | --print(x:size()) 50 | x1 = autoencoder:forward(x) 51 | dict[{{n,n+batchSize-1},{}}] = x1; 52 | end 53 | 54 | torch.save('AdvAE/mnist_dict.t7',dict) -------------------------------------------------------------------------------- /mnist_deploy.lua: -------------------------------------------------------------------------------- 1 | require 'nn'; 2 | require 'image'; 3 | mnist = require 'mnist'; 4 | require 'optim'; 5 | require 'gnuplot'; 6 | require 'cutorch'; 7 | require 'cunn'; 8 | require 'cudnn'; 9 | require './BinarizedNeurons' 10 | 11 | classes ={0,1,2,3,4,5,6,7,8,9} 12 | zSize = 16 13 | --encoder 14 | encoder = torch.load('/home/siplab/AE/mnist/encoder_1.t7'); 15 | classifier=torch.load('/home/siplab/AE/mnist/classifier.t7'); 16 | binariser = nn.Sequential(); 17 | binariser:add(BinarizedNeurons()) 18 | 19 | autoencoder = nn.Sequential() 20 | autoencoder:add(encoder) 21 | autoencoder:add(binariser) 22 | autoencoder:add(classifier) 23 | autoencoder = autoencoder:cuda() 24 | print(autoencoder) 25 | 26 | --load MNIST data 27 | trainData = mnist.traindataset().data:double():div(255):reshape(60000,1,28,28):cuda() 28 | trainlabels = (mnist.traindataset().label+1):cuda() 29 | N = mnist.traindataset().size 30 | 31 | testData = mnist.testdataset().data:double():div(255):reshape(10000,1,28,28):cuda() 32 | testlabels = (mnist.testdataset().label+1):cuda() 33 | teSize = mnist.testdataset().size 34 | print(N,teSize) 35 | class_performance = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 36 | gt=torch.Tensor(10000,1):cuda() 37 | for i=1,10000 do 38 | gt[i]=testlabels[i] 39 | end 40 | count=0 41 | 42 | for n=1,10000 do 43 | local groundtruth = gt[n] 44 | x=autoencoder:forward(testData[n]) 45 | confidences, indices = torch.sort(x, true) 46 | print (indices[1],gt[n]) 47 | if torch.Tensor(1):fill(indices[1]):cuda():equal(groundtruth) then 48 | --print(1,indices[1]) 49 | count=count+1 50 | --print(2,groundtruth) 51 | class_performance[indices[1]] = class_performance[indices[1]] + 1 52 | end 53 | end 54 | end 55 | for i=1,#classes do 56 | print (class_performance[i]) 57 | end 58 | for i=1,#classes do 59 | print(classes[i], 100*class_performance[i]/1000 .. ' %') 60 | end 61 | -------------------------------------------------------------------------------- /Cluster Visualisation/calc_imp_bits.lua: -------------------------------------------------------------------------------- 1 | require 'nn'; 2 | require 'image'; 3 | mnist = require 'mnist'; 4 | require 'optim'; 5 | require 'gnuplot'; 6 | require 'cutorch'; 7 | require 'cunn'; 8 | --require 'cudnn'; 9 | 10 | Data = torch.load('AdvAE/mnist_dict.t7') 11 | Labels = (mnist.traindataset().label+1) 12 | --print(dict_data:size(), trainlabels:size()) 13 | zSize = Data:size(2) 14 | 15 | adversary = torch.load('AdvAE/adversary.t7') 16 | adversary:cuda() 17 | 18 | print("all data in GPU") 19 | 20 | 21 | N = Data:size(1) 22 | local theta,gradTheta = adversary:getParameters() 23 | criterion = nn.ClassNLLCriterion():cuda() 24 | 25 | local x,y 26 | 27 | local feval = function(params) 28 | if theta~=params then 29 | theta:copy(params) 30 | end 31 | gradTheta:zero() 32 | out = adversary:forward(x) 33 | --print(#x,#out,#y) 34 | local loss = criterion:forward(out,y) 35 | local gradLoss = criterion:backward(out,y) 36 | adversary:backward(x,gradLoss) 37 | return loss, gradTheta 38 | end 39 | 40 | batchSize = 250 41 | 42 | errorTensor = torch.zeros(zSize+1); 43 | local optimParams = {learningRate = 0.001, learningRateDecay = 0.0001} 44 | local _,loss 45 | local losses = {} 46 | for n=1,N-batchSize, batchSize do 47 | x = Data:narrow(1,n,batchSize):cuda() 48 | y = Labels:narrow(1,n,batchSize):cuda() 49 | --print(y) 50 | _,loss = optim.adam(feval,theta,optimParams) 51 | losses[#losses + 1] = loss[1] 52 | --print('Batch '.. n..' done') 53 | errorTensor[zSize+1] = errorTensor[zSize+1] + loss[1]*batchSize; 54 | end 55 | 56 | for f=1,zSize do 57 | for n=1,N-batchSize, batchSize do 58 | x = Data:narrow(1,n,batchSize):cuda() 59 | x[{{},{f}}]:fill(0) 60 | --print(f,x[1]) 61 | y = Labels:narrow(1,n,batchSize):cuda() 62 | --print(y) 63 | _,loss = optim.adam(feval,theta,optimParams) 64 | losses[#losses + 1] = loss[1] 65 | --print('Batch '.. n..' done') 66 | errorTensor[f] = errorTensor[f] + loss[1]*batchSize; 67 | end 68 | end 69 | 70 | torch.save('AdvAE/bits_imp.t7',errorTensor); 71 | -------------------------------------------------------------------------------- /retrieval_from_dict.lua: -------------------------------------------------------------------------------- 1 | require 'nn'; 2 | require 'image'; 3 | mnist = require 'mnist'; 4 | require 'optim'; 5 | require 'gnuplot'; 6 | require 'cutorch'; 7 | require 'cunn'; 8 | --require 'cudnn'; 9 | require './BinarizedNeurons' 10 | 11 | zSize = 14 12 | --encoder 13 | encoder = torch.load('/media/arna/340fd3c9-2648-4333-9ec9-239babc34bb7/arna_data/AdvAE_data/encoder1.t7'); 14 | 15 | binariser = nn.Sequential(); 16 | binariser:add(BinarizedNeurons()) 17 | 18 | autoencoder = nn.Sequential() 19 | autoencoder:add(encoder) 20 | autoencoder:add(binariser) 21 | 22 | autoencoder = autoencoder:cuda() 23 | print(autoencoder) 24 | 25 | --load MNIST data 26 | trainData = mnist.traindataset().data:double():div(255):reshape(60000,1,28,28) 27 | trainlabels = (mnist.traindataset().label+1) 28 | N = mnist.traindataset().size 29 | 30 | testData = mnist.testdataset().data:double():div(255):reshape(10000,1,28,28):cuda() 31 | testlabels = (mnist.testdataset().label+1):cuda() 32 | teSize = mnist.testdataset().size 33 | print(N,teSize) 34 | 35 | local dict = torch.load('AdvAE/mnist_dict.t7'); 36 | 37 | local x,y 38 | 39 | batchSize = 3000 40 | iterations = 50 41 | num_retrieval = 5; 42 | retrieval_vec = torch.Tensor(teSize,3) 43 | 44 | --Train 45 | print('Dictionary retrieving') 46 | x1 = autoencoder:forward(testData) 47 | --testData = nil; 48 | --autoencoder = nil; 49 | --collectgarbage() 50 | --dict = dict:cuda() 51 | dict_val = torch.Tensor(dict:size(1)) 52 | dict_val = torch.mm(x1:double(),dict:transpose(1,2)) 53 | print("mult done") 54 | for n=1,teSize do 55 | collectgarbage() 56 | if n%100==0 then print("yeah") end 57 | --x = testData[n]; 58 | --print(x:size()) 59 | --x1 = autoencoder:forward(x) 60 | retrieval_vec[n][1] = testlabels[n]; 61 | retrieval_vec[n][2] = 0; 62 | retrieval_vec[n][3] = 0; 63 | for i=1,dict:size(1) do 64 | --dict_val[i] = x1:dot(dict[i]:cuda()) 65 | if (dict_val[n][i]>=zSize-4) then --- Hamming Distance<=2 --> dot product val>=zSize-4 66 | retrieval_vec[n][2] = retrieval_vec[n][2]+1; 67 | if(trainlabels[i]==testlabels[n]) then 68 | retrieval_vec[n][3] = retrieval_vec[n][3]+1; 69 | end 70 | end 71 | end 72 | --_,index = torch.sort(dict_val,true) 73 | --retrieval_vec[{{n},{2,num_retrieval+1}}] = trainlabels:index(1,index[{{1,num_retrieval}}]:long()); 74 | end 75 | 76 | torch.save('AdvAE/mnist_retrieval.t7',retrieval_vec) 77 | -------------------------------------------------------------------------------- /FGVC/dataPrep.lua: -------------------------------------------------------------------------------- 1 | require 'image'; 2 | 3 | folder = "/media/arna/340fd3c9-2648-4333-9ec9-239babc34bb7/arna_data/FGVC/fgvc-aircraft-2013b/data/" 4 | 5 | families = {}; 6 | family_file = folder.."families.txt"; 7 | family_f = io.open(family_file); 8 | l=0; 9 | for _ in io.lines(family_file) do 10 | families[_] = l; 11 | l=l+1; 12 | end 13 | 14 | variants = {}; 15 | variant_file = folder.."variants.txt"; 16 | variant_f = io.open(variant_file); 17 | l=0; 18 | for _ in io.lines(variant_file) do 19 | variants[_] = l; 20 | l=l+1; 21 | end 22 | 23 | manufacturers = {}; 24 | man_file = folder.."manufacturers.txt"; 25 | man_f = io.open(man_file); 26 | l=0; 27 | for _ in io.lines(man_file) do 28 | manufacturers[_] = l; 29 | l=l+1; 30 | end 31 | 32 | 33 | imNames_file = folder.."images_train.txt"; 34 | imNames_f = io.open(imNames_file); 35 | l=0; 36 | imNames = {}; 37 | for _ in io.lines(imNames_file) do 38 | l=l+1; 39 | --img = image.load(folder..'images/'.._..'.jpg'); 40 | imNames[l] = _; 41 | end 42 | 43 | print(#imNames) 44 | 45 | im_fam_file = folder.."images_family_train.txt"; 46 | im_fam_f = io.open(im_fam_file); 47 | l=0; 48 | im_fam = {}; 49 | for _ in io.lines(im_fam_file) do 50 | l=l+1; 51 | --img = image.load(folder..'images/'.._..'.jpg'); 52 | x = _:split(" "); 53 | im_fam[l]=x[2]; 54 | for i=3,#x do 55 | im_fam[l] = im_fam[l]..' '..x[i]; 56 | end 57 | im_fam[l] = families[im_fam[l]] 58 | if im_fam[l]==nil then print("poota1") end 59 | end 60 | 61 | im_var_file = folder.."images_variant_train.txt"; 62 | im_var_f = io.open(im_var_file); 63 | l=0; 64 | im_var = {}; 65 | for _ in io.lines(im_var_file) do 66 | l=l+1; 67 | --img = image.load(folder..'images/'.._..'.jpg'); 68 | x = _:split(" "); 69 | im_var[l]=x[2]; 70 | for i=3,#x do 71 | im_var[l] = im_var[l]..' '..x[i]; 72 | end 73 | im_var[l] = variants[im_var[l]] 74 | if im_var[l]==nil then print("poota2") end 75 | end 76 | 77 | im_man_file = folder.."images_manufacturer_train.txt"; 78 | im_man_f = io.open(im_man_file); 79 | l=0; 80 | im_man = {}; 81 | for _ in io.lines(im_man_file) do 82 | l=l+1; 83 | --img = image.load(folder..'images/'.._..'.jpg'); 84 | x = _:split(" "); 85 | im_man[l]=x[2]; 86 | for i=3,#x do 87 | im_man[l] = im_man[l]..' '..x[i]; 88 | end 89 | im_man[l] = manufacturers[im_man[l]] 90 | if im_man[l]==nil then print("poota3") end 91 | end 92 | 93 | torch.save('train_im_labels.t7',{imNames,im_fam,im_var,im_man}) 94 | -------------------------------------------------------------------------------- /Auto-encoder-cuda/deploy_adversarialAE_alex.lua: -------------------------------------------------------------------------------- 1 | require 'nn'; 2 | require 'image'; 3 | require 'optim'; 4 | require 'gnuplot'; 5 | require 'cutorch'; 6 | require 'cunn'; 7 | require 'cudnn'; 8 | 9 | --define function binarise 10 | function binarise(x,thresh,low,high) 11 | y = torch.Tensor(x:size()) 12 | for i=1,x:size(2) do 13 | if x[1][i]<=thresh then 14 | y[1][i]=low 15 | else 16 | y[1][i]=high 17 | end 18 | end 19 | return y 20 | end 21 | 22 | --define function to sort table 23 | function compare(a,b) 24 | return tostring(a[2]) self.threshold then 55 | error('in-place processing requires value (' .. self.val .. 56 | ') not exceed threshold (' .. self.threshold .. ')') 57 | end 58 | end 59 | end 60 | 61 | zSize = 128 62 | --encoder 63 | encoder = nn.Sequential(); 64 | encoder:add(nn.SpatialConvolution(3,96,3,3,1,1,1,1)) --1 65 | encoder:add(nn.ReLU()) --2 66 | encoder:add(nn.SpatialMaxPooling(2, 2, 2, 2)) --3 67 | 68 | encoder:add(nn.SpatialConvolution(96,256,3,3,1,1,1,1)) --4 69 | encoder:add(nn.ReLU()) --5 70 | encoder:add(nn.SpatialMaxPooling(2, 2, 2, 2)) --6 71 | 72 | encoder:add(nn.SpatialConvolution(256,384,3,3,1,1,1,1)) --7 73 | encoder:add(nn.ReLU()) --8 74 | encoder:add(nn.SpatialConvolution(384,384,3,3,1,1,1,1)) --9 75 | encoder:add(nn.ReLU()) --10 76 | encoder:add(nn.SpatialConvolution(384,256,3,3,1,1,1,1)) --11 77 | encoder:add(nn.ReLU()) --12 78 | encoder:add(nn.SpatialMaxPooling(2, 2, 2, 2)) --13 79 | 80 | encoder:add(nn.View(-1,4096)) --14 81 | encoder:add(nn.Linear(4096,1024)) --15 82 | --encoder:add(nn.BatchNormalization(1024)) --16 83 | encoder:add(nn.ReLU()) --17 84 | encoder:add(nn.Linear(1024,512)) --18 85 | --encoder:add(nn.BatchNormalization(512)) --19 86 | encoder:add(nn.ReLU()) --20 87 | encoder:add(nn.Linear(512,zSize)) --21 88 | --encoder:add(nn.Sigmoid()) --22 89 | 90 | --decoder 91 | decoder = nn.Sequential() 92 | decoder:add(nn.Linear(zSize, 512)) --1 93 | --decoder:add(nn.BatchNormalization(512)) --2 94 | decoder:add(nn.ReLU()) --3 95 | decoder:add(nn.Linear(512, 1024)) --4 96 | --decoder:add(nn.BatchNormalization(1024)) --5 97 | decoder:add(nn.ReLU()) --6 98 | decoder:add(nn.Linear(1024, 4096)) --7 99 | --decoder:add(nn.BatchNormalization(4096)) --8 100 | decoder:add(nn.ReLU()) --9 101 | decoder:add(nn.View(256,4,4)) --10 102 | 103 | decoder:add(nn.SpatialMaxUnpooling(encoder:get(13))) --11 104 | decoder:add(nn.SpatialConvolution(256,384,3,3,1,1,1,1)) --12 105 | decoder:add(nn.ReLU()) --13 106 | decoder:add(nn.SpatialConvolution(384,384,3,3,1,1,1,1)) --14 107 | decoder:add(nn.ReLU()) --15 108 | decoder:add(nn.SpatialConvolution(384,256,3,3,1,1,1,1)) --16 109 | decoder:add(nn.ReLU()) --17 110 | 111 | decoder:add(nn.SpatialMaxUnpooling(encoder:get(6))) --18 112 | decoder:add(nn.SpatialConvolution(256,96,3,3,1,1,1,1)) --19 113 | decoder:add(nn.ReLU()) --20 114 | 115 | decoder:add(nn.SpatialMaxUnpooling(encoder:get(3))) --21 116 | decoder:add(nn.SpatialConvolution(96,3,3,3,1,1,1,1)) --22 117 | decoder:add(nn.Sigmoid()) --23 118 | 119 | 120 | --autoencoder 121 | autoencoder = nn.Sequential() 122 | autoencoder:add(encoder) 123 | autoencoder:add(decoder) 124 | 125 | autoencoder = autoencoder:cuda() 126 | print(autoencoder) 127 | 128 | --adversary network 129 | adversary = nn.Sequential() 130 | adversary:add(nn.Linear(zSize, 64)) 131 | adversary:add(nn.BatchNormalization(64)) 132 | adversary:add(nn.ReLU()) 133 | adversary:add(nn.Linear(64, 16)) 134 | adversary:add(nn.BatchNormalization(16)) 135 | adversary:add(nn.ReLU()) 136 | adversary:add(nn.Linear(16, 1)) 137 | adversary:add(nn.BatchNormalization(1)) 138 | adversary:add(nn.Sigmoid()) 139 | 140 | adversary = adversary:cuda() 141 | print(adversary) 142 | 143 | --load MNIST data 144 | --[[trainData = mnist.traindataset().data:double():div(255):cuda() 145 | trainlabels = mnist.traindataset().label:cuda() 146 | N = mnist.traindataset().size 147 | 148 | testData = mnist.testdataset().data:double():div(255):cuda() 149 | testlabels = mnist.testdataset().label:cuda() 150 | --]] 151 | trainset = torch.load('cifar10-train.t7') 152 | testset = torch.load('cifar10-test.t7') 153 | classes = {'airplane', 'automobile', 'bird', 'cat', 154 | 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'} 155 | 156 | --trainData = trainset.data:double():div(255):cuda() 157 | trainData = torch.DoubleTensor(trainset.data:size()) 158 | print(#trainData) 159 | for i=1,trainData:size()[1] do 160 | trainData[i] = image.rgb2hsv(trainset.data[i]:double():div(255)) 161 | end 162 | 163 | trainData = trainData:cuda() 164 | 165 | trainlabel=trainset.label:cuda() 166 | N = trainData:size()[1] 167 | testData = testset.data 168 | testlabels = testset.label:cuda() 169 | 170 | 171 | local theta,gradTheta = autoencoder:getParameters() 172 | local thetaAdv,gradThetaAdv = adversary:getParameters() 173 | 174 | local criterion = nn.MSECriterion():cuda() 175 | 176 | local x 177 | 178 | batchSize = 512 179 | iterations = 250 180 | 181 | local feval = function(params) 182 | if theta~=params then 183 | theta:copy(params) 184 | end 185 | gradTheta:zero() 186 | gradThetaAdv:zero() 187 | --print("x") 188 | --print(x[1][1]) 189 | local xHat = autoencoder:forward(x) 190 | --print("xHat") 191 | --print(xHat[1][1]) 192 | local loss = criterion:forward(xHat,x) 193 | --print(loss) 194 | local gradLoss = criterion:backward(xHat,x) 195 | autoencoder:backward(x,gradLoss) 196 | 197 | local real = torch.Tensor(batchSize,zSize)--[[:bernoulli()--]]:normal(0,1):typeAs(trainData) -- Real Samples 198 | local YReal = torch.ones(batchSize):typeAs(trainData) -- labels for real samples 199 | local YFake = torch.zeros(batchSize):typeAs(trainData) --labels for generated samples 200 | 201 | -- Train adversary to maximise log probability of real samples: max_D log(D(x)) 202 | local pred = adversary:forward(real) 203 | local realLoss = criterion:forward(pred,YReal) 204 | local gradRealLoss = criterion:backward(pred,YReal) 205 | adversary:backward(real,gradRealLoss) 206 | 207 | --Train adversary to minimise log probability of fake samples: max_D log(1 - D(G(x))) 208 | pred = adversary:forward(encoder.output) 209 | local fakeLoss = criterion:forward(pred,YFake) 210 | advLoss = realLoss + fakeLoss 211 | local gradFakeLoss = criterion:backward(pred,YFake) 212 | local gradFake = adversary:backward(encoder.output, gradFakeLoss) 213 | 214 | -- Train encoder (generator) to play a minimax game with the adversary (discriminator): min_G max_D log(1 - D(G(x))) 215 | local minimaxLoss = criterion:forward(pred,YReal) -- Technically use max_G max_D log(D(G(x))) for same fixed point, stronger initial gradients 216 | loss = loss + minimaxLoss 217 | local gradMinimaxLoss = criterion:backward(pred,YReal) 218 | local gradMinimax = adversary:updateGradInput(encoder.output, gradMinimaxLoss) 219 | encoder:backward(x,gradMinimax) 220 | 221 | return loss, gradTheta 222 | end 223 | 224 | local advFeval = function(params) 225 | if thetaAdv~=params then 226 | thetaAdv:copy(params) 227 | end 228 | return advLoss, gradThetaAdv 229 | end 230 | 231 | --Train 232 | print('Training Starting') 233 | local optimParams = {learningRate = 0.002} 234 | local advOptimParams = {learningRate = 0.002} 235 | local _,loss 236 | local losses, advLosses = {},{} 237 | for epoch=1,iterations do 238 | collectgarbage() 239 | print('Epoch '..epoch..'/'..iterations) 240 | for n=1,N, batchSize do 241 | if n+batchSize-1>N then break end 242 | collectgarbage() 243 | x = trainData:narrow(1,n,batchSize) 244 | _,loss = optim.adam(feval,theta,optimParams) 245 | losses[#losses + 1] = loss[1] 246 | _,loss = optim.adam(advFeval,thetaAdv,advOptimParams) 247 | advLosses[#advLosses + 1] = loss[1] 248 | end 249 | local plots={{'Adv AE', torch.linspace(1,#losses,#losses), torch.Tensor(losses), '-'}} 250 | plots[2]={'Adversary', torch.linspace(1,#advLosses,#advLosses), torch.Tensor(advLosses), '-'} 251 | gnuplot.pngfigure('AdvAE/Training_normal_cifar_hsv_alex.png') 252 | gnuplot.plot(table.unpack(plots)) 253 | gnuplot.ylabel('Loss') 254 | gnuplot.xlabel('Batch #') 255 | gnuplot.plotflush() 256 | 257 | --permute training data 258 | trainData = trainData:index(1,torch.randperm(trainData:size(1)):long()) 259 | 260 | 261 | local x1 = trainData:narrow(1,1,50) 262 | local xHat= autoencoder:forward(x1) 263 | --print(x[1]) 264 | --print("xHat111") 265 | --print(xHat[1][3]) 266 | --[[xHat_hsv = xHat_hsv:mul(255):byte() 267 | for i=1,50 do 268 | print(i) 269 | print(xHat_hsv[i][1]:min(),xHat_hsv[i][1]:max()) 270 | print(xHat_hsv[i][2]:min(),xHat_hsv[i][2]:max()) 271 | print(xHat_hsv[i][3]:min(),xHat_hsv[i][3]:max()) 272 | end 273 | --]] 274 | --[[local xHat = torch.DoubleTensor(xHat_hsv:size()) 275 | for i=1,xHat_hsv:size()[1] do 276 | xHat[i] = image.hsv2rgb(xHat_hsv[i]:double()) 277 | end--]] 278 | 279 | --print (#x) 280 | ---print(#xHat) 281 | --temp=torch.cat(image.toDisplayTensor(x,2,50),image.toDisplayTensor(xHat,2,50),2) 282 | --print (#temp) 283 | image.save('AdvAE/Reconstructions_normal_cifar_hsv_alex_temp.png', torch.cat(image.toDisplayTensor(x1,2,50),image.toDisplayTensor(xHat,2,50),2)) 284 | end 285 | 286 | torch.save('AdvAE/autoencoder_model_normal.t7',autoencoder) 287 | torch.save('AdvAE/adversary_model_normal.t7',adversary) 288 | print('Testing') 289 | x = testData:narrow(1,1,50):double():div(255):cuda() 290 | local x_hsv = torch.Tensor(x:size()):typeAs(x) 291 | for i=1,x:size()[1] do 292 | x_hsv[i] = image.rgb2hsv(x[i]:double()) -- It showed an error in this line since the typecasting from CUDA to TorchDouble tensor was not written (DS / 7:20 PM @ 6 Jul) 293 | end 294 | x_hsv = x_hsv:cuda() 295 | local xHat_hsv= autoencoder:forward(x_hsv) 296 | --print(x[1]) 297 | --print("xHat111") 298 | --print(xHat[1][1]) 299 | --[[xHat_hsv = xHat_hsv:mul(255):byte() 300 | for i=1,50 do 301 | print(i) 302 | print(xHat_hsv[i][1]:min(),xHat_hsv[i][1]:max()) 303 | print(xHat_hsv[i][2]:min(),xHat_hsv[i][2]:max()) 304 | print(xHat_hsv[i][3]:min(),xHat_hsv[i][3]:max()) 305 | end 306 | --]] 307 | local xHat = torch.DoubleTensor(xHat_hsv:size()) 308 | for i=1,xHat_hsv:size()[1] do 309 | xHat[i] = image.hsv2rgb(xHat_hsv[i]:double()) 310 | end 311 | --print (#x) 312 | ---print(#xHat) 313 | --temp=torch.cat(image.toDisplayTensor(x,2,50),image.toDisplayTensor(xHat,2,50),2) 314 | --print (#temp) 315 | image.save('AdvAE/Reconstructions_normal_cifar_hsv_alex.png', torch.cat(image.toDisplayTensor(x,2,50),image.toDisplayTensor(xHat,2,50),2)) -------------------------------------------------------------------------------- /Pretrainedencoder.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch.autograd import Variable 3 | import torchvision 4 | import torch.nn as nn 5 | import torch.nn.functional as F 6 | from torchvision import datasets, models,transforms 7 | import torch.optim as optim 8 | from torch.optim import lr_scheduler 9 | import numpy as np 10 | import os 11 | import matplotlib.pyplot as plt 12 | from torch.autograd import Function 13 | 14 | zsize = 128 15 | batch_size = 250 16 | iterations = 500 17 | learningRate=0.0001 18 | 19 | '''Encoder = models.alexnet(pretrained=True) 20 | new_classifier = nn.Sequential(*list(Encoder.classifier.children())[:-1]) 21 | new_classifier.add_module('fc',nn.Linear(4096,zsize)) 22 | #new_classifier.add_module('softmax',nn.LogSoftmax()) 23 | Encoder.classifier = new_classifier 24 | ''' 25 | """ 26 | class Encoder(nn.Module): 27 | def __init__(self): 28 | super(Encoder, self).__init__() 29 | self.features = nn.Sequential( 30 | nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2), 31 | nn.ReLU(inplace=True), 32 | nn.MaxPool2d(kernel_size=3, stride=2, return_indices=True), 33 | nn.Conv2d(64, 192, kernel_size=5, padding=2), 34 | nn.ReLU(inplace=True), 35 | nn.MaxPool2d(kernel_size=3, stride=2, return_indices=True), 36 | nn.Conv2d(192, 384, kernel_size=3, padding=1), 37 | nn.ReLU(inplace=True), 38 | nn.Conv2d(384, 256, kernel_size=3, padding=1), 39 | nn.ReLU(inplace=True), 40 | nn.Conv2d(256, 256, kernel_size=3, padding=1), 41 | nn.ReLU(inplace=True), 42 | nn.MaxPool2d(kernel_size=3, stride=2, return_indices=True), 43 | ) 44 | self.classifier = nn.Sequential( 45 | nn.Dropout(), 46 | nn.Linear(256 * 6 * 6, 4096), 47 | nn.ReLU(inplace=True), 48 | nn.Dropout(), 49 | nn.Linear(4096, 4096), 50 | nn.ReLU(inplace=True), 51 | nn.Linear(4096, 1000), 52 | ) 53 | 54 | def forward(self, x): 55 | x = self.features(x) 56 | x = x.view(x.size(0), 256 * 6 * 6) 57 | x = self.classifier(x) 58 | return x 59 | """ 60 | #new_classifier = nn.Sequential(*list(net.classifier.children())[:-1]) 61 | #autoencoder network class Encoder(nn.Module): 62 | 63 | class Encoder(nn.Module): 64 | def __init__(self): 65 | super(Encoder,self).__init__() 66 | self.conv1 = nn.Conv2d(3, 64, 11, stride = 4, padding = 2) 67 | self.conv2 = nn.Conv2d(64, 192, 5, padding = 2) 68 | self.conv3 = nn.Conv2d(192, 384, 3, padding = 1) 69 | self.conv4 = nn.Conv2d(384, 256, 3, padding = 1) 70 | self.conv5 = nn.Conv2d(256, 256, 3, padding = 1) 71 | self.fc1 = nn.Linear(256 * 6 * 6, 4096) 72 | self.fc2 = nn.Linear(4096, 4096) 73 | self.fc3 = nn.Linear(4096, zsize) 74 | #[u'features.0.weight', u'features.3.weight', u'features.6.weight', u'features.8.weight', u'features.10.bias', u'classifier.1.bias', u'classifier.4.bias', u'classifier.6.bias'] 75 | def forward(self,x): 76 | x = F.relu(self.conv1(x)) 77 | x,indices1 = F.max_pool2d(x,(3,3),(2,2),return_indices = True) 78 | 79 | x = F.relu(self.conv2(x)) 80 | x,indices2 = F.max_pool2d(x,(3,3),(2,2),return_indices = True) 81 | 82 | x = F.relu(self.conv3(x)) 83 | 84 | x = F.relu(self.conv4(x)) 85 | 86 | x = F.relu(self.conv5(x)) 87 | x,indices3 = F.max_pool2d(x,(3,3),(2,2),return_indices = True) 88 | 89 | x = x.view(x.size(0), 256 * 6 * 6) 90 | x = F.dropout(x) 91 | 92 | x = F.relu(self.fc1(x)) 93 | x = F.dropout(x) 94 | 95 | x = F.relu(self.fc2(x)) 96 | 97 | x = self.fc3(x) 98 | 99 | return x,indices1,indices2,indices3 100 | encoder = Encoder() 101 | 102 | 103 | #encoder_keys = encoder.state_dict().keys() 104 | #print "Before adding weights", encoder.conv1.weight.data[0,0,0,0]#state_dict()[encoder_keys[0]][63][0] 105 | #print "Before adding bias", encoder.conv1.bias.data[0] 106 | #encoder.load_state_dict(torch.load('/home/siplab/Saket/alexnet-owt-4df8aa71.pth'),strict=False)#,map_location=lambda storage, loc: storage.cuda(1)),strict=False) 107 | #print "After adding weights", encoder.conv1.weight.data[0,0,0,0]#encoder.state_dict()[0]#[encoder_keys[0]][63][0] 108 | loaded_weights = torch.load('/home/deepkliv/Desktop/AE/ram/AE_classifier/alexnet-owt-4df8aa71.pth') 109 | #print loaded_weights['features.0.weight'][0,0,0,0] 110 | #print loaded_weights['features.0.bias'][0] 111 | 112 | encoder.conv1.weight = loaded_weights['features.0.weight'] 113 | encoder.conv1.bias = loaded_weights['features.0.bias'] 114 | 115 | encoder.conv2.weight = loaded_weights['features.3.weight'] 116 | encoder.conv2.bias = loaded_weights['features.3.bias'] 117 | 118 | encoder.conv3.weight = loaded_weights['features.6.weight'] 119 | encoder.conv3.bias = loaded_weights['features.6.bias'] 120 | 121 | encoder.conv4.weight = loaded_weights['features.8.weight'] 122 | encoder.conv4.bias = loaded_weights['features.8.bias'] 123 | 124 | encoder.conv5.weight = loaded_weights['features.10.weight'] 125 | encoder.conv5.bias = loaded_weights['features.10.bias'] 126 | 127 | encoder.fc1.weight = loaded_weights['classifier.1.weight'] 128 | encoder.fc1.bias = loaded_weights['classifier.1.bias'] 129 | 130 | encoder.fc2.weight = loaded_weights['classifier.4.weight'] 131 | encoder.fc2.bias = loaded_weights['classifier.4.bias'] 132 | 133 | #encoder = torch.nn.DataParallel(encoder, device_ids=[0, 1, 2]) 134 | 135 | #encoder.fc3.weight = loaded_weights['classifier.6.weight'] 136 | #encoder.fc3.bias = loaded_weights['classifier.6.bias'] 137 | 138 | #print "After adding weights", encoder.conv1.weight.data[0,0,0,0]#encoder.state_dict()[0]#[encoder_keys[0]][63][0] 139 | #print "After adding bias", encoder.conv1.bias.data[0] 140 | '''class Binary(nn.Module): 141 | def __init__(self): 142 | super(Binary,self).__init__() 143 | self.encoder = Encoder() 144 | 145 | def forward(self, x): 146 | x,i2,i1 = self.encoder(x) 147 | x = torch.sign(x) 148 | #print "x grad ", x.grad 149 | return x, i2, i1 150 | 151 | 152 | def backward(self, grad_output): 153 | return grad_output''' 154 | class Binary(Function): 155 | 156 | @staticmethod 157 | def forward(ctx, input): 158 | return F.relu(Variable(input.sign())).data 159 | 160 | @staticmethod 161 | def backward(ctx, grad_output): 162 | return grad_output 163 | 164 | binary = Binary() 165 | #binary = torch.nn.DataParallel(binary, device_ids=[0, 1, 2]) 166 | 167 | class Decoder(nn.Module): 168 | def __init__(self): 169 | super(Decoder,self).__init__() 170 | self.dfc3 = nn.Linear(zsize, 4096) 171 | self.bn3 = nn.BatchNorm2d(4096) 172 | self.dfc2 = nn.Linear(4096, 4096) 173 | self.bn2 = nn.BatchNorm2d(4096) 174 | self.dfc1 = nn.Linear(4096,256 * 6 * 6) 175 | self.bn1 = nn.BatchNorm2d(256*6*6) 176 | self.dconv5 = nn.ConvTranspose2d(256, 256, 3, padding = 1) 177 | self.dconv4 = nn.ConvTranspose2d(256, 384, 3, padding = 1) 178 | self.dconv3 = nn.ConvTranspose2d(384, 192, 3, padding = 1) 179 | self.dconv2 = nn.ConvTranspose2d(192, 64, 5, padding = 2) 180 | self.dconv1 = nn.ConvTranspose2d(64, 3, 12, stride = 4, padding = 2) 181 | 182 | def forward(self,x,i1,i2,i3): 183 | 184 | x = self.dfc3(x) 185 | #x = F.relu(x) 186 | x = F.relu(self.bn3(x)) 187 | 188 | x = self.dfc2(x) 189 | x = F.relu(self.bn2(x)) 190 | #x = F.relu(x) 191 | x = self.dfc1(x) 192 | x = F.relu(self.bn1(x)) 193 | #x = F.relu(x) 194 | #print(x.size()) 195 | x = x.view(batch_size,256,6,6) 196 | #print x 197 | x = self.dconv5(F.max_unpool2d(x,i3,kernel_size =(3,3),stride =(2,2))) 198 | x = F.relu(x) 199 | 200 | x = F.relu(self.dconv4(x)) 201 | 202 | x = F.relu(self.dconv3(x)) 203 | 204 | x = self.dconv2(F.max_unpool2d(x,i2,kernel_size = (3,3), stride = (2,2))) 205 | x = F.relu(x) 206 | 207 | x = self.dconv1(F.max_unpool2d(x,i1,kernel_size = 3 ,stride = 2)) 208 | #print x 209 | x = F.sigmoid(x) 210 | #print x 211 | return x 212 | decoder = Decoder() 213 | #decoder = torch.nn.DataParallel(decoder, device_ids=[0, 1, 2]) 214 | ''' 215 | decoder.dconv1.weight = loaded_weights['features.0.weight'] 216 | decoder.dconv1.bias = loaded_weights['features.0.bias'] 217 | 218 | decoder.dconv2.weight = loaded_weights['features.3.weight'] 219 | decoder.dconv2.bias = loaded_weights['features.3.bias'] 220 | 221 | decoder.dconv3.weight = loaded_weights['features.6.weight'] 222 | decoder.dconv3.bias = loaded_weights['features.6.bias'] 223 | 224 | decoder.dconv4.weight = loaded_weights['features.8.weight'] 225 | decoder.dconv4.bias = loaded_weights['features.8.bias'] 226 | 227 | decoder.dconv5.weight = loaded_weights['features.10.weight'] 228 | decoder.dconv5.bias = loaded_weights['features.10.bias'] 229 | 230 | decoder.dfc1.weight = loaded_weights['classifier.1.weight'] 231 | decoder.dfc1.bias = loaded_weights['classifier.1.bias'] 232 | 233 | decoder.dfc2.weight = loaded_weights['classifier.4.weight'] 234 | decoder.dfc2.bias = loaded_weights['classifier.4.bias'] 235 | ''' 236 | class Autoencoder(nn.Module): 237 | def __init__(self): 238 | super(Autoencoder,self).__init__() 239 | self.encoder = Encoder() 240 | self.binary = Binary() 241 | self.decoder = Decoder() 242 | 243 | def forward(self,x): 244 | #x=Encoder(x) 245 | x,i1,i2,i3 = self.encoder(x) 246 | x = binary.apply(x) 247 | #print x 248 | #x,i2,i1 = self.binary(x) 249 | #x=Variable(x) 250 | x = self.decoder(x,i1,i2,i3) 251 | return x 252 | 253 | print Autoencoder() 254 | 255 | autoencoder = Autoencoder() 256 | #autoencoder = torch.nn.DataParallel(autoencoder, device_ids=[0, 1, 2]) 257 | class Classifier(nn.Module): 258 | def __init__(self): 259 | super(Classifier,self).__init__() 260 | self.L1 = nn.Linear(zsize,64) 261 | self.L2 = nn.Linear(64,16) 262 | self.L3 = nn.Linear(16,10) 263 | 264 | def forward(self,x): 265 | x = F.relu(self.L1(x)) 266 | x = F.relu(self.L2(x)) 267 | x = F.log_softmax(self.L3(x)) 268 | return x 269 | 270 | 271 | print Classifier() 272 | classifier = Classifier() 273 | #classifier = torch.nn.DataParallel(classifier, device_ids=[0, 1, 2]) 274 | class Classification(nn.Module): 275 | def __init__(self): 276 | super(Classification,self).__init__() 277 | self.encoder = Encoder() 278 | #self.binary = Binary() 279 | self.classifier = Classifier() 280 | 281 | def forward(self,x): 282 | x,_,_,_= self.encoder(x) 283 | #x,_,_ = self.binary(x) 284 | x = self.classifier(x) 285 | return x 286 | 287 | print Classification() 288 | classification = Classification() 289 | 290 | 291 | if torch.cuda.is_available(): 292 | autoencoder.cuda() 293 | classification.cuda() 294 | decoder.cuda() 295 | encoder.cuda() 296 | classifier.cuda() 297 | #binary.cuda() 298 | #data 299 | 300 | plt.ion() 301 | 302 | transform = transforms.Compose( 303 | [ 304 | transforms.Scale((224,224), interpolation=2), 305 | transforms.ToTensor(), 306 | #transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225]), 307 | #transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)) 308 | ]) 309 | 310 | trainset = torchvision.datasets.CIFAR10('CIFAR10',train = True , download = True , transform = transform) 311 | trainloader = torch.utils.data.DataLoader(trainset, shuffle = True , batch_size = batch_size , num_workers = 2) 312 | testset = torchvision.datasets.CIFAR10('CIFAR10', train = False, download = True, transform = transform) 313 | testloader = torch.utils.data.DataLoader(testset, shuffle = False , batch_size = batch_size ,num_workers = 2) 314 | '''trainset=torchvision.datasets.STL10('home/siplab/Saket/STL10', split = 'train' , download = True , transform = transform) 315 | trainloader = torch.utils.data.DataLoader(trainset, shuffle = True , batch_size = batch_size , num_workers = 2) 316 | testset = torchvision.datasets.STL10('home/siplab/Saket/STL10', split = 'test', download = True, transform = transform) 317 | testloader = torch.utils.data.DataLoader(testset, shuffle = False , batch_size = batch_size ,num_workers = 2) 318 | #classes = ('airplane', 'bird', 'car', 'cat', 'deer', 'dog', 'horse', 'monkey', 'ship', 'truck')''' 319 | classes = ('airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') 320 | 321 | 322 | autoencoder_criterion = nn.MSELoss() 323 | classification_criterion = nn.NLLLoss() 324 | 325 | autoencoder_optimizer = optim.Adam(autoencoder.parameters(), lr = learningRate) 326 | classification_optimizer = optim.Adam(classification.parameters(), lr = learningRate) 327 | #encoder_optimizer = optim.Adam(Encoder.parameters(), lr = learningRate) 328 | list_a_loss = [] 329 | list_c_loss = [] 330 | 331 | #fig = plt.figure() 332 | for epoch in range(iterations): 333 | run_loss = 0 334 | run_c_loss = 0 335 | 336 | for i,data in enumerate(trainloader): 337 | #print i 338 | inputs, labels = data 339 | inputs, labels = Variable(inputs).cuda(), Variable(labels).cuda() 340 | 341 | autoencoder_optimizer.zero_grad() 342 | classification_optimizer.zero_grad() 343 | #print(inputs.size()) 344 | pred = autoencoder(inputs) 345 | #torchvision.utils.save_image(pred.data[0:8], os.path.join('/home/siplab/Saket/AE_Classifier/', 'batch_%d_%d'%((epoch+1)/1,i+1) + '.jpg')) 346 | a_loss = autoencoder_criterion(pred , inputs) 347 | a_loss.backward() 348 | autoencoder_optimizer.step() 349 | 350 | #print("efc3", autoencoder.encoder.fc3.bias.grad) 351 | 352 | class_pred = classification(inputs) 353 | c_loss = classification_criterion(class_pred , labels) 354 | #_,xxpred = torch.max(class_pred.data, 1) 355 | #print("class_pred") 356 | #print(xxpred.cpu().numpy()) 357 | c_loss.backward() 358 | classification_optimizer.step() 359 | #encoder_optimizer.step() 360 | 361 | run_loss += a_loss.data[0] 362 | run_c_loss += c_loss.data[0] 363 | #print i 364 | if (i +1) % 2 == 0: 365 | print('[%d, %5d] Autoencoder loss: %.3f Classification loss: %.3f' % (epoch + 1, i + 1 , run_loss/2 , run_c_loss/2)) 366 | #print('[%d,%5d] Classification loss: %.3f' % (epoch + 1, i + 1, run_c_loss/10)) 367 | run_c_loss = 0.0 368 | run_loss = 0.0 369 | 370 | 371 | decoder_path = os.path.join('Decoder/', 'decoder-%d.pkl' %(epoch+1)) 372 | encoder_path = os.path.join('Encoder/', 'encoder-%d.pkl' %(epoch+1)) 373 | autoencoder_path = os.path.join('Autoencoder/', 'autoencoder-%d.pkl' %(epoch+1)) 374 | classifier_path = os.path.join('Classifier/', 'classifier-%d.pkl' %(epoch+1)) 375 | classification_path = os.path.join('Classification/','classification-%d.pkl' %(epoch+1)) 376 | 377 | torch.save(decoder.state_dict(), decoder_path) 378 | torch.save(encoder.state_dict(), encoder_path) 379 | torch.save(autoencoder.state_dict(), autoencoder_path) 380 | torch.save(classifier.state_dict(), classifier_path) 381 | torch.save(classification.state_dict(), classification_path) 382 | 383 | if ( epoch+1 )% 1 == 0: 384 | list_a_loss.append(run_loss/5000) 385 | list_c_loss.append(run_c_loss/5000) 386 | 387 | #plt.plot(range(epoch+1),list_a_loss,'r--',label='autoencoder') 388 | #plt.plot(range(epoch+1),list_c_loss,'b--',label='classifier') 389 | #if epoch==0: 390 | #plt.legend(loc='upper left') 391 | #plt.xlabel('Epochs') 392 | #plt.ylabel('Loss') 393 | #fig.savefig('/home/siplab/Saket/loss_plot.png') 394 | correct = 0 395 | total = 0 396 | print('\n Testing ....') 397 | for t_i,t_data in enumerate(testloader): 398 | if t_i * batch_size >1000: 399 | break 400 | t_inputs,t_labels = t_data 401 | t_inputs = Variable(t_inputs).cuda() 402 | t_labels = t_labels.cuda() 403 | t_outputs = autoencoder(t_inputs) 404 | c_pred = classification(t_inputs) 405 | _, predicted = torch.max(c_pred.data, 1) 406 | #print predicted.type() , t_labels.type() 407 | total += t_labels.size(0) 408 | correct += (predicted == t_labels).sum() 409 | if (epoch + 1)%1 == 0: 410 | print("saving image") 411 | test_result_path = os.path.join('', 'batch_%d_%d'%((epoch+1)/1,t_i+1) + '.jpg') 412 | image_tensor = torch.cat((t_inputs.data[0:8], t_outputs.data[0:8]), 0) 413 | torchvision.utils.save_image(image_tensor, test_result_path) 414 | 415 | print('Accuracy of the network on the 8000 test images: %d %%' % (100 * correct / total)) 416 | 417 | print('Finished Training and Testing') 418 | 419 | 420 | """ 421 | classification_criterion = nn.NLLLoss() 422 | for i,data in enumerate(testloader): 423 | inputs,labels = data 424 | inputs,labels = Variable(inputs).cuda(), Variable(labels).cuda() 425 | test_model = Autoencoder().cuda() 426 | test_model.load_state_dict(torch.load('/home/siplab/Saket/AE_Classifier/Autoencoder/autoencoder-500.pkl',map_location=lambda storage, loc: storage.cuda(1))) 427 | outputs = test_model(inputs) 428 | test_result_path = os.path.join('/home/siplab/Saket/AE_Classifier/Test_results/', 'batch_%d'%(i+1) + '.jpg') 429 | image_tensor = torch.cat((inputs.data[0:8], outputs.data[0:8]), 0) 430 | torchvision.utils.save_image(image_tensor, test_result_path) 431 | """ 432 | -------------------------------------------------------------------------------- /resnet.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch.autograd import Variable 3 | import torchvision 4 | import torch.nn as nn 5 | import torch.nn.functional as F 6 | from torchvision import datasets, models,transforms 7 | import torch.optim as optim 8 | from torch.optim import lr_scheduler 9 | import numpy as np 10 | import os 11 | import matplotlib.pyplot as plt 12 | from torch.autograd import Function 13 | from collections import OrderedDict 14 | import torch.nn as nn 15 | import math 16 | zsize = 48 17 | batch_size = 11 18 | iterations = 500 19 | learningRate= 0.0001 20 | 21 | import torchvision.models as models 22 | #ResNEt##################################### 23 | def conv3x3(in_planes, out_planes, stride=1): 24 | """3x3 convolution with padding""" 25 | return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, 26 | padding=1, bias=False) 27 | 28 | class BasicBlock(nn.Module): 29 | expansion = 1 30 | 31 | def __init__(self, inplanes, planes, stride=1, downsample=None): 32 | super(BasicBlock, self).__init__() 33 | self.conv1 = conv3x3(inplanes, planes, stride) 34 | self.bn1 = nn.BatchNorm2d(planes) 35 | self.relu = nn.ReLU(inplace=True) 36 | self.conv2 = conv3x3(planes, planes) 37 | self.bn2 = nn.BatchNorm2d(planes) 38 | self.downsample = downsample 39 | self.stride = stride 40 | 41 | def forward(self, x): 42 | residual = x 43 | 44 | out = self.conv1(x) 45 | out = self.bn1(out) 46 | out = self.relu(out) 47 | 48 | out = self.conv2(out) 49 | out = self.bn2(out) 50 | 51 | if self.downsample is not None: 52 | residual = self.downsample(x) 53 | 54 | out += residual 55 | out = self.relu(out) 56 | 57 | return out 58 | 59 | 60 | class Bottleneck(nn.Module): 61 | expansion = 4 62 | 63 | def __init__(self, inplanes, planes, stride=1, downsample=None): 64 | super(Bottleneck, self).__init__() 65 | self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) 66 | self.bn1 = nn.BatchNorm2d(planes) 67 | self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, 68 | padding=1, bias=False) 69 | self.bn2 = nn.BatchNorm2d(planes) 70 | self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False) 71 | self.bn3 = nn.BatchNorm2d(planes * 4) 72 | self.relu = nn.ReLU(inplace=True) 73 | self.downsample = downsample 74 | self.stride = stride 75 | 76 | def forward(self, x): 77 | residual = x 78 | 79 | out = self.conv1(x) 80 | 81 | out = self.bn1(out) 82 | out = self.relu(out) 83 | 84 | out = self.conv2(out) 85 | out = self.bn2(out) 86 | out = self.relu(out) 87 | 88 | out = self.conv3(out) 89 | out = self.bn3(out) 90 | 91 | if self.downsample is not None: 92 | residual = self.downsample(x) 93 | 94 | out += residual 95 | out = self.relu(out) 96 | 97 | return out 98 | 99 | ############################################################### 100 | 101 | 102 | 103 | ############################################################### 104 | class Encoder(nn.Module): 105 | 106 | def __init__(self, block, layers, num_classes=23): 107 | self.inplanes = 64 108 | super (Encoder, self).__init__() 109 | self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, 110 | bias=False) 111 | self.bn1 = nn.BatchNorm2d(64) 112 | self.relu = nn.ReLU(inplace=True) 113 | self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)#, return_indices = True) 114 | self.layer1 = self._make_layer(block, 64, layers[0]) 115 | self.layer2 = self._make_layer(block, 128, layers[1], stride=2) 116 | self.layer3 = self._make_layer(block, 256, layers[2], stride=2) 117 | self.layer4 = self._make_layer(block, 512, layers[3], stride=2) 118 | self.avgpool = nn.AvgPool2d(7, stride=1) 119 | self.fc = nn.Linear(512 * block.expansion, 1000) 120 | #self.fc = nn.Linear(num_classes,16) 121 | for m in self.modules(): 122 | if isinstance(m, nn.Conv2d): 123 | n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels 124 | m.weight.data.normal_(0, math.sqrt(2. / n)) 125 | elif isinstance(m, nn.BatchNorm2d): 126 | m.weight.data.fill_(1) 127 | m.bias.data.zero_() 128 | 129 | def _make_layer(self, block, planes, blocks, stride=1): 130 | downsample = None 131 | if stride != 1 or self.inplanes != planes * block.expansion: 132 | downsample = nn.Sequential( 133 | nn.Conv2d(self.inplanes, planes * block.expansion, 134 | kernel_size=1, stride=stride, bias=False), 135 | nn.BatchNorm2d(planes * block.expansion), 136 | ) 137 | 138 | layers = [] 139 | layers.append(block(self.inplanes, planes, stride, downsample)) 140 | self.inplanes = planes * block.expansion 141 | for i in range(1, blocks): 142 | layers.append(block(self.inplanes, planes)) 143 | 144 | return nn.Sequential(*layers) 145 | 146 | def forward(self, x): 147 | x = self.conv1(x) 148 | 149 | x = self.bn1(x) 150 | x = self.relu(x) 151 | 152 | x = self.maxpool(x) 153 | 154 | x = self.layer1(x) 155 | x = self.layer2(x) 156 | x = self.layer3(x) 157 | x = self.layer4(x) 158 | 159 | x = self.avgpool(x) 160 | x = x.view(x.size(0), -1) 161 | x = self.fc(x) 162 | 163 | return x 164 | encoder = Encoder(Bottleneck, [3, 4, 6, 3]) 165 | encoder.load_state_dict(torch.load('/home/deepkliv/Downloads/resnet50-19c8e357.pth'))#,map_location=lambda storage, loc: storage.cuda(1)),strict=False) 166 | #loaded_weights = torch.load('/home/siplab/Saket/resnet18-5c106cde.pth') 167 | #print encoder.layer1[1].conv1.weight.data[0][0] 168 | encoder.fc = nn.Linear(2048, 48) 169 | #for param in encoder.parameters(): 170 | # param.requires_grad = False 171 | encoder=encoder.cuda() 172 | y=torch.rand(1,3,224,224) 173 | x=torch.rand(1,128) 174 | x=Variable(x.cuda()) 175 | #print decoder(x) 176 | #y=Variable(y.cuda()) 177 | #print("\n") 178 | #encoder(y) 179 | #print encoder(y) 180 | ########################################################################## 181 | class Binary(Function): 182 | 183 | @staticmethod 184 | def forward(ctx, input): 185 | return F.relu(Variable(input.sign())).data 186 | 187 | @staticmethod 188 | def backward(ctx, grad_output): 189 | return grad_output 190 | 191 | binary = Binary() 192 | ########################################################################## 193 | class Decoder(nn.Module): 194 | def __init__(self): 195 | super(Decoder,self).__init__() 196 | self.dfc3 = nn.Linear(zsize, 4096) 197 | self.bn3 = nn.BatchNorm2d(4096) 198 | self.dfc2 = nn.Linear(4096, 4096) 199 | self.bn2 = nn.BatchNorm2d(4096) 200 | self.dfc1 = nn.Linear(4096,256 * 6 * 6) 201 | self.bn1 = nn.BatchNorm2d(256*6*6) 202 | self.upsample1=nn.Upsample(scale_factor=2) 203 | self.dconv5 = nn.ConvTranspose2d(256, 256, 3, padding = 0) 204 | self.dconv4 = nn.ConvTranspose2d(256, 384, 3, padding = 1) 205 | self.dconv3 = nn.ConvTranspose2d(384, 192, 3, padding = 1) 206 | self.dconv2 = nn.ConvTranspose2d(192, 64, 5, padding = 2) 207 | self.dconv1 = nn.ConvTranspose2d(64, 3, 12, stride = 4, padding = 4) 208 | 209 | def forward(self,x):#,i1,i2,i3): 210 | 211 | x = self.dfc3(x) 212 | #x = F.relu(x) 213 | x = F.relu(self.bn3(x)) 214 | 215 | x = self.dfc2(x) 216 | x = F.relu(self.bn2(x)) 217 | #x = F.relu(x) 218 | x = self.dfc1(x) 219 | x = F.relu(self.bn1(x)) 220 | #x = F.relu(x) 221 | #print(x.size()) 222 | x = x.view(batch_size,256,6,6) 223 | #print (x.size()) 224 | x=self.upsample1(x) 225 | #print x.size() 226 | x = self.dconv5(x) 227 | #print x.size() 228 | x = F.relu(x) 229 | #print x.size() 230 | x = F.relu(self.dconv4(x)) 231 | #print x.size() 232 | x = F.relu(self.dconv3(x)) 233 | #print x.size() 234 | x=self.upsample1(x) 235 | #print x.size() 236 | x = self.dconv2(x) 237 | #print x.size() 238 | x = F.relu(x) 239 | x=self.upsample1(x) 240 | #print x.size() 241 | x = self.dconv1(x) 242 | #print x.size() 243 | x = F.sigmoid(x) 244 | #print x 245 | return x 246 | decoder = Decoder() 247 | ########################################## 248 | class Autoencoder(nn.Module): 249 | def __init__(self): 250 | super(Autoencoder,self).__init__() 251 | self.encoder = encoder 252 | self.binary = Binary() 253 | self.decoder = Decoder() 254 | 255 | def forward(self,x): 256 | #x=Encoder(x) 257 | x = self.encoder(x) 258 | x = binary.apply(x) 259 | #print x 260 | #x,i2,i1 = self.binary(x) 261 | #x=Variable(x) 262 | x = self.decoder(x) 263 | return x 264 | 265 | #print Autoencoder() 266 | 267 | autoencoder = Autoencoder() 268 | #autoencoder = torch.nn.DataParallel(autoencoder, device_ids=[0, 1, 2]) 269 | class Classifier(nn.Module): 270 | def __init__(self): 271 | super(Classifier,self).__init__() 272 | self.L1 = nn.Linear(zsize,64) 273 | self.L2 = nn.Linear(64,32) 274 | self.L3 = nn.Linear(32,23) 275 | 276 | def forward(self,x): 277 | x = F.relu(self.L1(x)) 278 | x = F.relu(self.L2(x)) 279 | x = F.log_softmax(self.L3(x)) 280 | return x 281 | 282 | 283 | #print Classifier() 284 | classifier = Classifier() 285 | #classifier = torch.nn.DataParallel(classifier, device_ids=[0, 1, 2]) 286 | class Classification(nn.Module): 287 | def __init__(self): 288 | super(Classification,self).__init__() 289 | self.encoder = encoder 290 | self.binary = Binary() 291 | self.classifier = Classifier() 292 | 293 | def forward(self,x): 294 | x= self.encoder(x) 295 | x = binary.apply(x) 296 | #x= self.binary(x) 297 | x = self.classifier(x) 298 | return x 299 | 300 | #print Classification() 301 | classification = Classification() 302 | 303 | ########################## 304 | 305 | if torch.cuda.is_available(): 306 | autoencoder.cuda() 307 | classification.cuda() 308 | decoder.cuda() 309 | encoder.cuda() 310 | classifier.cuda() 311 | #data 312 | 313 | plt.ion() 314 | 315 | 316 | use_gpu = torch.cuda.is_available() 317 | if use_gpu: 318 | pinMem = True # Flag for pinning GPU memory 319 | print('GPU is available!') 320 | else: 321 | pinMem = False 322 | net = models.resnet18(pretrained=False) 323 | transform = transforms.Compose( 324 | [ 325 | transforms.Scale((224,224), interpolation=2), 326 | transforms.ToTensor(), 327 | #transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225]), 328 | #transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)) 329 | ]) 330 | trainset=torchvision.datasets.ImageFolder("/home/deepkliv/Desktop/AE/ram/AE_classifier/fashion/dataset/train", transform=transform, target_transform=None) 331 | trainloader = torch.utils.data.DataLoader(trainset, shuffle = True , batch_size = batch_size , num_workers = 2) 332 | testset=torchvision.datasets.ImageFolder("/home/deepkliv/Desktop/AE/ram/AE_classifier/fashion/dataset/test", transform=transform, target_transform=None) 333 | testloader = torch.utils.data.DataLoader(testset, shuffle = True , batch_size = batch_size , num_workers = 2) 334 | 335 | autoencoder_criterion = nn.MSELoss() 336 | classification_criterion = nn.NLLLoss() 337 | 338 | autoencoder_optimizer = optim.Adam(autoencoder.parameters(), lr = learningRate) 339 | classification_optimizer = optim.Adam(classification.parameters(), lr = learningRate) 340 | #encoder_optimizer = optim.Adam(Encoder.parameters(), lr = learningRate) 341 | list_a_loss = [] 342 | list_c_loss = [] 343 | 344 | #fig = plt.figure() 345 | for epoch in range(iterations): 346 | run_loss = 0 347 | run_c_loss = 0 348 | autoencoder.train(True) # For training 349 | classification.train(True) 350 | for i,data in enumerate(trainloader): 351 | #print i 352 | inputs, labels = data 353 | inputs, labels = Variable(inputs).cuda(), Variable(labels).cuda() 354 | 355 | 356 | autoencoder_optimizer.zero_grad() 357 | classification_optimizer.zero_grad() 358 | #print(inputs.size()) 359 | pred = autoencoder(inputs) 360 | #torchvision.utils.save_image(pred.data[0:8], os.path.join('/home/deepkliv/Saket/AE_Classifier/', 'batch_%d_%d'%((epoch+1)/1,i+1) + '.jpg')) 361 | a_loss = autoencoder_criterion(pred , inputs) 362 | a_loss.backward() 363 | autoencoder_optimizer.step() 364 | 365 | #print("efc3", autoencoder.encoder.fc3.bias.grad) 366 | 367 | class_pred = classification(inputs) 368 | 369 | c_loss = classification_criterion(class_pred , labels) 370 | 371 | #_,xxpred = torch.max(class_pred.data, 1) 372 | #print("class_pred") 373 | #print(xxpred.cpu().numpy()) 374 | c_loss.backward(retain_graph=True) 375 | classification_optimizer.step() 376 | #encoder_optimizer.step() 377 | 378 | run_loss += a_loss.data[0] 379 | run_c_loss += c_loss.data[0] 380 | #print i 381 | if (i +1) % 2 == 0: 382 | print('[%d, %5d] Autoencoder loss: %.3f Classification loss: %.3f' % (epoch + 1, i + 1 , run_loss/2 , run_c_loss/2)) 383 | #print('[%d,%5d] Classification loss: %.3f' % (epoch + 1, i + 1, run_c_loss/10)) 384 | run_c_loss = 0.0 385 | run_loss = 0.0 386 | 387 | 388 | decoder_path = os.path.join('/home/deepkliv/Desktop/AE/ram/AE_classifier/fashion/Decoder/', 'decoder-%d.pkl' %(epoch+1)) 389 | encoder_path = os.path.join('/home/deepkliv/Desktop/AE/ram/AE_classifier/fashion/Encoder/', 'encoder-%d.pkl' %(epoch+1)) 390 | autoencoder_path = os.path.join('/home/deepkliv/Desktop/AE/ram/AE_classifier/fashion/Autoencoder/', 'autoencoder-%d.pkl' %(epoch+1)) 391 | classifier_path = os.path.join('/home/deepkliv/Desktop/AE/ram/AE_classifier/fashion/Classifier/', 'classifier-%d.pkl' %(epoch+1)) 392 | classification_path = os.path.join('/home/deepkliv/Desktop/AE/ram/AE_classifier/fashion/Classification','classification-%d.pkl' %(epoch+1)) 393 | 394 | torch.save(decoder.state_dict(), decoder_path) 395 | torch.save(encoder.state_dict(), encoder_path) 396 | torch.save(autoencoder.state_dict(), autoencoder_path) 397 | torch.save(classifier.state_dict(), classifier_path) 398 | torch.save(classification.state_dict(), classification_path) 399 | 400 | if ( epoch+1 )% 1 == 0: 401 | list_a_loss.append(run_loss/5000) 402 | list_c_loss.append(run_c_loss/5000) 403 | 404 | #plt.plot(range(epoch+1),list_a_loss,'r--',label='autoencoder') 405 | #plt.plot(range(epoch+1),list_c_loss,'b--',label='classifier') 406 | #if epoch==0: 407 | #plt.legend(loc='upper left') 408 | #plt.xlabel('Epochs') 409 | #plt.ylabel('Loss') 410 | #fig.savefig('/home/deepkliv/Saket/loss_plot.png') 411 | correct = 0 412 | total = 0 413 | print('\n Testing ....') 414 | autoencoder.train(False) # For training 415 | classification.train(False) 416 | for t_i,t_data in enumerate(testloader): 417 | 418 | if t_i * batch_size >1000: 419 | break 420 | t_inputs,t_labels = t_data 421 | t_inputs = Variable(t_inputs).cuda() 422 | t_labels = t_labels.cuda() 423 | t_outputs = autoencoder(t_inputs) 424 | c_pred = classification(t_inputs) 425 | _, predicted = torch.max(c_pred.data, 1) 426 | #print predicted.type() , t_labels.type() 427 | total += t_labels.size(0) 428 | correct += (predicted == t_labels).sum() 429 | if (epoch + 1)%1 == 0: 430 | print("saving image") 431 | test_result_path = os.path.join('/home/deepkliv/Desktop/AE/ram/AE_classifier/fashion/Test_results/', 'batch_%d_%d'%((epoch+1)/1,t_i+1) + '.jpg') 432 | image_tensor = torch.cat((t_inputs.data[0:8], t_outputs.data[0:8]), 0) 433 | torchvision.utils.save_image(image_tensor, test_result_path) 434 | 435 | print('Accuracy of the network on the 8000 test images: %d %%' % (100 * correct / total)) 436 | 437 | print('Finished Training and Testing') 438 | 439 | 440 | """ 441 | classification_criterion = nn.NLLLoss() 442 | for i,data in enumerate(testloader): 443 | inputs,labels = data 444 | inputs,labels = Variable(inputs).cuda(), Variable(labels).cuda() 445 | test_model = Autoencoder().cuda() 446 | test_model.load_state_dict(torch.load('/home/deepkliv/Saket/AE_Classifier/Autoencoder/autoencoder-500.pkl',map_location=lambda storage, loc: storage.cuda(1))) 447 | outputs = test_model(inputs) 448 | test_result_path = os.path.join('/home/deepkliv/Saket/AE_Classifier/Test_results/', 'batch_%d'%(i+1) + '.jpg') 449 | image_tensor = torch.cat((inputs.data[0:8], outputs.data[0:8]), 0) 450 | torchvision.utils.save_image(image_tensor, test_result_path) 451 | """ 452 | 453 | --------------------------------------------------------------------------------