socketio与apscheduler并用
celery变量共享

protobuf序列化numpy

妙音 posted @ 2020年6月15日 15:02 in python with tags protobuf , 611 阅读
 

说明

 
protobuf处理不能直接处理numpy,需要先把numpy转为字节
 

 numpy转字节

 
import numpy as np
from io import BytesIO
A = np.array([ 
      1, 2, 3, 4, 4,
      2, 3, 4, 5, 3,
      4, 5, 6, 7, 2,
      5, 6, 7, 8, 9,
      6, 7, 8, 9, 0 ]).reshape(5,5)

   
# numpy 转bytes
nda_bytes = BytesIO()
np.save(nda_bytes, A, allow_pickle=False)

# bytes转numpy
nda_bytes = BytesIO(nda_bytes.getvalue())
B = np.load(nda_bytes, allow_pickle=False)
print(np.array_equal(A, B))
 

定义protobuf message

 
ndarray.proto
 
syntax = "proto3";

message NDArray {
    bytes ndarray = 1;
}
 

使用

 
from io import BytesIO
import numpy as np
import ndarray_pb2   #上面ndarray.proto编译成python


def ndarray_to_proto(nda: np.ndarray) -> NDArray:
    """
    numpy转proto
    """
    nda_bytes = BytesIO()
    np.save(nda_bytes, nda, allow_pickle=False)
    return NDArray(ndarray=nda_bytes.getvalue())


def proto_to_ndarray(nda_proto: NDArray) -> np.ndarray:
    nda_bytes = BytesIO(nda_proto.ndarray)
    return np.load(nda_bytes, allow_pickle=False)



A = np.array([ 
      1, 2, 3, 4, 4,
      2, 3, 4, 5, 3,
      4, 5, 6, 7, 2,
      5, 6, 7, 8, 9,
      6, 7, 8, 9, 0 ]).reshape(5,5)
serialized_A = ndarray_to_proto(A)
deserialized_A = proto_to_ndarray(serialized_A)
assert np.array_equal(A, deserialized_A)
 
 
 
Avatar_small
jacksonseo 说:
2020年10月08日 15:29

I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. Mywegmansconnect

Avatar_small
rwr 说:
2020年10月08日 15:41

Great post, and great website. Thanks for the information! Dlive vs Twitch

Avatar_small
JHONY JACK 说:
2020年10月08日 18:53

I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well! แทงไฮโล

Avatar_small
rwr 说:
2020年10月09日 20:27

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. whatsapp quotes

Avatar_small
rwr 说:
2020年10月10日 20:06

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Zojirushi NS-TSC10 Micom Rice Cooker

Avatar_small
jacksonseo 说:
2020年10月10日 20:10

You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming. Talktomcalisters

Avatar_small
ali 说:
2020年10月11日 14:15

Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog. telescoping back scratcher

Avatar_small
jacksonseo 说:
2020年10月11日 15:30

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. Appeven

Avatar_small
jacksonseo 说:
2020年10月11日 19:36

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. iIniciarbldaoi

Avatar_small
jacksonseo 说:
2020年10月14日 19:48

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. putlockers

Avatar_small
ali 说:
2020年10月15日 03:05

Great post! I am actually getting ready to across this information, is very helpful my friend. Also great blog here with all of the valuable information you have. Keep up the good work you are doing here. NATURAL LANGUAGE PROCESSING Services

Avatar_small
ali 说:
2020年10月15日 14:37

Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post. amazon back scratcher

Avatar_small
ali 说:
2020年10月17日 00:55

Very nice article. I enjoyed reading your post. very nice share. I want to twit this to my followers. Thanks !. Best ice shaver

Avatar_small
ali 说:
2020年10月18日 14:43

There is definately a great deal to know about this subject. I like all of the points you've made. hand back scratcher

Avatar_small
ali 说:
2020年10月20日 00:57

I have bookmarked your website because this site contains valuable information in it. I am really happy with articles quality and presentation. Thanks a lot for keeping great stuff. I am very much thankful for this site. سيو

Avatar_small
Jonny jack 说:
2020年10月20日 20:05

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. Erste Hilfe Kurs

Avatar_small
jacksonseo 说:
2020年10月21日 13:10

It's really nice and meanful. it's really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information. Sushi in Lahaina

Avatar_small
screenprintingmachin 说:
2020年10月21日 13:27

That's good for us. If you are searching for a blog this best site for your blog and very interested in this site.

Avatar_small
screenprintingmachin 说:
2020年10月21日 13:27

That's good for us. If you are searching for a blog this best site for your blog and very interested in this site. <a href=" https://screenprintingmachine.net/best-heat-press-machines">screenprintingmachine</a>

Avatar_small
maaz 说:
2020年10月21日 17:08

Thanks for taking the time to discuss this, I feel strongly that love and read more on this topic. If possible, such as gain knowledge, would you mind updating your blog with additional information? It is very useful for me. roof installation

Avatar_small
Jonny jack 说:
2020年10月21日 18:52

This post is really magnificent. I really like this post. It is one of the best posts that I ve read in a long time. Thanks a lot for this really good post. I really appreciate it! 먹튀폴리스

Avatar_small
maaz 说:
2020年10月21日 19:36

That is the excellent mindset, nonetheless is just not help to make every sence whatsoever preaching about that mather. Virtually any method many thanks in addition to i had endeavor to promote your own article in to delicius nevertheless it is apparently a dilemma using your information sites can you please recheck the idea. thanks once more. supplements for toning and weight loss female

Avatar_small
Jonny jack 说:
2020年10月21日 21:25

Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. 먹튀검증커뮤니티

Avatar_small
jacksonseo 说:
2020年10月23日 17:27

I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... Money Control

Avatar_small
토토사이트 说:
2020年10月23日 17:28

I use basically superior fabrics : you will discover these products by:

Avatar_small
jacksonseo 说:
2020年10月24日 01:15

This is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info: realtor Temecula

Avatar_small
moiz 说:
2020年10月26日 16:43

I would like to say that this blog really convinced me to do it! Thanks, very good post. BODY DETOX – THREE COMMON MISTAKES TO AVOID

Avatar_small
ali 说:
2020年10月26日 22:18

I was just browsing through the internet looking for some information and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject. Bookmarked this page, will come back for more. amazon back scratcher

Avatar_small
ali 说:
2020年10月27日 21:57

Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. flour sifter amazon

Avatar_small
Facial oil 说:
2020年10月28日 19:31

That is enterprise associated knowledge gaining article. This put up is truly the first-class on this valuable subject matter.

Avatar_small
Best Darkweb Carding 说:
2020年10月29日 22:22

Thank you for some other informative blog. Where else could I get that type of information written in such an ideal means? I have a mission that I’m just now working on, and I have been at the look out for such information.

Avatar_small
Top SEO 说:
2020年10月31日 03:09

I'm impressed, I must say. Very rarely do I come across a blog thats both informative and entertaining, and let me tell you, you ve hit the nail on the head. Your blog is important.. ดาวน์โหลด slotxo

Avatar_small
william nabeel madan 说:
2020年11月02日 20:48

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work

Avatar_small
Top SEO 说:
2020年11月03日 18:31

I'm impressed, I must say. Very rarely do I come across a blog thats both informative and entertaining, and let me tell you, you ve hit the nail on the head. Your blog is important.. how to invest in bitcoin,

Avatar_small
Top SEO 说:
2020年11月04日 18:16

I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. Ottawa Web Design

Avatar_small
erum 说:
2020年11月05日 02:03

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page! magic mushrooms for sale

Avatar_small
erum 说:
2020年11月05日 18:23

Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here. 먹튀검증업체

Avatar_small
erum 说:
2020年11月06日 00:58

Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here. baby washcloths

Avatar_small
ali 说:
2020年11月07日 14:58

After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article. germany phonova.directory

Avatar_small
Live Draw hk 说:
2020年11月08日 00:00

Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I'll be sure to check back again real soon.

Avatar_small
vitamin x 说:
2020年11月09日 23:29

Vitamin X are specialists in Anti-Ageing and Liquid Vitamins. Our range includes the award winning Magic Potion 10,000MG Liquid Marine Hydrolyzed Collagen Types 1 & 2 Anti-Ageing Drink which is packed full of super ingredients

Avatar_small
Top SEO 说:
2020年11月10日 20:45

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. https://www.jalalibd.com/

Avatar_small
ali 说:
2020年11月11日 14:42

I was just browsing through the internet looking for some information and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject. Bookmarked this page, will come back for more. baking sieve

Avatar_small
PHP to EXE compiler 说:
2020年11月11日 23:15

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work

Avatar_small
ali 说:
2020年11月12日 00:03

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful. online casino

Avatar_small
jacksonseo 说:
2020年11月12日 19:50

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. buy facebook live viewers

Avatar_small
free guest post 说:
2020年11月12日 22:52

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Avatar_small
ali 说:
2020年11月13日 19:12

I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!.. rv body repair shops near me

Avatar_small
ali 说:
2020年11月14日 23:07

I’m excited to uncover this page. I need to to thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and i also have you saved to fav to look at new information in your site. Schilder landgraaf

Avatar_small
ali 说:
2020年11月15日 13:06

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. back scratcher shoe horn

Avatar_small
ali 说:
2020年11月17日 13:59

Awesome blog. I enjoyed reading your articles. 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! Monthly SEO packages

Avatar_small
Top SEO 说:
2020年11月17日 19:42

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. 먹튀폴리스

Avatar_small
Lambingan 说:
2020年11月18日 19:59

I would like to say that this blog really convinced me to do it! Thanks, very good post.

Avatar_small
Jackjohnny 说:
2020年11月18日 22:20

Here we have provided all educational board class 10th & 12th Standard Model Paper 2021 with Practice Question Paper for both medium student of the country in board wide, Question Paper 2021 hope you download and practice the question bank to get a better score in all exams.

Avatar_small
Custom Patches 说:
2020年11月21日 18:01

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.

Avatar_small
moiz 说:
2020年11月22日 18:59

I have been impressed after read this because of some quality work and informative thoughts. I just want to say thanks for the writer and wish you all the best for coming! Your exuberance is refreshing. sattamatka

Avatar_small
moiz 说:
2020年11月22日 19:14

I read that Post and got it fine and informative. indianmatka.mobi

Avatar_small
Top SEO 说:
2020年11月23日 15:06

A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. good product review

Avatar_small
moiz 说:
2020年11月23日 15:31

It is truly a well-researched content and excellent wording. I got so engaged in this material that I couldn’t wait reading. I am impressed with your work and skill. Thanks. Class 7 Assignment Solution

Avatar_small
moiz 说:
2020年11月23日 15:42

Excellent blog! I found it while surfing around on Google. Content of this page is unique as well as well researched. Appreciate it. Google Ratings

Avatar_small
ali 说:
2020年11月23日 23:32

Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me. stainless steel flour sifter


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter