archive/runapp.py [338:559]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        data = bottle.request.json

        images_to_label = [im['id'] for im in data['images']]
        label_to_assign = data['label']

        # Use image ids in images_to_label to get the corresponding dataset indices
        indices_to_label = []
        indices_detection_ids = [dataset.samples[i][0] for i in indices]
        for im in images_to_label:
            pos = indices_detection_ids.index(im)
            ind = indices[pos]
            indices_to_label.append(ind)

        label_category_name = label_to_assign.lower()
        if label_category_name == 'empty':
            # Update records in dataset dataloader but not in the PostgreSQL database
            moveRecords(dataset, DetectionKind.ModelDetection.value, DetectionKind.UserDetection.value, indices_to_label)
            # numLabeled = len(dataset.set_indices[DetectionKind.UserDetection.value])
        else:
            # Get the category id for the assigned label
            existing_category_entries = {cat.name: cat.id for cat in Category.select()}
            try:
                label_category_id = existing_category_entries[label_category_name]
            except:
                print('The label was not found in the database Category table')
                raise NotImplementedError
            
            # Update entries in the PostgreSQL database
            ## get Detection table entries corresponding to the images being labeled 
            matching_detection_entries = (Detection
                        .select(Detection.id, Detection.category_id)
                        .where((Detection.id << images_to_label))) # << means IN
            ## update category_id, category_confidence, and kind of each Detection entry in the PostgreSQL database      
            for mde in matching_detection_entries:
                command = Detection.update(category_id=label_category_id, category_confidence=1, kind=DetectionKind.UserDetection.value).where(Detection.id == mde.id)
                command.execute()
            
            # Update records in dataset dataloader
            for il in indices_to_label:
                sample_data = list(dataset.samples[il])
                sample_data[1] = label_category_id
                sample_data[2] = DetectionKind.UserDetection.value
                sample_data[3] = 1
                dataset.samples[il] = tuple(sample_data)
            moveRecords(dataset, DetectionKind.ModelDetection.value, DetectionKind.UserDetection.value, indices_to_label)
            # print(set(dataset.set_indices[4]).update(set(indices_to_label)))
            dataset.set_indices[4] = list(set(dataset.set_indices[4]).union(set(indices_to_label))) # add the index to the set of labeled/confirmed indices
            # numLabeled = len(dataset.set_indices[DetectionKind.UserDetection.value])
            print([len(x) for x in dataset.set_indices])

        bottle.response.content_type = 'application/json'
        bottle.response.status = 200
        return json.dumps(data)

    @webUIapp.route('/confirmPredictedLabel', method='POST')
    def confirm_predicted_label():
        global dataset
        global indices

        data = bottle.request.json

        image_to_label = data['image']
        label_to_assign = data['label']
        
        # Use image id images_to_label to get the corresponding dataset index
        indices_detection_ids = [dataset.samples[i][0] for i in indices]
        pos = indices_detection_ids.index(image_to_label)
        index_to_label = indices[pos]

        label_category_name = label_to_assign.lower()
        if label_category_name == 'empty':
            # Update records in dataset dataloader but not in the PostgreSQL database
            moveRecords(dataset, DetectionKind.ModelDetection.value, DetectionKind.ConfirmedDetection.value, [index_to_label])
            # numLabeled = len(dataset.set_indices[DetectionKind.UserDetection.value]) # userdetection + confirmed detection?
        else:
            # Get the category id for the assigned label
            existing_category_entries = {cat.name: cat.id for cat in Category.select()}
            try:
                label_category_id = existing_category_entries[label_category_name]
            except:
                print('The label was not found in the database Category table')
                raise NotImplementedError
            
            # Update entries in the PostgreSQL database
            ## get Detection table entries corresponding to the images being labeled 
            matching_detection_entries = (Detection
                        .select(Detection.id, Detection.category_id)
                        .where((Detection.id==image_to_label))) # << means IN
            ## update category_id, category_confidence, and kind of each Detection entry in the PostgreSQL database      
            mde = matching_detection_entries.get()
            command = Detection.update(category_id=label_category_id, category_confidence=1, kind=DetectionKind.ConfirmedDetection.value).where(Detection.id == mde.id)
            command.execute()
            
            # Update records in dataset dataloader
            sample_data = list(dataset.samples[index_to_label])
            sample_data[1] = label_category_id
            sample_data[2] = DetectionKind.ConfirmedDetection.value
            sample_data[3] = 1
            dataset.samples[index_to_label] = tuple(sample_data)
            moveRecords(dataset, DetectionKind.ModelDetection.value, DetectionKind.ConfirmedDetection.value, [index_to_label])
            dataset.set_indices[4] = list(set(dataset.set_indices[4]).union({index_to_label})) # add the index to the set of labeled/confirmed indices
            # numLabeled = len(dataset.set_indices[DetectionKind.UserDetection.value])
            print([len(x) for x in dataset.set_indices])
        
        bottle.response.content_type = 'application/json'
        bottle.response.status = 200
        return json.dumps(data)
    
    @webUIapp.route('/trainClassifier', method='POST')
    def train_classifier():
        global dataset
        global kwargs
        global sampler
        global classifier_trained
        global X_pred
        global y_pred

        data = bottle.request.json

        # Train on samples that have been labeled so far
        # dataset.set_kind(DetectionKind.UserDetection.value)
        dataset.set_kind(4)
        print(dataset.current_set)
        print(type(dataset.current_set))
        X_train = dataset.em[dataset.current_set]
        y_train = np.asarray(dataset.getlabels())
        # print(y_train)
        timer = time.time()
        kwargs["model"].fit(X_train, y_train)
        print('Training took %0.2f seconds'%(time.time() - timer))

        timer = time.time()
        joblib.dump(kwargs["model"], "%s/%s_%04d.skmodel"%(args.checkpoint_dir, 'classifier', len(dataset.current_set)))
        print('Saving classifier checkpoint took %0.2f seconds'%(time.time() - timer))

        
        # Predict on the samples that have not been labeled
        timer = time.time()
        dataset.set_kind(DetectionKind.ModelDetection.value)
        X_pred = dataset.em[dataset.current_set]
        y_pred = kwargs["model"].predict(X_pred)
        print('Predicting on unlabeled samples took %0.2f seconds'%(time.time() - timer))
        # print(y_pred)

        # Update model predicted class in PostgreSQL database
        # timer = time.time()
        # for pos in range(len(y_pred)):
        #     idx = dataset.current_set[pos]
        #     det_id = dataset.samples[idx][0]
        #     matching_detection_entries = (Detection
        #                                 .select(Detection.id, Detection.category_id)
        #                                 .where((Detection.id == det_id)))
        #     mde = matching_detection_entries.get()
        #     command = Detection.update(category_id=y_pred[pos]).where(Detection.id == mde.id)
        #     command.execute()
        # print('Updating the database took %0.2f seconds'%(time.time() - timer))

        # Alternative: batch update PostgreSQL database
        # timer = time.time()
        # det_ids = [dataset.samples[dataset.current_set[pos]][0] for pos in range(len(y_pred))]
        # y_pred = [int(y) for y in y_pred]
        # det_id_pred_pairs = list(zip(det_ids, y_pred))
        # case_statement = Case(Detection.id, det_id_pred_pairs)
        # command = Detection.update(category_id=case_statement).where(Detection.id.in_(det_ids))
        # command.execute()
        # print('Updating the database the other way took %0.2f seconds'%(time.time() - timer))

        # Update dataset dataloader
        timer = time.time()
        for pos in range(len(y_pred)):
            idx = dataset.current_set[pos]
            sample_data = list(dataset.samples[idx])
            sample_data[1] = y_pred[pos]
            dataset.samples[idx] = tuple(sample_data)
        print('Updating the dataset dataloader took %0.2f seconds'%(time.time() - timer))
        
        if not classifier_trained:
            # once the classifier has been trained the first time, switch to AL sampling
            classifier_trained = True
            sampler = get_AL_sampler('confidence')(dataset.em, dataset.getalllabels(), 1234)

        bottle.response.content_type = 'application/json'
        bottle.response.status = 200
        return json.dumps(data)
    
    @webUIapp.route('/showFullsizeImage', method='POST')
    def show_fullsize_image():
        data = bottle.request.json
        
        image_src = data['img_src']
        
        matching_image_entries = (Image
                                .select(Image.file_name, Image.source_file_name)
                                .where((Image.file_name == image_src)))
        try:
            mie = matching_image_entries.get()
            data['success_status'] = True
            data['fullsize_src'] = mie.source_file_name
        except:
            data['success_status'] = False
        
        bottle.response.content_type = 'application/json'
        bottle.response.status = 200
        return json.dumps(data)
    
    @webUIapp.route('/getSequentialImages', method='POST')
    def get_sequential_images():
        data = bottle.request.json
        
        image_src = data['img_src']
        
        matching_image_entries = (Image
                                .select(Image.seq_id, Image.seq_num_frames, Image.frame_num)
                                .where((Image.file_name == image_src)))
        try:
            mie = matching_image_entries.get()
            if mie.seq_num_frames > 1:
                images_in_seq = (Image
                                .select(Image.source_file_name)
                                .where((Image.seq_id == mie.seq_id))
                                .order_by(Image.frame_num))
                image_sequence = sorted(list(set([i.source_file_name for i in images_in_seq])))
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



research/active_learning/labeling_tool/runapp.py [340:561]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        data = bottle.request.json

        images_to_label = [im['id'] for im in data['images']]
        label_to_assign = data['label']

        # Use image ids in images_to_label to get the corresponding dataset indices
        indices_to_label = []
        indices_detection_ids = [dataset.samples[i][0] for i in indices]
        for im in images_to_label:
            pos = indices_detection_ids.index(im)
            ind = indices[pos]
            indices_to_label.append(ind)

        label_category_name = label_to_assign.lower()
        if label_category_name == 'empty':
            # Update records in dataset dataloader but not in the PostgreSQL database
            moveRecords(dataset, DetectionKind.ModelDetection.value, DetectionKind.UserDetection.value, indices_to_label)
            # numLabeled = len(dataset.set_indices[DetectionKind.UserDetection.value])
        else:
            # Get the category id for the assigned label
            existing_category_entries = {cat.name: cat.id for cat in Category.select()}
            try:
                label_category_id = existing_category_entries[label_category_name]
            except:
                print('The label was not found in the database Category table')
                raise NotImplementedError
            
            # Update entries in the PostgreSQL database
            ## get Detection table entries corresponding to the images being labeled 
            matching_detection_entries = (Detection
                        .select(Detection.id, Detection.category_id)
                        .where((Detection.id << images_to_label))) # << means IN
            ## update category_id, category_confidence, and kind of each Detection entry in the PostgreSQL database      
            for mde in matching_detection_entries:
                command = Detection.update(category_id=label_category_id, category_confidence=1, kind=DetectionKind.UserDetection.value).where(Detection.id == mde.id)
                command.execute()
            
            # Update records in dataset dataloader
            for il in indices_to_label:
                sample_data = list(dataset.samples[il])
                sample_data[1] = label_category_id
                sample_data[2] = DetectionKind.UserDetection.value
                sample_data[3] = 1
                dataset.samples[il] = tuple(sample_data)
            moveRecords(dataset, DetectionKind.ModelDetection.value, DetectionKind.UserDetection.value, indices_to_label)
            # print(set(dataset.set_indices[4]).update(set(indices_to_label)))
            dataset.set_indices[4] = list(set(dataset.set_indices[4]).union(set(indices_to_label))) # add the index to the set of labeled/confirmed indices
            # numLabeled = len(dataset.set_indices[DetectionKind.UserDetection.value])
            print([len(x) for x in dataset.set_indices])

        bottle.response.content_type = 'application/json'
        bottle.response.status = 200
        return json.dumps(data)

    @webUIapp.route('/confirmPredictedLabel', method='POST')
    def confirm_predicted_label():
        global dataset
        global indices

        data = bottle.request.json

        image_to_label = data['image']
        label_to_assign = data['label']
        
        # Use image id images_to_label to get the corresponding dataset index
        indices_detection_ids = [dataset.samples[i][0] for i in indices]
        pos = indices_detection_ids.index(image_to_label)
        index_to_label = indices[pos]

        label_category_name = label_to_assign.lower()
        if label_category_name == 'empty':
            # Update records in dataset dataloader but not in the PostgreSQL database
            moveRecords(dataset, DetectionKind.ModelDetection.value, DetectionKind.ConfirmedDetection.value, [index_to_label])
            # numLabeled = len(dataset.set_indices[DetectionKind.UserDetection.value]) # userdetection + confirmed detection?
        else:
            # Get the category id for the assigned label
            existing_category_entries = {cat.name: cat.id for cat in Category.select()}
            try:
                label_category_id = existing_category_entries[label_category_name]
            except:
                print('The label was not found in the database Category table')
                raise NotImplementedError
            
            # Update entries in the PostgreSQL database
            ## get Detection table entries corresponding to the images being labeled 
            matching_detection_entries = (Detection
                        .select(Detection.id, Detection.category_id)
                        .where((Detection.id==image_to_label))) # << means IN
            ## update category_id, category_confidence, and kind of each Detection entry in the PostgreSQL database      
            mde = matching_detection_entries.get()
            command = Detection.update(category_id=label_category_id, category_confidence=1, kind=DetectionKind.ConfirmedDetection.value).where(Detection.id == mde.id)
            command.execute()
            
            # Update records in dataset dataloader
            sample_data = list(dataset.samples[index_to_label])
            sample_data[1] = label_category_id
            sample_data[2] = DetectionKind.ConfirmedDetection.value
            sample_data[3] = 1
            dataset.samples[index_to_label] = tuple(sample_data)
            moveRecords(dataset, DetectionKind.ModelDetection.value, DetectionKind.ConfirmedDetection.value, [index_to_label])
            dataset.set_indices[4] = list(set(dataset.set_indices[4]).union({index_to_label})) # add the index to the set of labeled/confirmed indices
            # numLabeled = len(dataset.set_indices[DetectionKind.UserDetection.value])
            print([len(x) for x in dataset.set_indices])
        
        bottle.response.content_type = 'application/json'
        bottle.response.status = 200
        return json.dumps(data)
    
    @webUIapp.route('/trainClassifier', method='POST')
    def train_classifier():
        global dataset
        global kwargs
        global sampler
        global classifier_trained
        global X_pred
        global y_pred

        data = bottle.request.json

        # Train on samples that have been labeled so far
        # dataset.set_kind(DetectionKind.UserDetection.value)
        dataset.set_kind(4)
        print(dataset.current_set)
        print(type(dataset.current_set))
        X_train = dataset.em[dataset.current_set]
        y_train = np.asarray(dataset.getlabels())
        # print(y_train)
        timer = time.time()
        kwargs["model"].fit(X_train, y_train)
        print('Training took %0.2f seconds'%(time.time() - timer))

        timer = time.time()
        joblib.dump(kwargs["model"], "%s/%s_%04d.skmodel"%(args.checkpoint_dir, 'classifier', len(dataset.current_set)))
        print('Saving classifier checkpoint took %0.2f seconds'%(time.time() - timer))

        
        # Predict on the samples that have not been labeled
        timer = time.time()
        dataset.set_kind(DetectionKind.ModelDetection.value)
        X_pred = dataset.em[dataset.current_set]
        y_pred = kwargs["model"].predict(X_pred)
        print('Predicting on unlabeled samples took %0.2f seconds'%(time.time() - timer))
        # print(y_pred)

        # Update model predicted class in PostgreSQL database
        # timer = time.time()
        # for pos in range(len(y_pred)):
        #     idx = dataset.current_set[pos]
        #     det_id = dataset.samples[idx][0]
        #     matching_detection_entries = (Detection
        #                                 .select(Detection.id, Detection.category_id)
        #                                 .where((Detection.id == det_id)))
        #     mde = matching_detection_entries.get()
        #     command = Detection.update(category_id=y_pred[pos]).where(Detection.id == mde.id)
        #     command.execute()
        # print('Updating the database took %0.2f seconds'%(time.time() - timer))

        # Alternative: batch update PostgreSQL database
        # timer = time.time()
        # det_ids = [dataset.samples[dataset.current_set[pos]][0] for pos in range(len(y_pred))]
        # y_pred = [int(y) for y in y_pred]
        # det_id_pred_pairs = list(zip(det_ids, y_pred))
        # case_statement = Case(Detection.id, det_id_pred_pairs)
        # command = Detection.update(category_id=case_statement).where(Detection.id.in_(det_ids))
        # command.execute()
        # print('Updating the database the other way took %0.2f seconds'%(time.time() - timer))

        # Update dataset dataloader
        timer = time.time()
        for pos in range(len(y_pred)):
            idx = dataset.current_set[pos]
            sample_data = list(dataset.samples[idx])
            sample_data[1] = y_pred[pos]
            dataset.samples[idx] = tuple(sample_data)
        print('Updating the dataset dataloader took %0.2f seconds'%(time.time() - timer))
        
        if not classifier_trained:
            # once the classifier has been trained the first time, switch to AL sampling
            classifier_trained = True
            sampler = get_AL_sampler('confidence')(dataset.em, dataset.getalllabels(), 1234)

        bottle.response.content_type = 'application/json'
        bottle.response.status = 200
        return json.dumps(data)
    
    @webUIapp.route('/showFullsizeImage', method='POST')
    def show_fullsize_image():
        data = bottle.request.json
        
        image_src = data['img_src']
        
        matching_image_entries = (Image
                                .select(Image.file_name, Image.source_file_name)
                                .where((Image.file_name == image_src)))
        try:
            mie = matching_image_entries.get()
            data['success_status'] = True
            data['fullsize_src'] = mie.source_file_name
        except:
            data['success_status'] = False
        
        bottle.response.content_type = 'application/json'
        bottle.response.status = 200
        return json.dumps(data)
    
    @webUIapp.route('/getSequentialImages', method='POST')
    def get_sequential_images():
        data = bottle.request.json
        
        image_src = data['img_src']
        
        matching_image_entries = (Image
                                .select(Image.seq_id, Image.seq_num_frames, Image.frame_num)
                                .where((Image.file_name == image_src)))
        try:
            mie = matching_image_entries.get()
            if mie.seq_num_frames > 1:
                images_in_seq = (Image
                                .select(Image.source_file_name)
                                .where((Image.seq_id == mie.seq_id))
                                .order_by(Image.frame_num))
                image_sequence = sorted(list(set([i.source_file_name for i in images_in_seq])))
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



