def trainRobot(source_id): # first, see if we should train a new robot hasNewImagesToTrainOn = False nbrAnnotatedImages = 0 source = Source.objects.get(pk = source_id) allImages = Image.objects.filter(source = source) for image in allImages: if (image.status.featureFileHasHumanLabels and not image.status.usedInCurrentModel): hasNewImagesToTrainOn = True if image.status.featureFileHasHumanLabels: nbrAnnotatedImages = nbrAnnotatedImages + 1; if ( not hasNewImagesToTrainOn or ( nbrAnnotatedImages < 5 ) ) : #TODO, add field to souce object that specify this threshold. print 'Source ' + str(source_id) + ' has no new images to train on, aborting' return 1 ################### EVERYTHING OK, START TRAINING NEW MODEL ################ # create the new Robot object newRobot = Robot(source=source, version = Robot.objects.all().order_by('-version')[0].version + 1, time_to_train = 1); newRobot.path_to_model = join_project_root("images/models/robot" + str(newRobot.version)) newRobot.save(); print 'new robot version:' + str(newRobot.version) # update the data base. for image in allImages: # mark that these images are used in the current model. if image.status.featureFileHasHumanLabels: image.status.usedInCurrentModel = True; image.status.save() # grab the last robot previousRobot = source.get_latest_robot() if previousRobot == None: oldModelPath = ''; else: oldModelPath = previousRobot.path_to_model print 'previous robot version:' + str(previousRobot.version) # now, loop through the images and create some meta data files that MATLAB needs workingDir = newRobot.path_to_model + '.workdir/' pointInfoPath = os.path.join(workingDir + 'points') fileNamesPath = os.path.join(workingDir, 'fileNames') os.mkdir(workingDir) fItt = 0 #image iterator pointFile = open(pointInfoPath, 'w') fileNameFile = open(fileNamesPath, 'w') for image in allImages: if not image.status.featureFileHasHumanLabels: continue fItt = fItt + 1 #note that we start at 1, MATLAB style featureFile = os.path.join(FEATURES_DIR, str(image.id) + "_" + image.get_process_date_short_str() + ".dat") fileNameFile.write(featureFile + "\n") # write all file names in a file, so that mablat can find them points = Point.objects.filter(image=image) pItt = 0 for point in points: pItt = pItt + 1 #note that we start at 1, MATLAB style Ann = Annotation.objects.filter(point=point, image=image) pointFile.write(str(fItt) + ', ' + str(pItt) + ', ' + str(Ann[0].label.id) + '\n') pointFile.close() fileNameFile.close() # call the matlab function task_helpers.coralnet_trainRobot( modelPath = newRobot.path_to_model, oldModelPath = oldModelPath, pointInfoPath = pointInfoPath, fileNamesPath = fileNamesPath, workDir = workingDir, logFile = CV_LOG, errorLogfile = TRAIN_ERROR_LOG, ) # clean up # rmtree(workingDir) if os.path.isfile(TRAIN_ERROR_LOG): for image in allImages: # roll back changes. if image.status.featureFileHasHumanLabels: image.status.usedInCurrentModel = False image.status.save() print("Sorry error detected in robot training!") newRobot.delete() else: #if not (previousRobot == None): #os.remove(oldModelPath) # remove old model, but keep the meta data files. print 'Finished training new robot(' + str(newRobot.version) + ') for source id: ' + str(source_id)