0

I am doing some preprocessing, and after I am trying to open a file that has floats within the file name but I get a ValueError: invalid literal for int() with base 10: 'output_patch_fp' and I do not know why. Can anyone help? Code is below:

    out_blob = next(iter(net.outputs))
net.batch_size = len(args.input)

# Read and pre-process input images
n, c, h, w = net.inputs[input_blob].shape
images = np.ndarray(shape=(n, c, h, w))
for i in range(n):
    image = cv2.imread(args.input[i])
    if image.shape[:-1] != (h, w):
        log.warning("Image {} is resized from {} to {}".format(args.input[i], image.shape[:-1], (h, w)))
        image = cv2.resize(image, (w, h))
    # Swapping Red and Blue channels 
    #image[:, :, [0, 2]] = image[:, :, [2, 0]]
    # Change data layout from HWC to CHW
    image = image.transpose((2, 0, 1))  
    images[i] = image
    
    eoim = image
    eoim16 = eoim.astype(np.float16)
    
    # divide by 255 to get value in range 0->1 if necessary (depends on input pixel format)
    if(eoim16.max()>1.0):
        eoim16 = np.divide(eoim16,255)
        print(eoim16)
        
preprocessed_image_path = 'C:/Users/Owner/Desktop/Ubotica/IOD/cloud_detect/'
formated_image_file = "output_patch_fp"
f = open(preprocessed_image_path + "/" + formated_image_file + ".txt", 'r')
elem_counter = 0
for elem in eoim16:
    for elem1 in elem:
        for col in elem1:
            #f.read(int(float(formated_image_file)))
            f.read(float(f.readline())
f.close()
        
# calling the instance method using the object cloudDetector
res = cloudDetector.infer(eoim16)
res = res[out_blob]

labels_map = None
classid_str = "classid"
probability_str = "probability"
for i, probs in enumerate(res):
    probs = np.squeeze(probs)
    top_ind = np.argsort(probs)[-args.number_top:][::-1]
    print("Image {}\n".format(args.input[i]))
    print(classid_str, probability_str)
    print("{} {}".format('-' * len(classid_str), '-' * len(probability_str)))
    for id in top_ind:
        det_label = labels_map[id] if labels_map else "{}".format(id)
        label_length = len(det_label)
        space_num_before = (len(classid_str) - label_length) // 2
        space_num_after = len(classid_str) - (space_num_before + label_length) + 2
        space_num_before_prob = (len(probability_str) - len(str(probs[id]))) // 2
        print("{}{}{}{}{:.7f}".format(' ' * space_num_before, det_label,
                                      ' ' * space_num_after, ' ' * space_num_before_prob,
                                      probs[id]))
    print("\n")

The .txt file with the raw float data is followed below:

0.56494140625
0.521484375
0.56884765625
0.537109375
0.57666015625
0.64306640625
0.7841796875
0.7021484375
0.6982421875
0.6943359375
0.67431640625
0.7490234375
0.61181640625
0.65869140625
Tristan
  • 1
  • 2
  • 2
    What are you expecting `int(formated_image_file)` to return? `formated_image_file` is the string `output_patch_fp`, not a number. – Barmar Jul 22 '20 at 21:24
  • Why are you calling `f.read()` and not assigning the result anywhere? – Barmar Jul 22 '20 at 21:25
  • The question mentions floats within the filename. Where is that in your code? – Barmar Jul 22 '20 at 21:27
  • I have a separate file that receives an image and has inference is done on it (which will get written into the file) which will be the float values. This code seen above takes that file where the image was loaded and reads all of the float values which will then be passed into a result array and hae some classid probabiltiy done on it. – Tristan Jul 22 '20 at 21:33
  • But when I try to open and read the file that was created within a sperate code I get the value error – Tristan Jul 22 '20 at 21:34
  • The error is coming from `f.read(int(formated_image_file))`. The argument to `f.read()` is the number of bytes to read. But you did `formated_image_file = "output_patch_fp"`, so that doesn't look like a number. Why are you trying to use that as the number of bytes? – Barmar Jul 22 '20 at 21:37
  • The question says "floats within the filename". I think you mean it has floats in the data. – Barmar Jul 22 '20 at 21:38
  • I suspect what you really want is `float(f.readline())` – Barmar Jul 22 '20 at 21:39
  • ahh yes, sorry floats in the data, I am new to programming so this is all a little confusing. – Tristan Jul 22 '20 at 21:40
  • So I would use readline basically to read each float value within the file that I created – Tristan Jul 22 '20 at 21:40
  • If each float is on a different line in the file, yes. It would help if you showed a sample file, and explained that the expected result will be. – Barmar Jul 22 '20 at 21:41
  • Within the file output_patch_fp I have this 0.56494140625 0.521484375 0.56884765625 0.537109375 0.57666015625 0.64306640625 0.7841796875 0.7021484375 0.6982421875 0.6943359375 0.67431640625 0.7490234375 0.61181640625 0.65869140625 0.494140625 0.525390625 0.56494140625 0.38037109375 0.337158203125 0.423583984375 0.34521484375 0.2235107421875 0.294189453125 (of course all on new lines) and the expected result classid probabilities should match these numbers once the file is read and processed through my infrencer code – Tristan Jul 22 '20 at 21:45
  • Put it in the question so we can see the proper formatting. I can't tell where the newlines are in a comment. – Barmar Jul 22 '20 at 21:46
  • I just added it within the question – Tristan Jul 22 '20 at 21:53
  • `float(f.readline())` should work. Of course, you need to do something with the result, such as append it to a list, assign it to a list element, etc. – Barmar Jul 22 '20 at 22:12
  • Thank you ! Got it ! – Tristan Jul 22 '20 at 23:53

1 Answers1

0

It's not necessary to call f.read(). f.readline() read the line from the file, and float() converts it to a float. The code should be something like this:

for elem in eoim16:
    for elem1 in elem:
        for col in elem1:
            val = float(f.readline())
            # now do something with val
Barmar
  • 741,623
  • 53
  • 500
  • 612