An error occurred while cnn modeling.
When using dropout, the following error message occurs.
this is error message
UnboundLocalError: local variable 'a' referenced before assignment
model
def getModel(input_shape,filter_size=32,pool_size=(2,2),dropout=0.2):
model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape=input_shape, activation='elu', kernel_initializer="he_normal", padding='same', kernel_regularizer=regularizers.l2(0.01)))
I want to use dropout after maxpooling
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(dropout))
model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal", padding='same',strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(16, (2, 2), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
this is flatten area
model.add(Flatten())
model.add(Dense(126, kernel_initializer="glorot_normal" ,kernel_regularizer=regularizers.l2(0.01)))
model.add(Activation('tanh'))
model.add(Dense(classes))
model.add(Activation('sigmoid'))
complile
model.compile(loss='categorical_crossentropy',
optimizer='adadelta', #SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
metrics=['accuracy'])
return model
model fit
np.random.seed(42)
hist = model.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs, verbose = 1, validation_split = .2)
I couldn't figure out what is 'a' here and hence the error,but I think following code should help:
model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal",padding='same',strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
Related
Basically, I am taking two images as inputs and preprocessing them and passing them as input to the Siamese CNN model.
def create_base_network_signet(input_shape):
seq = Sequential()
seq.add(Conv2D(96, kernel_size=(11, 11), activation='relu', name='conv1_1', strides=4, input_shape= input_shape,
kernel_initializer='glorot_uniform'))
seq.add(BatchNormalization(epsilon=1e-06, axis=1, momentum=0.9))
seq.add(MaxPooling2D((3,3), strides=(2, 2)))
seq.add(ZeroPadding2D((2, 2)))
seq.add(Conv2D(256, kernel_size=(5, 5), activation='relu', name='conv2_1', strides=1, kernel_initializer='glorot_uniform'))
seq.add(BatchNormalization(epsilon=1e-06, axis=1, momentum=0.9))
seq.add(MaxPooling2D((3,3), strides=(2, 2)))
seq.add(Dropout(0.3))
seq.add(ZeroPadding2D((1, 1)))
seq.add(Conv2D(384, kernel_size=(3, 3), activation='relu', name='conv3_1', strides=1, kernel_initializer='glorot_uniform'))
seq.add(ZeroPadding2D((1, 1)))
seq.add(Conv2D(256, kernel_size=(3, 3), activation='relu', name='conv3_2', strides=1, kernel_initializer='glorot_uniform'))
seq.add(MaxPooling2D((3,3), strides=(2, 2)))
seq.add(Dropout(0.3))# added extra
seq.add(Flatten(name='flatten'))
seq.add(Dense(1024, kernel_regularizer=l2(0.0005), activation='relu', kernel_initializer='glorot_uniform'))
seq.add(Dropout(0.5))
seq.add(Dense(128, kernel_regularizer=l2(0.0005), activation='relu', kernel_initializer='glorot_uniform'))
seq.add(Dense(1, activation='sigmoid'))
return seq
My aim to pass images is something similar to this below
result = model.fit([image1, image2], y = 1, epochs=10)
However, I am getting an error
Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'numpy.ndarray'>"}), <class 'int'>
I want set up a Keras Sequential Deep UNET, but I dont know how to concatenate
specific layers.
from keras import models
from keras import layers
from keras.layers.convolutional import Conv2D, Conv2DTranspose
model = models.Sequential()
model.add(layers.Conv2D(8,(3,3), activation="relu", padding='same', input_shape=(512, 512, 4)))
model.add(layers.Conv2D(8,(3,3), activation="relu", padding='same'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(16,(3,3), activation="relu", padding='same'))
model.add(layers.Conv2D(16,(3,3), activation="relu", padding='same'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(32,(3,3), activation="relu", padding='same'))
model.add(layers.Conv2D(32,(3,3), activation="relu", padding='same'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Dropout(0.2))
model.add(Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same'))
model.add(layers.Conv2D(16,(3,3), activation="relu", padding='same'))
model.add(layers.Conv2D(16,(3,3), activation="relu", padding='same'))
model.add(Conv2DTranspose(8, (2, 2), strides=(2, 2), padding='same'))
model.add(layers.Conv2D(8,(3,3), activation="relu", padding='same'))
model.add(layers.Conv2D(classes,(3,3), activation="relu", padding='same'))
model = model.compile(optimizer=optimizer,loss=loss, metrics=['accuracy'])
model.summary()
In a non Sequential Model it would be something like that
u6 = Conv2DTranspose(n_filters * 8, (3, 3), strides = (2, 2), padding = 'same')(c5)
u6 = concatenate([u6, c4])
u6 = Dropout(dropout)(u6)
c6 = conv2d_block(u6, n_filters * 8, kernel_size = 3, batchnorm = batchnorm)
this is my code with error
def createModel():
model = Sequential()
# first set of CONV => RELU => MAX POOL layers
model.add(Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=inputShape))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
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.25))
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.25))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(output_dim=NUM_CLASSES, activation='softmax'))
# returns our fully constructed deep learning + Keras image classifier
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
# use binary_crossentropy if there are two classes
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
return model
ValueError: Error when checking input: expected conv2d_19_input to have 4 dimensions, but got array with shape (274, 1)
To recognize the image I am using cnn+svm hybrid model.
model:
model.add(Conv2D(20, (5, 5), padding="same",input_shape=inputShape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(50, (5, 5), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(500))
model.add(Activation('relu'))
model.add(Dense((classes), kernel_regularizer=regularizers.l2(0.01)))
model.add(Activation('linear'))
compiling model:
opt = Adam(lr=lr, decay=lr / epochs)
model.compile(loss="categorical_hinge", optimizer=opt, metrics=["accuracy"])
Is this correct? When the training process, the accuracy not improving ....
what is the wrong with this code...
How do I create a VGG-16 sequence for my data?
The data has the following :
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height))) model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1')) model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1')) model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1')) model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3')) model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1')) model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3')) model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1')) model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3')) model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=32)
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=32)
model.fit_generator(
train_generator,
samples_per_epoch=2000,
nb_epoch=1,
verbose=1,
validation_data=validation_generator,
nb_val_samples=800)
json_string = model.to_json()
open('my_model_architecture.json','w').write(json_string)
model.save_weights('Second_try.h5')
I got an error:
Exception: Error when checking model target: expected dense_3 to have
shape (None, 32) but got array with shape (32, 2)
How do I change Dense to make it work?
I have 10 species,
I have solved the problem by
changing:
model.add(Dense(1000, activation='softmax'))
to:
model.add(Dense(10, activation='softmax'))
then it works.
Here instead of 1000 you should have the total number of classes because it's the output layer.
model.add(Dense(1000, activation='softmax'))
Also shape of labels (or Y_train/Y_test) should be (total number of classes, total number records).
This helped me resolve similar kind of error.