简体   繁体   English

训练张量流神经网络的麻烦,我该如何解决这个问题?

[英]Troubles Training Tensor flow Neural Network, how can I fix this problem?

I am currently training an image classification model with three categories of vehicles (Vans/SUVs, Cars and Trucks).我目前正在使用三类车辆(货车/SUV、轿车和卡车)训练图像分类 model。 I have 1800 training images and 210 validation images.我有 1800 张训练图像和 210 张验证图像。 When I try to plug in the data.当我尝试插入数据时。 I pre-process data with keras.preprocessing.image.ImageDataGenerator() and Val_Data.flow( . It seems like absolutely is happening because of my accuracy staying constant. Below are my code and my results. I have tried to fix this for so long and cannot seem to fix this problem.我用keras.preprocessing.image.ImageDataGenerator()Val_Data.flow(预处理数据。由于我的准确性保持不变,这似乎绝对会发生。下面是我的代码和我的结果。我试图解决这个问题长,似乎无法解决这个问题。

The Code:编码:

    # Creating Training Data Shuffled and Organized
Train_Data = keras.preprocessing.image.ImageDataGenerator()

Train_Gen = Train_Data.flow(
        Train_Img, 
        Train_Labels,
        batch_size=BATCH_SIZE,
        shuffle=True)

# Creating Validation Data Shuffled and Organized
Val_Data = keras.preprocessing.image.ImageDataGenerator()

Val_Gen = Val_Data.flow(
        Train_Img, 
        Train_Labels,
        batch_size=BATCH_SIZE,
        shuffle=True)


print(Train_Gen)


###################################################################################
###################################################################################


#Outline the Model
hidden_layer_size = 300
output_size = 3

#Model Core
model = tf.keras.Sequential([

                             tf.keras.layers.Flatten(input_shape=(IMG_HEIGHT,IMG_WIDTH,CHANNELS)),
                             tf.keras.layers.Dense(hidden_layer_size, activation = 'relu'),
                             tf.keras.layers.Dense(hidden_layer_size, activation = 'relu'),
                             tf.keras.layers.Dense(hidden_layer_size, activation = 'relu'),
                             tf.keras.layers.Dense(hidden_layer_size, activation = 'relu'),
                             tf.keras.layers.Dense(hidden_layer_size, activation = 'relu'),
                             tf.keras.layers.Dense(output_size, activation = 'softmax')

                            ])

custom_optimizer = tf.keras.optimizers.SGD(learning_rate=0.001)

#Compile Model
model.compile(optimizer='adam', loss ='sparse_categorical_crossentropy', metrics = ['accuracy'])

#Train Model
NUM_EPOCHS = 15;
model.fit(Train_Gen, validation_steps = 10, epochs = NUM_EPOCHS, validation_data = Val_Gen, verbose = 2)

The Results:结果:

    180/180 - 27s - loss: 10.7153 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 2/15
180/180 - 23s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 3/15
180/180 - 23s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 4/15
180/180 - 22s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 5/15
180/180 - 22s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 6/15
180/180 - 21s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 7/15
180/180 - 22s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 8/15
180/180 - 22s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 9/15
180/180 - 23s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 10/15
180/180 - 22s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 11/15
180/180 - 22s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 12/15
180/180 - 22s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 13/15
180/180 - 22s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 14/15
180/180 - 22s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300
Epoch 15/15
180/180 - 22s - loss: 10.7454 - accuracy: 0.3333 - val_loss: 10.7991 - val_accuracy: 0.3300

I guess the first thing you'll have to look into are convolutional neural networks since i can see that you are trying to use a Dense network to solve an image based problem.我想您首先要研究的是卷积神经网络,因为我可以看到您正在尝试使用密集网络来解决基于图像的问题。 It will work but not as good as the CNN.它会起作用,但不如 CNN 好。

There are many reasons that the models stuck in tensorflow, the most common that i have fallen into are:模型卡在 tensorflow 的原因有很多,我最常见的是:

  1. there was a time that i had a model stuck because my input were not numpy arrays有一段时间我有一个 model 卡住了,因为我的输入不是 numpy arrays
  2. the optimizer learning rate is too high..优化器学习率太高..

Start by making a custom optimizer learning rate that is lower than the default, that means:首先制作一个低于默认值的自定义优化器学习率,这意味着:

custom_optimizer = tf.keras.optimizers.Adam(lr=0.0001)
model.compile(optimizer=custom_optimizer, loss="sparse_categorical_crossentropy", metrics = ['acc'])

check this link which is a CNN implementation that is close to yours https://gist.github.com/RyanAkilos/3808c17f79e77c4117de35aa68447045检查此链接,这是与您的 https://gist.github.com/RyanAkilos/3808c17f79e77c4117de35aa68447045接近的 CNN 实现的链接

暂无
暂无

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

相关问题 如何在Keras实施的卷积神经网络的训练过程中修复我的骰子损失计算? - How can I fix my dice loss calculation during the training process of a convolutional neural network implemented in Keras? Python / Pybrain:如何在训练期间修复神经网络的权重? - Python/Pybrain: How can I fix weights of a neural network during training? 如何实时可视化神经网络的训练? - How can I visualize the the training of neural network in real time? 如何计算或监控pybrain神经网络的训练? - How can I calculate or monitor the training of a neural network in pybrain? 训练我的神经网络后,如何从最后一个解码器层“登录”中提取图像? - how can i extract images from the last decoder layer “logits” after training my neural network? 在 sklearn 中训练神经网络时如何保存学习进度? - How can I save the learning progress when training the neural network in sklearn? 在 tfp 中训练变分贝叶斯神经网络时,如何分别可视化损失中不同项的演变? - When training a variational Bayesian neural network in tfp, how can I visualize the evolution of the different terms in the loss separately? 如何将.csv训练数据提供给mxnet中的卷积神经网络? - How can I feed .csv training data to a convolutional neural network in mxnet? 如何在训练后向神经网络 model 添加更多神经元/过滤器? - How can I add more neurons / filters to a neural network model after training? 在训练卷积神经网络(DenseNet)时,是否有任何选项或参数可以改变以减少训练时间? - Are there any options or parameters that I can change to reduce the training time when training the Convolutional neural network (DenseNet)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM