Esempio n. 1
0
 def predict(self, img_list=[], doubles_tol=0, edge_tol=0):
     '''
     output:
     predictions: list of features
     n images => n lists of features
     '''
     crop_px = self.estimator.pixels
     yolo_predictions = self.localizer.predict(img_list=img_list)
     yolo_predictions = nodoubles(yolo_predictions, tol=doubles_tol)
     (imcols, imrows, channels) = img_list[0].shape
     old_shape = (imrows, imcols)
     yolo_predictions = no_edges(yolo_predictions,
                                 tol=edge_tol,
                                 image_shape=old_shape)
     out_features, est_images, scales = crop_feature(
         img_list=img_list, xy_preds=yolo_predictions, new_shape=crop_px)
     structure = list(map(len, out_features))
     char_predictions = self.estimator.predict(img_list=est_images,
                                               scale_list=scales)
     zpop = char_predictions['z_p']
     apop = char_predictions['a_p']
     npop = char_predictions['n_p']
     for framenum in range(len(structure)):
         listlen = structure[framenum]
         frame = out_features[framenum]
         index = 0
         while listlen > index:
             feature = frame[index]
             feature.model.particle.z_p = zpop.pop(0)
             feature.model.particle.a_p = apop.pop(0)
             feature.model.particle.n_p = npop.pop(0)
             feature.model.instrument = self.instrument
             index += 1
     return out_features
Esempio n. 2
0
savedict = []

#just do one at a time for now
for i in range(numimgs):
    filepath = path + '/image' + str(i).zfill(4) + '.png'
    localim = cv2.imread(filepath)
    localpreds = [x for x in MLpreds if x['framenum'] == i]
    #reformat for cropping
    for pred in localpreds:
        localxy = {"conf": 1}
        x_p = pred['x_p']
        y_p = pred['y_p']
        ext = pred['shape'][0]
        localxy["bbox"] = [x_p, y_p, ext, ext]
        features, _, _ = crop_feature(img_list=[localim], xy_preds=[[localxy]])
        #instatiates a feature, puts in data, coords, x_p, y_p
        if len(features[0]) != 1:
            print('Something went wrong')
            print(len(features[0]))
        feature = features[0][0]
        feature.deserialize(pred)  #puts in rest of feature info
        start = time()
        result = feature.optimize(method='amoeba-lm')
        print("Time to fit: {:03f}".format(time() - start))
        print(result)
        localdict = feature.serialize(exclude=['data'])
        localdict['framenum'] = i
        localdict['framepath'] = os.path.abspath(filepath)
        localdict['redchi'] = result.redchi
        savedict.append(localdict)
Esempio n. 3
0
 def report_feature(self, conditions, predtype, crop_dir=None):
     self.do_omit()
     if predtype == 'ML':
         predictions = [
             x for x in self.ML_preds for cond in conditions
             if np.all(cond(x))
         ]
     elif predtype == 'refined':
         predictions = [
             x for x in self.refined_preds for cond in conditions
             if np.all(cond(x))
         ]
     else:
         print('Invalid predictions type')
     for pred in predictions:
         localim = cv2.imread(pred['framepath'])
         shape = pred['shape']
         localxy = {"conf": 1}
         x_p = pred['x_p']
         y_p = pred['y_p']
         ext = shape[0]
         localxy["bbox"] = [x_p, y_p, ext, ext]
         features, _, _ = crop_feature(img_list=[localim],
                                       xy_preds=[[localxy]])
         feature = features[0][0]
         feature.deserialize(pred)
         h = feature.model.hologram()
         res = feature.residuals()
         print(pred)
         fig, axes = plt.subplots(1, 3)
         for ax in axes:
             ax.set_xticks([])
             ax.set_yticks([])
             ax.xaxis.set_label_position('top')
         (ax1, ax2, ax3) = axes
         cropped_data = np.clip(feature.data.reshape(shape) * 100, 0, 255)
         if crop_dir is not None:
             bad_name = True
             counter = 0
             while bad_name:
                 crop_path = os.path.abspath(
                     crop_dir) + '/frame{}_img{}.png'.format(
                         pred['framenum'], counter)
                 if not os.path.isfile(crop_path):
                     bad_name = False
                 else:
                     counter += 1
             cv2.imwrite(crop_path, cropped_data)
             print(crop_path)
         ax1.imshow(cropped_data, cmap='gray')
         ax2.imshow(np.clip(h.reshape(shape) * 100, 0, 255), cmap='gray')
         ax3.imshow(res.reshape(shape), cmap='gray')
         ax1.set_xlabel('Data')
         ax2.set_xlabel('Predicted Hologram')
         ax3.set_xlabel('Residual')
         z_str = '%.3f' % pred['z_p']
         a_str = '%.3f' % pred['a_p']
         n_str = '%.3f' % pred['n_p']
         fig.suptitle('Frame {}'.format(pred['framenum']))
         fig.text(0.5,
                  0.2,
                  'z_p = {}px \n a_p = {}um \n n_p = {}'.format(
                      z_str, a_str, n_str),
                  horizontalalignment='center')
         plt.show()