Пример #1
0
def test_flownet():
	sum_error = 0
	sum_px_error = 0
	result = open('result.txt', 'wb')
	prediction_file = 'flownets-pred-0000000.flo'
	img1_list = open('img1_list_test.txt', 'r').readlines()
	img2_list = open('img2_list_test.txt', 'r').readlines()
	flow_list = open('flo_list_test.txt', 'r').readlines()
	length = len(img1_list)
	
	for i in range(length):
		img_files = []
		img_files.append(img1_list[i].strip())
		img_files.append(img2_list[i].strip())
		# sanity check
		if os.path.exists(prediction_file):
			os.remove(prediction_file)
		FlowNet.run(this_dir, img_files, './model_simple')
		epe = fl.evaluate_flow_file(flow_list[i].strip(), prediction_file)
		flow = fl.read_flow(prediction_file)
		[height, width, channels] = flow.shape
		sum_error += epe
		sum_px_error += epe / (height * width)
		result.write(str.format("%4d" % i) + ': ' + str(epe) + '\n')


	print 'Average Image EPE error: ', sum_error/length
	print 'Average Pixel EPE error: ', sum_px_error/length
	result.write('Average Image EPE error: ' + str(sum_error / length))
	result.write('\n')
	result.write('Average Pixel EPE error: ' + str(sum_px_error / length))
	result.close()
Пример #2
0
def test_rain():
	rain_image_path = 'haze_rain'
	prediction_file = 'flownets-pred-0000000.flo'
	left_name_base = 'haze_rain_light/render_haze_left_beta'
	right_name_base = 'haze_rain_light/render_haze_right_beta'
	flow_file = 'haze_rain_light/flow_left.flo'
	result = open('result.txt', 'wb')
	sum_error = 0
	for beta in range(0, 200, 5):
		for contrast in range(120, 201, 5):
			img_files = []
			left_name =  left_name_base + str(beta) + 'contrast' + str(contrast) + '.png'
			right_name = right_name_base + str(beta) + 'contrast' + str(contrast) + '.png'
			img_files.append(right_name)
			img_files.append(left_name)

			# sanity check
			if os.path.exists(prediction_file):
				os.remove(prediction_file)

			FlowNet.run(this_dir, img_files, './model_simple')
			epe = fl.evaluate_flow_file(flow_file, prediction_file)
			flow = fl.read_flow(prediction_file)
			flowpic = fl.flow_to_image(flow)
			flow_image = Image.fromarray(flowpic)
			flow_image.save('beta' + str(beta)+ 'contrast' + str(contrast) + 'flow.png')
			
			sum_error += epe

			result.write('beta: ' + str(beta) + ' contrast: ' + str(contrast) + ' epe: ' + str(epe) + '\n')


	print 'sum of average end point error: ', sum_error
	result.close()
Пример #3
0
def test_flownet():
    sum_error = 0
    sum_px_error = 0
    result = open('result.txt', 'wb')
    prediction_file = 'flownets-pred-0000000.flo'
    img1_list = open('img1_list_test.txt', 'r').readlines()
    img2_list = open('img2_list_test.txt', 'r').readlines()
    flow_list = open('flo_list_test.txt', 'r').readlines()
    length = len(img1_list)

    for i in range(length):
        img_files = []
        img_files.append(img1_list[i].strip())
        img_files.append(img2_list[i].strip())
        # sanity check
        if os.path.exists(prediction_file):
            os.remove(prediction_file)
        FlowNet.run(this_dir, img_files, './model_simple')
        epe = fl.evaluate_flow_file(flow_list[i].strip(), prediction_file)
        flow = fl.read_flow(prediction_file)
        [height, width, channels] = flow.shape
        sum_error += epe
        sum_px_error += epe / (height * width)
        result.write(str.format("%4d" % i) + ': ' + str(epe) + '\n')

    print 'Average Image EPE error: ', sum_error / length
    print 'Average Pixel EPE error: ', sum_px_error / length
    result.write('\n')
    result.write('Average Image EPE error: ' + str(sum_error / length))
    result.write('Average Pixel EPE error: ' + str(sum_px_error / length))
    result.close()
Пример #4
0
def test_kitti():

    kitti_image = '../../../../../../../media/data/LRT_Flow/KITTI/data_scene_flow/training/image_2'
    kitti_flow = 'noc'
    image_list = os.listdir(kitti_image)
    flow_list = os.listdir(kitti_flow)

    prediction_file = 'flownets-pred-0000000.flo'
    result = open('result.txt', 'wb')
    sum_error = 0

    # retrieve image and flow information
    image_list.sort()
    flow_list.sort()

    for i in range(0, 308, 2):
        # input images and ground truth flow
        img_files = []
        img_files.append(os.path.join(kitti_image, image_list[i]))
        img_files.append(os.path.join(kitti_image, image_list[i + 1]))
        ground_truth_file = flow_list[i / 2]

        # sanity check
        if os.path.exists(prediction_file):
            os.remove(prediction_file)

        #invoke FlowNet
        FlowNet.run(this_dir, img_files, './model_simple')
        print i
        print img_files[0]
        print img_files[1]
        print ground_truth_file

        #evaluate result
        epe = fl.evaluate_flow_file('noc/' + ground_truth_file,
                                    prediction_file)
        sum_error += epe

        # write to result file
        result.write(
            str(i) + ':\n' + img_files[0] + '\n' + img_files[1] + '\n' +
            ground_truth_file + '\n')
        result.write('Average end point error: ' + str(epe) + '\n')

    result.write('Total average point error: ' + str(sum_error) + '\n')
    print 'sum of average end point error: ', sum_error
    result.close()
Пример #5
0
def test_middlebury():
    # setting up
    middlebury_image_1 = 'data/test/t0'
    middlebury_image_2 = 'data/test/t1'
    middlebury_flow = 'data/test/flow'
    img1_name = '00000.png'
    img2_name = '00000.png'
    flow_name = '00000.flo'
    prediction_file = 'flownets-pred-0000000.flo'
    result_file = 'result.txt'
    result = open(result_file, 'wb')
    sum_error = 0

    # Retrieve image and flow information
    folder_names = os.listdir(data / test)

    for folder in folder_names:
        # input images and ground truth flow
        img_files = []
        img_files.append(os.path.join(middlebury_image_1, folder, img1_name))
        img_files.append(os.path.join(middlebury_image_2, folder, img2_name))
        ground_truth_file = os.path.join(middlebury_flow, folder, flow_name)

        # sanity check
        if os.path.exists(prediction_file):
            os.remove(prediction_file)

        # invoke FlowNet
        FlowNet.run(this_dir, img_files, './model_corr')

        # evaluate result
        epe = fl.evaluate_flow_file(ground_truth_file, prediction_file)
        sum_error += epe
        print folder, " average end point error is:", epe

        # write to result file
        result.write(folder + ':\n' + img_files[0] + '\n' + img_files[1] +
                     '\n' + ground_truth_file + '\n')
        result.write('Average end point error: ' + str(epe) + '\n')

    result.write('Total average point error: ' + str(sum_error) + '\n')
    print 'sum of average end point error: ', sum_error
    result.close()
Пример #6
0
def test_middlebury():
    # setting up
    middlebury_image = 'data/other-data'
    middlebury_flow = 'data/other-gt-flow'
    img1_name = 'frame10_rain.png'
    img2_name = 'frame11_rain.png'
    flow_name = 'flow10.flo'
    prediction_file = 'flownets-pred-0000000.flo'
    result_file = 'result.txt'
    result = open(result_file , 'wb')
    sum_error = 0

    # Retrieve image and flow information
    folder_names = os.listdir(middlebury_image)

    for folder in folder_names:
        # input images and ground truth flow
        img_files = []
        img_files.append(os.path.join(middlebury_image, folder, img1_name))
        img_files.append(os.path.join(middlebury_image, folder, img2_name))
        ground_truth_file = os.path.join(middlebury_flow, folder, flow_name)

        # sanity check
        if os.path.exists(prediction_file):
            os.remove(prediction_file)

        # invoke FlowNet
        FlowNet.run(this_dir, img_files, './model_simple')

        # evaluate result
        epe = fl.evaluate_flow_file(ground_truth_file, prediction_file)
        sum_error += epe
        print folder, " average end point error is:", epe

        # write to result file
        result.write(folder + ':\n' + img_files[0] +'\n' + img_files[1] + '\n' + ground_truth_file + '\n')
        result.write('Average end point error: ' + str(epe) + '\n')

    result.write('Total average point error: ' + str(sum_error) + '\n')
    print 'sum of average end point error: ', sum_error
    result.close()
Пример #7
0
def test_kitti():

    sum_error = 0
    sum_px_error = 0
    result = open('result.txt', 'wb')
    prediction_file = 'flownets-pred-0000000.flo'
    img1_list = open('img1_kitti_test.txt', 'r').readlines()
    img2_list = open('img2_kitti_test.txt', 'r').readlines()
    flow_list = open('flo_kitti_test.txt', 'r').readlines()
    length = len(img1_list)

    for i in range(length):
        # input images and ground truth flow
        img_files = []
        img_files.append(img1_list[i].strip())
        img_files.append(img2_list[i].strip())

        # sanity check
        if os.path.exists(prediction_file):
            os.remove(prediction_file)

        #invoke FlowNet
        FlowNet.run(this_dir, img_files, './model_simple')

        #evaluate result
        flow = fl.read_flow(prediction_file)
        gt = kittitool.flow_read(flow_list[i].strip())
        epe = fl.evaluate_flow(gt, flow)
        sum_error += epe

        # write to result file
        result.write(
            str(i) + ':\n' + img_files[0] + '\n' + img_files[1] + '\n' +
            flow_list[i].strip() + '\n')
        result.write('Average end point error: ' + str(epe) + '\n')

    result.write('Total average point error: ' + str(sum_error) + '\n')
    print 'sum of average end point error: ', sum_error
    result.close()
Пример #8
0
def test_kitti():
	
	sum_error = 0
	sum_px_error = 0
	result = open('result.txt', 'wb')
	prediction_file = 'flownets-pred-0000000.flo'
	img1_list = open('img1_list_test.txt', 'r').readlines()
	img2_list = open('img2_list_test.txt', 'r').readlines()
	flow_list = open('flo_list_test.txt', 'r').readlines()
	length = len(img1_list)

	for i in range(length):
		# input images and ground truth flow
		img_files = []
		img_files.append(img1_list[i].strip())
		img_files.append(img2_list[i].strip())

		# sanity check
		if os.path.exists(prediction_file):
			os.remove(prediction_file)

		#invoke FlowNet
		FlowNet.run(this_dir, img_files, './model_simple')

		#evaluate result
		flow = fl.read_flow(prediction_file)
		gt = kittitool.flow_read(flow_list[i].strip())
		epe = fl.evaluate_flow(gt, flow)
		sum_error += epe

		# write to result file
		result.write(str(i) + ':\n' + img_files[0] +'\n' + img_files[1] + '\n' + flow_list[i].strip() + '\n')
		result.write('Average end point error: ' + str(epe) + '\n')


	result.write('Total average point error: ' + str(sum_error) + '\n')
	print 'sum of average end point error: ', sum_error
	result.close()
Пример #9
0
def test_rain():
    rain_image_path = 'haze_rain'
    prediction_file = 'flownets-pred-0000000.flo'
    left_name_base = 'haze_rain_light/render_haze_left_beta'
    right_name_base = 'haze_rain_light/render_haze_right_beta'
    flow_file = 'haze_rain_light/flow_left.flo'
    result = open('result.txt', 'wb')
    sum_error = 0
    for beta in range(0, 200, 5):
        for contrast in range(120, 201, 5):
            img_files = []
            left_name = left_name_base + str(beta) + 'contrast' + str(
                contrast) + '.png'
            right_name = right_name_base + str(beta) + 'contrast' + str(
                contrast) + '.png'
            img_files.append(right_name)
            img_files.append(left_name)

            # sanity check
            if os.path.exists(prediction_file):
                os.remove(prediction_file)

            FlowNet.run(this_dir, img_files, './model_simple')
            epe = fl.evaluate_flow_file(flow_file, prediction_file)
            flow = fl.read_flow(prediction_file)
            flowpic = fl.flow_to_image(flow)
            flow_image = Image.fromarray(flowpic)
            flow_image.save('beta' + str(beta) + 'contrast' + str(contrast) +
                            'flow.png')

            sum_error += epe

            result.write('beta: ' + str(beta) + ' contrast: ' + str(contrast) +
                         ' epe: ' + str(epe) + '\n')

    print 'sum of average end point error: ', sum_error
    result.close()
Пример #10
0
#!/usr/bin/python

import os, sys
from scripts.flownet import FlowNet

my_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(my_dir + '/..')

if len(sys.argv) -1 != 3:
    print("Use this tool to test FlowNet on images\n"
          "Usage for single image pair:\n"
          "    ./demo_flownet.py {S|C} IMAGE1 IMAGE2\n"
          "\n"
          "Usage for a pair of image lists (must end with .txt):\n"
          "    ./demo_flownet.py {S|C} LIST1.TXT LIST2.TXT\n")
    sys.exit(1)

model_folder = ''
if sys.argv[1].upper() == 'S':
    model_folder = './model_simple'
elif sys.argv[1].upper() == 'C':
    model_folder = './model_corr'
else:
    print("Please specify S for FlowNetSimple or C for FlowNetCorr\n")
    sys.exit(1)

img_files = sys.argv[2:]

FlowNet.run(my_dir, img_files, model_folder)
Пример #11
0
#!/usr/bin/python

import os, sys, shutil
from scripts.flownet import FlowNet

# get root directory
my_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(my_dir + '/..')

# configuration
model_folder = './model_simple'
img_files = sorted(os.listdir('flownet/data/shaman_3/'))

# Running flow and store them up
for i in range(0, len(img_files)-1):
    filename_1 = os.path.join('data/shaman_3/', img_files[i])
    filename_2 = os.path.join('data/shaman_3/', img_files[i+1])
    FlowNet.run(my_dir, [filename_1, filename_2], model_folder)
    src = os.path.join(my_dir, 'flownets-pred-0000000.flo')
    dst = os.path.join(my_dir, 'out/', filename_1+'.flo')
    shutil.move(src,dst)



    if ret is True:
        indFrame = indFrame + 1

        if ((indFrame - 1) % step) == 0 and indFlowMap < numFlowMap:

            imgDisplay = np.hstack((prvs, next))

            # save the frames into png files for FlowNet to read
            # TODO: stupid but is the easiest way without reconfigure
            # the FlowNet and possible re-train the model
            cv2.imwrite('data/frame1.png', prvs)
            cv2.imwrite('data/frame2.png', next)

            # compute the optical flow from two adjacent frames
            # the FlowNet will save a .flo file (input is a frame?)
            FlowNet.run(prvs)

            # read the .flo file
            nameFlow = 'flownetc-pred-0000000.flo'
            # temporary output image in the Middlebury color style
            nameOutTemp = 'outTemp.ppm'

            # convert the flow file to the color image file
            ColorFlow.flow2color(nameOutTemp, nameFlow)

            # read the temporary output image (.ppm)
            img = cv2.imread(nameOutTemp)

            imgDisplay = np.hstack((imgDisplay, img))
            out.write(img)
Пример #13
0
#!/usr/bin/python

import os, sys
from scripts.flownet import FlowNet

my_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(my_dir + '/..')

if len(sys.argv) - 1 <= 1:
    print("Use this tool to train FlowNet using the train.prototxt\n"
          "Usage for FlowNetS or FlowNetC:\n"
          "    ./train_flownet.py {S|C}\n")
    sys.exit(1)

model_folder = ''
if sys.argv[1].upper() == 'S':
    model_folder = './model_simple'
elif sys.argv[1].upper() == 'C':
    model_folder = './model_corr'
else:
    print("Please specify S for FlowNetSimple or C for FlowNetCorr\n")
    sys.exit(1)

FlowNet.train(my_dir, model_folder, sys.argv[2:])
Пример #14
0
#!/usr/bin/python

import os, sys
from scripts.flownet import FlowNet

my_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(my_dir + '/..')

if len(sys.argv) - 1 != 3:
    print("Use this tool to test FlowNet on images\n"
          "Usage for single image pair:\n"
          "    ./demo_flownet.py {S|C} IMAGE1 IMAGE2\n"
          "\n"
          "Usage for a pair of image lists (must end with .txt):\n"
          "    ./demo_flownet.py {S|C} LIST1.TXT LIST2.TXT\n")
    sys.exit(1)

model_folder = ''
if sys.argv[1].upper() == 'S':
    model_folder = './model_simple'
elif sys.argv[1].upper() == 'C':
    model_folder = './model_corr'
else:
    print("Please specify S for FlowNetSimple or C for FlowNetCorr\n")
    sys.exit(1)

img_files = sys.argv[2:]

FlowNet.run(my_dir, img_files, model_folder)
                if ret is True:
                    indFrame = indFrame + 1

                    if ((indFrame - 1) % step) == 0 and indFlowMap < numFlowMap:

                        imgDisplay = np.hstack((prvs, next))

                        # save the frames into png files for FlowNet to read
                        # TODO: stupid but is the easiest way without reconfigure
                        # the FlowNet and possible re-train the model
                        cv2.imwrite('data/frame1.png', prvs)
                        cv2.imwrite('data/frame2.png', next)

                        # compute the optical flow from two adjacent frames
                        FlowNet.run(prvs)  # the FlowNet will save a .flo file

                        # read the .flo file
                        fileName = 'flownetc-pred-0000000.flo'
                        flowMapSize = np.fromfile(fileName, np.float32, count=1)
                        if flowMapSize != 202021.25:
                            print 'Dimension incorrect. Invalid .flo file'
                        else:
                            data = np.fromfile(fileName, np.float32,
                                               count=2 * width * height)

                        flow = np.resize(data, (height, width, 2))

                        # prunt the flow value if it's weirdly large
                        for index, x in np.ndenumerate(flow):
                            if x > 500:
Пример #16
0
                if ret is True:
                    indFrame = indFrame + 1

                    if ((indFrame - 1) %
                            step) == 0 and indFlowMap < numFlowMap:

                        imgDisplay = np.hstack((prvs, next))

                        # save the frames into png files for FlowNet to read
                        # TODO: stupid but is the easiest way without reconfigure
                        # the FlowNet and possible re-train the model
                        cv2.imwrite('data/frame1.png', prvs)
                        cv2.imwrite('data/frame2.png', next)

                        # compute the optical flow from two adjacent frames
                        FlowNet.run(prvs)  # the FlowNet will save a .flo file

                        # read the .flo file
                        nameFlow = 'flownetc-pred-0000000.flo'
                        flowMapSize = np.fromfile(nameFlow,
                                                  np.float32,
                                                  count=1)
                        if flowMapSize != 202021.25:
                            print 'Dimension incorrect. Invalid .flo file'
                        else:
                            data = np.fromfile(nameFlow,
                                               np.float32,
                                               count=2 * width * height)

                        flow = np.resize(data, (height, width, 2))
Пример #17
0
#!/usr/bin/python

import os, sys
from scripts.flownet import FlowNet

my_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(my_dir + '/..')

if len(sys.argv)-1 < 1:
    print("Use this tool to train FlowNet using the train.prototxt\n"
          "Usage for FlowNetS or FlowNetC:\n"
          "    ./train_flownet.py {S|C}\n")
    sys.exit(1)

model_folder = ''
if sys.argv[1].upper() == 'S':
    model_folder = './model_simple'
elif sys.argv[1].upper() == 'C':
    model_folder = './model_corr'
else:
    print("Please specify S for FlowNetSimple or C for FlowNetCorr\n")
    sys.exit(1)

FlowNet.train(my_dir, model_folder, sys.argv[2:])