0
votes

I am trying to create a CNN model in tensorflow and I can not understand a problem:

my model is a standard model 3d model build in a function called convolutional_neural_network.

configured as:

    self.prediction = self.convolutional_neural_network(self.x, img_sz, n_slices)
    self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.y_,
                                                                  logits=self.prediction))
    optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.cost)
    correct_prediction = tf.equal(tf.argmax(self.prediction, 1), tf.argmax(self.y_, 1))
    self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

where self.y_ is a placeholder where the labels are placed.

And at some point I am trying to execute that piece of code:

            feed_dict = {self.x: batch_img, self.y_: batch_label, self.keep_prob:
                    keep_rate, self.learning_rate: learning_rate}

            validation_cost = self.cost.eval(
                feed_dict = feed_dict )
            print "Validation cost: "+str(validation_cost)

            validation_cost = self.cost.eval(
                feed_dict = feed_dict)
            print "Validation cost: "+str(validation_cost)

where self.keep_prob: is a drop out of the model

It should give the sam result, but it don't: it outputs:

Validation cost: 1.05199,

Validation cost: 0.115607

Could anyone please give me some idea about what is happening?

thanks

ps: I have already remove the "optimizer" line, but it outputs different result

thanks so much

ADDED:

my model is the following:

def convolutional_neural_network(self, x, img_sz, n_slices):
    weights = {
        'W_conv1': self.weight_variable([6, 8, 8, 1, 32]),
        'W_conv2': self.weight_variable([2, 5, 5, 32, 48]),
        'W_fc': self.weight_variable([int(
            math.ceil(n_slices / 4) * (math.ceil(img_sz / 4) * math.ceil(img_sz / 4) *
                                        80)),
                                      512]),
        'W_fc2': self.weight_variable([512, 256]),
        'out': self.weight_variable([256, 2])
    }

    biases = {
        'b_conv1': self.bias_variable([32]),
        'b_conv2': self.bias_variable([48]),
        'b_fc': self.bias_variable([512]),
        'b_fc2': self.bias_variable([256]),
        'out': self.bias_variable([2])
    }

    self.x_im = tf.reshape(x, shape=[-1, n_slices, img_sz, img_sz, 1])

    conv1 = tf.tanh(self.conv3d(self.x_im, weights['W_conv1']) + biases['b_conv1'])
    conv1 =self.maxpool3d(conv1)

    conv2 = tf.tanh(self.conv3d(conv1, weights['W_conv2']) + biases['b_conv2'])
    conv2 = self.maxpool3d(conv2)


    fc = tf.reshape(conv4, [-1,int(math.ceil(n_slices/8)*math.ceil(img_sz/8)*math.ceil(
        img_sz/8))*80])
    fc = tf.tanh(tf.matmul(fc, weights['W_fc'])+biases['b_fc'])
    fc = tf.tanh(tf.matmul(fc, weights['W_fc2'])+biases['b_fc2'])
    fc = tf.nn.dropout(fc, self.keep_prob)

    output = tf.matmul(fc, weights['out'])+biases['out']
    return output

where x = tf.placeholder

Also mention that batch_img and batch_label are numpy arrays.

And:

def weight_variable(self, shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(self, shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

And the trainning process:

def train_neural_network(self, data_img, labels,
                         batch_size, img_sz, n_slices, last_batch,
                         keep_rate, model_path):

    self.prediction = self.convolutional_neural_network(self.x, img_sz, n_slices)
    self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.y_,
                                                         logits=self.prediction))
    optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.cost)
    correct_prediction = tf.equal(tf.argmax(self.prediction, 1), tf.argmax(self.y_, 1))
    self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    hm_epochs = 1000
    saver = tf.train.Saver(tf.trainable_variables())
    n_epoch = 0
    learning_rate = 1e-4
    model_path_train = 'model_train_3/my_model.ckpt'

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        if model_path:
            saver.restore(sess, model_path_train)
        while n_epoch < hm_epochs:
            if len(data_img)>last_batch+batch_size:
                with tf.device('/cpu:0'):
                    #batch_img, batch_label, last_batch = self.get_image(
                    #    data_img, labels, last_batch, batch_size, img_sz, n_slices
                    #)
                    batch_img, batch_label, last_batch = self.wrapper_image(data_img, labels, last_batch, batch_size)

                print "Batch label images: "+str(batch_label)
                batch_label = self.dense_to_one_hot(np.array(batch_label, dtype=np.int),
                                                    2).astype(np.float32)


                ####### at the end of EACH EPOCH ###
                n_epoch += 1
                print "n_epoch: "+str(n_epoch)

                _, c = sess.run(
                    [optimizer, self.cost], feed_dict={
                        self.x: batch_img, self.y_: batch_label, self.keep_prob: keep_rate,
                        self.learning_rate: learning_rate
                    }
                )


                c = self.cost.eval(feed_dict = {self.x: batch_img, self.y_: batch_label,
                                             self.keep_prob:
                        keep_rate, self.learning_rate: learning_rate})

                print "train cost: "+str(c)

                c = self.cost.eval(feed_dict = {self.x: batch_img, self.y_: batch_label,
                                             self.keep_prob:
                        keep_rate, self.learning_rate: learning_rate})

                print "train cost: "+str(c)
                if model_path:
                    saver.save(sess, model_path_train)
2

2 Answers

0
votes

Most often than not, this is a result of not having properly saved and restored either a variable, or if you use batch normalization, then the data for that. A result of which is that the unrestored variable gets initialized at each run with random weights, giving you a different result each time.

As a quick test insert a tf.set_random_seed( 1 ) right after all the imports, and see if this fixes the value for the different runs. If yes, then my theory is even more likely to be valid. Then try printing the mean and standard deviation for the activations of each layer and you will see where the two runs start to diverge.

0
votes

Fixed, i have just realized about the dropout keep probability. It was set to 0.5 then the model activate different networks of the model in each cost run. If I set keep prob =1.0 the result outputs the same in the two consecutive runs. Thanks Peter for your help