How to train the ResNet50? - keras

Creating Deeper Bottleneck ResNet from Scratch using Tensorflow
Hi everyone, recently I've been learning how to create ResNet50 using tf.keras according to the link given above. My question is, to train this machine, should I assign the 'input' variable (line 75) with the dataset I wished to train with? If so, how should I do that?
#import the needed libraries
from tensorflow.python.keras.utils.vis_utils import model_to_dot
from IPython.display import SVG
import pydot
import graphviz
from tensorflow.keras.layers import Input, Conv2D, BatchNormalization
from tensorflow.keras.layers import MaxPool2D, GlobalAvgPool2D
from tensorflow.keras.layers import Add, ReLU, Dense
from tensorflow.keras import Model
'''import tensorflow
print(tensorflow.__version__)'''
#Conv-BatchNorm-ReLU block
def conv_batchnorm_relu(x, filters, kernel_size, strides=1):
x = Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, padding = 'same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
return x
#identity block
def identity_block(tensor, filters):
x = conv_batchnorm_relu(tensor, filters = filters, kernel_size = 1, strides = 1)
x = conv_batchnorm_relu(x, filters = filters, kernel_size = 3, strides = 1)
x = Conv2D(filters = 4 * filters, kernel_size = 1, strides = 1)(x)
x = BatchNormalization()(x)
x = Add()([tensor, x]) #skip connection
x = ReLU()(x)
return x
#Projection block
def projection_block(tensor, filters, strides):
#left stream
x = conv_batchnorm_relu(tensor, filters = filters, kernel_size = 1, strides = strides)
x = conv_batchnorm_relu(x, filters = filters, kernel_size = 3, strides = 1)
x = Conv2D(filters = 4 * filters, kernel_size = 1, strides = 1)(x)
x = BatchNormalization()(x)
#right stream
shortcut = Conv2D(filters = 4 * filters, kernel_size = 1, strides = strides)(tensor)
shortcut = BatchNormalization()(shortcut)
x = Add()([shortcut, x]) #skip connection
x = ReLU()(x)
return x
#Resnet Block
#First block is always projection. While others are identity blocks
def resnet_block(x, filters, reps, strides):
x = projection_block(x, filters, strides)
for i in range(reps-1):
x = identity_block(x, filters)
return x
#Create Model
input = Input(shape = (244,244,3)) #Should I assign this input variable with dataset? If so, how do I do that?
#################
x = conv_batchnorm_relu(input, filters = 64, kernel_size = 7, strides = 2)
x = MaxPool2D(pool_size = 3, strides = 2)(x)
x = resnet_block(x, filters = 64, reps = 3, strides = 1)
x = resnet_block(x, filters=128, reps =4, strides=2)
x = resnet_block(x, filters=256, reps =6, strides=2)
x = resnet_block(x, filters=512, reps =3, strides=2)
x = GlobalAvgPool2D()(x)
#################
output = Dense(1000, activation = 'softmax')(x)
model = Model(inputs=input, outputs=output)
model.summary()
SVG(model_to_dot(model, show_shapes=True, show_layer_names=True, rankdir='TB',expand_nested=False,
dpi=60, subgraph=False).create(prog='dot',format='svg'))

Related

Converting TensorFlow Keras model API to model subclassing

For a simple TF2 Object detection CNN architecture defined using Keras's functional API as follows:
input_ = Input(shape = (144, 144, 3), name = 'image')
# name - An optional name string for the Input layer. Should be unique in
# a model (do not reuse the same name twice). It will be autogenerated if it isn't provided.
# Here 'image' is the Python3 dict's key used to map the data to one of the layer in the model.
x = input_
# Define a conv block-
x = Conv2D(filters = 64, kernel_size = 3, activation = 'relu')(x)
x = BatchNormalization()(x)
x = MaxPool2D(pool_size = 2)(x)
x = Flatten()(x) # flatten the last pooling layer's output volume
x = Dense(256, activation='relu')(x)
# We are using a data generator which yields dictionaries. Using 'name' argument makes it
# possible to map the correct data generator's output to the appropriate layer
class_out = Dense(units = 9, activation = 'softmax', name = 'class_out')(x) # classification output
box_out = Dense(units = 2, activation = 'linear', name = 'box_out')(x) # regression output
# Define the CNN model-
model = tf.keras.models.Model(input_, [class_out, box_out]) # since we have 2 outputs, we use a list
I am attempting to define it using Model sub-classing as:
class OD(Model):
def __init__(self):
super(OD, self).__init__()
self.conv1 = Conv2D(filters = 64, kernel_size = 3, activation = None)
self.bn = BatchNormalization()
self.pool = MaxPool2D(pool_size = 2)
self.flatten = Flatten()
self.dense = Dense(256, activation = None)
self.class_out = Dense(units = 9, activation = None, name = 'class_out')
self.box_out = Dense(units = 2, activation = 'linear', name = 'box_out')
def call(self, x):
x = tf.nn.relu(self.bn(self.conv1(x)))
x = self.pool(x)
x = self.flatten(x)
x = tf.nn.relu(self.dense(x))
x = [tf.nn.softmax(self.class_out(x)), self.box_out(x)]
return x
A batch of training data is obtained as:
example, label = next(data_generator(batch_size = 32))
example.keys()
# dict_keys(['image'])
image = example['image']
image.shape
# (32, 144, 144, 3)
label.keys()
# dict_keys(['class_out', 'box_out'])
label['class_out'].shape, label['box_out'].shape
# ((32, 9), (32, 2))
Is my Model sub-classing architecture equivalent to Keras's functional API?

Applying VGG16 for 10 images but get the value error

The proposed method can automatically detect the features of medical images under the condition determined by the algorithms, and achieve the correct and fast recognition results.
I was trying to run the image classification with using CNN method but then I got the error message below
File "<ipython-input-2-4e7ea6cc5087>", line 1, in <module>
runfile('C:/Users/MDIC/Desktop/VGG for 10 Images.py', wdir='C:/Users/MDIC/Desktop')
File "C:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "C:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/MDIC/Desktop/VGG for 10 Images.py", line 224, in <module>
sp = plt.subplot(nrows, ncols, i + 1)
File "C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 1084, in subplot
a = fig.add_subplot(*args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1367, in add_subplot
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_subplots.py", line 60, in __init__
).format(maxn=rows*cols, num=num))
ValueError: num must be 1 <= num <= 25, not 26
This is my Python code
# Importing libraries
from matplotlib import pyplot as plt
from tensorflow.keras.preprocessing.image import array_to_img, img_to_array, load_img
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.image as mpimg
import numpy as np
import os
# Preparing dataset
# Setting names of the directies for ten sets
base_dir = 'data'
seta ='Man1'
setb ='Man2'
setc ='Man3'
setd ='Man4'
sete ='Man5'
setf ='Man6'
setg ='Man7'
seth ='Man8'
seti ='Man9'
setj ='Man10'
# Each of the sets has three sub directories train, validation and test
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
test_dir = os.path.join(base_dir, 'test')
def prepare_data(base_dir, seta, setb, setc, setd, sete, setf, setg, seth, seti, setj):
# Take the directory names for the base directory and both the sets
# Returns the paths for train, validation for each of the sets
seta_train_dir = os.path.join(train_dir, seta)
setb_train_dir = os.path.join(train_dir, setb)
setc_train_dir = os.path.join(train_dir, setc)
setd_train_dir = os.path.join(train_dir, setd)
sete_train_dir = os.path.join(train_dir, sete)
setf_train_dir = os.path.join(train_dir, setf)
setg_train_dir = os.path.join(train_dir, setg)
seth_train_dir = os.path.join(train_dir, seth)
seti_train_dir = os.path.join(train_dir, seti)
setj_train_dir = os.path.join(train_dir, setj)
seta_valid_dir = os.path.join(validation_dir, seta)
setb_valid_dir = os.path.join(validation_dir, setb)
setc_valid_dir = os.path.join(validation_dir, setc)
setd_valid_dir = os.path.join(validation_dir, setd)
sete_valid_dir = os.path.join(validation_dir, sete)
setf_valid_dir = os.path.join(validation_dir, setf)
setg_valid_dir = os.path.join(validation_dir, setg)
seth_valid_dir = os.path.join(validation_dir, seth)
seti_valid_dir = os.path.join(validation_dir, seti)
setj_valid_dir = os.path.join(validation_dir, setj)
seta_train_fnames = os.listdir(seta_train_dir)
setb_train_fnames = os.listdir(setb_train_dir)
setc_train_fnames = os.listdir(setc_train_dir)
setd_train_fnames = os.listdir(setd_train_dir)
sete_train_fnames = os.listdir(sete_train_dir)
setf_train_fnames = os.listdir(setf_train_dir)
setg_train_fnames = os.listdir(setg_train_dir)
seth_train_fnames = os.listdir(seth_train_dir)
seti_train_fnames = os.listdir(seti_train_dir)
setj_train_fnames = os.listdir(setj_train_dir)
return seta_train_dir, setb_train_dir, setc_train_dir, setd_train_dir, sete_train_dir, setf_train_dir, setg_train_dir, seth_train_dir, seti_train_dir, setj_train_dir, seta_valid_dir, setb_valid_dir, setc_valid_dir, setd_valid_dir, sete_valid_dir, setf_valid_dir, setg_valid_dir, seth_valid_dir, seti_valid_dir, setj_valid_dir, seta_train_fnames, setb_train_fnames, setc_train_fnames, setd_train_fnames, sete_train_fnames, setf_train_fnames, setg_train_fnames, seth_train_fnames, seti_train_fnames, setj_train_fnames
seta_train_dir, setb_train_dir, setc_train_dir, setd_train_dir, sete_train_dir, setf_train_dir, setg_train_dir, seth_train_dir, seti_train_dir, setj_train_dir, seta_valid_dir, setb_valid_dir, setc_valid_dir, setd_valid_dir, sete_valid_dir, setf_valid_dir, setg_valid_dir, seth_valid_dir, seti_valid_dir, setj_valid_dir, seta_train_fnames, setb_train_fnames, setc_train_fnames, setd_train_fnames, sete_train_fnames, setf_train_fnames, setg_train_fnames, seth_train_fnames, seti_train_fnames, setj_train_fnames = prepare_data(base_dir, seta, setb, setc, setd, sete, setf, setg, seth, seti, setj)
seta_test_dir = os.path.join(test_dir, seta)
setb_test_dir = os.path.join(test_dir, setb)
setc_test_dir = os.path.join(test_dir, setc)
setd_test_dir = os.path.join(test_dir, setd)
sete_test_dir = os.path.join(test_dir, sete)
setf_test_dir = os.path.join(test_dir, setf)
setg_test_dir = os.path.join(test_dir, setg)
seth_test_dir = os.path.join(test_dir, seth)
seti_test_dir = os.path.join(test_dir, seti)
setj_test_dir = os.path.join(test_dir, setj)
test_fnames_seta = os.listdir(seta_test_dir)
test_fnames_setb = os.listdir(setb_test_dir)
test_fnames_setc = os.listdir(setc_test_dir)
test_fnames_setd = os.listdir(setd_test_dir)
test_fnames_sete = os.listdir(sete_test_dir)
test_fnames_setf = os.listdir(setf_test_dir)
test_fnames_setg = os.listdir(setg_test_dir)
test_fnames_seth = os.listdir(seth_test_dir)
test_fnames_seti = os.listdir(seti_test_dir)
test_fnames_setj = os.listdir(setj_test_dir)
datagen = ImageDataGenerator(
height_shift_range = 0.2,
width_shift_range = 0.2,
rotation_range = 40,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = 'nearest')
img_path = os.path.join(seta_train_dir, seta_train_fnames[3])
img = load_img(img_path, target_size = (150, 150))
x = img_to_array(img)
x = x.reshape((1,) + x.shape)
i = 0
for batch in datagen.flow(x, batch_size = 1):
plt.figure(i)
imgplot = plt.imshow(array_to_img(batch[0]))
i += 1
if i % 10 == 0:
break
# Convolutional Neural Network model
# Import TensorFlow libraries
from tensorflow.keras import layers
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
img_input = layers.Input(shape = (150, 150, 3))
# 2D Convolution layer with 64 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(64, 3, activation = 'relu')(img_input)
# 2D max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 128 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(128, 3, activation = 'relu')(x)
# 2D Max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 256 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(256, 3, activation = 'relu')(x)
# 2D Max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 512 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(512, 3, activation = 'relu')(x)
# 2D Max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 512 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(512, 3, activation = 'relu')(x)
# Flatten layer
x = layers.Flatten()(x)
# Fully connected layers and ReLU activation algorithm
x = layers.Dense(4096, activation = 'relu')(x)
x = layers.Dense(4096, activation = 'relu')(x)
x = layers.Dense(1000, activation = 'relu')(x)
# Dropout layers for optimisation
x = layers.Dropout(0.5)(x)
# Fully connected layers and sigmoid activation algorithm
model = Sequential()
model.add(Dense(10))
output = layers.Dense(10, activation = 'sigmoid')(x)
model = Model(img_input, output)
model.summary()
import tensorflow as tf
# Using binary_crossentropy as the loss function and
# Adam optimizer as the optimizing function when training
model.compile(loss = 'sparse_categorical_crossentropy',
optimizer = tf.optimizers.Adam(learning_rate = 0.0005),
metrics = ['acc'])
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(rescale = 1./255)
test_datagen = ImageDataGenerator(rescale = 1./255)
# Flow training images in batches of 20 using train_datagen generator
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size = (150, 150),
batch_size = 20,
class_mode = 'binary')
validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size = (150, 150),
batch_size = 20,
class_mode = 'binary')
# 4x4 grid
nrows = 5
ncols = 5
pic_index = 0
# Set up matpotlib fig and size it to fit 5x5 pics
fig = plt.gcf()
fig.set_size_inches(nrows * 5, ncols * 5)
pic_index += 10
next_seta_pix = [os.path.join(seta_train_dir, fname)
for fname in seta_train_fnames[pic_index-10:pic_index]]
next_setb_pix = [os.path.join(setb_train_dir, fname)
for fname in setb_train_fnames[pic_index-10:pic_index]]
next_setc_pix = [os.path.join(setc_train_dir, fname)
for fname in setc_train_fnames[pic_index-10:pic_index]]
next_setd_pix = [os.path.join(setd_train_dir, fname)
for fname in setd_train_fnames[pic_index-10:pic_index]]
next_sete_pix = [os.path.join(sete_train_dir, fname)
for fname in sete_train_fnames[pic_index-10:pic_index]]
next_setf_pix = [os.path.join(setf_train_dir, fname)
for fname in setf_train_fnames[pic_index-10:pic_index]]
next_setg_pix = [os.path.join(setg_train_dir, fname)
for fname in setg_train_fnames[pic_index-10:pic_index]]
next_seth_pix = [os.path.join(seth_train_dir, fname)
for fname in seth_train_fnames[pic_index-10:pic_index]]
next_seti_pix = [os.path.join(seti_train_dir, fname)
for fname in seti_train_fnames[pic_index-10:pic_index]]
next_setj_pix = [os.path.join(setj_train_dir, fname)
for fname in setj_train_fnames[pic_index-10:pic_index]]
for i, img_path in enumerate(next_seta_pix + next_setb_pix + next_setc_pix + next_setd_pix + next_sete_pix + next_setf_pix + next_setg_pix + next_seth_pix + next_seti_pix + next_setj_pix):
# Set up subplot; subplot indices start at 1
sp = plt.subplot(nrows, ncols, i + 1)
# Dont show axes
sp.axis('Off')
img = mpimg.imread(img_path)
plt.imshow(img)
plt.show()
# Train the model
mymodel = model.fit_generator(
train_generator,
steps_per_epoch = 10,
epochs = 80,
validation_data = validation_generator,
validation_steps = 7,
verbose = 2)
import random
from tensorflow.keras.preprocessing.image import img_to_array, load_img
successive_outputs = [layer.output for layer in model.layers[1:]]
visualization_model = Model(img_input, successive_outputs)
a_img_files = [os.path.join(seta_train_dir, f) for f in seta_train_fnames]
b_img_files = [os.path.join(setb_train_dir, f) for f in setb_train_fnames]
c_img_files = [os.path.join(setc_train_dir, f) for f in setc_train_fnames]
d_img_files = [os.path.join(setd_train_dir, f) for f in setd_train_fnames]
e_img_files = [os.path.join(sete_train_dir, f) for f in sete_train_fnames]
f_img_files = [os.path.join(setf_train_dir, f) for f in setf_train_fnames]
g_img_files = [os.path.join(setg_train_dir, f) for f in setg_train_fnames]
h_img_files = [os.path.join(seth_train_dir, f) for f in seth_train_fnames]
i_img_files = [os.path.join(seti_train_dir, f) for f in seti_train_fnames]
j_img_files = [os.path.join(setj_train_dir, f) for f in setj_train_fnames]
img_path = random.choice(a_img_files + b_img_files + c_img_files + d_img_files + e_img_files + f_img_files + g_img_files + h_img_files + i_img_files + j_img_files)
img = load_img(img_path, target_size = (150, 150))
x = img_to_array(img)
x = x.reshape((1,) + x.shape)
x /= 255
successive_feature_maps = visualization_model.predict(x)
layer_names = [layer.name for layer in model.layers]
for layer_name, feature_map in zip(layer_names, successive_feature_maps):
if len(feature_map.shape) == 4:
# Just do this for the conv/maxpool layers
n_features = feature_map.shape[-1]
# The feature map has shape(1, size, size, n_features)
size = feature_map.shape[1]
# Will tile images in this matrix
display_grid = np.zeros((size, size * n_features))
for i in range(n_features):
# Postprocess the feature
x = feature_map[0, :, :, i]
x -= x.mean()
x *= 64
x += 128
x = np.clip(x, 0, 255).astype('float32')
# Will tile each filter into this big horizontal grid
display_grid[:, i * size : (i + 1) * size] = x
# Accuracy results for each training and validation epoch
acc = mymodel.history['acc']
val_acc = mymodel.history['val_acc']
# Loss results for each training and validation epoch
loss = mymodel.history['loss']
val_loss = mymodel.history['val_loss']
what i understood from your code , your doing multi class classification because you have used Dense(10) at the last layer so
you need to change class_mode = 'binary' into
class model ='categorical' &
also change activation function sigmoid into
output = layers.Dense(10, activation = 'softmax')(x)

How do I test a complex CNN model on a new image?

I am learning CNN and I have found a script online that classifies building rooftops from satellite images. The script works just fine but I am not able to figure out a way to test the script on a new single image. I am showing the code briefly and then I will show what I have tried:
seq = iaa.Sequential([
iaa.imgcorruptlike.Fog(severity=1),
iaa.imgcorruptlike.Spatter(severity =1),
])
batch_size = 16
size = 512
epochs =50
version = 1 # version 2 for MobilV2unet
data_augmentation = True
model_type = 'UNet%d' % (version)
translearn = True
from tensorflow.keras.applications import MobileNetV2
def m_u_net(input_shape):
inputs = Input(shape=input_shape, name="input_image")
encoder = MobileNetV2(input_tensor=inputs, weights="imagenet", include_top=False, alpha=1.3)
#encoder.trainable=False
skip_connection_names = ["input_image", "block_1_expand_relu", "block_3_expand_relu", "block_6_expand_relu"]
encoder_output = encoder.get_layer("block_13_expand_relu").output
f = [16, 32, 48, 64]
x = encoder_output
for i in range(1, len(skip_connection_names)+1, 1):
x_skip = encoder.get_layer(skip_connection_names[-i]).output
x = UpSampling2D((2, 2))(x)
x = Concatenate()([x, x_skip])
x = Conv2D(f[-i], (3, 3), padding="same")(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(f[-i], (3, 3), padding="same")(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(1, (1, 1), padding="same")(x)
x = Activation("sigmoid")(x)
model = Model(inputs, x)
return model
def load_rasters_simple(path, pathX, pathY ): # Subset from original raster with extent and upperleft coord
"""Load training data pairs (two high resolution images and two low resolution images)"""
pathXabs = os.path.join(path, pathX)
pathYabs = os.path.join(path, pathY)
le = len(os.listdir(pathXabs) )
stackX = []
stackY = []
for i in range(0, le):
fileX = os.path.join(pathXabs, os.listdir(pathXabs)[i])
fileY = os.path.join(pathYabs, os.listdir(pathXabs)[i])
dataX = gdal_array.LoadFile(fileX) #.astype(np.int),ysize=extent[1],xsize=extent[0]
stackX.append(dataX)
dataY = gdal_array.LoadFile(fileY) #.astype(np.int),ysize=extent[1],xsize=extent[0]
stackY.append(dataY)
stackX = np.array(stackX)
stackY = np.array(stackY)
return stackX, stackY
X, Y= load_rasters_simple('/Users/vaibhavsaxena/Desktop/segmentation/Classification/Satellite dataset ó± (global cities)','image','label')
def slice (arr, size, inputsize,stride):
result = []
if stride is None:
stride = size
for i in range(0, (inputsize-size)+1, stride):
for j in range(0, (inputsize-size)+1, stride):
s = arr[i:(i+size),j:(j+size), ]
result.append(s)
result = np.array(result)
return result
def batchslice (arr, size, inputsize, stride, num_img):
result = []
for i in range(0, num_img):
s= slice(arr[i,], size, inputsize, stride )
result.append(s )
result = np.array(result)
result = result.reshape(result.shape[0]*result.shape[1], result.shape[2], result.shape[3], -1)
return result
Y=batchslice(Y, size, Y.shape[1], size, Y.shape[0]).squeeze()
X_cl =batchslice(X_cl, size, X_cl.shape[1], size, X_cl.shape[0])
X_train = X_cl[:int(X_cl.shape[0]*0.8),]
Y_train = Y[:int(Y.shape[0]*0.8),]
X_test = X_cl[int(X_cl.shape[0]*0.8)+1:,]
Y_test = Y[int(Y.shape[0]*0.8)+1:,]
THEN the big unet model architecture. The whole script can be found here.
This model just works fine with the dataset. I am trying to test it with my own out of dataset image and this is what I have tried:
model = load_model('no_aug_unet_model.h5', custom_objects=dependencies)
model.compile(loss='binary_crossentropy', metrics=[iou],
optimizer=Adam(learning_rate=lr_schedule(0)))
from keras.preprocessing import image
test_image= image.load_img('bangkok_noi_2.jpg', target_size = (2000, 2000))
test_image = image.img_to_array(test_image)
test_image1 = test_image.reshape((1,2000,2000,3))
testpre = model.predict(test_image1)
img = Image.fromarray(test_image, 'RGB')
img.show()
The original shape of my test image is (1852, 3312, 3).
I am getting a weirdly predicted image that makes no sense unlike the expectations. I believe, I am doing the wrong preprocessing with my test image. Any help would be extremely appreciated.
The whole script can be found here.

Accessing Variables of Custom Layers in Keras

Let's say we have a custom layer in Keras like this:
import numpy as np
import tensorflow as tf
from keras import backend as K
from keras.layers import Layer
class Custom_Layer(Layer):
def __init__(self,**kwargs):
super(ProbabilisticActivation, self).__init__(**kwargs)
self.params_1 = 0
self.params_2 = 0
def build(self, input_shape):
self.params_1 = K.variable(np.zeros(shape=input_shape[1::]))
self.params_2 = K.variable(np.zeros(shape=input_shape[1::]))
super(Custom_Layer,self).build(input_shape)
def call(self, x, training=None):
# DO SOMETHING
How could I access the value of the parameters (params_1, params_2) in the training process? I tried to get parameters by using model.get_layer('Name of Custom Layer').params_1, but in this case, I can not access the value of the parameters.
Here is the model architecture:
def get_model(img_height, img_width:
input_layer = Input(shape=(img_height, img_width, 3))
x = Conv2D(32, (3, 3), padding='same', name='conv2d_1', activation='relu')(input_layer)
x = Custom_Layer()(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
x = Conv2D(64, kernel_size=(3, 3), name='conv2d_2', activation='relu')(x)
x = Conv2D(64, (3, 3), name='conv2d_4', activation='relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
x = Flatten()(x)
x = Dense(512)(x)
x = Activation('relu')(x)
x = Dropout(0.5)(x)
x = Dense(10)(x)
x = Activation('softmax')(x)
model = Model(inputs=[input_layer], outputs=[x])
model.summary()
return model
Note that params_1 and params_2 are TensorFlow tensors. To get their value, you should run them within a tf.Session. You could do something along the lines of:
from keras import backend as K
# ... train model
sess = K.get_session()
params_1 = model.get_layer('Name of Custom Layer').params_1
values_1 = sess.run(params_1)
print(values_1)
NOTE: Not tested.

K.gradients returning [None]

K.gradients() return “None”. (Keras with tensorflow backend). Is there a problem with my model structure or something?
I have modified WGAN-GP model with keras for text, refer to this code: [https://github.com/OctThe16th/WGAN-GP-with-keras-for-text/blob/master/Exploration/GenerativeAdverserialWGAN-GP.py].
from keras.layers import Dense, Flatten, Input, BatchNormalization, Dropout, GRU, Bidirectional, Reshape, Activation
from keras.layers.noise import GaussianNoise
from keras.models import Model
from keras.layers.merge import _Merge
from keras.layers import Convolution1D, AveragePooling1D, ZeroPadding1D, UpSampling1D, concatenate, regularizers
from keras.layers import Embedding, Reshape, Lambda
from keras.layers import LSTM, multiply
from keras.optimizers import Adam, RMSprop
from random import randint
from keras.initializers import Constant
from keras import backend as K
from keras import layers
import numpy as np
from functools import partial
import pickle
import os
import tensorflow as tf
def wasserstein_loss(y_true, y_pred):
return K.mean(y_true * y_pred)
def gradient_penalty_loss(y_true, y_pred, averaged_samples):
'''Computes gradient penalty based on prediction and weighted real / fake samples'''
gradients = K.gradients(K.sum(y_pred), averaged_samples)
# compute the euclidean norm by squaring ...
gradients_sqr = K.square(gradients)
# ... summing over the rows ...
gradients_sqr_sum = K.sum(gradients_sqr)
# ... and sqrt
gradient_l2_norm = K.sqrt(gradients_sqr_sum)
# compute lambda * (1 - ||grad||)^2 still for each single sample
gradient_penalty = K.square(1 - gradient_l2_norm)
# return the mean as loss over all the batch samples
return K.mean(gradient_penalty)
class RandomWeightedAverage(_Merge):
def _merge_function(self, inputs):
weights = K.random_uniform((BATCH_SIZE, 1), dtype='float32')
return (weights * inputs[0]) + ((1 - weights) * inputs[1])
#K.argmax() is not differentiable, this function is defined to repalce K.argmax(), and it is differentiable.
def argmax(x):
y = tf.reduce_sum(tf.cumsum(tf.ones_like(x), axis=-1) * tf.exp(beta * x) / tf.reduce_sum(tf.exp(beta * x), axis=-1, keep_dims=True), axis=-1) - 1
return y
def generator_mod(softmax_shape):
person1_input = Input(shape=(1,), dtype='float32')
noise_input = Input(shape=(1, person_embedding_dim), dtype='float32')
relation_input = Input(shape=(1,), dtype='float32')
person1_embedded = Embedding(1,person_embedding_dim)(person1_input)
relation_embedded = Embedding(1,relation_embedding_dim)(relation_input)
embedded_layer = concatenate([person1_embedded, relation_embedded, noise_input], axis=1)
drop_1 = BatchNormalization(momentum=0.8)(embedded_layer)
x_1 = Convolution1D(filters=64, kernel_size=3, padding='same', activation='elu')(drop_1)
x_1 = BatchNormalization()(x_1)
x_1 = Convolution1D(filters=32, kernel_size=3, padding='same', activation='elu')(x_1)
x_1 = BatchNormalization()(x_1)
x_1 = Flatten()(x_1)
x_1 = Dense(32, activation='relu')(x_1)
######################################################################
person1_description = Input(shape=(max_sequence_length,), dtype='float32')
embedded_sequences1 = Embedding(len(word_index) + 1, word_embeddings_dim)(person1_description)
lstm_out1 = Bidirectional(LSTM(64))(embedded_sequences1)
attention_1 = Dense(128, activation='softmax', name='attention_vec')(lstm_out1)
attention_mul = multiply([lstm_out1, attention_1], name='attention_mul')
#####globel attention finish#####
x_2 = Dense(32, activation='relu')(attention_mul)
full_connected = multiply([x_1, x_2], name='full_connected')
x = Dense(softmax_shape, activation='softmax')(full_connected)
output = Lambda(argmax)(x)#shape (?,)
output = Lambda(K.expand_dims, arguments={'axis': -1})(output) #shape (?,1)
model = Model(inputs = [person1_input, noise_input, relation_input, person1_description], outputs = output)
return model
def discriminator_mod():
person1_input = Input(shape=(1,), dtype='float32')
person2_input = Input(shape=(1,), dtype='float32')
relation_input = Input(shape=(1,), dtype='float32')
person1_embedded = Embedding(1, person_embedding_dim)(person1_input)
person2_embedded = Embedding(1, person_embedding_dim)(person2_input)
relation_embedded = Embedding(len(word_index) + 1, word_embeddings_dim)(relation_input)
embedded_layer = concatenate([person1_embedded, person2_embedded, relation_embedded], axis=1)
drop_1 = Dropout(0.5)(embedded_layer)
x = Convolution1D(128, 1, activation='relu')(drop_1)
x = BatchNormalization()(x)
x = Convolution1D(filters=64, kernel_size=3, padding='same', activation='elu')(x)
x = BatchNormalization()(x)
x = Convolution1D(filters=32, kernel_size=3, padding='same', activation='elu')(x)
x = BatchNormalization()(x)
x = Flatten()(x)
x = Dense(32, activation='relu')(x)
auxiliary_input1 = Input(shape=(max_sequence_length,), dtype='float32', name='aux_input1')
embedded_sequences1 = Embedding(len(word_index) + 1, word_embeddings_dim)(auxiliary_input1)
lstm_out1 = Bidirectional(LSTM(64))(embedded_sequences1)
lstm_drop1 = Dropout(0.5)(lstm_out1)
auxiliary_input2 = Input(shape=(max_sequence_length,), dtype='float32', name='aux_input2')
embedded_sequences2 = Embedding(len(word_index) + 1, word_embeddings_dim)(auxiliary_input2)
lstm_out2 = Bidirectional(LSTM(64))(embedded_sequences2)
lstm_drop2 = Dropout(0.5)(lstm_out2)
lstm_drop = multiply([lstm_drop1, lstm_drop2])
#####globel attention start#####
attention_1 = Dense(128, activation='softmax', name='attention_vec')(lstm_drop)
attention_mul = multiply([lstm_drop, attention_1], name='attention_mul')
#####globel attention finish#####
# attention_mul = Flatten()(attention_mul)
attention_mul = Dense(32, activation='relu')(attention_mul)
#####globel attention start#####
full_connected = multiply([x, attention_mul], name='full_connected')
attention_2 = Dense(32, activation='softmax')(full_connected)
attention_final = multiply([full_connected, attention_2])
#####globel attention finish#####
dense_layer = Dense(16, activation='relu')(attention_final)
main_output = Dense(1, activation='tanh', name='main_output')(dense_layer)
model = Model(inputs=[person1_input, person2_input, relation_input, auxiliary_input1, auxiliary_input2], outputs= main_output)
return model
def train(from_save_point=False, suffix='rnn'):
X_train = np.random.randn(10,243)
generator = generator_mod(person_total)
discriminator = discriminator_mod()
generator.summary()
discriminator.summary()
for layer in discriminator.layers:
layer.trainable = False
discriminator.trainable = False
for layer in discriminator.layers:
layer.trainable = False
discriminator.trainable = False
person1 = Input(shape=(1,))
relation = Input(shape=(1,))
seed = Input(shape=(1,person_embedding_dim))
person1_description = Input(shape=(max_sequence_length,))
genarated_person2 = generator([person1, seed, relation, person1_description])
person2_description = Input(shape=(max_sequence_length,))
discriminator_layers_for_generator = discriminator([person1, genarated_person2, relation, person1_description, person2_description])
generator_model = Model(inputs=[person1, relation, seed, person1_description, person2_description], outputs=[discriminator_layers_for_generator])
generator_model.compile(optimizer= RMSprop(lr=0.0001, rho=0.9), loss=wasserstein_loss)
for layer in discriminator.layers:
layer.trainable = True
for layer in generator.layers:
layer.trainable = False
discriminator.trainable = True
generator.trainable = False
person2 = Input(shape=(1,))
generated_samples_for_discriminator = generator([person1, seed, relation, person1_description])
discriminator_output_from_generator = discriminator([person1, generated_samples_for_discriminator, relation, person1_description, person2_description])
discriminator_output_from_real_samples = discriminator([person1, person2, relation, person1_description, person2_description])
averaged_samples = RandomWeightedAverage()([person2, generated_samples_for_discriminator])
averaged_samples_out = discriminator([person1, averaged_samples, relation, person1_description, person2_description])
partial_gp_loss = partial(gradient_penalty_loss, averaged_samples= averaged_samples)
partial_gp_loss.__name__ = 'gradient_penalty'
discriminator_model = Model(inputs=[person1, person2, relation, person1_description, person2_description, seed], outputs=[discriminator_output_from_real_samples, discriminator_output_from_generator, averaged_samples_out])
# averaged_samples_out
discriminator_model.compile(optimizer=RMSprop(lr=0.0001, rho=0.9), loss=[wasserstein_loss, wasserstein_loss, partial_gp_loss])
# partial_gp_loss
positive_y = np.ones((BATCH_SIZE, 1), dtype=np.float32)
negative_y = -positive_y
dummy_y = np.zeros((BATCH_SIZE, 1), dtype=np.float32)
if __name__ == "__main__":
# convert_text_to_nptensor(cutoff=50, min_frequency_words=100000, max_lines=20000000)
train(from_save_point=False, suffix='Google')
However, when the code execute to this line:
gradients = K.gradients(K.sum(y_pred), averaged_samples)
The error message is that:
'TypeError: Failed to convert object of type to Tensor. Contents: [None]. Consider casting elements to a supported type'
Can anyone help me? Thank you very much!

Resources