我如何获得与0和1分类不同的结果,keras

时间:2019-04-14 06:06:34

标签: python tensorflow keras

我有我的模型,当我预测自己的图像时,我得到这样的结果 我知道这是正确的,但是我需要一个“百分比”来表示此结果 [0,0,0,1]

我需要这样的东西

[。04,.1,.94,.07] 当我使用argmax时,我得到了结果[3] 但我不能 我已经尝试过使用predict(),predict_proba()和predict_classes(),但是没有用 我的训练代码是:

data_entrenamiento = './data/entrenamiento'
data_validacion = './data/validacion'

epocas=20
longitud, altura = 150, 150
batch_size = 32
pasos = 200
validation_steps = 50
filtrosConv1 = 32
filtrosConv2 = 64
tamano_filtro1 = (3, 3)
tamano_filtro2 = (2, 2)
tamano_pool = (2, 2)
clases = 3
lr = 0.0004

entrenamiento_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)

entrenamiento_generador = entrenamiento_datagen.flow_from_directory(
data_entrenamiento,
target_size=(altura, longitud),
batch_size=batch_size,
class_mode='categorical')

validacion_generador = test_datagen.flow_from_directory(
data_validacion,
target_size=(altura, longitud),
batch_size=batch_size,
class_mode='categorical')

cnn = Sequential()
cnn.add(Convolution2D(filtrosConv1, tamano_filtro1, padding ="same",         input_shape=(longitud, altura, 3), activation='relu'))
cnn.add(MaxPooling2D(pool_size=tamano_pool))

cnn.add(Convolution2D(filtrosConv2, tamano_filtro2, padding ="same"))
cnn.add(MaxPooling2D(pool_size=tamano_pool))

cnn.add(Flatten())
cnn.add(Dense(256, activation='relu'))
cnn.add(Dropout(0.5))
cnn.add(Dense(clases, activation='softmax'))

cnn.compile(loss='categorical_crossentropy ',
        optimizer=optimizers.Adam(lr=lr),
        metrics=['accuracy'])

callbacks = [EarlyStopping(monitor='val_loss', patience=2),



cnn.fit_generator(
entrenamiento_generador,
steps_per_epoch=pasos,
epochs=epocas,
callbacks=callbacks,
validation_data=validacion_generador,
validation_steps=validation_steps)

我的分类代码是:

def predict(file):
x = load_img(file, target_size=(longitud, altura))
x = img_to_array(x)
x = np.expand_dims(x, axis=0)
array = cnn.predict(x,batch_size=32,verbose=1)
print(array)
result = array[0]
answer = np.argmax(result)
if answer == 0:
  print("pred: Perro")
elif answer == 1:
  print("pred: Gato")
elif answer == 2:
  print("pred: Gorila")

return answer

我已经尝试过使用predict(),predict_proba()和predict_classes(),但是没有用

0 个答案:

没有答案