def run_images(self, images, cutoff=0.05): """ Run the neural net on a batch of input images. Inputs: images: batch of pictures as a [b, X, Y, C] numpy array. values between 0 and 255 (uint8 range) cutoff: value to clip boxes at. Returns: Array of BoundingBox objects representing the found boxes. """ k = p.ANCHOR_COUNT gs = p.GRID_SIZE batch_size = 1 start_time = time.time() activations, deltas, gammas, classes, chosen_anchor = \ self.sess.run(self.all_out, feed_dict={self.inp_batch: images}) # , # self.do: 1.0}) print('Took %f seconds!' % (time.time() - start_time)) gammas = np.reshape(gammas, [-1, gs**2 * k]) chosen_anchor = np.reshape(chosen_anchor, [-1, gs**2]) deltas = np.reshape(deltas, [-1, gs**2 * k, 4]) classes = np.reshape(classes, [-1, gs**2 * k, p.OUT_CLASSES]) class_numbers = np.argmax(classes, axis=2) box_list = [] anchors = u.create_anchors(gs) for ib in range(batch_size): boxes = u.delta_to_box(deltas[ib], anchors) nms_indices = tf.image.non_max_suppression(u.trans_boxes(boxes), gammas[ib], 5, iou_threshold=0.0) \ .eval(session=self.sess) selected_boxes = boxes[nms_indices] selected_gamma = gammas[ib, nms_indices] selected_class = class_numbers[ib, nms_indices] selected_class_scores = classes[ib, nms_indices] for i, box in enumerate(selected_boxes): sm_scores = u.softmax(selected_class_scores[i]) conf = selected_gamma[i] * sm_scores[selected_class[i]] if conf > cutoff: print(conf) box_list.append( BoundingBox(u.trans_boxes(box), conf, selected_class[i])) return box_list
k = p.ANCHOR_COUNT gs = p.GRID_SIZE #print(gammas) gammas = np.reshape(gammas, [-1, gs**2*k]) chosen_anchor = np.reshape(chosen_anchor,[-1,gs**2]) deltas = np.reshape(deltas, [-1, gs**2*k,4]) anchors = u.create_anchors(gs) classes = np.reshape(classes, [-1,gs**2*k, p.OUT_CLASSES]) class_numbers = np.argmax(classes, axis=2) for ib in range(batch_size): #pic = Image.fromarray(img[ib]) max_gamma= 0 for idx in range(gs**2): #print(np.shape(gammas)) ca = chosen_anchor[ib, idx] #print( idx+chosen_anchor[ib, idx]) #print(chosen_anchor.shape) if(gammas[ib,idx*k+ca] > 0.05): print('Anchor %i chosen at idx = %i for class %i with conf: %f'\ %(ca,idx,class_numbers[ib,idx*k+ca], gammas[ib,idx*k+ca])) box = u.delta_to_box(deltas[ib,ca+idx*k,:], anchors[ca+idx*k]) print(u.trans_boxes(box)) if(gammas[ib,idx*k+ca] > max_gamma): max_gamma = gammas[ib,idx*k+ca] print('BEST')