print('Starting Active Learning in Experiment ', e)


	for i in range(acquisition_iterations):
		print('POOLING ITERATION', i)

		pool_subset = 1000
		pool_subset_dropout = np.asarray(random.sample(range(0,X_Pool.shape[0]), pool_subset))
		X_Pool_Dropout = X_Pool[pool_subset_dropout, :, :, :]
		y_Pool_Dropout = y_Pool[pool_subset_dropout]		

		All_Dropout_Scores = np.zeros(shape=(X_Pool_Dropout.shape[0], nb_classes))
		print('Use trained model for test time dropout')
		for d in range(dropout_iterations):
			print ('Dropout Iteration', d)
			dropout_score = model.predict_stochastic(X_Pool_Dropout,batch_size=batch_size, verbose=1)
			#np.save('/Users/Riashat/Documents/Cambridge_THESIS/Code/Experiments/keras/active_learning/Acquisition_Functions/BCNN_Maximal_Uncertainty/GPU/Bayes_Segnet/Dropout_Scores/'+'Dropout_Score_'+str(d)+'.npy',dropout_score)
			All_Dropout_Scores = np.append(All_Dropout_Scores, dropout_score, axis=1)


		All_Std = np.zeros(shape=(X_Pool_Dropout.shape[0],nb_classes))
		BayesSegnet_Sigma = np.zeros(shape=(X_Pool_Dropout.shape[0],1))	
		
		for t in range(X_Pool_Dropout.shape[0]):
			for r in range(nb_classes):
				L = np.array([0])
				for d_iter in range(dropout_iterations):
					L = np.append(L, All_Dropout_Scores[t, r+10])

				L_std = np.std(L[1:])
				All_Std[t,r] = L_std
	rmse = np.sqrt(np.mean((y_test - y_predicted)**2))

	all_rmse = rmse

	print('Starting Active Learning Experiments')

	for i in range(acquisition_iterations):

		print('Acquisition Iteration', i)

		All_Dropout_Scores = np.zeros(shape=(X_pool.shape[0], 1))

		print('Dropout to compute variance estimates on Pool Set')
		for d in range(dropout_iterations):
			dropout_score = model.predict_stochastic(X_pool,batch_size=batch_size, verbose=1)
			All_Dropout_Scores = np.append(All_Dropout_Scores, dropout_score, axis=1)


		Variance = np.zeros(shape=(All_Dropout_Scores.shape[0]))
		Mean = np.zeros(shape=(All_Dropout_Scores.shape[0]))
		for j in range(All_Dropout_Scores.shape[0]):
			L = All_Dropout_Scores[j, :]
			L_var = np.var(L)
			L_mean = np.mean(L)
			Variance[j] = L_var
			Mean[j] = L_mean


		#select next x with highest predictive variance
		v_sort = Variance.flatten()
input_img_data = [X_train[0,:,:,:]]
sp.misc.imsave('test.jpg',input_img_data)



input_img = model.get_input() 
score = model.evaluate(X_test, Y_test, batch_size=batch_size)
grads = K.gradients(loss,input_img)
iterate = K.function([input_img], [loss, grads])


print("YODA_1")
step = 0.01
for i in range(10):
   loss_value, grads_value = iterate([input_img_data])
   input_img_data += grads_value*step
score = model.predict_stochastic(input_img_data,batch_size=batch_size)
print(score)
print("YODA")
json_string = model.to_json()
open('model_200_arch.json', 'w').write(json_string)
model.save_weights('model_200_weights.h5')


for i in range(1):
   score = model.predict_stochastic(X_test,batch_size=batch_size)
   np.save('/home/ar773/CNN/keras/examples/scores/score'+str(i)+'.npy',score)
print('Test score:', score)
loop the predict 10 times, average over the trials and take a argmax for correct label
#     verbose=1, append_epoch_name=True, save_every_X_epochs=10)
modeltest_1 = ModelTest(X_train[:100], mean_y_train + std_y_train * np.atleast_2d(Y_train[:100]).T, 
                      test_every_X_epochs=1, verbose=0, loss='euclidean',
                      mean_y_train=mean_y_train, std_y_train=std_y_train, tau=0.1)
modeltest_2 = ModelTest(X_test, np.atleast_2d(Y_test).T, test_every_X_epochs=1, verbose=0, loss='euclidean',
                      mean_y_train=mean_y_train, std_y_train=std_y_train, tau=0.1)
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=1250, 
          callbacks=[modeltest_1, modeltest_2]) #checkpointer, 
# score, acc = model.evaluate(X_test, y_test, batch_size=batch_size, show_accuracy=True)
# print('Test score:', score)
# print('Test accuracy:', acc)


# In[ ]:

standard_prob = model.predict(X_train, batch_size=500, verbose=1)
print(np.mean(((mean_y_train + std_y_train * np.atleast_2d(Y_train).T) 
               - (mean_y_train + std_y_train * standard_prob))**2, 0)**0.5)


# In[ ]:

standard_prob = model.predict(X_test, batch_size=500, verbose=1)
#print(standard_prob)
T = 50
prob = np.array([model.predict_stochastic(X_test, batch_size=500, verbose=0) 
                 for _ in xrange(T)])
prob_mean = np.mean(prob, 0)
print(np.mean((np.atleast_2d(Y_test).T - (mean_y_train + std_y_train * standard_prob))**2, 0)**0.5)
print(np.mean((np.atleast_2d(Y_test).T - (mean_y_train + std_y_train * prob_mean))**2, 0)**0.5)
                        border_mode='valid',
                        input_shape=(1, img_rows, img_cols)))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam')

# model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, Y_test))

model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1)


score= model.predict(X_test,batch_size=batch_size, verbose=1)
score2= model.predict(X_test,batch_size=batch_size, verbose=1)

print('USING PREDICT_STOCHASTIC FUNCTION')

score3 = model.predict_stochastic(X_test,batch_size=batch_size, verbose=1)
score4 = model.predict_stochastic(X_test,batch_size=batch_size, verbose=1)