'Reshape' object is not iterable - python

I am trying to combine encoder and decoder in keras
Here is minimal code to test
Data load
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Flatten, Reshape
from keras.models import Model
from keras import backend as K
from keras.datasets import mnist
import numpy as np
(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_train = x_train[:5,:,:,]
x_test = x_test.astype('float32') / 255.
x_test = x_train
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format
Creating the model
input_img = Input(shape=(28, 28, 1))
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
encoderOutputFlat = Flatten()(encoded)
encoder = Model(input_img, encoderOutputFlat)
decoder_input = Input(Reshape((7,7,32)))(encoderOutputFlat)
decoder = Conv2D(32, (3, 3), activation='relu', padding='same')(decoder_input)
x = UpSampling2D((2, 2))(decoder)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
decoder = Model(decoder_input, decoded)
auto_input = Input(shape=(28,28,1))
encoded = encoder(auto_input)
decoded = decoder(encoded)
autoencoder = Model(auto_input, decoded)
Here I have the error
decoder_input = Input(Reshape((7,7,32)))(encoderOutputFlat)
How can I make it work?
I want to train it as one model. Then get values from encoder. Then I want to use decoder on the data separately.

Related

Tensorflow : convolutional autoencoder via subclassing

I was playing with some Keras samples, defining models through subclassing, but I can't get it working.
from keras import layers, Model
from keras.datasets import mnist
from keras.callbacks import TensorBoard
import numpy as np
import datetime
import os
class Encoder(Model):
def __init__(self, name: str = "encoder"):
super(Encoder, self).__init__(name=name)
self._conv16 = layers.Conv2D(16, (3, 3), activation='relu', padding='same', name="conv16")
self._conv8 = layers.Conv2D(8, (3, 3), activation='relu', padding='same', name="conv8")
self._max_pool = layers.MaxPooling2D((2, 2), padding='same', name="max_pool")
def call(self, inputs: np.ndarray):
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(inputs)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = layers.MaxPooling2D((2, 2), padding='same')(x)
# x = self._conv16(inputs)
# x = self._max_pool(x)
# x = self._conv8(x)
# x = self._max_pool(x)
# x = self._conv8(x)
# encoded = self._max_pool(x)
return encoded
class Decoder(Model):
def __init__(self, name: str = "decoder"):
super(Decoder, self).__init__(name=name)
self._conv16 = layers.Conv2D(16, (3, 3), activation='relu', padding='same', name="conv16")
self._conv8 = layers.Conv2D(8, (3, 3), activation='relu', padding='same', name="conv8")
self._conv1 = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same', name="conv1")
self._up_sampling = layers.UpSampling2D((2, 2), name="up_samp")
def call(self, inputs: np.ndarray):
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(inputs)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(16, (3, 3), activation='relu')(x)
x = layers.UpSampling2D((2, 2))(x)
decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
# x = self._conv8(inputs)
# x = self._up_sampling(x)
# x = self._conv8(x)
# x = self._up_sampling(x)
# x = self._conv16(x)
# x = self._up_sampling(x)
# decoded = self._conv1(x)
return decoded
class Autoencoder(Model):
def __init__(self, name: str = "autoencoder"):
super(Autoencoder, self).__init__(name=name)
self.encoder = Encoder()
self.decoder = Decoder()
def call(self, inputs: np.ndarray):
encoded = self.encoder(inputs)
reconstructed = self.decoder(encoded)
return reconstructed
(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
print(x_train.shape)
print(x_test.shape)
autoencoder = Autoencoder()
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.fit(x_train, x_train,
epochs=50,
batch_size=128,
shuffle=True,
validation_data=(x_test, x_test)
First of all, I can't get the commented code working. Whenever I try to use it, I get errors such "ValueError: Input 0 of layer "conv8" is incompatible with the layer: expected axis -1of input shape to have value 1, but received input with shape (60000, 56, 56, 8)". I can't spot a difference in the model definition.
Secondly, running the above code gives me:
ValueError: tf.function only supports singleton tf.Variables created on the first call. Make sure the tf.Variable is only created once or created outside tf.function. See https://www.tensorflow.org/guide/function#creating_tfvariables for more information.
I'm not sure what I'm doing wrong.
In your code, there are many issues that need to be addressed. For example,
Issue 1: In the call method, the inputs argument should be tensor and not numpy array.
Issue 2: In model subclassing, you should initiate the trainable layer in the init method or build method and use the instances in the call function. It's the reason for the following error that you got.
ValueError: tf.function only supports singleton tf.Variables were created on the first call. Make sure the tf. Variable is only created once or created outside tf. function. See https://www.tensorflow.org/guide/function#creating_tfvariables for more information.
Issue 3: In your encoder block, it seems that you used self._conv8 twice, which happens also in the decoder block. It shouldn't be like that. If you need to call them twice (or multiple times), you need to initiate each of them in the init (or build) method independently. I think it's one of the so-called downsides of subclassing approach that you need to initiate the N times layer in init and forward their instances in the call method another N times. It's the reason for the following error that you got. ( Side note, passing the same input to the same trainable layer is also referred to as weight sharing, which I don't think it's the intention here. )
ValueError: Input 0 of layer "conv8" is incompatible with the layer: expected axis -1of input shape to have value 1, but received input with shape (60000, 56, 56, 8)
Solution
By addressing the above issue, here is the working code. Also, note that it's better and proper to import keras from tensorflow.
Encoder
from tensorflow.keras import layers, Model
from tensorflow.keras.datasets import mnist
from tensorflow.keras.callbacks import TensorBoard
import numpy as np
class Encoder(Model):
def __init__(self, name: str = "encoder"):
super().__init__(name=name)
self._conv16 = layers.Conv2D(16, (3, 3), activation='relu',
padding='same', name="conv16")
self._conv8 = layers.Conv2D(8, (3, 3), activation='relu',
padding='same', name="conv8")
self._max_pool = layers.MaxPooling2D((2, 2), padding='same',
name="max_pool")
def call(self, inputs):
x = self._conv16(inputs)
x = self._max_pool(x)
encoded = self._conv8(x)
return encoded
Decoder
class Decoder(Model):
def __init__(self, name: str = "decoder"):
super(Decoder, self).__init__(name=name)
self._conv16 = layers.Conv2D(16, (3, 3),
activation='relu',
padding='same', name="conv16")
self._conv8 = layers.Conv2D(8, (3, 3),
activation='relu',
padding='same', name="conv8")
self._conv1 = layers.Conv2D(1, (3, 3),
activation='sigmoid',
padding='same', name="conv1")
self._up_sampling = layers.UpSampling2D((2, 2), name="up_samp")
def call(self, inputs):
x = self._conv8(inputs)
x = self._up_sampling(x)
x = self._conv16(x)
decoded = self._conv1(x)
return decoded
Autoencoder
class Autoencoder(Model):
def __init__(self, name: str = "autoencoder"):
super(Autoencoder, self).__init__(name=name)
self.encoder = Encoder()
self.decoder = Decoder()
def call(self, inputs):
encoded = self.encoder(inputs)
reconstructed = self.decoder(encoded)
return reconstructed
Execute
(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
print(x_train.shape)
print(x_test.shape)
autoencoder = Autoencoder()
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.fit(x_train, x_train,
epochs=50,
batch_size=128,
shuffle=True,
validation_data=(x_test, x_test))
Epoch 1/5
12ms/step - loss: 0.1100 - val_loss: 0.0676
Epoch 2/5
12ms/step - loss: 0.0666 - val_loss: 0.0651
Epoch 3/5
12ms/step - loss: 0.0649 - val_loss: 0.0641
Epoch 4/5
12ms/step - loss: 0.0642 - val_loss: 0.0635
Epoch 5/5
11ms/step - loss: 0.0636 - val_loss: 0.0631
Predict
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 2)
y_pred = autoencoder.predict(x_test)
ax[0].imshow(x_test[10, :,:, 0])
ax[1].imshow(y_pred[10, :,:, 0])

How can I calculate score of a new image using entrained autoencoder model for anomaly detection in tensorflow?

I am beginner in tensorflow and I am trying to create a simple autoencoder for images to detect anomalies.Firstly, I created a simple autoencoder using dogs images , now I want to use this model to reconstruct my tests images and compare the result using some metrics.So how can I do it on tensorflow (because I am beginner on tensorflow )
(I found the same idea implemented on numerical datasets , and also on MNIST dataset ).
this is my code:
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import LearningRateScheduler
BATCH_SIZE = 256
EPOCHS = 2
train_datagen = ImageDataGenerator(rescale=1./255)
train_batches = train_datagen.flow_from_directory('C:/MyPath/PetImages1',
target_size=(64,64), shuffle=True, class_mode='input', batch_size=BATCH_SIZE)
input_img = Input(shape=(64, 64, 3))
x = Conv2D(48, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(96, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(192, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
encoded = Conv2D(32, (1, 1), activation='relu', padding='same')(x)
latentSize = (8,8,32)
# DECODER
direct_input = Input(shape=latentSize)
x = Conv2D(192, (1, 1), activation='relu', padding='same')(direct_input)
x = UpSampling2D((2, 2))(x)
x = Conv2D(192, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(96, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(48, (3, 3), activation='relu', padding='same')(x)
decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
# COMPILE
encoder = Model(input_img, encoded)
decoder = Model(direct_input, decoded)
autoencoder = Model(input_img, decoder(encoded))
autoencoder.compile(optimizer='Adam', loss='binary_crossentropy')
autoencoder.save_weights('autoencoder_DogsAuto.h5')
history=autoencoder.fit_generator(train_batches,steps_per_epoch=10,epochs =
EPOCHS)
#Images for tests
testGene = train_datagen.flow_from_directory('C:/PetImages/',
target_size=(64,64), shuffle=True, class_mode='input',
batch_size=BATCH_SIZE)
restored = autoencoder.predict_generator(testGene,
steps=testGene.n/BATCH_SIZE)
image_height=64
image_width=64
image_channels=3
x_train = np.zeros((0, image_height, image_width, image_channels), dtype=float)
for x, _ in train_batches :
if train_batches.total_batches_seen > train_batches.n/BATCH_SIZE:
break
else:
x_train = np.r_[x_train,x]
pred=autoencoder.predict(train_batches, steps=train_batches.n/BATCH_SIZE)
from sklearn import metrics
score1=np.sqrt(metrics.mean_squared_error(pred,x_train ))
print(score1)
And I got this error:
Traceback (most recent call last):
File "c:\autoencoder_anomaly.py", line 196, in
score1=np.sqrt(metrics.mean_squared_error(pred,x_train ))
File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics_regression.py", line 252, in mean_squared_error
y_true, y_pred, multioutput)
File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics_regression.py", line 84, in _check_reg_targets
check_consistent_length(y_true, y_pred)
ValueError: Found input variables with inconsistent numbers of samples: [6, 0]
Note that I am using only 6 images.
So how can I calculate the error of the reconstructed image using metrics and the autoencoder Model on tensorflow ?
This is simply because of shape mismatch.
when you calculate mean squared error,it calculates the element wise error of ground truth values and estimated values. so pred.shape and train_batches.shape should be equal.check the input data shapes and make sure they are equal.
step 1:
get all training images from the generator and add to one array
x_test = np.zeros((0, image_height, image_width, image_color), dtype=float)
for x, _ in testGene:
if testGene.total_batches_seen > testGene.n/BATCH_SIZE:
break
else:
x_test = np.r_[x_test , x]
step 2 : prediction
pred=autoencoder.predict(testGene, steps=testGene.n/BATCH_SIZE)
step 3 : calculate the difference
score1=np.sqrt(metrics.mean_squared_error(pred,testGene))

binary activation function for autoencoder

I have an autoencoder that has two output( decoded,pred_w), one output is the reconstructed input image and other one is a reconstructed binary image. I used sigmoid activation function in the last layer but the outputs are float number and I need the network label each pixel as 0 or 1. I attached my code here. could you please guide me what should I do to solve this problem?
thanks.
from keras.layers import Input, Concatenate, GaussianNoise,Dropout
from keras.layers import Conv2D
from keras.models import Model
from keras.datasets import mnist
from keras.callbacks import TensorBoard
from keras import backend as K
from keras import layers
import matplotlib.pyplot as plt
import tensorflow as tf
import keras as Kr
import numpy as np
import pylab as pl
import matplotlib.cm as cm
import keract
from tensorflow.python.keras.layers import Lambda;
#-----------------building w train---------------------------------------------
w_main = np.random.randint(2,size=(1,4,4,1))
w_main=w_main.astype(np.float32)
w_expand=np.zeros((1,28,28,1),dtype='float32')
w_expand[:,0:4,0:4]=w_main
w_expand.reshape(1,28,28,1)
w_expand=np.repeat(w_expand,49999,0)
#-----------------building w validation---------------------------------------------
w_valid = np.random.randint(2,size=(1,4,4,1))
w_valid=w_valid.astype(np.float32)
wv_expand=np.zeros((1,28,28,1),dtype='float32')
wv_expand[:,0:4,0:4]=w_valid
wv_expand.reshape(1,28,28,1)
wv_expand=np.repeat(wv_expand,9999,0)
#-----------------building w test---------------------------------------------
w_test = np.random.randint(2,size=(1,4,4,1))
w_test=w_test.astype(np.float32)
wt_expand=np.zeros((1,28,28,1),dtype='float32')
wt_expand[:,0:4,0:4]=w_test
wt_expand.reshape(1,28,28,1)
#wt_expand=np.repeat(wt_expand,10000,0)
#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------
wtm=Input((28,28,1))
image = Input((28, 28, 1))
conv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1e')(image)
conv2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2e')(conv1)
conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e')(conv2)
DrO1=Dropout(0.25)(conv3)
encoded = Conv2D(1, (3, 3), activation='relu', padding='same',name='reconstructed_I')(DrO1)
#-----------------------adding w---------------------------------------
#add_const = Kr.layers.Lambda(lambda x: x + Kr.backend.constant(w_expand))
#encoded_merged=Kr.layers.Add()([encoded,wtm])
add_const = Kr.layers.Lambda(lambda x: x + wtm)
encoded_merged = add_const(encoded)
encoder=Model(inputs=image, outputs= encoded_merged)
encoder.summary()
#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
#encoded_merged = Input((28, 28, 2))
deconv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1d')(encoded_merged)
deconv2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2d')(deconv1)
deconv3 = Conv2D(8, (3, 3), activation='relu',padding='same', name='convl3d')(deconv2)
DrO2=Dropout(0.25)(deconv3)
decoded = Conv2D(1, (3, 3), activation='relu', padding='same', name='decoder_output')(DrO2)
#decoder=Model(inputs=encoded_merged, outputs=decoded)
#decoder.summary()
model=Model(inputs=image,outputs=decoded)
#----------------------w extraction------------------------------------
convw1 = Conv2D(16, (3,3), activation='relu', padding='same', name='conl1w')(decoded)
convw2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2w')(convw1)
convw3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='conl3w')(convw2)
DrO3=Dropout(0.25)(convw3)
pred_w = Conv2D(1, (1, 1), activation='sigmoid', padding='same', name='reconstructed_W')(DrO3)
# reconsider activation (is W positive?)
# should be filter=1 to match W
w_extraction=Model(inputs=[image,wtm],outputs=[decoded,pred_w])
#----------------------training the model--------------------------------------
#------------------------------------------------------------------------------
#----------------------Data preparesion----------------------------------------
(x_train, _), (x_test, _) = mnist.load_data()
x_validation=x_train[1:10000,:,:]
x_train=x_train[10001:60000,:,:]
#
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_validation = x_validation.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_validation = np.reshape(x_validation, (len(x_validation), 28, 28, 1))
#---------------------compile and train the model------------------------------
# is accuracy sensible metric for this model?
w_extraction.compile(optimizer='adadelta', loss={'decoder_output':'mse','reconstructed_W':'mse'}, metrics=['mae'])
w_extraction.fit([x_train,w_expand], [x_train,w_expand],
epochs=100,
batch_size=128,
validation_data=([x_validation,wv_expand], [x_validation,wv_expand]),
callbacks=[TensorBoard(log_dir='E:/tmp/AutewithW200', histogram_freq=0, write_graph=False)])
model.summary()
If you need this inside the model, you can use K.round() from keras.backend. Notice this will not be differentiable and will not be able to be used very well in training.
If you need just the results, you can simple define a threshold (usually 0.5) and:
binary_reslts = results > threshold
Add metrics to your model
You can see the results by adding metrics that round the data.
Standard metrics for this could be "accuracy" or "categorical_accuracy". You you can define your own metrics like:
def diceMetric(yTrue, yPred):
yTrue = K.batch_flatten(yTrue)
yPred = K.batch_flatten(yPred)
#round
yPred = K.greater(yPred, 0.5)
yPred = K.cast(yPred, K.floatx())
intersection = yPred * yTrue
sum = yTrue + yPred
return (2*intersection + K.epsilon())/(sum + K.epsilon())
Metrics are added in compile:
model.compile(optimizer=..., loss=..., metrics = [diceMetric, 'categorical_accuracy'])
Metrics do not affect training, they're just feedback for you to know what's happening.
Why would you need your network to output exactly 0 or 1? You can interpret the output of your network as probability measure, of how likely it is that an input pixel correponds to class 0 or 1.
So during training the model tries to approximate the unknown probability distribution.
When it comes to predictions, you can use a threshold like .5 or you could use something like otsu threshold. Then you will obtain an binary output. Unfortunately thresholds will create some gaps or shrink the area of some predicted shapes.
Note:
typically you want to down and upsample in an autoencoder, because otherwise the model,could learn that the idendity function is optimal.

Validating the structure of a convolutional encoder

I am building a convolutional encoder to deal with some 128x128 images - like this.
I have been testing the structure by testing it with a image set of 500 images. The resulting decoded images are basically entirely black (not want I want!)
I was hoping to get some advice on here as I think I am making some obvious mistakes.
A small group of images can be downloaded here -> https://www.dropbox.com/sh/0oj1p6sqal32cvx/AAAYQJSK2SPfynD8wYMSo9bPa?dl=0
Current Code
################################# SETUP #######################################
import glob
import pandas as pd
import numpy as np
import sys
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import random
np.set_printoptions(threshold=np.nan)
######################### DATA PREPARATION #####################################
# create a list of XML files within the raw data folder
image_list = glob.glob("Images/test_images/*.jpeg")
print(image_list)
l = []
for i in image_list:
img = np.array(cv2.imread(i, 0))
l.append(img)
T = np.array(l)
# split into training and testing sets
labels = image_list
data_train, data_test, labels_train, labels_test = train_test_split(T, labels, test_size=0.5, random_state=42)
# convert to 0-1 floats (reconversion by * 255)
data_train = data_train.astype('float32') / 255.
data_test = data_test.astype('float32') / 255.
print(data_train.shape)
# reshape from channels first to channels last
data_train = np.rollaxis(data_train, 0, 3)
data_test = np.rollaxis(data_test, 0, 3)
######################### ENCODER MODELING #####################################
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K
input_img = Input(shape=(128, 128, 1)) # adapt this if using `channels_first` image data format
x = Conv2D(64, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
# create the model
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='mse')
# reference for reordering
data_train_dimensions = data_train.shape
data_test_dimensions = data_test.shape
# reshape the data sets
data_train = np.reshape(data_train, (data_train_dimensions[2], 128, 128, 1)) # adapt this if using `channels_first` image data format
data_test = np.reshape(data_test, (data_test_dimensions[2], 128, 128, 1))
from keras.callbacks import TensorBoard
autoencoder.fit(data_train, data_test,
epochs=10,
batch_size=128,
shuffle=True,
validation_data=(data_train, data_test),
callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
# create decoded images from model
decoded_imgs = autoencoder.predict(data_test)
# reorder columns
decoded_imgs = np.rollaxis(decoded_imgs, 3, 1)
# reshape from channels first to channels last
data_train = np.rollaxis(data_train, 0, 3)
data_test = np.rollaxis(data_test, 0, 3)
# convert to 0-1 floats (reconversion by * 255)
data_train = data_train.astype('float32') * 255.
data_test = data_test.astype('float32') * 255.
I think the main problem is that you fit on data_train, data_test instead of data_train, labels_train, that is, you should fit your model on samples and corresponding outputs but you train it only on inputs which are by a coincidence are of compatible shapes because of the 50/50 split.
If the intention of the model is to reproduce an image from compressed representation, then you could train fit(data_train, data_train, ..., validation_data=(data_test, data_test)).

change layers during learning in keras

I want to make an auto-encoder in Keras. Before sending the output of the encoder to the decoder, I want to add a noise image having the same size as the encoder, and after that send both to the decoder. I want to know if this is possible or not.
When this section of my code is encountered:
merge_encoded_w=cv2.merge(encoded,w)
I get this error:
TypeError: Tensor objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn.
My entire code is below:
from keras.models import Sequential
from keras.layers import Input, Dense, Dropout, Activation,UpSampling2D,Conv2D, MaxPooling2D, GaussianNoise
from keras.models import Model
from keras.optimizers import SGD
from keras.datasets import mnist
from keras import regularizers
from keras import backend as K
import numpy as np
import matplotlib.pyplot as plt
import cv2
from time import time
from keras.callbacks import TensorBoard
# Embedding phase
##encoder
w=np.random.random((1, 28,28))
input_img = Input(shape=(28, 28, 1)) # adapt this if using `channels_first` image data format
x = Conv2D(8, (5, 5), activation='relu', padding='same')(input_img)
#x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(4, (3, 3), activation='relu', padding='same')(x)
#x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(2, (3, 3), activation='relu', padding='same')(x)
encoded = Conv2D(1, (3, 3), activation='relu', padding='same')(x)
merge_encoded_w=cv2.merge(encoded,w)
#
#decoder
x = Conv2D(2, (5, 5), activation='relu', padding='same')(merge_encoded_w)
#x = UpSampling2D((2, 2))(x)
x = Conv2D(4, (3, 3), activation='relu', padding='same')(x)
#x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu',padding='same')(x)
#x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
#Extraction phase
decodedWithNois=GaussianNoise(0.5)(decoded)
x = Conv2D(8, (5, 5), activation='relu', padding='same')(decodedWithNois)
#x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(4, (3, 3), activation='relu', padding='same')(x)
#x = MaxPooling2D((2, 2), padding='same')(x)
final_image_watermark = Conv2D(2, (3, 3), activation='relu', padding='same')(x)
autoencoder = Model([input_img,w], [decoded,final_image_watermark(2)])
encoder=Model(input_img,encoded)
autoencoder.compile(optimizer='adadelta', loss=['mean_squared_error','mean_squared_error'],metrics=['accuracy'])
(x_train, _), (x_test, _) = mnist.load_data()
x_validation=x_train[1:10000,:,:]
x_train=x_train[10001:60000,:,:]
#
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_validation = x_validation.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_validation = np.reshape(x_validation, (len(x_validation), 28, 28, 1)) # adapt this if using `channels_first` image data format
autoencoder.fit(x_train, x_train,
epochs=5,
batch_size=128,
shuffle=True,
validation_data=(x_validation, x_validation),
callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
decoded_imgs = autoencoder.predict(x_test)
encoded_imgs=encoder.predict(x_test)
Please help me with this problem. Thanks.

Categories

Resources