def fromWebcam(): print("Opening webcam...") cap = cv2.VideoCapture(0) face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt.xml') while (cap.isOpened()): ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) roi = gray faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) roi = gray[y:y + h, x:x + w] if roi is not None: gray = roi resized_img = cv2.resize(gray, (48, 48)) test.predict(resized_img.reshape(-1, 48, 48, 1), "Predict") cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break print("Exiting...") cap.release() cv2.destroyAllWindows()
def prediction(): if request.method == "POST": print(request) response = json.loads(request.data) #print(response["number"]) pred = predict(response["number"]) return jsonify({"message": pred})
def quizButtonHandler(): # Function Body questionButton.config(state="normal") path = './images/' + self.imageNames[self.last_selected_img] print(path) img = cv2.imread(path) self.labels, self.results = predict(img) print(self.labels) random.seed(9001) object_index = random.randint(0, len(self.labels) - 1) question.config(text="Click on " + self.labels[object_index]) for i, box in enumerate(self.results): x2, y2, h2, w2 = box[0], box[1], box[2], box[3] click_area1 = viewframe.create_rectangle(x2 - w2, y2 - h2, x2 + w2, y2 + h2, width=3, outline="maroon", stipple="gray25", fill="white") if i == object_index: viewframe.tag_bind(click_area1, '<ButtonPress-1>', onObjectClick_right) else: viewframe.tag_bind(click_area1, '<ButtonPress-1>', onObjectClick_wrong)
def score_testfiles(model, feature_filename, translations_filename, mode="--clamp_round"): features = load_features(feature_filename) try: scaler = pickle.load(open("scaler.model")) #print "scaling..." features = scaler.transform(features) except: pass labels_predicted = predict(model, features) lengths = read_translation_lengths(translations_filename) if labels_predicted.ndim == 2 and np.shape(labels_predicted)[1] == 4: ter_predicted = [] if mode == "--clamp_round": for (i, d, s, b), l in zip(labels_predicted, lengths): i = clamp(round(i, 0)) d = clamp(round(d, 0), 0, l - 1) s = clamp(round(s, 0), 0, l) b = clamp(round(b, 0), 0, l) ter_predicted.append(clamp((i + d + s + b) / (l + i - d), 0, 1)) elif mode == "--clamp": for (i, d, s, b), l in zip(labels_predicted, lengths): i = clamp(i) d = clamp(d, 0, l - 1) s = clamp(s, 0, l) b = clamp(b, 0, l) ter_predicted.append(clamp((i + d + s + b) / (l + i - d), 0, 1)) labels_predicted = ter_predicted return labels_predicted
def main(): """ Main driver program for lab 2. Takes in cmd-line args, creates a feature object, parses the training examples and creates a data-set object that will be fed into the Decision Tree and Adaboost algorithms for training from argv[1]. train() then pickles (serializes) the model returned from training and predict() unpickles the model and makes predictions based on test data input via argv[4] :return: None """ # Command-line arguments training_data = argv[1] hypothesis_out = argv[2] learning_type = argv[3] test = argv[4] labels = None if len(argv) > 5: labels = argv[5] # Parse data and determine features feat_obj = FeatureParser(training_data) data = FeatureData(feat_obj.features) # Train model using DT or DT + adaboost train(data, hypothesis_out, learning_type) # Predict on test set with trained model predictions = predict(hypothesis_out, test, learning_type) # Evaluate accuracy of test data if provided lables if labels: accuracy = evaluate(predictions, labels) print('Model accuracy on test data:',str(accuracy) + '%')
def based_on_model(game): columns = [ 'AwayTeamPythagoreanExpectedWin%', 'HomeTeamPythagoreanExpectedWin%', 'AwayTeamRating', 'HomeTeamRating', 'AwayTeamStartingPitcherRating', 'HomeTeamStartingPitcherRating', 'AwayTeamLine', 'HomeTeamLine' ] data = game[columns] modelPick = test.predict(list(data)) wasHomeTeamWinner = modelPick[0] pick = game['HomeTeam'] if modelPick[0] == 0 else game['AwayTeam'] confidence = 0.01 if not math.isnan(float(modelPick[1])) and not math.isnan( float(modelPick[1])): awayTeamModelLine = convert_win_prob_to_line(modelPick[1]) homeTeamModelLine = convert_win_prob_to_line(modelPick[2]) else: awayTeamModelLine = None homeTeamModelLine = None if awayTeamModelLine and homeTeamModelLine: isHomeTeamUnderdog = game['HomeTeamLine'] > game['AwayTeamLine'] underdogTeam = game['HomeTeam'] if isHomeTeamUnderdog else game[ 'AwayTeam'] underdogLine = game['HomeTeamLine'] if isHomeTeamUnderdog else game[ 'AwayTeamLine'] underdogModelLine = homeTeamModelLine if isHomeTeamUnderdog else awayTeamModelLine if has_underdog_advantage(underdogLine, underdogModelLine): isPickUnderdog = 1 if pick == underdogLine else 0 if not isPickUnderdog: pick = underdogTeam return pick, confidence
def recevie_msg(msg): if ":" in msg: mode, _msg = msg.split(":") if mode in ["cn2an", "an2cn"]: answer = f"{transform(_msg, mode)} 【by {mode}】" else: intent = predict(msg) answer = answer_dict[intent] else: intent = predict(msg) answer = answer_dict[intent] with open("./dialog.txt", "a") as f_dialog: f_dialog.write(msg + "\t" + answer + "\n") emit("response", {"msg": answer})
def upload(): if request.method == 'POST': f = request.files['file'] filename = secure_filename(f.filename) f.save(filename) result = predict(filename) filename = f.filename return jsonify(result=result, filename=filename)
def root(): # app_pipe = joblib.load('web-app/assets/dating_rf2.joblib') # df = pd.read_csv('web-app/dating_csv.csv') # df.drop('status',axis=1,inplace=True) # test_file = df[df.index==0] # Test Predict function from test.py file. result = predict() return str(result)
def upload_file(): if request.method == 'POST': f = request.files['file'] filename = secure_filename(f.filename) f.save(filename) result = predict(filename) filename = f.filename return render_template('index.html', result=result, filename=filename)
def index(sent): from bottle import response #try: output = predict (session, model, vocab, inv_label_vocab, max_sent_len, sent) response.content_type = 'application/json' ''' Check if this is a good or bad response. ''' return json.dumps(output)
def predict(self): list_of_keys = list(self.dict_of_data) list_of_keys.remove('Years') for x in range(0, len(list_of_keys)): print(str(x + 1) + ": " + str(list_of_keys[x])) first_pick = input("Pick the 'y' data set (use the numbers): ") first_pick = int(first_pick) - 1 print("You picked " + list_of_keys[first_pick]) x = list_of_keys[first_pick] array_x = self.smart_nan_filler(self.dict_of_data[x]) self.nan_checker(x, self.dict_of_data[x]) x = test.list_of_lists_to_dataframe([array_x], [x]) #x.index = self.dict_of_data['Years'] # Future grab difference in previous and add it to previous # x3 is Nan so x2-x1 = y # x3 = x2 + y # The Y data is just the years y = 'Years' #self.nan_checker(y, self.dict_of_data[y]) y = test.list_of_lists_to_dataframe([self.dict_of_data[y]], [y]) #y = y.fillna(y.mean()) y.index = self.dict_of_data['Years'] year = input("Which year would you like to predict?: ") year_int = float(year) a = -500 b = -500 if year_int > 2010 or year_int < 1950: a, b= self.better_prediction(x, year_int) test.predict(x,y, year_int,a,b)
def inference(): if request.method == 'POST': if request.files.get("image"): image = request.files["image"].read() image = Image.open(io.BytesIO(image)) if image.mode != 'RGB': image = image.convert('RGB') img_arr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) img_path = os.path.join(temp_dir, 'temp.jpg') cv2.imwrite(img_path, img_arr) pred = predict(img_path, weight_path) print(pred) results = {"results": pred} return jsonify(results)
def get_prob_list(path): cap = cv2.VideoCapture(path) ret = True l = [] ct = 0 while ret: ret, frame = cap.read() #if ct == 1000: # break ct += 1 if ret: frame = frame[25 : 45, 1534 : 1562] frame = cv2.flip(frame, 1) l.append(test.predict(frame).squeeze().tolist()) return l
def recevie_msg(msg): intent = predict(msg) answer_dict = { "greet": "你好~", "name": "我叫锅贴。", "age": "喵龄一岁不到~", "sex": "我是公的!", "parents": "Ailln 啊!", "feature": "学猫叫,喵~喵~喵~", "bye": "886" } answer = answer_dict[intent] with open("./dialog.txt", "a") as f_dialog: f_dialog.write(msg + "\t" + answer + "\n") emit("response", {"msg": answer})
def main(): if request.method == 'POST': file = request.files['file'] filename = secure_filename(file.filename) if not os.path.exists(DATADIR): os.system(f'mkdir -p {DATADIR}') fp, tempname = tempfile.mkstemp( prefix='image_', suffix='.png', dir=DATADIR) #'data/test/image_000.jpg' file.save(tempname) testfilename = f'{DATADIR}/%s' % os.path.basename(tempname) preds = predict(testfilename) return render_template('main.html', preds=preds, filename=os.path.basename(tempname)) return render_template('main.html')
def upload_file2(): if request.method == 'POST': f = request.files['file'] lat = request.form['latitude'] long = request.form['longitude'] date = request.form['date'] Date = datetime.strptime(date, '%d/%m/%Y') ext = f.filename.split(sep='.') f.save(secure_filename('img.' + ext[-1])) result = test.predict(float(lat), float(long), Date) #message = Markup('<a href="http://192.168.43.19/hackinfi/index.php/user_authentication>Click Here</a>') #flash(message) #return render_template('http://192.168.43.19/hackinfi/index.php/user_authentication') if result == 'legal': return '<h1>The image and the loction sent by tells the billboard to be ' + result + '</h1><button id="close">close</button><script>document.getElementById("close").onclick = function(){ window.close();}</script>' else: result = ', '.join(result) return '<h1>These billboards are illegal - ' + result + '</h1><button id="close">close</button><script>document.getElementById("close").onclick = function(){ window.close();}</script>'
def generate_fact(message): # generate the beginning of the text initial_text = choose_beginning(dataset) # generate the text text = ' '.join(predict(dataset, model, initial_text)) # normalize text text = re.sub(r'\s([:?.!,;)](?:\s|$))', r'\1', text) text = re.sub(r'((?:\s|$)[(])\s', r'\1', text) # add the keyboard like_keyboard = types.InlineKeyboardMarkup() like_button = types.InlineKeyboardButton(text=emojize(':thumbs_up:'), callback_data='like') dislike_button = types.InlineKeyboardButton(text=emojize(':thumbs_down:'), callback_data="dislike") like_keyboard.add(like_button, dislike_button) # send the message with the fact bot.send_message(message.from_user.id, text=text, reply_markup=like_keyboard)
def train_model(remove_features, label_names, train_path, val_path, batch_size, seq_len, num_epoch, ctx, load_epoch, name): import os from test import predict, score, make_submission data_names = [ i[:-4] for i in os.listdir(train_path) if i.endswith('.npy') and not (i[:-4] in remove_features) ] train_iter = SeqDataIter(train_path, batch_size, data_names=data_names, label_names=label_names, shuffle=True, usampling=True, seq_len=seq_len) val_iter = SeqDataIter(val_path, batch_size, data_names=data_names, label_names=label_names, shuffle=False, max_len=batch_size * 1000, seq_len=seq_len) sym = make_network(train_iter, seq_len) sym = add_loss(sym) model = train(sym, train_iter, val_iter, name=name, load_epoch=load_epoch, batch_size=batch_size, exp_dir=exp_dir) test_iter = DataIter(test_path, batch_size, data_names=data_names, label_names=[], shuffle=False) score(val_iter, 7, name) prediction = predict(test_iter, 7, name) make_submission(prediction, name)
def visualize(model, pe, img, max_len=15): model.eval() resize = transforms.Resize(opt["img_size"]) norm = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) ]) img_resize = resize(img) img_for_pred = norm(img_resize).unsqueeze(dim=0) attns = [] start = time.time() text = predict(model, pe, img_for_pred, max_len, attns=attns)[0] end = time.time() text = text[:text.find('[s]')] print(f"time consumed:{time_interval(end - start)}") for i, char in enumerate(text): if torch.cuda.is_available(): attn = attns[i].cpu() attn = attn.detach().numpy() show(img_resize, char, attn) show(img_resize, text)
def test_model(model, candidates_dir, kp_names=("head", "ass"), box_score_thre=0.5, kp_score_thre=0, mask_thre=0.5, save_image_out=True, show_image_out=False): out_dir = candidates_dir.rstrip("/") + ".detected" os.makedirs(out_dir, exist_ok=True) device = torch.device( "cuda") if torch.cuda.is_available() else torch.device("cpu") model.to(device) model.eval() transforms = get_transforms(train=False) for p in tqdm(os.listdir(candidates_dir)): image_path = os.path.join(candidates_dir, p) image_cv2 = cv2.imread(image_path) canvas, results = predict(image_cv2, model, transforms=transforms, device=device, kp_names=kp_names, box_score_thre=box_score_thre, kp_score_thre=kp_score_thre, mask_thre=mask_thre, show=False) if save_image_out: cv2.imwrite(os.path.join(out_dir, p), canvas) if show_image_out: cv2.imshow("res", canvas) cv2.waitKey(0) cv2.destroyWindow("res")
def main(): im_id, im_label = get_raw(FLAGS.path_to_csv) final_images, temp_label, images_aert = image_labels(im_id, im_label) final_label = final_labels(temp_label) tf.reset_default_graph() model = img_Model(final_images, final_label, FLAGS) if FLAGS.test: print("testing") asw = predict(final_label, final_images, FLAGS, images_aert) print("-" * 50) return if FLAGS.real_time_pre: ase = predict_predict(final_label=None, final_images=None, FLAGS=None) reutrn with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) if FLAGS.load_train: a = 5 else: model.optimize(sess)
from torch.utils.data import DataLoader, Dataset from dataset import Image_Dataset from sklearn.decomposition import KernelPCA from sklearn.manifold import TSNE from sklearn.cluster import MiniBatchKMeans, KMeans from preprocess import preprocess import torchvision.transforms as transforms from test import inference, predict, save_prediction, invert #load model model = Improved_AE().cuda() model.load_state_dict(torch.load(sys.argv[2])) model.eval() # 準備 data trainX = np.load(sys.argv[1]) # 預測答案 latents = inference(X=trainX, model=model) pred, X_embedded = predict(latents) # 將預測結果存檔,上傳 kaggle if pred[6] == 1: save_prediction(pred, sys.argv[3]) # 由於是 unsupervised 的二分類問題,我們只在乎有沒有成功將圖片分成兩群 # 如果上面的檔案上傳 kaggle 後正確率不足 0.5,只要將 label 反過來就行了 else: save_prediction(invert(pred), sys.argv[3])
for e, d in zip(encoder.parameters(), decoder.parameters()): e.requires_grad = True d.requires_grad = True encoder.train() decoder.train() cost = trainBatch(encoder, decoder, criterion, encoder_optimizer, decoder_optimizer) loss_avg.add(cost) i += 1 if i % opt.displayInterval == 0: print( '[%d/%d][%d/%d] Loss: %f' % (epoch, opt.epochs, i, len(train_loader), loss_avg.val())) loss_avg.reset() # validate if epoch % opt.valInterval == 0: print("Validation:") predict(encoder, decoder, criterion, opt.batchSize, dataset=val_dataset) # save model if epoch % opt.saveInterval == 0: torch.save(encoder.state_dict(), '{0}/encoder_{1}.pth'.format(opt.savePath, epoch)) torch.save(decoder.state_dict(), '{0}/decoder_{1}.pth'.format(opt.savePath, epoch))
imgA = [] for img in glob.glob("images/yeet.jpg"): imgA.append(img) path_file = os.path.basename(img).replace(".jpg", "") imageA.append(path_file) Path("cropped_faces/" + path_file).mkdir(parents=True, exist_ok=True) countImg = ef.crop_faces(img) count = 0 for x in range(len(imageA)): Path("Data4/" + imageA[x]).mkdir(parents=True, exist_ok=True) path_to = "Data4/" + imageA[x] for img_a in glob.glob("cropped_faces/" + imageA[x] + "/*.jpg"): count = ef.data(imgA[x], img_a, count) valance = ef.predict(path_to) valance = (valance * 33.5) + 33.5 # color = pf.extract_colors(imgA[x])*10 # print(color) # valance = valance + color if valance > 50: positive += 1 else: negative += 1 dict[imgA[x]] = valance sentiment += valance print(valance) print(dict) print(positive) print(negative)
def list_to_string(s): # initialize an empty string str1 = "\n" # return string return str1.join(s) DATASET = PROJECT_DIR + "data/AllNameList.csv" # this is the preshuffled dataset WEIGHTS = PROJECT_DIR + "weights/nb/naive_bayes_weights_jup" # split the dataset into 70% train, 0% val, 30% test TRAINSET, VALSET, TESTSET = split_dataset(0.80, 0.001) speaker_list = open("../speaker_list.txt", "r").read().splitlines() predicted_genders = predict(names=speaker_list) print(predicted_genders) # split speakers into files by gender male_speaker_list = [] female_speaker_list = [] for i in range(len(predicted_genders)): if predicted_genders[i] == 'm': male_speaker_list.append(speaker_list[i]) else: female_speaker_list.append(speaker_list[i]) female_speakers_file = open("../female_speakers.txt", "w").write(list_to_string(female_speaker_list)) male_speakers_file = open("../male_speakers.txt", "w").write(list_to_string(male_speaker_list))
def respond(): username = request.args.get('username') try: connection = mysql.connector.connect(host='localhost', database='emotions', user='******', password='') sql_select_Query = "select name from image where id_str=%s" id_str = (username, ) cursor = connection.cursor() cursor.execute(sql_select_Query, id_str) record = cursor.fetchall() for row in record: print() except Error as e: print("Error reading data from MySQL table", e) finally: if (connection.is_connected()): connection.close() cursor.close() print("MySQL connection is closed") sentiment = 0 count = 0 countImg = 0 valance = 0 positive = 0 negative = 0 dict = {} error = "" for img in glob.glob("images/*.jpg"): path_file = os.path.basename(img).replace(".jpg", "") Path("cropped_faces/" + path_file).mkdir(parents=True, exist_ok=True) countImg = ef.crop_faces(img, countImg) print(countImg) count = 0 for img_a in glob.glob("cropped_faces/" + path_file + "/*.jpg"): Path("Data4/" + path_file).mkdir(parents=True, exist_ok=True) path_to = "Data4/" + path_file count = ef.data(img, img_a, count) valance = ef.predict(path_to) valance += pf.extract_colors(img) if valance > 40: positive += 1 else: negative += 1 dict[os.path.basename(img)] = valance sentiment += valance print(valance) print(dict) print(positive) print(negative) sentiment = sentiment / (positive + negative) # some JSON: a = { 'sentiment': sentiment, 'message': error, 'positive': positive, 'negative': negative, 'dict': dict } x = json.dumps(a) # parse x: y = json.loads(x) return x
def main(argv): print '\nSYSTEM START' print '\nMODE: Training' ################### # PREPROCESS DATA # ################### """ Load initial embedding file """ vocab_word = Vocab() emb = None if argv.init_emb: print '\n\tInitial Embedding Loading...' emb, vocab_word = load_init_emb(init_emb=argv.init_emb) print '\t\tVocabulary Size: %d' % vocab_word.size() """ Load corpora """ print '\n\tLoading Corpora...' tr_corpus, tr_doc_names, vocab_word = load_conll(path=argv.train_data, vocab=vocab_word, data_size=argv.data_size) dev_corpus, dev_doc_names, _ = load_conll(path=argv.dev_data, vocab=vocab_word, data_size=argv.data_size) print '\t\tTrain Documents: %d' % len(tr_corpus) print '\t\tDev Documents: %d' % len(dev_corpus) """ Extract gold mentions CoNLL-2012: Train=155,560, Dev=19,156, Test=19,764 """ # gold_mentions: 1D: n_doc, 2D: n_sents, 3D: n_mentions: elem=(bos, eos) # gold_corefs: 1D: n_doc, 2D: n_sents, 3D: n_mentions: elem=coref_id print '\n\tExtracting Gold Mentions...' print '\t\tTRAIN', tr_gold_ments = get_gold_mentions(tr_corpus, check=argv.check) print '\t\tDEV ', dev_gold_ments = get_gold_mentions(dev_corpus) """ Extract cand mentions """ # cand_mentions: 1D: n_doc, 2D: n_sents, 3D: n_mentions; elem=(bos, eos) print '\n\tExtracting Cand Mentions...' print '\t\tTRAIN', tr_cand_ments = get_cand_mentions(tr_corpus, check=argv.check) print '\t\tDEV ', dev_cand_ments = get_cand_mentions(dev_corpus) """ Convert words into IDs """ print '\n\tConverting Words into IDs...' print '\t\tVocabulary Size: %d' % vocab_word.size() tr_word_ids = convert_words_into_ids(corpus=tr_corpus, vocab_word=vocab_word) dev_word_ids = convert_words_into_ids(corpus=dev_corpus, vocab_word=vocab_word) """ Set word ids for mentions """ tr_gold_ments = set_word_id_for_ment(tr_word_ids, tr_gold_ments) tr_cand_ments = set_word_id_for_ment(tr_word_ids, tr_cand_ments) dev_gold_ments = set_word_id_for_ment(dev_word_ids, dev_gold_ments) dev_cand_ments = set_word_id_for_ment(dev_word_ids, dev_cand_ments) """ Set coref ids for cand mentions """ tr_cand_ments = set_cand_ment_coref(tr_gold_ments, tr_cand_ments) dev_cand_ments = set_cand_ment_coref(dev_gold_ments, dev_cand_ments) """ Check the coverage: Coverage 95.0%, Rate 1:3.5 by Berkeley System """ print '\n\tChecking the Coverage of the Candidate Mentions...' check_coverage_of_cand_mentions(tr_gold_ments, tr_cand_ments) check_coverage_of_cand_mentions(dev_gold_ments, dev_cand_ments) """ Extract features """ print '\n\tExtracting features...' """ phi = (span, word, ctx, dist, label, position) span : 1D: n_doc, 2D: n_ments, 3D: n_cand_ants, 4D: limit * 2; elem=word id word : 1D: n_doc, 2D: n_ments, 3D: n_cand_ants, 4D: [m_first, m_last, a_first, a_last]; elem=word id ctx : 1D: n_doc, 2D: n_ments, 3D: n_cand_ants, 4D: window * 2 * 2; elem=word id dist : 1D: n_doc, 2D: n_ments, 3D: n_cand_ants; elem=sent dist label : 1D: n_doc, 2D: n_ments; elem=0/1 position: 1D: n_doc, 2D: n_ments, 3D: n_cand_ants; elem=(sent_m_i, span_m, sent_a_i, span_a) """ tr_phi, tr_posit = get_features(tr_cand_ments, False, argv.n_cands) dev_phi, dev_posit = get_features(dev_cand_ments, True, argv.n_cands) """ Count the number of features """ n_tr_phi_total = reduce(lambda a, b: a + reduce(lambda c, d: c + len(d), b, 0), tr_phi, 0) n_tr_phi_t = reduce(lambda a, b: a + reduce(lambda c, d: c + reduce(lambda e, f: e + np.sum(f[-1]), d, 0), b, 0), tr_phi, 0) n_tr_phi_f = n_tr_phi_total - n_tr_phi_t n_dev_phi_total = reduce(lambda a, b: a + reduce(lambda c, d: c + len(d), b, 0), dev_phi, 0) n_dev_phi_t = reduce(lambda a, b: a + reduce(lambda c, d: c + reduce(lambda e, f: e + np.sum(f[-1]), d, 0), b, 0), dev_phi, 0) n_dev_phi_f = n_dev_phi_total - n_dev_phi_t print '\t\tTrain Features Total: %d\tRate: P:N\t%d:%d' % (n_tr_phi_total, n_tr_phi_t, n_tr_phi_f) print '\t\tDev Features Total: %d\tRate: P:N\t%d:%d' % (n_dev_phi_total, n_dev_phi_t, n_dev_phi_f) """ Convert into the Theano format """ print '\n\tConverting features into the Theano Format...' """ samples = (span, word, ctx, dist, label) span : 1D: n_doc * n_ments * n_cand_ants, 2D: limit * 2; elem=word id word : 1D: n_doc * n_ments * n_cand_ants, 2D: [m_first, m_last, a_first, a_last]; elem=word id ctx : 1D: n_doc * n_ments * n_cand_ants, 2D: window * 2 * 2; elem=word id dist : 1D: n_doc * n_ments * n_cand_ants; elem=sent dist label : 1D: n_doc * n_ments * n_cand_ants; elem=0/1 indices: 1D: n_doc * n_ments; elem=(bos, eos) """ tr_samples, tr_indices = theano_format(tr_phi) dev_samples, dev_indices = theano_format(dev_phi) ###################### # BUILD ACTUAL MODEL # ###################### print '\nBuilding the model...' model = set_model(argv, vocab_word, emb) bos = T.iscalar('bos') eos = T.iscalar('eos') train_f = theano.function( inputs=[bos, eos], outputs=[model.nll, model.correct, model.correct_t, model.correct_f, model.total_p, model.total_r], updates=model.updates, givens={ model.x_span: tr_samples[0][bos: eos], model.x_word: tr_samples[1][bos: eos], model.x_ctx : tr_samples[2][bos: eos], model.x_dist: tr_samples[3][bos: eos], model.x_slen: tr_samples[4][bos: eos], model.y : tr_samples[5][bos: eos] }, mode='FAST_RUN' ) dev_f = theano.function( inputs=[bos, eos], outputs=[model.y_hat_index, model.p_y_hat, model.correct, model.correct_t, model.correct_f, model.total_p, model.total_r], givens={ model.x_span: dev_samples[0][bos: eos], model.x_word: dev_samples[1][bos: eos], model.x_ctx : dev_samples[2][bos: eos], model.x_dist: dev_samples[3][bos: eos], model.x_slen: dev_samples[4][bos: eos], model.y : dev_samples[5][bos: eos] }, mode='FAST_RUN' ) ############### # TRAIN MODEL # ############### batch_size = argv.batch n_batches = n_tr_phi_total / batch_size indices = range(n_batches) print 'Training START\n' print 'Mini-Batch Samples: %d\n' % n_batches for epoch in xrange(argv.epoch): random.shuffle(indices) print '\nEpoch: %d' % (epoch + 1) print 'TRAIN' print '\tIndex: ', start = time.time() total_loss = 0. correct = np.zeros(9, dtype='float32') correct_t = np.zeros(9, dtype='float32') correct_f = np.zeros(9, dtype='float32') total = 0. total_r = np.zeros(9, dtype='float32') total_p = np.zeros(9, dtype='float32') for i, index in enumerate(indices): if i % 1000 == 0 and i != 0: print '%d' % i, sys.stdout.flush() loss, crr, crr_t, crr_f, ttl_p, ttl_r = train_f(index * batch_size, (index+1) * batch_size) assert not math.isnan(loss), 'Index: %d Batch Index: %d' % (i, index) total_loss += loss correct += crr correct_t += crr_t correct_f += crr_f total += batch_size total_p += ttl_p total_r += ttl_r end = time.time() print '\n\tTime: %f seconds' % (end - start) show_results(total, total_p, total_r, correct, correct_t, correct_f, total_loss) predict(epoch, dev_f, dev_corpus, dev_doc_names, dev_indices, dev_posit)
brush = pygame.image.load("brush.png") brush = pygame.transform.scale(brush, (18, 18)) pygame.display.update() clock = pygame.time.Clock() z = 0 while (1): clock.tick(60) x, y = pygame.mouse.get_pos() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == MOUSEBUTTONDOWN: z = 1 elif event.type == MOUSEBUTTONUP: z = 0 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_c: screen.fill((255, 255, 255)) pygame.display.update() elif event.key == pygame.K_t: pygame.image.save(screen, "image.png") img = image_to_mnist.convert("image.png") test.predict(img) if z == 1: screen.blit(brush, (x - 2, y - 2)) pygame.display.update()
json.dumps(config_train["snapshot_interval"]) + "\n") config_train["out_file"].write(json.dumps(config_train["hash_bit"]) + "\n") train.train(config_train) # ------ TEST ------ args_test = test.Arguments(snapshot=(args_train.loss_type, args_train.prefix, 'iter_01500'), portion_oo_10=5, batch_size=16, crop10=False, resize_size=args_train.resize_size, crop_size=args_train.crop_size, hash_bit=args_train.hash_bit) config_test = test.produce_config(args_test) code_and_label = test.predict(config_test) mAP = test.mean_average_precision(code_and_label, config_test["R"]) print(config_test["snapshot_path"]) print(args_test.snapshot[0] + ": " + args_test.snapshot[1] + " | MAP: " + str(mAP)) print("saving ...") test.save_code_and_label( code_and_label, join(config_test["output_path"], args_test.snapshot[0])) config_test["out_file"].write(args_test.snapshot[0] + ": " + args_test.snapshot[1] + " | MAP: " + str(mAP)) print("saving done")
with codecs.open(test_file, 'r', 'utf-8') as r: alllines = r.readlines() for oneline in alllines: imgname, label = oneline.split(',') label = label.strip() #print(imgname) #Actually, it has been resized to 32 * 350 im = cv2.imread(imgname, 0) im = preprocess2(im, 2) im = im.astype(np.float32) im = ((im / 255.0) - 0.5) * 2 im = im.reshape((32, 350, 1)) X = np.array([im]) out = predict(X, basemodel) out = ''.join([unicode(a) for a in out]) #print(out) #print(label) if debug: cv2.imshow("src", img) cv2.waitKey(0) #print(type(label)) #print(type(out)) #exit() right_ratio = 1 - Levenshtein.distance(out, label) / (len(label) + 0.0) if right_ratio > 0.99: