Ejemplo n.º 1
0
def prob_image(args):
  list_predictions = list()
  pred = Prediction(args.proto_path,args.bin_path)
  max_value = 512 
  curr_value = 0


  with open(args.images,'r') as file_image:
    list_images = list()
    list_good_class = list()
    for line in file_image:
      splitted = line.split('\t')
      list_images.append(splitted[2].strip())
      curr_value = curr_value + 1
      if curr_value < max_value:
        continue
      else:
        #predict using value
        predictions = pred.predict_multi(list_images)
        list_images = list()
        list_good_class = list()
        curr_value = 0
        list_predictions.append(predictions)
        print predictions.shape
        
    if len(list_images) > 0:
      #predict using value
      predictions = pred.predict_multi(list_images)
      list_predictions.append(predictions)
  
  all_pred = np.vstack(list_predictions)
  np.save('fine_sand.npy',all_pred)
  np.savetxt('probabilities_kaggle_aug_fine_m.txt', all_pred, fmt='%1.9e')
def check(event=None):
    show_info("")
    # try:
    raw_text = displayed_file.get('1.0', tk.END)
    global candidate
    candidate = Prediction(raw_text, jl, jl_multiClass)
    global propDict
    propDict = candidate.predict_technique()
    if len(propDict) > 0:
        displayed_file.delete('1.0', END)
        global percent_of_propaganda
        percent_of_propaganda = candidate.percent_of_propaganda()
        # displayed_file.insert(tk.END, )
        displayed_file.insert(tk.END, raw_text)
        displayed_file.tags()
        statistics_button.grid(row=3, column=4, padx=10, pady=10)
        print(propDict)
        for key in propDict:
            for value in propDict[key]:
                displayed_file.highlight_pattern(value, key)
                tag = key
                displayed_file.tag_configure(tag)
                displayed_file.tag_bind(tag,
                                        "<Enter>",
                                        lambda event, key=key: show_info(key))
                displayed_file.tag_bind(tag,
                                        "<Leave>",
                                        lambda event, key=key: show_info(""))

    else:
        show_info("No propaganda detected")
    return propDict
Ejemplo n.º 3
0
def join_connection():
    req_data = request.get_json()
    conn_id = req_data['_id']
    fileName = req_data['fileName']
    userId = req_data['userId']
    p = Prediction()
    # fo = open(app.config['UPLOAD_FOLDER'] + "/" + "ad.enc",'rb').read()
    # # Decrypts the data, requires a user-input password
    # CSVplaintext = decrypt('USERPASSWORD', fo).decode('utf8')

    # #create a temp csv-like file to pass to pandas.read_csv()
    # DATA=StringIO(CSVplaintext)

    # # Makes a panda dataframe with the data
    # df = pd.read_excel(DATA)
    # print df.head()
    value = int(p.run(app.config['UPLOAD_FOLDER'] + "/Data/" + fileName))
    #conn_id = '5bf8ab7c101c372f28c65c2b'
    users = mongo.db.users
    connections = mongo.db.connections
    parser = connections.find({'_id': ObjectId(conn_id)})
    for doc in parser:
        connDoc = doc
    currCount = connDoc['currRemainingCount']
    if currCount > 0:
        playerNum = connDoc['userCount'] - (currCount - 1)
        print os.system('pwd')
        command = 'python ./../viff-1.0/apps/sum.py --no-ssl player-' + str(
            playerNum) + '.ini ' + str(value)
        print command
        print connections.update({'_id': ObjectId(conn_id)},
                                 {'$inc': {
                                     'currRemainingCount': -1
                                 }})
        #os.system(command)
        proc = Popen(command,
                     shell=True,
                     stdin=None,
                     stdout=None,
                     stderr=None,
                     close_fds=True)
        print "Status = " + str(proc)
        status = 0
        if status == 0:
            #print output
            value *= 100
            users.insert({
                'userId': str(userId),
                'yourVal': value,
                'AvgValue': 0
            })
            retVal = {}
            retVal['sum'] = 'Will be updated soon!'
            return jsonify(retVal), 200
        else:
            'Joining Failed', 404
    else:
        return 'Connection Full', 404
Ejemplo n.º 4
0
class ChatBotInit:
    def __init__(self):

        #TODO move to outside
        self.model_name = CHATBOT_MODEL
        self.embedding_file = 'embedding_wiki_100d_pt.txt'
        self.ds = Dataset()
        self.messages = self.ds.import_dataset()
        self.questions = self.ds.get_questions(
            self.messages)  #['why?', 'how?', 'when?, ...']
        self.answers = self.ds.get_answers(
            self.messages)  #['yes', 'no', 'maybe', ...]
        self.model = load_model(CHATDATA_DIR + self.model_name)
        self.pc_questions = self.ds.get_page_compute(qea=1)
        self.pc_answers = self.ds.get_page_compute(qea=0)
        self.tokenizer = self.ds.load_tokenizer()
        self.p = Prediction(model=self.model,
                            messages=self.messages,
                            questions=self.questions,
                            answers=self.answers,
                            pc_questions=self.pc_questions,
                            pc_answers=self.pc_answers,
                            tokenizer=self.tokenizer)

    def init_chat_cmd(self, run_once=False):

        m = None
        exit = 'exit'

        print('I am ready to talk.')
        print(' Tip: You can type \"exit\" (lowercase) anytime to leave.')

        while (m != exit):
            #TODO save conversation to file_timestamp
            try:
                m = input(YOU_PREFIX)
                save_content_to_log_file(YOU_PREFIX + str(m))

            except KeyboardInterrupt:
                print(BOT_PREFIX + emergency_message())
                save_content_to_log(sys.exc_info())

            if m != exit:
                r = str(self.p.predict(m))
                print(BOT_PREFIX + r)
                save_content_to_log_file(BOT_PREFIX + str(r))

            if run_once == True:
                m = exit

        print(BOT_PREFIX + 'Bye!')

    def get_conversation(self, message):
        return str(self.p.predict(message))
Ejemplo n.º 5
0
def test_accuracy_multi(args):
  # Compute Accuracy of images from input file
  # Code predict many images at one time. The value is set by max_value
  # Same value should be set in proto file
  # It is done because of speed reason
  
  pred = Prediction(args.proto_path,args.bin_path)
  max_value = 512 
  curr_value = 0
  list_all_result = list()
  list_good_class_all = list()
    
  with open(args.images,'r') as file_image:
    list_images = list()
    list_good_class = list()
    for line in file_image:
      splitted = line.split(' ')
      #if not os.path.isfile(splitted[0].strip()):
        #continue
      list_good_class.append(int(splitted[1]))
      list_images.append(splitted[0].strip())
      curr_value = curr_value + 1
      if curr_value < max_value:
        continue
      else:
        #predict using value
        predictions = pred.predict_multi(list_images)
        list_all_result.append(predictions)
        list_good_class_all.extend(list_good_class)
        list_good_class = list()
        list_images = list()
        curr_value = 0
        
    #predict last package of data, which is smaller than max_value
    if len(list_images) > 0:
      predictions = pred.predict_multi(list_images)
      list_all_result.append(predictions)
          
  #test loss
  list_good_class_all.extend(list_good_class)
  y_truth = np.asarray(list_good_class_all)
  y_pred = np.vstack(list_all_result)
  y_pred_label = np.argmax(y_pred, axis=1)
  print y_truth.shape, y_pred.shape
  print "Accuracy: ", accuracy_score(y_truth,y_pred_label)
  print "Loss: ",multiclass_log_loss(y_truth,y_pred)
  plot_confusion_matrix(y_truth,y_pred_label)
  np.save('casia_deep.npy',y_pred)
Ejemplo n.º 6
0
def predict_image(model_path: str, image: np.ndarray, show: bool = False):

    pred = Prediction(model_path=model_path,
                      output_path=None,
                      patch_size=train_params['patch_size'],
                      batch_size=train_params['batch_size'],
                      offset=2 * train_params["n_layers"],
                      n_classes=len(classes_mask.keys()),
                      n_filters=train_params["n_filters"],
                      n_layers=train_params["n_layers"])

    # image = np.array(Image.open(image)).astype(np.float32) / 255
    predicted = pred.__predict_image__(image)
    predicted = np.argmax(predicted, axis=2)

    if show:
        Image.fromarray(
            colorize_mask(np.dstack((predicted, predicted, predicted)),
                          n_classes=pred.n_classes)).show()

    return predicted
Ejemplo n.º 7
0
    def __init__(self):

        #TODO move to outside
        self.model_name = CHATBOT_MODEL
        self.embedding_file = 'embedding_wiki_100d_pt.txt'
        self.ds = Dataset()
        self.messages = self.ds.import_dataset()
        self.questions = self.ds.get_questions(
            self.messages)  #['why?', 'how?', 'when?, ...']
        self.answers = self.ds.get_answers(
            self.messages)  #['yes', 'no', 'maybe', ...]
        self.model = load_model(CHATDATA_DIR + self.model_name)
        self.pc_questions = self.ds.get_page_compute(qea=1)
        self.pc_answers = self.ds.get_page_compute(qea=0)
        self.tokenizer = self.ds.load_tokenizer()
        self.p = Prediction(model=self.model,
                            messages=self.messages,
                            questions=self.questions,
                            answers=self.answers,
                            pc_questions=self.pc_questions,
                            pc_answers=self.pc_answers,
                            tokenizer=self.tokenizer)
Ejemplo n.º 8
0
    def __init__( self ):
        """Constructor."""
        
        super( QMainWindow, self ).__init__( )

        self.setGeometry(10, 50, 300, 150)
        self.setWindowTitle("Informative or NonInformative Tweets")

        self.filename   = ""
        self.prediction = Prediction( "../models/text.h5", "../models/old_image/damage.h5" )
        
        self.widget = QWidget    ( )
        self.widget.setObjectName("widget")
        self.widget.setStyleSheet("QWidget#widget{border-image:url('../images/background.jpg');}\n")
        
        self.layout = QGridLayout( self.widget )

        self.image_label  = QLabel( )
        self.result_label = QLabel( )
        
        self.image_label .setScaledContents( True )
        self.result_label.setScaledContents( True )
        
        self.browse     = QPushButton( "Browse" )
        self.text_field = QLineEdit  ( )
        self.submit     = QPushButton( "Submit" )

        self.layout.addWidget( self.image_label )
        self.layout.addWidget( self.browse )
        self.layout.addWidget( self.text_field )
        self.layout.addWidget( self.submit )
        self.layout.addWidget( self.result_label )

        self.browse.clicked.connect( self.pick_file )
        self.submit.clicked.connect( self.predict )

        self.setCentralWidget( self.widget )
Ejemplo n.º 9
0
def dispatch(values=None):

    #Validate parm
    if (values == None):
        return {'error': 'parameter is missing'}
    if (not (isinstance(values, dict))):
        return {'error': 'parameter is not a dictionary'}
    if (not ('op' in values)):
        values['error'] = 'no op  is specified'
        return values

    #Perform designated function
    if (values['op'] == 'adjust'):
        return AdjustedAltitude.dispatch(values)
    elif (values['op'] == 'predict'):
        return Prediction.dispatch(values)
    elif (values['op'] == 'correct'):
        return Correction.dispatch(values)
    elif (values['op'] == 'locate'):
        return values  #This calculation is stubbed out
    else:
        values['error'] = 'op is not a legal operation'
        return values
Ejemplo n.º 10
0
import MySQLdb.cursors
import re

import os
import cv2

import image_processing.filters as filters
import image_processing.preprocessing as preprocessing
import image_processing.to_domain as to_domain

import time

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

from predict import Prediction
predictor = Prediction("model_weights.h5")

app = Flask(__name__)
app.secret_key = 'thisissecret'

app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = '******'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'retinadb'

mysql = MySQL(app)

predicted = {}
processed_images = {}

@app.route("/index")
Ejemplo n.º 11
0
mode = sys.argv[1]
if mode == "train" or mode == "onlyexport":
    data_file = sys.argv[2]
    parameter_file = sys.argv[3]
    examples_file = sys.argv[4]
    with codecs.open(parameter_file, 'r', 'utf-8') as f:
        params = json.load(f)
    with codecs.open(examples_file, 'r', 'utf-8') as f:
        examples = json.load(f)
    t = Training(data_file=data_file, examples=examples)

    for p in params:
        print("Trying:{}".format(p))
        p["only_export"] = (mode == "onlyexport")
        t.train(**p)

if mode == "predict":
    vocab_file = sys.argv[2]
    model_file = sys.argv[3]
    text = sys.argv[4]
    p = Prediction(model_file, vocab_file)
    print(p.predict(text))

if mode == "predict_server":
    data_file = sys.argv[2]
    model_file = sys.argv[3]
    p = Prediction(model_file, data_file)
    db = get_database_from_file(data_file)
    PredictServer(p, db)
Ejemplo n.º 12
0
class MainWindow( QMainWindow ):
    """Class implementing GUI"""
    
    def __init__( self ):
        """Constructor."""
        
        super( QMainWindow, self ).__init__( )

        self.setGeometry(10, 50, 300, 150)
        self.setWindowTitle("Informative or NonInformative Tweets")

        self.filename   = ""
        self.prediction = Prediction( "../models/text.h5", "../models/old_image/damage.h5" )
        
        self.widget = QWidget    ( )
        self.widget.setObjectName("widget")
        self.widget.setStyleSheet("QWidget#widget{border-image:url('../images/background.jpg');}\n")
        
        self.layout = QGridLayout( self.widget )

        self.image_label  = QLabel( )
        self.result_label = QLabel( )
        
        self.image_label .setScaledContents( True )
        self.result_label.setScaledContents( True )
        
        self.browse     = QPushButton( "Browse" )
        self.text_field = QLineEdit  ( )
        self.submit     = QPushButton( "Submit" )

        self.layout.addWidget( self.image_label )
        self.layout.addWidget( self.browse )
        self.layout.addWidget( self.text_field )
        self.layout.addWidget( self.submit )
        self.layout.addWidget( self.result_label )

        self.browse.clicked.connect( self.pick_file )
        self.submit.clicked.connect( self.predict )

        self.setCentralWidget( self.widget )

    def pick_file( self ):
        """For selecting a file."""

        self.filename = QFileDialog.getOpenFileName( )[ 0 ]

        if self.filename != "":

            pixmap          = QPixmap ( self.filename )
            self.image_label.setPixmap( pixmap )

    def predict( self ):
        
        if self.filename != "":
            
            image               = cv2.imread( self.filename )
            text                = self.text_field.text( )
            im_class, txt_class = self.prediction.run( image, text )
            
            if im_class == 0 and txt_class == 0:
                self.result_label.setText( "<font color='red'>Informative</font>" )
            else:
                self.result_label.setText( "<font color='green'>Non-Informative</font>" )
Ejemplo n.º 13
0
from numpy import array
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.text import tokenizer_from_json
from predict import Prediction
import json

# Load saved tokenizer
with open('tokenizer_30000.json') as f:
    data = json.load(f)
    tokenizer = tokenizer_from_json(data)
max_length = 179
pred = Prediction(tokenizer, max_length)
pred.load_model()
#we were laughing , haha thats funny but then we were saying damn it all , what kind of example is this guy setting for the kids ?
#this is just great , seth shouted .
#he might be into it .
#just try to be less annoying than usual
#this may say something important

# Predict for given sequence
print(pred.predict_sequnce("just try to be less annoying than usual", 100))