uzh-rpg / rpg_public_dronet

Code for the paper Dronet: Learning to Fly by Driving
MIT License
418 stars 159 forks source link

How can I get the prediction of a single image? #17

Open ericvelazquez opened 6 years ago

ericvelazquez commented 6 years ago

I want just to see the prediction of a single image using the following code:

`

Model reconstruction from JSON file

with open('model_struct.json', 'r') as f: model = model_from_json(f.read())

Load weights into the new model

model.load_weights('best_weights.h5')

Load the image file

parser = argparse.ArgumentParser(description='Image to test') parser.add_argument('image', type=str, help='The image image file to check') args = parser.parse_args()

img = load_img(args.image)

Predict

prediction = model.predict(img) `

But it raises the following error: ValueError: Error when checking : expected input_1 to have 4 dimensions, but got array with shape (720, 960, 3)

eridgd commented 6 years ago

You need to have an additional dimension for batch, even with just one image:

img = load_img(args.image)
assert img.ndim == 3
img = np.expand_dims(img, axis=0) # Alternatively could do: img[None, ...]
assert img.ndim == 4
ericvelazquez commented 6 years ago

Thanks!!

ericvelazquez commented 6 years ago

So I got this program to work:

`

Model reconstruction from JSON file

model = utils.jsonToModel("../model_struct.json")

Load weights into the new model

model.load_weights('../best_weights.h5')

model.compile(loss='mse', optimizer='sgd')

Load the image file

parser = argparse.ArgumentParser(description='Image to test') parser.add_argument('image', type=str, help='The image image file to check') args = parser.parse_args()

img = utils.load_img(args.image, grayscale=True, target_size=(200,200),crop_size=(200, 200)) img = np.expand_dims(img, axis=0)

Predict

outs = model.predict([img]) steer, coll = outs[0][0], outs[1][0] print("Steer angle= " + str(steer)) print("Collision prob= "+ str(coll)) `

python predict.py ../testing/road.jpg

I used different images to test it. All of them from the collision dataset and even if the photo is of an empty road or a person, the collision probability is always 1. Which means that I'm missing some step.

antonilo commented 6 years ago

I think that your target_size is wrong, it should be target_size=(320, 240). (Have a look in common_flags.py)

msohaildanish commented 4 years ago

Hi @antonilo thank you for such a great and helpful project. I was trying to predict the single image as @ericvelazquez but I was always getting the collision probability as 1. I tested different images even from training dataset you provided.

`base = Path.cwd()

with open(str(base / 'model/model_struct.json'), 'r') as json_file: loaded_model_json = json_file.read()

model = model_from_json(loaded_model_json) //tried best_wieght.h5 as weell model.load_weights(str(base / 'model/model_weights.h5'))

imu = load_img(str(base / 'images/78.jpg'), grayscale=True, target_size=(320, 240), crop_size=(200,200))

imu = np.expand_dims(imu, axis=0)

model.compile(loss='mean_squared_error', optimizer='adam') pred = model.predict(imu) `

msohaildanish commented 4 years ago

I found the solution for this. I was using utils. load_img() to get image as np.array but in Dronet utils.callback_img() there is extra step before returning image as np array which is return np.asarray(img, dtype=np.float32) * np.float32(1.0/255.0) drone_control/dronet/dronet_perception/src/Dronet/utils.py