How to fix the issue of Input has undefined rank in TensorFlow? - tensorflow

I am trying to define a custom DensNet. But, I am getting a weird error and mot understand why. The code is as follows:
def densenet(input_shape, n_classes, filters = 32):
#batch norm + relu + conv
def bn_rl_conv(x,filters,kernel=1,strides=1):
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(filters, kernel, strides=strides,padding = 'same')(x)
return x
def dense_block(x, repetition):
for _ in range(repetition):
y = bn_rl_conv(x, 4*filters)
y = bn_rl_conv(y, filters, 3)
x = concatenate([y,x])
return x
def transition_layer(x):
x = bn_rl_conv(x, K.int_shape(x)[-1] //2 )
x = AvgPool2D(2, strides = 2, padding = 'same')(x)
return x
inp = Input (input_shape)
x = Conv2D(64, 7, strides = 2, padding = 'same')(inp)
x = MaxPool2D(3, strides = 2, padding = 'same')(x)
for repetition in [2,4,6,4]:
d = dense_block(x, repetition)
x = transition_layer(d)
x = GlobalAveragePooling2D()(x)
output = Dense(n_classes, activation = 'softmax')(x)
model = Model(inp, output)
return model
input_shape = (1024,2,1)
num_classes = 24
model = densenet(input_shape,num_classes)
The error is stating the following:
/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/keras/layers/normalization/batch_normalization.py in build(self, input_shape)
296 if not input_shape.ndims:
297 raise ValueError(
--> 298 f'Input has undefined rank. Received: input_shape={input_shape}.')
299 ndims = len(input_shape)
300
ValueError: Input has undefined rank. Received: input_shape=<unknown>.
Why am I getting this error? I have already indicated the input shape. How can I fix this issue?

You are calling Input layer incorrectly. You're passing input_shape to the __call__() method instead of the shape parameter.
Change:
inp = Input (input_shape)
To:
inp = Input(shape=input_shape)

Related

The truth value of an array with more than one element is ambiguous. Reinforcement Learning Agent

I'm starting in DQN, creating an agent to run in a custom environment :
class ReplayBuffer:
def __init__(self, capacity=10000):
self.buffer = deque(maxlen=capacity)
def store(self, state, action, reward, next_state, done):
self.buffer.append([state, action, reward, next_state, done])
def sample(self):
sample = random.sample(self.buffer, args.batch_size)
states, actions, rewards, next_states, done = map(np.asarray, zip(*sample))
states = np.array(states).reshape(args.batch_size, -1)
next_states = np.array(next_states).reshape(args.batch_size, -1)
return states, actions, rewards, next_states, done
def size(self):
return len(self.buffer)
class DQN:
def __init__(self, state_img, state_legal_actions,
state_price, state_risk_reward,
action_dim, state_dim):
self.state_img = state_img
self.state_legal_actions = state_legal_actions
self.state_price = state_price
self.state_risk_reward = state_risk_reward
self.action_dim = action_dim
self.state_dim = state_dim
self.epsilon = args.eps
# Define the input layers
self.df_img_input = self.state_img.shape
self.rr_input = self.state_risk_reward.shape
self.legal_actions_input = self.state_legal_actions.shape
self.price_input = self.state_price.shape
self.model = self.nn_model()
def nn_model(self):
# Create input layers
input_img = Input(shape = self.df_img_input)
input_numeric1 = Input(shape = self.price_input)
input_numeric2 = Input(shape = self.legal_actions_input)
input_numeric3 = Input(shape = self.rr_input)
# Create the CNN for the image input
cnn = Conv2D(64, (3,3), activation='relu',
padding='same')(input_img)
cnn = MaxPooling2D()(cnn)
cnn = Dropout(0.25)(cnn)
cnn = Conv2D(128, (3,3), activation='relu',
padding='same')(input_img)
cnn = MaxPooling2D()(cnn)
cnn = Dropout(0.25)(cnn)
cnn = Conv2D(128, (3,3), activation='relu',
padding='same')(input_img)
cnn = Dropout(0.2)(cnn)
cnn = Flatten()(cnn)
# Create the DNN for the numeric inputs
dnn1 = Dense(64, activation='relu')(input_numeric1)
dnn1 = Dropout(0.25)(dnn1)
dnn2 = Dense(32, activation='relu')(input_numeric2)
dnn2 = Dropout(0.25)(dnn2)
dnn3 = Dense(32, activation='relu')(input_numeric3)
dnn3 = Dropout(0.25)(dnn3)
# Concatenate the outputs from the CNN and DNN
concat = Concatenate()([cnn, dnn1, dnn2, dnn3])
# Create the output layers
dense1 = Dense(128, activation='relu')(concat)
dense1 = Dropout(0.25)(dense1)
dense2 = Dense(128, activation='relu')(dense1)
dense2 = Dropout(0.25)(dense2)
dense3 = Dense(self.action_dim, activation='sigmoid')(dense2)
# Create the model
model = Model(inputs=[input_img, input_numeric1, input_numeric2, input_numeric3], outputs=dense3)
# Compile the model
model.compile(optimizer='adam',
loss=tf.keras.losses.MeanSquaredError(),
metrics=['accuracy'])
return model
def predict(self, input_img,input_numeric1,input_numeric2, input_numeric3):
return self.model.predict(input_img,input_numeric1,input_numeric2, input_numeric3)
def get_action(self, state):
state = [state['df_img'], state['price'], state['legal_actions'], state['risk_reward']]
self.epsilon *= args.eps_decay
self.epsilon = max(self.epsilon, args.eps_min)
q_value = self.predict(state[0],state[1],state[2],state[3])
if np.random.random() < self.epsilon:
return random.randint(0, self.action_dim - 1)
return np.argmax(q_value)
def train(self, states, targets):
self.model.fit(states, targets, epochs=2)
class Agent:
def __init__(self, env):
self.env = env
self.obs_dim = ['df_img', 'legal_actions', 'price', 'risk_reward']
self.state_img = self.env.observation_space[self.obs_dim[0]]
self.state_legal_actions = self.env.observation_space[self.obs_dim[1]]
self.state_price = self.env.observation_space[self.obs_dim[2]]
self.state_risk_reward = self.env.observation_space[self.obs_dim[3]]
self.state_dim = np.hstack((self.state_img, self.state_risk_reward,
self.state_price, self.state_legal_actions))
self.action_dim = self.env.action_space.n
self.model = DQN(self.state_img, self.state_legal_actions,
self.state_price,self.state_risk_reward,
self.action_dim, self.state_dim)
self.target_model = DQN(self.state_img, self.state_legal_actions,
self.state_price,self.state_risk_reward,
self.action_dim, self.state_dim)
self.update_target()
self.buffer = ReplayBuffer()
def update_target(self):
weights = self.model.model.get_weights()
self.target_model.model.set_weights(weights)
def replay_experience(self):
for _ in range(10):
states, actions, rewards, next_states, done = self.buffer.sample()
targets = self.model.predict(states)
next_q_values = self.target_model.predict(next_states).max(axis=1)
targets[range(args.batch_size), actions] = (
rewards + (1 - done) * next_q_values * args.gamma
)
self.model.train(states, targets)
def train(self, max_episodes=1000):
for ep in range(max_episodes):
done, episode_reward = False, 0
observation = self.env.reset()
#print(observation[0])
while not done:
action = self.model.get_action(observation)
next_observation, reward, done, _ = self.env.step(action)
self.buffer.store(
observation, action, reward * 0.01, next_observation, done
)
episode_reward += reward
observation = next_observation
if self.buffer.size() >= args.batch_size:
self.replay_experience()
self.update_target()
print(f"Episode#{ep} Reward:{episode_reward}")
tf.summary.scalar("episode_reward", episode_reward, step=ep)
but when i run it with :
if __name__ == "__main__":
env = TradingEnv(df_)
agent = Agent(env)
agent.train(max_episodes=2)
i receive the folllowing error:
--------------------------------------------------------------------------- ValueError Traceback (most recent call
last) /tmp/ipykernel_3672/1951022009.py in
2 env = ForexTradingEnv(df_)
3 agent = Agent(env)
----> 4 agent.train(max_episodes=2) # Increase max_episodes value
5
/tmp/ipykernel_3672/3112839645.py in train(self, max_episodes)
43
44 while not done:
---> 45 action = self.model.get_action(observation)
46 next_observation, reward, done, _ = self.env.step(action)
47 self.buffer.store(
/tmp/ipykernel_3672/2209466094.py in get_action(self, state)
78 self.epsilon *= args.eps_decay
79 self.epsilon = max(self.epsilon, args.eps_min)
---> 80 q_value = self.predict(state[0],state[1],state[2],state[3])
81 if np.random.random() < self.epsilon:
82 return random.randint(0, self.action_dim - 1)
/tmp/ipykernel_3672/2209466094.py in predict(self, input_img,
input_numeric1, input_numeric2, input_numeric3)
71
72 def predict(self, input_img,input_numeric1,input_numeric2, input_numeric3):
---> 73 return self.model.predict(input_img,input_numeric1,input_numeric2,
input_numeric3)
74
75 def get_action(self, state):
~/miniconda3/envs/tf/lib/python3.9/site-packages/keras/utils/traceback_utils.py
in error_handler(*args, **kwargs)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
~/miniconda3/envs/tf/lib/python3.9/site-packages/keras/engine/data_adapter.py
in init(self, x, y, sample_weights, sample_weight_modes,
batch_size, epochs, steps, shuffle, **kwargs)
262 # If batch_size is not passed but steps is, calculate from the input
263 # data. Default to 32 for backwards compat.
--> 264 if not batch_size:
265 batch_size = int(math.ceil(num_samples / steps)) if steps else 32
266
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
if you think you can help here would be really nice!
thanks in advance
best regards

Saving weights in custom model in Tensorflow(using Custom Layers and low level API )

I have created a model in Tensorflow with the following structure:
def resnet50():
input_im = Input(shape=(train_im.shape[1], train_im.shape[2], train_im.shape[3])) # cifar 10 images size
x = ZeroPadding2D(padding=(3, 3))(input_im)
# 1st stage
# here we perform maxpooling, see the figure above
x = Conv2D(64, kernel_size=(7, 7), strides=(2, 2))(x)
x = BatchNormalization()(x)
x = Activation(activations.relu)(x)
x = MaxPooling2D((3, 3), strides=(2, 2))(x)
#2nd stage
# frm here on only conv block and identity block, no pooling
x = res_conv(x, s=1, filters=(64, 256))
x = res_identity(x, filters=(64, 256))
x = res_identity(x, filters=(64, 256))
# 3rd stage
x = res_conv(x, s=2, filters=(128, 512))
x = res_identity(x, filters=(128, 512))
x = res_identity(x, filters=(128, 512))
x = res_identity(x, filters=(128, 512))
# 4th stage
x = res_conv(x, s=2, filters=(256, 1024))
x = res_identity(x, filters=(256, 1024))
x = res_identity(x, filters=(256, 1024))
x = res_identity(x, filters=(256, 1024))
x = res_identity(x, filters=(256, 1024))
x = res_identity(x, filters=(256, 1024))
# 5th stage
x = res_conv(x, s=2, filters=(512, 2048))
x = res_identity(x, filters=(512, 2048))
x = res_identity(x, filters=(512, 2048))
# ends with average pooling and dense connection
x = AveragePooling2D((2, 2), padding='same')(x)
x = Flatten()(x)
x = Dense(len(class_types), activation='softmax', kernel_initializer='he_normal')(x) #multi-class
# define the model
model = Model(inputs=input_im, outputs=x, name='Resnet50')
return model
All of the layers are custom layers, which were created uswing low level API:
Foe example one of the layers:
class Softmax_layer(tf.keras.layers.Layer):
def __init__(self,dim_out):
super(Softmax_layer,self).__init__()
self.initializer = tf.initializers.GlorotUniform()
self.dim_out = dim_out
def build(self, input_shape):
self.W = self.add_weight(shape=(input_shape[-1], self.dim_out),initializer = self.initializer,trainable=True)
self.b = self.add_weight(shape=(self.dim_out,), initializer = self.initializer, trainable=True)
def call(self,x):
return tf.nn.softmax(tf.matmul(x,self.W) + self.b)
I have trained my model succesfully, but the problem is, that when I want to save weights of my model I am getting the following error:
model.save_weights('weights')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-13-864cc1978a76> in <module>
----> 1 model.save_weights('weights')
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/graph_view.py in _escape_local_name(name)
58 # edges traversed to reach the variable, so we escape forward slashes in
59 # names.
---> 60 return (name.replace(_ESCAPE_CHAR, _ESCAPE_CHAR + _ESCAPE_CHAR)
61 .replace(r"/", _ESCAPE_CHAR + "S"))
62
AttributeError: 'NoneType' object has no attribute 'replace'
How can I fix this issue? What is the correct way of saving model's weights using low level API?

Why flatten() is not working in co-lab whereas it worked in kaggle-notebook posted by other user?

I am working on a project for pneumonia detection. I have looked over kaggle for notebooks on the same. there was a user who stacked two pretrained model densenet169 and mobilenet. I copies whole kaggle notebook from the user where he didn't get any error, but when I ran it in google colab I get this error in this part:
part where error is:
from keras.layers.merge import concatenate
from keras.layers import Input
import tensorflow as tf
input_shape = (224,224,3)
input_layer = Input(shape = (224, 224, 3))
#first model
base_mobilenet = MobileNetV2(weights = 'imagenet', include_top = False, input_shape = input_shape)
base_densenet = DenseNet169(weights = 'imagenet', include_top = False, input_shape = input_shape)
for layer in base_mobilenet.layers:
layer.trainable = False
for layer in base_densenet.layers:
layer.trainable = False
model_mobilenet = base_mobilenet(input_layer)
model_mobilenet = GlobalAveragePooling2D()(model_mobilenet)
output_mobilenet = Flatten()(model_mobilenet)
model_densenet = base_densenet(input_layer)
model_densenet = GlobalAveragePooling2D()(model_densenet)
output_densenet = Flatten()(model_densenet)
merged = tf.keras.layers.Concatenate()([output_mobilenet, output_densenet])
x = BatchNormalization()(merged)
x = Dense(256,activation = 'relu')(x)
x = Dropout(0.5)(x)
x = BatchNormalization()(x)
x = Dense(128,activation = 'relu')(x)
x = Dropout(0.5)(x)
x = Dense(1, activation = 'sigmoid')(x)
stacked_model = tf.keras.models.Model(inputs = input_layer, outputs = x)
Error Traceback:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-69c389bc7252> in <module>()
18 model_mobilenet = base_mobilenet(input_layer)
19 model_mobilenet = GlobalAveragePooling2D()(model_mobilenet)
---> 20 output_mobilenet = Flatten(data_format=None)(model_mobilenet)
21
22 model_densenet = base_densenet(input_layer)
5 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
1028 with autocast_variable.enable_auto_cast_variables(
1029 self._compute_dtype_object):
-> 1030 outputs = call_fn(inputs, *args, **kwargs)
1031
1032 if self._activity_regularizer:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/core.py in call(self, inputs)
672 # Full static shape is guaranteed to be available.
673 # Performance: Using `constant_op` is much faster than passing a list.
--> 674 flattened_shape = constant_op.constant([inputs.shape[0], -1])
675 return array_ops.reshape(inputs, flattened_shape)
676 else:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
263 """
264 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 265 allow_broadcast=True)
266
267
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
274 with trace.Trace("tf.constant"):
275 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 276 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
277
278 g = ops.get_default_graph()
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
299 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
300 """Implementation of eager constant."""
--> 301 t = convert_to_eager_tensor(value, ctx, dtype)
302 if shape is None:
303 return t
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
96 dtype = dtypes.as_dtype(dtype).as_datatype_enum
97 ctx.ensure_initialized()
---> 98 return ops.EagerTensor(value, ctx.device_name, dtype)
99
100
ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.
You have mixed up your imports a bit.
Here is a fixed version of your code
from tensorflow.keras.layers import concatenate
from tensorflow.keras.layers import Input, GlobalAveragePooling2D, Flatten, BatchNormalization, Dense, Dropout
from tensorflow.keras.applications import MobileNetV2, DenseNet169
import tensorflow as tf
input_shape = (224,224,3)
input_layer = Input(shape = (224, 224, 3))
#first model
base_mobilenet = MobileNetV2(weights = 'imagenet', include_top = False, input_shape = input_shape)
base_densenet = DenseNet169(weights = 'imagenet', include_top = False, input_shape = input_shape)
for layer in base_mobilenet.layers:
layer.trainable = False
for layer in base_densenet.layers:
layer.trainable = False
model_mobilenet = base_mobilenet(input_layer)
model_mobilenet = GlobalAveragePooling2D()(model_mobilenet)
output_mobilenet = Flatten()(model_mobilenet)
model_densenet = base_densenet(input_layer)
model_densenet = GlobalAveragePooling2D()(model_densenet)
output_densenet = Flatten()(model_densenet)
merged = tf.keras.layers.Concatenate()([output_mobilenet, output_densenet])
x = BatchNormalization()(merged)
x = Dense(256,activation = 'relu')(x)
x = Dropout(0.5)(x)
x = BatchNormalization()(x)
x = Dense(128,activation = 'relu')(x)
x = Dropout(0.5)(x)
x = Dense(1, activation = 'sigmoid')(x)
stacked_model = tf.keras.models.Model(inputs = input_layer, outputs = x)

DeblurGAN can't load his own weights anymore

Hey I realy need some help =)
firstly, sorry that it's soo long^^ but I hope that you don't need the full code at the end.
I coded a GAN for deblurring. Now I'm training it. the first 71 epochs have been trained without any problems: I trained some epochs till the colab GPU-time limit was reached, the next day I loaded my weights into the gan and continued training.
2 or 3 weeks ago I wanted to load the weights of epoch 71 in my Gan but I recieved the following error (I'm quite sure that I didn't change anything in the code). Since this moment I only can load the first 65 weights and i get the same error for every epoch higher than 65:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-16-a35c9a2bbf3a> in <module>()
1 # Load weights
----> 2 gan.load_weights(F"/content/gdrive/My Drive/Colab Notebooks/data/deblurGAN_weights66_batchsize_1.h5")
5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in load_weights(self, filepath, by_name, skip_mismatch, options)
2209 f, self.layers, skip_mismatch=skip_mismatch)
2210 else:
-> 2211 hdf5_format.load_weights_from_hdf5_group(f, self.layers)
2212
2213 def _updated_config(self):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/hdf5_format.py in load_weights_from_hdf5_group(f, layers)
706 str(len(weight_values)) + ' elements.')
707 weight_value_tuples += zip(symbolic_weights, weight_values)
--> 708 K.batch_set_value(weight_value_tuples)
709
710
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
199 """Call target, and fall back on dispatchers if there is a TypeError."""
200 try:
--> 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in batch_set_value(tuples)
3574 if ops.executing_eagerly_outside_functions():
3575 for x, value in tuples:
-> 3576 x.assign(np.asarray(value, dtype=dtype(x)))
3577 else:
3578 with get_graph().as_default():
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py in assign(self, value, use_locking, name, read_value)
856 with _handle_graph(self.handle):
857 value_tensor = ops.convert_to_tensor(value, dtype=self.dtype)
--> 858 self._shape.assert_is_compatible_with(value_tensor.shape)
859 assign_op = gen_resource_variable_ops.assign_variable_op(
860 self.handle, value_tensor, name=name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in assert_is_compatible_with(self, other)
1132 """
1133 if not self.is_compatible_with(other):
-> 1134 raise ValueError("Shapes %s and %s are incompatible" % (self, other))
1135
1136 def most_specific_compatible_shape(self, other):
ValueError: Shapes (4, 4, 64, 128) and (64,) are incompatible
I was looking a long time for a solution and i didn't find a real one. But I found out, that if I train one epoch with one of the old weights (1-65) afterwards I can load one of the new weights. So I thought that I could use this "workaround" but yesterday I plotted the scores of the metric of the Test dataset for every epoch. I recieved this picture:
psnrscore/epoch
as you can see it looks like I'm producing trash since epoch 65 (on the pic since 60 because I lost the first 5 epochs, so it starts by 6)
I'm realy frustrated and hope that someone could help me =D
Here's the full code of the GAN:
# Libraries to build the model
from tensorflow import pad
from tensorflow.keras.layers import Layer
from keras.layers import Input, Activation, Add, UpSampling2D
from keras.layers.merge import Add
from keras.layers.core import Dropout, Dense, Flatten
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.core import Lambda
from keras.layers.normalization import BatchNormalization
from keras.models import Model
import keras.backend as K
from keras.applications.vgg16 import VGG16
from keras.optimizers import Adam
import keras
# Reflection padding
from keras.engine import InputSpec
import tensorflow as tf
from keras.engine.topology import Layer
'''
2D Reflection Padding
Attributes:
- padding: (padding_width, padding_height) tuple
'''
class ReflectionPadding2D(Layer):
def __init__(self, padding=(1, 1), **kwargs):
self.padding = tuple(padding)
self.input_spec = [InputSpec(ndim=4)]
super(ReflectionPadding2D, self).__init__(**kwargs)
def compute_output_shape(self, s):
""" If you are using "channels_last" configuration"""
return (s[0], s[1] + 2 * self.padding[0], s[2] + 2 * self.padding[1], s[3])
def call(self, x, mask=None):
w_pad,h_pad = self.padding
return tf.pad(x, [[0,0], [h_pad,h_pad], [w_pad,w_pad], [0,0] ], 'REFLECT')
# Res Block
def res_block(input, filters, kernel_size = (3,3), strides = (1,1), use_dropout = False):
"""
Instanciate a Keras Resnet Block using sequential API.
:param input: Input tensor
:param filters: Number of filters to use
:param kernel_size: Shape of the kernel for the convolution
:param strides: Shape of the strides for the convolution
:param use_dropout: Boolean value to determine the use of dropout
:return: Keras Model
"""
x = ReflectionPadding2D((1,1))(input)
x = Conv2D(filters = filters,
kernel_size = kernel_size,
strides = strides,)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
if use_dropout:
x = Dropout(0.5)(x)
x = ReflectionPadding2D((1,1))(x)
x = Conv2D(filters = filters,
kernel_size = kernel_size,
strides = strides,)(x)
x = BatchNormalization()(x)
# Two convolution layers followed by a direct connection between input and output (skip connection)
out = Add()([input, x])
return out
# Generator
n_res_blocks = 9
def generator_model():
# encoder
inputs = Input(shape = img_shape)
x = ReflectionPadding2D((3, 3))(inputs)
x = Conv2D(filters = 64, kernel_size = (7,7), padding = 'valid')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(128, (3,3), strides=2, padding='same') (x) #DIM(15,15,128)
x = BatchNormalization() (x)
x = Activation('relu') (x)
x = Conv2D(256, (3,3), strides = 2, padding = 'same') (x) #DIM(7,7,256)
x = BatchNormalization() (x)
x = Activation('relu') (x)
# Apply 9 res blocks
for i in range(n_res_blocks):
x = res_block(x, 256, use_dropout = True)
# decoder
#x = Conv2DTranspose(128, (3,3), strides = 2, padding = 'same') (x)
x = UpSampling2D()(x)
x = Conv2D(filters = 128, kernel_size=(3, 3), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
#x = Conv2DTranspose(64, (3,3), strides = 2, padding = 'same') (x)
x = UpSampling2D()(x)
x = Conv2D(filters = 64, kernel_size=(3, 3), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = ReflectionPadding2D((3,3))(x)
x = Conv2D(filters = 3, kernel_size = (7,7), padding = 'valid')(x)
x = Activation('tanh')(x)
# Add direct connection from input to output and recenter to [-1, 1] (skip connection)
outputs = Add()([x, inputs])
outputs = Lambda(lambda z: z/2)(outputs) # to keep normalized outputs
model = Model(inputs = inputs, outputs = outputs, name = 'Generator')
return model
# Discriminator
def discriminator_model():
Input_img = Input(shape=(img_shape))
x = Conv2D(filters = 64, kernel_size = (4, 4), strides = 2, padding='same')(Input_img)
x = LeakyReLU(0.2)(x)
nf_mult, nf_mult_prev = 1, 1
for n in range(3):
nf_mult_prev, nf_mult = nf_mult, min(2**n, 8)
x = Conv2D(filters = 64*nf_mult, kernel_size = (4, 4), strides = 2, padding = 'same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x)
nf_mult_prev, nf_mult = nf_mult, 8
x = Conv2D(filters = 64*nf_mult, kernel_size = (4, 4), strides = 1, padding = 'same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x)
x = Conv2D(filters = 1, kernel_size = (4, 4), strides = 1, padding = 'same')(x)
x = Flatten()(x)
x = Dense(1024, activation = 'tanh')(x)
x = Dense(1, activation = 'sigmoid')(x)
model = Model(inputs = Input_img, outputs = x, name = 'discriminator')
return model
def gan_model(generator, discriminator):
inputs = Input(shape = img_shape)
generated_images = generator(inputs)
outputs = discriminator(generated_images)
model = Model(inputs=inputs, outputs = [generated_images, outputs])
return model
#Losses
#Wassersteinloss:
def wasserstein_loss(y_true, y_pred):
return K.mean(y_true * y_pred)
# vgg16 model for perceptual loss
vgg = VGG16(include_top = False, weights = 'imagenet', input_shape = img_shape)
loss_model = Model(inputs = vgg.input, outputs = vgg.get_layer('block3_conv3').output)
loss_model.trainable = False
#perceptual loss:
def perceptual_loss(y_true, y_pred):
return K.mean(K.square(loss_model(y_true) - loss_model(y_pred)))
#Metrics:
#SSIM:
def ssim_metric(y_true, y_pred):
return tf.reduce_mean(tf.image.ssim(tf.convert_to_tensor(y_true),tf.convert_to_tensor(y_pred), max_val=1.0, ))
#PSNR:
def psnr_metric(y_true, y_pred):
return tf.reduce_mean(tf.image.psnr(y_true, y_pred, max_val=1.0))
def training(epochs, batch_size):
path_psnr = F"/content/gdrive/My Drive/Colab Notebooks/data/psnr"
path_ssim = F"/content/gdrive/My Drive/Colab Notebooks/data/ssim"
GAN_losses = []
#psnrs = []
#ssims = []
random_idx = np.arange(0, X_train.shape[0])
n_batches = int (len(random_idx)/batch_size) #divide trainingset into batches of batch_size
for e in range(epochs):
#weights_name = "deblurGAN_weights%s_batchsize_%r.h5" %(e + 66, batch_size)
weights_name = "deblurGAN_weights_test.h5"
print("epoch: %s " %(e + 66))
#randomize index of trainig set
random.shuffle(random_idx)
for i in range(n_batches):
img_batch_blured = X_train[i*batch_size:(i+1)*batch_size]
img_batch_generated = generator.predict(img_batch_blured)
img_batch_original = Y_train[i*batch_size:(i+1)*batch_size]
img_batch = np.concatenate((img_batch_generated , img_batch_original),0)
valid0 = -np.ones(batch_size)
valid1 = np.ones(batch_size)
valid = np.concatenate((valid0,valid1))
discriminator.trainable = True
for k in range(5):
loss = discriminator.train_on_batch(img_batch, valid)
discriminator.trainable = False
GAN_loss = gan.train_on_batch(img_batch_blured, [img_batch_original, valid1])
GAN_losses.append(GAN_loss)
if (100*i/n_batches).is_integer():
psnr = psnr_metric(img_batch_original, img_batch_generated)
ssim = ssim_metric(img_batch_original, img_batch_generated)
psnrs.append(psnr)
ssims.append(ssim)
#creating 2 files in Google Drive where the psnr and ssim data will be saved.
pickle.dump( psnrs, open( path_psnr, "wb" ) )
pickle.dump( ssims, open( path_ssim, "wb" ) )
print((100*i/n_batches) + 1, "% psnr: ", psnr," ssim: ", ssim)
# Save weights: mode the path to your directory
gan.save_weights(F"/content/gdrive/My Drive/Colab Notebooks/data/{weights_name}")
return [GAN_losses, psnrs, ssims]
# Initialize models
generator = generator_model()
discriminator = discriminator_model()
gan = gan_model(generator, discriminator)
# Initialize optimizers
d_opt = Adam(lr=1E-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
gan_opt = Adam(lr=1E-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
# Compile models
discriminator.trainable = True
discriminator.compile(optimizer = d_opt, loss = wasserstein_loss)
discriminator.trainable = False
loss = [perceptual_loss, wasserstein_loss]
loss_weights = [100, 1]
gan.compile(optimizer = gan_opt, loss = loss, loss_weights = loss_weights)
discriminator.trainable = True
gan.summary()
# Load weights
gan.load_weights(F"/content/gdrive/My Drive/Colab Notebooks/data/deblurGAN_weights66_batchsize_1.h5")
#connect to GPU
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))
loss = training(1, 1) #epochs, batchsize
It is solved an can be closed. I didn't know that the "discriminato.Trainable = True/False" was changed. It seems to be the reason for another ordering in the weights.

TypeError: argument 0 is not a Variable

I am trying to learn CNN with my own data. The shape of the data is (1224, 15, 23). 1224 is the number of data, and each data is (15, 23). CNN is built with PyTorch.
I think there is no logical error because conv2D needs 4-D tensor, and I feed (batch, channel, x, y).
when I build an instance of the Net class I got this error.
TypeError: argument 0 is not a Variable
I have been using PyTroch for half of a year but this error is the first time and I am still confused.
Here is my code.
class Net(nn.Module):
def __init__(self, n):
super(Net,self).__init__()
self.conv = nn.Sequential(nn.Conv2d(1, 32, kernel_size=3, stride=1),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=3, stride=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1), # 64 x 9 x 17
nn.ReLU()
)
conv_out_size = self._get_conv_out(input_shape)
self.fc = nn.Sequential(nn.Linear(64 * 9 * 17, 128),
nn.ReLU(),
nn.Linear(128, n)
)
def _get_conv_out(self, shape):
o = self.conv(torch.zeros(1, *shape))
return int(np.prod(o.size()))
def forward(self, x):
conv_out = self.conv(x).view(x.size()[0], -1)
return sefl.fc(conv_out)
if __name__=='__main__':
num_epochs = 1
num_classes = 2
input_shape = train_img[0].shape # 1, 15, 23
net = Net(num_classes)
iteration = 51
BATCH_SIZE = 24
LEARNING_RATE = 0.0001
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=LEARNING_RATE)
loss_list= []
batch_index = 0
# train
for epoch in range(num_epochs):
for i in range(iteration):
input_img = torch.FloatTensor(train_img[batch_index: batch_index + BATCH_SIZE])
print(input_img.size()) # 24, 1, 15, 23
outputs = net(input_img)
loss = criterion(outputs, labels)
loss_list.append(loss.item())
# Backprop
opimizer.zero_grad()
loss.backward()
optimizer.step()
And the error message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-179-0f6bc7588c29> in <module>
4 input_shape = train_img[0].shape # 1, 15, 23
5
----> 6 net = Net(num_classes)
7 iteration = 51
8 BATCH_SIZE = 24
<ipython-input-178-8a68d4a0dc4a> in __init__(self, n)
11 )
12
---> 13 conv_out_size = self._get_conv_out(input_shape)
14 self.fc = nn.Sequential(nn.Linear(64 * 9 * 17, 128),
15 nn.ReLU(),
<ipython-input-178-8a68d4a0dc4a> in _get_conv_out(self, shape)
18
19 def _get_conv_out(self, shape):
---> 20 o = self.conv(torch.zeros(1, *shape))
21 return int(np.prod(o.size()))
22
C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
355 result = self._slow_forward(*input, **kwargs)
356 else:
--> 357 result = self.forward(*input, **kwargs)
358 for hook in self._forward_hooks.values():
359 hook_result = hook(self, input, result)
C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
65 def forward(self, input):
66 for module in self._modules.values():
---> 67 input = module(input)
68 return input
69
C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
355 result = self._slow_forward(*input, **kwargs)
356 else:
--> 357 result = self.forward(*input, **kwargs)
358 for hook in self._forward_hooks.values():
359 hook_result = hook(self, input, result)
C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
280 def forward(self, input):
281 return F.conv2d(input, self.weight, self.bias, self.stride,
--> 282 self.padding, self.dilation, self.groups)
283
284
C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\functional.py in conv2d(input, weight, bias, stride, padding, dilation, groups)
88 _pair(0), groups, torch.backends.cudnn.benchmark,
89 torch.backends.cudnn.deterministic, torch.backends.cudnn.enabled)
---> 90 return f(input, weight, bias)
91
92
TypeError: argument 0 is not a Variable
Your code actually works for PyTorch >= 0.4.1. I guess your PyTorch version is < 0.4 and so you need to pass a Variable in the following line.
o = conv(torch.autograd.Variable(torch.zeros(1, *x.shape)))
In PyTorch >= 0.4.1, the concept of Variable no longer exists. So, torch.FloatTensor can be directly passed to NN layers.