rovim
rovim

Reputation: 68

Custom loss function: perform a model.predict on the data in y_pred

I am training a network to denoise images, for this I am using the CIFAR10 dataset. I am trying to generate a custom loss function so that the loss is mse / classification_accuracy. Given that my network receives as input 32x32 (noisy) images and predicts 32x32 (denoised) images, I am assuming that y_pred and Y_true would be arrays of 32x32 images. Thus my custom loss functions looks like this:

def custom_loss():
    def joint_optimized_loss(y_true, y_pred):
        mse =  K.mean(K.square(y_pred - y_true), axis=-1)
        preds = classif_model.predict(y_pred)
        correctPreds = 0
        totPreds = 0
        for pred in preds:
            predictedClass = pred.index(max(pred))
            totPreds += 1
            if predictedClass == currentClass: 
                correctPreds += 1
        classifAccuracy = correctPreds / totPreds
        loss = mse / classifAccuracy
        return loss
    return joint_optimized_loss
myModel.compile(optimizer='adadelta', loss=custom_loss())

classif_model is a pre-trained model that classifies CIFAR10 images into one of the 10 classes. It receives an array of 32x32 images.

However when I run my code I get the following error:

Traceback (most recent call last):

File "myCode.py", line 94, in

myModel.compile(optimizer='adadelta', loss=custom_loss()) File "/home/rvidalma/anaconda2/envs/tensorUpdated/lib/python2.7/site-packages/keras/engine/training.py", line 850, in compile

sample_weight, mask) File "/home/rvidalma/anaconda2/envs/tensorUpdated/lib/python2.7/site-packages/keras/engine/training.py", line 450, in weighted

score_array = fn(y_true, y_pred) File "myCode.py", line 57, in joint_optimized_loss

preds = classif_model.predict(y_pred) File "/home/rvidalma/anaconda2/envs/tensorUpdated/lib/python2.7/site-packages/keras/models.py", line 913, in predict

return self.model.predict(x, batch_size=batch_size, verbose=verbose) File "/home/rvidalma/anaconda2/envs/tensorUpdated/lib/python2.7/site-packages/keras/engine/training.py", line 1713, in predict

verbose=verbose, steps=steps) File "/home/rvidalma/anaconda2/envs/tensorUpdated/lib/python2.7/site-packages/keras/engine/training.py", line 1260, in _predict_loop

batches = _make_batches(num_samples, batch_size) File "/home/rvidalma/anaconda2/envs/tensorUpdated/lib/python2.7/site-packages/keras/engine/training.py", line 374, in _make_batches

num_batches = int(np.ceil(size / float(batch_size)))
AttributeError: 'Dimension' object has no attribute 'ceil'

I think this has something to do with the fact that y_true and y_pred are both tensors that, before training, are empty thus classif_model.predict fails as it is expecting an array. However I am not sure on how to fix this...

I tried getting instead the value of y_pred using K.get_value(y_pred), but that gives me the following error:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape [-1,32,32,3] has negative dimensions [[Node: input_1 = Placeholderdtype=DT_FLOAT, shape=[?,32,32,3], _device="/job:localhost/replica:0/task:0/cpu:0"]]

Upvotes: 1

Views: 1792

Answers (2)

Narayan Kothari
Narayan Kothari

Reputation: 1

I had almost same problem, and I tried this and it worked for me.

Instead of:

preds = classif_model.predict(y_pred)

try:

preds = classif_model(y_pred)

I am not sure about the reason but it is because when we use model.predict(y) it need batch_size and while compiling we don't have any, so we can not use model.predict(y). Please correct me if this is wrong.

Upvotes: 0

Dr. Snoopy
Dr. Snoopy

Reputation: 56407

You cannot use accuracy as a loss function, as it is not differentiable. This is why upper bounds on accuracy like the cross-entropy are used instead.

Additionally, the way you implemented accuracy is also non-symbolic, you should have used only functions in keras.backend to implement a loss for it to work properly.

Upvotes: 1

Related Questions