示例#1
0
def main():
	''' pipeline for testing and evaluation '''
	# preset parameters
	save_path = '../data_viper/model_feat/'

	# load data
	# imL = 48
	bs = 100
	datafile_viper = '../data_viper/viper.pkl'
	viper = loadfile(datafile_viper)

	# load model
	modelfile_viper = '../data_viper/model_feat/model.pkl'
	model = loadfile(modelfile_viper)

	# evaluation and testing
	# test_x = viper.test_x.get_value(borrow=True)
	test_x = np.asarray(viper.test_feat)
	test_y = viper.test_y
	n_test = test_x.shape[0]
	test_ypred = model.predict(viper.test_feat)
	test_ypred = np.asarray(test_ypred).flatten()

	# test_ims = test_x.reshape((n_test, imL, imL, -1))

	# assign predicted scores to images
	h, w = viper.imgs[0].shape[:2]
	mh, mw = len(np.unique(viper.yy)), len(np.unique(viper.xx))
	msk0 = np.zeros(mh*mw).astype(np.uint8)
	msks = [msk0.copy() for im in viper.imgs]

	showlist = []
	for i in range(n_test):
		imgid = viper.test_imgids[i]
		patid = viper.test_ctrids[i]
		score = test_ypred[i]
		msks[imgid][patid] = score*255

	# resize predicted salience map to match image size
	msks_rs = [imresize(msk.reshape((mw, mh)).T, size=(h, w))/255. for msk in msks]

	# save salience map for comparison
	test_imids = np.asarray(np.unique(viper.test_imgids))
	salmap_gt = np.asarray(viper.salmsks) #np.asarray([viper.salmsks[imid] for imid in test_imids])
	salmap_pred = np.asarray(msks_rs) #np.asarray([msks_rs[imid]/255. for imid in test_imids])
	savefile(save_path+'salmaps_comparison.pkl', [test_imids, salmap_gt, salmap_pred])

	# quantize to show different test patches
	# kmeans = KMeans(init='k-means++', n_clusters=10, n_init=10)
	# kmeans.fit(test_ypred.reshape(n_test, 1))

	# # save to result folder
	# for i in range(10):
	# 	idx = kmeans.labels_== i
	# 	if any(idx): 
	# 		im = immontage(list(test_ims[idx])) 
	# 		imsave(save_path+'{}.jpg'.format(kmeans.cluster_centers_[i]), im)

	print 'testing finished'
示例#2
0
def main():
	''' pipeline for supervised salience training '''

	if os.path.isdir('../data_viper/'):
		datapath = '../data_viper/'
	else:
		datapath = '../data/'

	save_path = '../data_viper/model_feat/'

	DATA_OPT = 'feat' 		# feature type
	TRAIN_OPT = 'SVR'				# training model option
	TRAIN = True 					# wheather re-train the model

	# prepare training data for supervised salience training 
	#=======================================================
	datafile_viper = datapath + 'viper.pkl'
	if not os.path.isfile(datafile_viper):
		viper = DataMan_viper_small()
		viper.make_data()
		savefile(datafile_viper, viper)
	else:
		viper = loadfile(datafile_viper)
	
	viper = preprocess_data(viper, DATA_OPT)

	# training 
	# ==============
	modelfile = datapath + 'model_feat/model.pkl'

	if TRAIN:
		tic = time.clock()
		model = train_model(viper, TRAIN_OPT)
		toc = time.clock()
		print 'Elapsed training time: {0:.2f} min'.format((toc-tic)/60.)

		savefile(modelfile, model)
		os.system('ls -lh ' + modelfile)		
	else:
		model = loadfile(modelfile)
	
	## validation
	#=========================================
	print 'validating trained model'
	nValid = 5000
	valididx = np.random.permutation(viper.valid_feat.shape[0])[:nValid]
	# valid_ypred = model.predict(viper.valid_feat[valididx])
	valid_ypred = predict(model, viper.valid_feat[valididx], viper.yy[viper.valid_ctrids][valididx], viper.imH)

	#- quantize patches based on testing scores
	kmeans = KMeans(init='k-means++', n_clusters=10, n_init=10, verbose=1)
	kmeans.fit(valid_ypred.reshape(nValid, 1))

	#- crop patches for testing image
	valid_patset = np.asarray(viper.get_patchset('valid'))[valididx]

	#- save to result folder
	os.system('rm '+save_path+'*.jpg')
	for i in range(10):
		idx = kmeans.labels_== i
		if any(idx): 
			pats = immontage(list(valid_patset[idx])) 
			imsave(save_path+'{}.jpg'.format(kmeans.cluster_centers_[i]), pats)
			print 'patchset {} saved'.format(i)

	### testing 
	#===============
	print 'testing'
	# test_ypred = model.predict(viper.test_feat)
	test_ypred = predict(model, viper.test_feat, viper.yy_test[viper.test_ctrids], viper.imH)
	
	## assign predicted scores to images
	h, w = viper.imgs[0].shape[:2]
	mh, mw = len(np.unique(viper.yy_test)), len(np.unique(viper.xx_test))
	msk0 = np.zeros(mh*mw, dtype=np.float32)
	msks = [msk0.copy() for im in viper.imgs]

	showlist = []
	n_test = len(test_ypred)
	for i in range(n_test):
		imgid = viper.test_imgids[i]
		patid = viper.test_ctrids[i]
		score = test_ypred[i]
		msks[imgid][patid] = score

	# resize predicted salience map to match image size
	msks_rs = [mapresize(msk.reshape((mw, mh)).T, size=(h, w)) for msk in msks]
	# msks_rs = msks

	# save salience map for comparison
	test_imids = np.asarray(np.unique(viper.test_imgids))
	salmap_gt = np.asarray(viper.salmsks) #np.asarray([viper.salmsks[imid] for imid in test_imids])
	salmap_pred = np.asarray(msks_rs) #np.asarray([msks_rs[imid]/255. for imid in test_imids])
	savefile(save_path+'salmaps_comparison.pkl', [test_imids, salmap_gt, salmap_pred])
示例#3
0
def main():
	''' pipeline for supervised salience training '''

	if os.path.isdir('../data_viper/'):
		datapath = '../data_viper/'
	else:
		datapath = '../data/'

	# prepare training data for supervised salience training 
	datafile_viper = datapath + 'viper.pkl'
	if not os.path.isfile(datafile_viper):
		viper = DataMan_viper_small()
		viper.make_data()
		savefile(datafile_viper, viper)

	viper = loadfile(datafile_viper)
	viper = change_label(viper)
	viper.train_feat = viper.get_pixeldata('train')
	viper.valid_feat = viper.get_pixeldata('valid')
	viper.test_feat = viper.get_pixeldata('test')

	bs = 100
	imL = 10
	nfilter1 = 16
	filterL = 3

	x = T.tensor4(name='x', dtype=theano.config.floatX)
	y = T.ivector(name='y')

	# layer0 = x.reshape((bs, 3, imL, imL))
	conv1 = ConvLayer(input=x, image_shape=(bs, 3, imL, imL), 
				filter_shape=(nfilter1, 3, filterL, filterL),  
				flatten=True,
				actfun=tanh,
				tag='_convpool1')

	# outL = np.floor((imL-filterL+1.)/recfield).astype(np.int)
	outL = imL-filterL+1

	# nfilter3 = 16
	# filterL3 = 3
	# conv3 = ConvLayer(input=conv2.output(), image_shape=(bs, nfilter2, outL2, outL2), 
	# 			filter_shape=(nfilter3, nfilter2, filterL3, filterL3), 
	# 			flatten=True,
	# 			actfun=tanh,
	# 			tag='_conv3')
	#
	# outL3 = outL2-filterL3+1

	fc2 = FCLayer(input=conv1.output(), n_in=nfilter1*outL*outL, n_out=256, actfun=tanh, tag='_fc2')
	fc3 = FCLayer(input=fc2.output(), n_in=256, n_out=10, actfun=sigmoid, tag='_fc3')
	params_cmb = conv1.params + fc2.params + fc3.params 

	# ypred = fc3.output().flatten()
	ypred = fc3.output()

	model = GeneralModel(input=x, data=viper, output=ypred,
				target=y, params=params_cmb,
				regularizers=0, 
				cost_func=negative_log_likelihood,
				error_func=sqr_error, 
				batch_size=bs)

	sgd = sgd_optimizer(data=viper,  
					model=model,
					batch_size=bs, 
					learning_rate=0.001,
					n_epochs=500)

	sgd.fit_viper()

	filepath = datapath + 'model/model.pkl'
	savefile(filepath, model)
	os.system('ls -lh ' + filepath)