def _record_results(self, game, game_answers, game_questions, guess, result, number_of_objects): """ Puts results into the DB as well as writing them to file for examination """ # TODO: make sure this can be used for new objects and when playing with the human's choice of object log.info('Recording object results to the database') for i in range(0, len(game_questions)): T = questions.get_t(self.id, game_questions[i], number_of_objects) if game_answers[i] == True: db.cursor.execute("SELECT yes_answers FROM Pqd where t_value = %s", (T,)) yes_count = db.cursor.fetchone()[0] #print yes_count, 'yes' db.cursor.execute("UPDATE Pqd SET yes_answers = %s WHERE t_value = %s", (yes_count + 1, T)) db.cursor.execute("SELECT total_answers FROM Pqd where t_value = %s", (T,)) total_count = db.cursor.fetchone()[0] #print total_count db.cursor.execute("UPDATE Pqd SET total_answers = %s WHERE t_value = %s", (total_count + 1, T)) db.cursor.execute("INSERT INTO answers (oid, qid, answer) VALUES (%s, %s, %s)", (str(self.id), game_questions[i], game_answers[i])) db.connection.commit() if result == 0: result = 'lose' else: result = 'win' # TODO: clean up all the text files because this is kind of ridiculous with open("game.txt", "a") as myfile: myfile.write(str(game.id)+','+ str(self.id) +','+ str(guess.name)+"," + str(self.name) + "," + str(len(game_questions)) + "," + result + "\n") myfile.close() with open("answers.txt", "a") as answerfile: answerfile.write("\n" + str(game.id) + " " + str(self.id) + " " + result + "\n") for i in range(0, len(game_questions)): answerfile.write(tags.get(game_questions[i]) + " -> " + str(game_answers[i]) + "\n") answerfile.close()
def _record_results(self, game, game_answers, game_questions, guess, result): """ Puts results into the DB as well as writing them to file for examination """ log.info('Recording object results') for i in range(0, len(game_questions)): T = questions.get_t(self.id, game_questions[i]) if game_answers[i] == True: db.cursor.execute("SELECT yes_answers FROM Pqd where t_value = %s", (T,)) yes_count = db.cursor.fetchone()[0] #print yes_count, 'yes' db.cursor.execute("UPDATE Pqd SET yes_answers = %s WHERE t_value = %s", (yes_count + 1, T)) db.cursor.execute("SELECT total_answers FROM Pqd where t_value = %s", (T,)) total_count = db.cursor.fetchone()[0] #print total_count db.cursor.execute("UPDATE Pqd SET total_answers = %s WHERE t_value = %s", (total_count + 1, T)) db.cursor.execute("INSERT INTO answers (oid, qid, answer) VALUES (%s, %s, %s)", (str(self.id), game_questions[i], game_answers[i])) db.connection.commit() if result == 0: result = 'lose' else: result = 'win' with open("game.txt", "a") as myfile: myfile.write(str(game.id)+','+ str(self.id) +','+ str(guess.name)+"," + str(len(game_questions)) + "," + result + "\n") myfile.close() with open("answers.txt", "a") as answerfile: answerfile.write("\n" + str(game.id) + " " + str(self.id) + " " + result + "\n") for i in range(0, len(game_questions)): answerfile.write(tags.get(game_questions[i]) + " -> " + str(game_answers[i]) + "\n") answerfile.close()
def build(_game, method, number_of_objects, game_questions={}, game_answers={}, skip={}): """ Builds the model for all keywords """ log.info("Retraining model") #get all the different tags available db.cursor.execute("SELECT DISTINCT(tag) FROM TagInfoBk") results = db.cursor.fetchall() all_answers = objects.get_all_answers(number_of_objects) answer_data = all_answers[_game.id-1] tags = [] for result in results: tags.append(result[0]) count = 0 #print game_answers if method == 3: #for each tag we select all the observation_ids that are related to it for tag in tags: print "Training tag:", tag if tag not in skip: feature_matrix=[]#initialize feature matrix for each different tag feature_matrix_labels = [] # Labels to indicate if the example is positive or negative tag_obs_ids = range(1,18) db.cursor.execute('SELECT id FROM Tags WHERE tag = %s', (tag,)) qid = db.cursor.fetchone()[0] should_train_over_3 = False should_train_zero = False should_train = False #for every observation/object of this specific tag for obs_id in tag_obs_ids: T = questions.get_t(obs_id, qid, number_of_objects) # For game 0, if a tag has been used 3 or more times in the object descriptions, that object is used as a positive example if _game.id == 0: if T >= 3: should_train_over_3 = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) # An object is only a negative example if it is used 0 times elif T == 0: should_train_zero = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) # games up to 15 trained using all available answer data elif _game.id < 16: model_folder = os.getcwd() + '/SVM_model_777' listing = os.listdir(model_folder) has_model = [] for mod in listing: if mod.endswith('.pkl'): model_clone = joblib.load(model_folder + '/' + mod) K = mod.split('_', 1)[0] K = K.lower() has_model.append(K) for game in range(0, _game.id+1): if tag.lower() in has_model: if game == 0: if T >= 3: should_train = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) elif T == 0: should_train = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) else: if answer_data[obs_id-1][qid-1] == 1: should_train = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) else: should_train = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) # Otherwise, use only questions from gameplay else: model_folder = os.getcwd() + '/SVM_model_777' listing = os.listdir(model_folder) has_model = [] for mod in listing: if mod.endswith('.pkl'): model_clone = joblib.load(model_folder + '/' + mod) K = mod.split('_', 1)[0] K = K.lower() has_model.append(K) for game in range(0, _game.id+1): if tag.lower() in has_model: if game == 0: if T >= 3: should_train = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) elif T == 0: should_train = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) elif game < 16: if answer_data[obs_id-1][qid-1] == 1: should_train = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) elif answer_data[obs_id-1][qid-1] == 0: should_train = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) else: db.cursor.execute("SELECT id FROM Tags WHERE tag = '{0}'".format(tag)) tag_id = db.cursor.fetchone()[0] #TODO: double check that it being just obs_id and not obs_id - 1 is correct if tag_id in game_questions[game][obs_id]: print game_questions index = game_questions[game][obs_id].index(tag_id) if game_answers[game][obs_id][index] == 1: should_train = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) elif game_answers[game][obs_id][index] == 0: should_train = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix(feature_matrix, feature_matrix_labels, obs_id, _game, label) if should_train or (should_train_over_3 and should_train_zero): print "Training image models" training_matrix, training_labels = select_training_data(feature_matrix_labels, feature_matrix) training_matrix=np.asarray(training_matrix) #model.ModelTraining(tag, feature_matrix, 777) #training the model with GMM model.ModelTrainingSVM(tag, training_matrix, training_labels, 777) #training the model with SVM
def build(_game, method, number_of_objects, game_questions={}, game_answers={}, skip={}): """ Builds the model for all keywords """ log.info("Retraining model") #get all the different tags available db.cursor.execute("SELECT DISTINCT(tag) FROM TagInfoBk") results = db.cursor.fetchall() all_answers = objects.get_all_answers(number_of_objects) answer_data = all_answers[_game.id - 1] tags = [] for result in results: tags.append(result[0]) count = 0 #print game_answers if method == 3: #for each tag we select all the observation_ids that are related to it for tag in tags: print "Training tag:", tag if tag not in skip: feature_matrix = [ ] #initialize feature matrix for each different tag feature_matrix_labels = [ ] # Labels to indicate if the example is positive or negative tag_obs_ids = range(1, 18) db.cursor.execute('SELECT id FROM Tags WHERE tag = %s', (tag, )) qid = db.cursor.fetchone()[0] should_train_over_3 = False should_train_zero = False should_train = False #for every observation/object of this specific tag for obs_id in tag_obs_ids: T = questions.get_t(obs_id, qid, number_of_objects) # For game 0, if a tag has been used 3 or more times in the object descriptions, that object is used as a positive example if _game.id == 0: if T >= 3: should_train_over_3 = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) # An object is only a negative example if it is used 0 times elif T == 0: should_train_zero = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) # games up to 15 trained using all available answer data elif _game.id < 16: model_folder = os.getcwd() + '/SVM_model_777' listing = os.listdir(model_folder) has_model = [] for mod in listing: if mod.endswith('.pkl'): model_clone = joblib.load(model_folder + '/' + mod) K = mod.split('_', 1)[0] K = K.lower() has_model.append(K) for game in range(0, _game.id + 1): if tag.lower() in has_model: if game == 0: if T >= 3: should_train = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) elif T == 0: should_train = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) else: if answer_data[obs_id - 1][qid - 1] == 1: should_train = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) else: should_train = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) # Otherwise, use only questions from gameplay else: model_folder = os.getcwd() + '/SVM_model_777' listing = os.listdir(model_folder) has_model = [] for mod in listing: if mod.endswith('.pkl'): model_clone = joblib.load(model_folder + '/' + mod) K = mod.split('_', 1)[0] K = K.lower() has_model.append(K) for game in range(0, _game.id + 1): if tag.lower() in has_model: if game == 0: if T >= 3: should_train = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) elif T == 0: should_train = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) elif game < 16: if answer_data[obs_id - 1][qid - 1] == 1: should_train = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) elif answer_data[obs_id - 1][qid - 1] == 0: should_train = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) else: db.cursor.execute( "SELECT id FROM Tags WHERE tag = '{0}'" .format(tag)) tag_id = db.cursor.fetchone()[0] #TODO: double check that it being just obs_id and not obs_id - 1 is correct if tag_id in game_questions[game][obs_id]: print game_questions index = game_questions[game][ obs_id].index(tag_id) if game_answers[game][obs_id][ index] == 1: should_train = True count += 1 label = 1 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) elif game_answers[game][obs_id][ index] == 0: should_train = True count += 1 label = 0 feature_matrix, feature_matrix_labels = updateFeatureMatrix( feature_matrix, feature_matrix_labels, obs_id, _game, label) if should_train or (should_train_over_3 and should_train_zero): print "Training image models" training_matrix, training_labels = select_training_data( feature_matrix_labels, feature_matrix) training_matrix = np.asarray(training_matrix) #model.ModelTraining(tag, feature_matrix, 777) #training the model with GMM model.ModelTrainingSVM(tag, training_matrix, training_labels, 777) #training the model with SVM
def build(_game, method, game_questions={}, game_answers={}, skip={}): """ Build the model for all keywords using 1 of 3 different methods Currently only 1 and 3 work """ log.info("Retraining model") #get all the different tags available db.cursor.execute("SELECT DISTINCT(tag) FROM TagInfoBk") results=db.cursor.fetchall() tags = [] for result in results: tags.append(result[0]) count = 0 # Builds models by using images where the answer set has a yes for the paring as a positive example if method == 1: #for each tag we select all the observation_ids that are related to it for tag in tags: feature_matrix=[]#initialize feature matrix for each different tag feature_matrix_labels = [] # Labels to indicate if the example is positive or negative #cursor.execute("SELECT DISTINCT(observation_id) FROM TagInfoBk WHERE tag=%s",(tag)) db.cursor.execute("SELECT DISTINCT(observation_id) FROM TagInfoBk") tag_obs_ids=db.cursor.fetchall() db.cursor.execute('SELECT id FROM Tags WHERE tag = %s', (tag,)) qid = db.cursor.fetchone()[0] should_train = False #for every observation/object of this spesific tag for obs_id in tag_obs_ids: object_matrix = [] T = questions.get_t(obs_id[0], qid) if _game.id == 0: if T >= 3: should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], _game.id)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],_game.id)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(1) else: #elif _game.id < 16: model_folder = os.getcwd() + '/GMM_model_777' listing = os.listdir(model_folder) has_model = [] for mod in listing: if mod.endswith('.pkl'): model_clone = joblib.load(model_folder + '/' + mod) T = mod.split('_', 1)[0] T = T.lower() has_model.append(T) for game in range(0, _game.id+1): if tag.lower() in has_model: if game == 0: should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(1) else: answer_data = np.genfromtxt(os.getcwd()+'/Answers/Game'+str(game)+'.csv',dtype=str, delimiter='\t') #cursor.execute("SELECT id FROM Tags where tag ='{0}'".format(tag)) #tag_id = cursor.fetchone()[0] if answer_data[int(obs_id[0])-1][qid-1] == 'yes' or answer_data[int(obs_id[0])-1][qid-1] is 'yes': should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(1) if should_train: #print len(feature_matrix) feature_matrix=np.asarray(feature_matrix) model.ModelTraining(tag, feature_matrix, 777) #training the model elif method == 2: pass # Up to game 15, uses images that received a yes as positive and no as negative (All questions) # Beyond that, only uses keywords from the game (Much smaller subset) elif method == 3: #for each tag we select all the observation_ids that are related to it for tag in tags: if tag not in skip: feature_matrix=[]#initialize feature matrix for each different tag feature_matrix_labels = [] # Labels to indicate if the example is positive or negative #cursor.execute("SELECT DISTINCT(observation_id) FROM TagInfoBk WHERE tag=%s",(tag)) db.cursor.execute("SELECT DISTINCT(observation_id) FROM TagInfoBk") tag_obs_ids=db.cursor.fetchall() db.cursor.execute('SELECT id FROM Tags WHERE tag = %s', (tag,)) qid = db.cursor.fetchone()[0] should_train = False #for every observation/object of this spesific tag for obs_id in tag_obs_ids: T = questions.get_t(obs_id[0], qid) # For game 0, if a tag has been used 3 or more times in the object descriptions, that object is used as a positive example if _game.id == 0: if T >= 3: should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], _game.id)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],_game.id)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(1) # An object is only a negative example if it is used 0 times elif T == 0: count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], _game.id)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],_game.id)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(0) #print len(feature_matrix_labels) # games up to 15 trained using all available answer data elif _game.id < 16: answer_data = np.genfromtxt(os.getcwd()+'/Answers/Game'+str(_game.id)+'.csv',dtype=str, delimiter='\t') model_folder = os.getcwd() + '/GMM_model_777' listing = os.listdir(model_folder) has_model = [] for mod in listing: if mod.endswith('.pkl'): model_clone = joblib.load(model_folder + '/' + mod) K = mod.split('_', 1)[0] K = K.lower() has_model.append(K) for game in range(0, _game.id+1): if tag.lower() in has_model: if game == 0: if T >= 3: should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(1) elif T == 0: should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(0) else: answer_data = np.genfromtxt(os.getcwd()+'/Answers/Game'+str(game)+'.csv',dtype=str, delimiter='\t') #cursor.execute("SELECT id FROM Tags where tag ='{0}'".format(tag)) #tag_id = cursor.fetchone()[0] if answer_data[int(obs_id[0])-1][qid-1] == 'yes' or answer_data[int(obs_id[0])-1][qid-1] is 'yes': should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(1) else: should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(0) # Otherwise, use only questions from gameplay else: model_folder = os.getcwd() + '/GMM_model_777' listing = os.listdir(model_folder) has_model = [] for mod in listing: if mod.endswith('.pkl'): model_clone = joblib.load(model_folder + '/' + mod) K = mod.split('_', 1)[0] K = K.lower() has_model.append(K) for game in range(0, _game.id+1): #print game if tag.lower() in has_model: if game == 0: if T >= 3: should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(1) elif T == 0: should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(0) elif game < 16: answer_data = np.genfromtxt(os.getcwd()+'/Answers/Game'+str(game)+'.csv',dtype=str, delimiter='\t') #cursor.execute("SELECT id FROM Tags where tag ='{0}'".format(tag)) #tag_id = cursor.fetchone()[0] if answer_data[int(obs_id[0])-1][qid-1] == 'yes' or answer_data[int(obs_id[0])-1][qid-1] is 'yes': should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(1) elif answer_data[int(obs_id[0])-1][qid-1] == 'no' or answer_data[int(obs_id[0])-1][qid-1] is 'no': should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(0) else: db.cursor.execute("SELECT id FROM Tags WHERE tag = '{0}'".format(tag)) tag_id = db.cursor.fetchone()[0] if tag_id in game_questions[game][int(obs_id[0])]: index = game_questions[game][int(obs_id[0])].index(tag_id) #cursor.execute("SELECT id FROM Tags where tag ='{0}'".format(tag)) #tag_id = cursor.fetchone()[0] if game_answers[game][int(obs_id[0])][index] == 1: should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(1) elif game_answers[game][int(obs_id[0])][index] == 0: should_train = True count = count + 1 db.cursor.execute("SELECT COUNT(*) FROM FeatureInfo WHERE feature_id='0' AND observation_id='{0}' AND game_id='{1}' ".format(obs_id[0], game)) num_of_images_per_oservation=db.cursor.fetchall() db.cursor.execute("SELECT feature_id,feature_value FROM FeatureInfo WHERE observation_id='{0}' AND game_id='{1}'".format(obs_id[0],game)) feature_info=db.cursor.fetchall() vv_seperator=len(feature_info)/num_of_images_per_oservation[0][0] new_fv=0 #flag to show when a feature vector given a capture starts (index in feature_info tuple) end_of_fv=vv_seperator#flag to show when a feature vector given a capture ends (index in feature_info tuple) for capture_id in xrange(0,num_of_images_per_oservation[0][0]): feature_vector=RetrieveFeatureVector(feature_info,new_fv,end_of_fv) #create a feature vector given a capture #print len(feature_vector) new_fv=new_fv+vv_seperator #update starting index of the vector end_of_fv=end_of_fv+vv_seperator #update ending index of the vector feature_matrix.append(feature_vector) #insert feature vectors into a matrix for each tag feature_matrix_labels.append(0) if should_train: feature_matrix=np.asarray(feature_matrix) #model.ModelTraining(tag, feature_matrix, 777) #training the model with GMM model.ModelTrainingSVM(tag, feature_matrix, feature_matrix_labels, 777) #training the model with SVM