1
$\begingroup$

I trained my model ResNet-50 for UCMerced_LandUse dataset and this my loss graph enter image description here but I have a problem when I try my first picture river give me the right class river then give me the first class river predicted for all the next picture I test

for example :
input picture is => pic A result class A
input picture is => pic B result class A
input picture is => pic C result class A
....

this is my code repository

my test code that give me always the first class

import tensorflow as tf
import numpy as np
from os import path as PATH
from os import makedirs
import cv2


classes =['agricultural', 'airplane', 'baseballdiamond', 'beach', 'buildings',
             'chaparral', 'denseresidential', 'forest', 'freeway', 'golfcourse', 'harbor',
             'intersection', 'mediumresidential', 'mobilehomepark', 'overpass',
             'parkinglot', 'river', 'runway', 'sparseresidential', 'storagetanks',
             'tenniscourt']


def testing(sess,path,classes=classes, save_dir = "Save/",save_file ="data"):

    data = cv2.imread(path)
    labels = np.zeros((1,len(classes)))

    # get image height, width, channels
    height, width, channels = data.shape

    data =np.array([data])

    print(data.shape)
    print("Input image size :", height, width, channels)

    if not PATH.isdir(save_dir):
        if makedirs(save_dir):
            print(save_dir,"is created")
    with sess:
        if PATH.isdir(save_dir) and PATH.isfile(save_dir+save_file+".meta") and PATH.isfile(save_dir+"checkpoint"):
            print("files are exist")
            saver = tf.train.import_meta_graph(save_dir+save_file+".meta")
            saver.restore(sess, tf.train.latest_checkpoint("Save/"))
            print("data are restored")
            graph = tf.get_default_graph()
            x = graph.get_tensor_by_name("t_picture:0")#vrai
            y = graph.get_tensor_by_name("t_labels:0")#vrai
            #train = tf.get_collection('train_op')#vrai
            #loss = tf.get_collection('loss_op')#vrai
            logists = tf.get_collection('logits_op')#vrai
            #errors = tf.get_collection('errors')#vrai
        else:
            exit("data are not exist")
        print("start")
        curr_logists = sess.run([logists], {x: data, y: labels})
        curr_logists = np.array(curr_logists)[0,0,0]
        softmax = sess.run(tf.nn.softmax(curr_logists))
        print("logists : ", curr_logists)
        print("softmax : ", softmax)
        print("class : ",classes[np.argmax(softmax)])
        #print("loss:\n%s" % ( curr_loss))
        print("test is finished")
        sess.close()

if __name__ == '__main__':

    # path="UCMerced_LandUse/Images/"
    path="runway.tif"
    save_dir = "Save/"
    save_file = "dataSaved"
    testing(tf.Session(),path,classes,save_dir,save_file)
$\endgroup$
  • 1
    $\begingroup$ I am not familiar with tensorflow but I was getting the same problem in my code...Turns out I was using an nn class to train on a train set and mistakenly instead of testing on the CV set I was training on it....I was getting the error of sorts that it was predicting all the categories as a single class...Maybe 1 or 2 or 3 .. $\endgroup$ – DuttaA Feb 25 '18 at 14:46
  • $\begingroup$ my model give array of value before I use Softmax ,contain négatif and positif value !! $\endgroup$ – Sakhri Houssem Feb 25 '18 at 21:31
  • 1
    $\begingroup$ You maybe having the vanishing gradient problem...Try normalizing the dataset $\endgroup$ – DuttaA Feb 26 '18 at 15:32
  • $\begingroup$ What if you try another image besides "river" as the first one? Do you still get correct prediction for the first image? $\endgroup$ – Adnan S Mar 2 '18 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.