Dimension error encoder input it different from decoder output - python

i am new to auto encoders and i cannot understand why i am getting this error.
'''
class Autoencoder(Model):
def __init__(self, name="autoencoder"):
super(Autoencoder, self).__init__()
self.encoder_input = tf.keras.Input(shape=(128,219,1))
self.encoder = layers.Conv2D(16, (3, 3), activation='relu', padding='same', strides=2, kernel_initializer=HeNormal())(self.encoder_input)
self.encoder_output = layers.Conv2D(8, (3, 3), activation='relu', padding='same', strides=2, kernel_initializer=HeNormal())(self.encoder)
self.encoder_model = tf.keras.Model(self.encoder_input, self.encoder_output)
# ------------------------------------------
# The Autoencoder has an decoder which we can make using normal layers
# Here we use the Functional API which requires 3 things (Input, model and output)
# ------------------------------------------
self.decoder_input = tf.keras.Input(shape=(32,55,8))
self.decoder = layers.Conv2DTranspose(8, kernel_size=3, strides=2, activation='relu', padding='same', kernel_initializer=HeNormal())(self.decoder_input)
self.decoder_second = layers.Conv2DTranspose(16, kernel_size=3, strides=2, activation='relu', padding='same', kernel_initializer=HeNormal())(self.decoder)
self.decoder_output = layers.Conv2D(1, kernel_size=(3, 3), activation='sigmoid', padding='same', kernel_initializer=HeNormal())(self.decoder_second)
self.decoder_model = tf.keras.Model(self.decoder_input, self.decoder_output)
# ------------------------------------------
# The forward pass
def call(self, x):
# Encode the inputs x using the propery defined above
# In the previous notebook we created the encoder and decoder as Sequential Models
# but now since we used the Functional API we need to call the Model objects we created above
encoded = self.encoder_model(x)
# Decode the encoded vector using the property defined above
decoded = self.decoder_model(encoded)
# Return the decoded (28,28) data
return decoded
autoencoder = Autoencoder()
I defined the encoder part then the decoder part but after calling the fit model i cam getting
ValueError: Dimensions must be equal, but are 220 and 219 for '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT](autoencoder_3/model_7/conv2d_15/Sigmoid, IteratorGetNext:1)' with input shapes: [?,128,220,1], [?,128,219,1].
Am i doing something wrong?
The input image is of shape 128,219,1

Related

Implementing DCGAN in keras but it is not properly trained

I am trying to implement DCGAN presented in this article. Here is my Generator and Discriminator:
ki = keras.initializers.RandomNormal(mean=0.0, stddev=0.02)
def discriminator_model():
discriminator = Sequential([
Conv2D(64, (3,3), strides=(2, 2), padding='same', kernel_initializer=ki, input_shape=[64,64, 3]), # No BatchNormilization in this layer
LeakyReLU(alpha=0.2),
Dropout(0.4),
Conv2D(64, (3,3), strides=(2, 2), padding='same', kernel_initializer=ki),
BatchNormalization(),
LeakyReLU(alpha=0.2),
Dropout(0.4),
Flatten(),
Dense(1, activation='sigmoid', kernel_initializer=ki)
])
return discriminator
===========================================
noise_shape = 100
def generator_model():
generator = Sequential([
Dense(4*4*512, input_shape=[noise_shape]),
Reshape([4,4,512]),
Conv2DTranspose(256, kernel_size=4, strides=2, padding="same", kernel_initializer= ki),
BatchNormalization(),
ReLU(),
Conv2DTranspose(128, kernel_size=4, strides=2, padding="same", kernel_initializer=ki),
BatchNormalization(),
ReLU(),
Conv2DTranspose(64, kernel_size=4, strides=2, padding="same", kernel_initializer=ki),
BatchNormalization(),
ReLU(),
Conv2DTranspose(3, kernel_size=4, strides=2, padding="same", kernel_initializer=ki, activation='tanh') # 3 filters, also no BatchNormilization in this layer
])
return generator
Here I have combined these to to build DCGAN:
DCGAN = Sequential([generator,discriminator])
opt = tf.keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.5)
discriminator.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])
discriminator.trainable = False
DCGAN.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])
Then I prepared my batches and tried to train my model. Here is the code:
epochs = 500
batch_size = 128
loss_from_discriminator_model=[]
loss_from_generator_model=[]
acc_dis = []
acc_gen = []
with tf.device('/gpu:0'):
for epoch in range(epochs):
for i in range(images.shape[0]//batch_size):
# Training Discriminator
noise = np.random.uniform(-1,1,size=[batch_size, noise_shape])
gen_image = generator.predict_on_batch(noise) # Generating fake images
train_dataset = images[i*batch_size:(i+1)*batch_size]
train_labels_real = np.ones(shape=(batch_size,1)) # Real image labels
discriminator.trainable = True
d_loss_real, d_acc_real = discriminator.train_on_batch(train_dataset,train_labels_real) # Training on real images
train_labels_fake = np.zeros(shape=(batch_size,1))
d_loss_fake, d_acc_fake = discriminator.train_on_batch(gen_image,train_labels_fake) # Training on fake images
# Training Generator
noise = np.random.uniform(-1, 1, size=[batch_size,noise_shape])
train_label_fake_for_gen_training = np.ones(shape=(batch_size,1))
discriminator.trainable = False
g_loss, g_acc = DCGAN.train_on_batch(noise, train_label_fake_for_gen_training)
loss_from_discriminator_model.append(d_loss_real+d_loss_fake)
loss_from_generator_model.append(g_loss)
acc_dis.append((d_acc_real + d_acc_fake) / 2)
acc_gen.append(g_acc)
The problem is my modell doesn't seem to learn anything. values of accuracy and loss don't seem rational. Here is a plot of values of generator and discriminator loss during training.
Thanks in advance.

Saving model on Tensorflow 2.7.0 with data augmentation layer

I am getting an error when trying to save a model with data augmentation layers with Tensorflow version 2.7.0.
Here is the code of data augmentation:
input_shape_rgb = (img_height, img_width, 3)
data_augmentation_rgb = tf.keras.Sequential(
[
layers.RandomFlip("horizontal"),
layers.RandomFlip("vertical"),
layers.RandomRotation(0.5),
layers.RandomZoom(0.5),
layers.RandomContrast(0.5),
RandomColorDistortion(name='random_contrast_brightness/none'),
]
)
Now I build my model like this:
# Build the model
input_shape = (img_height, img_width, 3)
model = Sequential([
layers.Input(input_shape),
data_augmentation_rgb,
layers.Rescaling((1./255)),
layers.Conv2D(16, kernel_size, padding=padding, activation='relu', strides=1,
data_format='channels_last'),
layers.MaxPooling2D(),
layers.BatchNormalization(),
layers.Conv2D(32, kernel_size, padding=padding, activation='relu'), # best 4
layers.MaxPooling2D(),
layers.BatchNormalization(),
layers.Conv2D(64, kernel_size, padding=padding, activation='relu'), # best 3
layers.MaxPooling2D(),
layers.BatchNormalization(),
layers.Conv2D(128, kernel_size, padding=padding, activation='relu'), # best 3
layers.MaxPooling2D(),
layers.BatchNormalization(),
layers.Flatten(),
layers.Dense(128, activation='relu'), # best 1
layers.Dropout(0.1),
layers.Dense(128, activation='relu'), # best 1
layers.Dropout(0.1),
layers.Dense(64, activation='relu'), # best 1
layers.Dropout(0.1),
layers.Dense(num_classes, activation = 'softmax')
])
model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=metrics)
model.summary()
Then after the training is done I just make:
model.save("./")
And I'm getting this error:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-84-87d3f09f8bee> in <module>()
----> 1 model.save("./")
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in
error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
/usr/local/lib/python3.7/dist-
packages/tensorflow/python/saved_model/function_serialization.py in
serialize_concrete_function(concrete_function, node_ids, coder)
66 except KeyError:
67 raise KeyError(
---> 68 f"Failed to add concrete function '{concrete_function.name}' to
object-"
69 f"based SavedModel as it captures tensor {capture!r} which is
unsupported"
70 " or not reachable from root. "
KeyError: "Failed to add concrete function
'b'__inference_sequential_46_layer_call_fn_662953'' to object-based SavedModel as it
captures tensor <tf.Tensor: shape=(), dtype=resource, value=<Resource Tensor>> which
is unsupported or not reachable from root. One reason could be that a stateful
object or a variable that the function depends on is not assigned to an attribute of
the serialized trackable object (see SaveTest.test_captures_unreachable_variable)."
I inspected the reason of getting this error by changing the architecture of my model and I just found that reason came from the data_augmentation layer since the RandomFlip and RandomRotation and others are changed from layers.experimental.prepocessing.RandomFlip to layers.RandomFlip, but still the error appears.
This seems to be a bug in Tensorflow 2.7 when using model.save combined with the parameter save_format="tf", which is set by default. The layers RandomFlip, RandomRotation, RandomZoom, and RandomContrast are causing the problems, since they are not serializable. Interestingly, the Rescaling layer can be saved without any problems. A workaround would be to simply save your model with the older Keras H5 format model.save("test", save_format='h5'):
import tensorflow as tf
import numpy as np
class RandomColorDistortion(tf.keras.layers.Layer):
def __init__(self, contrast_range=[0.5, 1.5],
brightness_delta=[-0.2, 0.2], **kwargs):
super(RandomColorDistortion, self).__init__(**kwargs)
self.contrast_range = contrast_range
self.brightness_delta = brightness_delta
def call(self, images, training=None):
if not training:
return images
contrast = np.random.uniform(
self.contrast_range[0], self.contrast_range[1])
brightness = np.random.uniform(
self.brightness_delta[0], self.brightness_delta[1])
images = tf.image.adjust_contrast(images, contrast)
images = tf.image.adjust_brightness(images, brightness)
images = tf.clip_by_value(images, 0, 1)
return images
def get_config(self):
config = super(RandomColorDistortion, self).get_config()
config.update({"contrast_range": self.contrast_range, "brightness_delta": self.brightness_delta})
return config
input_shape_rgb = (256, 256, 3)
data_augmentation_rgb = tf.keras.Sequential(
[
tf.keras.layers.RandomFlip("horizontal"),
tf.keras.layers.RandomFlip("vertical"),
tf.keras.layers.RandomRotation(0.5),
tf.keras.layers.RandomZoom(0.5),
tf.keras.layers.RandomContrast(0.5),
RandomColorDistortion(name='random_contrast_brightness/none'),
]
)
input_shape = (256, 256, 3)
padding = 'same'
kernel_size = 3
model = tf.keras.Sequential([
tf.keras.layers.Input(input_shape),
data_augmentation_rgb,
tf.keras.layers.Rescaling((1./255)),
tf.keras.layers.Conv2D(16, kernel_size, padding=padding, activation='relu', strides=1,
data_format='channels_last'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(32, kernel_size, padding=padding, activation='relu'), # best 4
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(64, kernel_size, padding=padding, activation='relu'), # best 3
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(128, kernel_size, padding=padding, activation='relu'), # best 3
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'), # best 1
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(128, activation='relu'), # best 1
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(64, activation='relu'), # best 1
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(5, activation = 'softmax')
])
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.summary()
model.save("test", save_format='h5')
Loading your model with your custom layer would look like this then:
model = tf.keras.models.load_model('test.h5', custom_objects={'RandomColorDistortion': RandomColorDistortion})
where RandomColorDistortion is the name of your custom layer.
You can also downgrade Keras and Tensorflow to version 2.6.

Converting model declaration in Keras (removing Sequential) into a new one without Sequential returns different shape

I'm newbie with Python, Tensorflow and Keras.
I have modified this code:
def build_base_network(input_shape):
seq = Sequential()
nb_filter = [6, 12]
kernel_size = 3
#convolutional layer 1
seq.add(Convolution2D(nb_filter[0], kernel_size, kernel_size, input_shape=input_shape,
border_mode='valid', dim_ordering='th'))
seq.add(Activation('relu'))
seq.add(MaxPooling2D(pool_size=(2, 2)))
seq.add(Dropout(.25))
#convolutional layer 2
seq.add(Convolution2D(nb_filter[1], kernel_size, kernel_size, border_mode='valid', dim_ordering='th'))
seq.add(Activation('relu'))
seq.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='th'))
seq.add(Dropout(.25))
#flatten
seq.add(Flatten())
seq.add(Dense(128, activation='relu'))
seq.add(Dropout(0.1))
seq.add(Dense(50, activation='relu'))
return seq
Writing my own version:
def build_base_network(input_shape):
inputs = Input(shape = input_shape)
nb_filter = [6, 12]
kernel_size = 3
conv1 = Conv2D(nb_filter[0], (kernel_size, kernel_size), activation='relu', padding="valid", data_format='channels_first')(inputs)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
drop1 = Dropout(.25)(pool1)
#convolutional layer 2
conv2 = Conv2D(nb_filter[1], (kernel_size, kernel_size), activation='relu', padding="valid", data_format="channels_first")(drop1)
pool2 = MaxPooling2D(pool_size=(2, 2), data_format="channels_first")(conv2)
drop2 = Dropout(.25)(pool2)
#flatten
dense1 = Dense(128, activation='relu')(drop2)
drop3 = Dropout(0.1)(dense1)
dense2 = Dense(50, activation='relu')(drop3)
model = Model(inputs=inputs, outputs=dense2)
return model
I call it with this code:
input_dim = x_train.shape[2:]
img_a = Input(shape=input_dim)
img_b = Input(shape=input_dim)
base_network = build_base_network(input_dim)
feat_vecs_a = base_network(img_a)
feat_vecs_b = base_network(img_b)
The unmodified code (the first one) returns this shape: (None, 50)
The modified code (my own version) returns this shape: (None, 12, 12, 50)
I hadn't modified any other piece of code. The function base_network is the only one I have changed it.
By the way, input_dim is (1, 56, 46).
What am I doing wrong?
You forgot a Flatten operation :
pool2 = MaxPooling2D(pool_size=(2, 2), data_format="channels_first")(conv2)
drop2 = Dropout(.25)(pool2)
#flatten
dense1 = Dense(128, activation='relu')(drop2)
should then be
pool2 = MaxPooling2D(pool_size=(2, 2), data_format="channels_first")(conv2)
drop2 = Dropout(.25)(pool2)
#flatten
flatten1 = Flatten()(drop2)
dense1 = Dense(128, activation='relu')(flatten1)

How to use Keras functional API in python function, getting error: TypeError: float() argument must be a string or a number, not 'Dimension'

I am trying to write a convolutional neural network in keras that uses an Inception style module, like the example from keras functional API page. To "simplify" things I decided it would be good to put this module in a python function to be reusable with different filter and input sizes. This however generates the error message:
TypeError: float() argument must be a string or a number, not
'Dimension'
How should I write this function, and what's wrong with this one
"""This is the "inception" module."""
def incepm_v1(out_filters, input_shape)->Model:
from keras.layers import Conv2D, MaxPooling2D, Input
input_img = Input(shape=input_shape)
tower_1 = Conv2D(out_filters, (1, 1), padding='same',
activation='relu')(input_img)
tower_1 = Conv2D(out_filters, (3, 3), padding='same',
activation='relu')(tower_1)
tower_2 = Conv2D(out_filters, (1, 1), padding='same',
activation='relu')(input_img)
tower_2 = Conv2D(out_filters, (5, 5), padding='same',
activation='relu')(tower_2)
tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')
(input_img)
tower_3 = Conv2D(out_filters, (1, 1), padding='same',
activation='relu')(tower_3)
output = keras.layers.concatenate([tower_1, tower_2, tower_3],
axis=1)
model = Model(inputs=input_img, outputs=output)
return model
"This is then used in the following model"
def Unetish_model1(image_shape=(IMAGE_SIZE, IMAGE_SIZE, 3)):
image = Input(shape=image_shape)
#First layer 96X96
conv1 = Conv2D(32, (3,3),padding='same', activation =
'relu'(image)
conv1out = Conv2D(16, (1,1),padding = 'same', activation =
'relu')(conv1)
conv1out = MaxPooling2D((2,2), strides = (2,2))(conv1out)
aux1out = Conv2D(16, (1,1), padding = 'same', activation =
'relu')(conv1)
#Second layer 48x48
#print(conv1out.shape)
conv2 = incepm_v1(64, conv1out.shape[1:])(conv1out)
conv2out = Conv2D(32, (1,1), padding = 'same', activation =
'relu')(conv2)
conv2out = MaxPooling2D((2,2), strides = (2,2))(conv2out)
aux2out = Conv2D(32, (1,1), padding = 'same', activation =
'relu')(conv2)
".... removed for sparsity"
model = Model(inputs =image, outputs = output)
model.summary()
return model
IMAGE_SIZE = 96
Unet = Unetish_model1(image_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
--------------------------------------------------------------------
-------
TypeError Traceback (most recent
call last)
<ipython-input-79-9f51199eb354> in <module>()
1 IMAGE_SIZE = 96
----> 2 Unet = Unetish_model1(image_shape=(IMAGE_SIZE, IMAGE_SIZE,
3))
<ipython-input-78-663bab493362> in Unetish_model1(image_shape)
10 #Second layer 48x48
11 #print(conv1out.shape)
---> 12 conv2 = incepm_v1(64, conv1out.shape[1:])(conv1out)
13 conv2out = Conv2D(32, (1,1), padding = 'same',
activation = 'relu')(conv2)
14 conv2out = MaxPooling2D((2,2), strides = (2,2))
(conv2out)
<ipython-input-72-b8563ad454e6> in incepm_v1(out_filters,
input_shape)
4 input_img = Input(shape=input_shape)
5
----> 6 tower_1 = Conv2D(out_filters, (1, 1), padding='same',
activation='relu')(input_img)
7 tower_1 = Conv2D(out_filters, (3, 3), padding='same',
activation='relu')(tower_1)
8
~/anaconda3/envs/dlib/lib/python3.5/site-
packages/keras/engine/topology.py in __call__(self, inputs,
**kwargs)
588
'`layer.build(batch_input_shape)`')
589 if len(input_shapes) == 1:
--> 590 self.build(input_shapes[0])
591 else:
592 self.build(input_shapes)
~/anaconda3/envs/dlib/lib/python3.5/site-
packages/keras/layers/convolutional.py in build(self,
input_shape)
136 name='kernel',
137
regularizer=self.kernel_regularizer,
--> 138
constraint=self.kernel_constraint)
139 if self.use_bias:
140 self.bias = self.add_weight(shape=
(self.filters,),
~/anaconda3/envs/dlib/lib/python3.5/site-
packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' +
object_name +
90 '` call to the Keras 2 API: '
+ signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/anaconda3/envs/dlib/lib/python3.5/site-
packages/keras/engine/topology.py in add_weight(self, name,
shape, dtype, initializer, regularizer, trainable, constraint)
409 if dtype is None:
410 dtype = K.floatx()
--> 411 weight = K.variable(initializer(shape),
412 dtype=dtype,
413 name=name,
~/anaconda3/envs/dlib/lib/python3.5/site-
packages/keras/initializers.py in __call__(self, shape, dtype)
207 scale /= max(1., fan_out)
208 else:
--> 209 scale /= max(1., float(fan_in + fan_out) / 2)
210 if self.distribution == 'normal':
211 stddev = np.sqrt(scale)
TypeError: float() argument must be a string or a number, not
'Dimension'
I am using keras version 2.1.3 in an anaconda enviroment with tensorflow backended with gpu
layer_name.shape will give you a tensorflow.python.framework.tensor_shape.TensorShape containing tensorflow.python.framework.tensor_shape.Dimension.
In this case, you should use K.int_shape(layer_name), which gives you tuple of integers.
from keras.layers import Conv2D, MaxPooling2D, Input, Concatenate
from keras.models import Model
import keras.backend as K
"""This is the "inception" module."""
def incepm_v1(out_filters, input_shape)->Model:
input_img = Input(shape=input_shape)
tower_1 = Conv2D(out_filters, (1, 1), padding='same',
activation='relu')(input_img)
tower_1 = Conv2D(out_filters, (3, 3), padding='same',
activation='relu')(tower_1)
tower_2 = Conv2D(out_filters, (1, 1), padding='same',
activation='relu')(input_img)
tower_2 = Conv2D(out_filters, (5, 5), padding='same',
activation='relu')(tower_2)
tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
tower_3 = Conv2D(out_filters, (1, 1), padding='same',
activation='relu')(tower_3)
output = Concatenate(axis=1)([tower_1, tower_2, tower_3])
model = Model(inputs=input_img, outputs=output)
return model
"""This is then used in the following model"""
def Unetish_model1(image_shape=(IMAGE_SIZE, IMAGE_SIZE, 3)):
image = Input(shape=image_shape)
#First layer 96X96
conv1 = Conv2D(32, (3,3),padding='same', activation = 'relu')(image)
conv1out = Conv2D(16, (1,1),padding = 'same', activation =
'relu')(conv1)
conv1out = MaxPooling2D((2,2), strides = (2,2))(conv1out)
aux1out = Conv2D(16, (1,1), padding = 'same', activation = 'relu')(conv1)
#Second layer 48x48
#conv2 = incepm_v1(64, conv1out.shape[1:])(conv1out)
conv2 = incepm_v1(64, K.int_shape(conv1out)[1:])(conv1out)
conv2out = Conv2D(32, (1,1), padding = 'same', activation =
'relu')(conv2)
conv2out = MaxPooling2D((2,2), strides = (2,2))(conv2out)
aux2out = Conv2D(32, (1,1), padding = 'same', activation =
'relu')(conv2)
#".... removed for sparsity"
model = Model(inputs =image, outputs = output)
model.summary()
return model
IMAGE_SIZE = 96
Unet = Unetish_model1(image_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
The code above also fixes the usage of Concatenate layer.
And note that this piece of code still can be run successfully since some parts of Unetish_model1 is omitted.

How do I optimize Tensorflow CNN?

I'm very new to Tensorflow, so I apologize if my question comes off as ignorant.
I have a very simple CNN Tensorflow that takes images and outputs another image. With just a batchsize of 5, it takes minutes to run between epochs and often crashes after 5 epochs.(I'm using python 3.6.5 on my mac with 16 gbs of RAM)
This is a snippet of my program
learning_rate = 0.01
inputs_ = tf.placeholder(tf.float32, (None, 224, 224, 3), name='inputs')
targets_ = tf.placeholder(tf.float32, (None, 224, 224, 1), name='targets')
### Encoder
conv1 = tf.layers.conv2d(inputs=inputs_, filters=32, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 224x224x32
maxpool1 = tf.layers.max_pooling2d(conv1, pool_size=(2,2), strides=(2,2), padding='same')
# Now 112x112x32
conv2 = tf.layers.conv2d(inputs=maxpool1, filters=32, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 112x112x32
maxpool2 = tf.layers.max_pooling2d(conv2, pool_size=(2,2), strides=(2,2), padding='same')
# Now 56x56x32
conv3 = tf.layers.conv2d(inputs=maxpool2, filters=32, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 56x56x32
maxpool3 = tf.layers.max_pooling2d(conv3, pool_size=(2,2), strides=(2,2), padding='same')
# Now 28x28x32
conv4 = tf.layers.conv2d(inputs=maxpool3, filters=32, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 28x28x32
maxpool4 = tf.layers.max_pooling2d(conv4, pool_size=(2,2), strides=(2,2), padding='same')
# Now 14x14x32
conv5 = tf.layers.conv2d(inputs=maxpool4, filters=32, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 14x14x32
maxpool5 = tf.layers.max_pooling2d(conv5, pool_size=(2,2), strides=(2,2), padding='same')
# Now 7x7x32
conv6 = tf.layers.conv2d(inputs=maxpool5, filters=16, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 7x7x16
encoded = tf.layers.max_pooling2d(conv6, pool_size=(2,2), strides=(2,2), padding='same')
# Now 4x4x16
### Decoder
upsample1 = tf.image.resize_images(encoded, size=(7,7), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
# Now 7x7x16
conv7 = tf.layers.conv2d(inputs=upsample1, filters=16, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 7x7x16
upsample2 = tf.image.resize_images(conv7, size=(14,14), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
# Now 14x14x16
conv8 = tf.layers.conv2d(inputs=upsample2, filters=32, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 14x14x32
upsample3 = tf.image.resize_images(conv8, size=(28,28), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
# Now 28x28x32
conv9 = tf.layers.conv2d(inputs=upsample3, filters=32, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 28x28x32
upsample4 = tf.image.resize_images(conv9, size=(56,56), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
# Now 56x56x32
conv10 = tf.layers.conv2d(inputs=upsample3, filters=32, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 56x56x32
upsample5 = tf.image.resize_images(conv10, size=(112,112), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
# Now 112x112x32
conv11 = tf.layers.conv2d(inputs=upsample5, filters=32, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 112x112x32
upsample6 = tf.image.resize_images(conv11, size=(224,224), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
# Now 224x224x32
conv12 = tf.layers.conv2d(inputs=upsample6, filters=32, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 224x224x32
logits = tf.layers.conv2d(inputs=conv12, filters=1, kernel_size=(3,3), padding='same', activation=None)
#Now 224x224x1
# Pass logits through sigmoid and calculate the cross-entropy loss
loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=targets_, logits=logits)
# Get cost and define the optimizer
cost = tf.reduce_mean(loss)
opt = tf.train.AdamOptimizer(learning_rate).minimize(cost)
imagelist = ... #array of all images with 3 channels
imagelabellist = ... #array of all images with 1 channel
epochs = 15
for e in range(epochs):
imgs_large = imagelist
imgs_target_large = imagelabellist
shaped_imgs = tf.image.resize_images(imgs_large, [224, 224])
shaped_imgs_target = tf.image.resize_images(imgs_target_large, [224, 224])
# Get images from the batch
imgs = sess.run(shaped_imgs)
imgs_target = sess.run(shaped_imgs_target)
batch_cost, _ = sess.run([cost, opt], feed_dict={inputs_: imgs, targets_: imgs_target})
This is the output of the CNN
epoch: #1
0 minutes between epoch
epoch: #2
3 minutes between epoch
epoch: #3
3 minutes between epoch
epoch: #4
12 minutes between epoch
epoch: #5
...
I'm open to any suggestions on how to fix this issue. Thank you.
tf.image.resize_images is a graph op, so you're appending more nodes to the graph (that explains the increasing run time). Before your training loop add sess.graph.finalize() if nodes are being added it will throw an error to check this.
If you move resize_images outside of the loop, that should fixed the issue.

Categories