简体   繁体   English

CNN 精度:0.0000e+00 用于图像的多分类

[英]CNN accuracy: 0.0000e+00 for multi-classification on images

I have the following code that produces my horrible accuracy dilema, has anyone else encountered this issue for multi classification task(49 different images to classify)?我有以下代码产生了我可怕的准确性困境,还有其他人在多分类任务(49 个不同的图像要分类)中遇到过这个问题吗?

I am running resnet50 on top of my CNN model with softmax as last activation FN , my loss is categorical_crossentropy and my optimizer is Adam .我在我的 CNN 模型上运行 resnet50, softmax 作为最后一次激活 FN ,我的损失是 categorical_crossentropy ,我的优化器是 Adam

What might I be doing wrong?我可能做错了什么?

## Build CNN architecture
model1 = Sequential()
model1.add(Conv2D(32, (3,3), strides=1, input_shape = (720, 720, 3)))
model1.add(Activation('relu'))
model1.add(Conv2D(32, (3,3), strides=1, padding="same"))
model1.add(Activation('relu'))
model1.add(MaxPooling2D(pool_size=(2,2)))

model1.add(Conv2D(64, (3,3), strides=1, padding="same"))
model1.add(Activation('relu'))
model1.add(Conv2D(64, (3,3), strides=1, padding="same"))
model1.add(Activation('relu'))
model1.add(MaxPooling2D(pool_size=(2,2)))

model1.add(Flatten())
model1.add(Dense(200))
model1.add(Activation('relu'))
model1.add(Dense(200))
model1.add(Dropout(0.24))
model1.add(Activation('relu'))
model1.add(Dense(49, activation='softmax')) 

model1.summary()

# Image data generator for on the fly image augmentation
directory = '/home/carlini-TF2/data/train/'
batch_size = 64
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
        rotation_range=90.,
        shear_range=0.2,
        zoom_range=[0.8,1.2],
        horizontal_flip=True,
        validation_split=0.2,
        preprocessing_function=tf.keras.applications.resnet50.preprocess_input)
train_generator = train_datagen.flow_from_directory(directory=directory,
                                                    subset='training',
                                                    target_size=(720, 720),
                                                    shuffle=True,
                                                    seed=42,
                                                    color_mode='rgb', 
                                                    class_mode='categorical', 
                                                    batch_size=batch_size)
valid_directory = '/home/carlini-TF2/data/test/'
valid_generator = train_datagen.flow_from_directory(directory=valid_directory,
                                                    target_size=(720, 720),
                                                    color_mode="rgb",
                                                    batch_size=batch_size,
                                                    class_mode="categorical",
                                                    subset='validation',
                                                    shuffle=True,
                                                    seed=42)

## Compile and train Neural Network 
METRICS = [
        tf.keras.metrics.Accuracy(name='accuracy'),
        tf.keras.metrics.Precision(name='precision'),
        tf.keras.metrics.Recall(name='recall')]

# optimal optimizer FN | loss FN to work with accuracy metric
model1.compile(loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),
               optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
               metrics=METRICS)

# stop training when loss gets worse after consecutive epochs
callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3) 

# fit model with augmented training set and validation set | shuffle batch 
history = model1.fit(train_generator,
                    validation_data = valid_generator,
                    steps_per_epoch = train_generator.n//batch_size,
                    validation_steps = valid_generator.n//batch_size,
                    shuffle=True, callbacks = [callback],
                    epochs=50)

The issue is that ResNet50 was being used for data augmentation and not in the CNN architecture.问题是 ResNet50 被用于数据增强,而不是在 CNN 架构中。 In order to reach somewhat robust model the following code is needed.为了达到某种稳健的模型,需要以下代码。

We can throw out the previous architecture and use a very simple model and the ResNet50 since this gives conclusive results.我们可以抛弃之前的架构并使用一个非常简单的模型和 ResNet50,因为这会给出结论性的结果。

We must use Functional API since ResNet50 was built on it我们必须使用函数式 API,因为 ResNet50 是基于它构建的

data_bias = np.log(1802./4657) 
initializer = tf.keras.initializers.Constant(data_bias)

resnet50_imagenet_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False, input_shape=(720,720,3) )
resnet50_imagenet_model.trainable = False

#Flatten output layer of Resnet
flattened = tf.keras.layers.Flatten()(resnet50_imagenet_model.output)

#Fully connected layer, output layer with 49 diff labels
fc2 = tf.keras.layers.Dense(49, activation='softmax', bias_initializer=initializer, name="AddedDense2")(flattened)

model1 = tf.keras.models.Model(inputs=resnet50_imagenet_model.input, outputs=fc2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 keras val_loss:0.0000e+00 - val_accuracy:0.0000e+00 - keras val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00 如何修复分类分类中的“val_accuracy: 0.0000e+00”? - How to fix “val_accuracy: 0.0000e+00” in categorical classification? 获得精度:0.0000e+00 在我的张量流 model - Getting accuracy: 0.0000e+00 in my Tensor flow model keras,val_accuracy,val_loss是loss: 0.0000e+00 val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00的问题 - keras , val_accuracy, val_loss is loss: 0.0000e+00 val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00 problem 为什么我的准确率总是0.0000e+00,而且损失巨大? - Why is my accuracy always 0.0000e+00, and loss and huge? Keras 损失:0.0000e+00 且精度保持不变 - Keras loss: 0.0000e+00 and accuracy stays constant Conv2d Tensorflow 结果错误 - 准确度 = 0.0000e+00 - Conv2d Tensorflow results wrong - accuracy = 0.0000e+00 如何解决 LSTM 问题中的 loss: nan &accuracy: 0.0000e+00? 张量流 2.x - How to solve loss: nan & accuracy: 0.0000e+00 in a LSTM problem? Tensorflow 2.x 获得固定精度:0.5000,有时 0.0000e+00 in Keras model 使用 Google Colab - Getting a fixed accuracy: 0.5000 and sometimes 0.0000e+00 in Keras model using Google Colab “损失:0.0000e+00 - acc: 1.0000 - val_loss: 0.0000e+00 - val_acc: 1.0000”是什么意思? - what does "loss: 0.0000e+00 - acc: 1.0000 - val_loss: 0.0000e+00 - val_acc: 1.0000" mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM