% function [e,ev,p,etr] = epitome(x,K,N,T,NIT,sc,e,ev); % % Uses the EM algorithm to learn an N x N epitome from T, K x K patches % drawn randomly from the input image x. If sc is provided, the epitome is learned % by learning a sequence of epitomes at different scales. sc(1) is the first scale, % sc(end) is the final scale. So, the first epitome is of size N*sc(1) x N*sc(1) % and it is learned from the image x, scaled by a factor of sc(1). The resulting % epitome is used to initialize the epitome at the next scale, and so on. At scale % sc(i), the number of training patches selected is sc(i)^2*T. NIT iterations of % EM are applied at each scale. e and ev contain the initial epitome mean and variance % images. If they are not provided, the mean is initialized to uniform image with small % noise, and the variances are uniformly initialized to 1. The probability of using each % in the epitome is returned in p. % % The input images should be scaled so that pixels have values betewen zero and one. % % Reference: % N.Jojic, B.Frey & A.Kannan , "Epitomic Analysis of Appearance and Shape", ICCV 2003 % % Copyright Brendan Frey, Nebojsa Jojic and Anitha Kannan % This code has been developed at Microsoft Research and is available to all researchers % for research purposes only. % % For more information, email jojic@microsoft.com function [e,ev,p,etr]=epitome(x,K,N,T,NIT,sc,e,ev); if nargin<6 sc=[1]; end; % Initialize parameters xFIN=x; x=imresize(xFIN,sc(1),'nearest'); x=(x>0).*x; x=(x<1).*x+(x>=1); SY=size(x,1); SX=size(x,2); numDim = size(x,3); TFIN=T; T=ceil(sc(1)^2*TFIN); NFIN=N; N=ceil(sc(1)*NFIN); if nargin<8 e=zeros(N,N,numDim); ev=ones(N,N,numDim); for i=1:numDim e(:,:,i)=std(reshape(x(:,:,i),[SX*SY 1]))*randn(N,N)/100; e(:,:,i)=e(:,:,i)+mean(reshape(x(:,:,i),[SX*SY 1])); end; end p=ones(N,N)/N^2; onemat=ones(K,K); MINP=1e-6; MINV=0.01; %MINP=1e-30; MINV=1e-6; % Allocate space for trace of epitome and start plotting etr=zeros(NFIN,NFIN,numDim,NIT*length(sc)+1); etr(:,:,:,1)=imresize(e,[NFIN NFIN],'bilinear',0); etri=2; figure(1432); subplot(2,2,1); image(xFIN); axis equal tight off; title([num2str(SY) 'x' num2str(SX) ' Input Image']); drawnow; subplot(2,2,2); image(etr(:,:,:,1)); axis equal tight off; title([num2str(N) 'x' num2str(N) ' Epitome']); drawnow; subplot(2,2,3); imagesc(p,[0 max(p(:))]); colormap(gray); axis equal tight off; title('Mixing Proportions'); drawnow; subplot(2,2,4); tmp=sum(ev,3); imagesc(tmp,[0 max(tmp(:))]); colormap(gray); axis equal tight off; title(['Variances, MAX=' num2str(max(tmp(:)))]); drawnow; disp('Press a key to start'); pause for sci=1:length(sc) % If not first round, scale epitome and training image if sci~=1 N=ceil(sc(sci)*NFIN); e=imresize(e,[N N],'bilinear',0); e=(e>0).*e; e=(e<1).*e+(e>=1); ev=imresize(ev,[N N],'bilinear',0); ev=(ev=MINV).*ev; p=imresize(p,[N N], 'bilinear',0); p=p/sum(p(:)); x=imresize(xFIN,sc(sci),'bilinear',0); x=(x>0).*x; x=(x<1).*x+(x>=1); T=ceil(sc(sci)^2*TFIN); end; % Allocate memory for statistics, etc lP=zeros(N,N); sumP=zeros(N,N); sumPy=zeros(N,N,numDim); sumPy2=zeros(N,N,numDim); ewrap=zeros(N+K-1,N+K-1,numDim); evwrap=zeros(N+K-1,N+K-1,numDim); % Create data set SY=size(x,1); SX=size(x,2); y=zeros(K,K,numDim,T); for t=1:T irnd=ceil(rand*(SY-K+1)); jrnd=ceil(rand*(SX-K+1)); y(:,:,:,t)=x(irnd:irnd+K-1,jrnd:jrnd+K-1,:); end; % Perform NIT iterations of EM lPtr=zeros(NIT,1); for nit=1:NIT % Compute things that are re-used in E step ewrap(1:N,1:N,:)=e; ewrap(N+1:end,:,:)=ewrap(1:K-1,:,:); ewrap(:,N+1:end,:)=ewrap(:,1:K-1,:); evwrap(1:N,1:N,:)=ev; evwrap(N+1:end,:,:)=evwrap(1:K-1,:,:); evwrap(:,N+1:end,:)=evwrap(:,1:K-1,:); logevwrap=log(evwrap); evwrapi=1./evwrap; % E step sumP(:,:)=0; sumPy(:,:,:)=0; sumPy2(:,:,:)=0; for t=1:T % Get current training case yc=y(:,:,:,t); yct=yc(end:-1:1,end:-1:1,:); % Compute posterior over patch location lP(:,:)=log(p); for i=1:numDim tmp1=conv2(evwrapi(:,:,i),yct(:,:,i).^2,'valid'); tmp2=conv2(ewrap(:,:,i).*evwrapi(:,:,i),yct(:,:,i),'valid'); tmp3=conv2(ewrap(:,:,i).^2.*evwrapi(:,:,i),onemat,'valid'); tmp4=conv2(logevwrap(:,:,i),onemat,'valid'); lP=lP-0.5*(tmp4+tmp1-2*tmp2+tmp3); end; mxlP=max(lP(:)); P=exp(lP-mxlP)+MINP; nrmP=sum(P(:)); P=P/nrmP; lPtr(nit)=lPtr(nit)+mxlP+log(nrmP); % Collect sufficient statistics tmp=conv2(P,onemat,'full'); tmp(:,1:K-1)=tmp(:,1:K-1)+tmp(:,N+1:end); tmp(1:K-1,:)=tmp(1:K-1,:)+tmp(N+1:end,:); sumP=sumP+tmp(1:N,1:N); for i=1:numDim % Mean tmp=conv2(P,yc(:,:,i),'full'); tmp(:,1:K-1)=tmp(:,1:K-1)+tmp(:,N+1:end); tmp(1:K-1,:)=tmp(1:K-1,:)+tmp(N+1:end,:); sumPy(:,:,i)=sumPy(:,:,i)+tmp(1:N,1:N); % Variance tmp=conv2(P,yc(:,:,i).^2,'full'); tmp(:,1:K-1)=tmp(:,1:K-1)+tmp(:,N+1:end); tmp(1:K-1,:)=tmp(1:K-1,:)+tmp(N+1:end,:); sumPy2(:,:,i)=sumPy2(:,:,i)+tmp(1:N,1:N); end; end; % Plot learning curve % figure(2); plot(0:nit-1,lPtr(1:nit),'r-'); drawnow; % M step p=sumP/sum(sumP(:)); for i=1:numDim e(:,:,i)=sumPy(:,:,i)./sumP; ev(:,:,i)=sumPy2(:,:,i)./sumP-2*e(:,:,i).*sumPy(:,:,i)./sumP+e(:,:,i).^2; end; ev=(ev=MINV).*ev; etr(:,:,:,etri)=imresize(e,[NFIN NFIN],'bilinear',0); etri=etri+1; % Plot epitome figure(1432); subplot(2,2,1); image(x); axis equal tight off; title([num2str(SY) 'x' num2str(SX) ' Input Image']); subplot(2,2,2); image(etr(:,:,:,etri-1)); axis equal tight off; title([num2str(N) 'x' num2str(N) ' Epitome']); subplot(2,2,3); imagesc(p,[0 max(p(:))]); colormap(gray); axis equal tight off; title('Mixing Proporations'); subplot(2,2,4); tmp=sum(ev,3); imagesc(tmp,[0 max(tmp(:))]); colormap(gray); axis equal tight off; title(['Variances, MAX=' num2str(max(tmp(:)))]); drawnow; end; save etrace.mat; end;