导致类权重的因素在keras fit生成器中不起作用

时间:2019-05-24 03:58:58

标签: python keras

我是python和机器学习领域的新手。我尝试使用https://github.com/0bserver07/Keras-SegNet-Basic中的keras segnet,并使用我自己的数据集(即裂缝和背景)进行训练。我所做的更改包括更改新数据集的路径,将班级标签从原始12更改为2。问题是,对于第一个时期,准确性始终为0.9567,即使我更改班级权重也不会降低或增加。 我尝试更改优化器和学习率,但无济于事 以下是其中的代码之一:

from __future__ import absolute_import
from __future__ import print_function
import os

os.environ['KERAS_BACKEND'] = 'theano'
os.environ['THEANO_FLAGS']='mode=FAST_RUN,device=cuda,floatX=float32,optimizer=fast_compile,gpuarray.preallocate=0.8'
import keras.models as models
from keras.layers import merge
from keras.layers.core import Layer, Dense, Dropout, Activation, Flatten, Reshape, Permute
from keras.layers.convolutional import Convolution2D, MaxPooling2D, UpSampling2D, ZeroPadding2D
from keras.layers.normalization import BatchNormalization
from keras.callbacks import ModelCheckpoint
from keras import optimizers
from keras import backend as K

import cv2
import numpy as np
import json
np.random.seed(7) # 0bserver07 for reproducibility
data_shape = 360*480
# load the data
train_data = np.load('C:/Users/LENOVO-2/PycharmProjects/segnet(crack)/train_data.npy')
train_label = np.load('C:/Users/LENOVO-2/PycharmProjects/segnet(crack)/train_label.npy')

test_data = np.load('C:/Users/LENOVO-2/PycharmProjects/segnet(crack)/test_data.npy')
test_label = np.load('C:/Users/LENOVO-2/PycharmProjects/segnet(crack)/test_label.npy')
class_weighting= [ 0.51499733 , 17.1696336]
# load the model:
with open('segnet_basic2.json') as model_file:
    segnet_basic = models.model_from_json(model_file.read())

sgd = optimizers.SGD(lr=0.001)
segnet_basic.compile(loss="categorical_crossentropy", optimizer='adadelta', metrics=["accuracy"])

# checkpoint
filepath="test2.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

epochs = 3
batch_size = 3

# Fit the model
history = segnet_basic.fit(train_data, train_label, callbacks=callbacks_list, batch_size=batch_size, epochs=epochs,
                    verbose=1, class_weight=class_weighting , validation_data=(test_data, test_label), shuffle=True) # validation_split=0.33

# This save the trained model weights to this file with number of epochs
segnet_basic.save_weights('C:/Users/LENOVO-2/PycharmProjects/Test 3/model_{}.hdf5'.format(epochs))

我在Internet上搜索,但未找到可行的解决方案。原始代码工作得很好,我已经尝试了我所知的解决方案,但没有结果。所以有人可以提供给我一些可能的解决方案或导致此问题的因素。欢迎有任何见识。谢谢

0 个答案:

没有答案