Skip to content

Commit

Permalink
Merge pull request #5 from dvgodoy/ch4
Browse files Browse the repository at this point in the history
Ch4
  • Loading branch information
dvgodoy authored Nov 4, 2020
2 parents f256ad3 + 48cc9ff commit 64e12d9
Show file tree
Hide file tree
Showing 10 changed files with 3,230 additions and 24 deletions.
2,913 changes: 2,913 additions & 0 deletions Chapter04.ipynb

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ You can easily **load the notebooks directly from GitHub** using Colab and run t

You can go through the chapters already using the links below:

- [Chapter 0](https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter00.ipynb)
- [Chapter 1](https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter01.ipynb)
- [Chapter 2](https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter02.ipynb)
- [Chapter 2.1](https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter02.1.ipynb)
- [Chapter 3](https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter03.ipynb)
- [Chapter 0 - Visualizing Gradient Descent](https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter00.ipynb)
- [Chapter 1 - A Simple Regression Problem](https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter01.ipynb)
- [Chapter 2 - Rethinking the Training Loop](https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter02.ipynb)
- [Chapter 2.1 - Going Classy](https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter02.1.ipynb)
- [Chapter 3 - A Simple Classification Problem](https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter03.ipynb)
- [Chapter 4 - Classifying Images](https://colab.research.google.com/github/dvgodoy/PyTorchStepByStep/blob/master/Chapter04.ipynb)

### Binder

Expand Down
43 changes: 25 additions & 18 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ def tensorboard_cleanup():
2: ['plots', 'data_generation', 'data_preparation', 'model_configuration', 'model_training'],
21: ['plots', 'data_generation', 'data_preparation', 'model_configuration', 'stepbystep'],
3: ['plots', 'stepbystep'],
4: ['plots', 'stepbystep', 'data_generation'],
}
FILENAMES = {
0: ['chapter0.py'],
1: ['chapter1.py'],
2: ['chapter2.py', 'simple_linear_regression.py', 'v0.py', 'v0.py', 'v0.py'],
21: ['chapter2_1.py', 'simple_linear_regression.py', 'v2.py', '', 'v0.py'],
3: ['chapter3.py', 'v0.py'],
}
4: ['chapter4.py', 'v0.py', 'image_classification.py'],}

try:
host = os.environ['BINDER_SERVICE_HOST']
Expand All @@ -40,8 +41,8 @@ def tensorboard_cleanup():

IS_LOCAL = (not IS_BINDER) and (not IS_COLAB)

def download_to_colab(chapter):
base_url = 'https://raw.githubusercontent.com/dvgodoy/PyTorchStepByStep/master/'
def download_to_colab(chapter, branch='master'):
base_url = 'https://raw.githubusercontent.com/dvgodoy/PyTorchStepByStep/{}/'.format(branch)

folders = FOLDERS[chapter]
filenames = FILENAMES[chapter]
Expand All @@ -53,10 +54,10 @@ def download_to_colab(chapter):
raise

if len(filename):
path = os.path.join(folder, filename)
url = '{}{}'.format(base_url, path)
r = requests.get(url, allow_redirects=True)
open(path, 'wb').write(r.content)
path = os.path.join(folder, filename)
url = '{}{}'.format(base_url, path)
r = requests.get(url, allow_redirects=True)
open(path, 'wb').write(r.content)

try:
os.mkdir('runs')
Expand All @@ -75,18 +76,18 @@ def download_to_colab(chapter):
</script>
''')

def config_chapter0():
def config_chapter0(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(0)
download_to_colab(0, branch)
print('Finished!')

def config_chapter1():
def config_chapter1(branch='master'):
if IS_COLAB:
print('Installing torchviz...')
subprocess.run([sys.executable, '-m', 'pip', 'install', 'torchviz'])
print('Downloading files from GitHub repo to Colab...')
download_to_colab(1)
download_to_colab(1, branch)
print('Creating folders...')
folders = ['data_preparation', 'model_configuration', 'model_training']

Expand All @@ -99,20 +100,26 @@ def config_chapter1():
raise
print('Finished!')

def config_chapter2():
def config_chapter2(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(2, branch)
print('Finished!')

def config_chapter2_1(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(2)
download_to_colab(21, branch)
print('Finished!')

def config_chapter2_1():
def config_chapter3(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(21)
download_to_colab(3, branch)
print('Finished!')

def config_chapter3():
def config_chapter4(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(3)
print('Finished!')
download_to_colab(4, branch)
print('Finished!')
56 changes: 56 additions & 0 deletions data_generation/image_classification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import numpy as np


def gen_img(start, target, fill=1, img_size=10):
# Generates empty image
img = np.zeros((img_size, img_size), dtype=np.float)

start_row, start_col = None, None

if start > 0:
start_row = start
else:
start_col = np.abs(start)

if target == 0:
if start_row is None:
img[:, start_col] = fill
else:
img[start_row, :] = fill
else:
if start_col == 0:
start_col = 1

if target == 1:
if start_row is not None:
up = (range(start_row, -1, -1),
range(0, start_row + 1))
else:
up = (range(img_size - 1, start_col - 1, -1),
range(start_col, img_size))
img[up] = fill
else:
if start_row is not None:
down = (range(start_row, img_size, 1),
range(0, img_size - start_row))
else:
down = (range(0, img_size - 1 - start_col + 1),
range(start_col, img_size))
img[down] = fill

return 255 * img.reshape(1, img_size, img_size)


def generate_dataset(img_size=10, n_images=100, binary=True, seed=17):
np.random.seed(seed)

starts = np.random.randint(-(img_size - 1), img_size, size=(n_images,))
targets = np.random.randint(0, 3, size=(n_images,))

images = np.array([gen_img(s, t, img_size=img_size)
for s, t in zip(starts, targets)], dtype=np.uint8)

if binary:
targets = (targets > 0).astype(np.int)

return images, targets
33 changes: 32 additions & 1 deletion helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,35 @@ def perform_val_step(x, y):
# There is no need to compute Steps 3 and 4, since we don't update parameters during evaluation
return loss.item()

return perform_val_step
return perform_val_step

def index_splitter(n, splits, seed=13):
idx = torch.arange(n)
# Makes the split argument a tensor
splits_tensor = torch.as_tensor(splits)
# Finds the correct multiplier, so we don't have
# to worry about summing up to N (or one)
multiplier = n / splits_tensor.sum()
splits_tensor = (multiplier * splits_tensor).long()
# If there is a difference, throws at the first split
# so random_split does not complain
diff = n - splits_tensor.sum()
splits_tensor[0] += diff
# Uses PyTorch random_split to split the indices
torch.manual_seed(seed)
return random_split(idx, splits_tensor)

def make_balanced_sampler(y):
# Computes weights for compensating imbalanced classes
classes, counts = y.unique(return_counts=True)
weights = 1.0 / counts.float()
sample_weights = weights[y.squeeze().long()]
# Builds sampler with compute weights
generator = torch.Generator()
sampler = WeightedRandomSampler(
weights=sample_weights,
num_samples=len(sample_weights),
generator=generator,
replacement=True
)
return sampler
Binary file added images/book4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/classification.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/classification_equiv.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/classification_relu2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 64e12d9

Please sign in to comment.