Halide - An amazing tool for Image Processing
其实选图像处理挺坑爹的
最后阶段选择的Halide比较符合我的口味,因为我不怎么会用Matlab的很无语的说……C++比较会一点的说,Python还在学习的说……
正题:
传统的图像处理的语言都比较让人望而却步,从数据并行,矢量化,多进程来考量这些语言的话
比如C —— 很不友好,要写很多行代码要搞起很多东西,像我这种懒人肯定不喜欢的说
比如CUDA, OpenCL, shaders —— 这种处理数据并发性很好,但是跪在数据融合的神神叨叨的语言(Libraries don't help)
比如BLAS, IPP, MKL, OpenCV, MATLAB —— 做了优化处理,但是还是没有融合(其实我还是没懂啥是fusion啊)
其实在为了Image Inpainting的作业到处找代码的时候我就发现一个问题,很多语言做的时候,算法和语言结合很紧密,以至于我想抠下来那个算法还是要纠结一段时间呢。并且使用Matlab的时候处理速度很慢很慢,处理一个800 × 600的一张灰度图需要我看两集电视剧的时间,并且最好不用双层嵌套循环,我这破本本2G内存完全就跪了,所以传统的图像处理还是非常难以忍受的。
综合以上,Halide就横空出世解决很多纠纠结结的问题。
Halide 可以做到
简化代码
代码模块化
实现数据融合,细化,并行,矢量化
很好地保护实现算法
提供了一个程序员能迅速上手简化代码让其能专注于算法,编译器能做更大优化的工具。
Halide是将Algorithm和Schedule分开
Algorithm 部分将算法剥离开来,不依赖编译语言的纠结。
Pipe stages are functions from coordinates to values.
No side effects
Coordinates span an infinite domain
Boundaries and required regions are inferred
Execution order and storage are unspecified.
Points can be evaluated(or reevaluated) in any order
results can be cached, duplicated, or recomputed anywhere
Shedule 部分专注于实现代码的高效处理。有这么几个函数来实现 pixels 的高效处理
Inline: compute as needed, do not store
Root: precompute entire required region
Chunk: compute, use, then discard
Reuse: load from an existing buffer
等等实现了促成 Halide 实现图像处理的简单化和高效化。
Halide 是怎么写啊。。。就跟写C++一样写,实现的时候 Halide 函数通过 JIT 来产生和使用 Halide pipeline。但是注意前面铺垫了那么久的算法和调度分开的思想,所以嘛,算法实现部分略像函数式编程风格。也没有其他的说法。
首先加上作者提供的库:戳这里 (我使用的是 Debian Sid,作者仅提供了 Ubuntu12.04 和 Mac OS 10.7 的库,Windows被刨除在外了><)
下面的一个例子是用 Halide 实现 Blur 的一个例子
#include "halide/Halide.h"
#include <stdio.h>
using namespace Halide;
int main(int argc, char **argv) {
UniformImage input(UInt(16), 2);
Func blur_x("blur_x"), blur_y("blur_y");
Var x("x"), y("y"), xi("xi"), yi("yi");
// The algorithm
blur_x(x, y) = (input(x-1, y) + input(x, y) + input(x+1, y))/3;
blur_y(x, y) = (blur_x(x, y-1) + blur_x(x, y) + blur_x(x, y+1))/3;
// How to schedule it
blur_y.tile(x, y, xi, yi, 128, 32);
blur_y.vectorize(xi, 8);
blur_y.parallel(y);
blur_x.chunk(x);
blur_x.vectorize(x, 8);
blur_y.split(y, y, yi, 8).parallel(y).vectorize(x, 8);
blur_x.chunk(y, yi).vectorize(x, 8);
blur_y.compileToFile("halide_blur");
return 0;
}
Halide 可以以最短的时间,最短的代码行书,速度为您解决图像处理的问题,实乃出门必备,生活常用的工具啊~
已经可以在 Nokia N900 上跑一些 Halide 的程序(其实这是废话),来看看这条狗
Halide algorithm: 145 lines
Schedule: 23 lines
time: 741ms
2.75x shorter
5% faster than tuned assembly
好像不错诶~
其实应该完了的,但是好奇看了看某部分代码,然后发现有用 Python 实现的 Halide ……然后效果我就不说了,Python 有多慢就多慢,本来代码行数一般要比 C 系列的少很多的是吧,在这里就完全跪了( 这说明 Halide 更加简洁有效啊><)下面一个 Python 实现 Halide 的例子
import sys; sys.path += ['..', '.']
from halide import *
OUT_DIMS = (6400, 4864, 3)
def filter_func(dtype=UInt(16)):
"Simple 3x3 blur."
input = UniformImage(dtype, 2, 'input')
x = Var('x')
y = Var('y')
#c = Var('c')
blur_x = Func('blur_x')
blur_y = Func('blur_y')
blur_x[x,y] = (input[x,y]+input[x+1,y]+input[x+2,y])/3
blur_y[x,y] = (blur_x[x,y]+blur_x[x,y+1]+blur_x[x,y+2])/3
tune_ref_schedules = {'human': \
"blur_y.split(y, y, yi, 8).parallel(y).vectorize(x, 8)\n" \
"blur_x.chunk(y, yi).vectorize(x, 8)"}
#tune_constraints = 'blur_y.bound(c, 0, 3)'
tune_in_images = ['apollo1.png']
tune_out_dims = OUT_DIMS
return (input, blur_y, None, locals())
def main():
(input, out_func, evaluate, local_d) = filter_func()
x, y = local_d['x'], local_d['y'] #, local_d['c']
blur_x, blur_y = local_d['blur_x'], local_d['blur_y']
xi, yi = Var('xi'), Var('yi')
schedule = 1
if schedule == 0: # Human schedule, no store-compute chunking
blur_y.tile(x, y, xi, yi, 8, 4).parallel(y).vectorize(xi, 8)
blur_x.chunk(x).vectorize(x, 8)
elif schedule == 1:
blur_y.split(y, y, yi, 8).parallel(y).vectorize(x, 8)
blur_x.chunk(y, yi).vectorize(x, 8)
test = filter_image(input, out_func, os.path.join(inputs_dir(), 'apollo.png'), disp_time=True, out_dims = OUT_DIMS, times=5)().show()
if __name__ == '__main__':
main()
啊。。。是的。。。就是这么囧,怪不得作者没有放出 Python 相关的东西。。。
嗯。。。然后完了,自习室里敲完这段然后去看编译原理。。。期末了,要考试了,要挂了。。。T^T
我自己的 slides(别太认真。。。略丢人)
May 14, 2021 04:21:26 PM
Very much useful for the students who are willing to learn algorithm.
http://www.delhi37.in/
Apr 23, 2023 03:31:47 PM
crediblebh
Apr 29, 2023 05:57:28 PM
JNVST Navodaya Result 2023 Class 5th Class 6th 9th 11th Latest Option List, Marks JNVST Navodaya Result 2023 5th Class 6th, 9th, 11th Class navodayaresult2020-5th.in, Jawahar Navodaya Vidyalaya Selection Examination JNVST Navodaya Officers Announced 5th Class 6th, 9th and 11th Class Admissions. The 2023 Result From the Official Webpage Where Understudies can Download Navodaya Vidyalaya. Furthermore, The JNVST Navodaya Choice Rundown we Have Recorded is 2023.
Jan 16, 2024 02:57:53 PM
Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your post
Jan 30, 2024 01:54:03 PM
To be successful at losing weight, you need to focus on more than just how you look. Approaches that leverage mood, overall health, and mental health are most effective. Because no two weight loss journeys are the same, we asked many women who have achieved major weight loss exactly how they did it.
Jan 30, 2024 02:04:11 PM
I have browsed maximum of your posts. This put up is probably wherein i have been given the maximum. That is a super website, thank you for sharing. There may be no doubt i'd sincerely price it after i read what the idea about this article is. You probable did a nice . You have got done a notable manner with you net web site
Jan 30, 2024 02:44:04 PM
To be successful at losing weight, you need to focus on more than just your appearance. Approaches that leverage mood, overall health, and mental health are most effective. Because no two weight loss journeys are the same, we asked many women who have achieved major weight loss exactly how they did it.
Jan 30, 2024 02:53:05 PM
Regular visits listed here are the easiest way to assess your energy, so visit our website every day and search for new and interesting information. Many thanks
Jan 30, 2024 03:00:21 PM
brilliant inteed to check out new belongings you positioned up. This is in reality an sensible and first-class data for all. Thank you for sharing this to us and extra strength
Jan 30, 2024 03:06:05 PM
writing with fashion and getting top compliments at the article is pretty tough, to be honest. But you have carried out it so lightly and with so cool feeling and you've nailed the process. This article is possessed with style and i am giving top compliment. High-quality! I suppose that is one of the most vast information for me. And i am satisfied studying your article. I ought to mention that, as much as i cherished listening to what you would possibly have to say, i got bored after a while. Live up to date for distinctive and thorough commands if you're searching out the exceptional . Very informative publish ! There is lots of data
Jan 30, 2024 03:21:12 PM
I have browsed maximum of your posts. This put up is probably wherein i have been given the maximum. That is a super website, thank you for sharing. There may be no doubt i'd sincerely price it after i read what the idea about this article is. You probable did a nice . You have got done a notable manner with you net web site
Jan 30, 2024 03:22:36 PM
i in my opinion use them my preferred minis for almost the entirety. From time to time i without a doubt draw wow gold online
Jan 30, 2024 03:27:40 PM
Regular visits listed here are the easiest way to assess your energy, so visit our website every day and search for new and interesting information. Many thanks
Jan 30, 2024 03:45:27 PM
Any distributor who provides merchandise to a retailer. Retailers can benefit from a lower price than if they were to acquire individual items by purchasing products from
Jan 30, 2024 03:48:52 PM
To be successful at losing weight, you need to focus on more than just how you look. Approaches that leverage mood, overall health, and mental health are most effective. Because no two weight loss journeys are the same, we asked many women who have achieved major weight loss exactly how they did it.
Jan 30, 2024 03:52:36 PM
I do agree with all of the ideas you’ve presented in your post. They’re very convincing and will certainly work. Still, the posts are very short for starters. Could you please extend them a bit from next time? Thanks for the post.
Jan 30, 2024 04:06:38 PM
You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers
Jan 30, 2024 04:21:04 PM
Interesting topic for a blog. I was searching the internet for fun and found your website. Great post. Thanks for sharing your knowledge! It's great to see that some people still put effort into maintaining their website. I'll check back soon.
Jan 30, 2024 04:24:44 PM
Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long. I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share.
Jan 30, 2024 04:41:01 PM
Regular visits listed here are the easiest way to assess your energy, so visit our website every day and search for new and interesting information. Many thanks,
Jan 30, 2024 04:49:02 PM
The comments are owned by the poster. We are not responsible for its content. We value free speech but remember this is a public forum and we hope that people would use common sense and decency. If you see an offensive comment please email us at news@thepinetree.net
Jan 30, 2024 04:59:33 PM
I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy.
Jan 30, 2024 05:11:33 PM
This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!.
Jan 30, 2024 05:14:02 PM
If more people that write articles really concerned themselves with writing great content like you, more readers would be interested in their writings. Thank you for caring about your content.
Jan 30, 2024 05:54:15 PM
To be successful at losing weight, you need to focus on more than just how you look. Approaches that leverage mood, overall health, and mental health are most effective. Because no two weight loss journeys are the same, we asked many women who have achieved major weight loss exactly how they did it.
Jan 30, 2024 06:09:44 PM
To be successful at losing weight, you need to focus on more than just your appearance. Approaches that leverage mood, overall health, and mental health are most effective. Because no two weight loss journeys are the same, we asked many women who have achieved major weight loss exactly how they did it.
Jan 30, 2024 06:23:14 PM
brilliant inteed to check out new belongings you positioned up. This is in reality an sensible and first-class data for all. Thank you for sharing this to us and extra strength
Jan 30, 2024 06:39:20 PM
I have browsed maximum of your posts. This put up is probably wherein i have been given the maximum. That is a super website, thank you for sharing. There may be no doubt i'd sincerely price it after i read what the idea about this article is. You probable did a nice . You have got done a notable manner with you net web site
Jan 30, 2024 06:40:38 PM
i in my opinion use them my preferred minis for almost the entirety. From time to time i without a doubt draw wow gold online
Jan 30, 2024 06:52:23 PM
Any distributor who provides merchandise to a retailer. Retailers can benefit from a lower price than if they were to acquire individual items by purchasing products from
Jan 30, 2024 07:05:55 PM
I do agree with all of the ideas you’ve presented in your post. They’re very convincing and will certainly work. Still, the posts are very short for starters. Could you please extend them a bit from next time? Thanks for the post.
Jan 30, 2024 07:20:35 PM
You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers
Jan 30, 2024 07:32:10 PM
Interesting topic for a blog. I was searching the internet for fun and found your website. Great post. Thanks for sharing your knowledge! It's great to see that some people still put effort into maintaining their website. I'll check back soon.
Jan 30, 2024 07:34:36 PM
Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long. I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share.
Jan 30, 2024 07:43:22 PM
Are you looking for the hottest call girls in Islamabad? Look no further! Islamabad is the capital of Pakistan and is home to some of the most stunning call girls in the country. Whether you're looking for a night of fun and pleasure or an unforgettable experience with a beautiful woman, Islamabad call girls offer a variety of services to suit your needs. In this blog post, we'll explore the best places to find the hottest
Jan 30, 2024 07:53:12 PM
fantastic weblog. I extremely joyful in perusing your articles. This is sincerely an tremendous perused for me. I have bookmarked it and i am expecting perusing new articles. Keep doing fantastic! There may be a lot in this text that i would by no means have notion of on my own. Your content material offers readers matters to think about in an exciting way. Exceptional article. Captivating to study. I really like to examine such an extraordinary article. Thank you! It has made my task more and extra clean. Keep rocking. Very exciting statistics, well worth recommending. However, i advise this
Jan 30, 2024 08:15:26 PM
Regular visits listed here are the easiest way to assess your energy, so visit our website every day and search for new and interesting information. Many thanks
Jan 30, 2024 08:33:06 PM
I found this post while searching for related information in blog search. Great post. Please keep posting and keep your information updated.
Jan 30, 2024 08:37:16 PM
I have read some excellent stuff here. Certainly price bookmarking for revisiting. I surprise how a lot attempt you place to create this type of great informative web site.