Skip to content

Commit

Permalink
Handle activations in saved file, handle args in run.py
Browse files Browse the repository at this point in the history
  • Loading branch information
aunetx committed Dec 3, 2019
1 parent 6bfc026 commit da58c00
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion scripts/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def arctan_prime(y) -> np.ndarray:
return 1 / y**2 + 1


def listToActivations(activations_list, architecture):
def listToActivations(activations_list, architecture) -> list:
activations_fn = []
activations_prime = []
for id, _ in enumerate(architecture):
Expand Down
4 changes: 2 additions & 2 deletions scripts/loulou.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ def train(weights: list, trX: np.ndarray, trY: np.ndarray, teX: np.ndarray, teY:
if save_timeout > 0:
if i % save_timeout == 0:
temp_filename = '../trains/temp/' + \
filename + '_epoch_' + str(i) + '.npy'
filename + '_epoch_' + str(i) + '.npz'
temp_filename = os.path.join(path, temp_filename)
utils.save(weights, activations_fn,
temp_filename, reduce_output)

# Save final file
if filename:
filename = os.path.join(path, '../trains/' + filename + '.npy')
filename = os.path.join(path, '../trains/' + filename + '.npz')
utils.save(weights, activations_fn, filename, reduce_output)

return accuracy
Expand Down
69 changes: 46 additions & 23 deletions scripts/run.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,67 @@
#!/usr/bin/env python3

import matplotlib.image as image
import numpy as np
import argparse
import sys
import matplotlib.image as image

from loulou import feed_forward
from utils import convertJson
import activations

if __name__ == '__main__':
# Handling errors for bad arguments
try:
assert len(sys.argv) == 3
except AssertionError:
print("Error ! Please give two arguments : path to weights file and to image to predict.")
exit()
verbosity = 0

parser = argparse.ArgumentParser(
description='Utility to run a loulou-based neural network.')
parser.add_argument('-f', '--file', dest='file', type=str, required=True,
help='path to the `.npz` training file')
parser.add_argument('-i', '--image', dest='image', type=str, required=True,
help='path to the image to predict')
parser.add_argument('-v', '--verbosity', dest='verbosity', action="count",
help='add verbosity to the output (you can type several)')
parser.add_argument('-j', '--return-json', dest='return_json', action="store_true",
help='print the prediction and hot vector in a json format')
args = parser.parse_args()

# Loading weights matrix
filename = sys.argv[1]
if args.verbosity is not None:
verbosity = args.verbosity

# Load weights and activations from file
try:
file = np.load(filename)
# TODO change behavior to allow_pickle=False for security
file = np.load(args.file, allow_pickle=True)
weights = file['weights']
activations = file['activations']
activations_names = file['activations']

activations_fn = activations.listToActivations(
activations_names, weights)[0]
if verbosity > 0:
print('Activations used :', activations_names)

except FileNotFoundError:
print(
"Error ! Weights matrix file could not be opened, please check that it exists.")
print("Fichier : ", filename)
'Error ! File ['+args.file+'] could not be opened, please check that it exists.')
exit()

# Loading image data
img = sys.argv[2]
# Load image data
try:
img = image.imread(img)
img = image.imread(args.image)
except FileNotFoundError:
print("Error ! Image could not be opened, please check that it exists.")
print("Image : ", img)
print(
'Error ! Image ['+args.image+'] could not be opened, please check that it exists.')
exit()

# Shaping image onto matrix
# Convert image to matrix
topred = 1 - img.reshape(784, 4).mean(axis=1)
# Making prediction
prediction = feed_forward(topred, weights, activations)[-1]
# Printing json output
print(convertJson(prediction))
# Make the prediction
prediction = feed_forward(topred, weights, activations_fn)[-1]
# Print final output
if args.return_json:
print(convertJson(prediction))
else:
if verbosity > 0:
print('Hot ones vector :', list(prediction))
print('Final prediction :', prediction.argmax())
else:
print(prediction.argmax())
5 changes: 3 additions & 2 deletions scripts/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
# Parser
parser = argparse.ArgumentParser(
description='Utility to train a loulou-based neural network.')
# TODO set option to choose path
parser.add_argument('-f', '--filename', dest='filename', type=str,
help='name of the file to write, extension added automatically (default : none)') # Need to set option for other paths
help='name of the file to write, extension added automatically (default : none)')
parser.add_argument('-e', '--epochs', dest='epochs', type=int,
help='number of epochs (default : 15, -1 for infinity)')
parser.add_argument('-b', '--batch-size', dest='batch_size', type=int,
Expand All @@ -38,7 +39,7 @@
parser.add_argument('-r', '--reduce-output', dest='reduce_output', action="count",
help='reduce verbosity of output (you can type several)')
parser.add_argument('-j', '--return-json', dest='return_json', action="store_true",
help='finally, print the progression of accuracy in a json format')
help='print the progression of accuracy in a json format')
args = parser.parse_args()

if args.epochs is not None:
Expand Down
12 changes: 8 additions & 4 deletions scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
import json


def save(weights: list, activations_list: list, filename: str, reduce_output: int) -> None:
np.save(filename, {'weights': weights,
'activations': activations_list})
def save(weights: list, activations_fn_list: list, filename: str, reduce_output: int) -> None:
activations_names_list = []
for act in activations_fn_list:
activations_names_list.append(act.__name__)

np.savez_compressed(filename, weights=weights,
activations=activations_names_list)
if reduce_output < 2:
print('Data saved successfully into ', filename)


def convertJson(pred: np.ndarray) -> str:
out = {}
out['hot_prediction'] = list(pred)
out['prediction'] = int(np.argmax(pred))
out['prediction'] = int(pred.argmax())
return json.dumps(out)


Expand Down
2 changes: 0 additions & 2 deletions todo.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# To-do

- Update README
- Handle args in `run.py` just like in `train.py`
- Write descriptions for functions
- Handle others types of data
- Save trainings to an archive with datas : `weights matrix`, `activations arch`, [`hyperparameters`], [`accuracy`]
- Rewrite as object ? (OOP)

0 comments on commit da58c00

Please sign in to comment.