예제 #1
0
def runValTest(args):
    #Val set's split -- test
    print('Running Val Test')
    config = Attention_LapConfig(load=True, args=args)
    valTestReader = InputProcessor(config.testAnnotFile,
                                   config.rawQnTrain,
                                   config.trainImgFile,
                                   config,
                                   is_training=False)

    model = ImageAttentionModel(config)
    model.loadTrainedModel(config.restoreModel, config.restoreModelPath)
    model.runPredict(valTestReader)
    model.destruct()
    valTestReader.destruct()
예제 #2
0
def runValTest(args):
    #Val set's split -- test
    print('Running Val Test')
    if args.config == 'CPU':
        config = CNNLapConfig(load=True, args=args)
    elif args.config == 'GPU':
        config = CNNGPUConfig(load=True, args=args)
    valTestReader = InputProcessor(config.testAnnotFile,
                                   config.rawQnValTestFile,
                                   config.valImgPaths,
                                   config,
                                   is_training=False)

    model = LSTMCNNModel(config)
    model.loadTrainedModel(config.restoreModel, config.restoreModelPath)
    model.runPredict(valTestReader, config.csvResults)
    model.destruct()
    valTestReader.destruct()
예제 #3
0
def runVisualise():
    print('Running Visuals')
    config = Attention_LapConfig(load=True, args=args)
    reader = InputProcessor(config.trainAnnotFile,
                            config.rawQnTrain,
                            config.trainImgFile,
                            config,
                            is_training=False)

    model = ImageAttentionModel(config)
    model.loadTrainedModel(config.restoreModel, config.restoreModelPath)
    alphas, img_ids, qns, preds = model.runPredict(reader)
    model.destruct()
    reader.destruct()

    imgpaths = '/media/jwong/Transcend/VQADataset/TrainSet/trainImgPaths.txt'
    out = OutputGenerator(imgpaths)
    out.displayOutput(alphas, img_ids, qns, preds)
예제 #4
0
def runtrain(args):
    if args.config == 'CPU':
        config = CNNLapConfig(load=True, args=args)
    elif args.config == 'GPU':
        config = CNNGPUConfig(load=True, args=args)

    trainReader = InputProcessor(config.trainAnnotFile,
                                 config.rawQnTrain,
                                 config.trainImgPaths,
                                 config,
                                 is_training=True)

    valReader = InputProcessor(config.valAnnotFile,
                               config.rawQnValTestFile,
                               config.valImgPaths,
                               config,
                               is_training=False)

    model = LSTMCNNModel(config)

    model.construct()
    model.train(trainReader, valReader, config.logFile)
    model.destruct()
def main():
	mostFreqAnswersFile = '/home/jwong/Documents/LinuxWorkspace/Visual-Question-Answering/resources/allTrainAnswers.csv'
	
	print('Loading files...')
	
	trainWVQnsFile = '/media/jwong/Transcend/VQADataset/TrainSet/ExtractedQnFeatures/word2VecAddQnFeatures_Train.json'
	trainImgFile = '/media/jwong/Transcend/VQADataset/TrainSet/ExtractedImageFeatures/VQAImgFeatures_Train.json'
	trainAnnotFile = '/media/jwong/Transcend/VQADataset/TrainSet/AllTrainAnnotationsList.json'
	trainProcessor = InputProcessor(trainAnnotFile, trainWVQnsFile, trainImgFile, mostFreqAnswersFile)

	valTestWVQnsFile = '/media/jwong/Transcend/VQADataset/ValTestSet/ExtractedQnFeatures/word2VecAddQnFeatures_valTest.json'
	valImgFile = '/media/jwong/Transcend/VQADataset/ValTestSet/VQAImgFeatures_Val.json'
	valAnnotFile = '/media/jwong/Transcend/VQADataset/ValTestSet/AllValAnnotationsList.json'
	testImgFile = '/media/jwong/Transcend/VQADataset/ValTestSet/VQAImgFeatures_Test.json'
	testAnnotFile = '/media/jwong/Transcend/VQADataset/ValTestSet/AllTestAnnotationsList.json'

	valProcessor = InputProcessor(valAnnotFile, valTestWVQnsFile, valImgFile, mostFreqAnswersFile)

	trainer = SoftmaxLayer()
	trainer.trainSoftmaxLayer(trainProcessor, valProcessor)
	
	#testProcessor = InputProcessor(valTestQnFile, vocabBOWfile, testImgFile, mostFreqAnswersFile)
	#testProcessor.readAnnotFile(testAnnotFile)
	print('Completed.')
예제 #6
0
from InputProcessor import InputProcessor
from HtmlGenerator import HtmlGenerator
import Validator

INPUT_FILE_PATH = "../data/smells.txt"
OUTPUT_PATH = "../smellCatalogHtml/"
SMELL_CATEGORY_FILE_PATH = "../data/smell-category.txt"
REF_FILE_PATH = "../data/references.txt"
TOOL_FILE_PATH = "../data/tools.txt"

input_processor = InputProcessor(INPUT_FILE_PATH)
tools_list = input_processor.get_tools_list(TOOL_FILE_PATH)
smell_list = input_processor.get_smell_list()
input_processor.populate_aka_obj(smell_list)
category_list = input_processor.get_category_list(SMELL_CATEGORY_FILE_PATH)
ref_list = input_processor.get_ref_list(REF_FILE_PATH)

Validator.validateAll(tools_list, smell_list, category_list, ref_list)

sorted_smell_list = sorted(smell_list, key=lambda smell: smell.name)

html_generator = HtmlGenerator(OUTPUT_PATH, sorted_smell_list, category_list,
                               tools_list)
html_generator.generate()
예제 #7
0
class Program:
    def __init__(self, db, config):
        self.db = db
        self.config = config
        self.currentUser = None
        self.inputProcessor = InputProcessor()

    # Function to print out welcoming string at the start of the program
    def start(self):
        self.db.setup()
        print("\nWelcome, this is mini project 1!")

    # Function to perform login action
    def login(self):
        # user name, password
        try:
            uid = self.inputProcessor.getUidInput()
            password = self.inputProcessor.getPasswordInput(
            )  # invisible password
            self.currentUser = self.db.getUser(uid, password)
            print("Logged in as {uid}".format(uid=self.currentUser.uid))
        except Exception as err:
            print(err.args[0])

    # Function to perform register action
    def register(self):
        print("\nREGISTER")
        try:
            uid = self.inputProcessor.getUidInput()
            password = self.inputProcessor.getPasswordInput(
            )  # invisible password
            name = input("Enter name (optional): ")
            city = input("Enter city (optional): ")

            self.db.register(uid, name, password, city)
            print("Register success")
        except Exception as err:
            print(err.args[0])

    # Function to perform posting question action
    def postQuestion(self):
        try:
            title = self.inputProcessor.getNonEmptyInput("Post title")
            body = self.inputProcessor.getNonEmptyInput("Post body")
            self.db.postRecord(self.currentUser.uid, title, body)
        except Exception as err:
            print(err.args[0])

    # Function to get all the posts with matching keywords
    def searchGetAll(self, keywords):
        result = self.db.searchPost(keywords, -1)
        if (len(result) == 0):
            raise Exception(
                "No posts have the entered keyword(s): {keywords}".format(
                    keywords=keywords))

        return result

    # Function to paginate the searching result ( at most 5 posts per page )
    def searchPaginate(self, keywords, currentPage):
        result = self.db.searchPost(keywords, (currentPage - 1) *
                                    5)  # searching by keyword
        if (len(result) == 0 and currentPage == 1):
            raise Exception(
                "No posts have the entered keyword(s): {keywords}".format(
                    keywords=keywords))

        return result

    # Function to perform search action
    def search(self):
        try:
            currentPage = 1
            keywords = self.inputProcessor.getNonEmptyInput(
                "Search keywords", "separated by space")

            headers = [("pid", "title", "body", "voteCnt", "ansCnt",
                        "matchCnt")]

            allResultCount = len(self.searchGetAll(keywords))

            while (True):
                result = self.searchPaginate(keywords,
                                             currentPage)  # pagnite the result
                resultCount = len(result)
                noNext = resultCount < 5 or (resultCount + currentPage * 5
                                             == allResultCount)
                noPrev = currentPage == 1
                if (resultCount > 0):
                    print(
                        "SEARCH RESULT: Page {currentPage}/{totalPage}".format(
                            currentPage=currentPage,
                            totalPage=math.ceil(allResultCount / 5)))
                    self.printTable(headers + result)

                try:
                    action = self.inputProcessor.getSearchActionInput(
                        noNext, noPrev)

                    if (action == 'next'):
                        currentPage += 1
                    elif (action == "prev"):
                        currentPage -= 1
                    elif (action == "back"):
                        break
                    else:
                        validPid = False
                        for row in result:
                            if (row[0] == action):
                                validPid = True
                                break
                        if (validPid):
                            while (True):
                                try:
                                    postAction = self.getPostAction(action)
                                    if (postAction == BACK_ACTION):
                                        break
                                    postAction(self, action)
                                except Exception as err:
                                    print(err.args[0])
                        else:
                            print(
                                "ERROR: PID {pid} not in search result. Please try again with another option."
                                .format(pid=action))
                except Exception as err:
                    print(err.args[0])
        except Exception as err:
            print(err.args[0])

    # Function to perform selecting a post by post ID action
    def getPostAction(self, postId):
        isQuestion = self.db.getQuestion(postId) != None
        isAnswer = self.db.getAnswer(postId) != None
        isPrivileged = self.currentUser.isPrivileged
        actionType = None

        if (isQuestion):
            print("\nThis post is a question.")
            if (isPrivileged):
                actionType = PRIVILEGED_QUESTION
            else:
                actionType = ORDINARY_QUESTION
        elif (isAnswer):
            print("\nThis post is an answer.")
            if (isPrivileged):
                actionType = PRIVILEGED_ANSWER
            else:
                actionType = ORDINARY_ANSWER
        else:
            raise Exception("\nPost does not exist.")

        postAction = self.config[actionType]
        userInput = input(postAction["prompt"]).lower()
        if (userInput in postAction["validInput"]):
            return postAction["postActionHandlers"][userInput]
        else:
            raise Exception("\nInvalid action input.")

    # Function to perform posting an answer post action
    def postAnswer(self, postId):
        title = input("Answer title: ")
        if (len(title) == 0):
            print("Title cannot be empty")
            return

        body = input("Answer body: ")
        if (len(body) == 0):
            print("Body cannot be empty")
            return
        self.db.postAnswer(self.currentUser.uid, postId, title, body)

    # Function to perform casting a vote to a post action
    def castVote(self, postId):
        try:
            self.db.postVote(self.currentUser.uid, postId)
            print("Vote success.")
        except Exception as err:
            print(err.args[0])

    # Function to perform giving a badge to a poster action
    def giveBadge(self, postId):
        bname = input("Badge name: ")
        self.db.giveBadge(bname, postId)

    # Function to perform adding a tag to a post action
    def addTag(self, postId):
        tag = input("Enter tag name: ")
        self.db.addTag(postId, tag)
        print("Add tag success.")
        allTags = self.db.getTags(postId)
        print("Current tags: {tags}".format(
            tags=", ".join(map(lambda t: t[0], allTags))))

    # Function to perform marking an accepted answer action
    def markAccepted(self, postId):
        answer = self.db.getAnswer(postId)
        qid = answer[1]
        acceptedAnswer = self.db.getAcceptedAnswer(qid)
        if (acceptedAnswer == None):
            self.db.markAnswer(qid, postId)
            print("SUCCESS - set {postId} as accepted answer for {qid}".format(
                postId=postId, qid=qid))
        elif (acceptedAnswer != None):
            userAccept = input(
                "This question already has an accepted answer, do you want to overwrite it? (Y/N): "
            ).lower()
            if (userAccept == 'y'):
                self.db.markAnswer(qid, postId)
        else:
            print("Unexpected error occurred")

    # Function to perform editing a post action
    def editPost(self, postId):
        while (True):
            action = input(EDIT_ACTION_PROMPT)
            try:
                if action == '1':
                    newTitle = self.inputProcessor.getNonEmptyInput(
                        "New title")
                    newBody = self.inputProcessor.getNonEmptyInput("New body")
                    self.db.editPost(postId, newTitle, newBody)
                elif action == '2':
                    newTitle = self.inputProcessor.getNonEmptyInput(
                        "New title")
                    self.db.editPost(postId, newTitle, "")
                elif action == '3':
                    newBody = self.inputProcessor.getNonEmptyInput("New body")
                    self.db.editPost(postId, "", newBody)
                elif action == '4':
                    break
                else:
                    print("Invalid action. Choose another option.")
            except Exception as err:
                print(err.args[0])

    # source: https://stackoverflow.com/a/12065663
    # Function to print out the result table in search
    def printTable(self, data):
        widths = [max(map(len, map(str, col))) for col in zip(*data)]
        for row in data:
            print("  ".join(
                str(val).ljust(width) for val, width in zip(row, widths)))

    # Function to perform logout action
    def logout(self):
        self.currentUser = None
        print("Logged out")

    # Function to quit the program
    def end(self):
        self.db.close()
        print("Good bye!")
예제 #8
0
 def __init__(self, db, config):
     self.db = db
     self.config = config
     self.currentUser = None
     self.inputProcessor = InputProcessor()
예제 #9
0
def main():
    """The entry point for the command line application"""
    input_processor = InputProcessor(Config.INPUT_FILE_LOCATION)
    input_processor.parse_file()