Super high accuracy with high loss? - python

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:

Related

Get ValueError when apply transfer learning in federated learning (TFF)

I want to use pre_trained model in federated learning as following code:
first I build my model and set the weights on model and then I freeze convolutional layers and remove 4 last layer.
def create_keras_model():
model = Sequential()
model.add(Conv2D(16, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu', input_shape=(226,232,1)))
model.add(MaxPooling2D((2,2), strides=(2,2), padding='same'))
model.add(Conv2D(32, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2), padding='same'))
model.add(Conv2D(64, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2), padding='same'))
model.add(Conv2D(64, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2), padding='same'))
model.add(Conv2D(128, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2), padding='same'))
model.add(Conv2D(128, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2), padding='same'))
model.add(Conv2D(256, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2), padding='same'))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))
return model
keras_model = create_keras_model()
server_state=FileCheckpointManager(root_dir= '/content/drive/MyDrive',
prefix= 'federated_clustering',
step= 1,
keep_total= 1,
keep_first= True).load_checkpoint(structure=server_state,round_num=10)
keras_model.set_weights(server_state)
for layer in keras_model.layers[:-4]:
layer.trainable = False
model_pre = Model(inputs=keras_model.input,outputs=keras_model.layers[14].output)
next, I build new model.
def create_keras_model1():
model = Sequential()
model.add(model_pre)
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(2, activation='softmax'))
return model
def model_fn():
# We _must_ create a new model here, and _not_ capture it from an external
# scope. TFF will call this within different graph contexts.
keras_model = create_keras_model1()
return tff.learning.from_keras_model(
keras_model,
input_spec=preprocessed_example_dataset.element_spec,
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
but I get ValueError when I want to use tff.learning.build_federated_averaging_process.
iterative_process = tff.learning.build_federated_averaging_process(
model_fn,
client_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=0.001),
server_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=1.0))
ValueError: Your Layer or Model is in an invalid state. This can happen for the following cases:
1. You might be interleaving estimator/non-estimator models or interleaving models/layers made in tf.compat.v1.Graph.as_default() with models/layers created outside of it. Converting a model to an estimator (via model_to_estimator) invalidates all models/layers made before the conversion (even if they were not the model converted to an estimator). Similarly, making a layer or a model inside a a tf.compat.v1.Graph invalidates all layers/models you previously made outside of the graph.
2. You might be using a custom keras layer implementation with custom __init__ which didn't call super().__init__. Please check the implementation of <class 'keras.engine.functional.Functional'> and its bases.
please help me to fix it.
You need to create a new keras model during the invocation of model_fn, as the code comment suggests. It seems you are using model_pre which you have already created before that, which is likely the core problem.

CNN model, How does dense layer in CNN divide into two streams using keras

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.

Keras ImageDataGenerator validation_data

Cannot really figure the error here.
Key requisites:
augmented_images and val_data_gen are keras.preprocessing.image.ImageDataGenerator functions.
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(3,3), strides=(1,1), activation='relu', input_shape=(32,32,3)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(filters=64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(filters=64, kernel_size=(3,3), strides=(1,1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
#model.add(Dropout(rate=0.5))
model.add(Flatten())
model.add(Dense(units=64, activation='relu'))
model.add(Dense(2, activation='sigmoid'))
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.05),
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
model.summary()
history = model.fit(x=augmented_images,
validation_data=val_data_gen,
epochs=10,
steps_per_epoch=2000,
validation_steps=1000)
ValueError: Layer sequential_10 expects 1 inputs, but it received 5 input tensors.

Increase val_acc in the audio classification

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.

Why does Neural Network predict all input negative?

I'm working on a sentiment analysis project in python with word2vec as an embedding method. (in my NON_ENGLISH corpus I considered every negative tweet with the 0 label, positive = 1 and neutral = 2)Since I'm really new in this field I don't have any idea as to why my model predicts everything negative. I surfed the net before and read something about the number of hidden layer and batches , etc. which related to this error but I'm not sure that I understood correctly.
here is my keras model:
model = Sequential()
model.add(Conv1D(32, kernel_size=3, activation='elu',
padding='same', input_shape=
(max_tweet_length,vector_size)))
model.add(Conv1D(32, kernel_size=3, activation='elu',
padding='same'))
model.add(Conv1D(32, kernel_size=3, activation='elu',
padding='same'))
model.add(Conv1D(32, kernel_size=3, activation='elu',
padding='same'))
model.add(Dropout(0.25))
model.add(Conv1D(32, kernel_size=2, activation='elu',
padding='same'))
model.add(Conv1D(32, kernel_size=2, activation='elu',
padding='same'))
model.add(Conv1D(32, kernel_size=2, activation='elu',
padding='same'))
model.add(Conv1D(32, kernel_size=2, activation='elu',
padding='same'))
model.add(Dropout(0.25))
model.add(Dense(256, activation='tanh'))
model.add(Dense(256, activation='tanh'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(2, activation='sigmoid'))
thanks a lot!

Categories