keras 颜色分类,data_split数据划分

data_split将数据及划分为训练集和测试集

import os
import numpy as np
from sklearn.model_selection import train_test_split
from keras_processing.image import img_to_array,load_img
import keras

def data_split(white_dir,yellow_dir):
    data_x = []
    data_y = []

    file_list1 = os.listdir(white_dir)
    file_list2 = os.listdir(yellow_dir)

    for name1 in file_list1:
        try:
            img = load_img(os.path.join(white_dir,name1),target_size=(224,224)) #target_size将图片转换到(224,224)
            img = img_to_array(img)/255.0 #彩色图像归一化
            data_x.append(img)
            data_y.append("1") # 标签
        except:
            continue

    for name2 in file_list2:
        try:
            img = load_img(os.path.join(white_dir, name2), target_size=(224, 224))
            img = img_to_array(img) / 255.0
            data_x.append(img)
            data_y.append("0")
        except:
            continue

    new = np.array(data_x)
    print(new.shape)

    x_train,x_test,y_train,y_test = train_test_split(data_x,data_y,test_size=0.2,random_state=42) #划分训练集和测试集

    x_train = np.array(x_train) # 转换为数组
    y_train = np.array(y_train)
    x_test = np.array(x_test)
    y_test = np.array(y_test)

    y_train = keras.utils.to_categorical(y_train,2) # 标签进行转换
    y_test = keras.utils.to_categorical(y_test, 2)

    return x_train,x_test,y_train,y_test

train_beer_net开始训练

import os
import numpy as np
os.environ["CUDA_VISIBLE_DEVISE"] = "0"
from Nets.beer_net import beer_net
from data_split import data_split
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint,ReduceLROnPlateau,EarlyStopping
from sklearn.metrics import f1_score,recall_score,precision_score,accuracy_score

white_dir = "train_white"
yellow_dir = "train_yellow"

if __name__ == "__main__":
    log_dir = "./logs/"
    x_train,x_test,y_train,y_test = data_split(white_dir,yellow_dir)

    model = beer_net(2)

    checkpoint = ModelCheckpoint(
        log_dir+"ep{epoch:03d}-acc{acc:.2f}.h5",
        monitor="acc",
        save_weight_only = True,
        period= 3
    )

    # 3个批次准确率没有变化就减小学习率
    reduce_lr = ReduceLROnPlateau(
        monitor="acc",
        factor = 0.5,
        patience = 3,
        verbose= 1
    )
    # 早停,6个批次没有acc没有上升就停止
    early_stopping = EarlyStopping(monitor="acc",mode = "max",min_delta = 0,patience = 6,verbose= 1)

    # 编译,学习率设置为1e-4
    model.compile(optimizer=Adam(lr=1e-4),loss="binary_crossentropy",metrics= ["acc"])
    # callbacks回调函数,调节控制作用
    model.fit(x_train,y_train,epochs = 80,batch_size = 64,callbacks = [checkpoint,reduce_lr,early_stopping],shuffle = True)
    model.save("beer_net_model_L2.h5")


    # 验证集结果
    pre = []
    for x in x_test:
        x = np.expend_dims(x,axis=0)
        pres = model.predict(x)
        pre.append(np.argmax(pres))

    true = []
    for y in y_test:
        true.append(np.argmax(y))
    #指标
    acc = accuracy_score(true,pre)
    pre = precision_score(true,pre)
    rec = recall_score(true,pre)
    f1 = f1_score(true,pre)

    print("acc:",acc)
    print("pre:",pre)
    print("rec:",rec)
    print("f1:",f1)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值