简体   繁体   中英

TensorFlow model gets zero loss

import tensorflow as tf
import numpy as np
import os
import re
import PIL


def read_image_label_list(img_directory, folder_name):
    # Input:
    #   -Name of folder (test\\\\train)
    # Output:
    #   -List of names of files in folder
    #   -Label associated with each file

    cat_label = 1
    dog_label = 0
    filenames = []
    labels = []

    dir_list = os.listdir(os.path.join(img_directory, folder_name))  # List of all image names in 'folder_name' folder

    # Loop through all images in directory
    for i, d in enumerate(dir_list):
        if re.search("train", folder_name):
            if re.search("cat", d):  # If image filename contains 'Cat', then true
                labels.append(cat_label)
            else:
                labels.append(dog_label)
        filenames.append(os.path.join(img_dir, folder_name, d))

    return filenames, labels


# Define convolutional layer
def conv_layer(input, channels_in, channels_out):
    w_1 = tf.get_variable("weight_conv", [5,5, channels_in, channels_out], initializer=tf.contrib.layers.xavier_initializer())
    b_1 = tf.get_variable("bias_conv", [channels_out], initializer=tf.zeros_initializer())
    conv = tf.nn.conv2d(input, w_1, strides=[1,1,1,1], padding="SAME")
    activation = tf.nn.relu(conv + b_1)
    return activation


# Define fully connected layer
def fc_layer(input, channels_in, channels_out):
    w_2 = tf.get_variable("weight_fc", [channels_in, channels_out], initializer=tf.contrib.layers.xavier_initializer())
    b_2 = tf.get_variable("bias_fc", [channels_out], initializer=tf.zeros_initializer())
    activation = tf.nn.relu(tf.matmul(input, w_2) + b_2)
    return activation


# Define parse function to make input data to decode image into
def _parse_function(img_path, label):
    img_file = tf.read_file(img_path)
    img_decoded = tf.image.decode_image(img_file, channels=3)
    img_decoded.set_shape([None,None,3])
    img_decoded = tf.image.resize_images(img_decoded, (28, 28), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    img_decoded = tf.image.per_image_standardization(img_decoded)
    img_decoded = tf.cast(img_decoded, dty=tf.float32)
    label = tf.one_hot(label, 1)
    return img_decoded, label


tf.reset_default_graph()

# Define parameterspe
EPOCHS = 10
BATCH_SIZE_training = 64
learning_rate = 0.001
img_dir = 'C:/Users/tharu/PycharmProjects/cat_vs_dog/data'
batch_size = 128

# Define data
features, labels = read_image_label_list(img_dir, "train")

# Define dataset
dataset = tf.data.Dataset.from_tensor_slices((features, labels))  # Takes slices in 0th dimension
dataset = dataset.map(_parse_function)
dataset = dataset.batch(batch_size)
iterator = dataset.make_initializable_iterator()

# Get next batch of data from iterator
x, y = iterator.get_next()

# Create the network (use different variable scopes for reuse of variables)
with tf.variable_scope("conv1"):
    conv_1 = conv_layer(x, 3, 32)
    pool_1 = tf.nn.max_pool(conv_1, ksize=[1,2,2,1], strides=[1,2,2,1], padding="SAME")

with tf.variable_scope("conv2"):
    conv_2 = conv_layer(pool_1, 32, 64)
    pool_2 = tf.nn.max_pool(conv_2, ksize=[1,2,2,1], strides=[1,2,2,1], padding="SAME")
    flattened = tf.contrib.layers.flatten(pool_2)

with tf.variable_scope("fc1"):
    fc_1 = fc_layer(flattened, 7*7*64, 1024)
with tf.variable_scope("fc2"):
    logits = fc_layer(fc_1, 1024, 1)


# Define loss function
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf.cast(y, dtype=tf.int32)))

# Define optimizer
train = tf.train.AdamOptimizer(learning_rate).minimize(loss)


with tf.Session() as sess:
    # Initiliaze all the variables
    sess.run(tf.global_variables_initializer())

    # Train the network
    for i in range(EPOCHS):
        # Initialize iterator so that it starts at beginning of training set for each epoch
        sess.run(iterator.initializer)
        print("EPOCH", i)
        while True:
            try:
                _, epoch_loss = sess.run([train, loss])

            except tf.errors.OutOfRangeError:  # Error given when out of data
                if i % 2 == 0:
                    # [train_accuaracy] = sess.run([accuracy])
                    # print("Step ", i, "training accuracy = %{}".format(train_accuaracy))
                    print(epoch_loss)
                break

I've spent a few hours trying to figure out systematically why I've been getting 0 loss when I run this model.

  • Features = list of file locations for each image (eg ['\\data\\train\\cat.0.jpg', /data\\train\\cat.1.jpg])
  • Labels = [Batch_size, 1] one_hot vector

Initially I thought it was because there was something wrong with my data. But I've viewed the data after being resized and the images seems fine.

Then I tried a few different loss functions because I thought maybe I'm misunderstanding what the the tensorflow function softmax_cross_entropy does, but that didn't fix anything.

I've tried running just the 'logits' section to see what the output is. This is just a small sample and the numbers seem fine to me:

 [[0.06388957]
 [0.        ]
 [0.16969752]
 [0.24913025]
 [0.09961276]]

Surely then the softmax_cross_entropy function should be able to compute this loss given that the corresponding labels are 0 or 1? I'm not sure if I'm missing something. Any help would be greatly appreciated.

As documented :

logits and labels must have the same shape, eg [batch_size, num_classes] and the same dtype (either float16 , float32 , or float64 ).

Since you mentioned your label is "[Batch_size, 1] one_hot vector", I would assume both your logits and labels are [Batch_size, 1] shape. This will certainly lead to zero loss. Conceptually speaking, you have only 1 class ( num_classes=1 ) and your cannot be wrong ( loss=0 ).

So at least for you labels , you should transform it: tf.one_hot(indices=labels, depth=num_classes) . Your prediction logits should also have a shape [batch_size, num_classes] output.

Alternatively, you can use sparse_softmax_cross_entropy_with_logits , where:

A common use case is to have logits of shape [batch_size, num_classes] and labels of shape [batch_size]. But higher dimensions are supported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM