diff --git a/dataset.py b/dataset.py index deb22e6..dd8c855 100644 --- a/dataset.py +++ b/dataset.py @@ -1,5 +1,6 @@ import numpy as np from datasets import load_dataset +from keras.datasets import mnist from binreader import open_binary_file from pathlib import Path from typing import Dict @@ -7,7 +8,9 @@ def import_dataset(name:str, split:float=0.2, shuffle=True, extra_args:Dict[str, bool]={}): datasets = {"minds14":import_minds_hugging_face, "trend":import_data_TREND, + "mnist": import_mnist, } + if name in datasets: return datasets[name](split, shuffle, extra_args) else: @@ -21,6 +24,29 @@ def import_minds_hugging_face(split:float, shuffle:bool, extra_args:Dict[str, bo return (minds["train"], minds["test"]) +def import_mnist(split:float, shuffle:bool, extra_args:Dict[str, bool]): + print(Warning("Split is not supported for MNIST yet")) + (data_train, labels_train), (data_test, labels_test) = mnist.load_data() + + max_classes = 10 + if "max_classes" in extra_args: + max_classes = extra_args["max_classes"] + + impurity = 0 + if "impurity" in extra_args: + impurity = extra_args["impurity"] + + indicies_impure = np.where(np.random.rand(len(labels_train))=0.7, True, False)])] = 1 +new_labels[logical_and_array([torch.where(model(torch.as_tensor(data_test, dtype=torch.float32, device=config["device"]))<=1-0.7, True, False)])] = 0 +print(np.where(new_labels.cpu().numpy()[:,0]==labels_test_impure[:,0])[0].shape) +print(np.where(new_labels.cpu().numpy()[:,0]==labels_test[:,0])[0].shape) +print(new_labels) writer.flush() writer.close() \ No newline at end of file diff --git a/model.py b/model.py index a11da69..b756468 100644 --- a/model.py +++ b/model.py @@ -1,23 +1,17 @@ import torch.nn as F import torch -class SimpleModel(F.Module): +class SimpleSignalModel(F.Module): def __init__(self): - super(SimpleModel, self).__init__() + super(SimpleSignalModel, self).__init__() self.layers = [] - self.conv1_sig = F.Conv1d(1, 64, kernel_size=15, padding=7) - self.layers.append(self.conv1_sig) + self.conv1 = F.Conv1d(1, 128, kernel_size=15, padding=7) + self.layers.append(self.conv1) - self.conv1_fft = F.Conv1d(2, 64, kernel_size=15, padding=7) - self.layers.append(self.conv1_fft) + self.batch_norm1 = F.BatchNorm1d(128) + self.layers.append(self.batch_norm1) - self.batch_norm1_sig = F.BatchNorm1d(64) - self.layers.append(self.batch_norm1_sig) - - self.batch_norm1_fft = F.BatchNorm1d(64) - self.layers.append(self.batch_norm1_fft) - self.conv2 = F.Conv1d(128, 128, kernel_size=7, padding=3) self.layers.append(self.conv2) @@ -54,13 +48,8 @@ def __init__(self): self.sigmoid = F.Sigmoid() def forward(self, x): - x_sig = self.maxpool(self.conv1_sig(x[:, :1, :])) - x_sig = self.dropout(self.activation(self.batch_norm1_sig(x_sig))) - - x_fft = self.maxpool(self.conv1_fft(x[:, 1:, :])) - x_fft = self.dropout(self.activation(self.batch_norm1_fft(x_fft))) - - x = torch.cat([x_sig, x_fft], axis=1) + x = self.maxpool(self.conv1(x)) + x = self.dropout(self.activation(self.batch_norm1(x))) x = self.dropout(self.activation(self.activation(self.batch_norm2(self.conv2(x))) + x)) x = self.maxpool(x) @@ -88,3 +77,64 @@ def save_txt(self, filename:str): f.write(str(layer._get_name) + "\n") f.close() +class SimpleImageModel(F.Module): + def __init__(self): + super(SimpleImageModel, self).__init__() + self.layers = [] + + self.conv1 = F.Conv2d(1, 8, kernel_size=5, stride=2) + self.layers.append(self.conv1) + + self.batch_norm1 = F.BatchNorm2d(8) + self.layers.append(self.batch_norm1) + + self.conv2 = F.Conv2d(8, 16, kernel_size=3) + self.layers.append(self.conv2) + + self.batch_norm2 = F.BatchNorm2d(16) + self.layers.append(self.batch_norm2) + + self.conv3 = F.Conv2d(16, 32, kernel_size=3) + self.layers.append(self.conv3) + + self.batch_norm3 = F.BatchNorm2d(32) + self.layers.append(self.batch_norm3) + + self.dense1 = F.Linear(32*64, 512) + self.layers.append(self.dense1) + self.dense2 = F.Linear(512, 1) + self.layers.append(self.dense2) + + self.dropout = F.Dropout(0) + self.layers.append(self.dropout) + + self.activation = F.ReLU() + self.layers.append(self.activation) + + self.flatten = F.Flatten() + self.sigmoid = F.Sigmoid() + + def forward(self, x): + x = self.conv1(x) + x = self.dropout(self.activation(self.batch_norm1(x))) + + x = self.dropout(self.activation(self.batch_norm2(self.conv2(x)))) + + x = self.dropout(self.activation(self.batch_norm3(self.conv3(x)))) + + x = self.flatten(x) + x = self.activation(self.dense1(x)) + x = self.sigmoid(self.dense2(x)) + + return x + + def save_txt(self, filename:str): + """Save the layers in a txt + + Args: + filename (str): path to the txt file + """ + with open(filename, 'w') as f: + for layer in self.layers: + f.write(str(layer._get_name) + "\n") + f.close() diff --git a/utils.py b/utils.py index 7e68c34..78199ed 100644 --- a/utils.py +++ b/utils.py @@ -1,6 +1,7 @@ from pathlib import Path import os import numpy as np +from typing import List def create_batch_tensorboard(dir:str): """Create the batch file to launch the TensorBoard @@ -14,4 +15,22 @@ def create_batch_tensorboard(dir:str): f.write(f"cd {os.getcwd()}\n") f.write(f'start "" http://localhost:{port}/#scalars\n') f.write("tensorboard --logdir " + dir + " --port " + str(port)) - f.close() \ No newline at end of file + f.close() + +def logical_and_array(arrays:List[np.ndarray]) -> np.ndarray: + """Return logical and for a list of arrays. + + Args: + arrays (List[np.ndarray]): The list of arrays + Returns: + (np.ndarray): The logical and of the list of arrays + """ + if len(arrays) == 1: + return arrays[0] + + result = arrays[0] + + for array in arrays[1:]: + result = np.logical_and(result, array) + + return result \ No newline at end of file