machrisaa / tensorflow-vgg

VGG19 and VGG16 on Tensorflow
2.21k stars 1.08k forks source link

train model seems not correct #12

Closed supermancc closed 7 years ago

supermancc commented 7 years ago

I want to train the model from the initial state. The images are all 224*224. But the problem is, when I train the model in a FOR loop, the probability is always closed to 50%.( My problem is a two classification problem.)Can you provide the complete codes? Or can you fix my problem?

supermancc commented 7 years ago

my code here:

    # for loop
    for index in range(720):
            ind=random.randint(0,720)
            img=utils.load_image("./03224jpg/file%d.jpg"%ind)
            img_true_result=[0,1] if ind%2==0 else [1,0]
            batch = img.reshape((1, 224, 224, 3))
            print ind
            prob = sess.run(vgg.prob, feed_dict={images: batch, train_mode: False})
            utils.print_prob(prob[0], './synset.txt') 

            # simple 1-step training
            cost = tf.reduce_sum((vgg.prob - true_out) ** 2)
            train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
            sess.run(train, feed_dict={images: batch, true_out: img_true_result, train_mode: True})

            # test classification again, should have a higher probability about tiger
            prob = sess.run(vgg.prob, feed_dict={images: batch, train_mode: False})
            utils.print_prob(prob[0], './synset.txt')
machrisaa commented 7 years ago

There is a critical error in your code. You should take cost = tf.reduce_sum((vgg.prob - true_out) ** 2) and train = tf.train.GradientDescentOptimizer(0.01).minimize(cost) out of the loop.

Apart from the critical issue, I think you don't need to print the prob 2 times in a loop. You also need to make sure you have correctly modified your VGG, util, and synset.txt to produce 2 classification results instead of 1000.

I think you probably copy the logic in test_vgg19_trainable.py to build your training. Beware that it is just a test runner and NOT a standard code for training a network. I suggest you build one in your case.

If you want to train the model from the initial state, I suggest you read this long discussion here Usually I would not train the network from scratch. Instead, I will modify the final layers and reuse the pre-trained variables to do a new classification.

supermancc commented 7 years ago

thanks a lot