Why does model=None, and how am I supposed to solve it? - python

I keep getting an error that says
runfile('C:/Users/admin/Documents/GitHub/Self-driving-Car/convo_nn.py', wdir='C:/Users/admin/Documents/GitHub/Self-driving-Car')
------------------------------
Parameters
------------------------------
data_dir := data
test_size := 0.2
keep_prob := 0.5
nb_epoch := 10
samples_per_epoch := 20000
batch_size := 40
save_best_only := True
learning_rate := 0.0001
------------------------------
Traceback (most recent call last):
File "C:\Users\admin\Documents\GitHub\Self-driving-Car\convo_nn.py", line 233, in <module>
main()
File "C:\Users\admin\Documents\GitHub\Self-driving-Car\convo_nn.py", line 226, in main
model = build_model(args)
File "C:\Users\admin\Documents\GitHub\Self-driving-Car\convo_nn.py", line 128, in build_model
Model.add(Convolution2D(24, (5, 5), padding='same', strides=(2, 2),input_shape=INPUT_SHAPE))
AttributeError: 'NoneType' object has no attribute 'add'
Code attached below
def build_model(args):
activation_relu = 'relu'
Model = Sequential()
#Model.add(Lambda(lambda x: x / 127.5 - 1.0, input_shape=INPUT_SHAPE))
Model.add(Convolution2D(24, (5, 5), padding='same', strides=(2, 2),input_shape=INPUT_SHAPE))
Model.add(Activation(activation_relu))
Model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1)))
Model.add(Convolution2D(36, (5, 5), padding='same', strides=(2, 2)))
Model.add(Activation(activation_relu))
Model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1)))
Model.add(Convolution2D(48, (5, 5), padding='same', strides=(2, 2)))
Model.add(Activation(activation_relu))
Model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1)))
Model.add(Convolution2D(64, (3, 3), padding='same', strides=(1, 1)))
Model.add(Activation(activation_relu))
Model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1)))
Model.add(Convolution2D(64, (3, 3), padding='same', strides=(1, 1)))
Model.add(Activation(activation_relu))
Model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1)))
Model.add(Flatten())
# Next, five fully connected layers
Model.add(Dense(1164))
Model.add(Activation("tanh"))
Model.add(Dense(100))
Model.add(Activation("tanh"))
Model.add(Dense(50))
Model.add(Activation("tanh"))
Model.add(Dense(10))
Model.add(Activation("tanh"))
Model.add(Dense(nb_classes,activation="softmax"))
Model.summary()
return Model
However, if I add a clause:
if Model is not None:
...
else:
pass
the code compiles successfully. Thus I could understand that model is always equalling to None. For the full code find the link attached below:
NeuroNetwork.py
To try this run Asphalt 9, run trainer.py to get training data. Then
run NeuroNetwork.py to build a convolutional neural network. Then run AI.py on Asphalt 9, for the AI to, hypothetically play the game. I've only coded it for TouchDrive right now.
I'm relatively new to Python, so do excuse me if it's a basic error
Use case: An Asphalt-9 AI based on previous codes...
I have made a training data extraction code and a hardware keyboard sending and receiving libraries and everything else works, except this... It's supposed to be a convolutional neural network...

Related

I would like to process an image through my neural network and visualize the image during processing [duplicate]

I'm trying to obtain output of an intermediate layer in Keras, Following is my code:
XX = model.input # Keras Sequential() model object
YY = model.layers[0].output
F = K.function([XX], [YY]) # K refers to keras.backend
Xaug = X_train[:9]
Xresult = F([Xaug.astype('float32')])
Running this, I got an Error :
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'dropout_1/keras_learning_phase' with dtype bool
i came to know that because I'm using dropout layer in my model, I have to specify a learning_phase() flag to my function as per keras documentation.
I changed my code to the following:
XX = model.input
YY = model.layers[0].output
F = K.function([XX, K.learning_phase()], [YY])
Xaug = X_train[:9]
Xresult = F([Xaug.astype('float32'), 0])
Now I'm getting a new Error that I'm unable to figure out:
TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a int into a Tensor.
Any help would be appreciated.
PS : I'm new to TensorFlow and Keras.
Edit 1 :
Following is the complete code that I'm using. I'm using Spatial Transformer Network as discussed in this NIPS paper and it's Kera's implementation here
input_shape = X_train.shape[1:]
# initial weights
b = np.zeros((2, 3), dtype='float32')
b[0, 0] = 1
b[1, 1] = 1
W = np.zeros((100, 6), dtype='float32')
weights = [W, b.flatten()]
locnet = Sequential()
locnet.add(Convolution2D(64, (3, 3), input_shape=input_shape, padding='same'))
locnet.add(Activation('relu'))
locnet.add(Convolution2D(64, (3, 3), padding='same'))
locnet.add(Activation('relu'))
locnet.add(MaxPooling2D(pool_size=(2, 2)))
locnet.add(Convolution2D(128, (3, 3), padding='same'))
locnet.add(Activation('relu'))
locnet.add(Convolution2D(128, (3, 3), padding='same'))
locnet.add(Activation('relu'))
locnet.add(MaxPooling2D(pool_size=(2, 2)))
locnet.add(Convolution2D(256, (3, 3), padding='same'))
locnet.add(Activation('relu'))
locnet.add(Convolution2D(256, (3, 3), padding='same'))
locnet.add(Activation('relu'))
locnet.add(MaxPooling2D(pool_size=(2, 2)))
locnet.add(Dropout(0.5))
locnet.add(Flatten())
locnet.add(Dense(100))
locnet.add(Activation('relu'))
locnet.add(Dense(6, weights=weights))
model = Sequential()
model.add(SpatialTransformer(localization_net=locnet,
output_size=(128, 128), input_shape=input_shape))
model.add(Convolution2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Convolution2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(128, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Convolution2D(128, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(256, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Convolution2D(256, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(256, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Convolution2D(256, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#==============================================================================
# Start Training
#==============================================================================
#define training results logger callback
csv_logger = keras.callbacks.CSVLogger(training_logs_path+'.csv')
model.fit(X_train, y_train,
batch_size=batch_size,
epochs=20,
validation_data=(X_valid, y_valid),
shuffle=True,
callbacks=[SaveModelCallback(), csv_logger])
#==============================================================================
# Visualize what Transformer layer has learned
#==============================================================================
XX = model.input
YY = model.layers[0].output
F = K.function([XX, K.learning_phase()], [YY])
Xaug = X_train[:9]
Xresult = F([Xaug.astype('float32'), 0])
# input
for i in range(9):
plt.subplot(3, 3, i+1)
plt.imshow(np.squeeze(Xaug[i]))
plt.axis('off')
for i in range(9):
plt.subplot(3, 3, i + 1)
plt.imshow(np.squeeze(Xresult[0][i]))
plt.axis('off')
The easiest way is to create a new model in Keras, without calling the backend. You'll need the functional model API for this:
from keras.models import Model
XX = model.input
YY = model.layers[0].output
new_model = Model(XX, YY)
Xaug = X_train[:9]
Xresult = new_model.predict(Xaug)
You could try:
model1 = tf.keras.models.Sequential(base_model.layers[:1])
model2 = tf.keras.models.Sequential(base_model.layers[1:])
Xaug = X_train[:9]
out = model1(Xaug)

Adding layers to CNN model (MobileNetV2)

Hello I have created a MobileNetV2 model i want to add layers onto it.
However, i kept getting this error:
"One of the dimensions in the output is <= 0 due to downsampling in conv2d_22. Consider increasing the input size. Received input shape [None, 3, 3, 1280] which would produce output shape with a zero or negative value in a dimension."
this is my code so far:
base_model = tf.keras.applications.MobileNetV2(input_shape=(96,96,3),
include_top=False,
weights='imagenet')
model = tf.keras.Sequential()
model.add(base_model)
model.add(layers.Conv2D(60, kernel_size=(5, 5), strides=(1, 1),
activation='relu',
input_shape=(96,96,3))) #error was highlighted on this line
model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(layers.Conv2D(64, kernel_size=(5, 5), strides=(1, 1), activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(500, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
i have tried changing the channel first and last ((96,96,3) and (3,96,96)) on the line that has problem and both produced the same error error.
Appreciate anyone who can help with this thanks.
You cannot downsample a tensor (None, 3, 3, 1280) with a kernel size of (5, 5). Think about what a 2D convolutional layer is actually doing. You could try first flattening the output of the base model and then reshaping the output with the dense and reshape layers:
import tensorflow as tf
base_model = tf.keras.applications.MobileNetV2(input_shape=(96,96,3),
include_top=False,
weights='imagenet')
model = tf.keras.Sequential()
model.add(base_model)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64 * 64 * 3)) # just an example --> choose what you want
model.add(tf.keras.layers.Reshape((64, 64, 3)))
model.add(tf.keras.layers.Conv2D(60, kernel_size=(5, 5), strides=(1, 1),
activation='relu',
input_shape=(96,96,3))) #error was highlighted on this line
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(tf.keras.layers.Conv2D(64, kernel_size=(5, 5), strides=(1, 1), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(500, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
You also use an Upsampling layer to get the dimensions you want, but that is up to you.

set_weights() in Tensorflow model

I have pretrained weights as np.array of shape (3, 3, 3, 64). I want to initialize this Tensorflow CNN with those weights using set_weights() like I show below.
However, when I try that, the following error pops up: ValueError: You called set_weights(weights) on layer "conv2d_3" with a weight list of length 3, but the layer was expecting 2 weights. Provided weights: [[[[-0.15836713 -0.178757 0.16782044 ...
model = models.Sequential()
model.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(224, 224, 3)))
model.layers[0].set_weights(weights)
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(256, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(256, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(512, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(512, (3, 3), activation='relu'))
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Dense(4, activation='softmax'))
print(model.summary())
adam = optimizers.Adam(learning_rate=0.0001, amsgrad=False)
model.compile(loss='categorical_crossentropy',
optimizer=adam,
metrics=['accuracy'])
history = model.fit_generator(
train_generator,
steps_per_epoch=np.ceil(nb_train_samples/batch_size),
epochs=epochs,
validation_data=validation_generator,
validation_steps=np.ceil(nb_validation_samples / batch_size),
class_weight=class_weight
)
My question is: how do I pass those (3, 3, 3, 64) shaped weights to initialize that CNN? I have already checked the weight shapes required for each layer and the shapes I am trying to pass and the required shape match.
You could just use kernel_initializer and bias_initializer arguments like this:
import numpy as np
# init_kernel and init_bias are initialization weights that you have
init_kernel = np.random.normal(0, 1, (3, 3, 3, 64))
init_bias = np.zeros((64,))
kernel_initializer = tf.keras.initializers.constant(init_kernel)
bias_initializer = tf.keras.initializers.constant(init_bias)
conv_layer = tf.keras.layers.Conv2D(64, (3, 3),
activation='relu',
input_shape=(224, 224, 3),
kernel_initializer=kernel_initializer,
bias_initializer=bias_initializer)
Note the kernel's and bias' shapes that I've chosen. The values with which you initialise your layer must have the exact same shapes.

Keras Reshape doesn't seem to run

I have a simple convolutional autoencoder in keras. My original inputs are flat arrays from a csv, so I want to reshape them from (196,) to (14,14,1). Following the keras docs, I did:
autoencoder = Sequential()
# first, reshape our (csv) inputs from (196,) to (14,14,1)
autoencoder.add(Reshape((14,14,1), input_shape=(196,)))
# encoding stage
autoencoder.add(Conv2D(16, (3,3), activation='relu', padding='same'))
autoencoder.add(MaxPooling2D((2, 2), padding='same'))
autoencoder.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
autoencoder.add(MaxPooling2D((2, 2), padding='same'))
autoencoder.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
autoencoder.add(MaxPooling2D((2, 2), padding='same'))
# decoding stage
autoencoder.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
autoencoder.add(UpSampling2D((2, 2)))
autoencoder.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
autoencoder.add(UpSampling2D((2, 2)))
autoencoder.add(Conv2D(16, (2, 2), activation='relu'))
autoencoder.add(UpSampling2D((2, 2)))
autoencoder.add(Conv2D(1, (3, 3), activation='sigmoid', padding='same'))
optimizer = optimizers.Adagrad(lr=0.01, epsilon=None, decay=0.001)
autoencoder.compile(optimizer=optimizer, loss='binary_crossentropy')
But I get the error: ValueError: Error when checking target: expected conv2d_35 to have 4 dimensions, but got array with shape (2870, 196)
So it seems to be ignoring the Reshape entirely. Am I making some obvious mistake?
The problem is not the input but the target so the y values you give are not reshaped. The final output of your network is a 4D tensor where you give instead (2870, 196).
Double check the your target array (y values) that you pass onto fit function.

Value error in convolutional neural network for shape input

I am facing a problem using a convolutional neural network using Keras with Tensorflow as backend with Anaconda Python.
While defining my CNN and compiling, an error occurs:
def cnn_model():
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=(3, 48, 48),
activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), padding='same',
activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(128, (3, 3), padding='same',
activation='relu'))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(NUM_CLASSES, activation='softmax'))
return model
The error that I get is:
File
"C:\Users\pandey\Anaconda3\lib\site-packages\keras\engine\training.py",
line 113, in _standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking input: expected conv2d_10_input to
have 4 dimensions, but got array with shape (0, 1)
I am using channel first in Keras and have defined the data format as channel first in starting only.
Any help is appreciated.

Categories

Resources