Home > other >  How to solve? ValueError: always convert a float NaN to integer
How to solve? ValueError: always convert a float NaN to integer

Time:11-22

After running the anchor generated script error, can you tell me how to modify the code won't be an error, thank you
 
# - * - coding: utf-8 - * -
The import numpy as np
Import the random
The import argparse
The import OS
# parameter name
Parser=argparse. ArgumentParser (description='use the script to generate YOLO - V3 anchor boxes \ n')
Parser. Add_argument (' - input_annotation_txt_dir, required=True, type=STR, help='files')
Parser. Add_argument (' - output_anchors_txt, required=True, type=STR, help='files')
Parser. Add_argument (' - input_num_anchors, required=True, the default=6, type=int, help='anchor number')
Parser. Add_argument (' - input_cfg_width, required=True, type=int, help="wide")
Parser. Add_argument (' - input_cfg_height, required=True, type=int, help="high")
Args=parser. Parse_args ()
"'
Dot size is numx2 centroids clustering, type is ndarray
Annotation_array one of the bubbles in
"'
Def IOU (annotation_array centroids) :
#
Similarities=[]
# one of the bubbles in
W, h=annotation_array
For centroid centroids in:
C_w, c_h=centroid
If c_w & gt;=w and c_h & gt;=h: # 1
the caseSimilarity=h/w * (c_w * c_h)
Elif c_w & gt;=w and c_h & lt;=h: the # 2 is
Similarity=w * c_h/(w * h + (c_w - w) * c_h)
Elif c_w & lt;=w and c_h & gt;=h: # 3 case
Similarity=c_w * h/(w * h + (c_h - h) * c_w)
Else: # 3 case
Similarity=(c_w * c_h)/(w * h)
Similarities. Append (similarity)
# converts list ndarray
Return np. Array (similarities, np. Float32) # returns the one dimensional array, size of (num,)

"'
K_means: k-means clustering
Annotations_array all labelling frame's width is high, N the bubbles, the size is Nx2, type is ndarray
Dot size is numx2 centroids clustering, type is ndarray
"'
Def k_means (annotations_array, centroids, eps=0.00005, the iterations=2) :
#
N=annotations_array. Shape [0] # C=2
Num=centroids. Shape [0]
# loss function
Distance_sum_pre=1
Assignments_pre=1 * np. 'ones (dtype=N, np, int64)
#
The iteration=0
# circulation processing
While (True) :
#
The iteration +=1
#
Distances=[]
A label # loop calculation per box with all the clustering point distance (IOU)
For I in range (N) :
Short=1 - IOU (annotations_array [I], centroids)
Distances. Append (short)
# list into ndarray
Distances_array=np. Array (distances, np. Float32) # this ndarray size for Nxnum
# to find a label for each frame to the current clustering points recent
Assignments=np. Argmin (distances_array, axis=1) # calculate each line of the location of the minimum value index
# the sum of calculating distance, equivalent to the loss of the k-means clustering function
Distances_sum=np. Sum (distances_array)
# computing new clustering point
Centroid_sums=np. Zeros (centroids. Shape, np. Float32)
For I in range (N) :
Centroid_sums [assignments [I]] +=annotations_array [I] # calculation belong to every clustering categories and
For j in range (num) :
[j]/[j]=centroid_sums centroids (np) sum (assignments==j))
Twice the distance change before and after #
Diff=abs (distances_sum - distance_sum_pre)
# print the results
Print (" iteration: {}, short: {}, diff: {}, avg_IOU: {} \ n ". The format (iteration, distances_sum, diff, np. The sum (1 - distances_array)/(n * num)))
Three out of the while loop: # 1: loop 20000 times, 2: eps to calculate the average distance is small 3:
the condition of the aboveIf (assignments==assignments_pre.) all () :
Print (" according to the clustering results of the first and the second is the same end of the cycle \ n ")
Break
If the diff & lt; Eps:
Print (" according to the end of the eps cycle \ n ")
Break
If the iteration & gt; Iterations:
Print (" according to the number of iterations over cycle \ n ")
Break
# record last iteration
Distance_sum_pre=distances_sum
Assignments_pre=assignments. The copy ()
If __name__=="__main__ ':
# the number of clustering points, anchor the number of boxes
Num_clusters=args. Input_num_anchors
# index each indicate the name of the file from the folder (. TXT)
Names=OS. Listdir (args. Input_annotation_txt_dir)
# annotation of the frame's width and height
Annotations_w_h=[]
For the name names in:
Txt_path=OS. Path. Join (args. Input_annotation_txt_dir, name)
# read each row in a TXT file
F=open (txt_path, 'r')
For the line in f.r eadlines () :
The line=line. Rstrip (' \ n ')
W, h=line. The split (')/3: # then read w, h is string type
# eval () function is used to convert a string to numeric
Annotations_w_h. Append ((eval (w), eval (h)))
F. lose ()
# converts list annotations_w_h numpy array of size is (N, 2), N for how many boxes
Annotations_array=np. Array (annotations_w_h, dtype=np. Float32)
N=annotations_array. Shape [0]
# for k means clustering, random initialization of clustering points
Random_indices=[random randrange (N) for I in range (num_clusters)] # generate a random number
Centroids=annotations_array [random_indices]
- # k means clustering
K_means (annotations_array, centroids, 0.00005, 2)
# the centroids sorted by wide and written to the file
Widths=centroids [:, 0]
Sorted_indices=np. Argsort (widths)
Anchors=centroids [sorted_indices]
# to write the anchor into the file and save the
F_anchors=open (args. Output_anchors_txt, 'w')
#
For the anchor in anchors:
F_anchors. Write (' % d, % d % (int (anchor [0] * args input_cfg_width), int (anchor [1] * args input_cfg_height)))

F_anchors. Write (' \ n ')


Will be run out error
Traceback (the most recent call last) :
The File "anchor. Py", line 123, in & lt; module>
F_anchors. Write (' % d, % d % (int (anchor [0] * args input_cfg_width), int (anchor [1] * args input_cfg_height)))
ValueError: always convert a float NaN to integer
Seek advice, how to change won't error
  • Related