<filename>AutoencoderCleanUp/dataset_autoencoder.py
print('Importing...')
import argparse
import dataset_creator

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf

from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from tensorflow import keras
from tensorflow.keras import layers, losses
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Model

from PIL import Image, ImageDraw
from os import listdir
from os.path import isfile, join

parser = argparse.ArgumentParser(description='Run the image denoiser model')
parser.add_argument('--train', help='Train the model', action='store_true')
args = parser.parse_args()

MODEL_PATH = '../models/test'
IMG_FOLDER = '../train/'
DATASET_SIZE = 15000
IMG_SIZE = 256
# IMG_SIZE = 128

def create_dataset():
  print('Loading dataset...')

  train_data, noisy_train_data = dataset_creator.load_data(DATASET_SIZE) #8000
  test_data, noisy_test_data = dataset_creator.load_data(int(DATASET_SIZE/10)) #1000

  x_train = train_data.astype('float32') / 255.
  x_train_noisy = noisy_train_data.astype('float32') / 255.
  x_test = test_data.astype('float32') / 255.
  x_test_noisy = noisy_test_data.astype('float32') / 255.

  x_train = x_train[..., tf.newaxis]
  x_train_noisy = x_train_noisy[..., tf.newaxis]
  x_test = x_test[..., tf.newaxis]
  x_test_noisy = x_test_noisy[..., tf.newaxis]

  # n = 10
  # plt.figure(figsize=(20, 2))
  # for i in range(n):
  #     ax = plt.subplot(1, n, i + 1)
  #     plt.title("original + noise")
  #     plt.imshow(tf.squeeze(x_test_noisy[i]))
  #     plt.gray()
  # plt.show()
  return x_train, x_train_noisy, x_test, x_test_noisy

def get_test_imgs():
  print('Loading test dataset...')

  test_data, noisy_test_data = dataset_creator.load_data(int(DATASET_SIZE/10))

  x_test_noisy = noisy_test_data.astype('float32') / 255.
  x_test_noisy = x_test_noisy[..., tf.newaxis]

  return x_test_noisy

def get_real_imgs():
  print('Loading validation dataset...')

  images = [f for f in listdir(IMG_FOLDER) if isfile(join(IMG_FOLDER, f)) and f.endswith(".tif")]
  images.sort()

  real_imgs = []

  for imagePath in images:
    im = Image.open(IMG_FOLDER + imagePath)
    im = im.convert('L')
    im = im.resize((IMG_SIZE, IMG_SIZE))
    # arr = im.point(lambda i:i*(1./256)).convert('L')
    # arr = np.array(im.point(lambda i:i*(1./256)).convert('L'))
    arr = np.array(im.convert('L'))
    real_imgs.append(arr)

  real_imgs = np.array(real_imgs)

  y_test = real_imgs.astype('float32') / 255.
  y_test = y_test[..., tf.newaxis]

  return y_test


class Denoise(Model):
  def __init__(self):
    super(Denoise, self).__init__()
    self.encoder = tf.keras.Sequential([
      layers.Input(shape=(IMG_SIZE, IMG_SIZE, 1)), 
      layers.Conv2D(64, (3,3), activation='relu', padding='same', strides=2),
      layers.Conv2D(32, (3,3), activation='relu', padding='same', strides=2)])

    self.decoder = tf.keras.Sequential([
      layers.Conv2DTranspose(32, kernel_size=3, strides=2, activation='relu', padding='same'),
      layers.Conv2DTranspose(64, kernel_size=3, strides=2, activation='relu', padding='same'),
      layers.Conv2D(1, kernel_size=(3,3), activation='sigmoid', padding='same')])

  def call(self, x):
    encoded = self.encoder(x)
    decoded = self.decoder(encoded)
    return decoded


x_train, y_train, x_test, y_test = create_dataset()

autoencoder = Denoise()
if(MODEL_PATH and not args.train):
  print('Loading model...')
  autoencoder = keras.models.load_model(MODEL_PATH)
else:
  print('Training model...')
  autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError())
  autoencoder.fit(y_train, x_train,
                  epochs=10,
                  shuffle=True,
                validation_data=(y_test, x_test))
  autoencoder.save(MODEL_PATH)
  autoencoder.encoder.summary()

decoded_imgs = autoencoder.call(x_test)

# n = 5
# plt.figure(figsize=(20, 7))
# for i in range(n):

#     # display original + noise
#     ax = plt.subplot(3, n, i + 1)
#     plt.title("original + noise")
#     plt.imshow(tf.squeeze(y_test[i]))
#     plt.gray()
#     ax.get_xaxis().set_visible(False)
#     ax.get_yaxis().set_visible(False)

#     # display reconstruction
#     bx = plt.subplot(3, n, i + n + 1)
#     plt.title("original")
#     plt.imshow(tf.squeeze(x_test[i]))
#     plt.gray()
#     bx.get_xaxis().set_visible(False)
#     bx.get_yaxis().set_visible(False)

#     # display reconstruction
#     cx = plt.subplot(3, n, i + 2*n + 1)
#     plt.title("reconstructed")
#     plt.imshow(tf.squeeze(decoded_imgs[i]))
#     plt.gray()
#     cx.get_xaxis().set_visible(False)
#     cx.get_yaxis().set_visible(False)
# plt.show()


real_imgs = get_real_imgs()
decoded_real_imgs = autoencoder.call(real_imgs)

n = 5
plt.figure(figsize=(20, 4))
for i in range(n):

    # display original from microscope
    ax = plt.subplot(2, n, i + 1)
    plt.title("original microscope")
    plt.imshow(tf.squeeze(real_imgs[i]))
    img = tf.keras.preprocessing.image.array_to_img(real_imgs[i])
    img.show()
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display reconstruction
    bx = plt.subplot(2, n, i + n + 1)
    plt.title("reconstructed")
    plt.imshow(tf.squeeze(decoded_real_imgs[i]))
    plt.gray()
    bx.get_xaxis().set_visible(False)
    bx.get_yaxis().set_visible(False)
plt.show()




<filename>AutoencoderCleanUp/dataset_autoencoder.py
print('Importing...')
import argparse
import dataset_creator

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf

from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from tensorflow import keras
from tensorflow.keras import layers, losses
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Model

from PIL import Image, ImageDraw
from os import listdir
from os.path import isfile, join

parser = argparse.ArgumentParser(description='Run the image denoiser model')
parser.add_argument('--train', help='Train the model', action='store_true')
args = parser.parse_args()

MODEL_PATH = '../models/test'
IMG_FOLDER = '../train/'
DATASET_SIZE = 15000
IMG_SIZE = 256
# IMG_SIZE = 128

def create_dataset():
  print('Loading dataset...')

  train_data, noisy_train_data = dataset_creator.load_data(DATASET_SIZE) #8000
  test_data, noisy_test_data = dataset_creator.load_data(int(DATASET_SIZE/10)) #1000

  x_train = train_data.astype('float32') / 255.
  x_train_noisy = noisy_train_data.astype('float32') / 255.
  x_test = test_data.astype('float32') / 255.
  x_test_noisy = noisy_test_data.astype('float32') / 255.

  x_train = x_train[..., tf.newaxis]
  x_train_noisy = x_train_noisy[..., tf.newaxis]
  x_test = x_test[..., tf.newaxis]
  x_test_noisy = x_test_noisy[..., tf.newaxis]

  # n = 10
  # plt.figure(figsize=(20, 2))
  # for i in range(n):
  #     ax = plt.subplot(1, n, i + 1)
  #     plt.title("original + noise")
  #     plt.imshow(tf.squeeze(x_test_noisy[i]))
  #     plt.gray()
  # plt.show()
  return x_train, x_train_noisy, x_test, x_test_noisy

def get_test_imgs():
  print('Loading test dataset...')

  test_data, noisy_test_data = dataset_creator.load_data(int(DATASET_SIZE/10))

  x_test_noisy = noisy_test_data.astype('float32') / 255.
  x_test_noisy = x_test_noisy[..., tf.newaxis]

  return x_test_noisy

def get_real_imgs():
  print('Loading validation dataset...')

  images = [f for f in listdir(IMG_FOLDER) if isfile(join(IMG_FOLDER, f)) and f.endswith(".tif")]
  images.sort()

  real_imgs = []

  for imagePath in images:
    im = Image.open(IMG_FOLDER + imagePath)
    im = im.convert('L')
    im = im.resize((IMG_SIZE, IMG_SIZE))
    # arr = im.point(lambda i:i*(1./256)).convert('L')
    # arr = np.array(im.point(lambda i:i*(1./256)).convert('L'))
    arr = np.array(im.convert('L'))
    real_imgs.append(arr)

  real_imgs = np.array(real_imgs)

  y_test = real_imgs.astype('float32') / 255.
  y_test = y_test[..., tf.newaxis]

  return y_test


class Denoise(Model):
  def __init__(self):
    super(Denoise, self).__init__()
    self.encoder = tf.keras.Sequential([
      layers.Input(shape=(IMG_SIZE, IMG_SIZE, 1)), 
      layers.Conv2D(64, (3,3), activation='relu', padding='same', strides=2),
      layers.Conv2D(32, (3,3), activation='relu', padding='same', strides=2)])

    self.decoder = tf.keras.Sequential([
      layers.Conv2DTranspose(32, kernel_size=3, strides=2, activation='relu', padding='same'),
      layers.Conv2DTranspose(64, kernel_size=3, strides=2, activation='relu', padding='same'),
      layers.Conv2D(1, kernel_size=(3,3), activation='sigmoid', padding='same')])

  def call(self, x):
    encoded = self.encoder(x)
    decoded = self.decoder(encoded)
    return decoded


x_train, y_train, x_test, y_test = create_dataset()

autoencoder = Denoise()
if(MODEL_PATH and not args.train):
  print('Loading model...')
  autoencoder = keras.models.load_model(MODEL_PATH)
else:
  print('Training model...')
  autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError())
  autoencoder.fit(y_train, x_train,
                  epochs=10,
                  shuffle=True,
                validation_data=(y_test, x_test))
  autoencoder.save(MODEL_PATH)
  autoencoder.encoder.summary()

decoded_imgs = autoencoder.call(x_test)

# n = 5
# plt.figure(figsize=(20, 7))
# for i in range(n):

#     # display original + noise
#     ax = plt.subplot(3, n, i + 1)
#     plt.title("original + noise")
#     plt.imshow(tf.squeeze(y_test[i]))
#     plt.gray()
#     ax.get_xaxis().set_visible(False)
#     ax.get_yaxis().set_visible(False)

#     # display reconstruction
#     bx = plt.subplot(3, n, i + n + 1)
#     plt.title("original")
#     plt.imshow(tf.squeeze(x_test[i]))
#     plt.gray()
#     bx.get_xaxis().set_visible(False)
#     bx.get_yaxis().set_visible(False)

#     # display reconstruction
#     cx = plt.subplot(3, n, i + 2*n + 1)
#     plt.title("reconstructed")
#     plt.imshow(tf.squeeze(decoded_imgs[i]))
#     plt.gray()
#     cx.get_xaxis().set_visible(False)
#     cx.get_yaxis().set_visible(False)
# plt.show()


real_imgs = get_real_imgs()
decoded_real_imgs = autoencoder.call(real_imgs)

n = 5
plt.figure(figsize=(20, 4))
for i in range(n):

    # display original from microscope
    ax = plt.subplot(2, n, i + 1)
    plt.title("original microscope")
    plt.imshow(tf.squeeze(real_imgs[i]))
    img = tf.keras.preprocessing.image.array_to_img(real_imgs[i])
    img.show()
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display reconstruction
    bx = plt.subplot(2, n, i + n + 1)
    plt.title("reconstructed")
    plt.imshow(tf.squeeze(decoded_real_imgs[i]))
    plt.gray()
    bx.get_xaxis().set_visible(False)
    bx.get_yaxis().set_visible(False)
plt.show()




# IMG_SIZE = 128 #8000 #1000 # n = 10 # plt.figure(figsize=(20, 2)) # for i in range(n): #     ax = plt.subplot(1, n, i + 1) #     plt.title("original + noise") #     plt.imshow(tf.squeeze(x_test_noisy[i])) #     plt.gray() # plt.show() # arr = im.point(lambda i:i*(1./256)).convert('L') # arr = np.array(im.point(lambda i:i*(1./256)).convert('L')) # n = 5 # plt.figure(figsize=(20, 7)) # for i in range(n): #     # display original + noise #     ax = plt.subplot(3, n, i + 1) #     plt.title("original + noise") #     plt.imshow(tf.squeeze(y_test[i])) #     plt.gray() #     ax.get_xaxis().set_visible(False) #     ax.get_yaxis().set_visible(False) #     # display reconstruction #     bx = plt.subplot(3, n, i + n + 1) #     plt.title("original") #     plt.imshow(tf.squeeze(x_test[i])) #     plt.gray() #     bx.get_xaxis().set_visible(False) #     bx.get_yaxis().set_visible(False) #     # display reconstruction #     cx = plt.subplot(3, n, i + 2*n + 1) #     plt.title("reconstructed") #     plt.imshow(tf.squeeze(decoded_imgs[i])) #     plt.gray() #     cx.get_xaxis().set_visible(False) #     cx.get_yaxis().set_visible(False) # plt.show() # display original from microscope # display reconstruction