'
13 |
14 | hr_faded: '
'
15 | hr_shaded: '
'
--------------------------------------------------------------------------------
/docs/_data/definitions.yml:
--------------------------------------------------------------------------------
1 | elephant: "This is a sample definition."
2 |
3 |
--------------------------------------------------------------------------------
/docs/_data/glossary.yml:
--------------------------------------------------------------------------------
1 | jekyll_platform: "Jekyll is a static site generator that builds sites using most modern web technologies."
2 |
3 |
--------------------------------------------------------------------------------
/docs/_data/tags.yml:
--------------------------------------------------------------------------------
1 | # Note:
2 | # If you are using the createtag script, don't leave an blank line at the end of this file.
3 | # In other words, the last line must be the last tag in the allowed-tags list.
4 | allowed-tags:
5 | - getting_started
6 | - content_types
7 | - navigation
8 | - formatting
9 | - publishing
10 | - single_sourcing
11 | - special_layouts
12 | - collaboration
13 | - news
14 | - troubleshooting
15 | - mobile
16 |
--------------------------------------------------------------------------------
/docs/_data/terms.yml:
--------------------------------------------------------------------------------
1 | apple: "apple - the fruit of a disiduous tree."
--------------------------------------------------------------------------------
/docs/_data/topnav.yml:
--------------------------------------------------------------------------------
1 | ## if you want to list an external url, use external_url instead of url. the theme will apply a different link base.
2 | topnav:
3 | - title: Topnav
4 | items:
5 | - title: GitHub
6 | external_url: https://github.com/fastai/fastai
7 | - title: News
8 | external_url: http://www.fast.ai
9 |
10 | #Topnav dropdowns
11 | topnav_dropdowns:
12 | - title: Topnav dropdowns
13 | folders:
14 | # - title: Products
15 | # folderitems:
16 | # - title: Jekyll Documentation Theme
17 | # url: /mydoc_introduction.html
18 |
--------------------------------------------------------------------------------
/docs/_includes/archive.html:
--------------------------------------------------------------------------------
1 | ---
2 | layout: default
3 | type: archive
4 | ---
5 |
6 |
9 |
10 |
11 | {{ content }}
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/docs/_includes/callout.html:
--------------------------------------------------------------------------------
1 |
{{include.content}}
2 |
--------------------------------------------------------------------------------
/docs/_includes/footer.html:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/docs/_includes/google_analytics.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | {% if site.google_analytics %}
4 |
5 |
6 | {% endif %}
--------------------------------------------------------------------------------
/docs/_includes/head_print.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
{% if page.homepage == true %} {{site.homepage_title}} {% elsif page.title %}{{ page.title }}{% endif %} | {{ site.site_title }}
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
23 |
24 |
29 |
--------------------------------------------------------------------------------
/docs/_includes/image.html:
--------------------------------------------------------------------------------
1 |
{% if {{include.url}} %}{% endif %} {% if {{include.url}} %} {% endif %}{% if {{include.caption}} %}{{include.caption}} {% endif %}
2 |
--------------------------------------------------------------------------------
/docs/_includes/important.html:
--------------------------------------------------------------------------------
1 |
Important: {{include.content}}
--------------------------------------------------------------------------------
/docs/_includes/inline_image.html:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/docs/_includes/links.html:
--------------------------------------------------------------------------------
1 | {% comment %}Get links from each sidebar, as listed in the _config.yml file under sidebars{% endcomment %}
2 |
3 | {% for sidebar in site.sidebars %}
4 | {% for entry in site.data.sidebars[sidebar].entries %}
5 | {% for folder in entry.folders %}
6 | {% for folderitem in folder.folderitems %}
7 | {% if folderitem.url contains "html#" %}
8 | [{{folderitem.url | remove: "/" }}]: {{folderitem.url | remove: "/"}}
9 | {% else %}
10 | [{{folderitem.url | remove: "/" | remove: ".html"}}]: {{folderitem.url | remove: "/"}}
11 | {% endif %}
12 | {% for subfolders in folderitem.subfolders %}
13 | {% for subfolderitem in subfolders.subfolderitems %}
14 | [{{subfolderitem.url | remove: "/" | remove: ".html"}}]: {{subfolderitem.url | remove: "/"}}
15 | {% endfor %}
16 | {% endfor %}
17 | {% endfor %}
18 | {% endfor %}
19 | {% endfor %}
20 | {% endfor %}
21 |
22 |
23 | {% comment %} Get links from topnav {% endcomment %}
24 |
25 | {% for entry in site.data.topnav.topnav %}
26 | {% for item in entry.items %}
27 | {% if item.external_url == null %}
28 | [{{item.url | remove: "/" | remove: ".html"}}]: {{item.url | remove: "/"}}
29 | {% endif %}
30 | {% endfor %}
31 | {% endfor %}
32 |
33 | {% comment %}Get links from topnav dropdowns {% endcomment %}
34 |
35 | {% for entry in site.data.topnav.topnav_dropdowns %}
36 | {% for folder in entry.folders %}
37 | {% for folderitem in folder.folderitems %}
38 | {% if folderitem.external_url == null %}
39 | [{{folderitem.url | remove: "/" | remove: ".html"}}]: {{folderitem.url | remove: "/"}}
40 | {% endif %}
41 | {% endfor %}
42 | {% endfor %}
43 | {% endfor %}
44 |
45 |
--------------------------------------------------------------------------------
/docs/_includes/note.html:
--------------------------------------------------------------------------------
1 |
Note: {{include.content}}
2 |
--------------------------------------------------------------------------------
/docs/_includes/search_simple_jekyll.html:
--------------------------------------------------------------------------------
1 |
5 |
6 |
17 |
--------------------------------------------------------------------------------
/docs/_includes/tip.html:
--------------------------------------------------------------------------------
1 |
Tip: {{include.content}}
--------------------------------------------------------------------------------
/docs/_includes/toc.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/docs/_includes/warning.html:
--------------------------------------------------------------------------------
1 |
Warning: {{include.content}}
--------------------------------------------------------------------------------
/docs/_layouts/default_print.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {% include head_print.html %}
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | {{content}}
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/docs/_layouts/none.html:
--------------------------------------------------------------------------------
1 | ---
2 | ---
3 | {{content}}
--------------------------------------------------------------------------------
/docs/_layouts/page.html:
--------------------------------------------------------------------------------
1 | ---
2 | layout: default
3 | ---
4 |
5 |
9 |
10 | {% if page.simple_map == true %}
11 |
12 |
17 |
18 | {% include custom/{{page.map_name}}.html %}
19 |
20 | {% elsif page.complex_map == true %}
21 |
22 |
27 |
28 | {% include custom/{{page.map_name}}.html %}
29 |
30 | {% endif %}
31 |
32 |
33 |
34 | {% if page.summary %}
35 |
{{page.summary}}
36 | {% endif %}
37 |
38 | {% unless page.toc == false %}
39 | {% include toc.html %}
40 | {% endunless %}
41 |
42 |
43 | {% if site.github_editme_path %}
44 |
45 |
Edit me
46 |
47 | {% endif %}
48 |
49 | {{content}}
50 |
51 |
62 |
63 |
64 |
65 | {{site.data.alerts.hr_shaded}}
66 |
67 | {% include footer.html %}
68 |
--------------------------------------------------------------------------------
/docs/_layouts/page_print.html:
--------------------------------------------------------------------------------
1 | ---
2 | layout: default_print
3 | comments: true
4 | ---
5 |
8 |
9 |
10 |
11 | {% if page.summary %}
12 |
{{page.summary}}
13 | {% endif %}
14 | {{ content }}
15 |
16 |
--------------------------------------------------------------------------------
/docs/css/boxshadowproperties.css:
--------------------------------------------------------------------------------
1 | /* box-shadow fonts return errors with prince, so extracting here to put in web output only */
2 |
3 | #search-demo-container ul#results-container {
4 | box-shadow: 2px 3px 2px #dedede;
5 | }
6 |
7 |
8 | hr.shaded {
9 | box-shadow: inset 0 6px 6px -6px rgba(0,0,0,0.5);
10 | }
11 |
12 | .videoThumbs img {
13 | box-shadow: 2px 2px 1px #f0f0f0;
14 | }
15 |
16 | .box {
17 | box-shadow: 2px 2px 4px #dedede;
18 | }
19 |
20 | @media (max-width: 1200px) {
21 | .navbar-collapse {
22 | box-shadow: inset 0 1px 0 rgba(255,255,255,0.1);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/docs/css/fonts/FontAwesome.otf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/css/fonts/FontAwesome.otf
--------------------------------------------------------------------------------
/docs/css/fonts/fontawesome-webfont.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/css/fonts/fontawesome-webfont.eot
--------------------------------------------------------------------------------
/docs/css/fonts/fontawesome-webfont.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/css/fonts/fontawesome-webfont.ttf
--------------------------------------------------------------------------------
/docs/css/fonts/fontawesome-webfont.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/css/fonts/fontawesome-webfont.woff
--------------------------------------------------------------------------------
/docs/css/fonts/fontawesome-webfont.woff2:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/css/fonts/fontawesome-webfont.woff2
--------------------------------------------------------------------------------
/docs/css/modern-business.css:
--------------------------------------------------------------------------------
1 | /*!
2 | * Start Bootstrap - Modern Business HTML Template (http://startbootstrap.com)
3 | * Code licensed under the Apache License v2.0.
4 | * For details, see http://www.apache.org/licenses/LICENSE-2.0.
5 | */
6 |
7 | /* Global Styles */
8 |
9 | html,
10 | body {
11 | height: 100%;
12 | }
13 |
14 | .img-portfolio {
15 | margin-bottom: 30px;
16 | }
17 |
18 | .img-hover:hover {
19 | opacity: 0.8;
20 | }
21 |
22 | /* Home Page Carousel */
23 |
24 | header.carousel {
25 | height: 50%;
26 | }
27 |
28 | header.carousel .item,
29 | header.carousel .item.active,
30 | header.carousel .carousel-inner {
31 | height: 100%;
32 | }
33 |
34 | header.carousel .fill {
35 | width: 100%;
36 | height: 100%;
37 | background-position: center;
38 | background-size: cover;
39 | }
40 |
41 | /* 404 Page Styles */
42 |
43 | .error-404 {
44 | font-size: 100px;
45 | }
46 |
47 | /* Pricing Page Styles */
48 |
49 | .price {
50 | display: block;
51 | font-size: 50px;
52 | line-height: 50px;
53 | }
54 |
55 | .price sup {
56 | top: -20px;
57 | left: 2px;
58 | font-size: 20px;
59 | }
60 |
61 | .period {
62 | display: block;
63 | font-style: italic;
64 | }
65 |
66 | /* Footer Styles */
67 |
68 | footer {
69 | margin: 50px 0;
70 | }
71 |
72 | /* Responsive Styles */
73 |
74 | @media(max-width:991px) {
75 | .client-img,
76 | .img-related {
77 | margin-bottom: 30px;
78 | }
79 | }
80 |
81 | @media(max-width:767px) {
82 | .img-portfolio {
83 | margin-bottom: 15px;
84 | }
85 |
86 | header.carousel .carousel {
87 | height: 70%;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/docs/feed.xml:
--------------------------------------------------------------------------------
1 | ---
2 | search: exclude
3 | layout: none
4 | ---
5 |
6 |
7 |
8 |
9 | {{ site.title | xml_escape }}
10 | {{ site.description | xml_escape }}
11 | {{ site.url }}/
12 |
13 | {{ site.time | date_to_rfc822 }}
14 | {{ site.time | date_to_rfc822 }}
15 | Jekyll v{{ jekyll.version }}
16 | {% for post in site.posts limit:10 %}
17 | -
18 |
{{ post.title | xml_escape }}
19 | {{ post.content | xml_escape }}
20 | {{ post.date | date_to_rfc822 }}
21 | {{ post.url | prepend: site.url }}
22 | {{ post.url | prepend: site.url }}
23 | {% for tag in post.tags %}
24 | {{ tag | xml_escape }}
25 | {% endfor %}
26 | {% for tag in page.tags %}
27 | {{ cat | xml_escape }}
28 | {% endfor %}
29 |
30 | {% endfor %}
31 |
32 |
33 |
--------------------------------------------------------------------------------
/docs/fonts/FontAwesome.otf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/fonts/FontAwesome.otf
--------------------------------------------------------------------------------
/docs/fonts/fontawesome-webfont.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/fonts/fontawesome-webfont.eot
--------------------------------------------------------------------------------
/docs/fonts/fontawesome-webfont.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/fonts/fontawesome-webfont.ttf
--------------------------------------------------------------------------------
/docs/fonts/fontawesome-webfont.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/fonts/fontawesome-webfont.woff
--------------------------------------------------------------------------------
/docs/fonts/glyphicons-halflings-regular.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/fonts/glyphicons-halflings-regular.eot
--------------------------------------------------------------------------------
/docs/fonts/glyphicons-halflings-regular.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/fonts/glyphicons-halflings-regular.ttf
--------------------------------------------------------------------------------
/docs/fonts/glyphicons-halflings-regular.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/fonts/glyphicons-halflings-regular.woff
--------------------------------------------------------------------------------
/docs/fonts/glyphicons-halflings-regular.woff2:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/fonts/glyphicons-halflings-regular.woff2
--------------------------------------------------------------------------------
/docs/images/company_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/images/company_logo.png
--------------------------------------------------------------------------------
/docs/images/company_logo_big.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/images/company_logo_big.png
--------------------------------------------------------------------------------
/docs/images/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/images/favicon.ico
--------------------------------------------------------------------------------
/docs/images/half.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/images/half.png
--------------------------------------------------------------------------------
/docs/images/pixelshuffle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/images/pixelshuffle.png
--------------------------------------------------------------------------------
/docs/images/workflowarrow.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/docs/images/workflowarrow.png
--------------------------------------------------------------------------------
/docs/index.html:
--------------------------------------------------------------------------------
1 | ---
2 |
3 | title: Welcome to fastai v2
4 | keywords: fastai
5 | sidebar: home_sidebar
6 |
7 | summary: "NB: This is still in early development. Use v1 unless you want to contribute to the next version of fastai"
8 | ---
9 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | TODO: write this page!
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/docs/js/jquery.ba-throttle-debounce.min.js:
--------------------------------------------------------------------------------
1 | /*
2 | * jQuery throttle / debounce - v1.1 - 3/7/2010
3 | * http://benalman.com/projects/jquery-throttle-debounce-plugin/
4 | *
5 | * Copyright (c) 2010 "Cowboy" Ben Alman
6 | * Dual licensed under the MIT and GPL licenses.
7 | * http://benalman.com/about/license/
8 | */
9 | (function(b,c){var $=b.jQuery||b.Cowboy||(b.Cowboy={}),a;$.throttle=a=function(e,f,j,i){var h,d=0;if(typeof f!=="boolean"){i=j;j=f;f=c}function g(){var o=this,m=+new Date()-d,n=arguments;function l(){d=+new Date();j.apply(o,n)}function k(){h=c}if(i&&!h){l()}h&&clearTimeout(h);if(i===c&&m>e){l()}else{if(f!==true){h=setTimeout(i?k:l,i===c?e-m:e)}}}if($.guid){g.guid=j.guid=j.guid||$.guid++}return g};$.debounce=function(d,e,f){return f===c?a(d,e,false):a(d,f,e!==false)}})(this);
--------------------------------------------------------------------------------
/docs/licenses/LICENSE:
--------------------------------------------------------------------------------
1 | /* This license pertains to the docs template, except for the Navgoco jQuery component. */
2 |
3 | The MIT License (MIT)
4 |
5 | Copyright (c) 2016 Tom Johnson
6 |
7 | Permission is hereby granted, free of charge, to any person obtaining a copy
8 | of this software and associated documentation files (the "Software"), to deal
9 | in the Software without restriction, including without limitation the rights
10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | copies of the Software, and to permit persons to whom the Software is
12 | furnished to do so, subject to the following conditions:
13 |
14 | The above copyright notice and this permission notice shall be included in all
15 | copies or substantial portions of the Software.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 | SOFTWARE.
24 |
--------------------------------------------------------------------------------
/docs/licenses/LICENSE-BSD-NAVGOCO.txt:
--------------------------------------------------------------------------------
1 | /* This license pertains to the Navgoco jQuery component used for the sidebar. */
2 |
3 | Copyright (c) 2013, Christodoulos Tsoulloftas, http://www.komposta.net
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without modification,
7 | are permitted provided that the following conditions are met:
8 |
9 | * Redistributions of source code must retain the above copyright notice,
10 | this list of conditions and the following disclaimer.
11 | * Redistributions in binary form must reproduce the above copyright notice,
12 | this list of conditions and the following disclaimer in the documentation
13 | and/or other materials provided with the distribution.
14 | * Neither the name of the
nor the names of its
15 | contributors may be used to endorse or promote products derived from this
16 | software without specific prior written permission.
17 |
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 | OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
/docs/medical.text.html:
--------------------------------------------------------------------------------
1 | ---
2 |
3 | title: Medical Text
4 | keywords: fastai
5 | sidebar: home_sidebar
6 |
7 | summary: "Medical NLP data and models"
8 | ---
9 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
fastai.medical.text
is coming in late 2019 or early 2020!
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/docs/search.json:
--------------------------------------------------------------------------------
1 | ---
2 | title: search
3 | layout: none
4 | search: exclude
5 | ---
6 |
7 | [
8 | {% for page in site.pages %}
9 | {%- assign newurl = page.url | remove: "/" -%}
10 | {% unless forloop.first or forloop.index == 2 or newurl == 'assetscssstyle.css' or page.search == 'exclude' %},{% endunless %}
11 | {% if newurl == 'assetscssstyle.css' or page.search == 'exclude' %}
12 | {% continue %}
13 | {% else %}
14 | {
15 | "title": "{{ page.title }}",
16 | "tags": "{{ page.tags }}",
17 | "keywords": "{{page.keywords}}",
18 | "url": "{{ page.url | remove: "/"}}",
19 | "summary": "{{page.summary | strip }}"
20 | }
21 | {% endif %}
22 | {% endfor %}
23 | ]
24 |
--------------------------------------------------------------------------------
/docs/sitemap.xml:
--------------------------------------------------------------------------------
1 | ---
2 | layout: none
3 | search: exclude
4 | ---
5 |
6 |
7 |
8 | {% for post in site.posts %}
9 | {% unless post.search == "exclude" %}
10 |
11 | {{site.url}}{{post.url}}
12 |
13 | {% endunless %}
14 | {% endfor %}
15 |
16 |
17 | {% for page in site.pages %}
18 | {% unless page.search == "exclude" %}
19 |
20 | {{site.url}}{{ page.url}}
21 |
22 | {% endunless %}
23 | {% endfor %}
24 |
--------------------------------------------------------------------------------
/docs/tooltips.json:
--------------------------------------------------------------------------------
1 | ---
2 | layout: null
3 | search: exclude
4 | ---
5 |
6 | {
7 | "entries":
8 | [
9 | {% for page in site.tooltips %}
10 | {
11 | "doc_id": "{{ page.doc_id }}",
12 | "body": "{{ page.content | strip_newlines | replace: '\', '\\\\' | replace: '"', '\\"' }}"
13 | } {% unless forloop.last %},{% endunless %}
14 | {% endfor %}
15 | ]
16 | }
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/environment.yml:
--------------------------------------------------------------------------------
1 | name: fastai_dev
2 | channels:
3 | - fastai
4 | - pytorch
5 | - defaults
6 | dependencies:
7 | - jupyter
8 | - pytorch>=1.2.0
9 | - torchvision
10 | - matplotlib
11 | - pandas
12 | - requests
13 | - pyyaml
14 | - fastprogress>=0.1.22
15 | - pillow
16 | - python>=3.6
17 | - pip
18 | - scikit-learn
19 | - scipy
20 | - spacy
21 |
22 |
--------------------------------------------------------------------------------
/fastai2:
--------------------------------------------------------------------------------
1 | dev/fastai2/
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | from setuptools import setup,find_packages
4 |
5 | exec(open('dev/fastai2/version.py').read())
6 | with open('README.md') as readme_file: readme = readme_file.read()
7 |
8 | requirements = """
9 | torch>=1.2.0 torchvision matplotlib pandas requests pyyaml fastprogress>=0.1.22 pillow scikit-learn scipy spacy
10 | """.split()
11 |
12 | setup_requirements = ['setuptools>=36.2']
13 |
14 | setup(
15 | name = 'fastai2',
16 | version = __version__,
17 | packages = find_packages(),
18 | include_package_data = True,
19 |
20 | install_requires = requirements,
21 | setup_requires = setup_requirements,
22 | python_requires = '>=3.6',
23 |
24 | description = "fastai v2",
25 | long_description = readme,
26 | long_description_content_type = 'text/markdown',
27 | keywords = 'fastai, deep learning, machine learning',
28 | license = "Apache Software License 2.0",
29 | url = 'https://github.com/fastai/fastai_dev',
30 | author = "Jeremy Howard, Sylvain Gugger, and contributors",
31 | author_email = 'info@fast.ai',
32 |
33 | classifiers = [
34 | 'Development Status :: 2 - Pre-Alpha',
35 | 'Intended Audience :: Developers',
36 | 'License :: OSI Approved :: Apache Software License',
37 | 'Natural Language :: English',
38 | 'Programming Language :: Python :: 3.6',
39 | 'Programming Language :: Python :: 3.7',
40 | ],
41 |
42 | zip_safe = False,
43 | )
44 |
--------------------------------------------------------------------------------
/swift/.gitignore:
--------------------------------------------------------------------------------
1 | *gz
2 | swift-install/
3 |
4 | .build/
5 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_00_load_data/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_00_load_data",
6 | products: [
7 | .library(name: "FastaiNotebook_00_load_data", targets: ["FastaiNotebook_00_load_data"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(url: "https://github.com/mxcl/Path.swift", from: "0.16.1"),
12 | .package(url: "https://github.com/saeta/Just", from: "0.7.2"),
13 | .package(url: "https://github.com/latenitesoft/NotebookExport", from: "0.5.0")
14 | ],
15 | targets: [
16 | .target(name: "FastaiNotebook_00_load_data", dependencies: ["Path", "Just", "NotebookExport"]),
17 |
18 | ]
19 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_01_matmul/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_01_matmul",
6 | products: [
7 | .library(name: "FastaiNotebook_01_matmul", targets: ["FastaiNotebook_01_matmul"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_00_load_data")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_01_matmul", dependencies: ["FastaiNotebook_00_load_data"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_01_matmul/Sources/FastaiNotebook_01_matmul/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_01a_fastai_layers/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "35dbe6823bf5f9b9bb996c1fe6c9ad9a4c59d821",
10 | "version": "0.7.3"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "dac007e907a4f4c565cfdc55a9ce148a761a11d5",
28 | "version": "0.16.3"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_01a_fastai_layers/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_01a_fastai_layers",
6 | products: [
7 | .library(name: "FastaiNotebook_01a_fastai_layers", targets: ["FastaiNotebook_01a_fastai_layers"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_01_matmul")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_01a_fastai_layers", dependencies: ["FastaiNotebook_01_matmul"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_01a_fastai_layers/Sources/FastaiNotebook_01a_fastai_layers/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_02_fully_connected/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "35dbe6823bf5f9b9bb996c1fe6c9ad9a4c59d821",
10 | "version": "0.7.3"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "dac007e907a4f4c565cfdc55a9ce148a761a11d5",
28 | "version": "0.16.3"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_02_fully_connected/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_02_fully_connected",
6 | products: [
7 | .library(name: "FastaiNotebook_02_fully_connected", targets: ["FastaiNotebook_02_fully_connected"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_01a_fastai_layers")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_02_fully_connected", dependencies: ["FastaiNotebook_01a_fastai_layers"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_02_fully_connected/Sources/FastaiNotebook_02_fully_connected/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_02_fully_connected/Sources/FastaiNotebook_02_fully_connected/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_02a_why_sqrt5/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "35dbe6823bf5f9b9bb996c1fe6c9ad9a4c59d821",
10 | "version": "0.7.3"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "dac007e907a4f4c565cfdc55a9ce148a761a11d5",
28 | "version": "0.16.3"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_02a_why_sqrt5/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_02a_why_sqrt5",
6 | products: [
7 | .library(name: "FastaiNotebook_02a_why_sqrt5", targets: ["FastaiNotebook_02a_why_sqrt5"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_02_fully_connected")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_02a_why_sqrt5", dependencies: ["FastaiNotebook_02_fully_connected"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_02a_why_sqrt5/Sources/FastaiNotebook_02a_why_sqrt5/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_02a_why_sqrt5/Sources/FastaiNotebook_02a_why_sqrt5/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_02a_why_sqrt5/Sources/FastaiNotebook_02a_why_sqrt5/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0...
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_03_minibatch_training/Sources/FastaiNotebook_03_minibatch_training/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_03_minibatch_training/Sources/FastaiNotebook_03_minibatch_training/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0...
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_04_callbacks/Sources/FastaiNotebook_04_callbacks/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_04_callbacks/Sources/FastaiNotebook_04_callbacks/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0..
13 |
14 | public func accuracy(_ output: TF, _ target: TI) -> TF{
15 | let corrects = TF(output.argmax(squeezingAxis: 1) .== target)
16 | return corrects.mean()
17 | }
18 |
19 | public func batchedRanges(start:Int, end:Int, bs:Int) -> UnfoldSequence,Int>
20 | {
21 | return sequence(state: start) { (batchStart) -> Range? in
22 | let remaining = end - batchStart
23 | guard remaining > 0 else { return nil}
24 | let currentBs = min(bs,remaining)
25 | let batchEnd = batchStart.advanced(by: currentBs)
26 | defer { batchStart = batchEnd }
27 | return batchStart ..< batchEnd
28 | }
29 | }
30 |
31 | public struct DataBatch: TensorGroup {
32 | public var xb: Inputs
33 | public var yb: Labels
34 |
35 | public init(xb: Inputs, yb: Labels){ (self.xb,self.yb) = (xb,yb) }
36 |
37 | public var _tensorHandles: [_AnyTensorHandle] {
38 | xb._tensorHandles + yb._tensorHandles
39 | }
40 |
41 | public init(_handles: C) where C.Element: _AnyTensorHandle {
42 | let xStart = _handles.startIndex
43 | let xEnd = _handles.index(
44 | xStart, offsetBy: Int(Inputs._tensorHandleCount))
45 | self.xb = Inputs.init(_handles: _handles[xStart.. TF {
52 | return softmaxCrossEntropy(logits: logits, labels: labels)
53 | }
54 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_05_anneal/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "35dbe6823bf5f9b9bb996c1fe6c9ad9a4c59d821",
10 | "version": "0.7.3"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "dac007e907a4f4c565cfdc55a9ce148a761a11d5",
28 | "version": "0.16.3"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_05_anneal/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_05_anneal",
6 | products: [
7 | .library(name: "FastaiNotebook_05_anneal", targets: ["FastaiNotebook_05_anneal"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_04_callbacks")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_05_anneal", dependencies: ["FastaiNotebook_04_callbacks"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_05_anneal/Sources/FastaiNotebook_05_anneal/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_05_anneal/Sources/FastaiNotebook_05_anneal/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_05_anneal/Sources/FastaiNotebook_05_anneal/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0..
13 |
14 | public func accuracy(_ output: TF, _ target: TI) -> TF{
15 | let corrects = TF(output.argmax(squeezingAxis: 1) .== target)
16 | return corrects.mean()
17 | }
18 |
19 | public func batchedRanges(start:Int, end:Int, bs:Int) -> UnfoldSequence,Int>
20 | {
21 | return sequence(state: start) { (batchStart) -> Range? in
22 | let remaining = end - batchStart
23 | guard remaining > 0 else { return nil}
24 | let currentBs = min(bs,remaining)
25 | let batchEnd = batchStart.advanced(by: currentBs)
26 | defer { batchStart = batchEnd }
27 | return batchStart ..< batchEnd
28 | }
29 | }
30 |
31 | public struct DataBatch: TensorGroup {
32 | public var xb: Inputs
33 | public var yb: Labels
34 |
35 | public init(xb: Inputs, yb: Labels){ (self.xb,self.yb) = (xb,yb) }
36 |
37 | public var _tensorHandles: [_AnyTensorHandle] {
38 | xb._tensorHandles + yb._tensorHandles
39 | }
40 |
41 | public init(_handles: C) where C.Element: _AnyTensorHandle {
42 | let xStart = _handles.startIndex
43 | let xEnd = _handles.index(
44 | xStart, offsetBy: Int(Inputs._tensorHandleCount))
45 | self.xb = Inputs.init(_handles: _handles[xStart.. TF {
52 | return softmaxCrossEntropy(logits: logits, labels: labels)
53 | }
54 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_05b_early_stopping/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "35dbe6823bf5f9b9bb996c1fe6c9ad9a4c59d821",
10 | "version": "0.7.3"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "dac007e907a4f4c565cfdc55a9ce148a761a11d5",
28 | "version": "0.16.3"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_05b_early_stopping/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_05b_early_stopping",
6 | products: [
7 | .library(name: "FastaiNotebook_05b_early_stopping", targets: ["FastaiNotebook_05b_early_stopping"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_05_anneal")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_05b_early_stopping", dependencies: ["FastaiNotebook_05_anneal"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_05b_early_stopping/Sources/FastaiNotebook_05b_early_stopping/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_05b_early_stopping/Sources/FastaiNotebook_05b_early_stopping/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_05b_early_stopping/Sources/FastaiNotebook_05b_early_stopping/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0..
13 |
14 | public func accuracy(_ output: TF, _ target: TI) -> TF{
15 | let corrects = TF(output.argmax(squeezingAxis: 1) .== target)
16 | return corrects.mean()
17 | }
18 |
19 | public func batchedRanges(start:Int, end:Int, bs:Int) -> UnfoldSequence,Int>
20 | {
21 | return sequence(state: start) { (batchStart) -> Range? in
22 | let remaining = end - batchStart
23 | guard remaining > 0 else { return nil}
24 | let currentBs = min(bs,remaining)
25 | let batchEnd = batchStart.advanced(by: currentBs)
26 | defer { batchStart = batchEnd }
27 | return batchStart ..< batchEnd
28 | }
29 | }
30 |
31 | public struct DataBatch: TensorGroup {
32 | public var xb: Inputs
33 | public var yb: Labels
34 |
35 | public init(xb: Inputs, yb: Labels){ (self.xb,self.yb) = (xb,yb) }
36 |
37 | public var _tensorHandles: [_AnyTensorHandle] {
38 | xb._tensorHandles + yb._tensorHandles
39 | }
40 |
41 | public init(_handles: C) where C.Element: _AnyTensorHandle {
42 | let xStart = _handles.startIndex
43 | let xEnd = _handles.index(
44 | xStart, offsetBy: Int(Inputs._tensorHandleCount))
45 | self.xb = Inputs.init(_handles: _handles[xStart.. TF {
52 | return softmaxCrossEntropy(logits: logits, labels: labels)
53 | }
54 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_06_cuda/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "35dbe6823bf5f9b9bb996c1fe6c9ad9a4c59d821",
10 | "version": "0.7.3"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "dac007e907a4f4c565cfdc55a9ce148a761a11d5",
28 | "version": "0.16.3"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_06_cuda/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_06_cuda",
6 | products: [
7 | .library(name: "FastaiNotebook_06_cuda", targets: ["FastaiNotebook_06_cuda"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_05b_early_stopping")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_06_cuda", dependencies: ["FastaiNotebook_05b_early_stopping"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_06_cuda/Sources/FastaiNotebook_06_cuda/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_06_cuda/Sources/FastaiNotebook_06_cuda/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_06_cuda/Sources/FastaiNotebook_06_cuda/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0..
13 |
14 | public func accuracy(_ output: TF, _ target: TI) -> TF{
15 | let corrects = TF(output.argmax(squeezingAxis: 1) .== target)
16 | return corrects.mean()
17 | }
18 |
19 | public func batchedRanges(start:Int, end:Int, bs:Int) -> UnfoldSequence,Int>
20 | {
21 | return sequence(state: start) { (batchStart) -> Range? in
22 | let remaining = end - batchStart
23 | guard remaining > 0 else { return nil}
24 | let currentBs = min(bs,remaining)
25 | let batchEnd = batchStart.advanced(by: currentBs)
26 | defer { batchStart = batchEnd }
27 | return batchStart ..< batchEnd
28 | }
29 | }
30 |
31 | public struct DataBatch: TensorGroup {
32 | public var xb: Inputs
33 | public var yb: Labels
34 |
35 | public init(xb: Inputs, yb: Labels){ (self.xb,self.yb) = (xb,yb) }
36 |
37 | public var _tensorHandles: [_AnyTensorHandle] {
38 | xb._tensorHandles + yb._tensorHandles
39 | }
40 |
41 | public init(_handles: C) where C.Element: _AnyTensorHandle {
42 | let xStart = _handles.startIndex
43 | let xEnd = _handles.index(
44 | xStart, offsetBy: Int(Inputs._tensorHandleCount))
45 | self.xb = Inputs.init(_handles: _handles[xStart.. TF {
52 | return softmaxCrossEntropy(logits: logits, labels: labels)
53 | }
54 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_06_cuda/Sources/FastaiNotebook_06_cuda/06_cuda.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 06_cuda.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 | #if canImport(PythonKit)
12 | import PythonKit
13 | #else
14 | import Python
15 | #endif
16 |
17 | extension Learner {
18 | public class AddChannel: Delegate {
19 | public override func batchWillStart(learner: Learner) {
20 | learner.currentInput = learner.currentInput!.expandingShape(at: -1)
21 | }
22 | }
23 |
24 | public func makeAddChannel() -> AddChannel { return AddChannel() }
25 | }
26 |
27 | public struct CnnModel: Layer {
28 | public var convs: [FAConv2D]
29 | public var pool = FAGlobalAvgPool2D()
30 | public var linear: FADense
31 |
32 | public init(channelIn: Int, nOut: Int, filters: [Int]){
33 | let allFilters = [channelIn] + filters
34 | convs = Array(0..(filters.last!, nOut)
38 | }
39 |
40 | @differentiable
41 | public func callAsFunction(_ input: TF) -> TF {
42 | //TODO: uncomment when https://github.com/tensorflow/swift-apis/issues/411 is solved
43 | //and array of Layer conform to Layer again
44 | //return linear(pool(convs(input)))
45 | let y = convs.differentiableReduce(input) { $1($0) }
46 | return linear(pool(y))
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_07_batchnorm/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "35dbe6823bf5f9b9bb996c1fe6c9ad9a4c59d821",
10 | "version": "0.7.3"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "dac007e907a4f4c565cfdc55a9ce148a761a11d5",
28 | "version": "0.16.3"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_07_batchnorm/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_07_batchnorm",
6 | products: [
7 | .library(name: "FastaiNotebook_07_batchnorm", targets: ["FastaiNotebook_07_batchnorm"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_06_cuda")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_07_batchnorm", dependencies: ["FastaiNotebook_06_cuda"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_07_batchnorm/Sources/FastaiNotebook_07_batchnorm/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_07_batchnorm/Sources/FastaiNotebook_07_batchnorm/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_07_batchnorm/Sources/FastaiNotebook_07_batchnorm/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0..
13 |
14 | public func accuracy(_ output: TF, _ target: TI) -> TF{
15 | let corrects = TF(output.argmax(squeezingAxis: 1) .== target)
16 | return corrects.mean()
17 | }
18 |
19 | public func batchedRanges(start:Int, end:Int, bs:Int) -> UnfoldSequence,Int>
20 | {
21 | return sequence(state: start) { (batchStart) -> Range? in
22 | let remaining = end - batchStart
23 | guard remaining > 0 else { return nil}
24 | let currentBs = min(bs,remaining)
25 | let batchEnd = batchStart.advanced(by: currentBs)
26 | defer { batchStart = batchEnd }
27 | return batchStart ..< batchEnd
28 | }
29 | }
30 |
31 | public struct DataBatch: TensorGroup {
32 | public var xb: Inputs
33 | public var yb: Labels
34 |
35 | public init(xb: Inputs, yb: Labels){ (self.xb,self.yb) = (xb,yb) }
36 |
37 | public var _tensorHandles: [_AnyTensorHandle] {
38 | xb._tensorHandles + yb._tensorHandles
39 | }
40 |
41 | public init(_handles: C) where C.Element: _AnyTensorHandle {
42 | let xStart = _handles.startIndex
43 | let xEnd = _handles.index(
44 | xStart, offsetBy: Int(Inputs._tensorHandleCount))
45 | self.xb = Inputs.init(_handles: _handles[xStart.. TF {
52 | return softmaxCrossEntropy(logits: logits, labels: labels)
53 | }
54 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_07_batchnorm/Sources/FastaiNotebook_07_batchnorm/06_cuda.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 06_cuda.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 | #if canImport(PythonKit)
12 | import PythonKit
13 | #else
14 | import Python
15 | #endif
16 |
17 | extension Learner {
18 | public class AddChannel: Delegate {
19 | public override func batchWillStart(learner: Learner) {
20 | learner.currentInput = learner.currentInput!.expandingShape(at: -1)
21 | }
22 | }
23 |
24 | public func makeAddChannel() -> AddChannel { return AddChannel() }
25 | }
26 |
27 | public struct CnnModel: Layer {
28 | public var convs: [FAConv2D]
29 | public var pool = FAGlobalAvgPool2D()
30 | public var linear: FADense
31 |
32 | public init(channelIn: Int, nOut: Int, filters: [Int]){
33 | let allFilters = [channelIn] + filters
34 | convs = Array(0..(filters.last!, nOut)
38 | }
39 |
40 | @differentiable
41 | public func callAsFunction(_ input: TF) -> TF {
42 | //TODO: uncomment when https://github.com/tensorflow/swift-apis/issues/411 is solved
43 | //and array of Layer conform to Layer again
44 | //return linear(pool(convs(input)))
45 | let y = convs.differentiableReduce(input) { $1($0) }
46 | return linear(pool(y))
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08_data_block/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "35dbe6823bf5f9b9bb996c1fe6c9ad9a4c59d821",
10 | "version": "0.7.3"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "dac007e907a4f4c565cfdc55a9ce148a761a11d5",
28 | "version": "0.16.3"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08_data_block/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_08_data_block",
6 | products: [
7 | .library(name: "FastaiNotebook_08_data_block", targets: ["FastaiNotebook_08_data_block"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_07_batchnorm")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_08_data_block", dependencies: ["FastaiNotebook_07_batchnorm"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08_data_block/Sources/FastaiNotebook_08_data_block/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08_data_block/Sources/FastaiNotebook_08_data_block/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08_data_block/Sources/FastaiNotebook_08_data_block/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0..
13 |
14 | public func accuracy(_ output: TF, _ target: TI) -> TF{
15 | let corrects = TF(output.argmax(squeezingAxis: 1) .== target)
16 | return corrects.mean()
17 | }
18 |
19 | public func batchedRanges(start:Int, end:Int, bs:Int) -> UnfoldSequence,Int>
20 | {
21 | return sequence(state: start) { (batchStart) -> Range? in
22 | let remaining = end - batchStart
23 | guard remaining > 0 else { return nil}
24 | let currentBs = min(bs,remaining)
25 | let batchEnd = batchStart.advanced(by: currentBs)
26 | defer { batchStart = batchEnd }
27 | return batchStart ..< batchEnd
28 | }
29 | }
30 |
31 | public struct DataBatch: TensorGroup {
32 | public var xb: Inputs
33 | public var yb: Labels
34 |
35 | public init(xb: Inputs, yb: Labels){ (self.xb,self.yb) = (xb,yb) }
36 |
37 | public var _tensorHandles: [_AnyTensorHandle] {
38 | xb._tensorHandles + yb._tensorHandles
39 | }
40 |
41 | public init(_handles: C) where C.Element: _AnyTensorHandle {
42 | let xStart = _handles.startIndex
43 | let xEnd = _handles.index(
44 | xStart, offsetBy: Int(Inputs._tensorHandleCount))
45 | self.xb = Inputs.init(_handles: _handles[xStart.. TF {
52 | return softmaxCrossEntropy(logits: logits, labels: labels)
53 | }
54 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08_data_block/Sources/FastaiNotebook_08_data_block/06_cuda.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 06_cuda.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 | #if canImport(PythonKit)
12 | import PythonKit
13 | #else
14 | import Python
15 | #endif
16 |
17 | extension Learner {
18 | public class AddChannel: Delegate {
19 | public override func batchWillStart(learner: Learner) {
20 | learner.currentInput = learner.currentInput!.expandingShape(at: -1)
21 | }
22 | }
23 |
24 | public func makeAddChannel() -> AddChannel { return AddChannel() }
25 | }
26 |
27 | public struct CnnModel: Layer {
28 | public var convs: [FAConv2D]
29 | public var pool = FAGlobalAvgPool2D()
30 | public var linear: FADense
31 |
32 | public init(channelIn: Int, nOut: Int, filters: [Int]){
33 | let allFilters = [channelIn] + filters
34 | convs = Array(0..(filters.last!, nOut)
38 | }
39 |
40 | @differentiable
41 | public func callAsFunction(_ input: TF) -> TF {
42 | //TODO: uncomment when https://github.com/tensorflow/swift-apis/issues/411 is solved
43 | //and array of Layer conform to Layer again
44 | //return linear(pool(convs(input)))
45 | let y = convs.differentiableReduce(input) { $1($0) }
46 | return linear(pool(y))
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08a_heterogeneous_dictionary/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "35dbe6823bf5f9b9bb996c1fe6c9ad9a4c59d821",
10 | "version": "0.7.3"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "dac007e907a4f4c565cfdc55a9ce148a761a11d5",
28 | "version": "0.16.3"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08a_heterogeneous_dictionary/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_08a_heterogeneous_dictionary",
6 | products: [
7 | .library(name: "FastaiNotebook_08a_heterogeneous_dictionary", targets: ["FastaiNotebook_08a_heterogeneous_dictionary"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_08_data_block")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_08a_heterogeneous_dictionary", dependencies: ["FastaiNotebook_08_data_block"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08a_heterogeneous_dictionary/Sources/FastaiNotebook_08a_heterogeneous_dictionary/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08a_heterogeneous_dictionary/Sources/FastaiNotebook_08a_heterogeneous_dictionary/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08a_heterogeneous_dictionary/Sources/FastaiNotebook_08a_heterogeneous_dictionary/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0.. AddChannel { return AddChannel() }
25 | }
26 |
27 | public struct CnnModel: Layer {
28 | public var convs: [FAConv2D]
29 | public var pool = FAGlobalAvgPool2D()
30 | public var linear: FADense
31 |
32 | public init(channelIn: Int, nOut: Int, filters: [Int]){
33 | let allFilters = [channelIn] + filters
34 | convs = Array(0..(filters.last!, nOut)
38 | }
39 |
40 | @differentiable
41 | public func callAsFunction(_ input: TF) -> TF {
42 | //TODO: uncomment when https://github.com/tensorflow/swift-apis/issues/411 is solved
43 | //and array of Layer conform to Layer again
44 | //return linear(pool(convs(input)))
45 | let y = convs.differentiableReduce(input) { $1($0) }
46 | return linear(pool(y))
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08a_heterogeneous_dictionary/Sources/FastaiNotebook_08a_heterogeneous_dictionary/08a_heterogeneous_dictionary.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 08a_heterogeneous_dictionary.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 |
11 | public protocol HetDictKey {
12 | associatedtype ValueType
13 | static var defaultValue: ValueType { get }
14 | }
15 |
16 |
17 | public struct HeterogeneousDictionary {
18 | private var underlying: [ObjectIdentifier : Any] = [:]
19 |
20 | public init() {}
21 | public init(_ key: T.Type, _ value: T.ValueType) {
22 | self.underlying = [ObjectIdentifier(key): value]
23 | }
24 | public init(_ key1: T1.Type, _ value1: T1.ValueType, _ key2: T2.Type, _ value2: T2.ValueType) {
25 | self.underlying = [ObjectIdentifier(key1): value1, ObjectIdentifier(key2): value2]
26 | }
27 |
28 | public subscript(key: T.Type) -> T.ValueType {
29 | get { return underlying[ObjectIdentifier(key), default: T.defaultValue] as! T.ValueType }
30 | set { underlying[ObjectIdentifier(key)] = newValue as Any }
31 | }
32 |
33 | public mutating func merge(_ other: HeterogeneousDictionary,
34 | uniquingKeysWith combine: (Any, Any) throws -> Any) rethrows {
35 | try self.underlying.merge(other.underlying, uniquingKeysWith: combine)
36 | }
37 | }
38 |
39 |
40 | // Common keys
41 | public struct LearningRate: HetDictKey {
42 | public static var defaultValue: Float = 0.4
43 | }
44 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08c_data_block_generic/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_08c_data_block_generic",
6 | products: [
7 | .library(name: "FastaiNotebook_08c_data_block_generic", targets: ["FastaiNotebook_08c_data_block_generic"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_07_batchnorm"),
12 | .package(path: "../SwiftCV")
13 | ],
14 | targets: [
15 | .target(name: "FastaiNotebook_08c_data_block_generic", dependencies: ["FastaiNotebook_07_batchnorm", "SwiftCV"]),
16 |
17 | ]
18 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08c_data_block_generic/Sources/FastaiNotebook_08c_data_block_generic/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08c_data_block_generic/Sources/FastaiNotebook_08c_data_block_generic/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_08c_data_block_generic/Sources/FastaiNotebook_08c_data_block_generic/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0.. AddChannel { return AddChannel() }
25 | }
26 |
27 | public struct CnnModel: Layer {
28 | public var convs: [FAConv2D]
29 | public var pool = FAGlobalAvgPool2D()
30 | public var linear: FADense
31 |
32 | public init(channelIn: Int, nOut: Int, filters: [Int]){
33 | let allFilters = [channelIn] + filters
34 | convs = Array(0..(filters.last!, nOut)
38 | }
39 |
40 | @differentiable
41 | public func callAsFunction(_ input: TF) -> TF {
42 | //TODO: uncomment when https://github.com/tensorflow/swift-apis/issues/411 is solved
43 | //and array of Layer conform to Layer again
44 | //return linear(pool(convs(input)))
45 | let y = convs.differentiableReduce(input) { $1($0) }
46 | return linear(pool(y))
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_09_optimizer/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_09_optimizer",
6 | products: [
7 | .library(name: "FastaiNotebook_09_optimizer", targets: ["FastaiNotebook_09_optimizer"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_08a_heterogeneous_dictionary")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_09_optimizer", dependencies: ["FastaiNotebook_08a_heterogeneous_dictionary"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_09_optimizer/Sources/FastaiNotebook_09_optimizer/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_09_optimizer/Sources/FastaiNotebook_09_optimizer/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_09_optimizer/Sources/FastaiNotebook_09_optimizer/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0..
13 |
14 | public func accuracy(_ output: TF, _ target: TI) -> TF{
15 | let corrects = TF(output.argmax(squeezingAxis: 1) .== target)
16 | return corrects.mean()
17 | }
18 |
19 | public func batchedRanges(start:Int, end:Int, bs:Int) -> UnfoldSequence,Int>
20 | {
21 | return sequence(state: start) { (batchStart) -> Range? in
22 | let remaining = end - batchStart
23 | guard remaining > 0 else { return nil}
24 | let currentBs = min(bs,remaining)
25 | let batchEnd = batchStart.advanced(by: currentBs)
26 | defer { batchStart = batchEnd }
27 | return batchStart ..< batchEnd
28 | }
29 | }
30 |
31 | public struct DataBatch: TensorGroup {
32 | public var xb: Inputs
33 | public var yb: Labels
34 |
35 | public init(xb: Inputs, yb: Labels){ (self.xb,self.yb) = (xb,yb) }
36 |
37 | public var _tensorHandles: [_AnyTensorHandle] {
38 | xb._tensorHandles + yb._tensorHandles
39 | }
40 |
41 | public init(_handles: C) where C.Element: _AnyTensorHandle {
42 | let xStart = _handles.startIndex
43 | let xEnd = _handles.index(
44 | xStart, offsetBy: Int(Inputs._tensorHandleCount))
45 | self.xb = Inputs.init(_handles: _handles[xStart.. TF {
52 | return softmaxCrossEntropy(logits: logits, labels: labels)
53 | }
54 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_09_optimizer/Sources/FastaiNotebook_09_optimizer/06_cuda.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 06_cuda.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 | #if canImport(PythonKit)
12 | import PythonKit
13 | #else
14 | import Python
15 | #endif
16 |
17 | extension Learner {
18 | public class AddChannel: Delegate {
19 | public override func batchWillStart(learner: Learner) {
20 | learner.currentInput = learner.currentInput!.expandingShape(at: -1)
21 | }
22 | }
23 |
24 | public func makeAddChannel() -> AddChannel { return AddChannel() }
25 | }
26 |
27 | public struct CnnModel: Layer {
28 | public var convs: [FAConv2D]
29 | public var pool = FAGlobalAvgPool2D()
30 | public var linear: FADense
31 |
32 | public init(channelIn: Int, nOut: Int, filters: [Int]){
33 | let allFilters = [channelIn] + filters
34 | convs = Array(0..(filters.last!, nOut)
38 | }
39 |
40 | @differentiable
41 | public func callAsFunction(_ input: TF) -> TF {
42 | //TODO: uncomment when https://github.com/tensorflow/swift-apis/issues/411 is solved
43 | //and array of Layer conform to Layer again
44 | //return linear(pool(convs(input)))
45 | let y = convs.differentiableReduce(input) { $1($0) }
46 | return linear(pool(y))
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_09_optimizer/Sources/FastaiNotebook_09_optimizer/08a_heterogeneous_dictionary.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 08a_heterogeneous_dictionary.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 |
11 | public protocol HetDictKey {
12 | associatedtype ValueType
13 | static var defaultValue: ValueType { get }
14 | }
15 |
16 |
17 | public struct HeterogeneousDictionary {
18 | private var underlying: [ObjectIdentifier : Any] = [:]
19 |
20 | public init() {}
21 | public init(_ key: T.Type, _ value: T.ValueType) {
22 | self.underlying = [ObjectIdentifier(key): value]
23 | }
24 | public init(_ key1: T1.Type, _ value1: T1.ValueType, _ key2: T2.Type, _ value2: T2.ValueType) {
25 | self.underlying = [ObjectIdentifier(key1): value1, ObjectIdentifier(key2): value2]
26 | }
27 |
28 | public subscript(key: T.Type) -> T.ValueType {
29 | get { return underlying[ObjectIdentifier(key), default: T.defaultValue] as! T.ValueType }
30 | set { underlying[ObjectIdentifier(key)] = newValue as Any }
31 | }
32 |
33 | public mutating func merge(_ other: HeterogeneousDictionary,
34 | uniquingKeysWith combine: (Any, Any) throws -> Any) rethrows {
35 | try self.underlying.merge(other.underlying, uniquingKeysWith: combine)
36 | }
37 | }
38 |
39 |
40 | // Common keys
41 | public struct LearningRate: HetDictKey {
42 | public static var defaultValue: Float = 0.4
43 | }
44 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_10_mixup_ls/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_10_mixup_ls",
6 | products: [
7 | .library(name: "FastaiNotebook_10_mixup_ls", targets: ["FastaiNotebook_10_mixup_ls"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_09_optimizer")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_10_mixup_ls", dependencies: ["FastaiNotebook_09_optimizer"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_10_mixup_ls/Sources/FastaiNotebook_10_mixup_ls/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_10_mixup_ls/Sources/FastaiNotebook_10_mixup_ls/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_10_mixup_ls/Sources/FastaiNotebook_10_mixup_ls/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0..
13 |
14 | public func accuracy(_ output: TF, _ target: TI) -> TF{
15 | let corrects = TF(output.argmax(squeezingAxis: 1) .== target)
16 | return corrects.mean()
17 | }
18 |
19 | public func batchedRanges(start:Int, end:Int, bs:Int) -> UnfoldSequence,Int>
20 | {
21 | return sequence(state: start) { (batchStart) -> Range? in
22 | let remaining = end - batchStart
23 | guard remaining > 0 else { return nil}
24 | let currentBs = min(bs,remaining)
25 | let batchEnd = batchStart.advanced(by: currentBs)
26 | defer { batchStart = batchEnd }
27 | return batchStart ..< batchEnd
28 | }
29 | }
30 |
31 | public struct DataBatch: TensorGroup {
32 | public var xb: Inputs
33 | public var yb: Labels
34 |
35 | public init(xb: Inputs, yb: Labels){ (self.xb,self.yb) = (xb,yb) }
36 |
37 | public var _tensorHandles: [_AnyTensorHandle] {
38 | xb._tensorHandles + yb._tensorHandles
39 | }
40 |
41 | public init(_handles: C) where C.Element: _AnyTensorHandle {
42 | let xStart = _handles.startIndex
43 | let xEnd = _handles.index(
44 | xStart, offsetBy: Int(Inputs._tensorHandleCount))
45 | self.xb = Inputs.init(_handles: _handles[xStart.. TF {
52 | return softmaxCrossEntropy(logits: logits, labels: labels)
53 | }
54 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_10_mixup_ls/Sources/FastaiNotebook_10_mixup_ls/06_cuda.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 06_cuda.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 | #if canImport(PythonKit)
12 | import PythonKit
13 | #else
14 | import Python
15 | #endif
16 |
17 | extension Learner {
18 | public class AddChannel: Delegate {
19 | public override func batchWillStart(learner: Learner) {
20 | learner.currentInput = learner.currentInput!.expandingShape(at: -1)
21 | }
22 | }
23 |
24 | public func makeAddChannel() -> AddChannel { return AddChannel() }
25 | }
26 |
27 | public struct CnnModel: Layer {
28 | public var convs: [FAConv2D]
29 | public var pool = FAGlobalAvgPool2D()
30 | public var linear: FADense
31 |
32 | public init(channelIn: Int, nOut: Int, filters: [Int]){
33 | let allFilters = [channelIn] + filters
34 | convs = Array(0..(filters.last!, nOut)
38 | }
39 |
40 | @differentiable
41 | public func callAsFunction(_ input: TF) -> TF {
42 | //TODO: uncomment when https://github.com/tensorflow/swift-apis/issues/411 is solved
43 | //and array of Layer conform to Layer again
44 | //return linear(pool(convs(input)))
45 | let y = convs.differentiableReduce(input) { $1($0) }
46 | return linear(pool(y))
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_10_mixup_ls/Sources/FastaiNotebook_10_mixup_ls/08a_heterogeneous_dictionary.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 08a_heterogeneous_dictionary.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 |
11 | public protocol HetDictKey {
12 | associatedtype ValueType
13 | static var defaultValue: ValueType { get }
14 | }
15 |
16 |
17 | public struct HeterogeneousDictionary {
18 | private var underlying: [ObjectIdentifier : Any] = [:]
19 |
20 | public init() {}
21 | public init(_ key: T.Type, _ value: T.ValueType) {
22 | self.underlying = [ObjectIdentifier(key): value]
23 | }
24 | public init(_ key1: T1.Type, _ value1: T1.ValueType, _ key2: T2.Type, _ value2: T2.ValueType) {
25 | self.underlying = [ObjectIdentifier(key1): value1, ObjectIdentifier(key2): value2]
26 | }
27 |
28 | public subscript(key: T.Type) -> T.ValueType {
29 | get { return underlying[ObjectIdentifier(key), default: T.defaultValue] as! T.ValueType }
30 | set { underlying[ObjectIdentifier(key)] = newValue as Any }
31 | }
32 |
33 | public mutating func merge(_ other: HeterogeneousDictionary,
34 | uniquingKeysWith combine: (Any, Any) throws -> Any) rethrows {
35 | try self.underlying.merge(other.underlying, uniquingKeysWith: combine)
36 | }
37 | }
38 |
39 |
40 | // Common keys
41 | public struct LearningRate: HetDictKey {
42 | public static var defaultValue: Float = 0.4
43 | }
44 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_11_imagenette/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 | import PackageDescription
3 |
4 | let package = Package(
5 | name: "FastaiNotebook_11_imagenette",
6 | products: [
7 | .library(name: "FastaiNotebook_11_imagenette", targets: ["FastaiNotebook_11_imagenette"]),
8 |
9 | ],
10 | dependencies: [
11 | .package(path: "../FastaiNotebook_10_mixup_ls")
12 | ],
13 | targets: [
14 | .target(name: "FastaiNotebook_11_imagenette", dependencies: ["FastaiNotebook_10_mixup_ls"]),
15 |
16 | ]
17 | )
--------------------------------------------------------------------------------
/swift/FastaiNotebook_11_imagenette/Sources/FastaiNotebook_11_imagenette/01_matmul.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 01_matmul.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public extension StringTensor {
13 | // Read a file into a Tensor.
14 | init(readFile filename: String) {
15 | self.init(readFile: StringTensor(filename))
16 | }
17 | init(readFile filename: StringTensor) {
18 | self = _Raw.readFile(filename: filename)
19 | }
20 |
21 | // Decode a StringTensor holding a JPEG file into a Tensor.
22 | func decodeJpeg(channels: Int = 0) -> Tensor {
23 | return _Raw.decodeJpeg(contents: self, channels: Int64(channels), dctMethod: "")
24 | }
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_11_imagenette/Sources/FastaiNotebook_11_imagenette/02_fully_connected.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02_fully_connected.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 |
12 | public typealias TF=Tensor
13 |
14 | public func normalize(_ x:TF, mean:TF, std:TF) -> TF {
15 | return (x-mean)/std
16 | }
17 |
18 | public func testNearZero(_ a: TF, tolerance: Float = 1e-3) {
19 | assert((abs(a) .< tolerance).all(), "Near zero: \(a)")
20 | }
21 |
22 | public func testSame(_ a: TF, _ b: TF) {
23 | // Check shapes match so broadcasting doesn't hide shape errors.
24 | assert(a.shape == b.shape)
25 | testNearZero(a-b)
26 | }
27 |
28 | public func mse(_ out: TF, _ targ: TF) -> TF {
29 | return (out.squeezingShape(at: -1) - targ).squared().mean()
30 | }
31 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_11_imagenette/Sources/FastaiNotebook_11_imagenette/02a_why_sqrt5.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 02a_why_sqrt5.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Foundation
10 | import TensorFlow
11 | import Path
12 |
13 | func leakyRelu(
14 | _ x: Tensor,
15 | negativeSlope: Double = 0.0
16 | ) -> Tensor {
17 | return max(0, x) + T(negativeSlope) * min(0, x)
18 | }
19 |
20 | extension Tensor where Scalar: TensorFlowFloatingPoint {
21 | init(kaimingUniform shape: TensorShape, negativeSlope: Double = 1.0) {
22 | // Assumes Leaky ReLU nonlinearity
23 | let gain = Scalar.init(TensorFlow.sqrt(2.0 / (1.0 + TensorFlow.pow(negativeSlope, 2))))
24 | let spatialDimCount = shape.count - 2
25 | let receptiveField = shape[0..
13 |
14 | public func accuracy(_ output: TF, _ target: TI) -> TF{
15 | let corrects = TF(output.argmax(squeezingAxis: 1) .== target)
16 | return corrects.mean()
17 | }
18 |
19 | public func batchedRanges(start:Int, end:Int, bs:Int) -> UnfoldSequence,Int>
20 | {
21 | return sequence(state: start) { (batchStart) -> Range? in
22 | let remaining = end - batchStart
23 | guard remaining > 0 else { return nil}
24 | let currentBs = min(bs,remaining)
25 | let batchEnd = batchStart.advanced(by: currentBs)
26 | defer { batchStart = batchEnd }
27 | return batchStart ..< batchEnd
28 | }
29 | }
30 |
31 | public struct DataBatch: TensorGroup {
32 | public var xb: Inputs
33 | public var yb: Labels
34 |
35 | public init(xb: Inputs, yb: Labels){ (self.xb,self.yb) = (xb,yb) }
36 |
37 | public var _tensorHandles: [_AnyTensorHandle] {
38 | xb._tensorHandles + yb._tensorHandles
39 | }
40 |
41 | public init(_handles: C) where C.Element: _AnyTensorHandle {
42 | let xStart = _handles.startIndex
43 | let xEnd = _handles.index(
44 | xStart, offsetBy: Int(Inputs._tensorHandleCount))
45 | self.xb = Inputs.init(_handles: _handles[xStart.. TF {
52 | return softmaxCrossEntropy(logits: logits, labels: labels)
53 | }
54 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_11_imagenette/Sources/FastaiNotebook_11_imagenette/06_cuda.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 06_cuda.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 | import TensorFlow
11 | #if canImport(PythonKit)
12 | import PythonKit
13 | #else
14 | import Python
15 | #endif
16 |
17 | extension Learner {
18 | public class AddChannel: Delegate {
19 | public override func batchWillStart(learner: Learner) {
20 | learner.currentInput = learner.currentInput!.expandingShape(at: -1)
21 | }
22 | }
23 |
24 | public func makeAddChannel() -> AddChannel { return AddChannel() }
25 | }
26 |
27 | public struct CnnModel: Layer {
28 | public var convs: [FAConv2D]
29 | public var pool = FAGlobalAvgPool2D()
30 | public var linear: FADense
31 |
32 | public init(channelIn: Int, nOut: Int, filters: [Int]){
33 | let allFilters = [channelIn] + filters
34 | convs = Array(0..(filters.last!, nOut)
38 | }
39 |
40 | @differentiable
41 | public func callAsFunction(_ input: TF) -> TF {
42 | //TODO: uncomment when https://github.com/tensorflow/swift-apis/issues/411 is solved
43 | //and array of Layer conform to Layer again
44 | //return linear(pool(convs(input)))
45 | let y = convs.differentiableReduce(input) { $1($0) }
46 | return linear(pool(y))
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/swift/FastaiNotebook_11_imagenette/Sources/FastaiNotebook_11_imagenette/08a_heterogeneous_dictionary.swift:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE WAS AUTOGENERATED! DO NOT EDIT!
3 | file to edit: 08a_heterogeneous_dictionary.ipynb
4 |
5 | */
6 |
7 |
8 |
9 | import Path
10 |
11 | public protocol HetDictKey {
12 | associatedtype ValueType
13 | static var defaultValue: ValueType { get }
14 | }
15 |
16 |
17 | public struct HeterogeneousDictionary {
18 | private var underlying: [ObjectIdentifier : Any] = [:]
19 |
20 | public init() {}
21 | public init(_ key: T.Type, _ value: T.ValueType) {
22 | self.underlying = [ObjectIdentifier(key): value]
23 | }
24 | public init(_ key1: T1.Type, _ value1: T1.ValueType, _ key2: T2.Type, _ value2: T2.ValueType) {
25 | self.underlying = [ObjectIdentifier(key1): value1, ObjectIdentifier(key2): value2]
26 | }
27 |
28 | public subscript(key: T.Type) -> T.ValueType {
29 | get { return underlying[ObjectIdentifier(key), default: T.defaultValue] as! T.ValueType }
30 | set { underlying[ObjectIdentifier(key)] = newValue as Any }
31 | }
32 |
33 | public mutating func merge(_ other: HeterogeneousDictionary,
34 | uniquingKeysWith combine: (Any, Any) throws -> Any) rethrows {
35 | try self.underlying.merge(other.underlying, uniquingKeysWith: combine)
36 | }
37 | }
38 |
39 |
40 | // Common keys
41 | public struct LearningRate: HetDictKey {
42 | public static var defaultValue: Float = 0.4
43 | }
44 |
--------------------------------------------------------------------------------
/swift/Imagenette/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | /.build
3 | /Packages
4 | /*.xcodeproj
5 | .idea
6 | .ipynb_checkpoints
--------------------------------------------------------------------------------
/swift/Imagenette/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "35dbe6823bf5f9b9bb996c1fe6c9ad9a4c59d821",
10 | "version": "0.7.3"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "dac007e907a4f4c565cfdc55a9ce148a761a11d5",
28 | "version": "0.16.3"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/Imagenette/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 |
3 | import PackageDescription
4 |
5 | let package = Package(
6 | name: "Imagenette",
7 | products: [
8 | .executable( name: "imagenette", targets: ["imagenette"]),
9 | ],
10 | dependencies: [.package(path: "../FastaiNotebook_11_imagenette")],
11 | targets: [
12 | .target( name: "imagenette", dependencies: ["FastaiNotebook_11_imagenette"]),
13 | ]
14 | )
15 |
--------------------------------------------------------------------------------
/swift/Imagenette/README.md:
--------------------------------------------------------------------------------
1 | # SwiftCV
2 |
3 | Minimal Swift for TensorFlow OpenCV4 bindings, partially ported from [gocv](https://github.com/hybridgroup/gocv).
4 |
5 | OpenCV Functions exposed:
6 | * resize
7 | * getRotationMatrix2D
8 | * warpAffine
9 | * copyMakeBorder
10 | * GaussianBlur
11 | * remap
12 | * imdecode
13 | * imread
14 | * cvtColor
15 | * flip
16 | * transpose
17 |
18 | OpenCV's `Mat` can be converted to S4TF's `Tensor` and `ShapedArray` types.
19 |
20 | See `Extra/Test.ipynb` and `Tests` as an example of usage.
21 |
22 | This notebook was executed using [updated swift-jupyter docker image](https://github.com/vvmnnnkv/swift-jupyter/tree/opencv4) with OpenCV4 installed.
23 |
24 | ## Usage
25 | Include as SwiftPM package:
26 |
27 | `.package(url: "https://github.com/vvmnnnkv/SwiftCV.git", .branch("master"))`
28 |
29 | NOTE: OpenCV4 must installed in order for package to compile.
30 |
31 |
32 | ## Disclaimer
33 | Currently this package is just an example of OpenCV/S4TF integration with no safety checks and guarantees to work properly :)
34 |
35 | ## License
36 | OpenCV C API, (c) Copyright [gocv](https://github.com/hybridgroup/gocv) authors.
--------------------------------------------------------------------------------
/swift/Imagenette/Sources/imagenette/main.swift:
--------------------------------------------------------------------------------
1 | import TensorFlow
2 | import Path
3 | import Foundation
4 | import FastaiNotebook_11_imagenette
5 |
6 | let path = downloadImagenette(sz:"-160")
7 | let il = ItemList(fromFolder: path, extensions: ["jpeg", "jpg"])
8 | let sd = SplitData(il) {grandParentSplitter(fName: $0, valid: "val")}
9 | var procLabel = CategoryProcessor()
10 | let sld = makeLabeledData(sd, fromFunc: parentLabeler, procLabel: &procLabel)
11 | let rawData = sld.toDataBunch(itemToTensor: pathsToTensor, labelToTensor: intsToTensor, bs: 128)
12 | let data = transformData(rawData) { openAndResize(fname: $0, size: 128) }
13 |
14 | func modelInit() -> XResNet { return xresnet18(cOut: 10) }
15 | let optFunc: (XResNet) -> StatefulOptimizer = adamOpt(lr: 1e-3, mom: 0.9, beta: 0.99, wd: 1e-2, eps: 1e-6)
16 | let learner = Learner(data: data, lossFunc: softmaxCrossEntropy, optFunc: optFunc, modelInit: modelInit)
17 | let recorder = learner.makeDefaultDelegates(metrics: [accuracy])
18 | learner.addDelegate(learner.makeNormalize(mean: imagenetStats.mean, std: imagenetStats.std))
19 |
20 | learner.addOneCycleDelegates(1e-3, pctStart: 0.5)
21 | time { try! learner.fit(5) }
22 |
23 |
--------------------------------------------------------------------------------
/swift/SwiftCV/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | /.build
3 | /Packages
4 | /*.xcodeproj
5 | .idea
6 | .ipynb_checkpoints
--------------------------------------------------------------------------------
/swift/SwiftCV/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "2b4aec55d7808a43526bef89341f8c3f962be89a",
10 | "version": "0.7.2"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "f324b4a562ca421fd2905414a10fb0fc91d58b67",
28 | "version": "0.16.2"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/SwiftCV/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 |
3 | import PackageDescription
4 |
5 | let package = Package(
6 | name: "SwiftCV",
7 | products: [
8 | .library( name: "SwiftCV", targets: ["SwiftCV"]),
9 | .executable( name: "dataload", targets: ["dataload"]),
10 | ],
11 | dependencies: [.package(path: "../FastaiNotebook_08_data_block")],
12 | targets: [
13 | .target( name: "dataload", dependencies: ["SwiftCV", "FastaiNotebook_08_data_block"]),
14 | .target( name: "SwiftCV", dependencies: ["COpenCV"]),
15 | .testTarget( name: "SwiftCVTests", dependencies: ["SwiftCV"]),
16 | .target( name: "COpenCV", dependencies: ["opencv4"]),
17 | .systemLibrary( name: "opencv4", pkgConfig: "opencv4")
18 | ]
19 | )
20 |
--------------------------------------------------------------------------------
/swift/SwiftCV/README.md:
--------------------------------------------------------------------------------
1 | # SwiftCV
2 |
3 | Minimal Swift for TensorFlow OpenCV4 bindings, partially ported from [gocv](https://github.com/hybridgroup/gocv).
4 |
5 | OpenCV Functions exposed:
6 | * resize
7 | * getRotationMatrix2D
8 | * warpAffine
9 | * copyMakeBorder
10 | * GaussianBlur
11 | * remap
12 | * imdecode
13 | * imread
14 | * cvtColor
15 | * flip
16 | * transpose
17 |
18 | OpenCV's `Mat` can be converted to S4TF's `Tensor` and `ShapedArray` types.
19 |
20 | See `Extra/Test.ipynb` and `Tests` as an example of usage.
21 |
22 | This notebook was executed using [updated swift-jupyter docker image](https://github.com/vvmnnnkv/swift-jupyter/tree/opencv4) with OpenCV4 installed.
23 |
24 | ## Usage
25 | Include as SwiftPM package:
26 |
27 | `.package(url: "https://github.com/vvmnnnkv/SwiftCV.git", .branch("master"))`
28 |
29 | NOTE: OpenCV4 must installed in order for package to compile.
30 |
31 |
32 | ## Disclaimer
33 | Currently this package is just an example of OpenCV/S4TF integration with no safety checks and guarantees to work properly :)
34 |
35 | ## License
36 | OpenCV C API, (c) Copyright [gocv](https://github.com/hybridgroup/gocv) authors.
--------------------------------------------------------------------------------
/swift/SwiftCV/Sources/COpenCV/imgcodecs.cpp:
--------------------------------------------------------------------------------
1 | #include "include/imgcodecs.h"
2 |
3 | Mat Image_IMRead(const char* filename, int flags) {
4 | cv::Mat img = cv::imread(filename, flags);
5 | return new cv::Mat(img);
6 | }
7 |
8 | bool Image_IMWrite(const char* filename, Mat img) { return cv::imwrite(filename, *img); }
9 |
10 | bool Image_IMWrite_WithParams(const char* filename, Mat img, IntVector params) {
11 | std::vector compression_params;
12 | for (int i = 0, *v = params.val; i < params.length; ++v, ++i) { compression_params.push_back(*v); }
13 | return cv::imwrite(filename, *img, compression_params);
14 | }
15 |
16 | struct ByteArray Image_IMEncode(const char* fileExt, Mat img) {
17 | std::vector data;
18 | cv::imencode(fileExt, *img, data);
19 | return toByteArray(reinterpret_cast(&data[0]), data.size());
20 | }
21 |
22 | struct ByteArray Image_IMEncode_WithParams(const char* fileExt, Mat img, IntVector params) {
23 | std::vector data;
24 | std::vector compression_params;
25 | for (int i = 0, *v = params.val; i < params.length; ++v, ++i) { compression_params.push_back(*v); }
26 | cv::imencode(fileExt, *img, data, compression_params);
27 | return toByteArray(reinterpret_cast(&data[0]), data.size());
28 | }
29 |
30 | Mat Image_IMDecode(ByteArray buf, int flags) {
31 | std::vector data;
32 | for (size_t i = 0; i < buf.length; i++) { data.push_back(buf.data[i]); }
33 | cv::Mat img = cv::imdecode(data, flags);
34 | return new cv::Mat(img);
35 | }
36 |
37 |
--------------------------------------------------------------------------------
/swift/SwiftCV/Sources/COpenCV/include/imgcodecs.h:
--------------------------------------------------------------------------------
1 | #ifndef _OPENCV3_IMGCODECS_H_
2 | #define _OPENCV3_IMGCODECS_H_
3 |
4 | #include
5 |
6 | #ifdef __cplusplus
7 | #include
8 | extern "C" {
9 | #endif
10 |
11 | #include "core.h"
12 |
13 | Mat Image_IMRead(const char* filename, int flags);
14 | bool Image_IMWrite(const char* filename, Mat img);
15 | bool Image_IMWrite_WithParams(const char* filename, Mat img, IntVector params);
16 | struct ByteArray Image_IMEncode(const char* fileExt, Mat img);
17 | struct ByteArray Image_IMEncode_WithParams(const char* fileExt, Mat img, IntVector params);
18 | Mat Image_IMDecode(ByteArray buf, int flags);
19 |
20 | #ifdef __cplusplus
21 | }
22 | #endif
23 |
24 | #endif //_OPENCV3_IMGCODECS_H_
25 |
--------------------------------------------------------------------------------
/swift/SwiftCV/Sources/COpenCV/include/version.h:
--------------------------------------------------------------------------------
1 | #ifndef _OPENCV3_VERSION_H_
2 | #define _OPENCV3_VERSION_H_
3 |
4 | #ifdef __cplusplus
5 | #include
6 | extern "C" {
7 | #endif
8 |
9 | #include "core.h"
10 |
11 | const char* openCVVersion();
12 |
13 | #ifdef __cplusplus
14 | }
15 | #endif
16 |
17 | #endif //_OPENCV3_VERSION_H_
18 |
--------------------------------------------------------------------------------
/swift/SwiftCV/Sources/COpenCV/version.cpp:
--------------------------------------------------------------------------------
1 | #include "include/version.h"
2 |
3 | const char* openCVVersion() {
4 | return CV_VERSION;
5 | }
6 |
--------------------------------------------------------------------------------
/swift/SwiftCV/Sources/SwiftCV/ConcurrentMap.swift:
--------------------------------------------------------------------------------
1 | import Foundation
2 |
3 | // ThreadSafe and concurrentMap based on https://talk.objc.io/episodes/S01E90-concurrent-map
4 | public final class ThreadSafe {
5 | var _value: A
6 | let queue = DispatchQueue(label: "ThreadSafe")
7 | init(_ value: A) { self._value = value }
8 |
9 | var value: A {
10 | return queue.sync { _value }
11 | }
12 | func atomically(_ transform: (inout A) -> ()) {
13 | queue.sync { transform(&self._value) }
14 | }
15 | }
16 |
17 | public extension Array {
18 | func concurrentMap(nthreads:Int?=nil, _ transform: (Element) -> B) -> [B] {
19 | let result = ThreadSafe(Array(repeating: nil, count: count))
20 | let nt = nthreads ?? count
21 | let cs = (count-1)/nt+1
22 | DispatchQueue.concurrentPerform(iterations: nt) { i in
23 | let min = i*cs
24 | let max = min+cs>count ? count : min+cs
25 | for idx in (min..Mat {
16 | let cvImg = imread(path)
17 | return cvtColor(cvImg, nil, ColorConversionCode.COLOR_BGR2RGB)
18 | }
19 |
20 | func readAndResize(_ name:String)->UInt8 {
21 | let cvImg = readImage(name)
22 | let rImg = resize(cvImg, nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_LINEAR)
23 | let ptr = UnsafeBufferPointer(start: UnsafeRawPointer(rImg.dataPtr).assumingMemoryBound(to: UInt8.self), count: rImg.count)
24 | return ptr[0]
25 | }
26 |
27 | var stats = [UInt8]()
28 | time {
29 | stats = ns.concurrentMap(nthreads:4, readAndResize)
30 | //stats = ns.map(readAndResize)
31 | }
32 | print(stats[0..<10])
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 | /*
55 | let q = DispatchQueue(label: "q", qos: .userInitiated, attributes: .concurrent)
56 | let l = DispatchQueue(label: "l")
57 | let nt = 4
58 |
59 | let semaphore = DispatchSemaphore(value: nt)
60 | var stats = [(Int,Double)]()
61 | time {
62 | for (i,n) in ns.enumerated() {
63 | semaphore.wait()
64 | q.async {
65 | let r = readAndResize(n)
66 | l.sync {stats.append((i,r))}
67 | semaphore.signal()
68 | }
69 | }
70 | for _ in (0..(cvMat: img)!
12 | XCTAssertEqual(arr.shape, [1200, 1200, 3])
13 | }
14 |
15 | func testTensorConversion() {
16 | let imgFilename = fixturesFolder.appendingPathComponent("test.png").path
17 | let img: Mat = imread(imgFilename)
18 | let tens = Tensor(cvMat: img)!
19 | XCTAssertEqual(tens.shape, [1200, 1200, 3])
20 | }
21 |
22 | static var allTests = [
23 | ("testShapedArrayConversion", testShapedArrayConversion),
24 | ("testTensorConversion", testTensorConversion),
25 | ]
26 | }
27 |
--------------------------------------------------------------------------------
/swift/SwiftCV/Tests/SwiftCVTests/XCTestManifests.swift:
--------------------------------------------------------------------------------
1 | import XCTest
2 |
3 | #if !os(macOS)
4 | public func allTests() -> [XCTestCaseEntry] {
5 | return [
6 | testCase(SwiftCVTests.allTests),
7 | testCase(TensorFlowConversionTests.allTests),
8 | ]
9 | }
10 | #endif
11 |
--------------------------------------------------------------------------------
/swift/SwiftCV/Tests/SwiftCVTests/fixtures/blur.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/swift/SwiftCV/Tests/SwiftCVTests/fixtures/blur.jpg
--------------------------------------------------------------------------------
/swift/SwiftCV/Tests/SwiftCVTests/fixtures/border.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/swift/SwiftCV/Tests/SwiftCVTests/fixtures/border.jpg
--------------------------------------------------------------------------------
/swift/SwiftCV/Tests/SwiftCVTests/fixtures/grey.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/swift/SwiftCV/Tests/SwiftCVTests/fixtures/grey.jpg
--------------------------------------------------------------------------------
/swift/SwiftCV/Tests/SwiftCVTests/fixtures/resize.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/swift/SwiftCV/Tests/SwiftCVTests/fixtures/resize.jpg
--------------------------------------------------------------------------------
/swift/SwiftCV/Tests/SwiftCVTests/fixtures/test.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/swift/SwiftCV/Tests/SwiftCVTests/fixtures/test.png
--------------------------------------------------------------------------------
/swift/SwiftCV/Tests/SwiftCVTests/fixtures/zoom.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/swift/SwiftCV/Tests/SwiftCVTests/fixtures/zoom.jpg
--------------------------------------------------------------------------------
/swift/SwiftCV/install/install_cv4.sh:
--------------------------------------------------------------------------------
1 | set -ex
2 |
3 | OPENCV_VERSION='4.1.0'
4 |
5 | APT_PROGRAM="apt-fast"
6 | command -v $APT_PROGRAM > /dev/null 2>&1 || {
7 | echo "Falling back to apt-get as apt-fast is not installed..."
8 | APT_PROGRAM="apt-get"
9 | }
10 |
11 | sudo $APT_PROGRAM -y update
12 | sudo $APT_PROGRAM install -y build-essential cmake zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libopenexr-dev libgdal-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev libtbb-dev libeigen3-dev python-dev python-tk python-numpy python3-dev python3-tk python3-numpy unzip wget libopenblas-dev libopenblas-base liblapacke-dev libatlas-base-dev liblapack-dev libblas-dev
13 | sudo ln -s /usr/include/lapacke.h /usr/include/x86_64-linux-gnu # corrected path for the library
14 |
15 | mkdir -p ~/download
16 | pushd ~/download
17 |
18 | wget https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip
19 | unzip -q ${OPENCV_VERSION}.zip
20 | rm ${OPENCV_VERSION}.zip
21 | mv opencv-${OPENCV_VERSION} OpenCV
22 | cd OpenCV
23 | mkdir build
24 | cd build
25 | cmake \
26 | -D BUILD_LIST=core,imgproc,imgcodecs \
27 | -D CMAKE_BUILD_TYPE=Release \
28 | -D OPENCV_GENERATE_PKGCONFIG=YES \
29 | -D WITH_CSTRIPES=OFF \
30 | -D WITH_PTHREADS_PF=OFF \
31 | -D WITH_QT=OFF \
32 | -D WITH_OPENGL=OFF \
33 | -D WITH_OPENCL=OFF \
34 | -D WITH_OPENMP=OFF \
35 | -D WITH_TBB=ON \
36 | -D WITH_GDAL=ON \
37 | -D WITH_XINE=ON \
38 | -D BUILD_DOCS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF \
39 | -D ENABLE_PRECOMPILED_HEADERS=OFF \
40 | -D WITH_IPP=ON \
41 | -D CPU_BASELINE=NATIVE \
42 | -D ENABLE_FAST_MATH=ON \
43 | .. | tee install_cv4.log
44 | make -j $(nproc --all)
45 | sudo make install
46 | sudo ldconfig
47 | popd
48 |
49 | echo "Success!"
50 |
--------------------------------------------------------------------------------
/swift/SwiftSox/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | /.build
3 | /Packages
4 | /*.xcodeproj
5 |
--------------------------------------------------------------------------------
/swift/SwiftSox/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 |
3 | import PackageDescription
4 |
5 | let package = Package(
6 | name: "SwiftSox",
7 | products: [ .library( name: "SwiftSox", targets: ["SwiftSox"]), ],
8 | targets: [
9 | .target( name: "SwiftSox", dependencies: ["sox"]),
10 | .testTarget( name: "SwiftSoxTests", dependencies: ["SwiftSox"]),
11 | .systemLibrary( name: "sox", pkgConfig: "sox")
12 | ]
13 | )
14 |
--------------------------------------------------------------------------------
/swift/SwiftSox/README.md:
--------------------------------------------------------------------------------
1 | # SwiftSox
2 |
3 | A delightful wrapper for libsox in Swift. Or at least whatever we were able to throw together in a couple of hours.
4 |
5 | ## Docs
6 |
7 | Read the source code.
8 |
9 |
--------------------------------------------------------------------------------
/swift/SwiftSox/Sources/SwiftSox/SwiftSox.swift:
--------------------------------------------------------------------------------
1 | import sox
2 |
3 | public func InitSox() {
4 | if sox_format_init() != SOX_SUCCESS.rawValue { fatalError("Can not init SOX!") }
5 | }
6 |
7 | public func ReadSoxAudio(_ name:String)->UnsafeMutablePointer {
8 | return sox_open_read(name, nil, nil, nil)
9 | }
10 |
11 |
--------------------------------------------------------------------------------
/swift/SwiftSox/Sources/sox/module.modulemap:
--------------------------------------------------------------------------------
1 | module sox [system] {
2 | umbrella header "soxu.h"
3 | link "sox"
4 | export *
5 | }
6 |
--------------------------------------------------------------------------------
/swift/SwiftSox/Sources/sox/soxu.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 |
--------------------------------------------------------------------------------
/swift/SwiftSox/Tests/LinuxMain.swift:
--------------------------------------------------------------------------------
1 | import XCTest
2 |
3 | import SwiftSoxTests
4 |
5 | var tests = [XCTestCaseEntry]()
6 | tests += SwiftSoxTests.allTests()
7 | XCTMain(tests)
8 |
9 |
--------------------------------------------------------------------------------
/swift/SwiftSox/Tests/SwiftSoxTests/SwiftSoxTests.swift:
--------------------------------------------------------------------------------
1 | import XCTest
2 | import sox
3 | @testable import SwiftSox
4 |
5 | final class SwiftSoxTests: XCTestCase {
6 | func testRead() {
7 | InitSox()
8 | let fd = sox_open_read("sounds/beep-01a.mp3", nil, nil, nil).pointee
9 | let sig = fd.signal
10 | XCTAssertEqual(sig.rate, 44100.0)
11 | XCTAssertEqual(sig.precision, 16)
12 | XCTAssertEqual(sig.channels, 1)
13 | XCTAssert(sig.length>0)
14 | }
15 |
16 | func testReadSwift() {
17 | let fd = ReadSoxAudio("sounds/beep-01a.mp3")
18 | let sig = fd.pointee.signal
19 | XCTAssertEqual(sig.rate, 44100.0)
20 | XCTAssertEqual(sig.precision, 16)
21 | XCTAssertEqual(sig.channels, 1)
22 | XCTAssert(sig.length>0)
23 | }
24 |
25 | static var allTests = [
26 | ("testRead", testRead),
27 | ("testReadSwift", testReadSwift),
28 | ]
29 | }
30 |
--------------------------------------------------------------------------------
/swift/SwiftSox/Tests/SwiftSoxTests/XCTestManifests.swift:
--------------------------------------------------------------------------------
1 | import XCTest
2 |
3 | #if !os(macOS)
4 | public func allTests() -> [XCTestCaseEntry] {
5 | return [
6 | testCase(SwiftSoxTests.allTests),
7 | ]
8 | }
9 | #endif
10 |
--------------------------------------------------------------------------------
/swift/SwiftSox/install.sh:
--------------------------------------------------------------------------------
1 | cd ~/download/
2 | wget https://sourceforge.net/projects/sox/files/sox/14.4.2/sox-14.4.2.tar.gz/download
3 | mv download sox-14.4.2.tar.gz
4 | tar xf sox-14.4.2.tar.gz
5 | cd sox-14.4.2/
6 | sudo apt install libopencore-amrnb-dev libopencore-amrwb-dev libao-dev libflac-dev libmp3lame-dev libtwolame-dev libltdl-dev libmad0-dev libid3tag0-dev libvorbis-dev libpng-dev libsndfile1-dev libwavpack-dev autoconf automake
7 | autoreconf -i
8 | ./configure
9 | make -j
10 | make -s
11 | sudo make install
12 |
13 |
--------------------------------------------------------------------------------
/swift/SwiftSox/sounds/beep-01a.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/swift/SwiftSox/sounds/beep-01a.mp3
--------------------------------------------------------------------------------
/swift/SwiftSox/sounds/chris.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/swift/SwiftSox/sounds/chris.mp3
--------------------------------------------------------------------------------
/swift/SwiftVips/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | /.build
3 | /Packages
4 | /*.xcodeproj
5 |
--------------------------------------------------------------------------------
/swift/SwiftVips/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/saeta/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "2b4aec55d7808a43526bef89341f8c3f962be89a",
10 | "version": "0.7.2"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9102631248217cc8ef405e92edae7bdcebcb7166",
19 | "version": "0.6.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "f324b4a562ca421fd2905414a10fb0fc91d58b67",
28 | "version": "0.16.2"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/SwiftVips/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 |
3 | import PackageDescription
4 |
5 | let package = Package(
6 | name: "SwiftVips",
7 | products: [
8 | .library( name: "SwiftVips", targets: ["SwiftVips"]),
9 | .library( name: "vips", targets: ["vips"]),
10 | .executable( name: "dataload", targets: ["dataload"]),
11 | ],
12 | dependencies: [.package(path: "../FastaiNotebook_08_data_block")],
13 | targets: [
14 | .target( name: "CSwiftVips", dependencies: ["vips"]),
15 | .target( name: "SwiftVips", dependencies: ["CSwiftVips", "vips"]),
16 | .target( name: "dataload", dependencies: ["vips", "FastaiNotebook_08_data_block", "SwiftVips"]),
17 | .systemLibrary( name: "vips", pkgConfig: "vips")
18 | ]
19 | )
20 |
21 |
--------------------------------------------------------------------------------
/swift/SwiftVips/README.md:
--------------------------------------------------------------------------------
1 | # dataload
2 |
3 | A description of this package.
4 |
--------------------------------------------------------------------------------
/swift/SwiftVips/Sources/CSwiftVips/core.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/swift/SwiftVips/Sources/CSwiftVips/core.c
--------------------------------------------------------------------------------
/swift/SwiftVips/Sources/CSwiftVips/include/core.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | VipsImage* vipsLoadImage(const char *name) { return vips_image_new_from_file( name, "memory", TRUE, NULL ); }
4 |
5 | VipsImage* vipsResize(VipsImage* in, double scale, double vscale) {
6 | VipsImage* out;
7 | if (vips_resize(in, &out, scale, "vscale", vscale, "kernel", VIPS_KERNEL_LINEAR, NULL)) vips_error_exit( NULL );
8 | return out;
9 | }
10 |
11 | VipsImage* vipsShrink(VipsImage* in, double hshrink, double vshrink) {
12 | VipsImage* out;
13 | if (vips_shrink(in, &out, hshrink, vshrink, NULL)) vips_error_exit( NULL );
14 | return out;
15 | }
16 |
17 | double vipsMax(VipsImage* in) {
18 | double d;
19 | if( vips_max( in, &d, NULL ) ) vips_error_exit( NULL );
20 | return d;
21 | }
22 |
23 | double vipsMin(VipsImage* in) {
24 | double d;
25 | if( vips_min( in, &d, NULL ) ) vips_error_exit( NULL );
26 | return d;
27 | }
28 |
29 | double vipsAvg(VipsImage* in) {
30 | double d;
31 | if( vips_avg( in, &d, NULL ) ) vips_error_exit( NULL );
32 | return d;
33 | }
34 |
35 | long vipsImageGetHeight(VipsImage* in) { return vips_image_get_height(in); }
36 | long vipsImageGetBands(VipsImage* in) { return vips_image_get_bands(in); }
37 | long vipsImageGetWidth(VipsImage* in) { return vips_image_get_width(in); }
38 |
39 | unsigned char* vipsGet(VipsImage* in, size_t* sz) { return vips_image_write_to_memory(in, sz); }
40 |
41 |
--------------------------------------------------------------------------------
/swift/SwiftVips/Sources/SwiftVips/ConcurrentMap.swift:
--------------------------------------------------------------------------------
1 | import Foundation
2 |
3 | // ThreadSafe and concurrentMap based on https://talk.objc.io/episodes/S01E90-concurrent-map
4 | public final class ThreadSafe {
5 | var _value: A
6 | let queue = DispatchQueue(label: "ThreadSafe")
7 | init(_ value: A) { self._value = value }
8 |
9 | var value: A {
10 | return queue.sync { _value }
11 | }
12 | func atomically(_ transform: (inout A) -> ()) {
13 | queue.sync { transform(&self._value) }
14 | }
15 | }
16 |
17 | public extension Array {
18 | func concurrentMap(nthreads:Int?=nil, _ transform: @escaping (Element) -> B) -> [B] {
19 | let result = ThreadSafe(Array(repeating: nil, count: count))
20 | let nt = nthreads ?? count
21 | let cs = (count-1)/nt+1
22 | DispatchQueue.concurrentPerform(iterations: nt) { i in
23 | let min = i*cs
24 | let max = min+cs>count ? count : min+cs
25 | for idx in (min..?
5 |
6 | public func vipsInit() {
7 | if vips_init("init") != 0 { fatalError("Failed in init vips") }
8 | vips_concurrency_set(1)
9 | }
10 |
11 | public func vipsShape(_ img:Image)->[Int] {
12 | return [vipsImageGetHeight(img),vipsImageGetWidth(img),vipsImageGetBands(img)]
13 | }
14 |
--------------------------------------------------------------------------------
/swift/SwiftVips/Sources/dataload/main.swift:
--------------------------------------------------------------------------------
1 | //import TensorFlow
2 | import Path
3 | import FastaiNotebook_08_data_block
4 | import vips
5 | import CSwiftVips
6 | import SwiftVips
7 | import Foundation
8 |
9 | vipsInit()
10 |
11 | let path = downloadImagenette(sz:"-320")
12 | let allNames = fetchFiles(path: path/"train", recurse: true, extensions: ["jpeg", "jpg"])
13 | let fNames = Array(allNames[0..<256])
14 | let ns = fNames.map {$0.string}
15 | //let ns = allNames.map {$0.string} // 34.3 s
16 |
17 | func readAndResize(_ name:String)->Double {
18 | guard let img = vipsLoadImage(name) else { fatalError("failed to read \(name)") }
19 | let w = Double(vips_image_get_width(img))
20 | let h = Double(vips_image_get_height(img))
21 | let rimg = vipsResize(img, 224/w, 224/h)
22 | return vipsMax(rimg)
23 | }
24 |
25 | time {
26 | let stats = ns.concurrentMap(nthreads:4, readAndResize)
27 | //let stats = ns.map(readAndResize)
28 | print(stats)
29 | }
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 | /*
52 | let q = DispatchQueue(label: "q", qos: .userInitiated, attributes: .concurrent)
53 | let l = DispatchQueue(label: "l")
54 | let nt = 4
55 |
56 | let semaphore = DispatchSemaphore(value: nt)
57 | var stats = [(Int,Double)]()
58 | time {
59 | for (i,n) in ns.enumerated() {
60 | semaphore.wait()
61 | q.async {
62 | let r = readAndResize(n)
63 | l.sync {stats.append((i,r))}
64 | semaphore.signal()
65 | }
66 | }
67 | for _ in (0..
2 |
3 |
--------------------------------------------------------------------------------
/swift/SwiftVips/install.sh:
--------------------------------------------------------------------------------
1 | pushd ~/git
2 | sudo apt-fast install git build-essential libxml2-dev libfftw3-dev libmagickwand-dev libopenexr-dev liborc-0.4-0 gobject-introspection libgsf-1-dev libglib2.0-dev liborc-0.4-dev python-gi-dev libgirepository1.0-dev automake libtool swig gtk-doc-tools libopenslide-dev libmatio-dev libgif-dev libwebp-dev
3 | git clone https://github.com/libvips/libvips.git
4 | cd libvips
5 | ./autogen.sh
6 | make -j
7 | sudo make install
8 | vips --vips-version
9 | popd
10 |
11 |
--------------------------------------------------------------------------------
/swift/datablock/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | /.build
3 | /Packages
4 | /*.xcodeproj
5 |
--------------------------------------------------------------------------------
/swift/datablock/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 | {
5 | "package": "Just",
6 | "repositoryURL": "https://github.com/JustHTTP/Just",
7 | "state": {
8 | "branch": null,
9 | "revision": "a6b530d0e7f237aee6262a4ba71086d9d884bb58",
10 | "version": "0.7.1"
11 | }
12 | },
13 | {
14 | "package": "NotebookExport",
15 | "repositoryURL": "https://github.com/latenitesoft/NotebookExport",
16 | "state": {
17 | "branch": null,
18 | "revision": "9fcca3a58713146a13ceb8812a0ee511ec3cc975",
19 | "version": "0.5.0"
20 | }
21 | },
22 | {
23 | "package": "Path.swift",
24 | "repositoryURL": "https://github.com/mxcl/Path.swift",
25 | "state": {
26 | "branch": null,
27 | "revision": "f324b4a562ca421fd2905414a10fb0fc91d58b67",
28 | "version": "0.16.2"
29 | }
30 | }
31 | ]
32 | },
33 | "version": 1
34 | }
35 |
--------------------------------------------------------------------------------
/swift/datablock/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:4.2
2 |
3 | import PackageDescription
4 |
5 | let package = Package(
6 | name: "datablock",
7 | products: [
8 | .executable( name: "datablock", targets: ["datablock"]),
9 | ],
10 | dependencies: [.package(path: "../FastaiNotebook_08c_data_block_generic")],
11 | targets: [
12 | .target( name: "datablock", dependencies: ["FastaiNotebook_08c_data_block_generic"]),
13 | ]
14 | )
15 |
--------------------------------------------------------------------------------
/swift/datablock/README.md:
--------------------------------------------------------------------------------
1 | # datablock
2 |
3 | A description of this package.
4 |
--------------------------------------------------------------------------------
/swift/datablock/Sources/datablock/main.swift:
--------------------------------------------------------------------------------
1 | import Path
2 | import TensorFlow
3 | #if canImport(PythonKit)
4 | import PythonKit
5 | #else
6 | import Python
7 | #endif
8 | import FastaiNotebook_08c_data_block_generic
9 | import SwiftCV
10 | SetNumThreads(0)
11 |
12 | var procL = CategoryProcessor()
13 | let sld = makeSLD(config: ImageNette.self, procL: &procL)
14 |
15 | let transforms = openImage >| BGRToRGB >| { resize($0, size: 224) }
16 | let pathToTF = transforms
17 |
18 | public func collateFunc(_ xb: [Mat], _ yb: [Int32]) -> DataBatch {
19 | let x = Tensor(concatenating: xb.map{ Tensor(cvMat: $0)!.expandingShape(at: 0)} )
20 | let y = Tensor(concatenating: yb.map{ Tensor($0).expandingShape(at: 0)} )
21 | return DataBatch(xb: TF(x)/255.0, yb: y)
22 | }
23 |
24 | let batcher = Batcher(sld.train, fX: pathToTF, fY: Int32.init, collateFunc: collateFunc,
25 | bs:256, numWorkers:6, shuffle:false)
26 |
27 | time {
28 | for _ in batcher {}
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/swift/minimal:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/swift/minimal
--------------------------------------------------------------------------------
/swift/minimal.swift:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastai/fastai_dev/4a33cb2e5f4d480e47f1f16875b8f10d8c205a8c/swift/minimal.swift
--------------------------------------------------------------------------------
/tools/read-nbs:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | import nbformat, os
4 | from pathlib import Path
5 |
6 | def read_nbs(path):
7 | "Check all notebooks in `path` (and subfolders) can be opened"
8 | path,nb_files = Path(path),[]
9 | for p,d,f in os.walk(path): nb_files += [Path(p)/f_ for f_ in f if f_.endswith('.ipynb')]
10 | for nb in nb_files:
11 | try:
12 | with open(nb, 'r') as f: _ = nbformat.reads(f.read(), as_version=4)
13 | except Exception as e:
14 | print(f"{nb} is corrupted and can't be opened.")
15 | raise e
16 |
17 | read_nbs('dev')
18 | read_nbs('dev_nb')
--------------------------------------------------------------------------------
/tools/run-after-git-clone:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | # if you're a fastai developer please make sure you do this:
4 | #
5 | # git clone https://github.com/fastai/fastai_docs
6 | # cd fastai_docs
7 | # tools/run-after-git-clone
8 | #
9 |
10 | import sys
11 | if sys.hexversion < 0x03060000: sys.exit("!!! Please re-run this script with python-3.6 or higher")
12 |
13 | import subprocess, os
14 | from pathlib import Path
15 |
16 | def run_script(script):
17 |
18 | cmd = f"{sys.executable} {script}"
19 | #print(f"Executing: {cmd}")
20 | result = subprocess.run(cmd.split(), shell=False, check=False,
21 | stdout=subprocess.PIPE, stderr=subprocess.PIPE)
22 |
23 | if result.returncode != 0: print(f"Failed to execute: {cmd}")
24 | if result.stdout: print(f"{result.stdout.decode('utf-8')}")
25 | if result.stderr: print(f"Error: {result.stderr.decode('utf-8')}")
26 |
27 | # make sure we are under the root of the project
28 | cur_dir = Path(".").resolve().name
29 | if (cur_dir == "tools"): os.chdir("..")
30 |
31 | path = Path("tools")
32 |
33 | # facilitate trusting of the repo-wide .gitconfig
34 | run_script(path/"trust-origin-git-config")
35 |
36 | # facilitate trusting notebooks under docs_src
37 | run_script(path/"trust-doc-nbs-install-hook")
38 |
--------------------------------------------------------------------------------
/tools/trust-doc-nbs:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | # This script signs all notebooks in the docs_src directory as trusted
4 | # It's is used as a git post-merge hook
5 |
6 | import nbformat.sign, sys, os, os.path
7 | from pathlib import Path
8 |
9 | # make sure we are under the root of the project
10 | cur_dir = Path(".").resolve().name
11 | if (cur_dir == "tools"): os.chdir("..")
12 |
13 | # Be almost invisible when run as a git hook, to keep the `git pull`
14 | # output familiar, yet telling the user that his system is busy
15 | # running the script.
16 | # We know we run under git when this script is invoked as
17 | # .git/hooks/post-merge. When it is invoked as tools/trust-doc-nbs we
18 | # can be more verbose.
19 | stealth = True if "post-merge" in __file__ else False
20 | end = "\r" if stealth else "\n"
21 |
22 |
23 | def trust_nbs(dirname):
24 | print(f"Attention! Signing notebooks under {dirname} as *trusted*", end=end)
25 | path = Path(dirname)
26 | fname_last_checked = path/".last_checked"
27 | last_checked = os.path.getmtime(fname_last_checked) if fname_last_checked.exists() else None
28 |
29 | for fname in path.glob("*.ipynb"):
30 |
31 | # speed up things by skipping the if-not-signed test if the nb hasn't changed since the last signing attempt
32 | if last_checked:
33 | last_changed = os.path.getmtime(fname)
34 | if last_changed < last_checked: continue
35 |
36 | with open(fname) as f:
37 | nb = nbformat.read(f, as_version=4)
38 | if not nbformat.sign.NotebookNotary().check_signature(nb):
39 | #print(f"unsigned {fname}")
40 | nbformat.sign.NotebookNotary().sign(nb)
41 |
42 | fname_last_checked.touch(exist_ok=True)
43 |
44 | trust_nbs('dev')
45 | trust_nbs('dev_nb')
46 |
47 | # clear the temporary output
48 | if stealth: sys.stdout.write("\033[K")
49 |
--------------------------------------------------------------------------------
/tools/trust-doc-nbs-install-hook:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | # This script facilitates trusting notebooks under docs_src.
4 | #
5 | # It should be run once immediately after running 'git clone'.
6 | #
7 | # It will install a post-merge hook in your .git/hooks/ directory.
8 | #
9 | # The installed hook will be executed by git automatically at the end
10 | # of 'git pull' only if it triggered an actual merge event and that
11 | # the latter was successful
12 | #
13 | # trust:
14 | # tools/trust-doc-nbs-install-hook
15 | #
16 | # distrust:
17 | # rm .git/hooks/post-merge
18 | #
19 | # note: windows users, not using bash emulation, will need to invoke this tool as:
20 | # python tools\trust-doc-nbs-install-hook
21 |
22 | import sys
23 | if sys.hexversion < 0x03060000: sys.exit("!!! Please re-run this script with python-3.6 or higher")
24 |
25 | from shutil import copy2
26 | from pathlib import Path
27 |
28 | fin = Path("tools")/"trust-doc-nbs"
29 | fout = Path(".git")/"hooks"/"post-merge"
30 |
31 | print("Installing .git/hooks/post-merge to automatically trust dev and dev_nb notebooks")
32 | copy2(fin, fout)
33 | print("Success")
34 |
--------------------------------------------------------------------------------