Comparar commits
1683 Commits
| Autor | SHA1 | Data | |
|---|---|---|---|
| 205c61178d | |||
| 3fe7837691 | |||
| bcc68e9040 | |||
| 985c441014 | |||
| 59cd1c3994 | |||
| 4de120f051 | |||
| d9255f15a4 | |||
| b42f6760bc | |||
| 6b51d149ca | |||
| 8040ad72dd | |||
| ec048edffc | |||
| 23269507fd | |||
| eafdffff75 | |||
| 5834747dc7 | |||
| 6c2dea64fc | |||
| ddcf66fbe8 | |||
| 5edd96a9f8 | |||
| c6ec39258b | |||
| 7866fbaa1a | |||
| 94397e08ae | |||
| 98db0285ee | |||
| 7413956e7e | |||
| 9a4598da50 | |||
| a09c9f6c2d | |||
| e4ab777d07 | |||
| 4fab0bf9a8 | |||
| 49a7c7376d | |||
| fb9dbdb10c | |||
| a85263fb3e | |||
| f6c1730cf3 | |||
| 598954d2c8 | |||
| 21c5e5479b | |||
| d1ad183770 | |||
| 9dca90e705 | |||
| 5a7f6b0e74 | |||
| 7dcd2982b2 | |||
| 3462835597 | |||
| 585f33f6b7 | |||
| 1aa9e9199b | |||
| fb97b6e0fa | |||
| 60cf7ca6b2 | |||
| 1b539993aa | |||
| de73eda89a | |||
| 219d6ee5be | |||
| f430de10fb | |||
| a2f6ae2c66 | |||
| b713122e77 | |||
| 75470e380f | |||
| b5ad5334fc | |||
| 04a20177cf | |||
| 58d1d0678f | |||
| abf8691ade | |||
| 7425e68cd6 | |||
| ab6b82c2db | |||
| 6814506528 | |||
| 1ddf23528e | |||
| 86c8d1dd45 | |||
| 929ae992c2 | |||
| 3d9428d344 | |||
| e0543fbfc8 | |||
| 767846e642 | |||
| d852c2d772 | |||
| ff45159b69 | |||
| 7766ab341f | |||
| 4135aeebc4 | |||
| ced84c4b42 | |||
| 8d5b2ce60c | |||
| c0f0b660a6 | |||
| d3c33613a1 | |||
| 5ca5699b00 | |||
| be6503a8a8 | |||
| c73ba916f6 | |||
| e1c3988198 | |||
| f65a56fb65 | |||
| 00a2724260 | |||
| a625fcde5c | |||
| 73f374ec67 | |||
| 21cf50734a | |||
| 4a6f06f06d | |||
| 295e4f8064 | |||
| f4cb890024 | |||
| cd943231d1 | |||
| f7b925a893 | |||
| 5d63ab4251 | |||
| d4b618bf23 | |||
| 5012678e17 | |||
| 11d9c995cc | |||
| d92fab69a2 | |||
| 19463a19b8 | |||
| ca1122fe80 | |||
| 7fc707e13e | |||
| 846d25ab97 | |||
| 8c0a8b4b04 | |||
| 1b1e09a366 | |||
| fd427b8cdb | |||
| 53303fdb10 | |||
| 720ed1adc4 | |||
| 43e418d1d2 | |||
| 75d9415c82 | |||
| 552978dc58 | |||
| 508bb8f541 | |||
| c3c97905fe | |||
| 33cee3f947 | |||
| f0659766fc | |||
| 62973243ae | |||
| 6a0c9a617d | |||
| 3c180eafed | |||
| 763bd6d8f1 | |||
| 36317214ae | |||
| a5f53155a5 | |||
| 78be823518 | |||
| 5810f7a9c7 | |||
| 0bc8fac446 | |||
| aea62d8baf | |||
| 7819b9c14e | |||
| eede3dc43d | |||
| ea29308eaa | |||
| 21b72a3b13 | |||
| f3bbf31497 | |||
| 17e073d87e | |||
| 60c52ea766 | |||
| bfa38fb747 | |||
| 7c73bfc50d | |||
| fccd4f8055 | |||
| 1b67c59de8 | |||
| a9d2a99500 | |||
| 0bb4e0fad5 | |||
| 1e09e0a9d4 | |||
| 07e0fbc963 | |||
| 7ef13165b7 | |||
| d939f14843 | |||
| ce0f97dbe3 | |||
| 7e870a97ec | |||
| a2c3fa2b96 | |||
| 7f09d45efb | |||
| 85fe6427a5 | |||
| b205ba1270 | |||
| e74a37438b | |||
| c8d35caa7f | |||
| cf57d28452 | |||
| c1a1c33ef9 | |||
| bac16379a2 | |||
| b5490b20d2 | |||
| 3061fcce60 | |||
| 7a3190de3b | |||
| ed9e8d2ff0 | |||
| 13303663ff | |||
| 0d27d903c2 | |||
| 6220e35ccd | |||
| bc9dbc5de0 | |||
| d67cf89759 | |||
| a2dde60a2f | |||
| e177397427 | |||
| 5f4f234f9b | |||
| 24db6bfaaf | |||
| 504bded884 | |||
| 08aa6ae555 | |||
| 737ae88a02 | |||
| 6642d496e5 | |||
| 2766074d19 | |||
| 672028a5f2 | |||
| 1a89b13cb4 | |||
| 268672df65 | |||
| bfae0a6191 | |||
| a2a0f66276 | |||
| ea8e2edf17 | |||
| d223cc0ff7 | |||
| 8ac1b1fdc9 | |||
| 23833417cf | |||
| 61c9cdc53c | |||
| 1c7e63e42c | |||
| 6582043276 | |||
| 85221ccd13 | |||
| cf550db5a5 | |||
| 75519651bb | |||
| b93d3b23f5 | |||
| dc3d164c6b | |||
| 47dddaa7fd | |||
| fdd822c03e | |||
| a736c2632b | |||
| 1a707ea11e | |||
| c430b6c492 | |||
| c627fa5bbd | |||
| affaa77078 | |||
| f1df88737c | |||
| 0ddc3360b7 | |||
| eaca5da3e2 | |||
| 70da22c31f | |||
| c158410168 | |||
| 0c237ebea2 | |||
| 8967d16d00 | |||
| 964023bec7 | |||
| 16aa56bb1d | |||
| bdf05c48ef | |||
| 653cfd2076 | |||
| bcbfcc000c | |||
| 54a417f616 | |||
| 5e51d02a94 | |||
| d3b9b9d5bb | |||
| 4f9e7bf93c | |||
| d491dafb80 | |||
| 365f621b24 | |||
| 7481b5d060 | |||
| 9295efb216 | |||
| 0d4fb04c7f | |||
| 791cba094c | |||
| 2bb9014c91 | |||
| 5be73f1ab3 | |||
| 04bf5ac57a | |||
| b8134f529c | |||
| 7d52af64c0 | |||
| 70ffba0766 | |||
| e7f3317de6 | |||
| 47350dc607 | |||
| d498a98465 | |||
| 0976afb46d | |||
| 7088ebd294 | |||
| f71831790f | |||
| 83001d195c | |||
| 8830c53135 | |||
| d89afdfd82 | |||
| 562860ca42 | |||
| fc4874f82c | |||
| 73a620b6e8 | |||
| e0697c3768 | |||
| 73bf06fb02 | |||
| 53bee20647 | |||
| 18ed60b9f2 | |||
| 707534e46e | |||
| cd6bbe7290 | |||
| f6cc059104 | |||
| 6572934f9a | |||
| 2a67506728 | |||
| 4507057e11 | |||
| eee1d90ef2 | |||
| 9d0efc081e | |||
| 2c284017d4 | |||
| 90758c3f4e | |||
| dcacdd3747 | |||
| 5bd3976e79 | |||
| 9eb7ecd3e5 | |||
| 05589a7c27 | |||
| 4aa41625bf | |||
| b2f0dd4cb2 | |||
| 17ef113ed7 | |||
| c029fa2f62 | |||
| 52b1377fe6 | |||
| 5598fcd33e | |||
| b558a7e97c | |||
| 172397ebf4 | |||
| 9adb43e44b | |||
| ac6fde801c | |||
| 0fb0c22f39 | |||
| 362bfdd651 | |||
| 28b731a3d1 | |||
| 6b3459ae4d | |||
| 76c553e68f | |||
| a8e7b19b79 | |||
| ba3e2cadbe | |||
| 1fe9ed7b55 | |||
| 65a215646c | |||
| 1a16857886 | |||
| 8fde4fe305 | |||
| 75b69a5615 | |||
| 98ec9fc972 | |||
| debbd47405 | |||
| 466bb39aa1 | |||
| d660bd15c5 | |||
| 3838f55489 | |||
| edaa1d479d | |||
| 938788bd01 | |||
| 90cf7b9ed2 | |||
| ae020bfee0 | |||
| 7c6463da6f | |||
| 4785d51705 | |||
| 655f5af76e | |||
| 98b95762b6 | |||
| 0930ca9eb7 | |||
| 4fe78f3400 | |||
| 64d2421599 | |||
| 3382c0bb89 | |||
| b943176d2a | |||
| f9c9c0ab3f | |||
| af8561eb19 | |||
| d7341b3f39 | |||
| e57965ec76 | |||
| 90d24ddf1a | |||
| 9749ea3309 | |||
| 48e056d31f | |||
| dbe13670d9 | |||
| 986ecdb8c6 | |||
| 3a666b497d | |||
| fe48b41c22 | |||
| 3308778b9d | |||
| 7c3f882237 | |||
| aec0e56ada | |||
| 86b12f6fd2 | |||
| b260333eed | |||
| b9fc5625fe | |||
| b64e591971 | |||
| 4eff36910b | |||
| c2321e61e1 | |||
| ff577d84c0 | |||
| 80b72fa7b3 | |||
| fa4c747b7e | |||
| 3dd5fc88f7 | |||
| 466f0b91f1 | |||
| 9f6fb452a2 | |||
| 568d1a5b8a | |||
| 50057d8fe2 | |||
| 57ff6e99ca | |||
| 0be8040e79 | |||
| f173255540 | |||
| befbdaa076 | |||
| 9405be8f83 | |||
| 109d9f4eb3 | |||
| de52b4bf4b | |||
| 1a353f06ec | |||
| 9217effdb4 | |||
| 31ecfb28c3 | |||
| b5dc734f4e | |||
| ae4a145ea4 | |||
| 6438a0bfcf | |||
| a4dc2a3d6b | |||
| e21c1fa7d3 | |||
| 4eaf56e59b | |||
| 15785660d6 | |||
| 330ffa41dd | |||
| 576f8fe8e6 | |||
| 0cc56a46e8 | |||
| 31d821d878 | |||
| 7dc09a34f6 | |||
| c0d185b467 | |||
| 01002689a6 | |||
| bd9214a547 | |||
| 4a429fbe7d | |||
| e5a33862a3 | |||
| b4f7340cc9 | |||
| 22e3232e4d | |||
| cb34ed881e | |||
| 1f0f2bb307 | |||
| ec9c95fdbd | |||
| 3f45fa02ba | |||
| 60deb6e2cf | |||
| ae8bcdc291 | |||
| 9c86aa21db | |||
| 2b3579ecfc | |||
| 2b1c4779ff | |||
| 6dca6c2531 | |||
| 5d512f82b5 | |||
| 8566ef7779 | |||
| f4f3567e15 | |||
| 90529b222e | |||
| 35b2aa9103 | |||
| 9cf7f816f2 | |||
| 6691b9e3fb | |||
| 467de6bb6c | |||
| 62fd5f7ab6 | |||
| 1039924245 | |||
| 3e81b668ea | |||
| 459d7fe3d7 | |||
| 1a0792ae13 | |||
| b5a02391e0 | |||
| c88a11c378 | |||
| 40e91020dd | |||
| c3472a5488 | |||
| 02ba149e57 | |||
| cb841ae079 | |||
| 0703d79606 | |||
| 5cef75219a | |||
| 8590e086a3 | |||
| aff40d8008 | |||
| 7095aca51b | |||
| e848463347 | |||
| a8eb2e97d0 | |||
| ce3093a3b2 | |||
| f448341e55 | |||
| 51c5f3f9c6 | |||
| 0cc52cf251 | |||
| c45f48eaea | |||
| 36e526edc6 | |||
| e98379b7d9 | |||
| 3f7b0ff954 | |||
| 69a6b1a028 | |||
| f78d417f94 | |||
| a18f9f5755 | |||
| 7f58b6fbe7 | |||
| fe8e8fd43f | |||
| 966ec311d1 | |||
| 1f87cff493 | |||
| e826c55981 | |||
| 1b0c056975 | |||
| 4c56f32624 | |||
| 859ffebfcc | |||
| b377edf974 | |||
| 397e3336d1 | |||
| 7841b7fcd7 | |||
| 85a61ba92b | |||
| c469f80f81 | |||
| 37e8234bd1 | |||
| 640d1db4e9 | |||
| 44b25b80b2 | |||
| 3916e62666 | |||
| 92d1cf8599 | |||
| e9222523ba | |||
| 53c0faa553 | |||
| 9655493978 | |||
| ac1afb3e5f | |||
| d704bea3b2 | |||
| 151b5f9778 | |||
| f420d89864 | |||
| b43caf7b49 | |||
| 7e071cd7df | |||
| 12907534f8 | |||
| 10d7e21efc | |||
| 6e289d7186 | |||
| 7374442a1c | |||
| 98af4eb39f | |||
| 11107c5f2c | |||
| 28c208deab | |||
| 2082bafd18 | |||
| a00eef21c9 | |||
| d04d05442c | |||
| d5a0737a01 | |||
| e71cbccc2a | |||
| fa4aba7a9b | |||
| 8b8ffe0ea4 | |||
| aa826d684d | |||
| 84711475f8 | |||
| 7696a13995 | |||
| fbc8697366 | |||
| a783ceae2a | |||
| 21bf90cbf5 | |||
| 214a54d40e | |||
| 16feb385e5 | |||
| d51fc7659e | |||
| 109b11016a | |||
| 0c7b9d57c1 | |||
| 4cf4aa1efe | |||
| 070037d449 | |||
| ee04a7c77a | |||
| da21c15180 | |||
| 6fef95a9fd | |||
| 7f1cdbfef6 | |||
| e723c3baf1 | |||
| d12d171553 | |||
| d3f4397797 | |||
| fd9acf6d73 | |||
| 81aa60a7cf | |||
| 118027fcae | |||
| 16343b3261 | |||
| eaa827584f | |||
| 045283bc68 | |||
| 93cf06c4c6 | |||
| 54d9a5184d | |||
| 2fc0d063d1 | |||
| 195cbdfb3f | |||
| 43c0c82501 | |||
| ea55cda984 | |||
| 6f4a86407d | |||
| 7e6ccb8a48 | |||
| 152d896a77 | |||
| ea7847328f | |||
| 89e1ce5fa1 | |||
| b2a992e08f | |||
| 2ad58e1dcc | |||
| 6cc11224fb | |||
| a9549eb632 | |||
| e645a18e83 | |||
| 4b75473690 | |||
| 8697cd9bcd | |||
| 9f7ed932f1 | |||
| 78eedad77a | |||
| 76b4db7767 | |||
| 080aa7a6ad | |||
| 0744a25778 | |||
| 5374cec3c5 | |||
| c6c9373954 | |||
| 74cf6df153 | |||
| 42b18506c5 | |||
| 46a3f9443d | |||
| 8f6d12f457 | |||
| 6419d52543 | |||
| 7207c78e10 | |||
| 19bef655af | |||
| 1606dc6a11 | |||
| 0b2c044d48 | |||
| e91dc42842 | |||
| adb15756ff | |||
| e99eac292d | |||
| 3b660145a7 | |||
| 5516c8fb42 | |||
| f9202817f3 | |||
| d72514d6cc | |||
| 5f94aef668 | |||
| 5dbb6121cd | |||
| e45bce14b7 | |||
| 77ea18e8f7 | |||
| 65ce238f03 | |||
| a214f4e64d | |||
| 5b0967a08f | |||
| 136f1b4292 | |||
| ada7aa12bf | |||
| 7da568d59f | |||
| 2b10c68980 | |||
| 5d0cb10949 | |||
| 0e9ac3dae0 | |||
| 4e50446279 | |||
| 50d83f7ab8 | |||
| 91bab91f3c | |||
| fbe7873fc0 | |||
| 8d34c7ed3c | |||
| eec61d9d49 | |||
| 38a6dae44a | |||
| 6d8cf2fd6e | |||
| 4b5984b8ce | |||
| 9683998b37 | |||
| ad21188a13 | |||
| 8b2f06de49 | |||
| a853789795 | |||
| 4487510c90 | |||
| 4992707d57 | |||
| 7f5be01da9 | |||
| c8282437a7 | |||
| 7ba07aa8ce | |||
| 579cc22cda | |||
| b666ef18a1 | |||
| 4101b5fdb2 | |||
| 173a40ddc1 | |||
| cad4e4e8cc | |||
| a164addd8d | |||
| f623b2ab57 | |||
| 1193427fd3 | |||
| 1ac23c2556 | |||
| f516cc6dde | |||
| e1a283e9d1 | |||
| 99fa71a916 | |||
| 53b9f9c440 | |||
| 3cfd306c76 | |||
| 6cb1fe9490 | |||
| 825d58e2bb | |||
| c81713367e | |||
| 0c8e6319cf | |||
| 5ae158e8b0 | |||
| e90b0713f2 | |||
| 5566c9db1d | |||
| 8fb8accb05 | |||
| 547640587a | |||
| d046cea1a1 | |||
| 0f308d4b72 | |||
| 8e25f8b1c2 | |||
| bf1d29c8c3 | |||
| 8d317c9867 | |||
| c81f9447c7 | |||
| aeb22266c9 | |||
| 0599ade6da | |||
| db22fdf9af | |||
| 9e25cb7d13 | |||
| cb567d27ea | |||
| c15d869775 | |||
| 70e5cd1620 | |||
| 309f13ad8c | |||
| a277dabc14 | |||
| b361e30146 | |||
| 2ab5295cee | |||
| cb3e82051f | |||
| 5ef7ce3f07 | |||
| 4f735c4b0d | |||
| 4828bde0ce | |||
| b03b2b6140 | |||
| dab228bc56 | |||
| 197fb886cb | |||
| b7116b991d | |||
| 79192f8358 | |||
| 13f271c7d6 | |||
| 1422baaa8d | |||
| ac2a7254c2 | |||
| 26e6df8a98 | |||
| ac4b365e0b | |||
| 00dc75116f | |||
| 7e7d2ed1f9 | |||
| 3cde2c1f8a | |||
| 10012ae9c6 | |||
| 97db7e2666 | |||
| 3a57f184de | |||
| fb21b1bad8 | |||
| 59e67fd049 | |||
| 67b43e3b0d | |||
| b257dad10c | |||
| 7dc8a32f89 | |||
| 701355a720 | |||
| 871809bb85 | |||
| 2c0809c125 | |||
| af0d18f616 | |||
| 16e8b93d38 | |||
| f46ccff056 | |||
| 1bfa665eed | |||
| 0564b940ec | |||
| 5bb86288e9 | |||
| ebe84eb3a1 | |||
| b95cf4a1b1 | |||
| 451d74c56d | |||
| 55e0347667 | |||
| 803e2869c7 | |||
| e092fa8b57 | |||
| f0e0527591 | |||
| a1c4bc0c96 | |||
| a60ee821a2 | |||
| b0500764a8 | |||
| 82f9e2358c | |||
| e1a4aea4be | |||
| 45a10bc6d7 | |||
| 10c76237ab | |||
| d663fda862 | |||
| 36ac91f057 | |||
| 18cce177a8 | |||
| 2562172adc | |||
| 10bfb1c565 | |||
| d282871393 | |||
| 03a7eb89e2 | |||
| a856451243 | |||
| bc92fb32c0 | |||
| e1a5fbf7f6 | |||
| dc11aa99c0 | |||
| ca23406974 | |||
| 011c1faeb4 | |||
| b118cef26f | |||
| 46649e5d97 | |||
| 4fa7e5d454 | |||
| 6710396aca | |||
| 3fe89b4f7b | |||
| 023331ec2a | |||
| 97f327317f | |||
| 14f35ab055 | |||
| 9777b51ee2 | |||
| 434545a11f | |||
| e58f0be8f0 | |||
| f85695cb7b | |||
| 66f2613416 | |||
| 793232fe76 | |||
| 3ad7463b60 | |||
| 5ea9f5bdd1 | |||
| 757b3ed1b0 | |||
| c13f890972 | |||
| 99ee2fb09a | |||
| d009ac8fba | |||
| 5c384a1bca | |||
| 0f450fe265 | |||
| c2a7c69f9d | |||
| 676e227b47 | |||
| 1de4bf1b59 | |||
| 7016e8f1d9 | |||
| 0f4fec30f0 | |||
| ff1f796032 | |||
| f1a95869eb | |||
| 4cd3d284e9 | |||
| 0f4be6d17b | |||
| 6ce428511a | |||
| ab3b93e8dd | |||
| 3d400116b9 | |||
| f41d5f021a | |||
| 7174a09d3c | |||
| 180fa47123 | |||
| 1585b8dd4e | |||
| 200b193282 | |||
| c07d0e6448 | |||
| aabda13a10 | |||
| dcb9fac577 | |||
| 2b674827c3 | |||
| a5a775b79f | |||
| 9736056a60 | |||
| 8a53df6338 | |||
| 6aa5730ad5 | |||
| d739b3c2cd | |||
| c751f81d0d | |||
| d675907654 | |||
| 72f1ce4ed4 | |||
| 5d38b04415 | |||
| d7e0621ed3 | |||
| c57d1a3219 | |||
| d87148c56b | |||
| 15338cc3da | |||
| 262e5751f4 | |||
| e153e560a1 | |||
| 445aecdeb7 | |||
| 57429d1567 | |||
| fff781cf15 | |||
| 8f8d97e615 | |||
| c243f39ce5 | |||
| f2fe51a9d2 | |||
| a56b1a5518 | |||
| 1c630c3e3c | |||
| 32cb83408a | |||
| d8911c2885 | |||
| 55487f33b1 | |||
| 1c6db08158 | |||
| e54d7951f2 | |||
| 82ca6d4185 | |||
| f3c60dc571 | |||
| c4166a9efc | |||
| 5adce5266f | |||
| 3d176e926f | |||
| 3a7cd05b48 | |||
| 8ef4a3da52 | |||
| 1b7800aceb | |||
| b5746331f6 | |||
| 3e933ca0ed | |||
| 53e541f7bf | |||
| fbc9a18f0a | |||
| 8a50f5dfc8 | |||
| c3c634f4b1 | |||
| 710d8e4dd3 | |||
| 887576b113 | |||
| 2ad3544b01 | |||
| 68bde67d0a | |||
| 0edecdd09e | |||
| 5d97657375 | |||
| cf8947da79 | |||
| c6bf7558b2 | |||
| 429e253fb6 | |||
| e5529d98fe | |||
| 6e03136116 | |||
| 4973fe3069 | |||
| cfa1f7c3bc | |||
| 538d368396 | |||
| 590a5a5382 | |||
| fa585c5151 | |||
| 7ae2f84783 | |||
| 088dbe6866 | |||
| 6fb7ba721c | |||
| 7aa3114d9f | |||
| 8bfd851133 | |||
| 9120a7251d | |||
| fdb9561ade | |||
| a5ec992b1f | |||
| 2c432ffeb3 | |||
| 0ab4b647f8 | |||
| 9f4734cbf1 | |||
| ac1a09c787 | |||
| c10945f53a | |||
| 309f586424 | |||
| 1f5455e29e | |||
| a90af6f22e | |||
| 38719480a8 | |||
| aa18604fec | |||
| 875bc59ecf | |||
| 89f0527f31 | |||
| 8c0c3774e6 | |||
| 9c93d8ec06 | |||
| 1ccad186fd | |||
| e8cd940cf8 | |||
| c39546ee10 | |||
| 8f75744379 | |||
| ea47e6de27 | |||
| a6525be4fc | |||
| 833c0b23f5 | |||
| a04d968422 | |||
| 7b261704cf | |||
| 97b0f9f6e4 | |||
| 3071e0de2f | |||
| fe72033b2e | |||
| b57b9d3f8e | |||
| 50b4f7fad5 | |||
| 6b05aebc0c | |||
| 5863fc74b1 | |||
| 293940600b | |||
| f0369909d0 | |||
| 9db82605d2 | |||
| c0d95fd6c2 | |||
| 150e0fa8a6 | |||
| 45ad509611 | |||
| cbefd323be | |||
| 0f0d837178 | |||
| a6c9227372 | |||
| f6b804263a | |||
| b5df1c6170 | |||
| 44bf298ec3 | |||
| 5d575a3eff | |||
| e63372e41f | |||
| 9ee0c8e634 | |||
| 431c76abc4 | |||
| 21023f7f9c | |||
| 1746ac463a | |||
| f573a86b42 | |||
| 0e18cb3efa | |||
| 50f7f03f6b | |||
| 3d4a48b120 | |||
| ffe013033e | |||
| 00cbeecf6c | |||
| 737bea8f39 | |||
| c2e36f369b | |||
| 883f74ca41 | |||
| d8b226f26b | |||
| c4f3155d19 | |||
| 72c7716902 | |||
| 1bc79f66f9 | |||
| 2b3eae5f08 | |||
| 497cff9772 | |||
| fdb20dbc7e | |||
| 942ed44fdd | |||
| 8bc3f4d916 | |||
| dcbc2b933a | |||
| d0b4779071 | |||
| 12d068f675 | |||
| 070609cbac | |||
| 6b1bf7d917 | |||
| fefb70b217 | |||
| 48d8853cad | |||
| 2a3d4722c2 | |||
| d137d00182 | |||
| 9333179ad9 | |||
| 0c842391d3 | |||
| 1fcb74f218 | |||
| 1278bf9cfa | |||
| 18e5b75f67 | |||
| 766572b5b8 | |||
| 1de4d7cfba | |||
| 04107252f2 | |||
| 5f0e0d6c38 | |||
| 79406f111b | |||
| 30fa61d457 | |||
| 914d976801 | |||
| 839d4f108e | |||
| 5e73db6c00 | |||
| e9b8424839 | |||
| e3dd5d7ca5 | |||
| 2752a58730 | |||
| 69eb5752ce | |||
| 9090704f1d | |||
| fa9f863dbf | |||
| 4b1b706aa4 | |||
| 5e75b8506c | |||
| bc9f341165 | |||
| b40b8a00e4 | |||
| d811048887 | |||
| 0ba2626bd2 | |||
| c6eea03c8d | |||
| 0fd0218ef0 | |||
| 5e1a5d07c4 | |||
| 518fa3aa44 | |||
| ef1da479ec | |||
| 3f12d7ae44 | |||
| c4579a9c43 | |||
| ff62eb251b | |||
| 2b336756b6 | |||
| 0f0d8be884 | |||
| 3f3e0aa90e | |||
| c0ee5b859c | |||
| edae178532 | |||
| a0a0308061 | |||
| 74329d0c1d | |||
| 5777355972 | |||
| 0272587c29 | |||
| 22d3c8810c | |||
| 4aa8aa100b | |||
| bd404b1c88 | |||
| bed17efae8 | |||
| 8d0199ed42 | |||
| 9f33f8af5f | |||
| 7c4f033c6a | |||
| 7e2e7a5e5a | |||
| 909fbd19ea | |||
| 2b27ab1c9e | |||
| d244d38047 | |||
| 2a0b112d08 | |||
| d9657b70c0 | |||
| e8939f43a6 | |||
| 8e587fb17a | |||
| 757ae95cca | |||
| cc6e65d145 | |||
| df464c103e | |||
| d517b55576 | |||
| 4c1353c188 | |||
| 4871208f02 | |||
| 08566f22c7 | |||
| ea7b37a42a | |||
| 302eef7bad | |||
| 825adad18d | |||
| 4491212da4 | |||
| 52e2f3ed64 | |||
| 1de4fe0ba8 | |||
| e1208f5b9f | |||
| 0a8ac44617 | |||
| 1fd2108bcf | |||
| ad5e29a2b7 | |||
| 93b7dd9915 | |||
| 9256b76226 | |||
| fbd12f7d44 | |||
| 90c4895a7a | |||
| 6dfa8b1d60 | |||
| 5430844453 | |||
| 9dd06082e7 | |||
| cb4f93913e | |||
| 149946c706 | |||
| 78988b5cd6 | |||
| a081e049db | |||
| 68af216772 | |||
| b4a532e970 | |||
| 3bf913dc35 | |||
| 55163b5999 | |||
| 9bfbe6ae3e | |||
| b23e873e0f | |||
| 79ec9b8079 | |||
| 24d6cca275 | |||
| 83b90c172c | |||
| 57f2f11005 | |||
| bf502be578 | |||
| dfeca151a2 | |||
| 2ddd2bd557 | |||
| b2aebb30bf | |||
| 0a9c0ca461 | |||
| c0b32a9a04 | |||
| 703d5a1298 | |||
| c5cc96a4f4 | |||
| de256cb5d5 | |||
| ce814302ac | |||
| 628bc6e03e | |||
| dfb606bb19 | |||
| 88f3b3f75e | |||
| 773d4ce8cb | |||
| 509d6d8235 | |||
| 7bd5c862a2 | |||
| 2878f60634 | |||
| 50fdb87888 | |||
| dad7790ec3 | |||
| 709bc5e15a | |||
| 06cc6d7fea | |||
| 97484ec9c1 | |||
| 6b04add932 | |||
| 04ea01f385 | |||
| 8653060ae6 | |||
| 8df3effa5f | |||
| 771010f43b | |||
| 8d20bac7fa | |||
| c4c4fac1ae | |||
| 016d85c9e6 | |||
| 3ab29205fc | |||
| fdd150eb4d | |||
| 789a2be8d9 | |||
| ae7ef37c1b | |||
| 94fba3d8f0 | |||
| 6ac9af0a5a | |||
| e916f748db | |||
| 92e8a20761 | |||
| cb3de665d1 | |||
| 49a5cdf76d | |||
| 08a090de43 | |||
| fa3b17cd96 | |||
| 5266fdacf1 | |||
| b74c5953f0 | |||
| 00e8d20eae | |||
| e8e63e307e | |||
| 7db6de848a | |||
| 8360ef3a5a | |||
| d32b8fa4bd | |||
| c95c32e473 | |||
| 02fe371839 | |||
| b7b7c2ea94 | |||
| 105dd031dd | |||
| 4fa289166a | |||
| a8bbcf611f | |||
| d5030b1f8c | |||
| f127b2f81d | |||
| 9d4087a1e9 | |||
| fd326ddf1b | |||
| 7f42253f46 | |||
| 18d7e5e6e4 | |||
| 6610880fd4 | |||
| 11b73ae6b4 | |||
| 2b51317be8 | |||
| 650c2c8cf9 | |||
| 49386e8da4 | |||
| 71494ffdbc | |||
| a9b6bef062 | |||
| 4840e435f7 | |||
| 531147c877 | |||
| 61c21ef9ee | |||
| 058e54061b | |||
| 32be731194 | |||
| 9bf55395f1 | |||
| 114b82a212 | |||
| 7d143370d8 | |||
| bc6880fa34 | |||
| c6d2ccd453 | |||
| cdab739471 | |||
| fee03bd5a6 | |||
| 6fd2d43bfe | |||
| 40fd415409 | |||
| 9c7020f7e7 | |||
| 556399cc48 | |||
| bef888c2d8 | |||
| a89dabe0cd | |||
| 80fbbc3a6a | |||
| 7a6ee934e1 | |||
| 8b11f13507 | |||
| 4401120ca6 | |||
| 8dd61c1dc4 | |||
| 6849589430 | |||
| 4cd83631ee | |||
| 028aae19bf | |||
| 41741c38e5 | |||
| 3feca20c59 | |||
| f1bc3c03ed | |||
| 66e5944799 | |||
| 6ffa6f39e6 | |||
| 94ee8e1570 | |||
| 3e95633b1f | |||
| 70ebb15a33 | |||
| d745d9ee96 | |||
| b89a93faae | |||
| 044071f0d5 | |||
| 79c1331432 | |||
| 86f28494a5 | |||
| d53a1cd0c0 | |||
| 2c96373a41 | |||
| 731e1bb206 | |||
| c1a72b3644 | |||
| e52740f09a | |||
| 5dd8c5c10c | |||
| 169c0896d6 | |||
| 1bc0468ada | |||
| 9a411f367d | |||
| 6074a18ec4 | |||
| 0e7f3e04b0 | |||
| 53552b1d6e | |||
| d7d1db5d79 | |||
| 9d7a2338b4 | |||
| 6e42b0e4a7 | |||
| ef7911310d | |||
| 999f402829 | |||
| 85c2d28e99 | |||
| 6b7421c448 | |||
| 7df184d3aa | |||
| 197005a791 | |||
| 52ee2380e4 | |||
| 530eff62e5 | |||
| 4de7eaa6a8 | |||
| 8281988842 | |||
| 4ed7138685 | |||
| 6689189819 | |||
| 0ce7e4976a | |||
| 6b18a908b8 | |||
| 570fdf31c5 | |||
| 929669bd1b | |||
| 240fd5b68e | |||
| 1d0d79f61a | |||
| b5dddeb419 | |||
| 9194052a94 | |||
| e0d871b7dc | |||
| c455a19f8e | |||
| d864512631 | |||
| 6ee5d61c91 | |||
| 04df170bea | |||
| 5f58a6d2ca | |||
| ffff5e99aa | |||
| 8fab33c245 | |||
| 3bf8964355 | |||
| a3697d097d | |||
| 51c85dd8d6 | |||
| 31f41b9822 | |||
| 458576bbe7 | |||
| e3a64cc8a7 | |||
| 9045616bda | |||
| 25dbe8097f | |||
| fb6a2941b9 | |||
| ed131973ef | |||
| 43060d8c7d | |||
| d5f1250a8b | |||
| 4c01c0c4d7 | |||
| af28101af1 | |||
| 56aa9f364a | |||
| f0d9867d09 | |||
| cfc9b4d41d | |||
| de66211afb | |||
| 414d5f0978 | |||
| 99bd066f38 | |||
| 82a22b20fc | |||
| 25ed701dbd | |||
| 875c521413 | |||
| 7b8363632e | |||
| 06f18fa1b9 | |||
| 54fc646537 | |||
| b2e3780e8c | |||
| 0b04ac3117 | |||
| 90d0eb9b88 | |||
| f2aa89f443 | |||
| 2a319c7255 | |||
| 4fb3f1b3f3 | |||
| 072d33599b | |||
| 56f3c85b87 | |||
| 8b42fff90e | |||
| 1dc5d43d32 | |||
| ee2d08ff79 | |||
| 305b3bed74 | |||
| 9f6acd960c | |||
| 672890b1c8 | |||
| c58bcc2c02 | |||
| 82318263a1 | |||
| d90e1db50b | |||
| 8af0264a77 | |||
| 8193287e08 | |||
| 13bd33e73f | |||
| b2e8d5ab7c | |||
| a375cb322f | |||
| d9c4d8a76a | |||
| 79edae58d5 | |||
| 6675776640 | |||
| 40685c3b2a | |||
| 25874ceab2 | |||
| 4b2093ef67 | |||
| 9bc2e60fd5 | |||
| 685ce7573d | |||
| f5ad1c5753 | |||
| cc92025fdc | |||
| f05cd95fad | |||
| 4325843ef0 | |||
| 607635d2ce | |||
| b8fddc862e | |||
| 0df0177437 | |||
| f90cbcd1e3 | |||
| 870d7f7f93 | |||
| 799bec66a2 | |||
| 2321fbbc1d | |||
| 48ae7217e4 | |||
| 6f54b233f1 | |||
| 1bf1055395 | |||
| 6417d90d5c | |||
| c939cebf0d | |||
| 7ae36d132a | |||
| c478409dad | |||
| 109441a708 | |||
| b267e8293d | |||
| 3a4c683d5c | |||
| d5649da5f8 | |||
| 9c28d21b4f | |||
| 9e58b8237b | |||
| b184c76205 | |||
| 065fb2a74c | |||
| a0a0d42630 | |||
| 756153899a | |||
| e02554412f | |||
| ca37e806b9 | |||
| fe0347dbf0 | |||
| 4984c5fc7c | |||
| f605769af9 | |||
| 534f6b7975 | |||
| ee8fd78383 | |||
| fbc4f37037 | |||
| f23f2ff2c9 | |||
| d0659327bd | |||
| 88b301f182 | |||
| d5e16807d2 | |||
| 79a2bcd05f | |||
| e582f9dcac | |||
| 4cefd6136b | |||
| 1cf04b7a10 | |||
| ad3db301f2 | |||
| e15eb40317 | |||
| 8459d0403c | |||
| 08014eea36 | |||
| c0b27108d0 | |||
| 40612facf3 | |||
| e418fc6937 | |||
| 045d442fcd | |||
| 2370f9f1db | |||
| 519d1e7420 | |||
| 82afc713d0 | |||
| a1e9a8addd | |||
| f59736a06c | |||
| d187059596 | |||
| 7d2f0b1ba8 | |||
| 6a6d939dea | |||
| 090b8b7f99 | |||
| e4dda27de1 | |||
| f2786d9d80 | |||
| c6daa24e3c | |||
| 2f4eed1f0f | |||
| acc5c45feb | |||
| 00c3335071 | |||
| 65e4c8e76e | |||
| d4f5dff8ee | |||
| 8d9cb782fb | |||
| 02ff1d4462 | |||
| 007d2c2e25 | |||
| 3bf7637986 | |||
| 33ff9dbce2 | |||
| f25e894558 | |||
| 52c1a7456f | |||
| b2392413fa | |||
| 1941eaabe0 | |||
| 3d5bf9753f | |||
| a4d191d4f9 | |||
| dad54ec211 | |||
| b525f5f4d7 | |||
| e8190a8d8d | |||
| 4e155139ca | |||
| 458edeed9a | |||
| 04d785f4bf | |||
| 28d9c0c511 | |||
| 91310971b9 | |||
| 5d2acf4897 | |||
| dc98019d49 | |||
| b008bb35cc | |||
| 46d5b197e0 | |||
| 2c510530b1 | |||
| ec6eda77ad | |||
| 4805e5856b | |||
| 55447cbb3d | |||
| 69d5139b8c | |||
| 89f1e05147 | |||
| bc779df8b7 | |||
| e3c260e7d3 | |||
| 0af7e004c7 | |||
| 447445388e | |||
| b2c66816d7 | |||
| b6f81c6cc3 | |||
| 98b289630a | |||
| d68c0bd795 | |||
| 5afda71f74 | |||
| 1b08a8d675 | |||
| b508ab64bd | |||
| 84f435e24b | |||
| 984ad34a61 | |||
| ad3231c29a | |||
| c3d20bbc53 | |||
| f9c03f183f | |||
| 046a3c8a28 | |||
| 05883934f1 | |||
| 97d2a73dd3 | |||
| 5367a44acb | |||
| 1deaf71388 | |||
| 99f564e972 | |||
| c725f8d354 | |||
| 257ace722c | |||
| 0cd9d46828 | |||
| cef9e28a6c | |||
| 6c42da2abf | |||
| a9fc2bed49 | |||
| 1855c49d1f | |||
| cce65ce34d | |||
| 70866c0154 | |||
| d06e3753b0 | |||
| cb4de1f859 | |||
| b6d23b2e2d | |||
| 6a8815de0c | |||
| e0179bad2f | |||
| 8778add0d6 | |||
| facc823612 | |||
| b91854ea9d | |||
| 05abe814ac | |||
| ea561ba6d8 | |||
| df84c69676 | |||
| 3726aba2ee | |||
| f6bcaffe4a | |||
| c689b52dd1 | |||
| 09d75a4347 | |||
| 59bd247603 | |||
| f221ef952f | |||
| d3c75e1d34 | |||
| 3aab55d29f | |||
| f9ef72c38a | |||
| 108159ed17 | |||
| defa1283c4 | |||
| 2788b60fe6 | |||
| 7e70e1768f | |||
| 896ba77061 | |||
| c034262b78 | |||
| b7edcf6eea | |||
| 23e1ad2df7 | |||
| 0a3939883a | |||
| 3c8f91ee3d | |||
| efa5b04797 | |||
| 2da66ed009 | |||
| 2ac6811362 | |||
| 74c51f213c | |||
| 4302d8060d | |||
| 576cf8978b | |||
| 3533912016 | |||
| cf9922ff1d | |||
| 4fa65fbb2f | |||
| f502ee2338 | |||
| 7a56925176 | |||
| 0a108b3fb2 | |||
| 381a108e6d | |||
| 726c9fc8a6 | |||
| 946ccd3228 | |||
| 8e1ebbfc11 | |||
| cc0e60c101 | |||
| ff3f00d845 | |||
| 40195c2fa2 | |||
| 7f7300b8cb | |||
| 1b158ff4ed | |||
| b686b85b52 | |||
| 8fa82ae5cb | |||
| 0d5289141e | |||
| 01d5e7bc47 | |||
| cfbaec60c7 | |||
| f3e7245910 | |||
| 892d9fae84 | |||
| 796f895f01 | |||
| 489bb4eb10 | |||
| 8f458066bb | |||
| 5dd7454260 | |||
| 571db82371 | |||
| d971e0cca5 | |||
| fde0aac733 | |||
| b9d904c12f | |||
| aa2ec42da6 | |||
| d90d473104 | |||
| 5a1e63990a | |||
| e836c10c6f | |||
| 47c09d9557 | |||
| b35b943364 | |||
| ca467cc50e | |||
| 51f7cf0367 | |||
| 642eaca618 | |||
| 55e5680535 | |||
| 52ea31b65c | |||
| b3a26a5b30 | |||
| 98974efa5f | |||
| b6a776b242 | |||
| 1ea3f44f06 | |||
| 64e1320ca0 | |||
| 6e0b50fbdc | |||
| 22502a8fe8 | |||
| a78ad01bb4 | |||
| 729e802e85 | |||
| 3ffff6d579 | |||
| 6e5f97fca5 | |||
| eaff5bdfd7 | |||
| 28819d36a4 | |||
| f9a4f6f306 | |||
| 27c83c693d | |||
| d106908a57 | |||
| b4adce34dc | |||
| 3927505d1a | |||
| ede79f818e | |||
| 742ac53262 | |||
| ee17ccc374 | |||
| 835a02c037 | |||
| ee8ff00a2a | |||
| 229f13a864 | |||
| 0d60d637af | |||
| c20e34a8b0 | |||
| 8d3f39852a | |||
| aa45dee5a4 | |||
| 885e6e621b | |||
| dc122c31ef | |||
| 3bc80d3db4 | |||
| 439f2f3b2b | |||
| a1610eb274 | |||
| 6b90eff03c | |||
| 4d404d1a54 | |||
| fffa6a80ca | |||
| 3f6b38b34f | |||
| 8166a55761 | |||
| 89f6d374e9 | |||
| 9b3c2cf348 | |||
| 76406dd0c2 | |||
| d266b75423 | |||
| 5bb5eb1657 | |||
| 258cf3b0f7 | |||
| cdb5b09cd7 | |||
| cb3469215a | |||
| 703c2925b3 | |||
| b6ca3ef051 | |||
| b235f91cc7 | |||
| 3513472467 | |||
| 60e0c96f6c | |||
| e37df7ca85 | |||
| a13a35fe52 | |||
| f421600218 | |||
| 2a4492d74f | |||
| 10368d867f | |||
| 936360020c | |||
| c53c64d7fa | |||
| 6b122ba25f | |||
| 6501b587c0 | |||
| 53aaa842ed | |||
| dc569e952d | |||
| c9aee4126e | |||
| 7397f4b0d0 | |||
| 3b83a1b1ac | |||
| 8f8e4574dc | |||
| c30432a665 | |||
| c4c2d8bbd4 | |||
| e49ba233f9 | |||
| cea6c1821f | |||
| ab4bf447c6 | |||
| 4e0c8cf25b | |||
| e4b3a052a4 | |||
| 825beb42c4 | |||
| c8d605db55 | |||
| f6ecab58cb | |||
| d7e39347b9 | |||
| 25c10af596 | |||
| ded23f14c7 | |||
| b0d52d930a | |||
| af5c5b6a55 | |||
| ce51e19970 | |||
| 97e31b6090 | |||
| 62053e68e2 | |||
| 489c07e748 | |||
| 604ea8d68a | |||
| fd3cfb196b | |||
| b71f6ba864 | |||
| dfc128b89a | |||
| 34b8b57c2f | |||
| 3bba409d9e | |||
| 7869cdccec | |||
| 0e18e345b0 | |||
| e5b99c7512 | |||
| 7d4c85018a | |||
| edbec2dbc9 | |||
| 973ece9809 | |||
| 90f441a6a0 | |||
| 5a71090476 | |||
| 76cae0ec44 | |||
| 273f0dda9d | |||
| 882b5a1d89 | |||
| 8c84ad1a86 | |||
| 80bfec7253 | |||
| 91b930298b | |||
| 9c56b91548 | |||
| 7b5bab83f4 | |||
| c5e2116ead | |||
| d9db73a791 | |||
| 01ece4ef7b | |||
| 601f3e7cdb | |||
| a9ca2c547f | |||
| 594cbed03b | |||
| 3938a905a1 | |||
| 88d523e01b | |||
| 0419fe67fc | |||
| 33ddeb5cbe | |||
| 1f5d5b391b | |||
| 198c515208 | |||
| 5156673e17 | |||
| 1529c9c438 | |||
| 32b10a8832 | |||
| 24501d4361 | |||
| bf0c08e24a | |||
| 34d8cce6bc | |||
| f0bfc24adc | |||
| 2f8acfe4bf | |||
| e2fb8b2786 | |||
| ebbc4d9fb8 | |||
| 8fc5b90e9a | |||
| 8a717f5b6c | |||
| aa91994166 | |||
| 2091347a71 | |||
| e0ed174f2c | |||
| 8c2a573ebf | |||
| d7ff7cde92 | |||
| 15d0b0ea08 | |||
| 3695bc2db5 | |||
| a08995a90d | |||
| aea00258e7 | |||
| b581eb3f27 | |||
| 610ccba9f5 | |||
| d5ae6f32dd | |||
| 5308033936 | |||
| e2abb5ef2c | |||
| 1b11b4eeb6 | |||
| 39357b3045 | |||
| ed7a5a1418 | |||
| ae682a71f9 | |||
| 8327b37a0b | |||
| 973b5570aa | |||
| 7cb41fc5cc | |||
| 595d67ad7d | |||
| bb626c120e | |||
| ba8fefa8ec | |||
| 4b24f6d7b1 | |||
| 1c460e1e08 | |||
| 7b4e157356 | |||
| 5749f1b971 | |||
| 3c57aff85b | |||
| 18504bcc86 | |||
| d8864bfe48 | |||
| 078b20169b | |||
| 5f7e78df65 | |||
| fc470db7ab | |||
| f576f37801 | |||
| b74118a766 | |||
| 1c7a0248b9 | |||
| 36a829c20d | |||
| 33af75aa39 | |||
| 844420425e | |||
| da57a530f9 | |||
| 1f17013949 | |||
| f18899cb36 | |||
| 877f946e24 | |||
| a981a8c42c | |||
| 5467107fc9 | |||
| ad3107073b | |||
| 8d62f4da6c | |||
| 3779b8a008 | |||
| 6ec5e48969 | |||
| bfa5ca553d | |||
| c9f7d970e9 | |||
| f26ce6e236 | |||
| b001e36f18 | |||
| 9abb6ef723 | |||
| bfbdbb05bc | |||
| bd2bd51b5d | |||
| 4e547a31ed | |||
| de8d0defcd | |||
| 344437c491 | |||
| ed365e94fd | |||
| 5910278ca8 | |||
| 18841fa58d | |||
| 6fb4e0e441 | |||
| 39051ef3ca | |||
| 1f4084870b | |||
| 00e9d5b219 | |||
| 7f93747602 | |||
| a7156b8c27 | |||
| b1e47f7741 | |||
| 59f8d6ca22 | |||
| 5f4019d980 | |||
| f84389da08 | |||
| 63c1757df5 | |||
| d6ab850f45 | |||
| 61dd53e262 | |||
| 423a633b5b | |||
| 256d4ef71b | |||
| ad49962ba9 | |||
| 4680d70a78 | |||
| ee7f056779 | |||
| 66c8d7baf2 | |||
| 9f929999d1 | |||
| 24f96262ec | |||
| 0e6e7a41f4 | |||
| 5cac088d98 | |||
| 85f0448fee | |||
| 106c0b753a | |||
| c525e634dc | |||
| c398c0891b | |||
| 5ab48ac5d4 | |||
| ba29cd8e46 | |||
| b61235b77f | |||
| 0ed00e38f0 | |||
| 36eef0dd9a | |||
| 1904194c7a | |||
| 7ce144881a | |||
| 55159cf451 | |||
| 7a12fd0f85 | |||
| 9d60126661 | |||
| e341e73c6a | |||
| 5dad3786f6 | |||
| ed0cd2c60d | |||
| 2eea3a4c5d | |||
| 090a46763e | |||
| c4ed82cdf6 | |||
| b32248d615 | |||
| ca7437502b | |||
| fe9b797a46 | |||
| b8059aeaba | |||
| 85f80714c2 | |||
| 2cc9ebf28b | |||
| cb5d69c769 | |||
| 3b961a6b7b | |||
| cba3ea9d90 | |||
| 57ea065db7 | |||
| 4f5f88b9ba | |||
| c1c2b330a1 | |||
| 05e1d8e5f4 | |||
| 3cbca7bdba | |||
| 3e3c210f1d | |||
| cb65139aa8 | |||
| 1206120d10 | |||
| 80ebe80138 | |||
| fa1d6b478e | |||
| 345413fb8c | |||
| 66ebd2a843 | |||
| d50f469c09 | |||
| 26714bc635 | |||
| 30208ae08b | |||
| 6ea3188971 | |||
| 1db555a530 | |||
| 0772210dea | |||
| df42e997b7 | |||
| 1e71732600 | |||
| 141e05e3a7 | |||
| cadd3e4e2c | |||
| 09034d9e17 | |||
| 5706d1d688 | |||
| 41b9777746 | |||
| d31fe1ac34 | |||
| c0eedfeca0 | |||
| dc8b5509f3 | |||
| ddec052dab | |||
| 2e45022c95 | |||
| cec7f73bca | |||
| 30989dc997 | |||
| 50a0d1cad4 | |||
| 3db1f132a7 | |||
| 3b196feda5 | |||
| dd766c68d9 | |||
| 599e070824 | |||
| 04c998a742 | |||
| cc985c3a9c | |||
| 36cc508030 | |||
| 444cd56740 | |||
| 88a86f7e45 | |||
| d6f94c0bc9 | |||
| 2157aa6172 | |||
| 2013527840 | |||
| 8c73c6f218 | |||
| 63059f6063 | |||
| e179198410 | |||
| ed4a95bdad | |||
| 1baddb9094 | |||
| 3160a445a8 | |||
| d627fd8781 | |||
| b4bdc5a0fa | |||
| 6911fa2cba | |||
| 0d7c8711bd | |||
| d8b0fe0957 | |||
| 263de77a5a | |||
| d3615e682e | |||
| 35da9d6ef2 | |||
| dbe7662e72 | |||
| 30118bbab0 | |||
| 2902149f77 | |||
| eb8b40cccd | |||
| 76da13dff6 | |||
| f7cbdff79c | |||
| 81233b3cd3 | |||
| af88e051fa | |||
| 6ad6b19bd6 | |||
| 0242ca59ac | |||
| c064963ef8 | |||
| 4318718769 | |||
| f981bdb551 | |||
| 3860e078a5 | |||
| d09e2a67bb | |||
| 56ba6b9c7e | |||
| a9c6f26412 | |||
| 73817b8b77 | |||
| 3f128b9838 | |||
| 9621bb5b8e | |||
| 836fb03aa0 | |||
| c3aaf50b64 | |||
| fe00f5ff64 | |||
| 8b3543fca9 | |||
| a6fe2ae341 | |||
| 3ca7751445 | |||
| ebfde534c0 | |||
| f8c7dbb758 | |||
| 62f9053330 | |||
| c429e651c1 | |||
| dacf017d38 | |||
| dc3c1488bb | |||
| 4d7ff76cfb | |||
| 8ad6865952 | |||
| 2a4f6b942d | |||
| 91a819fb34 | |||
| 52dbeb1f26 | |||
| 0836e47dfc | |||
| 75bef59016 | |||
| 337c0c66cf | |||
| f8e2df16f1 | |||
| 10deb8f267 | |||
| efe5916109 | |||
| 64449c196e | |||
| f57128bd3d | |||
| 8740791d5c | |||
| 6ddb5a0452 | |||
| 133699c2f3 | |||
| b0ea92bc12 | |||
| b587aeee1c | |||
| e754581ecb | |||
| fcb6ae8eed | |||
| bf4dab3501 | |||
| a066cf8680 | |||
| 7448dcea65 | |||
| cf3b3dff32 | |||
| ca96737b20 | |||
| 61ade48343 | |||
| 295bfe4e3a | |||
| 1145fec39f |
@@ -0,0 +1,19 @@
|
||||
# Configuration for probot-stale - https://github.com/probot/stale
|
||||
|
||||
# Number of days of inactivity before an Issue or Pull Request becomes stale
|
||||
daysUntilStale: 90
|
||||
# Number of days of inactivity before a stale Issue or Pull Request is closed
|
||||
daysUntilClose: 30
|
||||
# Issues or Pull Requests with these labels will never be considered stale
|
||||
exemptLabels:
|
||||
- bug
|
||||
- Announcement
|
||||
- help wanted
|
||||
- To investigate
|
||||
# Label to use when marking as stale
|
||||
staleLabel: stale
|
||||
# Comment to post when marking as stale. Set to `false` to disable
|
||||
markComment: >
|
||||
This issue has been automatically marked as stale because it has not had
|
||||
recent activity. It will be closed after 30 days if no further activity
|
||||
occurs, but feel free to re-open a closed issue if needed.
|
||||
@@ -8,8 +8,10 @@ keras/datasets/data/*
|
||||
keras/datasets/temp/*
|
||||
docs/site/*
|
||||
docs/theme/*
|
||||
docs/sources/*
|
||||
tags
|
||||
Keras.egg-info
|
||||
examples/img/*
|
||||
|
||||
# test-related
|
||||
.coverage
|
||||
@@ -17,3 +19,4 @@ Keras.egg-info
|
||||
|
||||
# developer environments
|
||||
.idea
|
||||
.vscode
|
||||
|
||||
+38
-19
@@ -3,18 +3,24 @@ dist: trusty
|
||||
language: python
|
||||
matrix:
|
||||
include:
|
||||
- python: 3.4
|
||||
env: KERAS_BACKEND=theano
|
||||
- python: 3.4
|
||||
env: KERAS_BACKEND=tensorflow
|
||||
- python: 2.7
|
||||
env: KERAS_BACKEND=theano
|
||||
env: KERAS_BACKEND=tensorflow TEST_MODE=PEP8
|
||||
- python: 2.7
|
||||
env: KERAS_BACKEND=tensorflow TEST_MODE=INTEGRATION_TESTS
|
||||
- python: 3.5
|
||||
env: KERAS_BACKEND=tensorflow TEST_MODE=DOC
|
||||
- python: 2.7
|
||||
env: KERAS_BACKEND=tensorflow
|
||||
- python: 3.5
|
||||
env: KERAS_BACKEND=tensorflow
|
||||
- python: 2.7
|
||||
env: KERAS_BACKEND=theano TEST_MODE=INTEGRATION_TESTS
|
||||
env: KERAS_BACKEND=theano THEANO_FLAGS=optimizer=fast_compile
|
||||
- python: 3.5
|
||||
env: KERAS_BACKEND=theano THEANO_FLAGS=optimizer=fast_compile
|
||||
- python: 2.7
|
||||
env: KERAS_BACKEND=theano TEST_MODE=PEP8
|
||||
env: KERAS_BACKEND=cntk
|
||||
- python: 3.5
|
||||
env: KERAS_BACKEND=cntk
|
||||
install:
|
||||
# code below is taken from http://conda.pydata.org/docs/travis.html
|
||||
# We do this conditionally because it saves us some downloading if the
|
||||
@@ -34,29 +40,42 @@ install:
|
||||
|
||||
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION numpy scipy matplotlib pandas pytest h5py
|
||||
- source activate test-environment
|
||||
- pip install pytest-cov python-coveralls pytest-xdist coverage==3.7.1 #we need this version of coverage for coveralls.io to work
|
||||
- pip install pep8 pytest-pep8
|
||||
- pip install git+git://github.com/Theano/Theano.git
|
||||
- pip install theano
|
||||
|
||||
# install PIL for preprocessing tests
|
||||
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
|
||||
conda install pil;
|
||||
elif [[ "$TRAVIS_PYTHON_VERSION" == "3.4" ]]; then
|
||||
elif [[ "$TRAVIS_PYTHON_VERSION" == "3.5" ]]; then
|
||||
conda install Pillow;
|
||||
fi
|
||||
|
||||
- python setup.py install
|
||||
- pip install -e .[tests]
|
||||
|
||||
# install TensorFlow
|
||||
# install TensorFlow (CPU version).
|
||||
- pip install tensorflow
|
||||
|
||||
# install cntk
|
||||
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
|
||||
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.6.0-cp27-none-linux_x86_64.whl;
|
||||
elif [[ "$TRAVIS_PYTHON_VERSION" == "3.4" ]]; then
|
||||
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.6.0-cp34-none-linux_x86_64.whl;
|
||||
pip install https://cntk.ai/PythonWheel/CPU-Only/cntk-2.0-cp27-cp27mu-linux_x86_64.whl;
|
||||
elif [[ "$TRAVIS_PYTHON_VERSION" == "3.5" ]]; then
|
||||
pip install https://cntk.ai/PythonWheel/CPU-Only/cntk-2.0-cp35-cp35m-linux_x86_64.whl;
|
||||
fi
|
||||
|
||||
#install open mpi
|
||||
- rm -rf ~/mpi
|
||||
- mkdir ~/mpi
|
||||
- pushd ~/mpi
|
||||
- wget http://cntk.ai/PythonWheel/ForKeras/depends/openmpi_1.10-3.zip
|
||||
- unzip ./openmpi_1.10-3.zip
|
||||
- sudo dpkg -i openmpi_1.10-3.deb
|
||||
- popd
|
||||
|
||||
# command to run tests
|
||||
script:
|
||||
# run keras backend init to initialize backend config
|
||||
- python -c "import keras.backend"
|
||||
# create dataset directory to avoid concurrent directory creation at runtime
|
||||
- mkdir ~/.keras/datasets
|
||||
# set up keras backend
|
||||
- sed -i -e 's/"backend":[[:space:]]*"[^"]*/"backend":\ "'$KERAS_BACKEND'/g' ~/.keras/keras.json;
|
||||
- echo -e "Running tests with the following config:\n$(cat ~/.keras/keras.json)"
|
||||
@@ -64,8 +83,8 @@ script:
|
||||
PYTHONPATH=$PWD:$PYTHONPATH py.test tests/integration_tests;
|
||||
elif [[ "$TEST_MODE" == "PEP8" ]]; then
|
||||
PYTHONPATH=$PWD:$PYTHONPATH py.test --pep8 -m pep8 -n0;
|
||||
elif [[ "$TEST_MODE" == "DOC" ]]; then
|
||||
PYTHONPATH=$PWD:$PYTHONPATH py.test tests/test_documentation.py;
|
||||
else
|
||||
PYTHONPATH=$PWD:$PYTHONPATH py.test tests/ --ignore=tests/integration_tests;
|
||||
PYTHONPATH=$PWD:$PYTHONPATH py.test tests/ --ignore=tests/integration_tests --ignore=tests/test_documentation.py --cov=keras tests/ --cov-fail-under 80 --cov-report term-missing;
|
||||
fi
|
||||
after_success:
|
||||
- coveralls
|
||||
|
||||
+37
-12
@@ -19,6 +19,7 @@ To easily update Theano: `pip install git+git://github.com/Theano/Theano.git --u
|
||||
|
||||
The more information you provide, the easier it is for us to validate that there is a bug and the faster we'll be able to take action. If you want your issue to be resolved quickly, following the steps above is crucial.
|
||||
|
||||
---
|
||||
|
||||
## Requesting a Feature
|
||||
|
||||
@@ -30,35 +31,59 @@ You can also use Github issues to request features you would like to see in Kera
|
||||
|
||||
3. After discussing the feature you may choose to attempt a Pull Request. If you're at all able, start writing some code. We always have more work to do than time to do it. If you can write some code then that will speed the process along.
|
||||
|
||||
|
||||
---
|
||||
|
||||
## Requests for Contributions
|
||||
|
||||
[This is the board](https://github.com/fchollet/keras/projects/1) where we list current outstanding issues and features to be added. If you want to start contributing to Keras, this is the place to start.
|
||||
|
||||
|
||||
---
|
||||
|
||||
## Pull Requests
|
||||
|
||||
We love pull requests. Here's a quick guide:
|
||||
**Where should I submit my pull request?**
|
||||
|
||||
1. If your PR introduces a change in functionality, make sure you start by opening an issue to discuss whether the change should be made, and how to handle it. This will save you from having your PR closed down the road! Of course, if your PR is a simple bug fix, you don't need to do that.
|
||||
1. **Keras improvements and bugfixes** go to the [Keras `master` branch](https://github.com/fchollet/keras/tree/master).
|
||||
2. **Experimental new features** such as layers and datasets go to [keras-contrib](https://github.com/farizrahman4u/keras-contrib). Unless it is a new feature listed in [Requests for Contributions](https://github.com/fchollet/keras/projects/1), in which case it belongs in core Keras. If you think your feature belongs in core Keras, you can submit a design doc to explain your feature and argue for it (see explainations below).
|
||||
|
||||
2. Write the code. This is the hard part!
|
||||
Here's a quick guide to submitting your improvements:
|
||||
|
||||
3. Make sure any new function or class you introduce has proper docstrings. Make sure any code you touch still has up-to-date docstrings and documentation.
|
||||
1. If your PR introduces a change in functionality, make sure you start by writing a design doc and sending it to the Keras mailing list to discuss whether the change should be made, and how to handle it. This will save you from having your PR closed down the road! Of course, if your PR is a simple bug fix, you don't need to do that. The process for writing and submitting design docs is as follow:
|
||||
- Start from [this Google Doc template](https://docs.google.com/document/d/1ZXNfce77LDW9tFAj6U5ctaJmI5mT7CQXOFMEAZo-mAA/edit#), and copy it to new Google doc.
|
||||
- Fill in the content. Note that you will need to insert code examples. To insert code, use a Google Doc extension such as [CodePretty](https://chrome.google.com/webstore/detail/code-pretty/igjbncgfgnfpbnifnnlcmjfbnidkndnh?hl=en) (there are several such extensions available).
|
||||
- Set sharing settings to "everyone with the link is allowed to comment"
|
||||
- Send the document to `keras-users@googlegroups.com` with a subject that starts with `[API DESIGN REVIEW]` (all caps) so that we notice it.
|
||||
- Wait for comments, and answer them as they come. Edit the proposal as necessary.
|
||||
- The proposal will finally be approved or rejected. Once approved, you can send out Pull Requests or ask others to write Pull Requests.
|
||||
|
||||
|
||||
2. Write the code (or get others to write it). This is the hard part!
|
||||
|
||||
3. Make sure any new function or class you introduce has proper docstrings. Make sure any code you touch still has up-to-date docstrings and documentation. **Docstring style should be respected.** In particular, they should be formatted in MarkDown, and there should be sections for `Arguments`, `Returns`, `Raises` (if applicable). Look at other docstrings in the codebase for examples.
|
||||
|
||||
4. Write tests. Your code should have full unit test coverage. If you want to see your PR merged promptly, this is crucial.
|
||||
|
||||
5. Run our test suite locally. It's easy: from the Keras folder, simply run: `py.test tests/`.
|
||||
- You will need to install `pytest`, `coveralls`, `pytest-cov`, `pytest-xdist`: `pip install pytest pytest-cov python-coveralls pytest-xdist pep8 pytest-pep8`
|
||||
- You will need to install the test requirements as well: `pip install -e .[tests]`.
|
||||
|
||||
6. Make sure all tests are passing:
|
||||
- with the Theano backend, on Python 2.7 and Python 3.5
|
||||
- with the TensorFlow backend, on Python 2.7
|
||||
- with the Theano backend, on Python 2.7 and Python 3.5. Make sure you have the development version of Theano.
|
||||
- with the TensorFlow backend, on Python 2.7 and Python 3.5. Make sure you have the development version of TensorFlow.
|
||||
|
||||
7. We use PEP8 syntax conventions, but we aren't dogmatic when it comes to line length. Make sure your lines stay reasonably sized, though. To make your life easier, we recommend running a PEP8 linter:
|
||||
- Install PEP8 packages: `pip install pep8 pytest-pep8 autopep8`
|
||||
- Run a standalone PEP8 check: `py.test --pep8 -m pep8`
|
||||
- You can automatically fix some PEP8 error by running: `autopep8 -i --select <errors> <FILENAME>` for example: `autopep8 -i --select E128 tests/keras/backend/test_backends.py`
|
||||
- Install PEP8 packages: `pip install pep8 pytest-pep8 autopep8`
|
||||
- Run a standalone PEP8 check: `py.test --pep8 -m pep8`
|
||||
- You can automatically fix some PEP8 error by running: `autopep8 -i --select <errors> <FILENAME>` for example: `autopep8 -i --select E128 tests/keras/backend/test_backends.py`
|
||||
|
||||
8. When committing, use appropriate, descriptive commit messages. Make sure that your branch history is not a string of "bug fix", "fix", "oops", etc. When submitting your PR, squash your commits into a single commit with an appropriate commit message, to make sure the project history stays clean and readable. See ['rebase and squash'](http://rebaseandsqua.sh/) for technical help on how to squash your commits.
|
||||
8. When committing, use appropriate, descriptive commit messages.
|
||||
|
||||
9. Update the documentation. If introducing new functionality, make sure you include code snippets demonstrating the usage of your new feature.
|
||||
|
||||
10. Submit your PR. If your changes have been approved in a previous discussion, and if you have complete (and passing) unit tests, your PR is likely to be merged promptly. Otherwise, well...
|
||||
10. Submit your PR. If your changes have been approved in a previous discussion, and if you have complete (and passing) unit tests as well as proper docstrings/documentation, your PR is likely to be merged promptly. Otherwise, well...
|
||||
|
||||
---
|
||||
|
||||
## Adding new examples
|
||||
|
||||
|
||||
+6
-2
@@ -1,9 +1,13 @@
|
||||
Please make sure that the boxes below are checked before you submit your issue. Thank you!
|
||||
Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on [StackOverflow](http://stackoverflow.com/questions/tagged/keras) or [join the Keras Slack channel](https://keras-slack-autojoin.herokuapp.com/) and ask there instead of filing a GitHub issue.
|
||||
|
||||
Thank you!
|
||||
|
||||
- [ ] Check that you are up-to-date with the master branch of Keras. You can update with:
|
||||
pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps
|
||||
|
||||
- [ ] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found [here](https://www.tensorflow.org/get_started/os_setup).
|
||||
|
||||
- [ ] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
|
||||
pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps
|
||||
|
||||
- [ ] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).
|
||||
- [ ] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).
|
||||
|
||||
+5
-1
@@ -8,8 +8,12 @@ All contributions by Google:
|
||||
Copyright (c) 2015, Google, Inc.
|
||||
All rights reserved.
|
||||
|
||||
All contributions by Microsoft:
|
||||
Copyright (c) 2017, Microsoft, Inc.
|
||||
All rights reserved.
|
||||
|
||||
All other contributions:
|
||||
Copyright (c) 2015, the respective contributors.
|
||||
Copyright (c) 2015 - 2017, the respective contributors.
|
||||
All rights reserved.
|
||||
|
||||
Each contributor holds copyright over their respective contributions.
|
||||
|
||||
+59
-45
@@ -1,17 +1,17 @@
|
||||
# Keras: Deep Learning library for Theano and TensorFlow
|
||||
# Keras: Deep Learning for Python
|
||||
|
||||

|
||||
[](https://travis-ci.org/fchollet/keras)
|
||||
[](https://github.com/fchollet/keras/blob/master/LICENSE)
|
||||
|
||||
## You have just found Keras.
|
||||
|
||||
Keras is a minimalist, highly modular neural networks library, written in Python and capable of running on top of either [TensorFlow](https://github.com/tensorflow/tensorflow) or [Theano](https://github.com/Theano/Theano). It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research.
|
||||
Keras is a high-level neural networks API, written in Python and capable of running on top of [TensorFlow](https://github.com/tensorflow/tensorflow), [CNTK](https://github.com/Microsoft/cntk), or [Theano](https://github.com/Theano/Theano). It was developed with a focus on enabling fast experimentation. *Being able to go from idea to result with the least possible delay is key to doing good research.*
|
||||
|
||||
Use Keras if you need a deep learning library that:
|
||||
|
||||
- allows for easy and fast prototyping (through total modularity, minimalism, and extensibility).
|
||||
- supports both convolutional networks and recurrent networks, as well as combinations of the two.
|
||||
- supports arbitrary connectivity schemes (including multi-input and multi-output training).
|
||||
- runs seamlessly on CPU and GPU.
|
||||
- Allows for easy and fast prototyping (through user friendliness, modularity, and extensibility).
|
||||
- Supports both convolutional networks and recurrent networks, as well as combinations of the two.
|
||||
- Runs seamlessly on CPU and GPU.
|
||||
|
||||
Read the documentation at [Keras.io](http://keras.io).
|
||||
|
||||
@@ -23,11 +23,11 @@ Keras is compatible with: __Python 2.7-3.5__.
|
||||
|
||||
## Guiding principles
|
||||
|
||||
- __User friendliness.__ Keras is an API designed for human beings, not machines. It puts user experience front and center. Keras follows best practices for reducing cognitive load: it offers consistent & simple APIs, it minimizes the number of user actions required for common use cases, and it provides clear and actionable feedback upon user error.
|
||||
|
||||
- __Modularity.__ A model is understood as a sequence or a graph of standalone, fully-configurable modules that can be plugged together with as little restrictions as possible. In particular, neural layers, cost functions, optimizers, initialization schemes, activation functions, regularization schemes are all standalone modules that you can combine to create new models.
|
||||
|
||||
- __Minimalism.__ Each module should be kept short and simple. Every piece of code should be transparent upon first reading. No black magic: it hurts iteration speed and ability to innovate.
|
||||
|
||||
- __Easy extensibility.__ New modules are dead simple to add (as new classes and functions), and existing modules provide ample examples. To be able to easily create new modules allows for total expressiveness, making Keras suitable for advanced research.
|
||||
- __Easy extensibility.__ New modules are simple to add (as new classes and functions), and existing modules provide ample examples. To be able to easily create new modules allows for total expressiveness, making Keras suitable for advanced research.
|
||||
|
||||
- __Work with Python__. No separate models configuration files in a declarative format. Models are described in Python code, which is compact, easier to debug, and allows for ease of extensibility.
|
||||
|
||||
@@ -37,9 +37,9 @@ Keras is compatible with: __Python 2.7-3.5__.
|
||||
|
||||
## Getting started: 30 seconds to Keras
|
||||
|
||||
The core data structure of Keras is a __model__, a way to organize layers. There are two types of models: [`Sequential`](http://keras.io/models/#sequential) and [`Graph`](http://keras.io/models/#graph).
|
||||
The core data structure of Keras is a __model__, a way to organize layers. The simplest type of model is the [`Sequential`](http://keras.io/getting-started/sequential-model-guide) model, a linear stack of layers. For more complex architectures, you should use the [Keras functional API](http://keras.io/getting-started/functional-api-guide), which allows to build arbitrary graphs of layers.
|
||||
|
||||
Here's the `Sequential` model (a linear pile of layers):
|
||||
Here is the `Sequential` model:
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
@@ -50,51 +50,61 @@ model = Sequential()
|
||||
Stacking layers is as easy as `.add()`:
|
||||
|
||||
```python
|
||||
from keras.layers.core import Dense, Activation
|
||||
from keras.layers import Dense, Activation
|
||||
|
||||
model.add(Dense(output_dim=64, input_dim=100, init="glorot_uniform"))
|
||||
model.add(Activation("relu"))
|
||||
model.add(Dense(output_dim=10, init="glorot_uniform"))
|
||||
model.add(Activation("softmax"))
|
||||
model.add(Dense(units=64, input_dim=100))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dense(units=10))
|
||||
model.add(Activation('softmax'))
|
||||
```
|
||||
|
||||
Once your model looks good, configure its learning process with `.compile()`:
|
||||
|
||||
```python
|
||||
model.compile(loss='categorical_crossentropy', optimizer='sgd')
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer='sgd',
|
||||
metrics=['accuracy'])
|
||||
```
|
||||
|
||||
If you need to, you can further configure your optimizer. A core principle of Keras is to make things reasonably simple, while allowing the user to be fully in control when they need to (the ultimate control being the easy extensibility of the source code).
|
||||
```python
|
||||
from keras.optimizers import SGD
|
||||
model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.01, momentum=0.9, nesterov=True))
|
||||
model.compile(loss=keras.losses.categorical_crossentropy,
|
||||
optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True))
|
||||
```
|
||||
|
||||
You can now iterate on your training data in batches:
|
||||
|
||||
```python
|
||||
model.fit(X_train, Y_train, nb_epoch=5, batch_size=32)
|
||||
# x_train and y_train are Numpy arrays --just like in the Scikit-Learn API.
|
||||
model.fit(x_train, y_train, epochs=5, batch_size=32)
|
||||
```
|
||||
|
||||
Alternatively, you can feed batches to your model manually:
|
||||
|
||||
```python
|
||||
model.train_on_batch(X_batch, Y_batch)
|
||||
model.train_on_batch(x_batch, y_batch)
|
||||
```
|
||||
|
||||
Evaluate your performance in one line:
|
||||
|
||||
```python
|
||||
objective_score = model.evaluate(X_test, Y_test, batch_size=32)
|
||||
loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)
|
||||
```
|
||||
|
||||
Or generate predictions on new data:
|
||||
|
||||
```python
|
||||
classes = model.predict_classes(X_test, batch_size=32)
|
||||
proba = model.predict_proba(X_test, batch_size=32)
|
||||
classes = model.predict(x_test, batch_size=128)
|
||||
```
|
||||
|
||||
Building a network of LSTMs, a deep CNN, a Neural Turing Machine, a word2vec embedder or any other model is just as fast. The ideas behind deep learning are simple, so why should their implementation be painful?
|
||||
Building a question answering system, an image classification model, a Neural Turing Machine, or any other model is just as fast. The ideas behind deep learning are simple, so why should their implementation be painful?
|
||||
|
||||
Have a look at these [starter examples](http://keras.io/examples/).
|
||||
For a more in-depth tutorial about Keras, you can check out:
|
||||
|
||||
In the [examples folder](https://github.com/fchollet/keras/tree/master/examples) of the repo, you will find more advanced models: question-answering with memory networks, text generation with stacked LSTMs, neural turing machines, etc.
|
||||
- [Getting started with the Sequential model](http://keras.io/getting-started/sequential-model-guide)
|
||||
- [Getting started with the functional API](http://keras.io/getting-started/functional-api-guide)
|
||||
|
||||
In the [examples folder](https://github.com/fchollet/keras/tree/master/examples) of the repository, you will find more advanced models: question-answering with memory networks, text generation with stacked LSTMs, etc.
|
||||
|
||||
|
||||
------------------
|
||||
@@ -105,50 +115,54 @@ In the [examples folder](https://github.com/fchollet/keras/tree/master/examples)
|
||||
Keras uses the following dependencies:
|
||||
|
||||
- numpy, scipy
|
||||
- pyyaml
|
||||
- yaml
|
||||
- HDF5 and h5py (optional, required if you use model saving/loading functions)
|
||||
- Optional but recommended if you use CNNs: cuDNN.
|
||||
|
||||
|
||||
*When using the TensorFlow backend:*
|
||||
|
||||
- TensorFlow
|
||||
- [See installation instructions](https://www.tensorflow.org/install/).
|
||||
|
||||
*When using the CNTK backend:*
|
||||
|
||||
- CNTK
|
||||
- [See installation instructions](https://docs.microsoft.com/en-us/cognitive-toolkit/setup-cntk-on-your-machine).
|
||||
|
||||
*When using the Theano backend:*
|
||||
|
||||
- Theano
|
||||
- [See installation instructions](http://deeplearning.net/software/theano/install.html#install).
|
||||
|
||||
**Note**: You should use the latest version of Theano, not the PyPI version. Install it with:
|
||||
```
|
||||
sudo pip install git+git://github.com/Theano/Theano.git
|
||||
```
|
||||
|
||||
*When using the TensorFlow backend:*
|
||||
|
||||
- TensorFlow
|
||||
- [See installation instructions](https://github.com/tensorflow/tensorflow#download-and-setup).
|
||||
|
||||
To install Keras, `cd` to the Keras folder and run the install command:
|
||||
```
|
||||
```sh
|
||||
sudo python setup.py install
|
||||
```
|
||||
|
||||
You can also install Keras from PyPI:
|
||||
```
|
||||
```sh
|
||||
sudo pip install keras
|
||||
```
|
||||
|
||||
------------------
|
||||
|
||||
|
||||
## Switching from Theano to TensorFlow
|
||||
## Switching from TensorFlow to CNTK or Theano
|
||||
|
||||
By default, Keras will use Theano as its tensor manipulation library. [Follow these instructions](http://keras.io/backend/) to configure the Keras backend.
|
||||
By default, Keras will use TensorFlow as its tensor manipulation library. [Follow these instructions](http://keras.io/backend/) to configure the Keras backend.
|
||||
|
||||
------------------
|
||||
|
||||
|
||||
## Support
|
||||
|
||||
You can ask questions and join the development discussion on the [Keras Google group](https://groups.google.com/forum/#!forum/keras-users).
|
||||
You can ask questions and join the development discussion:
|
||||
|
||||
You can also post bug reports and feature requests in [Github issues](https://github.com/fchollet/keras/issues). Make sure to read [our guidelines](https://github.com/fchollet/keras/blob/master/CONTRIBUTING.md) first.
|
||||
- On the [Keras Google group](https://groups.google.com/forum/#!forum/keras-users).
|
||||
- On the [Keras Slack channel](https://kerasteam.slack.com). Use [this link](https://keras-slack-autojoin.herokuapp.com/) to request an invitation to the channel.
|
||||
|
||||
You can also post **bug reports and feature requests** (only) in [Github issues](https://github.com/fchollet/keras/issues). Make sure to read [our guidelines](https://github.com/fchollet/keras/blob/master/CONTRIBUTING.md) first.
|
||||
|
||||
|
||||
------------------
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
FROM nvidia/cuda:8.0-cudnn5-devel
|
||||
|
||||
ENV CONDA_DIR /opt/conda
|
||||
ENV PATH $CONDA_DIR/bin:$PATH
|
||||
|
||||
RUN mkdir -p $CONDA_DIR && \
|
||||
echo export PATH=$CONDA_DIR/bin:'$PATH' > /etc/profile.d/conda.sh && \
|
||||
apt-get update && \
|
||||
apt-get install -y wget git libhdf5-dev g++ graphviz && \
|
||||
wget --quiet https://repo.continuum.io/miniconda/Miniconda3-4.2.12-Linux-x86_64.sh && \
|
||||
echo "c59b3dd3cad550ac7596e0d599b91e75d88826db132e4146030ef471bb434e9a *Miniconda3-4.2.12-Linux-x86_64.sh" | sha256sum -c - && \
|
||||
/bin/bash /Miniconda3-4.2.12-Linux-x86_64.sh -f -b -p $CONDA_DIR && \
|
||||
rm Miniconda3-4.2.12-Linux-x86_64.sh
|
||||
|
||||
ENV NB_USER keras
|
||||
ENV NB_UID 1000
|
||||
|
||||
RUN useradd -m -s /bin/bash -N -u $NB_UID $NB_USER && \
|
||||
mkdir -p $CONDA_DIR && \
|
||||
chown keras $CONDA_DIR -R && \
|
||||
mkdir -p /src && \
|
||||
chown keras /src
|
||||
|
||||
USER keras
|
||||
|
||||
# Python
|
||||
ARG python_version=3.5
|
||||
|
||||
RUN conda install -y python=${python_version} && \
|
||||
pip install --upgrade pip && \
|
||||
pip install tensorflow-gpu && \
|
||||
conda install Pillow scikit-learn notebook pandas matplotlib mkl nose pyyaml six h5py && \
|
||||
conda install theano pygpu && \
|
||||
git clone git://github.com/fchollet/keras.git /src && pip install -e /src[tests] && \
|
||||
pip install git+git://github.com/fchollet/keras.git && \
|
||||
conda clean -yt
|
||||
|
||||
ADD theanorc /home/keras/.theanorc
|
||||
|
||||
ENV PYTHONPATH='/src/:$PYTHONPATH'
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
EXPOSE 8888
|
||||
|
||||
CMD jupyter notebook --port=8888 --ip=0.0.0.0
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
help:
|
||||
@cat Makefile
|
||||
|
||||
DATA?="${HOME}/Data"
|
||||
GPU?=0
|
||||
DOCKER_FILE=Dockerfile
|
||||
DOCKER=GPU=$(GPU) nvidia-docker
|
||||
BACKEND=tensorflow
|
||||
TEST=tests/
|
||||
SRC=$(shell dirname `pwd`)
|
||||
|
||||
build:
|
||||
docker build -t keras --build-arg python_version=3.5 -f $(DOCKER_FILE) .
|
||||
|
||||
bash: build
|
||||
$(DOCKER) run -it -v $(SRC):/src -v $(DATA):/data --env KERAS_BACKEND=$(BACKEND) keras bash
|
||||
|
||||
ipython: build
|
||||
$(DOCKER) run -it -v $(SRC):/src -v $(DATA):/data --env KERAS_BACKEND=$(BACKEND) keras ipython
|
||||
|
||||
notebook: build
|
||||
$(DOCKER) run -it -v $(SRC):/src -v $(DATA):/data --net=host --env KERAS_BACKEND=$(BACKEND) keras
|
||||
|
||||
test: build
|
||||
$(DOCKER) run -it -v $(SRC):/src -v $(DATA):/data --env KERAS_BACKEND=$(BACKEND) keras py.test $(TEST)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
# Using Keras via Docker
|
||||
|
||||
This directory contains `Dockerfile` to make it easy to get up and running with
|
||||
Keras via [Docker](http://www.docker.com/).
|
||||
|
||||
## Installing Docker
|
||||
|
||||
General installation instructions are
|
||||
[on the Docker site](https://docs.docker.com/installation/), but we give some
|
||||
quick links here:
|
||||
|
||||
* [OSX](https://docs.docker.com/installation/mac/): [docker toolbox](https://www.docker.com/toolbox)
|
||||
* [ubuntu](https://docs.docker.com/installation/ubuntulinux/)
|
||||
|
||||
## Running the container
|
||||
|
||||
We are using `Makefile` to simplify docker commands within make commands.
|
||||
|
||||
Build the container and start a jupyter notebook
|
||||
|
||||
$ make notebook
|
||||
|
||||
Build the container and start an iPython shell
|
||||
|
||||
$ make ipython
|
||||
|
||||
Build the container and start a bash
|
||||
|
||||
$ make bash
|
||||
|
||||
For GPU support install NVidia drivers (ideally latest) and
|
||||
[nvidia-docker](https://github.com/NVIDIA/nvidia-docker). Run using
|
||||
|
||||
$ make notebook GPU=0 # or [ipython, bash]
|
||||
|
||||
Switch between Theano and TensorFlow
|
||||
|
||||
$ make notebook BACKEND=theano
|
||||
$ make notebook BACKEND=tensorflow
|
||||
|
||||
Mount a volume for external data sets
|
||||
|
||||
$ make DATA=~/mydata
|
||||
|
||||
Prints all make tasks
|
||||
|
||||
$ make help
|
||||
|
||||
You can change Theano parameters by editing `/docker/theanorc`.
|
||||
|
||||
|
||||
Note: If you would have a problem running nvidia-docker you may try the old way
|
||||
we have used. But it is not recommended. If you find a bug in the nvidia-docker report
|
||||
it there please and try using the nvidia-docker as described above.
|
||||
|
||||
$ export CUDA_SO=$(\ls /usr/lib/x86_64-linux-gnu/libcuda.* | xargs -I{} echo '-v {}:{}')
|
||||
$ export DEVICES=$(\ls /dev/nvidia* | xargs -I{} echo '--device {}:{}')
|
||||
$ docker run -it -p 8888:8888 $CUDA_SO $DEVICES gcr.io/tensorflow/tensorflow:latest-gpu
|
||||
@@ -0,0 +1,5 @@
|
||||
[global]
|
||||
floatX = float32
|
||||
optimizer=None
|
||||
device = cuda
|
||||
|
||||
+373
-112
@@ -1,45 +1,298 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
General documentation architecture:
|
||||
|
||||
Home
|
||||
Index
|
||||
|
||||
- Getting started
|
||||
Getting started with the sequential model
|
||||
Getting started with the functional api
|
||||
FAQ
|
||||
|
||||
- Models
|
||||
About Keras models
|
||||
explain when one should use Sequential or functional API
|
||||
explain compilation step
|
||||
explain weight saving, weight loading
|
||||
explain serialization, deserialization
|
||||
Sequential
|
||||
Model (functional API)
|
||||
|
||||
- Layers
|
||||
About Keras layers
|
||||
explain common layer functions: get_weights, set_weights, get_config
|
||||
explain input_shape
|
||||
explain usage on non-Keras tensors
|
||||
Core Layers
|
||||
Convolutional Layers
|
||||
Pooling Layers
|
||||
Locally-connected Layers
|
||||
Recurrent Layers
|
||||
Embedding Layers
|
||||
Merge Layers
|
||||
Advanced Activations Layers
|
||||
Normalization Layers
|
||||
Noise Layers
|
||||
Layer Wrappers
|
||||
Writing your own Keras layers
|
||||
|
||||
- Preprocessing
|
||||
Sequence Preprocessing
|
||||
Text Preprocessing
|
||||
Image Preprocessing
|
||||
|
||||
Losses
|
||||
Metrics
|
||||
Optimizers
|
||||
Activations
|
||||
Callbacks
|
||||
Datasets
|
||||
Applications
|
||||
Backend
|
||||
Initializers
|
||||
Regularizers
|
||||
Constraints
|
||||
Visualization
|
||||
Scikit-learn API
|
||||
Utils
|
||||
Contributing
|
||||
|
||||
'''
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import re
|
||||
import inspect
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
if sys.version[0] == '2':
|
||||
reload(sys)
|
||||
sys.setdefaultencoding('utf8')
|
||||
|
||||
from keras.layers import convolutional
|
||||
import keras
|
||||
from keras import utils
|
||||
from keras import layers
|
||||
from keras import initializers
|
||||
from keras.layers import pooling
|
||||
from keras.layers import local
|
||||
from keras.layers import recurrent
|
||||
from keras.layers import core
|
||||
from keras.layers import noise
|
||||
from keras.layers import normalization
|
||||
from keras.layers import advanced_activations
|
||||
from keras.layers import containers
|
||||
from keras.layers import embeddings
|
||||
from keras.layers import wrappers
|
||||
from keras import optimizers
|
||||
from keras import callbacks
|
||||
from keras import models
|
||||
from keras.engine import topology
|
||||
from keras import losses
|
||||
from keras import metrics
|
||||
from keras import backend
|
||||
from keras import constraints
|
||||
from keras import activations
|
||||
from keras import regularizers
|
||||
from keras.utils import data_utils
|
||||
from keras.utils import io_utils
|
||||
from keras.utils import layer_utils
|
||||
from keras.utils import np_utils
|
||||
from keras.utils import generic_utils
|
||||
|
||||
MODULES = [(convolutional, 'keras.layers.convolutional'),
|
||||
(recurrent, 'keras.layers.recurrent'),
|
||||
(noise, 'keras.layers.noise'),
|
||||
(normalization, 'keras.layers.normalization'),
|
||||
(advanced_activations, 'keras.layers.advanced_activations'),
|
||||
(containers, 'keras.layers.containers'),
|
||||
(core, 'keras.layers.core'),
|
||||
(embeddings, 'keras.layers.embeddings'),
|
||||
(optimizers, 'keras.optimizers'),
|
||||
(callbacks, 'keras.callbacks'),
|
||||
(models, 'keras.models')]
|
||||
|
||||
SKIP = ['build', 'get_params', 'MaskedLayer',
|
||||
'SiameseHead', 'MaskedLambda',
|
||||
'CallbackList']
|
||||
ROOT = 'http://keras.io/'
|
||||
INCLUDE_METHODS_FOR = [
|
||||
'Layer',
|
||||
'Graph',
|
||||
'Sequential',
|
||||
'Callback',
|
||||
EXCLUDE = {
|
||||
'Optimizer',
|
||||
'Wrapper',
|
||||
'get_session',
|
||||
'set_session',
|
||||
'CallbackList',
|
||||
'serialize',
|
||||
'deserialize',
|
||||
'get',
|
||||
}
|
||||
|
||||
PAGES = [
|
||||
{
|
||||
'page': 'models/sequential.md',
|
||||
'functions': [
|
||||
models.Sequential.compile,
|
||||
models.Sequential.fit,
|
||||
models.Sequential.evaluate,
|
||||
models.Sequential.predict,
|
||||
models.Sequential.train_on_batch,
|
||||
models.Sequential.test_on_batch,
|
||||
models.Sequential.predict_on_batch,
|
||||
models.Sequential.fit_generator,
|
||||
models.Sequential.evaluate_generator,
|
||||
models.Sequential.predict_generator,
|
||||
models.Sequential.get_layer,
|
||||
],
|
||||
},
|
||||
{
|
||||
'page': 'models/model.md',
|
||||
'functions': [
|
||||
models.Model.compile,
|
||||
models.Model.fit,
|
||||
models.Model.evaluate,
|
||||
models.Model.predict,
|
||||
models.Model.train_on_batch,
|
||||
models.Model.test_on_batch,
|
||||
models.Model.predict_on_batch,
|
||||
models.Model.fit_generator,
|
||||
models.Model.evaluate_generator,
|
||||
models.Model.predict_generator,
|
||||
models.Model.get_layer,
|
||||
]
|
||||
},
|
||||
{
|
||||
'page': 'layers/core.md',
|
||||
'classes': [
|
||||
layers.Dense,
|
||||
layers.Activation,
|
||||
layers.Dropout,
|
||||
layers.Flatten,
|
||||
layers.Reshape,
|
||||
layers.Permute,
|
||||
layers.RepeatVector,
|
||||
layers.Lambda,
|
||||
layers.ActivityRegularization,
|
||||
layers.Masking,
|
||||
],
|
||||
},
|
||||
{
|
||||
'page': 'layers/convolutional.md',
|
||||
'classes': [
|
||||
layers.Conv1D,
|
||||
layers.Conv2D,
|
||||
layers.SeparableConv2D,
|
||||
layers.Conv2DTranspose,
|
||||
layers.Conv3D,
|
||||
layers.Cropping1D,
|
||||
layers.Cropping2D,
|
||||
layers.Cropping3D,
|
||||
layers.UpSampling1D,
|
||||
layers.UpSampling2D,
|
||||
layers.UpSampling3D,
|
||||
layers.ZeroPadding1D,
|
||||
layers.ZeroPadding2D,
|
||||
layers.ZeroPadding3D,
|
||||
],
|
||||
},
|
||||
{
|
||||
'page': 'layers/pooling.md',
|
||||
'classes': [
|
||||
pooling.MaxPooling1D,
|
||||
pooling.MaxPooling2D,
|
||||
pooling.MaxPooling3D,
|
||||
pooling.AveragePooling1D,
|
||||
pooling.AveragePooling2D,
|
||||
pooling.AveragePooling3D,
|
||||
pooling.GlobalMaxPooling1D,
|
||||
pooling.GlobalAveragePooling1D,
|
||||
pooling.GlobalMaxPooling2D,
|
||||
pooling.GlobalAveragePooling2D,
|
||||
],
|
||||
},
|
||||
{
|
||||
'page': 'layers/local.md',
|
||||
'classes': [
|
||||
local.LocallyConnected1D,
|
||||
local.LocallyConnected2D,
|
||||
],
|
||||
},
|
||||
{
|
||||
'page': 'layers/recurrent.md',
|
||||
'classes': [
|
||||
recurrent.Recurrent,
|
||||
recurrent.SimpleRNN,
|
||||
recurrent.GRU,
|
||||
recurrent.LSTM,
|
||||
],
|
||||
},
|
||||
{
|
||||
'page': 'layers/embeddings.md',
|
||||
'classes': [
|
||||
embeddings.Embedding,
|
||||
],
|
||||
},
|
||||
{
|
||||
'page': 'layers/normalization.md',
|
||||
'classes': [
|
||||
normalization.BatchNormalization,
|
||||
],
|
||||
},
|
||||
{
|
||||
'page': 'layers/advanced-activations.md',
|
||||
'all_module_classes': [advanced_activations],
|
||||
},
|
||||
{
|
||||
'page': 'layers/noise.md',
|
||||
'all_module_classes': [noise],
|
||||
},
|
||||
{
|
||||
'page': 'layers/merge.md',
|
||||
'classes': [
|
||||
layers.Add,
|
||||
layers.Multiply,
|
||||
layers.Average,
|
||||
layers.Maximum,
|
||||
layers.Concatenate,
|
||||
layers.Dot,
|
||||
],
|
||||
'functions': [
|
||||
layers.add,
|
||||
layers.multiply,
|
||||
layers.average,
|
||||
layers.maximum,
|
||||
layers.concatenate,
|
||||
layers.dot,
|
||||
]
|
||||
},
|
||||
{
|
||||
'page': 'layers/wrappers.md',
|
||||
'all_module_classes': [wrappers],
|
||||
},
|
||||
{
|
||||
'page': 'metrics.md',
|
||||
'all_module_functions': [metrics],
|
||||
},
|
||||
{
|
||||
'page': 'losses.md',
|
||||
'all_module_functions': [losses],
|
||||
},
|
||||
{
|
||||
'page': 'initializers.md',
|
||||
'all_module_functions': [initializers],
|
||||
'all_module_classes': [initializers],
|
||||
},
|
||||
{
|
||||
'page': 'optimizers.md',
|
||||
'all_module_classes': [optimizers],
|
||||
},
|
||||
{
|
||||
'page': 'callbacks.md',
|
||||
'all_module_classes': [callbacks],
|
||||
},
|
||||
{
|
||||
'page': 'activations.md',
|
||||
'all_module_functions': [activations],
|
||||
},
|
||||
{
|
||||
'page': 'backend.md',
|
||||
'all_module_functions': [backend],
|
||||
},
|
||||
{
|
||||
'page': 'utils.md',
|
||||
'all_module_functions': [utils],
|
||||
'classes': [utils.CustomObjectScope,
|
||||
utils.HDF5Matrix,
|
||||
utils.Sequence]
|
||||
},
|
||||
]
|
||||
|
||||
ROOT = 'http://keras.io/'
|
||||
|
||||
|
||||
def get_earliest_class_that_defined_member(member, cls):
|
||||
ancestors = get_classes_ancestors([cls])
|
||||
@@ -67,23 +320,28 @@ def get_classes_ancestors(classes):
|
||||
return filtered_ancestors
|
||||
|
||||
|
||||
def get_method_signature(method):
|
||||
signature = inspect.getargspec(method)
|
||||
def get_function_signature(function, method=True):
|
||||
wrapped = getattr(function, '_original_function', None)
|
||||
if wrapped is None:
|
||||
signature = inspect.getargspec(function)
|
||||
else:
|
||||
signature = inspect.getargspec(wrapped)
|
||||
defaults = signature.defaults
|
||||
args = signature.args[1:]
|
||||
if method:
|
||||
args = signature.args[1:]
|
||||
else:
|
||||
args = signature.args
|
||||
if defaults:
|
||||
kwargs = zip(args[-len(defaults):], defaults)
|
||||
args = args[:-len(defaults)]
|
||||
else:
|
||||
kwargs = []
|
||||
st = '%s.%s(' % (method.__module__, method.__name__)
|
||||
st = '%s.%s(' % (function.__module__, function.__name__)
|
||||
for a in args:
|
||||
st += str(a) + ', '
|
||||
for a, v in kwargs:
|
||||
if type(v) == str:
|
||||
if isinstance(v, str):
|
||||
v = '\'' + v + '\''
|
||||
elif type(v) == unicode:
|
||||
v = 'u\'' + v + '\''
|
||||
st += str(a) + '=' + str(v) + ', '
|
||||
if kwargs or args:
|
||||
return st[:-2] + ')'
|
||||
@@ -91,6 +349,17 @@ def get_method_signature(method):
|
||||
return st + ')'
|
||||
|
||||
|
||||
def get_class_signature(cls):
|
||||
try:
|
||||
class_signature = get_function_signature(cls.__init__)
|
||||
class_signature = class_signature.replace('__init__', cls.__name__)
|
||||
except:
|
||||
# in case the class inherits from object and does not
|
||||
# define __init__
|
||||
class_signature = cls.__module__ + '.' + cls.__name__ + '()'
|
||||
return class_signature
|
||||
|
||||
|
||||
def class_to_docs_link(cls):
|
||||
module_name = cls.__module__
|
||||
assert module_name[:6] == 'keras.'
|
||||
@@ -121,7 +390,7 @@ def process_class_docstring(docstring):
|
||||
r'\n __\1__\n\n',
|
||||
docstring)
|
||||
|
||||
docstring = re.sub(r' ([^\s\\]+):(.*)\n',
|
||||
docstring = re.sub(r' ([^\s\\\(]+):(.*)\n',
|
||||
r' - __\1__:\2\n',
|
||||
docstring)
|
||||
|
||||
@@ -131,12 +400,15 @@ def process_class_docstring(docstring):
|
||||
return docstring
|
||||
|
||||
|
||||
def process_method_docstring(docstring):
|
||||
def process_function_docstring(docstring):
|
||||
docstring = re.sub(r'\n # (.*)\n',
|
||||
r'\n __\1__\n\n',
|
||||
docstring)
|
||||
docstring = re.sub(r'\n # (.*)\n',
|
||||
r'\n __\1__\n\n',
|
||||
docstring)
|
||||
|
||||
docstring = re.sub(r' ([^\s\\]+):(.*)\n',
|
||||
docstring = re.sub(r' ([^\s\\\(]+):(.*)\n',
|
||||
r' - __\1__:\2\n',
|
||||
docstring)
|
||||
|
||||
@@ -148,6 +420,7 @@ def process_method_docstring(docstring):
|
||||
print('Cleaning up existing sources directory.')
|
||||
if os.path.exists('sources'):
|
||||
shutil.rmtree('sources')
|
||||
|
||||
print('Populating sources directory with templates.')
|
||||
for subdir, dirs, fnames in os.walk('templates'):
|
||||
for fname in fnames:
|
||||
@@ -159,102 +432,90 @@ for subdir, dirs, fnames in os.walk('templates'):
|
||||
new_fpath = fpath.replace('templates', 'sources')
|
||||
shutil.copy(fpath, new_fpath)
|
||||
|
||||
# Take care of index page.
|
||||
readme = open('../README.md').read()
|
||||
index = open('templates/index.md').read()
|
||||
index = index.replace('{{autogenerated}}', readme[readme.find('##'):])
|
||||
f = open('sources/index.md', 'w')
|
||||
f.write(index)
|
||||
f.close()
|
||||
|
||||
print('Starting autogeneration.')
|
||||
covered_so_far = set()
|
||||
for module, module_name in MODULES:
|
||||
class_pages = []
|
||||
for name in dir(module):
|
||||
if name in SKIP:
|
||||
continue
|
||||
if name[0] == '_':
|
||||
continue
|
||||
module_member = getattr(module, name)
|
||||
if module_member in covered_so_far:
|
||||
continue
|
||||
if inspect.isclass(module_member):
|
||||
cls = module_member
|
||||
if cls.__module__ == module_name:
|
||||
for page_data in PAGES:
|
||||
blocks = []
|
||||
classes = page_data.get('classes', [])
|
||||
for module in page_data.get('all_module_classes', []):
|
||||
module_classes = []
|
||||
for name in dir(module):
|
||||
if name[0] == '_' or name in EXCLUDE:
|
||||
continue
|
||||
module_member = getattr(module, name)
|
||||
if inspect.isclass(module_member):
|
||||
cls = module_member
|
||||
if cls.__module__ == module.__name__:
|
||||
if cls not in module_classes:
|
||||
module_classes.append(cls)
|
||||
module_classes.sort(key=lambda x: id(x))
|
||||
classes += module_classes
|
||||
|
||||
try:
|
||||
class_signature = get_method_signature(cls.__init__)
|
||||
class_signature = class_signature.replace('__init__', cls.__name__)
|
||||
except:
|
||||
# in case the class inherits from object and does not
|
||||
# define __init__
|
||||
class_signature = module_name + '.' + cls.__name__ + '()'
|
||||
for cls in classes:
|
||||
subblocks = []
|
||||
signature = get_class_signature(cls)
|
||||
subblocks.append('<span style="float:right;">' + class_to_source_link(cls) + '</span>')
|
||||
subblocks.append('### ' + cls.__name__ + '\n')
|
||||
subblocks.append(code_snippet(signature))
|
||||
docstring = cls.__doc__
|
||||
if docstring:
|
||||
subblocks.append(process_class_docstring(docstring))
|
||||
blocks.append('\n'.join(subblocks))
|
||||
|
||||
methods = []
|
||||
methods_not_defined_here = []
|
||||
for name in dir(cls):
|
||||
if name in SKIP:
|
||||
continue
|
||||
if name[0] == '_':
|
||||
continue
|
||||
cls_member = getattr(cls, name)
|
||||
if inspect.ismethod(cls_member):
|
||||
method = cls_member
|
||||
signature = inspect.getargspec(method)
|
||||
defaults = signature.defaults
|
||||
args = signature.args[1:]
|
||||
if defaults:
|
||||
kwargs = zip(args[-len(defaults):], defaults)
|
||||
args = args[:-len(defaults)]
|
||||
else:
|
||||
kwargs = []
|
||||
functions = page_data.get('functions', [])
|
||||
for module in page_data.get('all_module_functions', []):
|
||||
module_functions = []
|
||||
for name in dir(module):
|
||||
if name[0] == '_' or name in EXCLUDE:
|
||||
continue
|
||||
module_member = getattr(module, name)
|
||||
if inspect.isfunction(module_member):
|
||||
function = module_member
|
||||
if module.__name__ in function.__module__:
|
||||
if function not in module_functions:
|
||||
module_functions.append(function)
|
||||
module_functions.sort(key=lambda x: id(x))
|
||||
functions += module_functions
|
||||
|
||||
defined_by = get_earliest_class_that_defined_member(method.__name__, cls)
|
||||
if cls == defined_by:
|
||||
methods.append(method)
|
||||
else:
|
||||
methods_not_defined_here.append((method, defined_by))
|
||||
for function in functions:
|
||||
subblocks = []
|
||||
signature = get_function_signature(function, method=False)
|
||||
signature = signature.replace(function.__module__ + '.', '')
|
||||
subblocks.append('### ' + function.__name__ + '\n')
|
||||
subblocks.append(code_snippet(signature))
|
||||
docstring = function.__doc__
|
||||
if docstring:
|
||||
subblocks.append(process_function_docstring(docstring))
|
||||
blocks.append('\n\n'.join(subblocks))
|
||||
|
||||
blocks = []
|
||||
blocks.append('<span style="float:right;">' + class_to_source_link(cls) + '</span>')
|
||||
blocks.append('# ' + cls.__name__ + '\n')
|
||||
blocks.append(code_snippet(class_signature))
|
||||
docstring = cls.__doc__
|
||||
if docstring:
|
||||
blocks.append(process_class_docstring(docstring))
|
||||
|
||||
if cls.__name__ in INCLUDE_METHODS_FOR:
|
||||
if methods or methods_not_defined_here:
|
||||
blocks.append('### Methods\n')
|
||||
for method in methods:
|
||||
signature = get_method_signature(method)
|
||||
signature = signature.replace(module_name + '.', '')
|
||||
blocks.append(code_snippet(signature))
|
||||
docstring = method.__doc__
|
||||
if docstring:
|
||||
blocks.append(process_method_docstring(docstring))
|
||||
for method, defined_by in methods_not_defined_here:
|
||||
signature = get_method_signature(method)
|
||||
method_module_name = method.__module__
|
||||
signature = signature.replace(method_module_name + '.', '')
|
||||
link = '[' + defined_by.__name__ + '](' + class_to_docs_link(defined_by) + ')'
|
||||
blocks.append(code_snippet(signature))
|
||||
blocks.append('Defined by ' + link + '.\n')
|
||||
|
||||
mkdown = '\n'.join(blocks)
|
||||
class_pages.append((id(cls), mkdown))
|
||||
covered_so_far.add(module_member)
|
||||
|
||||
class_pages.sort(key=lambda x: x[0])
|
||||
class_pages = [x[1] for x in class_pages]
|
||||
module_page = '\n----\n\n'.join(class_pages)
|
||||
if not blocks:
|
||||
raise RuntimeError('Found no content for page ' +
|
||||
page_data['page'])
|
||||
|
||||
mkdown = '\n----\n\n'.join(blocks)
|
||||
# save module page.
|
||||
# Either insert content into existing page,
|
||||
# or create page otherwise
|
||||
path = 'sources/' + module_name.replace('.', '/')[6:] + '.md'
|
||||
page_name = page_data['page']
|
||||
path = os.path.join('sources', page_name)
|
||||
if os.path.exists(path):
|
||||
template = open(path).read()
|
||||
assert '{{autogenerated}}' in template, ('Template found for ' + path +
|
||||
' but missing {{autogenerated}} tag.')
|
||||
module_page = template.replace('{{autogenerated}}', module_page)
|
||||
mkdown = template.replace('{{autogenerated}}', mkdown)
|
||||
print('...inserting autogenerated content into template:', path)
|
||||
else:
|
||||
print('...creating new page with autogenerated content:', path)
|
||||
subdir = os.path.dirname(path)
|
||||
if not os.path.exists(subdir):
|
||||
os.makedirs(subdir)
|
||||
open(path, 'w').write(module_page)
|
||||
open(path, 'w').write(mkdown)
|
||||
|
||||
shutil.copyfile('../CONTRIBUTING.md', 'sources/contributing.md')
|
||||
|
||||
+33
-21
@@ -1,42 +1,54 @@
|
||||
site_name: Keras Documentation
|
||||
theme: readthedocs
|
||||
#theme_dir: theme
|
||||
docs_dir: sources
|
||||
repo_url: http://github.com/fchollet/keras
|
||||
site_url: http://keras.io/
|
||||
#theme_dir: theme
|
||||
site_description: Documentation for fast and lightweight Keras Deep Learning library.
|
||||
site_description: 'Documentation for Keras, the Python Deep Learning library.'
|
||||
|
||||
dev_addr: '0.0.0.0:8000'
|
||||
google_analytics: ['UA-61785484-1', 'keras.io']
|
||||
|
||||
|
||||
pages:
|
||||
- Home: index.md
|
||||
- Index: documentation.md
|
||||
- Examples: examples.md
|
||||
- FAQ: faq.md
|
||||
- Backends: backend.md
|
||||
- Optimizers: optimizers.md
|
||||
- Objectives: objectives.md
|
||||
- Models: models.md
|
||||
- Activations: activations.md
|
||||
- Initializations: initializations.md
|
||||
- Regularizers: regularizers.md
|
||||
- Constraints: constraints.md
|
||||
- Callbacks: callbacks.md
|
||||
- Datasets: datasets.md
|
||||
- Visualization: visualization.md
|
||||
- Getting started:
|
||||
- Guide to the Sequential model: getting-started/sequential-model-guide.md
|
||||
- Guide to the Functional API: getting-started/functional-api-guide.md
|
||||
- FAQ: getting-started/faq.md
|
||||
- Models:
|
||||
- About Keras models: models/about-keras-models.md
|
||||
- Sequential: models/sequential.md
|
||||
- Model (functional API): models/model.md
|
||||
- Layers:
|
||||
- About Keras layers: layers/about-keras-layers.md
|
||||
- Core Layers: layers/core.md
|
||||
- Convolutional Layers: layers/convolutional.md
|
||||
- Pooling Layers: layers/pooling.md
|
||||
- Locally-connected Layers: layers/local.md
|
||||
- Recurrent Layers: layers/recurrent.md
|
||||
- Advanced Activations Layers: layers/advanced_activations.md
|
||||
- Normalization Layers: layers/normalization.md
|
||||
- Embedding Layers: layers/embeddings.md
|
||||
- Merge Layers: layers/merge.md
|
||||
- Advanced Activations Layers: layers/advanced-activations.md
|
||||
- Normalization Layers: layers/normalization.md
|
||||
- Noise layers: layers/noise.md
|
||||
- Containers: layers/containers.md
|
||||
- Layer wrappers: layers/wrappers.md
|
||||
- Writing your own Keras layers: layers/writing-your-own-keras-layers.md
|
||||
- Preprocessing:
|
||||
- Sequence Preprocessing: preprocessing/sequence.md
|
||||
- Text Preprocessing: preprocessing/text.md
|
||||
- Image Preprocessing: preprocessing/image.md
|
||||
|
||||
- Losses: losses.md
|
||||
- Metrics: metrics.md
|
||||
- Optimizers: optimizers.md
|
||||
- Activations: activations.md
|
||||
- Callbacks: callbacks.md
|
||||
- Datasets: datasets.md
|
||||
- Applications: applications.md
|
||||
- Backend: backend.md
|
||||
- Initializers: initializers.md
|
||||
- Regularizers: regularizers.md
|
||||
- Constraints: constraints.md
|
||||
- Visualization: visualization.md
|
||||
- Scikit-learn API: scikit-learn-api.md
|
||||
- Utils: utils.md
|
||||
- Contributing: contributing.md
|
||||
|
||||
externo
+10
-17
@@ -4,38 +4,31 @@
|
||||
Activations can either be used through an `Activation` layer, or through the `activation` argument supported by all forward layers:
|
||||
|
||||
```python
|
||||
from keras.layers.core import Activation, Dense
|
||||
from keras.layers import Activation, Dense
|
||||
|
||||
model.add(Dense(64))
|
||||
model.add(Activation('tanh'))
|
||||
```
|
||||
is equivalent to:
|
||||
|
||||
This is equivalent to:
|
||||
|
||||
```python
|
||||
model.add(Dense(64, activation='tanh'))
|
||||
```
|
||||
|
||||
You can also pass an element-wise Theano/TensorFlow function as an activation:
|
||||
You can also pass an element-wise TensorFlow/Theano/CNTK function as an activation:
|
||||
|
||||
```python
|
||||
from keras import backend as K
|
||||
|
||||
def tanh(x):
|
||||
return K.tanh(x)
|
||||
|
||||
model.add(Dense(64, activation=tanh))
|
||||
model.add(Activation(tanh))
|
||||
model.add(Dense(64, activation=K.tanh))
|
||||
model.add(Activation(K.tanh))
|
||||
```
|
||||
|
||||
## Available activations
|
||||
|
||||
- __softmax__: Softmax applied across inputs last dimension. Expects shape either `(nb_samples, nb_timesteps, nb_dims)` or `(nb_samples, nb_dims)`.
|
||||
- __softplus__
|
||||
- __relu__
|
||||
- __tanh__
|
||||
- __sigmoid__
|
||||
- __hard_sigmoid__
|
||||
- __linear__
|
||||
{{autogenerated}}
|
||||
|
||||
## On Advanced Activations
|
||||
## On "Advanced Activations"
|
||||
|
||||
Activations that are more complex than a simple Theano/TensorFlow function (eg. learnable activations, configurable activations, etc.) are available as [Advanced Activation layers](layers/advanced_activations.md), and can be found in the module `keras.layers.advanced_activations`. These include PReLU and LeakyReLU.
|
||||
Activations that are more complex than a simple TensorFlow/Theano/CNTK function (eg. learnable activations, which maintain a state) are available as [Advanced Activation layers](layers/advanced-activations.md), and can be found in the module `keras.layers.advanced_activations`. These include `PReLU` and `LeakyReLU`.
|
||||
|
||||
externo
+455
@@ -0,0 +1,455 @@
|
||||
# Applications
|
||||
|
||||
Keras Applications are deep learning models that are made available alongside pre-trained weights.
|
||||
These models can be used for prediction, feature extraction, and fine-tuning.
|
||||
|
||||
Weights are downloaded automatically when instantiating a model. They are stored at `~/.keras/models/`.
|
||||
|
||||
## Available models
|
||||
|
||||
### Models for image classification with weights trained on ImageNet:
|
||||
|
||||
- [Xception](#xception)
|
||||
- [VGG16](#vgg16)
|
||||
- [VGG19](#vgg19)
|
||||
- [ResNet50](#resnet50)
|
||||
- [InceptionV3](#inceptionv3)
|
||||
|
||||
All of these architectures (except Xception) are compatible with both TensorFlow and Theano, and upon instantiation the models will be built according to the image data format set in your Keras configuration file at `~/.keras/keras.json`. For instance, if you have set `image_data_format=channels_last`, then any model loaded from this repository will get built according to the TensorFlow data format convention, "Width-Height-Depth".
|
||||
|
||||
The Xception model is only available for TensorFlow, due to its reliance on `SeparableConvolution` layers.
|
||||
|
||||
-----
|
||||
|
||||
## Usage examples for image classification models
|
||||
|
||||
### Classify ImageNet classes with ResNet50
|
||||
|
||||
```python
|
||||
from keras.applications.resnet50 import ResNet50
|
||||
from keras.preprocessing import image
|
||||
from keras.applications.resnet50 import preprocess_input, decode_predictions
|
||||
import numpy as np
|
||||
|
||||
model = ResNet50(weights='imagenet')
|
||||
|
||||
img_path = 'elephant.jpg'
|
||||
img = image.load_img(img_path, target_size=(224, 224))
|
||||
x = image.img_to_array(img)
|
||||
x = np.expand_dims(x, axis=0)
|
||||
x = preprocess_input(x)
|
||||
|
||||
preds = model.predict(x)
|
||||
# decode the results into a list of tuples (class, description, probability)
|
||||
# (one such list for each sample in the batch)
|
||||
print('Predicted:', decode_predictions(preds, top=3)[0])
|
||||
# Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]
|
||||
```
|
||||
|
||||
### Extract features with VGG16
|
||||
|
||||
```python
|
||||
from keras.applications.vgg16 import VGG16
|
||||
from keras.preprocessing import image
|
||||
from keras.applications.vgg16 import preprocess_input
|
||||
import numpy as np
|
||||
|
||||
model = VGG16(weights='imagenet', include_top=False)
|
||||
|
||||
img_path = 'elephant.jpg'
|
||||
img = image.load_img(img_path, target_size=(224, 224))
|
||||
x = image.img_to_array(img)
|
||||
x = np.expand_dims(x, axis=0)
|
||||
x = preprocess_input(x)
|
||||
|
||||
features = model.predict(x)
|
||||
```
|
||||
|
||||
### Extract features from an arbitrary intermediate layer with VGG19
|
||||
|
||||
```python
|
||||
from keras.applications.vgg19 import VGG19
|
||||
from keras.preprocessing import image
|
||||
from keras.applications.vgg19 import preprocess_input
|
||||
from keras.models import Model
|
||||
import numpy as np
|
||||
|
||||
base_model = VGG19(weights='imagenet')
|
||||
model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)
|
||||
|
||||
img_path = 'elephant.jpg'
|
||||
img = image.load_img(img_path, target_size=(224, 224))
|
||||
x = image.img_to_array(img)
|
||||
x = np.expand_dims(x, axis=0)
|
||||
x = preprocess_input(x)
|
||||
|
||||
block4_pool_features = model.predict(x)
|
||||
```
|
||||
|
||||
### Fine-tune InceptionV3 on a new set of classes
|
||||
|
||||
```python
|
||||
from keras.applications.inception_v3 import InceptionV3
|
||||
from keras.preprocessing import image
|
||||
from keras.models import Model
|
||||
from keras.layers import Dense, GlobalAveragePooling2D
|
||||
from keras import backend as K
|
||||
|
||||
# create the base pre-trained model
|
||||
base_model = InceptionV3(weights='imagenet', include_top=False)
|
||||
|
||||
# add a global spatial average pooling layer
|
||||
x = base_model.output
|
||||
x = GlobalAveragePooling2D()(x)
|
||||
# let's add a fully-connected layer
|
||||
x = Dense(1024, activation='relu')(x)
|
||||
# and a logistic layer -- let's say we have 200 classes
|
||||
predictions = Dense(200, activation='softmax')(x)
|
||||
|
||||
# this is the model we will train
|
||||
model = Model(inputs=base_model.input, outputs=predictions)
|
||||
|
||||
# first: train only the top layers (which were randomly initialized)
|
||||
# i.e. freeze all convolutional InceptionV3 layers
|
||||
for layer in base_model.layers:
|
||||
layer.trainable = False
|
||||
|
||||
# compile the model (should be done *after* setting layers to non-trainable)
|
||||
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
|
||||
|
||||
# train the model on the new data for a few epochs
|
||||
model.fit_generator(...)
|
||||
|
||||
# at this point, the top layers are well trained and we can start fine-tuning
|
||||
# convolutional layers from inception V3. We will freeze the bottom N layers
|
||||
# and train the remaining top layers.
|
||||
|
||||
# let's visualize layer names and layer indices to see how many layers
|
||||
# we should freeze:
|
||||
for i, layer in enumerate(base_model.layers):
|
||||
print(i, layer.name)
|
||||
|
||||
# we chose to train the top 2 inception blocks, i.e. we will freeze
|
||||
# the first 249 layers and unfreeze the rest:
|
||||
for layer in model.layers[:249]:
|
||||
layer.trainable = False
|
||||
for layer in model.layers[249:]:
|
||||
layer.trainable = True
|
||||
|
||||
# we need to recompile the model for these modifications to take effect
|
||||
# we use SGD with a low learning rate
|
||||
from keras.optimizers import SGD
|
||||
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')
|
||||
|
||||
# we train our model again (this time fine-tuning the top 2 inception blocks
|
||||
# alongside the top Dense layers
|
||||
model.fit_generator(...)
|
||||
```
|
||||
|
||||
|
||||
### Build InceptionV3 over a custom input tensor
|
||||
|
||||
```python
|
||||
from keras.applications.inception_v3 import InceptionV3
|
||||
from keras.layers import Input
|
||||
|
||||
# this could also be the output a different Keras model or layer
|
||||
input_tensor = Input(shape=(224, 224, 3)) # this assumes K.image_data_format() == 'channels_last'
|
||||
|
||||
model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)
|
||||
```
|
||||
|
||||
-----
|
||||
|
||||
# Documentation for individual models
|
||||
|
||||
- [Xception](#xception)
|
||||
- [VGG16](#vgg16)
|
||||
- [VGG19](#vgg19)
|
||||
- [ResNet50](#resnet50)
|
||||
- [InceptionV3](#inceptionv3)
|
||||
|
||||
-----
|
||||
|
||||
|
||||
## Xception
|
||||
|
||||
|
||||
```python
|
||||
keras.applications.xception.Xception(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)
|
||||
```
|
||||
|
||||
Xception V1 model, with weights pre-trained on ImageNet.
|
||||
|
||||
On ImageNet, this model gets to a top-1 validation accuracy of 0.790
|
||||
and a top-5 validation accuracy of 0.945.
|
||||
|
||||
Note that this model is only available for the TensorFlow backend,
|
||||
due to its reliance on `SeparableConvolution` layers. Additionally it only supports
|
||||
the data format "channels_last" (height, width, channels).
|
||||
|
||||
The default input size for this model is 299x299.
|
||||
|
||||
### Arguments
|
||||
|
||||
- include_top: whether to include the fully-connected layer at the top of the network.
|
||||
- weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet).
|
||||
- input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model.
|
||||
- input_shape: optional shape tuple, only to be specified
|
||||
if `include_top` is False (otherwise the input shape
|
||||
has to be `(299, 299, 3)`.
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 71.
|
||||
E.g. `(150, 150, 3)` would be one valid value.
|
||||
- pooling: Optional pooling mode for feature extraction
|
||||
when `include_top` is `False`.
|
||||
- `None` means that the output of the model will be
|
||||
the 4D tensor output of the
|
||||
last convolutional layer.
|
||||
- `avg` means that global average pooling
|
||||
will be applied to the output of the
|
||||
last convolutional layer, and thus
|
||||
the output of the model will be a 2D tensor.
|
||||
- `max` means that global max pooling will
|
||||
be applied.
|
||||
- classes: optional number of classes to classify images
|
||||
into, only to be specified if `include_top` is True, and
|
||||
if no `weights` argument is specified.
|
||||
|
||||
### Returns
|
||||
|
||||
A Keras model instance.
|
||||
|
||||
### References
|
||||
|
||||
- [Xception: Deep Learning with Depthwise Separable Convolutions](https://arxiv.org/abs/1610.02357)
|
||||
|
||||
### License
|
||||
|
||||
These weights are trained by ourselves and are released under the MIT license.
|
||||
|
||||
|
||||
-----
|
||||
|
||||
|
||||
## VGG16
|
||||
|
||||
```python
|
||||
keras.applications.vgg16.VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)
|
||||
```
|
||||
|
||||
VGG16 model, with weights pre-trained on ImageNet.
|
||||
|
||||
This model is available for both the Theano and TensorFlow backend, and can be built both
|
||||
with "channels_first" data format (channels, height, width) or "channels_last" data format (height, width, channels).
|
||||
|
||||
The default input size for this model is 224x224.
|
||||
|
||||
### Arguments
|
||||
|
||||
- include_top: whether to include the 3 fully-connected layers at the top of the network.
|
||||
- weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet).
|
||||
- input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model.
|
||||
- input_shape: optional shape tuple, only to be specified
|
||||
if `include_top` is False (otherwise the input shape
|
||||
has to be `(224, 224, 3)` (with `channels_last` data format)
|
||||
or `(3, 224, 224)` (with `channels_first` data format).
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 48.
|
||||
E.g. `(200, 200, 3)` would be one valid value.
|
||||
- pooling: Optional pooling mode for feature extraction
|
||||
when `include_top` is `False`.
|
||||
- `None` means that the output of the model will be
|
||||
the 4D tensor output of the
|
||||
last convolutional layer.
|
||||
- `avg` means that global average pooling
|
||||
will be applied to the output of the
|
||||
last convolutional layer, and thus
|
||||
the output of the model will be a 2D tensor.
|
||||
- `max` means that global max pooling will
|
||||
be applied.
|
||||
- classes: optional number of classes to classify images
|
||||
into, only to be specified if `include_top` is True, and
|
||||
if no `weights` argument is specified.
|
||||
|
||||
### Returns
|
||||
|
||||
A Keras model instance.
|
||||
|
||||
### References
|
||||
|
||||
- [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556): please cite this paper if you use the VGG models in your work.
|
||||
|
||||
### License
|
||||
|
||||
These weights are ported from the ones [released by VGG at Oxford](http://www.robots.ox.ac.uk/~vgg/research/very_deep/) under the [Creative Commons Attribution License](https://creativecommons.org/licenses/by/4.0/).
|
||||
|
||||
-----
|
||||
|
||||
## VGG19
|
||||
|
||||
|
||||
```python
|
||||
keras.applications.vgg19.VGG19(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)
|
||||
```
|
||||
|
||||
|
||||
VGG19 model, with weights pre-trained on ImageNet.
|
||||
|
||||
This model is available for both the Theano and TensorFlow backend, and can be built both
|
||||
with "channels_first" data format (channels, height, width) or "channels_last" data format (height, width, channels).
|
||||
|
||||
The default input size for this model is 224x224.
|
||||
|
||||
### Arguments
|
||||
|
||||
- include_top: whether to include the 3 fully-connected layers at the top of the network.
|
||||
- weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet).
|
||||
- input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model.
|
||||
- input_shape: optional shape tuple, only to be specified
|
||||
if `include_top` is False (otherwise the input shape
|
||||
has to be `(224, 224, 3)` (with `channels_last` data format)
|
||||
or `(3, 224, 224)` (with `channels_first` data format).
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 48.
|
||||
E.g. `(200, 200, 3)` would be one valid value.
|
||||
- pooling: Optional pooling mode for feature extraction
|
||||
when `include_top` is `False`.
|
||||
- `None` means that the output of the model will be
|
||||
the 4D tensor output of the
|
||||
last convolutional layer.
|
||||
- `avg` means that global average pooling
|
||||
will be applied to the output of the
|
||||
last convolutional layer, and thus
|
||||
the output of the model will be a 2D tensor.
|
||||
- `max` means that global max pooling will
|
||||
be applied.
|
||||
- classes: optional number of classes to classify images
|
||||
into, only to be specified if `include_top` is True, and
|
||||
if no `weights` argument is specified.
|
||||
|
||||
### Returns
|
||||
|
||||
A Keras model instance.
|
||||
|
||||
|
||||
### References
|
||||
|
||||
- [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556)
|
||||
|
||||
### License
|
||||
|
||||
These weights are ported from the ones [released by VGG at Oxford](http://www.robots.ox.ac.uk/~vgg/research/very_deep/) under the [Creative Commons Attribution License](https://creativecommons.org/licenses/by/4.0/).
|
||||
|
||||
-----
|
||||
|
||||
## ResNet50
|
||||
|
||||
|
||||
```python
|
||||
keras.applications.resnet50.ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)
|
||||
```
|
||||
|
||||
|
||||
ResNet50 model, with weights pre-trained on ImageNet.
|
||||
|
||||
This model is available for both the Theano and TensorFlow backend, and can be built both
|
||||
with "channels_first" data format (channels, height, width) or "channels_last" data format (height, width, channels).
|
||||
|
||||
The default input size for this model is 224x224.
|
||||
|
||||
|
||||
### Arguments
|
||||
|
||||
- include_top: whether to include the fully-connected layer at the top of the network.
|
||||
- weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet).
|
||||
- input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model.
|
||||
- input_shape: optional shape tuple, only to be specified
|
||||
if `include_top` is False (otherwise the input shape
|
||||
has to be `(224, 224, 3)` (with `channels_last` data format)
|
||||
or `(3, 224, 224)` (with `channels_first` data format).
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 197.
|
||||
E.g. `(200, 200, 3)` would be one valid value.
|
||||
- pooling: Optional pooling mode for feature extraction
|
||||
when `include_top` is `False`.
|
||||
- `None` means that the output of the model will be
|
||||
the 4D tensor output of the
|
||||
last convolutional layer.
|
||||
- `avg` means that global average pooling
|
||||
will be applied to the output of the
|
||||
last convolutional layer, and thus
|
||||
the output of the model will be a 2D tensor.
|
||||
- `max` means that global max pooling will
|
||||
be applied.
|
||||
- classes: optional number of classes to classify images
|
||||
into, only to be specified if `include_top` is True, and
|
||||
if no `weights` argument is specified.
|
||||
|
||||
### Returns
|
||||
|
||||
A Keras model instance.
|
||||
|
||||
### References
|
||||
|
||||
- [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385)
|
||||
|
||||
### License
|
||||
|
||||
These weights are ported from the ones [released by Kaiming He](https://github.com/KaimingHe/deep-residual-networks) under the [MIT license](https://github.com/KaimingHe/deep-residual-networks/blob/master/LICENSE).
|
||||
|
||||
-----
|
||||
|
||||
## InceptionV3
|
||||
|
||||
|
||||
```python
|
||||
keras.applications.inception_v3.InceptionV3(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)
|
||||
```
|
||||
|
||||
Inception V3 model, with weights pre-trained on ImageNet.
|
||||
|
||||
This model is available for both the Theano and TensorFlow backend, and can be built both
|
||||
with "channels_first" data format (channels, height, width) or "channels_last" data format (height, width, channels).
|
||||
|
||||
The default input size for this model is 299x299.
|
||||
|
||||
|
||||
### Arguments
|
||||
|
||||
- include_top: whether to include the fully-connected layer at the top of the network.
|
||||
- weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet).
|
||||
- input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model.
|
||||
- input_shape: optional shape tuple, only to be specified
|
||||
if `include_top` is False (otherwise the input shape
|
||||
has to be `(299, 299, 3)` (with `channels_last` data format)
|
||||
or `(3, 299, 299)` (with `channels_first` data format).
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 139.
|
||||
E.g. `(150, 150, 3)` would be one valid value.
|
||||
- pooling: Optional pooling mode for feature extraction
|
||||
when `include_top` is `False`.
|
||||
- `None` means that the output of the model will be
|
||||
the 4D tensor output of the
|
||||
last convolutional layer.
|
||||
- `avg` means that global average pooling
|
||||
will be applied to the output of the
|
||||
last convolutional layer, and thus
|
||||
the output of the model will be a 2D tensor.
|
||||
- `max` means that global max pooling will
|
||||
be applied.
|
||||
- classes: optional number of classes to classify images
|
||||
into, only to be specified if `include_top` is True, and
|
||||
if no `weights` argument is specified.
|
||||
|
||||
### Returns
|
||||
|
||||
A Keras model instance.
|
||||
|
||||
### References
|
||||
|
||||
- [Rethinking the Inception Architecture for Computer Vision](http://arxiv.org/abs/1512.00567)
|
||||
|
||||
### License
|
||||
|
||||
These weights are released under [the Apache License](https://github.com/tensorflow/models/blob/master/LICENSE).
|
||||
externo
+63
-15
@@ -4,44 +4,82 @@
|
||||
|
||||
Keras is a model-level library, providing high-level building blocks for developing deep learning models. It does not handle itself low-level operations such as tensor products, convolutions and so on. Instead, it relies on a specialized, well-optimized tensor manipulation library to do so, serving as the "backend engine" of Keras. Rather than picking one single tensor library and making the implementation of Keras tied to that library, Keras handles the problem in a modular way, and several different backend engines can be plugged seamlessly into Keras.
|
||||
|
||||
At this time, Keras has two backend implementations available: the **Theano** backend and the **TensorFlow** backend.
|
||||
At this time, Keras has three backend implementations available: the **TensorFlow** backend, the **Theano** backend, and the **CNTK** backend.
|
||||
|
||||
- [Theano](http://deeplearning.net/software/theano/) is an open-source symbolic tensor manipulation framework developed by LISA/MILA Lab at Université de Montréal.
|
||||
- [TensorFlow](http://www.tensorflow.org/) is an open-source symbolic tensor manipulation framework developed by Google, Inc.
|
||||
- [Theano](http://deeplearning.net/software/theano/) is an open-source symbolic tensor manipulation framework developed by LISA/MILA Lab at Université de Montréal.
|
||||
- [CNTK](https://www.microsoft.com/en-us/cognitive-toolkit/) is an open-source, commercial-grade toolkit for deep learning developed by Microsoft.
|
||||
|
||||
In the future, we are likely to add more backend options.
|
||||
|
||||
----
|
||||
|
||||
## Switching from one backend to another
|
||||
|
||||
If you have run Keras at least once, you will find the Keras configuration file at:
|
||||
|
||||
`~/.keras/keras.json`
|
||||
`$HOME/.keras/keras.json`
|
||||
|
||||
If it isn't there, you can create it.
|
||||
|
||||
It probably looks like this:
|
||||
**NOTE for Windows Users:** Please change `$HOME` with `%USERPROFILE%`.
|
||||
|
||||
`{"epsilon": 1e-07, "floatx": "float32", "backend": "theano"}`
|
||||
The default configuration file looks like this:
|
||||
|
||||
Simply change the field `backend` to either `"theano"` or `"tensorflow"`, and Keras will use the new configuration next time you run any Keras code.
|
||||
```
|
||||
{
|
||||
"image_data_format": "channels_last",
|
||||
"epsilon": 1e-07,
|
||||
"floatx": "float32",
|
||||
"backend": "tensorflow"
|
||||
}
|
||||
```
|
||||
|
||||
Simply change the field `backend` to `"theano"`, `"tensorflow"`, or `"cntk"`, and Keras will use the new configuration next time you run any Keras code.
|
||||
|
||||
You can also define the environment variable ``KERAS_BACKEND`` and this will
|
||||
override what is defined in your config file :
|
||||
|
||||
```bash
|
||||
KERAS_BACKEND=tensorflow python -c "from keras import backend; print backend._BACKEND"
|
||||
KERAS_BACKEND=tensorflow python -c "from keras import backend"
|
||||
Using TensorFlow backend.
|
||||
tensorflow
|
||||
```
|
||||
|
||||
----
|
||||
|
||||
## keras.json details
|
||||
|
||||
|
||||
```
|
||||
{
|
||||
"image_data_format": "channels_last",
|
||||
"epsilon": 1e-07,
|
||||
"floatx": "float32",
|
||||
"backend": "tensorflow"
|
||||
}
|
||||
```
|
||||
|
||||
You can change these settings by editing `$HOME/.keras/keras.json`.
|
||||
|
||||
* `image_data_format`: string, either `"channels_last"` or `"channels_first"`. It specifies which data format convention Keras will follow. (`keras.backend.image_data_format()` returns it.)
|
||||
- For 2D data (e.g. image), `"channels_last"` assumes `(rows, cols, channels)` while `"channels_first"` assumes `(channels, rows, cols)`.
|
||||
- For 3D data, `"channels_last"` assumes `(conv_dim1, conv_dim2, conv_dim3, channels)` while `"channels_first"` assumes `(channels, conv_dim1, conv_dim2, conv_dim3)`.
|
||||
* `epsilon`: float, a numeric fuzzing constant used to avoid dividing by zero in some operations.
|
||||
* `floatx`: string, `"float16"`, `"float32"`, or `"float64"`. Default float precision.
|
||||
* `backend`: string, `"tensorflow"`, `"theano"`, or `"cntk"`.
|
||||
|
||||
----
|
||||
|
||||
## Using the abstract Keras backend to write new code
|
||||
|
||||
If you want the Keras modules you write to be compatible with both Theano and TensorFlow, you have to write them via the abstract Keras backend API. Here's an intro.
|
||||
If you want the Keras modules you write to be compatible with both Theano (`th`) and TensorFlow (`tf`), you have to write them via the abstract Keras backend API. Here's an intro.
|
||||
|
||||
You can import the backend module via:
|
||||
```python
|
||||
from keras import backend as K
|
||||
```
|
||||
|
||||
The code below instantiates an input placeholder. It's equivalent to `tf.placeholder()` or `T.matrix()`, `T.tensor3()`, etc.
|
||||
The code below instantiates an input placeholder. It's equivalent to `tf.placeholder()` or `th.tensor.matrix()`, `th.tensor.tensor3()`, etc.
|
||||
|
||||
```python
|
||||
input = K.placeholder(shape=(2, 4, 5))
|
||||
@@ -51,9 +89,10 @@ input = K.placeholder(shape=(None, 4, 5))
|
||||
input = K.placeholder(ndim=3)
|
||||
```
|
||||
|
||||
The code below instantiates a shared variable. It's equivalent to `tf.variable()` or `theano.shared()`.
|
||||
The code below instantiates a shared variable. It's equivalent to `tf.Variable()` or `th.shared()`.
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
val = np.random.random((3, 4, 5))
|
||||
var = K.variable(value=val)
|
||||
|
||||
@@ -66,16 +105,25 @@ var = K.ones(shape=(3, 4, 5))
|
||||
Most tensor operations you will need can be done as you would in TensorFlow or Theano:
|
||||
|
||||
```python
|
||||
# Initializing Tensors with Random Numbers
|
||||
b = K.random_uniform_variable(shape=(3, 4)). # Uniform distribution
|
||||
c = K.random_normal_variable(shape=(3, 4)). # Gaussian distribution
|
||||
d = K.random_normal_variable(shape=(3, 4)).
|
||||
# Tensor Arithmetics
|
||||
a = b + c * K.abs(d)
|
||||
c = K.dot(a, K.transpose(b))
|
||||
a = K.sum(b, axis=2)
|
||||
a = K.sum(b, axis=1)
|
||||
a = K.softmax(b)
|
||||
a = concatenate([b, c], axis=-1)
|
||||
a = K.concatenate([b, c], axis=-1)
|
||||
# etc...
|
||||
```
|
||||
|
||||
For more information, see the code at `keras/backend/theano_backend.py` and `keras/backend/tensorflow_backend.py`.
|
||||
|
||||
----
|
||||
|
||||
## Backend functions
|
||||
|
||||
|
||||
{{autogenerated}}
|
||||
|
||||
|
||||
|
||||
|
||||
externo
+7
-9
@@ -1,6 +1,6 @@
|
||||
## Usage of callbacks
|
||||
|
||||
A callback is a set of functions to be applied at given stages of the training procedure. You can use callbacks to get a view on internal states and statistics of the model during training. You can pass a list of callbacks (as the keyword argument `callbacks`) to the `.fit()` method of the `Sequential` model. The relevant methods of the callbacks will then be called at each stage of the training.
|
||||
A callback is a set of functions to be applied at given stages of the training procedure. You can use callbacks to get a view on internal states and statistics of the model during training. You can pass a list of callbacks (as the keyword argument `callbacks`) to the `.fit()` method of the `Sequential` or `Model` classes. The relevant methods of the callbacks will then be called at each stage of the training.
|
||||
|
||||
---
|
||||
|
||||
@@ -36,14 +36,14 @@ class LossHistory(keras.callbacks.Callback):
|
||||
self.losses.append(logs.get('loss'))
|
||||
|
||||
model = Sequential()
|
||||
model.add(Dense(10, input_dim=784, init='uniform'))
|
||||
model.add(Dense(10, input_dim=784, kernel_initializer='uniform'))
|
||||
model.add(Activation('softmax'))
|
||||
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
|
||||
|
||||
history = LossHistory()
|
||||
model.fit(X_train, Y_train, batch_size=128, nb_epoch=20, verbose=0, callbacks=[history])
|
||||
model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, callbacks=[history])
|
||||
|
||||
print history.losses
|
||||
print(history.losses)
|
||||
# outputs
|
||||
'''
|
||||
[0.66047596406559383, 0.3547245744908703, ..., 0.25953155204159617, 0.25901699725311789]
|
||||
@@ -58,15 +58,13 @@ print history.losses
|
||||
from keras.callbacks import ModelCheckpoint
|
||||
|
||||
model = Sequential()
|
||||
model.add(Dense(10, input_dim=784, init='uniform'))
|
||||
model.add(Dense(10, input_dim=784, kernel_initializer='uniform'))
|
||||
model.add(Activation('softmax'))
|
||||
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
|
||||
|
||||
'''
|
||||
saves the model weights after each epoch if the validation loss decreased
|
||||
'''
|
||||
checkpointer = ModelCheckpoint(filepath="/tmp/weights.hdf5", verbose=1, save_best_only=True)
|
||||
model.fit(X_train, Y_train, batch_size=128, nb_epoch=20, verbose=0, validation_data=(X_test, Y_test), callbacks=[checkpointer])
|
||||
|
||||
checkpointer = ModelCheckpoint(filepath='/tmp/weights.hdf5', verbose=1, save_best_only=True)
|
||||
model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, validation_data=(X_test, Y_test), callbacks=[checkpointer])
|
||||
```
|
||||
|
||||
|
||||
externo
+8
-7
@@ -2,21 +2,22 @@
|
||||
|
||||
Functions from the `constraints` module allow setting constraints (eg. non-negativity) on network parameters during optimization.
|
||||
|
||||
The penalties are applied on a per-layer basis. The exact API will depend on the layer, but the layers `Dense`, `TimeDistributedDense`, `MaxoutDense`, `Convolution1D` and `Convolution2D` have a unified API.
|
||||
The penalties are applied on a per-layer basis. The exact API will depend on the layer, but the layers `Dense`, `Conv1D`, `Conv2D` and `Conv3D` have a unified API.
|
||||
|
||||
These layers expose 2 keyword arguments:
|
||||
|
||||
- `W_constraint` for the main weights matrix
|
||||
- `b_constraint` for the bias.
|
||||
- `kernel_constraint` for the main weights matrix
|
||||
- `bias_constraint` for the bias.
|
||||
|
||||
|
||||
```python
|
||||
from keras.constraints import maxnorm
|
||||
model.add(Dense(64, W_constraint = maxnorm(2)))
|
||||
model.add(Dense(64, kernel_constraint=max_norm(2.)))
|
||||
```
|
||||
|
||||
## Available constraints
|
||||
|
||||
- __maxnorm__(m=2): maximum-norm constraint
|
||||
- __nonneg__(): non-negativity constraint
|
||||
- __unitnorm__(): unit-norm constraint, enforces the matrix to have unit norm along the last axis
|
||||
- __max_norm(max_value=2, axis=0)__: maximum-norm constraint
|
||||
- __non_neg()__: non-negativity constraint
|
||||
- __unit_norm(axis=0)__: unit-norm constraint
|
||||
- __min_max_norm(min_value=0.0, max_value=1.0, rate=1.0, axis=0)__: minimum/maximum-norm constraint
|
||||
|
||||
externo
+92
-37
@@ -2,50 +2,50 @@
|
||||
|
||||
## CIFAR10 small image classification
|
||||
|
||||
`keras.datasets.cifar10`
|
||||
|
||||
Dataset of 50,000 32x32 color training images, labeled over 10 categories, and 10,000 test images.
|
||||
|
||||
### Usage:
|
||||
|
||||
```python
|
||||
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
|
||||
from keras.datasets import cifar10
|
||||
|
||||
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
|
||||
```
|
||||
|
||||
- __Return:__
|
||||
- __Returns:__
|
||||
- 2 tuples:
|
||||
- __X_train, X_test__: uint8 array of RGB image data with shape (nb_samples, 3, 32, 32).
|
||||
- __y_train, y_test__: uint8 array of category labels (integers in range 0-9) with shape (nb_samples,).
|
||||
- __x_train, x_test__: uint8 array of RGB image data with shape (num_samples, 3, 32, 32).
|
||||
- __y_train, y_test__: uint8 array of category labels (integers in range 0-9) with shape (num_samples,).
|
||||
|
||||
|
||||
---
|
||||
|
||||
## CIFAR100 small image classification
|
||||
|
||||
`keras.datasets.cifar100`
|
||||
|
||||
Dataset of 50,000 32x32 color training images, labeled over 100 categories, and 10,000 test images.
|
||||
|
||||
### Usage:
|
||||
|
||||
```python
|
||||
(X_train, y_train), (X_test, y_test) = cifar100.load_data(label_mode='fine')
|
||||
from keras.datasets import cifar100
|
||||
|
||||
(x_train, y_train), (x_test, y_test) = cifar100.load_data(label_mode='fine')
|
||||
```
|
||||
|
||||
- __Return:__
|
||||
- __Returns:__
|
||||
- 2 tuples:
|
||||
- __X_train, X_test__: uint8 array of RGB image data with shape (nb_samples, 3, 32, 32).
|
||||
- __y_train, y_test__: uint8 array of category labels with shape (nb_samples,).
|
||||
- __x_train, x_test__: uint8 array of RGB image data with shape (num_samples, 3, 32, 32).
|
||||
- __y_train, y_test__: uint8 array of category labels with shape (num_samples,).
|
||||
|
||||
- __Arguments:__
|
||||
|
||||
- __label_mode__: "fine" or "coarse".
|
||||
|
||||
|
||||
---
|
||||
|
||||
## IMDB Movie reviews sentiment classification
|
||||
|
||||
`keras.datasets.imdb`
|
||||
|
||||
Dataset of 25,000 movies reviews from IMDB, labeled by sentiment (positive/negative). Reviews have been preprocessed, and each review is encoded as a [sequence](preprocessing/sequence.md) of word indexes (integers). For convenience, words are indexed by overall frequency in the dataset, so that for instance the integer "3" encodes the 3rd most frequent word in the data. This allows for quick filtering operations such as: "only consider the top 10,000 most common words, but eliminate the top 20 most common words".
|
||||
|
||||
As a convention, "0" does not stand for a specific word, but instead is used to encode any unknown word.
|
||||
@@ -53,69 +53,124 @@ As a convention, "0" does not stand for a specific word, but instead is used to
|
||||
### Usage:
|
||||
|
||||
```python
|
||||
(X_train, y_train), (X_test, y_test) = imdb.load_data(path="imdb.pkl", \
|
||||
nb_words=None, skip_top=0, maxlen=None, test_split=0.1, seed=113)
|
||||
from keras.datasets import imdb
|
||||
|
||||
(x_train, y_train), (x_test, y_test) = imdb.load_data(path="imdb.npz",
|
||||
num_words=None,
|
||||
skip_top=0,
|
||||
maxlen=None,
|
||||
seed=113,
|
||||
start_char=1,
|
||||
oov_char=2,
|
||||
index_from=3)
|
||||
```
|
||||
- __Return:__
|
||||
- __Returns:__
|
||||
- 2 tuples:
|
||||
- __X_train, X_test__: list of sequences, which are lists of indexes (integers). If the nb_words argument was specific, the maximum possible index value is nb_words-1. If the maxlen argument was specified, the largest possible sequence length is maxlen.
|
||||
- __x_train, x_test__: list of sequences, which are lists of indexes (integers). If the num_words argument was specific, the maximum possible index value is num_words-1. If the maxlen argument was specified, the largest possible sequence length is maxlen.
|
||||
- __y_train, y_test__: list of integer labels (1 or 0).
|
||||
|
||||
- __Arguments:__
|
||||
|
||||
- __path__: if you do have the data locally (at `'~/.keras/datasets/' + path`), if will be downloaded to this location (in cPickle format).
|
||||
- __nb_words__: integer or None. Top most frequent words to consider. Any less frequent word will appear as 0 in the sequence data.
|
||||
- __skip_top__: integer. Top most frequent words to ignore (they will appear as 0s in the sequence data).
|
||||
- __path__: if you do not have the data locally (at `'~/.keras/datasets/' + path`), it will be downloaded to this location.
|
||||
- __num_words__: integer or None. Top most frequent words to consider. Any less frequent word will appear as `oov_char` value in the sequence data.
|
||||
- __skip_top__: integer. Top most frequent words to ignore (they will appear as `oov_char` value in the sequence data).
|
||||
- __maxlen__: int. Maximum sequence length. Any longer sequence will be truncated.
|
||||
- __test_split__: float. Fraction of the dataset to be used as test data.
|
||||
- __seed__: int. Seed for reproducible data shuffling.
|
||||
- __start_char__: int. The start of a sequence will be marked with this character.
|
||||
Set to 1 because 0 is usually the padding character.
|
||||
- __oov_char__: int. words that were cut out because of the `num_words`
|
||||
or `skip_top` limit will be replaced with this character.
|
||||
- __index_from__: int. Index actual words with this index and higher.
|
||||
|
||||
|
||||
---
|
||||
|
||||
## Reuters newswire topics classification
|
||||
|
||||
`keras.datasets.reuters`
|
||||
|
||||
Dataset of 11,228 newswires from Reuters, labeled over 46 topics. As with the IMDB dataset, each wire is encoded as a sequence of word indexes (same conventions).
|
||||
|
||||
### Usage:
|
||||
|
||||
```python
|
||||
(X_train, y_train), (X_test, y_test) = reuters.load_data(path="reuters.pkl", \
|
||||
nb_words=None, skip_top=0, maxlen=None, test_split=0.1, seed=113)
|
||||
from keras.datasets import reuters
|
||||
|
||||
(x_train, y_train), (x_test, y_test) = reuters.load_data(path="reuters.npz",
|
||||
num_words=None,
|
||||
skip_top=0,
|
||||
maxlen=None,
|
||||
test_split=0.2,
|
||||
seed=113,
|
||||
start_char=1,
|
||||
oov_char=2,
|
||||
index_from=3)
|
||||
```
|
||||
|
||||
The specifications are the same as that of the IMDB dataset.
|
||||
The specifications are the same as that of the IMDB dataset, with the addition of:
|
||||
|
||||
- __test_split__: float. Fraction of the dataset to be used as test data.
|
||||
|
||||
This dataset also makes available the word index used for encoding the sequences:
|
||||
|
||||
```python
|
||||
word_index = reuters.get_word_index(path="reuters_word_index.pkl")
|
||||
word_index = reuters.get_word_index(path="reuters_word_index.json")
|
||||
```
|
||||
|
||||
- __Return:__ A dictionary where key are words (str) and values are indexes (integer). eg. `word_index["giraffe"]` might return `1234`.
|
||||
- __Returns:__ A dictionary where key are words (str) and values are indexes (integer). eg. `word_index["giraffe"]` might return `1234`.
|
||||
|
||||
- __Arguments:__
|
||||
|
||||
- __path__: if you do have the index file locally (at `'~/.keras/datasets/' + path`), if will be downloaded to this location (in cPickle format).
|
||||
- __path__: if you do not have the index file locally (at `'~/.keras/datasets/' + path`), it will be downloaded to this location.
|
||||
|
||||
## MNIST database of handwritten digits
|
||||
|
||||
`keras.datasets.mnist`
|
||||
---
|
||||
|
||||
## MNIST database of handwritten digits
|
||||
|
||||
Dataset of 60,000 28x28 grayscale images of the 10 digits, along with a test set of 10,000 images.
|
||||
|
||||
### Usage:
|
||||
|
||||
```python
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
from keras.datasets import mnist
|
||||
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
```
|
||||
|
||||
- __Return:__
|
||||
- __Returns:__
|
||||
- 2 tuples:
|
||||
- __X_train, X_test__: uint8 array of grayscale image data with shape (nb_samples, 28, 28).
|
||||
- __y_train, y_test__: uint8 array of digit labels (integers in range 0-9) with shape (nb_samples,).
|
||||
- __x_train, x_test__: uint8 array of grayscale image data with shape (num_samples, 28, 28).
|
||||
- __y_train, y_test__: uint8 array of digit labels (integers in range 0-9) with shape (num_samples,).
|
||||
|
||||
- __Arguments:__
|
||||
|
||||
- __path__: if you do have the index file locally (at `'~/.keras/datasets/' + path`), if will be downloaded to this location (in cPickle format).
|
||||
- __path__: if you do not have the index file locally (at `'~/.keras/datasets/' + path`), it will be downloaded to this location.
|
||||
|
||||
|
||||
---
|
||||
|
||||
## Boston housing price regression dataset
|
||||
|
||||
|
||||
Dataset taken from the StatLib library which is maintained at Carnegie Mellon University.
|
||||
|
||||
Samples contain 13 attributes of houses at different locations around the Boston suburbs in the late 1970s.
|
||||
Targets are the median values of the houses at a location (in k$).
|
||||
|
||||
|
||||
### Usage:
|
||||
|
||||
```python
|
||||
from keras.datasets import boston_housing
|
||||
|
||||
(x_train, y_train), (x_test, y_test) = boston_housing.load_data()
|
||||
```
|
||||
|
||||
- __Arguments:__
|
||||
- __path__: path where to cache the dataset locally
|
||||
(relative to ~/.keras/datasets).
|
||||
- __seed__: Random seed for shuffling the data
|
||||
before computing the test split.
|
||||
- __test_split__: fraction of the data to reserve as test set.
|
||||
|
||||
- __Returns:__
|
||||
Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
|
||||
externo
-40
@@ -1,40 +0,0 @@
|
||||
# Keras Documentation Index
|
||||
|
||||
## Introduction
|
||||
|
||||
- [Home](index.md)
|
||||
- [Index](documentation.md)
|
||||
- [Examples](examples.md)
|
||||
- [FAQ](faq.md)
|
||||
- [Backend](backend.md)
|
||||
|
||||
---
|
||||
|
||||
## Base functionality
|
||||
|
||||
- [Optimizers](optimizers.md)
|
||||
- [Objectives](objectives.md)
|
||||
- [Models](models.md)
|
||||
- [Activations](activations.md)
|
||||
- [Initializations](initializations.md)
|
||||
- [Regularizers](regularizers.md)
|
||||
- [Constraints](constraints.md)
|
||||
- [Callbacks](callbacks.md)
|
||||
- [Datasets](datasets.md)
|
||||
|
||||
---
|
||||
|
||||
## Layers
|
||||
- [Core](layers/core.md)
|
||||
- [Convolutional](layers/convolutional.md)
|
||||
- [Recurrent](layers/recurrent.md)
|
||||
- [Advanced Activations](layers/advanced_activations.md)
|
||||
- [Normalization](layers/normalization.md)
|
||||
- [Embeddings](layers/embeddings.md)
|
||||
|
||||
---
|
||||
|
||||
## Preprocessing
|
||||
- [Sequence](preprocessing/sequence.md)
|
||||
- [Text](preprocessing/text.md)
|
||||
- [Image](preprocessing/image.md)
|
||||
externo
-390
@@ -1,390 +0,0 @@
|
||||
|
||||
Here are a few examples to get you started!
|
||||
|
||||
In the examples folder, you will also find example models for real datasets:
|
||||
|
||||
- CIFAR10 small images classification: Convolutional Neural Network (CNN) with realtime data augmentation
|
||||
- IMDB movie review sentiment classification: LSTM over sequences of words
|
||||
- Reuters newswires topic classification: Multilayer Perceptron (MLP)
|
||||
- MNIST handwritten digits classification: MLP & CNN
|
||||
- Character-level text generation with LSTM
|
||||
|
||||
...and more.
|
||||
|
||||
------------------
|
||||
|
||||
### Multilayer Perceptron (MLP) for multi-class softmax classification:
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Dropout, Activation
|
||||
from keras.optimizers import SGD
|
||||
|
||||
model = Sequential()
|
||||
# Dense(64) is a fully-connected layer with 64 hidden units.
|
||||
# in the first layer, you must specify the expected input data shape:
|
||||
# here, 20-dimensional vectors.
|
||||
model.add(Dense(64, input_dim=20, init='uniform'))
|
||||
model.add(Activation('tanh'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(64, init='uniform'))
|
||||
model.add(Activation('tanh'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(10, init='uniform'))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer=sgd)
|
||||
|
||||
model.fit(X_train, y_train,
|
||||
nb_epoch=20,
|
||||
batch_size=16,
|
||||
show_accuracy=True)
|
||||
score = model.evaluate(X_test, y_test, batch_size=16)
|
||||
```
|
||||
|
||||
------------------
|
||||
|
||||
### Alternative implementation of a similar MLP:
|
||||
|
||||
```python
|
||||
model = Sequential()
|
||||
model.add(Dense(64, input_dim=20, activation='relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(64, activation='relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(10, activation='softmax'))
|
||||
|
||||
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
|
||||
```
|
||||
|
||||
------------------
|
||||
|
||||
### MLP for binary classification:
|
||||
```python
|
||||
model = Sequential()
|
||||
model.add(Dense(64, input_dim=20, init='uniform', activation='relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(64, activation='relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(1, activation='sigmoid'))
|
||||
|
||||
model.compile(loss='binary_crossentropy',
|
||||
optimizer='rmsprop')
|
||||
```
|
||||
|
||||
------------------
|
||||
|
||||
### VGG-like convnet:
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Dropout, Activation, Flatten
|
||||
from keras.layers import Convolution2D, MaxPooling2D
|
||||
from keras.optimizers import SGD
|
||||
|
||||
model = Sequential()
|
||||
# input: 100x100 images with 3 channels -> (3, 100, 100) tensors.
|
||||
# this applies 32 convolution filters of size 3x3 each.
|
||||
model.add(Convolution2D(32, 3, 3, border_mode='valid', input_shape=(3, 100, 100)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Convolution2D(32, 3, 3))
|
||||
model.add(Activation('relu'))
|
||||
model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
model.add(Dropout(0.25))
|
||||
|
||||
model.add(Convolution2D(64, 3, 3, border_mode='valid'))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Convolution2D(64, 3, 3))
|
||||
model.add(Activation('relu'))
|
||||
model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
model.add(Dropout(0.25))
|
||||
|
||||
model.add(Flatten())
|
||||
# Note: Keras does automatic shape inference.
|
||||
model.add(Dense(256))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dropout(0.5))
|
||||
|
||||
model.add(Dense(10))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
|
||||
model.compile(loss='categorical_crossentropy', optimizer=sgd)
|
||||
|
||||
model.fit(X_train, Y_train, batch_size=32, nb_epoch=1)
|
||||
|
||||
```
|
||||
|
||||
------------------
|
||||
|
||||
### Sequence classification with LSTM:
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Dropout, Activation
|
||||
from keras.layers import Embedding
|
||||
from keras.layers import LSTM
|
||||
|
||||
model = Sequential()
|
||||
model.add(Embedding(max_features, 256, input_length=maxlen))
|
||||
model.add(LSTM(output_dim=128, activation='sigmoid', inner_activation='hard_sigmoid'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(1))
|
||||
model.add(Activation('sigmoid'))
|
||||
|
||||
model.compile(loss='binary_crossentropy', optimizer='rmsprop')
|
||||
|
||||
model.fit(X_train, Y_train, batch_size=16, nb_epoch=10)
|
||||
score = model.evaluate(X_test, Y_test, batch_size=16)
|
||||
```
|
||||
|
||||
### Architecture for learning image captions with a convnet and a Gated Recurrent Unit:
|
||||
(word-level embedding, caption of maximum length 16 words).
|
||||
|
||||
Note that getting this to work well will require using a bigger convnet, initialized with pre-trained weights.
|
||||
|
||||
```python
|
||||
max_caption_len = 16
|
||||
vocab_size = 10000
|
||||
|
||||
# first, let's define an image model that
|
||||
# will encode pictures into 128-dimensional vectors.
|
||||
# it should be initialized with pre-trained weights.
|
||||
image_model = Sequential()
|
||||
image_model.add(Convolution2D(32, 3, 3, border_mode='valid', input_shape=(3, 100, 100)))
|
||||
image_model.add(Activation('relu'))
|
||||
image_model.add(Convolution2D(32, 3, 3))
|
||||
image_model.add(Activation('relu'))
|
||||
image_model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
|
||||
image_model.add(Convolution2D(64, 3, 3, border_mode='valid'))
|
||||
image_model.add(Activation('relu'))
|
||||
image_model.add(Convolution2D(64, 3, 3))
|
||||
image_model.add(Activation('relu'))
|
||||
image_model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
|
||||
image_model.add(Flatten())
|
||||
image_model.add(Dense(128))
|
||||
|
||||
# let's load the weights from a save file.
|
||||
image_model.load_weights('weight_file.h5')
|
||||
|
||||
# next, let's define a RNN model that encodes sequences of words
|
||||
# into sequences of 128-dimensional word vectors.
|
||||
language_model = Sequential()
|
||||
language_model.add(Embedding(vocab_size, 256, input_length=max_caption_len))
|
||||
language_model.add(GRU(output_dim=128, return_sequences=True))
|
||||
language_model.add(TimeDistributedDense(128))
|
||||
|
||||
# let's repeat the image vector to turn it into a sequence.
|
||||
image_model.add(RepeatVector(max_caption_len))
|
||||
|
||||
# the output of both models will be tensors of shape (samples, max_caption_len, 128).
|
||||
# let's concatenate these 2 vector sequences.
|
||||
model = Sequential()
|
||||
model.add(Merge([image_model, language_model], mode='concat', concat_axis=-1))
|
||||
# let's encode this vector sequence into a single vector
|
||||
model.add(GRU(256, return_sequences=False))
|
||||
# which will be used to compute a probability
|
||||
# distribution over what the next word in the caption should be!
|
||||
model.add(Dense(vocab_size))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
|
||||
|
||||
# "images" is a numpy float array of shape (nb_samples, nb_channels=3, width, height).
|
||||
# "captions" is a numpy integer array of shape (nb_samples, max_caption_len)
|
||||
# containing word index sequences representing partial captions.
|
||||
# "next_words" is a numpy float array of shape (nb_samples, vocab_size)
|
||||
# containing a categorical encoding (0s and 1s) of the next word in the corresponding
|
||||
# partial caption.
|
||||
model.fit([images, partial_captions], next_words, batch_size=16, nb_epoch=100)
|
||||
```
|
||||
|
||||
------------------
|
||||
|
||||
### Stacked LSTM for sequence classification
|
||||
|
||||
In this model, we stack 3 LSTM layers on top of each other,
|
||||
making the model capable of learning higher-level temporal representations.
|
||||
|
||||
The first two LSTMs return their full output sequences, but the last one only returns
|
||||
the last step in its output sequence, thus dropping the temporal dimension
|
||||
(i.e. converting the input sequence into a single vector).
|
||||
|
||||
<img src="http://keras.io/img/regular_stacked_lstm.png" alt="stacked LSTM" style="width: 300px;"/>
|
||||
|
||||
(N.B.: in Keras, "None" in an input shape indicates a variable dimension. In the graph above, the batch size is "None",
|
||||
meaning that any batch size is allowed for the input data).
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import LSTM, Dense
|
||||
import numpy as np
|
||||
|
||||
data_dim = 16
|
||||
timesteps = 8
|
||||
nb_classes = 10
|
||||
|
||||
# expected input data shape: (batch_size, timesteps, data_dim)
|
||||
model = Sequential()
|
||||
model.add(LSTM(32, return_sequences=True,
|
||||
input_shape=(timesteps, data_dim))) # returns a sequence of vectors of dimension 32
|
||||
model.add(LSTM(32, return_sequences=True)) # returns a sequence of vectors of dimension 32
|
||||
model.add(LSTM(32)) # return a single vector of dimension 32
|
||||
model.add(Dense(10, activation='softmax'))
|
||||
|
||||
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
|
||||
|
||||
# generate dummy training data
|
||||
x_train = np.random.random((1000, timesteps, data_dim))
|
||||
y_train = np.random.random((1000, nb_classes))
|
||||
|
||||
# generate dummy validation data
|
||||
x_val = np.random.random((100, timesteps, data_dim))
|
||||
y_val = np.random.random((100, nb_classes))
|
||||
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=64, nb_epoch=5, show_accuracy=True,
|
||||
validation_data=(x_val, y_val))
|
||||
```
|
||||
|
||||
------------------
|
||||
|
||||
### Same stacked LSTM model, rendered "stateful"
|
||||
|
||||
A stateful recurrent model is one for which the internal states (memories) obtained after processing a batch
|
||||
of samples are reused as initial states for the samples of the next batch. This allows to process longer sequences
|
||||
while keeping computational complexity manageable.
|
||||
|
||||
[You can read more about stateful RNNs in the FAQ.](/faq/#how-can-i-use-stateful-rnns)
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import LSTM, Dense
|
||||
import numpy as np
|
||||
|
||||
data_dim = 16
|
||||
timesteps = 8
|
||||
nb_classes = 10
|
||||
batch_size = 32
|
||||
|
||||
# expected input batch shape: (batch_size, timesteps, data_dim)
|
||||
# note that we have to provide the full batch_input_shape since the network is stateful.
|
||||
# the sample of index i in batch k is the follow-up for the sample i in batch k-1.
|
||||
model = Sequential()
|
||||
model.add(LSTM(32, return_sequences=True, stateful=True,
|
||||
batch_input_shape=(batch_size, timesteps, data_dim)))
|
||||
model.add(LSTM(32, return_sequences=True, stateful=True))
|
||||
model.add(LSTM(32, stateful=True))
|
||||
model.add(Dense(10, activation='softmax'))
|
||||
|
||||
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
|
||||
|
||||
# generate dummy training data
|
||||
x_train = np.random.random((batch_size * 10, timesteps, data_dim))
|
||||
y_train = np.random.random((batch_size * 10, nb_classes))
|
||||
|
||||
# generate dummy validation data
|
||||
x_val = np.random.random((batch_size * 3, timesteps, data_dim))
|
||||
y_val = np.random.random((batch_size * 3, nb_classes))
|
||||
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size, nb_epoch=5, show_accuracy=True,
|
||||
validation_data=(x_val, y_val))
|
||||
```
|
||||
|
||||
------------------
|
||||
|
||||
### Two merged LSTM encoders for classification over two parallel sequences
|
||||
|
||||
In this model, two input sequences are encoded into vectors by two separate LSTM modules.
|
||||
|
||||
These two vectors are then concatenated, and a fully connected network is trained on top of the concatenated representations.
|
||||
|
||||

|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Merge, LSTM, Dense
|
||||
import numpy as np
|
||||
|
||||
data_dim = 16
|
||||
timesteps = 8
|
||||
nb_classes = 10
|
||||
|
||||
encoder_a = Sequential()
|
||||
encoder_a.add(LSTM(32, input_shape=(timesteps, data_dim)))
|
||||
|
||||
encoder_b = Sequential()
|
||||
encoder_b.add(LSTM(32, input_shape=(timesteps, data_dim)))
|
||||
|
||||
decoder = Sequential()
|
||||
decoder.add(Merge([encoder_a, encoder_b], mode='concat'))
|
||||
decoder.add(Dense(32, activation='relu'))
|
||||
decoder.add(Dense(nb_classes, activation='softmax'))
|
||||
|
||||
decoder.compile(loss='categorical_crossentropy', optimizer='rmsprop')
|
||||
|
||||
# generate dummy training data
|
||||
x_train_a = np.random.random((1000, timesteps, data_dim))
|
||||
x_train_b = np.random.random((1000, timesteps, data_dim))
|
||||
y_train = np.random.random((1000, nb_classes))
|
||||
|
||||
# generate dummy validation data
|
||||
x_val_a = np.random.random((100, timesteps, data_dim))
|
||||
x_val_b = np.random.random((100, timesteps, data_dim))
|
||||
y_val = np.random.random((100, nb_classes))
|
||||
|
||||
decoder.fit([x_train_a, x_train_b], y_train,
|
||||
batch_size=64, nb_epoch=5, show_accuracy=True,
|
||||
validation_data=([x_val_a, x_val_b], y_val))
|
||||
```
|
||||
|
||||
------------------
|
||||
|
||||
### Single shared LSTM over two parallel sequences, for classification
|
||||
|
||||
This is a similar setup as above, but now a single LSTM encoder is used for both input sequences.
|
||||
Such a setup makes sense if the two input sequences are the same type of object.
|
||||
|
||||
<img src="http://keras.io/img/shared_lstm.png" alt="Shared LSTM" style="width: 500px;"/>
|
||||
|
||||
```python
|
||||
from keras.models import Graph
|
||||
from keras.layers import LSTM, Dense
|
||||
import numpy as np
|
||||
|
||||
data_dim = 16
|
||||
timesteps = 8
|
||||
nb_classes = 10
|
||||
|
||||
encoder = Sequential()
|
||||
encoder.add(LSTM(32, input_shape=(timesteps, data_dim)))
|
||||
|
||||
model = Graph()
|
||||
model.add_input(name='input_a', input_shape=(timesteps, data_dim))
|
||||
model.add_input(name='input_b', input_shape=(timesteps, data_dim))
|
||||
model.add_shared_node(encoder, name='shared_encoder', inputs=['input_a', 'input_b'],
|
||||
merge_mode='concat')
|
||||
model.add_node(Dense(64, activation='relu'), name='fc1', input='shared_encoder')
|
||||
model.add_node(Dense(3, activation='softmax'), name='output', input='fc1', create_output=True)
|
||||
|
||||
model.compile(optimizer='adam', loss={'output': 'categorical_crossentropy'})
|
||||
|
||||
# generate dummy training data
|
||||
x_train_a = np.random.random((1000, timesteps, data_dim))
|
||||
x_train_b = np.random.random((1000, timesteps, data_dim))
|
||||
y_train = np.random.random((1000, 3))
|
||||
|
||||
# generate dummy validation data
|
||||
x_val_a = np.random.random((100, timesteps, data_dim))
|
||||
x_val_b = np.random.random((100, timesteps, data_dim))
|
||||
y_val = np.random.random((100, 3))
|
||||
|
||||
model.fit({'input_a': x_train_a, 'input_b': x_train_b, 'output': y_train},
|
||||
batch_size=64, nb_epoch=5,
|
||||
validation_data={'input_a': x_val_a, 'input_b': x_val_b, 'output': y_val})
|
||||
```
|
||||
externo
-244
@@ -1,244 +0,0 @@
|
||||
# Keras FAQ: Frequently Asked Keras Questions
|
||||
|
||||
[How should I cite Keras?](#how-should-i-cite-keras)
|
||||
|
||||
[How can I run Keras on GPU?](#how-can-i-run-keras-on-gpu)
|
||||
|
||||
[How can I save a Keras model?](#how-can-i-save-a-keras-model)
|
||||
|
||||
[Why is the training loss much higher than the testing loss?](#why-is-the-training-loss-much-higher-than-the-testing-loss)
|
||||
|
||||
[How can I visualize the output of an intermediate layer?](#how-can-i-visualize-the-output-of-an-intermediate-layer)
|
||||
|
||||
[How can I use Keras with datasets that don't fit in memory?](#how-can-i-use-keras-with-datasets-that-dont-fit-in-memory)
|
||||
|
||||
[How can I interrupt training when the validation loss isn't decreasing anymore?](#how-can-i-interrupt-training-when-the-validation-loss-isnt-decreasing-anymore)
|
||||
|
||||
[How is the validation split computed?](#how-is-the-validation-split-computed)
|
||||
|
||||
[Is the data shuffled during training?](#is-the-data-shuffled-during-training)
|
||||
|
||||
[How can I record the training / validation loss / accuracy at each epoch?](#how-can-i-record-the-training-validation-loss-accuracy-at-each-epoch)
|
||||
|
||||
[How can I use stateful RNNs?](#how-can-i-use-stateful-rnns)
|
||||
|
||||
---
|
||||
|
||||
### How should I cite Keras?
|
||||
|
||||
Please cite Keras in your publications if it helps your research. Here is an example BibTeX entry:
|
||||
|
||||
```
|
||||
@misc{chollet2015keras,
|
||||
author = {Chollet, François},
|
||||
title = {Keras},
|
||||
year = {2015},
|
||||
publisher = {GitHub},
|
||||
journal = {GitHub repository},
|
||||
howpublished = {\url{https://github.com/fchollet/keras}}
|
||||
}
|
||||
```
|
||||
|
||||
### How can I run Keras on GPU?
|
||||
|
||||
If you are running on the TensorFlow backend, your code will automatically run on GPU if any available GPU is detected.
|
||||
If you are running on the Theano backend, you can use one of the following methods:
|
||||
|
||||
Method 1: use Theano flags.
|
||||
```bash
|
||||
THEANO_FLAGS=device=gpu,floatX=float32 python my_keras_script.py
|
||||
```
|
||||
|
||||
The name 'gpu' might have to be changed depending on your device's identifier (e.g. `gpu0`, `gpu1`, etc).
|
||||
|
||||
Method 2: set up your `.theanorc`: [Instructions](http://deeplearning.net/software/theano/library/config.html)
|
||||
|
||||
Method 3: manually set `theano.config.device`, `theano.config.floatX` at the beginning of your code:
|
||||
```python
|
||||
import theano
|
||||
theano.config.device = 'gpu'
|
||||
theano.config.floatX = 'float32'
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### How can I save a Keras model?
|
||||
|
||||
*It is not recommended to use pickle or cPickle to save a Keras model.*
|
||||
|
||||
If you only need to save the architecture of a model, and not its weights, you can do:
|
||||
|
||||
```python
|
||||
# save as JSON
|
||||
json_string = model.to_json()
|
||||
|
||||
# save as YAML
|
||||
yaml_string = model.to_yaml()
|
||||
```
|
||||
|
||||
You can then build a fresh model from this data:
|
||||
|
||||
```python
|
||||
# model reconstruction from JSON:
|
||||
from keras.models import model_from_json
|
||||
model = model_from_json(json_string)
|
||||
|
||||
# model reconstruction from YAML
|
||||
model = model_from_yaml(yaml_string)
|
||||
```
|
||||
|
||||
If you need to save the weights of a model, you can do so in HDF5 with the code below.
|
||||
|
||||
Note that you will first need to install HDF5 and the Python library h5py, which do not come bundled with Keras.
|
||||
|
||||
```python
|
||||
model.save_weights('my_model_weights.h5')
|
||||
```
|
||||
|
||||
Assuming you have code for instantiating your model, you can then load the weights you saved into a model with the same architecture:
|
||||
|
||||
```python
|
||||
model.load_weights('my_model_weights.h5')
|
||||
```
|
||||
|
||||
This leads us to a way to save and reconstruct models from only serialized data:
|
||||
```python
|
||||
json_string = model.to_json()
|
||||
open('my_model_architecture.json', 'w').write(json_string)
|
||||
model.save_weights('my_model_weights.h5')
|
||||
|
||||
# elsewhere...
|
||||
model = model_from_json(open('my_model_architecture.json').read())
|
||||
model.load_weights('my_model_weights.h5')
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Why is the training loss much higher than the testing loss?
|
||||
|
||||
A Keras model has two modes: training and testing. Regularization mechanisms, such as Dropout and L1/L2 weight regularization, are turned off at testing time.
|
||||
|
||||
Besides, the training loss is the average of the losses over each batch of training data. Because your model is changing over time, the loss over the first batches of an epoch is generally higher than over the last batches. On the other hand, the testing loss for an epoch is computed using the model as it is at the end of the epoch, resulting in a lower loss.
|
||||
|
||||
---
|
||||
|
||||
### How can I visualize the output of an intermediate layer?
|
||||
|
||||
You can build a Keras function that will return the output of a certain layer given a certain input, for example:
|
||||
|
||||
```python
|
||||
from keras import backend as K
|
||||
|
||||
# with a Sequential model
|
||||
get_3rd_layer_output = K.function([model.layers[0].input],
|
||||
[model.layers[3].get_output(train=False)])
|
||||
layer_output = get_3rd_layer_output([X])[0]
|
||||
|
||||
# with a Graph model
|
||||
get_conv_layer_output = K.function([model.inputs[i].input for i in model.input_order],
|
||||
[model.nodes['conv'].get_output(train=False)])
|
||||
conv_output = get_conv_layer_output([input_data_dict[i] for i in model.input_order])[0]
|
||||
```
|
||||
|
||||
Similarly, you could build a Theano and TensorFlow function directly.
|
||||
|
||||
---
|
||||
|
||||
### How can I use Keras with datasets that don't fit in memory?
|
||||
|
||||
You can do batch training using `model.train_on_batch(X, y)` and `model.test_on_batch(X, y)`. See the [models documentation](models.md).
|
||||
|
||||
Alternatively, you can write a generator that yields batches of training data and use the method `model.fit_generator(data_generator, samples_per_epoch, nb_epoch)`.
|
||||
|
||||
You can see batch training in action in our [CIFAR10 example](https://github.com/fchollet/keras/blob/master/examples/cifar10_cnn.py).
|
||||
|
||||
---
|
||||
|
||||
### How can I interrupt training when the validation loss isn't decreasing anymore?
|
||||
|
||||
You can use an `EarlyStopping` callback:
|
||||
|
||||
```python
|
||||
from keras.callbacks import EarlyStopping
|
||||
early_stopping = EarlyStopping(monitor='val_loss', patience=2)
|
||||
model.fit(X, y, validation_split=0.2, callbacks=[early_stopping])
|
||||
```
|
||||
|
||||
Find out more in the [callbacks documentation](callbacks.md).
|
||||
|
||||
---
|
||||
|
||||
### How is the validation split computed?
|
||||
|
||||
If you set the `validation_split` argument in `model.fit` to e.g. 0.1, then the validation data used will be the *last 10%* of the data. If you set it to 0.25, it will be the last 25% of the data, etc.
|
||||
|
||||
|
||||
---
|
||||
|
||||
### Is the data shuffled during training?
|
||||
|
||||
Yes, if the `shuffle` argument in `model.fit` is set to `True` (which is the default), the training data will be randomly shuffled at each epoch.
|
||||
|
||||
Validation data isn't shuffled.
|
||||
|
||||
---
|
||||
|
||||
|
||||
### How can I record the training / validation loss / accuracy at each epoch?
|
||||
|
||||
The `model.fit` method returns an `History` callback, which has a `history` attribute containing the lists of successive losses / accuracies.
|
||||
|
||||
```python
|
||||
hist = model.fit(X, y, validation_split=0.2)
|
||||
print(hist.history)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### How can I use stateful RNNs?
|
||||
|
||||
Making a RNN stateful means that the states for the samples of each batch will be reused as initial states for the samples in the next batch.
|
||||
|
||||
When using stateful RNNs, it is therefore assumed that:
|
||||
|
||||
- all batches have the same number of samples
|
||||
- If `X1` and `X2` are successive batches of samples, then `X2[i]` is the follow-up sequence to `X1[i]`, for every `i`.
|
||||
|
||||
To use statefulness in RNNs, you need to:
|
||||
|
||||
- explicitly specify the batch size you are using, by passing a `batch_input_shape` argument to the first layer in your model. It should be a tuple of integers, e.g. `(32, 10, 16)` for a 32-samples batch of sequences of 10 timesteps with 16 features per timestep.
|
||||
- set `stateful=True` in your RNN layer(s).
|
||||
|
||||
To reset the states accumulated:
|
||||
|
||||
- use `model.reset_states()` to reset the states of all layers in the model
|
||||
- use `layer.reset_states()` to reset the states of a specific stateful RNN layer
|
||||
|
||||
Example:
|
||||
|
||||
```python
|
||||
|
||||
X # this is our input data, of shape (32, 21, 16)
|
||||
# we will feed it to our model in sequences of length 10
|
||||
|
||||
model = Sequential()
|
||||
model.add(LSTM(32, batch_input_shape=(32, 10, 16), stateful=True))
|
||||
model.add(Dense(16, activation='softmax'))
|
||||
|
||||
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
|
||||
|
||||
# we train the network to predict the 11th timestep given the first 10:
|
||||
model.train_on_batch(X[:, :10, :], np.reshape(X[:, 10, :], (32, 16)))
|
||||
|
||||
# the state of the network has changed. We can feed the follow-up sequences:
|
||||
model.train_on_batch(X[:, 10:20, :], np.reshape(X[:, 20, :], (32, 16)))
|
||||
|
||||
# let's reset the states of the LSTM layer:
|
||||
model.reset_states()
|
||||
|
||||
# another way to do it in this case:
|
||||
model.layers[0].reset_states()
|
||||
```
|
||||
|
||||
Notes that the methods `predict`, `fit`, `train_on_batch`, `predict_classes`, etc. will *all* update the states of the stateful layers in a model. This allows you to do not only stateful training, but also stateful prediction.
|
||||
|
||||
+456
@@ -0,0 +1,456 @@
|
||||
# Keras FAQ: Frequently Asked Keras Questions
|
||||
|
||||
- [How should I cite Keras?](#how-should-i-cite-keras)
|
||||
- [How can I run Keras on GPU?](#how-can-i-run-keras-on-gpu)
|
||||
- [What does "sample", "batch", "epoch" mean?](#what-does-sample-batch-epoch-mean)
|
||||
- [How can I save a Keras model?](#how-can-i-save-a-keras-model)
|
||||
- [Why is the training loss much higher than the testing loss?](#why-is-the-training-loss-much-higher-than-the-testing-loss)
|
||||
- [How can I obtain the output of an intermediate layer?](#how-can-i-obtain-the-output-of-an-intermediate-layer)
|
||||
- [How can I use Keras with datasets that don't fit in memory?](#how-can-i-use-keras-with-datasets-that-dont-fit-in-memory)
|
||||
- [How can I interrupt training when the validation loss isn't decreasing anymore?](#how-can-i-interrupt-training-when-the-validation-loss-isnt-decreasing-anymore)
|
||||
- [How is the validation split computed?](#how-is-the-validation-split-computed)
|
||||
- [Is the data shuffled during training?](#is-the-data-shuffled-during-training)
|
||||
- [How can I record the training / validation loss / accuracy at each epoch?](#how-can-i-record-the-training-validation-loss-accuracy-at-each-epoch)
|
||||
- [How can I "freeze" layers?](#how-can-i-freeze-keras-layers)
|
||||
- [How can I use stateful RNNs?](#how-can-i-use-stateful-rnns)
|
||||
- [How can I remove a layer from a Sequential model?](#how-can-i-remove-a-layer-from-a-sequential-model)
|
||||
- [How can I use pre-trained models in Keras?](#how-can-i-use-pre-trained-models-in-keras)
|
||||
- [How can I use HDF5 inputs with Keras?](#how-can-i-use-hdf5-inputs-with-keras)
|
||||
- [Where is the Keras configuration file stored?](#where-is-the-keras-configuration-file-stored)
|
||||
|
||||
---
|
||||
|
||||
### How should I cite Keras?
|
||||
|
||||
Please cite Keras in your publications if it helps your research. Here is an example BibTeX entry:
|
||||
|
||||
```
|
||||
@misc{chollet2015keras,
|
||||
title={Keras},
|
||||
author={Chollet, Fran\c{c}ois and others},
|
||||
year={2015},
|
||||
publisher={GitHub},
|
||||
howpublished={\url{https://github.com/fchollet/keras}},
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### How can I run Keras on GPU?
|
||||
|
||||
If you are running on the TensorFlow or CNTK backends, your code will automatically run on GPU if any available GPU is detected.
|
||||
|
||||
If you are running on the Theano backend, you can use one of the following methods:
|
||||
|
||||
Method 1: use Theano flags.
|
||||
```bash
|
||||
THEANO_FLAGS=device=gpu,floatX=float32 python my_keras_script.py
|
||||
```
|
||||
|
||||
The name 'gpu' might have to be changed depending on your device's identifier (e.g. `gpu0`, `gpu1`, etc).
|
||||
|
||||
Method 2: set up your `.theanorc`: [Instructions](http://deeplearning.net/software/theano/library/config.html)
|
||||
|
||||
Method 3: manually set `theano.config.device`, `theano.config.floatX` at the beginning of your code:
|
||||
```python
|
||||
import theano
|
||||
theano.config.device = 'gpu'
|
||||
theano.config.floatX = 'float32'
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### What does "sample", "batch", "epoch" mean?
|
||||
|
||||
Below are some common definitions that are necessary to know and understand to correctly utilize Keras:
|
||||
|
||||
- **Sample**: one element of a dataset.
|
||||
- *Example:* one image is a **sample** in a convolutional network
|
||||
- *Example:* one audio file is a **sample** for a speech recognition model
|
||||
- **Batch**: a set of *N* samples. The samples in a **batch** are processed independently, in parallel. If training, a batch results in only one update to the model.
|
||||
- A **batch** generally approximates the distribution of the input data better than a single input. The larger the batch, the better the approximation; however, it is also true that the batch will take longer to processes and will still result in only one update. For inference (evaluate/predict), it is recommended to pick a batch size that is as large as you can afford without going out of memory (since larger batches will usually result in faster evaluating/prediction).
|
||||
- **Epoch**: an arbitrary cutoff, generally defined as "one pass over the entire dataset", used to separate training into distinct phases, which is useful for logging and periodic evaluation.
|
||||
- When using `evaluation_data` or `evaluation_split` with the `fit` method of Keras models, evaluation will be run at the end of every **epoch**.
|
||||
- Within Keras, there is the ability to add [callbacks](https://keras.io/callbacks/) specifically designed to be run at the end of an **epoch**. Examples of these are learning rate changes and model checkpointing (saving).
|
||||
|
||||
---
|
||||
|
||||
### How can I save a Keras model?
|
||||
|
||||
*It is not recommended to use pickle or cPickle to save a Keras model.*
|
||||
|
||||
You can use `model.save(filepath)` to save a Keras model into a single HDF5 file which will contain:
|
||||
|
||||
- the architecture of the model, allowing to re-create the model
|
||||
- the weights of the model
|
||||
- the training configuration (loss, optimizer)
|
||||
- the state of the optimizer, allowing to resume training exactly where you left off.
|
||||
|
||||
You can then use `keras.models.load_model(filepath)` to reinstantiate your model.
|
||||
`load_model` will also take care of compiling the model using the saved training configuration
|
||||
(unless the model was never compiled in the first place).
|
||||
|
||||
Example:
|
||||
|
||||
```python
|
||||
from keras.models import load_model
|
||||
|
||||
model.save('my_model.h5') # creates a HDF5 file 'my_model.h5'
|
||||
del model # deletes the existing model
|
||||
|
||||
# returns a compiled model
|
||||
# identical to the previous one
|
||||
model = load_model('my_model.h5')
|
||||
```
|
||||
|
||||
If you only need to save the **architecture of a model**, and not its weights or its training configuration, you can do:
|
||||
|
||||
```python
|
||||
# save as JSON
|
||||
json_string = model.to_json()
|
||||
|
||||
# save as YAML
|
||||
yaml_string = model.to_yaml()
|
||||
```
|
||||
|
||||
The generated JSON / YAML files are human-readable and can be manually edited if needed.
|
||||
|
||||
You can then build a fresh model from this data:
|
||||
|
||||
```python
|
||||
# model reconstruction from JSON:
|
||||
from keras.models import model_from_json
|
||||
model = model_from_json(json_string)
|
||||
|
||||
# model reconstruction from YAML
|
||||
from keras.models import model_from_yaml
|
||||
model = model_from_yaml(yaml_string)
|
||||
```
|
||||
|
||||
If you need to save the **weights of a model**, you can do so in HDF5 with the code below.
|
||||
|
||||
Note that you will first need to install HDF5 and the Python library h5py, which do not come bundled with Keras.
|
||||
|
||||
```python
|
||||
model.save_weights('my_model_weights.h5')
|
||||
```
|
||||
|
||||
Assuming you have code for instantiating your model, you can then load the weights you saved into a model with the *same* architecture:
|
||||
|
||||
```python
|
||||
model.load_weights('my_model_weights.h5')
|
||||
```
|
||||
|
||||
If you need to load weights into a *different* architecture (with some layers in common), for instance for fine-tuning or transfer-learning, you can load weights by *layer name*:
|
||||
|
||||
```python
|
||||
model.load_weights('my_model_weights.h5', by_name=True)
|
||||
```
|
||||
|
||||
For example:
|
||||
|
||||
```python
|
||||
"""
|
||||
Assume original model looks like this:
|
||||
model = Sequential()
|
||||
model.add(Dense(2, input_dim=3, name='dense_1'))
|
||||
model.add(Dense(3, name='dense_2'))
|
||||
...
|
||||
model.save_weights(fname)
|
||||
"""
|
||||
|
||||
# new model
|
||||
model = Sequential()
|
||||
model.add(Dense(2, input_dim=3, name='dense_1')) # will be loaded
|
||||
model.add(Dense(10, name='new_dense')) # will not be loaded
|
||||
|
||||
# load weights from first model; will only affect the first layer, dense_1.
|
||||
model.load_weights(fname, by_name=True)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Why is the training loss much higher than the testing loss?
|
||||
|
||||
A Keras model has two modes: training and testing. Regularization mechanisms, such as Dropout and L1/L2 weight regularization, are turned off at testing time.
|
||||
|
||||
Besides, the training loss is the average of the losses over each batch of training data. Because your model is changing over time, the loss over the first batches of an epoch is generally higher than over the last batches. On the other hand, the testing loss for an epoch is computed using the model as it is at the end of the epoch, resulting in a lower loss.
|
||||
|
||||
---
|
||||
|
||||
### How can I obtain the output of an intermediate layer?
|
||||
|
||||
One simple way is to create a new `Model` that will output the layers that you are interested in:
|
||||
|
||||
```python
|
||||
from keras.models import Model
|
||||
|
||||
model = ... # create the original model
|
||||
|
||||
layer_name = 'my_layer'
|
||||
intermediate_layer_model = Model(inputs=model.input,
|
||||
outputs=model.get_layer(layer_name).output)
|
||||
intermediate_output = intermediate_layer_model.predict(data)
|
||||
```
|
||||
|
||||
Alternatively, you can build a Keras function that will return the output of a certain layer given a certain input, for example:
|
||||
|
||||
```python
|
||||
from keras import backend as K
|
||||
|
||||
# with a Sequential model
|
||||
get_3rd_layer_output = K.function([model.layers[0].input],
|
||||
[model.layers[3].output])
|
||||
layer_output = get_3rd_layer_output([x])[0]
|
||||
```
|
||||
|
||||
Similarly, you could build a Theano and TensorFlow function directly.
|
||||
|
||||
Note that if your model has a different behavior in training and testing phase (e.g. if it uses `Dropout`, `BatchNormalization`, etc.), you will need
|
||||
to pass the learning phase flag to your function:
|
||||
|
||||
```python
|
||||
get_3rd_layer_output = K.function([model.layers[0].input, K.learning_phase()],
|
||||
[model.layers[3].output])
|
||||
|
||||
# output in test mode = 0
|
||||
layer_output = get_3rd_layer_output([x, 0])[0]
|
||||
|
||||
# output in train mode = 1
|
||||
layer_output = get_3rd_layer_output([x, 1])[0]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### How can I use Keras with datasets that don't fit in memory?
|
||||
|
||||
You can do batch training using `model.train_on_batch(x, y)` and `model.test_on_batch(x, y)`. See the [models documentation](/models/sequential).
|
||||
|
||||
Alternatively, you can write a generator that yields batches of training data and use the method `model.fit_generator(data_generator, steps_per_epoch, epochs)`.
|
||||
|
||||
You can see batch training in action in our [CIFAR10 example](https://github.com/fchollet/keras/blob/master/examples/cifar10_cnn.py).
|
||||
|
||||
---
|
||||
|
||||
### How can I interrupt training when the validation loss isn't decreasing anymore?
|
||||
|
||||
You can use an `EarlyStopping` callback:
|
||||
|
||||
```python
|
||||
from keras.callbacks import EarlyStopping
|
||||
early_stopping = EarlyStopping(monitor='val_loss', patience=2)
|
||||
model.fit(x, y, validation_split=0.2, callbacks=[early_stopping])
|
||||
```
|
||||
|
||||
Find out more in the [callbacks documentation](/callbacks).
|
||||
|
||||
---
|
||||
|
||||
### How is the validation split computed?
|
||||
|
||||
If you set the `validation_split` argument in `model.fit` to e.g. 0.1, then the validation data used will be the *last 10%* of the data. If you set it to 0.25, it will be the last 25% of the data, etc. Note that the data isn't shuffled before extracting the validation split, so the validation is literally just the *last* x% of samples in the input you passed.
|
||||
|
||||
The same validation set is used for all epochs (within a same call to `fit`).
|
||||
|
||||
---
|
||||
|
||||
### Is the data shuffled during training?
|
||||
|
||||
Yes, if the `shuffle` argument in `model.fit` is set to `True` (which is the default), the training data will be randomly shuffled at each epoch.
|
||||
|
||||
Validation data is never shuffled.
|
||||
|
||||
---
|
||||
|
||||
|
||||
### How can I record the training / validation loss / accuracy at each epoch?
|
||||
|
||||
The `model.fit` method returns an `History` callback, which has a `history` attribute containing the lists of successive losses and other metrics.
|
||||
|
||||
```python
|
||||
hist = model.fit(x, y, validation_split=0.2)
|
||||
print(hist.history)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### How can I "freeze" Keras layers?
|
||||
|
||||
To "freeze" a layer means to exclude it from training, i.e. its weights will never be updated. This is useful in the context of fine-tuning a model, or using fixed embeddings for a text input.
|
||||
|
||||
You can pass a `trainable` argument (boolean) to a layer constructor to set a layer to be non-trainable:
|
||||
|
||||
```python
|
||||
frozen_layer = Dense(32, trainable=False)
|
||||
```
|
||||
|
||||
Additionally, you can set the `trainable` property of a layer to `True` or `False` after instantiation. For this to take effect, you will need to call `compile()` on your model after modifying the `trainable` property. Here's an example:
|
||||
|
||||
```python
|
||||
x = Input(shape=(32,))
|
||||
layer = Dense(32)
|
||||
layer.trainable = False
|
||||
y = layer(x)
|
||||
|
||||
frozen_model = Model(x, y)
|
||||
# in the model below, the weights of `layer` will not be updated during training
|
||||
frozen_model.compile(optimizer='rmsprop', loss='mse')
|
||||
|
||||
layer.trainable = True
|
||||
trainable_model = Model(x, y)
|
||||
# with this model the weights of the layer will be updated during training
|
||||
# (which will also affect the above model since it uses the same layer instance)
|
||||
trainable_model.compile(optimizer='rmsprop', loss='mse')
|
||||
|
||||
frozen_model.fit(data, labels) # this does NOT update the weights of `layer`
|
||||
trainable_model.fit(data, labels) # this updates the weights of `layer`
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### How can I use stateful RNNs?
|
||||
|
||||
Making a RNN stateful means that the states for the samples of each batch will be reused as initial states for the samples in the next batch.
|
||||
|
||||
When using stateful RNNs, it is therefore assumed that:
|
||||
|
||||
- all batches have the same number of samples
|
||||
- If `x1` and `x2` are successive batches of samples, then `x2[i]` is the follow-up sequence to `x1[i]`, for every `i`.
|
||||
|
||||
To use statefulness in RNNs, you need to:
|
||||
|
||||
- explicitly specify the batch size you are using, by passing a `batch_size` argument to the first layer in your model. E.g. `batch_size=32` for a 32-samples batch of sequences of 10 timesteps with 16 features per timestep.
|
||||
- set `stateful=True` in your RNN layer(s).
|
||||
- specify `shuffle=False` when calling fit().
|
||||
|
||||
To reset the states accumulated:
|
||||
|
||||
- use `model.reset_states()` to reset the states of all layers in the model
|
||||
- use `layer.reset_states()` to reset the states of a specific stateful RNN layer
|
||||
|
||||
Example:
|
||||
|
||||
```python
|
||||
|
||||
x # this is our input data, of shape (32, 21, 16)
|
||||
# we will feed it to our model in sequences of length 10
|
||||
|
||||
model = Sequential()
|
||||
model.add(LSTM(32, input_shape=(10, 16), batch_size=32, stateful=True))
|
||||
model.add(Dense(16, activation='softmax'))
|
||||
|
||||
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
|
||||
|
||||
# we train the network to predict the 11th timestep given the first 10:
|
||||
model.train_on_batch(x[:, :10, :], np.reshape(x[:, 10, :], (32, 16)))
|
||||
|
||||
# the state of the network has changed. We can feed the follow-up sequences:
|
||||
model.train_on_batch(x[:, 10:20, :], np.reshape(x[:, 20, :], (32, 16)))
|
||||
|
||||
# let's reset the states of the LSTM layer:
|
||||
model.reset_states()
|
||||
|
||||
# another way to do it in this case:
|
||||
model.layers[0].reset_states()
|
||||
```
|
||||
|
||||
Notes that the methods `predict`, `fit`, `train_on_batch`, `predict_classes`, etc. will *all* update the states of the stateful layers in a model. This allows you to do not only stateful training, but also stateful prediction.
|
||||
|
||||
---
|
||||
|
||||
### How can I remove a layer from a Sequential model?
|
||||
|
||||
You can remove the last added layer in a Sequential model by calling `.pop()`:
|
||||
|
||||
```python
|
||||
model = Sequential()
|
||||
model.add(Dense(32, activation='relu', input_dim=784))
|
||||
model.add(Dense(32, activation='relu'))
|
||||
|
||||
print(len(model.layers)) # "2"
|
||||
|
||||
model.pop()
|
||||
print(len(model.layers)) # "1"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### How can I use pre-trained models in Keras?
|
||||
|
||||
Code and pre-trained weights are available for the following image classification models:
|
||||
|
||||
- Xception
|
||||
- VGG16
|
||||
- VGG19
|
||||
- ResNet50
|
||||
- Inception v3
|
||||
|
||||
They can be imported from the module `keras.applications`:
|
||||
|
||||
```python
|
||||
from keras.applications.xception import Xception
|
||||
from keras.applications.vgg16 import VGG16
|
||||
from keras.applications.vgg19 import VGG19
|
||||
from keras.applications.resnet50 import ResNet50
|
||||
from keras.applications.inception_v3 import InceptionV3
|
||||
|
||||
model = VGG16(weights='imagenet', include_top=True)
|
||||
```
|
||||
|
||||
For a few simple usage examples, see [the documentation for the Applications module](/applications).
|
||||
|
||||
For a detailed example of how to use such a pre-trained model for feature extraction or for fine-tuning, see [this blog post](http://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html).
|
||||
|
||||
The VGG16 model is also the basis for several Keras example scripts:
|
||||
|
||||
- [Style transfer](https://github.com/fchollet/keras/blob/master/examples/neural_style_transfer.py)
|
||||
- [Feature visualization](https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py)
|
||||
- [Deep dream](https://github.com/fchollet/keras/blob/master/examples/deep_dream.py)
|
||||
|
||||
---
|
||||
|
||||
### How can I use HDF5 inputs with Keras?
|
||||
|
||||
You can use the `HDF5Matrix` class from `keras.utils.io_utils`. See [the HDF5Matrix documentation](/utils/#hdf5matrix) for details.
|
||||
|
||||
You can also directly use a HDF5 dataset:
|
||||
|
||||
```python
|
||||
import h5py
|
||||
with h5py.File('input/file.hdf5', 'r') as f:
|
||||
x_data = f['x_data']
|
||||
model.predict(x_data)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Where is the Keras configuration file stored?
|
||||
|
||||
The default directory where all Keras data is stored is:
|
||||
|
||||
```bash
|
||||
$HOME/.keras/
|
||||
```
|
||||
|
||||
Note that Windows users should replace `$HOME` with `%USERPROFILE%`.
|
||||
In case Keras cannot create the above directory (e.g. due to permission issues), `/tmp/.keras/` is used as a backup.
|
||||
|
||||
The Keras configuration file is a JSON file stored at `$HOME/.keras/keras.json`. The default configuration file looks like this:
|
||||
|
||||
```
|
||||
{
|
||||
"image_data_format": "channels_last",
|
||||
"epsilon": 1e-07,
|
||||
"floatx": "float32",
|
||||
"backend": "tensorflow"
|
||||
}
|
||||
```
|
||||
|
||||
It contains the following fields:
|
||||
|
||||
- The image data format to be used as default by image processing layers and utilities (either `channels_last` or `channels_first`).
|
||||
- The `epsilon` numerical fuzz factor to be used to prevent division by zero in some operations.
|
||||
- The default float data type.
|
||||
- The default backend. See the [backend documentation](/backend).
|
||||
|
||||
Likewise, cached dataset files, such as those downloaded with [`get_file()`](/utils/#get_file), are stored by default in `$HOME/.keras/datasets/`.
|
||||
@@ -0,0 +1,422 @@
|
||||
# Getting started with the Keras functional API
|
||||
|
||||
The Keras functional API is the way to go for defining complex models, such as multi-output models, directed acyclic graphs, or models with shared layers.
|
||||
|
||||
This guide assumes that you are already familiar with the `Sequential` model.
|
||||
|
||||
Let's start with something simple.
|
||||
|
||||
-----
|
||||
|
||||
## First example: a densely-connected network
|
||||
|
||||
The `Sequential` model is probably a better choice to implement such a network, but it helps to start with something really simple.
|
||||
|
||||
- A layer instance is callable (on a tensor), and it returns a tensor
|
||||
- Input tensor(s) and output tensor(s) can then be used to define a `Model`
|
||||
- Such a model can be trained just like Keras `Sequential` models.
|
||||
|
||||
```python
|
||||
from keras.layers import Input, Dense
|
||||
from keras.models import Model
|
||||
|
||||
# This returns a tensor
|
||||
inputs = Input(shape=(784,))
|
||||
|
||||
# a layer instance is callable on a tensor, and returns a tensor
|
||||
x = Dense(64, activation='relu')(inputs)
|
||||
x = Dense(64, activation='relu')(x)
|
||||
predictions = Dense(10, activation='softmax')(x)
|
||||
|
||||
# This creates a model that includes
|
||||
# the Input layer and three Dense layers
|
||||
model = Model(inputs=inputs, outputs=predictions)
|
||||
model.compile(optimizer='rmsprop',
|
||||
loss='categorical_crossentropy',
|
||||
metrics=['accuracy'])
|
||||
model.fit(data, labels) # starts training
|
||||
```
|
||||
|
||||
-----
|
||||
|
||||
## All models are callable, just like layers
|
||||
|
||||
With the functional API, it is easy to re-use trained models: you can treat any model as if it were a layer, by calling it on a tensor. Note that by calling a model you aren't just re-using the *architecture* of the model, you are also re-using its weights.
|
||||
|
||||
```python
|
||||
x = Input(shape=(784,))
|
||||
# This works, and returns the 10-way softmax we defined above.
|
||||
y = model(x)
|
||||
```
|
||||
|
||||
This can allow, for instance, to quickly create models that can process *sequences* of inputs. You could turn an image classification model into a video classification model, in just one line.
|
||||
|
||||
```python
|
||||
from keras.layers import TimeDistributed
|
||||
|
||||
# Input tensor for sequences of 20 timesteps,
|
||||
# each containing a 784-dimensional vector
|
||||
input_sequences = Input(shape=(20, 784))
|
||||
|
||||
# This applies our previous model to every timestep in the input sequences.
|
||||
# the output of the previous model was a 10-way softmax,
|
||||
# so the output of the layer below will be a sequence of 20 vectors of size 10.
|
||||
processed_sequences = TimeDistributed(model)(input_sequences)
|
||||
```
|
||||
|
||||
-----
|
||||
|
||||
## Multi-input and multi-output models
|
||||
|
||||
Here's a good use case for the functional API: models with multiple inputs and outputs. The functional API makes it easy to manipulate a large number of intertwined datastreams.
|
||||
|
||||
Let's consider the following model. We seek to predict how many retweets and likes a news headline will receive on Twitter. The main input to the model will be the headline itself, as a sequence of words, but to spice things up, our model will also have an auxiliary input, receiving extra data such as the time of day when the headline was posted, etc.
|
||||
The model will also be supervised via two loss functions. Using the main loss function earlier in a model is a good regularization mechanism for deep models.
|
||||
|
||||
Here's what our model looks like:
|
||||
|
||||
<img src="https://s3.amazonaws.com/keras.io/img/multi-input-multi-output-graph.png" alt="multi-input-multi-output-graph" style="width: 400px;"/>
|
||||
|
||||
Let's implement it with the functional API.
|
||||
|
||||
The main input will receive the headline, as a sequence of integers (each integer encodes a word).
|
||||
The integers will be between 1 and 10,000 (a vocabulary of 10,000 words) and the sequences will be 100 words long.
|
||||
|
||||
```python
|
||||
from keras.layers import Input, Embedding, LSTM, Dense
|
||||
from keras.models import Model
|
||||
|
||||
# Headline input: meant to receive sequences of 100 integers, between 1 and 10000.
|
||||
# Note that we can name any layer by passing it a "name" argument.
|
||||
main_input = Input(shape=(100,), dtype='int32', name='main_input')
|
||||
|
||||
# This embedding layer will encode the input sequence
|
||||
# into a sequence of dense 512-dimensional vectors.
|
||||
x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input)
|
||||
|
||||
# A LSTM will transform the vector sequence into a single vector,
|
||||
# containing information about the entire sequence
|
||||
lstm_out = LSTM(32)(x)
|
||||
```
|
||||
|
||||
Here we insert the auxiliary loss, allowing the LSTM and Embedding layer to be trained smoothly even though the main loss will be much higher in the model.
|
||||
|
||||
```python
|
||||
auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)
|
||||
```
|
||||
|
||||
At this point, we feed into the model our auxiliary input data by concatenating it with the LSTM output:
|
||||
|
||||
```python
|
||||
auxiliary_input = Input(shape=(5,), name='aux_input')
|
||||
x = keras.layers.concatenate([lstm_out, auxiliary_input])
|
||||
|
||||
# We stack a deep densely-connected network on top
|
||||
x = Dense(64, activation='relu')(x)
|
||||
x = Dense(64, activation='relu')(x)
|
||||
x = Dense(64, activation='relu')(x)
|
||||
|
||||
# And finally we add the main logistic regression layer
|
||||
main_output = Dense(1, activation='sigmoid', name='main_output')(x)
|
||||
```
|
||||
|
||||
This defines a model with two inputs and two outputs:
|
||||
|
||||
```python
|
||||
model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])
|
||||
```
|
||||
|
||||
We compile the model and assign a weight of 0.2 to the auxiliary loss.
|
||||
To specify different `loss_weights` or `loss` for each different output, you can use a list or a dictionary.
|
||||
Here we pass a single loss as the `loss` argument, so the same loss will be used on all outputs.
|
||||
|
||||
```python
|
||||
model.compile(optimizer='rmsprop', loss='binary_crossentropy',
|
||||
loss_weights=[1., 0.2])
|
||||
```
|
||||
|
||||
We can train the model by passing it lists of input arrays and target arrays:
|
||||
|
||||
```python
|
||||
model.fit([headline_data, additional_data], [labels, labels],
|
||||
epochs=50, batch_size=32)
|
||||
```
|
||||
|
||||
Since our inputs and outputs are named (we passed them a "name" argument),
|
||||
We could also have compiled the model via:
|
||||
|
||||
```python
|
||||
model.compile(optimizer='rmsprop',
|
||||
loss={'main_output': 'binary_crossentropy', 'aux_output': 'binary_crossentropy'},
|
||||
loss_weights={'main_output': 1., 'aux_output': 0.2})
|
||||
|
||||
# And trained it via:
|
||||
model.fit({'main_input': headline_data, 'aux_input': additional_data},
|
||||
{'main_output': labels, 'aux_output': labels},
|
||||
epochs=50, batch_size=32)
|
||||
```
|
||||
|
||||
-----
|
||||
|
||||
## Shared layers
|
||||
|
||||
Another good use for the functional API are models that use shared layers. Let's take a look at shared layers.
|
||||
|
||||
Let's consider a dataset of tweets. We want to build a model that can tell whether two tweets are from the same person or not (this can allow us to compare users by the similarity of their tweets, for instance).
|
||||
|
||||
One way to achieve this is to build a model that encodes two tweets into two vectors, concatenates the vectors and adds a logistic regression of top, outputting a probability that the two tweets share the same author. The model would then be trained on positive tweet pairs and negative tweet pairs.
|
||||
|
||||
Because the problem is symmetric, the mechanism that encodes the first tweet should be reused (weights and all) to encode the second tweet. Here we use a shared LSTM layer to encode the tweets.
|
||||
|
||||
Let's build this with the functional API. We will take as input for a tweet a binary matrix of shape `(140, 256)`, i.e. a sequence of 140 vectors of size 256, where each dimension in the 256-dimensional vector encodes the presence/absence of a character (out of an alphabet of 256 frequent characters).
|
||||
|
||||
```python
|
||||
import keras
|
||||
from keras.layers import Input, LSTM, Dense
|
||||
from keras.models import Model
|
||||
|
||||
tweet_a = Input(shape=(140, 256))
|
||||
tweet_b = Input(shape=(140, 256))
|
||||
```
|
||||
|
||||
To share a layer across different inputs, simply instantiate the layer once, then call it on as many inputs as you want:
|
||||
|
||||
```python
|
||||
# This layer can take as input a matrix
|
||||
# and will return a vector of size 64
|
||||
shared_lstm = LSTM(64)
|
||||
|
||||
# When we reuse the same layer instance
|
||||
# multiple times, the weights of the layer
|
||||
# are also being reused
|
||||
# (it is effectively *the same* layer)
|
||||
encoded_a = shared_lstm(tweet_a)
|
||||
encoded_b = shared_lstm(tweet_b)
|
||||
|
||||
# We can then concatenate the two vectors:
|
||||
merged_vector = keras.layers.concatenate([encoded_a, encoded_b], axis=-1)
|
||||
|
||||
# And add a logistic regression on top
|
||||
predictions = Dense(1, activation='sigmoid')(merged_vector)
|
||||
|
||||
# We define a trainable model linking the
|
||||
# tweet inputs to the predictions
|
||||
model = Model(inputs=[tweet_a, tweet_b], outputs=predictions)
|
||||
|
||||
model.compile(optimizer='rmsprop',
|
||||
loss='binary_crossentropy',
|
||||
metrics=['accuracy'])
|
||||
model.fit([data_a, data_b], labels, epochs=10)
|
||||
```
|
||||
|
||||
Let's pause to take a look at how to read the shared layer's output or output shape.
|
||||
|
||||
-----
|
||||
|
||||
## The concept of layer "node"
|
||||
|
||||
Whenever you are calling a layer on some input, you are creating a new tensor (the output of the layer), and you are adding a "node" to the layer, linking the input tensor to the output tensor. When you are calling the same layer multiple times, that layer owns multiple nodes indexed as 0, 1, 2...
|
||||
|
||||
In previous versions of Keras, you could obtain the output tensor of a layer instance via `layer.get_output()`, or its output shape via `layer.output_shape`. You still can (except `get_output()` has been replaced by the property `output`). But what if a layer is connected to multiple inputs?
|
||||
|
||||
As long as a layer is only connected to one input, there is no confusion, and `.output` will return the one output of the layer:
|
||||
|
||||
```python
|
||||
a = Input(shape=(140, 256))
|
||||
|
||||
lstm = LSTM(32)
|
||||
encoded_a = lstm(a)
|
||||
|
||||
assert lstm.output == encoded_a
|
||||
```
|
||||
|
||||
Not so if the layer has multiple inputs:
|
||||
```python
|
||||
a = Input(shape=(140, 256))
|
||||
b = Input(shape=(140, 256))
|
||||
|
||||
lstm = LSTM(32)
|
||||
encoded_a = lstm(a)
|
||||
encoded_b = lstm(b)
|
||||
|
||||
lstm.output
|
||||
```
|
||||
```
|
||||
>> AssertionError: Layer lstm_1 has multiple inbound nodes,
|
||||
hence the notion of "layer output" is ill-defined.
|
||||
Use `get_output_at(node_index)` instead.
|
||||
```
|
||||
|
||||
Okay then. The following works:
|
||||
|
||||
```python
|
||||
assert lstm.get_output_at(0) == encoded_a
|
||||
assert lstm.get_output_at(1) == encoded_b
|
||||
```
|
||||
|
||||
Simple enough, right?
|
||||
|
||||
The same is true for the properties `input_shape` and `output_shape`: as long as the layer has only one node, or as long as all nodes have the same input/output shape, then the notion of "layer output/input shape" is well defined, and that one shape will be returned by `layer.output_shape`/`layer.input_shape`. But if, for instance, you apply a same `Conv2D` layer to an input of shape `(3, 32, 32)`, and then to an input of shape `(3, 64, 64)`, the layer will have multiple input/output shapes, and you will have to fetch them by specifying the index of the node they belong to:
|
||||
|
||||
```python
|
||||
a = Input(shape=(3, 32, 32))
|
||||
b = Input(shape=(3, 64, 64))
|
||||
|
||||
conv = Conv2D(16, (3, 3), padding='same')
|
||||
conved_a = conv(a)
|
||||
|
||||
# Only one input so far, the following will work:
|
||||
assert conv.input_shape == (None, 3, 32, 32)
|
||||
|
||||
conved_b = conv(b)
|
||||
# now the `.input_shape` property wouldn't work, but this does:
|
||||
assert conv.get_input_shape_at(0) == (None, 3, 32, 32)
|
||||
assert conv.get_input_shape_at(1) == (None, 3, 64, 64)
|
||||
```
|
||||
|
||||
-----
|
||||
|
||||
## More examples
|
||||
|
||||
Code examples are still the best way to get started, so here are a few more.
|
||||
|
||||
### Inception module
|
||||
|
||||
For more information about the Inception architecture, see [Going Deeper with Convolutions](http://arxiv.org/abs/1409.4842).
|
||||
|
||||
```python
|
||||
from keras.layers import Conv2D, MaxPooling2D, Input
|
||||
|
||||
input_img = Input(shape=(3, 256, 256))
|
||||
|
||||
tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
|
||||
tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)
|
||||
|
||||
tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
|
||||
tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)
|
||||
|
||||
tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
|
||||
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)
|
||||
|
||||
output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)
|
||||
```
|
||||
|
||||
### Residual connection on a convolution layer
|
||||
|
||||
For more information about residual networks, see [Deep Residual Learning for Image Recognition](http://arxiv.org/abs/1512.03385).
|
||||
|
||||
```python
|
||||
from keras.layers import Conv2D, Input
|
||||
|
||||
# input tensor for a 3-channel 256x256 image
|
||||
x = Input(shape=(3, 256, 256))
|
||||
# 3x3 conv with 3 output channels (same as input channels)
|
||||
y = Conv2D(3, (3, 3), padding='same')(x)
|
||||
# this returns x + y.
|
||||
z = keras.layers.add([x, y])
|
||||
```
|
||||
|
||||
### Shared vision model
|
||||
|
||||
This model re-uses the same image-processing module on two inputs, to classify whether two MNIST digits are the same digit or different digits.
|
||||
|
||||
```python
|
||||
from keras.layers import Conv2D, MaxPooling2D, Input, Dense, Flatten
|
||||
from keras.models import Model
|
||||
|
||||
# First, define the vision modules
|
||||
digit_input = Input(shape=(1, 27, 27))
|
||||
x = Conv2D(64, (3, 3))(digit_input)
|
||||
x = Conv2D(64, (3, 3))(x)
|
||||
x = MaxPooling2D((2, 2))(x)
|
||||
out = Flatten()(x)
|
||||
|
||||
vision_model = Model(digit_input, out)
|
||||
|
||||
# Then define the tell-digits-apart model
|
||||
digit_a = Input(shape=(1, 27, 27))
|
||||
digit_b = Input(shape=(1, 27, 27))
|
||||
|
||||
# The vision model will be shared, weights and all
|
||||
out_a = vision_model(digit_a)
|
||||
out_b = vision_model(digit_b)
|
||||
|
||||
concatenated = keras.layers.concatenate([out_a, out_b])
|
||||
out = Dense(1, activation='sigmoid')(concatenated)
|
||||
|
||||
classification_model = Model([digit_a, digit_b], out)
|
||||
```
|
||||
|
||||
### Visual question answering model
|
||||
|
||||
This model can select the correct one-word answer when asked a natural-language question about a picture.
|
||||
|
||||
It works by encoding the question into a vector, encoding the image into a vector, concatenating the two, and training on top a logistic regression over some vocabulary of potential answers.
|
||||
|
||||
```python
|
||||
from keras.layers import Conv2D, MaxPooling2D, Flatten
|
||||
from keras.layers import Input, LSTM, Embedding, Dense
|
||||
from keras.models import Model, Sequential
|
||||
|
||||
# First, let's define a vision model using a Sequential model.
|
||||
# This model will encode an image into a vector.
|
||||
vision_model = Sequential()
|
||||
vision_model.add(Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(3, 224, 224)))
|
||||
vision_model.add(Conv2D(64, (3, 3), activation='relu'))
|
||||
vision_model.add(MaxPooling2D((2, 2)))
|
||||
vision_model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
|
||||
vision_model.add(Conv2D(128, (3, 3), activation='relu'))
|
||||
vision_model.add(MaxPooling2D((2, 2)))
|
||||
vision_model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
|
||||
vision_model.add(Conv2D(256, (3, 3), activation='relu'))
|
||||
vision_model.add(Conv2D(256, (3, 3), activation='relu'))
|
||||
vision_model.add(MaxPooling2D((2, 2)))
|
||||
vision_model.add(Flatten())
|
||||
|
||||
# Now let's get a tensor with the output of our vision model:
|
||||
image_input = Input(shape=(3, 224, 224))
|
||||
encoded_image = vision_model(image_input)
|
||||
|
||||
# Next, let's define a language model to encode the question into a vector.
|
||||
# Each question will be at most 100 word long,
|
||||
# and we will index words as integers from 1 to 9999.
|
||||
question_input = Input(shape=(100,), dtype='int32')
|
||||
embedded_question = Embedding(input_dim=10000, output_dim=256, input_length=100)(question_input)
|
||||
encoded_question = LSTM(256)(embedded_question)
|
||||
|
||||
# Let's concatenate the question vector and the image vector:
|
||||
merged = keras.layers.concatenate([encoded_question, encoded_image])
|
||||
|
||||
# And let's train a logistic regression over 1000 words on top:
|
||||
output = Dense(1000, activation='softmax')(merged)
|
||||
|
||||
# This is our final model:
|
||||
vqa_model = Model(inputs=[image_input, question_input], outputs=output)
|
||||
|
||||
# The next stage would be training this model on actual data.
|
||||
```
|
||||
|
||||
### Video question answering model
|
||||
|
||||
Now that we have trained our image QA model, we can quickly turn it into a video QA model. With appropriate training, you will be able to show it a short video (e.g. 100-frame human action) and ask a natural language question about the video (e.g. "what sport is the boy playing?" -> "football").
|
||||
|
||||
```python
|
||||
from keras.layers import TimeDistributed
|
||||
|
||||
video_input = Input(shape=(100, 3, 224, 224))
|
||||
# This is our video encoded via the previously trained vision_model (weights are reused)
|
||||
encoded_frame_sequence = TimeDistributed(vision_model)(video_input) # the output will be a sequence of vectors
|
||||
encoded_video = LSTM(256)(encoded_frame_sequence) # the output will be a vector
|
||||
|
||||
# This is a model-level representation of the question encoder, reusing the same weights as before:
|
||||
question_encoder = Model(inputs=question_input, outputs=encoded_question)
|
||||
|
||||
# Let's use it to encode the question:
|
||||
video_question_input = Input(shape=(100,), dtype='int32')
|
||||
encoded_video_question = question_encoder(video_question_input)
|
||||
|
||||
# And this is our video question answering model:
|
||||
merged = keras.layers.concatenate([encoded_video, encoded_video_question])
|
||||
output = Dense(1000, activation='softmax')(merged)
|
||||
video_qa_model = Model(inputs=[video_input, video_question_input], outputs=output)
|
||||
```
|
||||
@@ -0,0 +1,394 @@
|
||||
# Getting started with the Keras Sequential model
|
||||
|
||||
The `Sequential` model is a linear stack of layers.
|
||||
|
||||
You can create a `Sequential` model by passing a list of layer instances to the constructor:
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Activation
|
||||
|
||||
model = Sequential([
|
||||
Dense(32, input_shape=(784,)),
|
||||
Activation('relu'),
|
||||
Dense(10),
|
||||
Activation('softmax'),
|
||||
])
|
||||
```
|
||||
|
||||
You can also simply add layers via the `.add()` method:
|
||||
|
||||
```python
|
||||
model = Sequential()
|
||||
model.add(Dense(32, input_dim=784))
|
||||
model.add(Activation('relu'))
|
||||
```
|
||||
|
||||
----
|
||||
|
||||
## Specifying the input shape
|
||||
|
||||
The model needs to know what input shape it should expect. For this reason, the first layer in a `Sequential` model (and only the first, because following layers can do automatic shape inference) needs to receive information about its input shape. There are several possible ways to do this:
|
||||
|
||||
- Pass an `input_shape` argument to the first layer. This is a shape tuple (a tuple of integers or `None` entries, where `None` indicates that any positive integer may be expected). In `input_shape`, the batch dimension is not included.
|
||||
- Some 2D layers, such as `Dense`, support the specification of their input shape via the argument `input_dim`, and some 3D temporal layers support the arguments `input_dim` and `input_length`.
|
||||
- If you ever need to specify a fixed batch size for your inputs (this is useful for stateful recurrent networks), you can pass a `batch_size` argument to a layer. If you pass both `batch_size=32` and `input_shape=(6, 8)` to a layer, it will then expect every batch of inputs to have the batch shape `(32, 6, 8)`.
|
||||
|
||||
As such, the following snippets are strictly equivalent:
|
||||
```python
|
||||
model = Sequential()
|
||||
model.add(Dense(32, input_shape=(784,)))
|
||||
```
|
||||
```python
|
||||
model = Sequential()
|
||||
model.add(Dense(32, input_dim=784))
|
||||
```
|
||||
|
||||
----
|
||||
|
||||
## Compilation
|
||||
|
||||
Before training a model, you need to configure the learning process, which is done via the `compile` method. It receives three arguments:
|
||||
|
||||
- An optimizer. This could be the string identifier of an existing optimizer (such as `rmsprop` or `adagrad`), or an instance of the `Optimizer` class. See: [optimizers](/optimizers).
|
||||
- A loss function. This is the objective that the model will try to minimize. It can be the string identifier of an existing loss function (such as `categorical_crossentropy` or `mse`), or it can be an objective function. See: [losses](/losses).
|
||||
- A list of metrics. For any classification problem you will want to set this to `metrics=['accuracy']`. A metric could be the string identifier of an existing metric or a custom metric function.
|
||||
|
||||
```python
|
||||
# For a multi-class classification problem
|
||||
model.compile(optimizer='rmsprop',
|
||||
loss='categorical_crossentropy',
|
||||
metrics=['accuracy'])
|
||||
|
||||
# For a binary classification problem
|
||||
model.compile(optimizer='rmsprop',
|
||||
loss='binary_crossentropy',
|
||||
metrics=['accuracy'])
|
||||
|
||||
# For a mean squared error regression problem
|
||||
model.compile(optimizer='rmsprop',
|
||||
loss='mse')
|
||||
|
||||
# For custom metrics
|
||||
import keras.backend as K
|
||||
|
||||
def mean_pred(y_true, y_pred):
|
||||
return K.mean(y_pred)
|
||||
|
||||
model.compile(optimizer='rmsprop',
|
||||
loss='binary_crossentropy',
|
||||
metrics=['accuracy', mean_pred])
|
||||
```
|
||||
|
||||
----
|
||||
|
||||
## Training
|
||||
|
||||
Keras models are trained on Numpy arrays of input data and labels. For training a model, you will typically use the `fit` function. [Read its documentation here](/models/sequential).
|
||||
|
||||
```python
|
||||
# For a single-input model with 2 classes (binary classification):
|
||||
|
||||
model = Sequential()
|
||||
model.add(Dense(32, activation='relu', input_dim=100))
|
||||
model.add(Dense(1, activation='sigmoid'))
|
||||
model.compile(optimizer='rmsprop',
|
||||
loss='binary_crossentropy',
|
||||
metrics=['accuracy'])
|
||||
|
||||
# Generate dummy data
|
||||
import numpy as np
|
||||
data = np.random.random((1000, 100))
|
||||
labels = np.random.randint(2, size=(1000, 1))
|
||||
|
||||
# Train the model, iterating on the data in batches of 32 samples
|
||||
model.fit(data, labels, epochs=10, batch_size=32)
|
||||
```
|
||||
|
||||
```python
|
||||
# For a single-input model with 10 classes (categorical classification):
|
||||
|
||||
model = Sequential()
|
||||
model.add(Dense(32, activation='relu', input_dim=100))
|
||||
model.add(Dense(10, activation='softmax'))
|
||||
model.compile(optimizer='rmsprop',
|
||||
loss='categorical_crossentropy',
|
||||
metrics=['accuracy'])
|
||||
|
||||
# Generate dummy data
|
||||
import numpy as np
|
||||
data = np.random.random((1000, 100))
|
||||
labels = np.random.randint(10, size=(1000, 1))
|
||||
|
||||
# Convert labels to categorical one-hot encoding
|
||||
one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)
|
||||
|
||||
# Train the model, iterating on the data in batches of 32 samples
|
||||
model.fit(data, one_hot_labels, epochs=10, batch_size=32)
|
||||
```
|
||||
|
||||
----
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
Here are a few examples to get you started!
|
||||
|
||||
In the [examples folder](https://github.com/fchollet/keras/tree/master/examples), you will also find example models for real datasets:
|
||||
|
||||
- CIFAR10 small images classification: Convolutional Neural Network (CNN) with realtime data augmentation
|
||||
- IMDB movie review sentiment classification: LSTM over sequences of words
|
||||
- Reuters newswires topic classification: Multilayer Perceptron (MLP)
|
||||
- MNIST handwritten digits classification: MLP & CNN
|
||||
- Character-level text generation with LSTM
|
||||
|
||||
...and more.
|
||||
|
||||
|
||||
### Multilayer Perceptron (MLP) for multi-class softmax classification:
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Dropout, Activation
|
||||
from keras.optimizers import SGD
|
||||
|
||||
# Generate dummy data
|
||||
import numpy as np
|
||||
x_train = np.random.random((1000, 20))
|
||||
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
|
||||
x_test = np.random.random((100, 20))
|
||||
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
|
||||
|
||||
model = Sequential()
|
||||
# Dense(64) is a fully-connected layer with 64 hidden units.
|
||||
# in the first layer, you must specify the expected input data shape:
|
||||
# here, 20-dimensional vectors.
|
||||
model.add(Dense(64, activation='relu', input_dim=20))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(64, activation='relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(10, activation='softmax'))
|
||||
|
||||
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer=sgd,
|
||||
metrics=['accuracy'])
|
||||
|
||||
model.fit(x_train, y_train,
|
||||
epochs=20,
|
||||
batch_size=128)
|
||||
score = model.evaluate(x_test, y_test, batch_size=128)
|
||||
```
|
||||
|
||||
|
||||
### MLP for binary classification:
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Dropout
|
||||
|
||||
# Generate dummy data
|
||||
x_train = np.random.random((1000, 20))
|
||||
y_train = np.random.randint(2, size=(1000, 1))
|
||||
x_test = np.random.random((100, 20))
|
||||
y_test = np.random.randint(2, size=(100, 1))
|
||||
|
||||
model = Sequential()
|
||||
model.add(Dense(64, input_dim=20, activation='relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(64, activation='relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(1, activation='sigmoid'))
|
||||
|
||||
model.compile(loss='binary_crossentropy',
|
||||
optimizer='rmsprop',
|
||||
metrics=['accuracy'])
|
||||
|
||||
model.fit(x_train, y_train,
|
||||
epochs=20,
|
||||
batch_size=128)
|
||||
score = model.evaluate(x_test, y_test, batch_size=128)
|
||||
```
|
||||
|
||||
|
||||
### VGG-like convnet:
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import keras
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Dropout, Flatten
|
||||
from keras.layers import Conv2D, MaxPooling2D
|
||||
from keras.optimizers import SGD
|
||||
|
||||
# Generate dummy data
|
||||
x_train = np.random.random((100, 100, 100, 3))
|
||||
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
|
||||
x_test = np.random.random((20, 100, 100, 3))
|
||||
y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)
|
||||
|
||||
model = Sequential()
|
||||
# input: 100x100 images with 3 channels -> (100, 100, 3) tensors.
|
||||
# this applies 32 convolution filters of size 3x3 each.
|
||||
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))
|
||||
model.add(Conv2D(32, (3, 3), activation='relu'))
|
||||
model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
model.add(Dropout(0.25))
|
||||
|
||||
model.add(Conv2D(64, (3, 3), activation='relu'))
|
||||
model.add(Conv2D(64, (3, 3), activation='relu'))
|
||||
model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
model.add(Dropout(0.25))
|
||||
|
||||
model.add(Flatten())
|
||||
model.add(Dense(256, activation='relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(10, activation='softmax'))
|
||||
|
||||
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
|
||||
model.compile(loss='categorical_crossentropy', optimizer=sgd)
|
||||
|
||||
model.fit(x_train, y_train, batch_size=32, epochs=10)
|
||||
score = model.evaluate(x_test, y_test, batch_size=32)
|
||||
```
|
||||
|
||||
|
||||
### Sequence classification with LSTM:
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Dropout
|
||||
from keras.layers import Embedding
|
||||
from keras.layers import LSTM
|
||||
|
||||
model = Sequential()
|
||||
model.add(Embedding(max_features, output_dim=256))
|
||||
model.add(LSTM(128))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(1, activation='sigmoid'))
|
||||
|
||||
model.compile(loss='binary_crossentropy',
|
||||
optimizer='rmsprop',
|
||||
metrics=['accuracy'])
|
||||
|
||||
model.fit(x_train, y_train, batch_size=16, epochs=10)
|
||||
score = model.evaluate(x_test, y_test, batch_size=16)
|
||||
```
|
||||
|
||||
### Sequence classification with 1D convolutions:
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Dropout
|
||||
from keras.layers import Embedding
|
||||
from keras.layers import Conv1D, GlobalAveragePooling1D, MaxPooling1D
|
||||
|
||||
model = Sequential()
|
||||
model.add(Conv1D(64, 3, activation='relu', input_shape=(seq_length, 100)))
|
||||
model.add(Conv1D(64, 3, activation='relu'))
|
||||
model.add(MaxPooling1D(3))
|
||||
model.add(Conv1D(128, 3, activation='relu'))
|
||||
model.add(Conv1D(128, 3, activation='relu'))
|
||||
model.add(GlobalAveragePooling1D())
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(1, activation='sigmoid'))
|
||||
|
||||
model.compile(loss='binary_crossentropy',
|
||||
optimizer='rmsprop',
|
||||
metrics=['accuracy'])
|
||||
|
||||
model.fit(x_train, y_train, batch_size=16, epochs=10)
|
||||
score = model.evaluate(x_test, y_test, batch_size=16)
|
||||
```
|
||||
|
||||
### Stacked LSTM for sequence classification
|
||||
|
||||
In this model, we stack 3 LSTM layers on top of each other,
|
||||
making the model capable of learning higher-level temporal representations.
|
||||
|
||||
The first two LSTMs return their full output sequences, but the last one only returns
|
||||
the last step in its output sequence, thus dropping the temporal dimension
|
||||
(i.e. converting the input sequence into a single vector).
|
||||
|
||||
<img src="https://keras.io/img/regular_stacked_lstm.png" alt="stacked LSTM" style="width: 300px;"/>
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import LSTM, Dense
|
||||
import numpy as np
|
||||
|
||||
data_dim = 16
|
||||
timesteps = 8
|
||||
num_classes = 10
|
||||
|
||||
# expected input data shape: (batch_size, timesteps, data_dim)
|
||||
model = Sequential()
|
||||
model.add(LSTM(32, return_sequences=True,
|
||||
input_shape=(timesteps, data_dim))) # returns a sequence of vectors of dimension 32
|
||||
model.add(LSTM(32, return_sequences=True)) # returns a sequence of vectors of dimension 32
|
||||
model.add(LSTM(32)) # return a single vector of dimension 32
|
||||
model.add(Dense(10, activation='softmax'))
|
||||
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer='rmsprop',
|
||||
metrics=['accuracy'])
|
||||
|
||||
# Generate dummy training data
|
||||
x_train = np.random.random((1000, timesteps, data_dim))
|
||||
y_train = np.random.random((1000, num_classes))
|
||||
|
||||
# Generate dummy validation data
|
||||
x_val = np.random.random((100, timesteps, data_dim))
|
||||
y_val = np.random.random((100, num_classes))
|
||||
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=64, epochs=5,
|
||||
validation_data=(x_val, y_val))
|
||||
```
|
||||
|
||||
|
||||
### Same stacked LSTM model, rendered "stateful"
|
||||
|
||||
A stateful recurrent model is one for which the internal states (memories) obtained after processing a batch
|
||||
of samples are reused as initial states for the samples of the next batch. This allows to process longer sequences
|
||||
while keeping computational complexity manageable.
|
||||
|
||||
[You can read more about stateful RNNs in the FAQ.](/getting-started/faq/#how-can-i-use-stateful-rnns)
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers import LSTM, Dense
|
||||
import numpy as np
|
||||
|
||||
data_dim = 16
|
||||
timesteps = 8
|
||||
num_classes = 10
|
||||
batch_size = 32
|
||||
|
||||
# Expected input batch shape: (batch_size, timesteps, data_dim)
|
||||
# Note that we have to provide the full batch_input_shape since the network is stateful.
|
||||
# the sample of index i in batch k is the follow-up for the sample i in batch k-1.
|
||||
model = Sequential()
|
||||
model.add(LSTM(32, return_sequences=True, stateful=True,
|
||||
batch_input_shape=(batch_size, timesteps, data_dim)))
|
||||
model.add(LSTM(32, return_sequences=True, stateful=True))
|
||||
model.add(LSTM(32, stateful=True))
|
||||
model.add(Dense(10, activation='softmax'))
|
||||
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer='rmsprop',
|
||||
metrics=['accuracy'])
|
||||
|
||||
# Generate dummy training data
|
||||
x_train = np.random.random((batch_size * 10, timesteps, data_dim))
|
||||
y_train = np.random.random((batch_size * 10, num_classes))
|
||||
|
||||
# Generate dummy validation data
|
||||
x_val = np.random.random((batch_size * 3, timesteps, data_dim))
|
||||
y_val = np.random.random((batch_size * 3, num_classes))
|
||||
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size, epochs=5, shuffle=False,
|
||||
validation_data=(x_val, y_val))
|
||||
```
|
||||
externo
+2
-162
@@ -1,163 +1,3 @@
|
||||
# Keras: Deep Learning library for Theano and TensorFlow
|
||||
# Keras: The Python Deep Learning library
|
||||
|
||||
## You have just found Keras.
|
||||
|
||||
Keras is a minimalist, highly modular neural networks library, written in Python and capable of running on top of either [TensorFlow](https://github.com/tensorflow/tensorflow) or [Theano](https://github.com/Theano/Theano). It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research.
|
||||
|
||||
Use Keras if you need a deep learning library that:
|
||||
|
||||
- allows for easy and fast prototyping (through total modularity, minimalism, and extensibility).
|
||||
- supports both convolutional networks and recurrent networks, as well as combinations of the two.
|
||||
- supports arbitrary connectivity schemes (including multi-input and multi-output training).
|
||||
- runs seamlessly on CPU and GPU.
|
||||
|
||||
Read the documentation at [Keras.io](http://keras.io).
|
||||
|
||||
Keras is compatible with: __Python 2.7-3.5__.
|
||||
|
||||
|
||||
------------------
|
||||
|
||||
|
||||
## Guiding principles
|
||||
|
||||
- __Modularity.__ A model is understood as a sequence or a graph of standalone, fully-configurable modules that can be plugged together with as little restrictions as possible. In particular, neural layers, cost functions, optimizers, initialization schemes, activation functions, regularization schemes are all standalone modules that you can combine to create new models.
|
||||
|
||||
- __Minimalism.__ Each module should be kept short and simple. Every piece of code should be transparent upon first reading. No black magic: it hurts iteration speed and ability to innovate.
|
||||
|
||||
- __Easy extensibility.__ New modules are dead simple to add (as new classes and functions), and existing modules provide ample examples. To be able to easily create new modules allows for total expressiveness, making Keras suitable for advanced research.
|
||||
|
||||
- __Work with Python__. No separate models configuration files in a declarative format. Models are described in Python code, which is compact, easier to debug, and allows for ease of extensibility.
|
||||
|
||||
|
||||
------------------
|
||||
|
||||
|
||||
## Getting started: 30 seconds to Keras
|
||||
|
||||
The core datastructure of Keras is a __model__, a way to organize layers. There are two types of models: [`Sequential`](http://keras.io/models/#sequential) and [`Graph`](http://keras.io/models/#graph).
|
||||
|
||||
Here's the `Sequential` model (a linear pile of layers):
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
|
||||
model = Sequential()
|
||||
```
|
||||
|
||||
Stacking layers is as easy as `.add()`:
|
||||
|
||||
```python
|
||||
from keras.layers.core import Dense, Activation
|
||||
|
||||
model.add(Dense(output_dim=64, input_dim=100, init="glorot_uniform"))
|
||||
model.add(Activation("relu"))
|
||||
model.add(Dense(output_dim=10, init="glorot_uniform"))
|
||||
model.add(Activation("softmax"))
|
||||
```
|
||||
|
||||
Once your model looks good, configure its learning process with `.compile()`:
|
||||
```python
|
||||
model.compile(loss='categorical_crossentropy', optimizer='sgd')
|
||||
```
|
||||
|
||||
If you need to, you can further configure your optimizer. A core principle of Keras is to make things reasonably simple, while allowing the user to be fully in control when they need to (the ultimate control being the easy extensibility of the source code).
|
||||
```python
|
||||
from keras.optimizers import SGD
|
||||
model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.01, momentum=0.9, nesterov=True))
|
||||
```
|
||||
|
||||
You can now iterate on your training data in batches:
|
||||
```python
|
||||
model.fit(X_train, Y_train, nb_epoch=5, batch_size=32)
|
||||
```
|
||||
|
||||
Alternatively, you can feed batches to your model manually:
|
||||
```python
|
||||
model.train_on_batch(X_batch, Y_batch)
|
||||
```
|
||||
|
||||
Evaluate your performance in one line:
|
||||
```python
|
||||
objective_score = model.evaluate(X_test, Y_test, batch_size=32)
|
||||
```
|
||||
|
||||
Or generate predictions on new data:
|
||||
```python
|
||||
classes = model.predict_classes(X_test, batch_size=32)
|
||||
proba = model.predict_proba(X_test, batch_size=32)
|
||||
```
|
||||
|
||||
Building a network of LSTMs, a deep CNN, a Neural Turing Machine, a word2vec embedder or any other model is just as fast. The ideas behind deep learning are simple, so why should their implementation be painful?
|
||||
|
||||
Have a look at these [starter examples](http://keras.io/examples/).
|
||||
|
||||
In the [examples folder](https://github.com/fchollet/keras/tree/master/examples) of the repo, you will find more advanced models: question-answering with memory networks, text generation with stacked LSTMs, neural turing machines, etc.
|
||||
|
||||
|
||||
------------------
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
Keras uses the following dependencies:
|
||||
|
||||
- numpy, scipy
|
||||
- pyyaml
|
||||
- HDF5 and h5py (optional, required if you use model saving/loading functions)
|
||||
- Optional but recommended if you use CNNs: cuDNN.
|
||||
|
||||
*When using the Theano backend:*
|
||||
|
||||
- Theano
|
||||
- [See installation instructions](http://deeplearning.net/software/theano/install.html#install).
|
||||
|
||||
**Note**: You should use the latest version of Theano, not the PyPI version. Install it with:
|
||||
```
|
||||
sudo pip install git+git://github.com/Theano/Theano.git
|
||||
```
|
||||
|
||||
*When using the TensorFlow backend:*
|
||||
|
||||
- TensorFlow
|
||||
- [See installation instructions](https://github.com/tensorflow/tensorflow#download-and-setup).
|
||||
|
||||
To install Keras, `cd` to the Keras folder and run the install command:
|
||||
```
|
||||
sudo python setup.py install
|
||||
```
|
||||
|
||||
You can also install Keras from PyPI:
|
||||
```
|
||||
sudo pip install keras
|
||||
```
|
||||
|
||||
------------------
|
||||
|
||||
|
||||
## Switching from Theano to TensorFlow
|
||||
|
||||
By default, Keras will use Theano as its tensor manipulation library. [Follow these instructions](http://keras.io/backend/) to configure the Keras backend.
|
||||
|
||||
------------------
|
||||
|
||||
|
||||
## Support
|
||||
|
||||
You can ask questions and join the development discussion on the [Keras Google group](https://groups.google.com/forum/#!forum/keras-users).
|
||||
|
||||
You can also post bug reports and feature requests in [Github issues](https://github.com/fchollet/keras/issues). Make sure to read [our guidelines](https://github.com/fchollet/keras/blob/master/CONTRIBUTING.md) first.
|
||||
|
||||
|
||||
------------------
|
||||
|
||||
|
||||
## Why this name, Keras?
|
||||
|
||||
Keras (κέρας) means _horn_ in Greek. It is a reference to a literary image from ancient Greek and Latin literature, first found in the _Odyssey_, where dream spirits (_Oneiroi_, singular _Oneiros_) are divided between those who deceive men with false visions, who arrive to Earth through a gate of ivory, and those who announce a future that will come to pass, who arrive through a gate of horn. It's a play on the words κέρας (horn) / κραίνω (fulfill), and ἐλέφας (ivory) / ἐλεφαίρομαι (deceive).
|
||||
|
||||
Keras was initially developed as part of the research effort of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System).
|
||||
|
||||
>_"Oneiroi are beyond our unravelling --who can be sure what tale they tell? Not all that men look for comes to pass. Two gates there are that give passage to fleeting Oneiroi; one is made of horn, one of ivory. The Oneiroi that pass through sawn ivory are deceitful, bearing a message that will not be fulfilled; those that come out through polished horn have truth behind them, to be accomplished for men who see them."_ Homer, Odyssey 19. 562 ff (Shewring translation).
|
||||
|
||||
------------------
|
||||
{{autogenerated}}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
|
||||
## Usage of initializations
|
||||
|
||||
Initializations define the probability distribution used to set the initial random weights of Keras layers.
|
||||
|
||||
The keyword arguments used for passing initializations to layers will depend on the layer. Usually it is simply `init`:
|
||||
|
||||
```python
|
||||
model.add(Dense(64, init='uniform'))
|
||||
```
|
||||
|
||||
## Available initializations
|
||||
|
||||
- __uniform__
|
||||
- __lecun_uniform__: Uniform initialization scaled by the square root of the number of inputs (LeCun 98).
|
||||
- __normal__
|
||||
- __identity__: Use with square 2D layers (`shape[0] == shape[1]`).
|
||||
- __orthogonal__: Use with square 2D layers (`shape[0] == shape[1]`).
|
||||
- __zero__
|
||||
- __glorot_normal__: Gaussian initialization scaled by fan_in + fan_out (Glorot 2010)
|
||||
- __glorot_uniform__
|
||||
- __he_normal__: Gaussian initialization scaled by fan_in (He et al., 2014)
|
||||
- __he_uniform__
|
||||
externo
+43
@@ -0,0 +1,43 @@
|
||||
## Usage of initializers
|
||||
|
||||
Initializations define the way to set the initial random weights of Keras layers.
|
||||
|
||||
The keyword arguments used for passing initializers to layers will depend on the layer. Usually it is simply `kernel_initializer` and `bias_initializer`:
|
||||
|
||||
```python
|
||||
model.add(Dense(64,
|
||||
kernel_initializer='random_uniform',
|
||||
bias_initializer='zeros'))
|
||||
```
|
||||
|
||||
## Available initializers
|
||||
|
||||
The following built-in initializers are available as part of the `keras.initializers` module:
|
||||
|
||||
{{autogenerated}}
|
||||
|
||||
|
||||
An initializer may be passed as a string (must match one of the available initializers above), or as a callable:
|
||||
|
||||
```python
|
||||
from keras import initializers
|
||||
|
||||
model.add(Dense(64, kernel_initializer=initializers.random_normal(stddev=0.01)))
|
||||
|
||||
# also works; will use the default parameters.
|
||||
model.add(Dense(64, kernel_initializer='random_normal'))
|
||||
```
|
||||
|
||||
|
||||
## Using custom initializers
|
||||
|
||||
If passing a custom callable, then it must take the argument `shape` (shape of the variable to initialize) and `dtype` (dtype of generated values):
|
||||
|
||||
```python
|
||||
from keras import backend as K
|
||||
|
||||
def my_init(shape, dtype=None):
|
||||
return K.random_normal(shape, dtype=dtype)
|
||||
|
||||
model.add(Dense(64, kernel_initializer=my_init))
|
||||
```
|
||||
@@ -0,0 +1,37 @@
|
||||
# About Keras layers
|
||||
|
||||
All Keras layers have a number of methods in common:
|
||||
|
||||
- `layer.get_weights()`: returns the weights of the layer as a list of Numpy arrays.
|
||||
- `layer.set_weights(weights)`: sets the weights of the layer from a list of Numpy arrays (with the same shapes as the output of `get_weights`).
|
||||
- `layer.get_config()`: returns a dictionary containing the configuration of the layer. The layer can be reinstantiated from its config via:
|
||||
|
||||
```python
|
||||
layer = Dense(32)
|
||||
config = layer.get_config()
|
||||
reconstructed_layer = Dense.from_config(config)
|
||||
```
|
||||
|
||||
Or:
|
||||
|
||||
```python
|
||||
from keras import layers
|
||||
|
||||
config = layer.get_config()
|
||||
layer = layers.deserialize({'class_name': layer.__class__.__name__,
|
||||
'config': config})
|
||||
```
|
||||
|
||||
If a layer has a single node (i.e. if it isn't a shared layer), you can get its input tensor, output tensor, input shape and output shape via:
|
||||
|
||||
- `layer.input`
|
||||
- `layer.output`
|
||||
- `layer.input_shape`
|
||||
- `layer.output_shape`
|
||||
|
||||
If the layer has multiple nodes (see: [the concept of layer node and shared layers](/getting-started/functional-api-guide/#the-concept-of-layer-node)), you can use the following methods:
|
||||
|
||||
- `layer.get_input_at(node_index)`
|
||||
- `layer.get_output_at(node_index)`
|
||||
- `layer.get_input_shape_at(node_index)`
|
||||
- `layer.get_output_shape_at(node_index)`
|
||||
@@ -0,0 +1,37 @@
|
||||
# Writing your own Keras layers
|
||||
|
||||
For simple, stateless custom operations, you are probably better off using `layers.core.Lambda` layers. But for any custom operation that has trainable weights, you should implement your own layer.
|
||||
|
||||
Here is the skeleton of a Keras layer, **as of Keras 2.0** (if you have an older version, please upgrade). There are only three methods you need to implement:
|
||||
|
||||
- `build(input_shape)`: this is where you will define your weights. This method must set `self.built = True`, which can be done by calling `super([Layer], self).build()`.
|
||||
- `call(x)`: this is where the layer's logic lives. Unless you want your layer to support masking, you only have to care about the first argument passed to `call`: the input tensor.
|
||||
- `compute_output_shape(input_shape)`: in case your layer modifies the shape of its input, you should specify here the shape transformation logic. This allows Keras to do automatic shape inference.
|
||||
|
||||
```python
|
||||
from keras import backend as K
|
||||
from keras.engine.topology import Layer
|
||||
import numpy as np
|
||||
|
||||
class MyLayer(Layer):
|
||||
|
||||
def __init__(self, output_dim, **kwargs):
|
||||
self.output_dim = output_dim
|
||||
super(MyLayer, self).__init__(**kwargs)
|
||||
|
||||
def build(self, input_shape):
|
||||
# Create a trainable weight variable for this layer.
|
||||
self.kernel = self.add_weight(name='kernel',
|
||||
shape=(input_shape[1], self.output_dim),
|
||||
initializer='uniform',
|
||||
trainable=True)
|
||||
super(MyLayer, self).build(input_shape) # Be sure to call this somewhere!
|
||||
|
||||
def call(self, x):
|
||||
return K.dot(x, self.kernel)
|
||||
|
||||
def compute_output_shape(self, input_shape):
|
||||
return (input_shape[0], self.output_dim)
|
||||
```
|
||||
|
||||
The existing Keras layers provide examples of how to implement almost anything. Never hesitate to read the source code!
|
||||
externo
+37
@@ -0,0 +1,37 @@
|
||||
|
||||
## Usage of loss functions
|
||||
|
||||
A loss function (or objective function, or optimization score function) is one of the two parameters required to compile a model:
|
||||
|
||||
```python
|
||||
model.compile(loss='mean_squared_error', optimizer='sgd')
|
||||
```
|
||||
|
||||
```python
|
||||
from keras import losses
|
||||
|
||||
model.compile(loss=losses.mean_squared_error, optimizer='sgd')
|
||||
```
|
||||
|
||||
You can either pass the name of an existing loss function, or pass a TensorFlow/Theano symbolic function that returns a scalar for each data-point and takes the following two arguments:
|
||||
|
||||
- __y_true__: True labels. TensorFlow/Theano tensor.
|
||||
- __y_pred__: Predictions. TensorFlow/Theano tensor of the same shape as y_true.
|
||||
|
||||
The actual optimized objective is the mean of the output array across all datapoints.
|
||||
|
||||
For a few examples of such functions, check out the [losses source](https://github.com/fchollet/keras/blob/master/keras/losses.py).
|
||||
|
||||
## Available loss functions
|
||||
|
||||
{{autogenerated}}
|
||||
|
||||
----
|
||||
|
||||
**Note**: when using the `categorical_crossentropy` loss, your targets should be in categorical format (e.g. if you have 10 classes, the target for each sample should be a 10-dimensional vector that is all-zeros expect for a 1 at the index corresponding to the class of the sample). In order to convert *integer targets* into *categorical targets*, you can use the Keras utility `to_categorical`:
|
||||
|
||||
```python
|
||||
from keras.utils.np_utils import to_categorical
|
||||
|
||||
categorical_labels = to_categorical(int_labels, num_classes=None)
|
||||
```
|
||||
externo
+56
@@ -0,0 +1,56 @@
|
||||
|
||||
## Usage of metrics
|
||||
|
||||
A metric is a function that is used to judge the performance of your model. Metric functions are to be supplied in the `metrics` parameter when a model is compiled.
|
||||
|
||||
```python
|
||||
model.compile(loss='mean_squared_error',
|
||||
optimizer='sgd',
|
||||
metrics=['mae', 'acc'])
|
||||
```
|
||||
|
||||
```python
|
||||
from keras import metrics
|
||||
|
||||
model.compile(loss='mean_squared_error',
|
||||
optimizer='sgd',
|
||||
metrics=[metrics.mae, metrics.categorical_accuracy])
|
||||
```
|
||||
|
||||
A metric function is similar to an [loss function](/losses), except that the results from evaluating a metric are not used when training the model.
|
||||
|
||||
You can either pass the name of an existing metric, or pass a Theano/TensorFlow symbolic function (see [Custom metrics](#custom-metrics)).
|
||||
|
||||
#### Arguments
|
||||
- __y_true__: True labels. Theano/TensorFlow tensor.
|
||||
- __y_pred__: Predictions. Theano/TensorFlow tensor of the same shape as y_true.
|
||||
|
||||
#### Returns
|
||||
Single tensor value representing the mean of the output array across all
|
||||
datapoints.
|
||||
|
||||
----
|
||||
|
||||
## Available metrics
|
||||
|
||||
|
||||
{{autogenerated}}
|
||||
|
||||
----
|
||||
|
||||
## Custom metrics
|
||||
|
||||
Custom metrics can be passed at the compilation step. The
|
||||
function would need to take `(y_true, y_pred)` as arguments and return
|
||||
a single tensor value.
|
||||
|
||||
```python
|
||||
import keras.backend as K
|
||||
|
||||
def mean_pred(y_true, y_pred):
|
||||
return K.mean(y_pred)
|
||||
|
||||
model.compile(optimizer='rmsprop',
|
||||
loss='binary_crossentropy',
|
||||
metrics=['accuracy', mean_pred])
|
||||
```
|
||||
externo
-114
@@ -1,114 +0,0 @@
|
||||
Keras has two models: __Sequential__, a linear stack of layers, and __Graph__, a directed acyclic graph of layers.
|
||||
|
||||
# Using the Sequential model
|
||||
|
||||
```python
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Dropout, Activation
|
||||
from keras.optimizers import SGD
|
||||
|
||||
model = Sequential()
|
||||
model.add(Dense(2, init='uniform', input_dim=64))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
model.compile(optimizer='sgd', loss='mse')
|
||||
|
||||
'''
|
||||
Train the model for 3 epochs, in batches of 16 samples,
|
||||
on data stored in the Numpy array X_train,
|
||||
and labels stored in the Numpy array y_train:
|
||||
'''
|
||||
model.fit(X_train, y_train, nb_epoch=3, batch_size=16, verbose=1)
|
||||
'''
|
||||
What you will see with mode verbose=1:
|
||||
Train on 37800 samples, validate on 4200 samples
|
||||
Epoch 0
|
||||
37800/37800 [==============================] - 7s - loss: 0.0385
|
||||
Epoch 1
|
||||
37800/37800 [==============================] - 8s - loss: 0.0140
|
||||
Epoch 2
|
||||
10960/37800 [=======>......................] - ETA: 4s - loss: 0.0109
|
||||
'''
|
||||
|
||||
model.fit(X_train, y_train, nb_epoch=3, batch_size=16, verbose=2)
|
||||
'''
|
||||
What you will see with mode verbose=2:
|
||||
Train on 37800 samples, validate on 4200 samples
|
||||
Epoch 0
|
||||
loss: 0.0190
|
||||
Epoch 1
|
||||
loss: 0.0146
|
||||
Epoch 2
|
||||
loss: 0.0049
|
||||
'''
|
||||
|
||||
'''
|
||||
Demonstration of the show_accuracy argument
|
||||
'''
|
||||
model.fit(X_train, y_train, nb_epoch=3, batch_size=16, verbose=2, show_accuracy=True)
|
||||
'''
|
||||
Train on 37800 samples, validate on 4200 samples
|
||||
Epoch 0
|
||||
loss: 0.0190 - acc.: 0.8750
|
||||
Epoch 1
|
||||
loss: 0.0146 - acc.: 0.8750
|
||||
Epoch 2
|
||||
loss: 0.0049 - acc.: 1.0000
|
||||
'''
|
||||
|
||||
'''
|
||||
Demonstration of the validation_split argument
|
||||
'''
|
||||
model.fit(X_train, y_train, nb_epoch=3, batch_size=16,
|
||||
validation_split=0.1, show_accuracy=True, verbose=1)
|
||||
'''
|
||||
Train on 37800 samples, validate on 4200 samples
|
||||
Epoch 0
|
||||
37800/37800 [==============================] - 7s - loss: 0.0385 - acc.: 0.7258 - val. loss: 0.0160 - val. acc.: 0.9136
|
||||
Epoch 1
|
||||
37800/37800 [==============================] - 8s - loss: 0.0140 - acc.: 0.9265 - val. loss: 0.0109 - val. acc.: 0.9383
|
||||
Epoch 2
|
||||
10960/37800 [=======>......................] - ETA: 4s - loss: 0.0109 - acc.: 0.9420
|
||||
'''
|
||||
```
|
||||
|
||||
# Using the Graph model
|
||||
|
||||
```python
|
||||
# graph model with one input and two outputs
|
||||
graph = Graph()
|
||||
graph.add_input(name='input', input_shape=(32,))
|
||||
graph.add_node(Dense(16), name='dense1', input='input')
|
||||
graph.add_node(Dense(4), name='dense2', input='input')
|
||||
graph.add_node(Dense(4), name='dense3', input='dense1')
|
||||
graph.add_output(name='output1', input='dense2')
|
||||
graph.add_output(name='output2', input='dense3')
|
||||
|
||||
graph.compile(optimizer='rmsprop', loss={'output1':'mse', 'output2':'mse'})
|
||||
history = graph.fit({'input':X_train, 'output1':y_train, 'output2':y2_train}, nb_epoch=10)
|
||||
|
||||
```
|
||||
|
||||
```python
|
||||
# graph model with two inputs and one output
|
||||
graph = Graph()
|
||||
graph.add_input(name='input1', input_shape=(32,))
|
||||
graph.add_input(name='input2', input_shape=(32,))
|
||||
graph.add_node(Dense(16), name='dense1', input='input1')
|
||||
graph.add_node(Dense(4), name='dense2', input='input2')
|
||||
graph.add_node(Dense(4), name='dense3', input='dense1')
|
||||
graph.add_output(name='output', inputs=['dense2', 'dense3'], merge_mode='sum')
|
||||
graph.compile(optimizer='rmsprop', loss={'output':'mse'})
|
||||
|
||||
history = graph.fit({'input1':X_train, 'input2':X2_train, 'output':y_train}, nb_epoch=10)
|
||||
predictions = graph.predict({'input1':X_test, 'input2':X2_test}) # {'output':...}
|
||||
|
||||
```
|
||||
|
||||
----
|
||||
|
||||
# Model API documentation
|
||||
|
||||
|
||||
|
||||
{{autogenerated}}
|
||||
@@ -0,0 +1,33 @@
|
||||
# About Keras models
|
||||
|
||||
There are two types of models available in Keras: [the Sequential model](/models/sequential) and [the Model class used with functional API](/models/model).
|
||||
|
||||
These models have a number of methods in common:
|
||||
|
||||
- `model.summary()`: prints a summary representation of your model.
|
||||
- `model.get_config()`: returns a dictionary containing the configuration of the model. The model can be reinstantiated from its config via:
|
||||
```python
|
||||
config = model.get_config()
|
||||
model = Model.from_config(config)
|
||||
# or, for Sequential:
|
||||
model = Sequential.from_config(config)
|
||||
```
|
||||
|
||||
- `model.get_weights()`: returns a list of all weight tensors in the model, as Numpy arrays.
|
||||
- `model.set_weights(weights)`: sets the values of the weights of the model, from a list of Numpy arrays. The arrays in the list should have the same shape as those returned by `get_weights()`.
|
||||
- `model.to_json()`: returns a representation of the model as a JSON string. Note that the representation does not include the weights, only the architecture. You can reinstantiate the same model (with reinitialized weights) from the JSON string via:
|
||||
```python
|
||||
from models import model_from_json
|
||||
|
||||
json_string = model.to_json()
|
||||
model = model_from_json(json_string)
|
||||
```
|
||||
- `model.to_yaml()`: returns a representation of the model as a YAML string. Note that the representation does not include the weights, only the architecture. You can reinstantiate the same model (with reinitialized weights) from the YAML string via:
|
||||
```python
|
||||
from models import model_from_yaml
|
||||
|
||||
yaml_string = model.to_yaml()
|
||||
model = model_from_yaml(yaml_string)
|
||||
```
|
||||
- `model.save_weights(filepath)`: saves the weights of the model as a HDF5 file.
|
||||
- `model.load_weights(filepath, by_name=False)`: loads the weights of the model from a HDF5 file (created by `save_weights`). By default, the architecture is expected to be unchanged. To load weights into a different architecture (with some layers in common), use `by_name=True` to load only those layers with the same name.
|
||||
externo
+32
@@ -0,0 +1,32 @@
|
||||
# Model class API
|
||||
|
||||
In the functional API, given some input tensor(s) and output tensor(s), you can instantiate a `Model` via:
|
||||
|
||||
```python
|
||||
from keras.models import Model
|
||||
from keras.layers import Input, Dense
|
||||
|
||||
a = Input(shape=(32,))
|
||||
b = Dense(32)(a)
|
||||
model = Model(inputs=a, outputs=b)
|
||||
```
|
||||
|
||||
This model will include all layers required in the computation of `b` given `a`.
|
||||
|
||||
In the case of multi-input or multi-output models, you can use lists as well:
|
||||
|
||||
```python
|
||||
model = Model(inputs=[a1, a2], outputs=[b1, b3, b3])
|
||||
```
|
||||
|
||||
For a detailed introduction of what `Model` can do, read [this guide to the Keras functional API](/getting-started/functional-api-guide).
|
||||
|
||||
## Useful attributes of Model
|
||||
|
||||
- `model.layers` is a flattened list of the layers comprising the model graph.
|
||||
- `model.inputs` is the list of input tensors.
|
||||
- `model.outputs` is the list of output tensors.
|
||||
|
||||
## Methods
|
||||
|
||||
{{autogenerated}}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
# The Sequential model API
|
||||
|
||||
To get started, read [this guide to the Keras Sequential model](/getting-started/sequential-model-guide).
|
||||
|
||||
## Useful attributes of Model
|
||||
|
||||
- `model.layers` is a list of the layers added to the model.
|
||||
|
||||
|
||||
----
|
||||
|
||||
## Sequential model methods
|
||||
|
||||
{{autogenerated}}
|
||||
externo
-30
@@ -1,30 +0,0 @@
|
||||
|
||||
## Usage of objectives
|
||||
|
||||
An objective function (or loss function, or optimization score function) is one of the two parameters required to compile a model:
|
||||
|
||||
```python
|
||||
model.compile(loss='mean_squared_error', optimizer='sgd')
|
||||
```
|
||||
|
||||
You can either pass the name of an existing objective, or pass a Theano/TensorFlow symbolic function that returns a scalar for each data-point and takes the following two arguments:
|
||||
|
||||
- __y_true__: True labels. Theano/TensorFlow tensor.
|
||||
- __y_pred__: Predictions. Theano/TensorFlow tensor of the same shape as y_true.
|
||||
|
||||
The actual optimized objective is the mean of the output array across all datapoints.
|
||||
|
||||
For a few examples of such functions, check out the [objectives source](https://github.com/fchollet/keras/blob/master/keras/objectives.py).
|
||||
|
||||
## Available objectives
|
||||
|
||||
- __mean_squared_error__ / __mse__
|
||||
- __mean_absolute_error__ / __mae__
|
||||
- __mean_absolute_percentage_error__ / __mape__
|
||||
- __mean_squared_logarithmic_error__ / __msle__
|
||||
- __squared_hinge__
|
||||
- __hinge__
|
||||
- __binary_crossentropy__: Also known as logloss.
|
||||
- __categorical_crossentropy__: Also known as multiclass logloss. __Note__: using this objective requires that your labels are binary arrays of shape `(nb_samples, nb_classes)`.
|
||||
- __poisson__: mean of `(predictions - targets * log(predictions))`
|
||||
- __cosine_proximity__: the opposite (negative) of the mean cosine proximity between predictions and targets.
|
||||
externo
+28
-3
@@ -4,12 +4,14 @@
|
||||
An optimizer is one of the two arguments required for compiling a Keras model:
|
||||
|
||||
```python
|
||||
from keras import optimizers
|
||||
|
||||
model = Sequential()
|
||||
model.add(Dense(64, init='uniform', input_dim=10))
|
||||
model.add(Dense(64, kernel_initializer='uniform', input_shape=(10,)))
|
||||
model.add(Activation('tanh'))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
|
||||
sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
|
||||
model.compile(loss='mean_squared_error', optimizer=sgd)
|
||||
```
|
||||
|
||||
@@ -22,4 +24,27 @@ model.compile(loss='mean_squared_error', optimizer='sgd')
|
||||
|
||||
---
|
||||
|
||||
{{autogenerated}}
|
||||
## Parameters common to all Keras optimizers
|
||||
|
||||
The parameters `clipnorm` and `clipvalue` can be used with all optimizers to control gradient clipping:
|
||||
|
||||
```python
|
||||
from keras import optimizers
|
||||
|
||||
# All parameter gradients will be clipped to
|
||||
# a maximum norm of 1.
|
||||
sgd = optimizers.SGD(lr=0.01, clipnorm=1.)
|
||||
```
|
||||
|
||||
```python
|
||||
from keras import optimizers
|
||||
|
||||
# All parameter gradients will be clipped to
|
||||
# a maximum value of 0.5 and
|
||||
# a minimum value of -0.5.
|
||||
sgd = optimizers.SGD(lr=0.01, clipvalue=0.5)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
{{autogenerated}}
|
||||
|
||||
+151
-25
@@ -2,55 +2,113 @@
|
||||
## ImageDataGenerator
|
||||
|
||||
```python
|
||||
keras.preprocessing.image.ImageDataGenerator(featurewise_center=True,
|
||||
keras.preprocessing.image.ImageDataGenerator(featurewise_center=False,
|
||||
samplewise_center=False,
|
||||
featurewise_std_normalization=True,
|
||||
featurewise_std_normalization=False,
|
||||
samplewise_std_normalization=False,
|
||||
zca_whitening=False,
|
||||
zca_epsilon=1e-6,
|
||||
rotation_range=0.,
|
||||
width_shift_range=0.,
|
||||
height_shift_range=0.,
|
||||
shear_range=0.,
|
||||
zoom_range=0.,
|
||||
channel_shift_range=0.,
|
||||
fill_mode='nearest',
|
||||
cval=0.,
|
||||
horizontal_flip=False,
|
||||
vertical_flip=False)
|
||||
vertical_flip=False,
|
||||
rescale=None,
|
||||
preprocessing_function=None,
|
||||
data_format=K.image_data_format())
|
||||
```
|
||||
|
||||
Generate batches of tensor image data with real-time data augmentation. The data will be looped over (in batches) indefinitely.
|
||||
|
||||
- __Arguments__:
|
||||
- __featurewise_center__: Boolean. Set input mean to 0 over the dataset.
|
||||
- __featurewise_center__: Boolean. Set input mean to 0 over the dataset, feature-wise.
|
||||
- __samplewise_center__: Boolean. Set each sample mean to 0.
|
||||
- __featurewise_std_normalization__: Boolean. Divide inputs by std of the dataset.
|
||||
- __featurewise_std_normalization__: Boolean. Divide inputs by std of the dataset, feature-wise.
|
||||
- __samplewise_std_normalization__: Boolean. Divide each input by its std.
|
||||
- __zca_epsilon__: epsilon for ZCA whitening. Default is 1e-6.
|
||||
- __zca_whitening__: Boolean. Apply ZCA whitening.
|
||||
- __rotation_range__: Int. Degree range for random rotations.
|
||||
- __width_shift_range__: Float (fraction of total width). Range for random horizontal shifts.
|
||||
- __height_shift_range__: Float (fraction of total height). Range for random vertical shifts.
|
||||
- __shear_range__: Float. Shear Intensity (Shear angle in counter-clockwise direction as radians)
|
||||
- __zoom_range__: Float or [lower, upper]. Range for random zoom. If a float, `[lower, upper] = [1-zoom_range, 1+zoom_range]`.
|
||||
- __channel_shift_range__: Float. Range for random channel shifts.
|
||||
- __fill_mode__: One of {"constant", "nearest", "reflect" or "wrap"}. Points outside the boundaries of the input are filled according to the given mode.
|
||||
- __cval__: Float or Int. Value used for points outside the boundaries when `fill_mode = "constant"`.
|
||||
- __horizontal_flip__: Boolean. Randomly flip inputs horizontally.
|
||||
- __vertical_flip__: Boolean. Randomly flip inputs vertically.
|
||||
- __rescale__: rescaling factor. Defaults to None. If None or 0, no rescaling is applied,
|
||||
otherwise we multiply the data by the value provided (before applying
|
||||
any other transformation).
|
||||
- __preprocessing_function__: function that will be implied on each input.
|
||||
The function will run before any other modification on it.
|
||||
The function should take one argument:
|
||||
one image (Numpy tensor with rank 3),
|
||||
and should output a Numpy tensor with the same shape.
|
||||
- _data_format_: One of {"channels_first", "channels_last"}.
|
||||
"channels_last" mode means that the images should have shape `(samples, height, width, channels)`,
|
||||
"channels_first" mode means that the images should have shape `(samples, channels, height, width)`.
|
||||
It defaults to the `image_data_format` value found in your
|
||||
Keras config file at `~/.keras/keras.json`.
|
||||
If you never set it, then it will be "channels_last".
|
||||
|
||||
- __Methods__:
|
||||
- __fit(X)__: Required if featurewise_center or featurewise_std_normalization or zca_whitening. Compute necessary quantities on some sample data.
|
||||
- __fit(x)__: Compute the internal data stats related to the data-dependent transformations, based on an array of sample data.
|
||||
Only required if featurewise_center or featurewise_std_normalization or zca_whitening.
|
||||
- __Arguments__:
|
||||
- __X__: sample data.
|
||||
- __x__: sample data. Should have rank 4.
|
||||
In case of grayscale data,
|
||||
the channels axis should have value 1, and in case
|
||||
of RGB data, it should have value 3.
|
||||
- __augment__: Boolean (default: False). Whether to fit on randomly augmented samples.
|
||||
- __rounds__: int (default: 1). If augment, how many augmentation passes over the data to use.
|
||||
- __flow(X, y)__:
|
||||
- __seed__: int (default: None). Random seed.
|
||||
- __flow(x, y)__: Takes numpy data & label arrays, and generates batches of augmented/normalized data. Yields batches indefinitely, in an infinite loop.
|
||||
- __Arguments__:
|
||||
- __X__: data.
|
||||
- __x__: data. Should have rank 4.
|
||||
In case of grayscale data,
|
||||
the channels axis should have value 1, and in case
|
||||
of RGB data, it should have value 3.
|
||||
- __y__: labels.
|
||||
- __batch_size__: int (default: 32).
|
||||
- __shuffle__: boolean (defaut: False).
|
||||
- __save_to_dir__: None or str. This allows you to optimally specify a directory to which to save the augmented pictures being generated (useful for visualizing what you are doing).
|
||||
- __save_prefix__: str. Prefix to use for filenames of saved pictures.
|
||||
- __save_format__: one of "png", jpeg".
|
||||
- __shuffle__: boolean (defaut: True).
|
||||
- __seed__: int (default: None).
|
||||
- __save_to_dir__: None or str (default: None). This allows you to optimally specify a directory to which to save the augmented pictures being generated (useful for visualizing what you are doing).
|
||||
- __save_prefix__: str (default: `''`). Prefix to use for filenames of saved pictures (only relevant if `save_to_dir` is set).
|
||||
- __save_format__: one of "png", "jpeg" (only relevant if `save_to_dir` is set). Default: "png".
|
||||
- __yields__: Tuples of `(x, y)` where `x` is a numpy array of image data and `y` is a numpy array of corresponding labels.
|
||||
The generator loops indefinitely.
|
||||
- __flow_from_directory(directory)__: Takes the path to a directory, and generates batches of augmented/normalized data. Yields batches indefinitely, in an infinite loop.
|
||||
- __Arguments__:
|
||||
- __directory__: path to the target directory. It should contain one subdirectory per class.
|
||||
Any PNG, JPG or BMP images inside each of the subdirectories directory tree will be included in the generator.
|
||||
See [this script](https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d) for more details.
|
||||
- __target_size__: tuple of integers, default: `(256, 256)`. The dimensions to which all images found will be resized.
|
||||
- __color_mode__: one of "grayscale", "rbg". Default: "rgb". Whether the images will be converted to have 1 or 3 color channels.
|
||||
- __classes__: optional list of class subdirectories (e.g. `['dogs', 'cats']`). Default: None. If not provided, the list of classes will be automatically inferred from the subdirectory names/structure under `directory`, where each subdirectory will be treated as a different class (and the order of the classes, which will map to the label indices, will be alphanumeric). The dictionary containing the mapping from class names to class indices can be obtained via the attribute `class_indices`.
|
||||
- __class_mode__: one of "categorical", "binary", "sparse" or None. Default: "categorical". Determines the type of label arrays that are returned: "categorical" will be 2D one-hot encoded labels, "binary" will be 1D binary labels, "sparse" will be 1D integer labels. If None, no labels are returned (the generator will only yield batches of image data, which is useful to use `model.predict_generator()`, `model.evaluate_generator()`, etc.). Please note that in case of class_mode None, the data still needs to reside in a subdirectory of `directory` for it to work correctly.
|
||||
- __batch_size__: size of the batches of data (default: 32).
|
||||
- __shuffle__: whether to shuffle the data (default: True)
|
||||
- __seed__: optional random seed for shuffling and transformations.
|
||||
- __save_to_dir__: None or str (default: None). This allows you to optimally specify a directory to which to save the augmented pictures being generated (useful for visualizing what you are doing).
|
||||
- __save_prefix__: str. Prefix to use for filenames of saved pictures (only relevant if `save_to_dir` is set).
|
||||
- __save_format__: one of "png", "jpeg" (only relevant if `save_to_dir` is set). Default: "png".
|
||||
- __follow_links__: whether to follow symlinks inside class subdirectories (default: False).
|
||||
|
||||
|
||||
- __Examples__:
|
||||
|
||||
Example of using `.flow(x, y)`:
|
||||
|
||||
- __Example__:
|
||||
```python
|
||||
(X_train, y_train), (X_test, y_test) = cifar10.load_data(test_split=0.1)
|
||||
Y_train = np_utils.to_categorical(y_train, nb_classes)
|
||||
Y_test = np_utils.to_categorical(y_test, nb_classes)
|
||||
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
|
||||
y_train = np_utils.to_categorical(y_train, num_classes)
|
||||
y_test = np_utils.to_categorical(y_test, num_classes)
|
||||
|
||||
datagen = ImageDataGenerator(
|
||||
featurewise_center=True,
|
||||
@@ -62,21 +120,89 @@ datagen = ImageDataGenerator(
|
||||
|
||||
# compute quantities required for featurewise normalization
|
||||
# (std, mean, and principal components if ZCA whitening is applied)
|
||||
datagen.fit(X_train)
|
||||
datagen.fit(x_train)
|
||||
|
||||
# fits the model on batches with real-time data augmentation:
|
||||
model.fit_generator(datagen.flow(X_train, Y_train, batch_size=32),
|
||||
samples_per_epoch=len(X_train), nb_epoch=nb_epoch)
|
||||
model.fit_generator(datagen.flow(x_train, y_train, batch_size=32),
|
||||
steps_per_epoch=len(x_train) / 32, epochs=epochs)
|
||||
|
||||
# here's a more "manual" example
|
||||
for e in range(nb_epoch):
|
||||
print 'Epoch', e
|
||||
for e in range(epochs):
|
||||
print('Epoch', e)
|
||||
batches = 0
|
||||
for X_batch, Y_batch in datagen.flow(X_train, Y_train, batch_size=32):
|
||||
loss = model.train(X_batch, Y_batch)
|
||||
for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=32):
|
||||
model.fit(x_batch, y_batch)
|
||||
batches += 1
|
||||
if batches >= len(X_train) / 32:
|
||||
if batches >= len(x_train) / 32:
|
||||
# we need to break the loop by hand because
|
||||
# the generator loops indefinitely
|
||||
break
|
||||
```
|
||||
|
||||
Example of using `.flow_from_directory(directory)`:
|
||||
|
||||
```python
|
||||
train_datagen = ImageDataGenerator(
|
||||
rescale=1./255,
|
||||
shear_range=0.2,
|
||||
zoom_range=0.2,
|
||||
horizontal_flip=True)
|
||||
|
||||
test_datagen = ImageDataGenerator(rescale=1./255)
|
||||
|
||||
train_generator = train_datagen.flow_from_directory(
|
||||
'data/train',
|
||||
target_size=(150, 150),
|
||||
batch_size=32,
|
||||
class_mode='binary')
|
||||
|
||||
validation_generator = test_datagen.flow_from_directory(
|
||||
'data/validation',
|
||||
target_size=(150, 150),
|
||||
batch_size=32,
|
||||
class_mode='binary')
|
||||
|
||||
model.fit_generator(
|
||||
train_generator,
|
||||
steps_per_epoch=2000,
|
||||
epochs=50,
|
||||
validation_data=validation_generator,
|
||||
validation_steps=800)
|
||||
```
|
||||
|
||||
Example of transforming images and masks together.
|
||||
|
||||
```python
|
||||
# we create two instances with the same arguments
|
||||
data_gen_args = dict(featurewise_center=True,
|
||||
featurewise_std_normalization=True,
|
||||
rotation_range=90.,
|
||||
width_shift_range=0.1,
|
||||
height_shift_range=0.1,
|
||||
zoom_range=0.2)
|
||||
image_datagen = ImageDataGenerator(**data_gen_args)
|
||||
mask_datagen = ImageDataGenerator(**data_gen_args)
|
||||
|
||||
# Provide the same seed and keyword arguments to the fit and flow methods
|
||||
seed = 1
|
||||
image_datagen.fit(images, augment=True, seed=seed)
|
||||
mask_datagen.fit(masks, augment=True, seed=seed)
|
||||
|
||||
image_generator = image_datagen.flow_from_directory(
|
||||
'data/images',
|
||||
class_mode=None,
|
||||
seed=seed)
|
||||
|
||||
mask_generator = mask_datagen.flow_from_directory(
|
||||
'data/masks',
|
||||
class_mode=None,
|
||||
seed=seed)
|
||||
|
||||
# combine generators into one which yields image and masks
|
||||
train_generator = zip(image_generator, mask_generator)
|
||||
|
||||
model.fit_generator(
|
||||
train_generator,
|
||||
steps_per_epoch=2000,
|
||||
epochs=50)
|
||||
```
|
||||
|
||||
+12
-11
@@ -1,17 +1,18 @@
|
||||
## pad_sequences
|
||||
|
||||
```python
|
||||
keras.preprocessing.sequence.pad_sequences(sequences, maxlen=None, dtype='int32')
|
||||
keras.preprocessing.sequence.pad_sequences(sequences, maxlen=None, dtype='int32',
|
||||
padding='pre', truncating='pre', value=0.)
|
||||
```
|
||||
|
||||
Transform a list of `nb_samples sequences` (lists of scalars) into a 2D numpy array of shape `(nb_samples, nb_timesteps)`. `nb_timesteps` is either the `maxlen` argument if provided, or the length of the longest sequence otherwise. Sequences that are shorter than `nb_timesteps` are padded with zeros at the end.
|
||||
Transform a list of `num_samples` sequences (lists of scalars) into a 2D Numpy array of shape `(num_samples, num_timesteps)`. `num_timesteps` is either the `maxlen` argument if provided, or the length of the longest sequence otherwise. Sequences that are shorter than `num_timesteps` are padded with `value` at the end. Sequences longer than `num_timesteps` are truncated so that it fits the desired length. Position where padding or truncation happens is determined by `padding` or `truncating`, respectively.
|
||||
|
||||
- __Return__: 2D numpy array of shape `(nb_samples, nb_timesteps)`.
|
||||
- __Return__: 2D Numpy array of shape `(num_samples, num_timesteps)`.
|
||||
|
||||
- __Arguments__:
|
||||
- __sequences__: List of lists of int or float.
|
||||
- __maxlen__: None or int. Maximum sequence length, longer sequences are truncated and shorter sequences are padded with zeros at the end.
|
||||
- __dtype__: datatype of the numpy array returned.
|
||||
- __dtype__: datatype of the Numpy array returned.
|
||||
- __padding__: 'pre' or 'post', pad either before or after each sequence.
|
||||
- __truncating__: 'pre' or 'post', remove values from sequences larger than maxlen either in the beginning or in the end of the sequence
|
||||
- __value__: float, value to pad the sequences to the desired value.
|
||||
@@ -21,12 +22,12 @@ Transform a list of `nb_samples sequences` (lists of scalars) into a 2D numpy ar
|
||||
## skipgrams
|
||||
|
||||
```python
|
||||
keras.preprocessing.sequence.skipgrams(sequence, vocabulary_size,
|
||||
window_size=4, negative_samples=1., shuffle=True,
|
||||
keras.preprocessing.sequence.skipgrams(sequence, vocabulary_size,
|
||||
window_size=4, negative_samples=1., shuffle=True,
|
||||
categorical=False, sampling_table=None)
|
||||
```
|
||||
|
||||
Transforms a sequence of word indexes (list of int) into couples of the form:
|
||||
Transforms a sequence of word indexes (list of int) into couples of the form:
|
||||
|
||||
- (word, word in the same window), with label 1 (positive samples).
|
||||
- (word, random word from the vocabulary), with label 0 (negative samples).
|
||||
@@ -34,8 +35,8 @@ Transforms a sequence of word indexes (list of int) into couples of the form:
|
||||
Read more about Skipgram in this gnomic paper by Mikolov et al.: [Efficient Estimation of Word Representations in
|
||||
Vector Space](http://arxiv.org/pdf/1301.3781v3.pdf)
|
||||
|
||||
- __Return__: tuple `(couples, labels)`.
|
||||
- `couples` is a list of 2-elements lists of int: `[word_index, other_word_index]`.
|
||||
- __Return__: tuple `(couples, labels)`.
|
||||
- `couples` is a list of 2-elements lists of int: `[word_index, other_word_index]`.
|
||||
- `labels` is a list of 0 and 1, where 1 indicates that `other_word_index` was found in the same window as `word_index`, and 0 indicates that `other_word_index` was random.
|
||||
- if categorical is set to True, the labels are categorical, ie. 1 becomes [0,1], and 0 becomes [1, 0].
|
||||
|
||||
@@ -46,7 +47,7 @@ Vector Space](http://arxiv.org/pdf/1301.3781v3.pdf)
|
||||
- __negative_samples__: float >= 0. 0 for no negative (=random) samples. 1 for same number as positive samples. etc.
|
||||
- __shuffle__: boolean. Whether to shuffle the samples.
|
||||
- __categorical__: boolean. Whether to make the returned labels categorical.
|
||||
- __sampling_table__: numpy array of shape `(vocabulary_size,)` where `sampling_table[i]` is the probability of sampling the word with index i (assumed to be i-th most common word in the dataset).
|
||||
- __sampling_table__: Numpy array of shape `(vocabulary_size,)` where `sampling_table[i]` is the probability of sampling the word with index i (assumed to be i-th most common word in the dataset).
|
||||
|
||||
|
||||
---
|
||||
@@ -59,7 +60,7 @@ keras.preprocessing.sequence.make_sampling_table(size, sampling_factor=1e-5)
|
||||
|
||||
Used for generating the `sampling_table` argument for `skipgrams`. `sampling_table[i]` is the probability of sampling the word i-th most common word in a dataset (more common words should be sampled less frequently, for balance).
|
||||
|
||||
- __Return__: numpy array of shape `(size,)`.
|
||||
- __Return__: Numpy array of shape `(size,)`.
|
||||
|
||||
- __Arguments__:
|
||||
- __size__: size of the vocabulary considered.
|
||||
|
||||
+60
-12
@@ -2,8 +2,10 @@
|
||||
## text_to_word_sequence
|
||||
|
||||
```python
|
||||
keras.preprocessing.text.text_to_word_sequence(text,
|
||||
filters=base_filter(), lower=True, split=" ")
|
||||
keras.preprocessing.text.text_to_word_sequence(text,
|
||||
filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n',
|
||||
lower=True,
|
||||
split=" ")
|
||||
```
|
||||
|
||||
Split a sentence into a list of words.
|
||||
@@ -12,35 +14,81 @@ Split a sentence into a list of words.
|
||||
|
||||
- __Arguments__:
|
||||
- __text__: str.
|
||||
- __filters__: list (or concatenation) of characters to filter out, such as punctuation. Default: base_filter(), includes basic punctuation, tabs, and newlines.
|
||||
- __filters__: list (or concatenation) of characters to filter out, such as
|
||||
punctuation. Default: '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n' , includes
|
||||
basic punctuation, tabs, and newlines.
|
||||
- __lower__: boolean. Whether to set the text to lowercase.
|
||||
- __split__: str. Separator for word splitting.
|
||||
|
||||
## one_hot
|
||||
|
||||
```python
|
||||
keras.preprocessing.text.one_hot(text, n,
|
||||
filters=base_filter(), lower=True, split=" ")
|
||||
keras.preprocessing.text.one_hot(text,
|
||||
n,
|
||||
filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n',
|
||||
lower=True,
|
||||
split=" ")
|
||||
```
|
||||
|
||||
One-hot encode a text into a list of word indexes in a vocabulary of size n.
|
||||
One-hot encodes a text into a list of word indexes in a vocabulary of size n.
|
||||
|
||||
This is a wrapper to the `hashing_trick` function using `hash` as the hashing function.
|
||||
|
||||
- __Return__: List of integers in [1, n]. Each integer encodes a word (unicity non-guaranteed).
|
||||
|
||||
- __Arguments__: Same as `text_to_word_sequence` above.
|
||||
- __Arguments__:
|
||||
- __text__: str.
|
||||
- __n__: int. Size of vocabulary.
|
||||
- __filters__: list (or concatenation) of characters to filter out, such as
|
||||
punctuation. Default: '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n' , includes
|
||||
basic punctuation, tabs, and newlines.
|
||||
- __lower__: boolean. Whether to set the text to lowercase.
|
||||
- __split__: str. Separator for word splitting.
|
||||
|
||||
## hashing_trick
|
||||
|
||||
```python
|
||||
keras.preprocessing.text.hashing_trick(text,
|
||||
n,
|
||||
hash_function=None,
|
||||
filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n',
|
||||
lower=True,
|
||||
split=' ')
|
||||
```
|
||||
|
||||
Converts a text to a sequence of indices in a fixed-size hashing space
|
||||
|
||||
- __Return__:
|
||||
A list of integer word indices (unicity non-guaranteed).
|
||||
- __Arguments__:
|
||||
- __text__: str.
|
||||
- __n__: Dimension of the hashing space.
|
||||
- __hash_function__: defaults to python `hash` function, can be 'md5' or
|
||||
any function that takes in input a string and returns a int.
|
||||
Note that 'hash' is not a stable hashing function, so
|
||||
it is not consistent across different runs, while 'md5'
|
||||
is a stable hashing function.
|
||||
- __filters__: list (or concatenation) of characters to filter out, such as
|
||||
punctuation. Default: '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n' , includes
|
||||
basic punctuation, tabs, and newlines.
|
||||
- __lower__: boolean. Whether to set the text to lowercase.
|
||||
- __split__: str. Separator for word splitting.
|
||||
|
||||
## Tokenizer
|
||||
|
||||
```python
|
||||
keras.preprocessing.text.Tokenizer(nb_words=None, filters=base_filter(),
|
||||
lower=True, split=" ")
|
||||
keras.preprocessing.text.Tokenizer(num_words=None,
|
||||
filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n',
|
||||
lower=True,
|
||||
split=" ",
|
||||
char_level=False)
|
||||
```
|
||||
|
||||
Class for vectorizing texts, or/and turning texts into sequences (=list of word indexes, where the word of rank i in the dataset (starting at 1) has index i).
|
||||
|
||||
- __Arguments__: Same as `text_to_word_sequence` above.
|
||||
- __nb_words__: None or int. Maximum number of words to work with (if set, tokenization will be restricted to the top nb_words most common words in the dataset).
|
||||
- __num_words__: None or int. Maximum number of words to work with (if set, tokenization will be restricted to the top num_words most common words in the dataset).
|
||||
- __char_level__: if True, every character will be treated as a token.
|
||||
|
||||
- __Methods__:
|
||||
|
||||
@@ -57,7 +105,7 @@ Class for vectorizing texts, or/and turning texts into sequences (=list of word
|
||||
- __Return__: yield one sequence per input text.
|
||||
|
||||
- __texts_to_matrix(texts)__:
|
||||
- __Return__: numpy array of shape `(len(texts), nb_words)`.
|
||||
- __Return__: numpy array of shape `(len(texts), num_words)`.
|
||||
- __Arguments__:
|
||||
- __texts__: list of texts to vectorize.
|
||||
- __mode__: one of "binary", "count", "tfidf", "freq" (default: "binary").
|
||||
@@ -67,7 +115,7 @@ Class for vectorizing texts, or/and turning texts into sequences (=list of word
|
||||
- __sequences__: list of sequences to train on.
|
||||
|
||||
- __sequences_to_matrix(sequences)__:
|
||||
- __Return__: numpy array of shape `(len(sequences), nb_words)`.
|
||||
- __Return__: numpy array of shape `(len(sequences), num_words)`.
|
||||
- __Arguments__:
|
||||
- __sequences__: list of sequences to vectorize.
|
||||
- __mode__: one of "binary", "count", "tfidf", "freq" (default: "binary").
|
||||
|
||||
externo
+24
-18
@@ -2,39 +2,45 @@
|
||||
|
||||
Regularizers allow to apply penalties on layer parameters or layer activity during optimization. These penalties are incorporated in the loss function that the network optimizes.
|
||||
|
||||
The penalties are applied on a per-layer basis. The exact API will depend on the layer, but the layers `Dense`, `TimeDistributedDense`, `MaxoutDense`, `Convolution1D` and `Convolution2D` have a unified API.
|
||||
The penalties are applied on a per-layer basis. The exact API will depend on the layer, but the layers `Dense`, `Conv1D`, `Conv2D` and `Conv3D` have a unified API.
|
||||
|
||||
These layers expose 3 keyword arguments:
|
||||
|
||||
- `W_regularizer`: instance of `keras.regularizers.WeightRegularizer`
|
||||
- `b_regularizer`: instance of `keras.regularizers.WeightRegularizer`
|
||||
- `activity_regularizer`: instance of `keras.regularizers.ActivityRegularizer`
|
||||
- `kernel_regularizer`: instance of `keras.regularizers.Regularizer`
|
||||
- `bias_regularizer`: instance of `keras.regularizers.Regularizer`
|
||||
- `activity_regularizer`: instance of `keras.regularizers.Regularizer`
|
||||
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from keras.regularizers import l2, activity_l2
|
||||
model.add(Dense(64, input_dim=64, W_regularizer=l2(0.01), activity_regularizer=activity_l2(0.01)))
|
||||
from keras import regularizers
|
||||
model.add(Dense(64, input_dim=64,
|
||||
kernel_regularizer=regularizers.l2(0.01),
|
||||
activity_regularizer=regularizers.l1(0.01)))
|
||||
```
|
||||
|
||||
## Available penalties
|
||||
|
||||
```python
|
||||
keras.regularizers.WeightRegularizer(l1=0., l2=0.)
|
||||
keras.regularizers.l1(0.)
|
||||
keras.regularizers.l2(0.)
|
||||
keras.regularizers.l1_l2(0.)
|
||||
```
|
||||
|
||||
## Developing new regularizers
|
||||
|
||||
Any function that takes in a weight matrix and returns a loss contribution tensor can be used as a regularizer, e.g.:
|
||||
|
||||
```python
|
||||
keras.regularizers.ActivityRegularizer(l1=0., l2=0.)
|
||||
from keras import backend as K
|
||||
|
||||
def l1_reg(weight_matrix):
|
||||
return 0.01 * K.sum(K.abs(weight_matrix))
|
||||
|
||||
model.add(Dense(64, input_dim=64,
|
||||
kernel_regularizer=l1_reg)
|
||||
```
|
||||
|
||||
## Shortcuts
|
||||
|
||||
These are shortcut functions available in `keras.regularizers`.
|
||||
|
||||
- __l1__(l=0.01): L1 weight regularization penalty, also known as LASSO
|
||||
- __l2__(l=0.01): L2 weight regularization penalty, also known as weight decay, or Ridge
|
||||
- __l1l2__(l1=0.01, l2=0.01): L1-L2 weight regularization penalty, also known as ElasticNet
|
||||
- __activity_l1__(l=0.01): L1 activity regularization
|
||||
- __activity_l2__(l=0.01): L2 activity regularization
|
||||
- __activity_l1l2__(l1=0.01, l2=0.01): L1+L2 activity regularization
|
||||
Alternatively, you can write your regularizers in an object-oriented way;
|
||||
see the [keras/regularizers.py](https://github.com/fchollet/keras/blob/master/keras/regularizers.py) module for examples.
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
# Wrappers for the Scikit-Learn API
|
||||
|
||||
You can use `Sequential` Keras models (single-input only) as part of your Scikit-Learn workflow via the wrappers found at `keras.wrappers.scikit_learn.py`.
|
||||
|
||||
There are two wrappers available:
|
||||
|
||||
`keras.wrappers.scikit_learn.KerasClassifier(build_fn=None, **sk_params)`, which implements the Scikit-Learn classifier interface,
|
||||
|
||||
`keras.wrappers.scikit_learn.KerasRegressor(build_fn=None, **sk_params)`, which implements the Scikit-Learn regressor interface.
|
||||
|
||||
### Arguments
|
||||
|
||||
- __build_fn__: callable function or class instance
|
||||
- __sk_params__: model parameters & fitting parameters
|
||||
|
||||
`build_fn` should construct, compile and return a Keras model, which
|
||||
will then be used to fit/predict. One of the following
|
||||
three values could be passed to build_fn:
|
||||
|
||||
1. A function
|
||||
2. An instance of a class that implements the __call__ method
|
||||
3. None. This means you implement a class that inherits from either
|
||||
`KerasClassifier` or `KerasRegressor`. The __call__ method of the
|
||||
present class will then be treated as the default build_fn.
|
||||
|
||||
`sk_params` takes both model parameters and fitting parameters. Legal model
|
||||
parameters are the arguments of `build_fn`. Note that like all other
|
||||
estimators in scikit-learn, 'build_fn' should provide default values for
|
||||
its arguments, so that you could create the estimator without passing any
|
||||
values to `sk_params`.
|
||||
|
||||
`sk_params` could also accept parameters for calling `fit`, `predict`,
|
||||
`predict_proba`, and `score` methods (e.g., `epochs`, `batch_size`).
|
||||
fitting (predicting) parameters are selected in the following order:
|
||||
|
||||
1. Values passed to the dictionary arguments of
|
||||
`fit`, `predict`, `predict_proba`, and `score` methods
|
||||
2. Values passed to `sk_params`
|
||||
3. The default values of the `keras.models.Sequential`
|
||||
`fit`, `predict`, `predict_proba` and `score` methods
|
||||
|
||||
When using scikit-learn's `grid_search` API, legal tunable parameters are
|
||||
those you could pass to `sk_params`, including fitting parameters.
|
||||
In other words, you could use `grid_search` to search for the best
|
||||
`batch_size` or `epochs` as well as the model parameters.
|
||||
externo
+9
-9
@@ -1,25 +1,25 @@
|
||||
|
||||
## Model visualization
|
||||
|
||||
The `keras.utils.visualize_util` module provides utility functions to plot
|
||||
a Keras model (using graphviz).
|
||||
The `keras.utils.vis_utils` module provides utility functions to plot
|
||||
a Keras model (using `graphviz`).
|
||||
|
||||
This will plot a graph of the model and save it to a file:
|
||||
```python
|
||||
from keras.utils.visualize_util import plot
|
||||
plot(model, to_file='model.png')
|
||||
from keras.utils import plot_model
|
||||
plot_model(model, to_file='model.png')
|
||||
```
|
||||
|
||||
`plot` takes two optional arguments:
|
||||
`plot_model` takes two optional arguments:
|
||||
|
||||
- `recursive` (defaults to True) controls whether we recursively explore container layers.
|
||||
- `show_shape` (defaults to False) controls whether output shapes are shown in the graph.
|
||||
- `show_shapes` (defaults to False) controls whether output shapes are shown in the graph.
|
||||
- `show_layer_names` (defaults to True) controls whether layer names are shown in the graph.
|
||||
|
||||
You can also directly obtain the `pydot.Graph` object and render it yourself,
|
||||
for example to show it in an ipython notebook :
|
||||
```python
|
||||
from IPython.display import SVG
|
||||
from keras.utils.visualize_util import to_graph
|
||||
from keras.utils.vis_utils import model_to_dot
|
||||
|
||||
SVG(to_graph(model).create(prog='dot', format='svg'))
|
||||
SVG(model_to_dot(model).create(prog='dot', format='svg'))
|
||||
```
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
# Keras examples directory
|
||||
|
||||
[addition_rnn.py](addition_rnn.py)
|
||||
Implementation of sequence to sequence learning for performing addition of two numbers (as strings).
|
||||
|
||||
[antirectifier.py](antirectifier.py)
|
||||
Demonstrates how to write custom layers for Keras.
|
||||
|
||||
[babi_memnn.py](babi_memnn.py)
|
||||
Trains a memory network on the bAbI dataset for reading comprehension.
|
||||
|
||||
[babi_rnn.py](babi_rnn.py)
|
||||
Trains a two-branch recurrent network on the bAbI dataset for reading comprehension.
|
||||
|
||||
[cifar10_cnn.py](cifar10_cnn.py)
|
||||
Trains a simple deep CNN on the CIFAR10 small images dataset.
|
||||
|
||||
[conv_filter_visualization.py](conv_filter_visualization.py)
|
||||
Visualization of the filters of VGG16, via gradient ascent in input space.
|
||||
|
||||
[conv_lstm.py](conv_lstm.py)
|
||||
Demonstrates the use of a convolutional LSTM network.
|
||||
|
||||
[deep_dream.py](deep_dream.py)
|
||||
Deep Dreams in Keras.
|
||||
|
||||
[image_ocr.py](image_ocr.py)
|
||||
Trains a convolutional stack followed by a recurrent stack and a CTC logloss function to perform optical character recognition (OCR).
|
||||
|
||||
[imdb_bidirectional_lstm.py](imdb_bidirectional_lstm.py)
|
||||
Trains a Bidirectional LSTM on the IMDB sentiment classification task.
|
||||
|
||||
[imdb_cnn.py](imdb_cnn.py)
|
||||
Demonstrates the use of Convolution1D for text classification.
|
||||
|
||||
[imdb_cnn_lstm.py](imdb_cnn_lstm.py)
|
||||
Trains a convolutional stack followed by a recurrent stack network on the IMDB sentiment classification task.
|
||||
|
||||
[imdb_fasttext.py](imdb_fasttext.py)
|
||||
Trains a FastText model on the IMDB sentiment classification task.
|
||||
|
||||
[imdb_lstm.py](imdb_lstm.py)
|
||||
Trains a LSTM on the IMDB sentiment classification task.
|
||||
|
||||
[lstm_benchmark.py](lstm_benchmark.py)
|
||||
Compares different LSTM implementations on the IMDB sentiment classification task.
|
||||
|
||||
[lstm_text_generation.py](lstm_text_generation.py)
|
||||
Generates text from Nietzsche's writings.
|
||||
|
||||
[mnist_acgan.py](mnist_acgan.py)
|
||||
Implementation of AC-GAN ( Auxiliary Classifier GAN ) on the MNIST dataset
|
||||
|
||||
[mnist_cnn.py](mnist_cnn.py)
|
||||
Trains a simple convnet on the MNIST dataset.
|
||||
|
||||
[mnist_hierarchical_rnn.py](mnist_hierarchical_rnn.py)
|
||||
Trains a Hierarchical RNN (HRNN) to classify MNIST digits.
|
||||
|
||||
[mnist_irnn.py](mnist_irnn.py)
|
||||
Reproduction of the IRNN experiment with pixel-by-pixel sequential MNIST in "A Simple Way to Initialize Recurrent Networks of Rectified Linear Units" by Le et al.
|
||||
|
||||
[mnist_mlp.py](mnist_mlp.py)
|
||||
Trains a simple deep multi-layer perceptron on the MNIST dataset.
|
||||
|
||||
[mnist_net2net.py](mnist_net2net.py)
|
||||
Reproduction of the Net2Net experiment with MNIST in "Net2Net: Accelerating Learning via Knowledge Transfer".
|
||||
|
||||
[mnist_siamese_graph.py](mnist_siamese_graph.py)
|
||||
Trains a Siamese multi-layer perceptron on pairs of digits from the MNIST dataset.
|
||||
|
||||
[mnist_sklearn_wrapper.py](mnist_sklearn_wrapper.py)
|
||||
Demonstrates how to use the sklearn wrapper.
|
||||
|
||||
[mnist_swwae.py](mnist_swwae.py)
|
||||
Trains a Stacked What-Where AutoEncoder built on residual blocks on the MNIST dataset.
|
||||
|
||||
[mnist_transfer_cnn.py](mnist_transfer_cnn.py)
|
||||
Transfer learning toy example.
|
||||
|
||||
[neural_doodle.py](neural_doodle.py)
|
||||
Neural doodle.
|
||||
|
||||
[neural_style_transfer.py](neural_style_transfer.py)
|
||||
Neural style transfer.
|
||||
|
||||
[pretrained_word_embeddings.py](pretrained_word_embeddings.py)
|
||||
Loads pre-trained word embeddings (GloVe embeddings) into a frozen Keras Embedding layer, and uses it to train a text classification model on the 20 Newsgroup dataset.
|
||||
|
||||
[reuters_mlp.py](reuters_mlp.py)
|
||||
Trains and evaluate a simple MLP on the Reuters newswire topic classification task.
|
||||
|
||||
[stateful_lstm.py](stateful_lstm.py)
|
||||
Demonstrates how to use stateful RNNs to model long sequences efficiently.
|
||||
|
||||
[variational_autoencoder.py](variational_autoencoder.py)
|
||||
Demonstrates how to build a variational autoencoder.
|
||||
|
||||
[variational_autoencoder_deconv.py](variational_autoencoder_deconv.py)
|
||||
Demonstrates how to build a variational autoencoder with Keras using deconvolution layers.
|
||||
+96
-60
@@ -23,41 +23,47 @@ Four digits inverted:
|
||||
|
||||
Five digits inverted:
|
||||
+ One layer LSTM (128 HN), 550k training examples = 99% train/test accuracy in 30 epochs
|
||||
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
from keras.models import Sequential, slice_X
|
||||
from keras.layers.core import Activation, TimeDistributedDense, RepeatVector
|
||||
from keras.layers import recurrent
|
||||
from keras.models import Sequential
|
||||
from keras import layers
|
||||
import numpy as np
|
||||
from six.moves import range
|
||||
|
||||
|
||||
class CharacterTable(object):
|
||||
'''
|
||||
Given a set of characters:
|
||||
"""Given a set of characters:
|
||||
+ Encode them to a one hot integer representation
|
||||
+ Decode the one hot integer representation to their character output
|
||||
+ Decode a vector of probabilties to their character output
|
||||
'''
|
||||
def __init__(self, chars, maxlen):
|
||||
+ Decode a vector of probabilities to their character output
|
||||
"""
|
||||
def __init__(self, chars):
|
||||
"""Initialize character table.
|
||||
|
||||
# Arguments
|
||||
chars: Characters that can appear in the input.
|
||||
"""
|
||||
self.chars = sorted(set(chars))
|
||||
self.char_indices = dict((c, i) for i, c in enumerate(self.chars))
|
||||
self.indices_char = dict((i, c) for i, c in enumerate(self.chars))
|
||||
self.maxlen = maxlen
|
||||
|
||||
def encode(self, C, maxlen=None):
|
||||
maxlen = maxlen if maxlen else self.maxlen
|
||||
X = np.zeros((maxlen, len(self.chars)))
|
||||
def encode(self, C, num_rows):
|
||||
"""One hot encode given string C.
|
||||
|
||||
# Arguments
|
||||
num_rows: Number of rows in the returned one hot encoding. This is
|
||||
used to keep the # of rows for each data the same.
|
||||
"""
|
||||
x = np.zeros((num_rows, len(self.chars)))
|
||||
for i, c in enumerate(C):
|
||||
X[i, self.char_indices[c]] = 1
|
||||
return X
|
||||
x[i, self.char_indices[c]] = 1
|
||||
return x
|
||||
|
||||
def decode(self, X, calc_argmax=True):
|
||||
def decode(self, x, calc_argmax=True):
|
||||
if calc_argmax:
|
||||
X = X.argmax(axis=-1)
|
||||
return ''.join(self.indices_char[x] for x in X)
|
||||
x = x.argmax(axis=-1)
|
||||
return ''.join(self.indices_char[x] for x in x)
|
||||
|
||||
|
||||
class colors:
|
||||
@@ -65,102 +71,132 @@ class colors:
|
||||
fail = '\033[91m'
|
||||
close = '\033[0m'
|
||||
|
||||
# Parameters for the model and dataset
|
||||
# Parameters for the model and dataset.
|
||||
TRAINING_SIZE = 50000
|
||||
DIGITS = 3
|
||||
INVERT = True
|
||||
# Try replacing GRU, or SimpleRNN
|
||||
RNN = recurrent.LSTM
|
||||
HIDDEN_SIZE = 128
|
||||
BATCH_SIZE = 128
|
||||
LAYERS = 1
|
||||
|
||||
# Maximum length of input is 'int + int' (e.g., '345+678'). Maximum length of
|
||||
# int is DIGITS.
|
||||
MAXLEN = DIGITS + 1 + DIGITS
|
||||
|
||||
# All the numbers, plus sign and space for padding.
|
||||
chars = '0123456789+ '
|
||||
ctable = CharacterTable(chars, MAXLEN)
|
||||
ctable = CharacterTable(chars)
|
||||
|
||||
questions = []
|
||||
expected = []
|
||||
seen = set()
|
||||
print('Generating data...')
|
||||
while len(questions) < TRAINING_SIZE:
|
||||
f = lambda: int(''.join(np.random.choice(list('0123456789')) for i in range(np.random.randint(1, DIGITS + 1))))
|
||||
f = lambda: int(''.join(np.random.choice(list('0123456789'))
|
||||
for i in range(np.random.randint(1, DIGITS + 1))))
|
||||
a, b = f(), f()
|
||||
# Skip any addition questions we've already seen
|
||||
# Also skip any such that X+Y == Y+X (hence the sorting)
|
||||
# Also skip any such that x+Y == Y+x (hence the sorting).
|
||||
key = tuple(sorted((a, b)))
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
# Pad the data with spaces such that it is always MAXLEN
|
||||
# Pad the data with spaces such that it is always MAXLEN.
|
||||
q = '{}+{}'.format(a, b)
|
||||
query = q + ' ' * (MAXLEN - len(q))
|
||||
ans = str(a + b)
|
||||
# Answers can be of maximum size DIGITS + 1
|
||||
# Answers can be of maximum size DIGITS + 1.
|
||||
ans += ' ' * (DIGITS + 1 - len(ans))
|
||||
if INVERT:
|
||||
# Reverse the query, e.g., '12+345 ' becomes ' 543+21'. (Note the
|
||||
# space used for padding.)
|
||||
query = query[::-1]
|
||||
questions.append(query)
|
||||
expected.append(ans)
|
||||
print('Total addition questions:', len(questions))
|
||||
|
||||
print('Vectorization...')
|
||||
X = np.zeros((len(questions), MAXLEN, len(chars)), dtype=np.bool)
|
||||
x = np.zeros((len(questions), MAXLEN, len(chars)), dtype=np.bool)
|
||||
y = np.zeros((len(questions), DIGITS + 1, len(chars)), dtype=np.bool)
|
||||
for i, sentence in enumerate(questions):
|
||||
X[i] = ctable.encode(sentence, maxlen=MAXLEN)
|
||||
x[i] = ctable.encode(sentence, MAXLEN)
|
||||
for i, sentence in enumerate(expected):
|
||||
y[i] = ctable.encode(sentence, maxlen=DIGITS + 1)
|
||||
y[i] = ctable.encode(sentence, DIGITS + 1)
|
||||
|
||||
# Shuffle (X, y) in unison as the later parts of X will almost all be larger digits
|
||||
# Shuffle (x, y) in unison as the later parts of x will almost all be larger
|
||||
# digits.
|
||||
indices = np.arange(len(y))
|
||||
np.random.shuffle(indices)
|
||||
X = X[indices]
|
||||
x = x[indices]
|
||||
y = y[indices]
|
||||
|
||||
# Explicitly set apart 10% for validation data that we never train over
|
||||
split_at = len(X) - len(X) / 10
|
||||
(X_train, X_val) = (slice_X(X, 0, split_at), slice_X(X, split_at))
|
||||
(y_train, y_val) = (y[:split_at], y[split_at:])
|
||||
# Explicitly set apart 10% for validation data that we never train over.
|
||||
split_at = len(x) - len(x) // 10
|
||||
(x_train, x_val) = x[:split_at], x[split_at:]
|
||||
(y_train, y_val) = y[:split_at], y[split_at:]
|
||||
|
||||
print(X_train.shape)
|
||||
print('Training Data:')
|
||||
print(x_train.shape)
|
||||
print(y_train.shape)
|
||||
|
||||
print('Validation Data:')
|
||||
print(x_val.shape)
|
||||
print(y_val.shape)
|
||||
|
||||
# Try replacing GRU, or SimpleRNN.
|
||||
RNN = layers.LSTM
|
||||
HIDDEN_SIZE = 128
|
||||
BATCH_SIZE = 128
|
||||
LAYERS = 1
|
||||
|
||||
print('Build model...')
|
||||
model = Sequential()
|
||||
# "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE
|
||||
# note: in a situation where your input sequences have a variable length,
|
||||
# use input_shape=(None, nb_feature).
|
||||
# "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE.
|
||||
# Note: In a situation where your input sequences have a variable length,
|
||||
# use input_shape=(None, num_feature).
|
||||
model.add(RNN(HIDDEN_SIZE, input_shape=(MAXLEN, len(chars))))
|
||||
# For the decoder's input, we repeat the encoded input for each time step
|
||||
model.add(RepeatVector(DIGITS + 1))
|
||||
# The decoder RNN could be multiple layers stacked or a single layer
|
||||
# As the decoder RNN's input, repeatedly provide with the last hidden state of
|
||||
# RNN for each time step. Repeat 'DIGITS + 1' times as that's the maximum
|
||||
# length of output, e.g., when DIGITS=3, max output is 999+999=1998.
|
||||
model.add(layers.RepeatVector(DIGITS + 1))
|
||||
# The decoder RNN could be multiple layers stacked or a single layer.
|
||||
for _ in range(LAYERS):
|
||||
# By setting return_sequences to True, return not only the last output but
|
||||
# all the outputs so far in the form of (num_samples, timesteps,
|
||||
# output_dim). This is necessary as TimeDistributed in the below expects
|
||||
# the first dimension to be the timesteps.
|
||||
model.add(RNN(HIDDEN_SIZE, return_sequences=True))
|
||||
|
||||
# For each of step of the output sequence, decide which character should be chosen
|
||||
model.add(TimeDistributedDense(len(chars)))
|
||||
model.add(Activation('softmax'))
|
||||
# Apply a dense layer to the every temporal slice of an input. For each of step
|
||||
# of the output sequence, decide which character should be chosen.
|
||||
model.add(layers.TimeDistributed(layers.Dense(len(chars))))
|
||||
model.add(layers.Activation('softmax'))
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer='adam',
|
||||
metrics=['accuracy'])
|
||||
model.summary()
|
||||
|
||||
model.compile(loss='categorical_crossentropy', optimizer='adam')
|
||||
|
||||
# Train the model each generation and show predictions against the validation dataset
|
||||
# Train the model each generation and show predictions against the validation
|
||||
# dataset.
|
||||
for iteration in range(1, 200):
|
||||
print()
|
||||
print('-' * 50)
|
||||
print('Iteration', iteration)
|
||||
model.fit(X_train, y_train, batch_size=BATCH_SIZE, nb_epoch=1,
|
||||
validation_data=(X_val, y_val), show_accuracy=True)
|
||||
###
|
||||
# Select 10 samples from the validation set at random so we can visualize errors
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=BATCH_SIZE,
|
||||
epochs=1,
|
||||
validation_data=(x_val, y_val))
|
||||
# Select 10 samples from the validation set at random so we can visualize
|
||||
# errors.
|
||||
for i in range(10):
|
||||
ind = np.random.randint(0, len(X_val))
|
||||
rowX, rowy = X_val[np.array([ind])], y_val[np.array([ind])]
|
||||
preds = model.predict_classes(rowX, verbose=0)
|
||||
q = ctable.decode(rowX[0])
|
||||
ind = np.random.randint(0, len(x_val))
|
||||
rowx, rowy = x_val[np.array([ind])], y_val[np.array([ind])]
|
||||
preds = model.predict_classes(rowx, verbose=0)
|
||||
q = ctable.decode(rowx[0])
|
||||
correct = ctable.decode(rowy[0])
|
||||
guess = ctable.decode(preds[0], calc_argmax=False)
|
||||
print('Q', q[::-1] if INVERT else q)
|
||||
print('T', correct)
|
||||
print(colors.ok + '☑' + colors.close if correct == guess else colors.fail + '☒' + colors.close, guess)
|
||||
if correct == guess:
|
||||
print(colors.ok + '☑' + colors.close, end=" ")
|
||||
else:
|
||||
print(colors.fail + '☒' + colors.close, end=" ")
|
||||
print(guess)
|
||||
print('---')
|
||||
|
||||
+39
-38
@@ -2,7 +2,7 @@
|
||||
|
||||
We build a custom activation layer called 'Antirectifier',
|
||||
which modifies the shape of the tensor that passes through it.
|
||||
We need to specify two methods: `output_shape` and `get_output`.
|
||||
We need to specify two methods: `compute_output_shape` and `call`.
|
||||
|
||||
Note that the same result can also be achieved via a Lambda layer.
|
||||
|
||||
@@ -11,15 +11,14 @@ backend (`K`), our code can run both on TensorFlow and Theano.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
import keras
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Dropout, Layer, Activation
|
||||
from keras import layers
|
||||
from keras.datasets import mnist
|
||||
from keras import backend as K
|
||||
from keras.utils import np_utils
|
||||
|
||||
|
||||
class Antirectifier(Layer):
|
||||
class Antirectifier(layers.Layer):
|
||||
'''This is the combination of a sample-wise
|
||||
L2 normalization with the concatenation of the
|
||||
positive part of the input with the negative part
|
||||
@@ -46,61 +45,63 @@ class Antirectifier(Layer):
|
||||
with twice less parameters yet with comparable
|
||||
classification accuracy as an equivalent ReLU-based network.
|
||||
'''
|
||||
@property
|
||||
def output_shape(self):
|
||||
shape = list(self.input_shape)
|
||||
|
||||
def compute_output_shape(self, input_shape):
|
||||
shape = list(input_shape)
|
||||
assert len(shape) == 2 # only valid for 2D tensors
|
||||
shape[-1] *= 2
|
||||
return tuple(shape)
|
||||
|
||||
def get_output(self, train):
|
||||
x = self.get_input(train)
|
||||
x -= K.mean(x, axis=1, keepdims=True)
|
||||
x = K.l2_normalize(x, axis=1)
|
||||
pos = K.relu(x)
|
||||
neg = K.relu(-x)
|
||||
def call(self, inputs):
|
||||
inputs -= K.mean(inputs, axis=1, keepdims=True)
|
||||
inputs = K.l2_normalize(inputs, axis=1)
|
||||
pos = K.relu(inputs)
|
||||
neg = K.relu(-inputs)
|
||||
return K.concatenate([pos, neg], axis=1)
|
||||
|
||||
# global parameters
|
||||
batch_size = 128
|
||||
nb_classes = 10
|
||||
nb_epoch = 40
|
||||
num_classes = 10
|
||||
epochs = 40
|
||||
|
||||
# the data, shuffled and split between train and test sets
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
|
||||
X_train = X_train.reshape(60000, 784)
|
||||
X_test = X_test.reshape(10000, 784)
|
||||
X_train = X_train.astype('float32')
|
||||
X_test = X_test.astype('float32')
|
||||
X_train /= 255
|
||||
X_test /= 255
|
||||
print(X_train.shape[0], 'train samples')
|
||||
print(X_test.shape[0], 'test samples')
|
||||
x_train = x_train.reshape(60000, 784)
|
||||
x_test = x_test.reshape(10000, 784)
|
||||
x_train = x_train.astype('float32')
|
||||
x_test = x_test.astype('float32')
|
||||
x_train /= 255
|
||||
x_test /= 255
|
||||
print(x_train.shape[0], 'train samples')
|
||||
print(x_test.shape[0], 'test samples')
|
||||
|
||||
# convert class vectors to binary class matrices
|
||||
Y_train = np_utils.to_categorical(y_train, nb_classes)
|
||||
Y_test = np_utils.to_categorical(y_test, nb_classes)
|
||||
y_train = keras.utils.to_categorical(y_train, num_classes)
|
||||
y_test = keras.utils.to_categorical(y_test, num_classes)
|
||||
|
||||
# build the model
|
||||
model = Sequential()
|
||||
model.add(Dense(256, input_shape=(784,)))
|
||||
model.add(layers.Dense(256, input_shape=(784,)))
|
||||
model.add(Antirectifier())
|
||||
model.add(Dropout(0.1))
|
||||
model.add(Dense(256))
|
||||
model.add(layers.Dropout(0.1))
|
||||
model.add(layers.Dense(256))
|
||||
model.add(Antirectifier())
|
||||
model.add(Dropout(0.1))
|
||||
model.add(Dense(10))
|
||||
model.add(Activation('softmax'))
|
||||
model.add(layers.Dropout(0.1))
|
||||
model.add(layers.Dense(10))
|
||||
model.add(layers.Activation('softmax'))
|
||||
|
||||
# compile the model
|
||||
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer='rmsprop',
|
||||
metrics=['accuracy'])
|
||||
|
||||
# train the model
|
||||
model.fit(X_train, Y_train,
|
||||
batch_size=batch_size, nb_epoch=nb_epoch,
|
||||
show_accuracy=True, verbose=1,
|
||||
validation_data=(X_test, Y_test))
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
verbose=1,
|
||||
validation_data=(x_test, y_test))
|
||||
|
||||
# next, compare with an equivalent network
|
||||
# with2x bigger Dense layers and ReLU
|
||||
|
||||
+84
-53
@@ -1,4 +1,4 @@
|
||||
'''Train a memory network on the bAbI dataset.
|
||||
'''Trains a memory network on the bAbI dataset.
|
||||
|
||||
References:
|
||||
- Jason Weston, Antoine Bordes, Sumit Chopra, Tomas Mikolov, Alexander M. Rush,
|
||||
@@ -12,12 +12,12 @@ References:
|
||||
Reaches 98.6% accuracy on task 'single_supporting_fact_10k' after 120 epochs.
|
||||
Time per epoch: 3s on CPU (core i7).
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
from keras.models import Sequential
|
||||
|
||||
from keras.models import Sequential, Model
|
||||
from keras.layers.embeddings import Embedding
|
||||
from keras.layers.core import Activation, Dense, Merge, Permute, Dropout
|
||||
from keras.layers.recurrent import LSTM
|
||||
from keras.layers import Input, Activation, Dense, Permute, Dropout, add, dot, concatenate
|
||||
from keras.layers import LSTM
|
||||
from keras.utils.data_utils import get_file
|
||||
from keras.preprocessing.sequence import pad_sequences
|
||||
from functools import reduce
|
||||
@@ -38,7 +38,8 @@ def tokenize(sent):
|
||||
def parse_stories(lines, only_supporting=False):
|
||||
'''Parse stories provided in the bAbi tasks format
|
||||
|
||||
If only_supporting is true, only the sentences that support the answer are kept.
|
||||
If only_supporting is true, only the sentences
|
||||
that support the answer are kept.
|
||||
'''
|
||||
data = []
|
||||
story = []
|
||||
@@ -68,9 +69,12 @@ def parse_stories(lines, only_supporting=False):
|
||||
|
||||
|
||||
def get_stories(f, only_supporting=False, max_length=None):
|
||||
'''Given a file name, read the file, retrieve the stories, and then convert the sentences into a single story.
|
||||
'''Given a file name, read the file,
|
||||
retrieve the stories,
|
||||
and then convert the sentences into a single story.
|
||||
|
||||
If max_length is supplied, any stories longer than max_length tokens will be discarded.
|
||||
If max_length is supplied,
|
||||
any stories longer than max_length tokens will be discarded.
|
||||
'''
|
||||
data = parse_stories(f.readlines(), only_supporting=only_supporting)
|
||||
flatten = lambda data: reduce(lambda x, y: x + y, data)
|
||||
@@ -85,7 +89,8 @@ def vectorize_stories(data, word_idx, story_maxlen, query_maxlen):
|
||||
for story, query, answer in data:
|
||||
x = [word_idx[w] for w in story]
|
||||
xq = [word_idx[w] for w in query]
|
||||
y = np.zeros(len(word_idx) + 1) # let's not forget that index 0 is reserved
|
||||
# let's not forget that index 0 is reserved
|
||||
y = np.zeros(len(word_idx) + 1)
|
||||
y[word_idx[answer]] = 1
|
||||
X.append(x)
|
||||
Xq.append(xq)
|
||||
@@ -93,9 +98,13 @@ def vectorize_stories(data, word_idx, story_maxlen, query_maxlen):
|
||||
return (pad_sequences(X, maxlen=story_maxlen),
|
||||
pad_sequences(Xq, maxlen=query_maxlen), np.array(Y))
|
||||
|
||||
|
||||
path = get_file('babi-tasks-v1-2.tar.gz',
|
||||
origin='http://www.thespermwhale.com/jaseweston/babi/tasks_1-20_v1-2.tar.gz')
|
||||
try:
|
||||
path = get_file('babi-tasks-v1-2.tar.gz', origin='https://s3.amazonaws.com/text-datasets/babi_tasks_1-20_v1-2.tar.gz')
|
||||
except:
|
||||
print('Error downloading dataset, please download it manually:\n'
|
||||
'$ wget http://www.thespermwhale.com/jaseweston/babi/tasks_1-20_v1-2.tar.gz\n'
|
||||
'$ mv tasks_1-20_v1-2.tar.gz ~/.keras/datasets/babi-tasks-v1-2.tar.gz')
|
||||
raise
|
||||
tar = tarfile.open(path)
|
||||
|
||||
challenges = {
|
||||
@@ -111,7 +120,11 @@ print('Extracting stories for the challenge:', challenge_type)
|
||||
train_stories = get_stories(tar.extractfile(challenge.format('train')))
|
||||
test_stories = get_stories(tar.extractfile(challenge.format('test')))
|
||||
|
||||
vocab = sorted(reduce(lambda x, y: x | y, (set(story + q + [answer]) for story, q, answer in train_stories + test_stories)))
|
||||
vocab = set()
|
||||
for story, q, answer in train_stories + test_stories:
|
||||
vocab |= set(story + q + [answer])
|
||||
vocab = sorted(vocab)
|
||||
|
||||
# Reserve 0 for masking via pad_sequences
|
||||
vocab_size = len(vocab) + 1
|
||||
story_maxlen = max(map(len, (x for x, _, _ in train_stories + test_stories)))
|
||||
@@ -130,8 +143,14 @@ print('-')
|
||||
print('Vectorizing the word sequences...')
|
||||
|
||||
word_idx = dict((c, i + 1) for i, c in enumerate(vocab))
|
||||
inputs_train, queries_train, answers_train = vectorize_stories(train_stories, word_idx, story_maxlen, query_maxlen)
|
||||
inputs_test, queries_test, answers_test = vectorize_stories(test_stories, word_idx, story_maxlen, query_maxlen)
|
||||
inputs_train, queries_train, answers_train = vectorize_stories(train_stories,
|
||||
word_idx,
|
||||
story_maxlen,
|
||||
query_maxlen)
|
||||
inputs_test, queries_test, answers_test = vectorize_stories(test_stories,
|
||||
word_idx,
|
||||
story_maxlen,
|
||||
query_maxlen)
|
||||
|
||||
print('-')
|
||||
print('inputs: integer tensor of shape (samples, max_length)')
|
||||
@@ -148,13 +167,25 @@ print('answers_test shape:', answers_test.shape)
|
||||
print('-')
|
||||
print('Compiling...')
|
||||
|
||||
# placeholders
|
||||
input_sequence = Input((story_maxlen,))
|
||||
question = Input((query_maxlen,))
|
||||
|
||||
# encoders
|
||||
# embed the input sequence into a sequence of vectors
|
||||
input_encoder_m = Sequential()
|
||||
input_encoder_m.add(Embedding(input_dim=vocab_size,
|
||||
output_dim=64,
|
||||
input_length=story_maxlen))
|
||||
output_dim=64))
|
||||
input_encoder_m.add(Dropout(0.3))
|
||||
# output: (samples, story_maxlen, embedding_dim)
|
||||
|
||||
# embed the input into a sequence of vectors of size query_maxlen
|
||||
input_encoder_c = Sequential()
|
||||
input_encoder_c.add(Embedding(input_dim=vocab_size,
|
||||
output_dim=query_maxlen))
|
||||
input_encoder_c.add(Dropout(0.3))
|
||||
# output: (samples, story_maxlen, query_maxlen)
|
||||
|
||||
# embed the question into a sequence of vectors
|
||||
question_encoder = Sequential()
|
||||
question_encoder.add(Embedding(input_dim=vocab_size,
|
||||
@@ -162,43 +193,43 @@ question_encoder.add(Embedding(input_dim=vocab_size,
|
||||
input_length=query_maxlen))
|
||||
question_encoder.add(Dropout(0.3))
|
||||
# output: (samples, query_maxlen, embedding_dim)
|
||||
# compute a 'match' between input sequence elements (which are vectors)
|
||||
# and the question vector sequence
|
||||
match = Sequential()
|
||||
match.add(Merge([input_encoder_m, question_encoder],
|
||||
mode='dot',
|
||||
dot_axes=[(2,), (2,)]))
|
||||
# output: (samples, story_maxlen, query_maxlen)
|
||||
# embed the input into a single vector with size = story_maxlen:
|
||||
input_encoder_c = Sequential()
|
||||
input_encoder_c.add(Embedding(input_dim=vocab_size,
|
||||
output_dim=query_maxlen,
|
||||
input_length=story_maxlen))
|
||||
input_encoder_c.add(Dropout(0.3))
|
||||
# output: (samples, story_maxlen, query_maxlen)
|
||||
# sum the match vector with the input vector:
|
||||
response = Sequential()
|
||||
response.add(Merge([match, input_encoder_c], mode='sum'))
|
||||
# output: (samples, story_maxlen, query_maxlen)
|
||||
response.add(Permute((2, 1))) # output: (samples, query_maxlen, story_maxlen)
|
||||
|
||||
# concatenate the match vector with the question vector,
|
||||
# and do logistic regression on top
|
||||
answer = Sequential()
|
||||
answer.add(Merge([response, question_encoder], mode='concat', concat_axis=-1))
|
||||
# encode input sequence and questions (which are indices)
|
||||
# to sequences of dense vectors
|
||||
input_encoded_m = input_encoder_m(input_sequence)
|
||||
input_encoded_c = input_encoder_c(input_sequence)
|
||||
question_encoded = question_encoder(question)
|
||||
|
||||
# compute a 'match' between the first input vector sequence
|
||||
# and the question vector sequence
|
||||
# shape: `(samples, story_maxlen, query_maxlen)`
|
||||
match = dot([input_encoded_m, question_encoded], axes=(2, 2))
|
||||
match = Activation('softmax')(match)
|
||||
|
||||
# add the match matrix with the second input vector sequence
|
||||
response = add([match, input_encoded_c]) # (samples, story_maxlen, query_maxlen)
|
||||
response = Permute((2, 1))(response) # (samples, query_maxlen, story_maxlen)
|
||||
|
||||
# concatenate the match matrix with the question vector sequence
|
||||
answer = concatenate([response, question_encoded])
|
||||
|
||||
# the original paper uses a matrix multiplication for this reduction step.
|
||||
# we choose to use a RNN instead.
|
||||
answer.add(LSTM(32))
|
||||
# one regularization layer -- more would probably be needed.
|
||||
answer.add(Dropout(0.3))
|
||||
answer.add(Dense(vocab_size))
|
||||
# we output a probability distribution over the vocabulary
|
||||
answer.add(Activation('softmax'))
|
||||
answer = LSTM(32)(answer) # (samples, 32)
|
||||
|
||||
answer.compile(optimizer='rmsprop', loss='categorical_crossentropy')
|
||||
# Note: you could use a Graph model to avoid repeat the input twice
|
||||
answer.fit([inputs_train, queries_train, inputs_train], answers_train,
|
||||
batch_size=32,
|
||||
nb_epoch=120,
|
||||
show_accuracy=True,
|
||||
validation_data=([inputs_test, queries_test, inputs_test], answers_test))
|
||||
# one regularization layer -- more would probably be needed.
|
||||
answer = Dropout(0.3)(answer)
|
||||
answer = Dense(vocab_size)(answer) # (samples, vocab_size)
|
||||
# we output a probability distribution over the vocabulary
|
||||
answer = Activation('softmax')(answer)
|
||||
|
||||
# build the final model
|
||||
model = Model([input_sequence, question], answer)
|
||||
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
|
||||
metrics=['accuracy'])
|
||||
|
||||
# train
|
||||
model.fit([inputs_train, queries_train], answers_train,
|
||||
batch_size=32,
|
||||
epochs=120,
|
||||
validation_data=([inputs_test, queries_test], answers_test))
|
||||
|
||||
+61
-39
@@ -12,7 +12,7 @@ QA2 - Two Supporting Facts | 20 | 50.0
|
||||
QA3 - Three Supporting Facts | 20 | 20.5
|
||||
QA4 - Two Arg. Relations | 61 | 62.9
|
||||
QA5 - Three Arg. Relations | 70 | 61.9
|
||||
QA6 - Yes/No Questions | 48 | 50.7
|
||||
QA6 - yes/No Questions | 48 | 50.7
|
||||
QA7 - Counting | 49 | 78.9
|
||||
QA8 - Lists/Sets | 45 | 77.2
|
||||
QA9 - Simple Negation | 64 | 64.0
|
||||
@@ -62,13 +62,12 @@ import re
|
||||
import tarfile
|
||||
|
||||
import numpy as np
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
from keras.utils.data_utils import get_file
|
||||
from keras.layers.embeddings import Embedding
|
||||
from keras.layers.core import Dense, Merge, Dropout, RepeatVector
|
||||
from keras import layers
|
||||
from keras.layers import recurrent
|
||||
from keras.models import Sequential
|
||||
from keras.models import Model
|
||||
from keras.preprocessing.sequence import pad_sequences
|
||||
|
||||
|
||||
@@ -84,7 +83,8 @@ def tokenize(sent):
|
||||
def parse_stories(lines, only_supporting=False):
|
||||
'''Parse stories provided in the bAbi tasks format
|
||||
|
||||
If only_supporting is true, only the sentences that support the answer are kept.
|
||||
If only_supporting is true,
|
||||
only the sentences that support the answer are kept.
|
||||
'''
|
||||
data = []
|
||||
story = []
|
||||
@@ -114,9 +114,11 @@ def parse_stories(lines, only_supporting=False):
|
||||
|
||||
|
||||
def get_stories(f, only_supporting=False, max_length=None):
|
||||
'''Given a file name, read the file, retrieve the stories, and then convert the sentences into a single story.
|
||||
'''Given a file name, read the file, retrieve the stories,
|
||||
and then convert the sentences into a single story.
|
||||
|
||||
If max_length is supplied, any stories longer than max_length tokens will be discarded.
|
||||
If max_length is supplied,
|
||||
any stories longer than max_length tokens will be discarded.
|
||||
'''
|
||||
data = parse_stories(f.readlines(), only_supporting=only_supporting)
|
||||
flatten = lambda data: reduce(lambda x, y: x + y, data)
|
||||
@@ -125,18 +127,19 @@ def get_stories(f, only_supporting=False, max_length=None):
|
||||
|
||||
|
||||
def vectorize_stories(data, word_idx, story_maxlen, query_maxlen):
|
||||
X = []
|
||||
Xq = []
|
||||
Y = []
|
||||
xs = []
|
||||
xqs = []
|
||||
ys = []
|
||||
for story, query, answer in data:
|
||||
x = [word_idx[w] for w in story]
|
||||
xq = [word_idx[w] for w in query]
|
||||
y = np.zeros(len(word_idx) + 1) # let's not forget that index 0 is reserved
|
||||
# let's not forget that index 0 is reserved
|
||||
y = np.zeros(len(word_idx) + 1)
|
||||
y[word_idx[answer]] = 1
|
||||
X.append(x)
|
||||
Xq.append(xq)
|
||||
Y.append(y)
|
||||
return pad_sequences(X, maxlen=story_maxlen), pad_sequences(Xq, maxlen=query_maxlen), np.array(Y)
|
||||
xs.append(x)
|
||||
xqs.append(xq)
|
||||
ys.append(y)
|
||||
return pad_sequences(xs, maxlen=story_maxlen), pad_sequences(xqs, maxlen=query_maxlen), np.array(ys)
|
||||
|
||||
RNN = recurrent.LSTM
|
||||
EMBED_HIDDEN_SIZE = 50
|
||||
@@ -144,9 +147,18 @@ SENT_HIDDEN_SIZE = 100
|
||||
QUERY_HIDDEN_SIZE = 100
|
||||
BATCH_SIZE = 32
|
||||
EPOCHS = 40
|
||||
print('RNN / Embed / Sent / Query = {}, {}, {}, {}'.format(RNN, EMBED_HIDDEN_SIZE, SENT_HIDDEN_SIZE, QUERY_HIDDEN_SIZE))
|
||||
print('RNN / Embed / Sent / Query = {}, {}, {}, {}'.format(RNN,
|
||||
EMBED_HIDDEN_SIZE,
|
||||
SENT_HIDDEN_SIZE,
|
||||
QUERY_HIDDEN_SIZE))
|
||||
|
||||
path = get_file('babi-tasks-v1-2.tar.gz', origin='http://www.thespermwhale.com/jaseweston/babi/tasks_1-20_v1-2.tar.gz')
|
||||
try:
|
||||
path = get_file('babi-tasks-v1-2.tar.gz', origin='https://s3.amazonaws.com/text-datasets/babi_tasks_1-20_v1-2.tar.gz')
|
||||
except:
|
||||
print('Error downloading dataset, please download it manually:\n'
|
||||
'$ wget http://www.thespermwhale.com/jaseweston/babi/tasks_1-20_v1-2.tar.gz\n'
|
||||
'$ mv tasks_1-20_v1-2.tar.gz ~/.keras/datasets/babi-tasks-v1-2.tar.gz')
|
||||
raise
|
||||
tar = tarfile.open(path)
|
||||
# Default QA1 with 1000 samples
|
||||
# challenge = 'tasks_1-20_v1-2/en/qa1_single-supporting-fact_{}.txt'
|
||||
@@ -159,43 +171,53 @@ challenge = 'tasks_1-20_v1-2/en/qa2_two-supporting-facts_{}.txt'
|
||||
train = get_stories(tar.extractfile(challenge.format('train')))
|
||||
test = get_stories(tar.extractfile(challenge.format('test')))
|
||||
|
||||
vocab = sorted(reduce(lambda x, y: x | y, (set(story + q + [answer]) for story, q, answer in train + test)))
|
||||
vocab = set()
|
||||
for story, q, answer in train + test:
|
||||
vocab |= set(story + q + [answer])
|
||||
vocab = sorted(vocab)
|
||||
|
||||
# Reserve 0 for masking via pad_sequences
|
||||
vocab_size = len(vocab) + 1
|
||||
word_idx = dict((c, i + 1) for i, c in enumerate(vocab))
|
||||
story_maxlen = max(map(len, (x for x, _, _ in train + test)))
|
||||
query_maxlen = max(map(len, (x for _, x, _ in train + test)))
|
||||
|
||||
X, Xq, Y = vectorize_stories(train, word_idx, story_maxlen, query_maxlen)
|
||||
tX, tXq, tY = vectorize_stories(test, word_idx, story_maxlen, query_maxlen)
|
||||
x, xq, y = vectorize_stories(train, word_idx, story_maxlen, query_maxlen)
|
||||
tx, txq, ty = vectorize_stories(test, word_idx, story_maxlen, query_maxlen)
|
||||
|
||||
print('vocab = {}'.format(vocab))
|
||||
print('X.shape = {}'.format(X.shape))
|
||||
print('Xq.shape = {}'.format(Xq.shape))
|
||||
print('Y.shape = {}'.format(Y.shape))
|
||||
print('x.shape = {}'.format(x.shape))
|
||||
print('xq.shape = {}'.format(xq.shape))
|
||||
print('y.shape = {}'.format(y.shape))
|
||||
print('story_maxlen, query_maxlen = {}, {}'.format(story_maxlen, query_maxlen))
|
||||
|
||||
print('Build model...')
|
||||
|
||||
sentrnn = Sequential()
|
||||
sentrnn.add(Embedding(vocab_size, EMBED_HIDDEN_SIZE, input_length=story_maxlen, mask_zero=True))
|
||||
sentrnn.add(Dropout(0.3))
|
||||
sentence = layers.Input(shape=(story_maxlen,), dtype='int32')
|
||||
encoded_sentence = layers.Embedding(vocab_size, EMBED_HIDDEN_SIZE)(sentence)
|
||||
encoded_sentence = layers.Dropout(0.3)(encoded_sentence)
|
||||
|
||||
qrnn = Sequential()
|
||||
qrnn.add(Embedding(vocab_size, EMBED_HIDDEN_SIZE, input_length=query_maxlen))
|
||||
qrnn.add(Dropout(0.3))
|
||||
qrnn.add(RNN(EMBED_HIDDEN_SIZE, return_sequences=False))
|
||||
qrnn.add(RepeatVector(story_maxlen))
|
||||
question = layers.Input(shape=(query_maxlen,), dtype='int32')
|
||||
encoded_question = layers.Embedding(vocab_size, EMBED_HIDDEN_SIZE)(question)
|
||||
encoded_question = layers.Dropout(0.3)(encoded_question)
|
||||
encoded_question = RNN(EMBED_HIDDEN_SIZE)(encoded_question)
|
||||
encoded_question = layers.RepeatVector(story_maxlen)(encoded_question)
|
||||
|
||||
model = Sequential()
|
||||
model.add(Merge([sentrnn, qrnn], mode='sum'))
|
||||
model.add(RNN(EMBED_HIDDEN_SIZE, return_sequences=False))
|
||||
model.add(Dropout(0.3))
|
||||
model.add(Dense(vocab_size, activation='softmax'))
|
||||
merged = layers.add([encoded_sentence, encoded_question])
|
||||
merged = RNN(EMBED_HIDDEN_SIZE)(merged)
|
||||
merged = layers.Dropout(0.3)(merged)
|
||||
preds = layers.Dense(vocab_size, activation='softmax')(merged)
|
||||
|
||||
model.compile(optimizer='adam', loss='categorical_crossentropy', class_mode='categorical')
|
||||
model = Model([sentence, question], preds)
|
||||
model.compile(optimizer='adam',
|
||||
loss='categorical_crossentropy',
|
||||
metrics=['accuracy'])
|
||||
|
||||
print('Training')
|
||||
model.fit([X, Xq], Y, batch_size=BATCH_SIZE, nb_epoch=EPOCHS, validation_split=0.05, show_accuracy=True)
|
||||
loss, acc = model.evaluate([tX, tXq], tY, batch_size=BATCH_SIZE, show_accuracy=True)
|
||||
model.fit([x, xq], y,
|
||||
batch_size=BATCH_SIZE,
|
||||
epochs=EPOCHS,
|
||||
validation_split=0.05)
|
||||
loss, acc = model.evaluate([tx, txq], ty,
|
||||
batch_size=BATCH_SIZE)
|
||||
print('Test loss / test accuracy = {:.4f} / {:.4f}'.format(loss, acc))
|
||||
|
||||
+47
-52
@@ -1,58 +1,48 @@
|
||||
'''Train a simple deep CNN on the CIFAR10 small images dataset.
|
||||
|
||||
GPU run command:
|
||||
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python cifar10_cnn.py
|
||||
GPU run command with Theano backend (with TensorFlow, the GPU is automatically used):
|
||||
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatx=float32 python cifar10_cnn.py
|
||||
|
||||
It gets down to 0.65 test logloss in 25 epochs, and down to 0.55 after 50 epochs.
|
||||
(it's still underfitting at that point, though).
|
||||
|
||||
Note: the data was pickled with Python 2, and some encoding issues might prevent you
|
||||
from loading it in Python 3. You might have to load it in Python 2,
|
||||
save it in a different format, load it in Python 3 and repickle it.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import keras
|
||||
from keras.datasets import cifar10
|
||||
from keras.preprocessing.image import ImageDataGenerator
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Dropout, Activation, Flatten
|
||||
from keras.layers.convolutional import Convolution2D, MaxPooling2D
|
||||
from keras.optimizers import SGD
|
||||
from keras.utils import np_utils
|
||||
from keras.layers import Dense, Dropout, Activation, Flatten
|
||||
from keras.layers import Conv2D, MaxPooling2D
|
||||
|
||||
batch_size = 32
|
||||
nb_classes = 10
|
||||
nb_epoch = 200
|
||||
num_classes = 10
|
||||
epochs = 200
|
||||
data_augmentation = True
|
||||
|
||||
# input image dimensions
|
||||
img_rows, img_cols = 32, 32
|
||||
# the CIFAR10 images are RGB
|
||||
img_channels = 3
|
||||
# The data, shuffled and split between train and test sets:
|
||||
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
|
||||
print('x_train shape:', x_train.shape)
|
||||
print(x_train.shape[0], 'train samples')
|
||||
print(x_test.shape[0], 'test samples')
|
||||
|
||||
# the data, shuffled and split between train and test sets
|
||||
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
|
||||
print('X_train shape:', X_train.shape)
|
||||
print(X_train.shape[0], 'train samples')
|
||||
print(X_test.shape[0], 'test samples')
|
||||
|
||||
# convert class vectors to binary class matrices
|
||||
Y_train = np_utils.to_categorical(y_train, nb_classes)
|
||||
Y_test = np_utils.to_categorical(y_test, nb_classes)
|
||||
# Convert class vectors to binary class matrices.
|
||||
y_train = keras.utils.to_categorical(y_train, num_classes)
|
||||
y_test = keras.utils.to_categorical(y_test, num_classes)
|
||||
|
||||
model = Sequential()
|
||||
|
||||
model.add(Convolution2D(32, 3, 3, border_mode='same',
|
||||
input_shape=(img_channels, img_rows, img_cols)))
|
||||
model.add(Conv2D(32, (3, 3), padding='same',
|
||||
input_shape=x_train.shape[1:]))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Convolution2D(32, 3, 3))
|
||||
model.add(Conv2D(32, (3, 3)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
model.add(Dropout(0.25))
|
||||
|
||||
model.add(Convolution2D(64, 3, 3, border_mode='same'))
|
||||
model.add(Conv2D(64, (3, 3), padding='same'))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Convolution2D(64, 3, 3))
|
||||
model.add(Conv2D(64, (3, 3)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
model.add(Dropout(0.25))
|
||||
@@ -61,27 +51,32 @@ model.add(Flatten())
|
||||
model.add(Dense(512))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(nb_classes))
|
||||
model.add(Dense(num_classes))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
# let's train the model using SGD + momentum (how original).
|
||||
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
|
||||
model.compile(loss='categorical_crossentropy', optimizer=sgd)
|
||||
# initiate RMSprop optimizer
|
||||
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
|
||||
|
||||
X_train = X_train.astype('float32')
|
||||
X_test = X_test.astype('float32')
|
||||
X_train /= 255
|
||||
X_test /= 255
|
||||
# Let's train the model using RMSprop
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer=opt,
|
||||
metrics=['accuracy'])
|
||||
|
||||
x_train = x_train.astype('float32')
|
||||
x_test = x_test.astype('float32')
|
||||
x_train /= 255
|
||||
x_test /= 255
|
||||
|
||||
if not data_augmentation:
|
||||
print('Not using data augmentation.')
|
||||
model.fit(X_train, Y_train, batch_size=batch_size,
|
||||
nb_epoch=nb_epoch, show_accuracy=True,
|
||||
validation_data=(X_test, Y_test), shuffle=True)
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
validation_data=(x_test, y_test),
|
||||
shuffle=True)
|
||||
else:
|
||||
print('Using real-time data augmentation.')
|
||||
|
||||
# this will do preprocessing and realtime data augmentation
|
||||
# This will do preprocessing and realtime data augmentation:
|
||||
datagen = ImageDataGenerator(
|
||||
featurewise_center=False, # set input mean to 0 over the dataset
|
||||
samplewise_center=False, # set each sample mean to 0
|
||||
@@ -94,13 +89,13 @@ else:
|
||||
horizontal_flip=True, # randomly flip images
|
||||
vertical_flip=False) # randomly flip images
|
||||
|
||||
# compute quantities required for featurewise normalization
|
||||
# (std, mean, and principal components if ZCA whitening is applied)
|
||||
datagen.fit(X_train)
|
||||
# Compute quantities required for feature-wise normalization
|
||||
# (std, mean, and principal components if ZCA whitening is applied).
|
||||
datagen.fit(x_train)
|
||||
|
||||
# fit the model on the batches generated by datagen.flow()
|
||||
model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
|
||||
samples_per_epoch=X_train.shape[0],
|
||||
nb_epoch=nb_epoch, show_accuracy=True,
|
||||
validation_data=(X_test, Y_test),
|
||||
nb_worker=1)
|
||||
# Fit the model on the batches generated by datagen.flow().
|
||||
model.fit_generator(datagen.flow(x_train, y_train,
|
||||
batch_size=batch_size),
|
||||
steps_per_epoch=x_train.shape[0] // batch_size,
|
||||
epochs=epochs,
|
||||
validation_data=(x_test, y_test))
|
||||
|
||||
@@ -3,34 +3,26 @@
|
||||
This script can run on CPU in a few minutes (with the TensorFlow backend).
|
||||
|
||||
Results example: http://i.imgur.com/4nj4KjN.jpg
|
||||
|
||||
Before running this script, download the weights for the VGG16 model at:
|
||||
https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmRCM0ROVHc/view?usp=sharing
|
||||
(source: https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3)
|
||||
and make sure the variable `weights_path` in this script matches the location of the file.
|
||||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
from scipy.misc import imsave
|
||||
import numpy as np
|
||||
import time
|
||||
import os
|
||||
import h5py
|
||||
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Convolution2D, ZeroPadding2D, MaxPooling2D
|
||||
from keras.applications import vgg16
|
||||
from keras import backend as K
|
||||
|
||||
# dimensions of the generated pictures for each filter.
|
||||
img_width = 128
|
||||
img_height = 128
|
||||
|
||||
# path to the model weights file.
|
||||
weights_path = 'vgg16_weights.h5'
|
||||
|
||||
# the name of the layer we want to visualize (see model definition below)
|
||||
layer_name = 'conv5_1'
|
||||
# the name of the layer we want to visualize
|
||||
# (see model definition at keras/applications/vgg16.py)
|
||||
layer_name = 'block5_conv1'
|
||||
|
||||
# util function to convert a tensor into a valid image
|
||||
|
||||
|
||||
def deprocess_image(x):
|
||||
# normalize tensor: center on 0., ensure std is 0.1
|
||||
x -= x.mean()
|
||||
@@ -43,72 +35,22 @@ def deprocess_image(x):
|
||||
|
||||
# convert to RGB array
|
||||
x *= 255
|
||||
x = x.transpose((1, 2, 0))
|
||||
if K.image_data_format() == 'channels_first':
|
||||
x = x.transpose((1, 2, 0))
|
||||
x = np.clip(x, 0, 255).astype('uint8')
|
||||
return x
|
||||
|
||||
# this will contain our generated image
|
||||
input_img = K.placeholder((1, 3, img_width, img_height))
|
||||
|
||||
# build the VGG16 network with our input_img as input
|
||||
first_layer = ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height))
|
||||
first_layer.input = input_img
|
||||
|
||||
model = Sequential()
|
||||
model.add(first_layer)
|
||||
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
# load the weights of the VGG16 networks
|
||||
# (trained on ImageNet, won the ILSVRC competition in 2014)
|
||||
# note: when there is a complete match between your model definition
|
||||
# and your weight savefile, you can simply call model.load_weights(filename)
|
||||
assert os.path.exists(weights_path), 'Model weights not found (see "weights_path" variable in script).'
|
||||
f = h5py.File(weights_path)
|
||||
for k in range(f.attrs['nb_layers']):
|
||||
if k >= len(model.layers):
|
||||
# we don't look at the last (fully-connected) layers in the savefile
|
||||
break
|
||||
g = f['layer_{}'.format(k)]
|
||||
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
|
||||
model.layers[k].set_weights(weights)
|
||||
f.close()
|
||||
# build the VGG16 network with ImageNet weights
|
||||
model = vgg16.VGG16(weights='imagenet', include_top=False)
|
||||
print('Model loaded.')
|
||||
|
||||
model.summary()
|
||||
|
||||
# this is the placeholder for the input images
|
||||
input_img = model.input
|
||||
|
||||
# get the symbolic outputs of each "key" layer (we gave them unique names).
|
||||
layer_dict = dict([(layer.name, layer) for layer in model.layers])
|
||||
layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]])
|
||||
|
||||
|
||||
def normalize(x):
|
||||
@@ -125,8 +67,11 @@ for filter_index in range(0, 200):
|
||||
|
||||
# we build a loss function that maximizes the activation
|
||||
# of the nth filter of the layer considered
|
||||
layer_output = layer_dict[layer_name].get_output()
|
||||
loss = K.mean(layer_output[:, filter_index, :, :])
|
||||
layer_output = layer_dict[layer_name].output
|
||||
if K.image_data_format() == 'channels_first':
|
||||
loss = K.mean(layer_output[:, filter_index, :, :])
|
||||
else:
|
||||
loss = K.mean(layer_output[:, :, :, filter_index])
|
||||
|
||||
# we compute the gradient of the input picture wrt this loss
|
||||
grads = K.gradients(loss, input_img)[0]
|
||||
@@ -141,7 +86,11 @@ for filter_index in range(0, 200):
|
||||
step = 1.
|
||||
|
||||
# we start from a gray image with some random noise
|
||||
input_img_data = np.random.random((1, 3, img_width, img_height)) * 20 + 128.
|
||||
if K.image_data_format() == 'channels_first':
|
||||
input_img_data = np.random.random((1, 3, img_width, img_height))
|
||||
else:
|
||||
input_img_data = np.random.random((1, img_width, img_height, 3))
|
||||
input_img_data = (input_img_data - 0.5) * 20 + 128
|
||||
|
||||
# we run gradient ascent for 20 steps
|
||||
for i in range(20):
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
""" This script demonstrates the use of a convolutional LSTM network.
|
||||
This network is used to predict the next frame of an artificially
|
||||
generated movie which contains moving squares.
|
||||
"""
|
||||
from keras.models import Sequential
|
||||
from keras.layers.convolutional import Conv3D
|
||||
from keras.layers.convolutional_recurrent import ConvLSTM2D
|
||||
from keras.layers.normalization import BatchNormalization
|
||||
import numpy as np
|
||||
import pylab as plt
|
||||
|
||||
# We create a layer which take as input movies of shape
|
||||
# (n_frames, width, height, channels) and returns a movie
|
||||
# of identical shape.
|
||||
|
||||
seq = Sequential()
|
||||
seq.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
|
||||
input_shape=(None, 40, 40, 1),
|
||||
padding='same', return_sequences=True))
|
||||
seq.add(BatchNormalization())
|
||||
|
||||
seq.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
|
||||
padding='same', return_sequences=True))
|
||||
seq.add(BatchNormalization())
|
||||
|
||||
seq.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
|
||||
padding='same', return_sequences=True))
|
||||
seq.add(BatchNormalization())
|
||||
|
||||
seq.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
|
||||
padding='same', return_sequences=True))
|
||||
seq.add(BatchNormalization())
|
||||
|
||||
seq.add(Conv3D(filters=1, kernel_size=(3, 3, 3),
|
||||
activation='sigmoid',
|
||||
padding='same', data_format='channels_last'))
|
||||
seq.compile(loss='binary_crossentropy', optimizer='adadelta')
|
||||
|
||||
|
||||
# Artificial data generation:
|
||||
# Generate movies with 3 to 7 moving squares inside.
|
||||
# The squares are of shape 1x1 or 2x2 pixels,
|
||||
# which move linearly over time.
|
||||
# For convenience we first create movies with bigger width and height (80x80)
|
||||
# and at the end we select a 40x40 window.
|
||||
|
||||
def generate_movies(n_samples=1200, n_frames=15):
|
||||
row = 80
|
||||
col = 80
|
||||
noisy_movies = np.zeros((n_samples, n_frames, row, col, 1), dtype=np.float)
|
||||
shifted_movies = np.zeros((n_samples, n_frames, row, col, 1),
|
||||
dtype=np.float)
|
||||
|
||||
for i in range(n_samples):
|
||||
# Add 3 to 7 moving squares
|
||||
n = np.random.randint(3, 8)
|
||||
|
||||
for j in range(n):
|
||||
# Initial position
|
||||
xstart = np.random.randint(20, 60)
|
||||
ystart = np.random.randint(20, 60)
|
||||
# Direction of motion
|
||||
directionx = np.random.randint(0, 3) - 1
|
||||
directiony = np.random.randint(0, 3) - 1
|
||||
|
||||
# Size of the square
|
||||
w = np.random.randint(2, 4)
|
||||
|
||||
for t in range(n_frames):
|
||||
x_shift = xstart + directionx * t
|
||||
y_shift = ystart + directiony * t
|
||||
noisy_movies[i, t, x_shift - w: x_shift + w,
|
||||
y_shift - w: y_shift + w, 0] += 1
|
||||
|
||||
# Make it more robust by adding noise.
|
||||
# The idea is that if during inference,
|
||||
# the value of the pixel is not exactly one,
|
||||
# we need to train the network to be robust and still
|
||||
# consider it as a pixel belonging to a square.
|
||||
if np.random.randint(0, 2):
|
||||
noise_f = (-1)**np.random.randint(0, 2)
|
||||
noisy_movies[i, t,
|
||||
x_shift - w - 1: x_shift + w + 1,
|
||||
y_shift - w - 1: y_shift + w + 1,
|
||||
0] += noise_f * 0.1
|
||||
|
||||
# Shift the ground truth by 1
|
||||
x_shift = xstart + directionx * (t + 1)
|
||||
y_shift = ystart + directiony * (t + 1)
|
||||
shifted_movies[i, t, x_shift - w: x_shift + w,
|
||||
y_shift - w: y_shift + w, 0] += 1
|
||||
|
||||
# Cut to a 40x40 window
|
||||
noisy_movies = noisy_movies[::, ::, 20:60, 20:60, ::]
|
||||
shifted_movies = shifted_movies[::, ::, 20:60, 20:60, ::]
|
||||
noisy_movies[noisy_movies >= 1] = 1
|
||||
shifted_movies[shifted_movies >= 1] = 1
|
||||
return noisy_movies, shifted_movies
|
||||
|
||||
# Train the network
|
||||
noisy_movies, shifted_movies = generate_movies(n_samples=1200)
|
||||
seq.fit(noisy_movies[:1000], shifted_movies[:1000], batch_size=10,
|
||||
epochs=300, validation_split=0.05)
|
||||
|
||||
# Testing the network on one movie
|
||||
# feed it with the first 7 positions and then
|
||||
# predict the new positions
|
||||
which = 1004
|
||||
track = noisy_movies[which][:7, ::, ::, ::]
|
||||
|
||||
for j in range(16):
|
||||
new_pos = seq.predict(track[np.newaxis, ::, ::, ::, ::])
|
||||
new = new_pos[::, -1, ::, ::, ::]
|
||||
track = np.concatenate((track, new), axis=0)
|
||||
|
||||
|
||||
# And then compare the predictions
|
||||
# to the ground truth
|
||||
track2 = noisy_movies[which][::, ::, ::, ::]
|
||||
for i in range(15):
|
||||
fig = plt.figure(figsize=(10, 5))
|
||||
|
||||
ax = fig.add_subplot(121)
|
||||
|
||||
if i >= 7:
|
||||
ax.text(1, 3, 'Predictions !', fontsize=20, color='w')
|
||||
else:
|
||||
ax.text(1, 3, 'Inital trajectory', fontsize=20)
|
||||
|
||||
toplot = track[i, ::, ::, 0]
|
||||
|
||||
plt.imshow(toplot)
|
||||
ax = fig.add_subplot(122)
|
||||
plt.text(1, 3, 'Ground truth', fontsize=20)
|
||||
|
||||
toplot = track2[i, ::, ::, 0]
|
||||
if i >= 2:
|
||||
toplot = shifted_movies[which][i - 1, ::, ::, 0]
|
||||
|
||||
plt.imshow(toplot)
|
||||
plt.savefig('%i_animate.png' % (i + 1))
|
||||
+141
-181
@@ -8,23 +8,15 @@ e.g.:
|
||||
```
|
||||
python deep_dream.py img/mypic.jpg results/dream
|
||||
```
|
||||
|
||||
It is preferrable to run this script on GPU, for speed.
|
||||
If running on CPU, prefer the TensorFlow backend (much faster).
|
||||
|
||||
Example results: http://i.imgur.com/FX6ROg9.jpg
|
||||
'''
|
||||
from __future__ import print_function
|
||||
from scipy.misc import imread, imresize, imsave
|
||||
import numpy as np
|
||||
from scipy.optimize import fmin_l_bfgs_b
|
||||
import time
|
||||
import argparse
|
||||
import h5py
|
||||
import os
|
||||
|
||||
from keras.models import Sequential
|
||||
from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D
|
||||
from keras.preprocessing.image import load_img, img_to_array
|
||||
import numpy as np
|
||||
import scipy
|
||||
import argparse
|
||||
|
||||
from keras.applications import inception_v3
|
||||
from keras import backend as K
|
||||
|
||||
parser = argparse.ArgumentParser(description='Deep Dreams with Keras.')
|
||||
@@ -37,199 +29,167 @@ args = parser.parse_args()
|
||||
base_image_path = args.base_image_path
|
||||
result_prefix = args.result_prefix
|
||||
|
||||
# dimensions of the generated picture.
|
||||
img_width = 600
|
||||
img_height = 600
|
||||
|
||||
# path to the model weights file.
|
||||
weights_path = 'vgg16_weights.h5'
|
||||
|
||||
# some settings we found interesting
|
||||
saved_settings = {
|
||||
'bad_trip': {'features': {'conv4_1': 0.05,
|
||||
'conv4_2': 0.01,
|
||||
'conv4_3': 0.01},
|
||||
'continuity': 0.1,
|
||||
'dream_l2': 0.8,
|
||||
'jitter': 5},
|
||||
'dreamy': {'features': {'conv5_1': 0.05,
|
||||
'conv5_2': 0.02},
|
||||
'continuity': 0.1,
|
||||
'dream_l2': 0.02,
|
||||
'jitter': 0},
|
||||
# These are the names of the layers
|
||||
# for which we try to maximize activation,
|
||||
# as well as their weight in the final loss
|
||||
# we try to maximize.
|
||||
# You can tweak these setting to obtain new visual effects.
|
||||
settings = {
|
||||
'features': {
|
||||
'mixed2': 0.2,
|
||||
'mixed3': 0.5,
|
||||
'mixed4': 2.,
|
||||
'mixed5': 1.5,
|
||||
},
|
||||
}
|
||||
# the settings we will use in this experiment
|
||||
settings = saved_settings['dreamy']
|
||||
|
||||
# util function to open, resize and format pictures into appropriate tensors
|
||||
|
||||
def preprocess_image(image_path):
|
||||
img = imresize(imread(image_path), (img_width, img_height))
|
||||
img = img.transpose((2, 0, 1)).astype('float64')
|
||||
# Util function to open, resize and format pictures
|
||||
# into appropriate tensors.
|
||||
img = load_img(image_path)
|
||||
img = img_to_array(img)
|
||||
img = np.expand_dims(img, axis=0)
|
||||
img = inception_v3.preprocess_input(img)
|
||||
return img
|
||||
|
||||
# util function to convert a tensor into a valid image
|
||||
|
||||
def deprocess_image(x):
|
||||
x = x.transpose((1, 2, 0))
|
||||
# Util function to convert a tensor into a valid image.
|
||||
if K.image_data_format() == 'channels_first':
|
||||
x = x.reshape((3, x.shape[2], x.shape[3]))
|
||||
x = x.transpose((1, 2, 0))
|
||||
else:
|
||||
x = x.reshape((x.shape[1], x.shape[2], 3))
|
||||
x /= 2.
|
||||
x += 0.5
|
||||
x *= 255.
|
||||
x = np.clip(x, 0, 255).astype('uint8')
|
||||
return x
|
||||
|
||||
# this will contain our generated image
|
||||
dream = K.placeholder((1, 3, img_width, img_height))
|
||||
K.set_learning_phase(0)
|
||||
|
||||
# build the VGG16 network with our dream as input
|
||||
first_layer = ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height))
|
||||
first_layer.input = dream
|
||||
|
||||
model = Sequential()
|
||||
model.add(first_layer)
|
||||
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
# load the weights of the VGG16 networks
|
||||
# (trained on ImageNet, won the ILSVRC competition in 2014)
|
||||
# note: when there is a complete match between your model definition
|
||||
# and your weight savefile, you can simply call model.load_weights(filename)
|
||||
assert os.path.exists(weights_path), 'Model weights not found (see "weights_path" variable in script).'
|
||||
f = h5py.File(weights_path)
|
||||
for k in range(f.attrs['nb_layers']):
|
||||
if k >= len(model.layers):
|
||||
# we don't look at the last (fully-connected) layers in the savefile
|
||||
break
|
||||
g = f['layer_{}'.format(k)]
|
||||
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
|
||||
model.layers[k].set_weights(weights)
|
||||
f.close()
|
||||
# Build the InceptionV3 network with our placeholder.
|
||||
# The model will be loaded with pre-trained ImageNet weights.
|
||||
model = inception_v3.InceptionV3(weights='imagenet',
|
||||
include_top=False)
|
||||
dream = model.input
|
||||
print('Model loaded.')
|
||||
|
||||
# get the symbolic outputs of each "key" layer (we gave them unique names).
|
||||
# Get the symbolic outputs of each "key" layer (we gave them unique names).
|
||||
layer_dict = dict([(layer.name, layer) for layer in model.layers])
|
||||
|
||||
# continuity loss util function
|
||||
def continuity_loss(x):
|
||||
assert K.ndim(x) == 4
|
||||
a = K.square(x[:, :, :img_width-1, :img_height-1] - x[:, :, 1:, :img_height-1])
|
||||
b = K.square(x[:, :, :img_width-1, :img_height-1] - x[:, :, :img_width-1, 1:])
|
||||
return K.sum(K.pow(a + b, 1.25))
|
||||
|
||||
# define the loss
|
||||
# Define the loss.
|
||||
loss = K.variable(0.)
|
||||
for layer_name in settings['features']:
|
||||
# add the L2 norm of the features of a layer to the loss
|
||||
# Add the L2 norm of the features of a layer to the loss.
|
||||
assert layer_name in layer_dict.keys(), 'Layer ' + layer_name + ' not found in model.'
|
||||
coeff = settings['features'][layer_name]
|
||||
x = layer_dict[layer_name].get_output()
|
||||
shape = layer_dict[layer_name].output_shape
|
||||
# we avoid border artifacts by only involving non-border pixels in the loss
|
||||
loss -= coeff * K.sum(K.square(x[:, :, 2: shape[2]-2, 2: shape[3]-2])) / np.prod(shape[1:])
|
||||
|
||||
# add continuity loss (gives image local coherence, can result in an artful blur)
|
||||
loss += settings['continuity'] * continuity_loss(dream) / (3 * img_width * img_height)
|
||||
# add image L2 norm to loss (prevents pixels from taking very high values, makes image darker)
|
||||
loss += settings['dream_l2'] * K.sum(K.square(dream)) / (3 * img_width * img_height)
|
||||
|
||||
# feel free to further modify the loss as you see fit, to achieve new effects...
|
||||
|
||||
# compute the gradients of the dream wrt the loss
|
||||
grads = K.gradients(loss, dream)
|
||||
|
||||
outputs = [loss]
|
||||
if type(grads) in {list, tuple}:
|
||||
outputs += grads
|
||||
else:
|
||||
outputs.append(grads)
|
||||
|
||||
f_outputs = K.function([dream], outputs)
|
||||
def eval_loss_and_grads(x):
|
||||
x = x.reshape((1, 3, img_width, img_height))
|
||||
outs = f_outputs([x])
|
||||
loss_value = outs[0]
|
||||
if len(outs[1:]) == 1:
|
||||
grad_values = outs[1].flatten().astype('float64')
|
||||
x = layer_dict[layer_name].output
|
||||
# We avoid border artifacts by only involving non-border pixels in the loss.
|
||||
scaling = K.prod(K.cast(K.shape(x), 'float32'))
|
||||
if K.image_data_format() == 'channels_first':
|
||||
loss += coeff * K.sum(K.square(x[:, :, 2: -2, 2: -2])) / scaling
|
||||
else:
|
||||
grad_values = np.array(outs[1:]).flatten().astype('float64')
|
||||
loss += coeff * K.sum(K.square(x[:, 2: -2, 2: -2, :])) / scaling
|
||||
|
||||
# Compute the gradients of the dream wrt the loss.
|
||||
grads = K.gradients(loss, dream)[0]
|
||||
# Normalize gradients.
|
||||
grads /= K.maximum(K.mean(K.abs(grads)), 1e-7)
|
||||
|
||||
# Set up function to retrieve the value
|
||||
# of the loss and gradients given an input image.
|
||||
outputs = [loss, grads]
|
||||
fetch_loss_and_grads = K.function([dream], outputs)
|
||||
|
||||
|
||||
def eval_loss_and_grads(x):
|
||||
outs = fetch_loss_and_grads([x])
|
||||
loss_value = outs[0]
|
||||
grad_values = outs[1]
|
||||
return loss_value, grad_values
|
||||
|
||||
# this Evaluator class makes it possible
|
||||
# to compute loss and gradients in one pass
|
||||
# while retrieving them via two separate functions,
|
||||
# "loss" and "grads". This is done because scipy.optimize
|
||||
# requires separate functions for loss and gradients,
|
||||
# but computing them separately would be inefficient.
|
||||
class Evaluator(object):
|
||||
def __init__(self):
|
||||
self.loss_value = None
|
||||
self.grads_values = None
|
||||
|
||||
def loss(self, x):
|
||||
assert self.loss_value is None
|
||||
def resize_img(img, size):
|
||||
img = np.copy(img)
|
||||
if K.image_data_format() == 'channels_first':
|
||||
factors = (1, 1,
|
||||
float(size[0]) / img.shape[2],
|
||||
float(size[1]) / img.shape[3])
|
||||
else:
|
||||
factors = (1,
|
||||
float(size[0]) / img.shape[1],
|
||||
float(size[1]) / img.shape[2],
|
||||
1)
|
||||
return scipy.ndimage.zoom(img, factors, order=1)
|
||||
|
||||
|
||||
def gradient_ascent(x, iterations, step, max_loss=None):
|
||||
for i in range(iterations):
|
||||
loss_value, grad_values = eval_loss_and_grads(x)
|
||||
self.loss_value = loss_value
|
||||
self.grad_values = grad_values
|
||||
return self.loss_value
|
||||
if max_loss is not None and loss_value > max_loss:
|
||||
break
|
||||
print('..Loss value at', i, ':', loss_value)
|
||||
x += step * grad_values
|
||||
return x
|
||||
|
||||
def grads(self, x):
|
||||
assert self.loss_value is not None
|
||||
grad_values = np.copy(self.grad_values)
|
||||
self.loss_value = None
|
||||
self.grad_values = None
|
||||
return grad_values
|
||||
|
||||
evaluator = Evaluator()
|
||||
def save_img(img, fname):
|
||||
pil_img = deprocess_image(np.copy(img))
|
||||
scipy.misc.imsave(fname, pil_img)
|
||||
|
||||
# run scipy-based optimization (L-BFGS) over the pixels of the generated image
|
||||
# so as to minimize the loss
|
||||
x = preprocess_image(base_image_path)
|
||||
for i in range(5):
|
||||
print('Start of iteration', i)
|
||||
start_time = time.time()
|
||||
|
||||
# add a random offset jitter to the initial image. This will be reverted at decoding time
|
||||
ox, oy = np.random.randint(-settings['jitter'], settings['jitter']+1, 2)
|
||||
x = np.roll(np.roll(x, ox, -1), oy, -2)
|
||||
"""Process:
|
||||
|
||||
# run L-BFGS for 7 steps
|
||||
x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(),
|
||||
fprime=evaluator.grads, maxfun=7)
|
||||
print('Current loss value:', min_val)
|
||||
# decode the dream and save it
|
||||
x = x.reshape((3, img_width, img_height))
|
||||
x = np.roll(np.roll(x, -ox, -1), -oy, -2) # unshift image
|
||||
img = deprocess_image(x)
|
||||
fname = result_prefix + '_at_iteration_%d.png' % i
|
||||
imsave(fname, img)
|
||||
end_time = time.time()
|
||||
print('Image saved as', fname)
|
||||
print('Iteration %d completed in %ds' % (i, end_time - start_time))
|
||||
- Load the original image.
|
||||
- Define a number of processing scales (i.e. image shapes),
|
||||
from smallest to largest.
|
||||
- Resize the original image to the smallest scale.
|
||||
- For every scale, starting with the smallest (i.e. current one):
|
||||
- Run gradient ascent
|
||||
- Upscale image to the next scale
|
||||
- Reinject the detail that was lost at upscaling time
|
||||
- Stop when we are back to the original size.
|
||||
|
||||
To obtain the detail lost during upscaling, we simply
|
||||
take the original image, shrink it down, upscale it,
|
||||
and compare the result to the (resized) original image.
|
||||
"""
|
||||
|
||||
|
||||
# Playing with these hyperparameters will also allow you to achieve new effects
|
||||
step = 0.01 # Gradient ascent step size
|
||||
num_octave = 3 # Number of scales at which to run gradient ascent
|
||||
octave_scale = 1.4 # Size ratio between scales
|
||||
iterations = 20 # Number of ascent steps per scale
|
||||
max_loss = 10.
|
||||
|
||||
img = preprocess_image(base_image_path)
|
||||
if K.image_data_format() == 'channels_first':
|
||||
original_shape = img.shape[2:]
|
||||
else:
|
||||
original_shape = img.shape[1:3]
|
||||
successive_shapes = [original_shape]
|
||||
for i in range(1, num_octave):
|
||||
shape = tuple([int(dim / (octave_scale ** i)) for dim in original_shape])
|
||||
successive_shapes.append(shape)
|
||||
successive_shapes = successive_shapes[::-1]
|
||||
original_img = np.copy(img)
|
||||
shrunk_original_img = resize_img(img, successive_shapes[0])
|
||||
|
||||
for shape in successive_shapes:
|
||||
print('Processing image shape', shape)
|
||||
img = resize_img(img, shape)
|
||||
img = gradient_ascent(img,
|
||||
iterations=iterations,
|
||||
step=step,
|
||||
max_loss=max_loss)
|
||||
upscaled_shrunk_original_img = resize_img(shrunk_original_img, shape)
|
||||
same_size_original = resize_img(original_img, shape)
|
||||
lost_detail = same_size_original - upscaled_shrunk_original_img
|
||||
|
||||
img += lost_detail
|
||||
shrunk_original_img = resize_img(original_img, shape)
|
||||
|
||||
save_img(img, fname=result_prefix + '.png')
|
||||
|
||||
@@ -0,0 +1,491 @@
|
||||
'''This example uses a convolutional stack followed by a recurrent stack
|
||||
and a CTC logloss function to perform optical character recognition
|
||||
of generated text images. I have no evidence of whether it actually
|
||||
learns general shapes of text, or just is able to recognize all
|
||||
the different fonts thrown at it...the purpose is more to demonstrate CTC
|
||||
inside of Keras. Note that the font list may need to be updated
|
||||
for the particular OS in use.
|
||||
|
||||
This starts off with 4 letter words. For the first 12 epochs, the
|
||||
difficulty is gradually increased using the TextImageGenerator class
|
||||
which is both a generator class for test/train data and a Keras
|
||||
callback class. After 20 epochs, longer sequences are thrown at it
|
||||
by recompiling the model to handle a wider image and rebuilding
|
||||
the word list to include two words separated by a space.
|
||||
|
||||
The table below shows normalized edit distance values. Theano uses
|
||||
a slightly different CTC implementation, hence the different results.
|
||||
|
||||
Norm. ED
|
||||
Epoch | TF | TH
|
||||
------------------------
|
||||
10 0.027 0.064
|
||||
15 0.038 0.035
|
||||
20 0.043 0.045
|
||||
25 0.014 0.019
|
||||
|
||||
This requires cairo and editdistance packages:
|
||||
pip install cairocffi
|
||||
pip install editdistance
|
||||
|
||||
Created by Mike Henry
|
||||
https://github.com/mbhenry/
|
||||
'''
|
||||
import os
|
||||
import itertools
|
||||
import re
|
||||
import datetime
|
||||
import cairocffi as cairo
|
||||
import editdistance
|
||||
import numpy as np
|
||||
from scipy import ndimage
|
||||
import pylab
|
||||
from keras import backend as K
|
||||
from keras.layers.convolutional import Conv2D, MaxPooling2D
|
||||
from keras.layers import Input, Dense, Activation
|
||||
from keras.layers import Reshape, Lambda
|
||||
from keras.layers.merge import add, concatenate
|
||||
from keras.models import Model
|
||||
from keras.layers.recurrent import GRU
|
||||
from keras.optimizers import SGD
|
||||
from keras.utils.data_utils import get_file
|
||||
from keras.preprocessing import image
|
||||
import keras.callbacks
|
||||
|
||||
|
||||
OUTPUT_DIR = 'image_ocr'
|
||||
|
||||
np.random.seed(55)
|
||||
|
||||
|
||||
# this creates larger "blotches" of noise which look
|
||||
# more realistic than just adding gaussian noise
|
||||
# assumes greyscale with pixels ranging from 0 to 1
|
||||
|
||||
def speckle(img):
|
||||
severity = np.random.uniform(0, 0.6)
|
||||
blur = ndimage.gaussian_filter(np.random.randn(*img.shape) * severity, 1)
|
||||
img_speck = (img + blur)
|
||||
img_speck[img_speck > 1] = 1
|
||||
img_speck[img_speck <= 0] = 0
|
||||
return img_speck
|
||||
|
||||
|
||||
# paints the string in a random location the bounding box
|
||||
# also uses a random font, a slight random rotation,
|
||||
# and a random amount of speckle noise
|
||||
|
||||
def paint_text(text, w, h, rotate=False, ud=False, multi_fonts=False):
|
||||
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
|
||||
with cairo.Context(surface) as context:
|
||||
context.set_source_rgb(1, 1, 1) # White
|
||||
context.paint()
|
||||
# this font list works in Centos 7
|
||||
if multi_fonts:
|
||||
fonts = ['Century Schoolbook', 'Courier', 'STIX', 'URW Chancery L', 'FreeMono']
|
||||
context.select_font_face(np.random.choice(fonts), cairo.FONT_SLANT_NORMAL,
|
||||
np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
|
||||
else:
|
||||
context.select_font_face('Courier', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
|
||||
context.set_font_size(25)
|
||||
box = context.text_extents(text)
|
||||
border_w_h = (4, 4)
|
||||
if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h - 2 * border_w_h[0]):
|
||||
raise IOError('Could not fit string into image. Max char count is too large for given image width.')
|
||||
|
||||
# teach the RNN translational invariance by
|
||||
# fitting text box randomly on canvas, with some room to rotate
|
||||
max_shift_x = w - box[2] - border_w_h[0]
|
||||
max_shift_y = h - box[3] - border_w_h[1]
|
||||
top_left_x = np.random.randint(0, int(max_shift_x))
|
||||
if ud:
|
||||
top_left_y = np.random.randint(0, int(max_shift_y))
|
||||
else:
|
||||
top_left_y = h // 2
|
||||
context.move_to(top_left_x - int(box[0]), top_left_y - int(box[1]))
|
||||
context.set_source_rgb(0, 0, 0)
|
||||
context.show_text(text)
|
||||
|
||||
buf = surface.get_data()
|
||||
a = np.frombuffer(buf, np.uint8)
|
||||
a.shape = (h, w, 4)
|
||||
a = a[:, :, 0] # grab single channel
|
||||
a = a.astype(np.float32) / 255
|
||||
a = np.expand_dims(a, 0)
|
||||
if rotate:
|
||||
a = image.random_rotation(a, 3 * (w - top_left_x) / w + 1)
|
||||
a = speckle(a)
|
||||
|
||||
return a
|
||||
|
||||
|
||||
def shuffle_mats_or_lists(matrix_list, stop_ind=None):
|
||||
ret = []
|
||||
assert all([len(i) == len(matrix_list[0]) for i in matrix_list])
|
||||
len_val = len(matrix_list[0])
|
||||
if stop_ind is None:
|
||||
stop_ind = len_val
|
||||
assert stop_ind <= len_val
|
||||
|
||||
a = list(range(stop_ind))
|
||||
np.random.shuffle(a)
|
||||
a += list(range(stop_ind, len_val))
|
||||
for mat in matrix_list:
|
||||
if isinstance(mat, np.ndarray):
|
||||
ret.append(mat[a])
|
||||
elif isinstance(mat, list):
|
||||
ret.append([mat[i] for i in a])
|
||||
else:
|
||||
raise TypeError('`shuffle_mats_or_lists` only supports '
|
||||
'numpy.array and list objects.')
|
||||
return ret
|
||||
|
||||
|
||||
def text_to_labels(text, num_classes):
|
||||
ret = []
|
||||
for char in text:
|
||||
if char >= 'a' and char <= 'z':
|
||||
ret.append(ord(char) - ord('a'))
|
||||
elif char == ' ':
|
||||
ret.append(26)
|
||||
return ret
|
||||
|
||||
|
||||
# only a-z and space..probably not to difficult
|
||||
# to expand to uppercase and symbols
|
||||
|
||||
def is_valid_str(in_str):
|
||||
search = re.compile(r'[^a-z\ ]').search
|
||||
return not bool(search(in_str))
|
||||
|
||||
|
||||
# Uses generator functions to supply train/test with
|
||||
# data. Image renderings are text are created on the fly
|
||||
# each time with random perturbations
|
||||
|
||||
class TextImageGenerator(keras.callbacks.Callback):
|
||||
|
||||
def __init__(self, monogram_file, bigram_file, minibatch_size,
|
||||
img_w, img_h, downsample_factor, val_split,
|
||||
absolute_max_string_len=16):
|
||||
|
||||
self.minibatch_size = minibatch_size
|
||||
self.img_w = img_w
|
||||
self.img_h = img_h
|
||||
self.monogram_file = monogram_file
|
||||
self.bigram_file = bigram_file
|
||||
self.downsample_factor = downsample_factor
|
||||
self.val_split = val_split
|
||||
self.blank_label = self.get_output_size() - 1
|
||||
self.absolute_max_string_len = absolute_max_string_len
|
||||
|
||||
def get_output_size(self):
|
||||
return 28
|
||||
|
||||
# num_words can be independent of the epoch size due to the use of generators
|
||||
# as max_string_len grows, num_words can grow
|
||||
def build_word_list(self, num_words, max_string_len=None, mono_fraction=0.5):
|
||||
assert max_string_len <= self.absolute_max_string_len
|
||||
assert num_words % self.minibatch_size == 0
|
||||
assert (self.val_split * num_words) % self.minibatch_size == 0
|
||||
self.num_words = num_words
|
||||
self.string_list = [''] * self.num_words
|
||||
tmp_string_list = []
|
||||
self.max_string_len = max_string_len
|
||||
self.Y_data = np.ones([self.num_words, self.absolute_max_string_len]) * -1
|
||||
self.X_text = []
|
||||
self.Y_len = [0] * self.num_words
|
||||
|
||||
# monogram file is sorted by frequency in english speech
|
||||
with open(self.monogram_file, 'rt') as f:
|
||||
for line in f:
|
||||
if len(tmp_string_list) == int(self.num_words * mono_fraction):
|
||||
break
|
||||
word = line.rstrip()
|
||||
if max_string_len == -1 or max_string_len is None or len(word) <= max_string_len:
|
||||
tmp_string_list.append(word)
|
||||
|
||||
# bigram file contains common word pairings in english speech
|
||||
with open(self.bigram_file, 'rt') as f:
|
||||
lines = f.readlines()
|
||||
for line in lines:
|
||||
if len(tmp_string_list) == self.num_words:
|
||||
break
|
||||
columns = line.lower().split()
|
||||
word = columns[0] + ' ' + columns[1]
|
||||
if is_valid_str(word) and \
|
||||
(max_string_len == -1 or max_string_len is None or len(word) <= max_string_len):
|
||||
tmp_string_list.append(word)
|
||||
if len(tmp_string_list) != self.num_words:
|
||||
raise IOError('Could not pull enough words from supplied monogram and bigram files. ')
|
||||
# interlace to mix up the easy and hard words
|
||||
self.string_list[::2] = tmp_string_list[:self.num_words // 2]
|
||||
self.string_list[1::2] = tmp_string_list[self.num_words // 2:]
|
||||
|
||||
for i, word in enumerate(self.string_list):
|
||||
self.Y_len[i] = len(word)
|
||||
self.Y_data[i, 0:len(word)] = text_to_labels(word, self.get_output_size())
|
||||
self.X_text.append(word)
|
||||
self.Y_len = np.expand_dims(np.array(self.Y_len), 1)
|
||||
|
||||
self.cur_val_index = self.val_split
|
||||
self.cur_train_index = 0
|
||||
|
||||
# each time an image is requested from train/val/test, a new random
|
||||
# painting of the text is performed
|
||||
def get_batch(self, index, size, train):
|
||||
# width and height are backwards from typical Keras convention
|
||||
# because width is the time dimension when it gets fed into the RNN
|
||||
if K.image_data_format() == 'channels_first':
|
||||
X_data = np.ones([size, 1, self.img_w, self.img_h])
|
||||
else:
|
||||
X_data = np.ones([size, self.img_w, self.img_h, 1])
|
||||
|
||||
labels = np.ones([size, self.absolute_max_string_len])
|
||||
input_length = np.zeros([size, 1])
|
||||
label_length = np.zeros([size, 1])
|
||||
source_str = []
|
||||
for i in range(0, size):
|
||||
# Mix in some blank inputs. This seems to be important for
|
||||
# achieving translational invariance
|
||||
if train and i > size - 4:
|
||||
if K.image_data_format() == 'channels_first':
|
||||
X_data[i, 0, 0:self.img_w, :] = self.paint_func('')[0, :, :].T
|
||||
else:
|
||||
X_data[i, 0:self.img_w, :, 0] = self.paint_func('',)[0, :, :].T
|
||||
labels[i, 0] = self.blank_label
|
||||
input_length[i] = self.img_w // self.downsample_factor - 2
|
||||
label_length[i] = 1
|
||||
source_str.append('')
|
||||
else:
|
||||
if K.image_data_format() == 'channels_first':
|
||||
X_data[i, 0, 0:self.img_w, :] = self.paint_func(self.X_text[index + i])[0, :, :].T
|
||||
else:
|
||||
X_data[i, 0:self.img_w, :, 0] = self.paint_func(self.X_text[index + i])[0, :, :].T
|
||||
labels[i, :] = self.Y_data[index + i]
|
||||
input_length[i] = self.img_w // self.downsample_factor - 2
|
||||
label_length[i] = self.Y_len[index + i]
|
||||
source_str.append(self.X_text[index + i])
|
||||
inputs = {'the_input': X_data,
|
||||
'the_labels': labels,
|
||||
'input_length': input_length,
|
||||
'label_length': label_length,
|
||||
'source_str': source_str # used for visualization only
|
||||
}
|
||||
outputs = {'ctc': np.zeros([size])} # dummy data for dummy loss function
|
||||
return (inputs, outputs)
|
||||
|
||||
def next_train(self):
|
||||
while 1:
|
||||
ret = self.get_batch(self.cur_train_index, self.minibatch_size, train=True)
|
||||
self.cur_train_index += self.minibatch_size
|
||||
if self.cur_train_index >= self.val_split:
|
||||
self.cur_train_index = self.cur_train_index % 32
|
||||
(self.X_text, self.Y_data, self.Y_len) = shuffle_mats_or_lists(
|
||||
[self.X_text, self.Y_data, self.Y_len], self.val_split)
|
||||
yield ret
|
||||
|
||||
def next_val(self):
|
||||
while 1:
|
||||
ret = self.get_batch(self.cur_val_index, self.minibatch_size, train=False)
|
||||
self.cur_val_index += self.minibatch_size
|
||||
if self.cur_val_index >= self.num_words:
|
||||
self.cur_val_index = self.val_split + self.cur_val_index % 32
|
||||
yield ret
|
||||
|
||||
def on_train_begin(self, logs={}):
|
||||
self.build_word_list(16000, 4, 1)
|
||||
self.paint_func = lambda text: paint_text(text, self.img_w, self.img_h,
|
||||
rotate=False, ud=False, multi_fonts=False)
|
||||
|
||||
def on_epoch_begin(self, epoch, logs={}):
|
||||
# rebind the paint function to implement curriculum learning
|
||||
if epoch >= 3 and epoch < 6:
|
||||
self.paint_func = lambda text: paint_text(text, self.img_w, self.img_h,
|
||||
rotate=False, ud=True, multi_fonts=False)
|
||||
elif epoch >= 6 and epoch < 9:
|
||||
self.paint_func = lambda text: paint_text(text, self.img_w, self.img_h,
|
||||
rotate=False, ud=True, multi_fonts=True)
|
||||
elif epoch >= 9:
|
||||
self.paint_func = lambda text: paint_text(text, self.img_w, self.img_h,
|
||||
rotate=True, ud=True, multi_fonts=True)
|
||||
if epoch >= 21 and self.max_string_len < 12:
|
||||
self.build_word_list(32000, 12, 0.5)
|
||||
|
||||
|
||||
# the actual loss calc occurs here despite it not being
|
||||
# an internal Keras loss function
|
||||
|
||||
def ctc_lambda_func(args):
|
||||
y_pred, labels, input_length, label_length = args
|
||||
# the 2 is critical here since the first couple outputs of the RNN
|
||||
# tend to be garbage:
|
||||
y_pred = y_pred[:, 2:, :]
|
||||
return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
|
||||
|
||||
|
||||
# For a real OCR application, this should be beam search with a dictionary
|
||||
# and language model. For this example, best path is sufficient.
|
||||
|
||||
def decode_batch(test_func, word_batch):
|
||||
out = test_func([word_batch])[0]
|
||||
ret = []
|
||||
for j in range(out.shape[0]):
|
||||
out_best = list(np.argmax(out[j, 2:], 1))
|
||||
out_best = [k for k, g in itertools.groupby(out_best)]
|
||||
# 26 is space, 27 is CTC blank char
|
||||
outstr = ''
|
||||
for c in out_best:
|
||||
if c >= 0 and c < 26:
|
||||
outstr += chr(c + ord('a'))
|
||||
elif c == 26:
|
||||
outstr += ' '
|
||||
ret.append(outstr)
|
||||
return ret
|
||||
|
||||
|
||||
class VizCallback(keras.callbacks.Callback):
|
||||
|
||||
def __init__(self, run_name, test_func, text_img_gen, num_display_words=6):
|
||||
self.test_func = test_func
|
||||
self.output_dir = os.path.join(
|
||||
OUTPUT_DIR, run_name)
|
||||
self.text_img_gen = text_img_gen
|
||||
self.num_display_words = num_display_words
|
||||
if not os.path.exists(self.output_dir):
|
||||
os.makedirs(self.output_dir)
|
||||
|
||||
def show_edit_distance(self, num):
|
||||
num_left = num
|
||||
mean_norm_ed = 0.0
|
||||
mean_ed = 0.0
|
||||
while num_left > 0:
|
||||
word_batch = next(self.text_img_gen)[0]
|
||||
num_proc = min(word_batch['the_input'].shape[0], num_left)
|
||||
decoded_res = decode_batch(self.test_func, word_batch['the_input'][0:num_proc])
|
||||
for j in range(0, num_proc):
|
||||
edit_dist = editdistance.eval(decoded_res[j], word_batch['source_str'][j])
|
||||
mean_ed += float(edit_dist)
|
||||
mean_norm_ed += float(edit_dist) / len(word_batch['source_str'][j])
|
||||
num_left -= num_proc
|
||||
mean_norm_ed = mean_norm_ed / num
|
||||
mean_ed = mean_ed / num
|
||||
print('\nOut of %d samples: Mean edit distance: %.3f Mean normalized edit distance: %0.3f'
|
||||
% (num, mean_ed, mean_norm_ed))
|
||||
|
||||
def on_epoch_end(self, epoch, logs={}):
|
||||
self.model.save_weights(os.path.join(self.output_dir, 'weights%02d.h5' % (epoch)))
|
||||
self.show_edit_distance(256)
|
||||
word_batch = next(self.text_img_gen)[0]
|
||||
res = decode_batch(self.test_func, word_batch['the_input'][0:self.num_display_words])
|
||||
if word_batch['the_input'][0].shape[0] < 256:
|
||||
cols = 2
|
||||
else:
|
||||
cols = 1
|
||||
for i in range(self.num_display_words):
|
||||
pylab.subplot(self.num_display_words // cols, cols, i + 1)
|
||||
if K.image_data_format() == 'channels_first':
|
||||
the_input = word_batch['the_input'][i, 0, :, :]
|
||||
else:
|
||||
the_input = word_batch['the_input'][i, :, :, 0]
|
||||
pylab.imshow(the_input.T, cmap='Greys_r')
|
||||
pylab.xlabel('Truth = \'%s\'\nDecoded = \'%s\'' % (word_batch['source_str'][i], res[i]))
|
||||
fig = pylab.gcf()
|
||||
fig.set_size_inches(10, 13)
|
||||
pylab.savefig(os.path.join(self.output_dir, 'e%02d.png' % (epoch)))
|
||||
pylab.close()
|
||||
|
||||
|
||||
def train(run_name, start_epoch, stop_epoch, img_w):
|
||||
# Input Parameters
|
||||
img_h = 64
|
||||
words_per_epoch = 16000
|
||||
val_split = 0.2
|
||||
val_words = int(words_per_epoch * (val_split))
|
||||
|
||||
# Network parameters
|
||||
conv_filters = 16
|
||||
kernel_size = (3, 3)
|
||||
pool_size = 2
|
||||
time_dense_size = 32
|
||||
rnn_size = 512
|
||||
|
||||
if K.image_data_format() == 'channels_first':
|
||||
input_shape = (1, img_w, img_h)
|
||||
else:
|
||||
input_shape = (img_w, img_h, 1)
|
||||
|
||||
fdir = os.path.dirname(get_file('wordlists.tgz',
|
||||
origin='http://www.mythic-ai.com/datasets/wordlists.tgz', untar=True))
|
||||
|
||||
img_gen = TextImageGenerator(monogram_file=os.path.join(fdir, 'wordlist_mono_clean.txt'),
|
||||
bigram_file=os.path.join(fdir, 'wordlist_bi_clean.txt'),
|
||||
minibatch_size=32,
|
||||
img_w=img_w,
|
||||
img_h=img_h,
|
||||
downsample_factor=(pool_size ** 2),
|
||||
val_split=words_per_epoch - val_words
|
||||
)
|
||||
act = 'relu'
|
||||
input_data = Input(name='the_input', shape=input_shape, dtype='float32')
|
||||
inner = Conv2D(conv_filters, kernel_size, padding='same',
|
||||
activation=act, kernel_initializer='he_normal',
|
||||
name='conv1')(input_data)
|
||||
inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max1')(inner)
|
||||
inner = Conv2D(conv_filters, kernel_size, padding='same',
|
||||
activation=act, kernel_initializer='he_normal',
|
||||
name='conv2')(inner)
|
||||
inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max2')(inner)
|
||||
|
||||
conv_to_rnn_dims = (img_w // (pool_size ** 2), (img_h // (pool_size ** 2)) * conv_filters)
|
||||
inner = Reshape(target_shape=conv_to_rnn_dims, name='reshape')(inner)
|
||||
|
||||
# cuts down input size going into RNN:
|
||||
inner = Dense(time_dense_size, activation=act, name='dense1')(inner)
|
||||
|
||||
# Two layers of bidirectional GRUs
|
||||
# GRU seems to work as well, if not better than LSTM:
|
||||
gru_1 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru1')(inner)
|
||||
gru_1b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru1_b')(inner)
|
||||
gru1_merged = add([gru_1, gru_1b])
|
||||
gru_2 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru2')(gru1_merged)
|
||||
gru_2b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru2_b')(gru1_merged)
|
||||
|
||||
# transforms RNN output to character activations:
|
||||
inner = Dense(img_gen.get_output_size(), kernel_initializer='he_normal',
|
||||
name='dense2')(concatenate([gru_2, gru_2b]))
|
||||
y_pred = Activation('softmax', name='softmax')(inner)
|
||||
Model(inputs=input_data, outputs=y_pred).summary()
|
||||
|
||||
labels = Input(name='the_labels', shape=[img_gen.absolute_max_string_len], dtype='float32')
|
||||
input_length = Input(name='input_length', shape=[1], dtype='int64')
|
||||
label_length = Input(name='label_length', shape=[1], dtype='int64')
|
||||
# Keras doesn't currently support loss funcs with extra parameters
|
||||
# so CTC loss is implemented in a lambda layer
|
||||
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length])
|
||||
|
||||
# clipnorm seems to speeds up convergence
|
||||
sgd = SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)
|
||||
|
||||
model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)
|
||||
|
||||
# the loss calc occurs elsewhere, so use a dummy lambda func for the loss
|
||||
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd)
|
||||
if start_epoch > 0:
|
||||
weight_file = os.path.join(OUTPUT_DIR, os.path.join(run_name, 'weights%02d.h5' % (start_epoch - 1)))
|
||||
model.load_weights(weight_file)
|
||||
# captures output of softmax so we can decode the output during visualization
|
||||
test_func = K.function([input_data], [y_pred])
|
||||
|
||||
viz_cb = VizCallback(run_name, test_func, img_gen.next_val())
|
||||
|
||||
model.fit_generator(generator=img_gen.next_train(), steps_per_epoch=(words_per_epoch - val_words),
|
||||
epochs=stop_epoch, validation_data=img_gen.next_val(), validation_steps=val_words,
|
||||
callbacks=[viz_cb, img_gen], initial_epoch=start_epoch)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_name = datetime.datetime.now().strftime('%Y:%m:%d:%H:%M:%S')
|
||||
train(run_name, 0, 20, 128)
|
||||
# increase to wider images and start at epoch 20. The learned weights are reloaded
|
||||
train(run_name, 20, 25, 512)
|
||||
@@ -1,62 +1,48 @@
|
||||
'''Train a Bidirectional LSTM on the IMDB sentiment classification task.
|
||||
|
||||
GPU command:
|
||||
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python imdb_bidirectional_lstm.py
|
||||
|
||||
Output after 4 epochs on CPU: ~0.8146
|
||||
Time per epoch on CPU (Core i7): ~150s.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
from keras.preprocessing import sequence
|
||||
from keras.utils.np_utils import accuracy
|
||||
from keras.models import Graph
|
||||
from keras.layers.core import Dense, Dropout
|
||||
from keras.layers.embeddings import Embedding
|
||||
from keras.layers.recurrent import LSTM
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional
|
||||
from keras.datasets import imdb
|
||||
|
||||
|
||||
max_features = 20000
|
||||
maxlen = 100 # cut texts after this number of words (among top max_features most common words)
|
||||
# cut texts after this number of words
|
||||
# (among top max_features most common words)
|
||||
maxlen = 100
|
||||
batch_size = 32
|
||||
|
||||
print('Loading data...')
|
||||
(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features,
|
||||
test_split=0.2)
|
||||
print(len(X_train), 'train sequences')
|
||||
print(len(X_test), 'test sequences')
|
||||
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
|
||||
print(len(x_train), 'train sequences')
|
||||
print(len(x_test), 'test sequences')
|
||||
|
||||
print("Pad sequences (samples x time)")
|
||||
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
|
||||
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
|
||||
print('X_train shape:', X_train.shape)
|
||||
print('X_test shape:', X_test.shape)
|
||||
print('Pad sequences (samples x time)')
|
||||
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
|
||||
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
|
||||
print('x_train shape:', x_train.shape)
|
||||
print('x_test shape:', x_test.shape)
|
||||
y_train = np.array(y_train)
|
||||
y_test = np.array(y_test)
|
||||
|
||||
print('Build model...')
|
||||
model = Graph()
|
||||
model.add_input(name='input', input_shape=(maxlen,), dtype=int)
|
||||
model.add_node(Embedding(max_features, 128, input_length=maxlen),
|
||||
name='embedding', input='input')
|
||||
model.add_node(LSTM(64), name='forward', input='embedding')
|
||||
model.add_node(LSTM(64, go_backwards=True), name='backward', input='embedding')
|
||||
model.add_node(Dropout(0.5), name='dropout', inputs=['forward', 'backward'])
|
||||
model.add_node(Dense(1, activation='sigmoid'), name='sigmoid', input='dropout')
|
||||
model.add_output(name='output', input='sigmoid')
|
||||
model = Sequential()
|
||||
model.add(Embedding(max_features, 128, input_length=maxlen))
|
||||
model.add(Bidirectional(LSTM(64)))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(1, activation='sigmoid'))
|
||||
|
||||
# try using different optimizers and different optimizer configs
|
||||
model.compile('adam', {'output': 'binary_crossentropy'})
|
||||
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
|
||||
|
||||
print('Train...')
|
||||
model.fit({'input': X_train, 'output': y_train},
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
nb_epoch=4, show_accuracy=True)
|
||||
acc = accuracy(y_test,
|
||||
np.round(np.array(model.predict({'input': X_test},
|
||||
batch_size=batch_size)['output'])))
|
||||
print('Test accuracy:', acc)
|
||||
epochs=4,
|
||||
validation_data=[x_test, y_test])
|
||||
|
||||
+37
-40
@@ -1,69 +1,64 @@
|
||||
'''This example demonstrates the use of Convolution1D for text classification.
|
||||
|
||||
Run on GPU: THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python imdb_cnn.py
|
||||
Gets to 0.89 test accuracy after 2 epochs.
|
||||
90s/epoch on Intel i5 2.4Ghz CPU.
|
||||
10s/epoch on Tesla K40 GPU.
|
||||
|
||||
Get to 0.835 test accuracy after 2 epochs. 100s/epoch on K520 GPU.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
from keras.preprocessing import sequence
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Dropout, Activation, Flatten
|
||||
from keras.layers.embeddings import Embedding
|
||||
from keras.layers.convolutional import Convolution1D, MaxPooling1D
|
||||
from keras.layers import Dense, Dropout, Activation
|
||||
from keras.layers import Embedding
|
||||
from keras.layers import Conv1D, GlobalMaxPooling1D
|
||||
from keras.datasets import imdb
|
||||
|
||||
|
||||
# set parameters:
|
||||
max_features = 5000
|
||||
maxlen = 100
|
||||
maxlen = 400
|
||||
batch_size = 32
|
||||
embedding_dims = 100
|
||||
nb_filter = 250
|
||||
filter_length = 3
|
||||
embedding_dims = 50
|
||||
filters = 250
|
||||
kernel_size = 3
|
||||
hidden_dims = 250
|
||||
nb_epoch = 2
|
||||
epochs = 2
|
||||
|
||||
print('Loading data...')
|
||||
(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features,
|
||||
test_split=0.2)
|
||||
print(len(X_train), 'train sequences')
|
||||
print(len(X_test), 'test sequences')
|
||||
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
|
||||
print(len(x_train), 'train sequences')
|
||||
print(len(x_test), 'test sequences')
|
||||
|
||||
print('Pad sequences (samples x time)')
|
||||
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
|
||||
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
|
||||
print('X_train shape:', X_train.shape)
|
||||
print('X_test shape:', X_test.shape)
|
||||
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
|
||||
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
|
||||
print('x_train shape:', x_train.shape)
|
||||
print('x_test shape:', x_test.shape)
|
||||
|
||||
print('Build model...')
|
||||
model = Sequential()
|
||||
|
||||
# we start off with an efficient embedding layer which maps
|
||||
# our vocab indices into embedding_dims dimensions
|
||||
model.add(Embedding(max_features, embedding_dims, input_length=maxlen))
|
||||
model.add(Dropout(0.25))
|
||||
model.add(Embedding(max_features,
|
||||
embedding_dims,
|
||||
input_length=maxlen))
|
||||
model.add(Dropout(0.2))
|
||||
|
||||
# we add a Convolution1D, which will learn nb_filter
|
||||
# we add a Convolution1D, which will learn filters
|
||||
# word group filters of size filter_length:
|
||||
model.add(Convolution1D(nb_filter=nb_filter,
|
||||
filter_length=filter_length,
|
||||
border_mode='valid',
|
||||
activation='relu',
|
||||
subsample_length=1))
|
||||
# we use standard max pooling (halving the output of the previous layer):
|
||||
model.add(MaxPooling1D(pool_length=2))
|
||||
|
||||
# We flatten the output of the conv layer,
|
||||
# so that we can add a vanilla dense layer:
|
||||
model.add(Flatten())
|
||||
model.add(Conv1D(filters,
|
||||
kernel_size,
|
||||
padding='valid',
|
||||
activation='relu',
|
||||
strides=1))
|
||||
# we use max pooling:
|
||||
model.add(GlobalMaxPooling1D())
|
||||
|
||||
# We add a vanilla hidden layer:
|
||||
model.add(Dense(hidden_dims))
|
||||
model.add(Dropout(0.25))
|
||||
model.add(Dropout(0.2))
|
||||
model.add(Activation('relu'))
|
||||
|
||||
# We project onto a single unit output layer, and squash it with a sigmoid:
|
||||
@@ -71,7 +66,9 @@ model.add(Dense(1))
|
||||
model.add(Activation('sigmoid'))
|
||||
|
||||
model.compile(loss='binary_crossentropy',
|
||||
optimizer='rmsprop')
|
||||
model.fit(X_train, y_train, batch_size=batch_size,
|
||||
nb_epoch=nb_epoch, show_accuracy=True,
|
||||
validation_data=(X_test, y_test))
|
||||
optimizer='adam',
|
||||
metrics=['accuracy'])
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
validation_data=(x_test, y_test))
|
||||
|
||||
+28
-34
@@ -1,41 +1,34 @@
|
||||
'''Train a recurrent convolutional network on the IMDB sentiment
|
||||
classification task.
|
||||
|
||||
GPU command:
|
||||
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python imdb_cnn_lstm.py
|
||||
|
||||
Get to 0.8498 test accuracy after 2 epochs. 41s/epoch on K520 GPU.
|
||||
Gets to 0.8498 test accuracy after 2 epochs. 41s/epoch on K520 GPU.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
from keras.preprocessing import sequence
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Dropout, Activation
|
||||
from keras.layers.embeddings import Embedding
|
||||
from keras.layers.recurrent import LSTM, GRU, SimpleRNN
|
||||
from keras.layers.convolutional import Convolution1D, MaxPooling1D
|
||||
from keras.layers import Dense, Dropout, Activation
|
||||
from keras.layers import Embedding
|
||||
from keras.layers import LSTM
|
||||
from keras.layers import Conv1D, MaxPooling1D
|
||||
from keras.datasets import imdb
|
||||
|
||||
|
||||
# Embedding
|
||||
max_features = 20000
|
||||
maxlen = 100
|
||||
embedding_size = 128
|
||||
|
||||
# Convolution
|
||||
filter_length = 3
|
||||
nb_filter = 64
|
||||
pool_length = 2
|
||||
kernel_size = 5
|
||||
filters = 64
|
||||
pool_size = 4
|
||||
|
||||
# LSTM
|
||||
lstm_output_size = 70
|
||||
|
||||
# Training
|
||||
batch_size = 30
|
||||
nb_epoch = 2
|
||||
epochs = 2
|
||||
|
||||
'''
|
||||
Note:
|
||||
@@ -44,39 +37,40 @@ Only 2 epochs are needed as the dataset is very small.
|
||||
'''
|
||||
|
||||
print('Loading data...')
|
||||
(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features, test_split=0.2)
|
||||
print(len(X_train), 'train sequences')
|
||||
print(len(X_test), 'test sequences')
|
||||
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
|
||||
print(len(x_train), 'train sequences')
|
||||
print(len(x_test), 'test sequences')
|
||||
|
||||
print('Pad sequences (samples x time)')
|
||||
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
|
||||
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
|
||||
print('X_train shape:', X_train.shape)
|
||||
print('X_test shape:', X_test.shape)
|
||||
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
|
||||
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
|
||||
print('x_train shape:', x_train.shape)
|
||||
print('x_test shape:', x_test.shape)
|
||||
|
||||
print('Build model...')
|
||||
|
||||
model = Sequential()
|
||||
model.add(Embedding(max_features, embedding_size, input_length=maxlen))
|
||||
model.add(Dropout(0.25))
|
||||
model.add(Convolution1D(nb_filter=nb_filter,
|
||||
filter_length=filter_length,
|
||||
border_mode='valid',
|
||||
activation='relu',
|
||||
subsample_length=1))
|
||||
model.add(MaxPooling1D(pool_length=pool_length))
|
||||
model.add(Conv1D(filters,
|
||||
kernel_size,
|
||||
padding='valid',
|
||||
activation='relu',
|
||||
strides=1))
|
||||
model.add(MaxPooling1D(pool_size=pool_size))
|
||||
model.add(LSTM(lstm_output_size))
|
||||
model.add(Dense(1))
|
||||
model.add(Activation('sigmoid'))
|
||||
|
||||
model.compile(loss='binary_crossentropy',
|
||||
optimizer='adam',
|
||||
class_mode='binary')
|
||||
metrics=['accuracy'])
|
||||
|
||||
print('Train...')
|
||||
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch,
|
||||
validation_data=(X_test, y_test), show_accuracy=True)
|
||||
score, acc = model.evaluate(X_test, y_test, batch_size=batch_size,
|
||||
show_accuracy=True)
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
validation_data=(x_test, y_test))
|
||||
score, acc = model.evaluate(x_test, y_test, batch_size=batch_size)
|
||||
print('Test score:', score)
|
||||
print('Test accuracy:', acc)
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
'''This example demonstrates the use of fasttext for text classification
|
||||
|
||||
Based on Joulin et al's paper:
|
||||
|
||||
Bags of Tricks for Efficient Text Classification
|
||||
https://arxiv.org/abs/1607.01759
|
||||
|
||||
Results on IMDB datasets with uni and bi-gram embeddings:
|
||||
Uni-gram: 0.8813 test accuracy after 5 epochs. 8s/epoch on i7 cpu.
|
||||
Bi-gram : 0.9056 test accuracy after 5 epochs. 2s/epoch on GTx 980M gpu.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
|
||||
from keras.preprocessing import sequence
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense
|
||||
from keras.layers import Embedding
|
||||
from keras.layers import GlobalAveragePooling1D
|
||||
from keras.datasets import imdb
|
||||
|
||||
|
||||
def create_ngram_set(input_list, ngram_value=2):
|
||||
"""
|
||||
Extract a set of n-grams from a list of integers.
|
||||
|
||||
>>> create_ngram_set([1, 4, 9, 4, 1, 4], ngram_value=2)
|
||||
{(4, 9), (4, 1), (1, 4), (9, 4)}
|
||||
|
||||
>>> create_ngram_set([1, 4, 9, 4, 1, 4], ngram_value=3)
|
||||
[(1, 4, 9), (4, 9, 4), (9, 4, 1), (4, 1, 4)]
|
||||
"""
|
||||
return set(zip(*[input_list[i:] for i in range(ngram_value)]))
|
||||
|
||||
|
||||
def add_ngram(sequences, token_indice, ngram_range=2):
|
||||
"""
|
||||
Augment the input list of list (sequences) by appending n-grams values.
|
||||
|
||||
Example: adding bi-gram
|
||||
>>> sequences = [[1, 3, 4, 5], [1, 3, 7, 9, 2]]
|
||||
>>> token_indice = {(1, 3): 1337, (9, 2): 42, (4, 5): 2017}
|
||||
>>> add_ngram(sequences, token_indice, ngram_range=2)
|
||||
[[1, 3, 4, 5, 1337, 2017], [1, 3, 7, 9, 2, 1337, 42]]
|
||||
|
||||
Example: adding tri-gram
|
||||
>>> sequences = [[1, 3, 4, 5], [1, 3, 7, 9, 2]]
|
||||
>>> token_indice = {(1, 3): 1337, (9, 2): 42, (4, 5): 2017, (7, 9, 2): 2018}
|
||||
>>> add_ngram(sequences, token_indice, ngram_range=3)
|
||||
[[1, 3, 4, 5, 1337], [1, 3, 7, 9, 2, 1337, 2018]]
|
||||
"""
|
||||
new_sequences = []
|
||||
for input_list in sequences:
|
||||
new_list = input_list[:]
|
||||
for i in range(len(new_list) - ngram_range + 1):
|
||||
for ngram_value in range(2, ngram_range + 1):
|
||||
ngram = tuple(new_list[i:i + ngram_value])
|
||||
if ngram in token_indice:
|
||||
new_list.append(token_indice[ngram])
|
||||
new_sequences.append(new_list)
|
||||
|
||||
return new_sequences
|
||||
|
||||
# Set parameters:
|
||||
# ngram_range = 2 will add bi-grams features
|
||||
ngram_range = 1
|
||||
max_features = 20000
|
||||
maxlen = 400
|
||||
batch_size = 32
|
||||
embedding_dims = 50
|
||||
epochs = 5
|
||||
|
||||
print('Loading data...')
|
||||
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
|
||||
print(len(x_train), 'train sequences')
|
||||
print(len(x_test), 'test sequences')
|
||||
print('Average train sequence length: {}'.format(np.mean(list(map(len, x_train)), dtype=int)))
|
||||
print('Average test sequence length: {}'.format(np.mean(list(map(len, x_test)), dtype=int)))
|
||||
|
||||
if ngram_range > 1:
|
||||
print('Adding {}-gram features'.format(ngram_range))
|
||||
# Create set of unique n-gram from the training set.
|
||||
ngram_set = set()
|
||||
for input_list in x_train:
|
||||
for i in range(2, ngram_range + 1):
|
||||
set_of_ngram = create_ngram_set(input_list, ngram_value=i)
|
||||
ngram_set.update(set_of_ngram)
|
||||
|
||||
# Dictionary mapping n-gram token to a unique integer.
|
||||
# Integer values are greater than max_features in order
|
||||
# to avoid collision with existing features.
|
||||
start_index = max_features + 1
|
||||
token_indice = {v: k + start_index for k, v in enumerate(ngram_set)}
|
||||
indice_token = {token_indice[k]: k for k in token_indice}
|
||||
|
||||
# max_features is the highest integer that could be found in the dataset.
|
||||
max_features = np.max(list(indice_token.keys())) + 1
|
||||
|
||||
# Augmenting x_train and x_test with n-grams features
|
||||
x_train = add_ngram(x_train, token_indice, ngram_range)
|
||||
x_test = add_ngram(x_test, token_indice, ngram_range)
|
||||
print('Average train sequence length: {}'.format(np.mean(list(map(len, x_train)), dtype=int)))
|
||||
print('Average test sequence length: {}'.format(np.mean(list(map(len, x_test)), dtype=int)))
|
||||
|
||||
print('Pad sequences (samples x time)')
|
||||
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
|
||||
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
|
||||
print('x_train shape:', x_train.shape)
|
||||
print('x_test shape:', x_test.shape)
|
||||
|
||||
print('Build model...')
|
||||
model = Sequential()
|
||||
|
||||
# we start off with an efficient embedding layer which maps
|
||||
# our vocab indices into embedding_dims dimensions
|
||||
model.add(Embedding(max_features,
|
||||
embedding_dims,
|
||||
input_length=maxlen))
|
||||
|
||||
# we add a GlobalAveragePooling1D, which will average the embeddings
|
||||
# of all words in the document
|
||||
model.add(GlobalAveragePooling1D())
|
||||
|
||||
# We project onto a single unit output layer, and squash it with a sigmoid:
|
||||
model.add(Dense(1, activation='sigmoid'))
|
||||
|
||||
model.compile(loss='binary_crossentropy',
|
||||
optimizer='adam',
|
||||
metrics=['accuracy'])
|
||||
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
validation_data=(x_test, y_test))
|
||||
+23
-34
@@ -1,8 +1,6 @@
|
||||
'''Train a LSTM on the IMDB sentiment classification task.
|
||||
|
||||
'''Trains a LSTM on the IMDB sentiment classification task.
|
||||
The dataset is actually too small for LSTM to be of any advantage
|
||||
compared to simpler, much faster methods such as TF-IDF+LogReg.
|
||||
|
||||
compared to simpler, much faster methods such as TF-IDF + LogReg.
|
||||
Notes:
|
||||
|
||||
- RNNs are tricky. Choice of batch size is important,
|
||||
@@ -11,56 +9,47 @@ Some configurations won't converge.
|
||||
|
||||
- LSTM loss decrease patterns during training can be quite different
|
||||
from what you see with CNNs/MLPs/etc.
|
||||
|
||||
GPU command:
|
||||
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python imdb_lstm.py
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
from keras.preprocessing import sequence
|
||||
from keras.utils import np_utils
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Dropout, Activation
|
||||
from keras.layers.embeddings import Embedding
|
||||
from keras.layers.recurrent import LSTM
|
||||
from keras.layers import Dense, Embedding
|
||||
from keras.layers import LSTM
|
||||
from keras.datasets import imdb
|
||||
|
||||
max_features = 20000
|
||||
maxlen = 100 # cut texts after this number of words (among top max_features most common words)
|
||||
maxlen = 80 # cut texts after this number of words (among top max_features most common words)
|
||||
batch_size = 32
|
||||
|
||||
print('Loading data...')
|
||||
(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features,
|
||||
test_split=0.2)
|
||||
print(len(X_train), 'train sequences')
|
||||
print(len(X_test), 'test sequences')
|
||||
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
|
||||
print(len(x_train), 'train sequences')
|
||||
print(len(x_test), 'test sequences')
|
||||
|
||||
print('Pad sequences (samples x time)')
|
||||
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
|
||||
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
|
||||
print('X_train shape:', X_train.shape)
|
||||
print('X_test shape:', X_test.shape)
|
||||
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
|
||||
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
|
||||
print('x_train shape:', x_train.shape)
|
||||
print('x_test shape:', x_test.shape)
|
||||
|
||||
print('Build model...')
|
||||
model = Sequential()
|
||||
model.add(Embedding(max_features, 128, input_length=maxlen, dropout=0.5))
|
||||
model.add(LSTM(128, dropout_W=0.5, dropout_U=0.1)) # try using a GRU instead, for fun
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(1))
|
||||
model.add(Activation('sigmoid'))
|
||||
model.add(Embedding(max_features, 128))
|
||||
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
|
||||
model.add(Dense(1, activation='sigmoid'))
|
||||
|
||||
# try using different optimizers and different optimizer configs
|
||||
model.compile(loss='binary_crossentropy',
|
||||
optimizer='adam')
|
||||
optimizer='adam',
|
||||
metrics=['accuracy'])
|
||||
|
||||
print('Train...')
|
||||
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=15,
|
||||
validation_data=(X_test, y_test), show_accuracy=True)
|
||||
score, acc = model.evaluate(X_test, y_test,
|
||||
batch_size=batch_size,
|
||||
show_accuracy=True)
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=15,
|
||||
validation_data=(x_test, y_test))
|
||||
score, acc = model.evaluate(x_test, y_test,
|
||||
batch_size=batch_size)
|
||||
print('Test score:', score)
|
||||
print('Test accuracy:', acc)
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
'''This demonstrates how to reach a score of 0.4890 (local validation)
|
||||
on the Kaggle Otto challenge, with a deep net using Keras.
|
||||
|
||||
Requires Scikit-Learn and Pandas.
|
||||
|
||||
Recommended to run on GPU:
|
||||
Command: THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python kaggle_otto_nn.py
|
||||
On EC2 g2.2xlarge instance: 19s/epoch. 6-7 minutes total training time.
|
||||
|
||||
Best validation score at epoch 21: 0.4881
|
||||
|
||||
Try it at home:
|
||||
- with/without BatchNormalization (BatchNormalization helps!)
|
||||
- with ReLU or with PReLU (PReLU helps!)
|
||||
- with smaller layers, largers layers
|
||||
- with more layers, less layers
|
||||
- with different optimizers (SGD+momentum+decay is probably better than Adam!)
|
||||
|
||||
Get the data from Kaggle:
|
||||
https://www.kaggle.com/c/otto-group-product-classification-challenge/data
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Dropout, Activation
|
||||
from keras.layers.normalization import BatchNormalization
|
||||
from keras.layers.advanced_activations import PReLU
|
||||
from keras.utils import np_utils, generic_utils
|
||||
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
|
||||
def load_data(path, train=True):
|
||||
df = pd.read_csv(path)
|
||||
X = df.values.copy()
|
||||
if train:
|
||||
np.random.shuffle(X) # https://youtu.be/uyUXoap67N8
|
||||
X, labels = X[:, 1:-1].astype(np.float32), X[:, -1]
|
||||
return X, labels
|
||||
else:
|
||||
X, ids = X[:, 1:].astype(np.float32), X[:, 0].astype(str)
|
||||
return X, ids
|
||||
|
||||
|
||||
def preprocess_data(X, scaler=None):
|
||||
if not scaler:
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X)
|
||||
X = scaler.transform(X)
|
||||
return X, scaler
|
||||
|
||||
|
||||
def preprocess_labels(labels, encoder=None, categorical=True):
|
||||
if not encoder:
|
||||
encoder = LabelEncoder()
|
||||
encoder.fit(labels)
|
||||
y = encoder.transform(labels).astype(np.int32)
|
||||
if categorical:
|
||||
y = np_utils.to_categorical(y)
|
||||
return y, encoder
|
||||
|
||||
|
||||
def make_submission(y_prob, ids, encoder, fname):
|
||||
with open(fname, 'w') as f:
|
||||
f.write('id,')
|
||||
f.write(','.join([str(i) for i in encoder.classes_]))
|
||||
f.write('\n')
|
||||
for i, probs in zip(ids, y_prob):
|
||||
probas = ','.join([i] + [str(p) for p in probs.tolist()])
|
||||
f.write(probas)
|
||||
f.write('\n')
|
||||
print('Wrote submission to file {}.'.format(fname))
|
||||
|
||||
print('Loading data...')
|
||||
X, labels = load_data('train.csv', train=True)
|
||||
X, scaler = preprocess_data(X)
|
||||
y, encoder = preprocess_labels(labels)
|
||||
|
||||
X_test, ids = load_data('test.csv', train=False)
|
||||
X_test, _ = preprocess_data(X_test, scaler)
|
||||
|
||||
nb_classes = y.shape[1]
|
||||
print(nb_classes, 'classes')
|
||||
|
||||
dims = X.shape[1]
|
||||
print(dims, 'dims')
|
||||
|
||||
print('Building model...')
|
||||
|
||||
model = Sequential()
|
||||
model.add(Dense(512, input_shape=(dims,)))
|
||||
model.add(PReLU())
|
||||
model.add(BatchNormalization())
|
||||
model.add(Dropout(0.5))
|
||||
|
||||
model.add(Dense(512))
|
||||
model.add(PReLU())
|
||||
model.add(BatchNormalization())
|
||||
model.add(Dropout(0.5))
|
||||
|
||||
model.add(Dense(512))
|
||||
model.add(PReLU())
|
||||
model.add(BatchNormalization())
|
||||
model.add(Dropout(0.5))
|
||||
|
||||
model.add(Dense(nb_classes))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
model.compile(loss='categorical_crossentropy', optimizer='adam')
|
||||
|
||||
print('Training model...')
|
||||
model.fit(X, y, nb_epoch=20, batch_size=128, validation_split=0.15)
|
||||
|
||||
print('Generating submission...')
|
||||
proba = model.predict_proba(X_test)
|
||||
make_submission(proba, ids, encoder, fname='keras-otto.csv')
|
||||
@@ -0,0 +1,88 @@
|
||||
'''Compare LSTM implementations on the IMDB sentiment classification task.
|
||||
|
||||
implementation=0 preprocesses input to the LSTM which typically results in
|
||||
faster computations at the expense of increased peak memory usage as the
|
||||
preprocessed input must be kept in memory.
|
||||
|
||||
implementation=1 does away with the preprocessing, meaning that it might take
|
||||
a little longer, but should require less peak memory.
|
||||
|
||||
implementation=2 concatenates the input, output and forget gate's weights
|
||||
into one, large matrix, resulting in faster computation time as the GPU can
|
||||
utilize more cores, at the expense of reduced regularization because the same
|
||||
dropout is shared across the gates.
|
||||
|
||||
Note that the relative performance of the different implementations can
|
||||
vary depending on your device, your model and the size of your data.
|
||||
'''
|
||||
|
||||
import time
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from keras.preprocessing import sequence
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Embedding, Dense, LSTM, Dropout
|
||||
from keras.datasets import imdb
|
||||
|
||||
max_features = 20000
|
||||
max_length = 80
|
||||
embedding_dim = 256
|
||||
batch_size = 128
|
||||
epochs = 10
|
||||
modes = [0, 1, 2]
|
||||
|
||||
print('Loading data...')
|
||||
(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=max_features)
|
||||
X_train = sequence.pad_sequences(X_train, max_length)
|
||||
X_test = sequence.pad_sequences(X_test, max_length)
|
||||
|
||||
# Compile and train different models while measuring performance.
|
||||
results = []
|
||||
for mode in modes:
|
||||
print('Testing mode: implementation={}'.format(mode))
|
||||
|
||||
model = Sequential()
|
||||
model.add(Embedding(max_features, embedding_dim,
|
||||
input_length=max_length))
|
||||
model.add(Dropout(0.2))
|
||||
model.add(LSTM(embedding_dim,
|
||||
dropout=0.2,
|
||||
recurrent_dropout=0.2,
|
||||
implementation=mode))
|
||||
model.add(Dense(1, activation='sigmoid'))
|
||||
model.compile(loss='binary_crossentropy',
|
||||
optimizer='adam',
|
||||
metrics=['accuracy'])
|
||||
|
||||
start_time = time.time()
|
||||
history = model.fit(X_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
validation_data=(X_test, y_test))
|
||||
average_time_per_epoch = (time.time() - start_time) / epochs
|
||||
|
||||
results.append((history, average_time_per_epoch))
|
||||
|
||||
# Compare models' accuracy, loss and elapsed time per epoch.
|
||||
plt.style.use('ggplot')
|
||||
ax1 = plt.subplot2grid((2, 2), (0, 0))
|
||||
ax1.set_title('Accuracy')
|
||||
ax1.set_ylabel('Validation Accuracy')
|
||||
ax1.set_xlabel('Epochs')
|
||||
ax2 = plt.subplot2grid((2, 2), (1, 0))
|
||||
ax2.set_title('Loss')
|
||||
ax2.set_ylabel('Validation Loss')
|
||||
ax2.set_xlabel('Epochs')
|
||||
ax3 = plt.subplot2grid((2, 2), (0, 1), rowspan=2)
|
||||
ax3.set_title('Time')
|
||||
ax3.set_ylabel('Seconds')
|
||||
for mode, result in zip(modes, results):
|
||||
ax1.plot(result[0].epoch, result[0].history['val_acc'], label=mode)
|
||||
ax2.plot(result[0].epoch, result[0].history['val_loss'], label=mode)
|
||||
ax1.legend()
|
||||
ax2.legend()
|
||||
ax3.bar(np.arange(len(results)), [x[1] for x in results],
|
||||
tick_label=modes, align='center')
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
@@ -12,30 +12,25 @@ has at least ~100k characters. ~1M is better.
|
||||
|
||||
from __future__ import print_function
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Activation, Dropout
|
||||
from keras.layers.recurrent import LSTM
|
||||
from keras.layers import Dense, Activation
|
||||
from keras.layers import LSTM
|
||||
from keras.optimizers import RMSprop
|
||||
from keras.utils.data_utils import get_file
|
||||
import numpy as np
|
||||
import random
|
||||
import sys
|
||||
|
||||
path = get_file('nietzsche.txt', origin="https://s3.amazonaws.com/text-datasets/nietzsche.txt")
|
||||
|
||||
try:
|
||||
text = open(path).read().lower()
|
||||
except UnicodeDecodeError:
|
||||
import codecs
|
||||
text = codecs.open(path, encoding='utf-8').read().lower()
|
||||
|
||||
path = get_file('nietzsche.txt', origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt')
|
||||
text = open(path).read().lower()
|
||||
print('corpus length:', len(text))
|
||||
|
||||
chars = set(text)
|
||||
chars = sorted(list(set(text)))
|
||||
print('total chars:', len(chars))
|
||||
char_indices = dict((c, i) for i, c in enumerate(chars))
|
||||
indices_char = dict((i, c) for i, c in enumerate(chars))
|
||||
|
||||
# cut the text in semi-redundant sequences of maxlen characters
|
||||
maxlen = 20
|
||||
maxlen = 40
|
||||
step = 3
|
||||
sentences = []
|
||||
next_chars = []
|
||||
@@ -53,31 +48,34 @@ for i, sentence in enumerate(sentences):
|
||||
y[i, char_indices[next_chars[i]]] = 1
|
||||
|
||||
|
||||
# build the model: 2 stacked LSTM
|
||||
# build the model: a single LSTM
|
||||
print('Build model...')
|
||||
model = Sequential()
|
||||
model.add(LSTM(512, return_sequences=True, input_shape=(maxlen, len(chars))))
|
||||
model.add(Dropout(0.2))
|
||||
model.add(LSTM(512, return_sequences=False))
|
||||
model.add(Dropout(0.2))
|
||||
model.add(LSTM(128, input_shape=(maxlen, len(chars))))
|
||||
model.add(Dense(len(chars)))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
|
||||
optimizer = RMSprop(lr=0.01)
|
||||
model.compile(loss='categorical_crossentropy', optimizer=optimizer)
|
||||
|
||||
|
||||
def sample(a, temperature=1.0):
|
||||
def sample(preds, temperature=1.0):
|
||||
# helper function to sample an index from a probability array
|
||||
a = np.log(a) / temperature
|
||||
a = np.exp(a) / np.sum(np.exp(a))
|
||||
return np.argmax(np.random.multinomial(1, a, 1))
|
||||
preds = np.asarray(preds).astype('float64')
|
||||
preds = np.log(preds) / temperature
|
||||
exp_preds = np.exp(preds)
|
||||
preds = exp_preds / np.sum(exp_preds)
|
||||
probas = np.random.multinomial(1, preds, 1)
|
||||
return np.argmax(probas)
|
||||
|
||||
# train the model, output generated text after each iteration
|
||||
for iteration in range(1, 60):
|
||||
print()
|
||||
print('-' * 50)
|
||||
print('Iteration', iteration)
|
||||
model.fit(X, y, batch_size=128, nb_epoch=1)
|
||||
model.fit(X, y,
|
||||
batch_size=128,
|
||||
epochs=1)
|
||||
|
||||
start_index = random.randint(0, len(text) - maxlen - 1)
|
||||
|
||||
|
||||
@@ -0,0 +1,315 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Train an Auxiliary Classifier Generative Adversarial Network (ACGAN) on the
|
||||
MNIST dataset. See https://arxiv.org/abs/1610.09585 for more details.
|
||||
|
||||
You should start to see reasonable images after ~5 epochs, and good images
|
||||
by ~15 epochs. You should use a GPU, as the convolution-heavy operations are
|
||||
very slow on the CPU. Prefer the TensorFlow backend if you plan on iterating,
|
||||
as the compilation time can be a blocker using Theano.
|
||||
|
||||
Timings:
|
||||
|
||||
Hardware | Backend | Time / Epoch
|
||||
-------------------------------------------
|
||||
CPU | TF | 3 hrs
|
||||
Titan X (maxwell) | TF | 4 min
|
||||
Titan X (maxwell) | TH | 7 min
|
||||
|
||||
Consult https://github.com/lukedeo/keras-acgan for more information and
|
||||
example output
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
from collections import defaultdict
|
||||
try:
|
||||
import cPickle as pickle
|
||||
except ImportError:
|
||||
import pickle
|
||||
from PIL import Image
|
||||
|
||||
from six.moves import range
|
||||
|
||||
import keras.backend as K
|
||||
from keras.datasets import mnist
|
||||
from keras import layers
|
||||
from keras.layers import Input, Dense, Reshape, Flatten, Embedding, Dropout
|
||||
from keras.layers.advanced_activations import LeakyReLU
|
||||
from keras.layers.convolutional import UpSampling2D, Conv2D
|
||||
from keras.models import Sequential, Model
|
||||
from keras.optimizers import Adam
|
||||
from keras.utils.generic_utils import Progbar
|
||||
import numpy as np
|
||||
|
||||
np.random.seed(1337)
|
||||
|
||||
K.set_image_data_format('channels_first')
|
||||
|
||||
|
||||
def build_generator(latent_size):
|
||||
# we will map a pair of (z, L), where z is a latent vector and L is a
|
||||
# label drawn from P_c, to image space (..., 1, 28, 28)
|
||||
cnn = Sequential()
|
||||
|
||||
cnn.add(Dense(1024, input_dim=latent_size, activation='relu'))
|
||||
cnn.add(Dense(128 * 7 * 7, activation='relu'))
|
||||
cnn.add(Reshape((128, 7, 7)))
|
||||
|
||||
# upsample to (..., 14, 14)
|
||||
cnn.add(UpSampling2D(size=(2, 2)))
|
||||
cnn.add(Conv2D(256, 5, padding='same',
|
||||
activation='relu',
|
||||
kernel_initializer='glorot_normal'))
|
||||
|
||||
# upsample to (..., 28, 28)
|
||||
cnn.add(UpSampling2D(size=(2, 2)))
|
||||
cnn.add(Conv2D(128, 5, padding='same',
|
||||
activation='relu',
|
||||
kernel_initializer='glorot_normal'))
|
||||
|
||||
# take a channel axis reduction
|
||||
cnn.add(Conv2D(1, 2, padding='same',
|
||||
activation='tanh',
|
||||
kernel_initializer='glorot_normal'))
|
||||
|
||||
# this is the z space commonly refered to in GAN papers
|
||||
latent = Input(shape=(latent_size, ))
|
||||
|
||||
# this will be our label
|
||||
image_class = Input(shape=(1,), dtype='int32')
|
||||
|
||||
# 10 classes in MNIST
|
||||
cls = Flatten()(Embedding(10, latent_size,
|
||||
embeddings_initializer='glorot_normal')(image_class))
|
||||
|
||||
# hadamard product between z-space and a class conditional embedding
|
||||
h = layers.multiply([latent, cls])
|
||||
|
||||
fake_image = cnn(h)
|
||||
|
||||
return Model([latent, image_class], fake_image)
|
||||
|
||||
|
||||
def build_discriminator():
|
||||
# build a relatively standard conv net, with LeakyReLUs as suggested in
|
||||
# the reference paper
|
||||
cnn = Sequential()
|
||||
|
||||
cnn.add(Conv2D(32, 3, padding='same', strides=2,
|
||||
input_shape=(1, 28, 28)))
|
||||
cnn.add(LeakyReLU())
|
||||
cnn.add(Dropout(0.3))
|
||||
|
||||
cnn.add(Conv2D(64, 3, padding='same', strides=1))
|
||||
cnn.add(LeakyReLU())
|
||||
cnn.add(Dropout(0.3))
|
||||
|
||||
cnn.add(Conv2D(128, 3, padding='same', strides=2))
|
||||
cnn.add(LeakyReLU())
|
||||
cnn.add(Dropout(0.3))
|
||||
|
||||
cnn.add(Conv2D(256, 3, padding='same', strides=1))
|
||||
cnn.add(LeakyReLU())
|
||||
cnn.add(Dropout(0.3))
|
||||
|
||||
cnn.add(Flatten())
|
||||
|
||||
image = Input(shape=(1, 28, 28))
|
||||
|
||||
features = cnn(image)
|
||||
|
||||
# first output (name=generation) is whether or not the discriminator
|
||||
# thinks the image that is being shown is fake, and the second output
|
||||
# (name=auxiliary) is the class that the discriminator thinks the image
|
||||
# belongs to.
|
||||
fake = Dense(1, activation='sigmoid', name='generation')(features)
|
||||
aux = Dense(10, activation='softmax', name='auxiliary')(features)
|
||||
|
||||
return Model(image, [fake, aux])
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
# batch and latent size taken from the paper
|
||||
epochs = 50
|
||||
batch_size = 100
|
||||
latent_size = 100
|
||||
|
||||
# Adam parameters suggested in https://arxiv.org/abs/1511.06434
|
||||
adam_lr = 0.0002
|
||||
adam_beta_1 = 0.5
|
||||
|
||||
# build the discriminator
|
||||
discriminator = build_discriminator()
|
||||
discriminator.compile(
|
||||
optimizer=Adam(lr=adam_lr, beta_1=adam_beta_1),
|
||||
loss=['binary_crossentropy', 'sparse_categorical_crossentropy']
|
||||
)
|
||||
|
||||
# build the generator
|
||||
generator = build_generator(latent_size)
|
||||
generator.compile(optimizer=Adam(lr=adam_lr, beta_1=adam_beta_1),
|
||||
loss='binary_crossentropy')
|
||||
|
||||
latent = Input(shape=(latent_size, ))
|
||||
image_class = Input(shape=(1,), dtype='int32')
|
||||
|
||||
# get a fake image
|
||||
fake = generator([latent, image_class])
|
||||
|
||||
# we only want to be able to train generation for the combined model
|
||||
discriminator.trainable = False
|
||||
fake, aux = discriminator(fake)
|
||||
combined = Model([latent, image_class], [fake, aux])
|
||||
|
||||
combined.compile(
|
||||
optimizer=Adam(lr=adam_lr, beta_1=adam_beta_1),
|
||||
loss=['binary_crossentropy', 'sparse_categorical_crossentropy']
|
||||
)
|
||||
|
||||
# get our mnist data, and force it to be of shape (..., 1, 28, 28) with
|
||||
# range [-1, 1]
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
X_train = (X_train.astype(np.float32) - 127.5) / 127.5
|
||||
X_train = np.expand_dims(X_train, axis=1)
|
||||
|
||||
X_test = (X_test.astype(np.float32) - 127.5) / 127.5
|
||||
X_test = np.expand_dims(X_test, axis=1)
|
||||
|
||||
num_train, num_test = X_train.shape[0], X_test.shape[0]
|
||||
|
||||
train_history = defaultdict(list)
|
||||
test_history = defaultdict(list)
|
||||
|
||||
for epoch in range(epochs):
|
||||
print('Epoch {} of {}'.format(epoch + 1, epochs))
|
||||
|
||||
num_batches = int(X_train.shape[0] / batch_size)
|
||||
progress_bar = Progbar(target=num_batches)
|
||||
|
||||
epoch_gen_loss = []
|
||||
epoch_disc_loss = []
|
||||
|
||||
for index in range(num_batches):
|
||||
progress_bar.update(index)
|
||||
# generate a new batch of noise
|
||||
noise = np.random.uniform(-1, 1, (batch_size, latent_size))
|
||||
|
||||
# get a batch of real images
|
||||
image_batch = X_train[index * batch_size:(index + 1) * batch_size]
|
||||
label_batch = y_train[index * batch_size:(index + 1) * batch_size]
|
||||
|
||||
# sample some labels from p_c
|
||||
sampled_labels = np.random.randint(0, 10, batch_size)
|
||||
|
||||
# generate a batch of fake images, using the generated labels as a
|
||||
# conditioner. We reshape the sampled labels to be
|
||||
# (batch_size, 1) so that we can feed them into the embedding
|
||||
# layer as a length one sequence
|
||||
generated_images = generator.predict(
|
||||
[noise, sampled_labels.reshape((-1, 1))], verbose=0)
|
||||
|
||||
X = np.concatenate((image_batch, generated_images))
|
||||
y = np.array([1] * batch_size + [0] * batch_size)
|
||||
aux_y = np.concatenate((label_batch, sampled_labels), axis=0)
|
||||
|
||||
# see if the discriminator can figure itself out...
|
||||
epoch_disc_loss.append(discriminator.train_on_batch(X, [y, aux_y]))
|
||||
|
||||
# make new noise. we generate 2 * batch size here such that we have
|
||||
# the generator optimize over an identical number of images as the
|
||||
# discriminator
|
||||
noise = np.random.uniform(-1, 1, (2 * batch_size, latent_size))
|
||||
sampled_labels = np.random.randint(0, 10, 2 * batch_size)
|
||||
|
||||
# we want to train the generator to trick the discriminator
|
||||
# For the generator, we want all the {fake, not-fake} labels to say
|
||||
# not-fake
|
||||
trick = np.ones(2 * batch_size)
|
||||
|
||||
epoch_gen_loss.append(combined.train_on_batch(
|
||||
[noise, sampled_labels.reshape((-1, 1))],
|
||||
[trick, sampled_labels]))
|
||||
|
||||
print('\nTesting for epoch {}:'.format(epoch + 1))
|
||||
|
||||
# evaluate the testing loss here
|
||||
|
||||
# generate a new batch of noise
|
||||
noise = np.random.uniform(-1, 1, (num_test, latent_size))
|
||||
|
||||
# sample some labels from p_c and generate images from them
|
||||
sampled_labels = np.random.randint(0, 10, num_test)
|
||||
generated_images = generator.predict(
|
||||
[noise, sampled_labels.reshape((-1, 1))], verbose=False)
|
||||
|
||||
X = np.concatenate((X_test, generated_images))
|
||||
y = np.array([1] * num_test + [0] * num_test)
|
||||
aux_y = np.concatenate((y_test, sampled_labels), axis=0)
|
||||
|
||||
# see if the discriminator can figure itself out...
|
||||
discriminator_test_loss = discriminator.evaluate(
|
||||
X, [y, aux_y], verbose=False)
|
||||
|
||||
discriminator_train_loss = np.mean(np.array(epoch_disc_loss), axis=0)
|
||||
|
||||
# make new noise
|
||||
noise = np.random.uniform(-1, 1, (2 * num_test, latent_size))
|
||||
sampled_labels = np.random.randint(0, 10, 2 * num_test)
|
||||
|
||||
trick = np.ones(2 * num_test)
|
||||
|
||||
generator_test_loss = combined.evaluate(
|
||||
[noise, sampled_labels.reshape((-1, 1))],
|
||||
[trick, sampled_labels], verbose=False)
|
||||
|
||||
generator_train_loss = np.mean(np.array(epoch_gen_loss), axis=0)
|
||||
|
||||
# generate an epoch report on performance
|
||||
train_history['generator'].append(generator_train_loss)
|
||||
train_history['discriminator'].append(discriminator_train_loss)
|
||||
|
||||
test_history['generator'].append(generator_test_loss)
|
||||
test_history['discriminator'].append(discriminator_test_loss)
|
||||
|
||||
print('{0:<22s} | {1:4s} | {2:15s} | {3:5s}'.format(
|
||||
'component', *discriminator.metrics_names))
|
||||
print('-' * 65)
|
||||
|
||||
ROW_FMT = '{0:<22s} | {1:<4.2f} | {2:<15.2f} | {3:<5.2f}'
|
||||
print(ROW_FMT.format('generator (train)',
|
||||
*train_history['generator'][-1]))
|
||||
print(ROW_FMT.format('generator (test)',
|
||||
*test_history['generator'][-1]))
|
||||
print(ROW_FMT.format('discriminator (train)',
|
||||
*train_history['discriminator'][-1]))
|
||||
print(ROW_FMT.format('discriminator (test)',
|
||||
*test_history['discriminator'][-1]))
|
||||
|
||||
# save weights every epoch
|
||||
generator.save_weights(
|
||||
'params_generator_epoch_{0:03d}.hdf5'.format(epoch), True)
|
||||
discriminator.save_weights(
|
||||
'params_discriminator_epoch_{0:03d}.hdf5'.format(epoch), True)
|
||||
|
||||
# generate some digits to display
|
||||
noise = np.random.uniform(-1, 1, (100, latent_size))
|
||||
|
||||
sampled_labels = np.array([
|
||||
[i] * 10 for i in range(10)
|
||||
]).reshape(-1, 1)
|
||||
|
||||
# get a batch to display
|
||||
generated_images = generator.predict(
|
||||
[noise, sampled_labels], verbose=0)
|
||||
|
||||
# arrange them into a grid
|
||||
img = (np.concatenate([r.reshape(-1, 28)
|
||||
for r in np.split(generated_images, 10)
|
||||
], axis=-1) * 127.5 + 127.5).astype(np.uint8)
|
||||
|
||||
Image.fromarray(img).save(
|
||||
'plot_epoch_{0:03d}_generated.png'.format(epoch))
|
||||
|
||||
pickle.dump({'train': train_history, 'test': test_history},
|
||||
open('acgan-history.pkl', 'wb'))
|
||||
+45
-48
@@ -1,73 +1,70 @@
|
||||
'''Train a simple convnet on the MNIST dataset.
|
||||
'''Trains a simple convnet on the MNIST dataset.
|
||||
|
||||
Run on GPU: THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python mnist_cnn.py
|
||||
|
||||
Get to 99.25% test accuracy after 12 epochs (there is still a lot of margin for parameter tuning).
|
||||
Gets to 99.25% test accuracy after 12 epochs
|
||||
(there is still a lot of margin for parameter tuning).
|
||||
16 seconds per epoch on a GRID K520 GPU.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
import keras
|
||||
from keras.datasets import mnist
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Dropout, Activation, Flatten
|
||||
from keras.layers.convolutional import Convolution2D, MaxPooling2D
|
||||
from keras.utils import np_utils
|
||||
from keras.layers import Dense, Dropout, Flatten
|
||||
from keras.layers import Conv2D, MaxPooling2D
|
||||
from keras import backend as K
|
||||
|
||||
batch_size = 128
|
||||
nb_classes = 10
|
||||
nb_epoch = 12
|
||||
num_classes = 10
|
||||
epochs = 12
|
||||
|
||||
# input image dimensions
|
||||
img_rows, img_cols = 28, 28
|
||||
# number of convolutional filters to use
|
||||
nb_filters = 32
|
||||
# size of pooling area for max pooling
|
||||
nb_pool = 2
|
||||
# convolution kernel size
|
||||
nb_conv = 3
|
||||
|
||||
# the data, shuffled and split between train and test sets
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
|
||||
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
|
||||
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
|
||||
X_train = X_train.astype('float32')
|
||||
X_test = X_test.astype('float32')
|
||||
X_train /= 255
|
||||
X_test /= 255
|
||||
print('X_train shape:', X_train.shape)
|
||||
print(X_train.shape[0], 'train samples')
|
||||
print(X_test.shape[0], 'test samples')
|
||||
if K.image_data_format() == 'channels_first':
|
||||
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
|
||||
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
|
||||
input_shape = (1, img_rows, img_cols)
|
||||
else:
|
||||
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
|
||||
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
|
||||
input_shape = (img_rows, img_cols, 1)
|
||||
|
||||
x_train = x_train.astype('float32')
|
||||
x_test = x_test.astype('float32')
|
||||
x_train /= 255
|
||||
x_test /= 255
|
||||
print('x_train shape:', x_train.shape)
|
||||
print(x_train.shape[0], 'train samples')
|
||||
print(x_test.shape[0], 'test samples')
|
||||
|
||||
# convert class vectors to binary class matrices
|
||||
Y_train = np_utils.to_categorical(y_train, nb_classes)
|
||||
Y_test = np_utils.to_categorical(y_test, nb_classes)
|
||||
y_train = keras.utils.to_categorical(y_train, num_classes)
|
||||
y_test = keras.utils.to_categorical(y_test, num_classes)
|
||||
|
||||
model = Sequential()
|
||||
|
||||
model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
|
||||
border_mode='valid',
|
||||
input_shape=(1, img_rows, img_cols)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
|
||||
model.add(Activation('relu'))
|
||||
model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
|
||||
model.add(Conv2D(32, kernel_size=(3, 3),
|
||||
activation='relu',
|
||||
input_shape=input_shape))
|
||||
model.add(Conv2D(64, (3, 3), activation='relu'))
|
||||
model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
model.add(Dropout(0.25))
|
||||
|
||||
model.add(Flatten())
|
||||
model.add(Dense(128))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dense(128, activation='relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(nb_classes))
|
||||
model.add(Activation('softmax'))
|
||||
model.add(Dense(num_classes, activation='softmax'))
|
||||
|
||||
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
|
||||
model.compile(loss=keras.losses.categorical_crossentropy,
|
||||
optimizer=keras.optimizers.Adadelta(),
|
||||
metrics=['accuracy'])
|
||||
|
||||
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
|
||||
show_accuracy=True, verbose=1, validation_data=(X_test, Y_test))
|
||||
score = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
|
||||
print('Test score:', score[0])
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
verbose=1,
|
||||
validation_data=(x_test, y_test))
|
||||
score = model.evaluate(x_test, y_test, verbose=0)
|
||||
print('Test loss:', score[0])
|
||||
print('Test accuracy:', score[1])
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
"""This is an example of using Hierarchical RNN (HRNN) to classify MNIST digits.
|
||||
|
||||
HRNNs can learn across multiple levels of temporal hiearchy over a complex sequence.
|
||||
Usually, the first recurrent layer of an HRNN encodes a sentence (e.g. of word vectors)
|
||||
into a sentence vector. The second recurrent layer then encodes a sequence of
|
||||
such vectors (encoded by the first layer) into a document vector. This
|
||||
document vector is considered to preserve both the word-level and
|
||||
sentence-level structure of the context.
|
||||
|
||||
# References
|
||||
- [A Hierarchical Neural Autoencoder for Paragraphs and Documents](https://arxiv.org/abs/1506.01057)
|
||||
Encodes paragraphs and documents with HRNN.
|
||||
Results have shown that HRNN outperforms standard
|
||||
RNNs and may play some role in more sophisticated generation tasks like
|
||||
summarization or question answering.
|
||||
- [Hierarchical recurrent neural network for skeleton based action recognition](http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7298714)
|
||||
Achieved state-of-the-art results on skeleton based action recognition with 3 levels
|
||||
of bidirectional HRNN combined with fully connected layers.
|
||||
|
||||
In the below MNIST example the first LSTM layer first encodes every
|
||||
column of pixels of shape (28, 1) to a column vector of shape (128,). The second LSTM
|
||||
layer encodes then these 28 column vectors of shape (28, 128) to a image vector
|
||||
representing the whole image. A final Dense layer is added for prediction.
|
||||
|
||||
After 5 epochs: train acc: 0.9858, val acc: 0.9864
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
import keras
|
||||
from keras.datasets import mnist
|
||||
from keras.models import Model
|
||||
from keras.layers import Input, Dense, TimeDistributed
|
||||
from keras.layers import LSTM
|
||||
|
||||
# Training parameters.
|
||||
batch_size = 32
|
||||
num_classes = 10
|
||||
epochs = 5
|
||||
|
||||
# Embedding dimensions.
|
||||
row_hidden = 128
|
||||
col_hidden = 128
|
||||
|
||||
# The data, shuffled and split between train and test sets.
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
|
||||
# Reshapes data to 4D for Hierarchical RNN.
|
||||
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
|
||||
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
|
||||
x_train = x_train.astype('float32')
|
||||
x_test = x_test.astype('float32')
|
||||
x_train /= 255
|
||||
x_test /= 255
|
||||
print('x_train shape:', x_train.shape)
|
||||
print(x_train.shape[0], 'train samples')
|
||||
print(x_test.shape[0], 'test samples')
|
||||
|
||||
# Converts class vectors to binary class matrices.
|
||||
y_train = keras.utils.to_categorical(y_train, num_classes)
|
||||
y_test = keras.utils.to_categorical(y_test, num_classes)
|
||||
|
||||
row, col, pixel = x_train.shape[1:]
|
||||
|
||||
# 4D input.
|
||||
x = Input(shape=(row, col, pixel))
|
||||
|
||||
# Encodes a row of pixels using TimeDistributed Wrapper.
|
||||
encoded_rows = TimeDistributed(LSTM(row_hidden))(x)
|
||||
|
||||
# Encodes columns of encoded rows.
|
||||
encoded_columns = LSTM(col_hidden)(encoded_rows)
|
||||
|
||||
# Final predictions and model.
|
||||
prediction = Dense(num_classes, activation='softmax')(encoded_columns)
|
||||
model = Model(x, prediction)
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer='rmsprop',
|
||||
metrics=['accuracy'])
|
||||
|
||||
# Training.
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
verbose=1,
|
||||
validation_data=(x_test, y_test))
|
||||
|
||||
# Evaluation.
|
||||
scores = model.evaluate(x_test, y_test, verbose=0)
|
||||
print('Test loss:', scores[0])
|
||||
print('Test accuracy:', scores[1])
|
||||
+34
-46
@@ -3,7 +3,7 @@ with pixel-by-pixel sequential MNIST in
|
||||
"A Simple Way to Initialize Recurrent Networks of Rectified Linear Units"
|
||||
by Quoc V. Le, Navdeep Jaitly, Geoffrey E. Hinton
|
||||
|
||||
arXiv:1504.00941v2 [cs.NE] 7 Apr 201
|
||||
arxiv:1504.00941v2 [cs.NE] 7 Apr 2015
|
||||
http://arxiv.org/pdf/1504.00941v2.pdf
|
||||
|
||||
Optimizer is replaced with RMSprop which yields more stable and steady
|
||||
@@ -14,72 +14,60 @@ Reaches 0.93 train/test accuracy after 900 epochs
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
import keras
|
||||
from keras.datasets import mnist
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Activation
|
||||
from keras.initializations import normal, identity
|
||||
from keras.layers.recurrent import SimpleRNN, LSTM
|
||||
from keras.layers import Dense, Activation
|
||||
from keras.layers import SimpleRNN
|
||||
from keras import initializers
|
||||
from keras.optimizers import RMSprop
|
||||
from keras.utils import np_utils
|
||||
|
||||
|
||||
batch_size = 32
|
||||
nb_classes = 10
|
||||
nb_epochs = 200
|
||||
num_classes = 10
|
||||
epochs = 200
|
||||
hidden_units = 100
|
||||
|
||||
learning_rate = 1e-6
|
||||
clip_norm = 1.0
|
||||
|
||||
# the data, shuffled and split between train and test sets
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
|
||||
X_train = X_train.reshape(X_train.shape[0], -1, 1)
|
||||
X_test = X_test.reshape(X_test.shape[0], -1, 1)
|
||||
X_train = X_train.astype('float32')
|
||||
X_test = X_test.astype('float32')
|
||||
X_train /= 255
|
||||
X_test /= 255
|
||||
print('X_train shape:', X_train.shape)
|
||||
print(X_train.shape[0], 'train samples')
|
||||
print(X_test.shape[0], 'test samples')
|
||||
x_train = x_train.reshape(x_train.shape[0], -1, 1)
|
||||
x_test = x_test.reshape(x_test.shape[0], -1, 1)
|
||||
x_train = x_train.astype('float32')
|
||||
x_test = x_test.astype('float32')
|
||||
x_train /= 255
|
||||
x_test /= 255
|
||||
print('x_train shape:', x_train.shape)
|
||||
print(x_train.shape[0], 'train samples')
|
||||
print(x_test.shape[0], 'test samples')
|
||||
|
||||
# convert class vectors to binary class matrices
|
||||
Y_train = np_utils.to_categorical(y_train, nb_classes)
|
||||
Y_test = np_utils.to_categorical(y_test, nb_classes)
|
||||
y_train = keras.utils.to_categorical(y_train, num_classes)
|
||||
y_test = keras.utils.to_categorical(y_test, num_classes)
|
||||
|
||||
print('Evaluate IRNN...')
|
||||
model = Sequential()
|
||||
model.add(SimpleRNN(output_dim=hidden_units,
|
||||
init=lambda shape: normal(shape, scale=0.001),
|
||||
inner_init=lambda shape: identity(shape, scale=1.0),
|
||||
activation='relu', input_shape=X_train.shape[1:]))
|
||||
model.add(Dense(nb_classes))
|
||||
model.add(SimpleRNN(hidden_units,
|
||||
kernel_initializer=initializers.RandomNormal(stddev=0.001),
|
||||
recurrent_initializer=initializers.Identity(gain=1.0),
|
||||
activation='relu',
|
||||
input_shape=x_train.shape[1:]))
|
||||
model.add(Dense(num_classes))
|
||||
model.add(Activation('softmax'))
|
||||
rmsprop = RMSprop(lr=learning_rate)
|
||||
model.compile(loss='categorical_crossentropy', optimizer=rmsprop)
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer=rmsprop,
|
||||
metrics=['accuracy'])
|
||||
|
||||
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epochs,
|
||||
show_accuracy=True, verbose=1, validation_data=(X_test, Y_test))
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
verbose=1,
|
||||
validation_data=(x_test, y_test))
|
||||
|
||||
scores = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
|
||||
scores = model.evaluate(x_test, y_test, verbose=0)
|
||||
print('IRNN test score:', scores[0])
|
||||
print('IRNN test accuracy:', scores[1])
|
||||
|
||||
print('Compare to LSTM...')
|
||||
model = Sequential()
|
||||
model.add(LSTM(hidden_units, input_shape=X_train.shape[1:]))
|
||||
model.add(Dense(nb_classes))
|
||||
model.add(Activation('softmax'))
|
||||
rmsprop = RMSprop(lr=learning_rate)
|
||||
model.compile(loss='categorical_crossentropy', optimizer=rmsprop)
|
||||
|
||||
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epochs,
|
||||
show_accuracy=True, verbose=1, validation_data=(X_test, Y_test))
|
||||
|
||||
scores = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
|
||||
print('LSTM test score:', scores[0])
|
||||
print('LSTM test accuracy:', scores[1])
|
||||
|
||||
+33
-35
@@ -1,59 +1,57 @@
|
||||
'''Train a simple deep NN on the MNIST dataset.
|
||||
'''Trains a simple deep NN on the MNIST dataset.
|
||||
|
||||
Get to 98.40% test accuracy after 20 epochs
|
||||
Gets to 98.40% test accuracy after 20 epochs
|
||||
(there is *a lot* of margin for parameter tuning).
|
||||
2 seconds per epoch on a K520 GPU.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
import keras
|
||||
from keras.datasets import mnist
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Dropout, Activation
|
||||
from keras.optimizers import SGD, Adam, RMSprop
|
||||
from keras.utils import np_utils
|
||||
from keras.layers import Dense, Dropout
|
||||
from keras.optimizers import RMSprop
|
||||
|
||||
|
||||
batch_size = 128
|
||||
nb_classes = 10
|
||||
nb_epoch = 20
|
||||
num_classes = 10
|
||||
epochs = 20
|
||||
|
||||
# the data, shuffled and split between train and test sets
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
|
||||
X_train = X_train.reshape(60000, 784)
|
||||
X_test = X_test.reshape(10000, 784)
|
||||
X_train = X_train.astype('float32')
|
||||
X_test = X_test.astype('float32')
|
||||
X_train /= 255
|
||||
X_test /= 255
|
||||
print(X_train.shape[0], 'train samples')
|
||||
print(X_test.shape[0], 'test samples')
|
||||
x_train = x_train.reshape(60000, 784)
|
||||
x_test = x_test.reshape(10000, 784)
|
||||
x_train = x_train.astype('float32')
|
||||
x_test = x_test.astype('float32')
|
||||
x_train /= 255
|
||||
x_test /= 255
|
||||
print(x_train.shape[0], 'train samples')
|
||||
print(x_test.shape[0], 'test samples')
|
||||
|
||||
# convert class vectors to binary class matrices
|
||||
Y_train = np_utils.to_categorical(y_train, nb_classes)
|
||||
Y_test = np_utils.to_categorical(y_test, nb_classes)
|
||||
y_train = keras.utils.to_categorical(y_train, num_classes)
|
||||
y_test = keras.utils.to_categorical(y_test, num_classes)
|
||||
|
||||
model = Sequential()
|
||||
model.add(Dense(512, input_shape=(784,)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dense(512, activation='relu', input_shape=(784,)))
|
||||
model.add(Dropout(0.2))
|
||||
model.add(Dense(512))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dense(512, activation='relu'))
|
||||
model.add(Dropout(0.2))
|
||||
model.add(Dense(10))
|
||||
model.add(Activation('softmax'))
|
||||
model.add(Dense(10, activation='softmax'))
|
||||
|
||||
rms = RMSprop()
|
||||
model.compile(loss='categorical_crossentropy', optimizer=rms)
|
||||
model.summary()
|
||||
|
||||
model.fit(X_train, Y_train,
|
||||
batch_size=batch_size, nb_epoch=nb_epoch,
|
||||
show_accuracy=True, verbose=2,
|
||||
validation_data=(X_test, Y_test))
|
||||
score = model.evaluate(X_test, Y_test,
|
||||
show_accuracy=True, verbose=0)
|
||||
print('Test score:', score[0])
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer=RMSprop(),
|
||||
metrics=['accuracy'])
|
||||
|
||||
history = model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
verbose=1,
|
||||
validation_data=(x_test, y_test))
|
||||
score = model.evaluate(x_test, y_test, verbose=0)
|
||||
print('Test loss:', score[0])
|
||||
print('Test accuracy:', score[1])
|
||||
|
||||
@@ -0,0 +1,389 @@
|
||||
'''This is an implementation of Net2Net experiment with MNIST in
|
||||
'Net2Net: Accelerating Learning via Knowledge Transfer'
|
||||
by Tianqi Chen, Ian Goodfellow, and Jonathon Shlens
|
||||
|
||||
arXiv:1511.05641v4 [cs.LG] 23 Apr 2016
|
||||
http://arxiv.org/abs/1511.05641
|
||||
|
||||
Notes
|
||||
- What:
|
||||
+ Net2Net is a group of methods to transfer knowledge from a teacher neural
|
||||
net to a student net,so that the student net can be trained faster than
|
||||
from scratch.
|
||||
+ The paper discussed two specific methods of Net2Net, i.e. Net2WiderNet
|
||||
and Net2DeeperNet.
|
||||
+ Net2WiderNet replaces a model with an equivalent wider model that has
|
||||
more units in each hidden layer.
|
||||
+ Net2DeeperNet replaces a model with an equivalent deeper model.
|
||||
+ Both are based on the idea of 'function-preserving transformations of
|
||||
neural nets'.
|
||||
- Why:
|
||||
+ Enable fast exploration of multiple neural nets in experimentation and
|
||||
design process,by creating a series of wider and deeper models with
|
||||
transferable knowledge.
|
||||
+ Enable 'lifelong learning system' by gradually adjusting model complexity
|
||||
to data availability,and reusing transferable knowledge.
|
||||
|
||||
Experiments
|
||||
- Teacher model: a basic CNN model trained on MNIST for 3 epochs.
|
||||
- Net2WiderNet experiment:
|
||||
+ Student model has a wider Conv2D layer and a wider FC layer.
|
||||
+ Comparison of 'random-padding' vs 'net2wider' weight initialization.
|
||||
+ With both methods, student model should immediately perform as well as
|
||||
teacher model, but 'net2wider' is slightly better.
|
||||
- Net2DeeperNet experiment:
|
||||
+ Student model has an extra Conv2D layer and an extra FC layer.
|
||||
+ Comparison of 'random-init' vs 'net2deeper' weight initialization.
|
||||
+ Starting performance of 'net2deeper' is better than 'random-init'.
|
||||
- Hyper-parameters:
|
||||
+ SGD with momentum=0.9 is used for training teacher and student models.
|
||||
+ Learning rate adjustment: it's suggested to reduce learning rate
|
||||
to 1/10 for student model.
|
||||
+ Addition of noise in 'net2wider' is used to break weight symmetry
|
||||
and thus enable full capacity of student models. It is optional
|
||||
when a Dropout layer is used.
|
||||
|
||||
Results
|
||||
- Tested with 'Theano' backend and 'channels_first' image_data_format.
|
||||
- Running on GPU GeForce GTX 980M
|
||||
- Performance Comparisons - validation loss values during first 3 epochs:
|
||||
(1) teacher_model: 0.075 0.041 0.041
|
||||
(2) wider_random_pad: 0.036 0.034 0.032
|
||||
(3) wider_net2wider: 0.032 0.030 0.030
|
||||
(4) deeper_random_init: 0.061 0.043 0.041
|
||||
(5) deeper_net2deeper: 0.032 0.031 0.029
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
from six.moves import xrange
|
||||
import numpy as np
|
||||
import keras
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten
|
||||
from keras.optimizers import SGD
|
||||
from keras.datasets import mnist
|
||||
|
||||
if keras.backend.image_data_format() == 'channels_first':
|
||||
input_shape = (1, 28, 28) # image shape
|
||||
else:
|
||||
input_shape = (28, 28, 1) # image shape
|
||||
num_class = 10 # number of class
|
||||
|
||||
|
||||
# load and pre-process data
|
||||
def preprocess_input(x):
|
||||
return x.reshape((-1, ) + input_shape) / 255.
|
||||
|
||||
|
||||
def preprocess_output(y):
|
||||
return keras.utils.to_categorical(y)
|
||||
|
||||
(train_x, train_y), (validation_x, validation_y) = mnist.load_data()
|
||||
train_x, validation_x = map(preprocess_input, [train_x, validation_x])
|
||||
train_y, validation_y = map(preprocess_output, [train_y, validation_y])
|
||||
print('Loading MNIST data...')
|
||||
print('train_x shape:', train_x.shape, 'train_y shape:', train_y.shape)
|
||||
print('validation_x shape:', validation_x.shape,
|
||||
'validation_y shape', validation_y.shape)
|
||||
|
||||
|
||||
# knowledge transfer algorithms
|
||||
def wider2net_conv2d(teacher_w1, teacher_b1, teacher_w2, new_width, init):
|
||||
'''Get initial weights for a wider conv2d layer with a bigger filters,
|
||||
by 'random-padding' or 'net2wider'.
|
||||
|
||||
# Arguments
|
||||
teacher_w1: `weight` of conv2d layer to become wider,
|
||||
of shape (filters1, num_channel1, kh1, kw1)
|
||||
teacher_b1: `bias` of conv2d layer to become wider,
|
||||
of shape (filters1, )
|
||||
teacher_w2: `weight` of next connected conv2d layer,
|
||||
of shape (filters2, num_channel2, kh2, kw2)
|
||||
new_width: new `filters` for the wider conv2d layer
|
||||
init: initialization algorithm for new weights,
|
||||
either 'random-pad' or 'net2wider'
|
||||
'''
|
||||
assert teacher_w1.shape[0] == teacher_w2.shape[1], (
|
||||
'successive layers from teacher model should have compatible shapes')
|
||||
assert teacher_w1.shape[0] == teacher_b1.shape[0], (
|
||||
'weight and bias from same layer should have compatible shapes')
|
||||
assert new_width > teacher_w1.shape[0], (
|
||||
'new width (filters) should be bigger than the existing one')
|
||||
|
||||
n = new_width - teacher_w1.shape[0]
|
||||
if init == 'random-pad':
|
||||
new_w1 = np.random.normal(0, 0.1, size=(n, ) + teacher_w1.shape[1:])
|
||||
new_b1 = np.ones(n) * 0.1
|
||||
new_w2 = np.random.normal(0, 0.1, size=(
|
||||
teacher_w2.shape[0], n) + teacher_w2.shape[2:])
|
||||
elif init == 'net2wider':
|
||||
index = np.random.randint(teacher_w1.shape[0], size=n)
|
||||
factors = np.bincount(index)[index] + 1.
|
||||
new_w1 = teacher_w1[index, :, :, :]
|
||||
new_b1 = teacher_b1[index]
|
||||
new_w2 = teacher_w2[:, index, :, :] / factors.reshape((1, -1, 1, 1))
|
||||
else:
|
||||
raise ValueError('Unsupported weight initializer: %s' % init)
|
||||
|
||||
student_w1 = np.concatenate((teacher_w1, new_w1), axis=0)
|
||||
if init == 'random-pad':
|
||||
student_w2 = np.concatenate((teacher_w2, new_w2), axis=1)
|
||||
elif init == 'net2wider':
|
||||
# add small noise to break symmetry, so that student model will have
|
||||
# full capacity later
|
||||
noise = np.random.normal(0, 5e-2 * new_w2.std(), size=new_w2.shape)
|
||||
student_w2 = np.concatenate((teacher_w2, new_w2 + noise), axis=1)
|
||||
student_w2[:, index, :, :] = new_w2
|
||||
student_b1 = np.concatenate((teacher_b1, new_b1), axis=0)
|
||||
|
||||
return student_w1, student_b1, student_w2
|
||||
|
||||
|
||||
def wider2net_fc(teacher_w1, teacher_b1, teacher_w2, new_width, init):
|
||||
'''Get initial weights for a wider fully connected (dense) layer
|
||||
with a bigger nout, by 'random-padding' or 'net2wider'.
|
||||
|
||||
# Arguments
|
||||
teacher_w1: `weight` of fc layer to become wider,
|
||||
of shape (nin1, nout1)
|
||||
teacher_b1: `bias` of fc layer to become wider,
|
||||
of shape (nout1, )
|
||||
teacher_w2: `weight` of next connected fc layer,
|
||||
of shape (nin2, nout2)
|
||||
new_width: new `nout` for the wider fc layer
|
||||
init: initialization algorithm for new weights,
|
||||
either 'random-pad' or 'net2wider'
|
||||
'''
|
||||
assert teacher_w1.shape[1] == teacher_w2.shape[0], (
|
||||
'successive layers from teacher model should have compatible shapes')
|
||||
assert teacher_w1.shape[1] == teacher_b1.shape[0], (
|
||||
'weight and bias from same layer should have compatible shapes')
|
||||
assert new_width > teacher_w1.shape[1], (
|
||||
'new width (nout) should be bigger than the existing one')
|
||||
|
||||
n = new_width - teacher_w1.shape[1]
|
||||
if init == 'random-pad':
|
||||
new_w1 = np.random.normal(0, 0.1, size=(teacher_w1.shape[0], n))
|
||||
new_b1 = np.ones(n) * 0.1
|
||||
new_w2 = np.random.normal(0, 0.1, size=(n, teacher_w2.shape[1]))
|
||||
elif init == 'net2wider':
|
||||
index = np.random.randint(teacher_w1.shape[1], size=n)
|
||||
factors = np.bincount(index)[index] + 1.
|
||||
new_w1 = teacher_w1[:, index]
|
||||
new_b1 = teacher_b1[index]
|
||||
new_w2 = teacher_w2[index, :] / factors[:, np.newaxis]
|
||||
else:
|
||||
raise ValueError('Unsupported weight initializer: %s' % init)
|
||||
|
||||
student_w1 = np.concatenate((teacher_w1, new_w1), axis=1)
|
||||
if init == 'random-pad':
|
||||
student_w2 = np.concatenate((teacher_w2, new_w2), axis=0)
|
||||
elif init == 'net2wider':
|
||||
# add small noise to break symmetry, so that student model will have
|
||||
# full capacity later
|
||||
noise = np.random.normal(0, 5e-2 * new_w2.std(), size=new_w2.shape)
|
||||
student_w2 = np.concatenate((teacher_w2, new_w2 + noise), axis=0)
|
||||
student_w2[index, :] = new_w2
|
||||
student_b1 = np.concatenate((teacher_b1, new_b1), axis=0)
|
||||
|
||||
return student_w1, student_b1, student_w2
|
||||
|
||||
|
||||
def deeper2net_conv2d(teacher_w):
|
||||
'''Get initial weights for a deeper conv2d layer by net2deeper'.
|
||||
|
||||
# Arguments
|
||||
teacher_w: `weight` of previous conv2d layer,
|
||||
of shape (filters, num_channel, kh, kw)
|
||||
'''
|
||||
filters, num_channel, kh, kw = teacher_w.shape
|
||||
student_w = np.zeros((filters, filters, kh, kw))
|
||||
for i in xrange(filters):
|
||||
student_w[i, i, (kh - 1) / 2, (kw - 1) / 2] = 1.
|
||||
student_b = np.zeros(filters)
|
||||
return student_w, student_b
|
||||
|
||||
|
||||
def copy_weights(teacher_model, student_model, layer_names):
|
||||
'''Copy weights from teacher_model to student_model,
|
||||
for layers with names listed in layer_names
|
||||
'''
|
||||
for name in layer_names:
|
||||
weights = teacher_model.get_layer(name=name).get_weights()
|
||||
student_model.get_layer(name=name).set_weights(weights)
|
||||
|
||||
|
||||
# methods to construct teacher_model and student_models
|
||||
def make_teacher_model(train_data, validation_data, epochs=3):
|
||||
'''Train a simple CNN as teacher model.
|
||||
'''
|
||||
model = Sequential()
|
||||
model.add(Conv2D(64, 3, input_shape=input_shape,
|
||||
padding='same', name='conv1'))
|
||||
model.add(MaxPooling2D(2, name='pool1'))
|
||||
model.add(Conv2D(64, 3, padding='same', name='conv2'))
|
||||
model.add(MaxPooling2D(2, name='pool2'))
|
||||
model.add(Flatten(name='flatten'))
|
||||
model.add(Dense(64, activation='relu', name='fc1'))
|
||||
model.add(Dense(num_class, activation='softmax', name='fc2'))
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer=SGD(lr=0.01, momentum=0.9),
|
||||
metrics=['accuracy'])
|
||||
|
||||
train_x, train_y = train_data
|
||||
history = model.fit(train_x, train_y,
|
||||
epochs=epochs,
|
||||
validation_data=validation_data)
|
||||
return model, history
|
||||
|
||||
|
||||
def make_wider_student_model(teacher_model, train_data,
|
||||
validation_data, init, epochs=3):
|
||||
'''Train a wider student model based on teacher_model,
|
||||
with either 'random-pad' (baseline) or 'net2wider'
|
||||
'''
|
||||
new_conv1_width = 128
|
||||
new_fc1_width = 128
|
||||
|
||||
model = Sequential()
|
||||
# a wider conv1 compared to teacher_model
|
||||
model.add(Conv2D(new_conv1_width, 3, input_shape=input_shape,
|
||||
padding='same', name='conv1'))
|
||||
model.add(MaxPooling2D(2, name='pool1'))
|
||||
model.add(Conv2D(64, 3, padding='same', name='conv2'))
|
||||
model.add(MaxPooling2D(2, name='pool2'))
|
||||
model.add(Flatten(name='flatten'))
|
||||
# a wider fc1 compared to teacher model
|
||||
model.add(Dense(new_fc1_width, activation='relu', name='fc1'))
|
||||
model.add(Dense(num_class, activation='softmax', name='fc2'))
|
||||
|
||||
# The weights for other layers need to be copied from teacher_model
|
||||
# to student_model, except for widened layers
|
||||
# and their immediate downstreams, which will be initialized separately.
|
||||
# For this example there are no other layers that need to be copied.
|
||||
|
||||
w_conv1, b_conv1 = teacher_model.get_layer('conv1').get_weights()
|
||||
w_conv2, b_conv2 = teacher_model.get_layer('conv2').get_weights()
|
||||
new_w_conv1, new_b_conv1, new_w_conv2 = wider2net_conv2d(
|
||||
w_conv1, b_conv1, w_conv2, new_conv1_width, init)
|
||||
model.get_layer('conv1').set_weights([new_w_conv1, new_b_conv1])
|
||||
model.get_layer('conv2').set_weights([new_w_conv2, b_conv2])
|
||||
|
||||
w_fc1, b_fc1 = teacher_model.get_layer('fc1').get_weights()
|
||||
w_fc2, b_fc2 = teacher_model.get_layer('fc2').get_weights()
|
||||
new_w_fc1, new_b_fc1, new_w_fc2 = wider2net_fc(
|
||||
w_fc1, b_fc1, w_fc2, new_fc1_width, init)
|
||||
model.get_layer('fc1').set_weights([new_w_fc1, new_b_fc1])
|
||||
model.get_layer('fc2').set_weights([new_w_fc2, b_fc2])
|
||||
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer=SGD(lr=0.001, momentum=0.9),
|
||||
metrics=['accuracy'])
|
||||
|
||||
train_x, train_y = train_data
|
||||
history = model.fit(train_x, train_y,
|
||||
epochs=epochs,
|
||||
validation_data=validation_data)
|
||||
return model, history
|
||||
|
||||
|
||||
def make_deeper_student_model(teacher_model, train_data,
|
||||
validation_data, init, epochs=3):
|
||||
'''Train a deeper student model based on teacher_model,
|
||||
with either 'random-init' (baseline) or 'net2deeper'
|
||||
'''
|
||||
model = Sequential()
|
||||
model.add(Conv2D(64, 3, input_shape=input_shape,
|
||||
padding='same', name='conv1'))
|
||||
model.add(MaxPooling2D(2, name='pool1'))
|
||||
model.add(Conv2D(64, 3, padding='same', name='conv2'))
|
||||
# add another conv2d layer to make original conv2 deeper
|
||||
if init == 'net2deeper':
|
||||
prev_w, _ = model.get_layer('conv2').get_weights()
|
||||
new_weights = deeper2net_conv2d(prev_w)
|
||||
model.add(Conv2D(64, 3, padding='same',
|
||||
name='conv2-deeper', weights=new_weights))
|
||||
elif init == 'random-init':
|
||||
model.add(Conv2D(64, 3, padding='same', name='conv2-deeper'))
|
||||
else:
|
||||
raise ValueError('Unsupported weight initializer: %s' % init)
|
||||
model.add(MaxPooling2D(2, name='pool2'))
|
||||
model.add(Flatten(name='flatten'))
|
||||
model.add(Dense(64, activation='relu', name='fc1'))
|
||||
# add another fc layer to make original fc1 deeper
|
||||
if init == 'net2deeper':
|
||||
# net2deeper for fc layer with relu, is just an identity initializer
|
||||
model.add(Dense(64, kernel_initializer='identity',
|
||||
activation='relu', name='fc1-deeper'))
|
||||
elif init == 'random-init':
|
||||
model.add(Dense(64, activation='relu', name='fc1-deeper'))
|
||||
else:
|
||||
raise ValueError('Unsupported weight initializer: %s' % init)
|
||||
model.add(Dense(num_class, activation='softmax', name='fc2'))
|
||||
|
||||
# copy weights for other layers
|
||||
copy_weights(teacher_model, model, layer_names=[
|
||||
'conv1', 'conv2', 'fc1', 'fc2'])
|
||||
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer=SGD(lr=0.001, momentum=0.9),
|
||||
metrics=['accuracy'])
|
||||
|
||||
train_x, train_y = train_data
|
||||
history = model.fit(train_x, train_y,
|
||||
epochs=epochs,
|
||||
validation_data=validation_data)
|
||||
return model, history
|
||||
|
||||
|
||||
# experiments setup
|
||||
def net2wider_experiment():
|
||||
'''Benchmark performances of
|
||||
(1) a teacher model,
|
||||
(2) a wider student model with `random_pad` initializer
|
||||
(3) a wider student model with `Net2WiderNet` initializer
|
||||
'''
|
||||
train_data = (train_x, train_y)
|
||||
validation_data = (validation_x, validation_y)
|
||||
print('\nExperiment of Net2WiderNet ...')
|
||||
print('\nbuilding teacher model ...')
|
||||
teacher_model, _ = make_teacher_model(train_data,
|
||||
validation_data,
|
||||
epochs=3)
|
||||
|
||||
print('\nbuilding wider student model by random padding ...')
|
||||
make_wider_student_model(teacher_model, train_data,
|
||||
validation_data, 'random-pad',
|
||||
epochs=3)
|
||||
print('\nbuilding wider student model by net2wider ...')
|
||||
make_wider_student_model(teacher_model, train_data,
|
||||
validation_data, 'net2wider',
|
||||
epochs=3)
|
||||
|
||||
|
||||
def net2deeper_experiment():
|
||||
'''Benchmark performances of
|
||||
(1) a teacher model,
|
||||
(2) a deeper student model with `random_init` initializer
|
||||
(3) a deeper student model with `Net2DeeperNet` initializer
|
||||
'''
|
||||
train_data = (train_x, train_y)
|
||||
validation_data = (validation_x, validation_y)
|
||||
print('\nExperiment of Net2DeeperNet ...')
|
||||
print('\nbuilding teacher model ...')
|
||||
teacher_model, _ = make_teacher_model(train_data,
|
||||
validation_data,
|
||||
epochs=3)
|
||||
|
||||
print('\nbuilding deeper student model by random init ...')
|
||||
make_deeper_student_model(teacher_model, train_data,
|
||||
validation_data, 'random-init',
|
||||
epochs=3)
|
||||
print('\nbuilding deeper student model by net2deeper ...')
|
||||
make_deeper_student_model(teacher_model, train_data,
|
||||
validation_data, 'net2deeper',
|
||||
epochs=3)
|
||||
|
||||
# run the experiments
|
||||
net2wider_experiment()
|
||||
net2deeper_experiment()
|
||||
@@ -7,37 +7,38 @@ for mode details).
|
||||
[1] "Dimensionality Reduction by Learning an Invariant Mapping"
|
||||
http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
|
||||
|
||||
Run on GPU: THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python mnist_siamese_graph.py
|
||||
|
||||
Gets to 99.5% test accuracy after 20 epochs.
|
||||
3 seconds per epoch on a Titan X GPU
|
||||
'''
|
||||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
import random
|
||||
from keras.datasets import mnist
|
||||
from keras.models import Sequential, Graph
|
||||
from keras.layers.core import Dense, Dropout, Lambda
|
||||
from keras.optimizers import SGD, RMSprop
|
||||
from keras.models import Sequential, Model
|
||||
from keras.layers import Dense, Dropout, Input, Lambda
|
||||
from keras.optimizers import RMSprop
|
||||
from keras import backend as K
|
||||
|
||||
|
||||
def euclidean_distance(inputs):
|
||||
assert len(inputs) == 2, ('Euclidean distance needs '
|
||||
'2 inputs, %d given' % len(inputs))
|
||||
u, v = inputs.values()
|
||||
return K.sqrt(K.sum(K.square(u - v), axis=1, keepdims=True))
|
||||
def euclidean_distance(vects):
|
||||
x, y = vects
|
||||
return K.sqrt(K.maximum(K.sum(K.square(x - y), axis=1, keepdims=True), K.epsilon()))
|
||||
|
||||
|
||||
def contrastive_loss(y, d):
|
||||
def eucl_dist_output_shape(shapes):
|
||||
shape1, shape2 = shapes
|
||||
return (shape1[0], 1)
|
||||
|
||||
|
||||
def contrastive_loss(y_true, y_pred):
|
||||
'''Contrastive loss from Hadsell-et-al.'06
|
||||
http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
|
||||
'''
|
||||
margin = 1
|
||||
return K.mean(y * K.square(d) + (1 - y) * K.square(K.maximum(margin - d, 0)))
|
||||
return K.mean(y_true * K.square(y_pred) +
|
||||
(1 - y_true) * K.square(K.maximum(margin - y_pred, 0)))
|
||||
|
||||
|
||||
def create_pairs(x, digit_indices):
|
||||
@@ -49,7 +50,7 @@ def create_pairs(x, digit_indices):
|
||||
n = min([len(digit_indices[d]) for d in range(10)]) - 1
|
||||
for d in range(10):
|
||||
for i in range(n):
|
||||
z1, z2 = digit_indices[d][i], digit_indices[d][i+1]
|
||||
z1, z2 = digit_indices[d][i], digit_indices[d][i + 1]
|
||||
pairs += [[x[z1], x[z2]]]
|
||||
inc = random.randrange(1, 10)
|
||||
dn = (d + inc) % 10
|
||||
@@ -78,46 +79,52 @@ def compute_accuracy(predictions, labels):
|
||||
|
||||
|
||||
# the data, shuffled and split between train and test sets
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
X_train = X_train.reshape(60000, 784)
|
||||
X_test = X_test.reshape(10000, 784)
|
||||
X_train = X_train.astype('float32')
|
||||
X_test = X_test.astype('float32')
|
||||
X_train /= 255
|
||||
X_test /= 255
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
x_train = x_train.reshape(60000, 784)
|
||||
x_test = x_test.reshape(10000, 784)
|
||||
x_train = x_train.astype('float32')
|
||||
x_test = x_test.astype('float32')
|
||||
x_train /= 255
|
||||
x_test /= 255
|
||||
input_dim = 784
|
||||
nb_epoch = 20
|
||||
epochs = 20
|
||||
|
||||
# create training+test positive and negative pairs
|
||||
digit_indices = [np.where(y_train == i)[0] for i in range(10)]
|
||||
tr_pairs, tr_y = create_pairs(X_train, digit_indices)
|
||||
tr_pairs, tr_y = create_pairs(x_train, digit_indices)
|
||||
|
||||
digit_indices = [np.where(y_test == i)[0] for i in range(10)]
|
||||
te_pairs, te_y = create_pairs(X_test, digit_indices)
|
||||
te_pairs, te_y = create_pairs(x_test, digit_indices)
|
||||
|
||||
# network definition
|
||||
base_network = create_base_network(input_dim)
|
||||
|
||||
g = Graph()
|
||||
g.add_input(name='input_a', input_shape=(input_dim,))
|
||||
g.add_input(name='input_b', input_shape=(input_dim,))
|
||||
g.add_shared_node(base_network, name='shared', inputs=['input_a', 'input_b'],
|
||||
merge_mode='join')
|
||||
g.add_node(Lambda(euclidean_distance), name='d', input='shared')
|
||||
g.add_output(name='output', input='d')
|
||||
input_a = Input(shape=(input_dim,))
|
||||
input_b = Input(shape=(input_dim,))
|
||||
|
||||
# because we re-use the same instance `base_network`,
|
||||
# the weights of the network
|
||||
# will be shared across the two branches
|
||||
processed_a = base_network(input_a)
|
||||
processed_b = base_network(input_b)
|
||||
|
||||
distance = Lambda(euclidean_distance,
|
||||
output_shape=eucl_dist_output_shape)([processed_a, processed_b])
|
||||
|
||||
model = Model([input_a, input_b], distance)
|
||||
|
||||
# train
|
||||
rms = RMSprop()
|
||||
g.compile(loss={'output': contrastive_loss}, optimizer=rms)
|
||||
g.fit({'input_a': tr_pairs[:, 0], 'input_b': tr_pairs[:, 1], 'output': tr_y},
|
||||
validation_data={'input_a': te_pairs[:, 0], 'input_b': te_pairs[:, 1], 'output': te_y},
|
||||
batch_size=128,
|
||||
nb_epoch=nb_epoch)
|
||||
model.compile(loss=contrastive_loss, optimizer=rms)
|
||||
model.fit([tr_pairs[:, 0], tr_pairs[:, 1]], tr_y,
|
||||
batch_size=128,
|
||||
epochs=epochs,
|
||||
validation_data=([te_pairs[:, 0], te_pairs[:, 1]], te_y))
|
||||
|
||||
# compute final accuracy on training and test sets
|
||||
pred = g.predict({'input_a': tr_pairs[:, 0], 'input_b': tr_pairs[:, 1]})['output']
|
||||
pred = model.predict([tr_pairs[:, 0], tr_pairs[:, 1]])
|
||||
tr_acc = compute_accuracy(pred, tr_y)
|
||||
pred = g.predict({'input_a': te_pairs[:, 0], 'input_b': te_pairs[:, 1]})['output']
|
||||
pred = model.predict([te_pairs[:, 0], te_pairs[:, 1]])
|
||||
te_acc = compute_accuracy(pred, te_y)
|
||||
|
||||
print('* Accuracy on training set: %0.2f%%' % (100 * tr_acc))
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
'''Example of how to use sklearn wrapper
|
||||
|
||||
Builds simple CNN models on MNIST and uses sklearn's GridSearchCV to find best model
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import keras
|
||||
from keras.datasets import mnist
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Dropout, Activation, Flatten
|
||||
from keras.layers import Conv2D, MaxPooling2D
|
||||
from keras.wrappers.scikit_learn import KerasClassifier
|
||||
from keras import backend as K
|
||||
from sklearn.grid_search import GridSearchCV
|
||||
|
||||
|
||||
num_classes = 10
|
||||
|
||||
# input image dimensions
|
||||
img_rows, img_cols = 28, 28
|
||||
|
||||
# load training data and do basic data normalization
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
|
||||
if K.image_data_format() == 'channels_first':
|
||||
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
|
||||
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
|
||||
input_shape = (1, img_rows, img_cols)
|
||||
else:
|
||||
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
|
||||
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
|
||||
input_shape = (img_rows, img_cols, 1)
|
||||
|
||||
x_train = x_train.astype('float32')
|
||||
x_test = x_test.astype('float32')
|
||||
x_train /= 255
|
||||
x_test /= 255
|
||||
|
||||
# convert class vectors to binary class matrices
|
||||
y_train = keras.utils.to_categorical(y_train, num_classes)
|
||||
y_test = keras.utils.to_categorical(y_test, num_classes)
|
||||
|
||||
|
||||
def make_model(dense_layer_sizes, filters, kernel_size, pool_size):
|
||||
'''Creates model comprised of 2 convolutional layers followed by dense layers
|
||||
|
||||
dense_layer_sizes: List of layer sizes.
|
||||
This list has one number for each layer
|
||||
filters: Number of convolutional filters in each convolutional layer
|
||||
kernel_size: Convolutional kernel size
|
||||
pool_size: Size of pooling area for max pooling
|
||||
'''
|
||||
|
||||
model = Sequential()
|
||||
model.add(Conv2D(filters, kernel_size,
|
||||
padding='valid',
|
||||
input_shape=input_shape))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Conv2D(filters, kernel_size))
|
||||
model.add(Activation('relu'))
|
||||
model.add(MaxPooling2D(pool_size=pool_size))
|
||||
model.add(Dropout(0.25))
|
||||
|
||||
model.add(Flatten())
|
||||
for layer_size in dense_layer_sizes:
|
||||
model.add(Dense(layer_size))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(num_classes))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer='adadelta',
|
||||
metrics=['accuracy'])
|
||||
|
||||
return model
|
||||
|
||||
dense_size_candidates = [[32], [64], [32, 32], [64, 64]]
|
||||
my_classifier = KerasClassifier(make_model, batch_size=32)
|
||||
validator = GridSearchCV(my_classifier,
|
||||
param_grid={'dense_layer_sizes': dense_size_candidates,
|
||||
# epochs is avail for tuning even when not
|
||||
# an argument to model building function
|
||||
'epochs': [3, 6],
|
||||
'filters': [8],
|
||||
'kernel_size': [3],
|
||||
'pool_size': [2]},
|
||||
scoring='neg_log_loss',
|
||||
n_jobs=1)
|
||||
validator.fit(x_train, y_train)
|
||||
|
||||
print('The parameters of the best model are: ')
|
||||
print(validator.best_params_)
|
||||
|
||||
# validator.best_estimator_ returns sklearn-wrapped version of best model.
|
||||
# validator.best_estimator_.model returns the (unwrapped) keras model
|
||||
best_model = validator.best_estimator_.model
|
||||
metric_names = best_model.metrics_names
|
||||
metric_values = best_model.evaluate(x_test, y_test)
|
||||
for metric, value in zip(metric_names, metric_values):
|
||||
print(metric, ': ', value)
|
||||
@@ -0,0 +1,203 @@
|
||||
'''Trains a stacked what-where autoencoder built on residual blocks on the
|
||||
MNIST dataset. It exemplifies two influential methods that have been developed
|
||||
in the past few years.
|
||||
|
||||
The first is the idea of properly 'unpooling.' During any max pool, the
|
||||
exact location (the 'where') of the maximal value in a pooled receptive field
|
||||
is lost, however it can be very useful in the overall reconstruction of an
|
||||
input image. Therefore, if the 'where' is handed from the encoder
|
||||
to the corresponding decoder layer, features being decoded can be 'placed' in
|
||||
the right location, allowing for reconstructions of much higher fidelity.
|
||||
|
||||
References:
|
||||
[1]
|
||||
'Visualizing and Understanding Convolutional Networks'
|
||||
Matthew D Zeiler, Rob Fergus
|
||||
https://arxiv.org/abs/1311.2901v3
|
||||
|
||||
[2]
|
||||
'Stacked What-Where Auto-encoders'
|
||||
Junbo Zhao, Michael Mathieu, Ross Goroshin, Yann LeCun
|
||||
https://arxiv.org/abs/1506.02351v8
|
||||
|
||||
The second idea exploited here is that of residual learning. Residual blocks
|
||||
ease the training process by allowing skip connections that give the network
|
||||
the ability to be as linear (or non-linear) as the data sees fit. This allows
|
||||
for much deep networks to be easily trained. The residual element seems to
|
||||
be advantageous in the context of this example as it allows a nice symmetry
|
||||
between the encoder and decoder. Normally, in the decoder, the final
|
||||
projection to the space where the image is reconstructed is linear, however
|
||||
this does not have to be the case for a residual block as the degree to which
|
||||
its output is linear or non-linear is determined by the data it is fed.
|
||||
However, in order to cap the reconstruction in this example, a hard softmax is
|
||||
applied as a bias because we know the MNIST digits are mapped to [0,1].
|
||||
|
||||
References:
|
||||
[3]
|
||||
'Deep Residual Learning for Image Recognition'
|
||||
Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
|
||||
https://arxiv.org/abs/1512.03385v1
|
||||
|
||||
[4]
|
||||
'Identity Mappings in Deep Residual Networks'
|
||||
Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
|
||||
https://arxiv.org/abs/1603.05027v3
|
||||
|
||||
'''
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
|
||||
from keras.datasets import mnist
|
||||
from keras.models import Model
|
||||
from keras.layers import Activation
|
||||
from keras.layers import UpSampling2D, Conv2D, MaxPooling2D
|
||||
from keras.layers import Input, BatchNormalization, ELU
|
||||
import matplotlib.pyplot as plt
|
||||
import keras.backend as K
|
||||
from keras import layers
|
||||
|
||||
|
||||
def convresblock(x, nfeats=8, ksize=3, nskipped=2, elu=True):
|
||||
"""The proposed residual block from [4].
|
||||
|
||||
Running with elu=True will use ELU nonlinearity and running with
|
||||
elu=False will use BatchNorm + RELU nonlinearity. While ELU's are fast
|
||||
due to the fact they do not suffer from BatchNorm overhead, they may
|
||||
overfit because they do not offer the stochastic element of the batch
|
||||
formation process of BatchNorm, which acts as a good regularizer.
|
||||
|
||||
# Arguments
|
||||
x: 4D tensor, the tensor to feed through the block
|
||||
nfeats: Integer, number of feature maps for conv layers.
|
||||
ksize: Integer, width and height of conv kernels in first convolution.
|
||||
nskipped: Integer, number of conv layers for the residual function.
|
||||
elu: Boolean, whether to use ELU or BN+RELU.
|
||||
|
||||
# Input shape
|
||||
4D tensor with shape:
|
||||
`(batch, channels, rows, cols)`
|
||||
|
||||
# Output shape
|
||||
4D tensor with shape:
|
||||
`(batch, filters, rows, cols)`
|
||||
"""
|
||||
y0 = Conv2D(nfeats, ksize, padding='same')(x)
|
||||
y = y0
|
||||
for i in range(nskipped):
|
||||
if elu:
|
||||
y = ELU()(y)
|
||||
else:
|
||||
y = BatchNormalization(axis=1)(y)
|
||||
y = Activation('relu')(y)
|
||||
y = Conv2D(nfeats, 1, padding='same')(y)
|
||||
return layers.add([y0, y])
|
||||
|
||||
|
||||
def getwhere(x):
|
||||
''' Calculate the 'where' mask that contains switches indicating which
|
||||
index contained the max value when MaxPool2D was applied. Using the
|
||||
gradient of the sum is a nice trick to keep everything high level.'''
|
||||
y_prepool, y_postpool = x
|
||||
return K.gradients(K.sum(y_postpool), y_prepool)
|
||||
|
||||
if K.backend() == 'tensorflow':
|
||||
raise RuntimeError('This example can only run with the '
|
||||
'Theano backend for the time being, '
|
||||
'because it requires taking the gradient '
|
||||
'of a gradient, which isn\'t '
|
||||
'supported for all TF ops.')
|
||||
|
||||
# This example assume 'channels_first' data format.
|
||||
K.set_image_data_format('channels_first')
|
||||
|
||||
# input image dimensions
|
||||
img_rows, img_cols = 28, 28
|
||||
|
||||
# the data, shuffled and split between train and test sets
|
||||
(x_train, _), (x_test, _) = mnist.load_data()
|
||||
|
||||
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
|
||||
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
|
||||
x_train = x_train.astype('float32')
|
||||
x_test = x_test.astype('float32')
|
||||
x_train /= 255
|
||||
x_test /= 255
|
||||
print('x_train shape:', x_train.shape)
|
||||
print(x_train.shape[0], 'train samples')
|
||||
print(x_test.shape[0], 'test samples')
|
||||
|
||||
# The size of the kernel used for the MaxPooling2D
|
||||
pool_size = 2
|
||||
# The total number of feature maps at each layer
|
||||
nfeats = [8, 16, 32, 64, 128]
|
||||
# The sizes of the pooling kernel at each layer
|
||||
pool_sizes = np.array([1, 1, 1, 1, 1]) * pool_size
|
||||
# The convolution kernel size
|
||||
ksize = 3
|
||||
# Number of epochs to train for
|
||||
epochs = 5
|
||||
# Batch size during training
|
||||
batch_size = 128
|
||||
|
||||
if pool_size == 2:
|
||||
# if using a 5 layer net of pool_size = 2
|
||||
x_train = np.pad(x_train, [[0, 0], [0, 0], [2, 2], [2, 2]],
|
||||
mode='constant')
|
||||
x_test = np.pad(x_test, [[0, 0], [0, 0], [2, 2], [2, 2]], mode='constant')
|
||||
nlayers = 5
|
||||
elif pool_size == 3:
|
||||
# if using a 3 layer net of pool_size = 3
|
||||
x_train = x_train[:, :, :-1, :-1]
|
||||
x_test = x_test[:, :, :-1, :-1]
|
||||
nlayers = 3
|
||||
else:
|
||||
import sys
|
||||
sys.exit('Script supports pool_size of 2 and 3.')
|
||||
|
||||
# Shape of input to train on (note that model is fully convolutional however)
|
||||
input_shape = x_train.shape[1:]
|
||||
# The final list of the size of axis=1 for all layers, including input
|
||||
nfeats_all = [input_shape[0]] + nfeats
|
||||
|
||||
# First build the encoder, all the while keeping track of the 'where' masks
|
||||
img_input = Input(shape=input_shape)
|
||||
|
||||
# We push the 'where' masks to the following list
|
||||
wheres = [None] * nlayers
|
||||
y = img_input
|
||||
for i in range(nlayers):
|
||||
y_prepool = convresblock(y, nfeats=nfeats_all[i + 1], ksize=ksize)
|
||||
y = MaxPooling2D(pool_size=(pool_sizes[i], pool_sizes[i]))(y_prepool)
|
||||
wheres[i] = layers.Lambda(
|
||||
getwhere, output_shape=lambda x: x[0])([y_prepool, y])
|
||||
|
||||
# Now build the decoder, and use the stored 'where' masks to place the features
|
||||
for i in range(nlayers):
|
||||
ind = nlayers - 1 - i
|
||||
y = UpSampling2D(size=(pool_sizes[ind], pool_sizes[ind]))(y)
|
||||
y = layers.multiply([y, wheres[ind]])
|
||||
y = convresblock(y, nfeats=nfeats_all[ind], ksize=ksize)
|
||||
|
||||
# Use hard_simgoid to clip range of reconstruction
|
||||
y = Activation('hard_sigmoid')(y)
|
||||
|
||||
# Define the model and it's mean square error loss, and compile it with Adam
|
||||
model = Model(img_input, y)
|
||||
model.compile('adam', 'mse')
|
||||
|
||||
# Fit the model
|
||||
model.fit(x_train, x_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
validation_data=(x_test, x_test))
|
||||
|
||||
# Plot
|
||||
x_recon = model.predict(x_test[:25])
|
||||
x_plot = np.concatenate((x_test[:25], x_recon), axis=1)
|
||||
x_plot = x_plot.reshape((5, 10, input_shape[-2], input_shape[-1]))
|
||||
x_plot = np.vstack([np.hstack(x) for x in x_plot])
|
||||
plt.figure()
|
||||
plt.axis('off')
|
||||
plt.title('Test Samples: Originals/Reconstructions')
|
||||
plt.imshow(x_plot, interpolation='none', cmap='gray')
|
||||
plt.savefig('reconstructions.png')
|
||||
@@ -12,105 +12,109 @@ and 99.2% for the last five digits after transfer + fine-tuning.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
|
||||
import datetime
|
||||
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
import keras
|
||||
from keras.datasets import mnist
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Dropout, Activation, Flatten
|
||||
from keras.layers.convolutional import Convolution2D, MaxPooling2D
|
||||
from keras.utils import np_utils
|
||||
|
||||
from keras.layers import Dense, Dropout, Activation, Flatten
|
||||
from keras.layers import Conv2D, MaxPooling2D
|
||||
from keras import backend as K
|
||||
|
||||
now = datetime.datetime.now
|
||||
|
||||
batch_size = 128
|
||||
nb_classes = 5
|
||||
nb_epoch = 5
|
||||
num_classes = 5
|
||||
epochs = 5
|
||||
|
||||
# input image dimensions
|
||||
img_rows, img_cols = 28, 28
|
||||
# number of convolutional filters to use
|
||||
nb_filters = 32
|
||||
filters = 32
|
||||
# size of pooling area for max pooling
|
||||
nb_pool = 2
|
||||
pool_size = 2
|
||||
# convolution kernel size
|
||||
nb_conv = 3
|
||||
kernel_size = 3
|
||||
|
||||
if K.image_data_format() == 'channels_first':
|
||||
input_shape = (1, img_rows, img_cols)
|
||||
else:
|
||||
input_shape = (img_rows, img_cols, 1)
|
||||
|
||||
|
||||
def train_model(model, train, test, nb_classes):
|
||||
X_train = train[0].reshape(train[0].shape[0], 1, img_rows, img_cols)
|
||||
X_test = test[0].reshape(test[0].shape[0], 1, img_rows, img_cols)
|
||||
X_train = X_train.astype('float32')
|
||||
X_test = X_test.astype('float32')
|
||||
X_train /= 255
|
||||
X_test /= 255
|
||||
print('X_train shape:', X_train.shape)
|
||||
print(X_train.shape[0], 'train samples')
|
||||
print(X_test.shape[0], 'test samples')
|
||||
def train_model(model, train, test, num_classes):
|
||||
x_train = train[0].reshape((train[0].shape[0],) + input_shape)
|
||||
x_test = test[0].reshape((test[0].shape[0],) + input_shape)
|
||||
x_train = x_train.astype('float32')
|
||||
x_test = x_test.astype('float32')
|
||||
x_train /= 255
|
||||
x_test /= 255
|
||||
print('x_train shape:', x_train.shape)
|
||||
print(x_train.shape[0], 'train samples')
|
||||
print(x_test.shape[0], 'test samples')
|
||||
|
||||
# convert class vectors to binary class matrices
|
||||
Y_train = np_utils.to_categorical(train[1], nb_classes)
|
||||
Y_test = np_utils.to_categorical(test[1], nb_classes)
|
||||
y_train = keras.utils.to_categorical(train[1], num_classes)
|
||||
y_test = keras.utils.to_categorical(test[1], num_classes)
|
||||
|
||||
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer='adadelta',
|
||||
metrics=['accuracy'])
|
||||
|
||||
t = now()
|
||||
model.fit(X_train, Y_train,
|
||||
batch_size=batch_size, nb_epoch=nb_epoch,
|
||||
show_accuracy=True, verbose=1,
|
||||
validation_data=(X_test, Y_test))
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
verbose=1,
|
||||
validation_data=(x_test, y_test))
|
||||
print('Training time: %s' % (now() - t))
|
||||
score = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
|
||||
score = model.evaluate(x_test, y_test, verbose=0)
|
||||
print('Test score:', score[0])
|
||||
print('Test accuracy:', score[1])
|
||||
|
||||
|
||||
# the data, shuffled and split between train and test sets
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
|
||||
# create two datasets one with digits below 5 and one with 5 and above
|
||||
X_train_lt5 = X_train[y_train < 5]
|
||||
x_train_lt5 = x_train[y_train < 5]
|
||||
y_train_lt5 = y_train[y_train < 5]
|
||||
X_test_lt5 = X_test[y_test < 5]
|
||||
x_test_lt5 = x_test[y_test < 5]
|
||||
y_test_lt5 = y_test[y_test < 5]
|
||||
|
||||
X_train_gte5 = X_train[y_train >= 5]
|
||||
y_train_gte5 = y_train[y_train >= 5] - 5 # make classes start at 0 for
|
||||
X_test_gte5 = X_test[y_test >= 5] # np_utils.to_categorical
|
||||
x_train_gte5 = x_train[y_train >= 5]
|
||||
y_train_gte5 = y_train[y_train >= 5] - 5
|
||||
x_test_gte5 = x_test[y_test >= 5]
|
||||
y_test_gte5 = y_test[y_test >= 5] - 5
|
||||
|
||||
# define two groups of layers: feature (convolutions) and classification (dense)
|
||||
feature_layers = [
|
||||
Convolution2D(nb_filters, nb_conv, nb_conv,
|
||||
border_mode='valid',
|
||||
input_shape=(1, img_rows, img_cols)),
|
||||
Conv2D(filters, kernel_size,
|
||||
padding='valid',
|
||||
input_shape=input_shape),
|
||||
Activation('relu'),
|
||||
Convolution2D(nb_filters, nb_conv, nb_conv),
|
||||
Conv2D(filters, kernel_size),
|
||||
Activation('relu'),
|
||||
MaxPooling2D(pool_size=(nb_pool, nb_pool)),
|
||||
MaxPooling2D(pool_size=pool_size),
|
||||
Dropout(0.25),
|
||||
Flatten(),
|
||||
]
|
||||
|
||||
classification_layers = [
|
||||
Dense(128),
|
||||
Activation('relu'),
|
||||
Dropout(0.5),
|
||||
Dense(nb_classes),
|
||||
Dense(num_classes),
|
||||
Activation('softmax')
|
||||
]
|
||||
|
||||
# create complete model
|
||||
model = Sequential()
|
||||
for l in feature_layers + classification_layers:
|
||||
model.add(l)
|
||||
model = Sequential(feature_layers + classification_layers)
|
||||
|
||||
# train model for 5-digit classification [0..4]
|
||||
train_model(model,
|
||||
(X_train_lt5, y_train_lt5),
|
||||
(X_test_lt5, y_test_lt5), nb_classes)
|
||||
(x_train_lt5, y_train_lt5),
|
||||
(x_test_lt5, y_test_lt5), num_classes)
|
||||
|
||||
# freeze feature layers and rebuild model
|
||||
for l in feature_layers:
|
||||
@@ -118,5 +122,5 @@ for l in feature_layers:
|
||||
|
||||
# transfer: train dense layers for new classification task [5..9]
|
||||
train_model(model,
|
||||
(X_train_gte5, y_train_gte5),
|
||||
(X_test_gte5, y_test_gte5), nb_classes)
|
||||
(x_train_gte5, y_train_gte5),
|
||||
(x_test_gte5, y_test_gte5), num_classes)
|
||||
|
||||
@@ -0,0 +1,367 @@
|
||||
'''Neural doodle with Keras
|
||||
|
||||
Script Usage:
|
||||
# Arguments:
|
||||
```
|
||||
--nlabels: # of regions (colors) in mask images
|
||||
--style-image: image to learn style from
|
||||
--style-mask: semantic labels for style image
|
||||
--target-mask: semantic labels for target image (your doodle)
|
||||
--content-image: optional image to learn content from
|
||||
--target-image-prefix: path prefix for generated target images
|
||||
```
|
||||
|
||||
# Example 1: doodle using a style image, style mask
|
||||
and target mask.
|
||||
```
|
||||
python neural_doodle.py --nlabels 4 --style-image Monet/style.png \
|
||||
--style-mask Monet/style_mask.png --target-mask Monet/target_mask.png \
|
||||
--target-image-prefix generated/monet
|
||||
```
|
||||
|
||||
# Example 2: doodle using a style image, style mask,
|
||||
target mask and an optional content image.
|
||||
```
|
||||
python neural_doodle.py --nlabels 4 --style-image Renoir/style.png \
|
||||
--style-mask Renoir/style_mask.png --target-mask Renoir/target_mask.png \
|
||||
--content-image Renoir/creek.jpg \
|
||||
--target-image-prefix generated/renoir
|
||||
```
|
||||
|
||||
References:
|
||||
[Dmitry Ulyanov's blog on fast-neural-doodle](http://dmitryulyanov.github.io/feed-forward-neural-doodle/)
|
||||
[Torch code for fast-neural-doodle](https://github.com/DmitryUlyanov/fast-neural-doodle)
|
||||
[Torch code for online-neural-doodle](https://github.com/DmitryUlyanov/online-neural-doodle)
|
||||
[Paper Texture Networks: Feed-forward Synthesis of Textures and Stylized Images](http://arxiv.org/abs/1603.03417)
|
||||
[Discussion on parameter tuning](https://github.com/fchollet/keras/issues/3705)
|
||||
|
||||
Resources:
|
||||
Example images can be downloaded from
|
||||
https://github.com/DmitryUlyanov/fast-neural-doodle/tree/master/data
|
||||
'''
|
||||
from __future__ import print_function
|
||||
import time
|
||||
import argparse
|
||||
import numpy as np
|
||||
from scipy.optimize import fmin_l_bfgs_b
|
||||
from scipy.misc import imread, imsave
|
||||
|
||||
from keras import backend as K
|
||||
from keras.layers import Input, AveragePooling2D
|
||||
from keras.models import Model
|
||||
from keras.preprocessing.image import load_img, img_to_array
|
||||
from keras.applications import vgg19
|
||||
|
||||
# Command line arguments
|
||||
parser = argparse.ArgumentParser(description='Keras neural doodle example')
|
||||
parser.add_argument('--nlabels', type=int,
|
||||
help='number of semantic labels'
|
||||
' (regions in differnet colors)'
|
||||
' in style_mask/target_mask')
|
||||
parser.add_argument('--style-image', type=str,
|
||||
help='path to image to learn style from')
|
||||
parser.add_argument('--style-mask', type=str,
|
||||
help='path to semantic mask of style image')
|
||||
parser.add_argument('--target-mask', type=str,
|
||||
help='path to semantic mask of target image')
|
||||
parser.add_argument('--content-image', type=str, default=None,
|
||||
help='path to optional content image')
|
||||
parser.add_argument('--target-image-prefix', type=str,
|
||||
help='path prefix for generated results')
|
||||
args = parser.parse_args()
|
||||
|
||||
style_img_path = args.style_image
|
||||
style_mask_path = args.style_mask
|
||||
target_mask_path = args.target_mask
|
||||
content_img_path = args.content_image
|
||||
target_img_prefix = args.target_image_prefix
|
||||
use_content_img = content_img_path is not None
|
||||
|
||||
num_labels = args.nlabels
|
||||
num_colors = 3 # RGB
|
||||
# determine image sizes based on target_mask
|
||||
ref_img = imread(target_mask_path)
|
||||
img_nrows, img_ncols = ref_img.shape[:2]
|
||||
|
||||
total_variation_weight = 50.
|
||||
style_weight = 1.
|
||||
content_weight = 0.1 if use_content_img else 0
|
||||
|
||||
content_feature_layers = ['block5_conv2']
|
||||
# To get better generation qualities, use more conv layers for style features
|
||||
style_feature_layers = ['block1_conv1', 'block2_conv1', 'block3_conv1',
|
||||
'block4_conv1', 'block5_conv1']
|
||||
|
||||
|
||||
# helper functions for reading/processing images
|
||||
def preprocess_image(image_path):
|
||||
img = load_img(image_path, target_size=(img_nrows, img_ncols))
|
||||
img = img_to_array(img)
|
||||
img = np.expand_dims(img, axis=0)
|
||||
img = vgg19.preprocess_input(img)
|
||||
return img
|
||||
|
||||
|
||||
def deprocess_image(x):
|
||||
if K.image_data_format() == 'channels_first':
|
||||
x = x.reshape((3, img_nrows, img_ncols))
|
||||
x = x.transpose((1, 2, 0))
|
||||
else:
|
||||
x = x.reshape((img_nrows, img_ncols, 3))
|
||||
# Remove zero-center by mean pixel
|
||||
x[:, :, 0] += 103.939
|
||||
x[:, :, 1] += 116.779
|
||||
x[:, :, 2] += 123.68
|
||||
# 'BGR'->'RGB'
|
||||
x = x[:, :, ::-1]
|
||||
x = np.clip(x, 0, 255).astype('uint8')
|
||||
return x
|
||||
|
||||
|
||||
def kmeans(xs, k):
|
||||
assert xs.ndim == 2
|
||||
try:
|
||||
from sklearn.cluster import k_means
|
||||
_, labels, _ = k_means(xs.astype('float64'), k)
|
||||
except ImportError:
|
||||
from scipy.cluster.vq import kmeans2
|
||||
_, labels = kmeans2(xs, k, missing='raise')
|
||||
return labels
|
||||
|
||||
|
||||
def load_mask_labels():
|
||||
'''Load both target and style masks.
|
||||
A mask image (nr x nc) with m labels/colors will be loaded
|
||||
as a 4D boolean tensor: (1, m, nr, nc) for 'channels_first' or (1, nr, nc, m) for 'channels_last'
|
||||
'''
|
||||
target_mask_img = load_img(target_mask_path,
|
||||
target_size=(img_nrows, img_ncols))
|
||||
target_mask_img = img_to_array(target_mask_img)
|
||||
style_mask_img = load_img(style_mask_path,
|
||||
target_size=(img_nrows, img_ncols))
|
||||
style_mask_img = img_to_array(style_mask_img)
|
||||
if K.image_data_format() == 'channels_first':
|
||||
mask_vecs = np.vstack([style_mask_img.reshape((3, -1)).T,
|
||||
target_mask_img.reshape((3, -1)).T])
|
||||
else:
|
||||
mask_vecs = np.vstack([style_mask_img.reshape((-1, 3)),
|
||||
target_mask_img.reshape((-1, 3))])
|
||||
|
||||
labels = kmeans(mask_vecs, num_labels)
|
||||
style_mask_label = labels[:img_nrows *
|
||||
img_ncols].reshape((img_nrows, img_ncols))
|
||||
target_mask_label = labels[img_nrows *
|
||||
img_ncols:].reshape((img_nrows, img_ncols))
|
||||
|
||||
stack_axis = 0 if K.image_data_format() == 'channels_first' else -1
|
||||
style_mask = np.stack([style_mask_label == r for r in xrange(num_labels)],
|
||||
axis=stack_axis)
|
||||
target_mask = np.stack([target_mask_label == r for r in xrange(num_labels)],
|
||||
axis=stack_axis)
|
||||
|
||||
return (np.expand_dims(style_mask, axis=0),
|
||||
np.expand_dims(target_mask, axis=0))
|
||||
|
||||
# Create tensor variables for images
|
||||
if K.image_data_format() == 'channels_first':
|
||||
shape = (1, num_colors, img_nrows, img_ncols)
|
||||
else:
|
||||
shape = (1, img_nrows, img_ncols, num_colors)
|
||||
|
||||
style_image = K.variable(preprocess_image(style_img_path))
|
||||
target_image = K.placeholder(shape=shape)
|
||||
if use_content_img:
|
||||
content_image = K.variable(preprocess_image(content_img_path))
|
||||
else:
|
||||
content_image = K.zeros(shape=shape)
|
||||
|
||||
images = K.concatenate([style_image, target_image, content_image], axis=0)
|
||||
|
||||
# Create tensor variables for masks
|
||||
raw_style_mask, raw_target_mask = load_mask_labels()
|
||||
style_mask = K.variable(raw_style_mask.astype('float32'))
|
||||
target_mask = K.variable(raw_target_mask.astype('float32'))
|
||||
masks = K.concatenate([style_mask, target_mask], axis=0)
|
||||
|
||||
# index constants for images and tasks variables
|
||||
STYLE, TARGET, CONTENT = 0, 1, 2
|
||||
|
||||
# Build image model, mask model and use layer outputs as features
|
||||
# image model as VGG19
|
||||
image_model = vgg19.VGG19(include_top=False, input_tensor=images)
|
||||
|
||||
# mask model as a series of pooling
|
||||
mask_input = Input(tensor=masks, shape=(None, None, None), name='mask_input')
|
||||
x = mask_input
|
||||
for layer in image_model.layers[1:]:
|
||||
name = 'mask_%s' % layer.name
|
||||
if 'conv' in layer.name:
|
||||
x = AveragePooling2D((3, 3), padding='same', strides=(
|
||||
1, 1), name=name)(x)
|
||||
elif 'pool' in layer.name:
|
||||
x = AveragePooling2D((2, 2), name=name)(x)
|
||||
mask_model = Model(mask_input, x)
|
||||
|
||||
# Collect features from image_model and task_model
|
||||
image_features = {}
|
||||
mask_features = {}
|
||||
for img_layer, mask_layer in zip(image_model.layers, mask_model.layers):
|
||||
if 'conv' in img_layer.name:
|
||||
assert 'mask_' + img_layer.name == mask_layer.name
|
||||
layer_name = img_layer.name
|
||||
img_feat, mask_feat = img_layer.output, mask_layer.output
|
||||
image_features[layer_name] = img_feat
|
||||
mask_features[layer_name] = mask_feat
|
||||
|
||||
|
||||
# Define loss functions
|
||||
def gram_matrix(x):
|
||||
assert K.ndim(x) == 3
|
||||
features = K.batch_flatten(x)
|
||||
gram = K.dot(features, K.transpose(features))
|
||||
return gram
|
||||
|
||||
|
||||
def region_style_loss(style_image, target_image, style_mask, target_mask):
|
||||
'''Calculate style loss between style_image and target_image,
|
||||
for one common region specified by their (boolean) masks
|
||||
'''
|
||||
assert 3 == K.ndim(style_image) == K.ndim(target_image)
|
||||
assert 2 == K.ndim(style_mask) == K.ndim(target_mask)
|
||||
if K.image_data_format() == 'channels_first':
|
||||
masked_style = style_image * style_mask
|
||||
masked_target = target_image * target_mask
|
||||
num_channels = K.shape(style_image)[0]
|
||||
else:
|
||||
masked_style = K.permute_dimensions(
|
||||
style_image, (2, 0, 1)) * style_mask
|
||||
masked_target = K.permute_dimensions(
|
||||
target_image, (2, 0, 1)) * target_mask
|
||||
num_channels = K.shape(style_image)[-1]
|
||||
num_channels = K.cast(num_channels, dtype='float32')
|
||||
s = gram_matrix(masked_style) / K.mean(style_mask) / num_channels
|
||||
c = gram_matrix(masked_target) / K.mean(target_mask) / num_channels
|
||||
return K.mean(K.square(s - c))
|
||||
|
||||
|
||||
def style_loss(style_image, target_image, style_masks, target_masks):
|
||||
'''Calculate style loss between style_image and target_image,
|
||||
in all regions.
|
||||
'''
|
||||
assert 3 == K.ndim(style_image) == K.ndim(target_image)
|
||||
assert 3 == K.ndim(style_masks) == K.ndim(target_masks)
|
||||
loss = K.variable(0)
|
||||
for i in xrange(num_labels):
|
||||
if K.image_data_format() == 'channels_first':
|
||||
style_mask = style_masks[i, :, :]
|
||||
target_mask = target_masks[i, :, :]
|
||||
else:
|
||||
style_mask = style_masks[:, :, i]
|
||||
target_mask = target_masks[:, :, i]
|
||||
loss += region_style_loss(style_image,
|
||||
target_image, style_mask, target_mask)
|
||||
return loss
|
||||
|
||||
|
||||
def content_loss(content_image, target_image):
|
||||
return K.sum(K.square(target_image - content_image))
|
||||
|
||||
|
||||
def total_variation_loss(x):
|
||||
assert 4 == K.ndim(x)
|
||||
if K.image_data_format() == 'channels_first':
|
||||
a = K.square(x[:, :, :img_nrows - 1, :img_ncols - 1] -
|
||||
x[:, :, 1:, :img_ncols - 1])
|
||||
b = K.square(x[:, :, :img_nrows - 1, :img_ncols - 1] -
|
||||
x[:, :, :img_nrows - 1, 1:])
|
||||
else:
|
||||
a = K.square(x[:, :img_nrows - 1, :img_ncols - 1, :] -
|
||||
x[:, 1:, :img_ncols - 1, :])
|
||||
b = K.square(x[:, :img_nrows - 1, :img_ncols - 1, :] -
|
||||
x[:, :img_nrows - 1, 1:, :])
|
||||
return K.sum(K.pow(a + b, 1.25))
|
||||
|
||||
# Overall loss is the weighted sum of content_loss, style_loss and tv_loss
|
||||
# Each individual loss uses features from image/mask models.
|
||||
loss = K.variable(0)
|
||||
for layer in content_feature_layers:
|
||||
content_feat = image_features[layer][CONTENT, :, :, :]
|
||||
target_feat = image_features[layer][TARGET, :, :, :]
|
||||
loss += content_weight * content_loss(content_feat, target_feat)
|
||||
|
||||
for layer in style_feature_layers:
|
||||
style_feat = image_features[layer][STYLE, :, :, :]
|
||||
target_feat = image_features[layer][TARGET, :, :, :]
|
||||
style_masks = mask_features[layer][STYLE, :, :, :]
|
||||
target_masks = mask_features[layer][TARGET, :, :, :]
|
||||
sl = style_loss(style_feat, target_feat, style_masks, target_masks)
|
||||
loss += (style_weight / len(style_feature_layers)) * sl
|
||||
|
||||
loss += total_variation_weight * total_variation_loss(target_image)
|
||||
loss_grads = K.gradients(loss, target_image)
|
||||
|
||||
# Evaluator class for computing efficiency
|
||||
outputs = [loss]
|
||||
if isinstance(loss_grads, (list, tuple)):
|
||||
outputs += loss_grads
|
||||
else:
|
||||
outputs.append(loss_grads)
|
||||
|
||||
f_outputs = K.function([target_image], outputs)
|
||||
|
||||
|
||||
def eval_loss_and_grads(x):
|
||||
if K.image_data_format() == 'channels_first':
|
||||
x = x.reshape((1, 3, img_nrows, img_ncols))
|
||||
else:
|
||||
x = x.reshape((1, img_nrows, img_ncols, 3))
|
||||
outs = f_outputs([x])
|
||||
loss_value = outs[0]
|
||||
if len(outs[1:]) == 1:
|
||||
grad_values = outs[1].flatten().astype('float64')
|
||||
else:
|
||||
grad_values = np.array(outs[1:]).flatten().astype('float64')
|
||||
return loss_value, grad_values
|
||||
|
||||
|
||||
class Evaluator(object):
|
||||
|
||||
def __init__(self):
|
||||
self.loss_value = None
|
||||
self.grads_values = None
|
||||
|
||||
def loss(self, x):
|
||||
assert self.loss_value is None
|
||||
loss_value, grad_values = eval_loss_and_grads(x)
|
||||
self.loss_value = loss_value
|
||||
self.grad_values = grad_values
|
||||
return self.loss_value
|
||||
|
||||
def grads(self, x):
|
||||
assert self.loss_value is not None
|
||||
grad_values = np.copy(self.grad_values)
|
||||
self.loss_value = None
|
||||
self.grad_values = None
|
||||
return grad_values
|
||||
|
||||
evaluator = Evaluator()
|
||||
|
||||
# Generate images by iterative optimization
|
||||
if K.image_data_format() == 'channels_first':
|
||||
x = np.random.uniform(0, 255, (1, 3, img_nrows, img_ncols)) - 128.
|
||||
else:
|
||||
x = np.random.uniform(0, 255, (1, img_nrows, img_ncols, 3)) - 128.
|
||||
|
||||
for i in range(50):
|
||||
print('Start of iteration', i)
|
||||
start_time = time.time()
|
||||
x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(),
|
||||
fprime=evaluator.grads, maxfun=20)
|
||||
print('Current loss value:', min_val)
|
||||
# save current generated image
|
||||
img = deprocess_image(x.copy())
|
||||
fname = target_img_prefix + '_at_iteration_%d.png' % i
|
||||
imsave(fname, img)
|
||||
end_time = time.time()
|
||||
print('Image saved as', fname)
|
||||
print('Iteration %d completed in %ds' % (i, end_time - start_time))
|
||||
@@ -1,10 +1,5 @@
|
||||
'''Neural style transfer with Keras.
|
||||
|
||||
Before running this script, download the weights for the VGG16 model at:
|
||||
https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmRCM0ROVHc/view?usp=sharing
|
||||
(source: https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3)
|
||||
and make sure the variable `weights_path` in this script matches the location of the file.
|
||||
|
||||
Run the script with:
|
||||
```
|
||||
python neural_style_transfer.py path_to_your_base_image.jpg path_to_your_reference.jpg prefix_for_results
|
||||
@@ -13,9 +8,15 @@ e.g.:
|
||||
```
|
||||
python neural_style_transfer.py img/tuebingen.jpg img/starry_night.jpg results/my_result
|
||||
```
|
||||
Optional parameters:
|
||||
```
|
||||
--iter, To specify the number of iterations the style transfer takes place (Default is 10)
|
||||
--content_weight, The weight given to the content loss (Default is 0.025)
|
||||
--style_weight, The weight given to the style loss (Default is 1.0)
|
||||
--tv_weight, The weight given to the total variation loss (Default is 1.0)
|
||||
```
|
||||
|
||||
It is preferrable to run this script on GPU, for speed.
|
||||
If running on CPU, prefer the TensorFlow backend (much faster).
|
||||
It is preferable to run this script on GPU, for speed.
|
||||
|
||||
Example result: https://twitter.com/fchollet/status/686631033085677568
|
||||
|
||||
@@ -34,7 +35,7 @@ the pixels of the combination image, giving it visual coherence.
|
||||
|
||||
- The style loss is where the deep learning keeps in --that one is defined
|
||||
using a deep convolutional neural network. Precisely, it consists in a sum of
|
||||
L2 distances betwen the Gram matrices of the representations of
|
||||
L2 distances between the Gram matrices of the representations of
|
||||
the base image and the style reference image, extracted from
|
||||
different layers of a convnet (trained on ImageNet). The general idea
|
||||
is to capture color/texture information at different spatial
|
||||
@@ -49,16 +50,14 @@ keeping the generated image close enough to the original one.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
from scipy.misc import imread, imresize, imsave
|
||||
from keras.preprocessing.image import load_img, img_to_array
|
||||
from scipy.misc import imsave
|
||||
import numpy as np
|
||||
from scipy.optimize import fmin_l_bfgs_b
|
||||
import time
|
||||
import os
|
||||
import argparse
|
||||
import h5py
|
||||
|
||||
from keras.models import Sequential
|
||||
from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D
|
||||
from keras.applications import vgg19
|
||||
from keras import backend as K
|
||||
|
||||
parser = argparse.ArgumentParser(description='Neural style transfer with Keras.')
|
||||
@@ -68,33 +67,56 @@ parser.add_argument('style_reference_image_path', metavar='ref', type=str,
|
||||
help='Path to the style reference image.')
|
||||
parser.add_argument('result_prefix', metavar='res_prefix', type=str,
|
||||
help='Prefix for the saved results.')
|
||||
parser.add_argument('--iter', type=int, default=10, required=False,
|
||||
help='Number of iterations to run.')
|
||||
parser.add_argument('--content_weight', type=float, default=0.025, required=False,
|
||||
help='Content weight.')
|
||||
parser.add_argument('--style_weight', type=float, default=1.0, required=False,
|
||||
help='Style weight.')
|
||||
parser.add_argument('--tv_weight', type=float, default=1.0, required=False,
|
||||
help='Total Variation weight.')
|
||||
|
||||
args = parser.parse_args()
|
||||
base_image_path = args.base_image_path
|
||||
style_reference_image_path = args.style_reference_image_path
|
||||
result_prefix = args.result_prefix
|
||||
weights_path = 'vgg16_weights.h5'
|
||||
iterations = args.iter
|
||||
|
||||
# these are the weights of the different loss components
|
||||
total_variation_weight = 1.
|
||||
style_weight = 1.
|
||||
content_weight = 0.025
|
||||
total_variation_weight = args.tv_weight
|
||||
style_weight = args.style_weight
|
||||
content_weight = args.content_weight
|
||||
|
||||
# dimensions of the generated picture.
|
||||
img_width = 400
|
||||
img_height = 400
|
||||
assert img_height == img_width, 'Due to the use of the Gram matrix, width and height must match.'
|
||||
width, height = load_img(base_image_path).size
|
||||
img_nrows = 400
|
||||
img_ncols = int(width * img_nrows / height)
|
||||
|
||||
# util function to open, resize and format pictures into appropriate tensors
|
||||
|
||||
|
||||
def preprocess_image(image_path):
|
||||
img = imresize(imread(image_path), (img_width, img_height))
|
||||
img = img.transpose((2, 0, 1)).astype('float64')
|
||||
img = load_img(image_path, target_size=(img_nrows, img_ncols))
|
||||
img = img_to_array(img)
|
||||
img = np.expand_dims(img, axis=0)
|
||||
img = vgg19.preprocess_input(img)
|
||||
return img
|
||||
|
||||
# util function to convert a tensor into a valid image
|
||||
|
||||
|
||||
def deprocess_image(x):
|
||||
x = x.transpose((1, 2, 0))
|
||||
if K.image_data_format() == 'channels_first':
|
||||
x = x.reshape((3, img_nrows, img_ncols))
|
||||
x = x.transpose((1, 2, 0))
|
||||
else:
|
||||
x = x.reshape((img_nrows, img_ncols, 3))
|
||||
# Remove zero-center by mean pixel
|
||||
x[:, :, 0] += 103.939
|
||||
x[:, :, 1] += 116.779
|
||||
x[:, :, 2] += 123.68
|
||||
# 'BGR'->'RGB'
|
||||
x = x[:, :, ::-1]
|
||||
x = np.clip(x, 0, 255).astype('uint8')
|
||||
return x
|
||||
|
||||
@@ -103,7 +125,10 @@ base_image = K.variable(preprocess_image(base_image_path))
|
||||
style_reference_image = K.variable(preprocess_image(style_reference_image_path))
|
||||
|
||||
# this will contain our generated image
|
||||
combination_image = K.placeholder((1, 3, img_width, img_height))
|
||||
if K.image_data_format() == 'channels_first':
|
||||
combination_image = K.placeholder((1, 3, img_nrows, img_ncols))
|
||||
else:
|
||||
combination_image = K.placeholder((1, img_nrows, img_ncols, 3))
|
||||
|
||||
# combine the 3 images into a single Keras tensor
|
||||
input_tensor = K.concatenate([base_image,
|
||||
@@ -111,72 +136,26 @@ input_tensor = K.concatenate([base_image,
|
||||
combination_image], axis=0)
|
||||
|
||||
# build the VGG16 network with our 3 images as input
|
||||
first_layer = ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height))
|
||||
first_layer.input = input_tensor
|
||||
|
||||
model = Sequential()
|
||||
model.add(first_layer)
|
||||
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(64, 3, 3, activation='relu'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(128, 3, 3, activation='relu'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, 3, 3, activation='relu'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, 3, 3, activation='relu'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu'))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, 3, 3, activation='relu'))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
# load the weights of the VGG16 networks
|
||||
# (trained on ImageNet, won the ILSVRC competition in 2014)
|
||||
# note: when there is a complete match between your model definition
|
||||
# and your weight savefile, you can simply call model.load_weights(filename)
|
||||
assert os.path.exists(weights_path), 'Model weights not found (see "weights_path" variable in script).'
|
||||
f = h5py.File(weights_path)
|
||||
for k in range(f.attrs['nb_layers']):
|
||||
if k >= len(model.layers):
|
||||
# we don't look at the last (fully-connected) layers in the savefile
|
||||
break
|
||||
g = f['layer_{}'.format(k)]
|
||||
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
|
||||
model.layers[k].set_weights(weights)
|
||||
f.close()
|
||||
# the model will be loaded with pre-trained ImageNet weights
|
||||
model = vgg19.VGG19(input_tensor=input_tensor,
|
||||
weights='imagenet', include_top=False)
|
||||
print('Model loaded.')
|
||||
|
||||
# get the symbolic outputs of each "key" layer (we gave them unique names).
|
||||
outputs_dict = dict([(layer.name, layer.get_output()) for layer in model.layers])
|
||||
outputs_dict = dict([(layer.name, layer.output) for layer in model.layers])
|
||||
|
||||
# compute the neural style loss
|
||||
# first we need to define 4 util functions
|
||||
|
||||
# the gram matrix of an image tensor (feature-wise outer product)
|
||||
|
||||
|
||||
def gram_matrix(x):
|
||||
assert K.ndim(x) == 3
|
||||
features = K.batch_flatten(x)
|
||||
if K.image_data_format() == 'channels_first':
|
||||
features = K.batch_flatten(x)
|
||||
else:
|
||||
features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1)))
|
||||
gram = K.dot(features, K.transpose(features))
|
||||
return gram
|
||||
|
||||
@@ -185,38 +164,50 @@ def gram_matrix(x):
|
||||
# It is based on the gram matrices (which capture style) of
|
||||
# feature maps from the style reference image
|
||||
# and from the generated image
|
||||
|
||||
|
||||
def style_loss(style, combination):
|
||||
assert K.ndim(style) == 3
|
||||
assert K.ndim(combination) == 3
|
||||
S = gram_matrix(style)
|
||||
C = gram_matrix(combination)
|
||||
channels = 3
|
||||
size = img_width * img_height
|
||||
size = img_nrows * img_ncols
|
||||
return K.sum(K.square(S - C)) / (4. * (channels ** 2) * (size ** 2))
|
||||
|
||||
# an auxiliary loss function
|
||||
# designed to maintain the "content" of the
|
||||
# base image in the generated image
|
||||
|
||||
|
||||
def content_loss(base, combination):
|
||||
return K.sum(K.square(combination - base))
|
||||
|
||||
# the 3rd loss function, total variation loss,
|
||||
# designed to keep the generated image locally coherent
|
||||
|
||||
|
||||
def total_variation_loss(x):
|
||||
assert K.ndim(x) == 4
|
||||
a = K.square(x[:, :, :img_width-1, :img_height-1] - x[:, :, 1:, :img_height-1])
|
||||
b = K.square(x[:, :, :img_width-1, :img_height-1] - x[:, :, :img_width-1, 1:])
|
||||
if K.image_data_format() == 'channels_first':
|
||||
a = K.square(x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, 1:, :img_ncols - 1])
|
||||
b = K.square(x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, :img_nrows - 1, 1:])
|
||||
else:
|
||||
a = K.square(x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, 1:, :img_ncols - 1, :])
|
||||
b = K.square(x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, :img_nrows - 1, 1:, :])
|
||||
return K.sum(K.pow(a + b, 1.25))
|
||||
|
||||
# combine these loss functions into a single scalar
|
||||
loss = K.variable(0.)
|
||||
layer_features = outputs_dict['conv4_2']
|
||||
layer_features = outputs_dict['block5_conv2']
|
||||
base_image_features = layer_features[0, :, :, :]
|
||||
combination_features = layer_features[2, :, :, :]
|
||||
loss += content_weight * content_loss(base_image_features,
|
||||
combination_features)
|
||||
|
||||
feature_layers = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1']
|
||||
feature_layers = ['block1_conv1', 'block2_conv1',
|
||||
'block3_conv1', 'block4_conv1',
|
||||
'block5_conv1']
|
||||
for layer_name in feature_layers:
|
||||
layer_features = outputs_dict[layer_name]
|
||||
style_reference_features = layer_features[1, :, :, :]
|
||||
@@ -229,14 +220,19 @@ loss += total_variation_weight * total_variation_loss(combination_image)
|
||||
grads = K.gradients(loss, combination_image)
|
||||
|
||||
outputs = [loss]
|
||||
if type(grads) in {list, tuple}:
|
||||
if isinstance(grads, (list, tuple)):
|
||||
outputs += grads
|
||||
else:
|
||||
outputs.append(grads)
|
||||
|
||||
f_outputs = K.function([combination_image], outputs)
|
||||
|
||||
|
||||
def eval_loss_and_grads(x):
|
||||
x = x.reshape((1, 3, img_width, img_height))
|
||||
if K.image_data_format() == 'channels_first':
|
||||
x = x.reshape((1, 3, img_nrows, img_ncols))
|
||||
else:
|
||||
x = x.reshape((1, img_nrows, img_ncols, 3))
|
||||
outs = f_outputs([x])
|
||||
loss_value = outs[0]
|
||||
if len(outs[1:]) == 1:
|
||||
@@ -251,7 +247,10 @@ def eval_loss_and_grads(x):
|
||||
# "loss" and "grads". This is done because scipy.optimize
|
||||
# requires separate functions for loss and gradients,
|
||||
# but computing them separately would be inefficient.
|
||||
|
||||
|
||||
class Evaluator(object):
|
||||
|
||||
def __init__(self):
|
||||
self.loss_value = None
|
||||
self.grads_values = None
|
||||
@@ -274,15 +273,16 @@ evaluator = Evaluator()
|
||||
|
||||
# run scipy-based optimization (L-BFGS) over the pixels of the generated image
|
||||
# so as to minimize the neural style loss
|
||||
x = np.random.uniform(0, 255, (1, 3, img_width, img_height))
|
||||
for i in range(10):
|
||||
x = preprocess_image(base_image_path)
|
||||
|
||||
for i in range(iterations):
|
||||
print('Start of iteration', i)
|
||||
start_time = time.time()
|
||||
x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(),
|
||||
fprime=evaluator.grads, maxfun=20)
|
||||
print('Current loss value:', min_val)
|
||||
# save current generated image
|
||||
img = deprocess_image(x.reshape((3, img_width, img_height)))
|
||||
img = deprocess_image(x.copy())
|
||||
fname = result_prefix + '_at_iteration_%d.png' % i
|
||||
imsave(fname, img)
|
||||
end_time = time.time()
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
'''This script loads pre-trained word embeddings (GloVe embeddings)
|
||||
into a frozen Keras Embedding layer, and uses it to
|
||||
train a text classification model on the 20 Newsgroup dataset
|
||||
(classication of newsgroup messages into 20 different categories).
|
||||
|
||||
GloVe embedding data can be found at:
|
||||
http://nlp.stanford.edu/data/glove.6B.zip
|
||||
(source page: http://nlp.stanford.edu/projects/glove/)
|
||||
|
||||
20 Newsgroup data can be found at:
|
||||
http://www.cs.cmu.edu/afs/cs.cmu.edu/project/theo-20/www/data/news20.html
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
import sys
|
||||
import numpy as np
|
||||
from keras.preprocessing.text import Tokenizer
|
||||
from keras.preprocessing.sequence import pad_sequences
|
||||
from keras.utils import to_categorical
|
||||
from keras.layers import Dense, Input, Flatten
|
||||
from keras.layers import Conv1D, MaxPooling1D, Embedding
|
||||
from keras.models import Model
|
||||
|
||||
|
||||
BASE_DIR = ''
|
||||
GLOVE_DIR = BASE_DIR + '/glove.6B/'
|
||||
TEXT_DATA_DIR = BASE_DIR + '/20_newsgroup/'
|
||||
MAX_SEQUENCE_LENGTH = 1000
|
||||
MAX_NB_WORDS = 20000
|
||||
EMBEDDING_DIM = 100
|
||||
VALIDATION_SPLIT = 0.2
|
||||
|
||||
# first, build index mapping words in the embeddings set
|
||||
# to their embedding vector
|
||||
|
||||
print('Indexing word vectors.')
|
||||
|
||||
embeddings_index = {}
|
||||
f = open(os.path.join(GLOVE_DIR, 'glove.6B.100d.txt'))
|
||||
for line in f:
|
||||
values = line.split()
|
||||
word = values[0]
|
||||
coefs = np.asarray(values[1:], dtype='float32')
|
||||
embeddings_index[word] = coefs
|
||||
f.close()
|
||||
|
||||
print('Found %s word vectors.' % len(embeddings_index))
|
||||
|
||||
# second, prepare text samples and their labels
|
||||
print('Processing text dataset')
|
||||
|
||||
texts = [] # list of text samples
|
||||
labels_index = {} # dictionary mapping label name to numeric id
|
||||
labels = [] # list of label ids
|
||||
for name in sorted(os.listdir(TEXT_DATA_DIR)):
|
||||
path = os.path.join(TEXT_DATA_DIR, name)
|
||||
if os.path.isdir(path):
|
||||
label_id = len(labels_index)
|
||||
labels_index[name] = label_id
|
||||
for fname in sorted(os.listdir(path)):
|
||||
if fname.isdigit():
|
||||
fpath = os.path.join(path, fname)
|
||||
if sys.version_info < (3,):
|
||||
f = open(fpath)
|
||||
else:
|
||||
f = open(fpath, encoding='latin-1')
|
||||
t = f.read()
|
||||
i = t.find('\n\n') # skip header
|
||||
if 0 < i:
|
||||
t = t[i:]
|
||||
texts.append(t)
|
||||
f.close()
|
||||
labels.append(label_id)
|
||||
|
||||
print('Found %s texts.' % len(texts))
|
||||
|
||||
# finally, vectorize the text samples into a 2D integer tensor
|
||||
tokenizer = Tokenizer(num_words=MAX_NB_WORDS)
|
||||
tokenizer.fit_on_texts(texts)
|
||||
sequences = tokenizer.texts_to_sequences(texts)
|
||||
|
||||
word_index = tokenizer.word_index
|
||||
print('Found %s unique tokens.' % len(word_index))
|
||||
|
||||
data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)
|
||||
|
||||
labels = to_categorical(np.asarray(labels))
|
||||
print('Shape of data tensor:', data.shape)
|
||||
print('Shape of label tensor:', labels.shape)
|
||||
|
||||
# split the data into a training set and a validation set
|
||||
indices = np.arange(data.shape[0])
|
||||
np.random.shuffle(indices)
|
||||
data = data[indices]
|
||||
labels = labels[indices]
|
||||
num_validation_samples = int(VALIDATION_SPLIT * data.shape[0])
|
||||
|
||||
x_train = data[:-num_validation_samples]
|
||||
y_train = labels[:-num_validation_samples]
|
||||
x_val = data[-num_validation_samples:]
|
||||
y_val = labels[-num_validation_samples:]
|
||||
|
||||
print('Preparing embedding matrix.')
|
||||
|
||||
# prepare embedding matrix
|
||||
num_words = min(MAX_NB_WORDS, len(word_index))
|
||||
embedding_matrix = np.zeros((num_words, EMBEDDING_DIM))
|
||||
for word, i in word_index.items():
|
||||
if i >= MAX_NB_WORDS:
|
||||
continue
|
||||
embedding_vector = embeddings_index.get(word)
|
||||
if embedding_vector is not None:
|
||||
# words not found in embedding index will be all-zeros.
|
||||
embedding_matrix[i] = embedding_vector
|
||||
|
||||
# load pre-trained word embeddings into an Embedding layer
|
||||
# note that we set trainable = False so as to keep the embeddings fixed
|
||||
embedding_layer = Embedding(num_words,
|
||||
EMBEDDING_DIM,
|
||||
weights=[embedding_matrix],
|
||||
input_length=MAX_SEQUENCE_LENGTH,
|
||||
trainable=False)
|
||||
|
||||
print('Training model.')
|
||||
|
||||
# train a 1D convnet with global maxpooling
|
||||
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
|
||||
embedded_sequences = embedding_layer(sequence_input)
|
||||
x = Conv1D(128, 5, activation='relu')(embedded_sequences)
|
||||
x = MaxPooling1D(5)(x)
|
||||
x = Conv1D(128, 5, activation='relu')(x)
|
||||
x = MaxPooling1D(5)(x)
|
||||
x = Conv1D(128, 5, activation='relu')(x)
|
||||
x = MaxPooling1D(35)(x)
|
||||
x = Flatten()(x)
|
||||
x = Dense(128, activation='relu')(x)
|
||||
preds = Dense(len(labels_index), activation='softmax')(x)
|
||||
|
||||
model = Model(sequence_input, preds)
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer='rmsprop',
|
||||
metrics=['acc'])
|
||||
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=128,
|
||||
epochs=10,
|
||||
validation_data=(x_val, y_val))
|
||||
+34
-31
@@ -1,57 +1,60 @@
|
||||
'''Train and evaluate a simple MLP on the Reuters newswire topic classification task.
|
||||
GPU run command:
|
||||
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python examples/reuters_mlp.py
|
||||
CPU run command:
|
||||
python examples/reuters_mlp.py
|
||||
'''Trains and evaluate a simple MLP
|
||||
on the Reuters newswire topic classification task.
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
np.random.seed(1337) # for reproducibility
|
||||
|
||||
import numpy as np
|
||||
import keras
|
||||
from keras.datasets import reuters
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Dropout, Activation
|
||||
from keras.layers.normalization import BatchNormalization
|
||||
from keras.utils import np_utils
|
||||
from keras.layers import Dense, Dropout, Activation
|
||||
from keras.preprocessing.text import Tokenizer
|
||||
|
||||
max_words = 1000
|
||||
batch_size = 32
|
||||
nb_epoch = 5
|
||||
epochs = 5
|
||||
|
||||
print('Loading data...')
|
||||
(X_train, y_train), (X_test, y_test) = reuters.load_data(nb_words=max_words, test_split=0.2)
|
||||
print(len(X_train), 'train sequences')
|
||||
print(len(X_test), 'test sequences')
|
||||
(x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=max_words,
|
||||
test_split=0.2)
|
||||
print(len(x_train), 'train sequences')
|
||||
print(len(x_test), 'test sequences')
|
||||
|
||||
nb_classes = np.max(y_train)+1
|
||||
print(nb_classes, 'classes')
|
||||
num_classes = np.max(y_train) + 1
|
||||
print(num_classes, 'classes')
|
||||
|
||||
print('Vectorizing sequence data...')
|
||||
tokenizer = Tokenizer(nb_words=max_words)
|
||||
X_train = tokenizer.sequences_to_matrix(X_train, mode='binary')
|
||||
X_test = tokenizer.sequences_to_matrix(X_test, mode='binary')
|
||||
print('X_train shape:', X_train.shape)
|
||||
print('X_test shape:', X_test.shape)
|
||||
tokenizer = Tokenizer(num_words=max_words)
|
||||
x_train = tokenizer.sequences_to_matrix(x_train, mode='binary')
|
||||
x_test = tokenizer.sequences_to_matrix(x_test, mode='binary')
|
||||
print('x_train shape:', x_train.shape)
|
||||
print('x_test shape:', x_test.shape)
|
||||
|
||||
print('Convert class vector to binary class matrix (for use with categorical_crossentropy)')
|
||||
Y_train = np_utils.to_categorical(y_train, nb_classes)
|
||||
Y_test = np_utils.to_categorical(y_test, nb_classes)
|
||||
print('Y_train shape:', Y_train.shape)
|
||||
print('Y_test shape:', Y_test.shape)
|
||||
print('Convert class vector to binary class matrix '
|
||||
'(for use with categorical_crossentropy)')
|
||||
y_train = keras.utils.to_categorical(y_train, num_classes)
|
||||
y_test = keras.utils.to_categorical(y_test, num_classes)
|
||||
print('y_train shape:', y_train.shape)
|
||||
print('y_test shape:', y_test.shape)
|
||||
|
||||
print('Building model...')
|
||||
model = Sequential()
|
||||
model.add(Dense(512, input_shape=(max_words,)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(nb_classes))
|
||||
model.add(Dense(num_classes))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
model.compile(loss='categorical_crossentropy', optimizer='adam')
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer='adam',
|
||||
metrics=['accuracy'])
|
||||
|
||||
history = model.fit(X_train, Y_train, nb_epoch=nb_epoch, batch_size=batch_size, verbose=1, show_accuracy=True, validation_split=0.1)
|
||||
score = model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=1, show_accuracy=True)
|
||||
history = model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
verbose=1,
|
||||
validation_split=0.1)
|
||||
score = model.evaluate(x_test, y_test,
|
||||
batch_size=batch_size, verbose=1)
|
||||
print('Test score:', score[0])
|
||||
print('Test accuracy:', score[1])
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
'''Compares self-normalizing MLPs with regular MLPs.
|
||||
|
||||
Compares the performance of a simple MLP using two
|
||||
different activation functions: RELU and SELU
|
||||
on the Reuters newswire topic classification task.
|
||||
|
||||
# Reference:
|
||||
Klambauer, G., Unterthiner, T., Mayr, A., & Hochreiter, S. (2017).
|
||||
Self-Normalizing Neural Networks. arXiv preprint arXiv:1706.02515.
|
||||
https://arxiv.org/abs/1706.02515
|
||||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import keras
|
||||
from keras.datasets import reuters
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense, Activation, Dropout
|
||||
from keras.layers.noise import AlphaDropout
|
||||
from keras.preprocessing.text import Tokenizer
|
||||
|
||||
max_words = 1000
|
||||
batch_size = 16
|
||||
epochs = 40
|
||||
plot = True
|
||||
|
||||
|
||||
def create_network(n_dense=6,
|
||||
dense_units=16,
|
||||
activation='selu',
|
||||
dropout=AlphaDropout,
|
||||
dropout_rate=0.1,
|
||||
kernel_initializer='lecun_normal',
|
||||
optimizer='adam',
|
||||
num_classes=1,
|
||||
max_words=max_words):
|
||||
"""Generic function to create a fully-connected neural network.
|
||||
|
||||
# Arguments
|
||||
n_dense: int > 0. Number of dense layers.
|
||||
dense_units: int > 0. Number of dense units per layer.
|
||||
dropout: keras.layers.Layer. A dropout layer to apply.
|
||||
dropout_rate: 0 <= float <= 1. The rate of dropout.
|
||||
kernel_initializer: str. The initializer for the weights.
|
||||
optimizer: str/keras.optimizers.Optimizer. The optimizer to use.
|
||||
num_classes: int > 0. The number of classes to predict.
|
||||
max_words: int > 0. The maximum number of words per data point.
|
||||
|
||||
# Returns
|
||||
A Keras model instance (compiled).
|
||||
"""
|
||||
model = Sequential()
|
||||
model.add(Dense(dense_units, input_shape=(max_words,),
|
||||
kernel_initializer=kernel_initializer))
|
||||
model.add(Activation(activation))
|
||||
model.add(dropout(dropout_rate))
|
||||
|
||||
for i in range(n_dense - 1):
|
||||
model.add(Dense(dense_units, kernel_initializer=kernel_initializer))
|
||||
model.add(Activation(activation))
|
||||
model.add(dropout(dropout_rate))
|
||||
|
||||
model.add(Dense(num_classes))
|
||||
model.add(Activation('softmax'))
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer=optimizer,
|
||||
metrics=['accuracy'])
|
||||
return model
|
||||
|
||||
|
||||
network1 = {
|
||||
'n_dense': 6,
|
||||
'dense_units': 16,
|
||||
'activation': 'relu',
|
||||
'dropout': Dropout,
|
||||
'dropout_rate': 0.5,
|
||||
'kernel_initializer': 'glorot_uniform',
|
||||
'optimizer': 'sgd'
|
||||
}
|
||||
|
||||
network2 = {
|
||||
'n_dense': 6,
|
||||
'dense_units': 16,
|
||||
'activation': 'selu',
|
||||
'dropout': AlphaDropout,
|
||||
'dropout_rate': 0.1,
|
||||
'kernel_initializer': 'lecun_normal',
|
||||
'optimizer': 'sgd'
|
||||
}
|
||||
|
||||
print('Loading data...')
|
||||
(x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=max_words,
|
||||
test_split=0.2)
|
||||
print(len(x_train), 'train sequences')
|
||||
print(len(x_test), 'test sequences')
|
||||
|
||||
num_classes = np.max(y_train) + 1
|
||||
print(num_classes, 'classes')
|
||||
|
||||
print('Vectorizing sequence data...')
|
||||
tokenizer = Tokenizer(num_words=max_words)
|
||||
x_train = tokenizer.sequences_to_matrix(x_train, mode='binary')
|
||||
x_test = tokenizer.sequences_to_matrix(x_test, mode='binary')
|
||||
print('x_train shape:', x_train.shape)
|
||||
print('x_test shape:', x_test.shape)
|
||||
|
||||
print('Convert class vector to binary class matrix '
|
||||
'(for use with categorical_crossentropy)')
|
||||
y_train = keras.utils.to_categorical(y_train, num_classes)
|
||||
y_test = keras.utils.to_categorical(y_test, num_classes)
|
||||
print('y_train shape:', y_train.shape)
|
||||
print('y_test shape:', y_test.shape)
|
||||
|
||||
print('\nBuilding network 1...')
|
||||
|
||||
model1 = create_network(num_classes=num_classes, **network1)
|
||||
history_model1 = model1.fit(x_train,
|
||||
y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
verbose=1,
|
||||
validation_split=0.1)
|
||||
|
||||
score_model1 = model1.evaluate(x_test,
|
||||
y_test,
|
||||
batch_size=batch_size,
|
||||
verbose=1)
|
||||
|
||||
|
||||
print('\nBuilding network 2...')
|
||||
model2 = create_network(num_classes=num_classes, **network2)
|
||||
|
||||
history_model2 = model2.fit(x_train,
|
||||
y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=epochs,
|
||||
verbose=1,
|
||||
validation_split=0.1)
|
||||
|
||||
score_model2 = model2.evaluate(x_test,
|
||||
y_test,
|
||||
batch_size=batch_size,
|
||||
verbose=1)
|
||||
|
||||
print('\nNetwork 1 results')
|
||||
print('Hyperparameters:', network1)
|
||||
print('Test score:', score_model1[0])
|
||||
print('Test accuracy:', score_model1[1])
|
||||
print('Network 2 results')
|
||||
print('Hyperparameters:', network2)
|
||||
print('Test score:', score_model2[0])
|
||||
print('Test accuracy:', score_model2[1])
|
||||
|
||||
plt.plot(range(epochs),
|
||||
history_model1.history['val_loss'],
|
||||
'g-',
|
||||
label='Network 1 Val Loss')
|
||||
plt.plot(range(epochs),
|
||||
history_model2.history['val_loss'],
|
||||
'r-',
|
||||
label='Network 2 Val Loss')
|
||||
plt.plot(range(epochs),
|
||||
history_model1.history['loss'],
|
||||
'g--',
|
||||
label='Network 1 Loss')
|
||||
plt.plot(range(epochs),
|
||||
history_model2.history['loss'],
|
||||
'r--',
|
||||
label='Network 2 Loss')
|
||||
plt.xlabel('Epochs')
|
||||
plt.ylabel('Loss')
|
||||
plt.legend()
|
||||
plt.savefig('comparison_of_networks.png')
|
||||
+19
-14
@@ -5,8 +5,7 @@ from __future__ import print_function
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense
|
||||
from keras.layers.recurrent import LSTM
|
||||
from keras.layers import Dense, LSTM
|
||||
|
||||
|
||||
# since we are using stateful rnn tsteps can be set to 1
|
||||
@@ -17,7 +16,7 @@ epochs = 25
|
||||
lahead = 1
|
||||
|
||||
|
||||
def gen_cosine_amp(amp=100, period=25, x0=0, xn=50000, step=1, k=0.0001):
|
||||
def gen_cosine_amp(amp=100, period=1000, x0=0, xn=50000, step=1, k=0.0001):
|
||||
"""Generates an absolute cosine time series with the amplitude
|
||||
exponentially decreasing
|
||||
|
||||
@@ -32,12 +31,12 @@ def gen_cosine_amp(amp=100, period=25, x0=0, xn=50000, step=1, k=0.0001):
|
||||
cos = np.zeros(((xn - x0) * step, 1, 1))
|
||||
for i in range(len(cos)):
|
||||
idx = x0 + i * step
|
||||
cos[i, 0, 0] = amp * np.cos(idx / (2 * np.pi * period))
|
||||
cos[i, 0, 0] = amp * np.cos(2 * np.pi * idx / period)
|
||||
cos[i, 0, 0] = cos[i, 0, 0] * np.exp(-k * idx)
|
||||
return cos
|
||||
|
||||
|
||||
print('Generating Data')
|
||||
print('Generating Data...')
|
||||
cos = gen_cosine_amp()
|
||||
print('Input shape:', cos.shape)
|
||||
|
||||
@@ -45,17 +44,16 @@ expected_output = np.zeros((len(cos), 1))
|
||||
for i in range(len(cos) - lahead):
|
||||
expected_output[i, 0] = np.mean(cos[i + 1:i + lahead + 1])
|
||||
|
||||
print('Output shape')
|
||||
print(expected_output.shape)
|
||||
print('Output shape:', expected_output.shape)
|
||||
|
||||
print('Creating Model')
|
||||
print('Creating Model...')
|
||||
model = Sequential()
|
||||
model.add(LSTM(50,
|
||||
batch_input_shape=(batch_size, tsteps, 1),
|
||||
input_shape=(tsteps, 1),
|
||||
batch_size=batch_size,
|
||||
return_sequences=True,
|
||||
stateful=True))
|
||||
model.add(LSTM(50,
|
||||
batch_input_shape=(batch_size, tsteps, 1),
|
||||
return_sequences=False,
|
||||
stateful=True))
|
||||
model.add(Dense(1))
|
||||
@@ -64,18 +62,25 @@ model.compile(loss='mse', optimizer='rmsprop')
|
||||
print('Training')
|
||||
for i in range(epochs):
|
||||
print('Epoch', i, '/', epochs)
|
||||
model.fit(cos,
|
||||
expected_output,
|
||||
|
||||
# Note that the last state for sample i in a batch will
|
||||
# be used as initial state for sample i in the next batch.
|
||||
# Thus we are simultaneously training on batch_size series with
|
||||
# lower resolution than the original series contained in cos.
|
||||
# Each of these series are offset by one step and can be
|
||||
# extracted with cos[i::batch_size].
|
||||
|
||||
model.fit(cos, expected_output,
|
||||
batch_size=batch_size,
|
||||
epochs=1,
|
||||
verbose=1,
|
||||
nb_epoch=1,
|
||||
shuffle=False)
|
||||
model.reset_states()
|
||||
|
||||
print('Predicting')
|
||||
predicted_output = model.predict(cos, batch_size=batch_size)
|
||||
|
||||
print('Ploting Results')
|
||||
print('Plotting Results')
|
||||
plt.subplot(2, 1, 1)
|
||||
plt.plot(expected_output)
|
||||
plt.title('Expected')
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
'''This script demonstrates how to build a variational autoencoder with Keras.
|
||||
|
||||
Reference: "Auto-Encoding Variational Bayes" https://arxiv.org/abs/1312.6114
|
||||
'''
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from scipy.stats import norm
|
||||
|
||||
from keras.layers import Input, Dense, Lambda, Layer
|
||||
from keras.models import Model
|
||||
from keras import backend as K
|
||||
from keras import metrics
|
||||
from keras.datasets import mnist
|
||||
|
||||
batch_size = 100
|
||||
original_dim = 784
|
||||
latent_dim = 2
|
||||
intermediate_dim = 256
|
||||
epochs = 50
|
||||
epsilon_std = 1.0
|
||||
|
||||
|
||||
x = Input(batch_shape=(batch_size, original_dim))
|
||||
h = Dense(intermediate_dim, activation='relu')(x)
|
||||
z_mean = Dense(latent_dim)(h)
|
||||
z_log_var = Dense(latent_dim)(h)
|
||||
|
||||
|
||||
def sampling(args):
|
||||
z_mean, z_log_var = args
|
||||
epsilon = K.random_normal(shape=(batch_size, latent_dim), mean=0.,
|
||||
stddev=epsilon_std)
|
||||
return z_mean + K.exp(z_log_var / 2) * epsilon
|
||||
|
||||
# note that "output_shape" isn't necessary with the TensorFlow backend
|
||||
z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])
|
||||
|
||||
# we instantiate these layers separately so as to reuse them later
|
||||
decoder_h = Dense(intermediate_dim, activation='relu')
|
||||
decoder_mean = Dense(original_dim, activation='sigmoid')
|
||||
h_decoded = decoder_h(z)
|
||||
x_decoded_mean = decoder_mean(h_decoded)
|
||||
|
||||
|
||||
# Custom loss layer
|
||||
class CustomVariationalLayer(Layer):
|
||||
def __init__(self, **kwargs):
|
||||
self.is_placeholder = True
|
||||
super(CustomVariationalLayer, self).__init__(**kwargs)
|
||||
|
||||
def vae_loss(self, x, x_decoded_mean):
|
||||
xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
|
||||
kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
|
||||
return K.mean(xent_loss + kl_loss)
|
||||
|
||||
def call(self, inputs):
|
||||
x = inputs[0]
|
||||
x_decoded_mean = inputs[1]
|
||||
loss = self.vae_loss(x, x_decoded_mean)
|
||||
self.add_loss(loss, inputs=inputs)
|
||||
# We won't actually use the output.
|
||||
return x
|
||||
|
||||
y = CustomVariationalLayer()([x, x_decoded_mean])
|
||||
vae = Model(x, y)
|
||||
vae.compile(optimizer='rmsprop', loss=None)
|
||||
|
||||
|
||||
# train the VAE on MNIST digits
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
|
||||
x_train = x_train.astype('float32') / 255.
|
||||
x_test = x_test.astype('float32') / 255.
|
||||
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
|
||||
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
|
||||
|
||||
vae.fit(x_train,
|
||||
shuffle=True,
|
||||
epochs=epochs,
|
||||
batch_size=batch_size,
|
||||
validation_data=(x_test, x_test))
|
||||
|
||||
# build a model to project inputs on the latent space
|
||||
encoder = Model(x, z_mean)
|
||||
|
||||
# display a 2D plot of the digit classes in the latent space
|
||||
x_test_encoded = encoder.predict(x_test, batch_size=batch_size)
|
||||
plt.figure(figsize=(6, 6))
|
||||
plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)
|
||||
plt.colorbar()
|
||||
plt.show()
|
||||
|
||||
# build a digit generator that can sample from the learned distribution
|
||||
decoder_input = Input(shape=(latent_dim,))
|
||||
_h_decoded = decoder_h(decoder_input)
|
||||
_x_decoded_mean = decoder_mean(_h_decoded)
|
||||
generator = Model(decoder_input, _x_decoded_mean)
|
||||
|
||||
# display a 2D manifold of the digits
|
||||
n = 15 # figure with 15x15 digits
|
||||
digit_size = 28
|
||||
figure = np.zeros((digit_size * n, digit_size * n))
|
||||
# linearly spaced coordinates on the unit square were transformed through the inverse CDF (ppf) of the Gaussian
|
||||
# to produce values of the latent variables z, since the prior of the latent space is Gaussian
|
||||
grid_x = norm.ppf(np.linspace(0.05, 0.95, n))
|
||||
grid_y = norm.ppf(np.linspace(0.05, 0.95, n))
|
||||
|
||||
for i, yi in enumerate(grid_x):
|
||||
for j, xi in enumerate(grid_y):
|
||||
z_sample = np.array([[xi, yi]])
|
||||
x_decoded = generator.predict(z_sample)
|
||||
digit = x_decoded[0].reshape(digit_size, digit_size)
|
||||
figure[i * digit_size: (i + 1) * digit_size,
|
||||
j * digit_size: (j + 1) * digit_size] = digit
|
||||
|
||||
plt.figure(figsize=(10, 10))
|
||||
plt.imshow(figure, cmap='Greys_r')
|
||||
plt.show()
|
||||
@@ -0,0 +1,194 @@
|
||||
'''This script demonstrates how to build a variational autoencoder
|
||||
with Keras and deconvolution layers.
|
||||
|
||||
Reference: "Auto-Encoding Variational Bayes" https://arxiv.org/abs/1312.6114
|
||||
'''
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from scipy.stats import norm
|
||||
|
||||
from keras.layers import Input, Dense, Lambda, Flatten, Reshape, Layer
|
||||
from keras.layers import Conv2D, Conv2DTranspose
|
||||
from keras.models import Model
|
||||
from keras import backend as K
|
||||
from keras import metrics
|
||||
from keras.datasets import mnist
|
||||
|
||||
# input image dimensions
|
||||
img_rows, img_cols, img_chns = 28, 28, 1
|
||||
# number of convolutional filters to use
|
||||
filters = 64
|
||||
# convolution kernel size
|
||||
num_conv = 3
|
||||
|
||||
batch_size = 100
|
||||
if K.image_data_format() == 'channels_first':
|
||||
original_img_size = (img_chns, img_rows, img_cols)
|
||||
else:
|
||||
original_img_size = (img_rows, img_cols, img_chns)
|
||||
latent_dim = 2
|
||||
intermediate_dim = 128
|
||||
epsilon_std = 1.0
|
||||
epochs = 5
|
||||
|
||||
x = Input(batch_shape=(batch_size,) + original_img_size)
|
||||
conv_1 = Conv2D(img_chns,
|
||||
kernel_size=(2, 2),
|
||||
padding='same', activation='relu')(x)
|
||||
conv_2 = Conv2D(filters,
|
||||
kernel_size=(2, 2),
|
||||
padding='same', activation='relu',
|
||||
strides=(2, 2))(conv_1)
|
||||
conv_3 = Conv2D(filters,
|
||||
kernel_size=num_conv,
|
||||
padding='same', activation='relu',
|
||||
strides=1)(conv_2)
|
||||
conv_4 = Conv2D(filters,
|
||||
kernel_size=num_conv,
|
||||
padding='same', activation='relu',
|
||||
strides=1)(conv_3)
|
||||
flat = Flatten()(conv_4)
|
||||
hidden = Dense(intermediate_dim, activation='relu')(flat)
|
||||
|
||||
z_mean = Dense(latent_dim)(hidden)
|
||||
z_log_var = Dense(latent_dim)(hidden)
|
||||
|
||||
|
||||
def sampling(args):
|
||||
z_mean, z_log_var = args
|
||||
epsilon = K.random_normal(shape=(batch_size, latent_dim),
|
||||
mean=0., stddev=epsilon_std)
|
||||
return z_mean + K.exp(z_log_var) * epsilon
|
||||
|
||||
# note that "output_shape" isn't necessary with the TensorFlow backend
|
||||
# so you could write `Lambda(sampling)([z_mean, z_log_var])`
|
||||
z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])
|
||||
|
||||
# we instantiate these layers separately so as to reuse them later
|
||||
decoder_hid = Dense(intermediate_dim, activation='relu')
|
||||
decoder_upsample = Dense(filters * 14 * 14, activation='relu')
|
||||
|
||||
if K.image_data_format() == 'channels_first':
|
||||
output_shape = (batch_size, filters, 14, 14)
|
||||
else:
|
||||
output_shape = (batch_size, 14, 14, filters)
|
||||
|
||||
decoder_reshape = Reshape(output_shape[1:])
|
||||
decoder_deconv_1 = Conv2DTranspose(filters,
|
||||
kernel_size=num_conv,
|
||||
padding='same',
|
||||
strides=1,
|
||||
activation='relu')
|
||||
decoder_deconv_2 = Conv2DTranspose(filters,
|
||||
kernel_size=num_conv,
|
||||
padding='same',
|
||||
strides=1,
|
||||
activation='relu')
|
||||
if K.image_data_format() == 'channels_first':
|
||||
output_shape = (batch_size, filters, 29, 29)
|
||||
else:
|
||||
output_shape = (batch_size, 29, 29, filters)
|
||||
decoder_deconv_3_upsamp = Conv2DTranspose(filters,
|
||||
kernel_size=(3, 3),
|
||||
strides=(2, 2),
|
||||
padding='valid',
|
||||
activation='relu')
|
||||
decoder_mean_squash = Conv2D(img_chns,
|
||||
kernel_size=2,
|
||||
padding='valid',
|
||||
activation='sigmoid')
|
||||
|
||||
hid_decoded = decoder_hid(z)
|
||||
up_decoded = decoder_upsample(hid_decoded)
|
||||
reshape_decoded = decoder_reshape(up_decoded)
|
||||
deconv_1_decoded = decoder_deconv_1(reshape_decoded)
|
||||
deconv_2_decoded = decoder_deconv_2(deconv_1_decoded)
|
||||
x_decoded_relu = decoder_deconv_3_upsamp(deconv_2_decoded)
|
||||
x_decoded_mean_squash = decoder_mean_squash(x_decoded_relu)
|
||||
|
||||
|
||||
# Custom loss layer
|
||||
class CustomVariationalLayer(Layer):
|
||||
def __init__(self, **kwargs):
|
||||
self.is_placeholder = True
|
||||
super(CustomVariationalLayer, self).__init__(**kwargs)
|
||||
|
||||
def vae_loss(self, x, x_decoded_mean_squash):
|
||||
x = K.flatten(x)
|
||||
x_decoded_mean_squash = K.flatten(x_decoded_mean_squash)
|
||||
xent_loss = img_rows * img_cols * metrics.binary_crossentropy(x, x_decoded_mean_squash)
|
||||
kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
|
||||
return K.mean(xent_loss + kl_loss)
|
||||
|
||||
def call(self, inputs):
|
||||
x = inputs[0]
|
||||
x_decoded_mean_squash = inputs[1]
|
||||
loss = self.vae_loss(x, x_decoded_mean_squash)
|
||||
self.add_loss(loss, inputs=inputs)
|
||||
# We don't use this output.
|
||||
return x
|
||||
|
||||
|
||||
y = CustomVariationalLayer()([x, x_decoded_mean_squash])
|
||||
vae = Model(x, y)
|
||||
vae.compile(optimizer='rmsprop', loss=None)
|
||||
vae.summary()
|
||||
|
||||
# train the VAE on MNIST digits
|
||||
(x_train, _), (x_test, y_test) = mnist.load_data()
|
||||
|
||||
x_train = x_train.astype('float32') / 255.
|
||||
x_train = x_train.reshape((x_train.shape[0],) + original_img_size)
|
||||
x_test = x_test.astype('float32') / 255.
|
||||
x_test = x_test.reshape((x_test.shape[0],) + original_img_size)
|
||||
|
||||
print('x_train.shape:', x_train.shape)
|
||||
|
||||
vae.fit(x_train,
|
||||
shuffle=True,
|
||||
epochs=epochs,
|
||||
batch_size=batch_size,
|
||||
validation_data=(x_test, x_test))
|
||||
|
||||
# build a model to project inputs on the latent space
|
||||
encoder = Model(x, z_mean)
|
||||
|
||||
# display a 2D plot of the digit classes in the latent space
|
||||
x_test_encoded = encoder.predict(x_test, batch_size=batch_size)
|
||||
plt.figure(figsize=(6, 6))
|
||||
plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)
|
||||
plt.colorbar()
|
||||
plt.show()
|
||||
|
||||
# build a digit generator that can sample from the learned distribution
|
||||
decoder_input = Input(shape=(latent_dim,))
|
||||
_hid_decoded = decoder_hid(decoder_input)
|
||||
_up_decoded = decoder_upsample(_hid_decoded)
|
||||
_reshape_decoded = decoder_reshape(_up_decoded)
|
||||
_deconv_1_decoded = decoder_deconv_1(_reshape_decoded)
|
||||
_deconv_2_decoded = decoder_deconv_2(_deconv_1_decoded)
|
||||
_x_decoded_relu = decoder_deconv_3_upsamp(_deconv_2_decoded)
|
||||
_x_decoded_mean_squash = decoder_mean_squash(_x_decoded_relu)
|
||||
generator = Model(decoder_input, _x_decoded_mean_squash)
|
||||
|
||||
# display a 2D manifold of the digits
|
||||
n = 15 # figure with 15x15 digits
|
||||
digit_size = 28
|
||||
figure = np.zeros((digit_size * n, digit_size * n))
|
||||
# linearly spaced coordinates on the unit square were transformed through the inverse CDF (ppf) of the Gaussian
|
||||
# to produce values of the latent variables z, since the prior of the latent space is Gaussian
|
||||
grid_x = norm.ppf(np.linspace(0.05, 0.95, n))
|
||||
grid_y = norm.ppf(np.linspace(0.05, 0.95, n))
|
||||
|
||||
for i, yi in enumerate(grid_x):
|
||||
for j, xi in enumerate(grid_y):
|
||||
z_sample = np.array([[xi, yi]])
|
||||
z_sample = np.tile(z_sample, batch_size).reshape(batch_size, 2)
|
||||
x_decoded = generator.predict(z_sample, batch_size=batch_size)
|
||||
digit = x_decoded[0].reshape(digit_size, digit_size)
|
||||
figure[i * digit_size: (i + 1) * digit_size,
|
||||
j * digit_size: (j + 1) * digit_size] = digit
|
||||
|
||||
plt.figure(figsize=(10, 10))
|
||||
plt.imshow(figure, cmap='Greys_r')
|
||||
plt.show()
|
||||
+23
-1
@@ -1 +1,23 @@
|
||||
__version__ = '0.3.3'
|
||||
from __future__ import absolute_import
|
||||
|
||||
from . import utils
|
||||
from . import activations
|
||||
from . import applications
|
||||
from . import backend
|
||||
from . import datasets
|
||||
from . import engine
|
||||
from . import layers
|
||||
from . import preprocessing
|
||||
from . import wrappers
|
||||
from . import callbacks
|
||||
from . import constraints
|
||||
from . import initializers
|
||||
from . import metrics
|
||||
from . import models
|
||||
from . import losses
|
||||
from . import optimizers
|
||||
from . import regularizers
|
||||
# Importable from root because it's technically not a layer
|
||||
from .layers import Input
|
||||
|
||||
__version__ = '2.0.5'
|
||||
|
||||
+71
-11
@@ -1,24 +1,61 @@
|
||||
from __future__ import absolute_import
|
||||
import six
|
||||
import warnings
|
||||
from . import backend as K
|
||||
from .utils.generic_utils import deserialize_keras_object
|
||||
from .engine import Layer
|
||||
|
||||
|
||||
def softmax(x):
|
||||
def softmax(x, axis=-1):
|
||||
"""Softmax activation function.
|
||||
|
||||
# Arguments
|
||||
x : Tensor.
|
||||
axis: Integer, axis along which the softmax normalization is applied.
|
||||
|
||||
# Returns
|
||||
Tensor, output of softmax transformation.
|
||||
|
||||
# Raises
|
||||
ValueError: In case `dim(x) == 1`.
|
||||
"""
|
||||
ndim = K.ndim(x)
|
||||
if ndim == 2:
|
||||
return K.softmax(x)
|
||||
elif ndim == 3:
|
||||
e = K.exp(x - K.max(x, axis=-1, keepdims=True))
|
||||
s = K.sum(e, axis=-1, keepdims=True)
|
||||
elif ndim > 2:
|
||||
e = K.exp(x - K.max(x, axis=axis, keepdims=True))
|
||||
s = K.sum(e, axis=axis, keepdims=True)
|
||||
return e / s
|
||||
else:
|
||||
raise Exception('Cannot apply softmax to a tensor that is not 2D or 3D. ' +
|
||||
'Here, ndim=' + str(ndim))
|
||||
raise ValueError('Cannot apply softmax to a tensor that is 1D')
|
||||
|
||||
|
||||
def elu(x, alpha=1.0):
|
||||
return K.elu(x, alpha)
|
||||
|
||||
|
||||
def selu(x):
|
||||
"""Scaled Exponential Linear Unit. (Klambauer et al., 2017)
|
||||
|
||||
# Arguments
|
||||
x: A tensor or variable to compute the activation function for.
|
||||
|
||||
# References
|
||||
- [Self-Normalizing Neural Networks](https://arxiv.org/abs/1706.02515)
|
||||
"""
|
||||
alpha = 1.6732632423543772848170429916717
|
||||
scale = 1.0507009873554804934193349852946
|
||||
return scale * K.elu(x, alpha)
|
||||
|
||||
|
||||
def softplus(x):
|
||||
return K.softplus(x)
|
||||
|
||||
|
||||
def softsign(x):
|
||||
return K.softsign(x)
|
||||
|
||||
|
||||
def relu(x, alpha=0., max_value=None):
|
||||
return K.relu(x, alpha=alpha, max_value=max_value)
|
||||
|
||||
@@ -36,12 +73,35 @@ def hard_sigmoid(x):
|
||||
|
||||
|
||||
def linear(x):
|
||||
'''
|
||||
The function returns the variable that is passed in, so all types work.
|
||||
'''
|
||||
return x
|
||||
|
||||
|
||||
from .utils.generic_utils import get_from_module
|
||||
def serialize(activation):
|
||||
return activation.__name__
|
||||
|
||||
|
||||
def deserialize(name, custom_objects=None):
|
||||
return deserialize_keras_object(name,
|
||||
module_objects=globals(),
|
||||
custom_objects=custom_objects,
|
||||
printable_module_name='activation function')
|
||||
|
||||
|
||||
def get(identifier):
|
||||
return get_from_module(identifier, globals(), 'activation function')
|
||||
if identifier is None:
|
||||
return linear
|
||||
if isinstance(identifier, six.string_types):
|
||||
identifier = str(identifier)
|
||||
return deserialize(identifier)
|
||||
elif callable(identifier):
|
||||
if isinstance(identifier, Layer):
|
||||
warnings.warn((
|
||||
'Do not pass a layer instance (such as {identifier}) as the '
|
||||
'activation argument of another layer. Instead, advanced '
|
||||
'activation layers should be used just like any other '
|
||||
'layer in a model.'
|
||||
).format(identifier=identifier.__class__.__name__))
|
||||
return identifier
|
||||
else:
|
||||
raise ValueError('Could not interpret '
|
||||
'activation function identifier:', identifier)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
from .vgg16 import VGG16
|
||||
from .vgg19 import VGG19
|
||||
from .resnet50 import ResNet50
|
||||
from .inception_v3 import InceptionV3
|
||||
from .xception import Xception
|
||||
from .mobilenet import MobileNet
|
||||
@@ -0,0 +1,138 @@
|
||||
import json
|
||||
|
||||
from ..utils.data_utils import get_file
|
||||
from .. import backend as K
|
||||
|
||||
CLASS_INDEX = None
|
||||
CLASS_INDEX_PATH = 'https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json'
|
||||
|
||||
|
||||
def preprocess_input(x, data_format=None):
|
||||
"""Preprocesses a tensor encoding a batch of images.
|
||||
|
||||
# Arguments
|
||||
x: input Numpy tensor, 4D.
|
||||
data_format: data format of the image tensor.
|
||||
|
||||
# Returns
|
||||
Preprocessed tensor.
|
||||
"""
|
||||
if data_format is None:
|
||||
data_format = K.image_data_format()
|
||||
assert data_format in {'channels_last', 'channels_first'}
|
||||
|
||||
if data_format == 'channels_first':
|
||||
# 'RGB'->'BGR'
|
||||
x = x[:, ::-1, :, :]
|
||||
# Zero-center by mean pixel
|
||||
x[:, 0, :, :] -= 103.939
|
||||
x[:, 1, :, :] -= 116.779
|
||||
x[:, 2, :, :] -= 123.68
|
||||
else:
|
||||
# 'RGB'->'BGR'
|
||||
x = x[:, :, :, ::-1]
|
||||
# Zero-center by mean pixel
|
||||
x[:, :, :, 0] -= 103.939
|
||||
x[:, :, :, 1] -= 116.779
|
||||
x[:, :, :, 2] -= 123.68
|
||||
return x
|
||||
|
||||
|
||||
def decode_predictions(preds, top=5):
|
||||
"""Decodes the prediction of an ImageNet model.
|
||||
|
||||
# Arguments
|
||||
preds: Numpy tensor encoding a batch of predictions.
|
||||
top: integer, how many top-guesses to return.
|
||||
|
||||
# Returns
|
||||
A list of lists of top class prediction tuples
|
||||
`(class_name, class_description, score)`.
|
||||
One list of tuples per sample in batch input.
|
||||
|
||||
# Raises
|
||||
ValueError: in case of invalid shape of the `pred` array
|
||||
(must be 2D).
|
||||
"""
|
||||
global CLASS_INDEX
|
||||
if len(preds.shape) != 2 or preds.shape[1] != 1000:
|
||||
raise ValueError('`decode_predictions` expects '
|
||||
'a batch of predictions '
|
||||
'(i.e. a 2D array of shape (samples, 1000)). '
|
||||
'Found array with shape: ' + str(preds.shape))
|
||||
if CLASS_INDEX is None:
|
||||
fpath = get_file('imagenet_class_index.json',
|
||||
CLASS_INDEX_PATH,
|
||||
cache_subdir='models')
|
||||
CLASS_INDEX = json.load(open(fpath))
|
||||
results = []
|
||||
for pred in preds:
|
||||
top_indices = pred.argsort()[-top:][::-1]
|
||||
result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices]
|
||||
result.sort(key=lambda x: x[2], reverse=True)
|
||||
results.append(result)
|
||||
return results
|
||||
|
||||
|
||||
def _obtain_input_shape(input_shape,
|
||||
default_size,
|
||||
min_size,
|
||||
data_format,
|
||||
include_top):
|
||||
"""Internal utility to compute/validate an ImageNet model's input shape.
|
||||
|
||||
# Arguments
|
||||
input_shape: either None (will return the default network input shape),
|
||||
or a user-provided shape to be validated.
|
||||
default_size: default input width/height for the model.
|
||||
min_size: minimum input width/height accepted by the model.
|
||||
data_format: image data format to use.
|
||||
include_top: whether the model is expected to
|
||||
be linked to a classifier via a Flatten layer.
|
||||
|
||||
# Returns
|
||||
An integer shape tuple (may include None entries).
|
||||
|
||||
# Raises
|
||||
ValueError: in case of invalid argument values.
|
||||
"""
|
||||
if data_format == 'channels_first':
|
||||
default_shape = (3, default_size, default_size)
|
||||
else:
|
||||
default_shape = (default_size, default_size, 3)
|
||||
if include_top:
|
||||
if input_shape is not None:
|
||||
if input_shape != default_shape:
|
||||
raise ValueError('When setting`include_top=True`, '
|
||||
'`input_shape` should be ' + str(default_shape) + '.')
|
||||
input_shape = default_shape
|
||||
else:
|
||||
if data_format == 'channels_first':
|
||||
if input_shape is not None:
|
||||
if len(input_shape) != 3:
|
||||
raise ValueError('`input_shape` must be a tuple of three integers.')
|
||||
if input_shape[0] != 3:
|
||||
raise ValueError('The input must have 3 channels; got '
|
||||
'`input_shape=' + str(input_shape) + '`')
|
||||
if ((input_shape[1] is not None and input_shape[1] < min_size) or
|
||||
(input_shape[2] is not None and input_shape[2] < min_size)):
|
||||
raise ValueError('Input size must be at least ' +
|
||||
str(min_size) + 'x' + str(min_size) + ', got '
|
||||
'`input_shape=' + str(input_shape) + '`')
|
||||
else:
|
||||
input_shape = (3, None, None)
|
||||
else:
|
||||
if input_shape is not None:
|
||||
if len(input_shape) != 3:
|
||||
raise ValueError('`input_shape` must be a tuple of three integers.')
|
||||
if input_shape[-1] != 3:
|
||||
raise ValueError('The input must have 3 channels; got '
|
||||
'`input_shape=' + str(input_shape) + '`')
|
||||
if ((input_shape[0] is not None and input_shape[0] < min_size) or
|
||||
(input_shape[1] is not None and input_shape[1] < min_size)):
|
||||
raise ValueError('Input size must be at least ' +
|
||||
str(min_size) + 'x' + str(min_size) + ', got '
|
||||
'`input_shape=' + str(input_shape) + '`')
|
||||
else:
|
||||
input_shape = (None, None, 3)
|
||||
return input_shape
|
||||
@@ -0,0 +1,393 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Inception V3 model for Keras.
|
||||
|
||||
Note that the input image format for this model is different than for
|
||||
the VGG16 and ResNet models (299x299 instead of 224x224),
|
||||
and that the input preprocessing function is also different (same as Xception).
|
||||
|
||||
# Reference
|
||||
|
||||
- [Rethinking the Inception Architecture for Computer Vision](http://arxiv.org/abs/1512.00567)
|
||||
|
||||
"""
|
||||
from __future__ import print_function
|
||||
from __future__ import absolute_import
|
||||
|
||||
import warnings
|
||||
|
||||
from ..models import Model
|
||||
from .. import layers
|
||||
from ..layers import Activation
|
||||
from ..layers import Dense
|
||||
from ..layers import Input
|
||||
from ..layers import BatchNormalization
|
||||
from ..layers import Conv2D
|
||||
from ..layers import MaxPooling2D
|
||||
from ..layers import AveragePooling2D
|
||||
from ..layers import GlobalAveragePooling2D
|
||||
from ..layers import GlobalMaxPooling2D
|
||||
from ..engine.topology import get_source_inputs
|
||||
from ..utils.data_utils import get_file
|
||||
from .. import backend as K
|
||||
from .imagenet_utils import decode_predictions
|
||||
from .imagenet_utils import _obtain_input_shape
|
||||
|
||||
|
||||
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels.h5'
|
||||
WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'
|
||||
|
||||
|
||||
def conv2d_bn(x,
|
||||
filters,
|
||||
num_row,
|
||||
num_col,
|
||||
padding='same',
|
||||
strides=(1, 1),
|
||||
name=None):
|
||||
"""Utility function to apply conv + BN.
|
||||
|
||||
# Arguments
|
||||
x: input tensor.
|
||||
filters: filters in `Conv2D`.
|
||||
num_row: height of the convolution kernel.
|
||||
num_col: width of the convolution kernel.
|
||||
padding: padding mode in `Conv2D`.
|
||||
strides: strides in `Conv2D`.
|
||||
name: name of the ops; will become `name + '_conv'`
|
||||
for the convolution and `name + '_bn'` for the
|
||||
batch norm layer.
|
||||
|
||||
# Returns
|
||||
Output tensor after applying `Conv2D` and `BatchNormalization`.
|
||||
"""
|
||||
if name is not None:
|
||||
bn_name = name + '_bn'
|
||||
conv_name = name + '_conv'
|
||||
else:
|
||||
bn_name = None
|
||||
conv_name = None
|
||||
if K.image_data_format() == 'channels_first':
|
||||
bn_axis = 1
|
||||
else:
|
||||
bn_axis = 3
|
||||
x = Conv2D(
|
||||
filters, (num_row, num_col),
|
||||
strides=strides,
|
||||
padding=padding,
|
||||
use_bias=False,
|
||||
name=conv_name)(x)
|
||||
x = BatchNormalization(axis=bn_axis, scale=False, name=bn_name)(x)
|
||||
x = Activation('relu', name=name)(x)
|
||||
return x
|
||||
|
||||
|
||||
def InceptionV3(include_top=True,
|
||||
weights='imagenet',
|
||||
input_tensor=None,
|
||||
input_shape=None,
|
||||
pooling=None,
|
||||
classes=1000):
|
||||
"""Instantiates the Inception v3 architecture.
|
||||
|
||||
Optionally loads weights pre-trained
|
||||
on ImageNet. Note that when using TensorFlow,
|
||||
for best performance you should set
|
||||
`image_data_format="channels_last"` in your Keras config
|
||||
at ~/.keras/keras.json.
|
||||
The model and the weights are compatible with both
|
||||
TensorFlow and Theano. The data format
|
||||
convention used by the model is the one
|
||||
specified in your Keras config file.
|
||||
Note that the default input image size for this model is 299x299.
|
||||
|
||||
# Arguments
|
||||
include_top: whether to include the fully-connected
|
||||
layer at the top of the network.
|
||||
weights: one of `None` (random initialization)
|
||||
or "imagenet" (pre-training on ImageNet).
|
||||
input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
|
||||
to use as image input for the model.
|
||||
input_shape: optional shape tuple, only to be specified
|
||||
if `include_top` is False (otherwise the input shape
|
||||
has to be `(299, 299, 3)` (with `channels_last` data format)
|
||||
or `(3, 299, 299)` (with `channels_first` data format).
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 139.
|
||||
E.g. `(150, 150, 3)` would be one valid value.
|
||||
pooling: Optional pooling mode for feature extraction
|
||||
when `include_top` is `False`.
|
||||
- `None` means that the output of the model will be
|
||||
the 4D tensor output of the
|
||||
last convolutional layer.
|
||||
- `avg` means that global average pooling
|
||||
will be applied to the output of the
|
||||
last convolutional layer, and thus
|
||||
the output of the model will be a 2D tensor.
|
||||
- `max` means that global max pooling will
|
||||
be applied.
|
||||
classes: optional number of classes to classify images
|
||||
into, only to be specified if `include_top` is True, and
|
||||
if no `weights` argument is specified.
|
||||
|
||||
# Returns
|
||||
A Keras model instance.
|
||||
|
||||
# Raises
|
||||
ValueError: in case of invalid argument for `weights`,
|
||||
or invalid input shape.
|
||||
"""
|
||||
if weights not in {'imagenet', None}:
|
||||
raise ValueError('The `weights` argument should be either '
|
||||
'`None` (random initialization) or `imagenet` '
|
||||
'(pre-training on ImageNet).')
|
||||
|
||||
if weights == 'imagenet' and include_top and classes != 1000:
|
||||
raise ValueError('If using `weights` as imagenet with `include_top`'
|
||||
' as true, `classes` should be 1000')
|
||||
|
||||
# Determine proper input shape
|
||||
input_shape = _obtain_input_shape(
|
||||
input_shape,
|
||||
default_size=299,
|
||||
min_size=139,
|
||||
data_format=K.image_data_format(),
|
||||
include_top=include_top)
|
||||
|
||||
if input_tensor is None:
|
||||
img_input = Input(shape=input_shape)
|
||||
else:
|
||||
if not K.is_keras_tensor(input_tensor):
|
||||
img_input = Input(tensor=input_tensor, shape=input_shape)
|
||||
else:
|
||||
img_input = input_tensor
|
||||
|
||||
if K.image_data_format() == 'channels_first':
|
||||
channel_axis = 1
|
||||
else:
|
||||
channel_axis = 3
|
||||
|
||||
x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid')
|
||||
x = conv2d_bn(x, 32, 3, 3, padding='valid')
|
||||
x = conv2d_bn(x, 64, 3, 3)
|
||||
x = MaxPooling2D((3, 3), strides=(2, 2))(x)
|
||||
|
||||
x = conv2d_bn(x, 80, 1, 1, padding='valid')
|
||||
x = conv2d_bn(x, 192, 3, 3, padding='valid')
|
||||
x = MaxPooling2D((3, 3), strides=(2, 2))(x)
|
||||
|
||||
# mixed 0, 1, 2: 35 x 35 x 256
|
||||
branch1x1 = conv2d_bn(x, 64, 1, 1)
|
||||
|
||||
branch5x5 = conv2d_bn(x, 48, 1, 1)
|
||||
branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)
|
||||
|
||||
branch3x3dbl = conv2d_bn(x, 64, 1, 1)
|
||||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
|
||||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
|
||||
|
||||
branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
|
||||
branch_pool = conv2d_bn(branch_pool, 32, 1, 1)
|
||||
x = layers.concatenate(
|
||||
[branch1x1, branch5x5, branch3x3dbl, branch_pool],
|
||||
axis=channel_axis,
|
||||
name='mixed0')
|
||||
|
||||
# mixed 1: 35 x 35 x 256
|
||||
branch1x1 = conv2d_bn(x, 64, 1, 1)
|
||||
|
||||
branch5x5 = conv2d_bn(x, 48, 1, 1)
|
||||
branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)
|
||||
|
||||
branch3x3dbl = conv2d_bn(x, 64, 1, 1)
|
||||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
|
||||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
|
||||
|
||||
branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
|
||||
branch_pool = conv2d_bn(branch_pool, 64, 1, 1)
|
||||
x = layers.concatenate(
|
||||
[branch1x1, branch5x5, branch3x3dbl, branch_pool],
|
||||
axis=channel_axis,
|
||||
name='mixed1')
|
||||
|
||||
# mixed 2: 35 x 35 x 256
|
||||
branch1x1 = conv2d_bn(x, 64, 1, 1)
|
||||
|
||||
branch5x5 = conv2d_bn(x, 48, 1, 1)
|
||||
branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)
|
||||
|
||||
branch3x3dbl = conv2d_bn(x, 64, 1, 1)
|
||||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
|
||||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
|
||||
|
||||
branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
|
||||
branch_pool = conv2d_bn(branch_pool, 64, 1, 1)
|
||||
x = layers.concatenate(
|
||||
[branch1x1, branch5x5, branch3x3dbl, branch_pool],
|
||||
axis=channel_axis,
|
||||
name='mixed2')
|
||||
|
||||
# mixed 3: 17 x 17 x 768
|
||||
branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid')
|
||||
|
||||
branch3x3dbl = conv2d_bn(x, 64, 1, 1)
|
||||
branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
|
||||
branch3x3dbl = conv2d_bn(
|
||||
branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid')
|
||||
|
||||
branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x)
|
||||
x = layers.concatenate(
|
||||
[branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3')
|
||||
|
||||
# mixed 4: 17 x 17 x 768
|
||||
branch1x1 = conv2d_bn(x, 192, 1, 1)
|
||||
|
||||
branch7x7 = conv2d_bn(x, 128, 1, 1)
|
||||
branch7x7 = conv2d_bn(branch7x7, 128, 1, 7)
|
||||
branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)
|
||||
|
||||
branch7x7dbl = conv2d_bn(x, 128, 1, 1)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)
|
||||
|
||||
branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
|
||||
branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
|
||||
x = layers.concatenate(
|
||||
[branch1x1, branch7x7, branch7x7dbl, branch_pool],
|
||||
axis=channel_axis,
|
||||
name='mixed4')
|
||||
|
||||
# mixed 5, 6: 17 x 17 x 768
|
||||
for i in range(2):
|
||||
branch1x1 = conv2d_bn(x, 192, 1, 1)
|
||||
|
||||
branch7x7 = conv2d_bn(x, 160, 1, 1)
|
||||
branch7x7 = conv2d_bn(branch7x7, 160, 1, 7)
|
||||
branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)
|
||||
|
||||
branch7x7dbl = conv2d_bn(x, 160, 1, 1)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)
|
||||
|
||||
branch_pool = AveragePooling2D(
|
||||
(3, 3), strides=(1, 1), padding='same')(x)
|
||||
branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
|
||||
x = layers.concatenate(
|
||||
[branch1x1, branch7x7, branch7x7dbl, branch_pool],
|
||||
axis=channel_axis,
|
||||
name='mixed' + str(5 + i))
|
||||
|
||||
# mixed 7: 17 x 17 x 768
|
||||
branch1x1 = conv2d_bn(x, 192, 1, 1)
|
||||
|
||||
branch7x7 = conv2d_bn(x, 192, 1, 1)
|
||||
branch7x7 = conv2d_bn(branch7x7, 192, 1, 7)
|
||||
branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)
|
||||
|
||||
branch7x7dbl = conv2d_bn(x, 192, 1, 1)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)
|
||||
branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)
|
||||
|
||||
branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
|
||||
branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
|
||||
x = layers.concatenate(
|
||||
[branch1x1, branch7x7, branch7x7dbl, branch_pool],
|
||||
axis=channel_axis,
|
||||
name='mixed7')
|
||||
|
||||
# mixed 8: 8 x 8 x 1280
|
||||
branch3x3 = conv2d_bn(x, 192, 1, 1)
|
||||
branch3x3 = conv2d_bn(branch3x3, 320, 3, 3,
|
||||
strides=(2, 2), padding='valid')
|
||||
|
||||
branch7x7x3 = conv2d_bn(x, 192, 1, 1)
|
||||
branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7)
|
||||
branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1)
|
||||
branch7x7x3 = conv2d_bn(
|
||||
branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid')
|
||||
|
||||
branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x)
|
||||
x = layers.concatenate(
|
||||
[branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8')
|
||||
|
||||
# mixed 9: 8 x 8 x 2048
|
||||
for i in range(2):
|
||||
branch1x1 = conv2d_bn(x, 320, 1, 1)
|
||||
|
||||
branch3x3 = conv2d_bn(x, 384, 1, 1)
|
||||
branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3)
|
||||
branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1)
|
||||
branch3x3 = layers.concatenate(
|
||||
[branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i))
|
||||
|
||||
branch3x3dbl = conv2d_bn(x, 448, 1, 1)
|
||||
branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3)
|
||||
branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3)
|
||||
branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1)
|
||||
branch3x3dbl = layers.concatenate(
|
||||
[branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis)
|
||||
|
||||
branch_pool = AveragePooling2D(
|
||||
(3, 3), strides=(1, 1), padding='same')(x)
|
||||
branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
|
||||
x = layers.concatenate(
|
||||
[branch1x1, branch3x3, branch3x3dbl, branch_pool],
|
||||
axis=channel_axis,
|
||||
name='mixed' + str(9 + i))
|
||||
if include_top:
|
||||
# Classification block
|
||||
x = GlobalAveragePooling2D(name='avg_pool')(x)
|
||||
x = Dense(classes, activation='softmax', name='predictions')(x)
|
||||
else:
|
||||
if pooling == 'avg':
|
||||
x = GlobalAveragePooling2D()(x)
|
||||
elif pooling == 'max':
|
||||
x = GlobalMaxPooling2D()(x)
|
||||
|
||||
# Ensure that the model takes into account
|
||||
# any potential predecessors of `input_tensor`.
|
||||
if input_tensor is not None:
|
||||
inputs = get_source_inputs(input_tensor)
|
||||
else:
|
||||
inputs = img_input
|
||||
# Create model.
|
||||
model = Model(inputs, x, name='inception_v3')
|
||||
|
||||
# load weights
|
||||
if weights == 'imagenet':
|
||||
if K.image_data_format() == 'channels_first':
|
||||
if K.backend() == 'tensorflow':
|
||||
warnings.warn('You are using the TensorFlow backend, yet you '
|
||||
'are using the Theano '
|
||||
'image data format convention '
|
||||
'(`image_data_format="channels_first"`). '
|
||||
'For best performance, set '
|
||||
'`image_data_format="channels_last"` in '
|
||||
'your Keras config '
|
||||
'at ~/.keras/keras.json.')
|
||||
if include_top:
|
||||
weights_path = get_file(
|
||||
'inception_v3_weights_tf_dim_ordering_tf_kernels.h5',
|
||||
WEIGHTS_PATH,
|
||||
cache_subdir='models',
|
||||
md5_hash='9a0d58056eeedaa3f26cb7ebd46da564')
|
||||
else:
|
||||
weights_path = get_file(
|
||||
'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5',
|
||||
WEIGHTS_PATH_NO_TOP,
|
||||
cache_subdir='models',
|
||||
md5_hash='bcbd6486424b2319ff4ef7d526e38f63')
|
||||
model.load_weights(weights_path)
|
||||
return model
|
||||
|
||||
|
||||
def preprocess_input(x):
|
||||
x /= 255.
|
||||
x -= 0.5
|
||||
x *= 2.
|
||||
return x
|
||||
@@ -0,0 +1,641 @@
|
||||
"""MobileNet v1 models for Keras.
|
||||
|
||||
MobileNet is a general architecture and can be used for multiple use cases.
|
||||
Depending on the use case, it can use different input layer size and
|
||||
different width factors. This allows different width models to reduce
|
||||
the number of multiply-adds and thereby
|
||||
reduce inference cost on mobile devices.
|
||||
|
||||
MobileNets support any input size greater than 32 x 32, with larger image sizes
|
||||
offering better performance.
|
||||
The number of parameters and number of multiply-adds
|
||||
can be modified by using the `alpha` parameter,
|
||||
which increases/decreases the number of filters in each layer.
|
||||
By altering the image size and `alpha` parameter,
|
||||
all 16 models from the paper can be built, with ImageNet weights provided.
|
||||
|
||||
The paper demonstrates the performance of MobileNets using `alpha` values of
|
||||
1.0 (also called 100 % MobileNet), 0.75, 0.5 and 0.25.
|
||||
For each of these `alpha` values, weights for 4 different input image sizes
|
||||
are provided (224, 192, 160, 128).
|
||||
|
||||
The following table describes the size and accuracy of the 100% MobileNet
|
||||
on size 224 x 224:
|
||||
----------------------------------------------------------------------------
|
||||
Width Multiplier (alpha) | ImageNet Acc | Multiply-Adds (M) | Params (M)
|
||||
----------------------------------------------------------------------------
|
||||
| 1.0 MobileNet-224 | 70.6 % | 529 | 4.2 |
|
||||
| 0.75 MobileNet-224 | 68.4 % | 325 | 2.6 |
|
||||
| 0.50 MobileNet-224 | 63.7 % | 149 | 1.3 |
|
||||
| 0.25 MobileNet-224 | 50.6 % | 41 | 0.5 |
|
||||
----------------------------------------------------------------------------
|
||||
|
||||
The following table describes the performance of
|
||||
the 100 % MobileNet on various input sizes:
|
||||
------------------------------------------------------------------------
|
||||
Resolution | ImageNet Acc | Multiply-Adds (M) | Params (M)
|
||||
------------------------------------------------------------------------
|
||||
| 1.0 MobileNet-224 | 70.6 % | 529 | 4.2 |
|
||||
| 1.0 MobileNet-192 | 69.1 % | 529 | 4.2 |
|
||||
| 1.0 MobileNet-160 | 67.2 % | 529 | 4.2 |
|
||||
| 1.0 MobileNet-128 | 64.4 % | 529 | 4.2 |
|
||||
------------------------------------------------------------------------
|
||||
|
||||
The weights for all 16 models are obtained and translated
|
||||
from TensorFlow checkpoints found at
|
||||
https://github.com/tensorflow/models/blob/master/slim/nets/mobilenet_v1.md
|
||||
|
||||
# Reference
|
||||
- [MobileNets: Efficient Convolutional Neural Networks for
|
||||
Mobile Vision Applications](https://arxiv.org/pdf/1704.04861.pdf))
|
||||
"""
|
||||
from __future__ import print_function
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
|
||||
import warnings
|
||||
|
||||
from ..models import Model
|
||||
from ..layers import Input
|
||||
from ..layers import Activation
|
||||
from ..layers import Dropout
|
||||
from ..layers import Reshape
|
||||
from ..layers import BatchNormalization
|
||||
from ..layers import GlobalAveragePooling2D
|
||||
from ..layers import GlobalMaxPooling2D
|
||||
from ..layers import Conv2D
|
||||
from .. import initializers
|
||||
from .. import regularizers
|
||||
from .. import constraints
|
||||
from ..utils import conv_utils
|
||||
from ..utils.data_utils import get_file
|
||||
from ..engine.topology import get_source_inputs
|
||||
from ..engine import InputSpec
|
||||
from ..applications.imagenet_utils import _obtain_input_shape
|
||||
from ..applications.imagenet_utils import decode_predictions
|
||||
from .. import backend as K
|
||||
|
||||
|
||||
BASE_WEIGHT_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.6/'
|
||||
|
||||
|
||||
def relu6(x):
|
||||
return K.relu(x, max_value=6)
|
||||
|
||||
|
||||
def preprocess_input(x):
|
||||
x /= 255.
|
||||
x -= 0.5
|
||||
x *= 2.
|
||||
return x
|
||||
|
||||
|
||||
class DepthwiseConv2D(Conv2D):
|
||||
"""Depthwise separable 2D convolution.
|
||||
|
||||
Depthwise Separable convolutions consists in performing
|
||||
just the first step in a depthwise spatial convolution
|
||||
(which acts on each input channel separately).
|
||||
The `depth_multiplier` argument controls how many
|
||||
output channels are generated per input channel in the depthwise step.
|
||||
|
||||
# Arguments
|
||||
kernel_size: An integer or tuple/list of 2 integers, specifying the
|
||||
width and height of the 2D convolution window.
|
||||
Can be a single integer to specify the same value for
|
||||
all spatial dimensions.
|
||||
strides: An integer or tuple/list of 2 integers,
|
||||
specifying the strides of the convolution along the width and height.
|
||||
Can be a single integer to specify the same value for
|
||||
all spatial dimensions.
|
||||
Specifying any stride value != 1 is incompatible with specifying
|
||||
any `dilation_rate` value != 1.
|
||||
padding: one of `"valid"` or `"same"` (case-insensitive).
|
||||
depth_multiplier: The number of depthwise convolution output channels
|
||||
for each input channel.
|
||||
The total number of depthwise convolution output
|
||||
channels will be equal to `filters_in * depth_multiplier`.
|
||||
data_format: A string,
|
||||
one of `channels_last` (default) or `channels_first`.
|
||||
The ordering of the dimensions in the inputs.
|
||||
`channels_last` corresponds to inputs with shape
|
||||
`(batch, height, width, channels)` while `channels_first`
|
||||
corresponds to inputs with shape
|
||||
`(batch, channels, height, width)`.
|
||||
It defaults to the `image_data_format` value found in your
|
||||
Keras config file at `~/.keras/keras.json`.
|
||||
If you never set it, then it will be "channels_last".
|
||||
activation: Activation function to use
|
||||
(see [activations](../activations.md)).
|
||||
If you don't specify anything, no activation is applied
|
||||
(ie. "linear" activation: `a(x) = x`).
|
||||
use_bias: Boolean, whether the layer uses a bias vector.
|
||||
depthwise_initializer: Initializer for the depthwise kernel matrix
|
||||
(see [initializers](../initializers.md)).
|
||||
bias_initializer: Initializer for the bias vector
|
||||
(see [initializers](../initializers.md)).
|
||||
depthwise_regularizer: Regularizer function applied to
|
||||
the depthwise kernel matrix
|
||||
(see [regularizer](../regularizers.md)).
|
||||
bias_regularizer: Regularizer function applied to the bias vector
|
||||
(see [regularizer](../regularizers.md)).
|
||||
activity_regularizer: Regularizer function applied to
|
||||
the output of the layer (its "activation").
|
||||
(see [regularizer](../regularizers.md)).
|
||||
depthwise_constraint: Constraint function applied to
|
||||
the depthwise kernel matrix
|
||||
(see [constraints](../constraints.md)).
|
||||
bias_constraint: Constraint function applied to the bias vector
|
||||
(see [constraints](../constraints.md)).
|
||||
|
||||
# Input shape
|
||||
4D tensor with shape:
|
||||
`[batch, channels, rows, cols]` if data_format='channels_first'
|
||||
or 4D tensor with shape:
|
||||
`[batch, rows, cols, channels]` if data_format='channels_last'.
|
||||
|
||||
# Output shape
|
||||
4D tensor with shape:
|
||||
`[batch, filters, new_rows, new_cols]` if data_format='channels_first'
|
||||
or 4D tensor with shape:
|
||||
`[batch, new_rows, new_cols, filters]` if data_format='channels_last'.
|
||||
`rows` and `cols` values might have changed due to padding.
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
kernel_size,
|
||||
strides=(1, 1),
|
||||
padding='valid',
|
||||
depth_multiplier=1,
|
||||
data_format=None,
|
||||
activation=None,
|
||||
use_bias=True,
|
||||
depthwise_initializer='glorot_uniform',
|
||||
bias_initializer='zeros',
|
||||
depthwise_regularizer=None,
|
||||
bias_regularizer=None,
|
||||
activity_regularizer=None,
|
||||
depthwise_constraint=None,
|
||||
bias_constraint=None,
|
||||
**kwargs):
|
||||
super(DepthwiseConv2D, self).__init__(
|
||||
filters=None,
|
||||
kernel_size=kernel_size,
|
||||
strides=strides,
|
||||
padding=padding,
|
||||
data_format=data_format,
|
||||
activation=activation,
|
||||
use_bias=use_bias,
|
||||
bias_regularizer=bias_regularizer,
|
||||
activity_regularizer=activity_regularizer,
|
||||
bias_constraint=bias_constraint,
|
||||
**kwargs)
|
||||
self.depth_multiplier = depth_multiplier
|
||||
self.depthwise_initializer = initializers.get(depthwise_initializer)
|
||||
self.depthwise_regularizer = regularizers.get(depthwise_regularizer)
|
||||
self.depthwise_constraint = constraints.get(depthwise_constraint)
|
||||
self.bias_initializer = initializers.get(bias_initializer)
|
||||
|
||||
def build(self, input_shape):
|
||||
if len(input_shape) < 4:
|
||||
raise ValueError('Inputs to `DepthwiseConv2D` should have rank 4. '
|
||||
'Received input shape:', str(input_shape))
|
||||
if self.data_format == 'channels_first':
|
||||
channel_axis = 1
|
||||
else:
|
||||
channel_axis = 3
|
||||
if input_shape[channel_axis] is None:
|
||||
raise ValueError('The channel dimension of the inputs to '
|
||||
'`DepthwiseConv2D` '
|
||||
'should be defined. Found `None`.')
|
||||
input_dim = int(input_shape[channel_axis])
|
||||
depthwise_kernel_shape = (self.kernel_size[0],
|
||||
self.kernel_size[1],
|
||||
input_dim,
|
||||
self.depth_multiplier)
|
||||
|
||||
self.depthwise_kernel = self.add_weight(
|
||||
shape=depthwise_kernel_shape,
|
||||
initializer=self.depthwise_initializer,
|
||||
name='depthwise_kernel',
|
||||
regularizer=self.depthwise_regularizer,
|
||||
constraint=self.depthwise_constraint)
|
||||
|
||||
if self.use_bias:
|
||||
self.bias = self.add_weight(shape=(input_dim * self.depth_multiplier,),
|
||||
initializer=self.bias_initializer,
|
||||
name='bias',
|
||||
regularizer=self.bias_regularizer,
|
||||
constraint=self.bias_constraint)
|
||||
else:
|
||||
self.bias = None
|
||||
# Set input spec.
|
||||
self.input_spec = InputSpec(ndim=4, axes={channel_axis: input_dim})
|
||||
self.built = True
|
||||
|
||||
def call(self, inputs, training=None):
|
||||
outputs = K.depthwise_conv2d(
|
||||
inputs,
|
||||
self.depthwise_kernel,
|
||||
strides=self.strides,
|
||||
padding=self.padding,
|
||||
dilation_rate=self.dilation_rate,
|
||||
data_format=self.data_format)
|
||||
|
||||
if self.bias:
|
||||
outputs = K.bias_add(
|
||||
outputs,
|
||||
self.bias,
|
||||
data_format=self.data_format)
|
||||
|
||||
if self.activation is not None:
|
||||
return self.activation(outputs)
|
||||
|
||||
return outputs
|
||||
|
||||
def compute_output_shape(self, input_shape):
|
||||
if self.data_format == 'channels_first':
|
||||
rows = input_shape[2]
|
||||
cols = input_shape[3]
|
||||
out_filters = input_shape[1] * self.depth_multiplier
|
||||
elif self.data_format == 'channels_last':
|
||||
rows = input_shape[1]
|
||||
cols = input_shape[2]
|
||||
out_filters = input_shape[3] * self.depth_multiplier
|
||||
|
||||
rows = conv_utils.conv_output_length(rows, self.kernel_size[0],
|
||||
self.padding,
|
||||
self.strides[0])
|
||||
cols = conv_utils.conv_output_length(cols, self.kernel_size[1],
|
||||
self.padding,
|
||||
self.strides[1])
|
||||
|
||||
if self.data_format == 'channels_first':
|
||||
return (input_shape[0], out_filters, rows, cols)
|
||||
elif self.data_format == 'channels_last':
|
||||
return (input_shape[0], rows, cols, out_filters)
|
||||
|
||||
def get_config(self):
|
||||
config = super(DepthwiseConv2D, self).get_config()
|
||||
config.pop('filters')
|
||||
config.pop('kernel_initializer')
|
||||
config.pop('kernel_regularizer')
|
||||
config.pop('kernel_constraint')
|
||||
config['depth_multiplier'] = self.depth_multiplier
|
||||
config['depthwise_initializer'] = initializers.serialize(self.depthwise_initializer)
|
||||
config['depthwise_regularizer'] = regularizers.serialize(self.depthwise_regularizer)
|
||||
config['depthwise_constraint'] = constraints.serialize(self.depthwise_constraint)
|
||||
return config
|
||||
|
||||
|
||||
def MobileNet(input_shape=None,
|
||||
alpha=1.0,
|
||||
depth_multiplier=1,
|
||||
dropout=1e-3,
|
||||
include_top=True,
|
||||
weights='imagenet',
|
||||
input_tensor=None,
|
||||
pooling=None,
|
||||
classes=1000):
|
||||
"""Instantiates the MobileNet architecture.
|
||||
|
||||
Note that only TensorFlow is supported for now,
|
||||
therefore it only works with the data format
|
||||
`image_data_format='channels_last'` in your Keras config
|
||||
at `~/.keras/keras.json`.
|
||||
|
||||
To load a MobileNet model via `load_model`, import the custom
|
||||
objects `relu6` and `DepthwiseConv2D` and pass them to the
|
||||
`custom_objects` parameter.
|
||||
E.g.
|
||||
model = load_model('mobilenet.h5', custom_objects={
|
||||
'relu6': mobilenet.relu6,
|
||||
'DepthwiseConv2D': mobilenet.DepthwiseConv2D})
|
||||
|
||||
# Arguments
|
||||
input_shape: optional shape tuple, only to be specified
|
||||
if `include_top` is False (otherwise the input shape
|
||||
has to be `(224, 224, 3)` (with `channels_last` data format)
|
||||
or (3, 224, 224) (with `channels_first` data format).
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 32.
|
||||
E.g. `(200, 200, 3)` would be one valid value.
|
||||
alpha: controls the width of the network.
|
||||
- If `alpha` < 1.0, proportionally decreases the number
|
||||
of filters in each layer.
|
||||
- If `alpha` > 1.0, proportionally increases the number
|
||||
of filters in each layer.
|
||||
- If `alpha` = 1, default number of filters from the paper
|
||||
are used at each layer.
|
||||
depth_multiplier: depth multiplier for depthwise convolution
|
||||
(also called the resolution multiplier)
|
||||
dropout: dropout rate
|
||||
include_top: whether to include the fully-connected
|
||||
layer at the top of the network.
|
||||
weights: `None` (random initialization) or
|
||||
`imagenet` (ImageNet weights)
|
||||
input_tensor: optional Keras tensor (i.e. output of
|
||||
`layers.Input()`)
|
||||
to use as image input for the model.
|
||||
pooling: Optional pooling mode for feature extraction
|
||||
when `include_top` is `False`.
|
||||
- `None` means that the output of the model
|
||||
will be the 4D tensor output of the
|
||||
last convolutional layer.
|
||||
- `avg` means that global average pooling
|
||||
will be applied to the output of the
|
||||
last convolutional layer, and thus
|
||||
the output of the model will be a
|
||||
2D tensor.
|
||||
- `max` means that global max pooling will
|
||||
be applied.
|
||||
classes: optional number of classes to classify images
|
||||
into, only to be specified if `include_top` is True, and
|
||||
if no `weights` argument is specified.
|
||||
|
||||
# Returns
|
||||
A Keras model instance.
|
||||
|
||||
# Raises
|
||||
ValueError: in case of invalid argument for `weights`,
|
||||
or invalid input shape.
|
||||
RuntimeError: If attempting to run this model with a
|
||||
backend that does not support separable convolutions.
|
||||
"""
|
||||
|
||||
if K.backend() != 'tensorflow':
|
||||
raise RuntimeError('Only TensorFlow backend is currently supported, '
|
||||
'as other backends do not support '
|
||||
'depthwise convolution.')
|
||||
|
||||
if weights not in {'imagenet', None}:
|
||||
raise ValueError('The `weights` argument should be either '
|
||||
'`None` (random initialization) or `imagenet` '
|
||||
'(pre-training on ImageNet).')
|
||||
|
||||
if weights == 'imagenet' and include_top and classes != 1000:
|
||||
raise ValueError('If using `weights` as ImageNet with `include_top` '
|
||||
'as true, `classes` should be 1000')
|
||||
|
||||
# Determine proper input shape.
|
||||
input_shape = _obtain_input_shape(input_shape,
|
||||
default_size=224,
|
||||
min_size=32,
|
||||
data_format=K.image_data_format(),
|
||||
include_top=include_top or weights)
|
||||
if K.image_data_format() == 'channels_last':
|
||||
row_axis, col_axis = (0, 1)
|
||||
else:
|
||||
row_axis, col_axis = (1, 2)
|
||||
rows = input_shape[row_axis]
|
||||
cols = input_shape[col_axis]
|
||||
|
||||
if weights == 'imagenet':
|
||||
if depth_multiplier != 1:
|
||||
raise ValueError('If imagenet weights are being loaded, '
|
||||
'depth multiplier must be 1')
|
||||
|
||||
if alpha not in [0.25, 0.50, 0.75, 1.0]:
|
||||
raise ValueError('If imagenet weights are being loaded, '
|
||||
'alpha can be one of'
|
||||
'`0.25`, `0.50`, `0.75` or `1.0` only.')
|
||||
|
||||
if rows != cols or rows not in [128, 160, 192, 224]:
|
||||
raise ValueError('If imagenet weights are being loaded, '
|
||||
'input must have a static square shape (one of '
|
||||
'(128,128), (160,160), (192,192), or (224, 224)).'
|
||||
' Input shape provided = %s' % (input_shape,))
|
||||
|
||||
if K.image_data_format() != 'channels_last':
|
||||
warnings.warn('The MobileNet family of models is only available '
|
||||
'for the input data format "channels_last" '
|
||||
'(width, height, channels). '
|
||||
'However your settings specify the default '
|
||||
'data format "channels_first" (channels, width, height).'
|
||||
' You should set `image_data_format="channels_last"` '
|
||||
'in your Keras config located at ~/.keras/keras.json. '
|
||||
'The model being returned right now will expect inputs '
|
||||
'to follow the "channels_last" data format.')
|
||||
K.set_image_data_format('channels_last')
|
||||
old_data_format = 'channels_first'
|
||||
else:
|
||||
old_data_format = None
|
||||
|
||||
if input_tensor is None:
|
||||
img_input = Input(shape=input_shape)
|
||||
else:
|
||||
if not K.is_keras_tensor(input_tensor):
|
||||
img_input = Input(tensor=input_tensor, shape=input_shape)
|
||||
else:
|
||||
img_input = input_tensor
|
||||
|
||||
x = _conv_block(img_input, 32, alpha, strides=(2, 2))
|
||||
x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1)
|
||||
|
||||
x = _depthwise_conv_block(x, 128, alpha, depth_multiplier,
|
||||
strides=(2, 2), block_id=2)
|
||||
x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3)
|
||||
|
||||
x = _depthwise_conv_block(x, 256, alpha, depth_multiplier,
|
||||
strides=(2, 2), block_id=4)
|
||||
x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5)
|
||||
|
||||
x = _depthwise_conv_block(x, 512, alpha, depth_multiplier,
|
||||
strides=(2, 2), block_id=6)
|
||||
x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7)
|
||||
x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8)
|
||||
x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9)
|
||||
x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10)
|
||||
x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11)
|
||||
|
||||
x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier,
|
||||
strides=(2, 2), block_id=12)
|
||||
x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13)
|
||||
|
||||
if include_top:
|
||||
if K.image_data_format() == 'channels_first':
|
||||
shape = (int(1024 * alpha), 1, 1)
|
||||
else:
|
||||
shape = (1, 1, int(1024 * alpha))
|
||||
|
||||
x = GlobalAveragePooling2D()(x)
|
||||
x = Reshape(shape, name='reshape_1')(x)
|
||||
x = Dropout(dropout, name='dropout')(x)
|
||||
x = Conv2D(classes, (1, 1),
|
||||
padding='same', name='conv_preds')(x)
|
||||
x = Activation('softmax', name='act_softmax')(x)
|
||||
x = Reshape((classes,), name='reshape_2')(x)
|
||||
else:
|
||||
if pooling == 'avg':
|
||||
x = GlobalAveragePooling2D()(x)
|
||||
elif pooling == 'max':
|
||||
x = GlobalMaxPooling2D()(x)
|
||||
|
||||
# Ensure that the model takes into account
|
||||
# any potential predecessors of `input_tensor`.
|
||||
if input_tensor is not None:
|
||||
inputs = get_source_inputs(input_tensor)
|
||||
else:
|
||||
inputs = img_input
|
||||
|
||||
# Create model.
|
||||
model = Model(inputs, x, name='mobilenet_%0.2f_%s' % (alpha, rows))
|
||||
|
||||
# load weights
|
||||
if weights == 'imagenet':
|
||||
if K.image_data_format() == 'channels_first':
|
||||
raise ValueError('Weights for "channels_last" format '
|
||||
'are not available.')
|
||||
if alpha == 1.0:
|
||||
alpha_text = '1_0'
|
||||
elif alpha == 0.75:
|
||||
alpha_text = '7_5'
|
||||
elif alpha == 0.50:
|
||||
alpha_text = '5_0'
|
||||
else:
|
||||
alpha_text = '2_5'
|
||||
|
||||
if include_top:
|
||||
model_name = 'mobilenet_%s_%d_tf.h5' % (alpha_text, rows)
|
||||
weigh_path = BASE_WEIGHT_PATH + model_name
|
||||
weights_path = get_file(model_name,
|
||||
weigh_path,
|
||||
cache_subdir='models')
|
||||
else:
|
||||
model_name = 'mobilenet_%s_%d_tf_no_top.h5' % (alpha_text, rows)
|
||||
weigh_path = BASE_WEIGHT_PATH + model_name
|
||||
weights_path = get_file(model_name,
|
||||
weigh_path,
|
||||
cache_subdir='models')
|
||||
model.load_weights(weights_path)
|
||||
|
||||
if old_data_format:
|
||||
K.set_image_data_format(old_data_format)
|
||||
return model
|
||||
|
||||
|
||||
def _conv_block(inputs, filters, alpha, kernel=(3, 3), strides=(1, 1)):
|
||||
"""Adds an initial convolution layer (with batch normalization and relu6).
|
||||
|
||||
# Arguments
|
||||
inputs: Input tensor of shape `(rows, cols, 3)`
|
||||
(with `channels_last` data format) or
|
||||
(3, rows, cols) (with `channels_first` data format).
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 32.
|
||||
E.g. `(224, 224, 3)` would be one valid value.
|
||||
filters: Integer, the dimensionality of the output space
|
||||
(i.e. the number output of filters in the convolution).
|
||||
alpha: controls the width of the network.
|
||||
- If `alpha` < 1.0, proportionally decreases the number
|
||||
of filters in each layer.
|
||||
- If `alpha` > 1.0, proportionally increases the number
|
||||
of filters in each layer.
|
||||
- If `alpha` = 1, default number of filters from the paper
|
||||
are used at each layer.
|
||||
kernel: An integer or tuple/list of 2 integers, specifying the
|
||||
width and height of the 2D convolution window.
|
||||
Can be a single integer to specify the same value for
|
||||
all spatial dimensions.
|
||||
strides: An integer or tuple/list of 2 integers,
|
||||
specifying the strides of the convolution along the width and height.
|
||||
Can be a single integer to specify the same value for
|
||||
all spatial dimensions.
|
||||
Specifying any stride value != 1 is incompatible with specifying
|
||||
any `dilation_rate` value != 1.
|
||||
|
||||
# Input shape
|
||||
4D tensor with shape:
|
||||
`(samples, channels, rows, cols)` if data_format='channels_first'
|
||||
or 4D tensor with shape:
|
||||
`(samples, rows, cols, channels)` if data_format='channels_last'.
|
||||
|
||||
# Output shape
|
||||
4D tensor with shape:
|
||||
`(samples, filters, new_rows, new_cols)` if data_format='channels_first'
|
||||
or 4D tensor with shape:
|
||||
`(samples, new_rows, new_cols, filters)` if data_format='channels_last'.
|
||||
`rows` and `cols` values might have changed due to stride.
|
||||
|
||||
# Returns
|
||||
Output tensor of block.
|
||||
"""
|
||||
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
|
||||
filters = int(filters * alpha)
|
||||
x = Conv2D(filters, kernel,
|
||||
padding='same',
|
||||
use_bias=False,
|
||||
strides=strides,
|
||||
name='conv1')(inputs)
|
||||
x = BatchNormalization(axis=channel_axis, name='conv1_bn')(x)
|
||||
return Activation(relu6, name='conv1_relu')(x)
|
||||
|
||||
|
||||
def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha,
|
||||
depth_multiplier=1, strides=(1, 1), block_id=1):
|
||||
"""Adds a depthwise convolution block.
|
||||
|
||||
A depthwise convolution block consists of a depthwise conv,
|
||||
batch normalization, relu6, pointwise convolution,
|
||||
batch normalization and relu6 activation.
|
||||
|
||||
# Arguments
|
||||
inputs: Input tensor of shape `(rows, cols, channels)`
|
||||
(with `channels_last` data format) or
|
||||
(channels, rows, cols) (with `channels_first` data format).
|
||||
pointwise_conv_filters: Integer, the dimensionality of the output space
|
||||
(i.e. the number output of filters in the pointwise convolution).
|
||||
alpha: controls the width of the network.
|
||||
- If `alpha` < 1.0, proportionally decreases the number
|
||||
of filters in each layer.
|
||||
- If `alpha` > 1.0, proportionally increases the number
|
||||
of filters in each layer.
|
||||
- If `alpha` = 1, default number of filters from the paper
|
||||
are used at each layer.
|
||||
depth_multiplier: The number of depthwise convolution output channels
|
||||
for each input channel.
|
||||
The total number of depthwise convolution output
|
||||
channels will be equal to `filters_in * depth_multiplier`.
|
||||
strides: An integer or tuple/list of 2 integers,
|
||||
specifying the strides of the convolution along the width and height.
|
||||
Can be a single integer to specify the same value for
|
||||
all spatial dimensions.
|
||||
Specifying any stride value != 1 is incompatible with specifying
|
||||
any `dilation_rate` value != 1.
|
||||
block_id: Integer, a unique identification designating the block number.
|
||||
|
||||
# Input shape
|
||||
4D tensor with shape:
|
||||
`(batch, channels, rows, cols)` if data_format='channels_first'
|
||||
or 4D tensor with shape:
|
||||
`(batch, rows, cols, channels)` if data_format='channels_last'.
|
||||
|
||||
# Output shape
|
||||
4D tensor with shape:
|
||||
`(batch, filters, new_rows, new_cols)` if data_format='channels_first'
|
||||
or 4D tensor with shape:
|
||||
`(batch, new_rows, new_cols, filters)` if data_format='channels_last'.
|
||||
`rows` and `cols` values might have changed due to stride.
|
||||
|
||||
# Returns
|
||||
Output tensor of block.
|
||||
"""
|
||||
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
|
||||
pointwise_conv_filters = int(pointwise_conv_filters * alpha)
|
||||
|
||||
x = DepthwiseConv2D((3, 3),
|
||||
padding='same',
|
||||
depth_multiplier=depth_multiplier,
|
||||
strides=strides,
|
||||
use_bias=False,
|
||||
name='conv_dw_%d' % block_id)(inputs)
|
||||
x = BatchNormalization(axis=channel_axis, name='conv_dw_%d_bn' % block_id)(x)
|
||||
x = Activation(relu6, name='conv_dw_%d_relu' % block_id)(x)
|
||||
|
||||
x = Conv2D(pointwise_conv_filters, (1, 1),
|
||||
padding='same',
|
||||
use_bias=False,
|
||||
strides=(1, 1),
|
||||
name='conv_pw_%d' % block_id)(x)
|
||||
x = BatchNormalization(axis=channel_axis, name='conv_pw_%d_bn' % block_id)(x)
|
||||
return Activation(relu6, name='conv_pw_%d_relu' % block_id)(x)
|
||||
@@ -0,0 +1,282 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""ResNet50 model for Keras.
|
||||
|
||||
# Reference:
|
||||
|
||||
- [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385)
|
||||
|
||||
Adapted from code contributed by BigMoyan.
|
||||
"""
|
||||
from __future__ import print_function
|
||||
from __future__ import absolute_import
|
||||
|
||||
import warnings
|
||||
|
||||
from ..layers import Input
|
||||
from .. import layers
|
||||
from ..layers import Dense
|
||||
from ..layers import Activation
|
||||
from ..layers import Flatten
|
||||
from ..layers import Conv2D
|
||||
from ..layers import MaxPooling2D
|
||||
from ..layers import ZeroPadding2D
|
||||
from ..layers import AveragePooling2D
|
||||
from ..layers import GlobalAveragePooling2D
|
||||
from ..layers import GlobalMaxPooling2D
|
||||
from ..layers import BatchNormalization
|
||||
from ..models import Model
|
||||
from .. import backend as K
|
||||
from ..engine.topology import get_source_inputs
|
||||
from ..utils import layer_utils
|
||||
from ..utils.data_utils import get_file
|
||||
from .imagenet_utils import decode_predictions
|
||||
from .imagenet_utils import preprocess_input
|
||||
from .imagenet_utils import _obtain_input_shape
|
||||
|
||||
|
||||
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5'
|
||||
WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
|
||||
|
||||
|
||||
def identity_block(input_tensor, kernel_size, filters, stage, block):
|
||||
"""The identity block is the block that has no conv layer at shortcut.
|
||||
|
||||
# Arguments
|
||||
input_tensor: input tensor
|
||||
kernel_size: default 3, the kernel size of middle conv layer at main path
|
||||
filters: list of integers, the filterss of 3 conv layer at main path
|
||||
stage: integer, current stage label, used for generating layer names
|
||||
block: 'a','b'..., current block label, used for generating layer names
|
||||
|
||||
# Returns
|
||||
Output tensor for the block.
|
||||
"""
|
||||
filters1, filters2, filters3 = filters
|
||||
if K.image_data_format() == 'channels_last':
|
||||
bn_axis = 3
|
||||
else:
|
||||
bn_axis = 1
|
||||
conv_name_base = 'res' + str(stage) + block + '_branch'
|
||||
bn_name_base = 'bn' + str(stage) + block + '_branch'
|
||||
|
||||
x = Conv2D(filters1, (1, 1), name=conv_name_base + '2a')(input_tensor)
|
||||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)
|
||||
x = Activation('relu')(x)
|
||||
|
||||
x = Conv2D(filters2, kernel_size,
|
||||
padding='same', name=conv_name_base + '2b')(x)
|
||||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)
|
||||
x = Activation('relu')(x)
|
||||
|
||||
x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)
|
||||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)
|
||||
|
||||
x = layers.add([x, input_tensor])
|
||||
x = Activation('relu')(x)
|
||||
return x
|
||||
|
||||
|
||||
def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):
|
||||
"""A block that has a conv layer at shortcut.
|
||||
|
||||
# Arguments
|
||||
input_tensor: input tensor
|
||||
kernel_size: default 3, the kernel size of middle conv layer at main path
|
||||
filters: list of integers, the filterss of 3 conv layer at main path
|
||||
stage: integer, current stage label, used for generating layer names
|
||||
block: 'a','b'..., current block label, used for generating layer names
|
||||
|
||||
# Returns
|
||||
Output tensor for the block.
|
||||
|
||||
Note that from stage 3, the first conv layer at main path is with strides=(2,2)
|
||||
And the shortcut should have strides=(2,2) as well
|
||||
"""
|
||||
filters1, filters2, filters3 = filters
|
||||
if K.image_data_format() == 'channels_last':
|
||||
bn_axis = 3
|
||||
else:
|
||||
bn_axis = 1
|
||||
conv_name_base = 'res' + str(stage) + block + '_branch'
|
||||
bn_name_base = 'bn' + str(stage) + block + '_branch'
|
||||
|
||||
x = Conv2D(filters1, (1, 1), strides=strides,
|
||||
name=conv_name_base + '2a')(input_tensor)
|
||||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)
|
||||
x = Activation('relu')(x)
|
||||
|
||||
x = Conv2D(filters2, kernel_size, padding='same',
|
||||
name=conv_name_base + '2b')(x)
|
||||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)
|
||||
x = Activation('relu')(x)
|
||||
|
||||
x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)
|
||||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)
|
||||
|
||||
shortcut = Conv2D(filters3, (1, 1), strides=strides,
|
||||
name=conv_name_base + '1')(input_tensor)
|
||||
shortcut = BatchNormalization(axis=bn_axis, name=bn_name_base + '1')(shortcut)
|
||||
|
||||
x = layers.add([x, shortcut])
|
||||
x = Activation('relu')(x)
|
||||
return x
|
||||
|
||||
|
||||
def ResNet50(include_top=True, weights='imagenet',
|
||||
input_tensor=None, input_shape=None,
|
||||
pooling=None,
|
||||
classes=1000):
|
||||
"""Instantiates the ResNet50 architecture.
|
||||
|
||||
Optionally loads weights pre-trained
|
||||
on ImageNet. Note that when using TensorFlow,
|
||||
for best performance you should set
|
||||
`image_data_format="channels_last"` in your Keras config
|
||||
at ~/.keras/keras.json.
|
||||
|
||||
The model and the weights are compatible with both
|
||||
TensorFlow and Theano. The data format
|
||||
convention used by the model is the one
|
||||
specified in your Keras config file.
|
||||
|
||||
# Arguments
|
||||
include_top: whether to include the fully-connected
|
||||
layer at the top of the network.
|
||||
weights: one of `None` (random initialization)
|
||||
or "imagenet" (pre-training on ImageNet).
|
||||
input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
|
||||
to use as image input for the model.
|
||||
input_shape: optional shape tuple, only to be specified
|
||||
if `include_top` is False (otherwise the input shape
|
||||
has to be `(224, 224, 3)` (with `channels_last` data format)
|
||||
or `(3, 224, 224)` (with `channels_first` data format).
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 197.
|
||||
E.g. `(200, 200, 3)` would be one valid value.
|
||||
pooling: Optional pooling mode for feature extraction
|
||||
when `include_top` is `False`.
|
||||
- `None` means that the output of the model will be
|
||||
the 4D tensor output of the
|
||||
last convolutional layer.
|
||||
- `avg` means that global average pooling
|
||||
will be applied to the output of the
|
||||
last convolutional layer, and thus
|
||||
the output of the model will be a 2D tensor.
|
||||
- `max` means that global max pooling will
|
||||
be applied.
|
||||
classes: optional number of classes to classify images
|
||||
into, only to be specified if `include_top` is True, and
|
||||
if no `weights` argument is specified.
|
||||
|
||||
# Returns
|
||||
A Keras model instance.
|
||||
|
||||
# Raises
|
||||
ValueError: in case of invalid argument for `weights`,
|
||||
or invalid input shape.
|
||||
"""
|
||||
if weights not in {'imagenet', None}:
|
||||
raise ValueError('The `weights` argument should be either '
|
||||
'`None` (random initialization) or `imagenet` '
|
||||
'(pre-training on ImageNet).')
|
||||
|
||||
if weights == 'imagenet' and include_top and classes != 1000:
|
||||
raise ValueError('If using `weights` as imagenet with `include_top`'
|
||||
' as true, `classes` should be 1000')
|
||||
|
||||
# Determine proper input shape
|
||||
input_shape = _obtain_input_shape(input_shape,
|
||||
default_size=224,
|
||||
min_size=197,
|
||||
data_format=K.image_data_format(),
|
||||
include_top=include_top)
|
||||
|
||||
if input_tensor is None:
|
||||
img_input = Input(shape=input_shape)
|
||||
else:
|
||||
if not K.is_keras_tensor(input_tensor):
|
||||
img_input = Input(tensor=input_tensor, shape=input_shape)
|
||||
else:
|
||||
img_input = input_tensor
|
||||
if K.image_data_format() == 'channels_last':
|
||||
bn_axis = 3
|
||||
else:
|
||||
bn_axis = 1
|
||||
|
||||
x = ZeroPadding2D((3, 3))(img_input)
|
||||
x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
|
||||
x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
|
||||
x = Activation('relu')(x)
|
||||
x = MaxPooling2D((3, 3), strides=(2, 2))(x)
|
||||
|
||||
x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
|
||||
x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
|
||||
x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
|
||||
|
||||
x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
|
||||
x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
|
||||
x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
|
||||
x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
|
||||
|
||||
x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
|
||||
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
|
||||
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
|
||||
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
|
||||
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
|
||||
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
|
||||
|
||||
x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
|
||||
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
|
||||
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')
|
||||
|
||||
x = AveragePooling2D((7, 7), name='avg_pool')(x)
|
||||
|
||||
if include_top:
|
||||
x = Flatten()(x)
|
||||
x = Dense(classes, activation='softmax', name='fc1000')(x)
|
||||
else:
|
||||
if pooling == 'avg':
|
||||
x = GlobalAveragePooling2D()(x)
|
||||
elif pooling == 'max':
|
||||
x = GlobalMaxPooling2D()(x)
|
||||
|
||||
# Ensure that the model takes into account
|
||||
# any potential predecessors of `input_tensor`.
|
||||
if input_tensor is not None:
|
||||
inputs = get_source_inputs(input_tensor)
|
||||
else:
|
||||
inputs = img_input
|
||||
# Create model.
|
||||
model = Model(inputs, x, name='resnet50')
|
||||
|
||||
# load weights
|
||||
if weights == 'imagenet':
|
||||
if include_top:
|
||||
weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels.h5',
|
||||
WEIGHTS_PATH,
|
||||
cache_subdir='models',
|
||||
md5_hash='a7b3fe01876f51b976af0dea6bc144eb')
|
||||
else:
|
||||
weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
|
||||
WEIGHTS_PATH_NO_TOP,
|
||||
cache_subdir='models',
|
||||
md5_hash='a268eb855778b3df3c7506639542a6af')
|
||||
model.load_weights(weights_path)
|
||||
if K.backend() == 'theano':
|
||||
layer_utils.convert_all_kernels_in_model(model)
|
||||
if include_top:
|
||||
maxpool = model.get_layer(name='avg_pool')
|
||||
shape = maxpool.output_shape[1:]
|
||||
dense = model.get_layer(name='fc1000')
|
||||
layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first')
|
||||
|
||||
if K.image_data_format() == 'channels_first' and K.backend() == 'tensorflow':
|
||||
warnings.warn('You are using the TensorFlow backend, yet you '
|
||||
'are using the Theano '
|
||||
'image data format convention '
|
||||
'(`image_data_format="channels_first"`). '
|
||||
'For best performance, set '
|
||||
'`image_data_format="channels_last"` in '
|
||||
'your Keras config '
|
||||
'at ~/.keras/keras.json.')
|
||||
return model
|
||||
@@ -0,0 +1,189 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""VGG16 model for Keras.
|
||||
|
||||
# Reference
|
||||
|
||||
- [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556)
|
||||
|
||||
"""
|
||||
from __future__ import print_function
|
||||
from __future__ import absolute_import
|
||||
|
||||
import warnings
|
||||
|
||||
from ..models import Model
|
||||
from ..layers import Flatten
|
||||
from ..layers import Dense
|
||||
from ..layers import Input
|
||||
from ..layers import Conv2D
|
||||
from ..layers import MaxPooling2D
|
||||
from ..layers import GlobalAveragePooling2D
|
||||
from ..layers import GlobalMaxPooling2D
|
||||
from ..engine.topology import get_source_inputs
|
||||
from ..utils import layer_utils
|
||||
from ..utils.data_utils import get_file
|
||||
from .. import backend as K
|
||||
from .imagenet_utils import decode_predictions
|
||||
from .imagenet_utils import preprocess_input
|
||||
from .imagenet_utils import _obtain_input_shape
|
||||
|
||||
|
||||
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5'
|
||||
WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
|
||||
|
||||
|
||||
def VGG16(include_top=True, weights='imagenet',
|
||||
input_tensor=None, input_shape=None,
|
||||
pooling=None,
|
||||
classes=1000):
|
||||
"""Instantiates the VGG16 architecture.
|
||||
|
||||
Optionally loads weights pre-trained
|
||||
on ImageNet. Note that when using TensorFlow,
|
||||
for best performance you should set
|
||||
`image_data_format="channels_last"` in your Keras config
|
||||
at ~/.keras/keras.json.
|
||||
|
||||
The model and the weights are compatible with both
|
||||
TensorFlow and Theano. The data format
|
||||
convention used by the model is the one
|
||||
specified in your Keras config file.
|
||||
|
||||
# Arguments
|
||||
include_top: whether to include the 3 fully-connected
|
||||
layers at the top of the network.
|
||||
weights: one of `None` (random initialization)
|
||||
or "imagenet" (pre-training on ImageNet).
|
||||
input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
|
||||
to use as image input for the model.
|
||||
input_shape: optional shape tuple, only to be specified
|
||||
if `include_top` is False (otherwise the input shape
|
||||
has to be `(224, 224, 3)` (with `channels_last` data format)
|
||||
or `(3, 224, 224)` (with `channels_first` data format).
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 48.
|
||||
E.g. `(200, 200, 3)` would be one valid value.
|
||||
pooling: Optional pooling mode for feature extraction
|
||||
when `include_top` is `False`.
|
||||
- `None` means that the output of the model will be
|
||||
the 4D tensor output of the
|
||||
last convolutional layer.
|
||||
- `avg` means that global average pooling
|
||||
will be applied to the output of the
|
||||
last convolutional layer, and thus
|
||||
the output of the model will be a 2D tensor.
|
||||
- `max` means that global max pooling will
|
||||
be applied.
|
||||
classes: optional number of classes to classify images
|
||||
into, only to be specified if `include_top` is True, and
|
||||
if no `weights` argument is specified.
|
||||
|
||||
# Returns
|
||||
A Keras model instance.
|
||||
|
||||
# Raises
|
||||
ValueError: in case of invalid argument for `weights`,
|
||||
or invalid input shape.
|
||||
"""
|
||||
if weights not in {'imagenet', None}:
|
||||
raise ValueError('The `weights` argument should be either '
|
||||
'`None` (random initialization) or `imagenet` '
|
||||
'(pre-training on ImageNet).')
|
||||
|
||||
if weights == 'imagenet' and include_top and classes != 1000:
|
||||
raise ValueError('If using `weights` as imagenet with `include_top`'
|
||||
' as true, `classes` should be 1000')
|
||||
# Determine proper input shape
|
||||
input_shape = _obtain_input_shape(input_shape,
|
||||
default_size=224,
|
||||
min_size=48,
|
||||
data_format=K.image_data_format(),
|
||||
include_top=include_top)
|
||||
|
||||
if input_tensor is None:
|
||||
img_input = Input(shape=input_shape)
|
||||
else:
|
||||
if not K.is_keras_tensor(input_tensor):
|
||||
img_input = Input(tensor=input_tensor, shape=input_shape)
|
||||
else:
|
||||
img_input = input_tensor
|
||||
# Block 1
|
||||
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)
|
||||
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
|
||||
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
|
||||
|
||||
# Block 2
|
||||
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)
|
||||
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)
|
||||
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
|
||||
|
||||
# Block 3
|
||||
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)
|
||||
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)
|
||||
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)
|
||||
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
|
||||
|
||||
# Block 4
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)
|
||||
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
|
||||
|
||||
# Block 5
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)
|
||||
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
|
||||
|
||||
if include_top:
|
||||
# Classification block
|
||||
x = Flatten(name='flatten')(x)
|
||||
x = Dense(4096, activation='relu', name='fc1')(x)
|
||||
x = Dense(4096, activation='relu', name='fc2')(x)
|
||||
x = Dense(classes, activation='softmax', name='predictions')(x)
|
||||
else:
|
||||
if pooling == 'avg':
|
||||
x = GlobalAveragePooling2D()(x)
|
||||
elif pooling == 'max':
|
||||
x = GlobalMaxPooling2D()(x)
|
||||
|
||||
# Ensure that the model takes into account
|
||||
# any potential predecessors of `input_tensor`.
|
||||
if input_tensor is not None:
|
||||
inputs = get_source_inputs(input_tensor)
|
||||
else:
|
||||
inputs = img_input
|
||||
# Create model.
|
||||
model = Model(inputs, x, name='vgg16')
|
||||
|
||||
# load weights
|
||||
if weights == 'imagenet':
|
||||
if include_top:
|
||||
weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels.h5',
|
||||
WEIGHTS_PATH,
|
||||
cache_subdir='models')
|
||||
else:
|
||||
weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
|
||||
WEIGHTS_PATH_NO_TOP,
|
||||
cache_subdir='models')
|
||||
model.load_weights(weights_path)
|
||||
if K.backend() == 'theano':
|
||||
layer_utils.convert_all_kernels_in_model(model)
|
||||
|
||||
if K.image_data_format() == 'channels_first':
|
||||
if include_top:
|
||||
maxpool = model.get_layer(name='block5_pool')
|
||||
shape = maxpool.output_shape[1:]
|
||||
dense = model.get_layer(name='fc1')
|
||||
layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first')
|
||||
|
||||
if K.backend() == 'tensorflow':
|
||||
warnings.warn('You are using the TensorFlow backend, yet you '
|
||||
'are using the Theano '
|
||||
'image data format convention '
|
||||
'(`image_data_format="channels_first"`). '
|
||||
'For best performance, set '
|
||||
'`image_data_format="channels_last"` in '
|
||||
'your Keras config '
|
||||
'at ~/.keras/keras.json.')
|
||||
return model
|
||||
@@ -0,0 +1,192 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""VGG19 model for Keras.
|
||||
|
||||
# Reference
|
||||
|
||||
- [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556)
|
||||
|
||||
"""
|
||||
from __future__ import print_function
|
||||
from __future__ import absolute_import
|
||||
|
||||
import warnings
|
||||
|
||||
from ..models import Model
|
||||
from ..layers import Flatten
|
||||
from ..layers import Dense
|
||||
from ..layers import Input
|
||||
from ..layers import Conv2D
|
||||
from ..layers import MaxPooling2D
|
||||
from ..layers import GlobalAveragePooling2D
|
||||
from ..layers import GlobalMaxPooling2D
|
||||
from ..engine.topology import get_source_inputs
|
||||
from ..utils import layer_utils
|
||||
from ..utils.data_utils import get_file
|
||||
from .. import backend as K
|
||||
from .imagenet_utils import decode_predictions
|
||||
from .imagenet_utils import preprocess_input
|
||||
from .imagenet_utils import _obtain_input_shape
|
||||
|
||||
|
||||
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg19_weights_tf_dim_ordering_tf_kernels.h5'
|
||||
WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5'
|
||||
|
||||
|
||||
def VGG19(include_top=True, weights='imagenet',
|
||||
input_tensor=None, input_shape=None,
|
||||
pooling=None,
|
||||
classes=1000):
|
||||
"""Instantiates the VGG19 architecture.
|
||||
|
||||
Optionally loads weights pre-trained
|
||||
on ImageNet. Note that when using TensorFlow,
|
||||
for best performance you should set
|
||||
`image_data_format="channels_last"` in your Keras config
|
||||
at ~/.keras/keras.json.
|
||||
|
||||
The model and the weights are compatible with both
|
||||
TensorFlow and Theano. The data format
|
||||
convention used by the model is the one
|
||||
specified in your Keras config file.
|
||||
|
||||
# Arguments
|
||||
include_top: whether to include the 3 fully-connected
|
||||
layers at the top of the network.
|
||||
weights: one of `None` (random initialization)
|
||||
or "imagenet" (pre-training on ImageNet).
|
||||
input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
|
||||
to use as image input for the model.
|
||||
input_shape: optional shape tuple, only to be specified
|
||||
if `include_top` is False (otherwise the input shape
|
||||
has to be `(224, 224, 3)` (with `channels_last` data format)
|
||||
or `(3, 224, 224)` (with `channels_first` data format).
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 48.
|
||||
E.g. `(200, 200, 3)` would be one valid value.
|
||||
pooling: Optional pooling mode for feature extraction
|
||||
when `include_top` is `False`.
|
||||
- `None` means that the output of the model will be
|
||||
the 4D tensor output of the
|
||||
last convolutional layer.
|
||||
- `avg` means that global average pooling
|
||||
will be applied to the output of the
|
||||
last convolutional layer, and thus
|
||||
the output of the model will be a 2D tensor.
|
||||
- `max` means that global max pooling will
|
||||
be applied.
|
||||
classes: optional number of classes to classify images
|
||||
into, only to be specified if `include_top` is True, and
|
||||
if no `weights` argument is specified.
|
||||
|
||||
# Returns
|
||||
A Keras model instance.
|
||||
|
||||
# Raises
|
||||
ValueError: in case of invalid argument for `weights`,
|
||||
or invalid input shape.
|
||||
"""
|
||||
if weights not in {'imagenet', None}:
|
||||
raise ValueError('The `weights` argument should be either '
|
||||
'`None` (random initialization) or `imagenet` '
|
||||
'(pre-training on ImageNet).')
|
||||
|
||||
if weights == 'imagenet' and include_top and classes != 1000:
|
||||
raise ValueError('If using `weights` as imagenet with `include_top`'
|
||||
' as true, `classes` should be 1000')
|
||||
# Determine proper input shape
|
||||
input_shape = _obtain_input_shape(input_shape,
|
||||
default_size=224,
|
||||
min_size=48,
|
||||
data_format=K.image_data_format(),
|
||||
include_top=include_top)
|
||||
|
||||
if input_tensor is None:
|
||||
img_input = Input(shape=input_shape)
|
||||
else:
|
||||
if not K.is_keras_tensor(input_tensor):
|
||||
img_input = Input(tensor=input_tensor, shape=input_shape)
|
||||
else:
|
||||
img_input = input_tensor
|
||||
# Block 1
|
||||
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)
|
||||
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
|
||||
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
|
||||
|
||||
# Block 2
|
||||
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)
|
||||
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)
|
||||
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
|
||||
|
||||
# Block 3
|
||||
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)
|
||||
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)
|
||||
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)
|
||||
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv4')(x)
|
||||
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
|
||||
|
||||
# Block 4
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv4')(x)
|
||||
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
|
||||
|
||||
# Block 5
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)
|
||||
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv4')(x)
|
||||
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
|
||||
|
||||
if include_top:
|
||||
# Classification block
|
||||
x = Flatten(name='flatten')(x)
|
||||
x = Dense(4096, activation='relu', name='fc1')(x)
|
||||
x = Dense(4096, activation='relu', name='fc2')(x)
|
||||
x = Dense(classes, activation='softmax', name='predictions')(x)
|
||||
else:
|
||||
if pooling == 'avg':
|
||||
x = GlobalAveragePooling2D()(x)
|
||||
elif pooling == 'max':
|
||||
x = GlobalMaxPooling2D()(x)
|
||||
|
||||
# Ensure that the model takes into account
|
||||
# any potential predecessors of `input_tensor`.
|
||||
if input_tensor is not None:
|
||||
inputs = get_source_inputs(input_tensor)
|
||||
else:
|
||||
inputs = img_input
|
||||
# Create model.
|
||||
model = Model(inputs, x, name='vgg19')
|
||||
|
||||
# load weights
|
||||
if weights == 'imagenet':
|
||||
if include_top:
|
||||
weights_path = get_file('vgg19_weights_tf_dim_ordering_tf_kernels.h5',
|
||||
WEIGHTS_PATH,
|
||||
cache_subdir='models')
|
||||
else:
|
||||
weights_path = get_file('vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5',
|
||||
WEIGHTS_PATH_NO_TOP,
|
||||
cache_subdir='models')
|
||||
model.load_weights(weights_path)
|
||||
if K.backend() == 'theano':
|
||||
layer_utils.convert_all_kernels_in_model(model)
|
||||
|
||||
if K.image_data_format() == 'channels_first':
|
||||
if include_top:
|
||||
maxpool = model.get_layer(name='block5_pool')
|
||||
shape = maxpool.output_shape[1:]
|
||||
dense = model.get_layer(name='fc1')
|
||||
layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first')
|
||||
|
||||
if K.backend() == 'tensorflow':
|
||||
warnings.warn('You are using the TensorFlow backend, yet you '
|
||||
'are using the Theano '
|
||||
'image data format convention '
|
||||
'(`image_data_format="channels_first"`). '
|
||||
'For best performance, set '
|
||||
'`image_data_format="channels_last"` in '
|
||||
'your Keras config '
|
||||
'at ~/.keras/keras.json.')
|
||||
return model
|
||||
@@ -0,0 +1,266 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Xception V1 model for Keras.
|
||||
|
||||
On ImageNet, this model gets to a top-1 validation accuracy of 0.790
|
||||
and a top-5 validation accuracy of 0.945.
|
||||
|
||||
Do note that the input image format for this model is different than for
|
||||
the VGG16 and ResNet models (299x299 instead of 224x224),
|
||||
and that the input preprocessing function
|
||||
is also different (same as Inception V3).
|
||||
|
||||
Also do note that this model is only available for the TensorFlow backend,
|
||||
due to its reliance on `SeparableConvolution` layers.
|
||||
|
||||
# Reference
|
||||
|
||||
- [Xception: Deep Learning with Depthwise Separable Convolutions](https://arxiv.org/abs/1610.02357)
|
||||
|
||||
"""
|
||||
from __future__ import print_function
|
||||
from __future__ import absolute_import
|
||||
|
||||
import warnings
|
||||
|
||||
from ..models import Model
|
||||
from .. import layers
|
||||
from ..layers import Dense
|
||||
from ..layers import Input
|
||||
from ..layers import BatchNormalization
|
||||
from ..layers import Activation
|
||||
from ..layers import Conv2D
|
||||
from ..layers import SeparableConv2D
|
||||
from ..layers import MaxPooling2D
|
||||
from ..layers import GlobalAveragePooling2D
|
||||
from ..layers import GlobalMaxPooling2D
|
||||
from ..engine.topology import get_source_inputs
|
||||
from ..utils.data_utils import get_file
|
||||
from .. import backend as K
|
||||
from .imagenet_utils import decode_predictions
|
||||
from .imagenet_utils import _obtain_input_shape
|
||||
|
||||
|
||||
TF_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels.h5'
|
||||
TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels_notop.h5'
|
||||
|
||||
|
||||
def Xception(include_top=True, weights='imagenet',
|
||||
input_tensor=None, input_shape=None,
|
||||
pooling=None,
|
||||
classes=1000):
|
||||
"""Instantiates the Xception architecture.
|
||||
|
||||
Optionally loads weights pre-trained
|
||||
on ImageNet. This model is available for TensorFlow only,
|
||||
and can only be used with inputs following the TensorFlow
|
||||
data format `(width, height, channels)`.
|
||||
You should set `image_data_format="channels_last"` in your Keras config
|
||||
located at ~/.keras/keras.json.
|
||||
|
||||
Note that the default input image size for this model is 299x299.
|
||||
|
||||
# Arguments
|
||||
include_top: whether to include the fully-connected
|
||||
layer at the top of the network.
|
||||
weights: one of `None` (random initialization)
|
||||
or "imagenet" (pre-training on ImageNet).
|
||||
input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
|
||||
to use as image input for the model.
|
||||
input_shape: optional shape tuple, only to be specified
|
||||
if `include_top` is False (otherwise the input shape
|
||||
has to be `(299, 299, 3)`.
|
||||
It should have exactly 3 inputs channels,
|
||||
and width and height should be no smaller than 71.
|
||||
E.g. `(150, 150, 3)` would be one valid value.
|
||||
pooling: Optional pooling mode for feature extraction
|
||||
when `include_top` is `False`.
|
||||
- `None` means that the output of the model will be
|
||||
the 4D tensor output of the
|
||||
last convolutional layer.
|
||||
- `avg` means that global average pooling
|
||||
will be applied to the output of the
|
||||
last convolutional layer, and thus
|
||||
the output of the model will be a 2D tensor.
|
||||
- `max` means that global max pooling will
|
||||
be applied.
|
||||
classes: optional number of classes to classify images
|
||||
into, only to be specified if `include_top` is True, and
|
||||
if no `weights` argument is specified.
|
||||
|
||||
# Returns
|
||||
A Keras model instance.
|
||||
|
||||
# Raises
|
||||
ValueError: in case of invalid argument for `weights`,
|
||||
or invalid input shape.
|
||||
RuntimeError: If attempting to run this model with a
|
||||
backend that does not support separable convolutions.
|
||||
"""
|
||||
if weights not in {'imagenet', None}:
|
||||
raise ValueError('The `weights` argument should be either '
|
||||
'`None` (random initialization) or `imagenet` '
|
||||
'(pre-training on ImageNet).')
|
||||
|
||||
if weights == 'imagenet' and include_top and classes != 1000:
|
||||
raise ValueError('If using `weights` as imagenet with `include_top`'
|
||||
' as true, `classes` should be 1000')
|
||||
|
||||
if K.backend() != 'tensorflow':
|
||||
raise RuntimeError('The Xception model is only available with '
|
||||
'the TensorFlow backend.')
|
||||
if K.image_data_format() != 'channels_last':
|
||||
warnings.warn('The Xception model is only available for the '
|
||||
'input data format "channels_last" '
|
||||
'(width, height, channels). '
|
||||
'However your settings specify the default '
|
||||
'data format "channels_first" (channels, width, height). '
|
||||
'You should set `image_data_format="channels_last"` in your Keras '
|
||||
'config located at ~/.keras/keras.json. '
|
||||
'The model being returned right now will expect inputs '
|
||||
'to follow the "channels_last" data format.')
|
||||
K.set_image_data_format('channels_last')
|
||||
old_data_format = 'channels_first'
|
||||
else:
|
||||
old_data_format = None
|
||||
|
||||
# Determine proper input shape
|
||||
input_shape = _obtain_input_shape(input_shape,
|
||||
default_size=299,
|
||||
min_size=71,
|
||||
data_format=K.image_data_format(),
|
||||
include_top=include_top)
|
||||
|
||||
if input_tensor is None:
|
||||
img_input = Input(shape=input_shape)
|
||||
else:
|
||||
if not K.is_keras_tensor(input_tensor):
|
||||
img_input = Input(tensor=input_tensor, shape=input_shape)
|
||||
else:
|
||||
img_input = input_tensor
|
||||
|
||||
x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False, name='block1_conv1')(img_input)
|
||||
x = BatchNormalization(name='block1_conv1_bn')(x)
|
||||
x = Activation('relu', name='block1_conv1_act')(x)
|
||||
x = Conv2D(64, (3, 3), use_bias=False, name='block1_conv2')(x)
|
||||
x = BatchNormalization(name='block1_conv2_bn')(x)
|
||||
x = Activation('relu', name='block1_conv2_act')(x)
|
||||
|
||||
residual = Conv2D(128, (1, 1), strides=(2, 2),
|
||||
padding='same', use_bias=False)(x)
|
||||
residual = BatchNormalization()(residual)
|
||||
|
||||
x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv1')(x)
|
||||
x = BatchNormalization(name='block2_sepconv1_bn')(x)
|
||||
x = Activation('relu', name='block2_sepconv2_act')(x)
|
||||
x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv2')(x)
|
||||
x = BatchNormalization(name='block2_sepconv2_bn')(x)
|
||||
|
||||
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block2_pool')(x)
|
||||
x = layers.add([x, residual])
|
||||
|
||||
residual = Conv2D(256, (1, 1), strides=(2, 2),
|
||||
padding='same', use_bias=False)(x)
|
||||
residual = BatchNormalization()(residual)
|
||||
|
||||
x = Activation('relu', name='block3_sepconv1_act')(x)
|
||||
x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv1')(x)
|
||||
x = BatchNormalization(name='block3_sepconv1_bn')(x)
|
||||
x = Activation('relu', name='block3_sepconv2_act')(x)
|
||||
x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv2')(x)
|
||||
x = BatchNormalization(name='block3_sepconv2_bn')(x)
|
||||
|
||||
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block3_pool')(x)
|
||||
x = layers.add([x, residual])
|
||||
|
||||
residual = Conv2D(728, (1, 1), strides=(2, 2),
|
||||
padding='same', use_bias=False)(x)
|
||||
residual = BatchNormalization()(residual)
|
||||
|
||||
x = Activation('relu', name='block4_sepconv1_act')(x)
|
||||
x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv1')(x)
|
||||
x = BatchNormalization(name='block4_sepconv1_bn')(x)
|
||||
x = Activation('relu', name='block4_sepconv2_act')(x)
|
||||
x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv2')(x)
|
||||
x = BatchNormalization(name='block4_sepconv2_bn')(x)
|
||||
|
||||
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block4_pool')(x)
|
||||
x = layers.add([x, residual])
|
||||
|
||||
for i in range(8):
|
||||
residual = x
|
||||
prefix = 'block' + str(i + 5)
|
||||
|
||||
x = Activation('relu', name=prefix + '_sepconv1_act')(x)
|
||||
x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv1')(x)
|
||||
x = BatchNormalization(name=prefix + '_sepconv1_bn')(x)
|
||||
x = Activation('relu', name=prefix + '_sepconv2_act')(x)
|
||||
x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv2')(x)
|
||||
x = BatchNormalization(name=prefix + '_sepconv2_bn')(x)
|
||||
x = Activation('relu', name=prefix + '_sepconv3_act')(x)
|
||||
x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv3')(x)
|
||||
x = BatchNormalization(name=prefix + '_sepconv3_bn')(x)
|
||||
|
||||
x = layers.add([x, residual])
|
||||
|
||||
residual = Conv2D(1024, (1, 1), strides=(2, 2),
|
||||
padding='same', use_bias=False)(x)
|
||||
residual = BatchNormalization()(residual)
|
||||
|
||||
x = Activation('relu', name='block13_sepconv1_act')(x)
|
||||
x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block13_sepconv1')(x)
|
||||
x = BatchNormalization(name='block13_sepconv1_bn')(x)
|
||||
x = Activation('relu', name='block13_sepconv2_act')(x)
|
||||
x = SeparableConv2D(1024, (3, 3), padding='same', use_bias=False, name='block13_sepconv2')(x)
|
||||
x = BatchNormalization(name='block13_sepconv2_bn')(x)
|
||||
|
||||
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block13_pool')(x)
|
||||
x = layers.add([x, residual])
|
||||
|
||||
x = SeparableConv2D(1536, (3, 3), padding='same', use_bias=False, name='block14_sepconv1')(x)
|
||||
x = BatchNormalization(name='block14_sepconv1_bn')(x)
|
||||
x = Activation('relu', name='block14_sepconv1_act')(x)
|
||||
|
||||
x = SeparableConv2D(2048, (3, 3), padding='same', use_bias=False, name='block14_sepconv2')(x)
|
||||
x = BatchNormalization(name='block14_sepconv2_bn')(x)
|
||||
x = Activation('relu', name='block14_sepconv2_act')(x)
|
||||
|
||||
if include_top:
|
||||
x = GlobalAveragePooling2D(name='avg_pool')(x)
|
||||
x = Dense(classes, activation='softmax', name='predictions')(x)
|
||||
else:
|
||||
if pooling == 'avg':
|
||||
x = GlobalAveragePooling2D()(x)
|
||||
elif pooling == 'max':
|
||||
x = GlobalMaxPooling2D()(x)
|
||||
|
||||
# Ensure that the model takes into account
|
||||
# any potential predecessors of `input_tensor`.
|
||||
if input_tensor is not None:
|
||||
inputs = get_source_inputs(input_tensor)
|
||||
else:
|
||||
inputs = img_input
|
||||
# Create model.
|
||||
model = Model(inputs, x, name='xception')
|
||||
|
||||
# load weights
|
||||
if weights == 'imagenet':
|
||||
if include_top:
|
||||
weights_path = get_file('xception_weights_tf_dim_ordering_tf_kernels.h5',
|
||||
TF_WEIGHTS_PATH,
|
||||
cache_subdir='models')
|
||||
else:
|
||||
weights_path = get_file('xception_weights_tf_dim_ordering_tf_kernels_notop.h5',
|
||||
TF_WEIGHTS_PATH_NO_TOP,
|
||||
cache_subdir='models')
|
||||
model.load_weights(weights_path)
|
||||
|
||||
if old_data_format:
|
||||
K.set_image_data_format(old_data_format)
|
||||
return model
|
||||
|
||||
|
||||
def preprocess_input(x):
|
||||
x /= 255.
|
||||
x -= 0.5
|
||||
x *= 2.
|
||||
return x
|
||||
+69
-19
@@ -3,49 +3,99 @@ from __future__ import print_function
|
||||
import os
|
||||
import json
|
||||
import sys
|
||||
from .common import epsilon, floatx, set_epsilon, set_floatx
|
||||
from .common import epsilon
|
||||
from .common import floatx
|
||||
from .common import set_epsilon
|
||||
from .common import set_floatx
|
||||
from .common import cast_to_floatx
|
||||
from .common import image_data_format
|
||||
from .common import set_image_data_format
|
||||
|
||||
# Obtain Keras base dir path: either ~/.keras or /tmp.
|
||||
_keras_base_dir = os.path.expanduser('~')
|
||||
if not os.access(_keras_base_dir, os.W_OK):
|
||||
_keras_base_dir = '/tmp'
|
||||
|
||||
_keras_dir = os.path.join(_keras_base_dir, '.keras')
|
||||
if not os.path.exists(_keras_dir):
|
||||
os.makedirs(_keras_dir)
|
||||
|
||||
_BACKEND = 'theano'
|
||||
# Default backend: TensorFlow.
|
||||
_BACKEND = 'tensorflow'
|
||||
|
||||
# Attempt to read Keras config file.
|
||||
_config_path = os.path.expanduser(os.path.join(_keras_dir, 'keras.json'))
|
||||
if os.path.exists(_config_path):
|
||||
_config = json.load(open(_config_path))
|
||||
try:
|
||||
_config = json.load(open(_config_path))
|
||||
except ValueError:
|
||||
_config = {}
|
||||
_floatx = _config.get('floatx', floatx())
|
||||
assert _floatx in {'float16', 'float32', 'float64'}
|
||||
_epsilon = _config.get('epsilon', epsilon())
|
||||
assert type(_epsilon) == float
|
||||
assert isinstance(_epsilon, float)
|
||||
_backend = _config.get('backend', _BACKEND)
|
||||
assert _backend in {'theano', 'tensorflow'}
|
||||
assert _backend in {'theano', 'tensorflow', 'cntk'}
|
||||
_image_data_format = _config.get('image_data_format',
|
||||
image_data_format())
|
||||
assert _image_data_format in {'channels_last', 'channels_first'}
|
||||
|
||||
set_floatx(_floatx)
|
||||
set_epsilon(_epsilon)
|
||||
set_image_data_format(_image_data_format)
|
||||
_BACKEND = _backend
|
||||
else:
|
||||
# save config file, for easy edition
|
||||
_config = {'floatx': floatx(),
|
||||
'epsilon': epsilon(),
|
||||
'backend': _BACKEND}
|
||||
with open(_config_path, 'w') as f:
|
||||
# add new line in order for bash 'cat' display the content correctly
|
||||
f.write(json.dumps(_config) + '\n')
|
||||
|
||||
# Save config file, if possible.
|
||||
if not os.path.exists(_keras_dir):
|
||||
try:
|
||||
os.makedirs(_keras_dir)
|
||||
except OSError:
|
||||
# Except permission denied and potential race conditions
|
||||
# in multi-threaded environments.
|
||||
pass
|
||||
|
||||
if not os.path.exists(_config_path):
|
||||
_config = {
|
||||
'floatx': floatx(),
|
||||
'epsilon': epsilon(),
|
||||
'backend': _BACKEND,
|
||||
'image_data_format': image_data_format()
|
||||
}
|
||||
try:
|
||||
with open(_config_path, 'w') as f:
|
||||
f.write(json.dumps(_config, indent=4))
|
||||
except IOError:
|
||||
# Except permission denied.
|
||||
pass
|
||||
|
||||
# Set backend based on KERAS_BACKEND flag, if applicable.
|
||||
if 'KERAS_BACKEND' in os.environ:
|
||||
_backend = os.environ['KERAS_BACKEND']
|
||||
assert _backend in {'theano', 'tensorflow'}
|
||||
assert _backend in {'theano', 'tensorflow', 'cntk'}
|
||||
_BACKEND = _backend
|
||||
|
||||
if _BACKEND == 'theano':
|
||||
# Import backend functions.
|
||||
if _BACKEND == 'cntk':
|
||||
sys.stderr.write('Using CNTK backend\n')
|
||||
from .cntk_backend import *
|
||||
elif _BACKEND == 'theano':
|
||||
sys.stderr.write('Using Theano backend.\n')
|
||||
from .theano_backend import *
|
||||
elif _BACKEND == 'tensorflow':
|
||||
sys.stderr.write('Using TensorFlow backend.\n')
|
||||
from .tensorflow_backend import *
|
||||
else:
|
||||
raise Exception('Unknown backend: ' + str(_BACKEND))
|
||||
raise ValueError('Unknown backend: ' + str(_BACKEND))
|
||||
|
||||
|
||||
def backend():
|
||||
"""Publicly accessible method
|
||||
for determining the current backend.
|
||||
|
||||
# Returns
|
||||
String, the name of the backend Keras is currently using.
|
||||
|
||||
# Example
|
||||
```python
|
||||
>>> keras.backend.backend()
|
||||
'tensorflow'
|
||||
```
|
||||
"""
|
||||
return _BACKEND
|
||||
|
||||
Diferenças do arquivo suprimidas por serem muito extensas
Carregar Diff
+161
-5
@@ -3,30 +3,186 @@ import numpy as np
|
||||
# the type of float to use throughout the session.
|
||||
_FLOATX = 'float32'
|
||||
_EPSILON = 10e-8
|
||||
_IMAGE_DATA_FORMAT = 'channels_last'
|
||||
|
||||
|
||||
def epsilon():
|
||||
"""Returns the value of the fuzz
|
||||
factor used in numeric expressions.
|
||||
|
||||
# Returns
|
||||
A float.
|
||||
|
||||
# Example
|
||||
```python
|
||||
>>> keras.backend.epsilon()
|
||||
1e-08
|
||||
```
|
||||
"""
|
||||
return _EPSILON
|
||||
|
||||
|
||||
def set_epsilon(e):
|
||||
"""Sets the value of the fuzz
|
||||
factor used in numeric expressions.
|
||||
|
||||
# Arguments
|
||||
e: float. New value of epsilon.
|
||||
|
||||
# Example
|
||||
```python
|
||||
>>> from keras import backend as K
|
||||
>>> K.epsilon()
|
||||
1e-08
|
||||
>>> K.set_epsilon(1e-05)
|
||||
>>> K.epsilon()
|
||||
1e-05
|
||||
```
|
||||
"""
|
||||
global _EPSILON
|
||||
_EPSILON = e
|
||||
|
||||
|
||||
def floatx():
|
||||
"""Returns the default float type, as a string.
|
||||
(e.g. 'float16', 'float32', 'float64').
|
||||
|
||||
# Returns
|
||||
String, the current default float type.
|
||||
|
||||
# Example
|
||||
```python
|
||||
>>> keras.backend.floatx()
|
||||
'float32'
|
||||
```
|
||||
"""
|
||||
return _FLOATX
|
||||
|
||||
|
||||
def set_floatx(floatx):
|
||||
"""Sets the default float type.
|
||||
|
||||
# Arguments
|
||||
floatx: String, 'float16', 'float32', or 'float64'.
|
||||
|
||||
# Example
|
||||
```python
|
||||
>>> from keras import backend as K
|
||||
>>> K.floatx()
|
||||
'float32'
|
||||
>>> K.set_floatx('float16')
|
||||
>>> K.floatx()
|
||||
'float16'
|
||||
```
|
||||
"""
|
||||
global _FLOATX
|
||||
if floatx not in {'float16', 'float32', 'float64'}:
|
||||
raise Exception('Unknown floatx type: ' + str(floatx))
|
||||
floatx = str(floatx)
|
||||
_FLOATX = floatx
|
||||
raise ValueError('Unknown floatx type: ' + str(floatx))
|
||||
_FLOATX = str(floatx)
|
||||
|
||||
|
||||
def cast_to_floatx(x):
|
||||
'''Cast a Numpy array to floatx.
|
||||
'''
|
||||
"""Cast a Numpy array to the default Keras float type.
|
||||
|
||||
# Arguments
|
||||
x: Numpy array.
|
||||
|
||||
# Returns
|
||||
The same Numpy array, cast to its new type.
|
||||
|
||||
# Example
|
||||
```python
|
||||
>>> from keras import backend as K
|
||||
>>> K.floatx()
|
||||
'float32'
|
||||
>>> arr = numpy.array([1.0, 2.0], dtype='float64')
|
||||
>>> arr.dtype
|
||||
dtype('float64')
|
||||
>>> new_arr = K.cast_to_floatx(arr)
|
||||
>>> new_arr
|
||||
array([ 1., 2.], dtype=float32)
|
||||
>>> new_arr.dtype
|
||||
dtype('float32')
|
||||
```
|
||||
"""
|
||||
return np.asarray(x, dtype=_FLOATX)
|
||||
|
||||
|
||||
def image_data_format():
|
||||
"""Returns the default image data format convention ('channels_first' or 'channels_last').
|
||||
|
||||
# Returns
|
||||
A string, either `'channels_first'` or `'channels_last'`
|
||||
|
||||
# Example
|
||||
```python
|
||||
>>> keras.backend.image_data_format()
|
||||
'channels_first'
|
||||
```
|
||||
"""
|
||||
return _IMAGE_DATA_FORMAT
|
||||
|
||||
|
||||
def set_image_data_format(data_format):
|
||||
"""Sets the value of the data format convention.
|
||||
|
||||
# Arguments
|
||||
data_format: string. `'channels_first'` or `'channels_last'`.
|
||||
|
||||
# Example
|
||||
```python
|
||||
>>> from keras import backend as K
|
||||
>>> K.image_data_format()
|
||||
'channels_first'
|
||||
>>> K.set_image_data_format('channels_last')
|
||||
>>> K.image_data_format()
|
||||
'channels_last'
|
||||
```
|
||||
"""
|
||||
global _IMAGE_DATA_FORMAT
|
||||
if data_format not in {'channels_last', 'channels_first'}:
|
||||
raise ValueError('Unknown data_format:', data_format)
|
||||
_IMAGE_DATA_FORMAT = str(data_format)
|
||||
|
||||
|
||||
# Legacy methods
|
||||
|
||||
def set_image_dim_ordering(dim_ordering):
|
||||
"""Legacy setter for `image_data_format`.
|
||||
|
||||
# Arguments
|
||||
dim_ordering: string. `tf` or `th`.
|
||||
|
||||
# Example
|
||||
```python
|
||||
>>> from keras import backend as K
|
||||
>>> K.image_data_format()
|
||||
'channels_first'
|
||||
>>> K.set_image_data_format('channels_last')
|
||||
>>> K.image_data_format()
|
||||
'channels_last'
|
||||
```
|
||||
|
||||
# Raises
|
||||
ValueError: if `dim_ordering` is invalid.
|
||||
"""
|
||||
global _IMAGE_DATA_FORMAT
|
||||
if dim_ordering not in {'tf', 'th'}:
|
||||
raise ValueError('Unknown dim_ordering:', dim_ordering)
|
||||
if dim_ordering == 'th':
|
||||
data_format = 'channels_first'
|
||||
else:
|
||||
data_format = 'channels_last'
|
||||
_IMAGE_DATA_FORMAT = data_format
|
||||
|
||||
|
||||
def image_dim_ordering():
|
||||
"""Legacy getter for `image_data_format`.
|
||||
|
||||
# Returns
|
||||
string, one of `'th'`, `'tf'`
|
||||
"""
|
||||
if _IMAGE_DATA_FORMAT == 'channels_first':
|
||||
return 'th'
|
||||
else:
|
||||
return 'tf'
|
||||
|
||||
+3445
-342
Diferenças do arquivo suprimidas por serem muito extensas
Carregar Diff
+1943
-386
Diferenças do arquivo suprimidas por serem muito extensas
Carregar Diff
+757
-192
Diferenças do arquivo suprimidas por serem muito extensas
Carregar Diff
+142
-57
@@ -1,95 +1,180 @@
|
||||
from __future__ import absolute_import
|
||||
import six
|
||||
from . import backend as K
|
||||
from .utils.generic_utils import serialize_keras_object
|
||||
from .utils.generic_utils import deserialize_keras_object
|
||||
|
||||
|
||||
class Constraint(object):
|
||||
def __call__(self, p):
|
||||
return p
|
||||
|
||||
def __call__(self, w):
|
||||
return w
|
||||
|
||||
def get_config(self):
|
||||
return {"name": self.__class__.__name__}
|
||||
return {}
|
||||
|
||||
|
||||
class MaxNorm(Constraint):
|
||||
'''Constrain the weights incident to each hidden unit to have a norm less than or equal to a desired value.
|
||||
"""MaxNorm weight constraint.
|
||||
|
||||
Constrains the weights incident to each hidden unit
|
||||
to have a norm less than or equal to a desired value.
|
||||
|
||||
# Arguments
|
||||
m: the maximum norm for the incoming weights.
|
||||
axis: integer, axis along which to calculate weight norms. For instance,
|
||||
in a `Dense` layer the weight matrix has shape (input_dim, output_dim),
|
||||
set `axis` to `0` to constrain each weight vector of length (input_dim).
|
||||
In a `MaxoutDense` layer the weight tensor has shape (nb_feature, input_dim, output_dim),
|
||||
set `axis` to `1` to constrain each weight vector of length (input_dim),
|
||||
i.e. constrain the filters incident to the `max` operation.
|
||||
In a `Convolution2D` layer with the Theano backend, the weight tensor
|
||||
has shape (nb_filter, stack_size, nb_row, nb_col), set `axis` to `[1,2,3]`
|
||||
to constrain the weights of each filter tensor of size (stack_size, nb_row, nb_col).
|
||||
In a `Convolution2D` layer with the TensorFlow backend, the weight tensor
|
||||
has shape (nb_row, nb_col, stack_size, nb_filter), set `axis` to `[0,1,2]`
|
||||
to constrain the weights of each filter tensor of size (nb_row, nb_col, stack_size).
|
||||
axis: integer, axis along which to calculate weight norms.
|
||||
For instance, in a `Dense` layer the weight matrix
|
||||
has shape `(input_dim, output_dim)`,
|
||||
set `axis` to `0` to constrain each weight vector
|
||||
of length `(input_dim,)`.
|
||||
In a `Conv2D` layer with `data_format="channels_last"`,
|
||||
the weight tensor has shape
|
||||
`(rows, cols, input_depth, output_depth)`,
|
||||
set `axis` to `[0, 1, 2]`
|
||||
to constrain the weights of each filter tensor of size
|
||||
`(rows, cols, input_depth)`.
|
||||
|
||||
# References
|
||||
- [Dropout: A Simple Way to Prevent Neural Networks from Overfitting Srivastava, Hinton, et al. 2014](http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf)
|
||||
'''
|
||||
def __init__(self, m=2, axis=0):
|
||||
self.m = m
|
||||
"""
|
||||
|
||||
def __init__(self, max_value=2, axis=0):
|
||||
self.max_value = max_value
|
||||
self.axis = axis
|
||||
|
||||
def __call__(self, p):
|
||||
norms = K.sqrt(K.sum(K.square(p), axis=self.axis, keepdims=True))
|
||||
desired = K.clip(norms, 0, self.m)
|
||||
p = p * (desired / (K.epsilon() + norms))
|
||||
return p
|
||||
def __call__(self, w):
|
||||
norms = K.sqrt(K.sum(K.square(w), axis=self.axis, keepdims=True))
|
||||
desired = K.clip(norms, 0, self.max_value)
|
||||
w *= (desired / (K.epsilon() + norms))
|
||||
return w
|
||||
|
||||
def get_config(self):
|
||||
return {"name": self.__class__.__name__,
|
||||
"m": self.m,
|
||||
"axis": self.axis}
|
||||
return {'max_value': self.max_value,
|
||||
'axis': self.axis}
|
||||
|
||||
|
||||
class NonNeg(Constraint):
|
||||
'''Constrain the weights to be non-negative.
|
||||
'''
|
||||
def __call__(self, p):
|
||||
p *= K.cast(p >= 0., K.floatx())
|
||||
return p
|
||||
"""Constrains the weights to be non-negative.
|
||||
"""
|
||||
|
||||
def __call__(self, w):
|
||||
w *= K.cast(K.greater_equal(w, 0.), K.floatx())
|
||||
return w
|
||||
|
||||
|
||||
class UnitNorm(Constraint):
|
||||
'''Constrain the weights incident to each hidden unit to have unit norm.
|
||||
"""Constrains the weights incident to each hidden unit to have unit norm.
|
||||
|
||||
# Arguments
|
||||
axis: integer, axis along which to calculate weight norms. For instance,
|
||||
in a `Dense` layer the weight matrix has shape (input_dim, output_dim),
|
||||
set `axis` to `0` to constrain each weight vector of length (input_dim).
|
||||
In a `MaxoutDense` layer the weight tensor has shape (nb_feature, input_dim, output_dim),
|
||||
set `axis` to `1` to constrain each weight vector of length (input_dim),
|
||||
i.e. constrain the filters incident to the `max` operation.
|
||||
In a `Convolution2D` layer with the Theano backend, the weight tensor
|
||||
has shape (nb_filter, stack_size, nb_row, nb_col), set `axis` to `[1,2,3]`
|
||||
to constrain the weights of each filter tensor of size (stack_size, nb_row, nb_col).
|
||||
In a `Convolution2D` layer with the TensorFlow backend, the weight tensor
|
||||
has shape (nb_row, nb_col, stack_size, nb_filter), set `axis` to `[0,1,2]`
|
||||
to constrain the weights of each filter tensor of size (nb_row, nb_col, stack_size).
|
||||
'''
|
||||
axis: integer, axis along which to calculate weight norms.
|
||||
For instance, in a `Dense` layer the weight matrix
|
||||
has shape `(input_dim, output_dim)`,
|
||||
set `axis` to `0` to constrain each weight vector
|
||||
of length `(input_dim,)`.
|
||||
In a `Conv2D` layer with `data_format="channels_last"`,
|
||||
the weight tensor has shape
|
||||
`(rows, cols, input_depth, output_depth)`,
|
||||
set `axis` to `[0, 1, 2]`
|
||||
to constrain the weights of each filter tensor of size
|
||||
`(rows, cols, input_depth)`.
|
||||
"""
|
||||
|
||||
def __init__(self, axis=0):
|
||||
self.axis = axis
|
||||
|
||||
def __call__(self, p):
|
||||
return p / (K.epsilon() + K.sqrt(K.sum(K.square(p), axis=self.axis, keepdims=True)))
|
||||
def __call__(self, w):
|
||||
return w / (K.epsilon() + K.sqrt(K.sum(K.square(w),
|
||||
axis=self.axis,
|
||||
keepdims=True)))
|
||||
|
||||
def get_config(self):
|
||||
return {"name": self.__class__.__name__,
|
||||
"axis": self.axis}
|
||||
return {'axis': self.axis}
|
||||
|
||||
|
||||
identity = Constraint
|
||||
maxnorm = MaxNorm
|
||||
nonneg = NonNeg
|
||||
unitnorm = UnitNorm
|
||||
class MinMaxNorm(Constraint):
|
||||
"""MinMaxNorm weight constraint.
|
||||
|
||||
from .utils.generic_utils import get_from_module
|
||||
Constrains the weights incident to each hidden unit
|
||||
to have the norm between a lower bound and an upper bound.
|
||||
|
||||
# Arguments
|
||||
min_value: the minimum norm for the incoming weights.
|
||||
max_value: the maximum norm for the incoming weights.
|
||||
rate: rate for enforcing the constraint: weights will be
|
||||
rescaled to yield
|
||||
`(1 - rate) * norm + rate * norm.clip(min_value, max_value)`.
|
||||
Effectively, this means that rate=1.0 stands for strict
|
||||
enforcement of the constraint, while rate<1.0 means that
|
||||
weights will be rescaled at each step to slowly move
|
||||
towards a value inside the desired interval.
|
||||
axis: integer, axis along which to calculate weight norms.
|
||||
For instance, in a `Dense` layer the weight matrix
|
||||
has shape `(input_dim, output_dim)`,
|
||||
set `axis` to `0` to constrain each weight vector
|
||||
of length `(input_dim,)`.
|
||||
In a `Conv2D` layer with `data_format="channels_last"`,
|
||||
the weight tensor has shape
|
||||
`(rows, cols, input_depth, output_depth)`,
|
||||
set `axis` to `[0, 1, 2]`
|
||||
to constrain the weights of each filter tensor of size
|
||||
`(rows, cols, input_depth)`.
|
||||
"""
|
||||
|
||||
def __init__(self, min_value=0.0, max_value=1.0, rate=1.0, axis=0):
|
||||
self.min_value = min_value
|
||||
self.max_value = max_value
|
||||
self.rate = rate
|
||||
self.axis = axis
|
||||
|
||||
def __call__(self, w):
|
||||
norms = K.sqrt(K.sum(K.square(w), axis=self.axis, keepdims=True))
|
||||
desired = (self.rate * K.clip(norms, self.min_value, self.max_value) +
|
||||
(1 - self.rate) * norms)
|
||||
w *= (desired / (K.epsilon() + norms))
|
||||
return w
|
||||
|
||||
def get_config(self):
|
||||
return {'min_value': self.min_value,
|
||||
'max_value': self.max_value,
|
||||
'rate': self.rate,
|
||||
'axis': self.axis}
|
||||
|
||||
|
||||
def get(identifier, kwargs=None):
|
||||
return get_from_module(identifier, globals(), 'constraint', instantiate=True, kwargs=kwargs)
|
||||
# Aliases.
|
||||
|
||||
max_norm = MaxNorm
|
||||
non_neg = NonNeg
|
||||
unit_norm = UnitNorm
|
||||
min_max_norm = MinMaxNorm
|
||||
|
||||
|
||||
# Legacy aliases.
|
||||
maxnorm = max_norm
|
||||
nonneg = non_neg
|
||||
unitnorm = unit_norm
|
||||
|
||||
|
||||
def serialize(constraint):
|
||||
return serialize_keras_object(constraint)
|
||||
|
||||
|
||||
def deserialize(config, custom_objects=None):
|
||||
return deserialize_keras_object(config,
|
||||
module_objects=globals(),
|
||||
custom_objects=custom_objects,
|
||||
printable_module_name='constraint')
|
||||
|
||||
|
||||
def get(identifier):
|
||||
if identifier is None:
|
||||
return None
|
||||
if isinstance(identifier, dict):
|
||||
return deserialize(identifier)
|
||||
elif isinstance(identifier, six.string_types):
|
||||
config = {'class_name': str(identifier), 'config': {}}
|
||||
return deserialize(config)
|
||||
elif callable(identifier):
|
||||
return identifier
|
||||
else:
|
||||
raise ValueError('Could not interpret constraint identifier:',
|
||||
identifier)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from . import mnist
|
||||
from . import imdb
|
||||
from . import reuters
|
||||
from . import cifar10
|
||||
from . import cifar100
|
||||
from . import boston_housing
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
from ..utils.data_utils import get_file
|
||||
import numpy as np
|
||||
|
||||
|
||||
def load_data(path='boston_housing.npz', seed=113, test_split=0.2):
|
||||
"""Loads the Boston Housing dataset.
|
||||
|
||||
# Arguments
|
||||
path: path where to cache the dataset locally
|
||||
(relative to ~/.keras/datasets).
|
||||
seed: Random seed for shuffling the data
|
||||
before computing the test split.
|
||||
test_split: fraction of the data to reserve as test set.
|
||||
|
||||
# Returns
|
||||
Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
|
||||
"""
|
||||
assert 0 <= test_split < 1
|
||||
path = get_file(path,
|
||||
origin='https://s3.amazonaws.com/keras-datasets/boston_housing.npz',
|
||||
file_hash='f553886a1f8d56431e820c5b82552d9d95cfcb96d1e678153f8839538947dff5')
|
||||
f = np.load(path)
|
||||
x = f['x']
|
||||
y = f['y']
|
||||
f.close()
|
||||
|
||||
np.random.seed(seed)
|
||||
np.random.shuffle(x)
|
||||
np.random.seed(seed)
|
||||
np.random.shuffle(y)
|
||||
|
||||
x_train = np.array(x[:int(len(x) * (1 - test_split))])
|
||||
y_train = np.array(y[:int(len(x) * (1 - test_split))])
|
||||
x_test = np.array(x[int(len(x) * (1 - test_split)):])
|
||||
y_test = np.array(y[int(len(x) * (1 - test_split)):])
|
||||
return (x_train, y_train), (x_test, y_test)
|
||||
@@ -2,21 +2,31 @@
|
||||
from __future__ import absolute_import
|
||||
import sys
|
||||
from six.moves import cPickle
|
||||
from six.moves import range
|
||||
|
||||
|
||||
def load_batch(fpath, label_key='labels'):
|
||||
"""Internal utility for parsing CIFAR data.
|
||||
|
||||
# Arguments
|
||||
fpath: path the file to parse.
|
||||
label_key: key for label data in the retrieve
|
||||
dictionary.
|
||||
|
||||
# Returns
|
||||
A tuple `(data, labels)`.
|
||||
"""
|
||||
f = open(fpath, 'rb')
|
||||
if sys.version_info < (3,):
|
||||
d = cPickle.load(f)
|
||||
else:
|
||||
d = cPickle.load(f, encoding="bytes")
|
||||
d = cPickle.load(f, encoding='bytes')
|
||||
# decode utf8
|
||||
d_decoded = {}
|
||||
for k, v in d.items():
|
||||
del(d[k])
|
||||
d[k.decode("utf8")] = v
|
||||
d_decoded[k.decode('utf8')] = v
|
||||
d = d_decoded
|
||||
f.close()
|
||||
data = d["data"]
|
||||
data = d['data']
|
||||
labels = d[label_key]
|
||||
|
||||
data = data.reshape(data.shape[0], 3, 32, 32)
|
||||
|
||||
Alguns arquivos não foram exibidos porque demasiados arquivos foram alterados neste diff Mostrar Mais
Referência em uma Nova Issue
Bloquear um usuário