Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 巨蟒。高精度模型一直分类错误_Python_Machine Learning_Classification_Keras - Fatal编程技术网

Python 巨蟒。高精度模型一直分类错误

Python 巨蟒。高精度模型一直分类错误,python,machine-learning,classification,keras,Python,Machine Learning,Classification,Keras,我有两门课。0=狗,1=非狗。 8800张图像(150150像素)用于培训,4400张图像用于验证。 4400只狗,4400只非狗在训练中。2200只狗,2200只非狗在验证中。 非日志图像包含船只、树木、钢琴等的随机图像。 我已经将我的网络训练到了87%+的准确度。 绘图: AccvsValAcc- LossValloss- 我的网络: #model dog/nondog model = Sequential() model.add(Convolution2D(16, 3, 3, input_

我有两门课。0=狗,1=非狗。 8800张图像(150150像素)用于培训,4400张图像用于验证。 4400只狗,4400只非狗在训练中。2200只狗,2200只非狗在验证中。 非日志图像包含船只、树木、钢琴等的随机图像。 我已经将我的网络训练到了87%+的准确度。 绘图: AccvsValAcc- LossValloss-

我的网络:

#model dog/nondog
model = Sequential()
model.add(Convolution2D(16, 3, 3, input_shape=(3, img_width, img_height)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(16, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
我在模型的末尾有一个节点,据我所知,如果我只需要对狗和非狗进行分类,我将处理二进制分类问题

我面临的问题是当我建模时。预测一张看不见的狗图片,它总是把它归类为非狗。我处理这个问题的方法有误吗?如果我的准确率如此之高,有人能向我解释为什么它从不将狗的图片归类为狗吗?您是否可以向我的网络或方法推荐任何更改

编辑: 我最初是在70x70图像上训练的。刚刚完成150x150图像的再培训。也可以使用now model.predict_类代替model.predict Im。但还是一样的问题。在我尝试的每个图像上,结果始终是非狗结果:(

EDIT2:完整代码:

    # -*- coding: utf-8 -*-
"""
Created on Thu Jan 26 16:21:36 2017

@author: PoLL
"""

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

from PIL import Image
import numpy as np

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import PIL
from PIL import Image
#draw rect
import matplotlib.patches as patches






#########################################################################################################
#VALUES
# dimensions of images.
img_width, img_height = 150,150
train_data_dir = 'data1/train'
validation_data_dir = 'data1/validation'
nb_train_samples = 8800 #1000 cats/dogs
nb_validation_samples = 4400 #400cats/dogs
nb_epoch = 20
#########################################################################################################


#model dog/nondog
model = Sequential()
model.add(Convolution2D(16, 3, 3, input_shape=(3, img_width, img_height)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(16, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])








#augmentation configuration for training
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
############################################################################################
#PRINT MODEL
from keras.utils.visualize_util import plot
plot(model, to_file='C:\Users\PoLL\Documents\Python Scripts\catdog\model.png')  
##########################################################################################################
#TEST AUGMENTATION
img = load_img('data/train/cats/cat.0.jpg')  # this is a PIL image
x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)

# the .flow() command below generates batches of randomly transformed images
# and saves the results to the `preview/` directory
i = 0
for batch in train_datagen.flow(x, batch_size=1,
                          save_to_dir='data/TEST AUGMENTATION', save_prefix='cat', save_format='jpeg'):
    i += 1
    if i > 20:
        break  # otherwise the generator would loop indefinitely
##########################################################################################################

# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)

#PREPARE TRAINING DATA
train_generator = train_datagen.flow_from_directory(
        train_data_dir, #data/train
        target_size=(img_width, img_height),  #RESIZE to 150/150
        batch_size=32,
        class_mode='binary')  #since we are using binarycrosentropy need binary labels

#PREPARE VALIDATION DATA
validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,  #data/validation
        target_size=(img_width, img_height), #RESIZE 150/150
        batch_size=32,
        class_mode='binary')


#START model.fit
history =model.fit_generator(
        train_generator, #train data
        samples_per_epoch=nb_train_samples,
        nb_epoch=nb_epoch,
        validation_data=validation_generator,  #validation data
        nb_val_samples=nb_validation_samples)

############################################################################################
#LOAD WEIGHTS
model.load_weights('savedweights2.h5')
############################################################################################
#check labels 0=cat 1=dog
#dog = 0, nondog =1
labels = (train_generator.class_indices)
print(labels)
############################################################################################
#TESTING
#load test DOG
img=load_img('data/prediction/catordog/dog.1234.jpg')
#reshape to 1,3,150,150
img = np.array(img).reshape((1,3,img_width, img_height))
plt.imshow(img.reshape((150, 150, 3)))
print(model.predict_classes(img))
#load test CAT
img2=load_img('data/prediction/catordog/cat.187.jpg')
#reshape to 1,3,150,150
img2 = np.array(img2).reshape((1,3,img_width, img_height))
plt.imshow(img2.reshape((150, 150, 3)))
print(model.predict_classes(img))
print(model.predict_classes(img2))


############################################################################################
#RESIZE IMAGES
baseheight = 70
basewidth = 70
img = Image.open('data/prediction/catordog/dog.1297.jpg')
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img.save('resized_dog.jpg')
############################################################################################

#load test DOG
img=load_img('resized_dog.jpg')
#reshape to 1,3,150,150
img = np.array(img).reshape((1,3,img_width, img_height))
plt.imshow(img.reshape((70, 70, 3)))

print(model.predict(img))

#plt.imshow(image)
print(img.shape)
############################################################################################
##### WINDOW BOX  TO GO THROUGH THIS IMAGE
image=load_img('finddog/findadog2.jpg')
image= np.array(image).reshape((600,1050,3))
plt.imshow(image)
print(image.shape)

############################################################################################
############################################################################################
#OBJECT IS HERE

#object x,y,w,h,
object0 =  (140, 140, 150,150)
object1 =  (340, 340, 150,150)
#object2 = (130,130,150,150)
objloc  = []
objloc.append(object0)
objloc.append(object1)
#objloc.append(object2)



#SLIDING WINDOW
def find_a_dog(image, step=20, window_sizes=[70]):
    boxCATDOG = 0  
    locations = []
    for win_size in window_sizes:
        #top =y, left =x
        for Y in range(0, image.shape[0] - win_size + 1, step):
            for X in range(0, image.shape[1] - win_size + 1, step):
                # compute the (top, left, bottom, right) of the bounding box
                box = (Y, X, Y + win_size, X + win_size)
                # crop
                cropped_img = image[box[0]:box[2], box[1]:box[3]]
                #reshape cropped image by window
                cropped_img = np.array(cropped_img).reshape((1,3,70,70))
                #classify it
                boxCATDOG = predict_function(cropped_img)              
                if boxCATDOG ==0:
                 #   print('box classified as dog') 
                    #save location of it                 
                    locations.append(box)
                    print("found dog")
    return locations                                 



############################################################################################                   
#FUNCTIONS   #
def predict_function(x):
    result = model.predict_classes(x)
    if result==1:
        return 1
    else:
        return 0   
#SHOW CROPPED IMAGE
def show_image(im):
   plt.imshow(im.reshape((150,150,3)))
#SHOW INPUT IMAGE
def show_ori_image(im):
   plt.imshow(im.reshape((600,1050,3)))

def draw_obj_loc(image,objectloc):
    fix,ax = plt.subplots(1)
    ax.imshow(image)
    for l in objloc:
        rectG = patches.Rectangle((l[0],l[1]),l[2],l[3],linewidth=1,edgecolor='G',facecolor='none')
        ax.add_patch(rectG)
    print len(objectloc)

    #draw box when classifies as dog
def draw_boxes(image, locations):
    fix,ax = plt.subplots(1)
    ax.imshow(image)       
    for l in locations:
        print l
        rectR = patches.Rectangle((l[1],l[0]),150,150,linewidth=1,edgecolor='R',facecolor='none')
        ax.add_patch(rectR)
    print len(locations)

def draw_both(image, locations,objectloc):
    fix,ax = plt.subplots(1)
    ax.imshow(image)
    for l in objloc:
        rectG = patches.Rectangle((l[0],l[1]),l[2],l[3],linewidth=1,edgecolor='G',facecolor='none')
        ax.add_patch(rectG)
    for l in locations:
        print l
        rectR = patches.Rectangle((l[1],l[0]),150,150,linewidth=1,edgecolor='R',facecolor='none')
        ax.add_patch(rectR)
#check if overlaps
def check_overlapping(image,locations,objloc):   

    for ol in objloc:
        objX = (ol[0])
        objY = (ol[1])
        objW = (ol[2])
        objH = (ol[3])

        for ok in locations:
            X=(ok[0])
            Y=(ok[1])
   # for l in locations:
        #  if (objX+objW<X or X+150<objX or objY+objH<Y or Y+150<objY):
            if (objX+objW<X or X+150<objX or objY+objH<Y or Y+150<objY):
                # Intersection = Empty
                       #no overlapping, false positive
                       print('THERES NO OVERLAPPING :',objloc.index(ol))
                        #                                              
            else:
                        #Intersection = Not Empty
                        print('THERE IS OVERLAPPING WITH OBJECT: ',objloc.index(ol), 'WITH BOX NUMBER: ',locations.index(ok))



############################################################################################       
#get locations from image
locations = find_a_dog(image)
#show where windowslide classifed as positive
draw_boxes(image,locations)
#show where objects actually are
draw_obj_loc(image,objloc)
#check for overlapping between slider classification and actual
check_overlapping(image,locations,objloc)
#drawboth boxes
draw_both(image, locations,objloc)







#GREEN RECT
#   X,Y    X+W,Y
      ######
      #    #
      #    #
      ######
#   X,Y+H   X+W,Y+H


#WINDOW
#  Y1,X1   Y1+W,X1
      ######
      #    #
      #    #
      ######
# Y1,X+H   Y1+W,X1+H

###REMOVED FUNCTIONS
##DRAW RECT RED
def draw_rect(im,Y,X):
    fig,ax = plt.subplots(1)
    ax.imshow(im)
    rect = patches.Rectangle((Y,X),150,150,linewidth=1,edgecolor='r',facecolor='none')
    ax.add_patch(rect) 
   # im =plt.savefig('rect.jpg')

######OBJECT LOCATION AND H W GREEN
def draw_box_object(im,X,Y,W,H):
    fig,ax = plt.subplots(1)
    ax.imshow(im)
    rect = patches.Rectangle((X,Y),W,H,linewidth=1,edgecolor='G',facecolor='none')
    ax.add_patch(rect)
  #  im = plt.savefig('boxfordog.jpg')


################################################################################################
#PLOT
#ACC VS VAL_ACC
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy ACC VS VAL_ACC')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
#LOSS VS VAL_LOSS
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss LOSS vs VAL_LOSS')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
################################################################################################
#SAVE WEIGHTS
model.save_weights('savedweights.h5')
#70x70
model.save_weights('savedweights2.h5')
#150x150
model.save_weights('savedweights3.h5')
#-*-编码:utf-8-*-
"""
创建于2017年1月26日星期四16:21:36
@作者:PoLL
"""
从keras.preprocessing.image导入ImageDataGenerator
从keras.models导入顺序
从keras.layers导入卷积2D、MaxPoolig2D
从keras.layers导入激活、脱落、变平、致密
将numpy作为np导入
将matplotlib.pyplot作为plt导入
导入matplotlib
从PIL导入图像
将numpy作为np导入
从keras.preprocessing.image导入ImageDataGenerator,数组\u到\u img,img\u到\u数组,加载\u img
进口PIL
从PIL导入图像
#绘制矩形
将matplotlib.patches导入为修补程序
#########################################################################################################
#价值观
#图像的尺寸。
img_宽度,img_高度=150150
列车数据目录='data1/列车'
验证\数据\目录='data1/验证'
nb#U训练样本=8800#1000只猫/狗
nb#U验证#U样本=4400#400只猫/狗
nb_epoch=20
#########################################################################################################
#模型狗/非模型狗
模型=顺序()
添加(卷积2D(16,3,3,输入形状=(3,img\u宽度,img\u高度)))
添加(激活('relu'))
add(MaxPooling2D(池大小=(2,2)))
模型添加(卷积2D(16,3,3))
添加(激活('relu'))
add(MaxPooling2D(池大小=(2,2)))
模型添加(卷积2D(32,3,3))
添加(激活('relu'))
add(MaxPooling2D(池大小=(2,2)))
模型添加(卷积2D(64,3,3))
添加(激活('relu'))
add(MaxPooling2D(池大小=(2,2)))
model.add(展平())
模型.添加(密度(128))
添加(激活('relu'))
模型。添加(辍学率(0.5))
模型.添加(密度(1))
添加(激活('sigmoid'))
model.compile(loss='binary\u crossentropy',
优化器='rmsprop',
指标=[‘准确度’])
#训练用增强配置
列车\数据发生器=图像数据发生器(
重新缩放=1./255,
剪切范围=0.2,
缩放范围=0.2,
水平(翻转=真)
############################################################################################
#打印模型
从keras.utils.visualize\u util导入绘图
绘图(模型,到\u file='C:\Users\PoLL\Documents\Python Scripts\catdog\model.png')
##########################################################################################################
#测试扩充
img=load_img('data/train/cats/cat.0.jpg')#这是一个PIL图像
x=img_to_数组(img)#这是一个具有形状(3、150、150)的Numpy数组
x=x.restorate((1,)+x.shape)#这是一个具有形状(1,3,150,150)的Numpy数组
#下面的.flow()命令生成随机转换的图像批
#并将结果保存到“preview/”目录中
i=0
对于序列中的批次数据生成流(x,批次大小=1,
保存到\u dir='data/TEST AUGMENTATION',保存\u prefix='cat',保存\u format='jpeg'):
i+=1
如果i>20:
断开,否则发电机将无限期地循环
##########################################################################################################
#只有重新缩放
test_datagen=ImageDataGenerator(重缩放=1./255)
#准备培训数据
train_generator=来自目录的train_datagen.flow_(
列车数据方向,#数据/列车
目标尺寸=(图像宽度、图像高度),#调整为150/150
批次大小=32,
类_mode='binary')#因为我们使用的是二进制,所以需要二进制标签
#准备验证数据
验证\u生成器=来自\u目录的测试\u datagen.flow\u(
验证_数据_目录,#数据/验证
目标尺寸=(图像宽度、图像高度),#调整大小为150/150
批次大小=32,
class_mode='binary')
#启动model.fit
历史=model.fit\u生成器(
列车发电机,列车数据
每个历元的样本数=nb列样本数,
nb_epoch=nb_epoch,
验证\数据=验证\生成器,\验证数据
nb_val_样本=nb_验证_样本)
############################################################################################
#负载重量
模型荷载重量('savedweights2.h5')
############################################################################################
#检查标签0=猫1=狗
#狗=0,非狗=1
标签=(列生成器类索引)
打印(标签)
############################################################################################
#测试
#负载测试狗
img=load\u img('data/prediction/catordog/dog.1234.jpg'))
#重塑为1,3150150
img=np.数组(img).重塑((1,3,img\u宽度,img\u高度))
plt.imshow(img.重塑((150,150,3)))
打印(模型预测类(img))
#负载测试猫
img2=load\u img('data/prediction/catordog/cat.187.jpg'))
#重塑为1,3150150
img2=np.数组(img2).重塑((1,3,img_宽度,img_高度))
plt.imshow(img2.重塑((1501503)))
打印(model.u类)