I'm working on a ML model, and I have to convert a list of arrays (the weights of the layers) to a string, to send it over MQTT. Then I have to convert it to a list of arrays again, and thats where I don't know how to solve it. The initial look of the list is like this:
Initial list from model.get_weights()
:
[array([[ 0.05541647, -0.00467741, 0.06709623, ..., -0.06240537,
-0.05044469, -0.06255569],
[ 0.05793238, -0.04376897, -0.03331734, ..., 0.04109375,
-0.05561347, -0.05630576],
[ 0.03568218, 0.00916858, 0.02733664, ..., 0.04085189,
0.07445424, 0.05173937],
...,
[ 0.00326935, 0.05949181, -0.02493389, ..., 0.01619817,
0.02883349, -0.00364999],
[ 0.05162556, -0.07704586, -0.00726594, ..., 0.03567791,
0.06234651, 0.05147751],
[-0.04587721, 0.06365172, -0.06174358, ..., -0.07004303,
-0.00196535, -0.05049317]], dtype=float32), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32), array([[ 0.11118214, -0.00349338, 0.18680657, ..., 0.01847704,
-0.03098661, -0.04094526],
[-0.06314829, 0.00289522, -0.11807185, ..., -0.10976926,
-0.12070866, 0.19067971],
[-0.05408052, -0.02283411, 0.16553403, ..., -0.12856016,
0.00681128, -0.05486405],
...,
[-0.12182648, -0.03314751, 0.04840027, ..., 0.13398318,
-0.092302 , 0.13001741],
[ 0.01030177, 0.14168383, -0.18688273, ..., -0.17727108,
-0.1098071 , -0.12000293],
[ 0.03310342, 0.17201088, -0.08573408, ..., 0.15494372,
-0.16848558, 0.12254588]], dtype=float32), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)][array([[-0.07572845, 0.07035964, -0.00726507, ..., -0.01283053,
-0.02842413, 0.02551443],
[-0.00741241, -0.02386538, 0.00442091, ..., 0.0693512 ,
0.02695736, -0.07246653],
[ 0.06941632, -0.01986459, 0.02596217, ..., 0.04713184,
0.03926247, 0.07958693],
...,
[ 0.04515444, -0.02030407, -0.00393321, ..., 0.025347 ,
-0.01182116, 0.04929114],
[-0.06743087, 0.02246762, 0.0225632 , ..., 0.03987813,
-0.00048529, 0.00320805],
[ 0.07628443, -0.06414777, 0.04115602, ..., -0.03207976,
-0.01118261, 0.00946496]], dtype=float32), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32), array([[ 0.19875868, -0.0724885 , -0.15991165, ..., -0.04141769,
0.11540116, 0.1246707 ],
[ 0.03422281, 0.09608312, 0.18289839, ..., 0.20248671,
-0.05454096, -0.11580068],
[ 0.12459688, 0.17984338, 0.02630243, ..., -0.20585045,
-0.08128738, 0.08814187],
...,
[ 0.07335795, -0.02979451, 0.18084474, ..., 0.10529856,
-0.01682918, 0.09111448],
[-0.04859972, 0.00864089, 0.12390362, ..., 0.17152672,
-0.00713953, 0.06918244],
[ 0.07703741, 0.08441998, 0.07430147, ..., 0.08184789,
-0.17301415, -0.11319483]], dtype=float32), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)][array([[ 0.0367789 , 0.00915425, -0.02733853, ..., -0.02040792,
-0.03245208, 0.05279592],
[-0.07986325, -0.0093028 , 0.04690679, ..., -0.03594837,
-0.03365551, 0.04181867],
[-0.01529652, -0.04739384, -0.04961624, ..., 0.03608193,
-0.02728439, 0.03388698],
...,
[ 0.06456115, -0.06791718, 0.02804885, ..., -0.02433868,
-0.06182578, -0.01848171],
[ 0.02070352, -0.03081129, -0.06013838, ..., 0.00220076,
-0.05257946, 0.04429463],
[-0.00666717, -0.05574629, -0.03431721, ..., 0.07651306,
0.02397371, -0.06563253]], dtype=float32), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32), array([[-0.03700976, 0.03013754, -0.10353263, ..., -0.02945483,
0.0997458 , -0.00535272],
[-0.09297995, -0.00978217, -0.15470384, ..., 0.18909012,
-0.02411154, 0.03662926],
[-0.14865722, 0.13019712, -0.16894627, ..., 0.02009523,
0.18213274, -0.0228352 ],
...,
[-0.01553613, 0.09343223, 0.08486612, ..., -0.05365789,
0.01778294, -0.16807753],
[-0.18208605, 0.04372226, 0.00357029, ..., -0.19741432,
-0.05363443, 0.02788939],
[ 0.08774336, -0.01484367, 0.20057438, ..., -0.14653617,
-0.01546355, 0.05677335]], dtype=float32), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)][array([[-0.06932048, 0.04931927, 0.02986243, ..., -0.00124229,
-0.04131682, 0.04874287],
[ 0.02503149, -0.01789933, 0.01456298, ..., -0.07483141,
-0.00834411, 0.06528252],
[-0.07246303, -0.05168567, -0.07982197, ..., 0.03553585,
-0.07355539, 0.0455386 ],
...,
[-0.03427464, -0.05049596, 0.04526667, ..., 0.0540349 ,
-0.07729132, 0.02335045],
[ 0.00899633, 0.02592985, -0.06459068, ..., -0.06000284,
-0.06346118, 0.00611115],
[ 0.05585308, -0.00852666, -0.01165473, ..., -0.07250661,
-0.07178727, 0.04963235]], dtype=float32), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32), array([[ 0.1062278 , 0.07988457, -0.20682454, ..., 0.0976506 ,
-0.0116874 , -0.06627488],
[ 0.02052386, -0.20188682, -0.15016697, ..., 0.15503861,
0.04030807, 0.17274798],
[-0.0675576 , 0.09332336, -0.1745064 , ..., 0.07768513,
-0.04787958, 0.06289487],
...,
[-0.20753261, 0.06955643, -0.19981481, ..., -0.01403984,
0.04701854, -0.20236667],
[ 0.11430956, 0.02020629, 0.03855045, ..., -0.05780427,
0.0012497 , -0.12894002],
[ 0.1534607 , -0.18565604, 0.13524099, ..., -0.184562 ,
-0.06643088, 0.08209728]], dtype=float32), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]
but after convert it to a String it looks like this
List after convert it to String, weightsStr = ''.join(str(e) for e in weights)
:
[[ 0.05541647 -0.00467741 0.06709623 ... -0.06240537 -0.05044469
-0.06255569]
[ 0.05793238 -0.04376897 -0.03331734 ... 0.04109375 -0.05561347
-0.05630576]
[ 0.03568218 0.00916858 0.02733664 ... 0.04085189 0.07445424
0.05173937]
...
[ 0.00326935 0.05949181 -0.02493389 ... 0.01619817 0.02883349
-0.00364999]
[ 0.05162556 -0.07704586 -0.00726594 ... 0.03567791 0.06234651
0.05147751]
[-0.04587721 0.06365172 -0.06174358 ... -0.07004303 -0.00196535
-0.05049317]][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0.][[ 0.11118214 -0.00349338 0.18680657 ... 0.01847704 -0.03098661
-0.04094526]
[-0.06314829 0.00289522 -0.11807185 ... -0.10976926 -0.12070866
0.19067971]
[-0.05408052 -0.02283411 0.16553403 ... -0.12856016 0.00681128
-0.05486405]
...
[-0.12182648 -0.03314751 0.04840027 ... 0.13398318 -0.092302
0.13001741]
[ 0.01030177 0.14168383 -0.18688273 ... -0.17727108 -0.1098071
-0.12000293]
[ 0.03310342 0.17201088 -0.08573408 ... 0.15494372 -0.16848558
0.12254588]][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][[-0.07572845 0.07035964 -0.00726507 ... -0.01283053 -0.02842413
0.02551443]
[-0.00741241 -0.02386538 0.00442091 ... 0.0693512 0.02695736
-0.07246653]
[ 0.06941632 -0.01986459 0.02596217 ... 0.04713184 0.03926247
0.07958693]
...
[ 0.04515444 -0.02030407 -0.00393321 ... 0.025347 -0.01182116
0.04929114]
[-0.06743087 0.02246762 0.0225632 ... 0.03987813 -0.00048529
0.00320805]
[ 0.07628443 -0.06414777 0.04115602 ... -0.03207976 -0.01118261
0.00946496]][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0.][[ 0.19875868 -0.0724885 -0.15991165 ... -0.04141769 0.11540116
0.1246707 ]
[ 0.03422281 0.09608312 0.18289839 ... 0.20248671 -0.05454096
-0.11580068]
[ 0.12459688 0.17984338 0.02630243 ... -0.20585045 -0.08128738
0.08814187]
...
[ 0.07335795 -0.02979451 0.18084474 ... 0.10529856 -0.01682918
0.09111448]
[-0.04859972 0.00864089 0.12390362 ... 0.17152672 -0.00713953
0.06918244]
[ 0.07703741 0.08441998 0.07430147 ... 0.08184789 -0.17301415
-0.11319483]][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][[ 0.0367789 0.00915425 -0.02733853 ... -0.02040792 -0.03245208
0.05279592]
[-0.07986325 -0.0093028 0.04690679 ... -0.03594837 -0.03365551
0.04181867]
[-0.01529652 -0.04739384 -0.04961624 ... 0.03608193 -0.02728439
0.03388698]
...
[ 0.06456115 -0.06791718 0.02804885 ... -0.02433868 -0.06182578
-0.01848171]
[ 0.02070352 -0.03081129 -0.06013838 ... 0.00220076 -0.05257946
0.04429463]
[-0.00666717 -0.05574629 -0.03431721 ... 0.07651306 0.02397371
-0.06563253]][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0.][[-0.03700976 0.03013754 -0.10353263 ... -0.02945483 0.0997458
-0.00535272]
[-0.09297995 -0.00978217 -0.15470384 ... 0.18909012 -0.02411154
0.03662926]
[-0.14865722 0.13019712 -0.16894627 ... 0.02009523 0.18213274
-0.0228352 ]
...
[-0.01553613 0.09343223 0.08486612 ... -0.05365789 0.01778294
-0.16807753]
[-0.18208605 0.04372226 0.00357029 ... -0.19741432 -0.05363443
0.02788939]
[ 0.08774336 -0.01484367 0.20057438 ... -0.14653617 -0.01546355
0.05677335]][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][[-0.06932048 0.04931927 0.02986243 ... -0.00124229 -0.04131682
0.04874287]
[ 0.02503149 -0.01789933 0.01456298 ... -0.07483141 -0.00834411
0.06528252]
[-0.07246303 -0.05168567 -0.07982197 ... 0.03553585 -0.07355539
0.0455386 ]
...
[-0.03427464 -0.05049596 0.04526667 ... 0.0540349 -0.07729132
0.02335045]
[ 0.00899633 0.02592985 -0.06459068 ... -0.06000284 -0.06346118
0.00611115]
[ 0.05585308 -0.00852666 -0.01165473 ... -0.07250661 -0.07178727
0.04963235]][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0.][[ 0.1062278 0.07988457 -0.20682454 ... 0.0976506 -0.0116874
-0.06627488]
[ 0.02052386 -0.20188682 -0.15016697 ... 0.15503861 0.04030807
0.17274798]
[-0.0675576 0.09332336 -0.1745064 ... 0.07768513 -0.04787958
0.06289487]
...
[-0.20753261 0.06955643 -0.19981481 ... -0.01403984 0.04701854
-0.20236667]
[ 0.11430956 0.02020629 0.03855045 ... -0.05780427 0.0012497
-0.12894002]
[ 0.1534607 -0.18565604 0.13524099 ... -0.184562 -0.06643088
0.08209728]][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
I'm not really sure how to return it to his original form.
Thank you!!
As far as I see, you have numpy arrays there. In this case, I think, the best approach would be to use numpy load and save methods.
https://numpy.org/doc/stable/reference/generated/numpy.save.html
https://numpy.org/doc/stable/reference/generated/numpy.load.html
Or if you need a human readable string representation array2string and fromstring
https://numpy.org/doc/stable/reference/generated/numpy.array2string.html
https://numpy.org/doc/stable/reference/generated/numpy.fromstring.html
First check with the dimension of the array that it is 2d,3d, or multi-dimension and then according to that create properly for loops to iterate through all the item of array list and use manual type casting to convert all values to string.
Example for 1D array to convert int & float to string
arr = [1,2,5,6,4,8,1.55,6.33,2.22,3.03,-0.222,-52222]
print(arr)
for i in range(len(arr)):
arr[i] = str(arr[i])
print(arr)
Output :
[1,2,5,6,4,8,1.55,6.33,2.22,3.03,-0.222,-52222]
['1', '2', '5', '6', '4', '8', '1.55', '6.33', '2.22', '3.03', '-0.222', '-52222']
https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.morphology.distance_transform_edt.html
I'm having trouble understanding how the Euclidean distance transform function works in Scipy. From what I understand, it is different than the Matlab function (bwdist). As an example, for the input:
[[ 0. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 0.]]
The scipy.ndimage.distance_transform_edt function returns the same array:
[[ 0. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 0.]]
But the matlab function returns this:
1.4142 1.0000 1.4142 2.2361 3.1623
1.0000 0 1.0000 2.0000 2.2361
1.4142 1.0000 1.4142 1.0000 1.4142
2.2361 2.0000 1.0000 0 1.0000
3.1623 2.2361 1.4142 1.0000 1.4142
which makes more sense, as it is returning the "distance" to the nearest one.
It is not clear from the docstring, but distance_transform_edt computes the distance from non-zero (i.e. non-background) points to the nearest zero (i.e. background) point.
For example:
In [42]: x
Out[42]:
array([[0, 0, 0, 0, 0, 1, 1, 1],
[0, 1, 1, 1, 0, 1, 1, 1],
[0, 1, 1, 1, 0, 1, 1, 1],
[0, 0, 1, 1, 0, 0, 0, 1]])
In [43]: np.set_printoptions(precision=3) # Easier to read the result with fewer digits.
In [44]: distance_transform_edt(x)
Out[44]:
array([[ 0. , 0. , 0. , 0. , 0. , 1. , 2. , 3. ],
[ 0. , 1. , 1. , 1. , 0. , 1. , 2. , 2.236],
[ 0. , 1. , 1.414, 1. , 0. , 1. , 1. , 1.414],
[ 0. , 0. , 1. , 1. , 0. , 0. , 0. , 1. ]])
You can get the equivalent of Matlab's bwdist(a) by applying distance_transform_edt() to np.logical_not(a) (i.e. invert the foreground and background):
In [71]: a
Out[71]:
array([[ 0., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 0.]])
In [72]: distance_transform_edt(np.logical_not(a))
Out[72]:
array([[ 1.414, 1. , 1.414, 2.236, 3.162],
[ 1. , 0. , 1. , 2. , 2.236],
[ 1.414, 1. , 1.414, 1. , 1.414],
[ 2.236, 2. , 1. , 0. , 1. ],
[ 3.162, 2.236, 1.414, 1. , 1.414]])
Warren has already explained how distance_transform_edt works.
In your case,you could change sampling units along x and y
ndimage.distance_transform_edt(a)
array([[ 0., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 0.]])
But
>>> ndimage.distance_transform_edt(a, sampling=[2,2])
array([[ 0., 0., 0., 0., 0.],
[ 0., 2., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 2., 0.],
[ 0., 0., 0., 0., 0.]])
Or
ndimage.distance_transform_edt(a, sampling=[3,3])
array([[ 0., 0., 0., 0., 0.],
[ 0., 3., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 3., 0.],
[ 0., 0., 0., 0., 0.]])