├── .gitignore
├── CHANGELOG
├── CNAME
├── LICENSE
├── README.md
├── _config.yml
├── deepdreamer.py
├── deepdreamer
├── __init__.py
├── deepdreamer.py
└── images2gif.py
└── requirements.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 |
5 | # C extensions
6 | *.so
7 |
8 | # Distribution / packaging
9 | .Python
10 | env/
11 | venv/
12 | build/
13 | develop-eggs/
14 | dist/
15 | downloads/
16 | eggs/
17 | .eggs/
18 | lib/
19 | lib64/
20 | parts/
21 | sdist/
22 | var/
23 | *.egg-info/
24 | .installed.cfg
25 | *.egg
26 |
27 | # PyInstaller
28 | # Usually these files are written by a python script from a template
29 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
30 | *.manifest
31 | *.spec
32 |
33 | # Installer logs
34 | pip-log.txt
35 | pip-delete-this-directory.txt
36 |
37 | # Unit test / coverage reports
38 | htmlcov/
39 | .tox/
40 | .coverage
41 | .coverage.*
42 | .cache
43 | nosetests.xml
44 | coverage.xml
45 | *,cover
46 |
47 | # Translations
48 | *.mo
49 | *.pot
50 |
51 | # Django stuff:
52 | *.log
53 |
54 | # Sphinx documentation
55 | docs/_build/
56 |
57 | # PyBuilder
58 | target/
59 | *.caffemodel
60 | *.prototxt
61 | *.protxt
62 | log.txt
63 | *.jpg
64 | *.jpeg
65 |
--------------------------------------------------------------------------------
/CHANGELOG:
--------------------------------------------------------------------------------
1 | Changelog
2 | =========
3 | v0.0.2a, 04/11/2017
4 | * Changed the script to use Python 3.
5 | * Works with Caffe 1.0.0.
6 |
7 | v0.0.1a, historical entry
8 | * Initial release in 2015.
9 |
--------------------------------------------------------------------------------
/CNAME:
--------------------------------------------------------------------------------
1 | deepdreamer.fq.nz
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | {project} Copyright (C) {year} {fullname}
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
676 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # [Deep Dreamer](https://deepdreamer.fq.nz/)
2 | Easy to configure Python program that make use of [Google's DeepDream](https://github.com/google/deepdream/)
3 |
4 | * [Requirements](#requirements)
5 | * [Installation](#installation)
6 | * [Usage](#usage)
7 | * [Configuration options](#configuration-options)
8 | * [Examples](#examples)
9 |
10 | ## Requirements
11 | * Python 3
12 | * [NumPy](https://pypi.python.org/pypi/numpy)
13 | * [SciPy](https://pypi.python.org/pypi/scipy/)
14 | * [Pillow](https://pypi.python.org/pypi/Pillow/)
15 | * [Caffe](http://caffe.berkeleyvision.org/)
16 | * [FFmpeg](https://www.ffmpeg.org/) (Optional, required for videos.)
17 |
18 | ## Installation
19 | 1. Install [NumPy](https://pypi.python.org/pypi/numpy), [SciPy](https://pypi.python.org/pypi/scipy/), [Pillow](https://pypi.python.org/pypi/Pillow/) and [Caffe](http://caffe.berkeleyvision.org/). *On Ubuntu 17.10 installing caffe will usually install all other dependencies.*
20 | 2. Clone this project. `git clone https://github.com/kesara/deepdreamer.git`
21 | 3. Go to project directory. `cd deepdeamer`
22 | 4. Download **deploy.prototxt** from [bvlc_googlenet](https://github.com/BVLC/caffe/tree/master/models/bvlc_googlenet) into the project directory.
23 | 5. Add line `force_backward: true` to **deploy.prototxt** file.
24 | 6. Download **bvlc_googlenet.caffemodel** from [bvlc_googlenet](https://github.com/BVLC/caffe/tree/master/models/bvlc_googlenet) into the project directory.
25 | 7. (Optional) Download MIT's "Places" neural net, download the **Places205-GoogLeNet** from [their website](http://places.csail.mit.edu/downloadCNN.html). You need the **deploy_places205.protxt** and **googlelet_places205_train_iter_2400000.caffemodel** files from the archive.
26 |
27 | ## Usage
28 | * Just deep dreaming
29 | `python3 deepdreamer.py image.jpg`
30 | * Create a deepdream gif
31 | `python3 deepdreamer.py --gif true image.jpg`
32 | * Create a deepdream video (requires ffmpeg)
33 | `python3 deepdreamer.py --video video.mp4`
34 |
35 | ## Configuration options
36 | ```
37 | usage: deepdreamer.py [-h] [--zoom {true,false}] [--scale SCALE]
38 | [--dreams DREAMS] [--itern ITERN] [--octaves OCTAVES]
39 | [--octave-scale OCTAVE_SCALE] [--layers LAYERS]
40 | [--clip {true,false}] [--gpuid GPUID]
41 | [--network {bvlc_googlenet,googlenet_place205}]
42 | [--gif {true,false}] [--reverse {true,false}]
43 | [--duration DURATION] [--loop {true,false}]
44 | [--framerate FRAMERATE] [--list-layers] [--video VIDEO]
45 | [image]
46 |
47 | positional arguments:
48 | image
49 |
50 | optional arguments:
51 | -h, --help show this help message and exit
52 | --gpuid GPUID enable GPU with id GPUID (default: disabled)
53 | --zoom {true,false} zoom dreams (default: true)
54 | --scale SCALE scale coefficient for zoom (default: 0.05)
55 | --dreams DREAMS number of images (default: 100)
56 | --itern ITERN dream iterations (default: 10)
57 | --octaves OCTAVES dream octaves (default: 4)
58 | --octave-scale OCTAVE_SCALE
59 | dream octave scale (default: 1.4)
60 | --layers LAYERS dream layers (default: inception_4c/output)
61 | --clip {true,false} clip dreams (default: true)
62 | --network {bvlc_googlenet,googlenet_place205}
63 | choose the network to use (default: bvlc_googlenet)
64 | --gif {true,false} make a gif (default: false)
65 | --reverse {true,false}
66 | make a reverse gif (default: false)
67 | --duration DURATION gif frame duration in seconds (default: 0.1)
68 | --loop {true,false} enable gif loop (default: false)
69 | --framerate FRAMERATE
70 | framerate for video (default: 24)
71 | --list-layers list layers
72 | --video VIDEO video file
73 | ```
74 |
75 | ## Examples
76 | 
77 | 
78 | 
79 | 
80 |
--------------------------------------------------------------------------------
/_config.yml:
--------------------------------------------------------------------------------
1 | theme: jekyll-theme-cayman
--------------------------------------------------------------------------------
/deepdreamer.py:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Deep Dreamer
3 | # Author: Kesara Rathnayake ( kesara [at] kesara [dot] lk )
4 | ###############################################################################
5 |
6 | from argparse import ArgumentParser
7 | import sys
8 |
9 | from deepdreamer.deepdreamer import deepdream, deepdream_video, list_layers
10 |
11 |
12 | def main():
13 | try:
14 | parser = ArgumentParser(description="Deep dreamer")
15 | parser.add_argument(
16 | "--zoom", choices=["true", "false"], default="true",
17 | help="zoom dreams (default: true)")
18 | parser.add_argument(
19 | "--scale", type=float, default=0.05,
20 | help="scale coefficient for zoom (default: 0.05)")
21 | parser.add_argument(
22 | "--dreams", type=int, default=100,
23 | help="number of images (default: 100)")
24 | parser.add_argument(
25 | "--itern", type=int, default=10,
26 | help="dream iterations (default: 10)")
27 | parser.add_argument(
28 | "--octaves", type=int, default=4,
29 | help="dream octaves (default: 4)")
30 | parser.add_argument(
31 | "--octave-scale", type=float, default=1.4,
32 | help="dream octave scale (default: 1.4)")
33 | parser.add_argument(
34 | "--layers", type=str, default="inception_4c/output",
35 | help="dream layers (default: inception_4c/output)")
36 | parser.add_argument(
37 | "--clip", choices=["true", "false"], default="true",
38 | help="clip dreams (default: true)")
39 | parser.add_argument(
40 | "--network", choices=['bvlc_googlenet', 'googlenet_place205'],
41 | default='bvlc_googlenet',
42 | help="choose the network to use (default: bvlc_googlenet)")
43 | parser.add_argument(
44 | "--gif", choices=["true", "false"], default="false",
45 | help="make a gif (default: false)")
46 | parser.add_argument(
47 | "--reverse", choices=["true", "false"], default="false",
48 | help="make a reverse gif (default: false)")
49 | parser.add_argument(
50 | "--duration", type=float, default=0.1,
51 | help="gif frame duration in seconds (default: 0.1)")
52 | parser.add_argument(
53 | "--loop", choices=["true", "false"], default="false",
54 | help="enable gif loop (default: false)")
55 | parser.add_argument(
56 | "--framerate", type=int, default=24,
57 | help="framerate for video (default: 24)")
58 | parser.add_argument(
59 | "--gpuid", type=int, default=-1,
60 | help="enable GPU with id GPUID (default: disabled)")
61 | group = parser.add_mutually_exclusive_group(required=True)
62 | group.add_argument("image", nargs="?")
63 | group.add_argument(
64 | "--list-layers", action="store_true", help="list layers")
65 | group.add_argument(
66 | "--video", type=str, help="video file")
67 | args = parser.parse_args()
68 | if args.list_layers:
69 | list_layers(network=args.network)
70 | elif args.video:
71 | clip = True
72 | if args.clip == "false":
73 | clip = False
74 | deepdream_video(
75 | args.video, iter_n=args.itern, octave_n=args.octaves,
76 | octave_scale=args.octave_scale, end=args.layers, clip=clip,
77 | network=args.network, frame_rate=args.framerate)
78 | else:
79 | zoom = True
80 | if args.zoom == "false":
81 | zoom = False
82 | clip = True
83 | if args.clip == "false":
84 | clip = False
85 | gif = False
86 | if args.gif == "true":
87 | gif = True
88 | reverse = False
89 | if args.reverse == "true":
90 | reverse = True
91 | loop = False
92 | if args.loop == "true":
93 | loop = True
94 | gpu = False
95 | if args.gpuid >= 0:
96 | gpu = True
97 | deepdream(
98 | args.image, zoom=zoom, scale_coefficient=args.scale,
99 | irange=args.dreams, iter_n=args.itern, octave_n=args.octaves,
100 | octave_scale=args.octave_scale, end=args.layers, clip=clip,
101 | network=args.network, gif=gif, reverse=reverse,
102 | duration=args.duration, loop=loop, gpu=gpu, gpuid=args.gpuid)
103 | except Exception as e:
104 | print("Error: {}".format(e))
105 | sys.exit(2)
106 |
107 |
108 | if __name__ == "__main__":
109 | main()
110 |
--------------------------------------------------------------------------------
/deepdreamer/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kesara/deepdreamer/9e1a3deebb677878a12c6c25178a78a8be230556/deepdreamer/__init__.py
--------------------------------------------------------------------------------
/deepdreamer/deepdreamer.py:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Deep Dreamer
3 | # Based on https://github.com/google/deepdream/blob/master/dream.ipynb
4 | # Author: Kesara Rathnayake ( kesara [at] kesara [dot] lk )
5 | ###############################################################################
6 |
7 | from os import mkdir, listdir
8 | from subprocess import PIPE, Popen
9 |
10 | import numpy as np
11 | from caffe import Classifier, set_device, set_mode_gpu
12 | from deepdreamer.images2gif import writeGif
13 | from scipy.ndimage import affine_transform, zoom
14 | from PIL.Image import fromarray as img_fromarray, open as img_open
15 | import logging
16 |
17 | logging.basicConfig(
18 | filename='log.txt',
19 | format='%(asctime)s %(message)s',
20 | datefmt='%m/%d/%Y %I:%M:%S %p',
21 | level=logging.NOTSET)
22 |
23 |
24 | def _select_network(netname):
25 | if netname == 'bvlc_googlenet':
26 | NET_FN = "deploy.prototxt" # Make sure force_backward: true
27 | PARAM_FN = "bvlc_googlenet.caffemodel"
28 | CHANNEL_SWAP = (2, 1, 0)
29 | # ImageNet mean, training set dependent
30 | CAFFE_MEAN = np.float32([104.0, 116.0, 122.0])
31 | return NET_FN, PARAM_FN, CHANNEL_SWAP, CAFFE_MEAN
32 | elif netname == 'googlenet_place205':
33 | # TODO: refit SWAP and MEAN for places205? These work for now.
34 | NET_FN = "deploy_places205.protxt" # Make sure force_backward: true
35 | PARAM_FN = "googlelet_places205_train_iter_2400000.caffemodel"
36 | CHANNEL_SWAP = (2, 1, 0)
37 | # ImageNet mean, training set dependent
38 | CAFFE_MEAN = np.float32([104.0, 116.0, 122.0])
39 | return NET_FN, PARAM_FN, CHANNEL_SWAP, CAFFE_MEAN
40 | else:
41 | print("Error: network {} not implemented".format(netname))
42 |
43 |
44 | def _preprocess(net, img):
45 | return np.float32(np.rollaxis(img, 2)[::-1]) - net.transformer.mean["data"]
46 |
47 |
48 | def _deprocess(net, img):
49 | return np.dstack((img + net.transformer.mean["data"])[::-1])
50 |
51 |
52 | def _make_step(
53 | net, step_size=1.5, end="inception_4c/output", jitter=32, clip=True):
54 | """ Basic gradient ascent step. """
55 |
56 | src = net.blobs["data"]
57 | dst = net.blobs[end]
58 |
59 | ox, oy = np.random.randint(-jitter, jitter+1, 2)
60 |
61 | # apply jitter shift
62 | src.data[0] = np.roll(np.roll(src.data[0], ox, -1), oy, -2)
63 |
64 | net.forward(end=end)
65 | dst.diff[:] = dst.data # specify the optimization objective
66 | net.backward(start=end)
67 | g = src.diff[0]
68 | # apply normalized ascent step to the input image
69 | src.data[:] += step_size/np.abs(g).mean() * g
70 | # unshift image
71 | src.data[0] = np.roll(np.roll(src.data[0], -ox, -1), -oy, -2)
72 |
73 | if clip:
74 | bias = net.transformer.mean["data"]
75 | src.data[:] = np.clip(src.data, -bias, 255-bias)
76 |
77 |
78 | def _deepdream(
79 | net, base_img, iter_n=10, octave_n=4, octave_scale=1.4,
80 | end="inception_4c/output", clip=True, **step_params):
81 | # prepare base images for all octaves
82 | octaves = [_preprocess(net, base_img)]
83 |
84 | for i in range(octave_n-1):
85 | octaves.append(zoom(
86 | octaves[-1], (1, 1.0/octave_scale, 1.0/octave_scale), order=1))
87 |
88 | src = net.blobs["data"]
89 |
90 | # allocate image for network-produced details
91 | detail = np.zeros_like(octaves[-1])
92 |
93 | for octave, octave_base in enumerate(octaves[::-1]):
94 | h, w = octave_base.shape[-2:]
95 | if octave > 0:
96 | # upscale details from the previous octave
97 | h1, w1 = detail.shape[-2:]
98 | detail = zoom(detail, (1, 1.0*h/h1, 1.0*w/w1), order=1)
99 |
100 | src.reshape(1, 3, h, w) # resize the network's input image size
101 | src.data[0] = octave_base+detail
102 |
103 | for i in range(iter_n):
104 | _make_step(net, end=end, clip=clip, **step_params)
105 |
106 | # visualization
107 | vis = _deprocess(net, src.data[0])
108 | if not clip: # adjust image contrast if clipping is disabled
109 | vis = vis*(255.0/np.percentile(vis, 99.98))
110 |
111 | # extract details produced on the current octave
112 | detail = src.data[0]-octave_base
113 |
114 | # returning the resulting image
115 | return _deprocess(net, src.data[0])
116 |
117 |
118 | def _output_video_dir(video):
119 | return "{}_images".format(video)
120 |
121 |
122 | def _extract_video(video):
123 | output_dir = _output_video_dir(video)
124 | mkdir(output_dir)
125 | output = Popen(
126 | "ffmpeg -loglevel quiet -i {} -f image2 {}/img_%4d.jpg".format(
127 | video, output_dir), shell=True, stdout=PIPE).stdout.read()
128 |
129 |
130 | def _create_video(video, frame_rate=24):
131 | output_dir = _output_video_dir(video)
132 | output = Popen((
133 | "ffmpeg -loglevel quiet -r {} -f image2 -pattern_type glob "
134 | "-i \"{}/img_*.jpg\" {}.mp4").format(
135 | frame_rate, output_dir, video),
136 | shell=True, stdout=PIPE).stdout.read()
137 |
138 |
139 | def list_layers(network="bvlc_googlenet"):
140 | # Load DNN model
141 | NET_FN, PARAM_FN, CHANNEL_SWAP, CAFFE_MEAN = _select_network(network)
142 | net = Classifier(
143 | NET_FN, PARAM_FN, mean=CAFFE_MEAN, channel_swap=CHANNEL_SWAP)
144 | net.blobs.keys()
145 |
146 |
147 | def deepdream(
148 | img_path, zoom=True, scale_coefficient=0.05, irange=100, iter_n=10,
149 | octave_n=4, octave_scale=1.4, end="inception_4c/output", clip=True,
150 | network="bvlc_googlenet", gif=False, reverse=False, duration=0.1,
151 | loop=False, gpu=False, gpuid=0):
152 | img = np.float32(img_open(img_path))
153 | s = scale_coefficient
154 | h, w = img.shape[:2]
155 |
156 | if gpu:
157 | print("Enabling GPU {}...".format(gpuid))
158 | set_device(gpuid)
159 | set_mode_gpu()
160 |
161 | # Select, load DNN model
162 | NET_FN, PARAM_FN, CHANNEL_SWAP, CAFFE_MEAN = _select_network(network)
163 | net = Classifier(
164 | NET_FN, PARAM_FN, mean=CAFFE_MEAN, channel_swap=CHANNEL_SWAP)
165 |
166 | img_pool = [img_path]
167 |
168 | # Save settings used in a log file
169 | logging.info((
170 | "{} zoom={}, scale_coefficient={}, irange={}, iter_n={}, "
171 | "octave_n={}, octave_scale={}, end={}, clip={}, network={}, gif={}, "
172 | "reverse={}, duration={}, loop={}").format(
173 | img_path, zoom, scale_coefficient, irange, iter_n, octave_n,
174 | octave_scale, end, clip, network, gif, reverse, duration, loop))
175 |
176 | print("Dreaming...")
177 | for i in range(irange):
178 | img = _deepdream(
179 | net, img, iter_n=iter_n, octave_n=octave_n,
180 | octave_scale=octave_scale, end=end, clip=clip)
181 | img_fromarray(np.uint8(img)).save("{}_{}.jpg".format(
182 | img_path, i))
183 | if gif:
184 | img_pool.append("{}_{}.jpg".format(img_path, i))
185 | print("Dream {} saved.".format(i))
186 | if zoom:
187 | img = affine_transform(
188 | img, [1-s, 1-s, 1], [h*s/2, w*s/2, 0], order=1)
189 | if gif:
190 | print("Creating gif...")
191 | frames = None
192 | if reverse:
193 | frames = [img_open(f) for f in img_pool[::-1]]
194 | else:
195 | frames = [img_open(f) for f in img_pool]
196 | writeGif(
197 | "{}.gif".format(img_path), frames, duration=duration,
198 | repeat=loop)
199 | print("gif created.")
200 |
201 |
202 | def deepdream_video(
203 | video, iter_n=10, octave_n=4, octave_scale=1.4,
204 | end="inception_4c/output", clip=True, network="bvlc_googlenet",
205 | frame_rate=24):
206 |
207 | # Select, load DNN model
208 | NET_FN, PARAM_FN, CHANNEL_SWAP, CAFFE_MEAN = _select_network(network)
209 | net = Classifier(
210 | NET_FN, PARAM_FN, mean=CAFFE_MEAN, channel_swap=CHANNEL_SWAP)
211 |
212 | print("Extracting video...")
213 | _extract_video(video)
214 |
215 | output_dir = _output_video_dir(video)
216 | images = listdir(output_dir)
217 |
218 | print("Dreaming...")
219 | for image in images:
220 | image = "{}/{}".format(output_dir, image)
221 | img = np.float32(img_open(image))
222 | img = _deepdream(
223 | net, img, iter_n=iter_n, octave_n=octave_n,
224 | octave_scale=octave_scale, end=end, clip=clip)
225 | img_fromarray(np.uint8(img)).save(image)
226 |
227 | print("Creating dream video...")
228 | _create_video(video, frame_rate)
229 | print("Dream video created.")
230 |
--------------------------------------------------------------------------------
/deepdreamer/images2gif.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | # Copyright (C) 2012, Almar Klein, Ant1, Marius van Voorden
3 | #
4 | # This code is subject to the (new) BSD license:
5 | #
6 | # Redistribution and use in source and binary forms, with or without
7 | # modification, are permitted provided that the following conditions are met:
8 | # * Redistributions of source code must retain the above copyright
9 | # notice, this list of conditions and the following disclaimer.
10 | # * Redistributions in binary form must reproduce the above copyright
11 | # notice, this list of conditions and the following disclaimer in the
12 | # documentation and/or other materials provided with the distribution.
13 | # * Neither the name of the nor the
14 | # names of its contributors may be used to endorse or promote products
15 | # derived from this software without specific prior written permission.
16 | #
17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 | # ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
21 | # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 |
28 | """ Module images2gif
29 |
30 | Provides functionality for reading and writing animated GIF images.
31 | Use writeGif to write a series of numpy arrays or PIL images as an
32 | animated GIF. Use readGif to read an animated gif as a series of numpy
33 | arrays.
34 |
35 | Note that since July 2004, all patents on the LZW compression patent have
36 | expired. Therefore the GIF format may now be used freely.
37 |
38 | Acknowledgements
39 | ----------------
40 |
41 | Many thanks to Ant1 for:
42 | * noting the use of "palette=PIL.Image.ADAPTIVE", which significantly
43 | improves the results.
44 | * the modifications to save each image with its own palette, or optionally
45 | the global palette (if its the same).
46 |
47 | Many thanks to Marius van Voorden for porting the NeuQuant quantization
48 | algorithm of Anthony Dekker to Python (See the NeuQuant class for its
49 | license).
50 |
51 | Many thanks to Alex Robinson for implementing the concept of subrectangles,
52 | which (depening on image content) can give a very significant reduction in
53 | file size.
54 |
55 | This code is based on gifmaker (in the scripts folder of the source
56 | distribution of PIL)
57 |
58 |
59 | Usefull links
60 | -------------
61 | * http://tronche.com/computer-graphics/gif/
62 | * http://en.wikipedia.org/wiki/Graphics_Interchange_Format
63 | * http://www.w3.org/Graphics/GIF/spec-gif89a.txt
64 |
65 | """
66 | # todo: This module should be part of imageio (or at least based on)
67 |
68 | import os
69 | import time
70 |
71 | try:
72 | import PIL
73 | from PIL import Image
74 | from PIL.GifImagePlugin import getheader, getdata
75 | except ImportError:
76 | PIL = None
77 |
78 | try:
79 | import numpy as np
80 | except ImportError:
81 | np = None
82 |
83 |
84 | def get_cKDTree():
85 | try:
86 | from scipy.spatial import cKDTree
87 | except ImportError:
88 | cKDTree = None
89 | return cKDTree
90 |
91 |
92 | # getheader gives a 87a header and a color palette (two elements in a list).
93 | # getdata()[0] gives the Image Descriptor up to (including) "LZW min code size"
94 | # getdatas()[1:] is the image data itself in chuncks of 256 bytes (well
95 | # technically the first byte says how many bytes follow, after which that
96 | # amount (max 255) follows).
97 |
98 | def checkImages(images):
99 | """ checkImages(images)
100 | Check numpy images and correct intensity range etc.
101 | The same for all movie formats.
102 | """
103 | # Init results
104 | images2 = []
105 |
106 | for im in images:
107 | if PIL and isinstance(im, PIL.Image.Image):
108 | # We assume PIL images are allright
109 | images2.append(im)
110 |
111 | elif np and isinstance(im, np.ndarray):
112 | # Check and convert dtype
113 | if im.dtype == np.uint8:
114 | images2.append(im) # Ok
115 | elif im.dtype in [np.float32, np.float64]:
116 | im = im.copy()
117 | im[im < 0] = 0
118 | im[im > 1] = 1
119 | im *= 255
120 | images2.append(im.astype(np.uint8))
121 | else:
122 | im = im.astype(np.uint8)
123 | images2.append(im)
124 | # Check size
125 | if im.ndim == 2:
126 | pass # ok
127 | elif im.ndim == 3:
128 | if im.shape[2] not in [3, 4]:
129 | raise ValueError('This array can not represent an image.')
130 | else:
131 | raise ValueError('This array can not represent an image.')
132 | else:
133 | raise ValueError('Invalid image type: ' + str(type(im)))
134 |
135 | # Done
136 | return images2
137 |
138 |
139 | def intToBin(i):
140 | """ Integer to two bytes """
141 | # devide in two parts (bytes)
142 | i1 = i % 256
143 | i2 = int(i / 256)
144 | # make string (little endian)
145 | return chr(i1) + chr(i2)
146 |
147 |
148 | class GifWriter:
149 |
150 | """ GifWriter()
151 |
152 | Class that contains methods for helping write the animated GIF file.
153 |
154 | """
155 |
156 | def getheaderAnim(self, im):
157 | """ getheaderAnim(im)
158 |
159 | Get animation header. To replace PILs getheader()[0]
160 |
161 | """
162 | bb = "GIF89a"
163 | bb += intToBin(im.size[0])
164 | bb += intToBin(im.size[1])
165 | bb += "\x87\x00\x00"
166 | return bb
167 |
168 | def getImageDescriptor(self, im, xy=None):
169 | """ getImageDescriptor(im, xy=None)
170 |
171 | Used for the local color table properties per image.
172 | Otherwise global color table applies to all frames irrespective of
173 | whether additional colors comes in play that require a redefined
174 | palette. Still a maximum of 256 color per frame, obviously.
175 |
176 | Written by Ant1 on 2010-08-22
177 | Modified by Alex Robinson in Janurari 2011 to implement subrectangles.
178 |
179 | """
180 |
181 | # Defaule use full image and place at upper left
182 | if xy is None:
183 | xy = (0, 0)
184 |
185 | # Image separator,
186 | bb = '\x2C'
187 |
188 | # Image position and size
189 | bb += intToBin(xy[0]) # Left position
190 | bb += intToBin(xy[1]) # Top position
191 | bb += intToBin(im.size[0]) # image width
192 | bb += intToBin(im.size[1]) # image height
193 |
194 | # packed field: local color table flag1, interlace0, sorted table0,
195 | # reserved00, lct size111=7=2^(7+1)=256.
196 | bb += '\x87'
197 |
198 | # LZW minimum size code now comes later, begining of [image data]
199 | # blocks
200 | return bb
201 |
202 | def getAppExt(self, loops=float('inf')):
203 | """ getAppExt(loops=float('inf'))
204 |
205 | Application extention. This part specifies the amount of loops.
206 | If loops is 0 or inf, it goes on infinitely.
207 |
208 | """
209 |
210 | if loops == 0 or loops == float('inf'):
211 | loops = 2**16 - 1
212 | # bb = "" # application extension should not be used
213 | # (the extension interprets zero loops
214 | # to mean an infinite number of loops)
215 | # Mmm, does not seem to work
216 | if True:
217 | bb = "\x21\xFF\x0B" # application extension
218 | bb += "NETSCAPE2.0"
219 | bb += "\x03\x01"
220 | bb += intToBin(loops)
221 | bb += '\x00' # end
222 | return bb
223 |
224 | def getGraphicsControlExt(
225 | self, duration=0.1, dispose=2, transparent_flag=0,
226 | transparency_index=0):
227 | """ getGraphicsControlExt(duration=0.1, dispose=2)
228 |
229 | Graphics Control Extension. A sort of header at the start of
230 | each image. Specifies duration and transparancy.
231 |
232 | Dispose
233 | -------
234 | * 0 - No disposal specified.
235 | * 1 - Do not dispose. The graphic is to be left in place.
236 | * 2 - Restore to background color. The area used by the graphic
237 | must be restored to the background color.
238 | * 3 - Restore to previous. The decoder is required to restore the
239 | area overwritten by the graphic with what was there prior to
240 | rendering the graphic.
241 | * 4-7 -To be defined.
242 |
243 | """
244 |
245 | bb = '\x21\xF9\x04'
246 | # low bit 1 == transparency,
247 | bb += chr(((dispose & 3) << 2) | (transparent_flag & 1))
248 | # 2nd bit 1 == user input , next 3 bits, the low two of which are used,
249 | # are dispose.
250 | bb += intToBin(int(duration * 100)) # in 100th of seconds
251 | bb += chr(transparency_index) # transparency index
252 | bb += '\x00' # end
253 | return bb
254 |
255 | def handleSubRectangles(self, images, subRectangles):
256 | """ handleSubRectangles(images)
257 |
258 | Handle the sub-rectangle stuff. If the rectangles are given by the
259 | user, the values are checked. Otherwise the subrectangles are
260 | calculated automatically.
261 |
262 | """
263 | image_info = [im.info for im in images]
264 | if isinstance(subRectangles, (tuple, list)):
265 | # xy given directly
266 |
267 | # Check xy
268 | xy = subRectangles
269 | if xy is None:
270 | xy = (0, 0)
271 | if hasattr(xy, '__len__'):
272 | if len(xy) == len(images):
273 | xy = [xxyy for xxyy in xy]
274 | else:
275 | raise ValueError("len(xy) doesn't match amount of images.")
276 | else:
277 | xy = [xy for im in images]
278 | xy[0] = (0, 0)
279 |
280 | else:
281 | # Calculate xy using some basic image processing
282 |
283 | # Check Numpy
284 | if np is None:
285 | raise RuntimeError("Need Numpy to use auto-subRectangles.")
286 |
287 | # First make numpy arrays if required
288 | for i in range(len(images)):
289 | im = images[i]
290 | if isinstance(im, Image.Image):
291 | tmp = im.convert() # Make without palette
292 | a = np.asarray(tmp)
293 | if len(a.shape) == 0:
294 | raise MemoryError(
295 | "Too little memory to convert PIL image to array")
296 | images[i] = a
297 |
298 | # Determine the sub rectangles
299 | images, xy = self.getSubRectangles(images)
300 |
301 | # Done
302 | return images, xy, image_info
303 |
304 | def getSubRectangles(self, ims):
305 | """ getSubRectangles(ims)
306 |
307 | Calculate the minimal rectangles that need updating each frame.
308 | Returns a two-element tuple containing the cropped images and a
309 | list of x-y positions.
310 |
311 | Calculating the subrectangles takes extra time, obviously. However,
312 | if the image sizes were reduced, the actual writing of the GIF
313 | goes faster. In some cases applying this method produces a GIF faster.
314 |
315 | """
316 |
317 | # Check image count
318 | if len(ims) < 2:
319 | return ims, [(0, 0) for i in ims]
320 |
321 | # We need numpy
322 | if np is None:
323 | raise RuntimeError("Need Numpy to calculate sub-rectangles. ")
324 |
325 | # Prepare
326 | ims2 = [ims[0]]
327 | xy = [(0, 0)]
328 | t0 = time.time()
329 |
330 | # Iterate over images
331 | prev = ims[0]
332 | for im in ims[1:]:
333 |
334 | # Get difference, sum over colors
335 | diff = np.abs(im - prev)
336 | if diff.ndim == 3:
337 | diff = diff.sum(2)
338 | # Get begin and end for both dimensions
339 | X = np.argwhere(diff.sum(0))
340 | Y = np.argwhere(diff.sum(1))
341 | # Get rect coordinates
342 | if X.size and Y.size:
343 | x0, x1 = X[0], X[-1] + 1
344 | y0, y1 = Y[0], Y[-1] + 1
345 | else: # No change ... make it minimal
346 | x0, x1 = 0, 2
347 | y0, y1 = 0, 2
348 |
349 | # Cut out and store
350 | im2 = im[y0:y1, x0:x1]
351 | prev = im
352 | ims2.append(im2)
353 | xy.append((x0, y0))
354 |
355 | # Done
356 | # print('%1.2f seconds to determine subrectangles of %i images' %
357 | # (time.time()-t0, len(ims2)) )
358 | return ims2, xy
359 |
360 | def convertImagesToPIL(self, images, dither, nq=0, images_info=None):
361 | """ convertImagesToPIL(images, nq=0)
362 |
363 | Convert images to Paletted PIL images, which can then be
364 | written to a single animaged GIF.
365 |
366 | """
367 |
368 | # Convert to PIL images
369 | images2 = []
370 | for im in images:
371 | if isinstance(im, Image.Image):
372 | images2.append(im)
373 | elif np and isinstance(im, np.ndarray):
374 | if im.ndim == 3 and im.shape[2] == 3:
375 | im = Image.fromarray(im, 'RGB')
376 | elif im.ndim == 3 and im.shape[2] == 4:
377 | # im = Image.fromarray(im[:,:,:3],'RGB')
378 | self.transparency = True
379 | im = Image.fromarray(im[:, :, :4], 'RGBA')
380 | elif im.ndim == 2:
381 | im = Image.fromarray(im, 'L')
382 | images2.append(im)
383 |
384 | # Convert to paletted PIL images
385 | images, images2 = images2, []
386 | if nq >= 1:
387 | # NeuQuant algorithm
388 | for im in images:
389 | im = im.convert("RGBA") # NQ assumes RGBA
390 | nqInstance = NeuQuant(im, int(nq)) # Learn colors from image
391 | if dither:
392 | im = im.convert("RGB").quantize(
393 | palette=nqInstance.paletteImage(),
394 | colors=255)
395 | else:
396 | im = nqInstance.quantize(
397 | im,
398 | colors=255) # Use to quantize the image itself
399 |
400 | self.transparency = True # since NQ assumes transparency
401 | if self.transparency:
402 | alpha = im.split()[3]
403 | mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
404 | im.paste(255, mask=mask)
405 | images2.append(im)
406 | else:
407 | # Adaptive PIL algorithm
408 | AD = Image.ADAPTIVE
409 | # for index,im in enumerate(images):
410 | for i in range(len(images)):
411 | im = images[i].convert('RGB').convert(
412 | 'P',
413 | palette=AD,
414 | dither=dither,
415 | colors=255)
416 | if self.transparency:
417 | alpha = images[i].split()[3]
418 | mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
419 | im.paste(255, mask=mask)
420 | images2.append(im)
421 |
422 | # Done
423 | return images2
424 |
425 | def writeGifToFile(self, fp, images, durations, loops, xys, disposes):
426 | """ writeGifToFile(fp, images, durations, loops, xys, disposes)
427 |
428 | Given a set of images writes the bytes to the specified stream.
429 |
430 | """
431 |
432 | # Obtain palette for all images and count each occurance
433 | palettes, occur = [], []
434 | for im in images:
435 | palettes.append(im.palette.getdata()[1])
436 | for palette in palettes:
437 | occur.append(palettes.count(palette))
438 |
439 | # Select most-used palette as the global one (or first in case no max)
440 | globalPalette = palettes[occur.index(max(occur))]
441 |
442 | # Init
443 | frames = 0
444 | firstFrame = True
445 |
446 | for im, palette in zip(images, palettes):
447 |
448 | if firstFrame:
449 | # Write header
450 |
451 | # Gather info
452 | header = self.getheaderAnim(im)
453 | appext = self.getAppExt(loops)
454 |
455 | # Write
456 | fp.write(header)
457 | fp.write(globalPalette)
458 | fp.write(appext)
459 |
460 | # Next frame is not the first
461 | firstFrame = False
462 |
463 | if True:
464 | # Write palette and image data
465 |
466 | # Gather info
467 | data = getdata(im)
468 | imdes, data = data[0], data[1:]
469 |
470 | transparent_flag = 0
471 | if self.transparency:
472 | transparent_flag = 1
473 |
474 | graphext = self.getGraphicsControlExt(
475 | durations[frames], disposes[frames],
476 | transparent_flag=transparent_flag, transparency_index=255)
477 |
478 | # Make image descriptor suitable for using 256 local color
479 | # palette
480 | lid = self.getImageDescriptor(im, xys[frames])
481 |
482 | # Write local header
483 | if (palette != globalPalette) or (disposes[frames] != 2):
484 | # Use local color palette
485 | fp.write(graphext)
486 | fp.write(lid) # write suitable image descriptor
487 | fp.write(palette) # write local color table
488 | fp.write('\x08') # LZW minimum size code
489 | else:
490 | # Use global color palette
491 | fp.write(graphext)
492 | fp.write(imdes) # write suitable image descriptor
493 |
494 | # Write image data
495 | for d in data:
496 | fp.write(d)
497 |
498 | # Prepare for next round
499 | frames = frames + 1
500 |
501 | fp.write(";") # end gif
502 | return frames
503 |
504 |
505 | # Exposed functions
506 |
507 | def writeGif(filename, images, duration=0.1, repeat=True, dither=False,
508 | nq=0, subRectangles=True, dispose=None):
509 | """ writeGif(filename, images, duration=0.1, repeat=True, dither=False,
510 | nq=0, subRectangles=True, dispose=None)
511 |
512 | Write an animated gif from the specified images.
513 |
514 | Parameters
515 | ----------
516 | filename : string
517 | The name of the file to write the image to.
518 | images : list
519 | Should be a list consisting of PIL images or numpy arrays.
520 | The latter should be between 0 and 255 for integer types, and
521 | between 0 and 1 for float types.
522 | duration : scalar or list of scalars
523 | The duration for all frames, or (if a list) for each frame.
524 | repeat : bool or integer
525 | The amount of loops. If True, loops infinitetely.
526 | dither : bool
527 | Whether to apply dithering
528 | nq : integer
529 | If nonzero, applies the NeuQuant quantization algorithm to create
530 | the color palette. This algorithm is superior, but slower than
531 | the standard PIL algorithm. The value of nq is the quality
532 | parameter. 1 represents the best quality. 10 is in general a
533 | good tradeoff between quality and speed. When using this option,
534 | better results are usually obtained when subRectangles is False.
535 | subRectangles : False, True, or a list of 2-element tuples
536 | Whether to use sub-rectangles. If True, the minimal rectangle that
537 | is required to update each frame is automatically detected. This
538 | can give significant reductions in file size, particularly if only
539 | a part of the image changes. One can also give a list of x-y
540 | coordinates if you want to do the cropping yourself. The default
541 | is True.
542 | dispose : int
543 | How to dispose each frame. 1 means that each frame is to be left
544 | in place. 2 means the background color should be restored after
545 | each frame. 3 means the decoder should restore the previous frame.
546 | If subRectangles==False, the default is 2, otherwise it is 1.
547 |
548 | """
549 |
550 | # Check PIL
551 | if PIL is None:
552 | raise RuntimeError("Need PIL to write animated gif files.")
553 |
554 | # Check images
555 | images = checkImages(images)
556 |
557 | # Instantiate writer object
558 | gifWriter = GifWriter()
559 | # init transparency flag used in GifWriter functions
560 | gifWriter.transparency = False
561 |
562 | # Check loops
563 | if repeat is False:
564 | loops = 1
565 | elif repeat is True:
566 | loops = 0 # zero means infinite
567 | else:
568 | loops = int(repeat)
569 |
570 | # Check duration
571 | if hasattr(duration, '__len__'):
572 | if len(duration) == len(images):
573 | duration = [d for d in duration]
574 | else:
575 | raise ValueError("len(duration) doesn't match amount of images.")
576 | else:
577 | duration = [duration for im in images]
578 |
579 | # Check subrectangles
580 | if subRectangles:
581 | images, xy, images_info = gifWriter.handleSubRectangles(
582 | images, subRectangles)
583 | defaultDispose = 1 # Leave image in place
584 | else:
585 | # Normal mode
586 | xy = [(0, 0) for im in images]
587 | defaultDispose = 2 # Restore to background color.
588 |
589 | # Check dispose
590 | if dispose is None:
591 | dispose = defaultDispose
592 | if hasattr(dispose, '__len__'):
593 | if len(dispose) != len(images):
594 | raise ValueError("len(xy) doesn't match amount of images.")
595 | else:
596 | dispose = [dispose for im in images]
597 |
598 | # Make images in a format that we can write easy
599 | images = gifWriter.convertImagesToPIL(images, dither, nq)
600 |
601 | # Write
602 | fp = open(filename, 'wb')
603 | try:
604 | gifWriter.writeGifToFile(fp, images, duration, loops, xy, dispose)
605 | finally:
606 | fp.close()
607 |
608 |
609 | def readGif(filename, asNumpy=True):
610 | """ readGif(filename, asNumpy=True)
611 |
612 | Read images from an animated GIF file. Returns a list of numpy
613 | arrays, or, if asNumpy is false, a list if PIL images.
614 |
615 | """
616 |
617 | # Check PIL
618 | if PIL is None:
619 | raise RuntimeError("Need PIL to read animated gif files.")
620 |
621 | # Check Numpy
622 | if np is None:
623 | raise RuntimeError("Need Numpy to read animated gif files.")
624 |
625 | # Check whether it exists
626 | if not os.path.isfile(filename):
627 | raise IOError('File not found: ' + str(filename))
628 |
629 | # Load file using PIL
630 | pilIm = PIL.Image.open(filename)
631 | pilIm.seek(0)
632 |
633 | # Read all images inside
634 | images = []
635 | try:
636 | while True:
637 | # Get image as numpy array
638 | tmp = pilIm.convert() # Make without palette
639 | a = np.asarray(tmp)
640 | if len(a.shape) == 0:
641 | raise MemoryError(
642 | "Too little memory to convert PIL image to array")
643 | # Store, and next
644 | images.append(a)
645 | pilIm.seek(pilIm.tell() + 1)
646 | except EOFError:
647 | pass
648 |
649 | # Convert to normal PIL images if needed
650 | if not asNumpy:
651 | images2 = images
652 | images = []
653 | for index, im in enumerate(images2):
654 | tmp = PIL.Image.fromarray(im)
655 | images.append(tmp)
656 |
657 | # Done
658 | return images
659 |
660 |
661 | class NeuQuant:
662 |
663 | """ NeuQuant(image, samplefac=10, colors=256)
664 |
665 | samplefac should be an integer number of 1 or higher, 1
666 | being the highest quality, but the slowest performance.
667 | With avalue of 10, one tenth of all pixels are used during
668 | training. This value seems a nice tradeof between speed
669 | and quality.
670 |
671 | colors is the amount of colors to reduce the image to. This
672 | should best be a power of two.
673 |
674 | See also:
675 | http://members.ozemail.com.au/~dekker/NEUQUANT.HTML
676 |
677 | License of the NeuQuant Neural-Net Quantization Algorithm
678 | ---------------------------------------------------------
679 |
680 | Copyright (c) 1994 Anthony Dekker
681 | Ported to python by Marius van Voorden in 2010
682 |
683 | NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994.
684 | See "Kohonen neural networks for optimal colour quantization"
685 | in "network: Computation in Neural Systems" Vol. 5 (1994) pp 351-367.
686 | for a discussion of the algorithm.
687 | See also http://members.ozemail.com.au/~dekker/NEUQUANT.HTML
688 |
689 | Any party obtaining a copy of these files from the author, directly or
690 | indirectly, is granted, free of charge, a full and unrestricted
691 | irrevocable, world-wide, paid up, royalty-free, nonexclusive right and
692 | license to deal in this software and documentation files (the "Software"),
693 | including without limitation the rights to use, copy, modify, merge,
694 | publish, distribute, sublicense, and/or sell copies of the Software, and
695 | to permit persons who receive copies from any such party to do so, with
696 | the only requirement being that this copyright notice remain intact.
697 |
698 | """
699 |
700 | NCYCLES = None # Number of learning cycles
701 | NETSIZE = None # Number of colours used
702 | SPECIALS = None # Number of reserved colours used
703 | BGCOLOR = None # Reserved background colour
704 | CUTNETSIZE = None
705 | MAXNETPOS = None
706 |
707 | INITRAD = None # For 256 colours, radius starts at 32
708 | RADIUSBIASSHIFT = None
709 | RADIUSBIAS = None
710 | INITBIASRADIUS = None
711 | RADIUSDEC = None # Factor of 1/30 each cycle
712 |
713 | ALPHABIASSHIFT = None
714 | INITALPHA = None # biased by 10 bits
715 |
716 | GAMMA = None
717 | BETA = None
718 | BETAGAMMA = None
719 |
720 | network = None # The network itself
721 | colormap = None # The network itself
722 |
723 | netindex = None # For network lookup - really 256
724 |
725 | bias = None # Bias and freq arrays for learning
726 | freq = None
727 |
728 | pimage = None
729 |
730 | # Four primes near 500 - assume no image has a length so large
731 | # that it is divisible by all four primes
732 | PRIME1 = 499
733 | PRIME2 = 491
734 | PRIME3 = 487
735 | PRIME4 = 503
736 | MAXPRIME = PRIME4
737 |
738 | pixels = None
739 | samplefac = None
740 |
741 | a_s = None
742 |
743 | def setconstants(self, samplefac, colors):
744 | self.NCYCLES = 100 # Number of learning cycles
745 | self.NETSIZE = colors # Number of colours used
746 | self.SPECIALS = 3 # Number of reserved colours used
747 | self.BGCOLOR = self.SPECIALS - 1 # Reserved background colour
748 | self.CUTNETSIZE = self.NETSIZE - self.SPECIALS
749 | self.MAXNETPOS = self.NETSIZE - 1
750 |
751 | self.INITRAD = self.NETSIZE / 8 # For 256 colours, radius starts at 32
752 | self.RADIUSBIASSHIFT = 6
753 | self.RADIUSBIAS = 1 << self.RADIUSBIASSHIFT
754 | self.INITBIASRADIUS = self.INITRAD * self.RADIUSBIAS
755 | self.RADIUSDEC = 30 # Factor of 1/30 each cycle
756 |
757 | self.ALPHABIASSHIFT = 10 # Alpha starts at 1
758 | self.INITALPHA = 1 << self.ALPHABIASSHIFT # biased by 10 bits
759 |
760 | self.GAMMA = 1024.0
761 | self.BETA = 1.0 / 1024.0
762 | self.BETAGAMMA = self.BETA * self.GAMMA
763 |
764 | self.network = np.empty(
765 | (self.NETSIZE, 3), dtype='float64') # The network itself
766 | self.colormap = np.empty(
767 | (self.NETSIZE, 4), dtype='int32') # The network itself
768 |
769 | self.netindex = np.empty(
770 | 256,
771 | dtype='int32') # For network lookup - really 256
772 |
773 | self.bias = np.empty(
774 | self.NETSIZE,
775 | dtype='float64') # Bias and freq arrays for learning
776 | self.freq = np.empty(self.NETSIZE, dtype='float64')
777 |
778 | self.pixels = None
779 | self.samplefac = samplefac
780 |
781 | self.a_s = {}
782 |
783 | def __init__(self, image, samplefac=10, colors=256):
784 |
785 | # Check Numpy
786 | if np is None:
787 | raise RuntimeError("Need Numpy for the NeuQuant algorithm.")
788 |
789 | # Check image
790 | if image.size[0] * image.size[1] < NeuQuant.MAXPRIME:
791 | raise IOError("Image is too small")
792 | if image.mode != "RGBA":
793 | raise IOError("Image mode should be RGBA.")
794 |
795 | # Initialize
796 | self.setconstants(samplefac, colors)
797 | self.pixels = np.fromstring(image.tostring(), np.uint32)
798 | self.setUpArrays()
799 |
800 | self.learn()
801 | self.fix()
802 | self.inxbuild()
803 |
804 | def writeColourMap(self, rgb, outstream):
805 | for i in range(self.NETSIZE):
806 | bb = self.colormap[i, 0]
807 | gg = self.colormap[i, 1]
808 | rr = self.colormap[i, 2]
809 | outstream.write(rr if rgb else bb)
810 | outstream.write(gg)
811 | outstream.write(bb if rgb else rr)
812 | return self.NETSIZE
813 |
814 | def setUpArrays(self):
815 | self.network[0, 0] = 0.0 # Black
816 | self.network[0, 1] = 0.0
817 | self.network[0, 2] = 0.0
818 |
819 | self.network[1, 0] = 255.0 # White
820 | self.network[1, 1] = 255.0
821 | self.network[1, 2] = 255.0
822 |
823 | # RESERVED self.BGCOLOR # Background
824 |
825 | for i in range(self.SPECIALS):
826 | self.freq[i] = 1.0 / self.NETSIZE
827 | self.bias[i] = 0.0
828 |
829 | for i in range(self.SPECIALS, self.NETSIZE):
830 | p = self.network[i]
831 | p[:] = (255.0 * (i - self.SPECIALS)) / self.CUTNETSIZE
832 |
833 | self.freq[i] = 1.0 / self.NETSIZE
834 | self.bias[i] = 0.0
835 |
836 | # Omitted: setPixels
837 |
838 | def altersingle(self, alpha, i, b, g, r):
839 | """Move neuron i towards biased (b,g,r) by factor alpha"""
840 | n = self.network[i] # Alter hit neuron
841 | n[0] -= (alpha * (n[0] - b))
842 | n[1] -= (alpha * (n[1] - g))
843 | n[2] -= (alpha * (n[2] - r))
844 |
845 | def geta(self, alpha, rad):
846 | try:
847 | return self.a_s[(alpha, rad)]
848 | except KeyError:
849 | length = rad * 2 - 1
850 | mid = length / 2
851 | q = np.array(list(range(mid - 1, -1, -1)) + list(range(-1, mid)))
852 | a = alpha * (rad * rad - q * q) / (rad * rad)
853 | a[mid] = 0
854 | self.a_s[(alpha, rad)] = a
855 | return a
856 |
857 | def alterneigh(self, alpha, rad, i, b, g, r):
858 | if i - rad >= self.SPECIALS - 1:
859 | lo = i - rad
860 | start = 0
861 | else:
862 | lo = self.SPECIALS - 1
863 | start = (self.SPECIALS - 1 - (i - rad))
864 |
865 | if i + rad <= self.NETSIZE:
866 | hi = i + rad
867 | end = rad * 2 - 1
868 | else:
869 | hi = self.NETSIZE
870 | end = (self.NETSIZE - (i + rad))
871 |
872 | a = self.geta(alpha, rad)[start:end]
873 |
874 | p = self.network[lo + 1:hi]
875 | p -= np.transpose(np.transpose(p - np.array([b, g, r])) * a)
876 |
877 | def contest(self, b, g, r):
878 | """ Search for biased BGR values
879 | Finds closest neuron (min dist) and updates self.freq
880 | finds best neuron (min dist-self.bias) and returns position
881 | for frequently chosen neurons, self.freq[i] is high and
882 | self.bias[i] is negative
883 | self.bias[i] = self.GAMMA*((1/self.NETSIZE)-self.freq[i])"""
884 | i, j = self.SPECIALS, self.NETSIZE
885 | dists = abs(self.network[i:j] - np.array([b, g, r])).sum(1)
886 | bestpos = i + np.argmin(dists)
887 | biasdists = dists - self.bias[i:j]
888 | bestbiaspos = i + np.argmin(biasdists)
889 | self.freq[i:j] *= (1 - self.BETA)
890 | self.bias[i:j] += self.BETAGAMMA * self.freq[i:j]
891 | self.freq[bestpos] += self.BETA
892 | self.bias[bestpos] -= self.BETAGAMMA
893 | return bestbiaspos
894 |
895 | def specialFind(self, b, g, r):
896 | for i in range(self.SPECIALS):
897 | n = self.network[i]
898 | if n[0] == b and n[1] == g and n[2] == r:
899 | return i
900 | return -1
901 |
902 | def learn(self):
903 | biasRadius = self.INITBIASRADIUS
904 | alphadec = 30 + ((self.samplefac - 1) / 3)
905 | lengthcount = self.pixels.size
906 | samplepixels = lengthcount / self.samplefac
907 | delta = samplepixels / self.NCYCLES
908 | alpha = self.INITALPHA
909 |
910 | i = 0
911 | rad = biasRadius >> self.RADIUSBIASSHIFT
912 | if rad <= 1:
913 | rad = 0
914 |
915 | print("Beginning 1D learning: samplepixels = %1.2f rad = %i" %
916 | (samplepixels, rad))
917 | step = 0
918 | pos = 0
919 | if lengthcount % NeuQuant.PRIME1 != 0:
920 | step = NeuQuant.PRIME1
921 | elif lengthcount % NeuQuant.PRIME2 != 0:
922 | step = NeuQuant.PRIME2
923 | elif lengthcount % NeuQuant.PRIME3 != 0:
924 | step = NeuQuant.PRIME3
925 | else:
926 | step = NeuQuant.PRIME4
927 |
928 | i = 0
929 | printed_string = ''
930 | while i < samplepixels:
931 | if i % 100 == 99:
932 | tmp = '\b' * len(printed_string)
933 | printed_string = str((i + 1) * 100 / samplepixels) + "%\n"
934 | print(tmp + printed_string)
935 | p = self.pixels[pos]
936 | r = (p >> 16) & 0xff
937 | g = (p >> 8) & 0xff
938 | b = (p) & 0xff
939 |
940 | if i == 0: # Remember background colour
941 | self.network[self.BGCOLOR] = [b, g, r]
942 |
943 | j = self.specialFind(b, g, r)
944 | if j < 0:
945 | j = self.contest(b, g, r)
946 |
947 | if j >= self.SPECIALS: # Don't learn for specials
948 | a = (1.0 * alpha) / self.INITALPHA
949 | self.altersingle(a, j, b, g, r)
950 | if rad > 0:
951 | self.alterneigh(a, rad, j, b, g, r)
952 |
953 | pos = (pos + step) % lengthcount
954 |
955 | i += 1
956 | if i % delta == 0:
957 | alpha -= alpha / alphadec
958 | biasRadius -= biasRadius / self.RADIUSDEC
959 | rad = biasRadius >> self.RADIUSBIASSHIFT
960 | if rad <= 1:
961 | rad = 0
962 |
963 | finalAlpha = (1.0 * alpha) / self.INITALPHA
964 | print("Finished 1D learning: final alpha = %1.2f!" % finalAlpha)
965 |
966 | def fix(self):
967 | for i in range(self.NETSIZE):
968 | for j in range(3):
969 | x = int(0.5 + self.network[i, j])
970 | x = max(0, x)
971 | x = min(255, x)
972 | self.colormap[i, j] = x
973 | self.colormap[i, 3] = i
974 |
975 | def inxbuild(self):
976 | previouscol = 0
977 | startpos = 0
978 | for i in range(self.NETSIZE):
979 | p = self.colormap[i]
980 | q = None
981 | smallpos = i
982 | smallval = p[1] # Index on g
983 | # Find smallest in i..self.NETSIZE-1
984 | for j in range(i + 1, self.NETSIZE):
985 | q = self.colormap[j]
986 | if q[1] < smallval: # Index on g
987 | smallpos = j
988 | smallval = q[1] # Index on g
989 |
990 | q = self.colormap[smallpos]
991 | # Swap p (i) and q (smallpos) entries
992 | if i != smallpos:
993 | p[:], q[:] = q, p.copy()
994 |
995 | # smallval entry is now in position i
996 | if smallval != previouscol:
997 | self.netindex[previouscol] = (startpos + i) >> 1
998 | for j in range(previouscol + 1, smallval):
999 | self.netindex[j] = i
1000 | previouscol = smallval
1001 | startpos = i
1002 | self.netindex[previouscol] = (startpos + self.MAXNETPOS) >> 1
1003 | for j in range(previouscol + 1, 256): # Really 256
1004 | self.netindex[j] = self.MAXNETPOS
1005 |
1006 | def paletteImage(self):
1007 | """ PIL weird interface for making a paletted image: create an image
1008 | which already has the palette, and use that in Image.quantize. This
1009 | function returns this palette image. """
1010 | if self.pimage is None:
1011 | palette = []
1012 | for i in range(self.NETSIZE):
1013 | palette.extend(self.colormap[i][:3])
1014 |
1015 | palette.extend([0] * (256 - self.NETSIZE) * 3)
1016 |
1017 | # a palette image to use for quant
1018 | self.pimage = Image.new("P", (1, 1), 0)
1019 | self.pimage.putpalette(palette)
1020 | return self.pimage
1021 |
1022 | def quantize(self, image):
1023 | """ Use a kdtree to quickly find the closest palette colors for the
1024 | pixels """
1025 | if get_cKDTree():
1026 | return self.quantize_with_scipy(image)
1027 | else:
1028 | print('Scipy not available, falling back to slower version.')
1029 | return self.quantize_without_scipy(image)
1030 |
1031 | def quantize_with_scipy(self, image):
1032 | w, h = image.size
1033 | px = np.asarray(image).copy()
1034 | px2 = px[:, :, :3].reshape((w * h, 3))
1035 |
1036 | cKDTree = get_cKDTree()
1037 | kdtree = cKDTree(self.colormap[:, :3], leafsize=10)
1038 | result = kdtree.query(px2)
1039 | colorindex = result[1]
1040 | print("Distance: %1.2f" % (result[0].sum() / (w * h)))
1041 | px2[:] = self.colormap[colorindex, :3]
1042 |
1043 | return Image.fromarray(px).convert(
1044 | "RGB").quantize(palette=self.paletteImage())
1045 |
1046 | def quantize_without_scipy(self, image):
1047 | """" This function can be used if no scipy is availabe.
1048 | It's 7 times slower though.
1049 | """
1050 | w, h = image.size
1051 | px = np.asarray(image).copy()
1052 | memo = {}
1053 | for j in range(w):
1054 | for i in range(h):
1055 | key = (px[i, j, 0], px[i, j, 1], px[i, j, 2])
1056 | try:
1057 | val = memo[key]
1058 | except KeyError:
1059 | val = self.convert(*key)
1060 | memo[key] = val
1061 | px[i, j, 0], px[i, j, 1], px[i, j, 2] = val
1062 | return Image.fromarray(px).convert(
1063 | "RGB").quantize(palette=self.paletteImage())
1064 |
1065 | def convert(self, *color):
1066 | i = self.inxsearch(*color)
1067 | return self.colormap[i, :3]
1068 |
1069 | def inxsearch(self, r, g, b):
1070 | """Search for BGR values 0..255 and return colour index"""
1071 | dists = (self.colormap[:, :3] - np.array([r, g, b]))
1072 | a = np.argmin((dists * dists).sum(1))
1073 | return a
1074 |
1075 |
1076 | if __name__ == '__main__':
1077 | im = np.zeros((200, 200), dtype=np.uint8)
1078 | im[10:30, :] = 100
1079 | im[:, 80:120] = 255
1080 | im[-50:-40, :] = 50
1081 |
1082 | images = [im * 1.0, im * 0.8, im * 0.6, im * 0.4, im * 0]
1083 | writeGif('lala3.gif', images, duration=0.5, dither=0)
1084 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | #caffe
2 | numpy
3 | Pillow
4 | scipy
5 |
--------------------------------------------------------------------------------