How to build an unsupervised CNN model with keras/tensorflow?
Asked Answered
H

1

7

I'm trying to build a CNN for an image-to-image translation application, the input of the model is an image, and the output is a confidence map. There are no labeled confidence as the ground truth during training, but a loss function is designed to guide the model to a proper output.

I'm having trouble building the neural network with keras, because the model.fit() function needs both the training data and the labeled data (ground truth data).

So my question is ,is there a way to build an unsupervised CNN with keras or tensorflow? If so, what should i do to build one? Is there an application example or something i can refer to?

Thanks in advance!

Heywood answered 15/4, 2019 at 1:41 Comment(3)
You need to use TensorFlow's low level APIs. Keras is intended for supervised learning. The low level APIs provide more flexibility. See here -> tensorflow.org/guide/low_level_introRuhnke
Thanks for you reply. In this website, the low level APIs also need to define the expected output y_true, could you be a little bit more specific on how to build an unsupervised neural network with the low level APIs?Heywood
@Jemma, Is your issue resolved now? Else, you can try using Auto Encoders, to build Unsupervised CNN.Chyle
C
7

You can build an unsupervised CNN with keras using Auto Encoders. The code for it, for Fashion MNIST Data, is shown below:

# Python ≥3.5 is required
import sys
assert sys.version_info >= (3, 5)

# Scikit-Learn ≥0.20 is required
import sklearn
assert sklearn.__version__ >= "0.20"

# TensorFlow ≥2.0-preview is required
import tensorflow as tf
from tensorflow import keras
assert tf.__version__ >= "2.0"

# Common imports
import numpy as np
import os

(X_train_full, y_train_full), (X_test, y_test) = keras.datasets.fashion_mnist.load_data()
X_train_full = X_train_full.astype(np.float32) / 255
X_test = X_test.astype(np.float32) / 255
X_train, X_valid = X_train_full[:-5000], X_train_full[-5000:]
y_train, y_valid = y_train_full[:-5000], y_train_full[-5000:]

def rounded_accuracy(y_true, y_pred):
    return keras.metrics.binary_accuracy(tf.round(y_true), tf.round(y_pred))

tf.random.set_seed(42)
np.random.seed(42)

conv_encoder = keras.models.Sequential([
    keras.layers.Reshape([28, 28, 1], input_shape=[28, 28]),
    keras.layers.Conv2D(16, kernel_size=3, padding="SAME", activation="selu"),
    keras.layers.MaxPool2D(pool_size=2),
    keras.layers.Conv2D(32, kernel_size=3, padding="SAME", activation="selu"),
    keras.layers.MaxPool2D(pool_size=2),
    keras.layers.Conv2D(64, kernel_size=3, padding="SAME", activation="selu"),
    keras.layers.MaxPool2D(pool_size=2)
])
conv_decoder = keras.models.Sequential([
    keras.layers.Conv2DTranspose(32, kernel_size=3, strides=2, padding="VALID", activation="selu",
                                 input_shape=[3, 3, 64]),
    keras.layers.Conv2DTranspose(16, kernel_size=3, strides=2, padding="SAME", activation="selu"),
    keras.layers.Conv2DTranspose(1, kernel_size=3, strides=2, padding="SAME", activation="sigmoid"),
    keras.layers.Reshape([28, 28])
])
conv_ae = keras.models.Sequential([conv_encoder, conv_decoder])

conv_ae.compile(loss="binary_crossentropy", optimizer=keras.optimizers.SGD(lr=1.0),
                metrics=[rounded_accuracy])
history = conv_ae.fit(X_train, X_train, epochs=5,
                      validation_data=[X_valid, X_valid])

conv_encoder.summary()
conv_decoder.summary()

You can refer to this link for more information.

Chyle answered 12/9, 2019 at 1:27 Comment(2)
Just came across this. This works - but I would like to use my own data. How would I put in my own dataset into this code?Scour
You can use either ImageDataGenerator or image_dataset_from_directory from Keras to load custom dataset. tensorflow.org/api_docs/python/tf/keras/preprocessing/image/…. keras.io/api/data_loadingChyle

© 2022 - 2024 — McMap. All rights reserved.