Skip to content
Snippets Groups Projects
Commit 8eeebaa6 authored by julien.dejasmin's avatar julien.dejasmin
Browse files

add results omniglot classif

parent 9e3b8fdc
Branches
No related tags found
No related merge requests found
import sys
# sys.path.append('/data1/data/expes/julien.dejasmin/Thesis/Binary_weights/MNIST_binary_V2')
import os
sys.path.append(os.path.dirname(os.path.realpath('')))
import torch.optim as optim
from DataLoader.dataLoaders import get_omniglot_dataloaders_v1
from utils.models import NoBinaryNetOmniglotClassification, BinaryNetOmniglotClassification
......@@ -8,9 +13,9 @@ from utils.training import training
from config import PATH
# parameters default values
lr = 1e-3
lr = 1e-5
# momentum = 0.9
nb_epoch = 50
nb_epoch = 30
batch_size_train = 64
batch_size_test = 128
......@@ -18,11 +23,11 @@ slope_annealing = True
reinforce = False
stochastic = True
binary = True
plot_result = False
first_conv_layer = True
plot_result = True
first_conv_layer = False
second_conv_layer = False
third_conv_layer = False
fourth_conv_layer = False
fourth_conv_layer = True
omniglot = True
......@@ -66,8 +71,8 @@ train_loader, test_loader = get_omniglot_dataloaders_v1(batch_size_train, batch_
# visualize example
# show_som_examples(train_loader)
path_save_plot = '/results/Omniglot_classif/plot_acc_loss/'
path_save_model = '/trained_models/Omniglot_classif/'
path_save_plot = PATH + '/results/Omniglot_results/plot_acc_loss/Omniglot_classif/'
path_save_model = PATH + '/trained_models/Omniglot_classif/'
# train
"""
......@@ -76,7 +81,7 @@ callbacks = [
filepath=PATH + path_save_model + names_model + '.pth',
monitor=f'val_classif_{names_model}acc',
),
ReduceLROnPlateau(patience=20, factor=0.5, monitor=f'val_classif_{names_model}_acc'),
lassifi_classif_{names_model}_acc'),
CSVLogger(PATH + f'/logs/omniglot_classif/{names_model}.csv'),
]
"""
......
results/Omniglot_results/plot_acc_loss/NonBinaryNetacc_model_.png

14.6 KiB

results/Omniglot_results/plot_acc_loss/NonBinaryNetloss_model_.png

13 KiB

No preview for this file type
......@@ -30,138 +30,18 @@ class NoBinaryNetMnist(Net):
self.fc = nn.Linear(7 * 7 * 32, 10)
def forward(self, input):
x, slope = input
x_layer1 = self.act_layer1(self.maxpool1(self.batchnorm1(self.layer1(x) * slope)))
x_layer2 = self.act_layer2(self.maxpool2(self.batchnorm2(self.layer2(x_layer1))))
self.layer1 = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=5, padding=2),
nn.BatchNorm2d(16),
nn.MaxPool2d(2))
self.act_layer1 = Hardsigmoid()
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.MaxPool2d(2))
self.act_layer2 = Hardsigmoid()
self.fc = nn.Linear(7 * 7 * 32, 10)
def forward(self, input):
x, slope = input
x_layer1 = self.act_layer1(self.layer1(x) * slope)
x_layer2 = self.act_layer2(self.layer2(x_layer1))
x_layer1 = self.act_layer1(self.maxpool1(self.batchnorm1(self.layer1(x) * slope)))
x_layer2 = self.act_layer2(self.maxpool2(self.batchnorm2(self.layer2(x_layer1) * slope)))
x_layer2 = x_layer2.view(x_layer2.size(0), -1)
x_fc = self.fc(x_layer2)
x_out = F.log_softmax(x_fc, dim=1)
return x_out
class NoBinaryNetOmniglotClassification(Net):
def __init__(self):
super(NoBinaryNetOmniglotClassification, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.MaxPool2d(2))
self.act_layer1 = Hardsigmoid()
self.layer2 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.MaxPool2d(2))
self.act_layer2 = Hardsigmoid()
self.layer3 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.MaxPool2d(2))
self.act_layer3 = Hardsigmoid()
self.fc = nn.Linear(13 * 13 * 32, 1623)
def forward(self, input):
x, slope = input
x_layer1 = self.act_layer1(self.layer1(x) * slope)
x_layer2 = self.act_layer2(self.layer2(x_layer1))
x_layer3 = self.act_layer3(self.layer3(x_layer2))
x_layer3 = x_layer3.view(x_layer3.size(0), -1)
x_fc = self.fc(x_layer3)
x_out = F.log_softmax(x_fc, dim=1)
return x_out
class BinaryNetOmniglotClassif(Net):
def __init__(self, first_conv_layer, second_conv_layer, third_conv_layer, mode='Deterministic', estimator='ST'):
super(BinaryNetOmniglotClassif, self).__init__()
assert mode in ['Deterministic', 'Stochastic']
assert estimator in ['ST', 'REINFORCE']
# if mode == 'Deterministic':
# assert estimator == 'ST'
self.mode = mode
self.estimator = estimator
self.first_conv_layer = first_conv_layer
self.second_conv_layer = second_conv_layer
self.third_conv_layer = third_conv_layer
self.layer1 = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.MaxPool2d(2))
if self.first_conv_layer:
if self.mode == 'Deterministic':
self.act_layer1 = DeterministicBinaryActivation(estimator=estimator)
elif self.mode == 'Stochastic':
self.act_layer1 = StochasticBinaryActivation(estimator=estimator)
else:
self.act_layer1 = Hardsigmoid()
self.layer2 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.MaxPool2d(2))
if self.second_conv_layer:
if self.mode == 'Deterministic':
self.act_layer2 = DeterministicBinaryActivation(estimator=estimator)
elif self.mode == 'Stochastic':
self.act_layer2 = StochasticBinaryActivation(estimator=estimator)
else:
self.act_layer2 = Hardsigmoid()
self.layer3 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.MaxPool2d(2))
if self.third_conv_layer:
if self.mode == 'Deterministic':
self.act_layer3 = DeterministicBinaryActivation(estimator=estimator)
elif self.mode == 'Stochastic':
self.act_layer3 = StochasticBinaryActivation(estimator=estimator)
else:
self.act_layer3 = Hardsigmoid()
self.fc = nn.Linear(13 * 13 * 32, 1623)
def forward(self, input):
x, slope = input
if self.first_conv_layer:
x_layer1 = self.act_layer1((self.layer1(x), slope))
else:
x_layer1 = self.act_layer1(self.layer1(x) * slope)
if self.second_conv_layer:
x_layer2 = self.act_layer2((self.layer2(x_layer1), slope))
else:
x_layer2 = self.act_layer2(self.layer2(x_layer1) * slope)
if self.third_conv_layer:
x_layer3 = self.act_layer3((self.layer3(x_layer2), slope))
else:
x_layer3 = self.act_layer3(self.layer3(x_layer2) * slope)
x_layer3 = x_layer3.view(x_layer3.size(0), -1)
x_fc = self.fc(x_layer3)
x_out = F.log_softmax(x_fc, dim=1)
return x_out
class BinaryNet(Net):
class BinaryNetMNIST(Net):
def __init__(self, first_conv_layer, last_conv_layer, mode='Deterministic', estimator='ST'):
super(BinaryNet, self).__init__()
......
......@@ -106,7 +106,7 @@ def training(path_save_plot, path_save_model, use_gpu, model, names_model, nb_ep
if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
save(model.state_dict(), PATH + path_save_model + names_model + '.pt')
save(model.state_dict(), path_save_model + names_model + '.pt')
end_time = time.time()
epoch_minutes, epoch_secs = epoch_time(start_time, end_time)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment