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

Abdellah #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Binary file added results/1/loss.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 results/1/model.pth
Binary file not shown.
Binary file added results/1/original_images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions results/1/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
default_batch_size = 128 # 32 instead of 128 specified in the paper
default_num_training_updates = 25000 # 25K instead of 250K specified in the paper
default_num_hiddens = 128
default_num_residual_hiddens = 32 # 32 instead of 256 specified in the paper
default_num_residual_layers = 2

npimg = (npimg - np.min(npimg))/(np.max(npimg) - np.min(npimg))
Binary file added results/1/validation_images.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 results/2/loss.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 results/2/model.pth
Binary file not shown.
Binary file added results/2/original_images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions results/2/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
default_batch_size = 32 # 32 instead of 128 specified in the paper
default_num_training_updates = 25000 # 25K instead of 250K specified in the paper
default_num_hiddens = 128
default_num_residual_hiddens = 32 # 32 instead of 256 specified in the paper
default_num_residual_layers = 2

Delete the +0.5 in and out of make_grid and use normalize = True
Binary file added results/2/validation_images.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 results/3/loss.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 results/3/model.pth
Binary file not shown.
Binary file added results/3/original_images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions results/3/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
default_batch_size = 32 # 32 instead of 128 specified in the paper
default_num_training_updates = 25000 # 25K instead of 250K specified in the paper
default_num_hiddens = 128
default_num_residual_hiddens = 32 # 32 instead of 256 specified in the paper
default_num_residual_layers = 3

Delete the +0.5 in and out of make_grid and use normalize = True
Binary file added results/3/validation_images.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 results/4/loss.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 results/4/model.pth
Binary file not shown.
Binary file added results/4/original_images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions results/4/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
default_batch_size = 64 # 32 instead of 128 specified in the paper
default_num_training_updates = 25000 # 25K instead of 250K specified in the paper
default_num_hiddens = 128
default_num_residual_hiddens = 32 # 32 instead of 256 specified in the paper
default_num_residual_layers = 2

Delete the +0.5 in and out of make_grid and use normalize = True
Binary file added results/4/validation_images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions results/5/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
default_batch_size = 64 # 32 instead of 128 specified in the paper
default_num_training_updates = 25000 # 25K instead of 250K specified in the paper
default_num_hiddens = 128
default_num_residual_hiddens = 64 # 32 instead of 256 specified in the paper
default_num_residual_layers = 2

Delete the +0.5 in and out of make_grid and use normalize = True
Binary file added results/6S/loss.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 results/6S/model.pth
Binary file not shown.
Binary file added results/6S/original_images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions results/6S/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
default_batch_size = 32 # 32 instead of 128 specified in the paper
default_num_training_updates = 250 # 25K instead of 250K specified in the paper
default_num_hiddens = 128
default_num_residual_hiddens = 32 # 32 instead of 256 specified in the paper
default_num_residual_layers = 2

Delete the +0.5 in and out of make_grid and use normalize = True
Binary file added results/6S/validation_images.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 results/StanfordCars/1500/loss.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 results/StanfordCars/1500/model.pth
Binary file not shown.
Binary file added results/StanfordCars/1500/original_images.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 results/StanfordCars/1500/validation_images.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 results/StanfordCars/250/loss.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 results/StanfordCars/250/model.pth
Binary file not shown.
Binary file added results/StanfordCars/250/original_images.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 results/StanfordCars/250/validation_images.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 results/StanfordCars/2500/loss.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 results/StanfordCars/2500/model.pth
Binary file not shown.
Binary file added results/StanfordCars/2500/original_images.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 results/StanfordCars/2500/validation_images.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 results/StanfordCars/5000/loss.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 results/StanfordCars/5000/model.pth
Binary file not shown.
Binary file added results/StanfordCars/5000/original_images.png
Binary file added results/StanfordCars/5000/validation_images.png
Binary file added results/StanfordCars/750/loss.png
Binary file added results/StanfordCars/750/model.pth
Binary file not shown.
Binary file added results/StanfordCars/750/original_images.png
Binary file added results/StanfordCars/750/validation_images.png
Binary file added src/__pycache__/auto_encoder.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/celebA_dataset.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/cifar10_dataset.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/cityscapes_dataset.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/configuration.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/decoder.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/encoder.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/evaluator.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file added src/__pycache__/general_dataset.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/mnist_dataset.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/residual.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/residual_stack.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file added src/__pycache__/trainer.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/vector_quantizer.cpython-39.pyc
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion src/cifar10_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(self, batch_size, path, shuffle_dataset=True):
pin_memory=True
)

self._train_data_variance = np.var(self._training_data.train_data / 255.0)
self._train_data_variance = np.var(self._training_data.data / 255.0)

@property
def training_data(self):
Expand Down
15 changes: 11 additions & 4 deletions src/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,16 @@ class Configuration(object):
default_use_kaiming_normal = True

default_shuffle_dataset = True

default_dataset_name = "Cifar10"

def __init__(self, batch_size=default_batch_size, num_training_updates=default_num_training_updates, \
num_hiddens=default_num_hiddens, num_residual_hiddens=default_num_residual_hiddens, \
num_residual_layers=default_num_residual_layers, embedding_dim=default_embedding_dim, \
num_embeddings=default_num_embeddings, commitment_cost=default_commitment_cost, \
decay=default_decay, learning_rate=default_learning_rate, use_kaiming_normal=default_use_kaiming_normal, \
shuffle_dataset=default_shuffle_dataset):

shuffle_dataset=default_shuffle_dataset, dataset_name = default_dataset_name):
self._dataset_name = dataset_name
self._batch_size = batch_size
self._num_training_updates = num_training_updates
self._num_hiddens = num_hiddens
Expand All @@ -106,7 +108,7 @@ def __init__(self, batch_size=default_batch_size, num_training_updates=default_n
self._learning_rate = learning_rate
self._use_kaiming_normal = use_kaiming_normal
self._shuffle_dataset = shuffle_dataset

@property
def batch_size(self):
return self._batch_size
Expand Down Expand Up @@ -154,6 +156,10 @@ def use_kaiming_normal(self):
@property
def shuffle_dataset(self):
return self._shuffle_dataset

@property
def dataset_name(self):
return self._dataset_name

@staticmethod
def build_from_args(args):
Expand All @@ -169,5 +175,6 @@ def build_from_args(args):
decay=args.decay,
learning_rate=args.learning_rate,
use_kaiming_normal=args.use_kaiming_normal,
shuffle_dataset=not args.unshuffle_dataset
shuffle_dataset=not args.unshuffle_dataset,
dataset_name = args.dataset_name
)
4 changes: 2 additions & 2 deletions src/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def reconstruct(self):
_, self._train_reconstructions, _, _ = self._model.vq_vae(train_originals)

def save_original_images_plot(self, path):
self._save_image(make_grid(self._valid_originals.cpu()+0.5), path)
self._save_image(make_grid(self._valid_originals.cpu(), normalize = True), path)

def save_validation_reconstructions_plot(self, path):
self._save_image(make_grid(self._valid_reconstructions.cpu().data)+0.5, path)
self._save_image(make_grid(self._valid_reconstructions.cpu().data, normalize = True), path)

def save_embedding_plot(self, path):
try:
Expand Down
20 changes: 16 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
from auto_encoder import AutoEncoder
from trainer import Trainer
from evaluator import Evaluator
from stanfordCars_dataset import StanfordCarsDataset
from cifar10_dataset import Cifar10Dataset
from configuration import Configuration

import torchvision.datasets as datasets
import torch
import torch.optim as optim
import os
Expand All @@ -50,27 +52,37 @@
parser.add_argument('--learning_rate', nargs='?', default=Configuration.default_learning_rate, type=float, help='The learning rate of the optimizer during training updates')
parser.add_argument('--use_kaiming_normal', nargs='?', default=Configuration.default_use_kaiming_normal, type=bool, help='Use the weight normalization proposed in [He, K et al., 2015]')
parser.add_argument('--unshuffle_dataset', default=not Configuration.default_shuffle_dataset, action='store_true', help='Do not shuffle the dataset before training')
parser.add_argument('--data_path', nargs='?', default='data', type=str, help='The path of the data directory')
parser.add_argument('--results_path', nargs='?', default='results', type=str, help='The path of the results directory')
parser.add_argument('--data_path', nargs='?', default='data/', type=str, help='The path of the data directory')
parser.add_argument('--results_path', nargs='?', default='results/', type=str, help='The path of the results directory')
parser.add_argument('--loss_plot_name', nargs='?', default='loss.png', type=str, help='The file name of the training loss plot')
parser.add_argument('--model_name', nargs='?', default='model.pth', type=str, help='The file name of trained model')
parser.add_argument('--original_images_name', nargs='?', default='original_images.png', type=str, help='The file name of the original images used in evaluation')
parser.add_argument('--validation_images_name', nargs='?', default='validation_images.png', type=str, help='The file name of the reconstructed images used in evaluation')
parser.add_argument('--dataset_name', nargs='?', default=Configuration.default_dataset_name, type=str, help='The dataset to use')
args = parser.parse_args()

# Dataset and model hyperparameters
configuration = Configuration.build_from_args(args)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Use GPU if cuda is available


print("GPU is available: ", torch.cuda.is_available())

# Set the result path and create the directory if it doesn't exist
results_path = '..' + os.sep + args.results_path
if not os.path.isdir(results_path):
os.mkdir(results_path)

dataset_path = '..' + os.sep + args.data_path

dataset = Cifar10Dataset(configuration.batch_size, dataset_path, configuration.shuffle_dataset) # Create an instance of CIFAR10 dataset
if configuration.dataset_name != "Cifar10":
print("We are using the StanfordCars Dataset!")
dataset = StanfordCarsDataset(configuration.batch_size, dataset_path, configuration.shuffle_dataset) # Create an instance of StanfordCars dataset

else:
print("We are using the Cifar10 Dataset!")
dataset = Cifar10Dataset(configuration.batch_size, dataset_path, configuration.shuffle_dataset) # Create an instance of CIFAR10 dataset

auto_encoder = AutoEncoder(device, configuration).to(device) # Create an AutoEncoder model using our GPU device

optimizer = optim.Adam(auto_encoder.parameters(), lr=configuration.learning_rate, amsgrad=True) # Create an Adam optimizer instance
Expand Down
124 changes: 124 additions & 0 deletions src/stanfordCars_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import os
import shutil
import torch
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

class StanfordCarsDataset(object):

def __init__(self, batch_size, path, shuffle_dataset=True):
if not os.path.isdir(path):
os.mkdir(path)

datasets.StanfordCars(
root=path,
split="train",
download = True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))
])
)
datasets.StanfordCars(
root=path,
split="test",
download = True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))
])
)
path = os.path.join(path, "stanford_cars/")
# create temporary directory structure with class folders
temp_path_train = os.path.join(path, "temp_train")
temp_path_test = os.path.join(path, "temp_test")

if not os.path.isdir(temp_path_train):
os.mkdir(temp_path_train)

if not os.path.isdir(temp_path_test):
os.mkdir(temp_path_test)

for root, _, files in os.walk(os.path.join(path, "cars_train")):
for file in files:
src_path = os.path.join(root, file)
class_folder = os.path.basename(root)
dst_path = os.path.join(temp_path_train, class_folder, file)
if not os.path.isdir(os.path.join(temp_path_train, class_folder)):
os.mkdir(os.path.join(temp_path_train, class_folder))
shutil.copy(src_path, dst_path)

self._training_data = datasets.ImageFolder(
root=temp_path_train,
transform=transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
)

for root, _, files in os.walk(os.path.join(path, "cars_test")):
for file in files:
src_path = os.path.join(root, file)
class_folder = os.path.basename(root)
dst_path = os.path.join(temp_path_test, class_folder, file)
if not os.path.isdir(os.path.join(temp_path_test, class_folder)):
os.mkdir(os.path.join(temp_path_test, class_folder))
shutil.copy(src_path, dst_path)

self._validation_data = datasets.ImageFolder(
root=temp_path_test,
transform=transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
)

self._training_loader = DataLoader(
self._training_data,
batch_size=batch_size,
num_workers=2,
shuffle=shuffle_dataset,
pin_memory=True
)

self._validation_loader = DataLoader(
self._validation_data,
batch_size=batch_size,
num_workers=2,
shuffle=shuffle_dataset,
pin_memory=True
)

mean = torch.zeros(3)
sq_mean = torch.zeros(3)
for inputs, _ in self._training_loader:
mean += inputs.mean(dim=(0, 2, 3))
sq_mean += (inputs**2).mean(dim=(0, 2, 3))

mean /= len(self._training_loader)
sq_mean /= len(self._training_loader)
self._train_data_variance = (sq_mean - mean**2).sum()

@property
def training_data(self):
return self._training_data

@property
def validation_data(self):
return self._validation_data

@property
def training_loader(self):
return self._training_loader

@property
def validation_loader(self):
return self._validation_loader

@property
def train_data_variance(self):
return self._train_data_variance
7 changes: 4 additions & 3 deletions src/vector_quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ def forward(self, inputs):
- 2 * torch.matmul(flat_input, self._embedding.weight.t()))

# Encoding
encoding_indices = torch.argmin(distances, dim=1).unsqueeze(1)
encodings = torch.zeros(encoding_indices.shape[0], self._num_embeddings).to(self._device)
encodings.scatter_(1, encoding_indices, 1)
# encoding_indices = torch.argmin(distances, dim=1).unsqueeze(1)
# encodings = torch.zeros(encoding_indices.shape[0], self._num_embeddings).to(self._device)
# encodings.scatter_(1, encoding_indices, 1)
encodings = nn.Softmax(- distances, dim = 1).unsequeeze(1)

# Quantize and unflatten
quantized = torch.matmul(encodings, self._embedding.weight).view(input_shape)
Expand Down