Ejemplo n.º 1
0
 def get_options():
     return {
         'architectures': list(ModelBuilder.ARCHITECTURES.keys()),
         'datasets': list(ModelBuilder.DATASETS.keys()),
         'losses': list(ModelBuilder.LOSSES.keys()),
         'optimizers': list(ModelBuilder.OPTIMIZERS.keys()),
         'clusters': ClusterRegistry.keys(),
     }
Ejemplo n.º 2
0
 def run():
     try:
         if not flask.request.is_json:
             response = flask.Response()
             response.status_code = 400
             response.status = 'Error: Not JSON'
             return response
         request_data = flask.request.get_json()
         directory = request_data['url']
         settings = Settings()
         settings.__dict__.update(request_data)
         # if not settings.exists():
         #     settings.save()
         # else:
         #     settings.load()
         cluster = ClusterRegistry.get(settings.cluster)
         model = ModelBuilder.create(
             settings.architecture,
             settings.dataset,
             settings.loss,
             settings.optimizer,
             settings.metrics,
             settings.epochs,
             settings.patience,
         )
         split = model.split(settings.split)
         results = main(directory, cluster, settings.clusterArgs, split)
         results = flask.jsonify(results)
         return results
     except TrainingIncompleteException:
         response = flask.Response()
         response.status_code = 400
         response.status = 'Error: Training incomplete'
         return response
     except ModelStateMissingError:
         response = flask.Response()
         response.status_code = 400
         response.status = 'Error: Model state missing'
         return response
     except BadModelSettings:
         response = flask.Response()
         response.status_code = 400
         response.status = 'Error: Incompatible architecture/dataset'
         return response
     except ClusterRegistryNameError:
         response = flask.Response()
         response.status_code = 400
         response.status = 'Error: Bad cluster name'
         return response
     except:
         print_exc()
         response = flask.Response()
         response.status_code = 500
         response.status = 'Error: Unknown'
         return response
Ejemplo n.º 3
0
 def cluster_algorithms():
     results = list()
     for name, algorithm in ClusterRegistry.items():
         parameters = function_signature(algorithm.run)
         if 'images' in parameters:
             del parameters['images']
         results.append({
             'name': name,
             'parameters': parameters,
         })
     return flask.jsonify(results)
Ejemplo n.º 4
0
 def cluster():
     try:
         if not flask.request.is_json:
             response = flask.Response()
             response.status_code = 400
             response.status = 'Error: Not JSON'
             return response
         settings = flask.request.get_json()
         images = ImageDirectory(settings['directory']).jpeg(False)
         cluster = ClusterRegistry.get(settings['cluster'])
         results = cluster.run_cached(images, **settings['args']).urls()
         results = flask.jsonify(results)
         return results
     except ClusterRegistryNameError:
         response = flask.Response()
         response.status_code = 400
         response.status = 'Error: Unknown clustering algorithm'
         return response
     except:
         print_exc()
         response = flask.Response()
         response.status_code = 500
         response.status = 'Error: Unknown'
         return response
Ejemplo n.º 5
0

class HistogramCluster(ClusterStrategy):
    """
    """

    def run(
        self,
        images: List[Url],
        hue_bins: int = 180,
        saturation_bins: int = 256,
        value_bins: int = 256,
        bandwidth: Optional[float] = None,
    ) -> ClusterResults:
        """
        Clusters images.
        """
        c = list()
        for i, img in enumerate(images):
            print("HISTOGRAM: %i / %i" % (i, len(images)))
            hh = HsvHistogram(img)
            histogram = HsvHistogram.scale(hh.hsv(hue_bins, saturation_bins, value_bins), hh.size())
            c.append(histogram)
        d = vstack(c)
        print('CLUSTER: Mean Shift')
        cluster = MeanShift(bandwidth=bandwidth).fit_predict(d).tolist()
        return ClusterResults(images, cluster)


ClusterRegistry.add('histogram', HistogramCluster())
Ejemplo n.º 6
0
                if similarity_metric == SimilarityMetric.INVERSE_DISTANCE:
                    inverse_distance = 0
                    for k in good:
                        inverse_distance += 1 - k.distance
                    if len(good) > 0:
                        matrix[i][j] = inverse_distance / len(good)
                        matrix[j][i] = inverse_distance / len(good)
                elif similarity_metric == SimilarityMetric.COUNT:
                    matrix[i][j] = len(good)
                    matrix[j][i] = len(good)
            else:
                if similarity_metric == SimilarityMetric.INVERSE_DISTANCE:
                    matrix[i][i] = 1
                elif similarity_metric == SimilarityMetric.COUNT:
                    matrix[i][i] = nfeatures
        print('CLUSTER: AffinityPropagation')
        cluster = AffinityPropagation(
            damping=damping,
            max_iter=max_iter,
            convergence_iter=convergence_iter,
            affinity=affinity.value,
            random_state=0,
        ).fit_predict(matrix).tolist()
        return ClusterResults(images, cluster)


ClusterRegistry.add('sift', SiftCluster(Similarity1()))
ClusterRegistry.add('sift2', SiftCluster(Similarity2()))
ClusterRegistry.add('sift3', SiftCluster(Similarity3()))
ClusterRegistry.add('sift4', SiftCluster2())
            damping=damping,
            max_iter=max_iter,
            convergence_iter=convergence_iter,
            affinity=affinity,
            descriptor_matcher=descriptor_matcher,
        )
        results2 = HistogramCluster().run_cached(
            images,
            hue_bins=hue_bins,
            saturation_bins=saturation_bins,
            value_bins=value_bins,
            bandwidth=bandwidth,
        )
        labels1 = results1.labels()
        labels2 = results2.labels()
        k1 = results1.k()
        cluster = [
            self.combine(c1, c2, k1) for c1, c2 in zip(labels1, labels2)
        ]
        self.remove_empty_clusters(cluster)
        return ClusterResults(images, cluster)


ClusterRegistry.add('hybrid1a', HybridCluster(Similarity1()))
ClusterRegistry.add('hybrid1b', HybridCluster(Similarity2()))
ClusterRegistry.add('hybrid1c', HybridCluster(Similarity3()))
ClusterRegistry.add('hybrid2a', HybridCluster2(Similarity1()))
ClusterRegistry.add('hybrid2b', HybridCluster2(Similarity2()))
ClusterRegistry.add('hybrid2c', HybridCluster2(Similarity3()))
ClusterRegistry.add('hybrid3', HybridCluster3())