CNN 准确性不准确
CNN accuracy isn't accurate
我有一个像这样的 cnn
import pandas as pd
import numpy as np
import cv2
import matplotlib.pyplot as plt
from tensorflow.keras.layers import *
from tensorflow.keras.models import *
import random
import pickle
df = pd.read_csv('minor/age_gender.csv')
image_list = []
labels = []
image_labels = []
print(df.shape[0])
for x in range(df.shape[0]):
png = np.fromstring(df.iloc[x]['pixels'], dtype=int, sep=' ').reshape((48,48,1))
png = png/250.0
age = df.iloc[x]['age']
minor=None
if int(age)<18:
#print(2)
minor =1
else:
minor = 0
image_labels.append((png,minor))
young = []
old = []
for png,minor in image_labels:
if minor==1:
young.append((png,minor))
else:
old.append((png,minor))
pickle.dump(young,open('minor/young.pickle','wb'))
print('saved')
input()
young = young[:min(len(young),len(old))]
old = old[:min(len(young),len(old))]
print(len(young))
input()
image_labels = old+young
print(image_labels)
input()
random.shuffle(image_labels)
for png,minor in image_labels:
image_list.append(png)
print(minor)
labels.append(minor)
image_list=np.array(image_list)
labels = np.array(labels)
print(image_list.shape)
#imag_list = image_list.reshape(-1,48,48,1)
print(image_list.shape)
def model2():
inputs = Input((None,None,1))
x = Conv2D(128,(5,5),input_shape = (48,48,1),padding='same')(inputs)
x= BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(64,(5,5),padding='same')(x)
x= BatchNormalization()(x)
x = Activation('relu')(x)
x= BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(1,(5,5),padding='same')(x)
x=GlobalMaxPooling2D()(x)
outputs = Activation('sigmoid')(x)
model = Model(inputs=inputs,outputs=outputs)
model.compile('adam','binary_crossentropy',metrics=['accuracy'])
return model
input()
model = model2()
print(model.summary())
model.fit(image_list,labels,epochs=10,batch_size=32)
model.save('minor/age_detection6')
当我 运行 它时,我得到一个纪元日志,看起来像
Epoch 1/10
265/265 [==============================] - 40s 148ms/step - loss: 0.6445 - accuracy: 0.6447
Epoch 2/10
265/265 [==============================] - 37s 139ms/step - loss: 0.4422 - accuracy: 0.7994
Epoch 3/10
265/265 [==============================] - 37s 138ms/step - loss: 0.4186 - accuracy: 0.8168
Epoch 4/10
265/265 [==============================] - 37s 138ms/step - loss: 0.3720 - accuracy: 0.8381
Epoch 5/10
265/265 [==============================] - 37s 138ms/step - loss: 0.3559 - accuracy: 0.8517
Epoch 6/10
265/265 [==============================] - 39s 149ms/step - loss: 0.3487 - accuracy: 0.8564
Epoch 7/10
265/265 [==============================] - 40s 151ms/step - loss: 0.3341 - accuracy: 0.8597
Epoch 8/10
265/265 [==============================] - 37s 139ms/step - loss: 0.3252 - accuracy: 0.8699
Epoch 9/10
265/265 [==============================] - 37s 139ms/step - loss: 0.3084 - accuracy: 0.8765
Epoch 10/10
265/265 [==============================] - 37s 138ms/step - loss: 0.3074 - accuracy: 0.8746
到目前为止一切都很好。有相当高的准确率。这就是它变得奇怪的地方。在下面的代码中
import cv2
import numpy as np
from tensorflow.keras.models import load_model
import pandas as pd
import pickle
from tqdm import tqdm
model = load_model('minor/age_detection6')
df = pd.read_csv('minor/age_gender.csv')
image_list = []
labels = []
print(df.shape[0])
for x in range(df.shape[0]):
png = np.fromstring(df.iloc[x]['pixels'], dtype=int, sep=' ').reshape((48,48,1))
png = png/250.0
age = df.iloc[x]['age']
minor=None
if int(age)<18:
minor =1
else:
minor = 0
image_list.append(png)
labels.append(minor)
print(labels.index(1))
input()
image2 = cv2.imread('minor/kid.png')
print(image2.shape)
image2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)
print(image2.shape)
image = image_list[0]
print(labels[0])
image = image/255.0
image = np.reshape(image,(1,image.shape[0],image.shape[1],1))
image2 = image2/255.0
image2 = np.reshape(image2,(1,image2.shape[0],image2.shape[1],1))
print(image2.shape)
input()
valu = 0
predictions=model.predict(image)
print(predictions)
for items in predictions:
for item in items:
valu+=item
valu = int(valu/predictions.shape[0])
print(valu)
print(labels[0])
young = pickle.load(open('minor/young.pickle','rb'))
print('no')
score = 0
for png,minor in tqdm(young):
score += int(model.predict(png)[0][0])
print(score/len(young))
我拍了一堆未成年人的照片(其中一些是模型训练过的),但是在预测所有未成年人时,成功率为 0%。然而,对于非未成年人,它是 100%。我的数据被洗牌和平衡,所以我很困惑为什么会这样。任何帮助将不胜感激。
通常在二进制预测中,您设置一个阈值,将输出分成两个 类。您只是想将您的输出(0 到 1 之间的数字)放入您的 类 中而不设置阈值。您的比较应如下所示:
class_1_correct += np.sum(outputs>=0.5 * class_labels)
class_0_correct += np.sum(np.abs(1 - outputs<0.5 * class_labels))
这只是一个示例,需要根据您的代码进行调整。
另外,这部分是错误的:
for png,minor in tqdm(young):
score += int(model.predict(png)[0][0])
您正在将 sigmoid 输出转换为 int,它几乎总是等于零。
同样适用于此代码:
valu = int(valu/predictions.shape[0])
我认为你想成为的人:
valu = int(valu/predictions.shape[0]*100)
我有一个像这样的 cnn
import pandas as pd
import numpy as np
import cv2
import matplotlib.pyplot as plt
from tensorflow.keras.layers import *
from tensorflow.keras.models import *
import random
import pickle
df = pd.read_csv('minor/age_gender.csv')
image_list = []
labels = []
image_labels = []
print(df.shape[0])
for x in range(df.shape[0]):
png = np.fromstring(df.iloc[x]['pixels'], dtype=int, sep=' ').reshape((48,48,1))
png = png/250.0
age = df.iloc[x]['age']
minor=None
if int(age)<18:
#print(2)
minor =1
else:
minor = 0
image_labels.append((png,minor))
young = []
old = []
for png,minor in image_labels:
if minor==1:
young.append((png,minor))
else:
old.append((png,minor))
pickle.dump(young,open('minor/young.pickle','wb'))
print('saved')
input()
young = young[:min(len(young),len(old))]
old = old[:min(len(young),len(old))]
print(len(young))
input()
image_labels = old+young
print(image_labels)
input()
random.shuffle(image_labels)
for png,minor in image_labels:
image_list.append(png)
print(minor)
labels.append(minor)
image_list=np.array(image_list)
labels = np.array(labels)
print(image_list.shape)
#imag_list = image_list.reshape(-1,48,48,1)
print(image_list.shape)
def model2():
inputs = Input((None,None,1))
x = Conv2D(128,(5,5),input_shape = (48,48,1),padding='same')(inputs)
x= BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(64,(5,5),padding='same')(x)
x= BatchNormalization()(x)
x = Activation('relu')(x)
x= BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(1,(5,5),padding='same')(x)
x=GlobalMaxPooling2D()(x)
outputs = Activation('sigmoid')(x)
model = Model(inputs=inputs,outputs=outputs)
model.compile('adam','binary_crossentropy',metrics=['accuracy'])
return model
input()
model = model2()
print(model.summary())
model.fit(image_list,labels,epochs=10,batch_size=32)
model.save('minor/age_detection6')
当我 运行 它时,我得到一个纪元日志,看起来像
Epoch 1/10
265/265 [==============================] - 40s 148ms/step - loss: 0.6445 - accuracy: 0.6447
Epoch 2/10
265/265 [==============================] - 37s 139ms/step - loss: 0.4422 - accuracy: 0.7994
Epoch 3/10
265/265 [==============================] - 37s 138ms/step - loss: 0.4186 - accuracy: 0.8168
Epoch 4/10
265/265 [==============================] - 37s 138ms/step - loss: 0.3720 - accuracy: 0.8381
Epoch 5/10
265/265 [==============================] - 37s 138ms/step - loss: 0.3559 - accuracy: 0.8517
Epoch 6/10
265/265 [==============================] - 39s 149ms/step - loss: 0.3487 - accuracy: 0.8564
Epoch 7/10
265/265 [==============================] - 40s 151ms/step - loss: 0.3341 - accuracy: 0.8597
Epoch 8/10
265/265 [==============================] - 37s 139ms/step - loss: 0.3252 - accuracy: 0.8699
Epoch 9/10
265/265 [==============================] - 37s 139ms/step - loss: 0.3084 - accuracy: 0.8765
Epoch 10/10
265/265 [==============================] - 37s 138ms/step - loss: 0.3074 - accuracy: 0.8746
到目前为止一切都很好。有相当高的准确率。这就是它变得奇怪的地方。在下面的代码中
import cv2
import numpy as np
from tensorflow.keras.models import load_model
import pandas as pd
import pickle
from tqdm import tqdm
model = load_model('minor/age_detection6')
df = pd.read_csv('minor/age_gender.csv')
image_list = []
labels = []
print(df.shape[0])
for x in range(df.shape[0]):
png = np.fromstring(df.iloc[x]['pixels'], dtype=int, sep=' ').reshape((48,48,1))
png = png/250.0
age = df.iloc[x]['age']
minor=None
if int(age)<18:
minor =1
else:
minor = 0
image_list.append(png)
labels.append(minor)
print(labels.index(1))
input()
image2 = cv2.imread('minor/kid.png')
print(image2.shape)
image2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)
print(image2.shape)
image = image_list[0]
print(labels[0])
image = image/255.0
image = np.reshape(image,(1,image.shape[0],image.shape[1],1))
image2 = image2/255.0
image2 = np.reshape(image2,(1,image2.shape[0],image2.shape[1],1))
print(image2.shape)
input()
valu = 0
predictions=model.predict(image)
print(predictions)
for items in predictions:
for item in items:
valu+=item
valu = int(valu/predictions.shape[0])
print(valu)
print(labels[0])
young = pickle.load(open('minor/young.pickle','rb'))
print('no')
score = 0
for png,minor in tqdm(young):
score += int(model.predict(png)[0][0])
print(score/len(young))
我拍了一堆未成年人的照片(其中一些是模型训练过的),但是在预测所有未成年人时,成功率为 0%。然而,对于非未成年人,它是 100%。我的数据被洗牌和平衡,所以我很困惑为什么会这样。任何帮助将不胜感激。
通常在二进制预测中,您设置一个阈值,将输出分成两个 类。您只是想将您的输出(0 到 1 之间的数字)放入您的 类 中而不设置阈值。您的比较应如下所示:
class_1_correct += np.sum(outputs>=0.5 * class_labels)
class_0_correct += np.sum(np.abs(1 - outputs<0.5 * class_labels))
这只是一个示例,需要根据您的代码进行调整。
另外,这部分是错误的:
for png,minor in tqdm(young):
score += int(model.predict(png)[0][0])
您正在将 sigmoid 输出转换为 int,它几乎总是等于零。
同样适用于此代码:
valu = int(valu/predictions.shape[0])
我认为你想成为的人:
valu = int(valu/predictions.shape[0]*100)