keras色分類、data_splitデータ分割
3247 ワード
data_splitはデータを訓練セットとテストセットに分割する
train_beer_Netトレーニング開始
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)