I have 530 data points belonging to 10 classes. I am not sure which numbers should I use for the num_rows and num_columns.
In this code I have num_rows = 40, num_columns = 174:
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=2, input_shape=(num_rows, num_columns, num_channels), activation='relu'))
model.add(MaxPooling2D(pool_size=2))
#model.add(Dropout(0.2))
model.add(Conv2D(filters=64, kernel_size=2, kernel_regularizer=l2(0.00001), bias_regularizer=l2(0.0001), activation='relu'))
model.add(MaxPooling2D(pool_size=2))
#model.add(Dropout(0.2))
model.add(Conv2D(filters=128, kernel_size=2, kernel_regularizer=l2(0.00001), bias_regularizer=l2(0.0001), activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(Conv2D(filters=128, kernel_size=2, kernel_regularizer=l2(0.00001), bias_regularizer=l2(0.0001), activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
#model.add(GlobalAveragePooling2D())
model.add(Flatten())
model.add(Dense(512, activation='relu'))
#model.add(Dropout(0.2))
model.add(Dense(256, activation='relu'))
#model.add(Dropout(0.2))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(10, activation='softmax'))
# Compile the model
#opt = keras.optimizers.Adam(learning_rate=0.001)
model.compile(loss='categorical_crossentropy', metrics=\['accuracy'\], optimizer="Adam")
I am guessing you have some sort of spectrograms on your input (since you're working with audio, but have 3-dimensional shape on input). Your input_shape has to reflect the size of images that you pass on input. Simply check their width and height - these are your num_rows and num_columns.
According to that code, the images have 3 colour bands. That makes sense for photos, but rarely for spectrograms. Remember these are false colours that typically are generated to create visually-pleasing visualisations, but don't get you anything when doing classification. Single channel is enough, the pixel intensity reflects strength (amplitude) of the signal.
Three simple things you can do:
Use monochromatic images, e.g. input_shape=(num_rows, num_columns, 1). Colour only confuses the classifier.
Get more data and use augmentation.
kernel_size=2 makes little sense. Read on convolutions first and what are the kernels.
Related
I am working on a project where I have to classify genomic sequences as either positive or negative. So basically, I have sequences that are in the form 'accccttttttgggg...'
These sequences are 150 characters long consisting of a, c, t and g characters. I perform one hot encoding of the data and create a dataframe that contains 600 columns (150 x 4) for sequences plus one column for label (either 0 or 1). I then pass this data through CNN. The model performs well on the training and validation data but when I see the predictions on test data it always predicts a single label 0. Can anyone help me know why does this happen and what I am doing wrong here. This is the model that I have been using with epochs=50, learning_rate=0.00001 and batch_size=64
model = Sequential()
model.add(Conv1D(32, kernel_size=5, input_shape=(600, 1), activation='relu', padding='same'))
model.add(MaxPooling1D())
model.add(Conv1D(16, kernel_size=5, activation='relu', padding='same'))
model.add(MaxPooling1D())
model.add(Conv1D(8, kernel_size=5, activation='relu', padding='same'))
model.add(MaxPooling1D())
model.add(Flatten())
model.add(Dense(4, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
adam = Adam(learning_rate=learning_rate)
model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
It is my network structure. I used visualkeras but it was not practical and I tried to use NN-SVG but I couldn't.
model = Sequential()
model.add(Conv2D(8, (10,8), padding='same',
activation='relu',activity_regularizer=l2(0.0001),input_shape=
(n_timesteps, 16,1))
model.add(Conv2D(10,( 10,4), activation='relu',
padding='same'))#,activity_regularizer=l2(0.001
model.add(AveragePooling2D(pool_size=(2,2)))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(6, activation='softmax'))
I am working on the CNN model. In the given CNN model I can not handle how to divide the 4th layer into two streams and get output.
I also built a model in Keras.
def _build_model(self):
model = Sequential()
model.add(Conv2D(8, (3, 3), strides=4, padding='same', input_shape=self.state_size))
model.add(Activation('relu'))
model.add(Conv2D(2, (2, 2), strides=4, padding='same'))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(self.action_size, activation='relu'))
model.compile(loss='mse', optimizer=Adam())
return model
How to handle it? An example would be appreciated.
I am training a 1D CNN on a bunch of images with multi-label classification. I have an accuracy of almost 100% but loss of ~5 at the same time. What's going on?, any help is appreciated.
model = Sequential()
model.add(Conv1D(filters=128, kernel_size=3, activation='relu', input_shape=(137,236)))
model.add(Dropout(0.2))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.2))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.2))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.2))
model.add(GlobalMaxPooling1D())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(3, activation='softmax'))
model.compile(optimizer='adam', loss='mean_squared_logarithmic_error', metrics=['accuracy'])
Training:
I created a CNN model using the cifar100 dataset from Keras. When adding the top_k_categorical_accuracy metric, I should be seeing the accuracy of when one of the top 5 predicted classes is the correct class. However, when training, top_k_categorical_accuracy stays very small, around 4-5%, as accuracy and validation accuracy increase all the way to 40-50%. Top 5 accuracy should be much higher than normal accuracy, instead its giving very odd results. I wrote my own metric using different k values but still the same issue. Even when I use k=1, which should then give the same value of accuracy, the same issue occurs.
Model code:
cnn = Sequential()
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu', input_shape=(train_images.shape[1:])))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(MaxPooling2D(pool_size=2, padding='same'))
cnn.add(Dropout(0.4))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(Dropout(0.4))
cnn.add(MaxPooling2D(pool_size=2, padding='same'))
cnn.add(Dropout(0.5))
cnn.add(Flatten())
cnn.add(Dense(550, activation='relu'))
cnn.add(Dropout(0.4))
cnn.add(Dense(100, activation='softmax'))
Compile code:
cnn.compile(loss='sparse_categorical_crossentropy', optimizer=opt.Adam(lr=learn_rate), metrics=['accuracy', 'top_k_categorical_accuracy'])
Turns out, since I am using the sparse_categorical_crossentropy loss function I need to use the sparse_top_k_categorical_accuracy function. This metric also requires your labels be flattened to one dimension. After that, metric is correct and model is training!