def test_ais_with_dbn(self): dbn = DBN(RBM(10, 10)) dbn.add_layer(RBM(10, 20)) hidden_states = utils.binary_numbers(dbn[0].Y.shape[0]) ais = Estimator(dbn) ais_logz = ais.estimate_log_partition_function(100, np.sqrt(np.arange(0, 1, 0.001))) brf_logz = utils.logsumexp(dbn[1]._ulogprob_vis(hidden_states)) brf_probs = [] dbn_probs = [] dbn_bound = [] for i in range(50): sample = np.matrix(np.random.rand(10, 1)) > 0.5 prob, bound = ais.estimate_log_probability(sample, 200) brf_probs.append(utils.logsumexp(dbn[1]._ulogprob_vis(hidden_states) + dbn[0]._clogprob_vis_hid(sample, hidden_states), 1) - brf_logz) dbn_probs.append(prob) dbn_bound.append(bound) self.assertTrue(np.mean(dbn_bound) < np.mean(dbn_probs)) self.assertTrue(np.abs(np.mean(dbn_probs) - np.mean(brf_probs)) < 0.1)
def test_ais_with_semirbm_dbn(self): dbn = DBN(RBM(5, 5)) dbn.add_layer(SemiRBM(5, 5)) ais = Estimator(dbn) ais.estimate_log_partition_function(100, np.arange(0, 1, 1E-3), layer=0) ais.estimate_log_partition_function(10, np.arange(0, 1, 1E-3), layer=1) dbn[0]._brf_logz = utils.logsumexp(dbn[0]._ulogprob_vis( utils.binary_numbers(dbn[0].X.shape[0]))) dbn[1]._brf_logz = utils.logsumexp(dbn[1]._ulogprob_vis( utils.binary_numbers(dbn[1].X.shape[0]))) samples = np.concatenate( [dbn.sample(25, 100, 20), np.matrix(np.random.rand(5, 25) > 0.5)], 1) Y = utils.binary_numbers(dbn[0].Y.shape[0]) X = utils.binary_numbers(dbn[0].X.shape[0]) logRy = dbn[1]._ulogprob_vis(Y) logQy = utils.logsumexp(dbn[0]._ulogprob(X, Y, all_pairs=True), 0) log_sum = utils.logsumexp( dbn[0]._clogprob_hid_vis(samples, Y, all_pairs=True) - logQy + logRy, 1) logPx = log_sum + dbn[0]._ulogprob_vis(samples) - dbn[1]._brf_logz logPx_ = ais.estimate_log_probability(samples)[0] self.assertTrue(np.abs(logPx_.mean() - logPx.mean()) < 0.1)
def test_ais_with_dbn_sanity_check(self): dbn = DBN(RBM(5, 20)) dbn.add_layer(RBM(20, 5)) dbn.add_layer(RBM(5, 20)) dbn[0].W = np.matrix(np.random.randn(5, 20)) dbn[0].b = np.matrix(np.random.rand(5, 1) - 0.5) dbn[0].c = np.matrix(np.random.rand(20, 1) - 0.5) dbn[1].W = dbn[0].W.T dbn[1].b = dbn[0].c dbn[1].c = dbn[0].b dbn[2].W = dbn[0].W dbn[2].b = dbn[0].b dbn[2].c = dbn[0].c samples = dbn.sample(100) ais = Estimator(dbn[0]) rbm_logz = ais.estimate_log_partition_function(100, np.sqrt(np.arange(0, 1, 0.001))) rbm_probs = ais.estimate_log_probability(samples) ais = Estimator(dbn) dbn_logz = ais.estimate_log_partition_function(100, np.sqrt(np.arange(0, 1, 0.001))) dbn_probs = ais.estimate_log_probability(samples) self.assertTrue(abs(dbn_logz - rbm_logz) / rbm_logz < 0.02) self.assertTrue((np.exp(dbn_probs) - np.exp(rbm_probs)).mean() < 0.02)
def test_ais_with_dbn(self): dbn = DBN(RBM(10, 10)) dbn.add_layer(RBM(10, 20)) hidden_states = utils.binary_numbers(dbn[0].Y.shape[0]) ais = Estimator(dbn) ais_logz = ais.estimate_log_partition_function( 100, np.sqrt(np.arange(0, 1, 0.001))) brf_logz = utils.logsumexp(dbn[1]._ulogprob_vis(hidden_states)) brf_probs = [] dbn_probs = [] dbn_bound = [] for i in range(50): sample = np.matrix(np.random.rand(10, 1)) > 0.5 prob, bound = ais.estimate_log_probability(sample, 200) brf_probs.append( utils.logsumexp( dbn[1]._ulogprob_vis(hidden_states) + dbn[0]._clogprob_vis_hid(sample, hidden_states), 1) - brf_logz) dbn_probs.append(prob) dbn_bound.append(bound) self.assertTrue(np.mean(dbn_bound) < np.mean(dbn_probs)) self.assertTrue(np.abs(np.mean(dbn_probs) - np.mean(brf_probs)) < 0.1)
def test_ais_with_dbn_sanity_check(self): dbn = DBN(RBM(5, 20)) dbn.add_layer(RBM(20, 5)) dbn.add_layer(RBM(5, 20)) dbn[0].W = np.matrix(np.random.randn(5, 20)) dbn[0].b = np.matrix(np.random.rand(5, 1) - 0.5) dbn[0].c = np.matrix(np.random.rand(20, 1) - 0.5) dbn[1].W = dbn[0].W.T dbn[1].b = dbn[0].c dbn[1].c = dbn[0].b dbn[2].W = dbn[0].W dbn[2].b = dbn[0].b dbn[2].c = dbn[0].c samples = dbn.sample(100) ais = Estimator(dbn[0]) rbm_logz = ais.estimate_log_partition_function( 100, np.sqrt(np.arange(0, 1, 0.001))) rbm_probs = ais.estimate_log_probability(samples) ais = Estimator(dbn) dbn_logz = ais.estimate_log_partition_function( 100, np.sqrt(np.arange(0, 1, 0.001))) dbn_probs = ais.estimate_log_probability(samples) self.assertTrue(abs(dbn_logz - rbm_logz) / rbm_logz < 0.02) self.assertTrue((np.exp(dbn_probs) - np.exp(rbm_probs)).mean() < 0.02)
def main(argv): # load preprocessed data samples print 'loading data...\t', data = load('../data/vanhateren.npz') print '[DONE]' print # remove DC component (first component) data_train = data['train'][1:, :] data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=data_train.shape[0], num_hiddens=100)) # hyperparameters dbn[0].learning_rate = 5E-3 dbn[0].weight_decay = 1E-2 dbn[0].momentum = 0.9 dbn[0].sigma = 0.65 dbn[0].cd_steps = 1 dbn[0].persistent = True # train 1st layer print 'training...\t', dbn.train(data_train, num_epochs=100, batch_size=100) print '[DONE]' # evaluate 1st layer print 'evaluating...\t', logptf = dbn.estimate_log_partition_function(num_ais_samples=100, beta_weights=arange(0, 1, 1E-3)) loglik = dbn.estimate_log_likelihood(data_test) print '[DONE]' print print 'estimated log-partf.:\t', logptf print 'estimated log-loss:\t', -loglik / data_test.shape[0] / log(2) print
def test_ais_with_semirbm_dbn(self): dbn = DBN(RBM(5, 5)) dbn.add_layer(SemiRBM(5, 5)) ais = Estimator(dbn) ais.estimate_log_partition_function(100, np.arange(0, 1, 1E-3), layer=0) ais.estimate_log_partition_function(10, np.arange(0, 1, 1E-3), layer=1) dbn[0]._brf_logz = utils.logsumexp(dbn[0]._ulogprob_vis(utils.binary_numbers(dbn[0].X.shape[0]))) dbn[1]._brf_logz = utils.logsumexp(dbn[1]._ulogprob_vis(utils.binary_numbers(dbn[1].X.shape[0]))) samples = np.concatenate([dbn.sample(25, 100, 20), np.matrix(np.random.rand(5, 25) > 0.5)], 1) Y = utils.binary_numbers(dbn[0].Y.shape[0]) X = utils.binary_numbers(dbn[0].X.shape[0]) logRy = dbn[1]._ulogprob_vis(Y) logQy = utils.logsumexp(dbn[0]._ulogprob(X, Y, all_pairs=True), 0) log_sum = utils.logsumexp(dbn[0]._clogprob_hid_vis(samples, Y, all_pairs=True) - logQy + logRy, 1) logPx = log_sum + dbn[0]._ulogprob_vis(samples) - dbn[1]._brf_logz logPx_ = ais.estimate_log_probability(samples)[0] self.assertTrue(np.abs(logPx_.mean() - logPx.mean()) < 0.1)
def main(argv): num_visibles = 28 * 28 num_hiddens = [1000, 1000] num_epochs = 50 batch_size = 100 # load data samples data = load('../data/mnist.npz')['train'] / 255. # train 1st layer dbn = DBN(RBM(num_visibles, num_hiddens[0])) dbn.train(data, num_epochs, batch_size) # train 2nd layer dbn.add_layer(RBM(num_hiddens[0], num_hiddens[1])) dbn.train(data, num_epochs, batch_size, [1E-1, 1E-2]) return 0
def main(argv): # load preprocessed data samples print 'loading data...\t', data = load('../data/vanhateren.npz') print '[DONE]' print # remove DC component (first component) data_train = data['train'][1:, :] data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=data_train.shape[0], num_hiddens=100)) # hyperparameters dbn[0].learning_rate = 5E-3 dbn[0].weight_decay = 1E-2 dbn[0].momentum = 0.9 dbn[0].sigma = 0.65 dbn[0].cd_steps = 1 dbn[0].persistent = True # train 1st layer print 'training...\t', dbn.train(data_train, num_epochs=100, batch_size=100) print '[DONE]' # evaluate 1st layer print 'evaluating...\t', logptf = dbn.estimate_log_partition_function(num_ais_samples=100, beta_weights=arange( 0, 1, 1E-3)) loglik = dbn.estimate_log_likelihood(data_test) print '[DONE]' print print 'estimated log-partf.:\t', logptf print 'estimated log-loss:\t', -loglik / data_test.shape[0] / log(2) print
def main(argv): # load preprocessed data samples print 'loading data...\t', #data_train, data_test = genData() #load('../data/vanhateren.npz') category = "office" data_train_0,data_train_1,data_train_2, data_test = load_data_BS("..\data\watersurface\orignal_color.pkl") img = Image.open("..\data\watersurface\hand_segmented_01850.bmp") x_, y_ = (asarray(img)[:,:,0]).shape print "Doie : " , (asarray(img)[:,:,0]).shape groundtruth = (((asarray(img)[:,:,0])/255.0 > 0.5) * 1).flatten() #groundtruth = (((asarray(img)[:,:])/255.0 > 0.5) * 1).flatten() print '[DONE]' print data_test.shape # remove DC component (first component) # data_train = data['train'][1:, :] # data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=data_train_0.shape[0], num_hiddens=8)) dbn1 = DBN(GaussianRBM(num_visibles=data_train_1.shape[0], num_hiddens=8)) dbn2 = DBN(GaussianRBM(num_visibles=data_train_2.shape[0], num_hiddens=8)) dbn[0].learning_rate = 0.001 dbn1[0].learning_rate = 0.001 dbn2[0].learning_rate = 0.001 # train 1st layer print 'training...\t', dbn.train(data_train_0, num_epochs=5, batch_size=1,shuffle=False) dbn1.train(data_train_1, num_epochs=5, batch_size=1,shuffle=False) dbn2.train(data_train_2, num_epochs=5, batch_size=1,shuffle=False) print '[DONE]' data_test_0 = ((data_test.T)[:,::3]).T data_test_1 = ((data_test.T)[:,1::3]).T data_test_2 = ((data_test.T)[:,2::3]).T # global runtime # sFilename = "data\\weightlogs" # f = h5.File(sFileName+".hdf5") # f.create_group(runtime) # group.create_dataset('parameters',data=par) #storeWeights(category,"data\\weightlogs", dbn[0].W, dbn[0].vsigma,dbn1[0].W, dbn1[0].vsigma,dbn2[0].W, dbn2[0].vsigma ) Ndat = 25 #data_test_0.shape[1] Nsteps = 5 # evaluate 1st layer print 'evaluating 1...\t', dataout = zeros(x_*y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_0[:,point]).T X = asmatrix(data_test_0[:,-1]).T #dataout = vstack((dataout,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn[0].forward(X) # self.activ(1) X = dbn[0].backward(Y,X) #print "S hsape:", X.shape #dataout = vstack((dataout,X.flatten())) dataout = vstack((dataout,subtract(asarray(X),data_test_0[:,-1],asarray(dbn[0].vsigma),point+1))) print 'evaluating 2...\t', dataout1 = zeros(x_*y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_1[:,point]).T X = asmatrix(data_test_1[:,-1]).T #dataout1 = vstack((dataout1,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn1[0].forward(X) # self.activ(1) X = dbn1[0].backward(Y,X) #print "S hsape:", X.shape #dataout1 = vstack((dataout1,X.flatten())) dataout1 = vstack((dataout1,subtract(asarray(X),data_test_1[:,-1],asarray(dbn1[0].vsigma),point+1))) print 'evaluating 3...\t', dataout2 = zeros(x_*y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_2[:,point]).T X = asmatrix(data_test_2[:,-1]).T #dataout2 = vstack((dataout2,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn2[0].forward(X) # self.activ(1) X = dbn2[0].backward(Y,X) #print "S hsape:", X.shape #dataout2 = vstack((dataout2,X.flatten())) dataout2 = vstack((dataout2,subtract(asarray(X),data_test_2[:,-1],asarray(dbn2[0].vsigma),point+1))) # plt.imshow((reshape(data_test[::3,5],(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.axis('off') # plt.show() plt.figure(1) for i in range(Ndat): plt.subplot(5,5,i+1) d = multiply(asarray(dataout[i+1,:]),asarray(dataout1[i+1,:]),asarray(dataout2[i+1,:])) d = mod(d+1,2) #open_img = ndimage.binary_opening(reshape(d,(x_,y_))) #close_img = ndimage.binary_closing(open_img) m_filter = ndimage.median_filter(reshape(d,(x_,y_)),8) # plt.figure(3) # plt.imshow(close_img) # plt.show() d = m_filter.flatten() print "Image Example Fmeaure: ",i," : ", f_measure(d,groundtruth) * 100 # d[0::3] = asarray(dataout[i+1,:]) # d[1::3] = asarray(dataout1[i+1,:]) # d[2::3] = asarray(dataout2[i+1,:]) # d[:,:,0] = (reshape(asarray(dataout[i+1,:]),(x_,y_))) # d[:,:,1] = (reshape(asarray(dataout1[i+1,:]),(x_,y_))) # d[:,:,2] = (reshape(asarray(dataout2[i+1,:]),(x_,y_))) #img_s = Image.fromarray(asarray(reshape(d,(x_,y_))*255,dtype="uint8")) #img_s.save("C:\work\\backgdSubt\dataset\datasets\change detection\\baseline\\baseline\office\grbm" + "\\" + str(i) + ".bmp") plt.imshow(reshape(d,(x_,y_)), cmap = cm.Greys_r, interpolation ="nearest") plt.axis('off') plt.figure(2) for k in range(8): plt.subplot(4,2,k+1) d = zeros((x_*y_*3)) d[0::3] = asarray(dbn[0].W[:,k].flatten()) d[1::3] = asarray(dbn1[0].W[:,k].flatten()) d[2::3] = asarray(dbn2[0].W[:,k].flatten()) plt.imshow(reshape(d,(x_,y_,3)))#, cmap = cm.Greys_r, interpolation ="nearest") plt.axis('off') # plt.figure() # plt.imshow((reshape(dbn[0].vsigma[:19200],(x_,y_)))) # plt.figure(2) # plt.imshow((reshape(dbn[0].vsigma[19200:19200*2],(x_,y_)))) # plt.figure(3) # plt.imshow((reshape(dbn[0].vsigma[19200*2:19200*3],(x_,y_)))) plt.figure(3) print type(dbn[0].vsigma) plt.imshow(reshape(asarray(dbn[0].vsigma),(x_,y_))) plt.show() print dbn[0].vsigma p.show()
def main(argv): # load preprocessed data samples print "loading data...\t", # data_train, data_test = genData() #load('../data/vanhateren.npz') data_train_0, data_train_1, data_train_2, data_test = load_data_BS("data/lightswitch/orignal_color.pkl") print "[DONE]" # remove DC component (first component) # data_train = data['train'][1:, :] # data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=data_train_0.shape[0], num_hiddens=8)) dbn1 = DBN(GaussianRBM(num_visibles=data_train_1.shape[0], num_hiddens=8)) dbn2 = DBN(GaussianRBM(num_visibles=data_train_2.shape[0], num_hiddens=8)) # train 1st layer print "training...\t", dbn.train(data_train_0, num_epochs=10, batch_size=1, shuffle=False) dbn1.train(data_train_1, num_epochs=10, batch_size=1, shuffle=False) dbn2.train(data_train_2, num_epochs=10, batch_size=1, shuffle=False) print "[DONE]" data_test_0 = ((data_test.T)[:, ::3]).T data_test_1 = ((data_test.T)[:, 1::3]).T data_test_2 = ((data_test.T)[:, 2::3]).T Ndat = data_test_0.shape[1] Nsteps = 5 # evaluate 1st layer print "evaluating 1...\t", dataout = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): X = asmatrix(data_test_0[:, point]).T # dataout = vstack((dataout,X.flatten())) # print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn[0].forward(X) # self.activ(1) X = dbn[0].backward(Y, X) # print "S hsape:", X.shape # dataout = vstack((dataout,X.flatten())) dataout = vstack((dataout, subtract(asarray(X), data_test_0[:, point], asarray(dbn[0].vsigma)))) print "evaluating 2...\t", dataout1 = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): X = asmatrix(data_test_1[:, point]).T # dataout1 = vstack((dataout1,X.flatten())) # print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn1[0].forward(X) # self.activ(1) X = dbn1[0].backward(Y, X) # print "S hsape:", X.shape # dataout1 = vstack((dataout1,X.flatten())) dataout1 = vstack((dataout1, subtract(asarray(X), data_test_1[:, point], asarray(dbn1[0].vsigma)))) print "evaluating 3...\t", dataout2 = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): X = asmatrix(data_test_2[:, point]).T # dataout2 = vstack((dataout2,X.flatten())) # print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn2[0].forward(X) # self.activ(1) X = dbn2[0].backward(Y, X) # print "S hsape:", X.shape # dataout2 = vstack((dataout2,X.flatten())) dataout2 = vstack((dataout2, subtract(asarray(X), data_test_2[:, point], asarray(dbn2[0].vsigma)))) # plt.imshow((reshape(data_test[::3,5],(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.axis('off') # plt.show() plt.figure(1) for i in range(Ndat): plt.subplot(5, 5, i + 1) d = multiply(asarray(dataout[i + 1, :]), asarray(dataout1[i + 1, :]), asarray(dataout2[i + 1, :])) # d[0::3] = asarray(dataout[i+1,:]) # d[1::3] = asarray(dataout1[i+1,:]) # d[2::3] = asarray(dataout2[i+1,:]) # d[:,:,0] = (reshape(asarray(dataout[i+1,:]),(x_,y_))) # d[:,:,1] = (reshape(asarray(dataout1[i+1,:]),(x_,y_))) # d[:,:,2] = (reshape(asarray(dataout2[i+1,:]),(x_,y_))) plt.imshow(reshape(d, (x_, y_)), cmap=cm.Greys_r, interpolation="nearest") plt.axis("off") plt.figure(2) for k in range(8): plt.subplot(4, 2, k + 1) d = zeros((x_ * y_ * 3)) d[0::3] = asarray(dbn[0].W[:, k].flatten()) d[1::3] = asarray(dbn1[0].W[:, k].flatten()) d[2::3] = asarray(dbn2[0].W[:, k].flatten()) plt.imshow(reshape(d, (x_, y_, 3))) # , cmap = cm.Greys_r, interpolation ="nearest") plt.axis("off") # plt.figure() # plt.imshow((reshape(dbn[0].vsigma[:19200],(x_,y_)))) # plt.figure(2) # plt.imshow((reshape(dbn[0].vsigma[19200:19200*2],(x_,y_)))) # plt.figure(3) # plt.imshow((reshape(dbn[0].vsigma[19200*2:19200*3],(x_,y_)))) plt.figure(3) print type(dbn[0].vsigma) plt.imshow(reshape(asarray(dbn[0].vsigma), (x_, y_))) plt.show() print dbn[0].vsigma p.show()
def main(argv): # load preprocessed data samples print 'loading data...\t', #data_train, data_test = genData() #load('../data/vanhateren.npz') #data_train_0,data_train_1,data_train_2, data_test = load_data_BS("data/changedetection\\baseline\highway/orignal_color.pkl") #groundtruth = (((asarray(img)[:,:])/255.0 > 0.5) * 1).flatten() print '[DONE]' # remove DC component (first component) # data_train = data['train'][1:, :] # data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=x_ * y_, num_hiddens=8)) dbn1 = DBN(GaussianRBM(num_visibles=x_ * y_, num_hiddens=8)) dbn2 = DBN(GaussianRBM(num_visibles=x_ * y_, num_hiddens=8)) f = h5.File("experiments\weightlogs.hdf5", 'r') category = "office" dbn[0].W = (f[category])["weights"][:] dbn1[0].W = (f[category])["weights1"][:] dbn2[0].W = (f[category])["weights2"][:] dbn[0].vsigma = (f[category])["sigma"][:] dbn1[0].vsigma = (f[category])["sigma1"][:] dbn2[0].vsigma = (f[category])["sigma2"][:] # dbn[0].learning_rate = 0.001 # dbn1[0].learning_rate = 0.001 # dbn2[0].learning_rate = 0.001 # train 1st layer # print 'training...\t', # dbn.train(data_train_0, num_epochs=1, batch_size=1,shuffle=False) # dbn1.train(data_train_1, num_epochs=1, batch_size=1,shuffle=False) # dbn2.train(data_train_2, num_epochs=1, batch_size=1,shuffle=False) # print '[DONE]' srcDir = "C:\work\\backgdSubt\dataset\datasets\change detection\\baseline\\baseline\highway\input" targetDir = "C:\work\\backgdSubt\dataset\datasets\change detection\\baseline\\baseline\highway\grbm" ii = 1 for k in os.listdir(srcDir): imf = Image.open(srcDir + '/' + k) print array(imf).shape data_test = (array(imf)).flatten() #dataset = np.vstack((dataset,d)) data_test_0 = ((data_test)[::3]).T data_test_1 = ((data_test)[1::3]).T data_test_2 = ((data_test)[2::3]).T Nsteps = 5 # evaluate 1st layer print 'evaluating 1...\t', # #datasub = zeros(x_*y_) #for point in xrange(Ndat): #X = asmatrix(data_test_0[:,point]).T X = asmatrix(data_test_0).T #dataout = vstack((dataout,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn[0].forward(X) # self.activ(1) X = dbn[0].backward(Y, X) #print "S hsape:", X.shape #dataout = vstack((dataout,X.flatten())) dataout = subtract(asarray(X), data_test_0, asarray(dbn[0].vsigma), 10) #X = asmatrix(data_test_1[:,point]).T X1 = asmatrix(data_test_1).T #dataout1 = vstack((dataout1,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y1 = dbn1[0].forward(X1) # self.activ(1) X1 = dbn1[0].backward(Y1, X1) #print "S hsape:", X.shape #dataout1 = vstack((dataout1,X.flatten())) dataout1 = subtract(asarray(X1), data_test_1, asarray(dbn1[0].vsigma), 10) X2 = asmatrix(data_test_2).T #dataout2 = vstack((dataout2,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y2 = dbn2[0].forward(X2) # self.activ(1) X2 = dbn2[0].backward(Y2, X2) #print "S hsape:", X.shape #dataout2 = vstack((dataout2,X.flatten())) # plt.imshow(reshape(X,(x_,y_))) # plt.show() dataout2 = subtract(asarray(X2), data_test_2, asarray(dbn2[0].vsigma), 10) # plt.imshow((reshape(dataout,(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.figure(2) # plt.imshow((reshape(dataout1,(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.figure(3) # plt.imshow((reshape(dataout2,(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.axis('off') # plt.show() # plt.figure(1) # for i in range(Ndat): # plt.subplot(5,5,i+1) d = multiply(asarray(dataout[:]), asarray(dataout1[:]), asarray(dataout2[:])) d = mod(d + 1, 2) # plt.imshow(reshape(d,(x_,y_)),cmap = cm.Greys_r, interpolation ="nearest") # print type(d[0]) # #plt.savefig(targetDir + "\\" + str(ii) + ".png") #print img_s = Image.fromarray( asarray(reshape(d, (x_, y_)) * 255, dtype="uint8")) # plt.figure(2) # plt.imshow(img_s)#,cmap = cm.Greys_r, interpolation ="nearest") #plt.show() img_s.save(targetDir + "\\bin00" + str(ii) + ".bmp") ii = ii + 1
def main(argv): # load preprocessed data samples print 'loading data...\t', #data_train, data_test = genData() #load('../data/vanhateren.npz') data_train_0,data_train_1,data_train_2, data_test = load_data_BS("data/changedetection\\baseline\highway\orignal_color.pkl") img = Image.open("data/changedetection\\baseline\highway/gt001367.png") print "Doie : " , (asarray(img)[:,:]).shape groundtruth = (((asarray(img)[:,:])/255.0 > 0.5) * 1).flatten() #groundtruth = (((asarray(img)[:,:])/255.0 > 0.5) * 1).flatten() print '[DONE]' # remove DC component (first component) # data_train = data['train'][1:, :] # data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=data_train_0.shape[0], num_hiddens=8)) dbn1 = DBN(GaussianRBM(num_visibles=data_train_1.shape[0], num_hiddens=8)) dbn2 = DBN(GaussianRBM(num_visibles=data_train_2.shape[0], num_hiddens=8)) dbn[0].learning_rate = 0.0001 dbn1[0].learning_rate = 0.0001 dbn2[0].learning_rate = 0.0001 # train 1st layer print 'training...\t', dbn.train(data_train_0, num_epochs=25, batch_size=1,shuffle=False) dbn1.train(data_train_1, num_epochs=25, batch_size=1,shuffle=False) dbn2.train(data_train_2, num_epochs=25, batch_size=1,shuffle=False) print '[DONE]' data_test_0 = ((data_test.T)[:,::3]).T data_test_1 = ((data_test.T)[:,1::3]).T data_test_2 = ((data_test.T)[:,2::3]).T Ndat = 20 #data_test_0.shape[1] Nsteps = 5 # evaluate 1st layer print 'evaluating 1...\t', dataout = zeros(x_*y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_0[:,point]).T X = asmatrix(data_test_0[:,-1]).T #dataout = vstack((dataout,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn[0].forward(X) # self.activ(1) X = dbn[0].backward(Y,X) #print "S hsape:", X.shape #dataout = vstack((dataout,X.flatten())) dataout = vstack((dataout,subtract(asarray(X),data_test_0[:,-1],asarray(dbn[0].vsigma),point+1))) print 'evaluating 2...\t', dataout1 = zeros(x_*y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_1[:,point]).T X = asmatrix(data_test_1[:,-1]).T #dataout1 = vstack((dataout1,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn1[0].forward(X) # self.activ(1) X = dbn1[0].backward(Y,X) #print "S hsape:", X.shape #dataout1 = vstack((dataout1,X.flatten())) dataout1 = vstack((dataout1,subtract(asarray(X),data_test_1[:,-1],asarray(dbn1[0].vsigma),point+1))) print 'evaluating 3...\t', dataout2 = zeros(x_*y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_2[:,point]).T X = asmatrix(data_test_2[:,-1]).T #dataout2 = vstack((dataout2,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn2[0].forward(X) # self.activ(1) X = dbn2[0].backward(Y,X) #print "S hsape:", X.shape #dataout2 = vstack((dataout2,X.flatten())) dataout2 = vstack((dataout2,subtract(asarray(X),data_test_2[:,-1],asarray(dbn2[0].vsigma),point+1))) # plt.imshow((reshape(data_test[::3,5],(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.axis('off') # plt.show() plt.figure(1) for i in range(Ndat): plt.subplot(5,5,i+1) d = multiply(asarray(dataout[i+1,:]),asarray(dataout1[i+1,:]),asarray(dataout2[i+1,:])) d = mod(d+1,2) f_measure(d,groundtruth) #print "Image Example Fmeaure: ",i," : ", f_measure(d,groundtruth) * 100 # d[0::3] = asarray(dataout[i+1,:]) # d[1::3] = asarray(dataout1[i+1,:]) # d[2::3] = asarray(dataout2[i+1,:]) # d[:,:,0] = (reshape(asarray(dataout[i+1,:]),(x_,y_))) # d[:,:,1] = (reshape(asarray(dataout1[i+1,:]),(x_,y_))) # d[:,:,2] = (reshape(asarray(dataout2[i+1,:]),(x_,y_))) plt.imshow(reshape(d,(x_,y_)), cmap = cm.Greys_r, interpolation ="nearest") plt.axis('off') plt.figure(2) for k in range(8): plt.subplot(4,2,k+1) d = zeros((x_*y_*3)) d[0::3] = asarray(dbn[0].W[:,k].flatten()) d[1::3] = asarray(dbn1[0].W[:,k].flatten()) d[2::3] = asarray(dbn2[0].W[:,k].flatten()) plt.imshow(reshape(d,(x_,y_,3)))#, cmap = cm.Greys_r, interpolation ="nearest") plt.axis('off') # plt.figure() # plt.imshow((reshape(dbn[0].vsigma[:19200],(x_,y_)))) # plt.figure(2) # plt.imshow((reshape(dbn[0].vsigma[19200:19200*2],(x_,y_)))) # plt.figure(3) # plt.imshow((reshape(dbn[0].vsigma[19200*2:19200*3],(x_,y_)))) plt.figure(3) print type(dbn[0].vsigma) plt.imshow(reshape(asarray(dbn[0].vsigma),(x_,y_))) plt.show() print dbn[0].vsigma p.show()
def main(argv): # load preprocessed data samples print 'loading data...\t', #data_train, data_test = genData() #load('../data/vanhateren.npz') DIR1 = "data/changedetection\\nightvideos\\tramstation" data_train_0, data_train_1, data_train_2, data_test = load_data_BS( DIR1 + "/orignal_color.pkl") img = Image.open(DIR1 + "/gt001330.png") x_, y_ = (asarray(img)[:, :]).shape print "Doie : ", x_, y_ groundtruth = (((asarray(img)[:, :]) / 255.0 > 0.5) * 1).flatten() #groundtruth = (((asarray(img)[:,:])/255.0 > 0.5) * 1).flatten() print '[DONE]' print data_test.shape # remove DC component (first component) # data_train = data['train'][1:, :] # data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=data_train_0.shape[0], num_hiddens=8)) dbn1 = DBN(GaussianRBM(num_visibles=data_train_1.shape[0], num_hiddens=8)) dbn2 = DBN(GaussianRBM(num_visibles=data_train_2.shape[0], num_hiddens=8)) dbn[0].learning_rate = 0.001 dbn1[0].learning_rate = 0.001 dbn2[0].learning_rate = 0.001 # train 1st layer print 'training...\t', dbn.train(data_train_0, num_epochs=5, batch_size=1, shuffle=False) dbn1.train(data_train_1, num_epochs=5, batch_size=1, shuffle=False) dbn2.train(data_train_2, num_epochs=5, batch_size=1, shuffle=False) print '[DONE]' data_test_0 = ((data_test.T)[:, ::3]).T data_test_1 = ((data_test.T)[:, 1::3]).T data_test_2 = ((data_test.T)[:, 2::3]).T # global runtime # sFilename = "data\\weightlogs" # f = h5.File(sFileName+".hdf5") # f.create_group(runtime) # group.create_dataset('parameters',data=par) #storeWeights(category,"data\\weightlogs", dbn[0].W, dbn[0].vsigma,dbn1[0].W, dbn1[0].vsigma,dbn2[0].W, dbn2[0].vsigma ) coff = 8 Ndat = 20 #data_test_0.shape[1] Nsteps = 5 # evaluate 1st layer print 'evaluating 1...\t', dataout = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_0[:,point]).T X = asmatrix(data_test_0[:, -1]).T #dataout = vstack((dataout,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn[0].forward(X) # self.activ(1) X = dbn[0].backward(Y, X) #print "S hsape:", X.shape #dataout = vstack((dataout,X.flatten())) dataout = vstack((dataout, subtract(asarray(X), data_test_0[:, -1], asarray(dbn[0].vsigma), point + 1))) print 'evaluating 2...\t', dataout1 = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_1[:,point]).T X = asmatrix(data_test_1[:, -1]).T #dataout1 = vstack((dataout1,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn1[0].forward(X) # self.activ(1) X = dbn1[0].backward(Y, X) #print "S hsape:", X.shape #dataout1 = vstack((dataout1,X.flatten())) dataout1 = vstack((dataout1, subtract(asarray(X), data_test_1[:, -1], asarray(dbn1[0].vsigma), point + 1))) print 'evaluating 3...\t', dataout2 = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_2[:,point]).T X = asmatrix(data_test_2[:, -1]).T #dataout2 = vstack((dataout2,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn2[0].forward(X) # self.activ(1) X = dbn2[0].backward(Y, X) #print "S hsape:", X.shape #dataout2 = vstack((dataout2,X.flatten())) dataout2 = vstack((dataout2, subtract(asarray(X), data_test_2[:, -1], asarray(dbn2[0].vsigma), point + 1))) # plt.imshow((reshape(data_test[::3,5],(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.axis('off') # plt.show() fmeasureV = zeros(Ndat) plt.figure(1) for i in range(Ndat): plt.subplot(5, 5, i + 1) d = multiply(asarray(dataout[i + 1, :]), asarray(dataout1[i + 1, :]), asarray(dataout2[i + 1, :])) d = mod(d + 1, 2) open_img = ndimage.median_filter(reshape(d, (x_, y_)), coff) d = open_img.flatten() fmeasureV[i] = f_measure(d, groundtruth) print "Image Example Fmeaure: ", i, " : ", fmeasureV[i] * 100 # d[0::3] = asarray(dataout[i+1,:]) # d[1::3] = asarray(dataout1[i+1,:]) # d[2::3] = asarray(dataout2[i+1,:]) # d[:,:,0] = (reshape(asarray(dataout[i+1,:]),(x_,y_))) # d[:,:,1] = (reshape(asarray(dataout1[i+1,:]),(x_,y_))) # d[:,:,2] = (reshape(asarray(dataout2[i+1,:]),(x_,y_))) # img_s = Image.fromarray(asarray(reshape(d,(x_,y_))*255,dtype="uint8")) # img_s.save("C:\work\\backgdSubt\dataset\datasets\change detection\\baseline\\baseline\office\grbm" + "\\" + str(i) + ".bmp") plt.imshow(reshape(d, (x_, y_)), cmap=cm.Greys_r, interpolation="nearest") plt.axis('off') #plt.figure(2) plt.show() DIR2 = "C:\work\\backgdSubt\dataset\datasets\change detection\\nightvideos\\tramstation" srcDir = DIR2 + "\input" targetDir = DIR2 + "\grbm" scalevaraince = input("Enter value: ") #argmax(fmeasureV) + 12 ii = 1 for k in os.listdir(srcDir): if (ii < 500): ii = ii + 1 continue if (ii > 1749): break imf = Image.open(srcDir + '/' + k) print array(imf).shape data_test = (array(imf)).flatten() #dataset = np.vstack((dataset,d)) data_test_0 = ((data_test)[::3]).T data_test_1 = ((data_test)[1::3]).T data_test_2 = ((data_test)[2::3]).T Nsteps = 5 # evaluate 1st layer print 'evaluating ', k, "\t" # #datasub = zeros(x_*y_) #for point in xrange(Ndat): #X = asmatrix(data_test_0[:,point]).T X = asmatrix(data_test_0).T #dataout = vstack((dataout,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn[0].forward(X) # self.activ(1) X = dbn[0].backward(Y, X) #print "S hsape:", X.shape #dataout = vstack((dataout,X.flatten())) dataout = subtract(asarray(X), data_test_0, asarray(dbn[0].vsigma), scalevaraince + 1) #X = asmatrix(data_test_1[:,point]).T X = asmatrix(data_test_1).T #dataout1 = vstack((dataout1,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn1[0].forward(X) # self.activ(1) X = dbn1[0].backward(Y, X) #print "S hsape:", X.shape #dataout1 = vstack((dataout1,X.flatten())) dataout1 = subtract(asarray(X), data_test_1, asarray(dbn1[0].vsigma), scalevaraince + 1) X = asmatrix(data_test_2).T #dataout2 = vstack((dataout2,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn2[0].forward(X) # self.activ(1) X = dbn2[0].backward(Y, X) #print "S hsape:", X.shape #dataout2 = vstack((dataout2,X.flatten())) # plt.imshow(reshape(X,(x_,y_))) # plt.show() dataout2 = subtract(asarray(X), data_test_2, asarray(dbn2[0].vsigma), scalevaraince + 1) # plt.imshow((reshape(data_test[::3,5],(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.axis('off') # plt.show() # plt.figure(1) # for i in range(Ndat): # plt.subplot(5,5,i+1) d = multiply(asarray(dataout), asarray(dataout1), asarray(dataout2)) d = mod(d + 1, 2) open_img = ndimage.median_filter( asarray(reshape(d, (x_, y_)) * 255, dtype="uint8"), coff) #d = open_img.flatten() #print type(d[0]) # #plt.savefig(targetDir + "\\" + str(ii) + ".png") #print img_s = Image.fromarray(open_img) # plt.figure(2) # plt.imshow(img_s)#,cmap = cm.Greys_r, interpolation ="nearest") # plt.show() if (ii < 1000): img_s.save(targetDir + "\\bin000" + str(ii) + ".bmp") else: img_s.save(targetDir + "\\bin00" + str(ii) + ".bmp") ii = ii + 1
def main(argv): # load preprocessed data samples print 'loading data...\t', data = load('./data/vanhateren.npz') print '[DONE]' print # remove DC component (first component) data_train = data['train'][1:, :] data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=data_train.shape[0], num_hiddens=100)) # hyperparameters dbn[0].learning_rate = 5E-3 dbn[0].weight_decay = 1E-2 dbn[0].momentum = 0.9 dbn[0].sigma = 0.65 dbn[0].cd_steps = 1 dbn[0].persistent = True # train 1st layer print 'training...\t', dbn.train(data_train, num_epochs=100, batch_size=100) print '[DONE]' # evaluate 1st layer print 'evaluating...\t', logptf = dbn.estimate_log_partition_function(num_ais_samples=100, beta_weights=arange(0, 1, 1E-3)) loglik = dbn.estimate_log_likelihood(data_test) print '[DONE]' print print 'estimated log-partf.:\t', logptf print 'estimated log-loss:\t', -loglik / data_test.shape[0] / log(2) print # create 2nd layer dbn.add_layer(SemiRBM(num_visibles=100, num_hiddens=100)) # initialize parameters dbn[1].L = dbn[0].W.T * dbn[0].W dbn[1].b = dbn[0].W.T * dbn[0].b + dbn[0].c + 0.5 * asmatrix(diag(dbn[1].L)).T dbn[1].L = dbn[1].L - asmatrix(diag(diag(dbn[1].L))) # hyperparameters dbn[1].learning_rate = 5E-3 dbn[1].learning_rate_lateral = 5E-4 dbn[1].weight_decay = 5E-3 dbn[1].weight_decay_lateral = 5E-3 dbn[1].momentum = 0.9 dbn[1].momentum_lateral = 0.9 dbn[1].num_lateral_updates = 20 dbn[1].damping = 0.2 dbn[1].cd_steps = 1 dbn[1].persistent = True # train 2nd layer print 'training...\t', dbn.train(data_train, num_epochs=100, batch_size=100) print '[DONE]' # evaluate 2nd layer print 'evaluating...\t', logptf = dbn.estimate_log_partition_function(num_ais_samples=100, beta_weights=arange(0, 1, 1E-3)) loglik = dbn.estimate_log_likelihood(data_test, num_samples=100) print '[DONE]' print print 'estimated log-partf.:\t', logptf print 'estimated log-loss:\t', -loglik / data_test.shape[0] / log(2) print # fine-tune with wake-sleep dbn[0].learning_rate /= 4. dbn[1].learning_rate /= 4. print 'fine-tuning...\t', dbn.train_wake_sleep(data_train, num_epochs=10, batch_size=10) print '[DONE]' # reevaluate print 'evaluating...\t', logptf = dbn.estimate_log_partition_function(num_ais_samples=100, beta_weights=arange(0, 1, 1E-3)) loglik = dbn.estimate_log_likelihood(data_test, num_samples=100) print '[DONE]' print print 'estimated log-partf.:\t', logptf print 'estimated log-loss:\t', -loglik / data_test.shape[0] / log(2) return 0
def main(argv): # load preprocessed data samples print 'loading data...\t', #data_train, data_test = genData() #load('../data/vanhateren.npz') DIR1 = "../data/changedetection/camerajitter/traffic" data_train_0,data_train_1,data_train_2, data_test = load_data_BS(DIR1 + "/orignal_color.pkl") img = Image.open(DIR1 + "/gt001537.png") x_, y_ = (asarray(img)[:,:]).shape #determine procesing image size print "Doie : " , x_,y_ groundtruth = (((asarray(img)[:,:])/255.0 > 0.5) * 1).flatten() #groundtruth = (((asarray(img)[:,:])/255.0 > 0.5) * 1).flatten() print '[DONE]' # remove DC component (first component) # data_train = data['train'][1:, :] # data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=data_train_0.shape[0], num_hiddens=20)) dbn1 = DBN(GaussianRBM(num_visibles=data_train_1.shape[0], num_hiddens=20)) dbn2 = DBN(GaussianRBM(num_visibles=data_train_2.shape[0], num_hiddens=20)) dbn[0].learning_rate = 0.001 dbn1[0].learning_rate = 0.001 dbn2[0].learning_rate = 0.001 # train 1st layer print 'training...\t', dbn.train(data_train_0, num_epochs=1, batch_size=1,shuffle=False) dbn1.train(data_train_1, num_epochs=1, batch_size=1,shuffle=False) dbn2.train(data_train_2, num_epochs=1, batch_size=1,shuffle=False) print '[DONE]' data_test_0 = ((data_test.T)[:,::3]).T data_test_1 = ((data_test.T)[:,1::3]).T data_test_2 = ((data_test.T)[:,2::3]).T # global runtime # sFilename = "data\\weightlogs" # f = h5.File(sFileName+".hdf5") # f.create_group(runtime) # group.create_dataset('parameters',data=par) #storeWeights(category,"data\\weightlogs", dbn[0].W, dbn[0].vsigma,dbn1[0].W, dbn1[0].vsigma,dbn2[0].W, dbn2[0].vsigma ) Ndat = 20 #data_test_0.shape[1] Nsteps = 5 # evaluate 1st layer print 'evaluating 1...\t', dataout = zeros(x_*y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_0[:,point]).T X = asmatrix(data_test_0[:,-1]).T #dataout = vstack((dataout,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn[0].forward(X) # self.activ(1) X = dbn[0].backward(Y,X) #print "S hsape:", X.shape #dataout = vstack((dataout,X.flatten())) dataout = vstack((dataout,subtract(asarray(X),data_test_0[:,-1],asarray(dbn[0].vsigma),point+1))) print 'evaluating 2...\t', dataout1 = zeros(x_*y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_1[:,point]).T X = asmatrix(data_test_1[:,-1]).T #dataout1 = vstack((dataout1,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn1[0].forward(X) # self.activ(1) X = dbn1[0].backward(Y,X) #print "S hsape:", X.shape #dataout1 = vstack((dataout1,X.flatten())) dataout1 = vstack((dataout1,subtract(asarray(X),data_test_1[:,-1],asarray(dbn1[0].vsigma),point+1))) print 'evaluating 3...\t', dataout2 = zeros(x_*y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_2[:,point]).T X = asmatrix(data_test_2[:,-1]).T #dataout2 = vstack((dataout2,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn2[0].forward(X) # self.activ(1) X = dbn2[0].backward(Y,X) #print "S hsape:", X.shape #dataout2 = vstack((dataout2,X.flatten())) dataout2 = vstack((dataout2,subtract(asarray(X),data_test_2[:,-1],asarray(dbn2[0].vsigma),point+1))) # plt.imshow((reshape(data_test[::3,5],(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.axis('off') # plt.show() fmeasureV = zeros(Ndat) plt.figure(2) for i in range(Ndat): plt.subplot(5,5,i+1) d = multiply(asarray(dataout[i+1,:]),asarray(dataout1[i+1,:]),asarray(dataout2[i+1,:])) d = mod(d+1,2) fmeasureV[i] = f_measure(d,groundtruth) print "Image Example Fmeaure: ",i," : ", fmeasureV[i] * 100 # d[0::3] = asarray(dataout[i+1,:]) # d[1::3] = asarray(dataout1[i+1,:]) # d[2::3] = asarray(dataout2[i+1,:]) # d[:,:,0] = (reshape(asarray(dataout[i+1,:]),(x_,y_))) # d[:,:,1] = (reshape(asarray(dataout1[i+1,:]),(x_,y_))) # d[:,:,2] = (reshape(asarray(dataout2[i+1,:]),(x_,y_))) # img_s = Image.fromarray(asarray(reshape(d,(x_,y_))*255,dtype="uint8")) # img_s.save("C:\work\\backgdSubt\dataset\datasets\change detection\\baseline\\baseline\office\grbm" + "\\" + str(i) + ".bmp") plt.imshow(reshape(d,(x_,y_)), cmap = cm.Greys_r, interpolation ="nearest") plt.axis('off') #plt.figure(2) plt.show() DIR2 = "../data/changedetection/camerajitter/traffic" srcDir = DIR2 + "/input" targetDir = DIR2 + "/grbm" scalevaraince = input("Enter value: ") #argmax(fmeasureV) + 12 ii = 1 for k in os.listdir(srcDir): imf = Image.open(srcDir + '/' + k) print array(imf).shape data_test = (array(imf)).flatten() #dataset = np.vstack((dataset,d)) data_test_0 = ((data_test)[::3]).T data_test_1 = ((data_test)[1::3]).T data_test_2 = ((data_test)[2::3]).T Nsteps = 5 # evaluate 1st layer print 'evaluating 1...\t', # #datasub = zeros(x_*y_) #for point in xrange(Ndat): #X = asmatrix(data_test_0[:,point]).T X = asmatrix(data_test_0).T #dataout = vstack((dataout,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn[0].forward(X) # self.activ(1) X = dbn[0].backward(Y,X) #print "S hsape:", X.shape #dataout = vstack((dataout,X.flatten())) dataout = subtract(asarray(X),data_test_0,asarray(dbn[0].vsigma),scalevaraince + 1 ) #X = asmatrix(data_test_1[:,point]).T X = asmatrix(data_test_1).T #dataout1 = vstack((dataout1,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn1[0].forward(X) # self.activ(1) X = dbn1[0].backward(Y,X) #print "S hsape:", X.shape #dataout1 = vstack((dataout1,X.flatten())) dataout1 = subtract(asarray(X),data_test_1,asarray(dbn1[0].vsigma),scalevaraince + 1) X = asmatrix(data_test_2).T #dataout2 = vstack((dataout2,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn2[0].forward(X) # self.activ(1) X = dbn2[0].backward(Y,X) #print "S hsape:", X.shape #dataout2 = vstack((dataout2,X.flatten())) # plt.imshow(reshape(X,(x_,y_))) # plt.show() dataout2 = subtract(asarray(X),data_test_2,asarray(dbn2[0].vsigma),scalevaraince + 1) # plt.imshow((reshape(data_test[::3,5],(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.axis('off') # plt.show() # plt.figure(1) # for i in range(Ndat): # plt.subplot(5,5,i+1) d = multiply(asarray(dataout),asarray(dataout1),asarray(dataout2)) d = mod(d+1,2) #print type(d[0]) # #plt.savefig(targetDir + "\\" + str(ii) + ".png") #print img_s = Image.fromarray(asarray(reshape(d,(x_,y_))*255,dtype="uint8")) # plt.figure(2) # plt.imshow(img_s)#,cmap = cm.Greys_r, interpolation ="nearest") # plt.show() if (ii <1000): img_s.save(targetDir + "\\bin000" + str(ii) + ".bmp") else: img_s.save(targetDir + "\\bin00" + str(ii) + ".bmp") ii = ii +1
def main(argv): # load preprocessed data samples print 'loading data...\t', #data_train, data_test = genData() #load('../data/vanhateren.npz') data_train_0, data_train_1, data_train_2, data_test = load_data_BS( "./data/gf_dborignal_color.pkl") img = Image.open("./data/gf_db/mensa_seq0_1.1/seq0_0000_0.jpg") print "Doie : ", (asarray(img)[:, :]).shape groundtruth = (((asarray(img)[:, :]) / 255.0 > 0.5) * 1).flatten() #groundtruth = (((asarray(img)[:,:])/255.0 > 0.5) * 1).flatten() print '[DONE]' # remove DC component (first component) # data_train = data['train'][1:, :] # data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=data_train_0.shape[0], num_hiddens=8)) dbn1 = DBN(GaussianRBM(num_visibles=data_train_1.shape[0], num_hiddens=8)) dbn2 = DBN(GaussianRBM(num_visibles=data_train_2.shape[0], num_hiddens=8)) # train 1st layer print 'training...\t', dbn.train(data_train_0, num_epochs=100, batch_size=1, shuffle=False) dbn1.train(data_train_1, num_epochs=100, batch_size=1, shuffle=False) dbn2.train(data_train_2, num_epochs=100, batch_size=1, shuffle=False) print '[DONE]' data_test_0 = ((data_test.T)[:, ::3]).T data_test_1 = ((data_test.T)[:, 1::3]).T data_test_2 = ((data_test.T)[:, 2::3]).T Ndat = 25 #data_test_0.shape[1] Nsteps = 5 # evaluate 1st layer print 'evaluating 1...\t', dataout = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_0[:,point]).T X = asmatrix(data_test_0[:, -1]).T #dataout = vstack((dataout,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn[0].forward(X) # self.activ(1) X = dbn[0].backward(Y, X) #print "S hsape:", X.shape #dataout = vstack((dataout,X.flatten())) dataout = vstack((dataout, subtract(asarray(X), data_test_0[:, -1], asarray(dbn[0].vsigma), point + 1))) print 'evaluating 2...\t', dataout1 = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_1[:,point]).T X = asmatrix(data_test_1[:, -1]).T #dataout1 = vstack((dataout1,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn1[0].forward(X) # self.activ(1) X = dbn1[0].backward(Y, X) #print "S hsape:", X.shape #dataout1 = vstack((dataout1,X.flatten())) dataout1 = vstack((dataout1, subtract(asarray(X), data_test_1[:, -1], asarray(dbn1[0].vsigma), point + 1))) print 'evaluating 3...\t', dataout2 = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_2[:,point]).T X = asmatrix(data_test_2[:, -1]).T #dataout2 = vstack((dataout2,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn2[0].forward(X) # self.activ(1) X = dbn2[0].backward(Y, X) #print "S hsape:", X.shape #dataout2 = vstack((dataout2,X.flatten())) dataout2 = vstack((dataout2, subtract(asarray(X), data_test_2[:, -1], asarray(dbn2[0].vsigma), point + 1))) # plt.imshow((reshape(data_test[::3,5],(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.axis('off') # plt.show() plt.figure(1) for i in range(Ndat): plt.subplot(5, 5, i + 1) d = multiply(asarray(dataout[i + 1, :]), asarray(dataout1[i + 1, :]), asarray(dataout2[i + 1, :])) d = mod(d + 1, 2) print "Image Example Fmeaure: ", i, " : ", f_measure(d, groundtruth) * 100 # d[0::3] = asarray(dataout[i+1,:]) # d[1::3] = asarray(dataout1[i+1,:]) # d[2::3] = asarray(dataout2[i+1,:]) # d[:,:,0] = (reshape(asarray(dataout[i+1,:]),(x_,y_))) # d[:,:,1] = (reshape(asarray(dataout1[i+1,:]),(x_,y_))) # d[:,:,2] = (reshape(asarray(dataout2[i+1,:]),(x_,y_))) plt.imshow(reshape(d, (x_, y_)), cmap=cm.Greys_r, interpolation="nearest") plt.axis('off') plt.figure(2) for k in range(8): plt.subplot(4, 2, k + 1) d = zeros((x_ * y_ * 3)) d[0::3] = asarray(dbn[0].W[:, k].flatten()) d[1::3] = asarray(dbn1[0].W[:, k].flatten()) d[2::3] = asarray(dbn2[0].W[:, k].flatten()) plt.imshow(reshape( d, (x_, y_, 3))) #, cmap = cm.Greys_r, interpolation ="nearest") plt.axis('off') # plt.figure() # plt.imshow((reshape(dbn[0].vsigma[:19200],(x_,y_)))) # plt.figure(2) # plt.imshow((reshape(dbn[0].vsigma[19200:19200*2],(x_,y_)))) # plt.figure(3) # plt.imshow((reshape(dbn[0].vsigma[19200*2:19200*3],(x_,y_)))) plt.figure(3) print type(dbn[0].vsigma) plt.imshow(reshape(asarray(dbn[0].vsigma), (x_, y_))) plt.show() print dbn[0].vsigma p.show()
def main(argv): # load preprocessed data samples print 'loading data...\t', #data_train, data_test = genData() #load('../data/vanhateren.npz') data_train, data_test = load_data_BS("data/wavingtree/imagedata_blr_01.pkl") print '[DONE]' # remove DC component (first component) # data_train = data['train'][1:, :] # data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=data_train.shape[0], num_hiddens=8)) # hyperparameters dbn[0].learning_rate = 0.001 #dbn[0].weight_decay = 1E-2 dbn[0].momentum = 0.9 dbn[0].sigma = 0.07 dbn[0].cd_steps = 15 # dbn[0].persistent = Trueahsanrafique # train 1st layer print 'training...\t', dbn.train(data_train, num_epochs=20, batch_size=1,shuffle=False) print '[DONE]' Ndat = data_test.shape[1] Nsteps = 1 # evaluate 1st layer print 'evaluating...\t', dataout = zeros(120*160) #datasub = zeros(120*160) for point in xrange(Ndat): X = asmatrix(data_test[:,point]).T dataout = vstack((dataout,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn[0].forward(X) # self.activ(1) X = dbn[0].backward(Y,X) #print "S hsape:", X.shape dataout = vstack((dataout,X.flatten())) dataout = vstack((dataout,subtract(asarray(X),data_test[:,point],asarray(dbn[0].vsigma)))) # plt.imshow((reshape(data_test[:,point],(120,160))), cmap = cm.Greys_r, interpolation ="nearest") # plt.axis('off') # plt.figure(2) for i in range(10*3): plt.subplot(10,3,i+1) plt.imshow((reshape(dataout[i+1,:],(120,160))), cmap = cm.Greys_r, interpolation ="nearest") plt.axis('off') plt.figure(2) for k in range(8): plt.subplot(4,2,k+1) plt.imshow((reshape(dbn[0].W[:,k],(120,160))), cmap = cm.Greys_r, interpolation ="nearest") plt.axis('off') plt.figure(3) plt.imshow((reshape(dbn[0].vsigma,(120,160))), cmap = cm.Greys_r, interpolation ="nearest") plt.show() # p.figure(1) # p.plot(data_train[0,:],data_train[1,:], 'b.') # p.axis([0.0, 1.0, 0.0, 1.0]) # p.figure(2) # p.plot(data_test[0,:],data_test[1,:], 'b.') # p.axis([0.0, 1.0, 0.0, 1.0]) # p.figure(3) # p.plot(datout[0,:],datout[1,:], 'b.') # p.axis([0.0, 1.0, 0.0, 1.0]) # p.figure(4) # p.plot(datout[0,:],datout[1,:], 'b.') #p.axis([0.0, 1.0, 0.0, 1.0]) #p.figure(4) #p.hist(datout) print dbn[0].vsigma p.show()
def main(argv): # load preprocessed data samples print 'loading data...\t', #data_train, data_test = genData() #load('../data/vanhateren.npz') # data_train_0,data_train_1,data_train_2, data_test = load_data_BS("data/changedetection\intermitentmotion\streetlight/orignal_color.pkl") img = Image.open("data/changedetection\ptz\continuous/gt000962.png") print "Doie : ", (asarray(img)[:, :]).shape groundtruth = (((asarray(img)[:, :]) / 255.0 > 0.5) * 1).flatten() # #groundtruth = (((asarray(img)[:,:])/255.0 > 0.5) * 1).flatten() # print '[DONE]' batches = 3 # remove DC component (first component) # data_train = data['train'][1:, :] # data_test = data['test'][1:, :] # create 1st layer dbn = DBN(GaussianRBM(num_visibles=x_ * y_, num_hiddens=15)) dbn1 = DBN(GaussianRBM(num_visibles=x_ * y_, num_hiddens=15)) dbn2 = DBN(GaussianRBM(num_visibles=x_ * y_, num_hiddens=15)) dbn[0].learning_rate = 0.0001 dbn1[0].learning_rate = 0.0001 dbn2[0].learning_rate = 0.0001 # train 1st layer print 'training...\t', for k in range(2): print "epoc :", k for i in range(batches): global batch_no batch_no = i os.path.walk( 'C:\work\\backgdSubt\dataset\changedetection\ptz\continuous\\orignal', load_batch, 0) global train_set_x0 global train_set_x1 global train_set_x2 dbn.train(train_set_x0.T, num_epochs=1, batch_size=1, shuffle=False) dbn1.train(train_set_x1.T, num_epochs=1, batch_size=1, shuffle=False) dbn2.train(train_set_x2.T, num_epochs=1, batch_size=1, shuffle=False) print '[DONE]' os.path.walk( 'C:\work\\backgdSubt\dataset\changedetection\ptz\continuous\\test', load_test_batch, 0) data_test_0 = ((data_test.T)[:, ::3]).T data_test_1 = ((data_test.T)[:, 1::3]).T data_test_2 = ((data_test.T)[:, 2::3]).T Ndat = 25 #data_test_0.shape[1] Nsteps = 5 # evaluate 1st layer print 'evaluating 1...\t', dataout = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_0[:,point]).T X = asmatrix(data_test_0[:, -1]).T #dataout = vstack((dataout,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn[0].forward(X) # self.activ(1) X = dbn[0].backward(Y, X) #print "S hsape:", X.shape #dataout = vstack((dataout,X.flatten())) dataout = vstack((dataout, subtract(asarray(X), data_test_0[:, -1], asarray(dbn[0].vsigma), point + 1))) print 'evaluating 2...\t', dataout1 = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_1[:,point]).T X = asmatrix(data_test_1[:, -1]).T #dataout1 = vstack((dataout1,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn1[0].forward(X) # self.activ(1) X = dbn1[0].backward(Y, X) #print "S hsape:", X.shape #dataout1 = vstack((dataout1,X.flatten())) dataout1 = vstack((dataout1, subtract(asarray(X), data_test_1[:, -1], asarray(dbn1[0].vsigma), point + 1))) print 'evaluating 3...\t', dataout2 = zeros(x_ * y_) # #datasub = zeros(x_*y_) for point in xrange(Ndat): #X = asmatrix(data_test_2[:,point]).T X = asmatrix(data_test_2[:, -1]).T #dataout2 = vstack((dataout2,X.flatten())) #print "testing:", X.shape for recstep in xrange(Nsteps): Y = dbn2[0].forward(X) # self.activ(1) X = dbn2[0].backward(Y, X) #print "S hsape:", X.shape #dataout2 = vstack((dataout2,X.flatten())) dataout2 = vstack((dataout2, subtract(asarray(X), data_test_2[:, -1], asarray(dbn2[0].vsigma), point + 1))) # plt.imshow((reshape(data_test[::3,5],(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest") # plt.axis('off') # plt.show() plt.figure(1) for i in range(Ndat): plt.subplot(5, 5, i + 1) d = multiply(asarray(dataout[i + 1, :]), asarray(dataout1[i + 1, :]), asarray(dataout2[i + 1, :])) d = mod(d + 1, 2) #print "Image Example Fmeaure: ",i," : ", f_measure(d,groundtruth) * 100 # d[0::3] = asarray(dataout[i+1,:]) # d[1::3] = asarray(dataout1[i+1,:]) # d[2::3] = asarray(dataout2[i+1,:]) # d[:,:,0] = (reshape(asarray(dataout[i+1,:]),(x_,y_))) # d[:,:,1] = (reshape(asarray(dataout1[i+1,:]),(x_,y_))) # d[:,:,2] = (reshape(asarray(dataout2[i+1,:]),(x_,y_))) plt.imshow(reshape(d, (x_, y_)), cmap=cm.Greys_r, interpolation="nearest") plt.axis('off') plt.figure(2) for k in range(15): plt.subplot(5, 3, k + 1) d = zeros((x_ * y_ * 3)) d[0::3] = asarray(dbn[0].W[:, k].flatten()) d[1::3] = asarray(dbn1[0].W[:, k].flatten()) d[2::3] = asarray(dbn2[0].W[:, k].flatten()) plt.imshow(reshape(d[0::3], (x_, y_)), cmap=cm.Greys_r, interpolation="nearest") plt.axis('off') # plt.figure() # plt.imshow((reshape(dbn[0].vsigma[:19200],(x_,y_)))) # plt.figure(2) # plt.imshow((reshape(dbn[0].vsigma[19200:19200*2],(x_,y_)))) # plt.figure(3) # plt.imshow((reshape(dbn[0].vsigma[19200*2:19200*3],(x_,y_)))) plt.figure(3) print type(dbn[0].vsigma) plt.imshow(reshape(asarray(dbn[0].vsigma), (x_, y_))) plt.show() print dbn[0].vsigma p.show()