├── floyd_requirements.txt ├── _config.yml ├── softmax.png ├── from_web ├── 1.jpg ├── 2.jpg ├── 3.jpg ├── 4.jpg └── 5.jpg ├── images ├── chart.jpg ├── softmax.png └── tensorboard.png ├── floyd_run.txt ├── .floydignore ├── settings └── hyperparameters.json ├── data_handler.py ├── signnames.csv ├── logger.py ├── utils.py ├── .gitignore ├── test_web_images.py ├── README.md ├── test.py ├── train.py ├── caps_net.py ├── LICENSE ├── model_base.py └── model.py /floyd_requirements.txt: -------------------------------------------------------------------------------- 1 | docopt 2 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /softmax.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thibo73800/capsnet-traffic-sign-classifier/master/softmax.png -------------------------------------------------------------------------------- /from_web/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thibo73800/capsnet-traffic-sign-classifier/master/from_web/1.jpg -------------------------------------------------------------------------------- /from_web/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thibo73800/capsnet-traffic-sign-classifier/master/from_web/2.jpg -------------------------------------------------------------------------------- /from_web/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thibo73800/capsnet-traffic-sign-classifier/master/from_web/3.jpg -------------------------------------------------------------------------------- /from_web/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thibo73800/capsnet-traffic-sign-classifier/master/from_web/4.jpg -------------------------------------------------------------------------------- /from_web/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thibo73800/capsnet-traffic-sign-classifier/master/from_web/5.jpg -------------------------------------------------------------------------------- /images/chart.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thibo73800/capsnet-traffic-sign-classifier/master/images/chart.jpg -------------------------------------------------------------------------------- /images/softmax.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thibo73800/capsnet-traffic-sign-classifier/master/images/softmax.png -------------------------------------------------------------------------------- /floyd_run.txt: -------------------------------------------------------------------------------- 1 | floyd run --gpu --data thibo73800/datasets/trafic_sign/1:/datasets 'python train.py /datasets /output' 2 | -------------------------------------------------------------------------------- /images/tensorboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thibo73800/capsnet-traffic-sign-classifier/master/images/tensorboard.png -------------------------------------------------------------------------------- /.floydignore: -------------------------------------------------------------------------------- 1 | 2 | # Directories and files to ignore when uploading code to floyd 3 | 4 | images/* 5 | dataset/* 6 | outputs/* 7 | .git 8 | .eggs 9 | eggs 10 | lib 11 | lib64 12 | parts 13 | sdist 14 | var 15 | *.pyc 16 | *.swp 17 | .DS_Store 18 | -------------------------------------------------------------------------------- /settings/hyperparameters.json: -------------------------------------------------------------------------------- 1 | { 2 | "conv_1_size": 9, 3 | "conv_1_nb": 256, 4 | "conv_2_size": 6, 5 | "conv_2_nb": 64, 6 | "conv_2_dropout": 0.7, 7 | "caps_1_vec_len": 16, 8 | "caps_1_size": 5, 9 | "caps_1_nb_filter": 16, 10 | "caps_2_vec_len": 32, 11 | "learning_rate": 0.0001, 12 | "routing_steps": 1 13 | } 14 | -------------------------------------------------------------------------------- /data_handler.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import pickle 6 | 7 | TRAIN_FILE = "train.p" 8 | VALID_FILE = "valid.p" 9 | TEST_FILE = "test.p" 10 | 11 | def get_data(folder): 12 | """ 13 | Load traffic sign data 14 | **input: ** 15 | *folder: (String) Path to the dataset folder 16 | """ 17 | # Load the dataset 18 | training_file = os.path.join(folder, TRAIN_FILE) 19 | validation_file= os.path.join(folder, VALID_FILE) 20 | testing_file = os.path.join(folder, TEST_FILE) 21 | 22 | with open(training_file, mode='rb') as f: 23 | train = pickle.load(f) 24 | with open(validation_file, mode='rb') as f: 25 | valid = pickle.load(f) 26 | with open(testing_file, mode='rb') as f: 27 | test = pickle.load(f) 28 | 29 | # Retrive all datas 30 | X_train, y_train = train['features'], train['labels'] 31 | X_valid, y_valid = valid['features'], valid['labels'] 32 | X_test, y_test = test['features'], test['labels'] 33 | 34 | return X_train, y_train, X_valid, y_valid, X_test, y_test 35 | -------------------------------------------------------------------------------- /signnames.csv: -------------------------------------------------------------------------------- 1 | ClassId,SignName 2 | 0,Speed limit (20km/h) 3 | 1,Speed limit (30km/h) 4 | 2,Speed limit (50km/h) 5 | 3,Speed limit (60km/h) 6 | 4,Speed limit (70km/h) 7 | 5,Speed limit (80km/h) 8 | 6,End of speed limit (80km/h) 9 | 7,Speed limit (100km/h) 10 | 8,Speed limit (120km/h) 11 | 9,No passing 12 | 10,No passing for vehicles over 3.5 metric tons 13 | 11,Right-of-way at the next intersection 14 | 12,Priority road 15 | 13,Yield 16 | 14,Stop 17 | 15,No vehicles 18 | 16,Vehicles over 3.5 metric tons prohibited 19 | 17,No entry 20 | 18,General caution 21 | 19,Dangerous curve to the left 22 | 20,Dangerous curve to the right 23 | 21,Double curve 24 | 22,Bumpy road 25 | 23,Slippery road 26 | 24,Road narrows on the right 27 | 25,Road work 28 | 26,Traffic signals 29 | 27,Pedestrians 30 | 28,Children crossing 31 | 29,Bicycles crossing 32 | 30,Beware of ice/snow 33 | 31,Wild animals crossing 34 | 32,End of all speed and passing limits 35 | 33,Turn right ahead 36 | 34,Turn left ahead 37 | 35,Ahead only 38 | 36,Go straight or right 39 | 37,Go straight or left 40 | 38,Keep right 41 | 39,Keep left 42 | 40,Roundabout mandatory 43 | 41,End of no passing 44 | 42,End of no passing by vehicles over 3.5 metric tons 45 | -------------------------------------------------------------------------------- /logger.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import logging 5 | from logging.handlers import RotatingFileHandler 6 | 7 | logger = logging.getLogger() 8 | logger.setLevel(logging.DEBUG) 9 | formatter = logging.Formatter('%(asctime)s:: %(levelname)s:: %(message)s') 10 | file_handler = RotatingFileHandler('dory_ai.log', 'a', 1000000, 1) 11 | file_handler.setLevel(logging.INFO) 12 | file_handler.setFormatter(formatter) 13 | logger.addHandler(file_handler) 14 | stream_handler = logging.StreamHandler() 15 | stream_handler.setLevel(logging.DEBUG) 16 | logger.addHandler(stream_handler) 17 | 18 | 19 | class Logger(object): 20 | 21 | def __init__(self, label): 22 | super(Logger, self).__init__() 23 | self.label = label 24 | self.logger = logger 25 | 26 | def debug(self, string): 27 | self.logger.debug("%s::%s" % (self.label, string)) 28 | 29 | def info(self, string): 30 | self.logger.info("%s::%s" % (self.label, string)) 31 | 32 | def warning(self, string): 33 | self.logger.warning("%s::%s" % (self.label, string)) 34 | 35 | def error(self, string): 36 | self.logger.error("%s::%s" % (self.label, string)) 37 | 38 | def critical(self, string): 39 | self.logger.critical("%s::%s" % (self.label, string)) 40 | -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | import numpy as np 4 | import json 5 | import sys 6 | import os 7 | 8 | 9 | class Utils(object): 10 | """ 11 | Util class to store all common method use in this project 12 | """ 13 | 14 | def __init__(self, arg): 15 | super(Utils, self).__init__() 16 | 17 | @staticmethod 18 | def progress(count, total, suffix=''): 19 | """ 20 | Utils method to display a progress bar 21 | **input: ** 22 | *count: current progression 23 | *total: Max progress bar length 24 | """ 25 | bar_len = 60 26 | filled_len = int(round(bar_len * count / float(total))) 27 | 28 | percents = round(100.0 * count / float(total), 1) 29 | bar = '=' * filled_len + '-' * (bar_len - filled_len) 30 | 31 | sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', suffix)) 32 | sys.stdout.flush() 33 | 34 | @staticmethod 35 | def read_json_file(path): 36 | """ 37 | Utils method to open, read and return a json file content 38 | **input: ** 39 | *path: (String) Path to the json file to read 40 | """ 41 | with open(path, "r") as f: 42 | json_content = json.loads(f.read()) 43 | return json_content 44 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | outputs/* 7 | dataset/* 8 | 9 | # C extensions 10 | *.so 11 | 12 | # Distribution / packaging 13 | .Python 14 | env/ 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | downloads/ 19 | eggs/ 20 | .eggs/ 21 | lib/ 22 | lib64/ 23 | parts/ 24 | sdist/ 25 | var/ 26 | wheels/ 27 | *.egg-info/ 28 | .installed.cfg 29 | *.egg 30 | 31 | # PyInstaller 32 | # Usually these files are written by a python script from a template 33 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 34 | *.manifest 35 | *.spec 36 | 37 | # Installer logs 38 | pip-log.txt 39 | pip-delete-this-directory.txt 40 | 41 | # Unit test / coverage reports 42 | htmlcov/ 43 | .tox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | .hypothesis/ 51 | 52 | # Translations 53 | *.mo 54 | *.pot 55 | 56 | # Django stuff: 57 | *.log 58 | local_settings.py 59 | 60 | # Flask stuff: 61 | instance/ 62 | .webassets-cache 63 | 64 | # Scrapy stuff: 65 | .scrapy 66 | 67 | # Sphinx documentation 68 | docs/_build/ 69 | 70 | # PyBuilder 71 | target/ 72 | 73 | # Jupyter Notebook 74 | .ipynb_checkpoints 75 | 76 | # pyenv 77 | .python-version 78 | 79 | # celery beat schedule file 80 | celerybeat-schedule 81 | 82 | # SageMath parsed files 83 | *.sage.py 84 | 85 | # dotenv 86 | .env 87 | 88 | # virtualenv 89 | .venv 90 | venv/ 91 | ENV/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /test_web_images.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Test the model 6 | 7 | Usage: 8 | test.py 9 | 10 | Options: 11 | -h --help Show this help. 12 | Dataset folder 13 | Path to the checkpoints to restore 14 | """ 15 | 16 | from docopt import docopt 17 | import tensorflow as tf 18 | import matplotlib.pyplot as plt 19 | from PIL import Image 20 | import numpy as np 21 | import random 22 | import pickle 23 | import os 24 | 25 | from model import ModelTrafficSign 26 | from data_handler import get_data 27 | 28 | def test_web_images(dataset, ckpt): 29 | """ 30 | Test images located into the "from_web" folder. 31 | **input: ** 32 | *dataset: (String) Dataset folder to used 33 | *ckpt: (String) [Optional] Path to the ckpt file to restore 34 | """ 35 | 36 | # Load name of id 37 | with open("signnames.csv", "r") as f: 38 | signnames = f.read() 39 | id_to_name = { int(line.split(",")[0]):line.split(",")[1] for line in signnames.split("\n")[1:] if len(line) > 0} 40 | 41 | images = [] 42 | 43 | # Read all image into the folder 44 | for filename in os.listdir("from_web"): 45 | img = Image.open(os.path.join("from_web", filename)) 46 | img = img.resize((32, 32)) 47 | img = np.array(img) / 255 48 | images.append(img) 49 | 50 | # Load the model 51 | model = ModelTrafficSign("TrafficSign", output_folder=None) 52 | model.load(ckpt) 53 | 54 | # Get the prediction 55 | predictions = model.predict(images) 56 | 57 | # Plot the result 58 | fig, axs = plt.subplots(5, 2, figsize=(10, 25)) 59 | axs = axs.ravel() 60 | for i in range(10): 61 | if i%2 == 0: 62 | axs[i].axis('off') 63 | axs[i].imshow(images[i // 2]) 64 | axs[i].set_title("Prediction: %s" % id_to_name[np.argmax(predictions[i // 2])]) 65 | else: 66 | axs[i].bar(np.arange(43), predictions[i // 2]) 67 | axs[i].set_ylabel("Softmax") 68 | axs[i].set_xlabel("Labels") 69 | 70 | plt.show() 71 | 72 | 73 | if __name__ == '__main__': 74 | arguments = docopt(__doc__) 75 | test_web_images(arguments[""], arguments[""]) 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Capsnet - Traffic sign classifier - Tensorflow 2 | 3 | A Tensorflow implementation of CapsNet(Capsules Net) apply on the German traffic sign dataset 4 | 5 | [![Contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=plastic)](CONTRIBUTING.md) 6 | [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg?style=plastic)](https://opensource.org/licenses/Apache-2.0) 7 | ![completion](https://img.shields.io/badge/completion%20state-80%25-blue.svg?style=plastic) 8 | 9 | This implementation is based on this paper: Dynamic Routing Between Capsules (https://arxiv.org/abs/1710.09829) from Sara Sabour, Nicholas Frosst and Geoffrey E. Hinton. 10 | 11 | This repository is a work in progress implementation of a Capsules Net. Since I am using a different dataset (Not MNIST) some details in the architecture are different. The code for the CapsNet is located in the following file: caps_net.py while the whole model is created inside the model.py file. The two main methods used to build the CapsNet are conv_caps_layer and fully_connected_caps_layer 12 | 13 | 14 | 15 | ## Requirements 16 | - Python 3 17 | - NumPy 1.13.1 18 | - Tensorflow 1.3.0 19 | - docopt 0.6.2 20 | - Sklearn: 0.18.1 21 | - Matplotlib 22 | 23 | ## Install 24 | 25 | $> git clone https://github.com/thibo73800/capsnet_traffic_sign_classifier.git 26 | $> cd capsnet_traffic_sign_classifier.git 27 | $> wget https://d17h27t6h515a5.cloudfront.net/topher/2017/February/5898cd6f_traffic-signs-data/traffic-signs-data.zip 28 | $> unzip traffic-signs-data.zip 29 | $> mkdir dataset 30 | $> mv *.p dataset/ 31 | $> rm traffic-signs-data.zip 32 | 33 | ## Train 34 | 35 | $> python train.py -h 36 | $> python train.py dataset/ 37 | 38 | During the training, the checkpoint is saved by default into the outputs/checkpoints/ folder. The exact path and name of the checkpoint is print during the training. 39 | 40 | ## Test 41 | 42 | In order to measure the accuracy and the loss on the Test dataset you need to used the test.py script as follow: 43 | 44 | $> python test.py outputs/checkpoints/ckpt_name dataset/ 45 | 46 | ## Metrics / Tensorboard 47 | 48 | Accuracy: 49 |
    50 |
  • Train: 99%
  • 51 |
  • Validation: 98%
  • 52 |
  • Test: 97%
  • 53 |
54 | 55 | Checkpoints and tensorboard files are stored inside the outputs folder. 56 | 57 | 58 | 59 | Exemple of some prediction: 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Test the model 6 | 7 | Usage: 8 | test.py 9 | 10 | Options: 11 | -h --help Show this help. 12 | Dataset folder 13 | Path to the checkpoints to restore 14 | """ 15 | 16 | from docopt import docopt 17 | import matplotlib.pyplot as plt 18 | from sklearn.metrics import confusion_matrix 19 | import itertools 20 | import tensorflow as tf 21 | import numpy as np 22 | import random 23 | import pickle 24 | import os 25 | 26 | from model import ModelTrafficSign 27 | from data_handler import get_data 28 | 29 | 30 | def plot_confusion_matrix(cm, classes, normalize=True, title='Confusion matrix', cmap=plt.cm.Blues): 31 | """ 32 | This function prints and plots the confusion matrix. 33 | Normalization can be applied by setting `normalize=True`. 34 | """ 35 | if normalize: 36 | cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] 37 | print("Normalized confusion matrix") 38 | else: 39 | print('Confusion matrix, without normalization') 40 | 41 | print(cm) 42 | 43 | plt.imshow(cm, interpolation='nearest', cmap=cmap) 44 | plt.title(title) 45 | plt.colorbar() 46 | tick_marks = np.arange(len(classes)) 47 | plt.xticks(tick_marks, classes, rotation=45) 48 | plt.yticks(tick_marks, classes) 49 | 50 | fmt = '.2f' if normalize else 'd' 51 | thresh = cm.max() / 2. 52 | for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): 53 | plt.text(j, i, format(cm[i, j], fmt), 54 | horizontalalignment="center", 55 | color="white" if cm[i, j] > thresh else "black") 56 | 57 | plt.tight_layout() 58 | plt.ylabel('True label') 59 | plt.xlabel('Predicted label') 60 | 61 | 62 | def test(dataset, ckpt): 63 | """ 64 | Train the model 65 | **input: ** 66 | *dataset: (String) Dataset folder to used 67 | *ckpt: (String) [Optional] Path to the ckpt file to restore 68 | """ 69 | 70 | # Load name of id 71 | with open("signnames.csv", "r") as f: 72 | signnames = f.read() 73 | id_to_name = { int(line.split(",")[0]):line.split(",")[1] for line in signnames.split("\n")[1:] if len(line) > 0} 74 | 75 | # Get Test dataset 76 | _, _, _, _, X_test, y_test = get_data(dataset) 77 | X_test = X_test / 255 78 | 79 | model = ModelTrafficSign("TrafficSign", output_folder=None) 80 | # Load the model 81 | model.load(ckpt) 82 | 83 | # Evaluate all the dataset 84 | loss, acc, predicted_class = model.evaluate_dataset(X_test, y_test) 85 | 86 | print("Accuracy = ", acc) 87 | print("Loss = ", loss) 88 | 89 | # Get the confusion matrix 90 | cnf_matrix = confusion_matrix(y_test, predicted_class) 91 | 92 | # Plot the confusion matrix 93 | plt.figure() 94 | plot_confusion_matrix(cnf_matrix, classes=[str(i) for i in range(43)], title='Confusion matrix, without normalization') 95 | 96 | plt.show() 97 | 98 | if __name__ == '__main__': 99 | arguments = docopt(__doc__) 100 | test(arguments[""], arguments[""]) 101 | -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Train the model. 6 | 7 | Usage: 8 | train.py [] [--ckpt=] 9 | 10 | Options: 11 | -h --help Show this help. 12 | Dataset folder 13 | Ouput folder. By default: ./outputs/ 14 | Path to the checkpoints to restore 15 | """ 16 | 17 | from keras.preprocessing.image import ImageDataGenerator 18 | from PIL import Image 19 | from PIL import Image, ImageEnhance 20 | from docopt import docopt 21 | import tensorflow as tf 22 | import numpy as np 23 | import random 24 | import pickle 25 | import os 26 | 27 | from model import ModelTrafficSign 28 | from data_handler import get_data 29 | 30 | 31 | BATCH_SIZE = 50 32 | DATASET_FOLDER = "dataset/" 33 | 34 | 35 | def train(dataset, ckpt=None, output=None): 36 | """ 37 | Train the model 38 | **input: ** 39 | *dataset: (String) Dataset folder to used 40 | *ckpt: (String) [Optional] Path to the ckpt file to restore 41 | *output: (String) [Optional] Path to the output folder to used. ./outputs/ by default 42 | """ 43 | 44 | def preprocessing_function(img): 45 | """ 46 | Custom preprocessing_function 47 | """ 48 | img = img * 255 49 | img = Image.fromarray(img.astype('uint8'), 'RGB') 50 | img = ImageEnhance.Brightness(img).enhance(random.uniform(0.6, 1.5)) 51 | img = ImageEnhance.Contrast(img).enhance(random.uniform(0.6, 1.5)) 52 | 53 | return np.array(img) / 255 54 | 55 | X_train, y_train, X_valid, y_valid, X_test, y_test = get_data(dataset) 56 | 57 | X_train = X_train / 255 58 | X_valid = X_valid / 255 59 | X_test = X_test / 255 60 | 61 | train_datagen = ImageDataGenerator() 62 | train_datagen_augmented = ImageDataGenerator( 63 | rotation_range=20, 64 | shear_range=0.2, 65 | width_shift_range=0.2, 66 | height_shift_range=0.2, 67 | horizontal_flip=True, 68 | preprocessing_function=preprocessing_function) 69 | inference_datagen = ImageDataGenerator() 70 | train_datagen.fit(X_train) 71 | train_datagen_augmented.fit(X_train) 72 | inference_datagen.fit(X_valid) 73 | inference_datagen.fit(X_test) 74 | 75 | # Utils method to print the current progression 76 | def plot_progression(b, cost, acc, label): print( 77 | "[%s] Batch ID = %s, loss = %s, acc = %s" % (label, b, cost, acc)) 78 | 79 | # Init model 80 | model = ModelTrafficSign("TrafficSign", output_folder=output) 81 | if ckpt is None: 82 | model.init() 83 | else: 84 | model.load(ckpt) 85 | 86 | # Training pipeline 87 | b = 0 88 | valid_batch = inference_datagen.flow(X_valid, y_valid, batch_size=BATCH_SIZE) 89 | best_validation_loss = None 90 | augmented_factor = 0.99 91 | decrease_factor = 0.80 92 | train_batches = train_datagen.flow(X_train, y_train, batch_size=BATCH_SIZE) 93 | augmented_train_batches = train_datagen_augmented.flow(X_train, y_train, batch_size=BATCH_SIZE) 94 | 95 | while True: 96 | next_batch = next( 97 | augmented_train_batches if random.uniform(0, 1) < augmented_factor else train_batches) 98 | x_batch, y_batch = next_batch 99 | 100 | ### Training 101 | cost, acc = model.optimize(x_batch, y_batch) 102 | ### Validation 103 | x_batch, y_batch = next(valid_batch, None) 104 | # Retrieve the cost and acc on this validation batch and save it in tensorboard 105 | cost_val, acc_val = model.evaluate(x_batch, y_batch, tb_test_save=True) 106 | 107 | if b % 10 == 0: # Plot the last results 108 | plot_progression(b, cost, acc, "Train") 109 | plot_progression(b, cost_val, acc_val, "Validation") 110 | if b % 1000 == 0: # Test the model on all the validation 111 | print("Evaluate full validation dataset ...") 112 | loss, acc, _ = model.evaluate_dataset(X_valid, y_valid) 113 | print("Current loss: %s Best loss: %s" % (loss, best_validation_loss)) 114 | plot_progression(b, loss, acc, "TOTAL Validation") 115 | if best_validation_loss is None or loss < best_validation_loss: 116 | best_validation_loss = loss 117 | model.save() 118 | augmented_factor = augmented_factor * decrease_factor 119 | print("Augmented Factor = %s" % augmented_factor) 120 | 121 | b += 1 122 | 123 | if __name__ == '__main__': 124 | arguments = docopt(__doc__) 125 | train(arguments[""], arguments["--ckpt"], arguments[""]) 126 | -------------------------------------------------------------------------------- /caps_net.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tensorflow as tf 3 | import numpy as np 4 | 5 | 6 | def conv_caps_layer(input_layer, capsules_size, nb_filters, kernel, stride=2): 7 | """ 8 | Capsule layer for the convolutional inputs 9 | **input: 10 | *input_layer: (Tensor) 11 | *capsule_numbers: (Integer) the number of capsule in this layer. 12 | *kernel_size: (Integer) Size of the kernel for each filter. 13 | *stride: (Integer) 2 by default 14 | """ 15 | # "In convolutional capsule layers each unit in a capsule is a convolutional unit. 16 | # Therefore, each capsule will output a grid of vectors rather than a single vector output." 17 | capsules = tf.contrib.layers.conv2d( 18 | input_layer, nb_filters * capsules_size, kernel, stride, padding="VALID") 19 | # conv shape: [?, kernel, kernel, nb_filters] 20 | shape = capsules.get_shape().as_list() 21 | capsules = tf.reshape(capsules, shape=(-1, np.prod(shape[1:3]) * nb_filters, capsules_size, 1)) 22 | # capsules shape: [?, nb_capsules, capsule_size, 1] 23 | return squash(capsules) 24 | 25 | def routing(u_hat, b_ij, nb_capsules, nb_capsules_p, iterations=4): 26 | """ 27 | Routing algorithm 28 | 29 | **input: 30 | *u_hat: Dot product (weights between previous capsule and current capsule) 31 | *b_ij: the log prior probabilities that capsule i should be coupled to capsule j 32 | *nb_capsules_p: Number of capsule in the previous layer 33 | *nb_capsules: Number of capsule in this layer 34 | """ 35 | # Start the routing algorithm 36 | for it in range(iterations): 37 | with tf.variable_scope('routing_' + str(it)): 38 | # Line 4 of algo 39 | # probabilities that capsule i should be coupled to capsule j. 40 | # c_ij: [nb_capsules_p, nb_capsules, 1, 1] 41 | c_ij = tf.nn.softmax(b_ij, dim=2) 42 | 43 | # Line 5 of algo 44 | # c_ij: [ nb_capsules_p, nb_capsules, 1, 1] 45 | # u_hat: [?, nb_capsules_p, nb_capsules, len_v_j, 1] 46 | s_j = tf.multiply(c_ij, u_hat) 47 | # s_j: [?, nb_capsules_p, nb_capsules, len_v_j, 1] 48 | s_j = tf.reduce_sum(s_j, axis=1, keep_dims=True) 49 | # s_j: [?, 1, nb_capsules, len_v_j, 1) 50 | 51 | # line 6: 52 | # squash using Eq.1, 53 | v_j = squash(s_j) 54 | # v_j: [1, 1, nb_capsules, len_v_j, 1) 55 | 56 | # line 7: 57 | # Frist reshape & tile v_j 58 | # [? , 1, nb_capsules, len_v_j, 1] -> 59 | # [?, nb_capsules_p, nb_capsules, len_v_j, 1] 60 | v_j_tiled = tf.tile(v_j, [1, nb_capsules_p, 1, 1, 1]) 61 | # u_hat: [?, nb_capsules_p, nb_capsules, len_v_j, 1] 62 | # v_j_tiled [1, nb_capsules_p, nb_capsules, len_v_j, 1] 63 | u_dot_v = tf.matmul(u_hat, v_j_tiled, transpose_a=True) 64 | # u_produce_v: [?, nb_capsules_p, nb_capsules, 1, 1] 65 | b_ij += tf.reduce_sum(u_dot_v, axis=0, keep_dims=True) 66 | #b_ih: [1, nb_capsules_p, nb_capsules, 1, 1] 67 | 68 | return tf.squeeze(v_j, axis=1) 69 | 70 | def fully_connected_caps_layer(input_layer, capsules_size, nb_capsules, iterations=4): 71 | """ 72 | Second layer receiving inputs from all capsules of the layer below 73 | **input: 74 | *input_layer: (Tensor) 75 | *capsules_size: (Integer) Size of each capsule 76 | *nb_capsules: (Integer) Number of capsule 77 | *iterations: (Integer) Number of iteration for the routing algorithm 78 | 79 | i refer to the layer below. 80 | j refer to the layer above (the current layer). 81 | """ 82 | shape = input_layer.get_shape().as_list() 83 | # Get the size of each capsule in the previous layer and the current layer. 84 | len_u_i = np.prod(shape[2]) 85 | len_v_j = capsules_size 86 | # Get the number of capsule in the layer bellow. 87 | nb_capsules_p = np.prod(shape[1]) 88 | 89 | # w_ij: Used to compute u_hat by multiplying the output ui of a capsule in the layer below 90 | # with this matrix 91 | # [nb_capsules_p, nb_capsules, len_v_j, len_u_i] 92 | _init = tf.random_normal_initializer(stddev=0.01, seed=0) 93 | _shape = (nb_capsules_p, nb_capsules, len_v_j, len_u_i) 94 | w_ij = tf.get_variable('weight', shape=_shape, dtype=tf.float32, initializer=_init) 95 | 96 | # Adding one dimension to the input [batch_size, nb_capsules_p, length(u_i), 1] -> 97 | # [batch_size, nb_capsules_p, 1, length(u_i), 1] 98 | # To allow the next dot product 99 | input_layer = tf.reshape(input_layer, shape=(-1, nb_capsules_p, 1, len_u_i, 1)) 100 | input_layer = tf.tile(input_layer, [1, 1, nb_capsules, 1, 1]) 101 | 102 | # Eq.2, calc u_hat 103 | # Prediction uj|i made by capsule i 104 | # w_ij: [ nb_capsules_p, nb_capsules, len_v_j, len_u_i, ] 105 | # input: [batch_size, nb_capsules_p, nb_capsules, len_ui, 1] 106 | # u_hat: [batch_size, nb_capsules_p, nb_capsules, len_v_j, 1] 107 | # Each capsule of the previous layer capsule layer is associated to a capsule of this layer 108 | u_hat = tf.einsum('abdc,iabcf->iabdf', w_ij, input_layer) 109 | 110 | # bij are the log prior probabilities that capsule i should be coupled to capsule j 111 | # [nb_capsules_p, nb_capsules, 1, 1] 112 | b_ij = tf.zeros(shape=[nb_capsules_p, nb_capsules, 1, 1], dtype=np.float32) 113 | 114 | return routing(u_hat, b_ij, nb_capsules, nb_capsules_p, iterations=iterations) 115 | 116 | def squash(vector): 117 | """ 118 | Squashing function corresponding to Eq. 1 119 | **input: ** 120 | *vector 121 | """ 122 | vector += 0.00001 # Workaround for the squashing function ... 123 | vec_squared_norm = tf.reduce_sum(tf.square(vector), -2, keep_dims=True) 124 | scalar_factor = vec_squared_norm / (1 + vec_squared_norm) / tf.sqrt(vec_squared_norm) 125 | vec_squashed = scalar_factor * vector # element-wise 126 | return(vec_squashed) 127 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /model_base.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import tensorflow as tf 5 | from collections import Counter 6 | from utils import Utils as U 7 | import json 8 | import numpy as np 9 | from logger import Logger 10 | import time 11 | import pickle 12 | import os 13 | 14 | log = Logger("ModelBase") 15 | 16 | class Hyperparameters(object): 17 | """ 18 | Simple class used to store Hyperparameters 19 | """ 20 | def __init__(self): 21 | super(Hyperparameters, self).__init__() 22 | # List used to store list of hyperparameters name 23 | self.hyp_list = [] 24 | 25 | def set_hyp(self, hyp): 26 | """ 27 | Method used to store hyperparameters inside this class 28 | **input: ** 29 | *hyp (Dict) Dictionary storing all hyperparameters values 30 | """ 31 | for key in hyp: 32 | self.hyp_list.append(key) 33 | setattr(self, key, hyp[key]) 34 | 35 | class ModelBase(object): 36 | """ 37 | Base Model Class 38 | """ 39 | 40 | # Hyp : Hyperparameters 41 | DEFAULT_OUTPUT = "outputs" 42 | DEFAULT_CHECKPOINT_FOLDER = "checkpoints" 43 | 44 | def __init__(self, model_name, hyperparameters_name=None, hyperparameters_content=None, output_folder=None): 45 | """ 46 | **input: 47 | *hyperparameters_name: [Optional] (String|None) Path to the hyperparameters file 48 | By default: hyperparameters.json 49 | *model_name: (Integer) Name of this model 50 | """ 51 | super(ModelBase, self).__init__() 52 | 53 | self.current_dir = os.path.dirname(os.path.realpath(__file__)) 54 | # Output folder 55 | if output_folder is None: 56 | self.output_folder = os.path.join( 57 | os.path.dirname(os.path.abspath(__file__)), self.DEFAULT_OUTPUT) 58 | else: 59 | self.output_folder = output_folder 60 | 61 | hyp_folder = "settings" 62 | hyp_filename = "hyperparameters.json" 63 | hyp_path = os.path.join(self.current_dir, os.path.join(hyp_folder, hyp_filename)) 64 | self.checkpoints_folder = os.path.join(self.output_folder, self.DEFAULT_CHECKPOINT_FOLDER) 65 | 66 | # Set hyperparameters path 67 | if hyperparameters_name is not None: 68 | hyp_path = os.path.join( 69 | self.current_dir, os.path.join(hyp_folder, hyperparameters_name)) 70 | hyp_path = hyp_path if hyperparameters_name is None else hyp_path 71 | # Load hyperparameters content 72 | if hyperparameters_content is None: 73 | hyp_content = U.read_json_file(hyp_path) 74 | else: 75 | hyp_content = hyperparameters_content 76 | # Set hyperparameters 77 | self.h = Hyperparameters() 78 | self.h.set_hyp(hyp_content) 79 | # Set model names 80 | self.name = model_name 81 | self.model_name = model_name 82 | self._set_hyperparameters_name() 83 | # Since hyperparameters had changed, we need to set again each name 84 | self._set_names() 85 | 86 | def _create_conv(self, prev, shape, padding='VALID', strides=[1, 1, 1, 1], relu=False, 87 | max_pooling=False, mp_ksize=[1, 2, 2, 1], mp_strides=[1, 2, 2, 1]): 88 | """ 89 | Create a convolutional layer with relu and/mor max pooling(Optional) 90 | """ 91 | conv_w = tf.Variable(tf.truncated_normal(shape=shape, mean = 0, stddev = 0.1, seed=0)) 92 | conv_b = tf.Variable(tf.zeros(shape[-1])) 93 | conv = tf.nn.conv2d(prev, conv_w, strides=strides, padding=padding) + conv_b 94 | 95 | if relu: 96 | conv = tf.nn.relu(conv) 97 | 98 | if max_pooling: 99 | conv = tf.nn.max_pool(conv, ksize=mp_ksize, strides=mp_strides, padding='VALID') 100 | 101 | return conv 102 | 103 | def _fc(self, prev, input_size, output_size, relu=False, sigmoid=False, no_bias=False, 104 | softmax=False): 105 | """ 106 | Create fully connecter layer with relu(Optional) 107 | """ 108 | fc_w = tf.Variable( 109 | tf.truncated_normal(shape=(input_size, output_size), mean = 0., stddev = 0.1)) 110 | fc_b = tf.Variable(tf.zeros(output_size)) 111 | pre_activation = tf.matmul(prev, fc_w) 112 | activation = None 113 | 114 | if not no_bias: 115 | pre_activation = pre_activation + fc_b 116 | if relu: 117 | activation = tf.nn.relu(pre_activation) 118 | if sigmoid: 119 | activation = tf.nn.sigmoid(pre_activation) 120 | if softmax: 121 | activation = tf.nn.softmax(pre_activation) 122 | 123 | if activation is None: 124 | activation = pre_activation 125 | 126 | return activation, pre_activation 127 | 128 | def init_session(self): 129 | """ 130 | Init tensorflow session 131 | A saver property is create at the same time 132 | """ 133 | # Create session 134 | self.saver = tf.train.Saver() 135 | self.sess = tf.Session() 136 | # Init variables 137 | self.sess.run(tf.global_variables_initializer()) 138 | # Tensorboard 139 | self.tf_tensorboard = tf.summary.merge_all() 140 | train_log_name = os.path.join( 141 | os.path.join(self.output_folder, "tensorboard"), self.name, self.sub_train_log_name) 142 | test_log_name = os.path.join( 143 | os.path.join(self.output_folder, "tensorboard"), self.name, self.sub_test_log_name) 144 | self.train_writer = tf.summary.FileWriter(train_log_name, self.sess.graph) 145 | self.test_writer = tf.summary.FileWriter(test_log_name) 146 | self.train_writer_it = 0 147 | self.test_writer_it = 0 148 | 149 | # Backup tensors 150 | backup_tensors = {} 151 | for field in dir(self): 152 | if "tf_" in field and field.index("tf_") == 0: 153 | backup_tensors[field] = getattr(self, field).name 154 | tf.constant(json.dumps(backup_tensors), dtype=tf.string, name="model_base_tensors_backup") 155 | # Backup hyperparameters 156 | backup_hyp = {} 157 | for field in self.h.hyp_list: 158 | value = getattr(self.h, field) 159 | d_type = tf.int32 if isinstance(value, int) else tf.float32 160 | n_cst = tf.constant(value, dtype=d_type, name="hyp/%s" % field) 161 | backup_hyp[field] = n_cst.name 162 | tf.constant(json.dumps(backup_hyp), dtype=tf.string, name="model_base_hyp_backup") 163 | 164 | def get_equal_batches(self, data, labels, batch_size): 165 | """ 166 | This method will return a generator class which could be used to 167 | get new batches with the same number of rows for each class 168 | 169 | **input:** 170 | *batch_size (int) Size of each batch 171 | **return (Python Generator of Batch class)** 172 | """ 173 | labels = np.array(labels) 174 | 175 | indexs = np.arange(len(data)) 176 | np.random.shuffle(indexs) 177 | 178 | data = data[indexs] 179 | labels = labels[indexs] 180 | 181 | max_size = Counter(labels).most_common()[-1][1] 182 | unique_label = np.array(list(set(labels))) 183 | nb_classes = len(unique_label) 184 | 185 | if batch_size > max_size: 186 | batch_size = max_size 187 | 188 | batch_per_class = batch_size // nb_classes 189 | iterations = max_size // batch_per_class 190 | 191 | for it in range(iterations): 192 | 193 | indexes = [] 194 | 195 | for label in unique_label: 196 | n_indexes = np.where(labels==label)[0][it * batch_per_class: (it + 1) * batch_per_class] 197 | n_indexes = n_indexes.tolist() 198 | indexes += n_indexes 199 | 200 | indexes = np.array(indexes) 201 | 202 | x = data[indexes] 203 | y = labels[indexes] 204 | 205 | yield x, y 206 | 207 | 208 | def get_batches(self, data_list, batch_size, shuffle=True): 209 | """ 210 | This method will return a generator class which could be used to 211 | get new batches. 212 | 213 | **input:** 214 | *batch_size (int) Size of each batch 215 | **return (Python Generator of Batch class)** 216 | """ 217 | if shuffle: 218 | indexs = np.arange(len(data_list[0])) 219 | np.random.shuffle(indexs) 220 | 221 | for d, data in enumerate(data_list): 222 | data_list[d] = np.array(data_list[d]) 223 | data_list[d] = data_list[d][indexs] 224 | 225 | iterations = len(data_list[0]) // batch_size 226 | for iteration in range(iterations): 227 | yield (dt[iteration * batch_size: (iteration + 1) * batch_size] for dt in data_list) 228 | 229 | def save(self, name=None): 230 | """ 231 | Save the model 232 | """ 233 | log.info("Saving model ...") 234 | 235 | if name is None: 236 | name = self.model_name 237 | 238 | if not os.path.exists(self.checkpoints_folder): 239 | os.makedirs(self.checkpoints_folder) 240 | 241 | save_path = self.saver.save( 242 | self.sess, os.path.join(self.checkpoints_folder, name)) 243 | 244 | log.info("Model successfully saved here: %s" % save_path) 245 | 246 | def _set_hyperparameters_name(self): 247 | """ 248 | Convert hyperparameters dict to a string 249 | This string will be used to set the models names 250 | """ 251 | # Generate a little name for each hyperparameters 252 | hyperparameters_names = [("".join([p[0] for p in hyp.split("_")]), getattr(self.h, hyp)) 253 | for hyp in self.h.hyp_list] 254 | self.hyperparameters_name = "" 255 | for index_hyperparameter, hyperparameter in enumerate(hyperparameters_names): 256 | short_name, value = hyperparameter 257 | prepend = "" if index_hyperparameter == 0 else "_" 258 | self.hyperparameters_name += "%s%s_%s" % (prepend, short_name, value) 259 | 260 | def _set_names(self): 261 | """ 262 | Set all model names 263 | """ 264 | name_time = "%s--%s" % (self.model_name, time.time()) 265 | # model_name is used to set the ckpt name 266 | self.model_name = "%s--%s" % (self.hyperparameters_name, name_time) 267 | # sub_train_log_name is used to set the name of the training part in tensorboard 268 | self.sub_train_log_name = "%s-train--%s" % (self.hyperparameters_name, name_time) 269 | # sub_test_log_name is used to set the name of the testing part in tensorboard 270 | self.sub_test_log_name = "%s-test--%s" % (self.hyperparameters_name, name_time) 271 | 272 | def dump_batch(self, folder, data): 273 | """ 274 | Save batches 275 | Mainly used for Reinforcement Learning 276 | """ 277 | folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), folder) 278 | # Create folder if not exist 279 | if not os.path.exists(folder): 280 | os.makedirs(folder) 281 | 282 | pickle.dump(data, open(os.path.join(folder, str(time.time())), "wb" )) 283 | 284 | 285 | def load(self, ckpt): 286 | """ 287 | Load a model 288 | """ 289 | log.info("Loading ckpt ...") 290 | #loaded_graph = tf.Graph() 291 | #tf.reset_default_graph() 292 | #g = tf.Graph() 293 | #with g.as_default(): 294 | self.sess = tf.Session() 295 | # Load the graph 296 | loader = tf.train.import_meta_graph(ckpt + '.meta') 297 | loader.restore(self.sess, ckpt) 298 | 299 | g = tf.get_default_graph() 300 | 301 | # Search for the backup tensor 302 | tensor_names = [ 303 | n.name for n in g.as_graph_def().node if "model_base_tensors_backup" in n.name] 304 | 305 | # Search for the backup hyp 306 | hyp_names = [ 307 | n.name for n in g.as_graph_def().node if "model_base_hyp_backup" in n.name] 308 | 309 | # Get the tensor string 310 | #tensors = g.get_tensor_by_name(names[0]) 311 | tensors = g.get_operation_by_name(tensor_names[0]).outputs 312 | hyps = g.get_operation_by_name(hyp_names[0]).outputs 313 | 314 | #self.sess.run(tf.global_variables_initializer()) 315 | 316 | tensors = self.sess.run(tensors)[0] 317 | tensors = json.loads(tensors) 318 | for tensor in tensors: 319 | try: 320 | n_tensor = g.get_tensor_by_name(tensors[tensor]) 321 | except Exception as e: 322 | n_tensor = g.get_operation_by_name(tensors[tensor]) 323 | setattr(self, tensor, n_tensor) 324 | 325 | hyps = self.sess.run(hyps)[0] 326 | hyps = json.loads(hyps) 327 | for hyp in hyps: 328 | n_hyp = g.get_tensor_by_name(hyps[hyp]) 329 | setattr(self.h, hyp, self.sess.run(n_hyp)) 330 | 331 | log.info("Ckpt ready") 332 | 333 | # Tensorboard 334 | self.tf_tensorboard = tf.summary.merge_all() 335 | train_log_name = os.path.join( 336 | os.path.join(self.output_folder, "tensorboard"), self.name, self.sub_train_log_name) 337 | test_log_name = os.path.join( 338 | os.path.join(self.output_folder, "tensorboard"), self.name, self.sub_test_log_name) 339 | self.train_writer = tf.summary.FileWriter(train_log_name, self.sess.graph) 340 | self.test_writer = tf.summary.FileWriter(test_log_name) 341 | self.train_writer_it = 0 342 | self.test_writer_it = 0 343 | 344 | self.model_name = ckpt.split("/")[-1] 345 | self.saver = tf.train.Saver() 346 | 347 | 348 | if __name__ == '__main__': 349 | base_model = BaseModel("test") 350 | -------------------------------------------------------------------------------- /model.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | # -*- coding: utf-8 -*- 3 | 4 | import numpy as np 5 | from model_base import ModelBase 6 | from caps_net import conv_caps_layer, fully_connected_caps_layer 7 | import tensorflow as tf 8 | 9 | class ModelTrafficSign(ModelBase): 10 | """ 11 | ModelTrafficSign. 12 | This class is used to create the conv graph using: 13 | Dynamic Routing Between Capsules 14 | """ 15 | 16 | # Numbers of label to predict 17 | NB_LABELS = 43 18 | 19 | def __init__(self, model_name, output_folder): 20 | """ 21 | **input: 22 | *model_name: (Integer) Name of this model 23 | *output_folder: Output folder to saved data (tensorboard, checkpoints) 24 | """ 25 | ModelBase.__init__(self, model_name, output_folder=output_folder) 26 | 27 | def _build_inputs(self): 28 | """ 29 | Build tensorflow inputs 30 | (Placeholder) 31 | **return: ** 32 | *tf_images: Images Placeholder 33 | *tf_labels: Labels Placeholder 34 | """ 35 | # Images 32*32*3 36 | tf_images = tf.placeholder(tf.float32, [None, 32, 32, 3], name='images') 37 | # Labels: [0, 1, 6, 20, ...] 38 | tf_labels = tf.placeholder(tf.int64, [None], name='labels') 39 | return tf_images, tf_labels 40 | 41 | def _build_main_network(self, images, conv_2_dropout): 42 | """ 43 | This method is used to create the two convolutions and the CapsNet on the top 44 | **input: 45 | *images: Image PLaceholder 46 | *conv_2_dropout: Dropout value placeholder 47 | **return: ** 48 | *Caps1: Output of first Capsule layer 49 | *Caps2: Output of second Capsule layer 50 | """ 51 | # First BLock: 52 | # Layer 1: Convolution. 53 | shape = (self.h.conv_1_size, self.h.conv_1_size, 3, self.h.conv_1_nb) 54 | conv1 = self._create_conv(self.tf_images, shape, relu=True, max_pooling=False, padding='VALID') 55 | # Layer 2: Convolution. 56 | #shape = (self.h.conv_2_size, self.h.conv_2_size, self.h.conv_1_nb, self.h.conv_2_nb) 57 | #conv2 = self._create_conv(conv1, shape, relu=True, max_pooling=False, padding='VALID') 58 | conv1 = tf.nn.dropout(conv1, keep_prob=conv_2_dropout) 59 | 60 | # Create the first capsules layer 61 | caps1 = conv_caps_layer( 62 | input_layer=conv1, 63 | capsules_size=self.h.caps_1_vec_len, 64 | nb_filters=self.h.caps_1_nb_filter, 65 | kernel=self.h.caps_1_size) 66 | # Create the second capsules layer used to predict the output 67 | caps2 = fully_connected_caps_layer( 68 | input_layer=caps1, 69 | capsules_size=self.h.caps_2_vec_len, 70 | nb_capsules=self.NB_LABELS, 71 | iterations=self.h.routing_steps) 72 | 73 | return caps1, caps2 74 | 75 | def _build_decoder(self, caps2, one_hot_labels, batch_size): 76 | """ 77 | Build the decoder part from the last capsule layer 78 | **input: 79 | *Caps2: Output of second Capsule layer 80 | *one_hot_labels 81 | *batch_size 82 | """ 83 | labels = tf.reshape(one_hot_labels, (-1, self.NB_LABELS, 1)) 84 | # squeeze(caps2): [?, len_v_j, capsules_nb] 85 | # labels: [?, NB_LABELS, 1] with capsules_nb == NB_LABELS 86 | mask = tf.matmul(tf.squeeze(caps2), labels, transpose_a=True) 87 | # Select the good capsule vector 88 | capsule_vector = tf.reshape(mask, shape=(batch_size, self.h.caps_2_vec_len)) 89 | # capsule_vector: [?, len_v_j] 90 | 91 | # Reconstruct image 92 | fc1 = tf.contrib.layers.fully_connected(capsule_vector, num_outputs=400) 93 | fc1 = tf.reshape(fc1, shape=(batch_size, 5, 5, 16)) 94 | upsample1 = tf.image.resize_nearest_neighbor(fc1, (8, 8)) 95 | conv1 = tf.layers.conv2d(upsample1, 4, (3,3), padding='same', activation=tf.nn.relu) 96 | 97 | upsample2 = tf.image.resize_nearest_neighbor(conv1, (16, 16)) 98 | conv2 = tf.layers.conv2d(upsample2, 8, (3,3), padding='same', activation=tf.nn.relu) 99 | 100 | upsample3 = tf.image.resize_nearest_neighbor(conv2, (32, 32)) 101 | conv6 = tf.layers.conv2d(upsample3, 16, (3,3), padding='same', activation=tf.nn.relu) 102 | 103 | # 3 channel for RGG 104 | logits = tf.layers.conv2d(conv6, 3, (3,3), padding='same', activation=None) 105 | decoded = tf.nn.sigmoid(logits, name='decoded') 106 | tf.summary.image('reconstruction_img', decoded) 107 | 108 | return decoded 109 | 110 | def init(self): 111 | """ 112 | Init the graph 113 | """ 114 | # Get graph inputs 115 | self.tf_images, self.tf_labels = self._build_inputs() 116 | # Dropout inputs 117 | self.tf_conv_2_dropout = tf.placeholder(tf.float32, shape=(), name='conv_2_dropout') 118 | # Dynamic batch size 119 | batch_size = tf.shape(self.tf_images)[0] 120 | # Translate labels to one hot array 121 | one_hot_labels = tf.one_hot(self.tf_labels, depth=self.NB_LABELS) 122 | # Create the first convolution and the CapsNet 123 | self.tf_caps1, self.tf_caps2 = self._build_main_network(self.tf_images, self.tf_conv_2_dropout) 124 | 125 | # Build the images reconstruction 126 | self.tf_decoded = self._build_decoder(self.tf_caps2, one_hot_labels, batch_size) 127 | 128 | # Build the loss 129 | _loss = self._build_loss( 130 | self.tf_caps2, one_hot_labels, self.tf_labels, self.tf_decoded, self.tf_images) 131 | (self.tf_loss_squared_rec, self.tf_margin_loss_sum, self.tf_predicted_class, 132 | self.tf_correct_prediction, self.tf_accuracy, self.tf_loss, self.tf_margin_loss, 133 | self.tf_reconstruction_loss) = _loss 134 | 135 | # Build optimizer 136 | optimizer = tf.train.AdamOptimizer(learning_rate=self.h.learning_rate) 137 | self.tf_optimizer = optimizer.minimize(self.tf_loss, global_step=tf.Variable(0, trainable=False)) 138 | 139 | # Log value into tensorboard 140 | tf.summary.scalar('margin_loss', self.tf_margin_loss) 141 | tf.summary.scalar('accuracy', self.tf_accuracy) 142 | tf.summary.scalar('total_loss', self.tf_loss) 143 | tf.summary.scalar('reconstruction_loss', self.tf_reconstruction_loss) 144 | 145 | self.tf_test = tf.random_uniform([2], minval=0, maxval=None, dtype=tf.float32, seed=None, name="tf_test") 146 | 147 | self.init_session() 148 | 149 | 150 | def _build_loss(self, caps2, one_hot_labels, labels, decoded, images): 151 | """ 152 | Build the loss of the graph 153 | """ 154 | # Get the length of each capsule 155 | capsules_length = tf.sqrt(tf.reduce_sum(tf.square(caps2), axis=2, keep_dims=True)) 156 | 157 | max_l = tf.square(tf.maximum(0., 0.9 - capsules_length)) 158 | max_l = tf.reshape(max_l, shape=(-1, self.NB_LABELS)) 159 | max_r = tf.square(tf.maximum(0., capsules_length - 0.1)) 160 | max_r = tf.reshape(max_r, shape=(-1, self.NB_LABELS)) 161 | t_c = one_hot_labels 162 | m_loss = t_c * max_l + 0.5 * (1 - t_c) * max_r 163 | margin_loss_sum = tf.reduce_sum(m_loss, axis=1) 164 | margin_loss = tf.reduce_mean(margin_loss_sum) 165 | 166 | # Reconstruction loss 167 | loss_squared_rec = tf.square(decoded - images) 168 | reconstruction_loss = tf.reduce_mean(loss_squared_rec) 169 | 170 | # 3. Total loss 171 | loss = margin_loss + (0.0005 * reconstruction_loss) 172 | 173 | # Accuracy 174 | predicted_class = tf.argmax(capsules_length, axis=1) 175 | predicted_class = tf.reshape(predicted_class, [tf.shape(capsules_length)[0]]) 176 | correct_prediction = tf.equal(predicted_class, labels) 177 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 178 | 179 | return (loss_squared_rec, margin_loss_sum, predicted_class, correct_prediction, accuracy, 180 | loss, margin_loss, reconstruction_loss) 181 | 182 | def optimize(self, images, labels, tb_save=True): 183 | """ 184 | Train the model 185 | **input: ** 186 | *images: Image to train the model on 187 | *labels: True classes 188 | *tb_save: (Boolean) Log this optimization in tensorboard 189 | **return: ** 190 | Loss: The loss of the model on this batch 191 | Acc: Accuracy of the model on this batch 192 | """ 193 | tensors = [self.tf_optimizer, self.tf_margin_loss, self.tf_accuracy, self.tf_tensorboard] 194 | _, loss, acc, summary = self.sess.run(tensors, 195 | feed_dict={ 196 | self.tf_images: images, 197 | self.tf_labels: labels, 198 | self.tf_conv_2_dropout: self.h.conv_2_dropout 199 | }) 200 | 201 | if tb_save: 202 | # Write data to tensorboard 203 | self.train_writer.add_summary(summary, self.train_writer_it) 204 | self.train_writer_it += 1 205 | 206 | return loss, acc 207 | 208 | def evaluate(self, images, labels, tb_train_save=False, tb_test_save=False): 209 | """ 210 | Evaluate dataset 211 | **input: ** 212 | *images: Image to train the model on 213 | *labels: True classes 214 | *tb_train_save: (Boolean) Log this optimization in tensorboard under the train part 215 | *tb_test_save: (Boolean) Log this optimization in tensorboard under the test part 216 | **return: ** 217 | Loss: The loss of the model on this batch 218 | Acc: Accuracy of the model on this batch 219 | """ 220 | tensors = [self.tf_margin_loss, self.tf_accuracy, self.tf_tensorboard] 221 | loss, acc, summary = self.sess.run(tensors, 222 | feed_dict={ 223 | self.tf_images: images, 224 | self.tf_labels: labels, 225 | self.tf_conv_2_dropout: 1. 226 | }) 227 | 228 | if tb_test_save: 229 | # Write data to tensorboard 230 | self.test_writer.add_summary(summary, self.test_writer_it) 231 | self.test_writer_it += 1 232 | 233 | if tb_train_save: 234 | # Write data to tensorboard 235 | self.train_writer.add_summary(summary, self.train_writer_it) 236 | self.train_writer_it += 1 237 | 238 | return loss, acc 239 | 240 | def predict(self, images): 241 | """ 242 | Method used to predict a class 243 | Return a softmax 244 | **input: ** 245 | *images: Image to train the model on 246 | **return: 247 | *softmax: Softmax between all capsules 248 | """ 249 | tensors = [self.tf_caps2] 250 | 251 | caps2 = self.sess.run(tensors, 252 | feed_dict={ 253 | self.tf_images: images, 254 | self.tf_conv_2_dropout: 1. 255 | })[0] 256 | 257 | # tf.sqrt(tf.reduce_sum(tf.square(caps2), axis=2, keep_dims=True)) 258 | caps2 = np.sqrt(np.sum(np.square(caps2), axis=2, keepdims=True)) 259 | caps2 = np.reshape(caps2, (len(images), self.NB_LABELS)) 260 | # softmax 261 | softmax = np.exp(caps2) / np.sum(np.exp(caps2), axis=1, keepdims=True) 262 | 263 | return softmax 264 | 265 | def reconstruction(self, images, labels): 266 | """ 267 | Method used to get the reconstructions given a batch 268 | Return the result as a softmax 269 | **input: ** 270 | *images: Image to train the model on 271 | *labels: True classes 272 | """ 273 | tensors = [self.tf_decoded] 274 | 275 | decoded = self.sess.run(tensors, 276 | feed_dict={ 277 | self.tf_images: images, 278 | self.tf_labels: labels, 279 | self.tf_conv_2_dropout: 1. 280 | })[0] 281 | 282 | return decoded 283 | 284 | def evaluate_dataset(self, images, labels, batch_size=10): 285 | """ 286 | Evaluate a full dataset 287 | This method is used to fully evaluate the dataset batch per batch. Useful when 288 | the dataset can't be fit inside to the GPU. 289 | *input: ** 290 | *images: Image to train the model on 291 | *labels: True classes 292 | *return: ** 293 | *loss: Loss overall your dataset 294 | *accuracy: Accuracy overall your dataset 295 | *predicted_class: Predicted class 296 | """ 297 | tensors = [self.tf_loss_squared_rec, self.tf_margin_loss_sum, self.tf_correct_prediction, 298 | self.tf_predicted_class] 299 | 300 | loss_squared_rec_list = None 301 | margin_loss_sum_list = None 302 | correct_prediction_list = None 303 | predicted_class = None 304 | 305 | b = 0 306 | for batch in self.get_batches([images, labels], batch_size, shuffle=False): 307 | images_batch, labels_batch = batch 308 | loss_squared_rec, margin_loss_sum, correct_prediction, classes = self.sess.run(tensors, 309 | feed_dict={ 310 | self.tf_images: images_batch, 311 | self.tf_labels: labels_batch, 312 | self.tf_conv_2_dropout: 1. 313 | }) 314 | if loss_squared_rec_list is not None: 315 | predicted_class = np.concatenate((predicted_class, classes)) 316 | loss_squared_rec_list = np.concatenate((loss_squared_rec_list, loss_squared_rec)) 317 | margin_loss_sum_list = np.concatenate((margin_loss_sum_list, margin_loss_sum)) 318 | correct_prediction_list = np.concatenate((correct_prediction_list, correct_prediction)) 319 | else: 320 | predicted_class = classes 321 | loss_squared_rec_list = loss_squared_rec 322 | margin_loss_sum_list = margin_loss_sum 323 | correct_prediction_list = correct_prediction 324 | b += batch_size 325 | 326 | margin_loss = np.mean(margin_loss_sum_list) 327 | reconstruction_loss = np.mean(loss_squared_rec_list) 328 | accuracy = np.mean(correct_prediction_list) 329 | 330 | loss = margin_loss 331 | 332 | return loss, accuracy, predicted_class 333 | 334 | 335 | if __name__ == '__main__': 336 | model_traffic_sign = ModelTrafficSign("test", output_folder=None) 337 | model_traffic_sign.init() 338 | --------------------------------------------------------------------------------