测试集准确率1.66
来源:8-4 实战(一)
WeishunFu
2022-11-03
#去掉AVX warning
import os
import random
os.environ[‘TF_CPP_MIN_LOG_LEVEL’]='2’
import tensorflow as tf
#load the data
from keras.preprocessing.image import ImageDataGenerator
train_datagen=ImageDataGenerator(rescale=1./255)#缩放处理,使其进行归一化,模型训练时更高效
training_set=train_datagen.flow_from_directory(’./dataset/training_set’,target_size=(50,50),batch_size=32,class_mode=‘binary’)
#batch_size是我们希望每次训练时希望从图库中取得多少张图片
#set up the cnn model
#建立模型======
from keras.models import Sequential
from keras.layers import Conv2D,MaxPool2D,Flatten,Dense
model=Sequential()
#卷积层
model.add(Conv2D(32,(3,3),input_shape=(50,50,3),activation=‘ReLU’))
#池化层
model.add(MaxPool2D(pool_size=(2,2)))
#卷积层
model.add(Conv2D(32,(3,3),activation=‘ReLU’))
#池化层
model.add(MaxPool2D(pool_size=(2,2)))
#flattening layer
model.add(Flatten())
#FC layer
model.add(Dense(units=128,activation=‘ReLU’))
model.add(Dense(units=1,activation=‘sigmoid’))
#=完成主模型结构配置========
#configure the model
model.compile(optimizer=‘adam’,loss=‘binary_crossentropy’,metrics=[‘accuracy’])#加入metrics是为了在训练时有一个评估的方式,训练时可以看到
model.summary()
#train the model
model.fit_generator(training_set,epochs=25)#是因为之前数据是采用数据增强image_generator产生
#accuracy on the training data
accuracy_train=model.evaluate_generator(training_set)
print(accuracy_train)#[0.0003043838369194418, 1.0]
#accuracy on the testing data
test_set=train_datagen.flow_from_directory(’./dataset/test_set’,target_size=(50,50),batch_size=32,class_mode=‘binary’)
accuracy_test=model.evaluate_generator(test_set)
print(accuracy_test)#[1.6699533462524414, 0.7730000019073486]
老师,想问问我这个测试集为什么跑出来准确率是1.66多呢?跟着您的代码一步步走的
1回答
-
WeishunFu
提问者
2022-11-03
第一个数据为损失函数的值,而非准确率
10
相似问题