Quantcast
Channel: Data Science, Analytics and Big Data discussions - Latest topics
Viewing all articles
Browse latest Browse all 4448

Keras binary classification: I am getting same prediction class for all images

$
0
0

@gayathrimenath93 wrote:

I am using Keras sequential model for image classification. My problem is to find particular watermark present in any part of the image or not. I am using a dataset of 18000 images and 25 types of watermarks. Keras backend is Tensorflow.

The program used for training is given below.

import h5py
import numpy as np
import cv2
import keras
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Activation, Dropout
from keras.preprocessing.image import ImageDataGenerator
from PIL import ImageFile

ImageFile.LOAD_TRUNCATED_IMAGES = True

classifier = Sequential()
classifier.add(Convolution2D(32, 3, 3, input_shape = (150, 150, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Convolution2D(64, 3, 3))
classifier.add(Activation('relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Flatten())
classifier.add(Dense(output_dim = 64, activation = 'relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(output_dim = 1, activation = 'sigmoid'))
classifier.compile(optimizer = 'rmsprop', loss = 'binary_crossentropy', metrics = ['accuracy'])


train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
                                   #rotation_range=15,
                                   #zca_whitening=True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('train',
                                                 target_size = (150, 150),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

test_set = test_datagen.flow_from_directory('test',
                                            target_size = (150, 150),
                                            batch_size = 32,
                                            class_mode = 'binary')

callbacks = [
  keras.callbacks.EarlyStopping(
    monitor='val_loss', patience=10, verbose=0)
  ]

hist = classifier.fit_generator(training_set,
                         samples_per_epoch = 9000,
                         nb_epoch = 10,
                         validation_data = test_set,
                         nb_val_samples = 3000,
                         callbacks=callbacks)
                         # )

The train directory contains 2 subdirectories: watermark and nonwatermark.
Why is the prediction result 1 everytime? Am I doing anything wrong?

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4448

Trending Articles