0

I am using imitation learning to teach a car how to drive in Gazebo. I am using an image from the camera feed on the car as the data, and the respective velocity command for that frame as its label.

I saved all this data from a python script, into a text file, and it is formatted as "[[image]], dtype=uint8), ['velcmd1', ... 'velcmd6'] , with lots more entries that follow in same format as the first, as seen below.

"[[0, 0, 0, ..., 0, 0, 0],       [0, 0, 0, ..., 0, 0, 0],       ...,       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8), ['0.295245', '0.0', '0.0', '0.0', '0.0', '0.0']]", "[[0, 0, 0, ..., 0, 0, 0],

I need to convert this out of string formats and into two respective data types, one as an array that represents the image, and the other as a list that is the label. I have been able to separate the two by doing some ugly string.split() and string.replace(), and I have been able to get the label into the format, with its type being printed below:

[0.295245, 0.0, 0.0, 0.0, 0.0, 0.0]
<class 'list'>

I did this by evaluating the string of the label with ast.literal_eval().

For the image, I am able to get it into the format:

[[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]]
<class 'str'>

However, it is still a string, and ast.literal_eval() raises a Malformed String ValueError. I tried both of the last solutions here Malformed String ValueError ast.literal_eval() with String representation of Tuple

I instead tried to manually create an array then append the values to this array, which gives

['0, 0, 0, ..., 0, 0, 0', '0, 0, 0, ..., 0, 0, 0', '0, 0, 0, ..., 0, 0, 0', '..., 0, 0, 0, ..., 0, 0, 0', '0, 0, 0, ..., 0, 0, 0', '0, 0, 0, ..., 0, 0, 0']
<class 'list'>

However, now the individual entries are strings, and not arrays themselves.

When I have trained NN's in the past, the image data has been in the form:

[[ 32  31  30 ... 100 101 103]
[ 30  30  30 ... 100 101 103]
[ 30  30  31 ... 101 101 102]
...
[ 34  34  32 ...  87  87  87]
[ 30  30  29 ... 100 100  98]
[ 30  29  30 ... 100  99 100]]
<class 'numpy.ndarray'>

How can I convert that original string of the image from the text file into this final form?

Note: Training data is not all 0's (black), it is just binarized, so entries shown are black.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 2
    You can't recover anything from that. The '...,` is lost data. Don't try to save image data as a string like that. 2d arrays can, with care, be saved a `csv` format, but that's not good for large amounts of data. Don't confuse the array with it's display string. The string is just a summary that gives you an idea of what the array contains, but it is not designed to be a reproducible object. `ast` can create a list from a list display string, but that doesn't work with arrays. – hpaulj Dec 27 '21 at 18:21
  • 1
    Are the "..."s literally in your file or are you doing that here just to keep the data examples short? – Code-Apprentice Dec 27 '21 at 21:15
  • 1
    @Code-Apprentice They are literally in my file. I believe hpaulj is correct... I need to find a new way to be able to save/track the images with their labels. – develkneifel Dec 28 '21 at 00:15

2 Answers2

1

The solution I found for tracking the labels for my images: there are only 6 distinct labels, so instead of trying to save the image next to its label somewhere, I created six different folders for each label. Then if velcmd1 is the label for the outputted image, I put it in the folder with all the other images that will be labeled with velcmd1.

I am doing this using :

cv.imwrite(path+uniqueidentifier, img)

This way there is no need to track a specific label for each image. They are just grouped together in six folders, so no more data loss by forcing images to be strings.

Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
0

Your solution works as each image has only one label. If you need multiple labels per file, you would have to place the same image in multiple folders which can quickly eat up disk space.

As an alternative, you could create a database that associates labels with the path of an image file. Then you can still save images directly as .png, .jpeg, etc. and have labels associated with them.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268