Skip to content

Commit 445c90e

Browse files
committed
Added Tutorial Hvass-Labs#7
1 parent dcf3fbd commit 445c90e

16 files changed

Lines changed: 1469 additions & 47 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ sandbox*.py
33

44
# Data for TensorFlow
55
data/
6+
inception/
67
checkpoints/
78
checkpoints*
89
logs/

07_Inception_Model.ipynb

Lines changed: 992 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# TensorFlow-Tutorials
22

3+
[Original repository on GitHub](https://github.com/Hvass-Labs/TensorFlow-Tutorials)
4+
35
## Contents
46

57
1. Simple Linear Model ([Notebook](https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/01_Simple_Linear_Model.ipynb))
@@ -14,6 +16,8 @@
1416

1517
6. CIFAR-10 ([Notebook](https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/06_CIFAR-10.ipynb))
1618

19+
7. Inception Model ([Notebook](https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/07_Inception_Model.ipynb))
20+
1721
## Videos
1822

1923
These tutorials are also available as [YouTube Videos](https://www.youtube.com/playlist?list=PL9Hr9sNUjfsmEu1ZniY0XpHSzl5uihcXZ).

cifar10.py

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
########################################################################
2424
#
2525
# This file is part of the TensorFlow Tutorials available at:
26+
#
2627
# https://github.com/Hvass-Labs/TensorFlow-Tutorials
2728
#
2829
# Published under the MIT License. See the file LICENSE for details.
@@ -33,10 +34,7 @@
3334

3435
import numpy as np
3536
import pickle
36-
import urllib.request
37-
import tarfile
38-
import sys
39-
import os
37+
import download
4038

4139
########################################################################
4240

@@ -79,22 +77,6 @@
7977
########################################################################
8078
# Private functions for downloading, unpacking and loading data-files.
8179

82-
def _print_download_progress(count, block_size, total_size):
83-
"""
84-
Used for printing the download progress.
85-
Used as a call-back function in maybe_download_and_extract().
86-
"""
87-
88-
# Percentage completion.
89-
pct_complete = float(count * block_size) / total_size
90-
91-
# Status-message. Note the \r which means the line should overwrite itself.
92-
msg = "\r- Download progress: {0:.1%}".format(pct_complete)
93-
94-
# Print it.
95-
sys.stdout.write(msg)
96-
sys.stdout.flush()
97-
9880

9981
def _get_file_path(filename=""):
10082
"""
@@ -187,33 +169,7 @@ def maybe_download_and_extract():
187169
in data_path (set this variable first to the desired path).
188170
"""
189171

190-
# Check if the path for unpacked data-files already exists.
191-
# If it exists then we assume the tar-ball has already been
192-
# downloaded and extracted, otherwise we need to do that now.
193-
if not os.path.exists(_get_file_path()):
194-
# Check if the destination path exists, otherwise create it.
195-
if not os.path.exists(data_path):
196-
os.makedirs(data_path)
197-
198-
# Filename for saving the file downloaded from the internet.
199-
# Use the original file-name and add it to the data_path.
200-
filename = data_url.split('/')[-1]
201-
file_path = os.path.join(data_path, filename)
202-
203-
# Download the file from the internet.
204-
file_path, _ = urllib.request.urlretrieve(url=data_url,
205-
filename=file_path,
206-
reporthook=_print_download_progress)
207-
208-
print()
209-
print("Download finished. Extracting files.")
210-
211-
# Unpack the tar-ball.
212-
tarfile.open(name=file_path, mode="r:gz").extractall(data_path)
213-
214-
print("Done.")
215-
else:
216-
print("CIFAR-10 data has apparently already been downloaded and unpacked.")
172+
download.maybe_download_and_extract(url=data_url, download_dir=data_path)
217173

218174

219175
def load_class_names():

download.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
########################################################################
2+
#
3+
# Functions for downloading and extracting data-files from the internet.
4+
#
5+
# Implemented in Python 3.5
6+
#
7+
########################################################################
8+
#
9+
# This file is part of the TensorFlow Tutorials available at:
10+
#
11+
# https://github.com/Hvass-Labs/TensorFlow-Tutorials
12+
#
13+
# Published under the MIT License. See the file LICENSE for details.
14+
#
15+
# Copyright 2016 by Magnus Erik Hvass Pedersen
16+
#
17+
########################################################################
18+
19+
import sys
20+
import os
21+
import urllib.request
22+
import tarfile
23+
24+
########################################################################
25+
26+
27+
def _print_download_progress(count, block_size, total_size):
28+
"""
29+
Function used for printing the download progress.
30+
Used as a call-back function in maybe_download_and_extract().
31+
"""
32+
33+
# Percentage completion.
34+
pct_complete = float(count * block_size) / total_size
35+
36+
# Status-message. Note the \r which means the line should overwrite itself.
37+
msg = "\r- Download progress: {0:.1%}".format(pct_complete)
38+
39+
# Print it.
40+
sys.stdout.write(msg)
41+
sys.stdout.flush()
42+
43+
44+
########################################################################
45+
46+
47+
def maybe_download_and_extract(url, download_dir):
48+
"""
49+
Download and extract the data if it doesn't already exist.
50+
Assumes the url is a tar-ball file.
51+
52+
:param url:
53+
Internet URL for the tar-file to download.
54+
Example: "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
55+
56+
:param download_dir:
57+
Directory where the downloaded file is saved.
58+
Example: "data/CIFAR-10/"
59+
60+
:return:
61+
Nothing.
62+
"""
63+
64+
# Filename for saving the file downloaded from the internet.
65+
# Use the filename from the URL and add it to the download_dir.
66+
filename = url.split('/')[-1]
67+
file_path = os.path.join(download_dir, filename)
68+
69+
# Check if the file already exists.
70+
# If it exists then we assume it has also been extracted,
71+
# otherwise we need to download and extract it now.
72+
if not os.path.exists(file_path):
73+
# Check if the download directory exists, otherwise create it.
74+
if not os.path.exists(download_dir):
75+
os.makedirs(download_dir)
76+
77+
# Download the file from the internet.
78+
file_path, _ = urllib.request.urlretrieve(url=url,
79+
filename=file_path,
80+
reporthook=_print_download_progress)
81+
82+
print()
83+
print("Download finished. Extracting files.")
84+
85+
# Unpack the tar-ball.
86+
tarfile.open(name=file_path, mode="r:gz").extractall(download_dir)
87+
88+
print("Done.")
89+
else:
90+
print("Data has apparently already been downloaded and unpacked.")
91+
92+
93+
########################################################################

images/07_inception_flowchart.png

339 KB
Loading

images/elon_musk.jpg

23.8 KB
Loading

images/elon_musk_100x100.jpg

6.4 KB
Loading

images/parrot.jpg

69.8 KB
Loading

images/parrot_cropped1.jpg

36.2 KB
Loading

0 commit comments

Comments
 (0)