├── LICENSE ├── README.md ├── demos ├── .DS_Store ├── 3dvision │ ├── 3DFace │ │ ├── .ipynb_checkpoints │ │ │ └── Analyze-checkpoint.ipynb │ │ ├── demo.png │ │ ├── generate_image.m │ │ ├── get_cnn_ftrs_pic.m │ │ ├── load_model_pic.m │ │ ├── main.jl │ │ ├── rotx.m │ │ ├── roty.m │ │ └── rotz.m │ ├── DR_Tests │ │ ├── DR_Test.jl │ │ ├── gpgp_dr_multiple_objs.jl │ │ ├── gpgp_dr_test1.jl │ │ └── test.py │ ├── human_pose │ │ ├── HumanKTH.blend │ │ ├── body_simulator_client.py │ │ ├── body_simulator_server.py │ │ ├── config.py │ │ ├── pose_program.jl │ │ ├── start_blender_KTH.sh │ │ └── test.png │ └── mesh_example │ │ ├── earth.jl │ │ ├── test_only_translation.png │ │ ├── test_trans_light1.png │ │ └── test_trans_light2.png └── sanity │ ├── BetaUniformMixture.jl │ └── GMM.jl └── engine ├── codetransform.jl ├── elliptical.jl ├── erps.jl ├── gibbs_kernel.jl ├── hmc.jl ├── mcmcML.jl ├── optimize.jl ├── picture.jl ├── proposals.jl ├── test.lua ├── torch_interface.jl ├── torch_server.lua └── unit_tests.jl /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MIT-Picture 2 | A probabilistic programming language for visual scene perception 3 | 4 | ### Requirements 5 | * Julia (http://julialang.org/downloads/) 6 | * Python (optinal) 7 | * MATLAB (optional) 8 | 9 | ### Quick Start 10 | ```python 11 | cd /programs 12 | julia .jl 13 | ``` 14 | Only developers should touch the /engine folder. Any probabilistic program should reside in /programs. Please see demo programs to learn how to get started. The demo programs are quite easy to follow. Once the system is mature, I will probably put out a brief paper describing the system. Please note that this system is under heavy initial development and has a lot of known bugs. Use at your own risk. 15 | -------------------------------------------------------------------------------- /demos/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrkulk/MIT-Picture/57bc1fd87895a5f376d10e83ef8c816657feddf1/demos/.DS_Store -------------------------------------------------------------------------------- /demos/3dvision/3DFace/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrkulk/MIT-Picture/57bc1fd87895a5f376d10e83ef8c816657feddf1/demos/3dvision/3DFace/demo.png -------------------------------------------------------------------------------- /demos/3dvision/3DFace/generate_image.m: -------------------------------------------------------------------------------- 1 | function image = generate_image(model, alpha,beta, phi, elv, mode_az,mode_ev, particle_id) 2 | shape = coef2object( alpha, model.shapeMU, model.shapePC, model.shapeEV, model.segMM, model.segMB ); 3 | tex = coef2object( beta, model.texMU, model.texPC, model.texEV, model.segMM, model.segMB ); 4 | 5 | close all; 6 | 7 | %render 8 | rp = defrp; 9 | rp.phi = phi;%0; 10 | rp.elevation = elv; %0; 11 | rp.dir_light.dir = [0;1;1]; 12 | rp.dir_light.intens = 0.6*ones(3,1); 13 | handle = display_face(shape, tex, model.tl, rp, mode_az,mode_ev, particle_id); 14 | rp.sbufsize=2000; 15 | 16 | image = hardcopy(handle, '-dzbuffer', '-r0'); 17 | % uncomment to see 18 | % frame = getframe(1,[0 0 rp.width rp.height]); 19 | % image = frame.cdata; 20 | 21 | % if phi == -0.75 || phi == 0.75 %phi ~= 0 || phi ~= -1 || phi ~= 1 22 | % off = 120; 23 | % image = image(rp.width/2-off:rp.width/2+off,rp.height/2-off:rp.height/2+off,:); 24 | % image = imresize(image, [rp.width, rp.height]); 25 | % end 26 | 27 | end -------------------------------------------------------------------------------- /demos/3dvision/3DFace/get_cnn_ftrs_pic.m: -------------------------------------------------------------------------------- 1 | function ftrs = get_cnn_ftrs(net, im, layer_id) 2 | im_ = single(im); 3 | im_ = imresize(im_, net.normalization.imageSize(1:2)); 4 | im_ = im_ - net.normalization.averageImage; 5 | 6 | res = vl_simplenn(net,im_); 7 | 8 | ftrs = res(layer_id).x(:); 9 | end 10 | 11 | -------------------------------------------------------------------------------- /demos/3dvision/3DFace/load_model_pic.m: -------------------------------------------------------------------------------- 1 | function [model msz] = load_model() 2 | global model; 3 | 4 | global OPENMIND; 5 | 6 | if OPENMIND == 1 7 | model = load('/om/user/tejask/PublicMM1/01_MorphableModel.mat'); 8 | else 9 | if isempty(model); 10 | filename = '/Users/tejas/Documents/MIT/DATASETS/BaselFace/PublicMM1/01_MorphableModel.mat'; 11 | 12 | if isequal(exist(filename,'file'),2) 13 | model = load('/Users/tejas/Documents/MIT/DATASETS/BaselFace/PublicMM1/01_MorphableModel.mat'); 14 | else 15 | model = load('/home/tejas/Documents/MIT/DATASET/PublicMM1/01_MorphableModel.mat'); 16 | end 17 | end 18 | end 19 | 20 | msz.n_shape_dim = size(model.shapePC, 2); 21 | msz.n_tex_dim = size(model.texPC, 2); 22 | msz.n_seg = size(model.segbin, 2); 23 | end 24 | 25 | -------------------------------------------------------------------------------- /demos/3dvision/3DFace/main.jl: -------------------------------------------------------------------------------- 1 | # Usage: julia main.jl 2 | 3 | # DESCRIPTION: 3D generative face analysis demo 4 | # Given a single image of a face, the model will compute the 3D mesh, lighting and camera parameters. 5 | # In this demo you can choose to either run with CNN features in a likelihood-free 6 | # setting (approximate baeysian computation - ABC), run it with pixel based likelihood or both (better). 7 | 8 | # NOTE: This demo is unoptimized and therefore will be very slow. Most of the computational slow-down is due to 9 | # MATLAB mesh rendering. This can be mitigated by a faster GPU based rendering along with discriminative data-driven proposals 10 | 11 | # REQUIREMENTS: (1) Download the basel face model (and update the paths) from here: 12 | # http://faces.cs.unibas.ch/bfm/main.php?nav=1-0&id=basel_face_model 13 | # (2) Download matconvnet : http://www.vlfeat.org/matconvnet/ 14 | # Picture also supports Torch. You can also add any other custom features you like from external MATLAB libraries 15 | 16 | include("../../../engine/picture.jl") 17 | reload("../../../engine/torch_interface.jl") 18 | using Debug 19 | import JSON 20 | using MATLAB 21 | using HDF5, JLD 22 | 23 | #### Initialize matlab #### 24 | @matlab begin 25 | clear; 26 | addpath ("/home/tejas/Documents/software/matconvnet-1.0-beta5/matlab"); #replace with your matconvnet PATH 27 | vl_setupnn; 28 | net = load("/home/tejas/Documents/software/matconvnet-1.0-beta5/imagenet-caffe-ref"); 29 | addpath("/home/tejas/Documents/MIT/DATASET/PublicMM1"); #replace with your basel model PATH 30 | addpath("/home/tejas/Documents/MIT/DATASET/PublicMM1/matlab");#replace with your basel model PATH 31 | disp("MATLAB session started ...") 32 | [model msz] = load_model_pic(); 33 | end 34 | 35 | ######### Book keeping variables ############ 36 | ABC = 0 #enable CNN feature computation for likelihood-free inference 37 | global START_TIME = time(); global LOGL_ARRAY = []; global TIME_ARRAY = [] 38 | global IMAGE_COUNTER = 0; OBSERVATIONS=Dict(); WIDTH = 299; HEIGHT = 299 39 | STORE_TMP = string(pwd(), "/tmp/", time(),".png") 40 | IMAGENET_LAYERID = 15; IMAGENET_SIGMA = 10 #6--18 | 10 -- 16 41 | 42 | @mput WIDTH; @mput HEIGHT; @mput IMAGENET_LAYERID; @mput IMAGENET_SIGMA; @mput STORE_TMP 43 | @matlab begin 44 | IMAGENET_SIGMA = double(IMAGENET_SIGMA); IMAGENET_LAYERID = int32(IMAGENET_LAYERID); 45 | savedir = ["results/" datestr(clock()) ];mkdir(savedir); 46 | obs_image = imread(["demo.png"]); 47 | 48 | obs_image = obs_image(1:WIDTH,1:HEIGHT,:); 49 | OBS_FTRS = get_cnn_ftrs_pic(net, obs_image, IMAGENET_LAYERID); 50 | obs_image = im2double(obs_image); 51 | obs_img_Arr = reshape(im2double(obs_image), 3*(WIDTH*HEIGHT), 1); 52 | imwrite(obs_image, STORE_TMP); 53 | end 54 | @mget savedir 55 | 56 | DIM = 99 #keep this 57 | ALPHA_PARAMS = zeros(DIM,4) #keep this 58 | BETA_PARAMS = zeros(DIM,4) #keep this 59 | 60 | ################### HELPER FUNCTION ############### 61 | @debug function GLRender(alpha1, alpha2, alpha3, alpha4, beta1, beta2, beta3, beta4, mode_az, phi) 62 | ALPHA_PARAMS[:,1] = alpha1; ALPHA_PARAMS[:,2] = alpha2; ALPHA_PARAMS[:,3] = alpha3; ALPHA_PARAMS[:,4] = alpha4 63 | BETA_PARAMS[:,1] = beta1; BETA_PARAMS[:,2] = beta2; BETA_PARAMS[:,3] = beta3; BETA_PARAMS[:,4] = beta4 64 | 65 | alpha_mat = mxarray(ALPHA_PARAMS); beta_mat = mxarray(BETA_PARAMS) 66 | @mput alpha_mat; @mput beta_mat; @mput mode_az; @mput phi 67 | @matlab begin 68 | phi = double(phi); mode_az = double(mode_az); 69 | shape = coef2object(alpha_mat, model.shapeMU, model.shapePC, model.shapeEV, model.segMM, model.segMB); 70 | tex = coef2object(beta_mat, model.texMU, model.texPC, model.texEV, model.segMM, model.segMB); 71 | elv=0; mode_ev = 0; #can be latents 72 | llscore=0; 73 | inferredimage = generate_image(model, alpha_mat,beta_mat, phi, elv, mode_az,mode_ev, 1); 74 | inferredimage = inferredimage(1:WIDTH,1:HEIGHT,:); 75 | 76 | cnn_ftrs = get_cnn_ftrs_pic(net, inferredimage, IMAGENET_LAYERID); 77 | allscore = double(sum(sum(log(normpdf(cnn_ftrs, OBS_FTRS, IMAGENET_SIGMA))))); 78 | abc_llscore = allscore; 79 | 80 | inferredimage = im2double(inferredimage); 81 | renderingArr = reshape(inferredimage, 3*(WIDTH*HEIGHT) ,1); 82 | pxerror = norm(renderingArr - obs_img_Arr)/numel(renderingArr); 83 | px_llscore = sum(log(mvnpdf(renderingArr,obs_img_Arr,0.001))); 84 | imwrite(inferredimage, STORE_TMP); 85 | end 86 | @mget abc_llscore; @mget px_llscore 87 | llscore = abc_llscore 88 | if ABC == 0 llscore = px_llscore end 89 | return llscore 90 | end 91 | 92 | ################### PROBABILISTIC CODE ############### 93 | function PROGRAM() 94 | LINE=Stack(Int);FUNC=Stack(Int);LOOP=Stack(Int);MEM=NaN; BID=NaN; 95 | 96 | alpha1 = block("part1_alpha", Normal(0,1, 1, DIM)) 97 | beta1 = block("part1_beta", Normal(0,1, 1, DIM)) 98 | 99 | alpha2 = block("part2_alpha", Normal(0,1, 1, DIM)) 100 | beta2 = block("part2_beta", Normal(0,1, 1, DIM)) 101 | 102 | alpha3 = block("part3_alpha", Normal(0,1, 1, DIM)) 103 | beta3 = block("part3_beta", Normal(0,1, 1, DIM)) 104 | 105 | alpha4 = block("part4_alpha", Normal(0,1, 1, DIM)) 106 | beta4 = block("part4_beta", Normal(0,1, 1, DIM)) 107 | 108 | mode_az = 0#block("light", Uniform(-80,80,1,1)) 109 | phi = 0#block("phi", Uniform(-1.5,1.5,1,1)) 110 | 111 | # renders and caches CNN features on rendered image 112 | llscore = GLRender(alpha1, alpha2, alpha3, alpha4, beta1, beta2, beta3, beta4, mode_az, phi) 113 | 114 | # constraint to observation 115 | params.CURRENT_TRACE["ll"] += llscore 116 | return 0 117 | end 118 | 119 | ########### USER DIAGNOSTICS ############## 120 | function debug_callback(TRACE) 121 | global IMAGE_COUNTER 122 | global START_TIME 123 | global LOGL_ARRAY 124 | global TIME_ARRAY 125 | cur_time = time() - START_TIME 126 | @mget pxerror 127 | println("LOGL=>", TRACE["ll"], " Test-reconstruction:", pxerror) 128 | LOGL_ARRAY = [LOGL_ARRAY, TRACE["ll"]] 129 | TIME_ARRAY = [TIME_ARRAY, cur_time] 130 | save(string(savedir,"/data.jld"), "LOGL_ARRAY", LOGL_ARRAY, "TIME_ARRAY", TIME_ARRAY) 131 | @mput IMAGE_COUNTER 132 | @matlab begin 133 | imwrite(inferredimage, [savedir "/" num2str(IMAGE_COUNTER) ".png"]) 134 | end 135 | IMAGE_COUNTER += 1 136 | end 137 | 138 | #demonstration of variable blocking 139 | function runner() 140 | #for repeats=1:10 141 | # infer( debug_callback, 5,"light", "MH_SingleSite", "","") 142 | # infer( debug_callback, 5,"phi", "MH_SingleSite", "","") 143 | #end 144 | 145 | for repeats=1:30 146 | infer( debug_callback, 1,"part1_alpha", "ELLIPTICAL", "","") 147 | infer( debug_callback, 1,"part1_beta", "ELLIPTICAL", "","") 148 | 149 | infer( debug_callback, 1,"part2_alpha", "ELLIPTICAL", "","") 150 | infer( debug_callback, 1,"part2_beta", "ELLIPTICAL", "","") 151 | 152 | infer( debug_callback, 1,"part3_alpha", "ELLIPTICAL", "","") 153 | infer( debug_callback, 1,"part3_beta", "ELLIPTICAL", "","") 154 | 155 | infer( debug_callback, 1,"part4_alpha", "ELLIPTICAL", "","") 156 | infer( debug_callback, 1,"part4_beta", "ELLIPTICAL", "","") 157 | 158 | #infer( debug_callback, 1,"light", "MH_SingleSite", "","") 159 | #infer( debug_callback, 1,"phi", "MH_SingleSite", "","") 160 | end 161 | end 162 | 163 | load_program(PROGRAM) 164 | load_observations(OBSERVATIONS) 165 | init() 166 | runner() 167 | 168 | 169 | -------------------------------------------------------------------------------- /demos/3dvision/3DFace/rotx.m: -------------------------------------------------------------------------------- 1 | function R = rotx(phi) 2 | %ROTX rotate around X by PHI 3 | % 4 | % R = ROTX(PHI) 5 | % 6 | % See also: ROTY, ROTZ, ROT, POS. 7 | 8 | % $ID$ 9 | % Copyright (C) 2005, by Brad Kratochvil 10 | 11 | R = [1 0 0; ... 12 | 0 cos(phi) -sin(phi); ... 13 | 0 sin(phi) cos(phi)]; 14 | 15 | % this just cleans up little floating point errors around 0 16 | % so that things look nicer in the display 17 | if exist('roundn'), 18 | R = roundn(R, -15); 19 | end 20 | 21 | end -------------------------------------------------------------------------------- /demos/3dvision/3DFace/roty.m: -------------------------------------------------------------------------------- 1 | function R = roty(beta) 2 | %ROTY rotate around Y by BETA 3 | % 4 | % R = ROTY(BETA) 5 | % 6 | % See also: ROTX, ROTZ, ROT, POS. 7 | 8 | % $ID$ 9 | % Copyright (C) 2005, by Brad Kratochvil 10 | 11 | R = [cos(beta) 0 sin(beta); ... 12 | 0 1 0; ... 13 | -sin(beta) 0 cos(beta)]; 14 | 15 | 16 | % this just cleans up little floating point errors around 0 17 | % so that things look nicer in the display 18 | if exist('roundn'), 19 | R = roundn(R, -15); 20 | end 21 | 22 | end -------------------------------------------------------------------------------- /demos/3dvision/3DFace/rotz.m: -------------------------------------------------------------------------------- 1 | function R = rotz(alpha) 2 | %ROTX rotate around Z by ALPHA 3 | % 4 | % R = ROTZ(ALPHA) 5 | % 6 | % See also: ROTX, ROTY, ROT, POS. 7 | 8 | % $ID$ 9 | % Copyright (C) 2005, by Brad Kratochvil 10 | 11 | R = [cos(alpha) -sin(alpha) 0; ... 12 | sin(alpha) cos(alpha) 0; ... 13 | 0 0 1]; 14 | 15 | % this just cleans up little floating point errors around 0 16 | % so that things look nicer in the display 17 | if exist('roundn'), 18 | R = roundn(R, -15); 19 | end 20 | 21 | end -------------------------------------------------------------------------------- /demos/3dvision/DR_Tests/DR_Test.jl: -------------------------------------------------------------------------------- 1 | 2 | using Debug 3 | 4 | using PyCall 5 | @pyimport numpy as np 6 | @pyimport matplotlib.pyplot as plt 7 | @pyimport opendr.util_tests as util_tests 8 | @pyimport chumpy as ch 9 | @pyimport opendr.simple as odr 10 | @pyimport numpy.random as npr 11 | @pyimport scipy.misc as scpy 12 | 13 | include("../../../engine/picture.jl") 14 | 15 | @debug begin 16 | 17 | 18 | function load_observation(rendering, A) 19 | observed = rendering["r"] 20 | tr =pyeval("translation.r + toffset",translation=translation,toffset=[0.1,0,0]) 21 | rot = pyeval("rotation.r + roffset", rotation=rotation, roffset=rand(3)*2) 22 | # set!(A["components"], 1, 0) 23 | return observed 24 | end 25 | 26 | 27 | function render(m, V, w, h) 28 | A = odr.SphericalHarmonics(vn=odr.VertNormals(v=V, f= m["f"]), 29 | components=[3.,2.,0.,0.,0.,0.,0.,0.,0.], 30 | light_color=ch.ones(3)) 31 | 32 | U = odr.ProjectPoints(v=V, f=[300,300.], c=[w/2.,h/2.], k=ch.zeros(5), 33 | t=ch.zeros(3), rt=ch.zeros(3)) 34 | 35 | f = odr.TexturedRenderer(vc=A, camera=U, f=m["f"], 36 | bgcolor=[0.,0.,0.],texture_image=m["texture_image"], 37 | vt=m["vt"], ft=m["ft"], 38 | frustum=pyeval("{\"width\":w, \"height\":h,\"near\":1,\"far\":20}",w=w,h=h)) 39 | return f, A 40 | end 41 | 42 | function gpr_difference(rendering, observed) 43 | # # Create the energy 44 | difference = pyeval("rendering - observed",rendering=rendering,observed=observed) 45 | GPR = pyeval("gpr(difference,n_levels=6)",npsum=np.sum,gpr=odr.gaussian_pyramid,difference=difference) 46 | return GPR 47 | end 48 | 49 | ################################# GLOBAL ################################# 50 | w, h = 320, 240 51 | 52 | m=util_tests.get_earthmesh(trans=ch.array([0,0,4]), rotation=ch.zeros(3)) 53 | V=ch.array(m["v"]) 54 | 55 | rendering, A = render(m,V,w,h) 56 | 57 | # Parameterize the vertices 58 | TR = [0,0,0]#rand(Uniform(-3,3),1,3)# 59 | ROT = [0,0,0] 60 | 61 | translation = ch.array(TR) 62 | rotation = ch.array(ROT) 63 | 64 | #rendering["v"]=pyeval("translation + V.dot(Rodrigues(rotation))",translation=translation, rotation=rotation,V=V,Rodrigues=odr.Rodrigues) 65 | 66 | #this is great! never use above i.e. pyeval as it will always create new arrays and thus create memory leaks 67 | chumpy_lib = pyimport(:chumpy) 68 | rendering["v"]=pycall(V["dot"], PyAny, odr.Rodrigues(rotation)) 69 | rendering["v"]=pycall(chumpy_lib["add"],PyAny,rendering["v"],translation) 70 | 71 | # observed = load_observation(rendering, A) 72 | @pyimport scipy.misc as scpy 73 | observed = scpy.imread("obs.png") 74 | 75 | 76 | #works 77 | # E = pyeval("npsum(gpr(difference,n_levels=6).r)",npsum=np.sum,gpr=odr.gaussian_pyramid,difference=difference) 78 | # E = E/(w*h) 79 | # println("E:", E) 80 | 81 | # GPR = gpr_difference(rendering, observed) 82 | 83 | # println("GPR:", GPR) 84 | 85 | 86 | # println("Begin Optimization") 87 | # # ch.minimize({'energy'=> E}, x0=[translation], callback=lambda _ : cb(difference)) 88 | # ch.minimize({"energy"=> E}, x0=[translation]) 89 | 90 | 91 | SCALING = w*h*3; 92 | df_dtranslation = pyeval("npsum(rendering.dr_wrt(translation).A,0)/(SCALING*1.0)",npsum=np.sum,rendering=rendering,translation=translation,SCALING=SCALING) 93 | 94 | df_alternative_translation = pycall(rendering["dr_wrt"], PyAny, translation) 95 | df_alternative_translation = convert(PyAny,df_alternative_translation["A"]) 96 | df_alternative_translation = sum(df_alternative_translation,1)/size(df_alternative_translation,1) 97 | 98 | 99 | difference = pycall(chumpy_lib["subtract"], PyAny, rendering, observed) 100 | gpr = odr.gaussian_pyramid(difference,n_levels=6,as_list=true)# gpr = gpr[4:] 101 | 102 | #normal pdf 103 | mu = 0; sigma = 0.001 104 | 105 | 106 | E=NaN 107 | XVALS=NaN 108 | U_Potential = NaN 109 | for id = 1:length(gpr) 110 | layer = pycall(chumpy_lib["ravel"],PyAny,gpr[id]) 111 | dlayer_pdf = layer["r"] 112 | dlayer_pdf = convert(PyAny, dlayer_pdf) 113 | dlayer_pdf = (dlayer_pdf-mu)./(sigma*sigma) 114 | layer = pycall(chumpy_lib["multiply"], PyAny, layer, dlayer_pdf) 115 | if id == 1 116 | E = layer 117 | # XVALS = layer_val 118 | else 119 | E = ch.concatenate((E, layer)) 120 | # XVALS = ch.concatenate((XVALS,layer_val)) 121 | end 122 | 123 | ### CORRECT ### 124 | ######### Likelihood ########## 125 | t0=pycall(chumpy_lib["ravel"],PyAny,gpr[id]) 126 | t1=pycall(chumpy_lib["subtract"],PyAny,t0,mu); 127 | t2=pycall(chumpy_lib["multiply"],PyAny,t1,t1); 128 | t3=pycall(chumpy_lib["multiply"],PyAny,t2,-1.0/(2*sigma*sigma)); 129 | t4=pycall(chumpy_lib["subtract"],PyAny,t3,log(sqrt(2*pi*sigma*sigma))); 130 | ######### Prior -- add from tape object ########## 131 | ## TODO - prior from tape in main program 132 | logl_layer=pycall(chumpy_lib["multiply"],PyAny,t4,-1); #Potential energy is negative of posterior 133 | 134 | if id == 1 135 | U_Potential = logl_layer 136 | else 137 | U_Potential = ch.concatenate((U_Potential, logl_layer)) 138 | end 139 | println(id) 140 | end 141 | grad = pycall(E["dr_wrt"],PyAny,translation) 142 | grad = convert(PyAny,grad["A"]) 143 | grad = sum(grad,1)/size(grad,1) 144 | 145 | grad_U = pycall(U_Potential["dr_wrt"],PyAny,translation); 146 | grad_U = convert(PyAny,grad_U["A"]); 147 | grad_U = sum(grad_U,1)/size(grad_U,1); 148 | @bp 149 | 150 | print("GRAD: ") 151 | for i=1:length(grad) 152 | print(grad[i]," ") 153 | end 154 | println() 155 | @bp 156 | 157 | 158 | # E=0 159 | # final_grad = NaN 160 | # for id=1:length(gpr) 161 | # sz = pycall(chumpy_lib["shape"],PyAny,gpr[id]) 162 | # E_id = pycall(chumpy_lib["ravel"],PyAny,gpr[id]) 163 | # E_id = E_id["r"] 164 | # E_id = convert(PyAny, E_id) 165 | 166 | # drt_Eid_wrt_translation = pycall(gpr[id]["dr_wrt"],PyAny,translation) 167 | # drt_Eid_wrt_translation = convert(PyAny,drt_Eid_wrt_translation["A"]) 168 | 169 | # # normal logpdf 170 | # mu = 0; sigma = 0.001 171 | # grad = drt_Eid_wrt_translation#.*(E_id-mu)/sigma^2 172 | # grad = sum(grad,1)/size(grad,1) 173 | 174 | # if id == 1 175 | # final_grad = grad 176 | # else 177 | # final_grad = final_grad + grad 178 | # end 179 | # # println(grad) 180 | # end 181 | # final_grad = final_grad / length(gpr) 182 | # println(final_grad) 183 | # @bp 184 | 185 | #E = gaussian_pyramid(difference, n_levels=6, as_list=True)[-3:] 186 | #E = ch.concatenate([e.ravel() for e in E]) 187 | 188 | scpy.imsave("rendered.png",rendering) 189 | 190 | end #debug 191 | 192 | -------------------------------------------------------------------------------- /demos/3dvision/DR_Tests/gpgp_dr_multiple_objs.jl: -------------------------------------------------------------------------------- 1 | 2 | using Debug 3 | using Distributions 4 | using PyCall 5 | @pyimport numpy as np 6 | @pyimport matplotlib.pyplot as plt 7 | @pyimport opendr.util_tests as util_tests 8 | @pyimport chumpy as ch 9 | @pyimport opendr.simple as odr 10 | @pyimport numpy.random as npr 11 | include("../../engine/sjulia.jl") 12 | 13 | 14 | 15 | ################### PROBABILISTIC CODE ############### 16 | WIDTH, HEIGHT = 320, 240 17 | OBSERVATIONS = [0.1,0.2,0.5,0.00067880843013236,0.03340098415969128,7.401553364861542e-9] 18 | 19 | function PROGRAM() 20 | LINE=Stack(Int);FUNC=Stack(Int);LOOP=Stack(Int) 21 | t1=time() 22 | 23 | dummy = Uniform(0,1) 24 | n_spheres = 4 # how many spheres we want to stack 25 | 26 | # Construct free variables (translations and rotations) 27 | translations = [ch.array([i*4., 0., 20.]) for i=1:n_spheres] 28 | rotations = [ch.zeros(3) for i=1:n_spheres] 29 | 30 | m = util_tests.get_earthmesh(trans=ch.array([0,0,0]), rotation=ch.zeros(3)) 31 | vs = pyeval("[ch.array(m.v).dot(Rodrigues(rotations[i])) + translations[i] for i in range(n_spheres)]", 32 | ch=ch,m=m,Rodrigues=odr.Rodrigues,rotations=rotations,translations=translations,n_spheres=n_spheres) 33 | # fs = [m.f + m.v.shape[0]*i for i in range(n_spheres)] 34 | 35 | # m=util_tests.get_earthmesh(trans=ch.array([0,0,4]), rotation=ch.zeros(3)) 36 | # V=ch.array(m["v"]) 37 | 38 | # A = odr.SphericalHarmonics(vn=odr.VertNormals(v=V, f= m["f"]), 39 | # components=[3.,2.,0.,0.,0.,0.,0.,0.,0.], 40 | # light_color=ch.ones(3)) 41 | 42 | # U = odr.ProjectPoints(v=V, f=[300,300.], c=[WIDTH/2.,HEIGHT/2.], k=ch.zeros(5), 43 | # t=ch.zeros(3), rt=ch.zeros(3)) 44 | 45 | # f = odr.TexturedRenderer(vc=A, camera=U, f=m["f"], 46 | # bgcolor=[0.,0.,0.],texture_image=m["texture_image"], 47 | # vt=m["vt"], ft=m["ft"], 48 | # frustum=pyeval("{\"width\":w, \"height\":h,\"near\":1,\"far\":20}",w=WIDTH,h=HEIGHT)) 49 | 50 | # # Parameterize the vertices 51 | # TR_Z = Uniform(-1,4) 52 | # TR=[0,0,TR_Z] 53 | # translation, rotation = ch.array(TR), ch.zeros(3) 54 | # f["v"]=pyeval("translation + V.dot(Rodrigues(rotation))",translation=translation, rotation=rotation,V=V,Rodrigues=odr.Rodrigues) 55 | 56 | # println(np.shape(V)) 57 | 58 | # t2=time() 59 | # println(string("FR:", t2-t1)) 60 | # println("-------------------") 61 | # return f 62 | return 0 63 | end 64 | 65 | ########### USER DIAGNOSTICS ############## 66 | # plt.ion() 67 | @debug function debug_callback(TRACE) 68 | rendered = TRACE["PROGRAM_OUTPUT"] 69 | # plt.imshow(rendered); 70 | # plt.pause(0.0001) 71 | end 72 | 73 | 74 | infer(debug_callback,3) 75 | # plt.show(block=true) #need this if using plt for diagnostics 76 | 77 | 78 | -------------------------------------------------------------------------------- /demos/3dvision/DR_Tests/gpgp_dr_test1.jl: -------------------------------------------------------------------------------- 1 | 2 | 3 | using Debug 4 | using Distributions 5 | using PyCall 6 | @pyimport numpy as np 7 | @pyimport matplotlib.pyplot as plt 8 | @pyimport opendr.util_tests as util_tests 9 | @pyimport chumpy as ch 10 | @pyimport opendr.simple as odr 11 | @pyimport numpy.random as npr 12 | include("../../engine/sjulia.jl") 13 | 14 | 15 | 16 | ################### PROBABILISTIC CODE ############### 17 | WIDTH, HEIGHT = 320, 240 18 | OBSERVATIONS = [0.1,0.2,0.5,0.00067880843013236,0.03340098415969128,7.401553364861542e-9] 19 | 20 | function PROGRAM() 21 | LINE=Stack(Int);FUNC=Stack(Int);LOOP=Stack(Int) 22 | t1=time() 23 | 24 | m=util_tests.get_earthmesh(trans=ch.array([0,0,4]), rotation=ch.zeros(3)) 25 | V=ch.array(m["v"]) 26 | 27 | A = odr.SphericalHarmonics(vn=odr.VertNormals(v=V, f= m["f"]), 28 | components=[3.,2.,0.,0.,0.,0.,0.,0.,0.], 29 | light_color=ch.ones(3)) 30 | 31 | U = odr.ProjectPoints(v=V, f=[300,300.], c=[WIDTH/2.,HEIGHT/2.], k=ch.zeros(5), 32 | t=ch.zeros(3), rt=ch.zeros(3)) 33 | 34 | f = odr.TexturedRenderer(vc=A, camera=U, f=m["f"], 35 | bgcolor=[0.,0.,0.],texture_image=m["texture_image"], 36 | vt=m["vt"], ft=m["ft"], 37 | frustum=pyeval("{\"width\":w, \"height\":h,\"near\":1,\"far\":20}",w=WIDTH,h=HEIGHT)) 38 | 39 | # Parameterize the vertices 40 | TR_Z = Uniform(-1,4) 41 | TR=[0,0,TR_Z] 42 | translation, rotation = ch.array(TR), ch.zeros(3) 43 | f["v"]=pyeval("translation + V.dot(Rodrigues(rotation))",translation=translation, rotation=rotation,V=V,Rodrigues=odr.Rodrigues) 44 | 45 | println(np.shape(V)) 46 | # df_dtranslation = pyeval("npsum(f.dr_wrt(translation).A,0)/(w*h*1.0)",npsum=np.sum,f=f,translation=translation,w=WIDTH,h=HEIGHT) 47 | # println(df_dtranslation) 48 | 49 | # m=DiscreteUniform(2,6) 50 | # X=zeros(2*m) 51 | # for i=1:m 52 | # X[i]=Uniform(0,0.4) 53 | # end 54 | # for i=m+1:2*m 55 | # X[i]=Beta(0.1,1) 56 | # end 57 | 58 | # if length(OBSERVATIONS) != length(X) 59 | # return "OVERFLOW_ERROR" 60 | # end 61 | 62 | # #observations 63 | # for k=1:length(OBSERVATIONS) 64 | # observe(Normal(X[k],0.0005),OBSERVATIONS[k]) 65 | # end 66 | t2=time() 67 | println(string("FR:", t2-t1)) 68 | println("-------------------") 69 | return f 70 | end 71 | 72 | ########### USER DIAGNOSTICS ############## 73 | plt.ion() 74 | @debug function debug_callback(TRACE) 75 | rendered = TRACE["PROGRAM_OUTPUT"] 76 | plt.imshow(rendered); 77 | plt.pause(0.0001) 78 | end 79 | 80 | 81 | infer(debug_callback,3) 82 | plt.show(block=true) #need this if using plt for diagnostics 83 | 84 | 85 | -------------------------------------------------------------------------------- /demos/3dvision/DR_Tests/test.py: -------------------------------------------------------------------------------- 1 | from opendr.simple import * 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import pdb,scipy.misc 5 | 6 | w, h = 320, 240 7 | 8 | try: 9 | m = load_mesh('earth.obj') 10 | except: 11 | from opendr.util_tests import get_earthmesh 12 | m = get_earthmesh(trans=ch.array([0,0,15]), rotation=ch.zeros(3)) 13 | 14 | # Create V, A, U, f: geometry, brightness, camera, renderer 15 | V = ch.array(m.v) 16 | A = SphericalHarmonics(vn=VertNormals(v=V, f=m.f), 17 | components=[3.,2.,0.,0.,0.,0.,0.,0.,0.], 18 | light_color=ch.ones(3)) 19 | U = ProjectPoints(v=V, f=[300,300.], c=[w/2.,h/2.], k=ch.zeros(5), 20 | t=ch.zeros(3), rt=ch.zeros(3)) 21 | f = TexturedRenderer(vc=A, camera=U, f=m.f, bgcolor=[0.,0.,0.], 22 | texture_image=m.texture_image, vt=m.vt, ft=m.ft, 23 | frustum={'width':w, 'height':h, 'near':1,'far':20}) 24 | 25 | # Parameterize the vertices 26 | translation, rotation = ch.array([-2,-2,3]), ch.zeros(3) 27 | f.v = translation + V.dot(Rodrigues(rotation)) 28 | 29 | 30 | observed = scipy.misc.imread("../test_trans_light2.png")/255.0 31 | # observed = f.r 32 | # translation[:] = translation.r + np.random.rand(3)*.2 33 | # rotation[:] = rotation.r + np.random.rand(3)*.2 34 | # A.components[1:] = 0 35 | 36 | # Create the energy 37 | difference = f - observed 38 | E = gaussian_pyramid(difference, n_levels=6, as_list=True)#[-3:] 39 | 40 | E = ch.concatenate([e.ravel() for e in E]) 41 | 42 | 43 | print np.sum(E.dr_wrt(translation).A,0)/len(E) 44 | 45 | 46 | plt.ion() 47 | global cb 48 | global difference 49 | global plt 50 | 51 | def cb(_): 52 | plt.imshow(np.abs(difference.r)) 53 | plt.title('Absolute difference') 54 | plt.draw() 55 | 56 | pdb.set_trace() 57 | 58 | # Minimize the energy 59 | light_parms = A.components 60 | # print 'OPTIMIZING TRANSLATION' 61 | # ch.minimize({'energy': E}, x0=[translation], callback=lambda _ : cb(difference)) 62 | 63 | # print 'OPTIMIZING TRANSLATION, ROTATION, AND LIGHT PARMS (coarse)' 64 | # ch.minimize({'energy': E}, x0=[translation, rotation, light_parms], callback=cb) 65 | 66 | print 'OPTIMIZING TRANSLATION, ROTATION, AND LIGHT PARMS (refined)' 67 | E = gaussian_pyramid(difference, n_levels=6, normalization='size') 68 | ch.minimize({'energy': E}, x0=[translation, rotation, light_parms], callback=cb) 69 | 70 | 71 | -------------------------------------------------------------------------------- /demos/3dvision/human_pose/HumanKTH.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrkulk/MIT-Picture/57bc1fd87895a5f376d10e83ef8c816657feddf1/demos/3dvision/human_pose/HumanKTH.blend -------------------------------------------------------------------------------- /demos/3dvision/human_pose/body_simulator_client.py: -------------------------------------------------------------------------------- 1 | #Human Program Simulator 2 | import socket,json,pdb,time 3 | import numpy as np 4 | 5 | class BodySimulatorClient: 6 | 7 | def __init__(self,port): 8 | self.HOST = 'localhost' # The remote host 9 | self.PORT = port#50014 # The same port as used by the server 10 | 11 | 12 | def execute(self,data): 13 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 14 | self.sock.connect((self.HOST, self.PORT)) 15 | self.sock.send(data) 16 | recdata = self.sock.recv(10240) 17 | recdata = recdata.decode("utf-8") 18 | recdata = json.loads(recdata) 19 | self.sock.close() 20 | return recdata 21 | 22 | def test(self): 23 | if False: 24 | data=json.dumps({'cmd':'getBoneNames'}) 25 | bones = self.execute(data) 26 | print '[getBoneNames]: ' 27 | print bones 28 | print '------------------------' 29 | 30 | if False: 31 | data=json.dumps({'cmd':'captureViewport'}) 32 | ret = self.execute(data) 33 | print '[captureViewport]: ', ret 34 | print '------------------------' 35 | 36 | 37 | if True: 38 | data=json.dumps({'cmd':'getBoneRotationEuler','name':'MASTER','id':0}) 39 | ret = self.execute(data) 40 | print ret 41 | 42 | # boneid = bones.index('hip') 43 | # data=json.dumps({'cmd':'getBoneRotationEuler','name':'hip','id':boneid}) 44 | # rot = self.execute(data) 45 | # rot = np.array(rot) 46 | # print rot 47 | 48 | # boneid = bones.index('LEGS') 49 | # data=json.dumps({'cmd':'setBoneLocation','name':'LEGS','id':boneid,'M':[0,1,0]}) 50 | # ret = self.execute(data) 51 | 52 | if __name__ == "__main__": 53 | simclient = BodySimulatorClient() 54 | simclient.test() 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /demos/3dvision/human_pose/body_simulator_server.py: -------------------------------------------------------------------------------- 1 | #Human Program Simulator 2 | import bpy 3 | import socket,json,select 4 | import sys 5 | import math,time,pdb 6 | 7 | class BodySimulatorServer: 8 | 9 | def __init__(self): 10 | #self.rootdir='/Users/tejas/Documents/MIT/UAI2014/src/tmp/' 11 | #self.rootdir='/Users/tejas/Documents/MIT/UAI2014/src/tmp/'+str(time.time())+'/' 12 | self.rootdir='tmp/'+str(time.time())+'/' 13 | self.rig=bpy.data.objects['rig'] 14 | self.pose=self.rig.pose 15 | self.bones = self.pose.bones 16 | self.capture_cnt = 0 17 | self.HOST='' 18 | self.PORT=5000#int(sys.argv[4])#50014 19 | self.CONNECTION_LIST=[] 20 | self.connect() 21 | 22 | 23 | def connect(self): 24 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 25 | self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 26 | self.sock.bind((self.HOST, self.PORT)) 27 | self.sock.listen(5) 28 | #self.CONNECTION_LIST.append(self.sock) 29 | 30 | def getBoneNames(self): 31 | return [s.name for s in self.bones] 32 | 33 | def getBoneRotationEuler(self,name,id): 34 | # if self.bones[id].name != name: 35 | # print ('[Error: Bone name does not match name in Blender!]') 36 | # raise 37 | return list(self.bones[id].rotation_euler) 38 | 39 | def setBoneRotationEuler(self,name,_id,M): 40 | print('setBoneRotationEuler:', M) 41 | # if self.bones[_id].name != name: 42 | # print ('[Error: Bone name does not match name in Blender!]') 43 | # return -1 44 | self.bones[_id].rotation_mode = 'XYZ' 45 | 46 | 47 | if M[0] != 'None' and abs(M[0] - self.bones[_id].rotation_euler[0])>0.02: 48 | self.bones[_id].rotation_euler[0] = math.radians(M[0]) 49 | if M[1] != 'None' and abs(M[1] - self.bones[_id].rotation_euler[1])>0.02: 50 | self.bones[_id].rotation_euler[1] = math.radians(M[1]) 51 | if M[2] != 'None' and abs(M[2] - self.bones[_id].rotation_euler[2])>0.02: 52 | self.bones[_id].rotation_euler[2] = math.radians(M[2]) 53 | 54 | # store_X = self.bones[_id].rotation_euler[0] 55 | # store_Y = self.bones[_id].rotation_euler[1] 56 | # store_Z = self.bones[_id].rotation_euler[2] 57 | 58 | # self.bones[_id].rotation_euler.rotate_axis('X',-store_X) 59 | # self.bones[_id].rotation_euler.rotate_axis('Y',-store_Y) 60 | # self.bones[_id].rotation_euler.rotate_axis('Z',-store_Z) 61 | 62 | # if M[0] != 'None': 63 | # store_X = math.radians(M[0]) 64 | # if M[1] != 'None': 65 | # store_Y = math.radians(M[1]) 66 | # if M[2] != 'None': 67 | # store_Z = math.radians(M[2]) 68 | 69 | # self.bones[_id].rotation_euler.rotate_axis('X',store_X) 70 | # self.bones[_id].rotation_euler.rotate_axis('Y',store_Y) 71 | # self.bones[_id].rotation_euler.rotate_axis('Z',store_Z) 72 | 73 | fname=1#self.captureViewport() 74 | print('[END] setBoneRotationEuler:', M) 75 | return fname 76 | 77 | 78 | def setBoneLocation(self,name,id,M): 79 | print('setBoneLocation') 80 | # if self.bones[id].name != name: 81 | # print ('[Error: Bone name does not match name in Blender!]') 82 | # return -1 83 | if M[0] != 'None' and M[0] != self.bones[id].location[0]: 84 | self.bones[id].location[0]=M[0] 85 | if M[1] != 'None' and M[1] != self.bones[id].location[1]: 86 | self.bones[id].location[1]=M[1] 87 | if M[2] != 'None' and M[2] != self.bones[id].location[2]: 88 | self.bones[id].location[2]=M[2] 89 | fname=1#self.captureViewport() 90 | return fname 91 | 92 | def setGlobalAffine(self,name,id,M): 93 | print('setGlobalAffine:', M) 94 | if M[0] != 'None' and M[0] != self.rig.scale[0]: 95 | self.rig.scale=(M[0],M[0],M[0]) 96 | 97 | 98 | if M[1] != 'None' and abs(M[1] - self.bones[id].rotation_euler[0])>0.02: 99 | self.rig.rotation_euler[0] = math.radians(M[1]) 100 | if M[2] != 'None' and abs(M[2] - self.bones[id].rotation_euler[1])>0.02: 101 | self.rig.rotation_euler[1] = math.radians(M[2]) 102 | if M[3] != 'None' and abs(M[3] - self.bones[id].rotation_euler[2])>0.02: 103 | self.rig.rotation_euler[2] = math.radians(M[3]) 104 | 105 | # store_X = self.rig.rotation_euler[0] 106 | # store_Y = self.rig.rotation_euler[1] 107 | # store_Z = self.rig.rotation_euler[2] 108 | 109 | # self.rig.rotation_euler.rotate_axis('X',-store_X) 110 | # self.rig.rotation_euler.rotate_axis('Y',-store_Y) 111 | # self.rig.rotation_euler.rotate_axis('Z',-store_Z) 112 | 113 | # if M[1] != 'None': 114 | # store_X = math.radians(M[1]) 115 | # if M[2] != 'None': 116 | # store_Y = math.radians(M[2]) 117 | # if M[3] != 'None': 118 | # store_Z = math.radians(M[3]) 119 | 120 | # self.rig.rotation_euler.rotate_axis('X',store_X) 121 | # self.rig.rotation_euler.rotate_axis('Y',store_Y) 122 | # self.rig.rotation_euler.rotate_axis('Z',store_Z) 123 | 124 | if M[4] != 'None' and M[4] != self.bones[id].location[0]: 125 | self.rig.location[0]=M[4] 126 | if M[5] != 'None' and M[5] != self.bones[id].location[1]: 127 | self.rig.location[1]=M[5] 128 | if M[6] != 'None' and M[6] != self.bones[id].location[2]: 129 | self.rig.location[2]=M[6] 130 | 131 | fname=1#self.captureViewport() 132 | return fname 133 | 134 | def captureViewport(self): 135 | # bpy.data.scenes["Scene"].use_nodes=True 136 | bpy.context.scene.render.filepath=self.rootdir+str(self.capture_cnt)+'.png' #'rendered.png' 137 | bpy.ops.render.opengl( write_still=True ) 138 | self.capture_cnt+=1 139 | return bpy.context.scene.render.filepath 140 | 141 | def captureViewport_Texture(self): 142 | bpy.data.scenes["Scene"].use_nodes=False 143 | bpy.context.scene.render.filepath=self.rootdir+str(self.capture_cnt)+'_texture.png' 144 | bpy.ops.render.render( write_still=True ) 145 | self.capture_cnt+=1 146 | return bpy.context.scene.render.filepath 147 | 148 | 149 | def process(self,data): 150 | #print (data) 151 | data = json.loads(data) 152 | cmd = data['cmd'] 153 | 154 | #replacing for matlab -- should fix this later 155 | if 'M' in data: 156 | if isinstance(data['M'],list): 157 | for i in range(len(data['M'])): 158 | if data['M'][i] == -999: 159 | data['M'][i] = 'None' 160 | else: 161 | print('ERROR in process') 162 | sys.exit() 163 | 164 | ret = None 165 | if cmd == 'getBoneNames': 166 | ret = self.getBoneNames() 167 | if cmd == 'setBoneRotationEuler': 168 | ret = self.setBoneRotationEuler(data['name'],data['id'],data['M']) 169 | if cmd == 'getBoneRotationEuler': 170 | ret = self.getBoneRotationEuler(data['name'],data['id']) 171 | if cmd == 'setBoneLocation': 172 | ret = self.setBoneLocation(data['name'],data['id'],data['M']) 173 | if cmd == 'captureViewport': 174 | ret = self.captureViewport() 175 | if cmd == 'captureViewport_Texture': 176 | ret = self.captureViewport_Texture() 177 | if cmd == 'setGlobalAffine': 178 | ret = self.setGlobalAffine(data['name'],data['id'],data['M']) 179 | return json.dumps(ret) 180 | 181 | def run(self): 182 | #for i in range(100000): 183 | while True: 184 | sockfd, addr = self.sock.accept() 185 | #print ("Client (%s, %s) connected" % addr) 186 | 187 | data = sockfd.recv(10240) 188 | data = data.decode("utf-8") 189 | print(data) 190 | if len(data) > 0 and data != None: 191 | ret = self.process(data) 192 | #first send number of bytes 193 | #print(str(sys.getsizeof(ret)).encode('utf-8')) 194 | #sockfd.send(str(sys.getsizeof(ret)).encode('utf-8')) 195 | #recv OK 196 | #sockfd.recv(24) 197 | #then send actual data 198 | 199 | #REQUIRED 200 | sockfd.send(ret.encode('utf-8')) 201 | sockfd.close() 202 | 203 | 204 | if __name__ == "__main__": 205 | simserver = BodySimulatorServer() 206 | simserver.run() 207 | -------------------------------------------------------------------------------- /demos/3dvision/human_pose/config.py: -------------------------------------------------------------------------------- 1 | #contains all the blender configs for experiments 2 | 3 | from boto.utils import get_instance_metadata 4 | import numpy as np 5 | 6 | class INTERACT: 7 | def priors(self): 8 | ###### Hand ###### 9 | bones = { 10 | #FINGERS 11 | #'Controlador.003':{'r':[[-50,0],[-50,15],[None,None]]}, #thumb 12 | 'Controlador.003':{'r':[[-60,0],[None,None],[-50,15]]}, #thumb 13 | #'Controlador.003':{'r':[[-20,10],[-20,10],[None,None]]}, #thumb 14 | 15 | 'Controlador':{'r':[[None,None],[None,None],[-80,0]]}, 16 | 'Controlador.000':{'r':[[None,None],[None,None],[-80,0]]}, 17 | 'Controlador.001':{'r':[[None,None],[None,None],[-80,0]]}, 18 | 'Controlador.002':{'r':[[None,None],[None,None],[-80,0]]}, #pinky 19 | #'Bone':{'r':[[None,None],[None,None],[-25,25]]}, 20 | #compound controllers 21 | 'dedo1.009':{'r':[[-80,0],[None,None],[None,None]]}, 22 | 'dedo1.010':{'r':[[None,None],[None,None],[-120,120]]} } 23 | bones_global_scale = [0.80,1.2]#[0.7,0.9] 24 | #bones_global_translate = [[0,0.001],[0,0.001],[0,0.001]]#[[-2,2],[-2.5,2.5],[-1.5,1.5]] #[[0,0.001],[0,0.001],[0,0.001]] 25 | #bones_global_rotate = [[0,0.001],[0,0.001],[0,0.001]]#[[0,360],[0,360],[0,360]] #[[0,0.1],[0,0.1],[0,0.1]] 26 | bones_global_translate = [[-1,1],[0,1],[-2,2]]#[[-2,2],[-2.5,2.5],[-1.5,1.5]] 27 | bones_global_rotate = [[0,60],[-90,0],[-90,0]]#[[0,60],[-90,0],[-90,0]] #[[0,360],[0,360],[0,360]] 28 | 29 | ####### Object ##### 30 | lathe_global_scale = [0.4,0.7] #1.5 31 | #lathe_global_translate = [[0,0.001],[0,0.001],[0,0.001]]#[[-2,2],[-2.5,2.5],[-1.5,1.5]] #[[0,0.001],[0,0.001],[0,0.001]] 32 | #lathe_global_rotate = [[0,0.001],[0,0.001],[0,0.001]]#[[0,360],[0,360],[0,360]] #[[0,0.1],[0,0.1],[0,0.1]] 33 | lathe_global_translate = [[-1,1],[0,1],[-2,2]]#[[-2,2],[-2.5,2.5],[-1.5,1.5]] 34 | lathe_global_rotate = [[-30,30],[-30,30],[-30,30]] 35 | return bones,bones_global_scale,bones_global_translate,bones_global_rotate,lathe_global_scale,lathe_global_translate,lathe_global_rotate 36 | 37 | def delta(self,q,i): 38 | if q[i]['cmd'] == 'setBoneRotationEuler': 39 | delta = 5#1 #5 40 | if q[i]['cmd'] == 'setBoneLocation': 41 | delta = 0.05#0.01 42 | if q[i]['cmd'] == 'setGlobalAffine': 43 | if q[i]['valid'] == 0: #scale 44 | delta = 0.05 45 | if q[i]['valid'] == 1 or q[i]['valid'] == 2 or q[i]['valid'] == 3: #rotation 46 | delta = 5 #1 47 | if q[i]['valid'] == 4 or q[i]['valid'] == 5 or q[i]['valid'] == 6: #translation: 48 | delta = 0.05#0.1#translation 49 | return delta 50 | 51 | def leaps_epsilon(self, OBJ, v_indx): 52 | if OBJ[v_indx]['cmd'] == 'setBoneRotationEuler': 53 | leaps = np.random.randint(10,20)#10-15 54 | epsilon = 0.2#0.2#0.05#0.1 55 | if OBJ[v_indx]['cmd'] == 'setBoneLocation': 56 | leaps = np.random.randint(10,15)#10 57 | epsilon = 0.05 58 | if OBJ[v_indx]['cmd'] == 'setGlobalAffine': 59 | if OBJ[v_indx]['valid'] == 0: #scale 60 | leaps = np.random.randint(10,15)#10 61 | epsilon = 0.05#0.05 62 | if OBJ[v_indx]['valid'] == 1 or OBJ[v_indx]['valid'] == 2 or OBJ[v_indx]['valid'] == 3: #rotation 63 | leaps = np.random.randint(10,25)#10 64 | epsilon = 0.1#0.05#0.1 65 | if OBJ[v_indx]['valid'] == 4 or OBJ[v_indx]['valid'] == 5 or OBJ[v_indx]['valid'] == 6: #translation 66 | leaps = np.random.randint(10,25)#10 67 | epsilon = 0.02 68 | return leaps,epsilon 69 | 70 | 71 | class LATHE: 72 | def priors(self): 73 | global_scale = [[1,1.5], [1,1.5], [1,1.5]]#[1,1.5] [0.7-1.5](bottle2) #for inference 74 | global_translate = [[-1,1],[None,None],[-5, -3.0]] #for inference 75 | #global_translate = [[-1,1],[None,None],[-4.5, -3.0]] #used for nips ex bottle2 76 | global_rotate = [[0,20],[None,None],[None,None]] #for inference 77 | 78 | # global_scale = [[5,5.1], [5,5.1], [5,5.1]] #### for getting sample prior only 79 | # global_translate = [[-1,1],[None,None],[-15,-14]] #### for getting sample prior only 80 | # global_rotate = [[-30,-30],[-30,-30],[-30,-30]] #### for getting sample prior only 81 | 82 | return global_scale,global_translate,global_rotate 83 | 84 | def delta(self,q,i): 85 | if q[i]['cmd'] == 'setGlobalAffine': 86 | if q[i]['valid'] == 0: #scale 87 | delta = 0.01 88 | elif q[i]['valid'] == 3: #rotation 89 | delta = 1 90 | else: 91 | delta = 0.01#0.05 #translation 92 | return delta 93 | 94 | def leaps_epsilon(self, OBJ, v_indx): 95 | if OBJ[v_indx]['cmd'] == 'setGlobalAffine': 96 | if OBJ[v_indx]['valid'] == 0: #scale 97 | leaps = 10 98 | epsilon = 0.05#0.005 99 | if OBJ[v_indx]['valid'] == 1 or OBJ[v_indx]['valid'] == 2 or OBJ[v_indx]['valid'] == 3: #rotation 100 | leaps = 10 101 | epsilon = 0.1 102 | if OBJ[v_indx]['valid'] == 4 or OBJ[v_indx]['valid'] == 5 or OBJ[v_indx]['valid'] == 6: #translation 103 | leaps = 10 104 | epsilon = 2*0.005 105 | return leaps,epsilon 106 | 107 | 108 | #human pose 109 | class KTH: 110 | def priors(self): 111 | bones = { 112 | 'arm elbow_R':{'r':[[None,None],[None,None],[0,360]],'d':[[-1,0],[-1,1],[-1,1]]}, #, 'd':[[-1,1],[-1,1],[-1,1]]}, 113 | 'arm elbow_L':{'r':[[None,None],[None,None],[0,360]],'d':[[0,1],[-1,1],[-1,1]]}, #, 'd':[[-1,1],[-1,1],[-1,1]]}, 114 | 'hip':{'d':[[None,None],[None,None],[-0.35,0]]}, 115 | 'heel_L':{ 'd':[[-0.1,0.45],[0,0.15],[-0.2,0.2]]}, 116 | 'heel_R':{'d':[[-0.45,0.1],[0,0.15],[-0.2,0.2]]} 117 | } 118 | global_scale = [0.95, 1.00] 119 | global_translate = [[-1,1],[None,None],[0, 0.5]] 120 | global_rotate = [[None,None],[None,None],[-1,1]]#[[None,None],[None,None],[-60,60]] 121 | return bones,global_scale,global_translate,global_rotate 122 | 123 | def delta(self,q,i): 124 | if q[i]['cmd'] == 'setBoneRotationEuler': 125 | delta = 1 #5 126 | if q[i]['cmd'] == 'setBoneLocation': 127 | delta = 0.05#0.01 128 | if q[i]['cmd'] == 'setGlobalAffine': 129 | if q[i]['valid'] == 0: #scale 130 | delta = 0.01 131 | elif q[i]['valid'] == 3: #rotation 132 | delta = 1 133 | else: 134 | delta = 0.01#0.05 #translation 135 | return delta 136 | 137 | def leaps_epsilon(self, OBJ, v_indx): 138 | if OBJ[v_indx]['cmd'] == 'setBoneRotationEuler': 139 | leaps = 10 140 | epsilon = 0.1#0.05 141 | if OBJ[v_indx]['cmd'] == 'setBoneLocation': 142 | leaps = 10 143 | epsilon = 0.05 144 | if OBJ[v_indx]['cmd'] == 'setGlobalAffine': 145 | if OBJ[v_indx]['valid'] == 0: #scale 146 | leaps = 10 147 | epsilon = 0.05#0.005 148 | if OBJ[v_indx]['valid'] == 3: #rotation 149 | leaps = 1 150 | epsilon = 0.5 151 | if OBJ[v_indx]['valid'] == 4 or OBJ[v_indx]['valid'] == 5 or OBJ[v_indx]['valid'] == 6: #translation 152 | leaps = 10 153 | epsilon = 2*0.005 154 | return leaps,epsilon 155 | 156 | 157 | 158 | 159 | 160 | #human pose 161 | class Sports: 162 | def priors(self): 163 | #ec2 = get_instance_metadata() 164 | normal = False 165 | 166 | if normal: 167 | bones = { 168 | 'arm elbow_R':{'r':[[0,360],[0,360],[0,360]],'d':[[-3,0],[-3,3],[-3,3]]}, 169 | 'arm elbow_L':{'r':[[0,360],[0,360],[0,360]],'d':[[0,3],[-3,3],[-3,3]]}, 170 | 'hip':{'d':[[None,None],[None,None],[None,None]]}, 171 | 'heel_L':{ 'd':[[-0.1,2],[0,0.15],[-0.2,0.2]]}, 172 | 'heel_R':{'d':[[-2,0.1],[0,0.15],[-0.2,0.2]]} 173 | } 174 | if False:#len(ec2.keys()) > 0: 175 | print "Running on EC2" 176 | global_scale = [2.50,2.70] 177 | #raise Exception 178 | else: 179 | print "Not running on EC2" 180 | global_scale = [1.50,1.80]#[1.4,1.85] 181 | 182 | global_translate = [[-0.2,0.2],[None,None],[0.15,0.5]] 183 | global_rotate = [[None,None],[None,None],[-30,30]]#[[None,None],[None,None],[-60,60]] 184 | 185 | else: 186 | #proposal exp 187 | bones = { 188 | 'arm elbow_R':{'r':[[0,360],[0,360],[0,360]],'d':[[-3,0],[-3,3],[-3,3]]}, 189 | 'arm elbow_L':{'r':[[0,360],[0,360],[0,360]],'d':[[0,3],[-3,3],[-3,3]]}, 190 | 'hip':{'d':[[None,None],[None,None],[None,None]]}, 191 | 'heel_L':{ 'd':[[-0.1,0.2],[0,0.3],[-0.2,0.8]]}, 192 | 'heel_R':{'d':[[-0.2,0.1],[0,0.3],[-0.2,0.8]]} 193 | } 194 | global_scale = [1.50,1.80] 195 | global_translate = [[-0.2,0.2],[None,None],[0.15,0.5]]# proposal used this -- [[-0,0.01],[None,None],[0.2,0.21]] 196 | global_rotate = [[None,None],[None,None],[-30,30]] #proposal used this-[[None,None],[None,None],[0,1]] 197 | 198 | return bones,global_scale,global_translate,global_rotate 199 | 200 | def delta(self,q,i): 201 | if q[i]['cmd'] == 'setBoneRotationEuler': 202 | delta = 1 #5 203 | if q[i]['cmd'] == 'setBoneLocation': 204 | delta = 0.1#0.05#0.01 205 | if q[i]['cmd'] == 'setGlobalAffine': 206 | if q[i]['valid'] == 0: #scale 207 | delta = 0.01 208 | elif q[i]['valid'] == 3: #rotation 209 | delta = 1 210 | else: 211 | delta = 0.01#0.05 #translation 212 | return delta 213 | 214 | def leaps_epsilon(self, OBJ, v_indx): 215 | if OBJ[v_indx]['cmd'] == 'setBoneRotationEuler': 216 | leaps = -1 217 | epsilon = 0.1#0.05 218 | if OBJ[v_indx]['cmd'] == 'setBoneLocation': 219 | leaps = 15#10 220 | epsilon = 0.1 221 | if OBJ[v_indx]['cmd'] == 'setGlobalAffine': 222 | if OBJ[v_indx]['valid'] == 0: #scale 223 | leaps = 5#10 224 | epsilon = 0.05#0.005 225 | if OBJ[v_indx]['valid'] == 3: #rotation 226 | leaps = 2 227 | epsilon = 0.1 228 | if OBJ[v_indx]['valid'] == 4 or OBJ[v_indx]['valid'] == 5 or OBJ[v_indx]['valid'] == 6: #translation 229 | leaps = 10 230 | epsilon = 2*0.005 231 | return leaps,epsilon 232 | 233 | 234 | 235 | 236 | #human pose -- constrained to sit 237 | class Sitting_Conf: 238 | def priors(self): 239 | bones = { 240 | 241 | #use this 242 | #'arm elbow_R':{'r':[[0,360],[0,360],[0,360]],'d':[[-3,3],[1,3],[-3,-0.4]]}, 243 | #'arm elbow_L':{'r':[[0,360],[0,360],[0,360]],'d':[[-3,3],[1,3],[-3,-0.4]]}, 244 | 245 | 'arm elbow_R':{'r':[[0,360],[0,360],[0,360]],'d':[[-6,6],[1,6],[-6,-0.4]]}, 246 | 'arm elbow_L':{'r':[[0,360],[0,360],[0,360]],'d':[[-6,6],[1,6],[-6,-0.4]]}, 247 | 248 | 'fot_R':{'r':[[None,None],[None,None],[-30,0]],'d':[[-0.1,0.05],[0,0.80],[None,None]]}, 249 | 'fot_L':{'r':[[None,None],[None,None],[0,30]],'d':[[-0.05,0.1],[0,0.80],[None,None]]}, 250 | 'hip':{'r':[[-10,20],[None,None],[None,None]],'d':[[None,None],[None,None],[None,None]]} 251 | #'heel_L':{ 'd':[[-0.1,2],[0,0.15],[-0.2,0.2]]}, 252 | #'heel_R':{'d':[[-2,0.1],[0,0.15],[-0.2,0.2]]} 253 | } 254 | if False:#len(ec2.keys()) > 0: 255 | print "Running on EC2" 256 | global_scale = [2.50,2.57] 257 | raise Exception 258 | else: 259 | print "Not running on EC2" 260 | global_scale = [2.75,3.0]#[1.4,1.85] 261 | 262 | global_translate = [[-0.8,0.7],[None,None],[0.10,0.90]] 263 | global_rotate = [[None,None],[None,None],[-140,140]]#[[None,None],[None,None],[-60,60]] 264 | 265 | # global_translate = [[0,1],[None,None],[0.10,0.90]] 266 | # global_rotate = [[None,None],[None,None],[-140,0]]#[[None,None],[None,None],[-60,60]] 267 | 268 | return bones,global_scale,global_translate,global_rotate 269 | 270 | 271 | -------------------------------------------------------------------------------- /demos/3dvision/human_pose/pose_program.jl: -------------------------------------------------------------------------------- 1 | # Usage: (1) run start_blender_KTH.sh (initializes blender interface) 2 | # (2) julia pose_program.jl 3 | 4 | # DESCRIPTION: Generative 3D human pose estimation 5 | # Given a single image of a human, the model will compute the most likely 3D pose. 6 | 7 | include("../../../engine/picture.jl") 8 | using Debug 9 | import JSON 10 | 11 | #Note: pyimport calls are *very* slow so you are better off using something else for heavy use case. 12 | @pyimport scipy.misc as scpy; @pyimport skimage.filter as edge 13 | @pyimport scipy.ndimage.morphology as scp_morph; @pyimport numpy as np 14 | 15 | global IMAGE_COUNTER = 0 16 | OBSERVATIONS=Dict() 17 | OBS_FNAME = "test.png" #observed image 18 | OBS_IMAGE = int(scpy.imread(OBS_FNAME,true))/255.0 19 | OBS_IMAGE = edge.canny(OBS_IMAGE, sigma=1.0) 20 | #calculate and store distance transform 21 | dist_obs = pyeval("dt(npinvert(im))", npinvert=np.invert, dt=scp_morph.distance_transform_edt, im=OBS_IMAGE) 22 | OBSERVATIONS["dist_obs"] = dist_obs 23 | 24 | ################### HELPER FUNCTION ############### 25 | function arr2string(arr) 26 | str = "[" 27 | for i=1:length(arr) 28 | if arr[i] == None 29 | str = string(str,"\"", string(arr[i]),"\"") 30 | else 31 | str = string(str,string(arr[i])) 32 | end 33 | if i < length(arr) 34 | str = string(str,",") 35 | end 36 | end 37 | str = string(str,"]") 38 | return str 39 | end 40 | ## blender interface ## 41 | function render(CMDS) 42 | for i=1:length(CMDS) 43 | client = connect(5000) 44 | cmd = string("\"", CMDS[i]["cmd"] ,"\""); 45 | name = "0"; 46 | id = string(CMDS[i]["id"]) 47 | M = arr2string(CMDS[i]["M"]) 48 | msg = string("{\"cmd\":", cmd, ", \"name\": ", name, ", \"id\":", id, ", \"M\":",M,"}"); 49 | println(client,msg) 50 | ret = readline(client) 51 | close(client) 52 | end 53 | #render image 54 | client = connect(5000) 55 | println(client, "{\"cmd\" : \"captureViewport\"}") 56 | fname = JSON.parse(readline(client)) 57 | close(client) 58 | rendering = int(scpy.imread(fname))/255.0 59 | return rendering 60 | end 61 | 62 | ################### PROBABILISTIC CODE ############### 63 | function PROGRAM() 64 | LINE=Stack(Int);FUNC=Stack(Int);LOOP=Stack(Int) 65 | 66 | bone_index = {"arm_elbow_R" => 9, "arm_elbow_L" => 7, "hip" => 1, "heel_L" => 37, "heel_R" => 29}; 67 | 68 | CMDS = Dict(); cnt=1; 69 | 70 | arm_elbowR_rz = Uniform(0,360,1,1) 71 | CMDS[cnt]={"cmd"=>"setBoneRotationEuler", "name"=>0, "id"=>bone_index["arm_elbow_R"], "M"=>[None,None,arm_elbowR_rz]};cnt+=1; 72 | 73 | arm_elbowR_dx = Uniform(-1,0,1,1) 74 | arm_elbowR_dy = Uniform(-1,1,1,1) 75 | arm_elbowR_dz = Uniform(-1,1,1,1) 76 | CMDS[cnt]={"cmd"=>"setBoneLocation", "name"=>0, "id"=>bone_index["arm_elbow_R"], "M"=>[arm_elbowR_dx,arm_elbowR_dy,arm_elbowR_dz]};cnt+=1; 77 | 78 | arm_elbowL_rz = Uniform(0,360,1,1) 79 | CMDS[cnt]={"cmd"=>"setBoneRotationEuler", "name"=>0, "id"=>bone_index["arm_elbow_L"], "M"=>[None,None,arm_elbowL_rz]};cnt+=1; 80 | 81 | arm_elbowL_dx = Uniform(0,1,1,1) 82 | arm_elbowL_dy = Uniform(-1,1,1,1) 83 | arm_elbowL_dz = Uniform(-1,1,1,1) 84 | CMDS[cnt]={"cmd"=>"setBoneLocation", "name"=>0, "id"=>bone_index["arm_elbow_L"], "M"=>[arm_elbowL_dx,arm_elbowL_dy,arm_elbowL_dz]};cnt+=1; 85 | 86 | hip_dz = Uniform(-0.35,0,1,1) 87 | CMDS[cnt]={"cmd"=>"setBoneLocation", "name"=>0, "id"=>bone_index["hip"], "M"=>[None,None,hip_dz]};cnt+=1; 88 | 89 | heel_L_dx = Uniform(-0.1,0.45,1,1) 90 | heel_L_dy = Uniform(0,0.15,1,1) 91 | heel_L_dz = Uniform(-0.2,0.2,1,1) 92 | CMDS[cnt]={"cmd"=>"setBoneLocation", "name"=>0, "id"=>bone_index["heel_L"], "M"=>[heel_L_dx,heel_L_dy,heel_L_dz]};cnt+=1; 93 | 94 | heel_R_dx = Uniform(-0.45,0.1,1,1) 95 | heel_R_dy = Uniform(0,0.15,1,1) 96 | heel_R_dz = Uniform(-0.2,0.2,1,1) 97 | CMDS[cnt]={"cmd"=>"setBoneLocation", "name"=>0, "id"=>bone_index["heel_R"], "M"=>[heel_R_dx,heel_R_dy,heel_R_dz]};cnt+=1; 98 | 99 | global_scale = Normal(0.98,0.01,1,1) 100 | global_translate_x = Uniform(-2.599287271499634-1,-2.599287271499634+1,1,1) 101 | global_translate_z = Uniform(-2.5635364055633545,-2.5635364055633545+0.5,1,1) 102 | global_rotate_z = 0#Uniform(-1,1,1,1) 103 | 104 | camera = [global_scale, None, None, global_rotate_z, global_translate_x, None, global_translate_z] 105 | CMDS[cnt]={"cmd"=>"setGlobalAffine", "name"=>0, "id"=>0, "M"=>camera};cnt+=1; 106 | 107 | rendering = render(CMDS) 108 | 109 | edgemap = pyeval("canny(rendering,1.0)", canny = edge.canny, rendering=rendering) #edgemap = edge.canny(rendering, sigma=1.0) 110 | 111 | #calculate distance transform 112 | # dist_obs = scp_morph.distance_transform_edt(~OBSERVATIONS["IMAGE"]) 113 | valid_indxs = pyeval("npwhere(edgemap>0)", npwhere=np.where,edgemap=edgemap) 114 | #valid_indxs = np.where(edgemap > 0) 115 | D = pyeval("npmultiply(dist_obs[valid_indxs], ren[valid_indxs])",npmultiply=np.multiply, dist_obs=OBSERVATIONS["dist_obs"],valid_indxs=valid_indxs, ren=edgemap) 116 | 117 | #constraint to observation 118 | observe(0,Normal(0,0.35),D) 119 | 120 | return rendering 121 | end 122 | 123 | ########### USER DIAGNOSTICS ############## 124 | function debug_callback(TRACE) 125 | global IMAGE_COUNTER 126 | println("LOGL=>", TRACE["ll"]) 127 | scpy.imsave(string("samples/sample_",string(IMAGE_COUNTER),".png",), TRACE["PROGRAM_OUTPUT"]) 128 | IMAGE_COUNTER += 1 129 | end 130 | 131 | load_program(PROGRAM) 132 | load_observations(OBSERVATIONS) 133 | init() 134 | #run basic inference by cycling through all variables 135 | infer(debug_callback,20000,"CYCLE") 136 | 137 | 138 | -------------------------------------------------------------------------------- /demos/3dvision/human_pose/start_blender_KTH.sh: -------------------------------------------------------------------------------- 1 | blender HumanKTH.blend -P body_simulator_server.py $1 & 2 | -------------------------------------------------------------------------------- /demos/3dvision/human_pose/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrkulk/MIT-Picture/57bc1fd87895a5f376d10e83ef8c816657feddf1/demos/3dvision/human_pose/test.png -------------------------------------------------------------------------------- /demos/3dvision/mesh_example/earth.jl: -------------------------------------------------------------------------------- 1 | #Given a static image, infer the 3D mesh, light and pose parameters via gradient based techniques (or sampling) 2 | 3 | include("../../../engine/picture.jl") 4 | 5 | using Debug 6 | @pyimport scipy.misc as scpy 7 | @pyimport scipy.misc as scpy 8 | @pyimport skimage.filter as edge 9 | @pyimport scipy.ndimage.morphology as scp_morph 10 | @pyimport numpy as np 11 | @pyimport pickle as pkl 12 | 13 | 14 | global DATA_STORAGE = Dict() 15 | DATA_STORAGE["logl"] = [] 16 | DATA_STORAGE["time"] = [] 17 | 18 | ################### PROBABILISTIC CODE ############### 19 | function render_init(w, h, TR_ch, ROT_ch) 20 | m=util_tests.get_earthmesh(trans=ch.array([0,0,15]), rotation=ch.zeros(3)) 21 | V=ch.array(m["v"]) 22 | 23 | A = odr.SphericalHarmonics(vn=odr.VertNormals(v=V, f= m["f"]), 24 | components=[3.,2.,0.,0.,0.,0.,0.,0.,0.], 25 | light_color=ch.ones(3)) 26 | 27 | U = odr.ProjectPoints(v=V, f=[300,300.], c=[w/2.,h/2.], k=ch.zeros(5), 28 | t=ch.zeros(3), rt=ch.zeros(3)) 29 | 30 | ren = odr.TexturedRenderer(vc=A, camera=U, f=m["f"], 31 | bgcolor=[0.,0.,0.],texture_image=m["texture_image"], 32 | vt=m["vt"], ft=m["ft"], 33 | frustum=pyeval("{\"width\":w, \"height\":h,\"near\":1,\"far\":20}",w=w,h=h)) 34 | 35 | ren["v"]=pyeval("TR_ch + V.dot(Rodrigues(ROT_ch))",TR_ch=TR_ch, ROT_ch=ROT_ch,V=V,Rodrigues=odr.Rodrigues) 36 | 37 | return m,V,A,U,ren 38 | end 39 | 40 | @debug function render(TR_ch, ROT_ch, A_ch) 41 | 42 | #need this as for some reason, variables might be garbage collected 43 | np.shape(params.USER_DEFINED["ren"]["v"]) 44 | 45 | V = params.USER_DEFINED["V"] 46 | #old never use- params.USER_DEFINED["ren"]["v"]=pyeval("TR_ch + V.dot(Rodrigues(ROT_ch))",TR_ch=TR_ch, ROT_ch=ROT_ch,V=V,Rodrigues=odr.Rodrigues) 47 | params.USER_DEFINED["ren"]["v"]=pycall(V["dot"], PyAny, odr.Rodrigues(ROT_ch)) 48 | params.USER_DEFINED["ren"]["v"]=pycall(chumpy_lib["add"],PyAny,params.USER_DEFINED["ren"]["v"],TR_ch) 49 | 50 | params.USER_DEFINED["A"]["components"] = A_ch 51 | 52 | # try params.USER_DEFINED["ren"]["v"]=pycall(chumpy_lib["add"],PyAny,params.USER_DEFINED["ren"]["v"],TR_ch) 53 | # catch 54 | # @bp 55 | # end 56 | 57 | #plt.imshow(params.USER_DEFINED["ren"]) 58 | #plt.pause(0.0001) 59 | #plt.show(block=true) 60 | 61 | return params.USER_DEFINED["ren"] 62 | end 63 | 64 | OBS_FNAME = "test_trans_light2.png" 65 | OBS_IMAGE = scpy.imread(OBS_FNAME)/255.0 66 | 67 | tmp = scpy.imread(OBS_FNAME)/255.0 68 | tmp = pycall(chumpy_lib["subtract"], PyAny, tmp, 0) 69 | 70 | OBSERVATIONS_GPR = odr.gaussian_pyramid(tmp,n_levels=6,as_list=true) 71 | OBSERVATIONS_RAVEL = deepcopy(OBSERVATIONS_GPR) 72 | for id=1:length(OBSERVATIONS_RAVEL) 73 | OBSERVATIONS_RAVEL[id] = pycall(chumpy_lib["ravel"],PyAny,OBSERVATIONS_RAVEL[id]) 74 | end 75 | 76 | OBSERVATIONS=Dict() 77 | OBSERVATIONS["GPR_RAVEL"] = OBSERVATIONS_RAVEL 78 | OBSERVATIONS["IMAGE"] = OBS_IMAGE 79 | 80 | function PROGRAM() 81 | LINE=Stack(Int);FUNC=Stack(Int);LOOP=Stack(Int) 82 | 83 | t1=time() 84 | 85 | TR = block("G1",Uniform(-4,4,1,3))#ch.array([2,-3,1]) 86 | 87 | ROT = ch.array([0,0,0])#Uniform(0,0.1,1,3) 88 | 89 | #spherical harmonics components 90 | # SPH_Components = ch.array([3.,2.,0.,0.,0.,0.,0.,0.,0.]) 91 | SPH_Components = block("A1", Normal(0,3,1,9)) 92 | # SPH_Components = ch.array(rand(Normal(0,3),1,9)[:]) 93 | rendering = render(TR, ROT, SPH_Components) 94 | 95 | observe_NoisyGaussianPyramid(rendering, params.OBSERVATIONS["GPR_RAVEL"]) 96 | 97 | return rendering 98 | end 99 | 100 | ########### USER DIAGNOSTICS ############## 101 | plt.ion() 102 | @debug function debug_callback(TRACE) 103 | global base_dir 104 | global DATA_STORAGE 105 | global START_TIME 106 | global IMAGE_CNT 107 | 108 | println("LOGL:", TRACE["ll"]) 109 | rendered = TRACE["PROGRAM_OUTPUT"] 110 | 111 | #data caching for analysis 112 | DATA_STORAGE["logl"]=[DATA_STORAGE["logl"], TRACE["ll"]] 113 | DATA_STORAGE["time"]=[DATA_STORAGE["time"], time()-START_TIME] 114 | 115 | fhandle = pyeval("open(fname,'w')",fname = string(base_dir,"ll_",".pkl")) 116 | pkl.dump(DATA_STORAGE["logl"], fhandle) 117 | 118 | fhandle = pyeval("open(fname,'w')",fname = string(base_dir,"time_",".pkl")) 119 | pkl.dump(DATA_STORAGE["time"], fhandle) 120 | 121 | # return 122 | scpy.imsave(string(base_dir,IMAGE_CNT,".png"),rendered["r"]) 123 | IMAGE_CNT+=1 124 | 125 | # diff = pycall(chumpy_lib["subtract"], PyAny, OBS_IMAGE, rendered) 126 | # plt.imshow(np.abs(diff)) 127 | # plt.pause(0.0001) 128 | end 129 | 130 | for repeat = 1:10 131 | WIDTH, HEIGHT = 320, 240 132 | m,V,A,U,ren = render_init(WIDTH,HEIGHT, ch.array([0,0,0]), ch.array([0,0,0])) 133 | params.USER_DEFINED["WIDTH"] = WIDTH 134 | params.USER_DEFINED["HEIGHT"] = HEIGHT 135 | params.USER_DEFINED["m"] = m 136 | params.USER_DEFINED["V"] = V 137 | params.USER_DEFINED["A"] = A 138 | params.USER_DEFINED["U"] = U 139 | params.USER_DEFINED["ren"] = ren 140 | 141 | 142 | global START_TIME = time() 143 | global base_dir 144 | global IMAGE_CNT = 0 145 | 146 | mhonly = true 147 | 148 | base_dir = string("samples/mh=",mhonly,"_",time(),"/") 149 | mkdir(base_dir) 150 | 151 | load_program(PROGRAM) 152 | 153 | load_observations(OBSERVATIONS) 154 | 155 | init() 156 | 157 | with_opt = false 158 | 159 | if with_opt == true 160 | GRADIENT_CALC = true 161 | for ii=1:20 162 | infer(debug_callback,20, ["G1"], "MH_SingleSite") 163 | # infer(debug_callback,5, ["G1"], "HMC") 164 | infer(debug_callback,20, ["A1"], "MH_SingleSite") 165 | # infer(debug_callback,20, ["G1","A1"], "HMC") 166 | infer(debug_callback,1, ["G1"], "LBFGS", ["res"=>[5,7]]) 167 | end 168 | 169 | for ii=1:20 170 | infer(debug_callback,5, ["G1"], "MH_SingleSite") 171 | # infer(debug_callback,5, ["G1"], "HMC") 172 | infer(debug_callback,5, ["A1"], "MH_SingleSite") 173 | # infer(debug_callback,20, ["G1","A1"], "HMC") 174 | infer(debug_callback,1, ["G1","A1"], "LBFGS", ["res"=>[1,7]]) 175 | end 176 | else 177 | if mhonly == false 178 | GRADIENT_CALC = true 179 | for ii=1:20 180 | infer(debug_callback,5, ["G1"], "MH_SingleSite") 181 | # infer(debug_callback,5, ["G1"], "HMC") 182 | infer(debug_callback,5, ["A1"], "MH_SingleSite") 183 | # infer(debug_callback,20, ["G1","A1"], "HMC") 184 | end 185 | else 186 | GRADIENT_CALC = true 187 | for ii=1:20 188 | infer(debug_callback,5, ["G1"], "MH_SingleSite") 189 | infer(debug_callback,5, ["A1"], "MH_SingleSite") 190 | if ii > 10 191 | infer(debug_callback,1, ["G1","A1"], "HMC") 192 | end 193 | end 194 | end 195 | end 196 | end 197 | 198 | plt.show(block=true) 199 | 200 | 201 | -------------------------------------------------------------------------------- /demos/3dvision/mesh_example/test_only_translation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrkulk/MIT-Picture/57bc1fd87895a5f376d10e83ef8c816657feddf1/demos/3dvision/mesh_example/test_only_translation.png -------------------------------------------------------------------------------- /demos/3dvision/mesh_example/test_trans_light1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrkulk/MIT-Picture/57bc1fd87895a5f376d10e83ef8c816657feddf1/demos/3dvision/mesh_example/test_trans_light1.png -------------------------------------------------------------------------------- /demos/3dvision/mesh_example/test_trans_light2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrkulk/MIT-Picture/57bc1fd87895a5f376d10e83ef8c816657feddf1/demos/3dvision/mesh_example/test_trans_light2.png -------------------------------------------------------------------------------- /demos/sanity/BetaUniformMixture.jl: -------------------------------------------------------------------------------- 1 | 2 | include("../../engine/picture.jl") 3 | 4 | using Debug 5 | 6 | ################### PROBABILISTIC CODE ############### 7 | global OBSERVATIONS = [2.4,23.1,13.43,0.00067880843013236,0.03340098415969128,7.401553364861542e-9] 8 | function PROGRAM() 9 | LINE=Stack(Int);FUNC=Stack(Int);LOOP=Stack(Int);MEM=NaN; BID=NaN; 10 | m=DiscreteUniform(2,6, 1,1) 11 | X=zeros(2*m) 12 | 13 | X[1:m] = block("G1",Uniform(20,30, 1, m)) 14 | 15 | # for i=1:m 16 | # X[i]=Uniform(0,0.4, 1, 1) 17 | # end 18 | 19 | for i=m+1:2*m 20 | X[i]=Beta(0.1,1, 1,1) 21 | end 22 | 23 | if length(OBSERVATIONS) != length(X) 24 | println("OVERFLOW_ERROR") 25 | return "OVERFLOW_ERROR" 26 | end 27 | 28 | #observations 29 | for k=1:length(OBSERVATIONS) 30 | observe(k, Normal(X[k],0.0005),OBSERVATIONS[k]) 31 | end 32 | 33 | return m,X 34 | end 35 | 36 | ########### USER DIAGNOSTICS ############## 37 | function debug_callback(TRACE) 38 | println("------------------ACCEPTED------------------------") 39 | print("OBS:") 40 | for jj=1:length(OBSERVATIONS) 41 | @printf("%2f,", OBSERVATIONS[jj]) 42 | end 43 | println() 44 | print("INF:") 45 | for jj=1:length(TRACE["PROGRAM_OUTPUT"][2]) 46 | @printf("%2f,", TRACE["PROGRAM_OUTPUT"][2][jj]) 47 | end 48 | println("\nLOGL:", TRACE["ll"]) 49 | println() 50 | end 51 | 52 | 53 | trc = trace(PROGRAM,[]) 54 | infer(debug_callback,1000,"CYCLE") 55 | 56 | # infer(debug_callback,1000,"G1") 57 | # infer(debug_callback,1000) 58 | -------------------------------------------------------------------------------- /demos/sanity/GMM.jl: -------------------------------------------------------------------------------- 1 | 2 | 3 | include("../../engine/picture.jl") 4 | global OBSERVATIONS = [-0.44807272117069014,10.456668142334193,8.011686050745572,-0.08233999332655699,8.578355846740143,10.728612655587856,5.197286833474093,5.113429320476434,8.615976298430253,5.081957337632471] 5 | global OBS_CID = [1,2,2,1,2,2,3,3,2,3] 6 | ############# PROBABILISTIC CODE ########### 7 | function PROGRAM() 8 | LINE=Stack(Int);FUNC=Stack(Int);LOOP=Stack(Int);var=NaN 9 | 10 | mixing_coeff = [0.5,0.3,0.2] 11 | mu = [0, 10, 5] 12 | stds = [1,2,0.1] 13 | DIM = 10 14 | X=zeros(DIM) 15 | CID=zeros(Int,DIM) 16 | for k=1:DIM 17 | tmp = Multinomial(1,mixing_coeff,1,1) 18 | cid = findin(tmp,1)[1] 19 | X[k] = Normal(mu[cid], stds[cid],1,1) 20 | CID[k]=cid 21 | end 22 | if length(OBSERVATIONS) != length(X) 23 | return "OVERFLOW_ERROR" 24 | end 25 | for k=1:length(DIM) 26 | observe(k,Normal(mu[CID[k]],stds[CID[k]]),OBSERVATIONS[k]) 27 | end 28 | 29 | return X,CID 30 | end 31 | 32 | ########### USER DIAGNOSTICS ############## 33 | function debug_callback(TRACE) 34 | println("------------------ACCEPTED------------------------") 35 | print("OBS:") 36 | for jj=1:length(OBSERVATIONS) 37 | @printf("%2f,", OBSERVATIONS[jj]) 38 | end 39 | println() 40 | print("INF:") 41 | for jj=1:length(TRACE["PROGRAM_OUTPUT"][1]) 42 | @printf("%2f,", TRACE["PROGRAM_OUTPUT"][1][jj]) 43 | end 44 | println() 45 | end 46 | 47 | trc = trace(PROGRAM,[]) 48 | infer(debug_callback,1000, "CYCLE") 49 | 50 | 51 | -------------------------------------------------------------------------------- /engine/codetransform.jl: -------------------------------------------------------------------------------- 1 | 2 | using Debug 3 | 4 | function buildInsideLoopList(expressions) 5 | ind=false 6 | for i=1:length(expressions) 7 | sexpr = string(expressions[i]) 8 | #println(sexpr) 9 | if ind == true && isLoopBegin(sexpr) == false && isLine(sexpr) == true 10 | params.LOOP_LIST[sexpr] = 1 11 | end 12 | if isLoopBegin(sexpr) == true 13 | ind = true 14 | end 15 | if i>2 && isLoopExit(string(expressions[i-1]), string(expressions[i-2])) == true 16 | ind = false 17 | end 18 | end 19 | 20 | return params.LOOP_LIST 21 | end 22 | 23 | function isLoopBegin(strexpr) 24 | if search(strexpr,":(unless top") != 0:-1 25 | return true 26 | else 27 | return false 28 | end 29 | end 30 | 31 | function isLoopExit(prev_strexpr, prev_prev_strexpr) 32 | if search(prev_strexpr,"goto") != 0:-1 && isLoopBegin(prev_strexpr) == false && search(prev_prev_strexpr,"top(convert)") != 0:-1 33 | return true 34 | else 35 | return false 36 | end 37 | end 38 | 39 | function isInsideLoop(strexpr) 40 | return haskey(params.LOOP_LIST, strexpr) 41 | end 42 | 43 | function isLine(strexpr) 44 | if search(strexpr,"# line") != 0:-1 && isInsideLoop(strexpr) == false 45 | return true 46 | else 47 | return false 48 | end 49 | end 50 | 51 | function transform_code(AST) 52 | 53 | expressions = deepcopy(AST[1].args[3].args) 54 | 55 | TMP = {} 56 | for i=1:7 57 | push!(TMP,expressions[i]) 58 | end 59 | push!(TMP,:(push!(LINE,0))) 60 | 61 | #all expressions that lie inside for loops 62 | params.LOOP_LIST = buildInsideLoopList(expressions) 63 | 64 | for i=8:length(expressions) 65 | loopExit = false 66 | str_exp = string(expressions[i]) 67 | isRC = check_if_random_choice(expressions[i]) 68 | # println(string(isRC),"_", expressions[i]) 69 | if isLoopBegin(str_exp) == true 70 | tmp_store = pop!(TMP)#we want to append before the goto address 71 | push!(TMP,:(push!(LOOP,0))) 72 | push!(TMP,tmp_store) 73 | elseif isLoopExit(string(expressions[i-1]), string(expressions[i-2])) == true 74 | loopExit = true 75 | elseif isInsideLoop(str_exp) == true 76 | push!(TMP,:(var = pop!(LOOP)+1)) 77 | push!(TMP,:(push!(LOOP,var))) 78 | elseif isLine(str_exp) == true 79 | push!(TMP,:(var = pop!(LINE)+1)) 80 | push!(TMP,:(push!(LINE,var))) 81 | end 82 | 83 | if isRC == true 84 | if search(str_exp,"memoize") != 0:-1 || search(str_exp,"block") != 0:-1 85 | #memoize or block 86 | tmp_exp = deepcopy(expressions[i]) 87 | 88 | mem_or_block_var = tmp_exp.args[2].args[2] 89 | rv_exp = tmp_exp.args[2].args[3] 90 | tmp_exp = deepcopy(expressions[i]) 91 | tmp_exp.args[2] = rv_exp 92 | push!(tmp_exp.args[2].args,:LINE) 93 | push!(tmp_exp.args[2].args,:FUNC) 94 | push!(tmp_exp.args[2].args,:LOOP) 95 | push!(tmp_exp.args[2].args,:MEM) 96 | push!(tmp_exp.args[2].args,:BID) 97 | tmp_str = string(tmp_exp.args[2].args[1]) 98 | tmp_str = string(tmp_str, "_DB") 99 | tmp_exp.args[2].args[1] = symbol(tmp_str) 100 | mem_expr = :(MEM=NaN) 101 | block_expr = :(BID="") 102 | if search(str_exp,"memoize") != 0:-1 103 | mem_expr.args[2]=mem_or_block_var 104 | elseif search(str_exp,"block") != 0:-1 105 | block_expr.args[2]=mem_or_block_var 106 | end 107 | push!(TMP,mem_expr) 108 | push!(TMP,block_expr) 109 | push!(TMP,tmp_exp) 110 | 111 | else 112 | tmp_exp = deepcopy(expressions[i]) 113 | #push!(tmp_exp.args[2].args,:CURRENT_TRACE) 114 | push!(tmp_exp.args[2].args,:LINE) 115 | push!(tmp_exp.args[2].args,:FUNC) 116 | push!(tmp_exp.args[2].args,:LOOP) 117 | push!(tmp_exp.args[2].args,:MEM) 118 | push!(tmp_exp.args[2].args,:BID) 119 | tmp_str = string(tmp_exp.args[2].args[1]) 120 | tmp_str = string(tmp_str, "_DB") 121 | tmp_exp.args[2].args[1] = symbol(tmp_str) 122 | push!(TMP,:(MEM=NaN)) 123 | push!(TMP,:(BID="")) 124 | push!(TMP,tmp_exp) 125 | end 126 | else 127 | push!(TMP,expressions[i]) 128 | #we need to add this after loop closing for correctness 129 | if loopExit == true 130 | push!(TMP,:(pop!(LOOP))) 131 | end 132 | end 133 | end 134 | # println("========== Transformed Code =========") 135 | # for i=1:length(TMP) 136 | # println(TMP[i]) 137 | # end 138 | 139 | TAST = deepcopy(AST) 140 | TAST[1].args[3].args=TMP 141 | return TAST 142 | end 143 | -------------------------------------------------------------------------------- /engine/elliptical.jl: -------------------------------------------------------------------------------- 1 | # Reference: 2 | # % Elliptical slice sampling 3 | # % Iain Murray, Ryan Prescott Adams and David J.C. MacKay. 4 | # % The Proceedings of the 13th International Conference on Artificial 5 | # % Intelligence and Statistics (AISTATS), JMLR W&CP 9:541-548, 2010. 6 | 7 | using Debug 8 | using Distributions 9 | 10 | function ELLIPTICAL(names, debug_callback) 11 | params.CURRENT_TRACE = deepcopy(params.TRACE) 12 | trace_update(params.TAST) 13 | cur_log_like = params.CURRENT_TRACE["ll"] 14 | xx = params.CURRENT_TRACE["RC"][names[1]]["X"] 15 | # print("---------\n") 16 | # print("PREV:", cur_log_like, "\n") 17 | angle_range = 0; #parameter: currently explores whole ellipse 18 | 19 | prior = chol(eye(length(xx))); 20 | D = length(xx) 21 | nu = reshape(prior'*rand(Normal(0,1),(D, 1)), size(xx)) 22 | 23 | hh = log(rand()) + cur_log_like; 24 | 25 | #Set up a bracket of angles and pick a first proposal. 26 | # "phi = (theta'-theta)" is a change in angle. 27 | phi = rand()*2*pi; 28 | phi_min = phi - 2*pi; 29 | phi_max = phi; 30 | 31 | ERP = params.CURRENT_TRACE["RC"][names[1]]["ERP"] 32 | theta_c = {0,1} #hack -- should be passed in 33 | ERP_OBJ = ERP_CREATE(ERP, theta_c) 34 | 35 | #Slice sampling loop 36 | while true 37 | # Compute xx for proposed angle difference and check if it's on the slice 38 | xx_prop = xx*cos(phi) + nu*sin(phi); 39 | 40 | # cur_log_like = log_like_fn(xx_prop, varargin{:}); 41 | params.CURRENT_TRACE["RC"][names[1]]["X"] = xx_prop 42 | trace_update(params.TAST) 43 | cur_log_like = params.CURRENT_TRACE["ll"] 44 | 45 | if cur_log_like > hh 46 | #New point is on slice, ** EXIT LOOP ** 47 | # print("cur:", cur_log_like, " hh:", hh, "\n") 48 | logl = logscore_erp(ERP_OBJ,params.CURRENT_TRACE["RC"][names[1]]["X"], theta_c) 49 | params.CURRENT_TRACE["RC"][names[1]]["logl"] = logl 50 | 51 | params.TRACE = deepcopy(params.CURRENT_TRACE) 52 | debug_callback(params.CURRENT_TRACE) 53 | return params.CURRENT_TRACE 54 | end 55 | print(" Shrinking Slice\n") 56 | #Shrink slice to rejected point 57 | if phi > 0 58 | phi_max = phi; 59 | elseif phi < 0 60 | phi_min = phi; 61 | else 62 | print("\n BUG DETECTED: Shrunk to current position and still not acceptable.\n") 63 | end 64 | #Propose new angle difference 65 | phi = rand()*(phi_max - phi_min) + phi_min; 66 | end 67 | end 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /engine/erps.jl: -------------------------------------------------------------------------------- 1 | 2 | using PyCall 3 | @pyimport numpy.random as npr #there's something wrong with MvNormal. No other need for numpy 4 | 5 | function observe(id, distribution, DATA) 6 | flag=false 7 | logl = 0 8 | 9 | if haskey(params.CURRENT_TRACE["observe"],id) == true 10 | if params.CURRENT_TRACE["observe"][id]["distribution"] == distribution && params.CURRENT_TRACE["observe"][id]["data"] == DATA 11 | logl = params.CURRENT_TRACE["observe"][id]["logl"] 12 | else 13 | flag = true 14 | end 15 | else 16 | flag = true 17 | end 18 | 19 | if flag 20 | logl = sum(logpdf(distribution, DATA)) 21 | params.CURRENT_TRACE["observe"][id]=Dict() 22 | params.CURRENT_TRACE["observe"][id]["logl"]=logl 23 | params.CURRENT_TRACE["observe"][id]["distribution"]=distribution 24 | params.CURRENT_TRACE["observe"][id]["data"]=DATA 25 | end 26 | params.CURRENT_TRACE["ll"] += logl 27 | end 28 | 29 | 30 | function observe_normal_cf(id, distribution, DATA) 31 | logl = 1/length(D) * sum(logpdf(distribution, DATA)) 32 | params.CURRENT_TRACE["observe"][id]=Dict() 33 | params.CURRENT_TRACE["observe"][id]["logl"]=logl 34 | params.CURRENT_TRACE["observe"][id]["distribution"]=distribution 35 | params.CURRENT_TRACE["observe"][id]["data"]=DATA 36 | params.CURRENT_TRACE["ll"] += logl 37 | end 38 | 39 | 40 | function observe_NoisyGaussianPyramid(rendering, obs_gpr) 41 | 42 | #difference = pycall(chumpy_lib["subtract"], PyAny, rendering, observed) 43 | #gpr = odr.gaussian_pyramid(difference,n_levels=6,as_list=true)# gpr = gpr[4:] 44 | 45 | gpr = odr.gaussian_pyramid(rendering,n_levels=6,as_list=true)# gpr = gpr[4:] 46 | #normal pdf 47 | mu = 0; sigma = 0.001 48 | U_Potential=NaN 49 | # XVALS=NaN 50 | for id = 1:length(gpr) 51 | ### CORRECT ### 52 | ######### Likelihood ########## 53 | t0=pycall(chumpy_lib["ravel"],PyAny,gpr[id]) 54 | #t1=pycall(chumpy_lib["subtract"],PyAny,t0,mu); 55 | t1=pycall(chumpy_lib["subtract"],PyAny,t0,obs_gpr[id]); 56 | t2=pycall(chumpy_lib["multiply"],PyAny,t1,t1); 57 | t3=pycall(chumpy_lib["multiply"],PyAny,t2,-1.0/(2*sigma*sigma)); 58 | t4=pycall(chumpy_lib["subtract"],PyAny,t3,log(sqrt(2*pi*sigma*sigma))); 59 | ######### Prior -- add from tape object ########## 60 | ## TODO - prior from tape in main program 61 | 62 | logl_layer=pycall(chumpy_lib["multiply"],PyAny,t4,-1); #Potential energy is negative of posterior 63 | 64 | if id == 1 65 | U_Potential = logl_layer 66 | else 67 | U_Potential = ch.concatenate((U_Potential, logl_layer)) 68 | end 69 | 70 | # plt.imshow(gpr[id]);plt.show(block=true) 71 | # @bp 72 | end 73 | 74 | params.CURRENT_TRACE["tape_logl"] = U_Potential 75 | params.CURRENT_TRACE["ll"] += -sum(PyJCast(U_Potential)) #negative because we want logl and U_Potential is negative log-posterior 76 | 77 | # rvs = params.CURRENT_TRACE["RC"]["G1"]["ch_object"] 78 | 79 | # grad_U = pycall(U_Potential["dr_wrt"],PyAny,rvs); 80 | # grad_U = convert(PyAny,grad_U["A"]); 81 | # grad_U = sum(grad_U,1)/size(grad_U,1); 82 | 83 | # print("GRAD: ") 84 | # for i=1:length(grad_U) 85 | # print(grad_U[i]," ") 86 | # end 87 | # println() 88 | 89 | # return grad_U 90 | end 91 | 92 | 93 | function logscore_erp(ERP, X, theta_c) 94 | if ERP == "NPR_MVN" #there's something wrong with julia's mvn 95 | return 0#sum(-0.5*X'*inv(theta_c[2])*X-0.5*log(det(theta_c[2]))-(length(X)*0.5*log(2*pi))) 96 | else 97 | logl = sum(logpdf(ERP,X)) 98 | if isinf(logl) 99 | logl = log(0) 100 | end 101 | return logl 102 | end 103 | end 104 | 105 | function ERP_CREATE(ERP, theta_c) 106 | if ERP == MvNormal #there's something wrong with julia's mvn 107 | return "NPR_MVN" 108 | else 109 | return ERP(theta_c[1],theta_c[2]) 110 | end 111 | end 112 | 113 | function sample_new_randomness(params, name,ERP, theta_c, m, n) 114 | #sample new randomness 115 | ERP_OBJ = ERP_CREATE(ERP, theta_c) 116 | 117 | is_pregenerated_externally = false 118 | 119 | #first check if we have externally generated trace for initialization 120 | if typeof(params.TRACE_EXTERN) == typeof(Dict()) 121 | if haskey(params.TRACE_EXTERN,name) 122 | #retrieve from externally initialized trace 123 | is_pregenerated_externally = true 124 | X = params.TRACE_EXTERN[name]["X"] 125 | end 126 | end 127 | 128 | if is_pregenerated_externally == false 129 | if ERP == MvNormal #there's something wrong with julia's mvn 130 | X = npr.multivariate_normal(theta_c[1],theta_c[2]) 131 | else 132 | if m*n == 1 133 | X = rand(ERP_OBJ) 134 | else 135 | X = rand(ERP_OBJ, (m, n))[:] 136 | end 137 | end 138 | end 139 | 140 | 141 | logl = logscore_erp(ERP_OBJ, X, theta_c) 142 | 143 | params.CURRENT_TRACE["RC"][name]=Dict() 144 | params.CURRENT_TRACE["RC"][name]["ERP"]=ERP 145 | params.CURRENT_TRACE["RC"][name]["size"] = [m,n] 146 | params.CURRENT_TRACE["RC"][name]["logl"]=logl 147 | params.CURRENT_TRACE["RC"][name]["X"]=X 148 | params.CURRENT_TRACE["RC"][name]["theta_c"]=theta_c 149 | params.CURRENT_TRACE["ll"]+=logl; params.CURRENT_TRACE["ll_fresh"]+=logl 150 | return X, params 151 | end 152 | 153 | 154 | function DB_RandomManager(params, name, theta_c, ERP, m, n) 155 | X=NaN 156 | 157 | if (haskey(params.CURRENT_TRACE["RC"],name) == true) && (params.CURRENT_TRACE["RC"][name]["ERP"]==ERP) 158 | X = params.CURRENT_TRACE["RC"][name]["X"] 159 | if length(X) != m*n #case for block. If size changes of the LHS array, we need to resample with new dimensions so we don't get dimension mismatch error 160 | X, params=sample_new_randomness(params,name,ERP, theta_c, m, n) 161 | else 162 | if params.CURRENT_TRACE["RC"][name]["theta_c"] == theta_c 163 | params.CURRENT_TRACE["ll"]+= params.CURRENT_TRACE["RC"][name]["logl"] 164 | else 165 | #rescore ERP with new parameters 166 | ERP_OBJ = ERP_CREATE(ERP, theta_c) 167 | logl = logscore_erp(ERP_OBJ,X, theta_c) 168 | params.CURRENT_TRACE["RC"][name]["logl"]=logl 169 | params.CURRENT_TRACE["ll"]+=logl 170 | end 171 | end 172 | else 173 | X, params=sample_new_randomness(params,name,ERP, theta_c, m, n) 174 | if GRADIENT_CALC == true 175 | params.CURRENT_TRACE["RC"][name]["ch_object"] = ch.array(X) 176 | end 177 | end 178 | params.CURRENT_TRACE["ACTIVE_K"][name]=1 179 | 180 | if GRADIENT_CALC == true 181 | if haskey(params.CURRENT_TRACE["RC"][name], "ch_object") == true 182 | #avoid creating a different ch_array. shouldn't matter functionally but creates memory leak 183 | if length(X) == 1 184 | set!(params.CURRENT_TRACE["RC"][name]["ch_object"],0,X[1]) 185 | else 186 | for ii=1:length(X) 187 | #i-1 because python starts with 0 index 188 | set!(params.CURRENT_TRACE["RC"][name]["ch_object"],ii-1,X[ii]) 189 | end 190 | end 191 | else 192 | params.CURRENT_TRACE["RC"][name]["ch_object"] = ch.array(X) 193 | end 194 | return params.CURRENT_TRACE["RC"][name]["ch_object"] 195 | else 196 | return X 197 | end 198 | end 199 | 200 | function StochasticObject(objects, instance) 201 | move_p = [0.9,0.05,0.05] #update/add/remove 202 | if length(objects) == 0 203 | max_oid = 0 204 | move = "add" 205 | else 206 | _keys=collect(keys(objects)) 207 | max_oid = maximum(_keys)+1 208 | move = NaN 209 | chosen = find(rand(Multinomial(1,move_p)))[1] 210 | if chosen == 1 211 | move="update" 212 | elseif chosen == 2 213 | move="add" 214 | else 215 | move="remove" 216 | end 217 | end 218 | if move == "update" 219 | return objects 220 | elseif move=="add" 221 | objects[max_oid]=instance 222 | params.CURRENT_TRACE["ll"]+= log(move_p[3]) - log(move_p[2]) 223 | return objects 224 | elseif move == "remove" 225 | idx = rand(1:length(objects)) 226 | chosen_key = _keys[idx] 227 | delete!(objects, chosen_key) 228 | params.CURRENT_TRACE["ll"]+= log(move_p[2]) - log(move_p[3]) 229 | return objects 230 | end 231 | end 232 | 233 | function get_name(dist,FUNC,LINE,LOOP, MEM, BID) 234 | if isnan(MEM) == false 235 | name = string(dist,"_F", length(FUNC)>0?top(FUNC):0,"_L",length(LINE)>0?top(LINE):0,"_M",string(MEM)) 236 | elseif BID != "" 237 | #name = string(dist,"_F", length(FUNC)>0?top(FUNC):0,"_L",length(LINE)>0?top(LINE):0,"_B",string(BID)) 238 | name = string(BID) 239 | else 240 | name = string(dist,"_F", length(FUNC)>0?top(FUNC):0,"_P",length(LOOP)>0?top(LOOP):0,"_L",length(LINE)>0?top(LINE):0) 241 | end 242 | return name 243 | end 244 | 245 | function PyJCast(val) 246 | if typeof(val) == PyObject 247 | val = convert(PyAny,val["r"]) 248 | if length(val) == 1 249 | val = val[1] 250 | end 251 | end 252 | return val 253 | end 254 | 255 | function Beta_DB(a,b,m,n,LINE,FUNC,LOOP,MEM, BID) 256 | a = PyJCast(a) 257 | b = PyJCast(b) 258 | name = get_name("Beta",FUNC,LINE,LOOP, MEM, BID) 259 | theta_c = {a,b} 260 | ERP = Beta 261 | ret = DB_RandomManager(params, name, theta_c, ERP, m, n) 262 | 263 | ##### gradient ##### 264 | if GRADIENT_CALC 265 | data_ch = params.CURRENT_TRACE["RC"][name]["ch_object"] 266 | ch_lpdf = pycall(chumpy_lib["multiply"],PyAny,a-1,data_ch) 267 | tmp = pycall(chumpy_lib["subtract"],PyAny,1,data_ch) 268 | tmp2 = pycall(chumpy_lib["multiply"],PyAny,b-1,tmp) 269 | params.CURRENT_TRACE["RC"][name]["ch_lpdf"] = pycall(chumpy_lib["add"],PyAny,ch_lpdf,tmp2) 270 | end 271 | 272 | return ret 273 | end 274 | 275 | function Gamma_DB(a,b,m,n,LINE,FUNC,LOOP,MEM, BID) 276 | a = PyJCast(a) 277 | b = PyJCast(b) 278 | name = get_name("Gamma",FUNC,LINE,LOOP, MEM, BID) 279 | theta_c = {a,b} 280 | ERP = Gamma 281 | ret = DB_RandomManager(params, name, theta_c, ERP, m, n) 282 | 283 | ###### gradient ###### 284 | if GRADIENT_CALC 285 | k=a; theta=b; 286 | data_ch = params.CURRENT_TRACE["RC"][name]["ch_object"] 287 | ch_lpdf = pycall(chumpy_lib["multiply"],PyAny,data_ch,k-1) 288 | tmp = pycall(chumpy_lib["divide"],PyAny, data_ch, 1.0/theta) 289 | ch_lpdf = pycall(chumpy_lib["subtract"],PyAny,ch_lpdf,tmp) 290 | params.CURRENT_TRACE["RC"][name]["ch_lpdf"] = pycall(chumpy_lib["subtract"],PyAny,ch_lpdf,k*theta) 291 | end 292 | 293 | return ret 294 | end 295 | 296 | function MvNormal_DB(a,b,m,n,LINE,FUNC,LOOP,MEM, BID) 297 | a = PyJCast(a) 298 | b = PyJCast(b) 299 | name = get_name("MvNormal",FUNC,LINE,LOOP, MEM, BID) 300 | theta_c = {a,b} 301 | ERP = MvNormal 302 | return DB_RandomManager(params, name, theta_c, ERP, m, n) 303 | end 304 | 305 | function Uniform_DB(a,b,m,n,LINE,FUNC,LOOP,MEM, BID) 306 | a = PyJCast(a) 307 | b = PyJCast(b) 308 | name = get_name("Uniform",FUNC,LINE,LOOP, MEM, BID) 309 | theta_c = {a,b} 310 | ERP = Uniform 311 | ret = DB_RandomManager(params, name, theta_c, ERP, m, n) 312 | return ret 313 | end 314 | 315 | function DiscreteUniform_DB(a,b,m,n,LINE,FUNC,LOOP,MEM, BID) 316 | a = PyJCast(a) 317 | b = PyJCast(b) 318 | name = get_name("DiscreteUniform",FUNC,LINE,LOOP, MEM, BID) 319 | theta_c = {a,b} 320 | ERP = DiscreteUniform 321 | return DB_RandomManager(params, name, theta_c, ERP, m, n) 322 | end 323 | 324 | function Multinomial_DB(a,b,m,n,LINE,FUNC,LOOP,MEM, BID) 325 | a = PyJCast(a) 326 | b = PyJCast(b) 327 | name = get_name("Multinomial",FUNC,LINE,LOOP, MEM, BID) 328 | theta_c = {a,b} 329 | ERP = Multinomial 330 | return DB_RandomManager(params, name, theta_c, ERP, m, n) 331 | end 332 | 333 | function Normal_DB(a,b,m,n,LINE,FUNC,LOOP,MEM, BID) 334 | a = PyJCast(a) 335 | b = PyJCast(b) 336 | name = get_name("Normal",FUNC,LINE,LOOP, MEM, BID) 337 | theta_c = {a,b} 338 | ERP = Normal 339 | ret = DB_RandomManager(params, name, theta_c, ERP, m, n) 340 | 341 | ###### gradient ###### 342 | if GRADIENT_CALC 343 | mu=a; sigma=b; 344 | data_ch = params.CURRENT_TRACE["RC"][name]["ch_object"] 345 | ch_lpdf = pycall(chumpy_lib["subtract"],PyAny,data_ch,mu) 346 | ch_lpdf = pycall(chumpy_lib["multiply"],PyAny,ch_lpdf, ch_lpdf) 347 | ch_lpdf=pycall(chumpy_lib["multiply"],PyAny,ch_lpdf,-1.0/(2*sigma*sigma)); 348 | params.CURRENT_TRACE["RC"][name]["ch_lpdf"]=pycall(chumpy_lib["subtract"],PyAny,ch_lpdf,log(sqrt(2*pi*sigma*sigma))); 349 | end 350 | return ret 351 | end 352 | 353 | 354 | 355 | -------------------------------------------------------------------------------- /engine/gibbs_kernel.jl: -------------------------------------------------------------------------------- 1 | 2 | 3 | using Debug 4 | using Distributions 5 | using NumericExtensions 6 | 7 | function gibbs_propose(names, debug_callback) 8 | 9 | params.CURRENT_TRACE = deepcopy(params.TRACE) 10 | 11 | xx = params.CURRENT_TRACE["RC"][names[1]]["X"] 12 | range = params.CURRENT_TRACE["RC"][names[1]]["theta_c"] 13 | 14 | logl_list = zeros(range[2]-range[1]+1) 15 | val_list = {};ind=1 16 | for val = range[1]:range[2] #enumerate 17 | params.CURRENT_TRACE["RC"][names[1]]["X"] = val 18 | trace_update(params.TAST) 19 | cur_log_like = params.CURRENT_TRACE["ll"] 20 | logl_list[ind] = cur_log_like 21 | ind+=1 22 | val_list = [val_list, val] 23 | end 24 | 25 | #sample from multinomial and choose 26 | logl_list = logl_list - logsumexp(logl_list) 27 | logl_list = exp(logl_list) 28 | pvector = rand(Multinomial(1,logl_list),1) 29 | chosen_idx = findin(pvector,1)[1] 30 | 31 | #update trace with chosen value 32 | params.CURRENT_TRACE["RC"][names[1]]["X"] = val_list[chosen_idx] 33 | trace_update(params.TAST) 34 | params.TRACE = deepcopy(params.CURRENT_TRACE) 35 | debug_callback(params.CURRENT_TRACE) 36 | return params.CURRENT_TRACE 37 | end 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /engine/hmc.jl: -------------------------------------------------------------------------------- 1 | using Debug 2 | using Distributions 3 | 4 | @debug function set_chobject(DB, name , q, q_len) 5 | DB["RC"][name]["X"] = q 6 | 7 | if q_len == 1 8 | set!(DB["RC"][name]["ch_object"],0,q) 9 | else 10 | for i=1:q_len 11 | set!(DB["RC"][name]["ch_object"],i-1,q[i]) #i-1 because python starts with 0 index 12 | end 13 | end 14 | 15 | #add new logl 16 | theta_d = DB["RC"][name]["theta_c"] 17 | erp = DB["RC"][name]["ERP"] 18 | dist = ERP_CREATE(erp, theta_d) 19 | DB["RC"][name]["logl"] = logscore_erp(dist, q, theta_d) 20 | 21 | return DB 22 | end 23 | 24 | @debug function compute_gradU_exact( name_indices, q, q_len) 25 | for name in collect(keys(name_indices)) 26 | indxs = name_indices[name] 27 | params.CURRENT_TRACE = set_chobject(params.CURRENT_TRACE, name, q[indxs], length(indxs)) 28 | end 29 | trace_update(params.TAST)#for propagation 30 | 31 | final_gradient = zeros(q_len) 32 | 33 | for name in collect(keys(name_indices)) 34 | rvs = params.CURRENT_TRACE["RC"][name]["ch_object"] 35 | grad_U = pycall(params.CURRENT_TRACE["tape_logl"]["dr_wrt"],PyAny,rvs); 36 | if typeof(grad_U) == PyObject 37 | grad_U = convert(PyAny,grad_U["A"]); 38 | end 39 | grad_U = sum(grad_U,1)/size(grad_U,1); 40 | if haskey(params.CURRENT_TRACE["RC"][name],"ch_lpdf")#params.CURRENT_TRACE["RC"][name]["ERP"] == Normal 41 | grad_P = pycall(params.CURRENT_TRACE["RC"][name]["ch_lpdf"]["dr_wrt"],PyAny,rvs); 42 | if typeof(grad_P) == PyObject 43 | grad_P = convert(PyAny,grad_P["A"]); 44 | end 45 | grad_P = sum(grad_P,1)/size(grad_P,1); 46 | grad_U += grad_P 47 | end 48 | # println("H GRAD:", grad_U) 49 | 50 | indxs = name_indices[name] 51 | final_gradient[indxs] = grad_U[:] 52 | end 53 | 54 | 55 | return final_gradient 56 | end 57 | 58 | 59 | @debug function compute_gradU_SPSA( name_indices, q, q_len) 60 | num_samples = 5 61 | stepsize = 1e-9 62 | 63 | grad_q = zeros(length(q)) 64 | 65 | cur_name = "" 66 | for name in collect(keys(name_indices)) #clean later. only does first element anyways 67 | cur_name = name 68 | end 69 | for i = 1:num_samples 70 | 71 | delta = 2*rand(Bernoulli(0.5),(1,length(q)))-1 72 | delta = delta[:] 73 | 74 | q_plus = q + delta; q_minus = q - delta 75 | 76 | delta_step = 2*stepsize*delta 77 | 78 | #energy for q_plus 79 | params.CURRENT_TRACE["RC"][cur_name]["X"] = q_plus 80 | trace_update(params.TAST) 81 | score_q_plus = params.CURRENT_TRACE["ll"] 82 | 83 | #energy for q_plus 84 | params.CURRENT_TRACE["RC"][cur_name]["X"] = q_minus 85 | trace_update(params.TAST) 86 | score_q_minus = params.CURRENT_TRACE["ll"] 87 | 88 | grad_q = grad_q + ((score_q_plus-score_q_minus)./delta_step) 89 | end 90 | 91 | grad_q = grad_q ./ num_samples 92 | 93 | params.CURRENT_TRACE["RC"][cur_name]["X"] = q 94 | trace_update(params.TAST) 95 | 96 | return grad_q 97 | end 98 | 99 | 100 | function compute_gradU(name_indices, q, q_len, EXACT) 101 | if EXACT 102 | return compute_gradU_exact(name_indices, q,q_len) 103 | else 104 | return compute_gradU_SPSA(name_indices, q,q_len) 105 | end 106 | end 107 | 108 | function isBounded(erp,theta) 109 | if erp == Uniform 110 | return true, theta[1], theta[2] 111 | elseif erp == Beta 112 | return true, 0, 1 113 | end 114 | return false, NaN, NaN 115 | end 116 | 117 | @debug function hmc_propose(names, debug_callback) 118 | 119 | EXACT = true 120 | 121 | params.CURRENT_TRACE = deepcopy(params.TRACE) 122 | # trace_update(params.TAST) 123 | 124 | epsilon = 0.1 #0.1 125 | LEAP_FROG = rand(DiscreteUniform(5,10)) #10-20 126 | 127 | q_len = 0 128 | if EXACT #for the time being. testing SPSA 129 | for name in names 130 | tmp = np.shape(params.CURRENT_TRACE["RC"][name]["ch_object"]) 131 | if tmp == (1,) 132 | tmp = 1 133 | else 134 | tmp = tmp[1] 135 | end 136 | q_len += tmp 137 | end 138 | end 139 | 140 | current_q = 0 141 | name_indices = Dict() 142 | startid = 1; endid = 0; 143 | 144 | for name in names 145 | endid += length(params.CURRENT_TRACE["RC"][name]["X"]) 146 | name_indices[name] = startid:endid 147 | startid = endid + 1 148 | 149 | if current_q == 0 150 | current_q = deepcopy(params.CURRENT_TRACE["RC"][name]["X"]) 151 | else 152 | current_q = append!(current_q,deepcopy(params.CURRENT_TRACE["RC"][name]["X"])) 153 | end 154 | end 155 | 156 | q = deepcopy(current_q) 157 | p = rand(Normal(0,1),(1,length(q)))[:] 158 | current_p = deepcopy(p) 159 | 160 | # ########### DEBUG ############ 161 | # q = q + rand(1,3) 162 | # aa=compute_gradU(DB, name, q) 163 | # println(aa) 164 | 165 | # params.CURRENT_TRACE = set_chobject(params.CURRENT_TRACE, name, q, q_len) 166 | # trace_update(params.TAST) 167 | # DB = deepcopy(params.CURRENT_TRACE) 168 | 169 | # return DB 170 | # ############################### 171 | 172 | #Make half step for momentum at the beginning 173 | 174 | p = p - epsilon * compute_gradU(name_indices, q, q_len,EXACT) 175 | 176 | 177 | #Alternate full steps for position and momentum 178 | for i = 1:LEAP_FROG 179 | print("leap:#",i) 180 | q = q + epsilon * p 181 | 182 | for name in names 183 | handle_constraints, llimit, ulimit = isBounded(params.CURRENT_TRACE["RC"][name]["ERP"], params.CURRENT_TRACE["RC"][name]["theta_c"]) 184 | if handle_constraints 185 | indxs = name_indices[name] 186 | phat = deepcopy(p[indxs]) 187 | qhat = deepcopy(q[indxs]) 188 | for ii = 1:length(qhat) 189 | while qhat[ii] > ulimit || qhat[ii] < llimit 190 | if qhat[ii] > ulimit 191 | qhat[ii] = ulimit - (qhat[ii] - ulimit) 192 | phat[ii] = -phat[ii] 193 | elseif qhat[ii] < llimit 194 | qhat[ii] = llimit + (llimit - qhat[ii]) 195 | phat[ii] = -phat[ii] 196 | end 197 | end 198 | q[indxs[1]+ii-1] = deepcopy(qhat[ii]); p[indxs[1]+ii-1] = deepcopy(phat[ii]) 199 | end 200 | end 201 | end 202 | 203 | #Make full step for the momentum, except at the end of trajectory 204 | if i != LEAP_FROG 205 | p = p - epsilon*compute_gradU(name_indices, q, q_len,EXACT) 206 | end 207 | end 208 | 209 | #Make a half step for momentum at the end 210 | p = p - epsilon*compute_gradU(name_indices,q, q_len,EXACT)*0.5 211 | p = -p 212 | 213 | #Evaluate potential and kinect energy 214 | #DB = set_chobject(DB, name, current_q, q_len) 215 | #params.CURRENT_TRACE = deepcopy(DB) 216 | 217 | if EXACT #for the time being. testing SPSA 218 | for name in names 219 | indxs = name_indices[name] 220 | params.CURRENT_TRACE = set_chobject(params.CURRENT_TRACE, name, current_q[indxs], length(indxs)) 221 | end 222 | end 223 | 224 | trace_update(params.TAST) 225 | # CUR_TRACE = deepcopy(params.CURRENT_TRACE) 226 | current_U = -params.CURRENT_TRACE["ll"] 227 | current_K = sum(current_p.*current_p)/2.0 228 | old_rc_cnt = length(params.CURRENT_TRACE["RC"]) 229 | 230 | 231 | #DB = set_chobject(DB, name, q, q_len) 232 | #params.CURRENT_TRACE = deepcopy(DB) 233 | if EXACT #for the time being. testing SPSA 234 | for name in names 235 | indxs = name_indices[name] 236 | params.CURRENT_TRACE = set_chobject(params.CURRENT_TRACE, name, q[indxs], length(indxs)) 237 | end 238 | end 239 | 240 | trace_update(params.TAST) 241 | # NEW_TRACE = deepcopy(params.CURRENT_TRACE) 242 | proposed_U = -params.CURRENT_TRACE["ll"]; ll_fresh = params.CURRENT_TRACE["ll_fresh"]; ll_stale = params.CURRENT_TRACE["ll_stale"] 243 | proposed_K = sum(p.*p)/2.0 244 | new_rc_cnt = length(params.CURRENT_TRACE["RC"]) 245 | 246 | if isnan(proposed_U) 247 | proposed_U = 1e200 248 | end 249 | 250 | #accept or reject 251 | if rand() < exp(current_U - proposed_U + current_K - proposed_K + log(old_rc_cnt) - log(new_rc_cnt) + ll_stale - ll_fresh) 252 | #accept 253 | #DB = deepcopy(NEW_TRACE) 254 | 255 | if EXACT #for the time being. testing SPSA 256 | for name in names 257 | indxs = name_indices[name] 258 | params.CURRENT_TRACE = set_chobject(params.CURRENT_TRACE, name, q[indxs], length(indxs)) 259 | end 260 | end 261 | 262 | trace_update(params.TAST) 263 | debug_callback(params.CURRENT_TRACE) 264 | println("ACCEPT") 265 | # return NEW_TRACE 266 | return params.CURRENT_TRACE#deepcopy(params.CURRENT_TRACE) 267 | else 268 | if EXACT #for the time being. testing SPSA 269 | for name in names 270 | indxs = name_indices[name] 271 | params.CURRENT_TRACE = set_chobject(params.CURRENT_TRACE, name, current_q[indxs], length(indxs)) 272 | end 273 | end 274 | trace_update(params.TAST) 275 | println("REJECTED") 276 | #reject 277 | #DB = deepcopy(CUR_TRACE) 278 | # return CUR_TRACE 279 | return params.CURRENT_TRACE#deepcopy(params.CURRENT_TRACE) 280 | end 281 | end 282 | 283 | 284 | 285 | 286 | -------------------------------------------------------------------------------- /engine/mcmcML.jl: -------------------------------------------------------------------------------- 1 | using Debug 2 | using Distributions 3 | 4 | 5 | function setArray(v,newv) 6 | for i = 0:np.shape(v)[1]-1 7 | for j=0:np.shape(v)[2]-1 8 | if typeof(newv) == Array{Int64,2} 9 | set!(v,(i,j), newv[i+1,j+1]) 10 | else 11 | set!(v,(i,j),int(get(newv,(i,j)))) 12 | end 13 | end 14 | end 15 | return v 16 | end 17 | 18 | function gradient_update(param, cost, lrate) 19 | grad = pycall(cost["dr_wrt"],PyAny,param) 20 | grad = convert(PyAny,grad["A"]) 21 | grad = sum(grad,1)/size(grad,1) 22 | grad = reshape(grad, np.shape(param)[1], np.shape(param)[2]) 23 | 24 | new_param = np.subtract(param, lrate*grad) 25 | 26 | for i = 0:np.shape(param)[1]-1 27 | for j=0:np.shape(param)[2]-1 28 | if np.shape(param)[2] == 1 29 | set!(param,i,new_param[i+1]) 30 | else 31 | set!(param,(i,j), new_param[i+1,j+1]) 32 | end 33 | end 34 | end 35 | 36 | return param 37 | end 38 | 39 | function SGD(names, debug_callback, args) 40 | DATA = params.OBSERVATIONS["input"] 41 | pout = params.CURRENT_TRACE["PROGRAM_OUTPUT"] 42 | # W = pout["params"][1] 43 | # bias_h = pout["params"][2] 44 | # bias_v = pout["params"][3] 45 | 46 | v = pout["obs"] 47 | loss = pout["loss"] 48 | lrate = pout["lrate"] 49 | 50 | # grad = pycall(pout["cost"]["dr_wrt"],PyAny,pout["params"]) 51 | # grad = sum(grad)/length(grad) 52 | # set!(pout["params"],(0,0),0.5) 53 | 54 | num_batches = length(DATA)-1 #1 of them is test 55 | 56 | for i=1:args #args=iterations 57 | #choose random minibatch 58 | batch = rand(DiscreteUniform(1,num_batches),1)[1] 59 | 60 | v = setArray(v, np.array(DATA[string("training_batch_",batch)],"int")) 61 | 62 | #update params 63 | for j=1:length(pout["params"]) 64 | pout["params"][j] = gradient_update(pout["params"][j], loss, lrate) 65 | end 66 | debug_callback(pout["params"][1],pout["params"][2],pout["params"][3]) 67 | end 68 | exit() 69 | end 70 | 71 | function MCMC_ML(names, debug_callback, args) 72 | print("Not implemented in this version") 73 | end 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /engine/optimize.jl: -------------------------------------------------------------------------------- 1 | using Debug 2 | using Distributions 3 | 4 | function get_bounds(names) 5 | bounds = 0 6 | 7 | for name in names 8 | tmp = 0 9 | erp = params.CURRENT_TRACE["RC"][name]["ERP"] 10 | theta = params.CURRENT_TRACE["RC"][name]["theta_c"] 11 | dim = length(params.CURRENT_TRACE["RC"][name]["X"]) 12 | 13 | if erp == Uniform 14 | tmp = (theta[1], theta[2]) 15 | elseif erp == Beta 16 | tmp = (0, 1) 17 | elseif erp == Normal || erp == Gamma 18 | tmp = (-100, 100) #something wrong with ch.minimize. Should be None. Check later 19 | end 20 | for ii=1:dim 21 | if bounds == 0 22 | bounds = [tmp] 23 | else 24 | bounds = append!(bounds,[tmp]) 25 | end 26 | end 27 | end 28 | 29 | return bounds 30 | end 31 | 32 | 33 | function lbfgs_propose(names, debug_callback, args) 34 | ll = params.TRACE["ll"] 35 | params.CURRENT_TRACE = deepcopy(params.TRACE) 36 | # trace_update(params.TAST) 37 | # variables_to_opt = 0 38 | # for name in names 39 | # if variables_to_opt == 0 40 | # variables_to_opt = params.CURRENT_TRACE["RC"][name]["ch_object"] 41 | # variables_to_opt = [variables_to_opt] 42 | # else 43 | # @bp 44 | # variables_to_opt = append!(variables_to_opt, params.CURRENT_TRACE["RC"][name]["ch_object"]) 45 | # end 46 | # end 47 | variables_to_opt = [params.CURRENT_TRACE["RC"][name]["ch_object"] for name in names] 48 | 49 | difference = pycall(chumpy_lib["subtract"],PyAny, params.USER_DEFINED["ren"] , params.OBSERVATIONS["IMAGE"]) 50 | gpr = odr.gaussian_pyramid(difference, n_levels=6, as_list=true)#[-3:] 51 | gpr = gpr[args["res"][1]:args["res"][2]] 52 | E = 0 53 | for i = 1:length(gpr) 54 | if i == 1 55 | E = pycall(chumpy_lib["ravel"],PyAny, gpr[i]) 56 | else 57 | tmp = pycall(chumpy_lib["ravel"],PyAny, gpr[i]) 58 | E =ch.concatenate((E, tmp)) 59 | end 60 | end 61 | 62 | bounds = get_bounds(names) 63 | 64 | # bounds=[(-4,4),(-4,4),(-4,4),(-10,10),(-10,10),(-10,10),(-10,10),(-10,10),(-10,10),(-10,10),(-10,10),(-10,10)] 65 | 66 | # println("BEFORE:", params.CURRENT_TRACE["RC"]["G1"]["ch_object"]) 67 | ch.minimize(fun=E,x0=variables_to_opt)#method="L-BFGS-B",bounds=bounds) 68 | # println("AFTER:", params.CURRENT_TRACE["RC"]["G1"]["ch_object"]) 69 | 70 | for name in names 71 | params.CURRENT_TRACE["RC"][name]["X"] = convert(PyAny,params.CURRENT_TRACE["RC"][name]["ch_object"]["r"]) 72 | #add new logl 73 | theta_d = params.CURRENT_TRACE["RC"][name]["theta_c"] 74 | erp = params.CURRENT_TRACE["RC"][name]["ERP"] 75 | dist = ERP_CREATE(erp, theta_d) 76 | params.CURRENT_TRACE["RC"][name]["logl"] = logscore_erp(dist, params.CURRENT_TRACE["RC"][name]["X"], theta_d) 77 | end 78 | 79 | trace_update(params.TAST) 80 | new_ll = params.CURRENT_TRACE["ll"] 81 | 82 | 83 | new_ll = params.CURRENT_TRACE["ll"]; ll_fresh = params.CURRENT_TRACE["ll_fresh"]; ll_stale = params.CURRENT_TRACE["ll_stale"] 84 | ACC = new_ll - ll + log(length(params.TRACE["RC"])) - log(length(params.CURRENT_TRACE["RC"])) + ll_stale - ll_fresh 85 | 86 | # println("LBFGS: old_ll:", ll, " new_ll:", new_ll) 87 | if log(rand()) < ACC #accept 88 | println("ACCEPT") 89 | params.TRACE = deepcopy(params.CURRENT_TRACE) 90 | debug_callback(params.TRACE) 91 | else 92 | println("REJECT") 93 | end 94 | 95 | return params.TRACE 96 | end 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /engine/picture.jl: -------------------------------------------------------------------------------- 1 | 2 | # module Picture 3 | 4 | if isdefined(:struct) == false 5 | type struct 6 | PROGRAM 7 | TRACE_EXTERN 8 | TRACE 9 | NEW_TRACE 10 | CURRENT_TRACE 11 | LOOP_LIST 12 | TAST 13 | OBSERVATIONS 14 | USER_DEFINED 15 | TAPE 16 | end 17 | end 18 | 19 | global params = struct(NaN,NaN,Dict(), Dict(),Dict(),Dict(), NaN,NaN, Dict(), Dict()) 20 | 21 | global GRADIENT_CALC = false 22 | 23 | 24 | using Distributions 25 | using Debug 26 | using DataStructures 27 | 28 | using PyCall 29 | @pyimport numpy as np 30 | @pyimport matplotlib.pyplot as plt 31 | @pyimport opendr.util_tests as util_tests 32 | @pyimport chumpy as ch 33 | @pyimport opendr.simple as odr 34 | @pyimport numpy.random as npr 35 | @pyimport math as pmath 36 | @pyimport scipy.misc as scpy 37 | @pyimport gc as garbage 38 | chumpy_lib = pyimport(:chumpy) 39 | 40 | 41 | garbage.disable() 42 | 43 | include("erps.jl") 44 | include("codetransform.jl") 45 | include("proposals.jl") 46 | include("hmc.jl") 47 | include("optimize.jl") 48 | include("mcmcML.jl") 49 | include("elliptical.jl") 50 | include("torch_interface.jl") 51 | include("gibbs_kernel.jl") 52 | # @debug begin 53 | 54 | #################### GLOBAL STRUCTURES ################### 55 | # global TRACE = Dict() 56 | # global NEW_TRACE = Dict() 57 | # global CURRENT_TRACE = Dict() 58 | 59 | # global LOOP_LIST = Dict() 60 | 61 | # AST = NaN 62 | # TAST = NaN 63 | 64 | 65 | #################### HELPER FUNCTIONS #################### 66 | #Appending to arrays 67 | myappend{T}(v::Vector{T}, x::T) = [v..., x] 68 | 69 | 70 | function check_if_random_choice(expression) 71 | str_exp = string(expression) 72 | if search(str_exp,"observe") != 0:-1 73 | return false 74 | end 75 | 76 | if search(str_exp, "block") != 0:-1 77 | return true 78 | end 79 | 80 | if search(str_exp, "memoize") != 0:-1 81 | return true 82 | end 83 | 84 | # try check_block = string(expression.args[2].args[1]) 85 | # if check_block == "block" 86 | # return true 87 | # end 88 | # catch 89 | # ret = false 90 | # end 91 | 92 | ret = false 93 | try input_erp = string(expression.args[2].args[1]) #string(expression.args[2].args[2].args[2].args[1]) 94 | ERPS = ["DiscreteUniform", "Bernoulli","Beta","Poisson", "Uniform","Normal","Multinomial", "Gamma", "MvNormal"] 95 | 96 | for i=1:length(ERPS) 97 | #ret = ret | (match(Regex(ERPS[i]),expression) != Nothing()) 98 | ret = ret | (ERPS[i] == input_erp) 99 | if ret == true 100 | return ret 101 | end 102 | end 103 | catch 104 | ret = false 105 | end 106 | 107 | return ret 108 | end 109 | 110 | 111 | function is_theta_equal(theta_c, theta_d) #theta_c can have symbols 112 | equal = false 113 | 114 | for ii=1:length(theta_c) 115 | if typeof(theta_c[ii]) == Symbol 116 | theta_c[ii] = eval(theta_c[ii]) 117 | end 118 | equal += (theta_c[ii] == theta_d[ii]) 119 | end 120 | return equal 121 | end 122 | 123 | function trace_update(TAST) 124 | 125 | params.CURRENT_TRACE["ll"] = 0; params.CURRENT_TRACE["ll_fresh"] = 0; params.CURRENT_TRACE["ll_stale"] = 0; 126 | params.CURRENT_TRACE["ACTIVE_K"] = Dict() 127 | 128 | params.CURRENT_TRACE["PROGRAM_OUTPUT"] = eval(TAST[1].args[3]) 129 | 130 | all_choices = unique(append!(collect(keys(params.CURRENT_TRACE["RC"])),collect(keys(params.CURRENT_TRACE["ACTIVE_K"])))) 131 | for name in all_choices 132 | if haskey(params.CURRENT_TRACE["ACTIVE_K"], name) == false #inactive 133 | ERP = params.CURRENT_TRACE["RC"][name]["ERP"] 134 | theta_d = params.CURRENT_TRACE["RC"][name]["theta_c"] 135 | VAL = params.CURRENT_TRACE["RC"][name]["X"] 136 | params.CURRENT_TRACE["ll_stale"]+=logscore_erp(ERP_CREATE(ERP,theta_d),VAL, theta_d) 137 | delete!(params.CURRENT_TRACE,name) 138 | end 139 | end 140 | 141 | if params.CURRENT_TRACE["PROGRAM_OUTPUT"] == "OVERFLOW_ERROR" 142 | params.CURRENT_TRACE["ll"]=-1e200 143 | end 144 | end 145 | 146 | function load_program(PROGRAM) 147 | params.PROGRAM = PROGRAM 148 | end 149 | 150 | function load_observations(OBSERVATIONS) 151 | params.OBSERVATIONS = OBSERVATIONS 152 | end 153 | 154 | function initialize_trace_extern(funcFtr,args) 155 | params.TRACE_EXTERN = funcFtr(args) 156 | end 157 | 158 | function init() 159 | AST=code_lowered(params.PROGRAM,()) 160 | #AST=code_lowered(PROGRAM,(Dict{Any,Any},)) 161 | params.TAST = transform_code(AST) 162 | 163 | params.TRACE["RC"]=Dict() 164 | params.TRACE["observe"]=Dict() 165 | 166 | params.CURRENT_TRACE = deepcopy(params.TRACE) 167 | trace_update(params.TAST) 168 | params.TRACE = deepcopy(params.CURRENT_TRACE) 169 | end 170 | 171 | 172 | function observe_iid(trc,tag,input, output) 173 | trc.OBSERVATIONS = {"input"=>input, "output"=>output} 174 | end 175 | 176 | 177 | function trace(PROGRAM, args) 178 | params.OBSERVATIONS = args 179 | params.PROGRAM = PROGRAM 180 | AST=code_lowered(params.PROGRAM,()) 181 | #AST=code_lowered(PROGRAM,(Dict{Any,Any},)) 182 | params.TAST = transform_code(AST) 183 | 184 | params.TRACE["RC"]=Dict() 185 | params.TRACE["observe"]=Dict() 186 | 187 | params.CURRENT_TRACE = deepcopy(params.TRACE) 188 | trace_update(params.TAST) 189 | params.TRACE = deepcopy(params.CURRENT_TRACE) 190 | return params 191 | end 192 | 193 | 194 | function infer( debug_callback="", iterations = 100, group_name="", inference="MH_SingleSite", args="",mode="") 195 | println("Starting Inference [Scheme=",inference,"]") 196 | ll = params.TRACE["ll"]; 197 | for iters=1:iterations 198 | if iters%50 == 0 199 | println("Iter#: ",iters) 200 | end 201 | rv_choices = collect(keys(params.TRACE["RC"])) 202 | if group_name == "" 203 | #select random f_k via its name 204 | idx=rand(1:length(params.TRACE["RC"])) 205 | chosen_rv = [rv_choices[idx]] 206 | else 207 | if group_name == "CYCLE" #reserved name for cycling through all variables 208 | indx = iters%length(rv_choices) 209 | indx = indx + 1 210 | chosen_rv = [rv_choices[indx]] 211 | else 212 | chosen_rv = [group_name] 213 | end 214 | end 215 | ########## Metropolis Hastings ########## 216 | # println("CHOSEN:", chosen_rv) 217 | if inference == "MH" || inference == "MH_SingleSite" 218 | if typeof(chosen_rv) == Array{ASCIIString,1} 219 | chosen_rv = chosen_rv[1] 220 | new_X,new_logl, F, R = sample_from_proposal(params.TRACE,chosen_rv,inference) 221 | 222 | params.NEW_TRACE = deepcopy(params.TRACE) 223 | params.NEW_TRACE["RC"][chosen_rv]["X"]=new_X 224 | params.NEW_TRACE["RC"][chosen_rv]["logl"]=new_logl 225 | else 226 | #block proposal 227 | params.NEW_TRACE = deepcopy(params.TRACE) 228 | for ii=1:length(chosen_rv) 229 | crv = chosen_rv[ii] 230 | new_X,new_logl, F, R = sample_from_proposal(params.TRACE,crv,inference) 231 | params.NEW_TRACE["RC"][crv]["X"]=new_X 232 | params.NEW_TRACE["RC"][crv]["logl"]=new_logl 233 | end 234 | end 235 | 236 | params.CURRENT_TRACE = deepcopy(params.NEW_TRACE) 237 | # println("AFTER CHOBJ:", params.CURRENT_TRACE["RC"]["G1"]["ch_object"]) 238 | trace_update(params.TAST) 239 | params.NEW_TRACE = deepcopy(params.CURRENT_TRACE) 240 | new_ll = params.NEW_TRACE["ll"]; ll_fresh = params.NEW_TRACE["ll_fresh"]; ll_stale = params.NEW_TRACE["ll_stale"] 241 | 242 | #acceptance ratio 243 | if new_ll == -1e200 244 | ACC = -1e200 245 | else 246 | ACC = new_ll - ll + R - F + log(length(params.TRACE["RC"])) - log(length(params.NEW_TRACE["RC"])) + ll_stale - ll_fresh 247 | end 248 | 249 | if mode == "HALLUCINATE" 250 | ACC = 1e100 251 | end 252 | 253 | if log(rand()) < ACC #accept 254 | params.TRACE = deepcopy(params.NEW_TRACE) 255 | ll = new_ll 256 | debug_callback(params.TRACE) 257 | else# Rejected 258 | params.NEW_TRACE = Dict() 259 | end 260 | ########## HMC ########## 261 | elseif inference == "HMC" 262 | params.TRACE = hmc_propose(chosen_rv, debug_callback) 263 | elseif inference == "Gibbs" 264 | params.TRACE = gibbs_propose(chosen_rv, debug_callback) 265 | elseif inference == "LBFGS" 266 | params.TRACE = lbfgs_propose(chosen_rv, debug_callback, args) 267 | elseif inference == "SGD" 268 | params.TRACE = SGD(group_name, debug_callback, iterations) 269 | elseif inference == "ELLIPTICAL" 270 | params.TRACE = ELLIPTICAL(chosen_rv, debug_callback) 271 | end 272 | end 273 | 274 | end 275 | 276 | # end #debug 277 | 278 | # end #module Picture 279 | 280 | 281 | 282 | -------------------------------------------------------------------------------- /engine/proposals.jl: -------------------------------------------------------------------------------- 1 | 2 | function sample_from_proposal(DB,name,inference) 3 | 4 | random_var = deepcopy(DB["RC"][name]) 5 | dimensionality = length(random_var["X"]) 6 | 7 | theta_d = random_var["theta_c"] 8 | erp = random_var["ERP"] 9 | dist = ERP_CREATE(erp, theta_d) 10 | size = random_var["size"] 11 | if erp == MvNormal 12 | val = npr.multivariate_normal(theta_d[1],theta_d[2]) 13 | F=0 14 | R=0 15 | else 16 | if size[1]*size[2] == 1 17 | val = rand(dist) 18 | else 19 | if inference == "MH_SingleSite" 20 | sample = rand(dist) 21 | sampled_indx = rand(DiscreteUniform(1,dimensionality)) 22 | val = random_var["X"] 23 | val[sampled_indx] = sample 24 | elseif inference == "MH" 25 | val = rand(dist,(size[1],size[2])) 26 | else 27 | println("[ERROR] Specify valid inference scheme in sample_from_proposal()") 28 | return 29 | end 30 | end 31 | F = 0#logpdf(dist, val) 32 | R = 0#logpdf(dist, DB["RC"][name]["X"]) 33 | end 34 | return val, logscore_erp(dist, val, theta_d), F, R 35 | end 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /engine/test.lua: -------------------------------------------------------------------------------- 1 | --test script 2 | require('sys') 3 | function testdraw() 4 | sys.tic() 5 | ret = torch.rand(4) 6 | print(sys.toc()) 7 | return ret 8 | end 9 | 10 | samples = testdraw() 11 | 12 | function get_samples(index) 13 | return samples[index] 14 | end -------------------------------------------------------------------------------- /engine/torch_interface.jl: -------------------------------------------------------------------------------- 1 | #Torch interface from Julia 2 | 3 | module TORCH 4 | using JSON 5 | using ZMQ 6 | 7 | ctx=Context(1) 8 | s1=Socket(ctx, REQ) 9 | 10 | export load_torch_script 11 | export call 12 | export close 13 | 14 | #PORT = 7000 15 | global PORT = -1 16 | 17 | function setport(PORTN) 18 | global PORT 19 | PORT = PORTN 20 | end 21 | 22 | function load_torch_script(script_name) 23 | global PORT 24 | println("Port:", PORT) 25 | ZMQ.connect(s1, string("tcp://localhost:", PORT)) 26 | CMD = string("{\"cmd\":\"load\", \"name\":\"", script_name ,"\"}") 27 | ZMQ.send(s1,Message(CMD)) 28 | msg = ZMQ.recv(s1) 29 | out=convert(IOStream, msg) 30 | seek(out,0) 31 | msg = takebuf_string(out) 32 | msg = JSON.parse(msg) 33 | return msg 34 | end 35 | 36 | function call(func, args) 37 | global PORT 38 | # ZMQ.connect(s1, string("tcp://localhost:", PORT)) 39 | CMD = string("{\"cmd\":\"call\",", "\"msg\":{" ,"\"func\":\"", func , "\",\"args\":\"", args, "\"}}") 40 | ZMQ.send(s1,Message(CMD)) 41 | msg = ZMQ.recv(s1) 42 | out=convert(IOStream, msg) 43 | seek(out,0) 44 | msg = takebuf_string(out) 45 | msg = JSON.parse(msg) 46 | return msg 47 | end 48 | 49 | function close() 50 | ZMQ.close(sock) 51 | ZMQ.close(ctx) 52 | end 53 | 54 | end -------------------------------------------------------------------------------- /engine/torch_server.lua: -------------------------------------------------------------------------------- 1 | -- Torch interface to Julia (server) 2 | local json = require ("dkjson") 3 | local zmq = require "lzmq" 4 | local context = zmq.init(1) 5 | 6 | local socket = context:socket(zmq.REP) 7 | socket:bind("tcp://*:7000") 8 | 9 | function serialize(data) 10 | if torch.type(1) == torch.type(data) then 11 | return {data} 12 | end 13 | --for now it assumes data is a 1-D array 14 | local ret = {} 15 | for i=1,data:size(1) do 16 | ret[i] = data[i] 17 | end 18 | return ret 19 | end 20 | 21 | function test() 22 | require("/home/tejas/Documents/MIT/facegen/picture.lua") 23 | end 24 | 25 | test() 26 | 27 | --[[ 28 | while true do 29 | -- Wait for next request from client 30 | local request = socket:recv() 31 | -- print("Received Hello [" .. request .. "]") 32 | -- print(request) 33 | if request ~= nil then 34 | request = json.decode(request, 1, nil) 35 | -- print('req:',request, ' | cmd:', request.cmd) 36 | if request.cmd == "load" then 37 | require(request.name) 38 | ret = {1} 39 | ret = json.encode (ret, { indent = true }) 40 | socket:send(ret) 41 | elseif request.cmd == "call" then 42 | func_name = request.msg.func 43 | args = request.msg.args 44 | ret = _G[func_name](args) 45 | -- ret = torch.rand(10):float() 46 | ret = serialize(ret) 47 | ret = json.encode (ret,{ indent = true }) 48 | socket:send(ret) 49 | end 50 | end 51 | end 52 | -- We never get here but if we did, this would be how we end 53 | socket:close() 54 | context:term() 55 | 56 | 57 | --]] 58 | -------------------------------------------------------------------------------- /engine/unit_tests.jl: -------------------------------------------------------------------------------- 1 | #Picture : Unit tests 2 | include("torch_interface.jl") 3 | using TORCH 4 | TORCH.load_torch_script("test.lua") 5 | TORCH.call("get_samples", 1) --------------------------------------------------------------------------------