def test_identification_banana_vs_bowl_vs_food_can(self): """ Try to identify with 3 categories. """ # Getting the dataset bowl_ids = ['fa61e604661d4aa66658ecd96794a1cd', 'f74bba9a22e044dea3769fcd5f96f4', 'd2e1dc9ee02834c71621c7edb823fc53'] banana_ids = ['f6e6117261dca163713c042b393cc65b', 'ba0d56295321002718ddbf38fa69c501', '7d78e217e0ba160fe2b248b8bb97d290'] bowls = [] for bowl_id in bowl_ids: bowls.append(SketchupModel.find_google_id(bowl_id)) bananas = [] for banana_id in banana_ids: bananas.append(SketchupModel.find_google_id(banana_id)) # Training iden = Identifier() iden.add_models(bananas, 'banana') iden.add_models(bowls, 'bowl') iden.train() # Identification for i in range(20): example = Example.get_random(['banana', 'bowl']) pcd_file = example.pcd_file() print "Identification of file {}".format(example) cloud = PointCloud.load_pcd(pcd_file.name) iden.identify(cloud)
def setUp(self): test_model = SketchupModel() test_model.google_id = "test1" test_model.tags = ["tag1", "tag2"] test_model.title = "title1" test_model.text = "Description of 'title1' SketchupModel." test_model.mesh = file("sketchup_models/fixtures/mesh_can.tri").read() test_model.save() self.test_model = SketchupModel.find_google_id("test1")
def test_google_id_is_unique(self): """ Test that the Field google is unique. """ count = SketchupModel.objects.filter(google_id='test_unique').count() self.assertEqual(count, 0) SketchupModel(google_id='test_unique').save() count = SketchupModel.objects.filter(google_id='test_unique').count() self.assertEqual(count, 1) SketchupModel(google_id='test_unique').save() count = SketchupModel.objects.filter(google_id='test_unique').count() self.assertEqual(count, 1)
def retreive_model(google_id): """ Return the SketchupModel corresponding to the google_id. """ json_data = api_get("GetEntity", id=google_id) try: model = SketchupModel.objects.get(google_id=google_id) except SketchupModel.DoesNotExist: model = SketchupModel() try: _parse_model_entry(model, json_data) model.save() return model except KeyError: return None
def setUp(self): """ Run before starting testing. """ # TODO : use fixtures test_model = SketchupModel() test_model.google_id = "test1" test_model.tags = ["tag1", "tag2"] test_model.title = "title1" test_model.text = "Description of 'title1' SketchupModel." test_model.mesh = file("sketchup_models/fixtures/mesh_can.tri").read() test_model.save() self.test_model = SketchupModel.find_google_id("test1") self.view = PartialView(model=self.test_model, theta=0.0, phi=0.0) self.distribution = ShapeDistribution.compute(self.view.pointcloud)
def perform_learning(self, options): """ Perfrom the learning from the dataset. """ # Retreiving the dataset models print 'Training using a {} classifier.'.format( options['classifier_type']) models = {} for category in self.dataset.keys(): models[category] = [] for google_id in self.dataset[category]: models[category].append( SketchupModel.find_google_id(google_id)) # Training classifier = self.classifiers_available[options['classifier_type']] self.identifier = Identifier(classifier=classifier) for category in models.keys(): self.identifier.add_models(models[category], category) # (x, y, w) = self.identifier._get_example_matrix() # import matplotlib.pyplot as plt # import numpy as np # # w = np.array(w) # # print y # # print w # plt.plot(y, w, 'ro') # print self.dataset.keys() # plt.show() self.identifier.train(options['entropy'])
def detailed_view(request, google_id): model = SketchupModel.find_google_id(google_id) print "model {} has {} views.".format(model, model.partialview_set.count()) if model is None: return HttpResponseNotFound() return render_to_response('model_detailed_view.html', {'model': model}, context_instance=RequestContext(request))
def test_exception_when_no_existing_category(self): """ Test that if the identifier is empty it throws. """ pointcloud = PointCloud.load_pcd("pointcloud/fixtures/cloud.pcd") identifier = Identifier() self.assertRaises(IndexError, identifier.identify, pointcloud) # training will add a category : model = SketchupModel() model.google_id = "test1" model.mesh = file("sketchup_models/fixtures/mesh_can.tri").read() identifier.add_models([model], "test_category") try: identifier.identify(pointcloud) except IndexError: self.fail("identifier.identify() raised IndexError unexpectedly!") except: # can raise if Indentification failed pass
def search_for_models(keywords): models = [] if keywords: model_ids = search_by_keywords(keywords) for model_id in model_ids: models.append(SketchupModel.find_google_id(model_id)) # sleep(0.100) return models
def model_mesh(request, google_id): try: model = SketchupModel.find_google_id(google_id) if model.mesh is None: return HttpResponseNotFound() response = HttpResponse(model.mesh, mimetype="text/plain") response['Content-Disposition'] = 'attachment; filename=%s.tri' % model return response except SketchupModel.DoesNotExist, SketchupModel.AttributeError: return HttpResponseNotFound()
def test_empty_pointcloud(self): """ The pointcloud is sometimes returned empty. It seems to be random and about 1/10 of the time We test here the stability. """ test_model = SketchupModel.find_google_id("fb01ac920e6b68fd7236c5b88164c0b8") number_of_attempt = 10 for i in range(number_of_attempt): print "Compute cloud {} on {}".format(i, number_of_attempt) # Generate the pointcloud PartialView.compute_all_views( test_model )
def test_empty_pointcloud(self): """ The pointcloud is sometimes returned empty. It seems to be random and about 1/10 of the time We test here the stability. """ test_model = SketchupModel.find_google_id( "fb01ac920e6b68fd7236c5b88164c0b8") number_of_attempt = 10 for i in range(number_of_attempt): print "Compute cloud {} on {}".format(i, number_of_attempt) # Generate the pointcloud PartialView.compute_all_views(test_model)
def search_by_keywords(keywords, create_models=False): """ Search the API for the keywords, return a list of model_ids. """ params = { "startRow": 1, "endRow": NUMBER_OF_RESULTS, "q": keywords, "type": "SKETCHUP_MODEL", "class": "entity", "Lk": True, } json_data = api_get("Search", **params) if create_models: models = [] for entry in json_data["entries"]: try: model = SketchupModel() _parse_model_entry(model, entry) model.save() models.append(model) except KeyError as exception: print exception return models else: return [entry["id"] for entry in json_data["entries"]]
def search_by_keywords(keywords, create_models=False): """ Search the API for the keywords, return a list of model_ids. """ params = { 'startRow': 1, 'endRow': NUMBER_OF_RESULTS, 'q': keywords, 'type': 'SKETCHUP_MODEL', 'class': 'entity', 'Lk': True } json_data = api_get('Search', **params) if create_models: models = [] for entry in json_data['entries']: try: model = SketchupModel() _parse_model_entry(model, entry) model.save() models.append(model) except KeyError as exception: print exception return models else: return [entry['id'] for entry in json_data['entries']]
def train_identifier(request, evaluation_session_id): """ Train the identifier then redirect to a new attempt. """ session = get_object_or_404(EvaluationSession, pk=evaluation_session_id) attempt_index = int(request.GET['identification_attempt_index']) attempt = session.attempts[attempt_index] category = request.GET['category'] if request.GET['google_ids']: model_ids = request.GET['google_ids'].split(",") else: model_ids = [] models = [SketchupModel.find_google_id(id) for id in model_ids] attempt.selected_model_ids = model_ids session.identifier.add_models(models, category) session.identifier.train() session.save() return redirect('/system/session/{}/attempt/new'.format(session.pk))
def _get_example_matrix(self, use_entropy): """ Return the input matrix of example (X). """ X = numpy.zeros([0, SHAPE_DISTRIBUTION_SIZE]) Y = numpy.array([]) W = numpy.array([]) # Weights of the samples for idx, (category, model_ids) in (enumerate( self.dict_categories.items())): for model_id in model_ids: model = SketchupModel.find_google_id(model_id) if model.partialview_set.count() == 0: PartialView.compute_all_views(model) (x, w) = self._get_model_example_matrix(model, use_entropy) y = numpy.array([idx for _ in range(x.shape[0])]) X = numpy.vstack([X, x]) Y = numpy.concatenate([Y, y]) W = numpy.concatenate([W, w]) return (X, Y, W)
def _get_example_matrix(self, use_entropy): """ Return the input matrix of example (X). """ X = numpy.zeros([0, SHAPE_DISTRIBUTION_SIZE]) Y = numpy.array([]) W = numpy.array([]) # Weights of the samples for idx, (category, model_ids) in (enumerate(self.dict_categories.items())): for model_id in model_ids: model = SketchupModel.find_google_id(model_id) if model.partialview_set.count() == 0: PartialView.compute_all_views(model) (x, w) = self._get_model_example_matrix(model, use_entropy) y = numpy.array([idx for _ in range(x.shape[0])]) X = numpy.vstack([X, x]) Y = numpy.concatenate([Y, y]) W = numpy.concatenate([W, w]) return (X, Y, W)
def test_mesh_attribute_can_be_used_as_a_string(self): """ As mesh is store in a gridfsfield, the attribute mesh would normally be returned as a GridFSOut object. The model has been modified so that it is a string instead. """ model = SketchupModel() model.google_id = "test1" model.mesh = file("sketchup_models/fixtures/mesh_can.tri").read() def error_msg(model): return ("The mesh should be of type str but is {} instead.". format(model.mesh.__class__)) msg = error_msg(model) self.assertTrue(isinstance(model.mesh, basestring)) model.save() msg = error_msg(model) self.assertTrue(isinstance(model.mesh, basestring)) model = SketchupModel.find_google_id("test1") msg = error_msg(model) self.assertTrue(isinstance(model.mesh, basestring), msg=msg)
def test_mesh_attribute_can_be_used_as_a_string(self): """ As mesh is store in a gridfsfield, the attribute mesh would normally be returned as a GridFSOut object. The model has been modified so that it is a string instead. """ model = SketchupModel() model.google_id = "test1" model.mesh = file("sketchup_models/fixtures/mesh_can.tri").read() def error_msg(model): return "The mesh should be of type str but is {} instead.".format(model.mesh.__class__) msg = error_msg(model) self.assertTrue(isinstance(model.mesh, basestring)) model.save() msg = error_msg(model) self.assertTrue(isinstance(model.mesh, basestring)) model = SketchupModel.find_google_id("test1") msg = error_msg(model) self.assertTrue(isinstance(model.mesh, basestring), msg=msg)