精确度和损失在50个时期之间没有变化

时间:2019-04-01 13:40:34

标签: python-3.x tensorflow deep-learning computer-vision data-science

我正在尝试使用python和keras开发ALPR系统。我生成了数据集(600 000个样本),其中包含格式为C-I-I-I-C-C(C-char,I-Int)的板块

我的模型在50个时期内并没有改善或降低,只是起伏不定。我不知道举重是怎么回事。

请告诉我发生了什么(过拟合或欠拟合)? 而且,如果这不是问题,请提供有关如何解决该问题的建议。

P.S。我尝试增加或减少每层神经元的数量,尝试使用预先训练的模型(例如VGG)来增加或减少网络深度。

一些情节。 注意!:最后一个图是out1-out6层精度的中位数

Model accuracy for output "class_img", which indicate is a number on image

Total model loss

Median of accuracy the rest 6 outputs, which illustrates a character

这是我的代码。

count_train=0
count_test=0

def convertation(val):
    zero = np.zeros(len(sources.DIGITS))
    zero[sources.DIGITS.index(val)] = 1
    return np.array(zero)

def data_generator(batch=32,val=False):
    if val:
        directory='K:\DATASETS\syntetic_cars\\mix_test'
    else:
        directory = 'K:\DATASETS\syntetic_cars\\no_reg'
    files=os.listdir(directory)
    i = 0
    while True:
        x=[]
        l1=[]
        l2=[]
        l3=[]
        l4=[]
        l5=[]
        l0=[]
        y2=[]
        for b in range(batch):
            if i == len(files):
                i = 0
                random.shuffle(files)
            sample = random.choice(files)
            sam=sample.split('_')[-2]
            i += 1
            try:
                image = cv2.imread(os.path.join(directory,sample))/255

            except TypeError:
                pil_image = Image.open(os.path.join(directory, sample))
                r, g, b = pil_image.split()
                img = Image.merge("RGB", (b, g, r))
                open_cv_image = np.array(img)
                image = open_cv_image[:, :, ::-1].copy()/255
            x.append(image)
            y=sam
            y2.append(int(sample.split('_')[-1][:-4]))

            l0.append(convertation(y[0]))
            l1.append(convertation(y[1]))
            l2.append(convertation(y[2]))
            l3.append(convertation(y[3]))
            l4.append(convertation(y[4]))
            l5.append(convertation(y[5]))

            yield np.array(x), \
                  {'class_img': np.array(y2),
                   'out1': np.array(l0),
                   'out2': np.array(l1),
                   'out3': np.array(l2),
                   'out4': np.array(l3),
                   'out5': np.array(l4),
                   'out6': np.array(l5)}


def train(epoch,count_train,count_test,plotting=False):
    tensorboard = TensorBoard(log_dir='./logs', histogram_freq=0,
                              write_graph=True, write_images=True)
    checkpoint = ModelCheckpoint(filepath="K:\Diplom\\reco_model\model.{epoch:02d}-{val_loss:.6f}-{loss:.6f}.hdf5",
                                 monitor='val_loss',
                                 verbose=1,
                                 save_best_only=False,
                                 mode='min')

    batch=48


    train_Gen=data_generator(batch)
    test_Gen=data_generator(batch,True)    
    input_data = Input(name='the_input', shape=(64, 128, 3), dtype='float32')

    x = Conv2D(4,
               padding='same',
               kernel_initializer='random_uniform',
               bias_initializer='zeros',
               activation='relu',
               name='conv1',
               kernel_size=(3, 3))(input_data)
    x = MaxPooling2D(name='pooling1',pool_size=(2, 2))(x)

    x = Conv2D(64,
               padding='same',
               kernel_initializer='random_uniform',
               bias_initializer='zeros',
               activation='relu',
               name='conv2',
               kernel_size=(5, 5))(x)
    x = MaxPooling2D(name='pooling2',pool_size=(2, 2))(x)
    x = Flatten()(x)
    x =Dense(8,activation='relu',kernel_initializer='random_uniform',bias_initializer='zeros')(x)

    class_img = Dense(1,
                      name='class_img',
                      kernel_initializer='random_uniform',
                      bias_initializer='zeros',
                      activation='sigmoid')(x)
    out1 = Dense(len(sources.DIGITS),
                 kernel_initializer='random_uniform',
                 bias_initializer='zeros',
                 name='out1',
                 activation='softmax')(x)
    out2 = Dense(len(sources.DIGITS),
                 kernel_initializer='random_uniform',
                 bias_initializer='zeros',
                 name='out2',
                 activation='softmax')(x)
    out3 = Dense(len(sources.DIGITS),
                 kernel_initializer='random_uniform',
                 bias_initializer='zeros',
                 name='out3',
                 activation='softmax')(x)
    out4 = Dense(len(sources.DIGITS),
                 kernel_initializer='random_uniform',
                 bias_initializer='zeros',
                 name='out4',
                 activation='softmax')(x)
    out5 = Dense(len(sources.DIGITS),
                 kernel_initializer='random_uniform',
                 bias_initializer='zeros',
                 name='out5',
                 activation='softmax')(x)
    out6 = Dense(len(sources.DIGITS),
                 kernel_initializer='random_uniform',
                 bias_initializer='zeros',
                 name='out6',
                 activation='softmax')(x)


    model= Model(inputs=input_data,
                 outputs=[class_img,
                          out1,
                          out2,
                          out3,
                          out4,
                          out5,
                          out6])

    model.summary()

    losses = {
        "class_img": "binary_crossentropy",
        "out1": "categorical_crossentropy",
        "out2": "categorical_crossentropy",
        "out3": "categorical_crossentropy",
        "out4": "categorical_crossentropy",
        "out5": "categorical_crossentropy",
        "out6": "categorical_crossentropy"}   

    model.compile(optimizer='adam',
                  loss=losses,
                  # loss_weights=lossWeights,
                  metrics=["accuracy"]
                  )


    print('Count of train steps:',int(count_train/batch))
    print('Count of test steps:',int(count_test/batch),end='\n\n')
    history = model.fit_generator(train_Gen,
        steps_per_epoch=int(count_train/batch),
        validation_data=test_Gen,
        validation_steps=int(count_test/batch),
        initial_epoch=0,
        verbose=1,
        callbacks=[checkpoint,tensorboard],
        epochs=epoch)


    if plotting:
        val_res=[]
        for ep in range(0,len(history.history['val_out1_acc'])):
            acc = []
            acc.append(history.history['val_out1_acc'][ep])
            acc.append(history.history['val_out2_acc'][ep])
            acc.append(history.history['val_out3_acc'][ep])
            acc.append(history.history['val_out4_acc'][ep])
            acc.append(history.history['val_out5_acc'][ep])
            acc.append(history.history['val_out6_acc'][ep])
            val_res.append(statistics.median(acc))
        acc_res=[]

        for ep in range(0,len(history.history['val_out1_acc'])):
            acc = []
            acc.append(history.history['out1_acc'][ep])
            acc.append(history.history['out2_acc'][ep])
            acc.append(history.history['out3_acc'][ep])
            acc.append(history.history['out4_acc'][ep])
            acc.append(history.history['out5_acc'][ep])
            acc.append(history.history['out6_acc'][ep])
            acc_res.append(statistics.median(acc))

        plt.plot(acc_res)
        plt.plot(val_res)
        plt.title('Model accuracy (chars)')
        plt.ylabel('accuracy')
        plt.xlabel('epoch')
        plt.legend(['train','test'], loc='upper left')
        plt.show()

        plt.plot(history.history['class_img_loss'])
        plt.plot(history.history['val_class_img_loss'])
        plt.title('Model accuracy (plate)')
        plt.ylabel('accuracy')
        plt.xlabel('epoch')
        plt.legend(['train', 'test'], loc='upper left')
        plt.show()

        plt.plot(history.history['loss'])
        plt.plot(history.history['val_loss'])
        plt.title('Model loss')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.legend(['train', 'test'], loc='upper left')
        plt.show()

directory='K:\DATASETS\syntetic_cars\\no_reg'
count_train=len(os.listdir(directory))
directory = 'K:\DATASETS\syntetic_cars\\mix_test'
count_test=len(os.listdir(directory))
train(50,count_train,count_test,True)

0 个答案:

没有答案