Open rakashi opened 7 years ago
Use 'activation='softmax'
in the last layer and compile using loss='categorical_crossentropy'
thanks alot for your response I have tried with 'softmax' and I kept loss as 'categorical_crossentropy' but its giving probability as sumof all probabilities equal to one. But I need to get the independent class probability
But I need to get the independent class probability
Just multiply the individual prob by 100 and you get the percentage of that class probability.
Hi, I have tried with the below an trained got an accuracy 80%
'''This script goes along the blog post "Building powerful image classification models using very little data" from blog.keras.io. It uses data that can be downloaded at: https://www.kaggle.com/c/dogs-vs-cats/data In our setup, we:
'''
from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D from keras.layers import Activation, Dropout, Flatten, Dense from keras import backend as K
dimensions of our images.
img_width, img_height = 150, 150
train_data_dir = '../data/Train' validation_data_dir = '../data/Val' nb_train_samples = 4654 nb_validation_samples = 1168 epochs = 1 batch_size = 16
if K.image_data_format() == 'channels_first': input_shape = (3, img_width, img_height) else: input_shape = (img_width, img_height, 3)
model = Sequential() model.add(Conv2D(32, (3, 3), input_shape=input_shape)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) model.add(Dense(64)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(10)) model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator( rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
this is the augmentation configuration we will use for testing:
only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical')
validation_generator = test_datagen.flow_from_directory( validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical')
model.fit_generator( train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size)
model.save('first_sig_try.h5')
and the prediction code is:
from keras.models import load_model from keras.preprocessing.image import img_to_array, load_img import numpy as np img_width = 150 img_height = 150
with open('../weights/output1.txt') as f: labels = f.read().splitlines()
test_model = load_model('first_sig_try.h5') img = load_img('/home/rakashi/Desktop/123.jpg',False,target_size=(img_width,img_height)) x = img_to_array(img) x = np.expand_dims(x, axis=0) preds = test_model.predict_classes(x) prob = test_model.predict_proba(x) preds1 = test_model.predict(x) for i in xrange(len(preds1[0])):
if preds1[0][i]:
print labels[int(test_model.predict_classes(np.array([img])))]
but the probability is not coming independently. its coming in the way like this below Apple -> 0.0 Banana -> 0.0 Broccoli -> 0.0 Burger -> 0.0 Egg -> 0.0 Frenchfry -> 0.0 Hotdog -> 0.0 Pizza -> 1.0 Rice -> 0.0 Strawberry -> 0.0
but i need get the independent probability for each class