【发布时间】:2017-07-16 03:58:12
【问题描述】:
我正在尝试针对街景门牌号码数据集训练 keras CNN。您可以找到项目here。 问题在于,在训练期间,损失和准确率都不会随时间变化。我尝试使用 1 通道(灰度)图像,RGB(3 通道)图像,更宽(50,50)和更小(28,28)图像,卷积层中或多或少的过滤器,更宽和更小池化层中的补丁,有和没有 dropout,批次越来越小,优化器的学习步长越来越小,使用不同的优化器,...
训练仍然停留在不断的损失和准确性
这是我准备数据的方式
from PIL import Image
from PIL import ImageFilter
train_folders = 'sv_train/train'
test_folders = 'test'
extra_folders = 'extra'
SV_IMG_SIZE = 28
SV_CHANNELS = 3
train_imsize = np.ndarray([len(train_data),2])
k = 500
sv_images = []
max_images = 20000#len(train_data)
max_digits = 5
sv_labels = np.ones([max_images, max_digits], dtype=int) * 10 # init to 10 cause it would be no digit
nboxes = [[] for i in range(max_images)]
print ("%d to load" % len(train_data))
def getBBox(i,perc):
boxes = train_data[i]['boxes']
x_min=9990
y_min=9990
x_max=0
y_max=0
for bid,b in enumerate(boxes):
x_min = b['left'] if b['left'] <= x_min else x_min
y_min = b['top'] if b['top'] <= y_min else y_min
x_max = b['left']+b['width'] if b['left']+b['width'] >= x_max else x_max
y_max = b['top']+b['height'] if b['top']+b['height'] >= y_max else y_max
dy = y_max-y_min
dx = x_max-x_min
dpy = dy*perc
dpx = dx*perc
nboxes[i]=[dpx,dpy,dx,dy]
return x_min-dpx, y_min-dpy, x_max+dpx, y_max+dpy
for i in range(max_images):
print (" \r%d" % i ,end="")
filename = train_data[i]['filename']
fullname = os.path.join(train_folders, filename)
boxes = train_data[i]['boxes']
label = [10,10,10,10,10]
lb = len(boxes)
if lb <= max_digits:
im = Image.open(fullname)
x_min, y_min, x_max, y_max = getBBox(i,0.3)
im = im.crop([x_min,y_min,x_max,y_max])
owidth, oheight = im.size
wr = SV_IMG_SIZE/float(owidth)
hr = SV_IMG_SIZE/float(oheight)
for bid,box in enumerate(boxes):
sv_labels[i][max_digits-lb+bid] = int(box['label'])
box = nboxes[i]
box[0]*=wr
box[1]*=wr
box[2]*=hr
box[3]*=hr
im = im.resize((SV_IMG_SIZE,SV_IMG_SIZE),Image.ANTIALIAS)
array = np.asarray(im)
array = array.reshape((SV_IMG_SIZE,SV_IMG_SIZE,SV_CHANNELS)).astype(np.float32)
na = np.zeros([SV_IMG_SIZE,SV_IMG_SIZE,SV_CHANNELS],dtype=int)
sv_images.append(array.astype(np.float32))
这是模型
from keras.optimizers import Adam
from keras.utils.np_utils import to_categorical
adam = Adam(lr=0.5)
model = Sequential()
x = Input((SV_IMG_SIZE, SV_IMG_SIZE,SV_CHANNELS))
y = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x)
y = Convolution2D(32, 3, 3, activation='relu', border_mode='valid')(y)
y = MaxPooling2D((2, 2))(y)
y = Convolution2D(128, 3, 3, activation='relu', border_mode='valid')(y)
y = MaxPooling2D((2, 2))(y)
y = Flatten()(y)
y = Dense(512, activation='relu')(y)
digit1 = Dense(11, activation="softmax")(y)
digit2 = Dense(11, activation="softmax")(y)
digit3 = Dense(11, activation="softmax")(y)
digit4 = Dense(11, activation="softmax")(y)
digit5 = Dense(11, activation="softmax")(y)
model = Model(input=x, output=[digit1, digit2, digit3,digit4,digit5])
model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
sv_train_labels = [to_categorical(svt_labels[:,0]),
to_categorical(svt_labels[:,1]),
to_categorical(svt_labels[:,2]),
to_categorical(svt_labels[:,3]),
to_categorical(svt_labels[:,4])]
sv_validation_labels = [to_categorical(svv_labels[:,0]),
to_categorical(svv_labels[:,1]),
to_categorical(svv_labels[:,2]),
to_categorical(svv_labels[:,3]),
to_categorical(svv_labels[:,4])]
model.fit(sv_train, sv_train_labels, nb_epoch=50, batch_size=8,validation_data=(sv_validation, sv_validation_labels))
【问题讨论】:
-
显示一些代码?真的,您希望我们如何在没有代码的情况下帮助您解决此类问题
-
非常感谢您的评论。我所做的不仅仅是分享一些代码,我还分享了整个项目。您只需点击问题中的链接,即可找到完整的 Jupyter Notebook。
-
你应该明白,不会有很多人愿意点击你的链接并探索整个项目来调试它:) 请参阅“帮助他人重现问题”部分:stackoverflow.com/help/how-to-ask跨度>
-
你说的很对!我已经用我认为相关的代码部分更新了这个问题。我希望这能帮助您找到问题的原因
-
很少有建议(1)只使用 1 个 softmax 层(而不是 5 个),最后有 10 个输出(匹配类的数量)
predict = Dense(10, activation="softmax")(y),(2)只使用一个 @987654326 @层中的model和(3)只需调用to_categorical一次sv_train_labels = to_categorical(sv_train_labels)。
标签: python machine-learning deep-learning keras