Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tripletloss, TripletCNN and further Tensorflow 2 support #2

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config_test/test_train_nodaug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test:
orig:
daug_params: nodaug.yml
metrics:
- accuracy
- top3
train:
orig:
daug_params: nodaug.yml
metrics:
- accuracy
- top3
46 changes: 46 additions & 0 deletions config_train/cifar/tripletcnn/noreg_bn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
network:
name: tripletcnn
reg:
dropout: 0.
weight_decay: !!null
batch_norm: True
optimizer:
name: Adam
loss: categorical_crossentropy
# momentum: 0.9
# nesterov: True
daug_invariance_params_file: noinv.yml
class_invariance_params_file: noinv.yml
train:
lr:
init_lr: 0.001
decay_factor: 0.1
decay_epochs: [300]
batch_size:
tr: 128
val: 128
epochs: 40
simulate:
bs_lr: bs # bs or lr
rep_samples: !!null
norep_samples: False
true_epochs: False
data:
data_file: ~/datasets/hdf5/cifar10.hdf5
group_tr: cifar10_train
group_val: cifar10_test
shuffle_train_val: False
chunk_size: !!null
queue_size: 50
daug:
nodaug: nodaug.yml
daug_params_file: nodaug.yml
aug_per_img_tr: 1
aug_per_img_val: 1
seeds:
tf: 33
np: 39
daug: 59
batch_shuffle: 47
train_val: 27
metrics: [accuracy]
23 changes: 18 additions & 5 deletions data_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def get_generator(images, **dict_params):


def generate_batches(image_gen, images, labels, batch_size, aug_per_im, shuffle,
seed=None, n_inv_layers=0):
seed=None, n_inv_layers=0, uses_triplet_loss=False):
for batch in image_gen.flow_dask(images, labels,
batch_size=batch_size,
aug_per_im=aug_per_im,
Expand All @@ -117,18 +117,31 @@ def generate_batches(image_gen, images, labels, batch_size, aug_per_im, shuffle,
# The list of targets for the invariance outputs are:
# [daug_invariance_target, class_invariance_target, mean: ones]
# for each invariance layer
invariance_targets = []

bs = batch[0].shape[0]
class_inv_y = np.dot(batch[1][0], batch[1][0].T)
ones = np.ones([bs, bs], dtype=np.uint8)
tril = np.tril_indices(bs)
class_inv_y[tril] = 0
ones[tril] = 0

daug_inv_y = np.stack([batch[1][1], class_inv_y], axis=2)
invariance_targets.append(daug_inv_y)

class_inv_y = np.stack([class_inv_y, ones], axis=2)
invariance_targets = [daug_inv_y,
class_inv_y,
np.ones(batch[0].shape[0], dtype=np.uint8)] \
* n_inv_layers
invariance_targets.append(class_inv_y)

if uses_triplet_loss:
# append the daug inputs another time since it contains
# both class and daug labels needed for the triplet loss
invariance_targets.append(daug_inv_y)

mean_inv_y = np.ones(batch[0].shape[0], dtype=np.uint8)
invariance_targets.append(mean_inv_y)

invariance_targets *= n_inv_layers

yield (batch[0],
[batch[1][0]] + invariance_targets)
else:
Expand Down
16 changes: 16 additions & 0 deletions examples/score-invariance.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
for MODEL in r26 r29
do
echo -e "\nCOMPUTING INVARIANCE FOR MODEL $MODEL\n"
for EPOCH in 010 020 030 040
do
echo -e "\nMODEL $MODEL, EPOCH $EPOCH\n"
python invariance_numpy.py \
--data_file ~/Storage/Datasets/cifar10/hdf5/cifar10.hdf5 \
--group test \
--daug_params heavier.yml \
--model log/cifar10/tripletcnn/noreg/bn/inv/$MODEL/model_*_$EPOCH \
--output_mse_matrix_hdf5 mse_matrix_ep$EPOCH \
--output_pickle invscores_ep$EPOCH \
--layer_regex flatten
done
done
15 changes: 15 additions & 0 deletions examples/score-test-prediction.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
for MODEL in r26 r29
do
for EPOCH in 010 020 030 040
do
echo -e "\nMODEL $MODEL, EPOCH $EPOCH\n"
python test.py \
--data_file ~/Storage/Datasets/cifar10/hdf5/cifar10.hdf5 \
--group_tr train \
--group_tt test \
--test_config config_test/test_train_nodaug.yml \
--model log/cifar10/tripletcnn/noreg/bn/inv/$MODEL/model_*_$EPOCH \
--output_dir -1 \
--output_basename testscores/testscore_ep$EPOCH
done
done
20 changes: 20 additions & 0 deletions examples/train-tripletcnn-cifar-cls.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This script trains the TripletCNN architecture on CIFAR-10 with data
# augmentation and class triplet loss
#
# It is meant to to be run from the root of the repository
# Consider changing the paths to files and directories
#
# CIFAR-10
#
# All-CNN
#
# no-reg bn heavier inv
python train.py \
--data_file ~/Storage/Datasets/cifar10/hdf5/cifar10.hdf5 \
--group_tr train \
--group_val test \
--train_dir ./log/cifar10/tripletcnn/noreg/bn/inv/r24/ \
--daug_params heavier.yml \
--train_config_file config_train/cifar/tripletcnn/noreg_bn.yml \
--save_model_every 10 \
--no_fit_generator
25 changes: 25 additions & 0 deletions examples/train-tripletcnn-cifar-daug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This script trains the TripletCNN architecture on CIFAR-10 with data
# augmentation and class triplet loss
#
# It is meant to to be run from the root of the repository
# Consider changing the paths to files and directories
#
# CIFAR-10
#
# All-CNN
#
# no-reg bn heavier inv
python train.py \
--data_file ~/Storage/Datasets/cifar10/hdf5/cifar10.hdf5 \
--group_tr train \
--group_val test \
--train_dir ./log/cifar10/tripletcnn/noreg/bn/inv/r31/ \
--daug_params heavier.yml \
--train_config_file config_train/cifar/tripletcnn/noreg_bn.yml \
--aug_per_img_tr 16 \
--daug_invariance_params loss1_exp.yml \
--class_invariance_params noinv.yml \
--save_model_every 10 \
--no_fit_generator \
--resume_training ./log/cifar10/tripletcnn/noreg/bn/inv/r31/model_*_030 \
--triplet_loss
40 changes: 31 additions & 9 deletions invariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@

from data_input import hdf52dask, get_generator, batch_generator
from utils import get_daug_scheme_path
from utils import pairwise_loss, mean_loss, invariance_loss
from utils import pairwise_loss, mean_loss, invariance_loss, triplet_loss
from activations import get_activations

import keras.backend as K
from keras.models import load_model
import keras.losses
keras.losses.pairwise_loss = pairwise_loss
keras.losses.invariance_loss = invariance_loss
keras.losses.mean_loss = mean_loss
import tensorflow.compat.v1 as tf
from tensorflow.compat.v1 import keras
import tensorflow.compat.v1.keras.backend as K
from tensorflow.compat.v1.keras.models import load_model
import tensorflow.compat.v1.keras.losses

# Disable eager execution, otherwise K.function will not work when passing
# K.learning_phase() as input (see issue
# https://github.com/tensorflow/tensorflow/issues/34201)
tf.disable_eager_execution()

import os
import argparse
Expand All @@ -50,6 +54,10 @@

def main(argv=None):

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
K.set_session(tf.Session(config=config))

# cluster = LocalCluster(dashboard_address=None)
# client = Client(cluster, memory_limit='{}GB'.format(FLAGS.memory_limit),
# processes=False)
Expand All @@ -76,7 +84,14 @@ def main(argv=None):

# Initialize the network model
model_filename = FLAGS.model
model = load_model(model_filename)
model = load_model(
model_filename,
custom_objects = {
'invariance_loss': invariance_loss,
'triplet_loss': triplet_loss,
'pairwise_loss': pairwise_loss,
'mean_loss': mean_loss
})

# Print the model summary
model.summary()
Expand Down Expand Up @@ -119,7 +134,14 @@ def main(argv=None):
# Reload the model
if layer_idx > 0:
K.clear_session()
model = load_model(model_filename)
model = load_model(
model_filename,
custom_objects = {
'invariance_loss': invariance_loss,
'triplet_loss': triplet_loss,
'pairwise_loss': pairwise_loss,
'mean_loss': mean_loss
})

layer = model.get_layer(layer_name)

Expand Down
Loading