[英]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 的原因有很多,我最常见的是:
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.