我制作了一个模型,可以用图像数据集(约10500张图像)对82个数字进行分类
数据集在两个文件夹中:
火车文件夹的第一个文件夹在82个文件夹中具有8000张图像
第二个文件夹,测试文件夹在82个文件夹中具有2000张图像
在转到主数据集文件夹之前,我已经在其他2个文件夹上测试了该模型,并且工作正常
但是在这里我不知道为什么acc不会变得更好
请注意,并非我的数据集中的所有文件夹都具有相同数量的图像,图像的分辨率也不相同,而是均为210x50左右
也请注意,在我第一次尝试使用模型在两个文件夹上进行测试时,我制作了两个类别的小型数据集,且文件夹中的图像数量相同(与验证文件夹相同) )
下面是我用来创建模型的代码:
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
# dimensions of our images.
img_width, img_height = 251, 54
#img_width, img_height = 150, 33
train_data_dir = 'C:/Users/ADEM/Desktop/msi_youssef/PFE/test/numbers/data/train'
validation_data_dir = 'C:/Users/ADEM/Desktop/msi_youssef/PFE/test/numbers/data/valid'
nb_train_samples = 10435
nb_validation_samples = 2051
epochs = 20 # how much time you want to train your model on the data
batch_size = 16
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.1,
zoom_range=0.05,
horizontal_flip=False)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
model.save('first_try.h5')
,这里是结果:
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:133: The name tf.placeholder_with_default is deprecated. Please use tf.compat.v1.placeholder_with_default instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:3376: The name tf.log is deprecated. Please use tf.math.log instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\tensorflow_core\python\ops\nn_impl.py:183: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Found 10435 images belonging to 82 classes.
Found 2051 images belonging to 82 classes.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:986: The name tf.assign_add is deprecated. Please use tf.compat.v1.assign_add instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:973: The name tf.assign is deprecated. Please use tf.compat.v1.assign instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:2741: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.
Epoch 1/20
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:174: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:181: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:190: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:199: The name tf.is_variable_initialized is deprecated. Please use tf.compat.v1.is_variable_initialized instead.
WARNING:tensorflow:From C:\Users\ADEM\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:206: The name tf.variables_initializer is deprecated. Please use tf.compat.v1.variables_initializer instead.
652/652 [==============================] - 43s 65ms/step - loss: -625.7214 - acc: 0.0143 - val_loss: -632.8458 - val_acc: 0.0112
Epoch 2/20
652/652 [==============================] - 47s 72ms/step - loss: -627.1426 - acc: 0.0143 - val_loss: -632.6816 - val_acc: 0.0113
Epoch 3/20
652/652 [==============================] - 42s 65ms/step - loss: -627.8743 - acc: 0.0143 - val_loss: -633.1438 - val_acc: 0.0113
Epoch 4/20
652/652 [==============================] - 45s 69ms/step - loss: -627.0466 - acc: 0.0142 - val_loss: -632.6816 - val_acc: 0.0108
Epoch 5/20
652/652 [==============================] - 47s 73ms/step - loss: -628.4401 - acc: 0.0143 - val_loss: -632.7599 - val_acc: 0.0118
Epoch 6/20
652/652 [==============================] - 45s 69ms/step - loss: -626.8264 - acc: 0.0143 - val_loss: -631.9844 - val_acc: 0.0108
Epoch 7/20
652/652 [==============================] - 55s 85ms/step - loss: -627.8007 - acc: 0.0141 - val_loss: -636.2931 - val_acc: 0.0103
Epoch 8/20
652/652 [==============================] - 46s 71ms/step - loss: -626.7282 - acc: 0.0144 - val_loss: -633.0968 - val_acc: 0.0123
Epoch 9/20
652/652 [==============================] - 47s 72ms/step - loss: -628.2569 - acc: 0.0143 - val_loss: -633.8959 - val_acc: 0.0113
Epoch 10/20
652/652 [==============================] - 46s 71ms/step - loss: -627.1006 - acc: 0.0144 - val_loss: -629.7360 - val_acc: 0.0113
Epoch 11/20
652/652 [==============================] - 54s 83ms/step - loss: -627.1028 - acc: 0.0142 - val_loss: -636.8650 - val_acc: 0.0098
Epoch 12/20
652/652 [==============================] - 45s 70ms/step - loss: -627.8524 - acc: 0.0143 - val_loss: -627.5894 - val_acc: 0.0118
Epoch 13/20
652/652 [==============================] - 46s 70ms/step - loss: -627.1357 - acc: 0.0142 - val_loss: -631.9687 - val_acc: 0.0118
Epoch 14/20
652/652 [==============================] - 48s 73ms/step - loss: -627.5105 - acc: 0.0146 - val_loss: -638.9724 - val_acc: 0.0118
Epoch 15/20
652/652 [==============================] - 46s 70ms/step - loss: -629.0591 - acc: 0.0136 - val_loss: -630.7622 - val_acc: 0.0103
Epoch 16/20
652/652 [==============================] - 46s 71ms/step - loss: -625.9115 - acc: 0.0147 - val_loss: -630.3392 - val_acc: 0.0098
Epoch 17/20
652/652 [==============================] - 45s 70ms/step - loss: -627.0184 - acc: 0.0144 - val_loss: -636.2304 - val_acc: 0.0123
Epoch 18/20
652/652 [==============================] - 47s 72ms/step - loss: -626.8828 - acc: 0.0144 - val_loss: -634.5618 - val_acc: 0.0118
Epoch 19/20
652/652 [==============================] - 45s 70ms/step - loss: -627.3642 - acc: 0.0140 - val_loss: -629.8300 - val_acc: 0.0118
Epoch 20/20
652/652 [==============================] - 46s 71ms/step - loss: -627.4297 - acc: 0.0142 - val_loss: -637.6797 - val_acc: 0.0108
答案 0 :(得分:1)
由于您的模型现在正在处理多类问题,因此需要进行一些更改:
categorical_crossentropy
,而不是binary_crossentropy
Dense(82)
而不是Dense(1)
中应该有82个神经元。祝你好运!
答案 1 :(得分:0)
由于@danielcahall,我已经纠正了模型,现在它可以工作了,我唯一更改的是:
损失应为sparse_categorical_crossentropy
而不是
binary_crossentropy
最终激活功能应为softmax
,而不是sigmoid
Dense(82)
中应该有82个神经元,而不是
Dense(1)
,如果有82个课程。
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
# dimensions of our images.
img_width, img_height = 251, 54
#img_width, img_height = 150, 33
train_data_dir = 'C:/Users/ADEM/Desktop/msi_youssef/PFE/test/numbers/data/train'
validation_data_dir = 'C:/Users/ADEM/Desktop/msi_youssef/PFE/test/numbers/data/valid'
nb_train_samples = 8800 #10435
nb_validation_samples = 1763 #2051
epochs = 20 # how much time you want to train your model on the data
batch_size = 32 #16
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(80)) #1
model.add(Activation('softmax')) #sigmoid
model.compile(loss='sparse_categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])#categorical_crossentropy #binary_crossentropy
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.1,
zoom_range=0.05,
horizontal_flip=False)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
model.save('testX_1.h5') #first_try
请注意::如果您需要更多信息,我已经达到了acc: 0.6675
,则需要增加纪元!
答案 2 :(得分:0)
快速更新使用 30 测试了纪元,现在acc为:
acc: 0.7562 - val_loss: 0.1268 - val_acc: 0.9688