Skip to content

Commit

Permalink
NEW DATASET AND MODEL
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoValente3 committed Dec 5, 2021
1 parent 564ea49 commit 5379a22
Show file tree
Hide file tree
Showing 44 changed files with 1,624 additions and 1,616 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ dmypy.json
__pycache__/
images/

Report\proj_reference

Report/images/
*.aux
*.fdb_latexmk
Expand Down
545 changes: 164 additions & 381 deletions AE.ipynb

Large diffs are not rendered by default.

75 changes: 47 additions & 28 deletions MNIST_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,70 @@ class MNISTData:
"""MNIST data class. You can adjust the data_fraction to use when creating
the data, according to your system capabilities."""

def __init__(self, data_fraction=1., zoom_factor = None, flat = True):
def __init__(self, data_fraction = 1., size_initial = 20, size_final = 8, color_depth = 5, flat = True):
data = mnist
(self.x_train, self.y_train), (self.x_test, self.y_test) = data.load_data()

self.get_subset_of_data(data_fraction)

self.convert_label_to_categorical()

self.normalize_mnist_images()
#self.normalize_mnist_images()

if zoom_factor is not None:
self.interpolate(zoom_factor)


self.crop_and_interpolate(size_initial, size_final, color_depth)

self.reshape_to_color_channel()
#self.reshape_to_color_channel()

#self.crop_center( 22)

if flat is True:
self.flatten_pictures()
#if flat is True:
# self.flatten_pictures()

def crop_center(self,crop):
#y,x = self.x_test
startx = x//2-(crop//2)
starty = y//2-(crop//2)
self.x_test= self.x_test[starty:starty+crop,startx:startx+crop]
def get_subset_of_data(self, data_fraction):
"""Choosing a fraction of data according to the machine capabilities"""
index = int(len(self.x_train) * data_fraction)
self.x_train = self.x_train[:index]
self.y_train = self.y_train[:index]
index = int(len(self.x_test) * data_fraction)
self.x_test = self.x_test[:index]
self.y_test = self.y_test[:index]

def convert_label_to_categorical(self):
self.y_train = to_categorical(self.y_train)
self.y_test = to_categorical(self.y_test)

def crop_and_interpolate(self, size_initial, size_final, color_depth):
#defisce gli indici del cropping definito da size_initial
bordo = (28-size_initial)//2
bordo_top = -bordo
if bordo == 0:
bordo_top = None

X_train_flat_zoom = []
X_test_flat_zoom = []

for image in self.x_train:
tmp = scipy.ndimage.zoom(image[bordo:bordo_top, bordo:bordo_top],
size_final/size_initial).flatten()
tmp = (tmp/(256//2**color_depth)).astype(int)
X_train_flat_zoom.append(tmp/2**color_depth)
X_train_flat_zoom = np.array(X_train_flat_zoom)
self.x_train=X_train_flat_zoom

#processing Test Set
for image in self.x_test:
tmp = scipy.ndimage.zoom(image[bordo:bordo_top, bordo:bordo_top],
size_final/size_initial).flatten()
tmp = (tmp/(256//2**color_depth)).astype(int)
X_test_flat_zoom.append(tmp/2**color_depth)
X_test_flat_zoom = np.array(X_test_flat_zoom)
self.x_test=X_test_flat_zoom



def interpolate(self, zoom_factor):
self.x_train = scipy.ndimage.zoom(self.x_train,
(1, zoom_factor, zoom_factor))
self.x_test = scipy.ndimage.zoom(self.x_test,
(1, zoom_factor, zoom_factor))

def convert_label_to_categorical(self):
self.y_train = to_categorical(self.y_train)
self.y_test = to_categorical(self.y_test)

def normalize_mnist_images(self):
self.x_train = self.x_train / 255.0
Expand All @@ -63,14 +89,7 @@ def reshape_to_color_channel(self):
self.x_train = self.x_train[:, :, :, np.newaxis]
self.x_test = self.x_test[:, :, :, np.newaxis]

def get_subset_of_data(self, data_fraction):
"""Choosing a fraction of data according to the machine capabilities"""
index = int(len(self.x_train) * data_fraction)
self.x_train = self.x_train[:index]
self.y_train = self.y_train[:index]
index = int(len(self.x_test) * data_fraction)
self.x_test = self.x_test[:index]
self.y_test = self.y_test[:index]


def flatten_pictures(self):
self.x_train = self.x_train.reshape(self.x_train.shape[0], -1)
Expand Down
512 changes: 259 additions & 253 deletions VAE.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 5379a22

Please sign in to comment.